blob: aca29ec3c6b99cf92ac9f3154a6c62bea4bb4985 [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 Lundbladedd6e76e2021-03-10 01:54:01 -07003 Copyright (c) 2018-2021, 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++) {
76 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladeee851742020-01-08 08:37:05 -080077 printf("Position: %d Actual: 0x%x Expected: 0x%x\n",
78 (uint32_t)i,
79 ((uint8_t *)U1.ptr)[i],
80 ((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 Lundblade3df8c7e2018-11-02 13:12:41 +0700284 0x98, 0x22, 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,
299 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
300 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
301 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
302 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
303 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
304 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
305 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
306 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
307 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
308 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
309 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
310 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
311 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
312 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
313 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
314 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
315 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
316 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
317 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
318 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700319 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
320 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530321 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
322 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
323 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
324 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
325 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
326 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
327 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
328 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
329 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
330 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
331 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
332 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
333 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
334 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
335 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
336 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
337 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
338 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
339 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
340 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
341 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
342 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
343 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
344 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
345 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
346 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
347 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
348 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
349 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
350 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
351 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
352 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
353 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
354 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
355 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
356 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
357 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
358 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
359 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
360 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
361 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
362 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
363 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
364 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
365 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
366 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
367 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
368 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
369 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
370 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
371 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
372 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
373 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
374 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
375 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
376 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
377 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
378 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700379 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530380 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
381 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
382 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
383 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
384 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
385 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
386 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
387 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
388 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
389 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
390 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
391 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
392 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
393 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
394 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
395 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
396 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
397 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
398 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
399 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
400 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
401 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
402 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
403 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
404 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
405 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
406 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
407 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
408 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
409 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
410 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
411 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
412 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
413 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700414 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530415 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
416 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
417 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
418 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
419 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
420 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
421 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
422 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
423 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
424 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
425 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
426 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
427 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
428 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
429 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
430 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
431 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
432 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
433 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
434 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
435 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
436 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
437 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
438 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
439 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
440 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
441 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
442 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
443 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
444 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
445 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
446 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
447 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
448 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
449 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
450 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
451 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
452 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
453 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
454 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
455 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
456 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
457 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
458 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
459 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
460 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
461 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
462 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
463 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
464 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
465 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
466 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
467 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
468 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
469 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
470 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
471 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
472 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
473 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
474 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
475 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
476 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
477 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
478 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
479 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
480 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
481 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
484 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
487 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
488 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490};
491
492
493static const char *szMIME = "\
494MIME-Version: 1.0\n\
495Content-Type: multipart/mixed;\n\
496boundary=\"XXXXboundary text\"\n\
497\n\
498This is a multipart message in MIME format.\n\
499\n\
500--XXXXboundary text\n\
501Content-Type: text/plain\n\
502\n\
503this is the body text\n\
504\n\
505--XXXXboundary text\n\
506Content-Type: text/plain;\n\
507Content-Disposition: attachment;\n\
508filename=\"test.txt\"\n\
509\n\
510this is the attachment text\n\
511\n\
512--XXXXboundary text--";
513
514
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700515static void AddAll(QCBOREncodeContext *pECtx)
516{
517 QCBOREncode_OpenArray(pECtx);
518
519 /* Some ints that are tagged and have strings preceeding them
520 * (not labels becase it is not a map) */
521 QCBOREncode_AddSZString(pECtx, "UINT62");
522 QCBOREncode_AddTag(pECtx, 100);
523 QCBOREncode_AddUInt64(pECtx, 89989909);
524 QCBOREncode_AddSZString(pECtx, "INT64");
525 QCBOREncode_AddTag(pECtx, 76);
526 QCBOREncode_AddInt64(pECtx, 77689989909);
527 QCBOREncode_AddUInt64(pECtx,0);
528 QCBOREncode_AddInt64(pECtx, -44);
529
530 /* ints that go in maps */
531 QCBOREncode_OpenMap(pECtx);
532 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
533 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
534 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
535 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
536 QCBOREncode_CloseMap(pECtx);
537
538 /* Epoch Date */
539 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
540
541 /* Epoch date with labels */
542 QCBOREncode_OpenMap(pECtx);
543 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
544 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
545 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
546 QCBOREncode_CloseMap(pECtx);
547
548 /* Binary blobs */
549 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
550
551 /* binary blobs in maps */
552 QCBOREncode_OpenMap(pECtx);
553 QCBOREncode_AddSZString(pECtx, "binbin");
554 QCBOREncode_AddTag(pECtx, 100000);
555 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
556 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
557 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
558 QCBOREncode_CloseMap(pECtx);
559
560 /* text blobs */
561 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
562 QCBOREncode_AddSZString(pECtx, "oof\n");
563 const char *szURL =
564 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
565 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
566 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
567 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
568 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
569
570 /* text blobs in maps */
571 QCBOREncode_OpenMap(pECtx);
572 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
573 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
574 QCBOREncode_AddSZString(pECtx, "()()()");
575 QCBOREncode_AddTag(pECtx, 1000);
576 QCBOREncode_AddSZString(pECtx, "rab rab oof");
577 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
578 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
579 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
580 QCBOREncode_AddURIToMap(pECtx,
581 "RFC",
582 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
583 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
584 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
585 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
586 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
587 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
588 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
589 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
590 QCBOREncode_CloseMap(pECtx);
591
592 /* Date strings */
593 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
594 QCBOREncode_OpenMap(pECtx);
595 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
596 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
597 QCBOREncode_CloseMap(pECtx);
598
599 /* true / false ... */
600 QCBOREncode_AddSimple(pECtx, CBOR_SIMPLEV_UNDEF);
601 QCBOREncode_OpenMap(pECtx);
602 QCBOREncode_AddSZString(pECtx, "dare");
603 QCBOREncode_AddTag(pECtx, 66);
604 QCBOREncode_AddBool(pECtx, true);
605 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
606 QCBOREncode_AddSimpleToMapN(pECtx, 737634, CBOR_SIMPLEV_NULL);
607 QCBOREncode_CloseMap(pECtx);
608
609 /* opening an array */
610 QCBOREncode_OpenArray(pECtx);
611 QCBOREncode_CloseArray(pECtx);
612
613 /* opening arrays in a map */
614 QCBOREncode_OpenMap(pECtx);
615 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
616 QCBOREncode_AddTag(pECtx, 1093);
617 QCBOREncode_OpenArray(pECtx);
618 QCBOREncode_CloseArray(pECtx);
619 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
620 QCBOREncode_CloseArray(pECtx);
621 QCBOREncode_OpenArrayInMapN(pECtx, 42);
622 QCBOREncode_CloseArray(pECtx);
623 QCBOREncode_CloseMap(pECtx);
624
625 /* opening maps with labels and tagging */
626 QCBOREncode_OpenMap(pECtx);
627 QCBOREncode_OpenMapInMap(pECtx, "in a map");
628 QCBOREncode_OpenMapInMapN(pECtx, 5556);
629 QCBOREncode_AddSZString(pECtx, "in a in a in a");
630 QCBOREncode_AddTag(pECtx, 9087);
631 QCBOREncode_OpenMap(pECtx);
632 QCBOREncode_CloseMap(pECtx);
633 QCBOREncode_CloseMap(pECtx);
634 QCBOREncode_CloseMap(pECtx);
635 QCBOREncode_CloseMap(pECtx);
636
637 /* Extended simple values (these are not standard...) */
638 QCBOREncode_OpenMap(pECtx);
639 QCBOREncode_AddSZString(pECtx, "s1");
640 QCBOREncode_AddTag(pECtx, 88);
641 QCBOREncode_AddSimple(pECtx, 255);
642 QCBOREncode_AddSimpleToMap(pECtx, "s2", 0);
643 QCBOREncode_AddSZString(pECtx, "s3");
644 QCBOREncode_AddTag(pECtx, 88);
645 QCBOREncode_AddSimple(pECtx, 33);
646 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
647 QCBOREncode_AddTag(pECtx, 88);
648 QCBOREncode_AddSimple(pECtx, 255);
649 QCBOREncode_AddInt64(pECtx, 89); // label before tag
650 QCBOREncode_AddTag(pECtx, 88);
651 QCBOREncode_AddSimple(pECtx, 19);
652 QCBOREncode_CloseMap(pECtx);
653
654 /* UUIDs */
655 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
656 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
657 0x49, 0x43, 0x41, 0x32};
658 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
659 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
660 QCBOREncode_OpenMap(pECtx);
661 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
662 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
663 QCBOREncode_CloseMap(pECtx);
664
665 /* Bool */
666 QCBOREncode_AddBool(pECtx, true);
667 QCBOREncode_AddBool(pECtx, false);
668 QCBOREncode_OpenMap(pECtx);
669 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
670 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
671 QCBOREncode_CloseMap(pECtx);
672
673 /* Big numbers */
674 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
675 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
676 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
677 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
678 QCBOREncode_OpenMap(pECtx);
679 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
680 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
681 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
682 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
683 QCBOREncode_CloseMap(pECtx);
684
685 QCBOREncode_CloseArray(pECtx);
686}
687
688
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300689int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700691 /* Improvement: this test should be broken down into several so it is more
692 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 QCBOREncodeContext ECtx;
694 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800695
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530696 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800697
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700698 AddAll (&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800699
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530700 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700701 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800702 nReturn = -1;
703 goto Done;
704 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800705
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700706 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530707 nReturn = -2;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700708 }
709
710
711 /* Also test size calculation */
712 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
713
714 AddAll (&ECtx);
715
716 size_t size;
717 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
718 nReturn = -10;
719 goto Done;
720 }
721
722 if(size != sizeof(spExpectedEncodedAll)) {
723 nReturn = -11;
724 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800725
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800726Done:
727 return nReturn;
728}
729
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700730
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530731/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200732 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530733 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
734 3B 0000000100000000 # negative(4294967296)
735 3A FFFFFFFF # negative(4294967295)
736 3A FFFFFFFE # negative(4294967294)
737 3A FFFFFFFD # negative(4294967293)
738 3A 7FFFFFFF # negative(2147483647)
739 3A 7FFFFFFE # negative(2147483646)
740 3A 00010001 # negative(65537)
741 3A 00010000 # negative(65536)
742 39 FFFF # negative(65535)
743 39 FFFE # negative(65534)
744 39 FFFD # negative(65533)
745 39 0100 # negative(256)
746 38 FF # negative(255)
747 38 FE # negative(254)
748 38 FD # negative(253)
749 38 18 # negative(24)
750 37 # negative(23)
751 36 # negative(22)
752 20 # negative(0)
753 00 # unsigned(0)
754 00 # unsigned(0)
755 01 # unsigned(1)
756 16 # unsigned(22)
757 17 # unsigned(23)
758 18 18 # unsigned(24)
759 18 19 # unsigned(25)
760 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200761 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530762 18 FE # unsigned(254)
763 18 FF # unsigned(255)
764 19 0100 # unsigned(256)
765 19 0101 # unsigned(257)
766 19 FFFE # unsigned(65534)
767 19 FFFF # unsigned(65535)
768 1A 00010000 # unsigned(65536)
769 1A 00010001 # unsigned(65537)
770 1A 00010002 # unsigned(65538)
771 1A 7FFFFFFF # unsigned(2147483647)
772 1A 7FFFFFFF # unsigned(2147483647)
773 1A 80000000 # unsigned(2147483648)
774 1A 80000001 # unsigned(2147483649)
775 1A FFFFFFFE # unsigned(4294967294)
776 1A FFFFFFFF # unsigned(4294967295)
777 1B 0000000100000000 # unsigned(4294967296)
778 1B 0000000100000001 # unsigned(4294967297)
779 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
780 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
781 */
782static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200783 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
785 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
786 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
787 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
788 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
789 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
790 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
791 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
792 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
793 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200794 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
795 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
796 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
797 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
798 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
799 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
800 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
801 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
802 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
803 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
804 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
805 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
806 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800807
808/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800809
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800810 Test the generation of integers. This also ends up testing
811 encoding of all the different lengths. It encodes integers
812 of many lengths and values, especially around the boundaries
813 for different types of integers. It compares the output
814 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800816 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300817int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818{
819 QCBOREncodeContext ECtx;
820 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800821
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530822 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800823 QCBOREncode_OpenArray(&ECtx);
824
825 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
826 QCBOREncode_AddInt64(&ECtx, -4294967297);
827 QCBOREncode_AddInt64(&ECtx, -4294967296);
828 QCBOREncode_AddInt64(&ECtx, -4294967295);
829 QCBOREncode_AddInt64(&ECtx, -4294967294);
830 QCBOREncode_AddInt64(&ECtx, -2147483648);
831 QCBOREncode_AddInt64(&ECtx, -2147483647);
832 QCBOREncode_AddInt64(&ECtx, -65538);
833 QCBOREncode_AddInt64(&ECtx, -65537);
834 QCBOREncode_AddInt64(&ECtx, -65536);
835 QCBOREncode_AddInt64(&ECtx, -65535);
836 QCBOREncode_AddInt64(&ECtx, -65534);
837 QCBOREncode_AddInt64(&ECtx, -257);
838 QCBOREncode_AddInt64(&ECtx, -256);
839 QCBOREncode_AddInt64(&ECtx, -255);
840 QCBOREncode_AddInt64(&ECtx, -254);
841 QCBOREncode_AddInt64(&ECtx, -25);
842 QCBOREncode_AddInt64(&ECtx, -24);
843 QCBOREncode_AddInt64(&ECtx, -23);
844 QCBOREncode_AddInt64(&ECtx, -1);
845 QCBOREncode_AddInt64(&ECtx, 0);
846 QCBOREncode_AddUInt64(&ECtx, 0ULL);
847 QCBOREncode_AddInt64(&ECtx, 1);
848 QCBOREncode_AddInt64(&ECtx, 22);
849 QCBOREncode_AddInt64(&ECtx, 23);
850 QCBOREncode_AddInt64(&ECtx, 24);
851 QCBOREncode_AddInt64(&ECtx, 25);
852 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200853 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800854 QCBOREncode_AddInt64(&ECtx, 254);
855 QCBOREncode_AddInt64(&ECtx, 255);
856 QCBOREncode_AddInt64(&ECtx, 256);
857 QCBOREncode_AddInt64(&ECtx, 257);
858 QCBOREncode_AddInt64(&ECtx, 65534);
859 QCBOREncode_AddInt64(&ECtx, 65535);
860 QCBOREncode_AddInt64(&ECtx, 65536);
861 QCBOREncode_AddInt64(&ECtx, 65537);
862 QCBOREncode_AddInt64(&ECtx, 65538);
863 QCBOREncode_AddInt64(&ECtx, 2147483647);
864 QCBOREncode_AddInt64(&ECtx, 2147483647);
865 QCBOREncode_AddInt64(&ECtx, 2147483648);
866 QCBOREncode_AddInt64(&ECtx, 2147483649);
867 QCBOREncode_AddInt64(&ECtx, 4294967294);
868 QCBOREncode_AddInt64(&ECtx, 4294967295);
869 QCBOREncode_AddInt64(&ECtx, 4294967296);
870 QCBOREncode_AddInt64(&ECtx, 4294967297);
871 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
872 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800873
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800875
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530876 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700877 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 nReturn = -1;
879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530881 if(CheckResults(Enc, spExpectedEncodedInts))
882 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800883
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800884 return(nReturn);
885}
886
887
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530888/*
889 85 # array(5)
890 F5 # primitive(21)
891 F4 # primitive(20)
892 F6 # primitive(22)
893 F7 # primitive(23)
894 A1 # map(1)
895 65 # text(5)
896 554E446566 # "UNDef"
897 F7 # primitive(23)
898 */
899static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800900 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
901
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300902int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903{
904 QCBOREncodeContext ECtx;
905 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530907 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900909
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
911 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
912 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
913 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800914
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800915 QCBOREncode_OpenMap(&ECtx);
916
917 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
918 QCBOREncode_CloseMap(&ECtx);
919
920 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800921
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530922 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700923 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 nReturn = -1;
925 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800926
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530927 if(CheckResults(ECBOR, spExpectedEncodedSimple))
928 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800929
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930 return(nReturn);
931}
932
Jan Jongboom47d86c52019-07-25 08:54:16 +0200933/*
934 9F # array(5)
935 F5 # primitive(21)
936 F4 # primitive(20)
937 F6 # primitive(22)
938 F7 # primitive(23)
939 BF # map(1)
940 65 # text(5)
941 554E446566 # "UNDef"
942 F7 # primitive(23)
943 FF # break
944 FF # break
945 */
946static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
947 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
948
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300949int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +0200950{
951 QCBOREncodeContext ECtx;
952 int nReturn = 0;
953
954 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
955 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
956
957 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
958 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
959 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
960 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
961
962 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
963
964 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
965 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
966
967 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
968
969 UsefulBufC ECBOR;
970 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
971 nReturn = -1;
972 }
973
974 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
975 return -2;
976
977 return(nReturn);
978}
979
Jan Jongboom5d827882019-08-07 12:51:15 +0200980/*
981A5 # map(5)
982 63 # text(3)
983 617272 # "arr"
984 98 1F # array(31)
985 00 # unsigned(0)
986 01 # unsigned(1)
987 02 # unsigned(2)
988 03 # unsigned(3)
989 04 # unsigned(4)
990 05 # unsigned(5)
991 06 # unsigned(6)
992 07 # unsigned(7)
993 08 # unsigned(8)
994 09 # unsigned(9)
995 0A # unsigned(10)
996 0B # unsigned(11)
997 0C # unsigned(12)
998 0D # unsigned(13)
999 0E # unsigned(14)
1000 0F # unsigned(15)
1001 10 # unsigned(16)
1002 11 # unsigned(17)
1003 12 # unsigned(18)
1004 13 # unsigned(19)
1005 14 # unsigned(20)
1006 15 # unsigned(21)
1007 16 # unsigned(22)
1008 17 # unsigned(23)
1009 18 18 # unsigned(24)
1010 18 19 # unsigned(25)
1011 18 1A # unsigned(26)
1012 18 1B # unsigned(27)
1013 18 1C # unsigned(28)
1014 18 1D # unsigned(29)
1015 18 1E # unsigned(30)
1016 63 # text(3)
1017 6D6170 # "map"
1018 B8 1F # map(31)
1019 61 # text(1)
1020 61 # "a"
1021 00 # unsigned(0)
1022 61 # text(1)
1023 62 # "b"
1024 01 # unsigned(1)
1025 61 # text(1)
1026 63 # "c"
1027 02 # unsigned(2)
1028 61 # text(1)
1029 64 # "d"
1030 03 # unsigned(3)
1031 61 # text(1)
1032 65 # "e"
1033 04 # unsigned(4)
1034 61 # text(1)
1035 66 # "f"
1036 05 # unsigned(5)
1037 61 # text(1)
1038 67 # "g"
1039 06 # unsigned(6)
1040 61 # text(1)
1041 68 # "h"
1042 07 # unsigned(7)
1043 61 # text(1)
1044 69 # "i"
1045 08 # unsigned(8)
1046 61 # text(1)
1047 6A # "j"
1048 09 # unsigned(9)
1049 61 # text(1)
1050 6B # "k"
1051 0A # unsigned(10)
1052 61 # text(1)
1053 6C # "l"
1054 0B # unsigned(11)
1055 61 # text(1)
1056 6D # "m"
1057 0C # unsigned(12)
1058 61 # text(1)
1059 6E # "n"
1060 0D # unsigned(13)
1061 61 # text(1)
1062 6F # "o"
1063 0E # unsigned(14)
1064 61 # text(1)
1065 70 # "p"
1066 0F # unsigned(15)
1067 61 # text(1)
1068 71 # "q"
1069 10 # unsigned(16)
1070 61 # text(1)
1071 72 # "r"
1072 11 # unsigned(17)
1073 61 # text(1)
1074 73 # "s"
1075 12 # unsigned(18)
1076 61 # text(1)
1077 74 # "t"
1078 13 # unsigned(19)
1079 61 # text(1)
1080 75 # "u"
1081 14 # unsigned(20)
1082 61 # text(1)
1083 76 # "v"
1084 15 # unsigned(21)
1085 61 # text(1)
1086 77 # "w"
1087 16 # unsigned(22)
1088 61 # text(1)
1089 78 # "x"
1090 17 # unsigned(23)
1091 61 # text(1)
1092 79 # "y"
1093 18 18 # unsigned(24)
1094 61 # text(1)
1095 7A # "z"
1096 18 19 # unsigned(25)
1097 61 # text(1)
1098 41 # "A"
1099 18 1A # unsigned(26)
1100 61 # text(1)
1101 42 # "B"
1102 18 1B # unsigned(27)
1103 61 # text(1)
1104 43 # "C"
1105 18 1C # unsigned(28)
1106 61 # text(1)
1107 44 # "D"
1108 18 1D # unsigned(29)
1109 61 # text(1)
1110 45 # "E"
1111 18 1E # unsigned(30)
1112 65 # text(5)
1113 6D696E3331 # "min31"
1114 38 1E # negative(30)
1115 66 # text(6)
1116 706C75733331 # "plus31"
1117 18 1F # unsigned(31)
1118 63 # text(3)
1119 737472 # "str"
1120 78 1F # text(31)
1121 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1122 */
1123static const uint8_t EncodeLengthThirtyone[] = {
1124 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1125 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1126 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1127 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1128 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1129 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1130 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1131 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1132 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1133 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1134 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1135 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1136 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1137 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1138 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1139 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1140 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1141 0x31
1142};
1143
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001144int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001145{
1146 QCBOREncodeContext ECtx;
1147 int nReturn = 0;
1148
1149 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1150 QCBOREncode_OpenMap(&ECtx);
1151
1152 // add array with 31 items
1153 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1154 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001155 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001156 }
1157 QCBOREncode_CloseArray(&ECtx);
1158
1159 // add map with 31 items
1160 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001161 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001162 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001163 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001164 if (ix < 26) c = c + ix;
1165 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001166 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001167 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1168 }
1169 QCBOREncode_CloseMap(&ECtx);
1170
1171 // add -31 and +31
1172 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1173 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1174
1175 // add string with length 31
1176 const char *str = "testtesttesttesttesttestqcbor11";
1177 UsefulBufC str_b = { str, 31 };
1178 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1179
1180 QCBOREncode_CloseMap(&ECtx);
1181
1182 UsefulBufC ECBOR;
1183 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1184 nReturn = -1;
1185 }
1186
1187 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1188 return -2;
1189
1190 return(nReturn);
1191}
1192
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301193
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301194/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001195 * [ "2013-03-21T20:04:00Z",
1196 * 0("2013-03-21T20:04:00Z"),
1197 * 1363896240,
1198 * 1(1363896240),
1199 * 100(-10676),
1200 * 3994,
1201 * 1004("1940-10-09"),
1202 * "1980-12-08",
1203 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1204 * "SD": 1(999),
1205 * "Sample Date from RFC 8943": "1985-04-12",
1206 * 42: 1004("1985-04-12T23:20:50.52Z"),
1207 * "SY": 100(-10676),
1208 * 45: 3994
1209 * }
1210 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301211 */
1212static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001213 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1214 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1215 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1216 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1217 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1218 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1219 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1220 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1221 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1222 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1223 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1224 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1225 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1226 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1227 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1228 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1229 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1230 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1231 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1232 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1233 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1234 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1235 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001236
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001237int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001238{
1239 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001240
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301241 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243 QCBOREncode_OpenArray(&ECtx);
1244
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001245 /* The values are taken from the CBOR RFCs */
1246 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1247 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1248 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1249 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1250 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1251 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1252 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1253 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001254
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255 QCBOREncode_OpenMap(&ECtx);
1256
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001257 QCBOREncode_AddDateStringToMap(&ECtx,
1258 "Sample Date from RFC 3339",
1259 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001261 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1262 "Sample Date from RFC 8943",
1263 QCBOR_ENCODE_AS_BORROWED,
1264 "1985-04-12");
1265 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1266 42,
1267 QCBOR_ENCODE_AS_TAG,
1268 "1985-04-12T23:20:50.52Z");
1269 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1270 "SY",
1271 QCBOR_ENCODE_AS_TAG,
1272 -10676);
1273 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1274 45,
1275 QCBOR_ENCODE_AS_BORROWED,
1276 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001278 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001279
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280 QCBOREncode_CloseArray(&ECtx);
1281
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301282 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001283 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001284 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301287 if(CheckResults(ECBOR, spExpectedEncodedDates))
1288 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001289
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001290 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001291}
1292
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301293
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001294int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295{
1296 QCBOREncodeContext ECtx;
1297 int i;
1298 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001299
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301300 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001301 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1302 QCBOREncode_OpenArray(&ECtx);
1303 }
1304 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1305 QCBOREncode_CloseArray(&ECtx);
1306 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001307 UsefulBufC Encoded;
1308 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 nReturn = -1;
1310 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311
1312 return(nReturn);
1313}
1314
1315
1316
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001317int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001318{
1319 QCBOREncodeContext ECtx;
1320 int i;
1321 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301323 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001324 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1325 QCBOREncode_OpenArray(&ECtx);
1326 }
1327 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1328 QCBOREncode_CloseArray(&ECtx);
1329 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundblade0595e932018-11-02 22:22:47 +07001331 UsefulBufC Encoded;
1332 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001333 nReturn = -1;
1334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 return(nReturn);
1337}
1338
1339
1340
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001341int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342{
1343 QCBOREncodeContext ECtx;
1344 int i;
1345 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301347 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001348 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1349 QCBOREncode_OpenArray(&ECtx);
1350 }
1351 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1352 QCBOREncode_CloseArray(&ECtx);
1353 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001354 UsefulBufC Encoded;
1355 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001356 nReturn = -1;
1357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 return(nReturn);
1360}
1361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301363/*
1364 81 # array(1)
1365 81 # array(1)
1366 81 # array(1)
1367 81 # array(1)
1368 80 # array(0)
1369*/
1370static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301373/*
1374 82 # array(2)
1375 81 # array(1)
1376 81 # array(1)
1377 81 # array(1)
1378 81 # array(1)
1379 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001380 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301381 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1382 3B 0000000100000000 # negative(4294967296)
1383 3A FFFFFFFF # negative(4294967295)
1384 3A FFFFFFFE # negative(4294967294)
1385 3A FFFFFFFD # negative(4294967293)
1386 3A 7FFFFFFF # negative(2147483647)
1387 3A 7FFFFFFE # negative(2147483646)
1388 3A 00010001 # negative(65537)
1389 3A 00010000 # negative(65536)
1390 39 FFFF # negative(65535)
1391 39 FFFE # negative(65534)
1392 39 FFFD # negative(65533)
1393 39 0100 # negative(256)
1394 38 FF # negative(255)
1395 38 FE # negative(254)
1396 38 FD # negative(253)
1397 38 18 # negative(24)
1398 37 # negative(23)
1399 36 # negative(22)
1400 20 # negative(0)
1401 00 # unsigned(0)
1402 00 # unsigned(0)
1403 01 # unsigned(1)
1404 16 # unsigned(22)
1405 17 # unsigned(23)
1406 18 18 # unsigned(24)
1407 18 19 # unsigned(25)
1408 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001409 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301410 18 FE # unsigned(254)
1411 18 FF # unsigned(255)
1412 19 0100 # unsigned(256)
1413 19 0101 # unsigned(257)
1414 19 FFFE # unsigned(65534)
1415 19 FFFF # unsigned(65535)
1416 1A 00010000 # unsigned(65536)
1417 1A 00010001 # unsigned(65537)
1418 1A 00010002 # unsigned(65538)
1419 1A 7FFFFFFF # unsigned(2147483647)
1420 1A 7FFFFFFF # unsigned(2147483647)
1421 1A 80000000 # unsigned(2147483648)
1422 1A 80000001 # unsigned(2147483649)
1423 1A FFFFFFFE # unsigned(4294967294)
1424 1A FFFFFFFF # unsigned(4294967295)
1425 1B 0000000100000000 # unsigned(4294967296)
1426 1B 0000000100000001 # unsigned(4294967297)
1427 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1428 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1429 */
1430static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001431 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1433 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1434 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1435 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1436 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1437 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1438 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1439 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1440 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1441 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1442 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001443 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1444 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1445 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1446 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1447 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1448 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1449 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1450 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1451 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1452 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1453 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1454 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1455 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456
1457
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001458int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001459{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001460 QCBOREncodeContext ECtx;
1461
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301462 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001463 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301464 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1465 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001466 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001467
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001468 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundblade0595e932018-11-02 22:22:47 +07001470 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001471 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001473
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301474 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001475 return -5;
1476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001477
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001478 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479}
1480
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301481/*
1482 This returns a pointer to spBigBuf
1483 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001484static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485{
1486 QCBOREncodeContext ECtx;
1487 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001488
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001489 *pEncoded = NULL;
1490 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301491 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001492
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 // loop runs CBOR encoding twice. First with no buffer to
1494 // calucate the length so buffer can be allocated correctly,
1495 // and last with the buffer to do the actual encoding
1496 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301497 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 QCBOREncode_OpenMap(&ECtx);
1499 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1500 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1501 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1502 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1503 QCBOREncode_CloseArray(&ECtx);
1504 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1505 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1506 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1507 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1508 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1509 QCBOREncode_CloseMap(&ECtx);
1510 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001511
Laurence Lundblade0595e932018-11-02 22:22:47 +07001512 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001513 goto Done;
1514 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301515 if(uFirstSizeEstimate != *pEncodedLen) {
1516 nReturn = 1;
1517 } else {
1518 nReturn = 0;
1519 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 goto Done;
1521 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301522 *pEncoded = spBigBuf;
1523 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001527 Done:
1528 return(nReturn);
1529}
1530
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301531/*
1532 A3 # map(3)
1533 6D # text(13)
1534 666972737420696E7465676572 # "first integer"
1535 18 2A # unsigned(42)
1536 77 # text(23)
1537 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1538 82 # array(2)
1539 67 # text(7)
1540 737472696E6731 # "string1"
1541 67 # text(7)
1542 737472696E6732 # "string2"
1543 6C # text(12)
1544 6D617020696E2061206D6170 # "map in a map"
1545 A4 # map(4)
1546 67 # text(7)
1547 62797465732031 # "bytes 1"
1548 44 # bytes(4)
1549 78787878 # "xxxx"
1550 67 # text(7)
1551 62797465732032 # "bytes 2"
1552 44 # bytes(4)
1553 79797979 # "yyyy"
1554 6B # text(11)
1555 616E6F7468657220696E74 # "another int"
1556 18 62 # unsigned(98)
1557 66 # text(6)
1558 746578742032 # "text 2"
1559 78 1E # text(30)
1560 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1561 */
1562static const uint8_t spValidMapEncoded[] = {
1563 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1564 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1565 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1566 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1567 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1568 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1569 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1570 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1571 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1572 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1573 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1574 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1575 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1576 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1577 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1578 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001579
1580
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001581int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582{
1583 uint8_t *pEncodedMaps;
1584 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301587 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301591 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1592 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001593
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001594 return(nReturn);
1595}
1596
1597
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598/*
1599 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001600
Laurence Lundbladeee851742020-01-08 08:37:05 -08001601 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1602 CBOR_SIMPLEV_NULL
1603 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001604 @param[in] szAlexString Diagnostic code.
1605 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001606 @param[in/out] pnLen Size of pOut buffer when called; length of data
1607 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001609 @return
1610 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001612 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 -08001613 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001616 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1617 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 */
1620
Laurence Lundbladeee851742020-01-08 08:37:05 -08001621static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001622FormatRTICResults(uint8_t uRResult,
1623 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001624 const char *szType,
1625 const char *szAlexString,
1626 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001627{
1628 // Buffer that the result will be written in to
1629 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001630 // QCBOREncode will never write off the end of this buffer. If it won't
1631 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001633 // Context for the encoder
1634 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301635 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001637 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1638 // Contents are label / value pairs
1639 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001641 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001642
Laurence Lundbladeee851742020-01-08 08:37:05 -08001643 // The result: 0 if scan happened and found nothing; 1 if it happened and
1644 // found something wrong; 2 if it didn't happen
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001645 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646
1647 // Add the diagnostic code
1648 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1649
1650 // Add a time stamp
1651 if(time) {
1652 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1653 }
1654
1655 // Add the diagnostic code
1656 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1657
1658 // Open a subordinate map for telemtry data
1659 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1660
1661 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001663 // Add a few fake integers and buffers for now.
1664 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1665
1666 // Add a few fake integers and buffers for now.
1667 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1668
1669 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301670 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001671 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1674 }
1675 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001676
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001677 // Close the telemetry map
1678 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001679
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001680 // Close the map
1681 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001682
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301683 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001684
Laurence Lundblade0595e932018-11-02 22:22:47 +07001685 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001686
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301687 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001688}
1689
1690
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301691/*
1692 A5 # map(5)
1693 69 # text(9)
1694 696E74656772697479 # "integrity"
1695 F4 # primitive(20)
1696 64 # text(4)
1697 74797065 # "type"
1698 66 # text(6)
1699 726563656E74 # "recent"
1700 64 # text(4)
1701 74696D65 # "time"
1702 C1 # tag(1)
1703 1A 580D4172 # unsigned(1477263730)
1704 64 # text(4)
1705 64696167 # "diag"
1706 6A # text(10)
1707 30784131654335303031 # "0xA1eC5001"
1708 69 # text(9)
1709 74656C656D65747279 # "telemetry"
1710 A3 # map(3)
1711 69 # text(9)
1712 53686F652053697A65 # "Shoe Size"
1713 0C # unsigned(12)
1714 62 # text(2)
1715 4951 # "IQ"
1716 1A FFFFFFFF # unsigned(4294967295)
1717 77 # text(23)
1718 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1719 48 # bytes(8)
1720 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1721 */
1722static const uint8_t spExpectedRTIC[] = {
1723 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1724 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1725 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1726 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1727 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1728 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1729 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1730 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1731 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1732 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1733 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1734 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001735
1736
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001737int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001739 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301740 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301741 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301742 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001743 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001745
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301746 if(CheckResults(Encoded, spExpectedRTIC)) {
1747 return -2;
1748 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001749
1750 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001751}
1752
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301753
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301754/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001755 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001756
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301757 82 # array(2)
1758 19 01C3 # unsigned(451)
1759 43 # bytes(3)
1760 1901D2 # "\x19\x01\xD2"
1761*/
1762static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301763
Laurence Lundblade684aec22018-10-12 19:33:53 +08001764/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001765 81 #array(1)
1766 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1767 */
1768static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1769
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001770static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1771
Laurence Lundbladeda532272019-04-07 11:40:17 -07001772/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001773 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001774 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001775int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001776{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001777 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001778
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001779 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301780 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001781
Laurence Lundblade684aec22018-10-12 19:33:53 +08001782 QCBOREncode_OpenArray(&EC);
1783 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001784
Laurence Lundblade684aec22018-10-12 19:33:53 +08001785 QCBOREncode_BstrWrap(&EC);
1786 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001787
Laurence Lundblade684aec22018-10-12 19:33:53 +08001788 UsefulBufC Wrapped;
1789 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001790
Laurence Lundblade684aec22018-10-12 19:33:53 +08001791 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001792
Laurence Lundblade684aec22018-10-12 19:33:53 +08001793 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001794 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001795 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001796 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001797
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301798 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001799 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001800 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001801
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001802 // Second test - see if the length of the wrapped
1803 // bstr is correct. Also tests bstr wrapping
1804 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001805 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1806 QCBOREncode_OpenArray(&EC);
1807 QCBOREncode_BstrWrap(&EC);
1808 QCBOREncode_OpenArray(&EC);
1809 QCBOREncode_AddNULL(&EC);
1810 QCBOREncode_CloseArray(&EC);
1811 UsefulBufC BStr;
1812 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001813 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1814 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001815 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001816 return -5;
1817 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001818
Laurence Lundbladeda532272019-04-07 11:40:17 -07001819 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1820 // bstr wrapping use cases.
1821 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1822 QCBOREncode_Init(&EC, StuffBuf);
1823 QCBOREncode_OpenArray(&EC);
1824 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1825 QCBOREncode_CloseArray(&EC);
1826 if(QCBOREncode_Finish(&EC, &Encoded)) {
1827 return -6;
1828 }
1829 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1830 return -7;
1831 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001832
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001833 // Fourth test, cancelling a byte string
1834 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1835
1836 QCBOREncode_OpenArray(&EC);
1837 QCBOREncode_AddUInt64(&EC, 451);
1838
1839 QCBOREncode_BstrWrap(&EC);
1840 QCBOREncode_CancelBstrWrap(&EC);
1841
1842
1843 QCBOREncode_AddUInt64(&EC, 42);
1844 QCBOREncode_CloseArray(&EC);
1845 if(QCBOREncode_Finish(&EC, &Encoded)) {
1846 return -8;
1847 }
1848 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1849 return -9;
1850 }
1851
1852 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001853 // Fifth test, failed cancelling
1854 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1855
1856 QCBOREncode_OpenArray(&EC);
1857 QCBOREncode_AddUInt64(&EC, 451);
1858
1859 QCBOREncode_BstrWrap(&EC);
1860 QCBOREncode_AddUInt64(&EC, 99);
1861 QCBOREncode_CancelBstrWrap(&EC);
1862
1863 QCBOREncode_AddUInt64(&EC, 42);
1864 QCBOREncode_CloseArray(&EC);
1865 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001866#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001867 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1868 return -10;
1869 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001870#else
1871 if(uErr != QCBOR_SUCCESS) {
1872 return -110;
1873 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001874#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1875
1876 // Sixth test, another cancel, but the error is not caught
1877 // This use will produce unintended CBOR. The error
1878 // is not caught because it would require tracking state
1879 // for QCBOREncode_BstrWrapInMapN.
1880 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1881
1882 QCBOREncode_OpenMap(&EC);
1883 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1884
1885 QCBOREncode_BstrWrapInMapN(&EC, 55);
1886 QCBOREncode_CancelBstrWrap(&EC);
1887
1888 QCBOREncode_CloseMap(&EC);
1889 uErr = QCBOREncode_Finish(&EC, &Encoded);
1890 if(uErr != QCBOR_SUCCESS) {
1891 return -11;
1892 }
1893
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001894 return 0;
1895}
1896
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001897
1898
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001899int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001900{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001901 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001902 UsefulBufC Wrapped;
1903 UsefulBufC Encoded2;
1904 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001905
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001906 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001907
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301908 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001909
Laurence Lundblade684aec22018-10-12 19:33:53 +08001910 QCBOREncode_OpenArray(&EC);
1911 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001912
Laurence Lundblade684aec22018-10-12 19:33:53 +08001913 QCBOREncode_BstrWrap(&EC);
1914 QCBOREncode_AddUInt64(&EC, 466);
1915 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001916
Laurence Lundblade684aec22018-10-12 19:33:53 +08001917 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001918
Laurence Lundblade684aec22018-10-12 19:33:53 +08001919 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001920
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001921 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001922#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001923 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001924 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001925 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001926#else
1927 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1928 * is set and not to be sure to excerice all the relavant code in
1929 * both conditions. When the guards are disabled, there is no
1930 * error returned, but the code path is still covered.
1931 */
1932 if(uError != QCBOR_SUCCESS) {
1933 return (int32_t)(600 + uError);
1934 }
1935#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001936
Laurence Lundbladeee851742020-01-08 08:37:05 -08001937 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301938 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001939 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001940 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001941#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001942 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001943 return (int32_t)(700 + uError);
1944 }
1945#else
1946 if(uError != QCBOR_SUCCESS) {
1947 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001948 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001949#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade684aec22018-10-12 19:33:53 +08001951 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301952 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001953 for(int i = 1; i < 18; i++) {
1954 QCBOREncode_BstrWrap(&EC);
1955 }
1956 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001957
Laurence Lundblade684aec22018-10-12 19:33:53 +08001958 for(int i = 1; i < 18; i++) {
1959 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001961
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001962 uError = QCBOREncode_Finish(&EC, &Encoded2);
1963 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001964 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001965 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001966
Laurence Lundblade684aec22018-10-12 19:33:53 +08001967 return 0;
1968}
1969
1970
Laurence Lundblade684aec22018-10-12 19:33:53 +08001971/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001972 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001973
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001974 [
1975 h'82004E82014B8202488203458204428105',
1976 {
1977 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1978 8426568656C6C6F18234BA2131318436568656C6C6F'
1979 }
1980 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001981
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001982 Unwrapping the first byte string in the above gives
1983 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301984
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001985 Unwrapping again, the byte string immediately above gives
1986 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301987
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001988 ...
1989
1990 Unrapping the second byte string in the top-level CBOR above gives
1991 {16: 16,
1992 64: "hello",
1993 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1994 }
1995
1996 Unwrapping again, the byte string immediately above gives
1997 {17: 17,
1998 65: "hello",
1999 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2000 }
2001
2002 ...
2003
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302004 */
2005static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002006{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002007 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2008 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2009 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2010 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2011 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2012 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2013 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2014 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2015 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2016 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002017};
2018
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002019
2020/*
2021 Get an int64 out of the decoder or fail.
2022 */
2023static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002024{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002025 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002026 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002027
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002028 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002029 if(nReturn) {
2030 return nReturn;
2031 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002032 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002033 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002036 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002037 return 0;
2038}
2039
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002040/*
2041 Get an array out of the decoder or fail.
2042 */
2043static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002044{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002045 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002046 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002047
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002048 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002049 if(nReturn) {
2050 return nReturn;
2051 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002052 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2053 return -1;
2054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002056 *pInt = Item.val.uCount;
2057 return 0;
2058}
2059
2060/*
2061 Get a map out of the decoder or fail.
2062 */
2063static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2064{
2065 QCBORItem Item;
2066 int32_t nReturn;
2067
2068 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002069 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002070 return nReturn;
2071 }
2072 if(Item.uDataType != QCBOR_TYPE_MAP) {
2073 return -1;
2074 }
2075
2076 *pInt = Item.val.uCount;
2077 return 0;
2078}
2079
2080/*
2081 Get a byte string out of the decoder or fail.
2082 */
2083static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2084{
2085 QCBORItem Item;
2086 int32_t nReturn;
2087
2088 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2089 if(nReturn) {
2090 return nReturn;
2091 }
2092 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002093 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002094 }
2095
2096 *pBstr = Item.val.string;
2097 return 0;
2098}
2099
2100/*
2101 Get a byte string out of the decoder or fail.
2102 */
2103static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2104{
2105 QCBORItem Item;
2106 int nReturn;
2107
2108 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2109 if(nReturn) {
2110 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002111 }
2112 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002113 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002114 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002115
2116 *pTstr = Item.val.string;
2117 return 0;
2118}
2119
2120
2121/*
2122 Recursively decode array containing a little CBOR and a bstr wrapped array
2123 with a little CBOR and a bstr wrapped array...
2124
2125 Part of bstr_wrap_nest_test.
2126 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2127{
2128 int64_t nInt;
2129 UsefulBufC Bstr;
2130 uint16_t nArrayCount;
2131 QCBORDecodeContext DC;
2132 int32_t nResult;
2133
2134 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2135
2136 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2137 return -10;
2138 }
2139
2140 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002141 return -11;
2142 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002143
2144 nResult = GetByteString(&DC, &Bstr);
2145 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2146 if(nArrayCount != 1) {
2147 return -12;
2148 } else {
2149 // successful exit
2150 return 0;
2151 }
2152 }
2153 if(nResult) {
2154 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002156
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002157 // tail recursion; good compilers will reuse the stack frame
2158 return DecodeNextNested(Bstr);
2159}
2160
2161
2162/*
2163 Recursively decode map containing a little CBOR and a bstr wrapped map
2164 with a little CBOR and a bstr wrapped map...
2165
2166 Part of bstr_wrap_nest_test.
2167 */
2168static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2169{
2170 int32_t nResult;
2171 uint16_t nMapCount;
2172 int64_t nInt;
2173 UsefulBufC Bstr;
2174 QCBORDecodeContext DC;
2175
2176 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2177
2178 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2179 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002181
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002182 if(GetInt64(&DC, &nInt)) {
2183 return -21;
2184 }
2185
2186 // The "hello"
2187 if(GetTextString(&DC, &Bstr)) {
2188 return -22;
2189 }
2190
2191 nResult = GetByteString(&DC, &Bstr);
2192 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2193 if(nMapCount == 2) {
2194 // successful exit
2195 return 0;
2196 } else {
2197 return -23;
2198 }
2199 }
2200
2201 if(nResult) {
2202 return -24;
2203 }
2204
2205 // tail recursion; good compilers will reuse the stack frame
2206 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002207}
2208
2209
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002210int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002211{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002212 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302213 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade684aec22018-10-12 19:33:53 +08002215 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002216 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002217
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002218 QCBOREncode_OpenArray(&EC);
2219
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002220 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002221 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002222 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002223 QCBOREncode_AddInt64(&EC, i);
2224 }
2225 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2226 QCBOREncode_CloseArray(&EC);
2227 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002229
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002230 QCBOREncode_OpenMap(&EC);
2231 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2232 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2233 QCBOREncode_OpenMap(&EC);
2234 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002235 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002236 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002237
2238 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2239 QCBOREncode_CloseMap(&EC);
2240 QCBOREncode_CloseBstrWrap(&EC, NULL);
2241 }
2242 QCBOREncode_CloseMap(&EC);
2243
Laurence Lundblade684aec22018-10-12 19:33:53 +08002244 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundblade684aec22018-10-12 19:33:53 +08002246 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002247 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002248 return -1;
2249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002250
Laurence Lundblade684aec22018-10-12 19:33:53 +08002251 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302252 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002253 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002254 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002255
Laurence Lundblade684aec22018-10-12 19:33:53 +08002256 // ---- Decode it and see if it is OK ------
2257 QCBORDecodeContext DC;
2258 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002259
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002260 UsefulBufC Bstr;
2261 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002263 // Array surrounding the the whole thing
2264 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002265 return -3;
2266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002268 // Get the byte string wrapping some array stuff
2269 if(GetByteString(&DC, &Bstr)) {
2270 return -4;
2271 }
2272
2273 // Decode the wrapped nested structure
2274 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002275 if(nReturn) {
2276 return nReturn;
2277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002278
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002279 // A map enclosing some map-oriented bstr wraps
2280 if(GetMap(&DC, &nArrayCount)) {
2281 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002283
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002284 // Get the byte string wrapping some array stuff
2285 if(GetByteString(&DC, &Bstr)) {
2286 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002288
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002289 // Decode the wrapped nested structure
2290 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002291 if(nReturn) {
2292 return nReturn;
2293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002294
Laurence Lundblade684aec22018-10-12 19:33:53 +08002295 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002296 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002297 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002298
Laurence Lundblade684aec22018-10-12 19:33:53 +08002299 return 0;
2300}
2301
2302
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002303static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302304 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2305 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2306 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2307 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2308 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2309 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2310 0x45, 0xca, 0xcb, 0x36};
2311
2312/*
2313 D2 # tag(18)
2314 84 # array(4)
2315 43 # bytes(3)
2316 A10126 # "\xA1\x01&"
2317 A1 # map(1)
2318 04 # unsigned(4)
2319 42 # bytes(2)
2320 3131 # "11"
2321 54 # bytes(20)
2322 546869732069732074686520636F6E74656E742E # "This is the content."
2323 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002324 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2325 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2326 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2327 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2328 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2329 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2330 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2331 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302332 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002333static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302334 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2335 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2336 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2337 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2338 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2339 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2340 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2341 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2342 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2343 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2344
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002345static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2346
2347
Laurence Lundblade684aec22018-10-12 19:33:53 +08002348/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002349 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002350 C.2.1. This doesn't actually verify the signature (however
2351 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002352 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002353int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002354{
2355 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002356 const char *szKid = "11";
2357 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2358 const char *szPayload = "This is the content.";
2359 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2360 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2361 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade684aec22018-10-12 19:33:53 +08002363 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002364
2365 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302366 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundblade684aec22018-10-12 19:33:53 +08002368 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002369 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002370 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002371
Laurence Lundblade684aec22018-10-12 19:33:53 +08002372 // Add protected headers
2373 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002374
Laurence Lundblade684aec22018-10-12 19:33:53 +08002375 // Empty map with unprotected headers
2376 QCBOREncode_OpenMap(&EC);
2377 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2378 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002379
Laurence Lundblade684aec22018-10-12 19:33:53 +08002380 // The payload
2381 UsefulBufC WrappedPayload;
2382 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002383 // Payload is not actually CBOR in example C.2.1 like it would be
2384 // for a CWT or EAT. It is just a text string.
2385 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002386 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002387
Laurence Lundblade684aec22018-10-12 19:33:53 +08002388 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002389 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2390 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002391 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002392 return -1;
2393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002395/* if(UsefulBuf_Compare(WrappedPayload,
2396 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2397 return -1;
2398 } */
2399
Laurence Lundblade684aec22018-10-12 19:33:53 +08002400 // The signature
2401 QCBOREncode_AddBytes(&EC, Signature);
2402 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002403
Laurence Lundblade684aec22018-10-12 19:33:53 +08002404 // Finish and check the results
2405 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002406 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002407 return -2;
2408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002409
Laurence Lundblade684aec22018-10-12 19:33:53 +08002410 // 98 is the size from RFC 8152 C.2.1
2411 if(COSE_Sign1.len != 98) {
2412 return -3;
2413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002415 // It would be good to compare this to the output from a COSE
2416 // implementation like COSE-C. This has been checked against the
2417 // CBOR playground.
2418 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002419 return -4;
2420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002421
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002422
2423 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2424 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2425
2426 // top level array for cose sign1, 18 is the tag for COSE sign
2427 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2428 QCBOREncode_OpenArray(&EC);
2429
2430 // Add protected headers
2431 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2432
2433 // Empty map with unprotected headers
2434 QCBOREncode_OpenMap(&EC);
2435 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2436 QCBOREncode_CloseMap(&EC);
2437
2438 // The payload
2439 QCBOREncode_BstrWrap(&EC);
2440 // Payload is not actually CBOR in example C.2.1 like it would be
2441 // for a CWT or EAT. It is just a text string.
2442 QCBOREncode_AddEncoded(&EC, Payload);
2443 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2444
2445 // Check we got back the actual payload expected
2446 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2447 if(UsefulBuf_Compare(WrappedPayload,
2448 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2449 return -11;
2450 }
2451
2452 // The signature
2453 QCBOREncode_AddBytes(&EC, Signature);
2454 QCBOREncode_CloseArray(&EC);
2455
2456 // Finish and check the results
2457 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2458 return -12;
2459 }
2460
2461 // 98 is the size from RFC 8152 C.2.1
2462 if(COSE_Sign1.len != 98) {
2463 return -13;
2464 }
2465
2466 // It would be good to compare this to the output from a COSE
2467 // implementation like COSE-C. This has been checked against the
2468 // CBOR playground.
2469 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2470 return -14;
2471 }
2472
Laurence Lundblade684aec22018-10-12 19:33:53 +08002473 return 0;
2474}
2475
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002476
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002477int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002478{
2479 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002480 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002481
2482
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002483 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002484 // Do all of these tests with NULL buffers so no actual
2485 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002486 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002487
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002488 // First verify no error from a big buffer
2489 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002490 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002491 // 6 is the CBOR overhead for opening the array and encodng the length
2492 // This exactly fills the buffer.
2493 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002494 QCBOREncode_CloseArray(&EC);
2495 size_t xx;
2496 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2497 return -1;
2498 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002499
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002500 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002501 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002502 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002503 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002504 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002505 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002506 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2507 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002508 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002509 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002510 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002511 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2512 return -2;
2513 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002514
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002515 // Third, fit an array in exactly at max position allowed
2516 QCBOREncode_Init(&EC, Buffer);
2517 QCBOREncode_OpenArray(&EC);
2518 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2519 QCBOREncode_OpenArray(&EC);
2520 QCBOREncode_CloseArray(&EC);
2521 QCBOREncode_CloseArray(&EC);
2522 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2523 return -10;
2524 }
2525
2526
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002527 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2528 // Work close to the 4GB size limit for a better test
2529 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002530 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002531
2532 QCBOREncode_Init(&EC, Large);
2533 QCBOREncode_OpenArray(&EC);
2534 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2535 QCBOREncode_CloseArray(&EC);
2536 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2537 // Making sure it succeeds when it should first
2538 return -3;
2539 }
2540
2541 QCBOREncode_Init(&EC, Large);
2542 QCBOREncode_OpenArray(&EC);
2543 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2544 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2545 QCBOREncode_CloseArray(&EC);
2546 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2547 // Now just 1 byte over, see that it fails
2548 return -4;
2549 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002550
2551
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002552 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2553 QCBOREncode_Init(&EC, Large);
2554 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2555 QCBOREncode_OpenArray(&EC);
2556 }
2557 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2558 QCBOREncode_CloseArray(&EC);
2559 }
2560 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2561 // Making sure it succeeds when it should first
2562 return -5;
2563 }
2564
2565 QCBOREncode_Init(&EC, Large);
2566 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2567 QCBOREncode_OpenArray(&EC);
2568 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002569 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002570 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2571 QCBOREncode_CloseArray(&EC);
2572 }
2573 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002574 return -6;
2575 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002576
2577
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002578 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002579 QCBOREncode_Init(&EC, Large);
2580 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2581 QCBOREncode_OpenArray(&EC);
2582 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002583 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002584 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2585 QCBOREncode_CloseArray(&EC);
2586 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002587 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2588#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2589 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002590 return -7;
2591 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002592#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2593 if(uErr != QCBOR_SUCCESS) {
2594 return -107;
2595 }
2596#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002597
2598
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002599 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002600 QCBOREncode_Init(&EC, Large);
2601 QCBOREncode_OpenArray(&EC);
2602 UsefulBufC Wrap;
2603 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002604 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2605#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2606 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002607 return -8;
2608 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002609#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2610 if(uErr != QCBOR_SUCCESS) {
2611 return -108;
2612 }
2613#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002614
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002615 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002616 QCBOREncode_Init(&EC, Large);
2617 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2618 QCBOREncode_OpenArray(&EC);
2619 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002620 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002621 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2622 QCBOREncode_CloseArray(&EC);
2623 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002624
2625 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2626#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2627 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002628 return -9;
2629 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002630#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2631 if(uErr != QCBOR_SUCCESS) {
2632 return -109;
2633 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002634#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002635
Laurence Lundblade241705e2018-12-30 18:56:14 -08002636 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2637 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002638
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002639
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002640 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002641 QCBOREncode_Init(&EC, Buffer);
2642 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2643 return -11;
2644 }
2645
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002646 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002647 QCBOREncode_Init(&EC, Large);
2648 QCBOREncode_OpenArray(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002649 QCBOREncode_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
2650 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2651#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2652 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002653 return -12;
2654 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002655#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2656 if(uErr != QCBOR_SUCCESS) {
2657 return -112;
2658 }
2659#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2660
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002661
2662 QCBOREncode_Init(&EC, Large);
2663 QCBOREncode_OpenArray(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002664 QCBOREncode_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
2665 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2666#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2667 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002668 return -13;
2669 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002670#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2671 if(uErr != QCBOR_SUCCESS) {
2672 return -113;
2673 }
2674#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002675
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002676
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002677 return 0;
2678}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002679
2680
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002681#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002682/*
2683 [
2684 4([-1, 3]),
2685 4([-20, 4759477275222530853136]),
2686 4([9223372036854775807, -4759477275222530853137]),
2687 5([300, 100]),
2688 5([-20, 4759477275222530853136]),
2689 5([-9223372036854775808, -4759477275222530853137])
2690 ]
2691 */
2692static const uint8_t spExpectedExponentAndMantissaArray[] = {
2693 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2694 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2695 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2696 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2697 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2698 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2699 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2700 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2701 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2702 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2703 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2704
2705
2706/*
2707 {
2708 "decimal fraction": 4([-1, 3]),
2709 300: 4([-1, 3]),
2710 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2711 400: 4([2147483647, 4759477275222530853136]),
2712 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2713 500: 4([9223372036854775807, -4759477275222530853137]),
2714 "big float": 5([300, 100]),
2715 600: 5([300, 100]),
2716 "big float bignum positive": 5([-20, 4759477275222530853136]),
2717 700: 5([-20, 4759477275222530853136]),
2718 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2719 800: 5([-9223372036854775808, -4759477275222530853137])
2720 }
2721 */
2722static const uint8_t spExpectedExponentAndMantissaMap[] = {
2723 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2724 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2725 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2726 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2727 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2728 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2729 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2730 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2731 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2732 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2733 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2734 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2735 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2736 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2737 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2738 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2739 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2740 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2741 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2742 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2743 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2744 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2745 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2746 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2747 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2748 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2749 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2750 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2751 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2752 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2753 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2754 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2755 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2756 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2757 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2758 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2759 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2760 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2761 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2762 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2763 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2764 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2765 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2766 0x09, 0x10
2767};
2768
2769
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002770int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002771{
2772 QCBOREncodeContext EC;
2773 UsefulBufC EncodedExponentAndMantissa;
2774
2775 // Constant for the big number used in all the tests.
2776 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2777 0x06, 0x07, 0x08, 0x09, 0x010};
2778 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2779
2780 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2781 QCBOREncode_OpenArray(&EC);
2782 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2783 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2784 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2785 QCBOREncode_AddBigFloat(&EC, 100, 300);
2786 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2787 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2788 QCBOREncode_CloseArray(&EC);
2789
2790 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2791 return -2;
2792 }
2793
2794 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2795 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2796 NULL);
2797 if(nReturn) {
2798 return nReturn;
2799 }
2800
2801 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2802 QCBOREncode_OpenMap(&EC);
2803
2804 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2805
2806 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2807
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002808 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002809 "decimal fraction bignum postive",
2810 BigNum,
2811 false,
2812 -200);
2813
2814 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2815 400,
2816 BigNum,
2817 false,
2818 INT32_MAX);
2819
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002820 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002821 "decimal fraction bignum negative",
2822 BigNum,
2823 true,
2824 INT64_MAX);
2825
2826 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2827 500,
2828 BigNum,
2829 true,
2830 INT64_MAX);
2831
2832 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2833
2834 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2835
2836 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2837 "big float bignum positive",
2838 BigNum,
2839 false,
2840 -20);
2841
2842 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2843 700,
2844 BigNum,
2845 false,
2846 -20);
2847
2848 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2849 "big float bignum negative",
2850 BigNum,
2851 true,
2852 INT64_MIN);
2853
2854 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2855 800,
2856 BigNum,
2857 true,
2858 INT64_MIN);
2859
2860 QCBOREncode_CloseMap(&EC);
2861
2862 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2863 return -3;
2864 }
2865
2866
2867 struct UBCompareDiagnostic Diag;
2868
2869 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2870 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2871 &Diag);
2872 if(nReturn) {
2873 return nReturn + 1000000; // +1000000 to distinguish from first test above
2874 }
2875
2876 return 0;
2877}
2878
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002879#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002880
2881
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002882int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002883{
2884 /* This test doesn't have to be extensive, because just about every
2885 * other test exercises QCBOREncode_EncodeHead().
2886 */
2887 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002888 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002889
2890 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2891 CBOR_MAJOR_TYPE_POSITIVE_INT,
2892 0,
2893 0);
2894
2895 static const uint8_t expectedZero[] = {0x00};
2896
2897 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2898 return -1;
2899 }
2900
2901 // ---- Encode a zero padded out to an 8 byte integer ----
2902 encoded = QCBOREncode_EncodeHead(RightSize,
2903 CBOR_MAJOR_TYPE_POSITIVE_INT,
2904 8, // uMinSize is 8 bytes
2905 0);
2906
2907 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2908 0x00, 0x00, 0x00, 0x00};
2909
2910 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2911 return -2;
2912 }
2913
2914
2915 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002916 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002917
2918 encoded = QCBOREncode_EncodeHead(TooSmall,
2919 CBOR_MAJOR_TYPE_POSITIVE_INT,
2920 0,
2921 0);
2922
2923 if(!UsefulBuf_IsNULLC(encoded)) {
2924 return -3;
2925 }
2926
2927 return 0;
2928}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002929
2930
2931static const uint8_t spExpectedForOpenBytes[] = {
2932 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2933 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2934 0x78
2935};
2936
2937static const uint8_t spExpectedForOpenBytes2[] = {
2938 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2939 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2940 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2941};
2942
2943int32_t
2944OpenCloseBytesTest(void)
2945{
2946 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2947 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2948 QCBOREncodeContext EC;
2949 UsefulBuf Place;
2950 UsefulBufC Encoded;
2951 QCBORError uErr;
2952
2953 /* Normal use case -- add a byte string that fits */
2954 QCBOREncode_Init(&EC, TestBuf);
2955 QCBOREncode_OpenBytes(&EC, &Place);
2956 if(Place.ptr != TestBuf.ptr ||
2957 Place.len != TestBuf.len) {
2958 return 1;
2959 }
2960 Place.len -= 4;
2961 UsefulBuf_Set(Place, 'x');
2962 QCBOREncode_CloseBytes(&EC, Place.len);
2963 QCBOREncode_Finish(&EC, &Encoded);
2964 if(UsefulBuf_Compare(Encoded,
2965 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2966 return 2;
2967 }
2968
Paul Liétar79789772022-07-26 20:33:18 +01002969 /* Run the same test but with a NULL buffer */
2970 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
2971 QCBOREncode_OpenBytes(&EC, &Place);
2972 if(!UsefulBuf_IsNULL(Place)) {
2973 return 3;
2974 }
2975 Place.len -= 4;
2976 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
2977 QCBOREncode_CloseBytes(&EC, Place.len);
2978 uErr = QCBOREncode_Finish(&EC, &Encoded);
2979 if(uErr != QCBOR_SUCCESS ||
2980 Encoded.len != sizeof(spExpectedForOpenBytes)) {
2981 return 4;
2982 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002983
2984 /* Open a byte string with no room left */
2985 QCBOREncode_Init(&EC, TestBuf);
2986 QCBOREncode_AddSZString(&EC, "0123456789012345678");
2987 QCBOREncode_OpenBytes(&EC, &Place);
2988 if(Place.ptr != NULL ||
2989 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01002990 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002991 }
2992
2993 /* Try to extend byte string past end of encoding output buffer */
2994 QCBOREncode_Init(&EC, TestBuf);
2995 QCBOREncode_AddSZString(&EC, "012345678901234567");
2996 QCBOREncode_OpenBytes(&EC, &Place);
2997 /* Don't bother to write any bytes*/
2998 QCBOREncode_CloseBytes(&EC, Place.len+1);
2999 uErr = QCBOREncode_GetErrorState(&EC);
3000 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003001 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003002 }
3003
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003004 /* Close a byte string without opening one. */
3005 QCBOREncode_Init(&EC, TestBuf);
3006 QCBOREncode_AddSZString(&EC, "012345678");
3007 QCBOREncode_CloseBytes(&EC, 1);
3008 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003009#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003010 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003011 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003012 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003013#else
3014 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003015 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003016 }
3017#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003018
3019 /* Forget to close a byte string */
3020 QCBOREncode_Init(&EC, TestBuf);
3021 QCBOREncode_AddSZString(&EC, "012345678");
3022 QCBOREncode_OpenBytes(&EC, &Place);
3023 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003024#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003025 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003026 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003027 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003028#else
3029 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003030 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003031 }
3032#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003033
3034 /* Try to open a byte string in a byte string */
3035 QCBOREncode_Init(&EC, TestBuf);
3036 QCBOREncode_AddSZString(&EC, "012345678");
3037 QCBOREncode_OpenBytes(&EC, &Place);
3038 QCBOREncode_OpenBytes(&EC, &Place);
3039 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003040#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003041 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003042 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003043 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003044#else
3045 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003046 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003047 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003048#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3049
3050 /* A successful case with a little complexity */
3051 QCBOREncode_Init(&EC, TestBuf2);
3052 QCBOREncode_OpenMap(&EC);
3053 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3054 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3055 Place.len = 2;
3056 UsefulBuf_Set(Place, 'x');
3057 QCBOREncode_CloseBytes(&EC, 2);
3058 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3059 Place.len = 3;
3060 UsefulBuf_Set(Place, 'y');
3061 QCBOREncode_CloseBytes(&EC, 3);
3062 QCBOREncode_OpenArrayInMapN(&EC, 40);
3063 QCBOREncode_OpenBytes(&EC, &Place);
3064 QCBOREncode_CloseBytes(&EC, 0);
3065 QCBOREncode_CloseArray(&EC);
3066 QCBOREncode_CloseMap(&EC);
3067 uErr = QCBOREncode_Finish(&EC, &Encoded);
3068 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003069 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003070 }
3071 if(UsefulBuf_Compare(Encoded,
3072 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003073 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003074 }
3075
3076 return 0;
3077}