blob: af64f924971a1105d8fad0e6724a5234fdc33478 [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 Lundblade240ca822024-01-16 11:11:00 -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 Lundblade8d3b8552021-06-10 11:11:54 -07001765static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1766
Laurence Lundbladeda532272019-04-07 11:40:17 -07001767/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001768 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001769 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001770int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001771{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001772 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001773
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001774 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301775 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001776
Laurence Lundblade684aec22018-10-12 19:33:53 +08001777 QCBOREncode_OpenArray(&EC);
1778 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001779
Laurence Lundblade684aec22018-10-12 19:33:53 +08001780 QCBOREncode_BstrWrap(&EC);
1781 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001782
Laurence Lundblade684aec22018-10-12 19:33:53 +08001783 UsefulBufC Wrapped;
1784 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001785
Laurence Lundblade684aec22018-10-12 19:33:53 +08001786 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001787
Laurence Lundblade684aec22018-10-12 19:33:53 +08001788 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001789 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001790 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001792
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301793 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001794 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001795 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001796
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001797 // Second test - see if the length of the wrapped
1798 // bstr is correct. Also tests bstr wrapping
1799 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001800 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1801 QCBOREncode_OpenArray(&EC);
1802 QCBOREncode_BstrWrap(&EC);
1803 QCBOREncode_OpenArray(&EC);
1804 QCBOREncode_AddNULL(&EC);
1805 QCBOREncode_CloseArray(&EC);
1806 UsefulBufC BStr;
1807 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001808 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1809 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001810 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001811 return -5;
1812 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001813
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001814
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001815 // Fourth test, cancelling a byte string
1816 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1817
1818 QCBOREncode_OpenArray(&EC);
1819 QCBOREncode_AddUInt64(&EC, 451);
1820
1821 QCBOREncode_BstrWrap(&EC);
1822 QCBOREncode_CancelBstrWrap(&EC);
1823
1824
1825 QCBOREncode_AddUInt64(&EC, 42);
1826 QCBOREncode_CloseArray(&EC);
1827 if(QCBOREncode_Finish(&EC, &Encoded)) {
1828 return -8;
1829 }
1830 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1831 return -9;
1832 }
1833
1834 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001835 // Fifth test, failed cancelling
1836 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1837
1838 QCBOREncode_OpenArray(&EC);
1839 QCBOREncode_AddUInt64(&EC, 451);
1840
1841 QCBOREncode_BstrWrap(&EC);
1842 QCBOREncode_AddUInt64(&EC, 99);
1843 QCBOREncode_CancelBstrWrap(&EC);
1844
1845 QCBOREncode_AddUInt64(&EC, 42);
1846 QCBOREncode_CloseArray(&EC);
1847 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001848#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001849 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1850 return -10;
1851 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001852#else
1853 if(uErr != QCBOR_SUCCESS) {
1854 return -110;
1855 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001856#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1857
1858 // Sixth test, another cancel, but the error is not caught
1859 // This use will produce unintended CBOR. The error
1860 // is not caught because it would require tracking state
1861 // for QCBOREncode_BstrWrapInMapN.
1862 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1863
1864 QCBOREncode_OpenMap(&EC);
1865 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1866
1867 QCBOREncode_BstrWrapInMapN(&EC, 55);
1868 QCBOREncode_CancelBstrWrap(&EC);
1869
1870 QCBOREncode_CloseMap(&EC);
1871 uErr = QCBOREncode_Finish(&EC, &Encoded);
1872 if(uErr != QCBOR_SUCCESS) {
1873 return -11;
1874 }
1875
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001876 return 0;
1877}
1878
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001879
1880
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001881int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001882{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001883 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001884 UsefulBufC Wrapped;
1885 UsefulBufC Encoded2;
1886 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001887
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001888 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001889
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301890 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001891
Laurence Lundblade684aec22018-10-12 19:33:53 +08001892 QCBOREncode_OpenArray(&EC);
1893 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001894
Laurence Lundblade684aec22018-10-12 19:33:53 +08001895 QCBOREncode_BstrWrap(&EC);
1896 QCBOREncode_AddUInt64(&EC, 466);
1897 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001898
Laurence Lundblade684aec22018-10-12 19:33:53 +08001899 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001900
Laurence Lundblade684aec22018-10-12 19:33:53 +08001901 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001902
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001903 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001904#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001905 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001906 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001907 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001908#else
1909 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1910 * is set and not to be sure to excerice all the relavant code in
1911 * both conditions. When the guards are disabled, there is no
1912 * error returned, but the code path is still covered.
1913 */
1914 if(uError != QCBOR_SUCCESS) {
1915 return (int32_t)(600 + uError);
1916 }
1917#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001918
Laurence Lundbladeee851742020-01-08 08:37:05 -08001919 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301920 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001921 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
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_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001925 return (int32_t)(700 + uError);
1926 }
1927#else
1928 if(uError != QCBOR_SUCCESS) {
1929 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001930 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001931#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001932
Laurence Lundblade684aec22018-10-12 19:33:53 +08001933 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301934 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001935 for(int i = 1; i < 18; i++) {
1936 QCBOREncode_BstrWrap(&EC);
1937 }
1938 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001939
Laurence Lundblade684aec22018-10-12 19:33:53 +08001940 for(int i = 1; i < 18; i++) {
1941 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001944 uError = QCBOREncode_Finish(&EC, &Encoded2);
1945 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001946 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001947 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001948
Laurence Lundblade684aec22018-10-12 19:33:53 +08001949 return 0;
1950}
1951
1952
Laurence Lundblade684aec22018-10-12 19:33:53 +08001953/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001954 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001956 [
1957 h'82004E82014B8202488203458204428105',
1958 {
1959 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1960 8426568656C6C6F18234BA2131318436568656C6C6F'
1961 }
1962 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001963
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001964 Unwrapping the first byte string in the above gives
1965 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301966
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001967 Unwrapping again, the byte string immediately above gives
1968 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301969
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001970 ...
1971
1972 Unrapping the second byte string in the top-level CBOR above gives
1973 {16: 16,
1974 64: "hello",
1975 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1976 }
1977
1978 Unwrapping again, the byte string immediately above gives
1979 {17: 17,
1980 65: "hello",
1981 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
1982 }
1983
1984 ...
1985
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301986 */
1987static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001988{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001989 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
1990 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
1991 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
1992 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
1993 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
1994 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
1995 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
1996 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1997 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
1998 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08001999};
2000
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002001
2002/*
2003 Get an int64 out of the decoder or fail.
2004 */
2005static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002006{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002007 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002008 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002009
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002010 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002011 if(nReturn) {
2012 return nReturn;
2013 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002014 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002015 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002018 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002019 return 0;
2020}
2021
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002022/*
2023 Get an array out of the decoder or fail.
2024 */
2025static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002026{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002027 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002028 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002029
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002030 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002031 if(nReturn) {
2032 return nReturn;
2033 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002034 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2035 return -1;
2036 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002037
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002038 *pInt = Item.val.uCount;
2039 return 0;
2040}
2041
2042/*
2043 Get a map out of the decoder or fail.
2044 */
2045static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2046{
2047 QCBORItem Item;
2048 int32_t nReturn;
2049
2050 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002051 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002052 return nReturn;
2053 }
2054 if(Item.uDataType != QCBOR_TYPE_MAP) {
2055 return -1;
2056 }
2057
2058 *pInt = Item.val.uCount;
2059 return 0;
2060}
2061
2062/*
2063 Get a byte string out of the decoder or fail.
2064 */
2065static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2066{
2067 QCBORItem Item;
2068 int32_t nReturn;
2069
2070 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2071 if(nReturn) {
2072 return nReturn;
2073 }
2074 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002075 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002076 }
2077
2078 *pBstr = Item.val.string;
2079 return 0;
2080}
2081
2082/*
2083 Get a byte string out of the decoder or fail.
2084 */
2085static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2086{
2087 QCBORItem Item;
2088 int nReturn;
2089
2090 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2091 if(nReturn) {
2092 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002093 }
2094 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002095 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002096 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002097
2098 *pTstr = Item.val.string;
2099 return 0;
2100}
2101
2102
2103/*
2104 Recursively decode array containing a little CBOR and a bstr wrapped array
2105 with a little CBOR and a bstr wrapped array...
2106
2107 Part of bstr_wrap_nest_test.
2108 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2109{
2110 int64_t nInt;
2111 UsefulBufC Bstr;
2112 uint16_t nArrayCount;
2113 QCBORDecodeContext DC;
2114 int32_t nResult;
2115
2116 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2117
2118 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2119 return -10;
2120 }
2121
2122 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002123 return -11;
2124 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002125
2126 nResult = GetByteString(&DC, &Bstr);
2127 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2128 if(nArrayCount != 1) {
2129 return -12;
2130 } else {
2131 // successful exit
2132 return 0;
2133 }
2134 }
2135 if(nResult) {
2136 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002139 // tail recursion; good compilers will reuse the stack frame
2140 return DecodeNextNested(Bstr);
2141}
2142
2143
2144/*
2145 Recursively decode map containing a little CBOR and a bstr wrapped map
2146 with a little CBOR and a bstr wrapped map...
2147
2148 Part of bstr_wrap_nest_test.
2149 */
2150static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2151{
2152 int32_t nResult;
2153 uint16_t nMapCount;
2154 int64_t nInt;
2155 UsefulBufC Bstr;
2156 QCBORDecodeContext DC;
2157
2158 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2159
2160 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2161 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002163
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002164 if(GetInt64(&DC, &nInt)) {
2165 return -21;
2166 }
2167
2168 // The "hello"
2169 if(GetTextString(&DC, &Bstr)) {
2170 return -22;
2171 }
2172
2173 nResult = GetByteString(&DC, &Bstr);
2174 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2175 if(nMapCount == 2) {
2176 // successful exit
2177 return 0;
2178 } else {
2179 return -23;
2180 }
2181 }
2182
2183 if(nResult) {
2184 return -24;
2185 }
2186
2187 // tail recursion; good compilers will reuse the stack frame
2188 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002189}
2190
2191
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002192int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002193{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002194 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302195 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002196
Laurence Lundblade684aec22018-10-12 19:33:53 +08002197 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002198 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002200 QCBOREncode_OpenArray(&EC);
2201
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002202 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002203 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002204 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002205 QCBOREncode_AddInt64(&EC, i);
2206 }
2207 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2208 QCBOREncode_CloseArray(&EC);
2209 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002211
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002212 QCBOREncode_OpenMap(&EC);
2213 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2214 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2215 QCBOREncode_OpenMap(&EC);
2216 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002217 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002218 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002219
2220 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2221 QCBOREncode_CloseMap(&EC);
2222 QCBOREncode_CloseBstrWrap(&EC, NULL);
2223 }
2224 QCBOREncode_CloseMap(&EC);
2225
Laurence Lundblade684aec22018-10-12 19:33:53 +08002226 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002227
Laurence Lundblade684aec22018-10-12 19:33:53 +08002228 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002229 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002230 return -1;
2231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002232
Laurence Lundblade684aec22018-10-12 19:33:53 +08002233 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302234 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002235 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002237
Laurence Lundblade684aec22018-10-12 19:33:53 +08002238 // ---- Decode it and see if it is OK ------
2239 QCBORDecodeContext DC;
2240 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002241
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002242 UsefulBufC Bstr;
2243 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002244
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002245 // Array surrounding the the whole thing
2246 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002247 return -3;
2248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002250 // Get the byte string wrapping some array stuff
2251 if(GetByteString(&DC, &Bstr)) {
2252 return -4;
2253 }
2254
2255 // Decode the wrapped nested structure
2256 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002257 if(nReturn) {
2258 return nReturn;
2259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002261 // A map enclosing some map-oriented bstr wraps
2262 if(GetMap(&DC, &nArrayCount)) {
2263 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002264 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002265
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002266 // Get the byte string wrapping some array stuff
2267 if(GetByteString(&DC, &Bstr)) {
2268 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002270
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002271 // Decode the wrapped nested structure
2272 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002273 if(nReturn) {
2274 return nReturn;
2275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002276
Laurence Lundblade684aec22018-10-12 19:33:53 +08002277 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002278 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002279 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002280
Laurence Lundblade684aec22018-10-12 19:33:53 +08002281 return 0;
2282}
2283
2284
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002285static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302286 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2287 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2288 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2289 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2290 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2291 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2292 0x45, 0xca, 0xcb, 0x36};
2293
2294/*
2295 D2 # tag(18)
2296 84 # array(4)
2297 43 # bytes(3)
2298 A10126 # "\xA1\x01&"
2299 A1 # map(1)
2300 04 # unsigned(4)
2301 42 # bytes(2)
2302 3131 # "11"
2303 54 # bytes(20)
2304 546869732069732074686520636F6E74656E742E # "This is the content."
2305 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002306 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2307 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2308 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2309 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2310 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2311 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2312 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2313 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302314 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002315static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302316 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2317 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2318 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2319 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2320 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2321 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2322 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2323 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2324 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2325 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2326
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002327static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2328
2329
Laurence Lundblade684aec22018-10-12 19:33:53 +08002330/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002331 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002332 C.2.1. This doesn't actually verify the signature (however
2333 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002334 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002335int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002336{
2337 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002338 const char *szKid = "11";
2339 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2340 const char *szPayload = "This is the content.";
2341 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2342 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2343 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundblade684aec22018-10-12 19:33:53 +08002345 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002346
2347 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302348 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002349
Laurence Lundblade684aec22018-10-12 19:33:53 +08002350 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002351 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002352 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002353
Laurence Lundblade684aec22018-10-12 19:33:53 +08002354 // Add protected headers
2355 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002356
Laurence Lundblade684aec22018-10-12 19:33:53 +08002357 // Empty map with unprotected headers
2358 QCBOREncode_OpenMap(&EC);
2359 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2360 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002361
Laurence Lundblade684aec22018-10-12 19:33:53 +08002362 // The payload
2363 UsefulBufC WrappedPayload;
2364 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002365 // Payload is not actually CBOR in example C.2.1 like it would be
2366 // for a CWT or EAT. It is just a text string.
2367 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002368 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade684aec22018-10-12 19:33:53 +08002370 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002371 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2372 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002373 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002374 return -1;
2375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002376
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002377/* if(UsefulBuf_Compare(WrappedPayload,
2378 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2379 return -1;
2380 } */
2381
Laurence Lundblade684aec22018-10-12 19:33:53 +08002382 // The signature
2383 QCBOREncode_AddBytes(&EC, Signature);
2384 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002385
Laurence Lundblade684aec22018-10-12 19:33:53 +08002386 // Finish and check the results
2387 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002388 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002389 return -2;
2390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002391
Laurence Lundblade684aec22018-10-12 19:33:53 +08002392 // 98 is the size from RFC 8152 C.2.1
2393 if(COSE_Sign1.len != 98) {
2394 return -3;
2395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002397 // It would be good to compare this to the output from a COSE
2398 // implementation like COSE-C. This has been checked against the
2399 // CBOR playground.
2400 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002401 return -4;
2402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002403
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002404
2405 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2406 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2407
2408 // top level array for cose sign1, 18 is the tag for COSE sign
2409 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2410 QCBOREncode_OpenArray(&EC);
2411
2412 // Add protected headers
2413 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2414
2415 // Empty map with unprotected headers
2416 QCBOREncode_OpenMap(&EC);
2417 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2418 QCBOREncode_CloseMap(&EC);
2419
2420 // The payload
2421 QCBOREncode_BstrWrap(&EC);
2422 // Payload is not actually CBOR in example C.2.1 like it would be
2423 // for a CWT or EAT. It is just a text string.
2424 QCBOREncode_AddEncoded(&EC, Payload);
2425 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2426
2427 // Check we got back the actual payload expected
2428 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2429 if(UsefulBuf_Compare(WrappedPayload,
2430 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2431 return -11;
2432 }
2433
2434 // The signature
2435 QCBOREncode_AddBytes(&EC, Signature);
2436 QCBOREncode_CloseArray(&EC);
2437
2438 // Finish and check the results
2439 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2440 return -12;
2441 }
2442
2443 // 98 is the size from RFC 8152 C.2.1
2444 if(COSE_Sign1.len != 98) {
2445 return -13;
2446 }
2447
2448 // It would be good to compare this to the output from a COSE
2449 // implementation like COSE-C. This has been checked against the
2450 // CBOR playground.
2451 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2452 return -14;
2453 }
2454
Laurence Lundblade684aec22018-10-12 19:33:53 +08002455 return 0;
2456}
2457
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002458
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002459int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002460{
2461 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002462 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002463
2464
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002465 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002466 // Do all of these tests with NULL buffers so no actual
2467 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002468 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002469
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002470 // First verify no error from a big buffer
2471 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002472 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002473 // 6 is the CBOR overhead for opening the array and encodng the length
2474 // This exactly fills the buffer.
2475 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002476 QCBOREncode_CloseArray(&EC);
2477 size_t xx;
2478 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2479 return -1;
2480 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002481
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002482 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002483 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002484 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002485 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002486 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002487 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002488 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2489 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002490 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002491 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002492 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002493 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2494 return -2;
2495 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002496
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002497 // Third, fit an array in exactly at max position allowed
2498 QCBOREncode_Init(&EC, Buffer);
2499 QCBOREncode_OpenArray(&EC);
2500 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2501 QCBOREncode_OpenArray(&EC);
2502 QCBOREncode_CloseArray(&EC);
2503 QCBOREncode_CloseArray(&EC);
2504 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2505 return -10;
2506 }
2507
2508
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002509 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2510 // Work close to the 4GB size limit for a better test
2511 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002512 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002513
2514 QCBOREncode_Init(&EC, Large);
2515 QCBOREncode_OpenArray(&EC);
2516 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2517 QCBOREncode_CloseArray(&EC);
2518 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2519 // Making sure it succeeds when it should first
2520 return -3;
2521 }
2522
2523 QCBOREncode_Init(&EC, Large);
2524 QCBOREncode_OpenArray(&EC);
2525 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2526 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2527 QCBOREncode_CloseArray(&EC);
2528 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2529 // Now just 1 byte over, see that it fails
2530 return -4;
2531 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002532
2533
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002534 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2535 QCBOREncode_Init(&EC, Large);
2536 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2537 QCBOREncode_OpenArray(&EC);
2538 }
2539 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2540 QCBOREncode_CloseArray(&EC);
2541 }
2542 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2543 // Making sure it succeeds when it should first
2544 return -5;
2545 }
2546
2547 QCBOREncode_Init(&EC, Large);
2548 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2549 QCBOREncode_OpenArray(&EC);
2550 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002551 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002552 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2553 QCBOREncode_CloseArray(&EC);
2554 }
2555 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002556 return -6;
2557 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002558
2559
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002560 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002561 QCBOREncode_Init(&EC, Large);
2562 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2563 QCBOREncode_OpenArray(&EC);
2564 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002565 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002566 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2567 QCBOREncode_CloseArray(&EC);
2568 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002569 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2570#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2571 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002572 return -7;
2573 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002574#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2575 if(uErr != QCBOR_SUCCESS) {
2576 return -107;
2577 }
2578#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002579
2580
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002581 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002582 QCBOREncode_Init(&EC, Large);
2583 QCBOREncode_OpenArray(&EC);
2584 UsefulBufC Wrap;
2585 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002586 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2587#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2588 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002589 return -8;
2590 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002591#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2592 if(uErr != QCBOR_SUCCESS) {
2593 return -108;
2594 }
2595#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002596
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002597 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002598 QCBOREncode_Init(&EC, Large);
2599 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2600 QCBOREncode_OpenArray(&EC);
2601 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002602 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002603 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2604 QCBOREncode_CloseArray(&EC);
2605 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002606
2607 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2608#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2609 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002610 return -9;
2611 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002612#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2613 if(uErr != QCBOR_SUCCESS) {
2614 return -109;
2615 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002616#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002617
Laurence Lundblade241705e2018-12-30 18:56:14 -08002618 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2619 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002620
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002621
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002622 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002623 QCBOREncode_Init(&EC, Buffer);
2624 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2625 return -11;
2626 }
2627
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002628 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002629 QCBOREncode_Init(&EC, Large);
2630 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002631 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002632 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2633#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2634 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002635 return -12;
2636 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002637#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2638 if(uErr != QCBOR_SUCCESS) {
2639 return -112;
2640 }
2641#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2642
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002643
2644 QCBOREncode_Init(&EC, Large);
2645 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002646 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002647 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2648#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2649 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002650 return -13;
2651 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002652#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2653 if(uErr != QCBOR_SUCCESS) {
2654 return -113;
2655 }
2656#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002657
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002658
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002659 return 0;
2660}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002661
2662
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002663#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002664/*
2665 [
2666 4([-1, 3]),
2667 4([-20, 4759477275222530853136]),
2668 4([9223372036854775807, -4759477275222530853137]),
2669 5([300, 100]),
2670 5([-20, 4759477275222530853136]),
2671 5([-9223372036854775808, -4759477275222530853137])
2672 ]
2673 */
2674static const uint8_t spExpectedExponentAndMantissaArray[] = {
2675 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2676 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2677 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2678 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2679 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2680 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2681 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2682 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2683 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2684 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2685 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2686
2687
2688/*
2689 {
2690 "decimal fraction": 4([-1, 3]),
2691 300: 4([-1, 3]),
2692 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2693 400: 4([2147483647, 4759477275222530853136]),
2694 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2695 500: 4([9223372036854775807, -4759477275222530853137]),
2696 "big float": 5([300, 100]),
2697 600: 5([300, 100]),
2698 "big float bignum positive": 5([-20, 4759477275222530853136]),
2699 700: 5([-20, 4759477275222530853136]),
2700 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2701 800: 5([-9223372036854775808, -4759477275222530853137])
2702 }
2703 */
2704static const uint8_t spExpectedExponentAndMantissaMap[] = {
2705 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2706 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2707 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2708 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2709 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2710 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2711 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2712 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2713 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2714 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2715 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2716 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2717 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2718 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2719 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2720 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2721 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2722 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2723 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2724 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2725 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2726 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2727 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2728 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2729 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2730 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2731 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2732 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2733 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2734 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2735 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2736 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2737 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2738 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2739 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2740 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2741 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2742 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2743 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2744 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2745 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2746 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2747 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2748 0x09, 0x10
2749};
2750
2751
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002752int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002753{
2754 QCBOREncodeContext EC;
2755 UsefulBufC EncodedExponentAndMantissa;
2756
2757 // Constant for the big number used in all the tests.
2758 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2759 0x06, 0x07, 0x08, 0x09, 0x010};
2760 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2761
2762 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2763 QCBOREncode_OpenArray(&EC);
2764 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2765 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2766 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2767 QCBOREncode_AddBigFloat(&EC, 100, 300);
2768 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2769 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2770 QCBOREncode_CloseArray(&EC);
2771
2772 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2773 return -2;
2774 }
2775
2776 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2777 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2778 NULL);
2779 if(nReturn) {
2780 return nReturn;
2781 }
2782
2783 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2784 QCBOREncode_OpenMap(&EC);
2785
2786 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2787
2788 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2789
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002790 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002791 "decimal fraction bignum postive",
2792 BigNum,
2793 false,
2794 -200);
2795
2796 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2797 400,
2798 BigNum,
2799 false,
2800 INT32_MAX);
2801
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002802 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002803 "decimal fraction bignum negative",
2804 BigNum,
2805 true,
2806 INT64_MAX);
2807
2808 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2809 500,
2810 BigNum,
2811 true,
2812 INT64_MAX);
2813
2814 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2815
2816 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2817
2818 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2819 "big float bignum positive",
2820 BigNum,
2821 false,
2822 -20);
2823
2824 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2825 700,
2826 BigNum,
2827 false,
2828 -20);
2829
2830 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2831 "big float bignum negative",
2832 BigNum,
2833 true,
2834 INT64_MIN);
2835
2836 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2837 800,
2838 BigNum,
2839 true,
2840 INT64_MIN);
2841
2842 QCBOREncode_CloseMap(&EC);
2843
2844 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2845 return -3;
2846 }
2847
2848
2849 struct UBCompareDiagnostic Diag;
2850
2851 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2852 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2853 &Diag);
2854 if(nReturn) {
2855 return nReturn + 1000000; // +1000000 to distinguish from first test above
2856 }
2857
2858 return 0;
2859}
2860
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002861#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002862
2863
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002864int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002865{
2866 /* This test doesn't have to be extensive, because just about every
2867 * other test exercises QCBOREncode_EncodeHead().
2868 */
2869 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002870 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002871
2872 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2873 CBOR_MAJOR_TYPE_POSITIVE_INT,
2874 0,
2875 0);
2876
2877 static const uint8_t expectedZero[] = {0x00};
2878
2879 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2880 return -1;
2881 }
2882
2883 // ---- Encode a zero padded out to an 8 byte integer ----
2884 encoded = QCBOREncode_EncodeHead(RightSize,
2885 CBOR_MAJOR_TYPE_POSITIVE_INT,
2886 8, // uMinSize is 8 bytes
2887 0);
2888
2889 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2890 0x00, 0x00, 0x00, 0x00};
2891
2892 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2893 return -2;
2894 }
2895
2896
2897 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002898 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002899
2900 encoded = QCBOREncode_EncodeHead(TooSmall,
2901 CBOR_MAJOR_TYPE_POSITIVE_INT,
2902 0,
2903 0);
2904
2905 if(!UsefulBuf_IsNULLC(encoded)) {
2906 return -3;
2907 }
2908
2909 return 0;
2910}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002911
2912
2913static const uint8_t spExpectedForOpenBytes[] = {
2914 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2915 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2916 0x78
2917};
2918
2919static const uint8_t spExpectedForOpenBytes2[] = {
2920 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2921 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2922 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2923};
2924
2925int32_t
2926OpenCloseBytesTest(void)
2927{
2928 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2929 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2930 QCBOREncodeContext EC;
2931 UsefulBuf Place;
2932 UsefulBufC Encoded;
2933 QCBORError uErr;
2934
2935 /* Normal use case -- add a byte string that fits */
2936 QCBOREncode_Init(&EC, TestBuf);
2937 QCBOREncode_OpenBytes(&EC, &Place);
2938 if(Place.ptr != TestBuf.ptr ||
2939 Place.len != TestBuf.len) {
2940 return 1;
2941 }
2942 Place.len -= 4;
2943 UsefulBuf_Set(Place, 'x');
2944 QCBOREncode_CloseBytes(&EC, Place.len);
2945 QCBOREncode_Finish(&EC, &Encoded);
2946 if(UsefulBuf_Compare(Encoded,
2947 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2948 return 2;
2949 }
2950
Paul Liétar79789772022-07-26 20:33:18 +01002951 /* Run the same test but with a NULL buffer */
2952 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
2953 QCBOREncode_OpenBytes(&EC, &Place);
2954 if(!UsefulBuf_IsNULL(Place)) {
2955 return 3;
2956 }
2957 Place.len -= 4;
2958 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
2959 QCBOREncode_CloseBytes(&EC, Place.len);
2960 uErr = QCBOREncode_Finish(&EC, &Encoded);
2961 if(uErr != QCBOR_SUCCESS ||
2962 Encoded.len != sizeof(spExpectedForOpenBytes)) {
2963 return 4;
2964 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002965
2966 /* Open a byte string with no room left */
2967 QCBOREncode_Init(&EC, TestBuf);
2968 QCBOREncode_AddSZString(&EC, "0123456789012345678");
2969 QCBOREncode_OpenBytes(&EC, &Place);
2970 if(Place.ptr != NULL ||
2971 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01002972 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002973 }
2974
2975 /* Try to extend byte string past end of encoding output buffer */
2976 QCBOREncode_Init(&EC, TestBuf);
2977 QCBOREncode_AddSZString(&EC, "012345678901234567");
2978 QCBOREncode_OpenBytes(&EC, &Place);
2979 /* Don't bother to write any bytes*/
2980 QCBOREncode_CloseBytes(&EC, Place.len+1);
2981 uErr = QCBOREncode_GetErrorState(&EC);
2982 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01002983 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002984 }
2985
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002986 /* Close a byte string without opening one. */
2987 QCBOREncode_Init(&EC, TestBuf);
2988 QCBOREncode_AddSZString(&EC, "012345678");
2989 QCBOREncode_CloseBytes(&EC, 1);
2990 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002991#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002992 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01002993 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002994 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002995#else
2996 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01002997 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002998 }
2999#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003000
3001 /* Forget to close a byte string */
3002 QCBOREncode_Init(&EC, TestBuf);
3003 QCBOREncode_AddSZString(&EC, "012345678");
3004 QCBOREncode_OpenBytes(&EC, &Place);
3005 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003006#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003007 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003008 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003009 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003010#else
3011 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003012 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003013 }
3014#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003015
3016 /* Try to open a byte string in a byte string */
3017 QCBOREncode_Init(&EC, TestBuf);
3018 QCBOREncode_AddSZString(&EC, "012345678");
3019 QCBOREncode_OpenBytes(&EC, &Place);
3020 QCBOREncode_OpenBytes(&EC, &Place);
3021 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003022#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003023 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003024 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003025 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003026#else
3027 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003028 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003029 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003030#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3031
3032 /* A successful case with a little complexity */
3033 QCBOREncode_Init(&EC, TestBuf2);
3034 QCBOREncode_OpenMap(&EC);
3035 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3036 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3037 Place.len = 2;
3038 UsefulBuf_Set(Place, 'x');
3039 QCBOREncode_CloseBytes(&EC, 2);
3040 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3041 Place.len = 3;
3042 UsefulBuf_Set(Place, 'y');
3043 QCBOREncode_CloseBytes(&EC, 3);
3044 QCBOREncode_OpenArrayInMapN(&EC, 40);
3045 QCBOREncode_OpenBytes(&EC, &Place);
3046 QCBOREncode_CloseBytes(&EC, 0);
3047 QCBOREncode_CloseArray(&EC);
3048 QCBOREncode_CloseMap(&EC);
3049 uErr = QCBOREncode_Finish(&EC, &Encoded);
3050 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003051 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003052 }
3053 if(UsefulBuf_Compare(Encoded,
3054 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003055 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003056 }
3057
3058 return 0;
3059}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003060
3061
3062
3063int32_t
3064SortMapTest(void)
3065{
3066 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3067 QCBOREncodeContext EC;
3068 UsefulBufC EncodedAndSorted;
3069 QCBORError uErr;
3070 struct UBCompareDiagnostic CompareDiagnostics;
3071
3072
3073 /* --- Basic sort test case --- */
3074 QCBOREncode_Init(&EC, TestBuf);
3075 QCBOREncode_OpenMap(&EC);
3076 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3077 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3078 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3079 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3080 QCBOREncode_CloseAndSortMap(&EC);
3081 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3082 if(uErr) {
3083 return 11;
3084 }
3085
3086 static const uint8_t spBasic[] = {
3087 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3088
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003089 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003090 return 12;
3091 }
3092
3093 /* --- Empty map sort test case --- */
3094 QCBOREncode_Init(&EC, TestBuf);
3095 QCBOREncode_OpenMap(&EC);
3096 QCBOREncode_CloseAndSortMap(&EC);
3097 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3098 if(uErr) {
3099 return 21;
3100 }
3101
3102 static const uint8_t spEmpty[] = {0xA0};
3103 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3104 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3105 &CompareDiagnostics)) {
3106 return 22;
3107 }
3108
3109 /* --- Several levels of nested sorted maps --- */
3110 QCBOREncode_Init(&EC, TestBuf);
3111 QCBOREncode_OpenMap(&EC);
3112 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3113 QCBOREncode_OpenMapInMapN(&EC, 428);
3114 QCBOREncode_AddNULLToMap(&EC, "null");
3115 QCBOREncode_OpenArrayInMap(&EC, "array");
3116 QCBOREncode_AddSZString(&EC, "hi");
3117 QCBOREncode_AddSZString(&EC, "there");
3118 QCBOREncode_CloseArray(&EC);
3119 QCBOREncode_OpenMapInMap(&EC, "empty2");
3120 QCBOREncode_CloseAndSortMap(&EC);
3121 QCBOREncode_OpenMapInMap(&EC, "empty1");
3122 QCBOREncode_CloseAndSortMap(&EC);
3123 QCBOREncode_CloseAndSortMap(&EC);
3124 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3125 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3126 QCBOREncode_CloseAndSortMap(&EC);
3127 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3128 if(uErr) {
3129 return 31;
3130 }
3131 static const uint8_t spNested[] = {
3132 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3133 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3134 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3135 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3136 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3137 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3138 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3139 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3140
3141 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3142 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested),
3143 &CompareDiagnostics)) {
3144 return 32;
3145 }
3146
3147 /* --- Degenerate case of everything in order --- */
3148 QCBOREncode_Init(&EC, TestBuf);
3149 QCBOREncode_OpenMap(&EC);
3150 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3151 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3152 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3153 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3154 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3155 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3156 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3157 QCBOREncode_CloseAndSortMap(&EC);
3158 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3159 if(uErr) {
3160 return 41;
3161 }
3162
3163 static const uint8_t sp6Items[] = {
3164 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3165 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3166 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3167 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3168 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3169 &CompareDiagnostics)) {
3170 return 42;
3171 }
3172
3173 /* --- Degenerate case -- reverse order --- */
3174 QCBOREncode_Init(&EC, TestBuf);
3175 QCBOREncode_OpenMap(&EC);
3176 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3177 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3178 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3179 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3180 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3181 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3182 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3183 QCBOREncode_CloseAndSortMap(&EC);
3184 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3185 if(uErr) {
3186 return 51;
3187 }
3188
3189 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3190 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3191 &CompareDiagnostics)) {
3192 return 52;
3193 }
3194
3195 /* --- Same items, randomly out of order --- */
3196 QCBOREncode_Init(&EC, TestBuf);
3197 QCBOREncode_OpenMap(&EC);
3198 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3199 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3200 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3201 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3202 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3203 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3204 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3205 QCBOREncode_CloseAndSortMap(&EC);
3206 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3207 if(uErr) {
3208 return 61;
3209 }
3210
3211 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3212 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3213 &CompareDiagnostics)) {
3214 return 62;
3215 }
3216
3217 /* --- Stuff in front of and after array to sort --- */
3218 QCBOREncode_Init(&EC, TestBuf);
3219 QCBOREncode_OpenArray(&EC);
3220 QCBOREncode_AddInt64(&EC, 111);
3221 QCBOREncode_AddInt64(&EC, 222);
3222 QCBOREncode_OpenMap(&EC);
3223 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3224 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3225 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3226 QCBOREncode_CloseAndSortMap(&EC);
3227 QCBOREncode_AddInt64(&EC, 888);
3228 QCBOREncode_AddInt64(&EC, 999);
3229 QCBOREncode_CloseArray(&EC);
3230 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3231 if(uErr) {
3232 return 71;
3233 }
3234
3235 static const uint8_t spPreItems[] = {
3236 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3237 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3238 0x03, 0xE7};
3239 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3240 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3241 &CompareDiagnostics)) {
3242 return 72;
3243 }
3244
3245 /* --- map with labels of all CBOR major types and in reverse order --- */
3246 QCBOREncode_Init(&EC, TestBuf);
3247 QCBOREncode_OpenMap(&EC);
3248
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003249 /* Adding labels directly rather than AddToMap functions */
3250
3251#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003252 QCBOREncode_AddDouble(&EC, 8.77);
3253 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003254#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003255
3256 QCBOREncode_AddBool(&EC, true);
3257 QCBOREncode_AddInt64(&EC, 6);
3258
3259 QCBOREncode_AddDateEpoch(&EC, 88);
3260 QCBOREncode_AddInt64(&EC, 5);
3261
3262 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3263 QCBOREncode_AddInt64(&EC, 4);
3264
3265 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3266 QCBOREncode_AddInt64(&EC, 7);
3267
3268 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3269
3270 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3271 QCBOREncode_AddInt64(&EC, 2);
3272
3273 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3274 QCBOREncode_CloseAndSortMap(&EC);
3275
3276 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3277 if(uErr) {
3278 return 81;
3279 }
3280
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003281#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003282 static const uint8_t spLabelTypes[] = {
3283 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3284 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3285 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3286 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3287 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003288#else
3289 static const uint8_t spLabelTypes[] = {
3290 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3291 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3292 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3293#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3294
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003295 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3296 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3297 &CompareDiagnostics)) {
3298 return 82;
3299 }
3300
3301 /* --- labels are indefinitely encoded --- */
3302 QCBOREncode_Init(&EC, TestBuf);
3303 QCBOREncode_OpenMap(&EC);
3304
3305 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3306
3307 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3308
3309 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3310 QCBOREncode_AddInt64(&EC, 3);
3311
3312 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3313 QCBOREncode_AddInt64(&EC, 4);
3314
3315 QCBOREncode_CloseAndSortMap(&EC);
3316
3317 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3318 if(uErr) {
3319 return 91;
3320 }
3321
3322 static const uint8_t spIndefItems[] = {
3323 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3324 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3325 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3326 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3327 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3328 &CompareDiagnostics)) {
3329 return 92;
3330 }
3331
3332 /* --- Indefinitely encoded maps --- */
3333 QCBOREncode_Init(&EC, TestBuf);
3334 QCBOREncode_OpenMapIndefiniteLength(&EC);
3335
3336 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3337 QCBOREncode_CloseMapIndefiniteLength(&EC);
3338
3339 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3340 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3341
3342 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3343 QCBOREncode_CloseMapIndefiniteLength(&EC);
3344
3345 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3346 QCBOREncode_CloseMapIndefiniteLength(&EC);
3347
3348 QCBOREncode_CloseAndSortMapIndef(&EC);
3349 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3350 if(uErr) {
3351 return 101;
3352 }
3353
3354 static const uint8_t spIndeMaps[] = {
3355 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3356 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3357 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3358 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3359 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3360 &CompareDiagnostics)) {
3361 return 102;
3362 }
3363
3364 return 0;
3365}
Laurence Lundblade240ca822024-01-16 11:11:00 -07003366
3367
3368#include <math.h> /* For INFINITY and NAN and isnan() */
3369
3370
3371int32_t CDETest(void)
3372{
3373 QCBOREncodeContext EC;
3374 UsefulBufC Encoded;
3375
3376 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3377
3378 QCBOREncode_SerializationCDE(&EC);
3379
3380 /* Items added to test sorting and preferred encoding of numbers and floats */
3381 QCBOREncode_OpenMap(&EC);
3382 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3383 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3384 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3385 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3386 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3387 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3388
3389 QCBOREncode_CloseMap(&EC);
3390
3391 QCBOREncode_Finish(&EC, &Encoded);
3392
3393 static const uint8_t spExpectedCDE[] = {
3394 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3395 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3396 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3397 0x61, 0x78, 0xF9, 0x40, 0x00};
3398
3399 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3400 Encoded)) {
3401 return 1;
3402 }
3403
3404 /* Next, make sure methods that encode non-CDE error out */
3405 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3406 QCBOREncode_SerializationCDE(&EC);
3407 QCBOREncode_OpenMapIndefiniteLength(&EC);
3408 QCBOREncode_CloseMap(&EC);
3409 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_NOT_PREFERRED) {
3410 return 100;
3411 }
3412
3413
3414 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3415 QCBOREncode_SerializationCDE(&EC);
3416 QCBOREncode_AddDoubleNoPreferred(&EC, 0);
3417 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_NOT_PREFERRED) {
3418 return 101;
3419 }
3420
3421 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3422 QCBOREncode_SerializationCDE(&EC);
3423 QCBOREncode_AddFloatNoPreferred(&EC, 0);
3424 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_NOT_PREFERRED) {
3425 return 101;
3426 }
3427
3428 return 0;
3429}
3430
3431
3432int32_t DCBORTest(void)
3433{
3434 QCBOREncodeContext EC;
3435 UsefulBufC Encoded;
3436
3437 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3438
3439 QCBOREncode_SerializationdCBOR(&EC);
3440
3441 /* Items added to test sorting and preferred encoding of numbers and floats */
3442 QCBOREncode_OpenMap(&EC);
3443 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3444 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3445 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3446 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3447 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3448
3449 QCBOREncode_CloseMap(&EC);
3450
3451 QCBOREncode_Finish(&EC, &Encoded);
3452
3453 static const uint8_t spExpecteddCBOR[] = {
3454 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3455 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3456 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3457
3458 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3459 Encoded)) {
3460 return 1;
3461 }
3462
3463
3464 /* Next, make sure methods that encode non-CDE error out */
3465 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3466 QCBOREncode_SerializationdCBOR(&EC);
3467 QCBOREncode_OpenMapIndefiniteLength(&EC);
3468 QCBOREncode_CloseMap(&EC);
3469 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_NOT_PREFERRED) {
3470 return 100;
3471 }
3472
3473 /* Next, make sure methods that encode non-CDE error out */
3474 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3475 QCBOREncode_SerializationdCBOR(&EC);
3476 QCBOREncode_AddUndef(&EC);
3477 QCBOREncode_CloseMap(&EC);
3478 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_NOT_PREFERRED) {
3479 return 101;
3480 }
3481
3482
3483 return 0;
3484
3485}