blob: 7645475d368c41006b9f353c5569a5b2f7b22379 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundblade70fc1252024-05-31 10:57:28 -070051#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 Lundbladecbd7d132024-05-19 11:11:22 -0700642 QCBOREncode_AddSimple(pECtx, 255);
643 QCBOREncode_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700644 QCBOREncode_AddSZString(pECtx, "s3");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700646 QCBOREncode_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700647 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
648 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700649 QCBOREncode_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700650 QCBOREncode_AddInt64(pECtx, 89); // label before tag
651 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700652 QCBOREncode_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;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800695 UsefulBufC Enc;
696 size_t size;
697 int nReturn;
698 QCBORError uExpectedErr;
699
700 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530702 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800703 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2d493002024-02-01 11:09:17 -0700705 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706
Laurence Lundblade0595e932018-11-02 22:22:47 +0700707 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800708 nReturn = -1;
709 goto Done;
710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800711
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700712 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530713 nReturn = -2;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800714 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700715 }
716
717
718 /* Also test size calculation */
719 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800720 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700721
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800722 AddAll(&ECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700723
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700724 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
725 nReturn = -10;
726 goto Done;
727 }
728
729 if(size != sizeof(spExpectedEncodedAll)) {
730 nReturn = -11;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800731 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800733
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800734#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
735 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
736#else
737 uExpectedErr = QCBOR_SUCCESS;
738#endif
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800739 /* Test the QCBOR_ERR_NOT_ALLOWED error codes */
740 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
741 QCBOREncode_AddNegativeUInt64(&ECtx, 1);
742 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
743 nReturn = -21;
744 goto Done;
745 }
746
Laurence Lundbladed3f07842024-06-19 13:05:07 -0700747
748#if !defined(QCBOR_DISABLE_ENCODE_USAGE_GUARDS) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
749 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
750#else
751 uExpectedErr = QCBOR_SUCCESS;
752#endif
753
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800754#ifndef USEFULBUF_DISABLE_ALL_FLOAT
755 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
756 /* 0x7ff8000000000001ULL is a NaN with a payload. */
757 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
758 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
759 nReturn = -22;
760 goto Done;
761 }
762
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800763
764 /* 0x7ffc000000000000ULL is a NaN with a payload. */
765 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
766 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
767 nReturn = -23;
768 goto Done;
769 }
770
771 /* 0x7ff80001UL is a NaN with a payload. */
772 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
773 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
774 nReturn = -24;
775 goto Done;
776 }
777
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800778 /* 0x7ffc0000UL is a NaN with a payload. */
779 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
780 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
781 nReturn = -25;
782 goto Done;
783 }
784#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */
785
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786Done:
787 return nReturn;
788}
789
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700790
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530791/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200792 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530793 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
794 3B 0000000100000000 # negative(4294967296)
795 3A FFFFFFFF # negative(4294967295)
796 3A FFFFFFFE # negative(4294967294)
797 3A FFFFFFFD # negative(4294967293)
798 3A 7FFFFFFF # negative(2147483647)
799 3A 7FFFFFFE # negative(2147483646)
800 3A 00010001 # negative(65537)
801 3A 00010000 # negative(65536)
802 39 FFFF # negative(65535)
803 39 FFFE # negative(65534)
804 39 FFFD # negative(65533)
805 39 0100 # negative(256)
806 38 FF # negative(255)
807 38 FE # negative(254)
808 38 FD # negative(253)
809 38 18 # negative(24)
810 37 # negative(23)
811 36 # negative(22)
812 20 # negative(0)
813 00 # unsigned(0)
814 00 # unsigned(0)
815 01 # unsigned(1)
816 16 # unsigned(22)
817 17 # unsigned(23)
818 18 18 # unsigned(24)
819 18 19 # unsigned(25)
820 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200821 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530822 18 FE # unsigned(254)
823 18 FF # unsigned(255)
824 19 0100 # unsigned(256)
825 19 0101 # unsigned(257)
826 19 FFFE # unsigned(65534)
827 19 FFFF # unsigned(65535)
828 1A 00010000 # unsigned(65536)
829 1A 00010001 # unsigned(65537)
830 1A 00010002 # unsigned(65538)
831 1A 7FFFFFFF # unsigned(2147483647)
832 1A 7FFFFFFF # unsigned(2147483647)
833 1A 80000000 # unsigned(2147483648)
834 1A 80000001 # unsigned(2147483649)
835 1A FFFFFFFE # unsigned(4294967294)
836 1A FFFFFFFF # unsigned(4294967295)
837 1B 0000000100000000 # unsigned(4294967296)
838 1B 0000000100000001 # unsigned(4294967297)
839 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
840 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
841 */
842static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200843 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800844 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
845 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
846 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
847 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
848 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
849 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
850 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
851 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
852 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
853 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200854 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
855 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
856 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
857 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
858 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
859 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
860 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
861 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
862 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
863 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
864 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
865 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
866 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867
868/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 Test the generation of integers. This also ends up testing
871 encoding of all the different lengths. It encodes integers
872 of many lengths and values, especially around the boundaries
873 for different types of integers. It compares the output
874 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800875
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800876 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300877int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878{
879 QCBOREncodeContext ECtx;
880 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530882 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800883 QCBOREncode_OpenArray(&ECtx);
884
885 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
886 QCBOREncode_AddInt64(&ECtx, -4294967297);
887 QCBOREncode_AddInt64(&ECtx, -4294967296);
888 QCBOREncode_AddInt64(&ECtx, -4294967295);
889 QCBOREncode_AddInt64(&ECtx, -4294967294);
890 QCBOREncode_AddInt64(&ECtx, -2147483648);
891 QCBOREncode_AddInt64(&ECtx, -2147483647);
892 QCBOREncode_AddInt64(&ECtx, -65538);
893 QCBOREncode_AddInt64(&ECtx, -65537);
894 QCBOREncode_AddInt64(&ECtx, -65536);
895 QCBOREncode_AddInt64(&ECtx, -65535);
896 QCBOREncode_AddInt64(&ECtx, -65534);
897 QCBOREncode_AddInt64(&ECtx, -257);
898 QCBOREncode_AddInt64(&ECtx, -256);
899 QCBOREncode_AddInt64(&ECtx, -255);
900 QCBOREncode_AddInt64(&ECtx, -254);
901 QCBOREncode_AddInt64(&ECtx, -25);
902 QCBOREncode_AddInt64(&ECtx, -24);
903 QCBOREncode_AddInt64(&ECtx, -23);
904 QCBOREncode_AddInt64(&ECtx, -1);
905 QCBOREncode_AddInt64(&ECtx, 0);
906 QCBOREncode_AddUInt64(&ECtx, 0ULL);
907 QCBOREncode_AddInt64(&ECtx, 1);
908 QCBOREncode_AddInt64(&ECtx, 22);
909 QCBOREncode_AddInt64(&ECtx, 23);
910 QCBOREncode_AddInt64(&ECtx, 24);
911 QCBOREncode_AddInt64(&ECtx, 25);
912 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200913 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 QCBOREncode_AddInt64(&ECtx, 254);
915 QCBOREncode_AddInt64(&ECtx, 255);
916 QCBOREncode_AddInt64(&ECtx, 256);
917 QCBOREncode_AddInt64(&ECtx, 257);
918 QCBOREncode_AddInt64(&ECtx, 65534);
919 QCBOREncode_AddInt64(&ECtx, 65535);
920 QCBOREncode_AddInt64(&ECtx, 65536);
921 QCBOREncode_AddInt64(&ECtx, 65537);
922 QCBOREncode_AddInt64(&ECtx, 65538);
923 QCBOREncode_AddInt64(&ECtx, 2147483647);
924 QCBOREncode_AddInt64(&ECtx, 2147483647);
925 QCBOREncode_AddInt64(&ECtx, 2147483648);
926 QCBOREncode_AddInt64(&ECtx, 2147483649);
927 QCBOREncode_AddInt64(&ECtx, 4294967294);
928 QCBOREncode_AddInt64(&ECtx, 4294967295);
929 QCBOREncode_AddInt64(&ECtx, 4294967296);
930 QCBOREncode_AddInt64(&ECtx, 4294967297);
931 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
932 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800933
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800935
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530936 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700937 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 nReturn = -1;
939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800940
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530941 if(CheckResults(Enc, spExpectedEncodedInts))
942 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800943
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800944 return(nReturn);
945}
946
947
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530948/*
949 85 # array(5)
950 F5 # primitive(21)
951 F4 # primitive(20)
952 F6 # primitive(22)
953 F7 # primitive(23)
954 A1 # map(1)
955 65 # text(5)
956 554E446566 # "UNDef"
957 F7 # primitive(23)
958 */
959static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
961
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300962int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800963{
964 QCBOREncodeContext ECtx;
965 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800966
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530967 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900969
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700970 QCBOREncode_AddBool(&ECtx, true);
971 QCBOREncode_AddBool(&ECtx, false);
972 QCBOREncode_AddNULL(&ECtx);
973 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800974
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 QCBOREncode_OpenMap(&ECtx);
976
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700977 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 QCBOREncode_CloseMap(&ECtx);
979
980 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530982 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700983 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984 nReturn = -1;
985 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800986
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530987 if(CheckResults(ECBOR, spExpectedEncodedSimple))
988 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800989
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800990 return(nReturn);
991}
992
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700993#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Jan Jongboom47d86c52019-07-25 08:54:16 +0200994/*
995 9F # array(5)
996 F5 # primitive(21)
997 F4 # primitive(20)
998 F6 # primitive(22)
999 F7 # primitive(23)
1000 BF # map(1)
1001 65 # text(5)
1002 554E446566 # "UNDef"
1003 F7 # primitive(23)
1004 FF # break
1005 FF # break
1006 */
1007static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
1008 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
1009
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001010int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +02001011{
1012 QCBOREncodeContext ECtx;
1013 int nReturn = 0;
1014
1015 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1016 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
1017
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001018 QCBOREncode_AddBool(&ECtx, true);
1019 QCBOREncode_AddBool(&ECtx, false);
1020 QCBOREncode_AddNULL(&ECtx);
1021 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +02001022
1023 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
1024
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001025 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +02001026 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
1027
1028 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
1029
1030 UsefulBufC ECBOR;
1031 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1032 nReturn = -1;
1033 }
1034
1035 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
1036 return -2;
1037
1038 return(nReturn);
1039}
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001040#endif
Jan Jongboom47d86c52019-07-25 08:54:16 +02001041
Jan Jongboom5d827882019-08-07 12:51:15 +02001042/*
1043A5 # map(5)
1044 63 # text(3)
1045 617272 # "arr"
1046 98 1F # array(31)
1047 00 # unsigned(0)
1048 01 # unsigned(1)
1049 02 # unsigned(2)
1050 03 # unsigned(3)
1051 04 # unsigned(4)
1052 05 # unsigned(5)
1053 06 # unsigned(6)
1054 07 # unsigned(7)
1055 08 # unsigned(8)
1056 09 # unsigned(9)
1057 0A # unsigned(10)
1058 0B # unsigned(11)
1059 0C # unsigned(12)
1060 0D # unsigned(13)
1061 0E # unsigned(14)
1062 0F # unsigned(15)
1063 10 # unsigned(16)
1064 11 # unsigned(17)
1065 12 # unsigned(18)
1066 13 # unsigned(19)
1067 14 # unsigned(20)
1068 15 # unsigned(21)
1069 16 # unsigned(22)
1070 17 # unsigned(23)
1071 18 18 # unsigned(24)
1072 18 19 # unsigned(25)
1073 18 1A # unsigned(26)
1074 18 1B # unsigned(27)
1075 18 1C # unsigned(28)
1076 18 1D # unsigned(29)
1077 18 1E # unsigned(30)
1078 63 # text(3)
1079 6D6170 # "map"
1080 B8 1F # map(31)
1081 61 # text(1)
1082 61 # "a"
1083 00 # unsigned(0)
1084 61 # text(1)
1085 62 # "b"
1086 01 # unsigned(1)
1087 61 # text(1)
1088 63 # "c"
1089 02 # unsigned(2)
1090 61 # text(1)
1091 64 # "d"
1092 03 # unsigned(3)
1093 61 # text(1)
1094 65 # "e"
1095 04 # unsigned(4)
1096 61 # text(1)
1097 66 # "f"
1098 05 # unsigned(5)
1099 61 # text(1)
1100 67 # "g"
1101 06 # unsigned(6)
1102 61 # text(1)
1103 68 # "h"
1104 07 # unsigned(7)
1105 61 # text(1)
1106 69 # "i"
1107 08 # unsigned(8)
1108 61 # text(1)
1109 6A # "j"
1110 09 # unsigned(9)
1111 61 # text(1)
1112 6B # "k"
1113 0A # unsigned(10)
1114 61 # text(1)
1115 6C # "l"
1116 0B # unsigned(11)
1117 61 # text(1)
1118 6D # "m"
1119 0C # unsigned(12)
1120 61 # text(1)
1121 6E # "n"
1122 0D # unsigned(13)
1123 61 # text(1)
1124 6F # "o"
1125 0E # unsigned(14)
1126 61 # text(1)
1127 70 # "p"
1128 0F # unsigned(15)
1129 61 # text(1)
1130 71 # "q"
1131 10 # unsigned(16)
1132 61 # text(1)
1133 72 # "r"
1134 11 # unsigned(17)
1135 61 # text(1)
1136 73 # "s"
1137 12 # unsigned(18)
1138 61 # text(1)
1139 74 # "t"
1140 13 # unsigned(19)
1141 61 # text(1)
1142 75 # "u"
1143 14 # unsigned(20)
1144 61 # text(1)
1145 76 # "v"
1146 15 # unsigned(21)
1147 61 # text(1)
1148 77 # "w"
1149 16 # unsigned(22)
1150 61 # text(1)
1151 78 # "x"
1152 17 # unsigned(23)
1153 61 # text(1)
1154 79 # "y"
1155 18 18 # unsigned(24)
1156 61 # text(1)
1157 7A # "z"
1158 18 19 # unsigned(25)
1159 61 # text(1)
1160 41 # "A"
1161 18 1A # unsigned(26)
1162 61 # text(1)
1163 42 # "B"
1164 18 1B # unsigned(27)
1165 61 # text(1)
1166 43 # "C"
1167 18 1C # unsigned(28)
1168 61 # text(1)
1169 44 # "D"
1170 18 1D # unsigned(29)
1171 61 # text(1)
1172 45 # "E"
1173 18 1E # unsigned(30)
1174 65 # text(5)
1175 6D696E3331 # "min31"
1176 38 1E # negative(30)
1177 66 # text(6)
1178 706C75733331 # "plus31"
1179 18 1F # unsigned(31)
1180 63 # text(3)
1181 737472 # "str"
1182 78 1F # text(31)
1183 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1184 */
1185static const uint8_t EncodeLengthThirtyone[] = {
1186 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1187 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1188 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1189 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1190 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1191 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1192 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1193 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1194 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1195 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1196 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1197 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1198 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1199 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1200 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1201 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1202 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1203 0x31
1204};
1205
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001206int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001207{
1208 QCBOREncodeContext ECtx;
1209 int nReturn = 0;
1210
1211 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1212 QCBOREncode_OpenMap(&ECtx);
1213
1214 // add array with 31 items
1215 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1216 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001217 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001218 }
1219 QCBOREncode_CloseArray(&ECtx);
1220
1221 // add map with 31 items
1222 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001223 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001224 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001225 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001226 if (ix < 26) c = c + ix;
1227 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001228 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001229 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1230 }
1231 QCBOREncode_CloseMap(&ECtx);
1232
1233 // add -31 and +31
1234 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1235 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1236
1237 // add string with length 31
1238 const char *str = "testtesttesttesttesttestqcbor11";
1239 UsefulBufC str_b = { str, 31 };
1240 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1241
1242 QCBOREncode_CloseMap(&ECtx);
1243
1244 UsefulBufC ECBOR;
1245 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1246 nReturn = -1;
1247 }
1248
1249 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1250 return -2;
1251
1252 return(nReturn);
1253}
1254
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301255
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301256/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001257 * [ "2013-03-21T20:04:00Z",
1258 * 0("2013-03-21T20:04:00Z"),
1259 * 1363896240,
1260 * 1(1363896240),
1261 * 100(-10676),
1262 * 3994,
1263 * 1004("1940-10-09"),
1264 * "1980-12-08",
1265 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1266 * "SD": 1(999),
1267 * "Sample Date from RFC 8943": "1985-04-12",
1268 * 42: 1004("1985-04-12T23:20:50.52Z"),
1269 * "SY": 100(-10676),
1270 * 45: 3994
1271 * }
1272 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301273 */
1274static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001275 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1276 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1277 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1278 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1279 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1280 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1281 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1282 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1283 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1284 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1285 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1286 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1287 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1288 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1289 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1290 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1291 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1292 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1293 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1294 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1295 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1296 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1297 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001299int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300{
1301 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001302
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301303 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001305 QCBOREncode_OpenArray(&ECtx);
1306
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001307 /* The values are taken from the CBOR RFCs */
1308 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1309 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1310 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1311 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1312 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1313 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1314 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1315 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317 QCBOREncode_OpenMap(&ECtx);
1318
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001319 QCBOREncode_AddDateStringToMap(&ECtx,
1320 "Sample Date from RFC 3339",
1321 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001322 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001323 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1324 "Sample Date from RFC 8943",
1325 QCBOR_ENCODE_AS_BORROWED,
1326 "1985-04-12");
1327 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1328 42,
1329 QCBOR_ENCODE_AS_TAG,
1330 "1985-04-12T23:20:50.52Z");
1331 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1332 "SY",
1333 QCBOR_ENCODE_AS_TAG,
1334 -10676);
1335 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1336 45,
1337 QCBOR_ENCODE_AS_BORROWED,
1338 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001340 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001341
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342 QCBOREncode_CloseArray(&ECtx);
1343
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301344 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001345 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001346 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301349 if(CheckResults(ECBOR, spExpectedEncodedDates))
1350 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001351
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001352 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353}
1354
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301355
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001356int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357{
1358 QCBOREncodeContext ECtx;
1359 int i;
1360 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001361
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301362 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1364 QCBOREncode_OpenArray(&ECtx);
1365 }
1366 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1367 QCBOREncode_CloseArray(&ECtx);
1368 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001369 UsefulBufC Encoded;
1370 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371 nReturn = -1;
1372 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001373
1374 return(nReturn);
1375}
1376
1377
1378
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001379int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380{
1381 QCBOREncodeContext ECtx;
1382 int i;
1383 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001384
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301385 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1387 QCBOREncode_OpenArray(&ECtx);
1388 }
1389 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1390 QCBOREncode_CloseArray(&ECtx);
1391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
Laurence Lundblade0595e932018-11-02 22:22:47 +07001393 UsefulBufC Encoded;
1394 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001395 nReturn = -1;
1396 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001397
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 return(nReturn);
1399}
1400
1401
1402
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001403int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404{
1405 QCBOREncodeContext ECtx;
1406 int i;
1407 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301409 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001410 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1411 QCBOREncode_OpenArray(&ECtx);
1412 }
1413 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1414 QCBOREncode_CloseArray(&ECtx);
1415 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001416 UsefulBufC Encoded;
1417 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 nReturn = -1;
1419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001420
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421 return(nReturn);
1422}
1423
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301425/*
1426 81 # array(1)
1427 81 # array(1)
1428 81 # array(1)
1429 81 # array(1)
1430 80 # array(0)
1431*/
1432static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001434// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301435/*
1436 82 # array(2)
1437 81 # array(1)
1438 81 # array(1)
1439 81 # array(1)
1440 81 # array(1)
1441 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001442 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301443 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1444 3B 0000000100000000 # negative(4294967296)
1445 3A FFFFFFFF # negative(4294967295)
1446 3A FFFFFFFE # negative(4294967294)
1447 3A FFFFFFFD # negative(4294967293)
1448 3A 7FFFFFFF # negative(2147483647)
1449 3A 7FFFFFFE # negative(2147483646)
1450 3A 00010001 # negative(65537)
1451 3A 00010000 # negative(65536)
1452 39 FFFF # negative(65535)
1453 39 FFFE # negative(65534)
1454 39 FFFD # negative(65533)
1455 39 0100 # negative(256)
1456 38 FF # negative(255)
1457 38 FE # negative(254)
1458 38 FD # negative(253)
1459 38 18 # negative(24)
1460 37 # negative(23)
1461 36 # negative(22)
1462 20 # negative(0)
1463 00 # unsigned(0)
1464 00 # unsigned(0)
1465 01 # unsigned(1)
1466 16 # unsigned(22)
1467 17 # unsigned(23)
1468 18 18 # unsigned(24)
1469 18 19 # unsigned(25)
1470 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001471 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301472 18 FE # unsigned(254)
1473 18 FF # unsigned(255)
1474 19 0100 # unsigned(256)
1475 19 0101 # unsigned(257)
1476 19 FFFE # unsigned(65534)
1477 19 FFFF # unsigned(65535)
1478 1A 00010000 # unsigned(65536)
1479 1A 00010001 # unsigned(65537)
1480 1A 00010002 # unsigned(65538)
1481 1A 7FFFFFFF # unsigned(2147483647)
1482 1A 7FFFFFFF # unsigned(2147483647)
1483 1A 80000000 # unsigned(2147483648)
1484 1A 80000001 # unsigned(2147483649)
1485 1A FFFFFFFE # unsigned(4294967294)
1486 1A FFFFFFFF # unsigned(4294967295)
1487 1B 0000000100000000 # unsigned(4294967296)
1488 1B 0000000100000001 # unsigned(4294967297)
1489 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1490 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1491 */
1492static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001493 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1495 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1496 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1497 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1498 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1499 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1500 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1501 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1502 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1503 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1504 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001505 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1506 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1507 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1508 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1509 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1510 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1511 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1512 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1513 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1514 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1515 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1516 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1517 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518
1519
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001520int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001522 QCBOREncodeContext ECtx;
1523
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301524 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001525 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301526 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1527 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001528 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001530 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundblade0595e932018-11-02 22:22:47 +07001532 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001533 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001534 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001535
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301536 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001537 return -5;
1538 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001539
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001540 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541}
1542
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301543/*
1544 This returns a pointer to spBigBuf
1545 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001546static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547{
1548 QCBOREncodeContext ECtx;
1549 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 *pEncoded = NULL;
1552 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301553 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001554
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 // loop runs CBOR encoding twice. First with no buffer to
1556 // calucate the length so buffer can be allocated correctly,
1557 // and last with the buffer to do the actual encoding
1558 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301559 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 QCBOREncode_OpenMap(&ECtx);
1561 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1562 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1563 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1564 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1565 QCBOREncode_CloseArray(&ECtx);
1566 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1567 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1568 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1569 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1570 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1571 QCBOREncode_CloseMap(&ECtx);
1572 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001573
Laurence Lundblade0595e932018-11-02 22:22:47 +07001574 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 goto Done;
1576 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301577 if(uFirstSizeEstimate != *pEncodedLen) {
1578 nReturn = 1;
1579 } else {
1580 nReturn = 0;
1581 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 goto Done;
1583 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301584 *pEncoded = spBigBuf;
1585 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001588
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 Done:
1590 return(nReturn);
1591}
1592
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301593/*
1594 A3 # map(3)
1595 6D # text(13)
1596 666972737420696E7465676572 # "first integer"
1597 18 2A # unsigned(42)
1598 77 # text(23)
1599 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1600 82 # array(2)
1601 67 # text(7)
1602 737472696E6731 # "string1"
1603 67 # text(7)
1604 737472696E6732 # "string2"
1605 6C # text(12)
1606 6D617020696E2061206D6170 # "map in a map"
1607 A4 # map(4)
1608 67 # text(7)
1609 62797465732031 # "bytes 1"
1610 44 # bytes(4)
1611 78787878 # "xxxx"
1612 67 # text(7)
1613 62797465732032 # "bytes 2"
1614 44 # bytes(4)
1615 79797979 # "yyyy"
1616 6B # text(11)
1617 616E6F7468657220696E74 # "another int"
1618 18 62 # unsigned(98)
1619 66 # text(6)
1620 746578742032 # "text 2"
1621 78 1E # text(30)
1622 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1623 */
1624static const uint8_t spValidMapEncoded[] = {
1625 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1626 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1627 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1628 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1629 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1630 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1631 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1632 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1633 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1634 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1635 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1636 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1637 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1638 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1639 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1640 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001641
1642
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001643int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001644{
1645 uint8_t *pEncodedMaps;
1646 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001648 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301649 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001650 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001651
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001652 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301653 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1654 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001655
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001656 return(nReturn);
1657}
1658
1659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660/*
1661 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundbladeee851742020-01-08 08:37:05 -08001663 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1664 CBOR_SIMPLEV_NULL
1665 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001666 @param[in] szAlexString Diagnostic code.
1667 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001668 @param[in/out] pnLen Size of pOut buffer when called; length of data
1669 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671 @return
1672 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674 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 -08001675 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001676 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1679 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001681 */
1682
Laurence Lundbladeee851742020-01-08 08:37:05 -08001683static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001684FormatRTICResults(uint8_t uRResult,
1685 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001686 const char *szType,
1687 const char *szAlexString,
1688 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001689{
1690 // Buffer that the result will be written in to
1691 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001692 // QCBOREncode will never write off the end of this buffer. If it won't
1693 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001694
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001695 // Context for the encoder
1696 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301697 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001699 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1700 // Contents are label / value pairs
1701 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001703 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
Laurence Lundbladeee851742020-01-08 08:37:05 -08001705 // The result: 0 if scan happened and found nothing; 1 if it happened and
1706 // found something wrong; 2 if it didn't happen
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001707 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001708
1709 // Add the diagnostic code
1710 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1711
1712 // Add a time stamp
1713 if(time) {
1714 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1715 }
1716
1717 // Add the diagnostic code
1718 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1719
1720 // Open a subordinate map for telemtry data
1721 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1722
1723 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001724
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001725 // Add a few fake integers and buffers for now.
1726 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1727
1728 // Add a few fake integers and buffers for now.
1729 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1730
1731 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301732 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001733 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001734
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001735 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1736 }
1737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001738
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001739 // Close the telemetry map
1740 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001741
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742 // Close the map
1743 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001744
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301745 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001746
Laurence Lundblade0595e932018-11-02 22:22:47 +07001747 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001748
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301749 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001750}
1751
1752
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301753/*
1754 A5 # map(5)
1755 69 # text(9)
1756 696E74656772697479 # "integrity"
1757 F4 # primitive(20)
1758 64 # text(4)
1759 74797065 # "type"
1760 66 # text(6)
1761 726563656E74 # "recent"
1762 64 # text(4)
1763 74696D65 # "time"
1764 C1 # tag(1)
1765 1A 580D4172 # unsigned(1477263730)
1766 64 # text(4)
1767 64696167 # "diag"
1768 6A # text(10)
1769 30784131654335303031 # "0xA1eC5001"
1770 69 # text(9)
1771 74656C656D65747279 # "telemetry"
1772 A3 # map(3)
1773 69 # text(9)
1774 53686F652053697A65 # "Shoe Size"
1775 0C # unsigned(12)
1776 62 # text(2)
1777 4951 # "IQ"
1778 1A FFFFFFFF # unsigned(4294967295)
1779 77 # text(23)
1780 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1781 48 # bytes(8)
1782 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1783 */
1784static const uint8_t spExpectedRTIC[] = {
1785 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1786 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1787 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1788 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1789 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1790 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1791 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1792 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1793 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1794 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1795 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1796 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001797
1798
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001799int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001800{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001801 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301802 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301803 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301804 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001805 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301806 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001807
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301808 if(CheckResults(Encoded, spExpectedRTIC)) {
1809 return -2;
1810 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001811
1812 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001813}
1814
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301815
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301816/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001817 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001818
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301819 82 # array(2)
1820 19 01C3 # unsigned(451)
1821 43 # bytes(3)
1822 1901D2 # "\x19\x01\xD2"
1823*/
1824static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301825
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001826static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1827
Laurence Lundbladeda532272019-04-07 11:40:17 -07001828/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001829 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001830 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001831int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001832{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001833 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001834
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001835 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301836 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001837
Laurence Lundblade684aec22018-10-12 19:33:53 +08001838 QCBOREncode_OpenArray(&EC);
1839 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001840
Laurence Lundblade684aec22018-10-12 19:33:53 +08001841 QCBOREncode_BstrWrap(&EC);
1842 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001843
Laurence Lundblade684aec22018-10-12 19:33:53 +08001844 UsefulBufC Wrapped;
1845 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001846
Laurence Lundblade684aec22018-10-12 19:33:53 +08001847 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001848
Laurence Lundblade684aec22018-10-12 19:33:53 +08001849 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001850 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001851 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001852 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001853
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301854 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001855 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001856 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001857
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001858 // Second test - see if the length of the wrapped
1859 // bstr is correct. Also tests bstr wrapping
1860 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001861 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1862 QCBOREncode_OpenArray(&EC);
1863 QCBOREncode_BstrWrap(&EC);
1864 QCBOREncode_OpenArray(&EC);
1865 QCBOREncode_AddNULL(&EC);
1866 QCBOREncode_CloseArray(&EC);
1867 UsefulBufC BStr;
1868 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001869 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1870 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001871 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001872 return -5;
1873 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001874
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001875
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001876 // Fourth test, cancelling a byte string
1877 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1878
1879 QCBOREncode_OpenArray(&EC);
1880 QCBOREncode_AddUInt64(&EC, 451);
1881
1882 QCBOREncode_BstrWrap(&EC);
1883 QCBOREncode_CancelBstrWrap(&EC);
1884
1885
1886 QCBOREncode_AddUInt64(&EC, 42);
1887 QCBOREncode_CloseArray(&EC);
1888 if(QCBOREncode_Finish(&EC, &Encoded)) {
1889 return -8;
1890 }
1891 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1892 return -9;
1893 }
1894
1895 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001896 // Fifth test, failed cancelling
1897 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1898
1899 QCBOREncode_OpenArray(&EC);
1900 QCBOREncode_AddUInt64(&EC, 451);
1901
1902 QCBOREncode_BstrWrap(&EC);
1903 QCBOREncode_AddUInt64(&EC, 99);
1904 QCBOREncode_CancelBstrWrap(&EC);
1905
1906 QCBOREncode_AddUInt64(&EC, 42);
1907 QCBOREncode_CloseArray(&EC);
1908 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001909#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001910 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1911 return -10;
1912 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001913#else
1914 if(uErr != QCBOR_SUCCESS) {
1915 return -110;
1916 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001917#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1918
1919 // Sixth test, another cancel, but the error is not caught
1920 // This use will produce unintended CBOR. The error
1921 // is not caught because it would require tracking state
1922 // for QCBOREncode_BstrWrapInMapN.
1923 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1924
1925 QCBOREncode_OpenMap(&EC);
1926 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1927
1928 QCBOREncode_BstrWrapInMapN(&EC, 55);
1929 QCBOREncode_CancelBstrWrap(&EC);
1930
1931 QCBOREncode_CloseMap(&EC);
1932 uErr = QCBOREncode_Finish(&EC, &Encoded);
1933 if(uErr != QCBOR_SUCCESS) {
1934 return -11;
1935 }
1936
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001937 return 0;
1938}
1939
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001940
1941
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001942int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001943{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001944 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001945 UsefulBufC Wrapped;
1946 UsefulBufC Encoded2;
1947 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001948
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001949 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301951 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001952
Laurence Lundblade684aec22018-10-12 19:33:53 +08001953 QCBOREncode_OpenArray(&EC);
1954 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundblade684aec22018-10-12 19:33:53 +08001956 QCBOREncode_BstrWrap(&EC);
1957 QCBOREncode_AddUInt64(&EC, 466);
1958 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade684aec22018-10-12 19:33:53 +08001960 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001961
Laurence Lundblade684aec22018-10-12 19:33:53 +08001962 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001963
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001964 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001965#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001966 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001967 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001968 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001969#else
1970 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1971 * is set and not to be sure to excerice all the relavant code in
1972 * both conditions. When the guards are disabled, there is no
1973 * error returned, but the code path is still covered.
1974 */
1975 if(uError != QCBOR_SUCCESS) {
1976 return (int32_t)(600 + uError);
1977 }
1978#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001979
Laurence Lundbladeee851742020-01-08 08:37:05 -08001980 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301981 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001982 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001983 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001984#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001985 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001986 return (int32_t)(700 + uError);
1987 }
1988#else
1989 if(uError != QCBOR_SUCCESS) {
1990 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001991 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001992#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001993
Laurence Lundblade684aec22018-10-12 19:33:53 +08001994 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301995 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001996 for(int i = 1; i < 18; i++) {
1997 QCBOREncode_BstrWrap(&EC);
1998 }
1999 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundblade684aec22018-10-12 19:33:53 +08002001 for(int i = 1; i < 18; i++) {
2002 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
2003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002004
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002005 uError = QCBOREncode_Finish(&EC, &Encoded2);
2006 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08002007 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002008 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002009
Laurence Lundblade684aec22018-10-12 19:33:53 +08002010 return 0;
2011}
2012
2013
Laurence Lundblade684aec22018-10-12 19:33:53 +08002014/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002015 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002016
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002017 [
2018 h'82004E82014B8202488203458204428105',
2019 {
2020 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
2021 8426568656C6C6F18234BA2131318436568656C6C6F'
2022 }
2023 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002024
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002025 Unwrapping the first byte string in the above gives
2026 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302027
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002028 Unwrapping again, the byte string immediately above gives
2029 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302030
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002031 ...
2032
2033 Unrapping the second byte string in the top-level CBOR above gives
2034 {16: 16,
2035 64: "hello",
2036 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2037 }
2038
2039 Unwrapping again, the byte string immediately above gives
2040 {17: 17,
2041 65: "hello",
2042 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2043 }
2044
2045 ...
2046
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302047 */
2048static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002049{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002050 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2051 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2052 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2053 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2054 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2055 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2056 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2057 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2058 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2059 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002060};
2061
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002062
2063/*
2064 Get an int64 out of the decoder or fail.
2065 */
2066static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002067{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002068 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002069 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002070
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002071 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002072 if(nReturn) {
2073 return nReturn;
2074 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002075 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002076 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002078
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002079 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002080 return 0;
2081}
2082
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002083/*
2084 Get an array out of the decoder or fail.
2085 */
2086static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002087{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002088 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002089 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002090
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002091 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002092 if(nReturn) {
2093 return nReturn;
2094 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002095 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2096 return -1;
2097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002098
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002099 *pInt = Item.val.uCount;
2100 return 0;
2101}
2102
2103/*
2104 Get a map out of the decoder or fail.
2105 */
2106static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2107{
2108 QCBORItem Item;
2109 int32_t nReturn;
2110
2111 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002112 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002113 return nReturn;
2114 }
2115 if(Item.uDataType != QCBOR_TYPE_MAP) {
2116 return -1;
2117 }
2118
2119 *pInt = Item.val.uCount;
2120 return 0;
2121}
2122
2123/*
2124 Get a byte string out of the decoder or fail.
2125 */
2126static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2127{
2128 QCBORItem Item;
2129 int32_t nReturn;
2130
2131 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2132 if(nReturn) {
2133 return nReturn;
2134 }
2135 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002136 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002137 }
2138
2139 *pBstr = Item.val.string;
2140 return 0;
2141}
2142
2143/*
2144 Get a byte string out of the decoder or fail.
2145 */
2146static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2147{
2148 QCBORItem Item;
2149 int nReturn;
2150
2151 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2152 if(nReturn) {
2153 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002154 }
2155 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002156 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002157 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002158
2159 *pTstr = Item.val.string;
2160 return 0;
2161}
2162
2163
2164/*
2165 Recursively decode array containing a little CBOR and a bstr wrapped array
2166 with a little CBOR and a bstr wrapped array...
2167
2168 Part of bstr_wrap_nest_test.
2169 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2170{
2171 int64_t nInt;
2172 UsefulBufC Bstr;
2173 uint16_t nArrayCount;
2174 QCBORDecodeContext DC;
2175 int32_t nResult;
2176
2177 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2178
2179 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2180 return -10;
2181 }
2182
2183 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002184 return -11;
2185 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002186
2187 nResult = GetByteString(&DC, &Bstr);
2188 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2189 if(nArrayCount != 1) {
2190 return -12;
2191 } else {
2192 // successful exit
2193 return 0;
2194 }
2195 }
2196 if(nResult) {
2197 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002200 // tail recursion; good compilers will reuse the stack frame
2201 return DecodeNextNested(Bstr);
2202}
2203
2204
2205/*
2206 Recursively decode map containing a little CBOR and a bstr wrapped map
2207 with a little CBOR and a bstr wrapped map...
2208
2209 Part of bstr_wrap_nest_test.
2210 */
2211static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2212{
2213 int32_t nResult;
2214 uint16_t nMapCount;
2215 int64_t nInt;
2216 UsefulBufC Bstr;
2217 QCBORDecodeContext DC;
2218
2219 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2220
2221 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2222 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002224
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002225 if(GetInt64(&DC, &nInt)) {
2226 return -21;
2227 }
2228
2229 // The "hello"
2230 if(GetTextString(&DC, &Bstr)) {
2231 return -22;
2232 }
2233
2234 nResult = GetByteString(&DC, &Bstr);
2235 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2236 if(nMapCount == 2) {
2237 // successful exit
2238 return 0;
2239 } else {
2240 return -23;
2241 }
2242 }
2243
2244 if(nResult) {
2245 return -24;
2246 }
2247
2248 // tail recursion; good compilers will reuse the stack frame
2249 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002250}
2251
2252
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002253int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002254{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002255 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302256 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002257
Laurence Lundblade684aec22018-10-12 19:33:53 +08002258 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002259 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002261 QCBOREncode_OpenArray(&EC);
2262
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002263 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002264 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002265 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002266 QCBOREncode_AddInt64(&EC, i);
2267 }
2268 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2269 QCBOREncode_CloseArray(&EC);
2270 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002272
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002273 QCBOREncode_OpenMap(&EC);
2274 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2275 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2276 QCBOREncode_OpenMap(&EC);
2277 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002278 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002279 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002280
2281 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2282 QCBOREncode_CloseMap(&EC);
2283 QCBOREncode_CloseBstrWrap(&EC, NULL);
2284 }
2285 QCBOREncode_CloseMap(&EC);
2286
Laurence Lundblade684aec22018-10-12 19:33:53 +08002287 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002288
Laurence Lundblade684aec22018-10-12 19:33:53 +08002289 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002290 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002291 return -1;
2292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundblade684aec22018-10-12 19:33:53 +08002294 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302295 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002296 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002298
Laurence Lundblade684aec22018-10-12 19:33:53 +08002299 // ---- Decode it and see if it is OK ------
2300 QCBORDecodeContext DC;
2301 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002302
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002303 UsefulBufC Bstr;
2304 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002305
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002306 // Array surrounding the the whole thing
2307 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002308 return -3;
2309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002311 // Get the byte string wrapping some array stuff
2312 if(GetByteString(&DC, &Bstr)) {
2313 return -4;
2314 }
2315
2316 // Decode the wrapped nested structure
2317 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002318 if(nReturn) {
2319 return nReturn;
2320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002321
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002322 // A map enclosing some map-oriented bstr wraps
2323 if(GetMap(&DC, &nArrayCount)) {
2324 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002327 // Get the byte string wrapping some array stuff
2328 if(GetByteString(&DC, &Bstr)) {
2329 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002331
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002332 // Decode the wrapped nested structure
2333 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002334 if(nReturn) {
2335 return nReturn;
2336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002337
Laurence Lundblade684aec22018-10-12 19:33:53 +08002338 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002339 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002340 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002341
Laurence Lundblade684aec22018-10-12 19:33:53 +08002342 return 0;
2343}
2344
2345
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002346static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302347 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2348 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2349 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2350 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2351 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2352 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2353 0x45, 0xca, 0xcb, 0x36};
2354
2355/*
2356 D2 # tag(18)
2357 84 # array(4)
2358 43 # bytes(3)
2359 A10126 # "\xA1\x01&"
2360 A1 # map(1)
2361 04 # unsigned(4)
2362 42 # bytes(2)
2363 3131 # "11"
2364 54 # bytes(20)
2365 546869732069732074686520636F6E74656E742E # "This is the content."
2366 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002367 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2368 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2369 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2370 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2371 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2372 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2373 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2374 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302375 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002376static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302377 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2378 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2379 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2380 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2381 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2382 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2383 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2384 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2385 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2386 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2387
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002388static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2389
2390
Laurence Lundblade684aec22018-10-12 19:33:53 +08002391/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002392 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002393 C.2.1. This doesn't actually verify the signature (however
2394 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002395 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002396int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002397{
2398 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002399 const char *szKid = "11";
2400 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2401 const char *szPayload = "This is the content.";
2402 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2403 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2404 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundblade684aec22018-10-12 19:33:53 +08002406 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002407
2408 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302409 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002410
Laurence Lundblade684aec22018-10-12 19:33:53 +08002411 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002412 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002413 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundblade684aec22018-10-12 19:33:53 +08002415 // Add protected headers
2416 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002417
Laurence Lundblade684aec22018-10-12 19:33:53 +08002418 // Empty map with unprotected headers
2419 QCBOREncode_OpenMap(&EC);
2420 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2421 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002422
Laurence Lundblade684aec22018-10-12 19:33:53 +08002423 // The payload
2424 UsefulBufC WrappedPayload;
2425 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002426 // Payload is not actually CBOR in example C.2.1 like it would be
2427 // for a CWT or EAT. It is just a text string.
2428 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002429 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002430
Laurence Lundblade684aec22018-10-12 19:33:53 +08002431 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002432 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2433 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002434 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002435 return -1;
2436 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002437
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002438/* if(UsefulBuf_Compare(WrappedPayload,
2439 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2440 return -1;
2441 } */
2442
Laurence Lundblade684aec22018-10-12 19:33:53 +08002443 // The signature
2444 QCBOREncode_AddBytes(&EC, Signature);
2445 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade684aec22018-10-12 19:33:53 +08002447 // Finish and check the results
2448 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002449 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002450 return -2;
2451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002452
Laurence Lundblade684aec22018-10-12 19:33:53 +08002453 // 98 is the size from RFC 8152 C.2.1
2454 if(COSE_Sign1.len != 98) {
2455 return -3;
2456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002457
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002458 // It would be good to compare this to the output from a COSE
2459 // implementation like COSE-C. This has been checked against the
2460 // CBOR playground.
2461 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002462 return -4;
2463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002465
2466 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2467 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2468
2469 // top level array for cose sign1, 18 is the tag for COSE sign
2470 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2471 QCBOREncode_OpenArray(&EC);
2472
2473 // Add protected headers
2474 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2475
2476 // Empty map with unprotected headers
2477 QCBOREncode_OpenMap(&EC);
2478 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2479 QCBOREncode_CloseMap(&EC);
2480
2481 // The payload
2482 QCBOREncode_BstrWrap(&EC);
2483 // Payload is not actually CBOR in example C.2.1 like it would be
2484 // for a CWT or EAT. It is just a text string.
2485 QCBOREncode_AddEncoded(&EC, Payload);
2486 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2487
2488 // Check we got back the actual payload expected
2489 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2490 if(UsefulBuf_Compare(WrappedPayload,
2491 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2492 return -11;
2493 }
2494
2495 // The signature
2496 QCBOREncode_AddBytes(&EC, Signature);
2497 QCBOREncode_CloseArray(&EC);
2498
2499 // Finish and check the results
2500 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2501 return -12;
2502 }
2503
2504 // 98 is the size from RFC 8152 C.2.1
2505 if(COSE_Sign1.len != 98) {
2506 return -13;
2507 }
2508
2509 // It would be good to compare this to the output from a COSE
2510 // implementation like COSE-C. This has been checked against the
2511 // CBOR playground.
2512 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2513 return -14;
2514 }
2515
Laurence Lundblade684aec22018-10-12 19:33:53 +08002516 return 0;
2517}
2518
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002519
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002520int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002521{
2522 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002523 QCBORError uErr;
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002524 UsefulBufC EncodedResult;
2525 MakeUsefulBufOnStack(SmallBuffer, 4);
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002526
2527
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002528 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002529 // Do all of these tests with NULL buffers so no actual
2530 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002531 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002532
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002533 // First verify no error from a big buffer
2534 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002535 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002536 // 6 is the CBOR overhead for opening the array and encodng the length
2537 // This exactly fills the buffer.
2538 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002539 QCBOREncode_CloseArray(&EC);
2540 size_t xx;
2541 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2542 return -1;
2543 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002544
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002545 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002546 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002547 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002548 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002549 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002550 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002551 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2552 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002553 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002554 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002555 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002556 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2557 return -2;
2558 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002559
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002560 // Third, fit an array in exactly at max position allowed
2561 QCBOREncode_Init(&EC, Buffer);
2562 QCBOREncode_OpenArray(&EC);
2563 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2564 QCBOREncode_OpenArray(&EC);
2565 QCBOREncode_CloseArray(&EC);
2566 QCBOREncode_CloseArray(&EC);
2567 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2568 return -10;
2569 }
2570
2571
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002572 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2573 // Work close to the 4GB size limit for a better test
2574 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002575 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002576
2577 QCBOREncode_Init(&EC, Large);
2578 QCBOREncode_OpenArray(&EC);
2579 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2580 QCBOREncode_CloseArray(&EC);
2581 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2582 // Making sure it succeeds when it should first
2583 return -3;
2584 }
2585
2586 QCBOREncode_Init(&EC, Large);
2587 QCBOREncode_OpenArray(&EC);
2588 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2589 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2590 QCBOREncode_CloseArray(&EC);
2591 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2592 // Now just 1 byte over, see that it fails
2593 return -4;
2594 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002595
2596
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002597 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2598 QCBOREncode_Init(&EC, Large);
2599 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2600 QCBOREncode_OpenArray(&EC);
2601 }
2602 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2603 QCBOREncode_CloseArray(&EC);
2604 }
2605 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2606 // Making sure it succeeds when it should first
2607 return -5;
2608 }
2609
2610 QCBOREncode_Init(&EC, Large);
2611 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2612 QCBOREncode_OpenArray(&EC);
2613 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002614 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002615 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2616 QCBOREncode_CloseArray(&EC);
2617 }
2618 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002619 return -6;
2620 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002621
2622
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002623 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002624 QCBOREncode_Init(&EC, Large);
2625 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2626 QCBOREncode_OpenArray(&EC);
2627 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002628 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002629 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2630 QCBOREncode_CloseArray(&EC);
2631 }
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_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002635 return -7;
2636 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002637#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2638 if(uErr != QCBOR_SUCCESS) {
2639 return -107;
2640 }
2641#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002642
2643
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002644 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002645 QCBOREncode_Init(&EC, Large);
2646 QCBOREncode_OpenArray(&EC);
2647 UsefulBufC Wrap;
2648 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002649 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2650#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2651 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002652 return -8;
2653 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002654#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2655 if(uErr != QCBOR_SUCCESS) {
2656 return -108;
2657 }
2658#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002659
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002660 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002661 QCBOREncode_Init(&EC, Large);
2662 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2663 QCBOREncode_OpenArray(&EC);
2664 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002665 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002666 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2667 QCBOREncode_CloseArray(&EC);
2668 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002669
2670 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2671#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2672 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002673 return -9;
2674 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002675#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2676 if(uErr != QCBOR_SUCCESS) {
2677 return -109;
2678 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002679#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002680
Laurence Lundblade241705e2018-12-30 18:56:14 -08002681 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2682 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002683
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002684
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002685 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002686 QCBOREncode_Init(&EC, Buffer);
2687 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2688 return -11;
2689 }
2690
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002691 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002692 QCBOREncode_Init(&EC, Large);
2693 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002694 QCBOREncode_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002695 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2696#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2697 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002698 return -12;
2699 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002700#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2701 if(uErr != QCBOR_SUCCESS) {
2702 return -112;
2703 }
2704#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2705
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002706
2707 QCBOREncode_Init(&EC, Large);
2708 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002709 QCBOREncode_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002710 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2711#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2712 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002713 return -13;
2714 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002715#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2716 if(uErr != QCBOR_SUCCESS) {
2717 return -113;
2718 }
2719#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002720
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002721 /* Test that still-open error sticks */
2722 QCBOREncode_Init(&EC, Large);
2723 QCBOREncode_OpenArray(&EC);
2724 QCBOREncode_Finish(&EC, &EncodedResult);
2725#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2726 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2727 return -120;
2728 }
2729#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2730 if(QCBOREncode_GetErrorState(&EC) != QCBOR_SUCCESS) {
2731 return -122;
2732 }
2733#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2734
2735 /* Test that too-small error is sticky */
2736 QCBOREncode_Init(&EC, SmallBuffer);
2737 QCBOREncode_OpenArray(&EC);
2738 QCBOREncode_AddInt64(&EC, INT64_MAX);
2739 QCBOREncode_AddInt64(&EC, INT64_MAX);
2740 QCBOREncode_AddInt64(&EC, INT64_MAX);
2741 QCBOREncode_CloseArray(&EC);
2742 QCBOREncode_Finish(&EC, &EncodedResult);
2743 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2744 return -130;
2745 }
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002746
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002747 return 0;
2748}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002749
2750
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002751#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002752/*
2753 [
2754 4([-1, 3]),
2755 4([-20, 4759477275222530853136]),
2756 4([9223372036854775807, -4759477275222530853137]),
2757 5([300, 100]),
2758 5([-20, 4759477275222530853136]),
2759 5([-9223372036854775808, -4759477275222530853137])
2760 ]
2761 */
2762static const uint8_t spExpectedExponentAndMantissaArray[] = {
2763 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2764 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2765 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2766 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2767 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2768 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2769 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2770 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2771 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2772 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2773 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2774
2775
2776/*
2777 {
2778 "decimal fraction": 4([-1, 3]),
2779 300: 4([-1, 3]),
2780 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2781 400: 4([2147483647, 4759477275222530853136]),
2782 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2783 500: 4([9223372036854775807, -4759477275222530853137]),
2784 "big float": 5([300, 100]),
2785 600: 5([300, 100]),
2786 "big float bignum positive": 5([-20, 4759477275222530853136]),
2787 700: 5([-20, 4759477275222530853136]),
2788 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2789 800: 5([-9223372036854775808, -4759477275222530853137])
2790 }
2791 */
2792static const uint8_t spExpectedExponentAndMantissaMap[] = {
2793 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2794 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2795 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2796 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2797 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2798 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2799 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2800 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2801 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2802 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2803 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2804 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2805 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2806 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2807 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2808 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2809 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2810 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2811 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2812 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2813 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2814 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2815 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2816 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2817 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2818 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2819 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2820 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2821 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2822 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2823 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2824 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2825 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2826 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2827 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2828 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2829 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2830 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2831 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2832 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2833 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2834 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2835 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2836 0x09, 0x10
2837};
2838
2839
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002840int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002841{
2842 QCBOREncodeContext EC;
2843 UsefulBufC EncodedExponentAndMantissa;
2844
2845 // Constant for the big number used in all the tests.
2846 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2847 0x06, 0x07, 0x08, 0x09, 0x010};
2848 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2849
2850 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2851 QCBOREncode_OpenArray(&EC);
2852 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2853 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2854 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2855 QCBOREncode_AddBigFloat(&EC, 100, 300);
2856 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2857 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2858 QCBOREncode_CloseArray(&EC);
2859
2860 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2861 return -2;
2862 }
2863
2864 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2865 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2866 NULL);
2867 if(nReturn) {
2868 return nReturn;
2869 }
2870
2871 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2872 QCBOREncode_OpenMap(&EC);
2873
2874 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2875
2876 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2877
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002878 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002879 "decimal fraction bignum postive",
2880 BigNum,
2881 false,
2882 -200);
2883
2884 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2885 400,
2886 BigNum,
2887 false,
2888 INT32_MAX);
2889
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002890 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002891 "decimal fraction bignum negative",
2892 BigNum,
2893 true,
2894 INT64_MAX);
2895
2896 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2897 500,
2898 BigNum,
2899 true,
2900 INT64_MAX);
2901
2902 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2903
2904 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2905
2906 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2907 "big float bignum positive",
2908 BigNum,
2909 false,
2910 -20);
2911
2912 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2913 700,
2914 BigNum,
2915 false,
2916 -20);
2917
2918 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2919 "big float bignum negative",
2920 BigNum,
2921 true,
2922 INT64_MIN);
2923
2924 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2925 800,
2926 BigNum,
2927 true,
2928 INT64_MIN);
2929
2930 QCBOREncode_CloseMap(&EC);
2931
2932 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2933 return -3;
2934 }
2935
2936
2937 struct UBCompareDiagnostic Diag;
2938
2939 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2940 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2941 &Diag);
2942 if(nReturn) {
2943 return nReturn + 1000000; // +1000000 to distinguish from first test above
2944 }
2945
2946 return 0;
2947}
2948
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002949#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002950
2951
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002952int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002953{
2954 /* This test doesn't have to be extensive, because just about every
2955 * other test exercises QCBOREncode_EncodeHead().
2956 */
2957 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002958 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002959
2960 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2961 CBOR_MAJOR_TYPE_POSITIVE_INT,
2962 0,
2963 0);
2964
2965 static const uint8_t expectedZero[] = {0x00};
2966
2967 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2968 return -1;
2969 }
2970
2971 // ---- Encode a zero padded out to an 8 byte integer ----
2972 encoded = QCBOREncode_EncodeHead(RightSize,
2973 CBOR_MAJOR_TYPE_POSITIVE_INT,
2974 8, // uMinSize is 8 bytes
2975 0);
2976
2977 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2978 0x00, 0x00, 0x00, 0x00};
2979
2980 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2981 return -2;
2982 }
2983
2984
2985 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002986 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002987
2988 encoded = QCBOREncode_EncodeHead(TooSmall,
2989 CBOR_MAJOR_TYPE_POSITIVE_INT,
2990 0,
2991 0);
2992
2993 if(!UsefulBuf_IsNULLC(encoded)) {
2994 return -3;
2995 }
2996
2997 return 0;
2998}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002999
3000
3001static const uint8_t spExpectedForOpenBytes[] = {
3002 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
3003 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
3004 0x78
3005};
3006
3007static const uint8_t spExpectedForOpenBytes2[] = {
3008 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
3009 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
3010 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
3011};
3012
3013int32_t
3014OpenCloseBytesTest(void)
3015{
3016 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
3017 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
3018 QCBOREncodeContext EC;
3019 UsefulBuf Place;
3020 UsefulBufC Encoded;
3021 QCBORError uErr;
3022
3023 /* Normal use case -- add a byte string that fits */
3024 QCBOREncode_Init(&EC, TestBuf);
3025 QCBOREncode_OpenBytes(&EC, &Place);
3026 if(Place.ptr != TestBuf.ptr ||
3027 Place.len != TestBuf.len) {
3028 return 1;
3029 }
3030 Place.len -= 4;
3031 UsefulBuf_Set(Place, 'x');
3032 QCBOREncode_CloseBytes(&EC, Place.len);
3033 QCBOREncode_Finish(&EC, &Encoded);
3034 if(UsefulBuf_Compare(Encoded,
3035 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
3036 return 2;
3037 }
3038
Paul Liétar79789772022-07-26 20:33:18 +01003039 /* Run the same test but with a NULL buffer */
3040 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3041 QCBOREncode_OpenBytes(&EC, &Place);
3042 if(!UsefulBuf_IsNULL(Place)) {
3043 return 3;
3044 }
3045 Place.len -= 4;
3046 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3047 QCBOREncode_CloseBytes(&EC, Place.len);
3048 uErr = QCBOREncode_Finish(&EC, &Encoded);
3049 if(uErr != QCBOR_SUCCESS ||
3050 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3051 return 4;
3052 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003053
3054 /* Open a byte string with no room left */
3055 QCBOREncode_Init(&EC, TestBuf);
3056 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3057 QCBOREncode_OpenBytes(&EC, &Place);
3058 if(Place.ptr != NULL ||
3059 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003060 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003061 }
3062
3063 /* Try to extend byte string past end of encoding output buffer */
3064 QCBOREncode_Init(&EC, TestBuf);
3065 QCBOREncode_AddSZString(&EC, "012345678901234567");
3066 QCBOREncode_OpenBytes(&EC, &Place);
3067 /* Don't bother to write any bytes*/
3068 QCBOREncode_CloseBytes(&EC, Place.len+1);
3069 uErr = QCBOREncode_GetErrorState(&EC);
3070 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003071 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003072 }
3073
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003074 /* Close a byte string without opening one. */
3075 QCBOREncode_Init(&EC, TestBuf);
3076 QCBOREncode_AddSZString(&EC, "012345678");
3077 QCBOREncode_CloseBytes(&EC, 1);
3078 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003079#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003080 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003081 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003082 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003083#else
3084 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003085 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003086 }
3087#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003088
3089 /* Forget to close a byte string */
3090 QCBOREncode_Init(&EC, TestBuf);
3091 QCBOREncode_AddSZString(&EC, "012345678");
3092 QCBOREncode_OpenBytes(&EC, &Place);
3093 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003094#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003095 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003096 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003097 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003098#else
3099 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003100 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003101 }
3102#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003103
3104 /* Try to open a byte string in a byte string */
3105 QCBOREncode_Init(&EC, TestBuf);
3106 QCBOREncode_AddSZString(&EC, "012345678");
3107 QCBOREncode_OpenBytes(&EC, &Place);
3108 QCBOREncode_OpenBytes(&EC, &Place);
3109 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003110#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003111 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003112 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003113 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003114#else
3115 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003116 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003117 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003118#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3119
3120 /* A successful case with a little complexity */
3121 QCBOREncode_Init(&EC, TestBuf2);
3122 QCBOREncode_OpenMap(&EC);
3123 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3124 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3125 Place.len = 2;
3126 UsefulBuf_Set(Place, 'x');
3127 QCBOREncode_CloseBytes(&EC, 2);
3128 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3129 Place.len = 3;
3130 UsefulBuf_Set(Place, 'y');
3131 QCBOREncode_CloseBytes(&EC, 3);
3132 QCBOREncode_OpenArrayInMapN(&EC, 40);
3133 QCBOREncode_OpenBytes(&EC, &Place);
3134 QCBOREncode_CloseBytes(&EC, 0);
3135 QCBOREncode_CloseArray(&EC);
3136 QCBOREncode_CloseMap(&EC);
3137 uErr = QCBOREncode_Finish(&EC, &Encoded);
3138 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003139 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003140 }
3141 if(UsefulBuf_Compare(Encoded,
3142 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003143 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003144 }
3145
3146 return 0;
3147}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003148
3149
3150
3151int32_t
3152SortMapTest(void)
3153{
3154 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3155 QCBOREncodeContext EC;
3156 UsefulBufC EncodedAndSorted;
3157 QCBORError uErr;
3158 struct UBCompareDiagnostic CompareDiagnostics;
3159
3160
3161 /* --- Basic sort test case --- */
3162 QCBOREncode_Init(&EC, TestBuf);
3163 QCBOREncode_OpenMap(&EC);
3164 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3165 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3166 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3167 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3168 QCBOREncode_CloseAndSortMap(&EC);
3169 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3170 if(uErr) {
3171 return 11;
3172 }
3173
3174 static const uint8_t spBasic[] = {
3175 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3176
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003177 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003178 return 12;
3179 }
3180
3181 /* --- Empty map sort test case --- */
3182 QCBOREncode_Init(&EC, TestBuf);
3183 QCBOREncode_OpenMap(&EC);
3184 QCBOREncode_CloseAndSortMap(&EC);
3185 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3186 if(uErr) {
3187 return 21;
3188 }
3189
3190 static const uint8_t spEmpty[] = {0xA0};
3191 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3192 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3193 &CompareDiagnostics)) {
3194 return 22;
3195 }
3196
3197 /* --- Several levels of nested sorted maps --- */
3198 QCBOREncode_Init(&EC, TestBuf);
3199 QCBOREncode_OpenMap(&EC);
3200 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3201 QCBOREncode_OpenMapInMapN(&EC, 428);
3202 QCBOREncode_AddNULLToMap(&EC, "null");
3203 QCBOREncode_OpenArrayInMap(&EC, "array");
3204 QCBOREncode_AddSZString(&EC, "hi");
3205 QCBOREncode_AddSZString(&EC, "there");
3206 QCBOREncode_CloseArray(&EC);
3207 QCBOREncode_OpenMapInMap(&EC, "empty2");
3208 QCBOREncode_CloseAndSortMap(&EC);
3209 QCBOREncode_OpenMapInMap(&EC, "empty1");
3210 QCBOREncode_CloseAndSortMap(&EC);
3211 QCBOREncode_CloseAndSortMap(&EC);
3212 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3213 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3214 QCBOREncode_CloseAndSortMap(&EC);
3215 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3216 if(uErr) {
3217 return 31;
3218 }
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003219
3220 /* Correctly sorted.
3221 * {
3222 * 88: 1(888888),
3223 * 428: {
3224 * "null": null,
3225 * "array": [
3226 * "hi",
3227 * "there"
3228 * ],
3229 * "empty1": {},
3230 * "empty2": {}
3231 * },
3232 * "boo": true,
3233 * "three": 3
3234 * }
3235 */
3236 static const uint8_t spSorted[] = {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003237 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3238 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3239 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3240 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3241 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3242 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3243 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3244 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3245
3246 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003247 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003248 &CompareDiagnostics)) {
3249 return 32;
3250 }
3251
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003252
3253 /* Same data items, but added in a different order */
3254 QCBOREncode_Init(&EC, TestBuf);
3255 QCBOREncode_OpenMap(&EC);
3256 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3257 QCBOREncode_OpenMapInMapN(&EC, 428);
3258 QCBOREncode_OpenMapInMap(&EC, "empty1");
3259 QCBOREncode_CloseAndSortMap(&EC);
3260 QCBOREncode_OpenArrayInMap(&EC, "array");
3261 QCBOREncode_AddSZString(&EC, "hi");
3262 QCBOREncode_AddSZString(&EC, "there");
3263 QCBOREncode_CloseArray(&EC);
3264 QCBOREncode_OpenMapInMap(&EC, "empty2");
3265 QCBOREncode_CloseAndSortMap(&EC);
3266 QCBOREncode_AddNULLToMap(&EC, "null");
3267 QCBOREncode_CloseAndSortMap(&EC);
3268 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3269 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3270 QCBOREncode_CloseAndSortMap(&EC);
3271 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3272 if(uErr) {
3273 return 31;
3274 }
3275
3276 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3277 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3278 &CompareDiagnostics)) {
3279 return 32;
3280 }
3281
3282 /* Same data items, but added in a different order */
3283 QCBOREncode_Init(&EC, TestBuf);
3284 QCBOREncode_OpenMap(&EC);
3285 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3286 QCBOREncode_OpenMapInMapN(&EC, 428);
3287 QCBOREncode_OpenMapInMap(&EC, "empty1");
3288 QCBOREncode_CloseAndSortMap(&EC);
3289 QCBOREncode_OpenArrayInMap(&EC, "array");
3290 QCBOREncode_AddSZString(&EC, "hi");
3291 QCBOREncode_AddSZString(&EC, "there");
3292 QCBOREncode_CloseArray(&EC);
3293 QCBOREncode_OpenMapInMap(&EC, "empty2");
3294 QCBOREncode_CloseAndSortMap(&EC);
3295 QCBOREncode_AddNULLToMap(&EC, "null");
3296 QCBOREncode_CloseAndSortMap(&EC);
3297 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3298 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3299 QCBOREncode_CloseAndSortMap(&EC);
3300 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3301 if(uErr) {
3302 return 31;
3303 }
3304
3305 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3306 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3307 &CompareDiagnostics)) {
3308 return 32;
3309 }
3310
3311
3312
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003313 /* --- Degenerate case of everything in order --- */
3314 QCBOREncode_Init(&EC, TestBuf);
3315 QCBOREncode_OpenMap(&EC);
3316 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3317 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3318 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3319 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3320 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3321 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3322 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3323 QCBOREncode_CloseAndSortMap(&EC);
3324 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3325 if(uErr) {
3326 return 41;
3327 }
3328
3329 static const uint8_t sp6Items[] = {
3330 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3331 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3332 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3333 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3334 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3335 &CompareDiagnostics)) {
3336 return 42;
3337 }
3338
3339 /* --- Degenerate case -- reverse order --- */
3340 QCBOREncode_Init(&EC, TestBuf);
3341 QCBOREncode_OpenMap(&EC);
3342 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3343 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3344 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3345 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3346 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3347 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3348 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3349 QCBOREncode_CloseAndSortMap(&EC);
3350 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3351 if(uErr) {
3352 return 51;
3353 }
3354
3355 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3356 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3357 &CompareDiagnostics)) {
3358 return 52;
3359 }
3360
3361 /* --- Same items, randomly out of order --- */
3362 QCBOREncode_Init(&EC, TestBuf);
3363 QCBOREncode_OpenMap(&EC);
3364 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3365 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3366 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3367 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3368 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3369 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3370 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3371 QCBOREncode_CloseAndSortMap(&EC);
3372 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3373 if(uErr) {
3374 return 61;
3375 }
3376
3377 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3378 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3379 &CompareDiagnostics)) {
3380 return 62;
3381 }
3382
3383 /* --- Stuff in front of and after array to sort --- */
3384 QCBOREncode_Init(&EC, TestBuf);
3385 QCBOREncode_OpenArray(&EC);
3386 QCBOREncode_AddInt64(&EC, 111);
3387 QCBOREncode_AddInt64(&EC, 222);
3388 QCBOREncode_OpenMap(&EC);
3389 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3390 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3391 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3392 QCBOREncode_CloseAndSortMap(&EC);
3393 QCBOREncode_AddInt64(&EC, 888);
3394 QCBOREncode_AddInt64(&EC, 999);
3395 QCBOREncode_CloseArray(&EC);
3396 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3397 if(uErr) {
3398 return 71;
3399 }
3400
3401 static const uint8_t spPreItems[] = {
3402 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3403 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3404 0x03, 0xE7};
3405 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3406 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3407 &CompareDiagnostics)) {
3408 return 72;
3409 }
3410
3411 /* --- map with labels of all CBOR major types and in reverse order --- */
3412 QCBOREncode_Init(&EC, TestBuf);
3413 QCBOREncode_OpenMap(&EC);
3414
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003415 /* Adding labels directly rather than AddToMap functions */
3416
3417#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003418 QCBOREncode_AddDouble(&EC, 8.77);
3419 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003420#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003421
3422 QCBOREncode_AddBool(&EC, true);
3423 QCBOREncode_AddInt64(&EC, 6);
3424
3425 QCBOREncode_AddDateEpoch(&EC, 88);
3426 QCBOREncode_AddInt64(&EC, 5);
3427
3428 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3429 QCBOREncode_AddInt64(&EC, 4);
3430
3431 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3432 QCBOREncode_AddInt64(&EC, 7);
3433
3434 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3435
3436 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3437 QCBOREncode_AddInt64(&EC, 2);
3438
3439 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3440 QCBOREncode_CloseAndSortMap(&EC);
3441
3442 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3443 if(uErr) {
3444 return 81;
3445 }
3446
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003447#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003448 static const uint8_t spLabelTypes[] = {
3449 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3450 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3451 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3452 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3453 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003454#else
3455 static const uint8_t spLabelTypes[] = {
3456 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3457 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3458 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3459#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3460
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003461 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3463 &CompareDiagnostics)) {
3464 return 82;
3465 }
3466
3467 /* --- labels are indefinitely encoded --- */
3468 QCBOREncode_Init(&EC, TestBuf);
3469 QCBOREncode_OpenMap(&EC);
3470
3471 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3472
3473 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3474
3475 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3476 QCBOREncode_AddInt64(&EC, 3);
3477
3478 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3479 QCBOREncode_AddInt64(&EC, 4);
3480
3481 QCBOREncode_CloseAndSortMap(&EC);
3482
3483 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3484 if(uErr) {
3485 return 91;
3486 }
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003487#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003488 static const uint8_t spIndefItems[] = {
3489 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3490 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3491 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3492 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3493 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3494 &CompareDiagnostics)) {
3495 return 92;
3496 }
3497
3498 /* --- Indefinitely encoded maps --- */
3499 QCBOREncode_Init(&EC, TestBuf);
3500 QCBOREncode_OpenMapIndefiniteLength(&EC);
3501
3502 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3503 QCBOREncode_CloseMapIndefiniteLength(&EC);
3504
3505 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3506 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3507
3508 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3509 QCBOREncode_CloseMapIndefiniteLength(&EC);
3510
3511 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3512 QCBOREncode_CloseMapIndefiniteLength(&EC);
3513
3514 QCBOREncode_CloseAndSortMapIndef(&EC);
3515 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3516 if(uErr) {
3517 return 101;
3518 }
3519
3520 static const uint8_t spIndeMaps[] = {
3521 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3522 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3523 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3524 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3525 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3526 &CompareDiagnostics)) {
3527 return 102;
3528 }
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003529#endif
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003530
3531 /* --- Duplicate label test --- */
3532 QCBOREncode_Init(&EC, TestBuf);
3533 QCBOREncode_OpenMap(&EC);
3534 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3535 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3536 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3537 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3538 QCBOREncode_CloseAndSortMap(&EC);
3539 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3540 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3541 return 114;
3542 }
3543
3544 QCBOREncode_Init(&EC, TestBuf);
3545 QCBOREncode_OpenMap(&EC);
3546 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3547 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3548 QCBOREncode_AddInt64ToMapN(&EC, 1, 2);
3549 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3550 QCBOREncode_CloseAndSortMap(&EC);
3551 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3552 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3553 return 115;
3554 }
3555
3556 QCBOREncode_Init(&EC, TestBuf);
3557 QCBOREncode_OpenMap(&EC);
3558 QCBOREncode_AddInt64ToMap(&EC, "abc", 3);
3559 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3560 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3561 QCBOREncode_AddInt64ToMap(&EC, "def", 2);
3562 QCBOREncode_CloseAndSortMap(&EC);
3563 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3564 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3565 return 116;
3566 }
3567
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003568 return 0;
3569}
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003570
3571
3572#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
3573
3574#include <math.h> /* For INFINITY and NAN and isnan() */
3575
3576
3577/* Public function. See qcbor_encode_tests.h */
3578int32_t CDETest(void)
3579{
3580 QCBOREncodeContext EC;
3581 UsefulBufC Encoded;
3582 QCBORError uExpectedErr;
3583
3584 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3585
3586 QCBOREncode_SerializationCDE(&EC);
3587
3588 /* Items added to test sorting and preferred encoding of numbers and floats */
3589 QCBOREncode_OpenMap(&EC);
3590 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3591 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3592 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3593 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3594 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3595 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3596
3597 QCBOREncode_CloseMap(&EC);
3598
3599 uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
3600 if(uExpectedErr != QCBOR_SUCCESS) {
3601 return 2;
3602 }
3603
3604 static const uint8_t spExpectedCDE[] = {
3605 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3606 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3607 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3608 0x61, 0x78, 0xF9, 0x40, 0x00};
3609
3610 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3611 Encoded)) {
3612 return 1;
3613 }
3614
3615
3616#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3617 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3618#else
3619 uExpectedErr = QCBOR_SUCCESS;
3620#endif
3621
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003622#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003623 /* Next, make sure methods that encode non-CDE error out */
3624 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3625 QCBOREncode_SerializationCDE(&EC);
3626 QCBOREncode_OpenMapIndefiniteLength(&EC);
3627 QCBOREncode_CloseMap(&EC);
3628 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3629 return 100;
3630 }
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003631#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003632
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003633 return 0;
3634}
3635
3636/* Public function. See qcbor_encode_tests.h */
3637int32_t DCBORTest(void)
3638{
3639 QCBOREncodeContext EC;
3640 UsefulBufC Encoded;
3641 QCBORError uExpectedErr;
3642
3643 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3644
3645 QCBOREncode_SerializationdCBOR(&EC);
3646
3647 /* Items added to test sorting and preferred encoding of numbers and floats */
3648 QCBOREncode_OpenMap(&EC);
3649 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3650 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3651 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3652 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3653 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3654
3655 QCBOREncode_CloseMap(&EC);
3656
3657 QCBOREncode_Finish(&EC, &Encoded);
3658
3659 static const uint8_t spExpecteddCBOR[] = {
3660 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3661 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3662 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3663
3664 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3665 Encoded)) {
3666 return 1;
3667 }
3668
3669
3670#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3671 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3672#else
3673 uExpectedErr = QCBOR_SUCCESS;
3674#endif
3675
3676 /* Next, make sure methods that encode of non-CDE error out */
3677
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003678#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003679 /* Indefinite-length map */
3680 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3681 QCBOREncode_SerializationdCBOR(&EC);
3682 QCBOREncode_OpenMapIndefiniteLength(&EC);
3683 QCBOREncode_CloseMap(&EC);
3684 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3685 return 100;
3686 }
3687
3688 /* Indefinite-length array */
3689 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3690 QCBOREncode_SerializationdCBOR(&EC);
3691 QCBOREncode_OpenArrayIndefiniteLength(&EC);
3692 QCBOREncode_CloseMap(&EC);
3693 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3694 return 101;
3695 }
Laurence Lundbladed3f07842024-06-19 13:05:07 -07003696#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003697
3698 /* The "undef" special value */
3699 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3700 QCBOREncode_SerializationdCBOR(&EC);
3701 QCBOREncode_AddUndef(&EC);
3702 QCBOREncode_CloseMap(&EC);
3703 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3704 return 102;
3705 }
3706
3707 /* 65-bit negative integers */
3708 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3709 QCBOREncode_SerializationdCBOR(&EC);
3710 QCBOREncode_AddNegativeUInt64(&EC, 1);
3711 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3712 return 103;
3713 }
3714
3715 /* Improvement: when indefinite length string encoding is supported
3716 * test it here too. */
3717
3718 return 0;
3719
3720}
3721#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */