blob: dc186f2c05de1cb10d937946e06d4931bb153c8e [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade240ca822024-01-16 11:11:00 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -080051#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070076 if(((const uint8_t *)U1.ptr)[i] != ((const uint8_t *)U2.ptr)[i]) {
escherstairb5563422023-02-13 18:36:45 +010077 printf("Position: %u Actual: 0x%x Expected: 0x%x\n",
Laurence Lundbladeee851742020-01-08 08:37:05 -080078 (uint32_t)i,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070079 ((const uint8_t *)U1.ptr)[i],
80 ((const uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081 return 1;
82 }
83 }
84 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086}
87
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070088#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053089 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070091#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080094 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
95
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070096#endif
97
98
Laurence Lundblade59289e52019-12-30 13:44:37 -080099/*
100 Returns 0 if UsefulBufs are equal
101 Returns 1000000 + offeset if they are not equal.
Laurence Lundblade59289e52019-12-30 13:44:37 -0800102*/
103struct UBCompareDiagnostic {
104 uint8_t uActual;
105 uint8_t uExpected;
106 size_t uOffset;
107};
108
Laurence Lundbladeee851742020-01-08 08:37:05 -0800109static int32_t
110UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
111 UsefulBufC Expected,
112 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800113 size_t i;
114 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800115 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800116 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800117 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
118 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800119 pDiag->uOffset = i;
120 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800121 // Cast to int is OK as this is only a diagnostic and the sizes
122 // here are never over a few KB.
123 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800124 }
125 }
126 return 0;
127
128}
Laurence Lundblade59289e52019-12-30 13:44:37 -0800129
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700130
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530131// One big buffer that is used by all the tests to encode into
132// Putting it in uninitialized data is better than using a lot
133// of stack. The tests should run on small devices too.
134static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135
136
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800137
138/*
139 Some very minimal tests.
140 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300141int32_t BasicEncodeTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142{
143 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800144 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800145
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530146 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530147
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800148 QCBOREncode_OpenMap(&EC);
149 QCBOREncode_AddBoolToMapN(&EC, 66, true);
150 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
157
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800158 // Decode it and see that is right
159 QCBORDecodeContext DC;
160 QCBORItem Item;
161 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 QCBORDecode_GetNext(&DC, &Item);
164 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530165 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800168 QCBORDecode_GetNext(&DC, &Item);
169 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530174 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
177
Laurence Lundbladeee851742020-01-08 08:37:05 -0800178 // Make another encoded message with the CBOR from the previous
179 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530180 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800181 QCBOREncode_Init(&EC, MemoryForEncoded2);
182 QCBOREncode_OpenArray(&EC);
183 QCBOREncode_AddUInt64(&EC, 451);
184 QCBOREncode_AddEncoded(&EC, Encoded);
185 QCBOREncode_OpenMap(&EC);
186 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
187 QCBOREncode_CloseMap(&EC);
188 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700191 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530192 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800193 }
194 /*
195 [ // 0 1:3
196 451, // 1 1:2
197 { // 1 1:2 2:1
198 66: true // 2 1:1
199 },
200 { // 1 1:1 2:1
201 -70000: { // 2 1:1 2:1 3:1
202 66: true // 3 XXXXXX
203 }
204 }
205 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800209 83 # array(3)
210 19 01C3 # unsigned(451)
211 A1 # map(1)
212 18 42 # unsigned(66)
213 F5 # primitive(21)
214 A1 # map(1)
215 3A 0001116F # negative(69999)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800220
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221 // Decode it and see if it is OK
222 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800224 // 0 1:3
225 QCBORDecode_GetNext(&DC, &Item);
226 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530227 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 // 1 1:2
231 QCBORDecode_GetNext(&DC, &Item);
232 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530233 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800236 // 1 1:2 2:1
237 QCBORDecode_GetNext(&DC, &Item);
238 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530239 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800242 // 2 1:1
243 QCBORDecode_GetNext(&DC, &Item);
244 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530245 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800248 // 1 1:1 2:1
249 QCBORDecode_GetNext(&DC, &Item);
250 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530251 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800254 // 2 1:1 2:1 3:1
255 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800256 if(Item.uDataType != QCBOR_TYPE_MAP ||
257 Item.val.uCount != 1 ||
258 Item.uLabelType != QCBOR_TYPE_INT64 ||
259 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800263 // 3 XXXXXX
264 QCBORDecode_GetNext(&DC, &Item);
265 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530266 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800269 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530270 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800273 return 0;
274}
275
276
277
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530278static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade2d493002024-02-01 11:09:17 -0700279 0x98, 0x24, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530280 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
281 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
Laurence Lundblade2d493002024-02-01 11:09:17 -0700282 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0x20, 0xa4, 0x63, 0x4c, 0x42,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530283 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
284 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
285 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
286 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
287 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700288 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
290 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
291 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
292 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
293 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700294 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
295 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00,
296 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40,
297 0x66, 0x62,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530298 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
299 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
300 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700301 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530302 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
303 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
304 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
305 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
306 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
307 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
308 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
309 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
310 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
311 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
312 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
313 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
314 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
315 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700316 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
317 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530318 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
319 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
320 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
321 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
322 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
323 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
324 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
325 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
326 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
327 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
328 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
329 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
330 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
331 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
332 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
333 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
334 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
335 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
336 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
337 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
338 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
339 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
340 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
341 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
342 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
343 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
344 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
345 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
346 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
347 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
348 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
349 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
350 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
351 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
352 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
353 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
354 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
355 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
356 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
357 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
358 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
359 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
360 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
361 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
362 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
363 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
364 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
365 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
366 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
367 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
368 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
369 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
370 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
371 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
372 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
373 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
374 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
375 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700376 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530377 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
378 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
379 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
380 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
381 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
383 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
384 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
385 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
386 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
387 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
388 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
389 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
390 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
391 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
392 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
393 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
394 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
395 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
396 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
397 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
398 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
399 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
400 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
401 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
402 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
403 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
404 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
405 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
406 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
407 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
408 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
409 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
410 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700411 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530412 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
413 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
414 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
415 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
416 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
417 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
418 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
419 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
420 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
421 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
422 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
423 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
424 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
425 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
426 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
427 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
428 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
429 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
430 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
431 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
432 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
433 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
434 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
435 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
436 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
437 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
438 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
439 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
440 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
441 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
442 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
443 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
444 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
445 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
446 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
447 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
448 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
449 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
450 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
451 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
452 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
453 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
454 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
455 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
456 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
457 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
458 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
459 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
460 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
461 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
462 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
463 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
464 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
465 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
466 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
467 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
468 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
469 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
470 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
471 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
472 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
473 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
474 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
475 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
476 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
477 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
478 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
479 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
480 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
481 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
484 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
485 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487};
488
489
490static const char *szMIME = "\
491MIME-Version: 1.0\n\
492Content-Type: multipart/mixed;\n\
493boundary=\"XXXXboundary text\"\n\
494\n\
495This is a multipart message in MIME format.\n\
496\n\
497--XXXXboundary text\n\
498Content-Type: text/plain\n\
499\n\
500this is the body text\n\
501\n\
502--XXXXboundary text\n\
503Content-Type: text/plain;\n\
504Content-Disposition: attachment;\n\
505filename=\"test.txt\"\n\
506\n\
507this is the attachment text\n\
508\n\
509--XXXXboundary text--";
510
511
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700512static void AddAll(QCBOREncodeContext *pECtx)
513{
514 QCBOREncode_OpenArray(pECtx);
515
516 /* Some ints that are tagged and have strings preceeding them
517 * (not labels becase it is not a map) */
518 QCBOREncode_AddSZString(pECtx, "UINT62");
519 QCBOREncode_AddTag(pECtx, 100);
520 QCBOREncode_AddUInt64(pECtx, 89989909);
521 QCBOREncode_AddSZString(pECtx, "INT64");
522 QCBOREncode_AddTag(pECtx, 76);
523 QCBOREncode_AddInt64(pECtx, 77689989909);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700524 QCBOREncode_AddUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700525 QCBOREncode_AddInt64(pECtx, -44);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700526 QCBOREncode_AddNegativeUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700527
528 /* ints that go in maps */
529 QCBOREncode_OpenMap(pECtx);
530 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
531 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
532 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
533 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
534 QCBOREncode_CloseMap(pECtx);
535
536 /* Epoch Date */
537 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
538
539 /* Epoch date with labels */
540 QCBOREncode_OpenMap(pECtx);
541 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
542 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
543 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
544 QCBOREncode_CloseMap(pECtx);
545
546 /* Binary blobs */
547 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
548
549 /* binary blobs in maps */
550 QCBOREncode_OpenMap(pECtx);
551 QCBOREncode_AddSZString(pECtx, "binbin");
552 QCBOREncode_AddTag(pECtx, 100000);
553 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700554 QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700555 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
556 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
557 QCBOREncode_CloseMap(pECtx);
558
559 /* text blobs */
560 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
561 QCBOREncode_AddSZString(pECtx, "oof\n");
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700562 QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string
563
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700564 const char *szURL =
565 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
566 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
567 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
568 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
569 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
570
571 /* text blobs in maps */
572 QCBOREncode_OpenMap(pECtx);
573 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
574 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
575 QCBOREncode_AddSZString(pECtx, "()()()");
576 QCBOREncode_AddTag(pECtx, 1000);
577 QCBOREncode_AddSZString(pECtx, "rab rab oof");
578 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
579 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
580 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
581 QCBOREncode_AddURIToMap(pECtx,
582 "RFC",
583 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
584 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
585 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
586 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
587 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
588 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
589 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
590 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
591 QCBOREncode_CloseMap(pECtx);
592
593 /* Date strings */
594 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
595 QCBOREncode_OpenMap(pECtx);
596 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
597 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
598 QCBOREncode_CloseMap(pECtx);
599
600 /* true / false ... */
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700601 QCBOREncode_AddUndef(pECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700602 QCBOREncode_OpenMap(pECtx);
603 QCBOREncode_AddSZString(pECtx, "dare");
604 QCBOREncode_AddTag(pECtx, 66);
605 QCBOREncode_AddBool(pECtx, true);
606 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700607 QCBOREncode_AddNULLToMapN(pECtx, 737634);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700608 QCBOREncode_CloseMap(pECtx);
609
610 /* opening an array */
611 QCBOREncode_OpenArray(pECtx);
612 QCBOREncode_CloseArray(pECtx);
613
614 /* opening arrays in a map */
615 QCBOREncode_OpenMap(pECtx);
616 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
617 QCBOREncode_AddTag(pECtx, 1093);
618 QCBOREncode_OpenArray(pECtx);
619 QCBOREncode_CloseArray(pECtx);
620 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
621 QCBOREncode_CloseArray(pECtx);
622 QCBOREncode_OpenArrayInMapN(pECtx, 42);
623 QCBOREncode_CloseArray(pECtx);
624 QCBOREncode_CloseMap(pECtx);
625
626 /* opening maps with labels and tagging */
627 QCBOREncode_OpenMap(pECtx);
628 QCBOREncode_OpenMapInMap(pECtx, "in a map");
629 QCBOREncode_OpenMapInMapN(pECtx, 5556);
630 QCBOREncode_AddSZString(pECtx, "in a in a in a");
631 QCBOREncode_AddTag(pECtx, 9087);
632 QCBOREncode_OpenMap(pECtx);
633 QCBOREncode_CloseMap(pECtx);
634 QCBOREncode_CloseMap(pECtx);
635 QCBOREncode_CloseMap(pECtx);
636 QCBOREncode_CloseMap(pECtx);
637
638 /* Extended simple values (these are not standard...) */
639 QCBOREncode_OpenMap(pECtx);
640 QCBOREncode_AddSZString(pECtx, "s1");
641 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700642 QCBOREncode_Private_AddSimple(pECtx, 255);
643 QCBOREncode_Private_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700644 QCBOREncode_AddSZString(pECtx, "s3");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700646 QCBOREncode_Private_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700647 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
648 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700649 QCBOREncode_Private_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700650 QCBOREncode_AddInt64(pECtx, 89); // label before tag
651 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700652 QCBOREncode_Private_AddSimple(pECtx, 19);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700653 QCBOREncode_CloseMap(pECtx);
654
655 /* UUIDs */
656 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
657 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
658 0x49, 0x43, 0x41, 0x32};
659 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
660 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
661 QCBOREncode_OpenMap(pECtx);
662 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
663 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
664 QCBOREncode_CloseMap(pECtx);
665
666 /* Bool */
667 QCBOREncode_AddBool(pECtx, true);
668 QCBOREncode_AddBool(pECtx, false);
669 QCBOREncode_OpenMap(pECtx);
670 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
671 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
672 QCBOREncode_CloseMap(pECtx);
673
674 /* Big numbers */
675 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
676 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
677 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
678 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
679 QCBOREncode_OpenMap(pECtx);
680 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
681 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
682 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
683 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
684 QCBOREncode_CloseMap(pECtx);
685
686 QCBOREncode_CloseArray(pECtx);
687}
688
689
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300690int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700692 /* Improvement: this test should be broken down into several so it is more
693 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 QCBOREncodeContext ECtx;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800695 UsefulBufC Enc;
696 size_t size;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800697 int nReturn = 0;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800698 QCBORError uExpectedErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800699
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530700 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800701 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundblade2d493002024-02-01 11:09:17 -0700703 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704
Laurence Lundblade0595e932018-11-02 22:22:47 +0700705 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706 nReturn = -1;
707 goto Done;
708 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800709
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700710 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530711 nReturn = -2;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800712 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700713 }
714
715
716 /* Also test size calculation */
717 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800718 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700719
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800720 AddAll(&ECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700721
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700722 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
723 nReturn = -10;
724 goto Done;
725 }
726
727 if(size != sizeof(spExpectedEncodedAll)) {
728 nReturn = -11;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800729 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700730 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800731
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800732#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
733 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
734#else
735 uExpectedErr = QCBOR_SUCCESS;
736#endif
737
738 /* Test the QCBOR_ERR_NOT_ALLOWED error codes */
739 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
740 QCBOREncode_AddNegativeUInt64(&ECtx, 1);
741 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
742 nReturn = -21;
743 goto Done;
744 }
745
746 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
747 /* 0x7ff8000000000001ULL is a NaN with a payload. */
748 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
749 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
750 nReturn = -22;
751 goto Done;
752 }
753
754 /* 0x7ffc000000000000ULL is a NaN with a payload. */
755 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
756 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
757 nReturn = -23;
758 goto Done;
759 }
760
761 /* 0x7ff80001UL is a NaN with a payload. */
762 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
763 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
764 nReturn = -24;
765 goto Done;
766 }
767
768 /* 0x7ffc0000UL is a NaN with a payload. */
769 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
770 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
771 nReturn = -24;
772 goto Done;
773 }
774
775
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776Done:
777 return nReturn;
778}
779
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700780
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530781/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200782 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530783 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
784 3B 0000000100000000 # negative(4294967296)
785 3A FFFFFFFF # negative(4294967295)
786 3A FFFFFFFE # negative(4294967294)
787 3A FFFFFFFD # negative(4294967293)
788 3A 7FFFFFFF # negative(2147483647)
789 3A 7FFFFFFE # negative(2147483646)
790 3A 00010001 # negative(65537)
791 3A 00010000 # negative(65536)
792 39 FFFF # negative(65535)
793 39 FFFE # negative(65534)
794 39 FFFD # negative(65533)
795 39 0100 # negative(256)
796 38 FF # negative(255)
797 38 FE # negative(254)
798 38 FD # negative(253)
799 38 18 # negative(24)
800 37 # negative(23)
801 36 # negative(22)
802 20 # negative(0)
803 00 # unsigned(0)
804 00 # unsigned(0)
805 01 # unsigned(1)
806 16 # unsigned(22)
807 17 # unsigned(23)
808 18 18 # unsigned(24)
809 18 19 # unsigned(25)
810 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200811 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530812 18 FE # unsigned(254)
813 18 FF # unsigned(255)
814 19 0100 # unsigned(256)
815 19 0101 # unsigned(257)
816 19 FFFE # unsigned(65534)
817 19 FFFF # unsigned(65535)
818 1A 00010000 # unsigned(65536)
819 1A 00010001 # unsigned(65537)
820 1A 00010002 # unsigned(65538)
821 1A 7FFFFFFF # unsigned(2147483647)
822 1A 7FFFFFFF # unsigned(2147483647)
823 1A 80000000 # unsigned(2147483648)
824 1A 80000001 # unsigned(2147483649)
825 1A FFFFFFFE # unsigned(4294967294)
826 1A FFFFFFFF # unsigned(4294967295)
827 1B 0000000100000000 # unsigned(4294967296)
828 1B 0000000100000001 # unsigned(4294967297)
829 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
830 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
831 */
832static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200833 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800834 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
835 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
836 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
837 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
838 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
839 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
840 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
841 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
842 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
843 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200844 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
845 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
846 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
847 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
848 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
849 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
850 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
851 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
852 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
853 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
854 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
855 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
856 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800857
858/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800859
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860 Test the generation of integers. This also ends up testing
861 encoding of all the different lengths. It encodes integers
862 of many lengths and values, especially around the boundaries
863 for different types of integers. It compares the output
864 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800865
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800866 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300867int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800868{
869 QCBOREncodeContext ECtx;
870 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800871
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530872 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800873 QCBOREncode_OpenArray(&ECtx);
874
875 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
876 QCBOREncode_AddInt64(&ECtx, -4294967297);
877 QCBOREncode_AddInt64(&ECtx, -4294967296);
878 QCBOREncode_AddInt64(&ECtx, -4294967295);
879 QCBOREncode_AddInt64(&ECtx, -4294967294);
880 QCBOREncode_AddInt64(&ECtx, -2147483648);
881 QCBOREncode_AddInt64(&ECtx, -2147483647);
882 QCBOREncode_AddInt64(&ECtx, -65538);
883 QCBOREncode_AddInt64(&ECtx, -65537);
884 QCBOREncode_AddInt64(&ECtx, -65536);
885 QCBOREncode_AddInt64(&ECtx, -65535);
886 QCBOREncode_AddInt64(&ECtx, -65534);
887 QCBOREncode_AddInt64(&ECtx, -257);
888 QCBOREncode_AddInt64(&ECtx, -256);
889 QCBOREncode_AddInt64(&ECtx, -255);
890 QCBOREncode_AddInt64(&ECtx, -254);
891 QCBOREncode_AddInt64(&ECtx, -25);
892 QCBOREncode_AddInt64(&ECtx, -24);
893 QCBOREncode_AddInt64(&ECtx, -23);
894 QCBOREncode_AddInt64(&ECtx, -1);
895 QCBOREncode_AddInt64(&ECtx, 0);
896 QCBOREncode_AddUInt64(&ECtx, 0ULL);
897 QCBOREncode_AddInt64(&ECtx, 1);
898 QCBOREncode_AddInt64(&ECtx, 22);
899 QCBOREncode_AddInt64(&ECtx, 23);
900 QCBOREncode_AddInt64(&ECtx, 24);
901 QCBOREncode_AddInt64(&ECtx, 25);
902 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200903 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800904 QCBOREncode_AddInt64(&ECtx, 254);
905 QCBOREncode_AddInt64(&ECtx, 255);
906 QCBOREncode_AddInt64(&ECtx, 256);
907 QCBOREncode_AddInt64(&ECtx, 257);
908 QCBOREncode_AddInt64(&ECtx, 65534);
909 QCBOREncode_AddInt64(&ECtx, 65535);
910 QCBOREncode_AddInt64(&ECtx, 65536);
911 QCBOREncode_AddInt64(&ECtx, 65537);
912 QCBOREncode_AddInt64(&ECtx, 65538);
913 QCBOREncode_AddInt64(&ECtx, 2147483647);
914 QCBOREncode_AddInt64(&ECtx, 2147483647);
915 QCBOREncode_AddInt64(&ECtx, 2147483648);
916 QCBOREncode_AddInt64(&ECtx, 2147483649);
917 QCBOREncode_AddInt64(&ECtx, 4294967294);
918 QCBOREncode_AddInt64(&ECtx, 4294967295);
919 QCBOREncode_AddInt64(&ECtx, 4294967296);
920 QCBOREncode_AddInt64(&ECtx, 4294967297);
921 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
922 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800923
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800925
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530926 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700927 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800928 nReturn = -1;
929 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530931 if(CheckResults(Enc, spExpectedEncodedInts))
932 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800933
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 return(nReturn);
935}
936
937
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530938/*
939 85 # array(5)
940 F5 # primitive(21)
941 F4 # primitive(20)
942 F6 # primitive(22)
943 F7 # primitive(23)
944 A1 # map(1)
945 65 # text(5)
946 554E446566 # "UNDef"
947 F7 # primitive(23)
948 */
949static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
951
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300952int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953{
954 QCBOREncodeContext ECtx;
955 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800956
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530957 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900959
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700960 QCBOREncode_AddBool(&ECtx, true);
961 QCBOREncode_AddBool(&ECtx, false);
962 QCBOREncode_AddNULL(&ECtx);
963 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800964
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 QCBOREncode_OpenMap(&ECtx);
966
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700967 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 QCBOREncode_CloseMap(&ECtx);
969
970 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800971
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530972 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700973 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800974 nReturn = -1;
975 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800976
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530977 if(CheckResults(ECBOR, spExpectedEncodedSimple))
978 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800979
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980 return(nReturn);
981}
982
Jan Jongboom47d86c52019-07-25 08:54:16 +0200983/*
984 9F # array(5)
985 F5 # primitive(21)
986 F4 # primitive(20)
987 F6 # primitive(22)
988 F7 # primitive(23)
989 BF # map(1)
990 65 # text(5)
991 554E446566 # "UNDef"
992 F7 # primitive(23)
993 FF # break
994 FF # break
995 */
996static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
997 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
998
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300999int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +02001000{
1001 QCBOREncodeContext ECtx;
1002 int nReturn = 0;
1003
1004 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1005 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
1006
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001007 QCBOREncode_AddBool(&ECtx, true);
1008 QCBOREncode_AddBool(&ECtx, false);
1009 QCBOREncode_AddNULL(&ECtx);
1010 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +02001011
1012 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
1013
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001014 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +02001015 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
1016
1017 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
1018
1019 UsefulBufC ECBOR;
1020 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1021 nReturn = -1;
1022 }
1023
1024 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
1025 return -2;
1026
1027 return(nReturn);
1028}
1029
Jan Jongboom5d827882019-08-07 12:51:15 +02001030/*
1031A5 # map(5)
1032 63 # text(3)
1033 617272 # "arr"
1034 98 1F # array(31)
1035 00 # unsigned(0)
1036 01 # unsigned(1)
1037 02 # unsigned(2)
1038 03 # unsigned(3)
1039 04 # unsigned(4)
1040 05 # unsigned(5)
1041 06 # unsigned(6)
1042 07 # unsigned(7)
1043 08 # unsigned(8)
1044 09 # unsigned(9)
1045 0A # unsigned(10)
1046 0B # unsigned(11)
1047 0C # unsigned(12)
1048 0D # unsigned(13)
1049 0E # unsigned(14)
1050 0F # unsigned(15)
1051 10 # unsigned(16)
1052 11 # unsigned(17)
1053 12 # unsigned(18)
1054 13 # unsigned(19)
1055 14 # unsigned(20)
1056 15 # unsigned(21)
1057 16 # unsigned(22)
1058 17 # unsigned(23)
1059 18 18 # unsigned(24)
1060 18 19 # unsigned(25)
1061 18 1A # unsigned(26)
1062 18 1B # unsigned(27)
1063 18 1C # unsigned(28)
1064 18 1D # unsigned(29)
1065 18 1E # unsigned(30)
1066 63 # text(3)
1067 6D6170 # "map"
1068 B8 1F # map(31)
1069 61 # text(1)
1070 61 # "a"
1071 00 # unsigned(0)
1072 61 # text(1)
1073 62 # "b"
1074 01 # unsigned(1)
1075 61 # text(1)
1076 63 # "c"
1077 02 # unsigned(2)
1078 61 # text(1)
1079 64 # "d"
1080 03 # unsigned(3)
1081 61 # text(1)
1082 65 # "e"
1083 04 # unsigned(4)
1084 61 # text(1)
1085 66 # "f"
1086 05 # unsigned(5)
1087 61 # text(1)
1088 67 # "g"
1089 06 # unsigned(6)
1090 61 # text(1)
1091 68 # "h"
1092 07 # unsigned(7)
1093 61 # text(1)
1094 69 # "i"
1095 08 # unsigned(8)
1096 61 # text(1)
1097 6A # "j"
1098 09 # unsigned(9)
1099 61 # text(1)
1100 6B # "k"
1101 0A # unsigned(10)
1102 61 # text(1)
1103 6C # "l"
1104 0B # unsigned(11)
1105 61 # text(1)
1106 6D # "m"
1107 0C # unsigned(12)
1108 61 # text(1)
1109 6E # "n"
1110 0D # unsigned(13)
1111 61 # text(1)
1112 6F # "o"
1113 0E # unsigned(14)
1114 61 # text(1)
1115 70 # "p"
1116 0F # unsigned(15)
1117 61 # text(1)
1118 71 # "q"
1119 10 # unsigned(16)
1120 61 # text(1)
1121 72 # "r"
1122 11 # unsigned(17)
1123 61 # text(1)
1124 73 # "s"
1125 12 # unsigned(18)
1126 61 # text(1)
1127 74 # "t"
1128 13 # unsigned(19)
1129 61 # text(1)
1130 75 # "u"
1131 14 # unsigned(20)
1132 61 # text(1)
1133 76 # "v"
1134 15 # unsigned(21)
1135 61 # text(1)
1136 77 # "w"
1137 16 # unsigned(22)
1138 61 # text(1)
1139 78 # "x"
1140 17 # unsigned(23)
1141 61 # text(1)
1142 79 # "y"
1143 18 18 # unsigned(24)
1144 61 # text(1)
1145 7A # "z"
1146 18 19 # unsigned(25)
1147 61 # text(1)
1148 41 # "A"
1149 18 1A # unsigned(26)
1150 61 # text(1)
1151 42 # "B"
1152 18 1B # unsigned(27)
1153 61 # text(1)
1154 43 # "C"
1155 18 1C # unsigned(28)
1156 61 # text(1)
1157 44 # "D"
1158 18 1D # unsigned(29)
1159 61 # text(1)
1160 45 # "E"
1161 18 1E # unsigned(30)
1162 65 # text(5)
1163 6D696E3331 # "min31"
1164 38 1E # negative(30)
1165 66 # text(6)
1166 706C75733331 # "plus31"
1167 18 1F # unsigned(31)
1168 63 # text(3)
1169 737472 # "str"
1170 78 1F # text(31)
1171 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1172 */
1173static const uint8_t EncodeLengthThirtyone[] = {
1174 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1175 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1176 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1177 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1178 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1179 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1180 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1181 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1182 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1183 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1184 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1185 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1186 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1187 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1188 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1189 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1190 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1191 0x31
1192};
1193
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001194int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001195{
1196 QCBOREncodeContext ECtx;
1197 int nReturn = 0;
1198
1199 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1200 QCBOREncode_OpenMap(&ECtx);
1201
1202 // add array with 31 items
1203 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1204 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001205 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001206 }
1207 QCBOREncode_CloseArray(&ECtx);
1208
1209 // add map with 31 items
1210 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001211 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001212 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001213 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001214 if (ix < 26) c = c + ix;
1215 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001216 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001217 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1218 }
1219 QCBOREncode_CloseMap(&ECtx);
1220
1221 // add -31 and +31
1222 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1223 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1224
1225 // add string with length 31
1226 const char *str = "testtesttesttesttesttestqcbor11";
1227 UsefulBufC str_b = { str, 31 };
1228 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1229
1230 QCBOREncode_CloseMap(&ECtx);
1231
1232 UsefulBufC ECBOR;
1233 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1234 nReturn = -1;
1235 }
1236
1237 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1238 return -2;
1239
1240 return(nReturn);
1241}
1242
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301243
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301244/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001245 * [ "2013-03-21T20:04:00Z",
1246 * 0("2013-03-21T20:04:00Z"),
1247 * 1363896240,
1248 * 1(1363896240),
1249 * 100(-10676),
1250 * 3994,
1251 * 1004("1940-10-09"),
1252 * "1980-12-08",
1253 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1254 * "SD": 1(999),
1255 * "Sample Date from RFC 8943": "1985-04-12",
1256 * 42: 1004("1985-04-12T23:20:50.52Z"),
1257 * "SY": 100(-10676),
1258 * 45: 3994
1259 * }
1260 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301261 */
1262static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001263 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1264 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1265 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1266 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1267 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1268 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1269 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1270 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1271 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1272 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1273 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1274 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1275 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1276 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1277 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1278 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1279 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1280 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1281 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1282 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1283 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1284 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1285 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001286
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001287int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001288{
1289 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001290
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301291 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293 QCBOREncode_OpenArray(&ECtx);
1294
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001295 /* The values are taken from the CBOR RFCs */
1296 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1297 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1298 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1299 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1300 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1301 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1302 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1303 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001305 QCBOREncode_OpenMap(&ECtx);
1306
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001307 QCBOREncode_AddDateStringToMap(&ECtx,
1308 "Sample Date from RFC 3339",
1309 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001310 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001311 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1312 "Sample Date from RFC 8943",
1313 QCBOR_ENCODE_AS_BORROWED,
1314 "1985-04-12");
1315 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1316 42,
1317 QCBOR_ENCODE_AS_TAG,
1318 "1985-04-12T23:20:50.52Z");
1319 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1320 "SY",
1321 QCBOR_ENCODE_AS_TAG,
1322 -10676);
1323 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1324 45,
1325 QCBOR_ENCODE_AS_BORROWED,
1326 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001330 QCBOREncode_CloseArray(&ECtx);
1331
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301332 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001333 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001334 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001336
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301337 if(CheckResults(ECBOR, spExpectedEncodedDates))
1338 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001340 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001341}
1342
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301343
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001344int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345{
1346 QCBOREncodeContext ECtx;
1347 int i;
1348 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001349
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301350 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1352 QCBOREncode_OpenArray(&ECtx);
1353 }
1354 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1355 QCBOREncode_CloseArray(&ECtx);
1356 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001357 UsefulBufC Encoded;
1358 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 nReturn = -1;
1360 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361
1362 return(nReturn);
1363}
1364
1365
1366
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001367int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368{
1369 QCBOREncodeContext ECtx;
1370 int i;
1371 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001372
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301373 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001374 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1375 QCBOREncode_OpenArray(&ECtx);
1376 }
1377 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1378 QCBOREncode_CloseArray(&ECtx);
1379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001380
Laurence Lundblade0595e932018-11-02 22:22:47 +07001381 UsefulBufC Encoded;
1382 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 nReturn = -1;
1384 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001385
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 return(nReturn);
1387}
1388
1389
1390
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001391int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392{
1393 QCBOREncodeContext ECtx;
1394 int i;
1395 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001396
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301397 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1399 QCBOREncode_OpenArray(&ECtx);
1400 }
1401 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1402 QCBOREncode_CloseArray(&ECtx);
1403 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001404 UsefulBufC Encoded;
1405 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 nReturn = -1;
1407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001409 return(nReturn);
1410}
1411
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301413/*
1414 81 # array(1)
1415 81 # array(1)
1416 81 # array(1)
1417 81 # array(1)
1418 80 # array(0)
1419*/
1420static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301423/*
1424 82 # array(2)
1425 81 # array(1)
1426 81 # array(1)
1427 81 # array(1)
1428 81 # array(1)
1429 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001430 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301431 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1432 3B 0000000100000000 # negative(4294967296)
1433 3A FFFFFFFF # negative(4294967295)
1434 3A FFFFFFFE # negative(4294967294)
1435 3A FFFFFFFD # negative(4294967293)
1436 3A 7FFFFFFF # negative(2147483647)
1437 3A 7FFFFFFE # negative(2147483646)
1438 3A 00010001 # negative(65537)
1439 3A 00010000 # negative(65536)
1440 39 FFFF # negative(65535)
1441 39 FFFE # negative(65534)
1442 39 FFFD # negative(65533)
1443 39 0100 # negative(256)
1444 38 FF # negative(255)
1445 38 FE # negative(254)
1446 38 FD # negative(253)
1447 38 18 # negative(24)
1448 37 # negative(23)
1449 36 # negative(22)
1450 20 # negative(0)
1451 00 # unsigned(0)
1452 00 # unsigned(0)
1453 01 # unsigned(1)
1454 16 # unsigned(22)
1455 17 # unsigned(23)
1456 18 18 # unsigned(24)
1457 18 19 # unsigned(25)
1458 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001459 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301460 18 FE # unsigned(254)
1461 18 FF # unsigned(255)
1462 19 0100 # unsigned(256)
1463 19 0101 # unsigned(257)
1464 19 FFFE # unsigned(65534)
1465 19 FFFF # unsigned(65535)
1466 1A 00010000 # unsigned(65536)
1467 1A 00010001 # unsigned(65537)
1468 1A 00010002 # unsigned(65538)
1469 1A 7FFFFFFF # unsigned(2147483647)
1470 1A 7FFFFFFF # unsigned(2147483647)
1471 1A 80000000 # unsigned(2147483648)
1472 1A 80000001 # unsigned(2147483649)
1473 1A FFFFFFFE # unsigned(4294967294)
1474 1A FFFFFFFF # unsigned(4294967295)
1475 1B 0000000100000000 # unsigned(4294967296)
1476 1B 0000000100000001 # unsigned(4294967297)
1477 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1478 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1479 */
1480static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001481 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001482 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1483 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1484 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1485 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1486 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1487 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1488 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1489 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1490 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1491 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1492 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001493 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1494 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1495 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1496 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1497 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1498 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1499 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1500 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1501 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1502 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1503 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1504 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1505 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001506
1507
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001508int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001510 QCBOREncodeContext ECtx;
1511
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301512 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001513 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301514 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1515 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001516 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001517
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001518 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001519
Laurence Lundblade0595e932018-11-02 22:22:47 +07001520 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001521 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001523
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301524 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001525 return -5;
1526 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001527
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001528 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529}
1530
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301531/*
1532 This returns a pointer to spBigBuf
1533 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001534static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535{
1536 QCBOREncodeContext ECtx;
1537 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001538
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539 *pEncoded = NULL;
1540 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301541 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001543 // loop runs CBOR encoding twice. First with no buffer to
1544 // calucate the length so buffer can be allocated correctly,
1545 // and last with the buffer to do the actual encoding
1546 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301547 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 QCBOREncode_OpenMap(&ECtx);
1549 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1550 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1551 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1552 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1553 QCBOREncode_CloseArray(&ECtx);
1554 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1555 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1556 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1557 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1558 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1559 QCBOREncode_CloseMap(&ECtx);
1560 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001561
Laurence Lundblade0595e932018-11-02 22:22:47 +07001562 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 goto Done;
1564 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301565 if(uFirstSizeEstimate != *pEncodedLen) {
1566 nReturn = 1;
1567 } else {
1568 nReturn = 0;
1569 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 goto Done;
1571 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301572 *pEncoded = spBigBuf;
1573 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001577 Done:
1578 return(nReturn);
1579}
1580
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301581/*
1582 A3 # map(3)
1583 6D # text(13)
1584 666972737420696E7465676572 # "first integer"
1585 18 2A # unsigned(42)
1586 77 # text(23)
1587 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1588 82 # array(2)
1589 67 # text(7)
1590 737472696E6731 # "string1"
1591 67 # text(7)
1592 737472696E6732 # "string2"
1593 6C # text(12)
1594 6D617020696E2061206D6170 # "map in a map"
1595 A4 # map(4)
1596 67 # text(7)
1597 62797465732031 # "bytes 1"
1598 44 # bytes(4)
1599 78787878 # "xxxx"
1600 67 # text(7)
1601 62797465732032 # "bytes 2"
1602 44 # bytes(4)
1603 79797979 # "yyyy"
1604 6B # text(11)
1605 616E6F7468657220696E74 # "another int"
1606 18 62 # unsigned(98)
1607 66 # text(6)
1608 746578742032 # "text 2"
1609 78 1E # text(30)
1610 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1611 */
1612static const uint8_t spValidMapEncoded[] = {
1613 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1614 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1615 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1616 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1617 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1618 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1619 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1620 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1621 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1622 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1623 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1624 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1625 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1626 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1627 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1628 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001629
1630
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001631int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001632{
1633 uint8_t *pEncodedMaps;
1634 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001635
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001636 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301637 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001640 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301641 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1642 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001644 return(nReturn);
1645}
1646
1647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001648/*
1649 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001650
Laurence Lundbladeee851742020-01-08 08:37:05 -08001651 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1652 CBOR_SIMPLEV_NULL
1653 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001654 @param[in] szAlexString Diagnostic code.
1655 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001656 @param[in/out] pnLen Size of pOut buffer when called; length of data
1657 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001658
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001659 @return
1660 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001661
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001662 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 -08001663 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001664 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001665
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001666 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1667 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001669 */
1670
Laurence Lundbladeee851742020-01-08 08:37:05 -08001671static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001672FormatRTICResults(uint8_t uRResult,
1673 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001674 const char *szType,
1675 const char *szAlexString,
1676 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001677{
1678 // Buffer that the result will be written in to
1679 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001680 // QCBOREncode will never write off the end of this buffer. If it won't
1681 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001682
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001683 // Context for the encoder
1684 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301685 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1688 // Contents are label / value pairs
1689 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001691 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001692
Laurence Lundbladeee851742020-01-08 08:37:05 -08001693 // The result: 0 if scan happened and found nothing; 1 if it happened and
1694 // found something wrong; 2 if it didn't happen
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001695 QCBOREncode_Private_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001696
1697 // Add the diagnostic code
1698 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1699
1700 // Add a time stamp
1701 if(time) {
1702 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1703 }
1704
1705 // Add the diagnostic code
1706 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1707
1708 // Open a subordinate map for telemtry data
1709 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1710
1711 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001712
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001713 // Add a few fake integers and buffers for now.
1714 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1715
1716 // Add a few fake integers and buffers for now.
1717 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1718
1719 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301720 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001721 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001722
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001723 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1724 }
1725 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001727 // Close the telemetry map
1728 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001729
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001730 // Close the map
1731 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001732
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301733 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001734
Laurence Lundblade0595e932018-11-02 22:22:47 +07001735 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001736
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301737 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738}
1739
1740
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301741/*
1742 A5 # map(5)
1743 69 # text(9)
1744 696E74656772697479 # "integrity"
1745 F4 # primitive(20)
1746 64 # text(4)
1747 74797065 # "type"
1748 66 # text(6)
1749 726563656E74 # "recent"
1750 64 # text(4)
1751 74696D65 # "time"
1752 C1 # tag(1)
1753 1A 580D4172 # unsigned(1477263730)
1754 64 # text(4)
1755 64696167 # "diag"
1756 6A # text(10)
1757 30784131654335303031 # "0xA1eC5001"
1758 69 # text(9)
1759 74656C656D65747279 # "telemetry"
1760 A3 # map(3)
1761 69 # text(9)
1762 53686F652053697A65 # "Shoe Size"
1763 0C # unsigned(12)
1764 62 # text(2)
1765 4951 # "IQ"
1766 1A FFFFFFFF # unsigned(4294967295)
1767 77 # text(23)
1768 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1769 48 # bytes(8)
1770 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1771 */
1772static const uint8_t spExpectedRTIC[] = {
1773 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1774 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1775 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1776 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1777 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1778 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1779 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1780 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1781 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1782 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1783 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1784 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001785
1786
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001787int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001788{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001789 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301790 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301791 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301792 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001793 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301794 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001795
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301796 if(CheckResults(Encoded, spExpectedRTIC)) {
1797 return -2;
1798 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001799
1800 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001801}
1802
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301803
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301804/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001805 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001806
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301807 82 # array(2)
1808 19 01C3 # unsigned(451)
1809 43 # bytes(3)
1810 1901D2 # "\x19\x01\xD2"
1811*/
1812static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301813
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001814static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1815
Laurence Lundbladeda532272019-04-07 11:40:17 -07001816/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001817 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001818 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001819int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001820{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001821 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001822
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001823 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301824 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001825
Laurence Lundblade684aec22018-10-12 19:33:53 +08001826 QCBOREncode_OpenArray(&EC);
1827 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001828
Laurence Lundblade684aec22018-10-12 19:33:53 +08001829 QCBOREncode_BstrWrap(&EC);
1830 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001831
Laurence Lundblade684aec22018-10-12 19:33:53 +08001832 UsefulBufC Wrapped;
1833 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001834
Laurence Lundblade684aec22018-10-12 19:33:53 +08001835 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001836
Laurence Lundblade684aec22018-10-12 19:33:53 +08001837 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001838 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001839 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001841
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301842 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001843 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001844 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001845
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001846 // Second test - see if the length of the wrapped
1847 // bstr is correct. Also tests bstr wrapping
1848 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001849 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1850 QCBOREncode_OpenArray(&EC);
1851 QCBOREncode_BstrWrap(&EC);
1852 QCBOREncode_OpenArray(&EC);
1853 QCBOREncode_AddNULL(&EC);
1854 QCBOREncode_CloseArray(&EC);
1855 UsefulBufC BStr;
1856 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001857 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1858 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001859 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001860 return -5;
1861 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001862
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001863
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001864 // Fourth test, cancelling a byte string
1865 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1866
1867 QCBOREncode_OpenArray(&EC);
1868 QCBOREncode_AddUInt64(&EC, 451);
1869
1870 QCBOREncode_BstrWrap(&EC);
1871 QCBOREncode_CancelBstrWrap(&EC);
1872
1873
1874 QCBOREncode_AddUInt64(&EC, 42);
1875 QCBOREncode_CloseArray(&EC);
1876 if(QCBOREncode_Finish(&EC, &Encoded)) {
1877 return -8;
1878 }
1879 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1880 return -9;
1881 }
1882
1883 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001884 // Fifth test, failed cancelling
1885 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1886
1887 QCBOREncode_OpenArray(&EC);
1888 QCBOREncode_AddUInt64(&EC, 451);
1889
1890 QCBOREncode_BstrWrap(&EC);
1891 QCBOREncode_AddUInt64(&EC, 99);
1892 QCBOREncode_CancelBstrWrap(&EC);
1893
1894 QCBOREncode_AddUInt64(&EC, 42);
1895 QCBOREncode_CloseArray(&EC);
1896 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001897#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001898 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1899 return -10;
1900 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001901#else
1902 if(uErr != QCBOR_SUCCESS) {
1903 return -110;
1904 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001905#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1906
1907 // Sixth test, another cancel, but the error is not caught
1908 // This use will produce unintended CBOR. The error
1909 // is not caught because it would require tracking state
1910 // for QCBOREncode_BstrWrapInMapN.
1911 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1912
1913 QCBOREncode_OpenMap(&EC);
1914 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1915
1916 QCBOREncode_BstrWrapInMapN(&EC, 55);
1917 QCBOREncode_CancelBstrWrap(&EC);
1918
1919 QCBOREncode_CloseMap(&EC);
1920 uErr = QCBOREncode_Finish(&EC, &Encoded);
1921 if(uErr != QCBOR_SUCCESS) {
1922 return -11;
1923 }
1924
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001925 return 0;
1926}
1927
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001928
1929
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001930int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001931{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001932 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001933 UsefulBufC Wrapped;
1934 UsefulBufC Encoded2;
1935 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001936
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001937 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001938
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301939 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001940
Laurence Lundblade684aec22018-10-12 19:33:53 +08001941 QCBOREncode_OpenArray(&EC);
1942 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundblade684aec22018-10-12 19:33:53 +08001944 QCBOREncode_BstrWrap(&EC);
1945 QCBOREncode_AddUInt64(&EC, 466);
1946 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001947
Laurence Lundblade684aec22018-10-12 19:33:53 +08001948 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001949
Laurence Lundblade684aec22018-10-12 19:33:53 +08001950 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001951
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001952 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001953#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001954 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001955 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001956 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001957#else
1958 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1959 * is set and not to be sure to excerice all the relavant code in
1960 * both conditions. When the guards are disabled, there is no
1961 * error returned, but the code path is still covered.
1962 */
1963 if(uError != QCBOR_SUCCESS) {
1964 return (int32_t)(600 + uError);
1965 }
1966#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001967
Laurence Lundbladeee851742020-01-08 08:37:05 -08001968 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301969 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001970 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001971 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001972#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001973 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001974 return (int32_t)(700 + uError);
1975 }
1976#else
1977 if(uError != QCBOR_SUCCESS) {
1978 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001979 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001980#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001981
Laurence Lundblade684aec22018-10-12 19:33:53 +08001982 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301983 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001984 for(int i = 1; i < 18; i++) {
1985 QCBOREncode_BstrWrap(&EC);
1986 }
1987 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001988
Laurence Lundblade684aec22018-10-12 19:33:53 +08001989 for(int i = 1; i < 18; i++) {
1990 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001992
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001993 uError = QCBOREncode_Finish(&EC, &Encoded2);
1994 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001995 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001996 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001997
Laurence Lundblade684aec22018-10-12 19:33:53 +08001998 return 0;
1999}
2000
2001
Laurence Lundblade684aec22018-10-12 19:33:53 +08002002/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002003 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002004
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002005 [
2006 h'82004E82014B8202488203458204428105',
2007 {
2008 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
2009 8426568656C6C6F18234BA2131318436568656C6C6F'
2010 }
2011 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002012
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002013 Unwrapping the first byte string in the above gives
2014 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302015
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002016 Unwrapping again, the byte string immediately above gives
2017 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302018
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002019 ...
2020
2021 Unrapping the second byte string in the top-level CBOR above gives
2022 {16: 16,
2023 64: "hello",
2024 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2025 }
2026
2027 Unwrapping again, the byte string immediately above gives
2028 {17: 17,
2029 65: "hello",
2030 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2031 }
2032
2033 ...
2034
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302035 */
2036static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002037{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002038 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2039 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2040 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2041 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2042 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2043 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2044 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2045 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2046 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2047 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002048};
2049
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002050
2051/*
2052 Get an int64 out of the decoder or fail.
2053 */
2054static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002055{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002056 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002057 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002058
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002059 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002060 if(nReturn) {
2061 return nReturn;
2062 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002063 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002064 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002066
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002067 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002068 return 0;
2069}
2070
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002071/*
2072 Get an array out of the decoder or fail.
2073 */
2074static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002075{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002076 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002077 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002078
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002079 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002080 if(nReturn) {
2081 return nReturn;
2082 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002083 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2084 return -1;
2085 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002086
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002087 *pInt = Item.val.uCount;
2088 return 0;
2089}
2090
2091/*
2092 Get a map out of the decoder or fail.
2093 */
2094static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2095{
2096 QCBORItem Item;
2097 int32_t nReturn;
2098
2099 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002100 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002101 return nReturn;
2102 }
2103 if(Item.uDataType != QCBOR_TYPE_MAP) {
2104 return -1;
2105 }
2106
2107 *pInt = Item.val.uCount;
2108 return 0;
2109}
2110
2111/*
2112 Get a byte string out of the decoder or fail.
2113 */
2114static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2115{
2116 QCBORItem Item;
2117 int32_t nReturn;
2118
2119 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2120 if(nReturn) {
2121 return nReturn;
2122 }
2123 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002124 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002125 }
2126
2127 *pBstr = Item.val.string;
2128 return 0;
2129}
2130
2131/*
2132 Get a byte string out of the decoder or fail.
2133 */
2134static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2135{
2136 QCBORItem Item;
2137 int nReturn;
2138
2139 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2140 if(nReturn) {
2141 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002142 }
2143 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002144 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002145 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002146
2147 *pTstr = Item.val.string;
2148 return 0;
2149}
2150
2151
2152/*
2153 Recursively decode array containing a little CBOR and a bstr wrapped array
2154 with a little CBOR and a bstr wrapped array...
2155
2156 Part of bstr_wrap_nest_test.
2157 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2158{
2159 int64_t nInt;
2160 UsefulBufC Bstr;
2161 uint16_t nArrayCount;
2162 QCBORDecodeContext DC;
2163 int32_t nResult;
2164
2165 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2166
2167 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2168 return -10;
2169 }
2170
2171 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002172 return -11;
2173 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002174
2175 nResult = GetByteString(&DC, &Bstr);
2176 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2177 if(nArrayCount != 1) {
2178 return -12;
2179 } else {
2180 // successful exit
2181 return 0;
2182 }
2183 }
2184 if(nResult) {
2185 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002187
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002188 // tail recursion; good compilers will reuse the stack frame
2189 return DecodeNextNested(Bstr);
2190}
2191
2192
2193/*
2194 Recursively decode map containing a little CBOR and a bstr wrapped map
2195 with a little CBOR and a bstr wrapped map...
2196
2197 Part of bstr_wrap_nest_test.
2198 */
2199static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2200{
2201 int32_t nResult;
2202 uint16_t nMapCount;
2203 int64_t nInt;
2204 UsefulBufC Bstr;
2205 QCBORDecodeContext DC;
2206
2207 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2208
2209 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2210 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002212
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002213 if(GetInt64(&DC, &nInt)) {
2214 return -21;
2215 }
2216
2217 // The "hello"
2218 if(GetTextString(&DC, &Bstr)) {
2219 return -22;
2220 }
2221
2222 nResult = GetByteString(&DC, &Bstr);
2223 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2224 if(nMapCount == 2) {
2225 // successful exit
2226 return 0;
2227 } else {
2228 return -23;
2229 }
2230 }
2231
2232 if(nResult) {
2233 return -24;
2234 }
2235
2236 // tail recursion; good compilers will reuse the stack frame
2237 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002238}
2239
2240
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002241int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002242{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002243 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302244 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundblade684aec22018-10-12 19:33:53 +08002246 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002247 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002248
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002249 QCBOREncode_OpenArray(&EC);
2250
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002251 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002252 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002253 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002254 QCBOREncode_AddInt64(&EC, i);
2255 }
2256 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2257 QCBOREncode_CloseArray(&EC);
2258 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002261 QCBOREncode_OpenMap(&EC);
2262 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2263 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2264 QCBOREncode_OpenMap(&EC);
2265 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002266 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002267 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002268
2269 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2270 QCBOREncode_CloseMap(&EC);
2271 QCBOREncode_CloseBstrWrap(&EC, NULL);
2272 }
2273 QCBOREncode_CloseMap(&EC);
2274
Laurence Lundblade684aec22018-10-12 19:33:53 +08002275 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002276
Laurence Lundblade684aec22018-10-12 19:33:53 +08002277 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002278 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002279 return -1;
2280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundblade684aec22018-10-12 19:33:53 +08002282 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302283 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002284 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002286
Laurence Lundblade684aec22018-10-12 19:33:53 +08002287 // ---- Decode it and see if it is OK ------
2288 QCBORDecodeContext DC;
2289 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002290
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002291 UsefulBufC Bstr;
2292 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002294 // Array surrounding the the whole thing
2295 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002296 return -3;
2297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002298
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002299 // Get the byte string wrapping some array stuff
2300 if(GetByteString(&DC, &Bstr)) {
2301 return -4;
2302 }
2303
2304 // Decode the wrapped nested structure
2305 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002306 if(nReturn) {
2307 return nReturn;
2308 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002310 // A map enclosing some map-oriented bstr wraps
2311 if(GetMap(&DC, &nArrayCount)) {
2312 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002314
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002315 // Get the byte string wrapping some array stuff
2316 if(GetByteString(&DC, &Bstr)) {
2317 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002318 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002319
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002320 // Decode the wrapped nested structure
2321 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002322 if(nReturn) {
2323 return nReturn;
2324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundblade684aec22018-10-12 19:33:53 +08002326 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002327 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002328 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002329
Laurence Lundblade684aec22018-10-12 19:33:53 +08002330 return 0;
2331}
2332
2333
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002334static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302335 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2336 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2337 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2338 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2339 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2340 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2341 0x45, 0xca, 0xcb, 0x36};
2342
2343/*
2344 D2 # tag(18)
2345 84 # array(4)
2346 43 # bytes(3)
2347 A10126 # "\xA1\x01&"
2348 A1 # map(1)
2349 04 # unsigned(4)
2350 42 # bytes(2)
2351 3131 # "11"
2352 54 # bytes(20)
2353 546869732069732074686520636F6E74656E742E # "This is the content."
2354 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002355 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2356 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2357 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2358 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2359 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2360 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2361 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2362 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302363 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002364static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302365 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2366 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2367 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2368 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2369 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2370 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2371 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2372 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2373 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2374 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2375
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002376static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2377
2378
Laurence Lundblade684aec22018-10-12 19:33:53 +08002379/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002380 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002381 C.2.1. This doesn't actually verify the signature (however
2382 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002383 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002384int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002385{
2386 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002387 const char *szKid = "11";
2388 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2389 const char *szPayload = "This is the content.";
2390 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2391 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2392 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002393
Laurence Lundblade684aec22018-10-12 19:33:53 +08002394 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002395
2396 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302397 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002398
Laurence Lundblade684aec22018-10-12 19:33:53 +08002399 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002400 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002401 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002402
Laurence Lundblade684aec22018-10-12 19:33:53 +08002403 // Add protected headers
2404 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundblade684aec22018-10-12 19:33:53 +08002406 // Empty map with unprotected headers
2407 QCBOREncode_OpenMap(&EC);
2408 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2409 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002410
Laurence Lundblade684aec22018-10-12 19:33:53 +08002411 // The payload
2412 UsefulBufC WrappedPayload;
2413 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002414 // Payload is not actually CBOR in example C.2.1 like it would be
2415 // for a CWT or EAT. It is just a text string.
2416 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002417 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002418
Laurence Lundblade684aec22018-10-12 19:33:53 +08002419 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002420 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2421 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002422 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002423 return -1;
2424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002425
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002426/* if(UsefulBuf_Compare(WrappedPayload,
2427 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2428 return -1;
2429 } */
2430
Laurence Lundblade684aec22018-10-12 19:33:53 +08002431 // The signature
2432 QCBOREncode_AddBytes(&EC, Signature);
2433 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002434
Laurence Lundblade684aec22018-10-12 19:33:53 +08002435 // Finish and check the results
2436 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002437 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002438 return -2;
2439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002440
Laurence Lundblade684aec22018-10-12 19:33:53 +08002441 // 98 is the size from RFC 8152 C.2.1
2442 if(COSE_Sign1.len != 98) {
2443 return -3;
2444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002445
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002446 // It would be good to compare this to the output from a COSE
2447 // implementation like COSE-C. This has been checked against the
2448 // CBOR playground.
2449 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002450 return -4;
2451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002452
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002453
2454 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2455 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2456
2457 // top level array for cose sign1, 18 is the tag for COSE sign
2458 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2459 QCBOREncode_OpenArray(&EC);
2460
2461 // Add protected headers
2462 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2463
2464 // Empty map with unprotected headers
2465 QCBOREncode_OpenMap(&EC);
2466 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2467 QCBOREncode_CloseMap(&EC);
2468
2469 // The payload
2470 QCBOREncode_BstrWrap(&EC);
2471 // Payload is not actually CBOR in example C.2.1 like it would be
2472 // for a CWT or EAT. It is just a text string.
2473 QCBOREncode_AddEncoded(&EC, Payload);
2474 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2475
2476 // Check we got back the actual payload expected
2477 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2478 if(UsefulBuf_Compare(WrappedPayload,
2479 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2480 return -11;
2481 }
2482
2483 // The signature
2484 QCBOREncode_AddBytes(&EC, Signature);
2485 QCBOREncode_CloseArray(&EC);
2486
2487 // Finish and check the results
2488 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2489 return -12;
2490 }
2491
2492 // 98 is the size from RFC 8152 C.2.1
2493 if(COSE_Sign1.len != 98) {
2494 return -13;
2495 }
2496
2497 // It would be good to compare this to the output from a COSE
2498 // implementation like COSE-C. This has been checked against the
2499 // CBOR playground.
2500 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2501 return -14;
2502 }
2503
Laurence Lundblade684aec22018-10-12 19:33:53 +08002504 return 0;
2505}
2506
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002507
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002508int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002509{
2510 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002511 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002512
2513
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002514 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002515 // Do all of these tests with NULL buffers so no actual
2516 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002517 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002518
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002519 // First verify no error from a big buffer
2520 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002521 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002522 // 6 is the CBOR overhead for opening the array and encodng the length
2523 // This exactly fills the buffer.
2524 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002525 QCBOREncode_CloseArray(&EC);
2526 size_t xx;
2527 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2528 return -1;
2529 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002530
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002531 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002532 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002533 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002534 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002535 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002536 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002537 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2538 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002539 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002540 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002541 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002542 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2543 return -2;
2544 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002545
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002546 // Third, fit an array in exactly at max position allowed
2547 QCBOREncode_Init(&EC, Buffer);
2548 QCBOREncode_OpenArray(&EC);
2549 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2550 QCBOREncode_OpenArray(&EC);
2551 QCBOREncode_CloseArray(&EC);
2552 QCBOREncode_CloseArray(&EC);
2553 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2554 return -10;
2555 }
2556
2557
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002558 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2559 // Work close to the 4GB size limit for a better test
2560 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002561 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002562
2563 QCBOREncode_Init(&EC, Large);
2564 QCBOREncode_OpenArray(&EC);
2565 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2566 QCBOREncode_CloseArray(&EC);
2567 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2568 // Making sure it succeeds when it should first
2569 return -3;
2570 }
2571
2572 QCBOREncode_Init(&EC, Large);
2573 QCBOREncode_OpenArray(&EC);
2574 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2575 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2576 QCBOREncode_CloseArray(&EC);
2577 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2578 // Now just 1 byte over, see that it fails
2579 return -4;
2580 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002581
2582
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002583 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2584 QCBOREncode_Init(&EC, Large);
2585 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2586 QCBOREncode_OpenArray(&EC);
2587 }
2588 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2589 QCBOREncode_CloseArray(&EC);
2590 }
2591 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2592 // Making sure it succeeds when it should first
2593 return -5;
2594 }
2595
2596 QCBOREncode_Init(&EC, Large);
2597 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2598 QCBOREncode_OpenArray(&EC);
2599 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002600 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002601 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2602 QCBOREncode_CloseArray(&EC);
2603 }
2604 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002605 return -6;
2606 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002607
2608
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002609 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002610 QCBOREncode_Init(&EC, Large);
2611 for(int i = QCBOR_MAX_ARRAY_NESTING; 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 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002618 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2619#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2620 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002621 return -7;
2622 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002623#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2624 if(uErr != QCBOR_SUCCESS) {
2625 return -107;
2626 }
2627#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002628
2629
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002630 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002631 QCBOREncode_Init(&EC, Large);
2632 QCBOREncode_OpenArray(&EC);
2633 UsefulBufC Wrap;
2634 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002635 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2636#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2637 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002638 return -8;
2639 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002640#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2641 if(uErr != QCBOR_SUCCESS) {
2642 return -108;
2643 }
2644#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002645
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002646 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002647 QCBOREncode_Init(&EC, Large);
2648 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2649 QCBOREncode_OpenArray(&EC);
2650 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002651 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002652 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2653 QCBOREncode_CloseArray(&EC);
2654 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002655
2656 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2657#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2658 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002659 return -9;
2660 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002661#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2662 if(uErr != QCBOR_SUCCESS) {
2663 return -109;
2664 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002665#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002666
Laurence Lundblade241705e2018-12-30 18:56:14 -08002667 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2668 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002669
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002670
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002671 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002672 QCBOREncode_Init(&EC, Buffer);
2673 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2674 return -11;
2675 }
2676
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002677 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002678 QCBOREncode_Init(&EC, Large);
2679 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002680 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002681 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2682#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2683 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002684 return -12;
2685 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002686#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2687 if(uErr != QCBOR_SUCCESS) {
2688 return -112;
2689 }
2690#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2691
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002692
2693 QCBOREncode_Init(&EC, Large);
2694 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002695 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002696 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2697#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2698 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002699 return -13;
2700 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002701#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2702 if(uErr != QCBOR_SUCCESS) {
2703 return -113;
2704 }
2705#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002706
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002707
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002708 return 0;
2709}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002710
2711
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002712#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002713/*
2714 [
2715 4([-1, 3]),
2716 4([-20, 4759477275222530853136]),
2717 4([9223372036854775807, -4759477275222530853137]),
2718 5([300, 100]),
2719 5([-20, 4759477275222530853136]),
2720 5([-9223372036854775808, -4759477275222530853137])
2721 ]
2722 */
2723static const uint8_t spExpectedExponentAndMantissaArray[] = {
2724 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2725 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2726 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2727 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2728 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2729 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2730 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2731 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2732 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2733 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2734 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2735
2736
2737/*
2738 {
2739 "decimal fraction": 4([-1, 3]),
2740 300: 4([-1, 3]),
2741 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2742 400: 4([2147483647, 4759477275222530853136]),
2743 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2744 500: 4([9223372036854775807, -4759477275222530853137]),
2745 "big float": 5([300, 100]),
2746 600: 5([300, 100]),
2747 "big float bignum positive": 5([-20, 4759477275222530853136]),
2748 700: 5([-20, 4759477275222530853136]),
2749 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2750 800: 5([-9223372036854775808, -4759477275222530853137])
2751 }
2752 */
2753static const uint8_t spExpectedExponentAndMantissaMap[] = {
2754 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2755 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2756 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2757 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2758 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2759 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2760 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2761 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2762 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2763 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2764 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2765 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2766 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2767 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2768 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2769 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2770 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2771 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2772 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2773 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2774 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2775 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2776 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2777 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2778 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2779 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2780 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2781 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2782 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2783 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2784 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2785 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2786 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2787 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2788 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2789 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2790 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2791 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2792 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2793 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2794 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2795 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2796 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2797 0x09, 0x10
2798};
2799
2800
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002801int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002802{
2803 QCBOREncodeContext EC;
2804 UsefulBufC EncodedExponentAndMantissa;
2805
2806 // Constant for the big number used in all the tests.
2807 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2808 0x06, 0x07, 0x08, 0x09, 0x010};
2809 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2810
2811 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2812 QCBOREncode_OpenArray(&EC);
2813 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2814 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2815 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2816 QCBOREncode_AddBigFloat(&EC, 100, 300);
2817 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2818 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2819 QCBOREncode_CloseArray(&EC);
2820
2821 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2822 return -2;
2823 }
2824
2825 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2826 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2827 NULL);
2828 if(nReturn) {
2829 return nReturn;
2830 }
2831
2832 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2833 QCBOREncode_OpenMap(&EC);
2834
2835 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2836
2837 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2838
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002839 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002840 "decimal fraction bignum postive",
2841 BigNum,
2842 false,
2843 -200);
2844
2845 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2846 400,
2847 BigNum,
2848 false,
2849 INT32_MAX);
2850
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002851 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002852 "decimal fraction bignum negative",
2853 BigNum,
2854 true,
2855 INT64_MAX);
2856
2857 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2858 500,
2859 BigNum,
2860 true,
2861 INT64_MAX);
2862
2863 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2864
2865 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2866
2867 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2868 "big float bignum positive",
2869 BigNum,
2870 false,
2871 -20);
2872
2873 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2874 700,
2875 BigNum,
2876 false,
2877 -20);
2878
2879 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2880 "big float bignum negative",
2881 BigNum,
2882 true,
2883 INT64_MIN);
2884
2885 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2886 800,
2887 BigNum,
2888 true,
2889 INT64_MIN);
2890
2891 QCBOREncode_CloseMap(&EC);
2892
2893 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2894 return -3;
2895 }
2896
2897
2898 struct UBCompareDiagnostic Diag;
2899
2900 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2901 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2902 &Diag);
2903 if(nReturn) {
2904 return nReturn + 1000000; // +1000000 to distinguish from first test above
2905 }
2906
2907 return 0;
2908}
2909
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002910#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002911
2912
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002913int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002914{
2915 /* This test doesn't have to be extensive, because just about every
2916 * other test exercises QCBOREncode_EncodeHead().
2917 */
2918 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002919 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002920
2921 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2922 CBOR_MAJOR_TYPE_POSITIVE_INT,
2923 0,
2924 0);
2925
2926 static const uint8_t expectedZero[] = {0x00};
2927
2928 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2929 return -1;
2930 }
2931
2932 // ---- Encode a zero padded out to an 8 byte integer ----
2933 encoded = QCBOREncode_EncodeHead(RightSize,
2934 CBOR_MAJOR_TYPE_POSITIVE_INT,
2935 8, // uMinSize is 8 bytes
2936 0);
2937
2938 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2939 0x00, 0x00, 0x00, 0x00};
2940
2941 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2942 return -2;
2943 }
2944
2945
2946 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002947 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002948
2949 encoded = QCBOREncode_EncodeHead(TooSmall,
2950 CBOR_MAJOR_TYPE_POSITIVE_INT,
2951 0,
2952 0);
2953
2954 if(!UsefulBuf_IsNULLC(encoded)) {
2955 return -3;
2956 }
2957
2958 return 0;
2959}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002960
2961
2962static const uint8_t spExpectedForOpenBytes[] = {
2963 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2964 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2965 0x78
2966};
2967
2968static const uint8_t spExpectedForOpenBytes2[] = {
2969 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2970 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2971 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2972};
2973
2974int32_t
2975OpenCloseBytesTest(void)
2976{
2977 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2978 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2979 QCBOREncodeContext EC;
2980 UsefulBuf Place;
2981 UsefulBufC Encoded;
2982 QCBORError uErr;
2983
2984 /* Normal use case -- add a byte string that fits */
2985 QCBOREncode_Init(&EC, TestBuf);
2986 QCBOREncode_OpenBytes(&EC, &Place);
2987 if(Place.ptr != TestBuf.ptr ||
2988 Place.len != TestBuf.len) {
2989 return 1;
2990 }
2991 Place.len -= 4;
2992 UsefulBuf_Set(Place, 'x');
2993 QCBOREncode_CloseBytes(&EC, Place.len);
2994 QCBOREncode_Finish(&EC, &Encoded);
2995 if(UsefulBuf_Compare(Encoded,
2996 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2997 return 2;
2998 }
2999
Paul Liétar79789772022-07-26 20:33:18 +01003000 /* Run the same test but with a NULL buffer */
3001 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3002 QCBOREncode_OpenBytes(&EC, &Place);
3003 if(!UsefulBuf_IsNULL(Place)) {
3004 return 3;
3005 }
3006 Place.len -= 4;
3007 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3008 QCBOREncode_CloseBytes(&EC, Place.len);
3009 uErr = QCBOREncode_Finish(&EC, &Encoded);
3010 if(uErr != QCBOR_SUCCESS ||
3011 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3012 return 4;
3013 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003014
3015 /* Open a byte string with no room left */
3016 QCBOREncode_Init(&EC, TestBuf);
3017 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3018 QCBOREncode_OpenBytes(&EC, &Place);
3019 if(Place.ptr != NULL ||
3020 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003021 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003022 }
3023
3024 /* Try to extend byte string past end of encoding output buffer */
3025 QCBOREncode_Init(&EC, TestBuf);
3026 QCBOREncode_AddSZString(&EC, "012345678901234567");
3027 QCBOREncode_OpenBytes(&EC, &Place);
3028 /* Don't bother to write any bytes*/
3029 QCBOREncode_CloseBytes(&EC, Place.len+1);
3030 uErr = QCBOREncode_GetErrorState(&EC);
3031 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003032 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003033 }
3034
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003035 /* Close a byte string without opening one. */
3036 QCBOREncode_Init(&EC, TestBuf);
3037 QCBOREncode_AddSZString(&EC, "012345678");
3038 QCBOREncode_CloseBytes(&EC, 1);
3039 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003040#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003041 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003042 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003043 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003044#else
3045 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003046 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003047 }
3048#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003049
3050 /* Forget to close a byte string */
3051 QCBOREncode_Init(&EC, TestBuf);
3052 QCBOREncode_AddSZString(&EC, "012345678");
3053 QCBOREncode_OpenBytes(&EC, &Place);
3054 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003055#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003056 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003057 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003058 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003059#else
3060 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003061 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003062 }
3063#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003064
3065 /* Try to open a byte string in a byte string */
3066 QCBOREncode_Init(&EC, TestBuf);
3067 QCBOREncode_AddSZString(&EC, "012345678");
3068 QCBOREncode_OpenBytes(&EC, &Place);
3069 QCBOREncode_OpenBytes(&EC, &Place);
3070 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003071#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003072 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003073 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003074 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003075#else
3076 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003077 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003078 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003079#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3080
3081 /* A successful case with a little complexity */
3082 QCBOREncode_Init(&EC, TestBuf2);
3083 QCBOREncode_OpenMap(&EC);
3084 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3085 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3086 Place.len = 2;
3087 UsefulBuf_Set(Place, 'x');
3088 QCBOREncode_CloseBytes(&EC, 2);
3089 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3090 Place.len = 3;
3091 UsefulBuf_Set(Place, 'y');
3092 QCBOREncode_CloseBytes(&EC, 3);
3093 QCBOREncode_OpenArrayInMapN(&EC, 40);
3094 QCBOREncode_OpenBytes(&EC, &Place);
3095 QCBOREncode_CloseBytes(&EC, 0);
3096 QCBOREncode_CloseArray(&EC);
3097 QCBOREncode_CloseMap(&EC);
3098 uErr = QCBOREncode_Finish(&EC, &Encoded);
3099 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003100 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003101 }
3102 if(UsefulBuf_Compare(Encoded,
3103 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003104 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003105 }
3106
3107 return 0;
3108}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003109
3110
3111
3112int32_t
3113SortMapTest(void)
3114{
3115 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3116 QCBOREncodeContext EC;
3117 UsefulBufC EncodedAndSorted;
3118 QCBORError uErr;
3119 struct UBCompareDiagnostic CompareDiagnostics;
3120
3121
3122 /* --- Basic sort test case --- */
3123 QCBOREncode_Init(&EC, TestBuf);
3124 QCBOREncode_OpenMap(&EC);
3125 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3126 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3127 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3128 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3129 QCBOREncode_CloseAndSortMap(&EC);
3130 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3131 if(uErr) {
3132 return 11;
3133 }
3134
3135 static const uint8_t spBasic[] = {
3136 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3137
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003138 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003139 return 12;
3140 }
3141
3142 /* --- Empty map sort test case --- */
3143 QCBOREncode_Init(&EC, TestBuf);
3144 QCBOREncode_OpenMap(&EC);
3145 QCBOREncode_CloseAndSortMap(&EC);
3146 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3147 if(uErr) {
3148 return 21;
3149 }
3150
3151 static const uint8_t spEmpty[] = {0xA0};
3152 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3153 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3154 &CompareDiagnostics)) {
3155 return 22;
3156 }
3157
3158 /* --- Several levels of nested sorted maps --- */
3159 QCBOREncode_Init(&EC, TestBuf);
3160 QCBOREncode_OpenMap(&EC);
3161 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3162 QCBOREncode_OpenMapInMapN(&EC, 428);
3163 QCBOREncode_AddNULLToMap(&EC, "null");
3164 QCBOREncode_OpenArrayInMap(&EC, "array");
3165 QCBOREncode_AddSZString(&EC, "hi");
3166 QCBOREncode_AddSZString(&EC, "there");
3167 QCBOREncode_CloseArray(&EC);
3168 QCBOREncode_OpenMapInMap(&EC, "empty2");
3169 QCBOREncode_CloseAndSortMap(&EC);
3170 QCBOREncode_OpenMapInMap(&EC, "empty1");
3171 QCBOREncode_CloseAndSortMap(&EC);
3172 QCBOREncode_CloseAndSortMap(&EC);
3173 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3174 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3175 QCBOREncode_CloseAndSortMap(&EC);
3176 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3177 if(uErr) {
3178 return 31;
3179 }
3180 static const uint8_t spNested[] = {
3181 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3182 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3183 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3184 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3185 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3186 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3187 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3188 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3189
3190 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3191 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested),
3192 &CompareDiagnostics)) {
3193 return 32;
3194 }
3195
3196 /* --- Degenerate case of everything in order --- */
3197 QCBOREncode_Init(&EC, TestBuf);
3198 QCBOREncode_OpenMap(&EC);
3199 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3200 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3201 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3202 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3203 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3204 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3205 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3206 QCBOREncode_CloseAndSortMap(&EC);
3207 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3208 if(uErr) {
3209 return 41;
3210 }
3211
3212 static const uint8_t sp6Items[] = {
3213 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3214 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3215 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3216 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3217 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3218 &CompareDiagnostics)) {
3219 return 42;
3220 }
3221
3222 /* --- Degenerate case -- reverse order --- */
3223 QCBOREncode_Init(&EC, TestBuf);
3224 QCBOREncode_OpenMap(&EC);
3225 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3226 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3227 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3228 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3229 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3230 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3231 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3232 QCBOREncode_CloseAndSortMap(&EC);
3233 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3234 if(uErr) {
3235 return 51;
3236 }
3237
3238 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3239 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3240 &CompareDiagnostics)) {
3241 return 52;
3242 }
3243
3244 /* --- Same items, randomly out of order --- */
3245 QCBOREncode_Init(&EC, TestBuf);
3246 QCBOREncode_OpenMap(&EC);
3247 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3248 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3249 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3250 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3251 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3252 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3253 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3254 QCBOREncode_CloseAndSortMap(&EC);
3255 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3256 if(uErr) {
3257 return 61;
3258 }
3259
3260 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3261 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3262 &CompareDiagnostics)) {
3263 return 62;
3264 }
3265
3266 /* --- Stuff in front of and after array to sort --- */
3267 QCBOREncode_Init(&EC, TestBuf);
3268 QCBOREncode_OpenArray(&EC);
3269 QCBOREncode_AddInt64(&EC, 111);
3270 QCBOREncode_AddInt64(&EC, 222);
3271 QCBOREncode_OpenMap(&EC);
3272 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3273 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3274 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3275 QCBOREncode_CloseAndSortMap(&EC);
3276 QCBOREncode_AddInt64(&EC, 888);
3277 QCBOREncode_AddInt64(&EC, 999);
3278 QCBOREncode_CloseArray(&EC);
3279 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3280 if(uErr) {
3281 return 71;
3282 }
3283
3284 static const uint8_t spPreItems[] = {
3285 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3286 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3287 0x03, 0xE7};
3288 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3289 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3290 &CompareDiagnostics)) {
3291 return 72;
3292 }
3293
3294 /* --- map with labels of all CBOR major types and in reverse order --- */
3295 QCBOREncode_Init(&EC, TestBuf);
3296 QCBOREncode_OpenMap(&EC);
3297
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003298 /* Adding labels directly rather than AddToMap functions */
3299
3300#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003301 QCBOREncode_AddDouble(&EC, 8.77);
3302 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003303#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003304
3305 QCBOREncode_AddBool(&EC, true);
3306 QCBOREncode_AddInt64(&EC, 6);
3307
3308 QCBOREncode_AddDateEpoch(&EC, 88);
3309 QCBOREncode_AddInt64(&EC, 5);
3310
3311 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3312 QCBOREncode_AddInt64(&EC, 4);
3313
3314 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3315 QCBOREncode_AddInt64(&EC, 7);
3316
3317 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3318
3319 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3320 QCBOREncode_AddInt64(&EC, 2);
3321
3322 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3323 QCBOREncode_CloseAndSortMap(&EC);
3324
3325 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3326 if(uErr) {
3327 return 81;
3328 }
3329
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003330#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003331 static const uint8_t spLabelTypes[] = {
3332 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3333 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3334 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3335 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3336 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003337#else
3338 static const uint8_t spLabelTypes[] = {
3339 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3340 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3341 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3342#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3343
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003344 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3345 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3346 &CompareDiagnostics)) {
3347 return 82;
3348 }
3349
3350 /* --- labels are indefinitely encoded --- */
3351 QCBOREncode_Init(&EC, TestBuf);
3352 QCBOREncode_OpenMap(&EC);
3353
3354 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3355
3356 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3357
3358 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3359 QCBOREncode_AddInt64(&EC, 3);
3360
3361 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3362 QCBOREncode_AddInt64(&EC, 4);
3363
3364 QCBOREncode_CloseAndSortMap(&EC);
3365
3366 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3367 if(uErr) {
3368 return 91;
3369 }
3370
3371 static const uint8_t spIndefItems[] = {
3372 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3373 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3374 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3375 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3376 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3377 &CompareDiagnostics)) {
3378 return 92;
3379 }
3380
3381 /* --- Indefinitely encoded maps --- */
3382 QCBOREncode_Init(&EC, TestBuf);
3383 QCBOREncode_OpenMapIndefiniteLength(&EC);
3384
3385 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3386 QCBOREncode_CloseMapIndefiniteLength(&EC);
3387
3388 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3389 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3390
3391 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3392 QCBOREncode_CloseMapIndefiniteLength(&EC);
3393
3394 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3395 QCBOREncode_CloseMapIndefiniteLength(&EC);
3396
3397 QCBOREncode_CloseAndSortMapIndef(&EC);
3398 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3399 if(uErr) {
3400 return 101;
3401 }
3402
3403 static const uint8_t spIndeMaps[] = {
3404 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3405 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3406 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3407 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3409 &CompareDiagnostics)) {
3410 return 102;
3411 }
3412
3413 return 0;
3414}
Laurence Lundblade240ca822024-01-16 11:11:00 -07003415
3416
3417#include <math.h> /* For INFINITY and NAN and isnan() */
3418
3419
3420int32_t CDETest(void)
3421{
3422 QCBOREncodeContext EC;
3423 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003424 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003425
3426 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3427
3428 QCBOREncode_SerializationCDE(&EC);
3429
3430 /* Items added to test sorting and preferred encoding of numbers and floats */
3431 QCBOREncode_OpenMap(&EC);
3432 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3433 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3434 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3435 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3436 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3437 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3438
3439 QCBOREncode_CloseMap(&EC);
3440
3441 QCBOREncode_Finish(&EC, &Encoded);
3442
3443 static const uint8_t spExpectedCDE[] = {
3444 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3445 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3446 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3447 0x61, 0x78, 0xF9, 0x40, 0x00};
3448
3449 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3450 Encoded)) {
3451 return 1;
3452 }
3453
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003454
3455#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3456 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3457#else
3458 uExpectedErr = QCBOR_SUCCESS;
3459#endif
3460
3461
Laurence Lundblade240ca822024-01-16 11:11:00 -07003462 /* Next, make sure methods that encode non-CDE error out */
3463 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3464 QCBOREncode_SerializationCDE(&EC);
3465 QCBOREncode_OpenMapIndefiniteLength(&EC);
3466 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003467 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003468 return 100;
3469 }
3470
3471
3472 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3473 QCBOREncode_SerializationCDE(&EC);
3474 QCBOREncode_AddDoubleNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003475 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003476 return 101;
3477 }
3478
3479 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3480 QCBOREncode_SerializationCDE(&EC);
3481 QCBOREncode_AddFloatNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003482 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003483 return 101;
3484 }
3485
3486 return 0;
3487}
3488
3489
3490int32_t DCBORTest(void)
3491{
3492 QCBOREncodeContext EC;
3493 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003494 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003495
3496 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3497
3498 QCBOREncode_SerializationdCBOR(&EC);
3499
3500 /* Items added to test sorting and preferred encoding of numbers and floats */
3501 QCBOREncode_OpenMap(&EC);
3502 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3503 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3504 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3505 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3506 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3507
3508 QCBOREncode_CloseMap(&EC);
3509
3510 QCBOREncode_Finish(&EC, &Encoded);
3511
3512 static const uint8_t spExpecteddCBOR[] = {
3513 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3514 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3515 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3516
3517 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3518 Encoded)) {
3519 return 1;
3520 }
3521
3522
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003523#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3524 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3525#else
3526 uExpectedErr = QCBOR_SUCCESS;
3527#endif
3528
Laurence Lundblade240ca822024-01-16 11:11:00 -07003529 /* Next, make sure methods that encode non-CDE error out */
3530 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3531 QCBOREncode_SerializationdCBOR(&EC);
3532 QCBOREncode_OpenMapIndefiniteLength(&EC);
3533 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003534 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003535 return 100;
3536 }
3537
3538 /* Next, make sure methods that encode non-CDE error out */
3539 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3540 QCBOREncode_SerializationdCBOR(&EC);
3541 QCBOREncode_AddUndef(&EC);
3542 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003543 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003544 return 101;
3545 }
3546
3547
3548 return 0;
3549
3550}