blob: 5b8c071472172f43a805ffe6e79af3ac3bf48e88 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundbladeee851742020-01-08 08:37:05 -080051//#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070076 if(((const uint8_t *)U1.ptr)[i] != ((const uint8_t *)U2.ptr)[i]) {
escherstairb5563422023-02-13 18:36:45 +010077 printf("Position: %u Actual: 0x%x Expected: 0x%x\n",
Laurence Lundbladeee851742020-01-08 08:37:05 -080078 (uint32_t)i,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070079 ((const uint8_t *)U1.ptr)[i],
80 ((const uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081 return 1;
82 }
83 }
84 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086}
87
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070088#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053089 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070091#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080094 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
95
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070096#endif
97
98
Laurence Lundblade59289e52019-12-30 13:44:37 -080099/*
100 Returns 0 if UsefulBufs are equal
101 Returns 1000000 + offeset if they are not equal.
Laurence Lundblade59289e52019-12-30 13:44:37 -0800102*/
103struct UBCompareDiagnostic {
104 uint8_t uActual;
105 uint8_t uExpected;
106 size_t uOffset;
107};
108
Laurence Lundbladeee851742020-01-08 08:37:05 -0800109static int32_t
110UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
111 UsefulBufC Expected,
112 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800113 size_t i;
114 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800115 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800116 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800117 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
118 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800119 pDiag->uOffset = i;
120 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800121 // Cast to int is OK as this is only a diagnostic and the sizes
122 // here are never over a few KB.
123 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800124 }
125 }
126 return 0;
127
128}
Laurence Lundblade59289e52019-12-30 13:44:37 -0800129
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700130
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530131// One big buffer that is used by all the tests to encode into
132// Putting it in uninitialized data is better than using a lot
133// of stack. The tests should run on small devices too.
134static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135
136
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800137
138/*
139 Some very minimal tests.
140 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300141int32_t BasicEncodeTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142{
143 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800144 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800145
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530146 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530147
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800148 QCBOREncode_OpenMap(&EC);
149 QCBOREncode_AddBoolToMapN(&EC, 66, true);
150 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
157
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800158 // Decode it and see that is right
159 QCBORDecodeContext DC;
160 QCBORItem Item;
161 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 QCBORDecode_GetNext(&DC, &Item);
164 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530165 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800166 }
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_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530174 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
177
Laurence Lundbladeee851742020-01-08 08:37:05 -0800178 // Make another encoded message with the CBOR from the previous
179 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530180 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800181 QCBOREncode_Init(&EC, MemoryForEncoded2);
182 QCBOREncode_OpenArray(&EC);
183 QCBOREncode_AddUInt64(&EC, 451);
184 QCBOREncode_AddEncoded(&EC, Encoded);
185 QCBOREncode_OpenMap(&EC);
186 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
187 QCBOREncode_CloseMap(&EC);
188 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700191 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530192 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800193 }
194 /*
195 [ // 0 1:3
196 451, // 1 1:2
197 { // 1 1:2 2:1
198 66: true // 2 1:1
199 },
200 { // 1 1:1 2:1
201 -70000: { // 2 1:1 2:1 3:1
202 66: true // 3 XXXXXX
203 }
204 }
205 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800209 83 # array(3)
210 19 01C3 # unsigned(451)
211 A1 # map(1)
212 18 42 # unsigned(66)
213 F5 # primitive(21)
214 A1 # map(1)
215 3A 0001116F # negative(69999)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800220
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221 // Decode it and see if it is OK
222 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800224 // 0 1:3
225 QCBORDecode_GetNext(&DC, &Item);
226 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530227 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 // 1 1:2
231 QCBORDecode_GetNext(&DC, &Item);
232 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530233 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800236 // 1 1:2 2:1
237 QCBORDecode_GetNext(&DC, &Item);
238 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530239 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800242 // 2 1:1
243 QCBORDecode_GetNext(&DC, &Item);
244 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530245 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800248 // 1 1:1 2:1
249 QCBORDecode_GetNext(&DC, &Item);
250 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530251 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800254 // 2 1:1 2:1 3:1
255 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800256 if(Item.uDataType != QCBOR_TYPE_MAP ||
257 Item.val.uCount != 1 ||
258 Item.uLabelType != QCBOR_TYPE_INT64 ||
259 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800263 // 3 XXXXXX
264 QCBORDecode_GetNext(&DC, &Item);
265 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530266 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800269 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530270 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800273 return 0;
274}
275
276
277
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530278static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade2d493002024-02-01 11:09:17 -0700279 0x98, 0x24, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530280 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
281 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
Laurence Lundblade2d493002024-02-01 11:09:17 -0700282 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0x20, 0xa4, 0x63, 0x4c, 0x42,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530283 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
284 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
285 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
286 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
287 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700288 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
290 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
291 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
292 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
293 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700294 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
295 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00,
296 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40,
297 0x66, 0x62,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530298 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
299 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
300 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700301 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530302 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
303 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
304 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
305 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
306 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
307 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
308 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
309 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
310 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
311 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
312 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
313 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
314 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
315 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700316 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
317 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530318 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
319 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
320 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
321 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
322 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
323 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
324 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
325 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
326 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
327 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
328 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
329 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
330 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
331 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
332 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
333 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
334 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
335 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
336 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
337 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
338 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
339 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
340 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
341 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
342 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
343 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
344 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
345 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
346 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
347 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
348 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
349 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
350 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
351 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
352 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
353 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
354 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
355 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
356 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
357 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
358 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
359 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
360 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
361 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
362 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
363 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
364 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
365 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
366 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
367 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
368 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
369 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
370 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
371 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
372 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
373 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
374 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
375 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700376 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530377 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
378 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
379 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
380 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
381 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
383 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
384 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
385 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
386 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
387 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
388 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
389 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
390 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
391 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
392 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
393 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
394 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
395 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
396 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
397 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
398 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
399 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
400 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
401 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
402 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
403 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
404 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
405 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
406 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
407 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
408 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
409 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
410 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700411 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530412 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
413 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
414 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
415 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
416 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
417 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
418 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
419 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
420 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
421 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
422 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
423 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
424 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
425 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
426 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
427 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
428 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
429 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
430 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
431 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
432 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
433 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
434 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
435 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
436 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
437 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
438 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
439 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
440 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
441 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
442 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
443 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
444 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
445 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
446 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
447 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
448 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
449 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
450 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
451 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
452 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
453 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
454 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
455 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
456 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
457 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
458 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
459 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
460 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
461 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
462 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
463 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
464 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
465 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
466 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
467 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
468 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
469 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
470 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
471 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
472 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
473 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
474 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
475 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
476 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
477 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
478 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
479 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
480 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
481 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
484 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
485 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487};
488
489
490static const char *szMIME = "\
491MIME-Version: 1.0\n\
492Content-Type: multipart/mixed;\n\
493boundary=\"XXXXboundary text\"\n\
494\n\
495This is a multipart message in MIME format.\n\
496\n\
497--XXXXboundary text\n\
498Content-Type: text/plain\n\
499\n\
500this is the body text\n\
501\n\
502--XXXXboundary text\n\
503Content-Type: text/plain;\n\
504Content-Disposition: attachment;\n\
505filename=\"test.txt\"\n\
506\n\
507this is the attachment text\n\
508\n\
509--XXXXboundary text--";
510
511
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700512static void AddAll(QCBOREncodeContext *pECtx)
513{
514 QCBOREncode_OpenArray(pECtx);
515
516 /* Some ints that are tagged and have strings preceeding them
517 * (not labels becase it is not a map) */
518 QCBOREncode_AddSZString(pECtx, "UINT62");
519 QCBOREncode_AddTag(pECtx, 100);
520 QCBOREncode_AddUInt64(pECtx, 89989909);
521 QCBOREncode_AddSZString(pECtx, "INT64");
522 QCBOREncode_AddTag(pECtx, 76);
523 QCBOREncode_AddInt64(pECtx, 77689989909);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700524 QCBOREncode_AddUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700525 QCBOREncode_AddInt64(pECtx, -44);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700526 QCBOREncode_AddNegativeUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700527
528 /* ints that go in maps */
529 QCBOREncode_OpenMap(pECtx);
530 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
531 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
532 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
533 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
534 QCBOREncode_CloseMap(pECtx);
535
536 /* Epoch Date */
537 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
538
539 /* Epoch date with labels */
540 QCBOREncode_OpenMap(pECtx);
541 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
542 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
543 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
544 QCBOREncode_CloseMap(pECtx);
545
546 /* Binary blobs */
547 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
548
549 /* binary blobs in maps */
550 QCBOREncode_OpenMap(pECtx);
551 QCBOREncode_AddSZString(pECtx, "binbin");
552 QCBOREncode_AddTag(pECtx, 100000);
553 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700554 QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700555 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
556 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
557 QCBOREncode_CloseMap(pECtx);
558
559 /* text blobs */
560 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
561 QCBOREncode_AddSZString(pECtx, "oof\n");
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700562 QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string
563
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700564 const char *szURL =
565 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
566 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
567 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
568 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
569 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
570
571 /* text blobs in maps */
572 QCBOREncode_OpenMap(pECtx);
573 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
574 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
575 QCBOREncode_AddSZString(pECtx, "()()()");
576 QCBOREncode_AddTag(pECtx, 1000);
577 QCBOREncode_AddSZString(pECtx, "rab rab oof");
578 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
579 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
580 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
581 QCBOREncode_AddURIToMap(pECtx,
582 "RFC",
583 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
584 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
585 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
586 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
587 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
588 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
589 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
590 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
591 QCBOREncode_CloseMap(pECtx);
592
593 /* Date strings */
594 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
595 QCBOREncode_OpenMap(pECtx);
596 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
597 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
598 QCBOREncode_CloseMap(pECtx);
599
600 /* true / false ... */
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700601 QCBOREncode_AddUndef(pECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700602 QCBOREncode_OpenMap(pECtx);
603 QCBOREncode_AddSZString(pECtx, "dare");
604 QCBOREncode_AddTag(pECtx, 66);
605 QCBOREncode_AddBool(pECtx, true);
606 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700607 QCBOREncode_AddNULLToMapN(pECtx, 737634);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700608 QCBOREncode_CloseMap(pECtx);
609
610 /* opening an array */
611 QCBOREncode_OpenArray(pECtx);
612 QCBOREncode_CloseArray(pECtx);
613
614 /* opening arrays in a map */
615 QCBOREncode_OpenMap(pECtx);
616 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
617 QCBOREncode_AddTag(pECtx, 1093);
618 QCBOREncode_OpenArray(pECtx);
619 QCBOREncode_CloseArray(pECtx);
620 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
621 QCBOREncode_CloseArray(pECtx);
622 QCBOREncode_OpenArrayInMapN(pECtx, 42);
623 QCBOREncode_CloseArray(pECtx);
624 QCBOREncode_CloseMap(pECtx);
625
626 /* opening maps with labels and tagging */
627 QCBOREncode_OpenMap(pECtx);
628 QCBOREncode_OpenMapInMap(pECtx, "in a map");
629 QCBOREncode_OpenMapInMapN(pECtx, 5556);
630 QCBOREncode_AddSZString(pECtx, "in a in a in a");
631 QCBOREncode_AddTag(pECtx, 9087);
632 QCBOREncode_OpenMap(pECtx);
633 QCBOREncode_CloseMap(pECtx);
634 QCBOREncode_CloseMap(pECtx);
635 QCBOREncode_CloseMap(pECtx);
636 QCBOREncode_CloseMap(pECtx);
637
638 /* Extended simple values (these are not standard...) */
639 QCBOREncode_OpenMap(pECtx);
640 QCBOREncode_AddSZString(pECtx, "s1");
641 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700642 QCBOREncode_Private_AddSimple(pECtx, 255);
643 QCBOREncode_Private_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700644 QCBOREncode_AddSZString(pECtx, "s3");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700646 QCBOREncode_Private_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700647 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
648 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700649 QCBOREncode_Private_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700650 QCBOREncode_AddInt64(pECtx, 89); // label before tag
651 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700652 QCBOREncode_Private_AddSimple(pECtx, 19);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700653 QCBOREncode_CloseMap(pECtx);
654
655 /* UUIDs */
656 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
657 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
658 0x49, 0x43, 0x41, 0x32};
659 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
660 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
661 QCBOREncode_OpenMap(pECtx);
662 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
663 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
664 QCBOREncode_CloseMap(pECtx);
665
666 /* Bool */
667 QCBOREncode_AddBool(pECtx, true);
668 QCBOREncode_AddBool(pECtx, false);
669 QCBOREncode_OpenMap(pECtx);
670 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
671 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
672 QCBOREncode_CloseMap(pECtx);
673
674 /* Big numbers */
675 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
676 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
677 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
678 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
679 QCBOREncode_OpenMap(pECtx);
680 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
681 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
682 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
683 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
684 QCBOREncode_CloseMap(pECtx);
685
686 QCBOREncode_CloseArray(pECtx);
687}
688
689
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300690int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700692 /* Improvement: this test should be broken down into several so it is more
693 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 QCBOREncodeContext ECtx;
695 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530697 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800698
Laurence Lundblade2d493002024-02-01 11:09:17 -0700699 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800700
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530701 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700702 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800703 nReturn = -1;
704 goto Done;
705 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800706
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700707 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530708 nReturn = -2;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700709 }
710
711
712 /* Also test size calculation */
713 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
714
715 AddAll (&ECtx);
716
717 size_t size;
718 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
719 nReturn = -10;
720 goto Done;
721 }
722
723 if(size != sizeof(spExpectedEncodedAll)) {
724 nReturn = -11;
725 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727Done:
728 return nReturn;
729}
730
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700731
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530732/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200733 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530734 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
735 3B 0000000100000000 # negative(4294967296)
736 3A FFFFFFFF # negative(4294967295)
737 3A FFFFFFFE # negative(4294967294)
738 3A FFFFFFFD # negative(4294967293)
739 3A 7FFFFFFF # negative(2147483647)
740 3A 7FFFFFFE # negative(2147483646)
741 3A 00010001 # negative(65537)
742 3A 00010000 # negative(65536)
743 39 FFFF # negative(65535)
744 39 FFFE # negative(65534)
745 39 FFFD # negative(65533)
746 39 0100 # negative(256)
747 38 FF # negative(255)
748 38 FE # negative(254)
749 38 FD # negative(253)
750 38 18 # negative(24)
751 37 # negative(23)
752 36 # negative(22)
753 20 # negative(0)
754 00 # unsigned(0)
755 00 # unsigned(0)
756 01 # unsigned(1)
757 16 # unsigned(22)
758 17 # unsigned(23)
759 18 18 # unsigned(24)
760 18 19 # unsigned(25)
761 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200762 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530763 18 FE # unsigned(254)
764 18 FF # unsigned(255)
765 19 0100 # unsigned(256)
766 19 0101 # unsigned(257)
767 19 FFFE # unsigned(65534)
768 19 FFFF # unsigned(65535)
769 1A 00010000 # unsigned(65536)
770 1A 00010001 # unsigned(65537)
771 1A 00010002 # unsigned(65538)
772 1A 7FFFFFFF # unsigned(2147483647)
773 1A 7FFFFFFF # unsigned(2147483647)
774 1A 80000000 # unsigned(2147483648)
775 1A 80000001 # unsigned(2147483649)
776 1A FFFFFFFE # unsigned(4294967294)
777 1A FFFFFFFF # unsigned(4294967295)
778 1B 0000000100000000 # unsigned(4294967296)
779 1B 0000000100000001 # unsigned(4294967297)
780 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
781 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
782 */
783static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200784 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800785 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
786 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
787 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
788 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
789 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
790 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
791 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
792 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
793 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
794 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200795 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
796 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
797 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
798 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
799 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
800 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
801 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
802 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
803 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
804 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
805 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
806 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
807 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800808
809/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800810
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 Test the generation of integers. This also ends up testing
812 encoding of all the different lengths. It encodes integers
813 of many lengths and values, especially around the boundaries
814 for different types of integers. It compares the output
815 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800816
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300818int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819{
820 QCBOREncodeContext ECtx;
821 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800822
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530823 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800824 QCBOREncode_OpenArray(&ECtx);
825
826 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
827 QCBOREncode_AddInt64(&ECtx, -4294967297);
828 QCBOREncode_AddInt64(&ECtx, -4294967296);
829 QCBOREncode_AddInt64(&ECtx, -4294967295);
830 QCBOREncode_AddInt64(&ECtx, -4294967294);
831 QCBOREncode_AddInt64(&ECtx, -2147483648);
832 QCBOREncode_AddInt64(&ECtx, -2147483647);
833 QCBOREncode_AddInt64(&ECtx, -65538);
834 QCBOREncode_AddInt64(&ECtx, -65537);
835 QCBOREncode_AddInt64(&ECtx, -65536);
836 QCBOREncode_AddInt64(&ECtx, -65535);
837 QCBOREncode_AddInt64(&ECtx, -65534);
838 QCBOREncode_AddInt64(&ECtx, -257);
839 QCBOREncode_AddInt64(&ECtx, -256);
840 QCBOREncode_AddInt64(&ECtx, -255);
841 QCBOREncode_AddInt64(&ECtx, -254);
842 QCBOREncode_AddInt64(&ECtx, -25);
843 QCBOREncode_AddInt64(&ECtx, -24);
844 QCBOREncode_AddInt64(&ECtx, -23);
845 QCBOREncode_AddInt64(&ECtx, -1);
846 QCBOREncode_AddInt64(&ECtx, 0);
847 QCBOREncode_AddUInt64(&ECtx, 0ULL);
848 QCBOREncode_AddInt64(&ECtx, 1);
849 QCBOREncode_AddInt64(&ECtx, 22);
850 QCBOREncode_AddInt64(&ECtx, 23);
851 QCBOREncode_AddInt64(&ECtx, 24);
852 QCBOREncode_AddInt64(&ECtx, 25);
853 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200854 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800855 QCBOREncode_AddInt64(&ECtx, 254);
856 QCBOREncode_AddInt64(&ECtx, 255);
857 QCBOREncode_AddInt64(&ECtx, 256);
858 QCBOREncode_AddInt64(&ECtx, 257);
859 QCBOREncode_AddInt64(&ECtx, 65534);
860 QCBOREncode_AddInt64(&ECtx, 65535);
861 QCBOREncode_AddInt64(&ECtx, 65536);
862 QCBOREncode_AddInt64(&ECtx, 65537);
863 QCBOREncode_AddInt64(&ECtx, 65538);
864 QCBOREncode_AddInt64(&ECtx, 2147483647);
865 QCBOREncode_AddInt64(&ECtx, 2147483647);
866 QCBOREncode_AddInt64(&ECtx, 2147483648);
867 QCBOREncode_AddInt64(&ECtx, 2147483649);
868 QCBOREncode_AddInt64(&ECtx, 4294967294);
869 QCBOREncode_AddInt64(&ECtx, 4294967295);
870 QCBOREncode_AddInt64(&ECtx, 4294967296);
871 QCBOREncode_AddInt64(&ECtx, 4294967297);
872 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
873 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800874
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800876
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530877 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700878 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800879 nReturn = -1;
880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530882 if(CheckResults(Enc, spExpectedEncodedInts))
883 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 return(nReturn);
886}
887
888
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530889/*
890 85 # array(5)
891 F5 # primitive(21)
892 F4 # primitive(20)
893 F6 # primitive(22)
894 F7 # primitive(23)
895 A1 # map(1)
896 65 # text(5)
897 554E446566 # "UNDef"
898 F7 # primitive(23)
899 */
900static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
902
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300903int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800904{
905 QCBOREncodeContext ECtx;
906 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800907
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530908 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800909 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900910
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700911 QCBOREncode_AddBool(&ECtx, true);
912 QCBOREncode_AddBool(&ECtx, false);
913 QCBOREncode_AddNULL(&ECtx);
914 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800915
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 QCBOREncode_OpenMap(&ECtx);
917
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700918 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 QCBOREncode_CloseMap(&ECtx);
920
921 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800922
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530923 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700924 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925 nReturn = -1;
926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800927
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530928 if(CheckResults(ECBOR, spExpectedEncodedSimple))
929 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800931 return(nReturn);
932}
933
Jan Jongboom47d86c52019-07-25 08:54:16 +0200934/*
935 9F # array(5)
936 F5 # primitive(21)
937 F4 # primitive(20)
938 F6 # primitive(22)
939 F7 # primitive(23)
940 BF # map(1)
941 65 # text(5)
942 554E446566 # "UNDef"
943 F7 # primitive(23)
944 FF # break
945 FF # break
946 */
947static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
948 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
949
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300950int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +0200951{
952 QCBOREncodeContext ECtx;
953 int nReturn = 0;
954
955 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
956 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
957
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700958 QCBOREncode_AddBool(&ECtx, true);
959 QCBOREncode_AddBool(&ECtx, false);
960 QCBOREncode_AddNULL(&ECtx);
961 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +0200962
963 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
964
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700965 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +0200966 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
967
968 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
969
970 UsefulBufC ECBOR;
971 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
972 nReturn = -1;
973 }
974
975 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
976 return -2;
977
978 return(nReturn);
979}
980
Jan Jongboom5d827882019-08-07 12:51:15 +0200981/*
982A5 # map(5)
983 63 # text(3)
984 617272 # "arr"
985 98 1F # array(31)
986 00 # unsigned(0)
987 01 # unsigned(1)
988 02 # unsigned(2)
989 03 # unsigned(3)
990 04 # unsigned(4)
991 05 # unsigned(5)
992 06 # unsigned(6)
993 07 # unsigned(7)
994 08 # unsigned(8)
995 09 # unsigned(9)
996 0A # unsigned(10)
997 0B # unsigned(11)
998 0C # unsigned(12)
999 0D # unsigned(13)
1000 0E # unsigned(14)
1001 0F # unsigned(15)
1002 10 # unsigned(16)
1003 11 # unsigned(17)
1004 12 # unsigned(18)
1005 13 # unsigned(19)
1006 14 # unsigned(20)
1007 15 # unsigned(21)
1008 16 # unsigned(22)
1009 17 # unsigned(23)
1010 18 18 # unsigned(24)
1011 18 19 # unsigned(25)
1012 18 1A # unsigned(26)
1013 18 1B # unsigned(27)
1014 18 1C # unsigned(28)
1015 18 1D # unsigned(29)
1016 18 1E # unsigned(30)
1017 63 # text(3)
1018 6D6170 # "map"
1019 B8 1F # map(31)
1020 61 # text(1)
1021 61 # "a"
1022 00 # unsigned(0)
1023 61 # text(1)
1024 62 # "b"
1025 01 # unsigned(1)
1026 61 # text(1)
1027 63 # "c"
1028 02 # unsigned(2)
1029 61 # text(1)
1030 64 # "d"
1031 03 # unsigned(3)
1032 61 # text(1)
1033 65 # "e"
1034 04 # unsigned(4)
1035 61 # text(1)
1036 66 # "f"
1037 05 # unsigned(5)
1038 61 # text(1)
1039 67 # "g"
1040 06 # unsigned(6)
1041 61 # text(1)
1042 68 # "h"
1043 07 # unsigned(7)
1044 61 # text(1)
1045 69 # "i"
1046 08 # unsigned(8)
1047 61 # text(1)
1048 6A # "j"
1049 09 # unsigned(9)
1050 61 # text(1)
1051 6B # "k"
1052 0A # unsigned(10)
1053 61 # text(1)
1054 6C # "l"
1055 0B # unsigned(11)
1056 61 # text(1)
1057 6D # "m"
1058 0C # unsigned(12)
1059 61 # text(1)
1060 6E # "n"
1061 0D # unsigned(13)
1062 61 # text(1)
1063 6F # "o"
1064 0E # unsigned(14)
1065 61 # text(1)
1066 70 # "p"
1067 0F # unsigned(15)
1068 61 # text(1)
1069 71 # "q"
1070 10 # unsigned(16)
1071 61 # text(1)
1072 72 # "r"
1073 11 # unsigned(17)
1074 61 # text(1)
1075 73 # "s"
1076 12 # unsigned(18)
1077 61 # text(1)
1078 74 # "t"
1079 13 # unsigned(19)
1080 61 # text(1)
1081 75 # "u"
1082 14 # unsigned(20)
1083 61 # text(1)
1084 76 # "v"
1085 15 # unsigned(21)
1086 61 # text(1)
1087 77 # "w"
1088 16 # unsigned(22)
1089 61 # text(1)
1090 78 # "x"
1091 17 # unsigned(23)
1092 61 # text(1)
1093 79 # "y"
1094 18 18 # unsigned(24)
1095 61 # text(1)
1096 7A # "z"
1097 18 19 # unsigned(25)
1098 61 # text(1)
1099 41 # "A"
1100 18 1A # unsigned(26)
1101 61 # text(1)
1102 42 # "B"
1103 18 1B # unsigned(27)
1104 61 # text(1)
1105 43 # "C"
1106 18 1C # unsigned(28)
1107 61 # text(1)
1108 44 # "D"
1109 18 1D # unsigned(29)
1110 61 # text(1)
1111 45 # "E"
1112 18 1E # unsigned(30)
1113 65 # text(5)
1114 6D696E3331 # "min31"
1115 38 1E # negative(30)
1116 66 # text(6)
1117 706C75733331 # "plus31"
1118 18 1F # unsigned(31)
1119 63 # text(3)
1120 737472 # "str"
1121 78 1F # text(31)
1122 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1123 */
1124static const uint8_t EncodeLengthThirtyone[] = {
1125 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1126 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1127 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1128 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1129 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1130 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1131 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1132 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1133 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1134 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1135 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1136 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1137 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1138 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1139 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1140 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1141 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1142 0x31
1143};
1144
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001145int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001146{
1147 QCBOREncodeContext ECtx;
1148 int nReturn = 0;
1149
1150 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1151 QCBOREncode_OpenMap(&ECtx);
1152
1153 // add array with 31 items
1154 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1155 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001156 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001157 }
1158 QCBOREncode_CloseArray(&ECtx);
1159
1160 // add map with 31 items
1161 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001162 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001163 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001164 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001165 if (ix < 26) c = c + ix;
1166 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001167 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001168 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1169 }
1170 QCBOREncode_CloseMap(&ECtx);
1171
1172 // add -31 and +31
1173 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1174 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1175
1176 // add string with length 31
1177 const char *str = "testtesttesttesttesttestqcbor11";
1178 UsefulBufC str_b = { str, 31 };
1179 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1180
1181 QCBOREncode_CloseMap(&ECtx);
1182
1183 UsefulBufC ECBOR;
1184 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1185 nReturn = -1;
1186 }
1187
1188 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1189 return -2;
1190
1191 return(nReturn);
1192}
1193
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301194
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301195/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001196 * [ "2013-03-21T20:04:00Z",
1197 * 0("2013-03-21T20:04:00Z"),
1198 * 1363896240,
1199 * 1(1363896240),
1200 * 100(-10676),
1201 * 3994,
1202 * 1004("1940-10-09"),
1203 * "1980-12-08",
1204 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1205 * "SD": 1(999),
1206 * "Sample Date from RFC 8943": "1985-04-12",
1207 * 42: 1004("1985-04-12T23:20:50.52Z"),
1208 * "SY": 100(-10676),
1209 * 45: 3994
1210 * }
1211 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301212 */
1213static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001214 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1215 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1216 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1217 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1218 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1219 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1220 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1221 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1222 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1223 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1224 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1225 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1226 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1227 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1228 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1229 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1230 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1231 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1232 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1233 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1234 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1235 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1236 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001238int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001239{
1240 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001241
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301242 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244 QCBOREncode_OpenArray(&ECtx);
1245
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001246 /* The values are taken from the CBOR RFCs */
1247 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1248 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1249 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1250 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1251 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1252 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1253 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1254 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001256 QCBOREncode_OpenMap(&ECtx);
1257
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001258 QCBOREncode_AddDateStringToMap(&ECtx,
1259 "Sample Date from RFC 3339",
1260 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001262 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1263 "Sample Date from RFC 8943",
1264 QCBOR_ENCODE_AS_BORROWED,
1265 "1985-04-12");
1266 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1267 42,
1268 QCBOR_ENCODE_AS_TAG,
1269 "1985-04-12T23:20:50.52Z");
1270 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1271 "SY",
1272 QCBOR_ENCODE_AS_TAG,
1273 -10676);
1274 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1275 45,
1276 QCBOR_ENCODE_AS_BORROWED,
1277 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001278
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001279 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 QCBOREncode_CloseArray(&ECtx);
1282
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301283 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001284 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001285 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001287
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301288 if(CheckResults(ECBOR, spExpectedEncodedDates))
1289 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001290
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001291 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001292}
1293
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301294
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001295int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296{
1297 QCBOREncodeContext ECtx;
1298 int i;
1299 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001300
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301301 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1303 QCBOREncode_OpenArray(&ECtx);
1304 }
1305 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1306 QCBOREncode_CloseArray(&ECtx);
1307 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001308 UsefulBufC Encoded;
1309 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001310 nReturn = -1;
1311 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312
1313 return(nReturn);
1314}
1315
1316
1317
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001318int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319{
1320 QCBOREncodeContext ECtx;
1321 int i;
1322 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001323
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301324 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001325 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1326 QCBOREncode_OpenArray(&ECtx);
1327 }
1328 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1329 QCBOREncode_CloseArray(&ECtx);
1330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001331
Laurence Lundblade0595e932018-11-02 22:22:47 +07001332 UsefulBufC Encoded;
1333 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 nReturn = -1;
1335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001337 return(nReturn);
1338}
1339
1340
1341
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001342int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001343{
1344 QCBOREncodeContext ECtx;
1345 int i;
1346 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301348 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1350 QCBOREncode_OpenArray(&ECtx);
1351 }
1352 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1353 QCBOREncode_CloseArray(&ECtx);
1354 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001355 UsefulBufC Encoded;
1356 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357 nReturn = -1;
1358 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001360 return(nReturn);
1361}
1362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301364/*
1365 81 # array(1)
1366 81 # array(1)
1367 81 # array(1)
1368 81 # array(1)
1369 80 # array(0)
1370*/
1371static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001373// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301374/*
1375 82 # array(2)
1376 81 # array(1)
1377 81 # array(1)
1378 81 # array(1)
1379 81 # array(1)
1380 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001381 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301382 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1383 3B 0000000100000000 # negative(4294967296)
1384 3A FFFFFFFF # negative(4294967295)
1385 3A FFFFFFFE # negative(4294967294)
1386 3A FFFFFFFD # negative(4294967293)
1387 3A 7FFFFFFF # negative(2147483647)
1388 3A 7FFFFFFE # negative(2147483646)
1389 3A 00010001 # negative(65537)
1390 3A 00010000 # negative(65536)
1391 39 FFFF # negative(65535)
1392 39 FFFE # negative(65534)
1393 39 FFFD # negative(65533)
1394 39 0100 # negative(256)
1395 38 FF # negative(255)
1396 38 FE # negative(254)
1397 38 FD # negative(253)
1398 38 18 # negative(24)
1399 37 # negative(23)
1400 36 # negative(22)
1401 20 # negative(0)
1402 00 # unsigned(0)
1403 00 # unsigned(0)
1404 01 # unsigned(1)
1405 16 # unsigned(22)
1406 17 # unsigned(23)
1407 18 18 # unsigned(24)
1408 18 19 # unsigned(25)
1409 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001410 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301411 18 FE # unsigned(254)
1412 18 FF # unsigned(255)
1413 19 0100 # unsigned(256)
1414 19 0101 # unsigned(257)
1415 19 FFFE # unsigned(65534)
1416 19 FFFF # unsigned(65535)
1417 1A 00010000 # unsigned(65536)
1418 1A 00010001 # unsigned(65537)
1419 1A 00010002 # unsigned(65538)
1420 1A 7FFFFFFF # unsigned(2147483647)
1421 1A 7FFFFFFF # unsigned(2147483647)
1422 1A 80000000 # unsigned(2147483648)
1423 1A 80000001 # unsigned(2147483649)
1424 1A FFFFFFFE # unsigned(4294967294)
1425 1A FFFFFFFF # unsigned(4294967295)
1426 1B 0000000100000000 # unsigned(4294967296)
1427 1B 0000000100000001 # unsigned(4294967297)
1428 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1429 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1430 */
1431static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001432 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1434 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1435 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1436 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1437 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1438 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1439 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1440 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1441 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1442 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1443 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001444 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1445 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1446 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1447 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1448 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1449 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1450 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1451 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1452 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1453 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1454 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1455 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1456 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457
1458
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001459int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001461 QCBOREncodeContext ECtx;
1462
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301463 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001464 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301465 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1466 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001467 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001469 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001470
Laurence Lundblade0595e932018-11-02 22:22:47 +07001471 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001472 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001474
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301475 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001476 return -5;
1477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001479 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001480}
1481
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301482/*
1483 This returns a pointer to spBigBuf
1484 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001485static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486{
1487 QCBOREncodeContext ECtx;
1488 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001489
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 *pEncoded = NULL;
1491 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301492 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001493
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 // loop runs CBOR encoding twice. First with no buffer to
1495 // calucate the length so buffer can be allocated correctly,
1496 // and last with the buffer to do the actual encoding
1497 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301498 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001499 QCBOREncode_OpenMap(&ECtx);
1500 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1501 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1502 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1503 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1504 QCBOREncode_CloseArray(&ECtx);
1505 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1506 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1507 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1508 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1509 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1510 QCBOREncode_CloseMap(&ECtx);
1511 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001512
Laurence Lundblade0595e932018-11-02 22:22:47 +07001513 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 goto Done;
1515 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301516 if(uFirstSizeEstimate != *pEncodedLen) {
1517 nReturn = 1;
1518 } else {
1519 nReturn = 0;
1520 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521 goto Done;
1522 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301523 *pEncoded = spBigBuf;
1524 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528 Done:
1529 return(nReturn);
1530}
1531
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301532/*
1533 A3 # map(3)
1534 6D # text(13)
1535 666972737420696E7465676572 # "first integer"
1536 18 2A # unsigned(42)
1537 77 # text(23)
1538 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1539 82 # array(2)
1540 67 # text(7)
1541 737472696E6731 # "string1"
1542 67 # text(7)
1543 737472696E6732 # "string2"
1544 6C # text(12)
1545 6D617020696E2061206D6170 # "map in a map"
1546 A4 # map(4)
1547 67 # text(7)
1548 62797465732031 # "bytes 1"
1549 44 # bytes(4)
1550 78787878 # "xxxx"
1551 67 # text(7)
1552 62797465732032 # "bytes 2"
1553 44 # bytes(4)
1554 79797979 # "yyyy"
1555 6B # text(11)
1556 616E6F7468657220696E74 # "another int"
1557 18 62 # unsigned(98)
1558 66 # text(6)
1559 746578742032 # "text 2"
1560 78 1E # text(30)
1561 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1562 */
1563static const uint8_t spValidMapEncoded[] = {
1564 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1565 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1566 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1567 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1568 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1569 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1570 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1571 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1572 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1573 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1574 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1575 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1576 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1577 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1578 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1579 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580
1581
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001582int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583{
1584 uint8_t *pEncodedMaps;
1585 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301588 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301592 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1593 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001595 return(nReturn);
1596}
1597
1598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599/*
1600 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001601
Laurence Lundbladeee851742020-01-08 08:37:05 -08001602 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1603 CBOR_SIMPLEV_NULL
1604 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605 @param[in] szAlexString Diagnostic code.
1606 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001607 @param[in/out] pnLen Size of pOut buffer when called; length of data
1608 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001610 @return
1611 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613 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 -08001614 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001615 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1618 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 */
1621
Laurence Lundbladeee851742020-01-08 08:37:05 -08001622static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001623FormatRTICResults(uint8_t uRResult,
1624 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001625 const char *szType,
1626 const char *szAlexString,
1627 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628{
1629 // Buffer that the result will be written in to
1630 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001631 // QCBOREncode will never write off the end of this buffer. If it won't
1632 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001633
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001634 // Context for the encoder
1635 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301636 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1639 // Contents are label / value pairs
1640 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001642 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001643
Laurence Lundbladeee851742020-01-08 08:37:05 -08001644 // The result: 0 if scan happened and found nothing; 1 if it happened and
1645 // found something wrong; 2 if it didn't happen
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001646 QCBOREncode_Private_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001647
1648 // Add the diagnostic code
1649 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1650
1651 // Add a time stamp
1652 if(time) {
1653 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1654 }
1655
1656 // Add the diagnostic code
1657 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1658
1659 // Open a subordinate map for telemtry data
1660 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1661
1662 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001663
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001664 // Add a few fake integers and buffers for now.
1665 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1666
1667 // Add a few fake integers and buffers for now.
1668 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1669
1670 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301671 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001672 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1675 }
1676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678 // Close the telemetry map
1679 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001681 // Close the map
1682 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001683
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301684 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001685
Laurence Lundblade0595e932018-11-02 22:22:47 +07001686 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001687
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301688 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001689}
1690
1691
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301692/*
1693 A5 # map(5)
1694 69 # text(9)
1695 696E74656772697479 # "integrity"
1696 F4 # primitive(20)
1697 64 # text(4)
1698 74797065 # "type"
1699 66 # text(6)
1700 726563656E74 # "recent"
1701 64 # text(4)
1702 74696D65 # "time"
1703 C1 # tag(1)
1704 1A 580D4172 # unsigned(1477263730)
1705 64 # text(4)
1706 64696167 # "diag"
1707 6A # text(10)
1708 30784131654335303031 # "0xA1eC5001"
1709 69 # text(9)
1710 74656C656D65747279 # "telemetry"
1711 A3 # map(3)
1712 69 # text(9)
1713 53686F652053697A65 # "Shoe Size"
1714 0C # unsigned(12)
1715 62 # text(2)
1716 4951 # "IQ"
1717 1A FFFFFFFF # unsigned(4294967295)
1718 77 # text(23)
1719 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1720 48 # bytes(8)
1721 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1722 */
1723static const uint8_t spExpectedRTIC[] = {
1724 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1725 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1726 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1727 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1728 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1729 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1730 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1731 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1732 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1733 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1734 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1735 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001736
1737
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001738int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001739{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001740 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301741 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301742 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301743 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001744 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301745 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001746
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301747 if(CheckResults(Encoded, spExpectedRTIC)) {
1748 return -2;
1749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001750
1751 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001752}
1753
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301754
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301755/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001756 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001757
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301758 82 # array(2)
1759 19 01C3 # unsigned(451)
1760 43 # bytes(3)
1761 1901D2 # "\x19\x01\xD2"
1762*/
1763static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301764
Laurence Lundblade684aec22018-10-12 19:33:53 +08001765/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001766 81 #array(1)
1767 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1768 */
1769static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1770
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001771static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1772
Laurence Lundbladeda532272019-04-07 11:40:17 -07001773/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001774 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001775 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001776int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001777{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001778 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001779
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001780 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301781 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001782
Laurence Lundblade684aec22018-10-12 19:33:53 +08001783 QCBOREncode_OpenArray(&EC);
1784 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001785
Laurence Lundblade684aec22018-10-12 19:33:53 +08001786 QCBOREncode_BstrWrap(&EC);
1787 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001788
Laurence Lundblade684aec22018-10-12 19:33:53 +08001789 UsefulBufC Wrapped;
1790 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001791
Laurence Lundblade684aec22018-10-12 19:33:53 +08001792 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001793
Laurence Lundblade684aec22018-10-12 19:33:53 +08001794 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001795 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001796 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001798
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301799 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001800 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001801 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001802
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001803 // Second test - see if the length of the wrapped
1804 // bstr is correct. Also tests bstr wrapping
1805 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001806 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1807 QCBOREncode_OpenArray(&EC);
1808 QCBOREncode_BstrWrap(&EC);
1809 QCBOREncode_OpenArray(&EC);
1810 QCBOREncode_AddNULL(&EC);
1811 QCBOREncode_CloseArray(&EC);
1812 UsefulBufC BStr;
1813 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001814 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1815 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001816 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001817 return -5;
1818 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001819
Laurence Lundbladeda532272019-04-07 11:40:17 -07001820 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1821 // bstr wrapping use cases.
1822 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1823 QCBOREncode_Init(&EC, StuffBuf);
1824 QCBOREncode_OpenArray(&EC);
1825 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1826 QCBOREncode_CloseArray(&EC);
1827 if(QCBOREncode_Finish(&EC, &Encoded)) {
1828 return -6;
1829 }
1830 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1831 return -7;
1832 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001833
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001834 // Fourth test, cancelling a byte string
1835 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1836
1837 QCBOREncode_OpenArray(&EC);
1838 QCBOREncode_AddUInt64(&EC, 451);
1839
1840 QCBOREncode_BstrWrap(&EC);
1841 QCBOREncode_CancelBstrWrap(&EC);
1842
1843
1844 QCBOREncode_AddUInt64(&EC, 42);
1845 QCBOREncode_CloseArray(&EC);
1846 if(QCBOREncode_Finish(&EC, &Encoded)) {
1847 return -8;
1848 }
1849 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1850 return -9;
1851 }
1852
1853 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001854 // Fifth test, failed cancelling
1855 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1856
1857 QCBOREncode_OpenArray(&EC);
1858 QCBOREncode_AddUInt64(&EC, 451);
1859
1860 QCBOREncode_BstrWrap(&EC);
1861 QCBOREncode_AddUInt64(&EC, 99);
1862 QCBOREncode_CancelBstrWrap(&EC);
1863
1864 QCBOREncode_AddUInt64(&EC, 42);
1865 QCBOREncode_CloseArray(&EC);
1866 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001867#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001868 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1869 return -10;
1870 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001871#else
1872 if(uErr != QCBOR_SUCCESS) {
1873 return -110;
1874 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001875#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1876
1877 // Sixth test, another cancel, but the error is not caught
1878 // This use will produce unintended CBOR. The error
1879 // is not caught because it would require tracking state
1880 // for QCBOREncode_BstrWrapInMapN.
1881 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1882
1883 QCBOREncode_OpenMap(&EC);
1884 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1885
1886 QCBOREncode_BstrWrapInMapN(&EC, 55);
1887 QCBOREncode_CancelBstrWrap(&EC);
1888
1889 QCBOREncode_CloseMap(&EC);
1890 uErr = QCBOREncode_Finish(&EC, &Encoded);
1891 if(uErr != QCBOR_SUCCESS) {
1892 return -11;
1893 }
1894
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001895 return 0;
1896}
1897
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001898
1899
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001900int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001901{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001902 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001903 UsefulBufC Wrapped;
1904 UsefulBufC Encoded2;
1905 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001906
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001907 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001908
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301909 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001910
Laurence Lundblade684aec22018-10-12 19:33:53 +08001911 QCBOREncode_OpenArray(&EC);
1912 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001913
Laurence Lundblade684aec22018-10-12 19:33:53 +08001914 QCBOREncode_BstrWrap(&EC);
1915 QCBOREncode_AddUInt64(&EC, 466);
1916 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001917
Laurence Lundblade684aec22018-10-12 19:33:53 +08001918 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001919
Laurence Lundblade684aec22018-10-12 19:33:53 +08001920 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001921
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001922 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001923#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001924 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001925 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001926 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001927#else
1928 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1929 * is set and not to be sure to excerice all the relavant code in
1930 * both conditions. When the guards are disabled, there is no
1931 * error returned, but the code path is still covered.
1932 */
1933 if(uError != QCBOR_SUCCESS) {
1934 return (int32_t)(600 + uError);
1935 }
1936#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001937
Laurence Lundbladeee851742020-01-08 08:37:05 -08001938 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301939 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001940 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001941 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001942#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001943 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001944 return (int32_t)(700 + uError);
1945 }
1946#else
1947 if(uError != QCBOR_SUCCESS) {
1948 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001949 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001950#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001951
Laurence Lundblade684aec22018-10-12 19:33:53 +08001952 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301953 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001954 for(int i = 1; i < 18; i++) {
1955 QCBOREncode_BstrWrap(&EC);
1956 }
1957 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001958
Laurence Lundblade684aec22018-10-12 19:33:53 +08001959 for(int i = 1; i < 18; i++) {
1960 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1961 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001962
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001963 uError = QCBOREncode_Finish(&EC, &Encoded2);
1964 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001965 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001966 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001967
Laurence Lundblade684aec22018-10-12 19:33:53 +08001968 return 0;
1969}
1970
1971
Laurence Lundblade684aec22018-10-12 19:33:53 +08001972/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001973 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001974
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001975 [
1976 h'82004E82014B8202488203458204428105',
1977 {
1978 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1979 8426568656C6C6F18234BA2131318436568656C6C6F'
1980 }
1981 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001982
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001983 Unwrapping the first byte string in the above gives
1984 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301985
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001986 Unwrapping again, the byte string immediately above gives
1987 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301988
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001989 ...
1990
1991 Unrapping the second byte string in the top-level CBOR above gives
1992 {16: 16,
1993 64: "hello",
1994 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1995 }
1996
1997 Unwrapping again, the byte string immediately above gives
1998 {17: 17,
1999 65: "hello",
2000 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2001 }
2002
2003 ...
2004
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302005 */
2006static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002007{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002008 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2009 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2010 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2011 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2012 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2013 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2014 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2015 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2016 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2017 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002018};
2019
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002020
2021/*
2022 Get an int64 out of the decoder or fail.
2023 */
2024static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002025{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002026 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002027 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002028
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002029 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002030 if(nReturn) {
2031 return nReturn;
2032 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002033 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002034 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002036
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002037 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002038 return 0;
2039}
2040
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002041/*
2042 Get an array out of the decoder or fail.
2043 */
2044static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002045{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002046 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002047 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002048
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002049 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002050 if(nReturn) {
2051 return nReturn;
2052 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002053 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2054 return -1;
2055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002056
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002057 *pInt = Item.val.uCount;
2058 return 0;
2059}
2060
2061/*
2062 Get a map out of the decoder or fail.
2063 */
2064static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2065{
2066 QCBORItem Item;
2067 int32_t nReturn;
2068
2069 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002070 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002071 return nReturn;
2072 }
2073 if(Item.uDataType != QCBOR_TYPE_MAP) {
2074 return -1;
2075 }
2076
2077 *pInt = Item.val.uCount;
2078 return 0;
2079}
2080
2081/*
2082 Get a byte string out of the decoder or fail.
2083 */
2084static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2085{
2086 QCBORItem Item;
2087 int32_t nReturn;
2088
2089 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2090 if(nReturn) {
2091 return nReturn;
2092 }
2093 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002094 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002095 }
2096
2097 *pBstr = Item.val.string;
2098 return 0;
2099}
2100
2101/*
2102 Get a byte string out of the decoder or fail.
2103 */
2104static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2105{
2106 QCBORItem Item;
2107 int nReturn;
2108
2109 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2110 if(nReturn) {
2111 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002112 }
2113 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002114 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002115 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002116
2117 *pTstr = Item.val.string;
2118 return 0;
2119}
2120
2121
2122/*
2123 Recursively decode array containing a little CBOR and a bstr wrapped array
2124 with a little CBOR and a bstr wrapped array...
2125
2126 Part of bstr_wrap_nest_test.
2127 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2128{
2129 int64_t nInt;
2130 UsefulBufC Bstr;
2131 uint16_t nArrayCount;
2132 QCBORDecodeContext DC;
2133 int32_t nResult;
2134
2135 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2136
2137 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2138 return -10;
2139 }
2140
2141 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002142 return -11;
2143 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002144
2145 nResult = GetByteString(&DC, &Bstr);
2146 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2147 if(nArrayCount != 1) {
2148 return -12;
2149 } else {
2150 // successful exit
2151 return 0;
2152 }
2153 }
2154 if(nResult) {
2155 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002157
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002158 // tail recursion; good compilers will reuse the stack frame
2159 return DecodeNextNested(Bstr);
2160}
2161
2162
2163/*
2164 Recursively decode map containing a little CBOR and a bstr wrapped map
2165 with a little CBOR and a bstr wrapped map...
2166
2167 Part of bstr_wrap_nest_test.
2168 */
2169static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2170{
2171 int32_t nResult;
2172 uint16_t nMapCount;
2173 int64_t nInt;
2174 UsefulBufC Bstr;
2175 QCBORDecodeContext DC;
2176
2177 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2178
2179 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2180 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002182
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002183 if(GetInt64(&DC, &nInt)) {
2184 return -21;
2185 }
2186
2187 // The "hello"
2188 if(GetTextString(&DC, &Bstr)) {
2189 return -22;
2190 }
2191
2192 nResult = GetByteString(&DC, &Bstr);
2193 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2194 if(nMapCount == 2) {
2195 // successful exit
2196 return 0;
2197 } else {
2198 return -23;
2199 }
2200 }
2201
2202 if(nResult) {
2203 return -24;
2204 }
2205
2206 // tail recursion; good compilers will reuse the stack frame
2207 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002208}
2209
2210
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002211int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002212{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002213 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302214 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002215
Laurence Lundblade684aec22018-10-12 19:33:53 +08002216 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002217 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002218
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002219 QCBOREncode_OpenArray(&EC);
2220
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002221 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002222 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002223 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002224 QCBOREncode_AddInt64(&EC, i);
2225 }
2226 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2227 QCBOREncode_CloseArray(&EC);
2228 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002231 QCBOREncode_OpenMap(&EC);
2232 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2233 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2234 QCBOREncode_OpenMap(&EC);
2235 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002236 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002237 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002238
2239 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2240 QCBOREncode_CloseMap(&EC);
2241 QCBOREncode_CloseBstrWrap(&EC, NULL);
2242 }
2243 QCBOREncode_CloseMap(&EC);
2244
Laurence Lundblade684aec22018-10-12 19:33:53 +08002245 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundblade684aec22018-10-12 19:33:53 +08002247 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002248 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002249 return -1;
2250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
Laurence Lundblade684aec22018-10-12 19:33:53 +08002252 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302253 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002254 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundblade684aec22018-10-12 19:33:53 +08002257 // ---- Decode it and see if it is OK ------
2258 QCBORDecodeContext DC;
2259 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002261 UsefulBufC Bstr;
2262 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002263
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002264 // Array surrounding the the whole thing
2265 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002266 return -3;
2267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002269 // Get the byte string wrapping some array stuff
2270 if(GetByteString(&DC, &Bstr)) {
2271 return -4;
2272 }
2273
2274 // Decode the wrapped nested structure
2275 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002276 if(nReturn) {
2277 return nReturn;
2278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002279
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002280 // A map enclosing some map-oriented bstr wraps
2281 if(GetMap(&DC, &nArrayCount)) {
2282 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002283 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002284
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002285 // Get the byte string wrapping some array stuff
2286 if(GetByteString(&DC, &Bstr)) {
2287 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002289
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002290 // Decode the wrapped nested structure
2291 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002292 if(nReturn) {
2293 return nReturn;
2294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade684aec22018-10-12 19:33:53 +08002296 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002297 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002298 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002299
Laurence Lundblade684aec22018-10-12 19:33:53 +08002300 return 0;
2301}
2302
2303
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002304static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302305 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2306 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2307 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2308 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2309 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2310 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2311 0x45, 0xca, 0xcb, 0x36};
2312
2313/*
2314 D2 # tag(18)
2315 84 # array(4)
2316 43 # bytes(3)
2317 A10126 # "\xA1\x01&"
2318 A1 # map(1)
2319 04 # unsigned(4)
2320 42 # bytes(2)
2321 3131 # "11"
2322 54 # bytes(20)
2323 546869732069732074686520636F6E74656E742E # "This is the content."
2324 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002325 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2326 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2327 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2328 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2329 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2330 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2331 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2332 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302333 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002334static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302335 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2336 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2337 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2338 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2339 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2340 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2341 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2342 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2343 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2344 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2345
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002346static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2347
2348
Laurence Lundblade684aec22018-10-12 19:33:53 +08002349/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002350 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002351 C.2.1. This doesn't actually verify the signature (however
2352 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002353 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002354int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002355{
2356 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002357 const char *szKid = "11";
2358 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2359 const char *szPayload = "This is the content.";
2360 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2361 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2362 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002363
Laurence Lundblade684aec22018-10-12 19:33:53 +08002364 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002365
2366 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302367 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002368
Laurence Lundblade684aec22018-10-12 19:33:53 +08002369 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002370 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002371 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002372
Laurence Lundblade684aec22018-10-12 19:33:53 +08002373 // Add protected headers
2374 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002375
Laurence Lundblade684aec22018-10-12 19:33:53 +08002376 // Empty map with unprotected headers
2377 QCBOREncode_OpenMap(&EC);
2378 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2379 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundblade684aec22018-10-12 19:33:53 +08002381 // The payload
2382 UsefulBufC WrappedPayload;
2383 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002384 // Payload is not actually CBOR in example C.2.1 like it would be
2385 // for a CWT or EAT. It is just a text string.
2386 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002387 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002388
Laurence Lundblade684aec22018-10-12 19:33:53 +08002389 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002390 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2391 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002392 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002393 return -1;
2394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002396/* if(UsefulBuf_Compare(WrappedPayload,
2397 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2398 return -1;
2399 } */
2400
Laurence Lundblade684aec22018-10-12 19:33:53 +08002401 // The signature
2402 QCBOREncode_AddBytes(&EC, Signature);
2403 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002404
Laurence Lundblade684aec22018-10-12 19:33:53 +08002405 // Finish and check the results
2406 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002407 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002408 return -2;
2409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002410
Laurence Lundblade684aec22018-10-12 19:33:53 +08002411 // 98 is the size from RFC 8152 C.2.1
2412 if(COSE_Sign1.len != 98) {
2413 return -3;
2414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002415
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002416 // It would be good to compare this to the output from a COSE
2417 // implementation like COSE-C. This has been checked against the
2418 // CBOR playground.
2419 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002420 return -4;
2421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002422
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002423
2424 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2425 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2426
2427 // top level array for cose sign1, 18 is the tag for COSE sign
2428 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2429 QCBOREncode_OpenArray(&EC);
2430
2431 // Add protected headers
2432 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2433
2434 // Empty map with unprotected headers
2435 QCBOREncode_OpenMap(&EC);
2436 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2437 QCBOREncode_CloseMap(&EC);
2438
2439 // The payload
2440 QCBOREncode_BstrWrap(&EC);
2441 // Payload is not actually CBOR in example C.2.1 like it would be
2442 // for a CWT or EAT. It is just a text string.
2443 QCBOREncode_AddEncoded(&EC, Payload);
2444 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2445
2446 // Check we got back the actual payload expected
2447 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2448 if(UsefulBuf_Compare(WrappedPayload,
2449 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2450 return -11;
2451 }
2452
2453 // The signature
2454 QCBOREncode_AddBytes(&EC, Signature);
2455 QCBOREncode_CloseArray(&EC);
2456
2457 // Finish and check the results
2458 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2459 return -12;
2460 }
2461
2462 // 98 is the size from RFC 8152 C.2.1
2463 if(COSE_Sign1.len != 98) {
2464 return -13;
2465 }
2466
2467 // It would be good to compare this to the output from a COSE
2468 // implementation like COSE-C. This has been checked against the
2469 // CBOR playground.
2470 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2471 return -14;
2472 }
2473
Laurence Lundblade684aec22018-10-12 19:33:53 +08002474 return 0;
2475}
2476
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002477
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002478int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002479{
2480 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002481 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002482
2483
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002484 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002485 // Do all of these tests with NULL buffers so no actual
2486 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002487 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002488
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002489 // First verify no error from a big buffer
2490 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002491 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002492 // 6 is the CBOR overhead for opening the array and encodng the length
2493 // This exactly fills the buffer.
2494 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002495 QCBOREncode_CloseArray(&EC);
2496 size_t xx;
2497 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2498 return -1;
2499 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002500
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002501 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002502 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002503 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002504 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002505 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002506 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002507 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2508 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002509 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002510 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002511 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002512 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2513 return -2;
2514 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002515
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002516 // Third, fit an array in exactly at max position allowed
2517 QCBOREncode_Init(&EC, Buffer);
2518 QCBOREncode_OpenArray(&EC);
2519 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2520 QCBOREncode_OpenArray(&EC);
2521 QCBOREncode_CloseArray(&EC);
2522 QCBOREncode_CloseArray(&EC);
2523 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2524 return -10;
2525 }
2526
2527
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002528 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2529 // Work close to the 4GB size limit for a better test
2530 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002531 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002532
2533 QCBOREncode_Init(&EC, Large);
2534 QCBOREncode_OpenArray(&EC);
2535 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2536 QCBOREncode_CloseArray(&EC);
2537 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2538 // Making sure it succeeds when it should first
2539 return -3;
2540 }
2541
2542 QCBOREncode_Init(&EC, Large);
2543 QCBOREncode_OpenArray(&EC);
2544 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2545 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2546 QCBOREncode_CloseArray(&EC);
2547 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2548 // Now just 1 byte over, see that it fails
2549 return -4;
2550 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002551
2552
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002553 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2554 QCBOREncode_Init(&EC, Large);
2555 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2556 QCBOREncode_OpenArray(&EC);
2557 }
2558 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2559 QCBOREncode_CloseArray(&EC);
2560 }
2561 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2562 // Making sure it succeeds when it should first
2563 return -5;
2564 }
2565
2566 QCBOREncode_Init(&EC, Large);
2567 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2568 QCBOREncode_OpenArray(&EC);
2569 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002570 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002571 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2572 QCBOREncode_CloseArray(&EC);
2573 }
2574 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002575 return -6;
2576 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002577
2578
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002579 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002580 QCBOREncode_Init(&EC, Large);
2581 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2582 QCBOREncode_OpenArray(&EC);
2583 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002584 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002585 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2586 QCBOREncode_CloseArray(&EC);
2587 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002588 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2589#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2590 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002591 return -7;
2592 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002593#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2594 if(uErr != QCBOR_SUCCESS) {
2595 return -107;
2596 }
2597#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002598
2599
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002600 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002601 QCBOREncode_Init(&EC, Large);
2602 QCBOREncode_OpenArray(&EC);
2603 UsefulBufC Wrap;
2604 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002605 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2606#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2607 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002608 return -8;
2609 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002610#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2611 if(uErr != QCBOR_SUCCESS) {
2612 return -108;
2613 }
2614#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002615
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002616 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002617 QCBOREncode_Init(&EC, Large);
2618 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2619 QCBOREncode_OpenArray(&EC);
2620 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002621 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002622 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2623 QCBOREncode_CloseArray(&EC);
2624 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002625
2626 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2627#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2628 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002629 return -9;
2630 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002631#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2632 if(uErr != QCBOR_SUCCESS) {
2633 return -109;
2634 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002635#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002636
Laurence Lundblade241705e2018-12-30 18:56:14 -08002637 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2638 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002639
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002640
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002641 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002642 QCBOREncode_Init(&EC, Buffer);
2643 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2644 return -11;
2645 }
2646
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002647 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002648 QCBOREncode_Init(&EC, Large);
2649 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002650 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002651 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2652#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2653 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002654 return -12;
2655 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002656#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2657 if(uErr != QCBOR_SUCCESS) {
2658 return -112;
2659 }
2660#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2661
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002662
2663 QCBOREncode_Init(&EC, Large);
2664 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002665 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002666 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2667#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2668 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002669 return -13;
2670 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002671#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2672 if(uErr != QCBOR_SUCCESS) {
2673 return -113;
2674 }
2675#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002676
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002677
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002678 return 0;
2679}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002680
2681
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002682#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002683/*
2684 [
2685 4([-1, 3]),
2686 4([-20, 4759477275222530853136]),
2687 4([9223372036854775807, -4759477275222530853137]),
2688 5([300, 100]),
2689 5([-20, 4759477275222530853136]),
2690 5([-9223372036854775808, -4759477275222530853137])
2691 ]
2692 */
2693static const uint8_t spExpectedExponentAndMantissaArray[] = {
2694 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2695 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2696 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2697 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2698 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2699 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2700 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2701 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2702 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2703 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2704 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2705
2706
2707/*
2708 {
2709 "decimal fraction": 4([-1, 3]),
2710 300: 4([-1, 3]),
2711 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2712 400: 4([2147483647, 4759477275222530853136]),
2713 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2714 500: 4([9223372036854775807, -4759477275222530853137]),
2715 "big float": 5([300, 100]),
2716 600: 5([300, 100]),
2717 "big float bignum positive": 5([-20, 4759477275222530853136]),
2718 700: 5([-20, 4759477275222530853136]),
2719 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2720 800: 5([-9223372036854775808, -4759477275222530853137])
2721 }
2722 */
2723static const uint8_t spExpectedExponentAndMantissaMap[] = {
2724 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2725 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2726 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2727 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2728 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2729 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2730 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2731 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2732 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2733 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2734 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2735 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2736 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2737 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2738 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2739 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2740 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2741 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2742 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2743 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2744 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2745 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2746 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2747 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2748 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2749 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2750 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2751 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2752 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2753 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2754 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2755 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2756 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2757 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2758 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2759 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2760 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2761 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2762 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2763 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2764 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2765 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2766 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2767 0x09, 0x10
2768};
2769
2770
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002771int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002772{
2773 QCBOREncodeContext EC;
2774 UsefulBufC EncodedExponentAndMantissa;
2775
2776 // Constant for the big number used in all the tests.
2777 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2778 0x06, 0x07, 0x08, 0x09, 0x010};
2779 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2780
2781 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2782 QCBOREncode_OpenArray(&EC);
2783 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2784 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2785 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2786 QCBOREncode_AddBigFloat(&EC, 100, 300);
2787 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2788 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2789 QCBOREncode_CloseArray(&EC);
2790
2791 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2792 return -2;
2793 }
2794
2795 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2796 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2797 NULL);
2798 if(nReturn) {
2799 return nReturn;
2800 }
2801
2802 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2803 QCBOREncode_OpenMap(&EC);
2804
2805 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2806
2807 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2808
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002809 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002810 "decimal fraction bignum postive",
2811 BigNum,
2812 false,
2813 -200);
2814
2815 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2816 400,
2817 BigNum,
2818 false,
2819 INT32_MAX);
2820
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002821 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002822 "decimal fraction bignum negative",
2823 BigNum,
2824 true,
2825 INT64_MAX);
2826
2827 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2828 500,
2829 BigNum,
2830 true,
2831 INT64_MAX);
2832
2833 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2834
2835 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2836
2837 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2838 "big float bignum positive",
2839 BigNum,
2840 false,
2841 -20);
2842
2843 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2844 700,
2845 BigNum,
2846 false,
2847 -20);
2848
2849 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2850 "big float bignum negative",
2851 BigNum,
2852 true,
2853 INT64_MIN);
2854
2855 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2856 800,
2857 BigNum,
2858 true,
2859 INT64_MIN);
2860
2861 QCBOREncode_CloseMap(&EC);
2862
2863 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2864 return -3;
2865 }
2866
2867
2868 struct UBCompareDiagnostic Diag;
2869
2870 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2871 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2872 &Diag);
2873 if(nReturn) {
2874 return nReturn + 1000000; // +1000000 to distinguish from first test above
2875 }
2876
2877 return 0;
2878}
2879
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002880#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002881
2882
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002883int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002884{
2885 /* This test doesn't have to be extensive, because just about every
2886 * other test exercises QCBOREncode_EncodeHead().
2887 */
2888 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002889 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002890
2891 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2892 CBOR_MAJOR_TYPE_POSITIVE_INT,
2893 0,
2894 0);
2895
2896 static const uint8_t expectedZero[] = {0x00};
2897
2898 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2899 return -1;
2900 }
2901
2902 // ---- Encode a zero padded out to an 8 byte integer ----
2903 encoded = QCBOREncode_EncodeHead(RightSize,
2904 CBOR_MAJOR_TYPE_POSITIVE_INT,
2905 8, // uMinSize is 8 bytes
2906 0);
2907
2908 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2909 0x00, 0x00, 0x00, 0x00};
2910
2911 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2912 return -2;
2913 }
2914
2915
2916 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002917 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002918
2919 encoded = QCBOREncode_EncodeHead(TooSmall,
2920 CBOR_MAJOR_TYPE_POSITIVE_INT,
2921 0,
2922 0);
2923
2924 if(!UsefulBuf_IsNULLC(encoded)) {
2925 return -3;
2926 }
2927
2928 return 0;
2929}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002930
2931
2932static const uint8_t spExpectedForOpenBytes[] = {
2933 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2934 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2935 0x78
2936};
2937
2938static const uint8_t spExpectedForOpenBytes2[] = {
2939 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2940 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2941 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2942};
2943
2944int32_t
2945OpenCloseBytesTest(void)
2946{
2947 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2948 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2949 QCBOREncodeContext EC;
2950 UsefulBuf Place;
2951 UsefulBufC Encoded;
2952 QCBORError uErr;
2953
2954 /* Normal use case -- add a byte string that fits */
2955 QCBOREncode_Init(&EC, TestBuf);
2956 QCBOREncode_OpenBytes(&EC, &Place);
2957 if(Place.ptr != TestBuf.ptr ||
2958 Place.len != TestBuf.len) {
2959 return 1;
2960 }
2961 Place.len -= 4;
2962 UsefulBuf_Set(Place, 'x');
2963 QCBOREncode_CloseBytes(&EC, Place.len);
2964 QCBOREncode_Finish(&EC, &Encoded);
2965 if(UsefulBuf_Compare(Encoded,
2966 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2967 return 2;
2968 }
2969
Paul Liétar79789772022-07-26 20:33:18 +01002970 /* Run the same test but with a NULL buffer */
2971 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
2972 QCBOREncode_OpenBytes(&EC, &Place);
2973 if(!UsefulBuf_IsNULL(Place)) {
2974 return 3;
2975 }
2976 Place.len -= 4;
2977 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
2978 QCBOREncode_CloseBytes(&EC, Place.len);
2979 uErr = QCBOREncode_Finish(&EC, &Encoded);
2980 if(uErr != QCBOR_SUCCESS ||
2981 Encoded.len != sizeof(spExpectedForOpenBytes)) {
2982 return 4;
2983 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002984
2985 /* Open a byte string with no room left */
2986 QCBOREncode_Init(&EC, TestBuf);
2987 QCBOREncode_AddSZString(&EC, "0123456789012345678");
2988 QCBOREncode_OpenBytes(&EC, &Place);
2989 if(Place.ptr != NULL ||
2990 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01002991 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002992 }
2993
2994 /* Try to extend byte string past end of encoding output buffer */
2995 QCBOREncode_Init(&EC, TestBuf);
2996 QCBOREncode_AddSZString(&EC, "012345678901234567");
2997 QCBOREncode_OpenBytes(&EC, &Place);
2998 /* Don't bother to write any bytes*/
2999 QCBOREncode_CloseBytes(&EC, Place.len+1);
3000 uErr = QCBOREncode_GetErrorState(&EC);
3001 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003002 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003003 }
3004
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003005 /* Close a byte string without opening one. */
3006 QCBOREncode_Init(&EC, TestBuf);
3007 QCBOREncode_AddSZString(&EC, "012345678");
3008 QCBOREncode_CloseBytes(&EC, 1);
3009 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003010#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003011 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003012 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003013 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003014#else
3015 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003016 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003017 }
3018#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003019
3020 /* Forget to close a byte string */
3021 QCBOREncode_Init(&EC, TestBuf);
3022 QCBOREncode_AddSZString(&EC, "012345678");
3023 QCBOREncode_OpenBytes(&EC, &Place);
3024 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003025#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003026 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003027 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003028 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003029#else
3030 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003031 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003032 }
3033#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003034
3035 /* Try to open a byte string in a byte string */
3036 QCBOREncode_Init(&EC, TestBuf);
3037 QCBOREncode_AddSZString(&EC, "012345678");
3038 QCBOREncode_OpenBytes(&EC, &Place);
3039 QCBOREncode_OpenBytes(&EC, &Place);
3040 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003041#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003042 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003043 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003044 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003045#else
3046 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003047 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003048 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003049#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3050
3051 /* A successful case with a little complexity */
3052 QCBOREncode_Init(&EC, TestBuf2);
3053 QCBOREncode_OpenMap(&EC);
3054 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3055 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3056 Place.len = 2;
3057 UsefulBuf_Set(Place, 'x');
3058 QCBOREncode_CloseBytes(&EC, 2);
3059 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3060 Place.len = 3;
3061 UsefulBuf_Set(Place, 'y');
3062 QCBOREncode_CloseBytes(&EC, 3);
3063 QCBOREncode_OpenArrayInMapN(&EC, 40);
3064 QCBOREncode_OpenBytes(&EC, &Place);
3065 QCBOREncode_CloseBytes(&EC, 0);
3066 QCBOREncode_CloseArray(&EC);
3067 QCBOREncode_CloseMap(&EC);
3068 uErr = QCBOREncode_Finish(&EC, &Encoded);
3069 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003070 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003071 }
3072 if(UsefulBuf_Compare(Encoded,
3073 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003074 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003075 }
3076
3077 return 0;
3078}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003079
3080
3081
3082int32_t
3083SortMapTest(void)
3084{
3085 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3086 QCBOREncodeContext EC;
3087 UsefulBufC EncodedAndSorted;
3088 QCBORError uErr;
3089 struct UBCompareDiagnostic CompareDiagnostics;
3090
3091
3092 /* --- Basic sort test case --- */
3093 QCBOREncode_Init(&EC, TestBuf);
3094 QCBOREncode_OpenMap(&EC);
3095 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3096 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3097 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3098 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3099 QCBOREncode_CloseAndSortMap(&EC);
3100 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3101 if(uErr) {
3102 return 11;
3103 }
3104
3105 static const uint8_t spBasic[] = {
3106 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3107
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003108 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003109 return 12;
3110 }
3111
3112 /* --- Empty map sort test case --- */
3113 QCBOREncode_Init(&EC, TestBuf);
3114 QCBOREncode_OpenMap(&EC);
3115 QCBOREncode_CloseAndSortMap(&EC);
3116 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3117 if(uErr) {
3118 return 21;
3119 }
3120
3121 static const uint8_t spEmpty[] = {0xA0};
3122 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3123 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3124 &CompareDiagnostics)) {
3125 return 22;
3126 }
3127
3128 /* --- Several levels of nested sorted maps --- */
3129 QCBOREncode_Init(&EC, TestBuf);
3130 QCBOREncode_OpenMap(&EC);
3131 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3132 QCBOREncode_OpenMapInMapN(&EC, 428);
3133 QCBOREncode_AddNULLToMap(&EC, "null");
3134 QCBOREncode_OpenArrayInMap(&EC, "array");
3135 QCBOREncode_AddSZString(&EC, "hi");
3136 QCBOREncode_AddSZString(&EC, "there");
3137 QCBOREncode_CloseArray(&EC);
3138 QCBOREncode_OpenMapInMap(&EC, "empty2");
3139 QCBOREncode_CloseAndSortMap(&EC);
3140 QCBOREncode_OpenMapInMap(&EC, "empty1");
3141 QCBOREncode_CloseAndSortMap(&EC);
3142 QCBOREncode_CloseAndSortMap(&EC);
3143 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3144 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3145 QCBOREncode_CloseAndSortMap(&EC);
3146 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3147 if(uErr) {
3148 return 31;
3149 }
3150 static const uint8_t spNested[] = {
3151 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3152 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3153 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3154 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3155 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3156 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3157 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3158 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3159
3160 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3161 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested),
3162 &CompareDiagnostics)) {
3163 return 32;
3164 }
3165
3166 /* --- Degenerate case of everything in order --- */
3167 QCBOREncode_Init(&EC, TestBuf);
3168 QCBOREncode_OpenMap(&EC);
3169 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3170 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3171 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3172 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3173 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3174 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3175 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3176 QCBOREncode_CloseAndSortMap(&EC);
3177 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3178 if(uErr) {
3179 return 41;
3180 }
3181
3182 static const uint8_t sp6Items[] = {
3183 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3184 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3185 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3186 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3187 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3188 &CompareDiagnostics)) {
3189 return 42;
3190 }
3191
3192 /* --- Degenerate case -- reverse order --- */
3193 QCBOREncode_Init(&EC, TestBuf);
3194 QCBOREncode_OpenMap(&EC);
3195 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3196 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3197 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3198 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3199 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3200 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3201 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3202 QCBOREncode_CloseAndSortMap(&EC);
3203 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3204 if(uErr) {
3205 return 51;
3206 }
3207
3208 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3209 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3210 &CompareDiagnostics)) {
3211 return 52;
3212 }
3213
3214 /* --- Same items, randomly out of order --- */
3215 QCBOREncode_Init(&EC, TestBuf);
3216 QCBOREncode_OpenMap(&EC);
3217 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3218 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3219 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3220 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3221 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3222 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3223 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3224 QCBOREncode_CloseAndSortMap(&EC);
3225 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3226 if(uErr) {
3227 return 61;
3228 }
3229
3230 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3231 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3232 &CompareDiagnostics)) {
3233 return 62;
3234 }
3235
3236 /* --- Stuff in front of and after array to sort --- */
3237 QCBOREncode_Init(&EC, TestBuf);
3238 QCBOREncode_OpenArray(&EC);
3239 QCBOREncode_AddInt64(&EC, 111);
3240 QCBOREncode_AddInt64(&EC, 222);
3241 QCBOREncode_OpenMap(&EC);
3242 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3243 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3244 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3245 QCBOREncode_CloseAndSortMap(&EC);
3246 QCBOREncode_AddInt64(&EC, 888);
3247 QCBOREncode_AddInt64(&EC, 999);
3248 QCBOREncode_CloseArray(&EC);
3249 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3250 if(uErr) {
3251 return 71;
3252 }
3253
3254 static const uint8_t spPreItems[] = {
3255 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3256 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3257 0x03, 0xE7};
3258 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3259 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3260 &CompareDiagnostics)) {
3261 return 72;
3262 }
3263
3264 /* --- map with labels of all CBOR major types and in reverse order --- */
3265 QCBOREncode_Init(&EC, TestBuf);
3266 QCBOREncode_OpenMap(&EC);
3267
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003268 /* Adding labels directly rather than AddToMap functions */
3269
3270#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003271 QCBOREncode_AddDouble(&EC, 8.77);
3272 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003273#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003274
3275 QCBOREncode_AddBool(&EC, true);
3276 QCBOREncode_AddInt64(&EC, 6);
3277
3278 QCBOREncode_AddDateEpoch(&EC, 88);
3279 QCBOREncode_AddInt64(&EC, 5);
3280
3281 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3282 QCBOREncode_AddInt64(&EC, 4);
3283
3284 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3285 QCBOREncode_AddInt64(&EC, 7);
3286
3287 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3288
3289 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3290 QCBOREncode_AddInt64(&EC, 2);
3291
3292 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3293 QCBOREncode_CloseAndSortMap(&EC);
3294
3295 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3296 if(uErr) {
3297 return 81;
3298 }
3299
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003300#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003301 static const uint8_t spLabelTypes[] = {
3302 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3303 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3304 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3305 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3306 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003307#else
3308 static const uint8_t spLabelTypes[] = {
3309 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3310 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3311 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3312#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3313
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003314 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3315 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3316 &CompareDiagnostics)) {
3317 return 82;
3318 }
3319
3320 /* --- labels are indefinitely encoded --- */
3321 QCBOREncode_Init(&EC, TestBuf);
3322 QCBOREncode_OpenMap(&EC);
3323
3324 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3325
3326 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3327
3328 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3329 QCBOREncode_AddInt64(&EC, 3);
3330
3331 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3332 QCBOREncode_AddInt64(&EC, 4);
3333
3334 QCBOREncode_CloseAndSortMap(&EC);
3335
3336 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3337 if(uErr) {
3338 return 91;
3339 }
3340
3341 static const uint8_t spIndefItems[] = {
3342 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3343 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3344 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3345 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3346 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3347 &CompareDiagnostics)) {
3348 return 92;
3349 }
3350
3351 /* --- Indefinitely encoded maps --- */
3352 QCBOREncode_Init(&EC, TestBuf);
3353 QCBOREncode_OpenMapIndefiniteLength(&EC);
3354
3355 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3356 QCBOREncode_CloseMapIndefiniteLength(&EC);
3357
3358 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3359 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3360
3361 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3362 QCBOREncode_CloseMapIndefiniteLength(&EC);
3363
3364 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3365 QCBOREncode_CloseMapIndefiniteLength(&EC);
3366
3367 QCBOREncode_CloseAndSortMapIndef(&EC);
3368 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3369 if(uErr) {
3370 return 101;
3371 }
3372
3373 static const uint8_t spIndeMaps[] = {
3374 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3375 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3376 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3377 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3378 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3379 &CompareDiagnostics)) {
3380 return 102;
3381 }
3382
3383 return 0;
3384}