blob: 744b011c33329d8c21ae95b43dad86958d663e70 [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 Lundbladee75f5962024-02-15 17:51:32 -0800695 UsefulBufC Enc;
696 size_t size;
697 int nReturn;
698 QCBORError uExpectedErr;
699
700 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530702 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800703 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2d493002024-02-01 11:09:17 -0700705 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706
Laurence Lundblade0595e932018-11-02 22:22:47 +0700707 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800708 nReturn = -1;
709 goto Done;
710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800711
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700712 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530713 nReturn = -2;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800714 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700715 }
716
717
718 /* Also test size calculation */
719 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800720 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700721
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800722 AddAll(&ECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700723
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700724 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
725 nReturn = -10;
726 goto Done;
727 }
728
729 if(size != sizeof(spExpectedEncodedAll)) {
730 nReturn = -11;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800731 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800733
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800734#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
735 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
736#else
737 uExpectedErr = QCBOR_SUCCESS;
738#endif
739
740 /* Test the QCBOR_ERR_NOT_ALLOWED error codes */
741 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
742 QCBOREncode_AddNegativeUInt64(&ECtx, 1);
743 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
744 nReturn = -21;
745 goto Done;
746 }
747
748 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
749 /* 0x7ff8000000000001ULL is a NaN with a payload. */
750 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
751 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
752 nReturn = -22;
753 goto Done;
754 }
755
756 /* 0x7ffc000000000000ULL is a NaN with a payload. */
757 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
758 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
759 nReturn = -23;
760 goto Done;
761 }
762
763 /* 0x7ff80001UL is a NaN with a payload. */
764 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
765 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
766 nReturn = -24;
767 goto Done;
768 }
769
770 /* 0x7ffc0000UL is a NaN with a payload. */
771 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
772 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
Laurence Lundbladee75f5962024-02-15 17:51:32 -0800773 nReturn = -25;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800774 goto Done;
775 }
776
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800777Done:
778 return nReturn;
779}
780
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700781
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530782/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200783 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530784 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
785 3B 0000000100000000 # negative(4294967296)
786 3A FFFFFFFF # negative(4294967295)
787 3A FFFFFFFE # negative(4294967294)
788 3A FFFFFFFD # negative(4294967293)
789 3A 7FFFFFFF # negative(2147483647)
790 3A 7FFFFFFE # negative(2147483646)
791 3A 00010001 # negative(65537)
792 3A 00010000 # negative(65536)
793 39 FFFF # negative(65535)
794 39 FFFE # negative(65534)
795 39 FFFD # negative(65533)
796 39 0100 # negative(256)
797 38 FF # negative(255)
798 38 FE # negative(254)
799 38 FD # negative(253)
800 38 18 # negative(24)
801 37 # negative(23)
802 36 # negative(22)
803 20 # negative(0)
804 00 # unsigned(0)
805 00 # unsigned(0)
806 01 # unsigned(1)
807 16 # unsigned(22)
808 17 # unsigned(23)
809 18 18 # unsigned(24)
810 18 19 # unsigned(25)
811 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200812 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530813 18 FE # unsigned(254)
814 18 FF # unsigned(255)
815 19 0100 # unsigned(256)
816 19 0101 # unsigned(257)
817 19 FFFE # unsigned(65534)
818 19 FFFF # unsigned(65535)
819 1A 00010000 # unsigned(65536)
820 1A 00010001 # unsigned(65537)
821 1A 00010002 # unsigned(65538)
822 1A 7FFFFFFF # unsigned(2147483647)
823 1A 7FFFFFFF # unsigned(2147483647)
824 1A 80000000 # unsigned(2147483648)
825 1A 80000001 # unsigned(2147483649)
826 1A FFFFFFFE # unsigned(4294967294)
827 1A FFFFFFFF # unsigned(4294967295)
828 1B 0000000100000000 # unsigned(4294967296)
829 1B 0000000100000001 # unsigned(4294967297)
830 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
831 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
832 */
833static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200834 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800835 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
836 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
837 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
838 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
839 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
840 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
841 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
842 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
843 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
844 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200845 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
846 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
847 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
848 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
849 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
850 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
851 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
852 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
853 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
854 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
855 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
856 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
857 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858
859/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800860
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800861 Test the generation of integers. This also ends up testing
862 encoding of all the different lengths. It encodes integers
863 of many lengths and values, especially around the boundaries
864 for different types of integers. It compares the output
865 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800866
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300868int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869{
870 QCBOREncodeContext ECtx;
871 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800872
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530873 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874 QCBOREncode_OpenArray(&ECtx);
875
876 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
877 QCBOREncode_AddInt64(&ECtx, -4294967297);
878 QCBOREncode_AddInt64(&ECtx, -4294967296);
879 QCBOREncode_AddInt64(&ECtx, -4294967295);
880 QCBOREncode_AddInt64(&ECtx, -4294967294);
881 QCBOREncode_AddInt64(&ECtx, -2147483648);
882 QCBOREncode_AddInt64(&ECtx, -2147483647);
883 QCBOREncode_AddInt64(&ECtx, -65538);
884 QCBOREncode_AddInt64(&ECtx, -65537);
885 QCBOREncode_AddInt64(&ECtx, -65536);
886 QCBOREncode_AddInt64(&ECtx, -65535);
887 QCBOREncode_AddInt64(&ECtx, -65534);
888 QCBOREncode_AddInt64(&ECtx, -257);
889 QCBOREncode_AddInt64(&ECtx, -256);
890 QCBOREncode_AddInt64(&ECtx, -255);
891 QCBOREncode_AddInt64(&ECtx, -254);
892 QCBOREncode_AddInt64(&ECtx, -25);
893 QCBOREncode_AddInt64(&ECtx, -24);
894 QCBOREncode_AddInt64(&ECtx, -23);
895 QCBOREncode_AddInt64(&ECtx, -1);
896 QCBOREncode_AddInt64(&ECtx, 0);
897 QCBOREncode_AddUInt64(&ECtx, 0ULL);
898 QCBOREncode_AddInt64(&ECtx, 1);
899 QCBOREncode_AddInt64(&ECtx, 22);
900 QCBOREncode_AddInt64(&ECtx, 23);
901 QCBOREncode_AddInt64(&ECtx, 24);
902 QCBOREncode_AddInt64(&ECtx, 25);
903 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200904 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800905 QCBOREncode_AddInt64(&ECtx, 254);
906 QCBOREncode_AddInt64(&ECtx, 255);
907 QCBOREncode_AddInt64(&ECtx, 256);
908 QCBOREncode_AddInt64(&ECtx, 257);
909 QCBOREncode_AddInt64(&ECtx, 65534);
910 QCBOREncode_AddInt64(&ECtx, 65535);
911 QCBOREncode_AddInt64(&ECtx, 65536);
912 QCBOREncode_AddInt64(&ECtx, 65537);
913 QCBOREncode_AddInt64(&ECtx, 65538);
914 QCBOREncode_AddInt64(&ECtx, 2147483647);
915 QCBOREncode_AddInt64(&ECtx, 2147483647);
916 QCBOREncode_AddInt64(&ECtx, 2147483648);
917 QCBOREncode_AddInt64(&ECtx, 2147483649);
918 QCBOREncode_AddInt64(&ECtx, 4294967294);
919 QCBOREncode_AddInt64(&ECtx, 4294967295);
920 QCBOREncode_AddInt64(&ECtx, 4294967296);
921 QCBOREncode_AddInt64(&ECtx, 4294967297);
922 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
923 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800924
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800926
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530927 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700928 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929 nReturn = -1;
930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800931
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530932 if(CheckResults(Enc, spExpectedEncodedInts))
933 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800935 return(nReturn);
936}
937
938
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530939/*
940 85 # array(5)
941 F5 # primitive(21)
942 F4 # primitive(20)
943 F6 # primitive(22)
944 F7 # primitive(23)
945 A1 # map(1)
946 65 # text(5)
947 554E446566 # "UNDef"
948 F7 # primitive(23)
949 */
950static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800951 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
952
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300953int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954{
955 QCBOREncodeContext ECtx;
956 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530958 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900960
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700961 QCBOREncode_AddBool(&ECtx, true);
962 QCBOREncode_AddBool(&ECtx, false);
963 QCBOREncode_AddNULL(&ECtx);
964 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 QCBOREncode_OpenMap(&ECtx);
967
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700968 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800969 QCBOREncode_CloseMap(&ECtx);
970
971 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800972
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530973 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700974 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 nReturn = -1;
976 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800977
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530978 if(CheckResults(ECBOR, spExpectedEncodedSimple))
979 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800980
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981 return(nReturn);
982}
983
Jan Jongboom47d86c52019-07-25 08:54:16 +0200984/*
985 9F # array(5)
986 F5 # primitive(21)
987 F4 # primitive(20)
988 F6 # primitive(22)
989 F7 # primitive(23)
990 BF # map(1)
991 65 # text(5)
992 554E446566 # "UNDef"
993 F7 # primitive(23)
994 FF # break
995 FF # break
996 */
997static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
998 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
999
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001000int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +02001001{
1002 QCBOREncodeContext ECtx;
1003 int nReturn = 0;
1004
1005 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1006 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
1007
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001008 QCBOREncode_AddBool(&ECtx, true);
1009 QCBOREncode_AddBool(&ECtx, false);
1010 QCBOREncode_AddNULL(&ECtx);
1011 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +02001012
1013 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
1014
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001015 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +02001016 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
1017
1018 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
1019
1020 UsefulBufC ECBOR;
1021 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1022 nReturn = -1;
1023 }
1024
1025 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
1026 return -2;
1027
1028 return(nReturn);
1029}
1030
Jan Jongboom5d827882019-08-07 12:51:15 +02001031/*
1032A5 # map(5)
1033 63 # text(3)
1034 617272 # "arr"
1035 98 1F # array(31)
1036 00 # unsigned(0)
1037 01 # unsigned(1)
1038 02 # unsigned(2)
1039 03 # unsigned(3)
1040 04 # unsigned(4)
1041 05 # unsigned(5)
1042 06 # unsigned(6)
1043 07 # unsigned(7)
1044 08 # unsigned(8)
1045 09 # unsigned(9)
1046 0A # unsigned(10)
1047 0B # unsigned(11)
1048 0C # unsigned(12)
1049 0D # unsigned(13)
1050 0E # unsigned(14)
1051 0F # unsigned(15)
1052 10 # unsigned(16)
1053 11 # unsigned(17)
1054 12 # unsigned(18)
1055 13 # unsigned(19)
1056 14 # unsigned(20)
1057 15 # unsigned(21)
1058 16 # unsigned(22)
1059 17 # unsigned(23)
1060 18 18 # unsigned(24)
1061 18 19 # unsigned(25)
1062 18 1A # unsigned(26)
1063 18 1B # unsigned(27)
1064 18 1C # unsigned(28)
1065 18 1D # unsigned(29)
1066 18 1E # unsigned(30)
1067 63 # text(3)
1068 6D6170 # "map"
1069 B8 1F # map(31)
1070 61 # text(1)
1071 61 # "a"
1072 00 # unsigned(0)
1073 61 # text(1)
1074 62 # "b"
1075 01 # unsigned(1)
1076 61 # text(1)
1077 63 # "c"
1078 02 # unsigned(2)
1079 61 # text(1)
1080 64 # "d"
1081 03 # unsigned(3)
1082 61 # text(1)
1083 65 # "e"
1084 04 # unsigned(4)
1085 61 # text(1)
1086 66 # "f"
1087 05 # unsigned(5)
1088 61 # text(1)
1089 67 # "g"
1090 06 # unsigned(6)
1091 61 # text(1)
1092 68 # "h"
1093 07 # unsigned(7)
1094 61 # text(1)
1095 69 # "i"
1096 08 # unsigned(8)
1097 61 # text(1)
1098 6A # "j"
1099 09 # unsigned(9)
1100 61 # text(1)
1101 6B # "k"
1102 0A # unsigned(10)
1103 61 # text(1)
1104 6C # "l"
1105 0B # unsigned(11)
1106 61 # text(1)
1107 6D # "m"
1108 0C # unsigned(12)
1109 61 # text(1)
1110 6E # "n"
1111 0D # unsigned(13)
1112 61 # text(1)
1113 6F # "o"
1114 0E # unsigned(14)
1115 61 # text(1)
1116 70 # "p"
1117 0F # unsigned(15)
1118 61 # text(1)
1119 71 # "q"
1120 10 # unsigned(16)
1121 61 # text(1)
1122 72 # "r"
1123 11 # unsigned(17)
1124 61 # text(1)
1125 73 # "s"
1126 12 # unsigned(18)
1127 61 # text(1)
1128 74 # "t"
1129 13 # unsigned(19)
1130 61 # text(1)
1131 75 # "u"
1132 14 # unsigned(20)
1133 61 # text(1)
1134 76 # "v"
1135 15 # unsigned(21)
1136 61 # text(1)
1137 77 # "w"
1138 16 # unsigned(22)
1139 61 # text(1)
1140 78 # "x"
1141 17 # unsigned(23)
1142 61 # text(1)
1143 79 # "y"
1144 18 18 # unsigned(24)
1145 61 # text(1)
1146 7A # "z"
1147 18 19 # unsigned(25)
1148 61 # text(1)
1149 41 # "A"
1150 18 1A # unsigned(26)
1151 61 # text(1)
1152 42 # "B"
1153 18 1B # unsigned(27)
1154 61 # text(1)
1155 43 # "C"
1156 18 1C # unsigned(28)
1157 61 # text(1)
1158 44 # "D"
1159 18 1D # unsigned(29)
1160 61 # text(1)
1161 45 # "E"
1162 18 1E # unsigned(30)
1163 65 # text(5)
1164 6D696E3331 # "min31"
1165 38 1E # negative(30)
1166 66 # text(6)
1167 706C75733331 # "plus31"
1168 18 1F # unsigned(31)
1169 63 # text(3)
1170 737472 # "str"
1171 78 1F # text(31)
1172 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1173 */
1174static const uint8_t EncodeLengthThirtyone[] = {
1175 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1176 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1177 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1178 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1179 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1180 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1181 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1182 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1183 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1184 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1185 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1186 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1187 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1188 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1189 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1190 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1191 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1192 0x31
1193};
1194
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001195int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001196{
1197 QCBOREncodeContext ECtx;
1198 int nReturn = 0;
1199
1200 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1201 QCBOREncode_OpenMap(&ECtx);
1202
1203 // add array with 31 items
1204 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1205 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001206 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001207 }
1208 QCBOREncode_CloseArray(&ECtx);
1209
1210 // add map with 31 items
1211 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001212 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001213 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001214 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001215 if (ix < 26) c = c + ix;
1216 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001217 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001218 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1219 }
1220 QCBOREncode_CloseMap(&ECtx);
1221
1222 // add -31 and +31
1223 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1224 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1225
1226 // add string with length 31
1227 const char *str = "testtesttesttesttesttestqcbor11";
1228 UsefulBufC str_b = { str, 31 };
1229 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1230
1231 QCBOREncode_CloseMap(&ECtx);
1232
1233 UsefulBufC ECBOR;
1234 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1235 nReturn = -1;
1236 }
1237
1238 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1239 return -2;
1240
1241 return(nReturn);
1242}
1243
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301244
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301245/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001246 * [ "2013-03-21T20:04:00Z",
1247 * 0("2013-03-21T20:04:00Z"),
1248 * 1363896240,
1249 * 1(1363896240),
1250 * 100(-10676),
1251 * 3994,
1252 * 1004("1940-10-09"),
1253 * "1980-12-08",
1254 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1255 * "SD": 1(999),
1256 * "Sample Date from RFC 8943": "1985-04-12",
1257 * 42: 1004("1985-04-12T23:20:50.52Z"),
1258 * "SY": 100(-10676),
1259 * 45: 3994
1260 * }
1261 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301262 */
1263static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001264 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1265 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1266 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1267 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1268 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1269 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1270 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1271 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1272 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1273 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1274 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1275 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1276 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1277 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1278 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1279 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1280 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1281 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1282 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1283 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1284 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1285 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1286 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001287
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001288int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001289{
1290 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001291
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301292 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001294 QCBOREncode_OpenArray(&ECtx);
1295
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001296 /* The values are taken from the CBOR RFCs */
1297 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1298 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1299 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1300 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1301 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1302 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1303 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1304 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306 QCBOREncode_OpenMap(&ECtx);
1307
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001308 QCBOREncode_AddDateStringToMap(&ECtx,
1309 "Sample Date from RFC 3339",
1310 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001312 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1313 "Sample Date from RFC 8943",
1314 QCBOR_ENCODE_AS_BORROWED,
1315 "1985-04-12");
1316 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1317 42,
1318 QCBOR_ENCODE_AS_TAG,
1319 "1985-04-12T23:20:50.52Z");
1320 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1321 "SY",
1322 QCBOR_ENCODE_AS_TAG,
1323 -10676);
1324 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1325 45,
1326 QCBOR_ENCODE_AS_BORROWED,
1327 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331 QCBOREncode_CloseArray(&ECtx);
1332
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301333 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001334 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001335 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001337
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301338 if(CheckResults(ECBOR, spExpectedEncodedDates))
1339 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001340
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001341 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342}
1343
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301344
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001345int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346{
1347 QCBOREncodeContext ECtx;
1348 int i;
1349 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001350
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301351 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1353 QCBOREncode_OpenArray(&ECtx);
1354 }
1355 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1356 QCBOREncode_CloseArray(&ECtx);
1357 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001358 UsefulBufC Encoded;
1359 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001360 nReturn = -1;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362
1363 return(nReturn);
1364}
1365
1366
1367
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001368int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369{
1370 QCBOREncodeContext ECtx;
1371 int i;
1372 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301374 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001375 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1376 QCBOREncode_OpenArray(&ECtx);
1377 }
1378 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1379 QCBOREncode_CloseArray(&ECtx);
1380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundblade0595e932018-11-02 22:22:47 +07001382 UsefulBufC Encoded;
1383 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001384 nReturn = -1;
1385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 return(nReturn);
1388}
1389
1390
1391
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001392int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001393{
1394 QCBOREncodeContext ECtx;
1395 int i;
1396 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001397
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301398 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1400 QCBOREncode_OpenArray(&ECtx);
1401 }
1402 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1403 QCBOREncode_CloseArray(&ECtx);
1404 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001405 UsefulBufC Encoded;
1406 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001407 nReturn = -1;
1408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001410 return(nReturn);
1411}
1412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001413
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301414/*
1415 81 # array(1)
1416 81 # array(1)
1417 81 # array(1)
1418 81 # array(1)
1419 80 # array(0)
1420*/
1421static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301424/*
1425 82 # array(2)
1426 81 # array(1)
1427 81 # array(1)
1428 81 # array(1)
1429 81 # array(1)
1430 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001431 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301432 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1433 3B 0000000100000000 # negative(4294967296)
1434 3A FFFFFFFF # negative(4294967295)
1435 3A FFFFFFFE # negative(4294967294)
1436 3A FFFFFFFD # negative(4294967293)
1437 3A 7FFFFFFF # negative(2147483647)
1438 3A 7FFFFFFE # negative(2147483646)
1439 3A 00010001 # negative(65537)
1440 3A 00010000 # negative(65536)
1441 39 FFFF # negative(65535)
1442 39 FFFE # negative(65534)
1443 39 FFFD # negative(65533)
1444 39 0100 # negative(256)
1445 38 FF # negative(255)
1446 38 FE # negative(254)
1447 38 FD # negative(253)
1448 38 18 # negative(24)
1449 37 # negative(23)
1450 36 # negative(22)
1451 20 # negative(0)
1452 00 # unsigned(0)
1453 00 # unsigned(0)
1454 01 # unsigned(1)
1455 16 # unsigned(22)
1456 17 # unsigned(23)
1457 18 18 # unsigned(24)
1458 18 19 # unsigned(25)
1459 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001460 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301461 18 FE # unsigned(254)
1462 18 FF # unsigned(255)
1463 19 0100 # unsigned(256)
1464 19 0101 # unsigned(257)
1465 19 FFFE # unsigned(65534)
1466 19 FFFF # unsigned(65535)
1467 1A 00010000 # unsigned(65536)
1468 1A 00010001 # unsigned(65537)
1469 1A 00010002 # unsigned(65538)
1470 1A 7FFFFFFF # unsigned(2147483647)
1471 1A 7FFFFFFF # unsigned(2147483647)
1472 1A 80000000 # unsigned(2147483648)
1473 1A 80000001 # unsigned(2147483649)
1474 1A FFFFFFFE # unsigned(4294967294)
1475 1A FFFFFFFF # unsigned(4294967295)
1476 1B 0000000100000000 # unsigned(4294967296)
1477 1B 0000000100000001 # unsigned(4294967297)
1478 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1479 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1480 */
1481static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001482 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1484 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1485 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1486 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1487 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1488 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1489 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1490 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1491 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1492 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1493 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001494 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1495 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1496 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1497 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1498 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1499 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1500 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1501 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1502 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1503 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1504 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1505 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1506 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507
1508
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001509int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001511 QCBOREncodeContext ECtx;
1512
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301513 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001514 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301515 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1516 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001517 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001519 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001520
Laurence Lundblade0595e932018-11-02 22:22:47 +07001521 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001522 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301525 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001526 return -5;
1527 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001528
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001529 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530}
1531
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301532/*
1533 This returns a pointer to spBigBuf
1534 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001535static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536{
1537 QCBOREncodeContext ECtx;
1538 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 *pEncoded = NULL;
1541 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301542 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001543
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001544 // loop runs CBOR encoding twice. First with no buffer to
1545 // calucate the length so buffer can be allocated correctly,
1546 // and last with the buffer to do the actual encoding
1547 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301548 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 QCBOREncode_OpenMap(&ECtx);
1550 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1551 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1552 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1553 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1554 QCBOREncode_CloseArray(&ECtx);
1555 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1556 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1557 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1558 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1559 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1560 QCBOREncode_CloseMap(&ECtx);
1561 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
Laurence Lundblade0595e932018-11-02 22:22:47 +07001563 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 goto Done;
1565 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301566 if(uFirstSizeEstimate != *pEncodedLen) {
1567 nReturn = 1;
1568 } else {
1569 nReturn = 0;
1570 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 goto Done;
1572 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301573 *pEncoded = spBigBuf;
1574 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 Done:
1579 return(nReturn);
1580}
1581
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301582/*
1583 A3 # map(3)
1584 6D # text(13)
1585 666972737420696E7465676572 # "first integer"
1586 18 2A # unsigned(42)
1587 77 # text(23)
1588 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1589 82 # array(2)
1590 67 # text(7)
1591 737472696E6731 # "string1"
1592 67 # text(7)
1593 737472696E6732 # "string2"
1594 6C # text(12)
1595 6D617020696E2061206D6170 # "map in a map"
1596 A4 # map(4)
1597 67 # text(7)
1598 62797465732031 # "bytes 1"
1599 44 # bytes(4)
1600 78787878 # "xxxx"
1601 67 # text(7)
1602 62797465732032 # "bytes 2"
1603 44 # bytes(4)
1604 79797979 # "yyyy"
1605 6B # text(11)
1606 616E6F7468657220696E74 # "another int"
1607 18 62 # unsigned(98)
1608 66 # text(6)
1609 746578742032 # "text 2"
1610 78 1E # text(30)
1611 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1612 */
1613static const uint8_t spValidMapEncoded[] = {
1614 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1615 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1616 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1617 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1618 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1619 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1620 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1621 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1622 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1623 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1624 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1625 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1626 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1627 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1628 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1629 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001630
1631
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001632int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001633{
1634 uint8_t *pEncodedMaps;
1635 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001637 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301638 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001641 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301642 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1643 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001645 return(nReturn);
1646}
1647
1648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001649/*
1650 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001651
Laurence Lundbladeee851742020-01-08 08:37:05 -08001652 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1653 CBOR_SIMPLEV_NULL
1654 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001655 @param[in] szAlexString Diagnostic code.
1656 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001657 @param[in/out] pnLen Size of pOut buffer when called; length of data
1658 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660 @return
1661 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001663 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 -08001664 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001665 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001667 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1668 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670 */
1671
Laurence Lundbladeee851742020-01-08 08:37:05 -08001672static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001673FormatRTICResults(uint8_t uRResult,
1674 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001675 const char *szType,
1676 const char *szAlexString,
1677 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678{
1679 // Buffer that the result will be written in to
1680 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001681 // QCBOREncode will never write off the end of this buffer. If it won't
1682 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001684 // Context for the encoder
1685 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301686 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001688 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1689 // Contents are label / value pairs
1690 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001692 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001693
Laurence Lundbladeee851742020-01-08 08:37:05 -08001694 // The result: 0 if scan happened and found nothing; 1 if it happened and
1695 // found something wrong; 2 if it didn't happen
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001696 QCBOREncode_Private_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001697
1698 // Add the diagnostic code
1699 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1700
1701 // Add a time stamp
1702 if(time) {
1703 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1704 }
1705
1706 // Add the diagnostic code
1707 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1708
1709 // Open a subordinate map for telemtry data
1710 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1711
1712 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001713
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001714 // Add a few fake integers and buffers for now.
1715 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1716
1717 // Add a few fake integers and buffers for now.
1718 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1719
1720 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301721 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001722 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001723
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001724 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1725 }
1726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001727
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001728 // Close the telemetry map
1729 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001730
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001731 // Close the map
1732 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001733
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301734 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001735
Laurence Lundblade0595e932018-11-02 22:22:47 +07001736 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001737
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301738 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001739}
1740
1741
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301742/*
1743 A5 # map(5)
1744 69 # text(9)
1745 696E74656772697479 # "integrity"
1746 F4 # primitive(20)
1747 64 # text(4)
1748 74797065 # "type"
1749 66 # text(6)
1750 726563656E74 # "recent"
1751 64 # text(4)
1752 74696D65 # "time"
1753 C1 # tag(1)
1754 1A 580D4172 # unsigned(1477263730)
1755 64 # text(4)
1756 64696167 # "diag"
1757 6A # text(10)
1758 30784131654335303031 # "0xA1eC5001"
1759 69 # text(9)
1760 74656C656D65747279 # "telemetry"
1761 A3 # map(3)
1762 69 # text(9)
1763 53686F652053697A65 # "Shoe Size"
1764 0C # unsigned(12)
1765 62 # text(2)
1766 4951 # "IQ"
1767 1A FFFFFFFF # unsigned(4294967295)
1768 77 # text(23)
1769 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1770 48 # bytes(8)
1771 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1772 */
1773static const uint8_t spExpectedRTIC[] = {
1774 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1775 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1776 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1777 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1778 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1779 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1780 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1781 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1782 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1783 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1784 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1785 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001786
1787
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001788int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001789{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001790 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301791 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301792 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301793 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001794 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301795 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001796
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301797 if(CheckResults(Encoded, spExpectedRTIC)) {
1798 return -2;
1799 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001800
1801 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001802}
1803
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301804
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301805/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001806 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001807
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301808 82 # array(2)
1809 19 01C3 # unsigned(451)
1810 43 # bytes(3)
1811 1901D2 # "\x19\x01\xD2"
1812*/
1813static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301814
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001815static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1816
Laurence Lundbladeda532272019-04-07 11:40:17 -07001817/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001818 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001819 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001820int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001821{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001822 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001823
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001824 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301825 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001826
Laurence Lundblade684aec22018-10-12 19:33:53 +08001827 QCBOREncode_OpenArray(&EC);
1828 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001829
Laurence Lundblade684aec22018-10-12 19:33:53 +08001830 QCBOREncode_BstrWrap(&EC);
1831 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001832
Laurence Lundblade684aec22018-10-12 19:33:53 +08001833 UsefulBufC Wrapped;
1834 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001835
Laurence Lundblade684aec22018-10-12 19:33:53 +08001836 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001837
Laurence Lundblade684aec22018-10-12 19:33:53 +08001838 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001839 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001840 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001841 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001842
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301843 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001844 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001845 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001846
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001847 // Second test - see if the length of the wrapped
1848 // bstr is correct. Also tests bstr wrapping
1849 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001850 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1851 QCBOREncode_OpenArray(&EC);
1852 QCBOREncode_BstrWrap(&EC);
1853 QCBOREncode_OpenArray(&EC);
1854 QCBOREncode_AddNULL(&EC);
1855 QCBOREncode_CloseArray(&EC);
1856 UsefulBufC BStr;
1857 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001858 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1859 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001860 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001861 return -5;
1862 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001863
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001864
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001865 // Fourth test, cancelling a byte string
1866 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1867
1868 QCBOREncode_OpenArray(&EC);
1869 QCBOREncode_AddUInt64(&EC, 451);
1870
1871 QCBOREncode_BstrWrap(&EC);
1872 QCBOREncode_CancelBstrWrap(&EC);
1873
1874
1875 QCBOREncode_AddUInt64(&EC, 42);
1876 QCBOREncode_CloseArray(&EC);
1877 if(QCBOREncode_Finish(&EC, &Encoded)) {
1878 return -8;
1879 }
1880 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1881 return -9;
1882 }
1883
1884 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001885 // Fifth test, failed cancelling
1886 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1887
1888 QCBOREncode_OpenArray(&EC);
1889 QCBOREncode_AddUInt64(&EC, 451);
1890
1891 QCBOREncode_BstrWrap(&EC);
1892 QCBOREncode_AddUInt64(&EC, 99);
1893 QCBOREncode_CancelBstrWrap(&EC);
1894
1895 QCBOREncode_AddUInt64(&EC, 42);
1896 QCBOREncode_CloseArray(&EC);
1897 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001898#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001899 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1900 return -10;
1901 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001902#else
1903 if(uErr != QCBOR_SUCCESS) {
1904 return -110;
1905 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001906#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1907
1908 // Sixth test, another cancel, but the error is not caught
1909 // This use will produce unintended CBOR. The error
1910 // is not caught because it would require tracking state
1911 // for QCBOREncode_BstrWrapInMapN.
1912 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1913
1914 QCBOREncode_OpenMap(&EC);
1915 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1916
1917 QCBOREncode_BstrWrapInMapN(&EC, 55);
1918 QCBOREncode_CancelBstrWrap(&EC);
1919
1920 QCBOREncode_CloseMap(&EC);
1921 uErr = QCBOREncode_Finish(&EC, &Encoded);
1922 if(uErr != QCBOR_SUCCESS) {
1923 return -11;
1924 }
1925
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001926 return 0;
1927}
1928
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001929
1930
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001931int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001932{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001933 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001934 UsefulBufC Wrapped;
1935 UsefulBufC Encoded2;
1936 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001937
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001938 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001939
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301940 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001941
Laurence Lundblade684aec22018-10-12 19:33:53 +08001942 QCBOREncode_OpenArray(&EC);
1943 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001944
Laurence Lundblade684aec22018-10-12 19:33:53 +08001945 QCBOREncode_BstrWrap(&EC);
1946 QCBOREncode_AddUInt64(&EC, 466);
1947 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001948
Laurence Lundblade684aec22018-10-12 19:33:53 +08001949 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade684aec22018-10-12 19:33:53 +08001951 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001952
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001953 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001954#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001955 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001956 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001957 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001958#else
1959 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1960 * is set and not to be sure to excerice all the relavant code in
1961 * both conditions. When the guards are disabled, there is no
1962 * error returned, but the code path is still covered.
1963 */
1964 if(uError != QCBOR_SUCCESS) {
1965 return (int32_t)(600 + uError);
1966 }
1967#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001968
Laurence Lundbladeee851742020-01-08 08:37:05 -08001969 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301970 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001971 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001972 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001973#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001974 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001975 return (int32_t)(700 + uError);
1976 }
1977#else
1978 if(uError != QCBOR_SUCCESS) {
1979 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001980 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001981#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001982
Laurence Lundblade684aec22018-10-12 19:33:53 +08001983 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301984 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001985 for(int i = 1; i < 18; i++) {
1986 QCBOREncode_BstrWrap(&EC);
1987 }
1988 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001989
Laurence Lundblade684aec22018-10-12 19:33:53 +08001990 for(int i = 1; i < 18; i++) {
1991 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001993
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001994 uError = QCBOREncode_Finish(&EC, &Encoded2);
1995 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001996 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001997 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001998
Laurence Lundblade684aec22018-10-12 19:33:53 +08001999 return 0;
2000}
2001
2002
Laurence Lundblade684aec22018-10-12 19:33:53 +08002003/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002004 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002006 [
2007 h'82004E82014B8202488203458204428105',
2008 {
2009 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
2010 8426568656C6C6F18234BA2131318436568656C6C6F'
2011 }
2012 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002013
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002014 Unwrapping the first byte string in the above gives
2015 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302016
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002017 Unwrapping again, the byte string immediately above gives
2018 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302019
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002020 ...
2021
2022 Unrapping the second byte string in the top-level CBOR above gives
2023 {16: 16,
2024 64: "hello",
2025 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2026 }
2027
2028 Unwrapping again, the byte string immediately above gives
2029 {17: 17,
2030 65: "hello",
2031 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2032 }
2033
2034 ...
2035
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302036 */
2037static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002038{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002039 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2040 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2041 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2042 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2043 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2044 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2045 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2046 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2047 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2048 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002049};
2050
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002051
2052/*
2053 Get an int64 out of the decoder or fail.
2054 */
2055static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002056{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002057 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002058 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002059
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002060 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002061 if(nReturn) {
2062 return nReturn;
2063 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002064 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002065 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002067
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002068 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002069 return 0;
2070}
2071
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002072/*
2073 Get an array out of the decoder or fail.
2074 */
2075static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002076{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002077 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002078 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002079
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002080 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002081 if(nReturn) {
2082 return nReturn;
2083 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002084 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2085 return -1;
2086 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002087
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002088 *pInt = Item.val.uCount;
2089 return 0;
2090}
2091
2092/*
2093 Get a map out of the decoder or fail.
2094 */
2095static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2096{
2097 QCBORItem Item;
2098 int32_t nReturn;
2099
2100 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002101 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002102 return nReturn;
2103 }
2104 if(Item.uDataType != QCBOR_TYPE_MAP) {
2105 return -1;
2106 }
2107
2108 *pInt = Item.val.uCount;
2109 return 0;
2110}
2111
2112/*
2113 Get a byte string out of the decoder or fail.
2114 */
2115static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2116{
2117 QCBORItem Item;
2118 int32_t nReturn;
2119
2120 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2121 if(nReturn) {
2122 return nReturn;
2123 }
2124 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002125 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002126 }
2127
2128 *pBstr = Item.val.string;
2129 return 0;
2130}
2131
2132/*
2133 Get a byte string out of the decoder or fail.
2134 */
2135static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2136{
2137 QCBORItem Item;
2138 int nReturn;
2139
2140 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2141 if(nReturn) {
2142 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002143 }
2144 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002145 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002146 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002147
2148 *pTstr = Item.val.string;
2149 return 0;
2150}
2151
2152
2153/*
2154 Recursively decode array containing a little CBOR and a bstr wrapped array
2155 with a little CBOR and a bstr wrapped array...
2156
2157 Part of bstr_wrap_nest_test.
2158 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2159{
2160 int64_t nInt;
2161 UsefulBufC Bstr;
2162 uint16_t nArrayCount;
2163 QCBORDecodeContext DC;
2164 int32_t nResult;
2165
2166 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2167
2168 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2169 return -10;
2170 }
2171
2172 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002173 return -11;
2174 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002175
2176 nResult = GetByteString(&DC, &Bstr);
2177 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2178 if(nArrayCount != 1) {
2179 return -12;
2180 } else {
2181 // successful exit
2182 return 0;
2183 }
2184 }
2185 if(nResult) {
2186 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002188
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002189 // tail recursion; good compilers will reuse the stack frame
2190 return DecodeNextNested(Bstr);
2191}
2192
2193
2194/*
2195 Recursively decode map containing a little CBOR and a bstr wrapped map
2196 with a little CBOR and a bstr wrapped map...
2197
2198 Part of bstr_wrap_nest_test.
2199 */
2200static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2201{
2202 int32_t nResult;
2203 uint16_t nMapCount;
2204 int64_t nInt;
2205 UsefulBufC Bstr;
2206 QCBORDecodeContext DC;
2207
2208 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2209
2210 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2211 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002213
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002214 if(GetInt64(&DC, &nInt)) {
2215 return -21;
2216 }
2217
2218 // The "hello"
2219 if(GetTextString(&DC, &Bstr)) {
2220 return -22;
2221 }
2222
2223 nResult = GetByteString(&DC, &Bstr);
2224 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2225 if(nMapCount == 2) {
2226 // successful exit
2227 return 0;
2228 } else {
2229 return -23;
2230 }
2231 }
2232
2233 if(nResult) {
2234 return -24;
2235 }
2236
2237 // tail recursion; good compilers will reuse the stack frame
2238 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002239}
2240
2241
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002242int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002243{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002244 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302245 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundblade684aec22018-10-12 19:33:53 +08002247 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002248 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002250 QCBOREncode_OpenArray(&EC);
2251
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002252 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002253 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002254 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002255 QCBOREncode_AddInt64(&EC, i);
2256 }
2257 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2258 QCBOREncode_CloseArray(&EC);
2259 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002261
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002262 QCBOREncode_OpenMap(&EC);
2263 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2264 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2265 QCBOREncode_OpenMap(&EC);
2266 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002267 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002268 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002269
2270 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2271 QCBOREncode_CloseMap(&EC);
2272 QCBOREncode_CloseBstrWrap(&EC, NULL);
2273 }
2274 QCBOREncode_CloseMap(&EC);
2275
Laurence Lundblade684aec22018-10-12 19:33:53 +08002276 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002277
Laurence Lundblade684aec22018-10-12 19:33:53 +08002278 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002279 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002280 return -1;
2281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade684aec22018-10-12 19:33:53 +08002283 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302284 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002285 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundblade684aec22018-10-12 19:33:53 +08002288 // ---- Decode it and see if it is OK ------
2289 QCBORDecodeContext DC;
2290 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002292 UsefulBufC Bstr;
2293 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002294
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002295 // Array surrounding the the whole thing
2296 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002297 return -3;
2298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002300 // Get the byte string wrapping some array stuff
2301 if(GetByteString(&DC, &Bstr)) {
2302 return -4;
2303 }
2304
2305 // Decode the wrapped nested structure
2306 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002307 if(nReturn) {
2308 return nReturn;
2309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002311 // A map enclosing some map-oriented bstr wraps
2312 if(GetMap(&DC, &nArrayCount)) {
2313 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002316 // Get the byte string wrapping some array stuff
2317 if(GetByteString(&DC, &Bstr)) {
2318 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002320
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002321 // Decode the wrapped nested structure
2322 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002323 if(nReturn) {
2324 return nReturn;
2325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundblade684aec22018-10-12 19:33:53 +08002327 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002328 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002329 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002330
Laurence Lundblade684aec22018-10-12 19:33:53 +08002331 return 0;
2332}
2333
2334
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002335static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302336 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2337 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2338 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2339 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2340 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2341 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2342 0x45, 0xca, 0xcb, 0x36};
2343
2344/*
2345 D2 # tag(18)
2346 84 # array(4)
2347 43 # bytes(3)
2348 A10126 # "\xA1\x01&"
2349 A1 # map(1)
2350 04 # unsigned(4)
2351 42 # bytes(2)
2352 3131 # "11"
2353 54 # bytes(20)
2354 546869732069732074686520636F6E74656E742E # "This is the content."
2355 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002356 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2357 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2358 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2359 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2360 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2361 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2362 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2363 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302364 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002365static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302366 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2367 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2368 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2369 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2370 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2371 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2372 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2373 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2374 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2375 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2376
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002377static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2378
2379
Laurence Lundblade684aec22018-10-12 19:33:53 +08002380/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002381 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002382 C.2.1. This doesn't actually verify the signature (however
2383 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002384 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002385int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002386{
2387 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002388 const char *szKid = "11";
2389 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2390 const char *szPayload = "This is the content.";
2391 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2392 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2393 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundblade684aec22018-10-12 19:33:53 +08002395 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002396
2397 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302398 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002399
Laurence Lundblade684aec22018-10-12 19:33:53 +08002400 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002401 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002402 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002403
Laurence Lundblade684aec22018-10-12 19:33:53 +08002404 // Add protected headers
2405 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002406
Laurence Lundblade684aec22018-10-12 19:33:53 +08002407 // Empty map with unprotected headers
2408 QCBOREncode_OpenMap(&EC);
2409 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2410 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002411
Laurence Lundblade684aec22018-10-12 19:33:53 +08002412 // The payload
2413 UsefulBufC WrappedPayload;
2414 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002415 // Payload is not actually CBOR in example C.2.1 like it would be
2416 // for a CWT or EAT. It is just a text string.
2417 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002418 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002419
Laurence Lundblade684aec22018-10-12 19:33:53 +08002420 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002421 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2422 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002423 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002424 return -1;
2425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002427/* if(UsefulBuf_Compare(WrappedPayload,
2428 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2429 return -1;
2430 } */
2431
Laurence Lundblade684aec22018-10-12 19:33:53 +08002432 // The signature
2433 QCBOREncode_AddBytes(&EC, Signature);
2434 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002435
Laurence Lundblade684aec22018-10-12 19:33:53 +08002436 // Finish and check the results
2437 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002438 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002439 return -2;
2440 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002441
Laurence Lundblade684aec22018-10-12 19:33:53 +08002442 // 98 is the size from RFC 8152 C.2.1
2443 if(COSE_Sign1.len != 98) {
2444 return -3;
2445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002447 // It would be good to compare this to the output from a COSE
2448 // implementation like COSE-C. This has been checked against the
2449 // CBOR playground.
2450 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002451 return -4;
2452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002454
2455 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2456 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2457
2458 // top level array for cose sign1, 18 is the tag for COSE sign
2459 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2460 QCBOREncode_OpenArray(&EC);
2461
2462 // Add protected headers
2463 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2464
2465 // Empty map with unprotected headers
2466 QCBOREncode_OpenMap(&EC);
2467 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2468 QCBOREncode_CloseMap(&EC);
2469
2470 // The payload
2471 QCBOREncode_BstrWrap(&EC);
2472 // Payload is not actually CBOR in example C.2.1 like it would be
2473 // for a CWT or EAT. It is just a text string.
2474 QCBOREncode_AddEncoded(&EC, Payload);
2475 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2476
2477 // Check we got back the actual payload expected
2478 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2479 if(UsefulBuf_Compare(WrappedPayload,
2480 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2481 return -11;
2482 }
2483
2484 // The signature
2485 QCBOREncode_AddBytes(&EC, Signature);
2486 QCBOREncode_CloseArray(&EC);
2487
2488 // Finish and check the results
2489 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2490 return -12;
2491 }
2492
2493 // 98 is the size from RFC 8152 C.2.1
2494 if(COSE_Sign1.len != 98) {
2495 return -13;
2496 }
2497
2498 // It would be good to compare this to the output from a COSE
2499 // implementation like COSE-C. This has been checked against the
2500 // CBOR playground.
2501 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2502 return -14;
2503 }
2504
Laurence Lundblade684aec22018-10-12 19:33:53 +08002505 return 0;
2506}
2507
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002508
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002509int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002510{
2511 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002512 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002513
2514
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002515 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002516 // Do all of these tests with NULL buffers so no actual
2517 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002518 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002519
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002520 // First verify no error from a big buffer
2521 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002522 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002523 // 6 is the CBOR overhead for opening the array and encodng the length
2524 // This exactly fills the buffer.
2525 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002526 QCBOREncode_CloseArray(&EC);
2527 size_t xx;
2528 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2529 return -1;
2530 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002531
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002532 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002533 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002534 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002535 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002536 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002537 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002538 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2539 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002540 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002541 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002542 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002543 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2544 return -2;
2545 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002546
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002547 // Third, fit an array in exactly at max position allowed
2548 QCBOREncode_Init(&EC, Buffer);
2549 QCBOREncode_OpenArray(&EC);
2550 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2551 QCBOREncode_OpenArray(&EC);
2552 QCBOREncode_CloseArray(&EC);
2553 QCBOREncode_CloseArray(&EC);
2554 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2555 return -10;
2556 }
2557
2558
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002559 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2560 // Work close to the 4GB size limit for a better test
2561 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002562 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002563
2564 QCBOREncode_Init(&EC, Large);
2565 QCBOREncode_OpenArray(&EC);
2566 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2567 QCBOREncode_CloseArray(&EC);
2568 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2569 // Making sure it succeeds when it should first
2570 return -3;
2571 }
2572
2573 QCBOREncode_Init(&EC, Large);
2574 QCBOREncode_OpenArray(&EC);
2575 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2576 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2577 QCBOREncode_CloseArray(&EC);
2578 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2579 // Now just 1 byte over, see that it fails
2580 return -4;
2581 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002582
2583
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002584 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2585 QCBOREncode_Init(&EC, Large);
2586 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2587 QCBOREncode_OpenArray(&EC);
2588 }
2589 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2590 QCBOREncode_CloseArray(&EC);
2591 }
2592 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2593 // Making sure it succeeds when it should first
2594 return -5;
2595 }
2596
2597 QCBOREncode_Init(&EC, Large);
2598 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2599 QCBOREncode_OpenArray(&EC);
2600 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002601 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002602 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2603 QCBOREncode_CloseArray(&EC);
2604 }
2605 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002606 return -6;
2607 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002608
2609
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002610 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002611 QCBOREncode_Init(&EC, Large);
2612 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2613 QCBOREncode_OpenArray(&EC);
2614 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002615 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002616 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2617 QCBOREncode_CloseArray(&EC);
2618 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002619 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2620#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2621 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002622 return -7;
2623 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002624#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2625 if(uErr != QCBOR_SUCCESS) {
2626 return -107;
2627 }
2628#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002629
2630
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002631 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002632 QCBOREncode_Init(&EC, Large);
2633 QCBOREncode_OpenArray(&EC);
2634 UsefulBufC Wrap;
2635 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002636 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2637#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2638 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002639 return -8;
2640 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002641#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2642 if(uErr != QCBOR_SUCCESS) {
2643 return -108;
2644 }
2645#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002646
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002647 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002648 QCBOREncode_Init(&EC, Large);
2649 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2650 QCBOREncode_OpenArray(&EC);
2651 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002652 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002653 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2654 QCBOREncode_CloseArray(&EC);
2655 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002656
2657 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2658#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2659 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002660 return -9;
2661 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002662#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2663 if(uErr != QCBOR_SUCCESS) {
2664 return -109;
2665 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002666#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002667
Laurence Lundblade241705e2018-12-30 18:56:14 -08002668 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2669 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002670
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002671
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002672 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002673 QCBOREncode_Init(&EC, Buffer);
2674 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2675 return -11;
2676 }
2677
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002678 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002679 QCBOREncode_Init(&EC, Large);
2680 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002681 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002682 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2683#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2684 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002685 return -12;
2686 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002687#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2688 if(uErr != QCBOR_SUCCESS) {
2689 return -112;
2690 }
2691#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2692
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002693
2694 QCBOREncode_Init(&EC, Large);
2695 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002696 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002697 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2698#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2699 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002700 return -13;
2701 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002702#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2703 if(uErr != QCBOR_SUCCESS) {
2704 return -113;
2705 }
2706#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002707
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002708
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002709 return 0;
2710}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002711
2712
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002713#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002714/*
2715 [
2716 4([-1, 3]),
2717 4([-20, 4759477275222530853136]),
2718 4([9223372036854775807, -4759477275222530853137]),
2719 5([300, 100]),
2720 5([-20, 4759477275222530853136]),
2721 5([-9223372036854775808, -4759477275222530853137])
2722 ]
2723 */
2724static const uint8_t spExpectedExponentAndMantissaArray[] = {
2725 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2726 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2727 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2728 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2729 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2730 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2731 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2732 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2733 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2734 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2735 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2736
2737
2738/*
2739 {
2740 "decimal fraction": 4([-1, 3]),
2741 300: 4([-1, 3]),
2742 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2743 400: 4([2147483647, 4759477275222530853136]),
2744 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2745 500: 4([9223372036854775807, -4759477275222530853137]),
2746 "big float": 5([300, 100]),
2747 600: 5([300, 100]),
2748 "big float bignum positive": 5([-20, 4759477275222530853136]),
2749 700: 5([-20, 4759477275222530853136]),
2750 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2751 800: 5([-9223372036854775808, -4759477275222530853137])
2752 }
2753 */
2754static const uint8_t spExpectedExponentAndMantissaMap[] = {
2755 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2756 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2757 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2758 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2759 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2760 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2761 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2762 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2763 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2764 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2765 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2766 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2767 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2768 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2769 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2770 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2771 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2772 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2773 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2774 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2775 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2776 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2777 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2778 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2779 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2780 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2781 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2782 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2783 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2784 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2785 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2786 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2787 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2788 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2789 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2790 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2791 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2792 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2793 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2794 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2795 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2796 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2797 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2798 0x09, 0x10
2799};
2800
2801
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002802int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002803{
2804 QCBOREncodeContext EC;
2805 UsefulBufC EncodedExponentAndMantissa;
2806
2807 // Constant for the big number used in all the tests.
2808 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2809 0x06, 0x07, 0x08, 0x09, 0x010};
2810 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2811
2812 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2813 QCBOREncode_OpenArray(&EC);
2814 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2815 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2816 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2817 QCBOREncode_AddBigFloat(&EC, 100, 300);
2818 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2819 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2820 QCBOREncode_CloseArray(&EC);
2821
2822 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2823 return -2;
2824 }
2825
2826 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2827 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2828 NULL);
2829 if(nReturn) {
2830 return nReturn;
2831 }
2832
2833 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2834 QCBOREncode_OpenMap(&EC);
2835
2836 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2837
2838 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2839
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002840 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002841 "decimal fraction bignum postive",
2842 BigNum,
2843 false,
2844 -200);
2845
2846 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2847 400,
2848 BigNum,
2849 false,
2850 INT32_MAX);
2851
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002852 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002853 "decimal fraction bignum negative",
2854 BigNum,
2855 true,
2856 INT64_MAX);
2857
2858 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2859 500,
2860 BigNum,
2861 true,
2862 INT64_MAX);
2863
2864 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2865
2866 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2867
2868 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2869 "big float bignum positive",
2870 BigNum,
2871 false,
2872 -20);
2873
2874 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2875 700,
2876 BigNum,
2877 false,
2878 -20);
2879
2880 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2881 "big float bignum negative",
2882 BigNum,
2883 true,
2884 INT64_MIN);
2885
2886 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2887 800,
2888 BigNum,
2889 true,
2890 INT64_MIN);
2891
2892 QCBOREncode_CloseMap(&EC);
2893
2894 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2895 return -3;
2896 }
2897
2898
2899 struct UBCompareDiagnostic Diag;
2900
2901 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2902 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2903 &Diag);
2904 if(nReturn) {
2905 return nReturn + 1000000; // +1000000 to distinguish from first test above
2906 }
2907
2908 return 0;
2909}
2910
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002911#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002912
2913
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002914int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002915{
2916 /* This test doesn't have to be extensive, because just about every
2917 * other test exercises QCBOREncode_EncodeHead().
2918 */
2919 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002920 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002921
2922 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2923 CBOR_MAJOR_TYPE_POSITIVE_INT,
2924 0,
2925 0);
2926
2927 static const uint8_t expectedZero[] = {0x00};
2928
2929 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2930 return -1;
2931 }
2932
2933 // ---- Encode a zero padded out to an 8 byte integer ----
2934 encoded = QCBOREncode_EncodeHead(RightSize,
2935 CBOR_MAJOR_TYPE_POSITIVE_INT,
2936 8, // uMinSize is 8 bytes
2937 0);
2938
2939 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2940 0x00, 0x00, 0x00, 0x00};
2941
2942 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2943 return -2;
2944 }
2945
2946
2947 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002948 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002949
2950 encoded = QCBOREncode_EncodeHead(TooSmall,
2951 CBOR_MAJOR_TYPE_POSITIVE_INT,
2952 0,
2953 0);
2954
2955 if(!UsefulBuf_IsNULLC(encoded)) {
2956 return -3;
2957 }
2958
2959 return 0;
2960}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002961
2962
2963static const uint8_t spExpectedForOpenBytes[] = {
2964 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2965 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2966 0x78
2967};
2968
2969static const uint8_t spExpectedForOpenBytes2[] = {
2970 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2971 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2972 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2973};
2974
2975int32_t
2976OpenCloseBytesTest(void)
2977{
2978 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2979 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2980 QCBOREncodeContext EC;
2981 UsefulBuf Place;
2982 UsefulBufC Encoded;
2983 QCBORError uErr;
2984
2985 /* Normal use case -- add a byte string that fits */
2986 QCBOREncode_Init(&EC, TestBuf);
2987 QCBOREncode_OpenBytes(&EC, &Place);
2988 if(Place.ptr != TestBuf.ptr ||
2989 Place.len != TestBuf.len) {
2990 return 1;
2991 }
2992 Place.len -= 4;
2993 UsefulBuf_Set(Place, 'x');
2994 QCBOREncode_CloseBytes(&EC, Place.len);
2995 QCBOREncode_Finish(&EC, &Encoded);
2996 if(UsefulBuf_Compare(Encoded,
2997 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2998 return 2;
2999 }
3000
Paul Liétar79789772022-07-26 20:33:18 +01003001 /* Run the same test but with a NULL buffer */
3002 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3003 QCBOREncode_OpenBytes(&EC, &Place);
3004 if(!UsefulBuf_IsNULL(Place)) {
3005 return 3;
3006 }
3007 Place.len -= 4;
3008 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3009 QCBOREncode_CloseBytes(&EC, Place.len);
3010 uErr = QCBOREncode_Finish(&EC, &Encoded);
3011 if(uErr != QCBOR_SUCCESS ||
3012 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3013 return 4;
3014 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003015
3016 /* Open a byte string with no room left */
3017 QCBOREncode_Init(&EC, TestBuf);
3018 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3019 QCBOREncode_OpenBytes(&EC, &Place);
3020 if(Place.ptr != NULL ||
3021 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003022 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003023 }
3024
3025 /* Try to extend byte string past end of encoding output buffer */
3026 QCBOREncode_Init(&EC, TestBuf);
3027 QCBOREncode_AddSZString(&EC, "012345678901234567");
3028 QCBOREncode_OpenBytes(&EC, &Place);
3029 /* Don't bother to write any bytes*/
3030 QCBOREncode_CloseBytes(&EC, Place.len+1);
3031 uErr = QCBOREncode_GetErrorState(&EC);
3032 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003033 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003034 }
3035
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003036 /* Close a byte string without opening one. */
3037 QCBOREncode_Init(&EC, TestBuf);
3038 QCBOREncode_AddSZString(&EC, "012345678");
3039 QCBOREncode_CloseBytes(&EC, 1);
3040 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003041#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003042 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003043 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003044 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003045#else
3046 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003047 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003048 }
3049#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003050
3051 /* Forget to close a byte string */
3052 QCBOREncode_Init(&EC, TestBuf);
3053 QCBOREncode_AddSZString(&EC, "012345678");
3054 QCBOREncode_OpenBytes(&EC, &Place);
3055 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003056#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003057 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003058 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003059 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003060#else
3061 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003062 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003063 }
3064#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003065
3066 /* Try to open a byte string in a byte string */
3067 QCBOREncode_Init(&EC, TestBuf);
3068 QCBOREncode_AddSZString(&EC, "012345678");
3069 QCBOREncode_OpenBytes(&EC, &Place);
3070 QCBOREncode_OpenBytes(&EC, &Place);
3071 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003072#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003073 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003074 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003075 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003076#else
3077 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003078 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003079 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003080#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3081
3082 /* A successful case with a little complexity */
3083 QCBOREncode_Init(&EC, TestBuf2);
3084 QCBOREncode_OpenMap(&EC);
3085 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3086 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3087 Place.len = 2;
3088 UsefulBuf_Set(Place, 'x');
3089 QCBOREncode_CloseBytes(&EC, 2);
3090 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3091 Place.len = 3;
3092 UsefulBuf_Set(Place, 'y');
3093 QCBOREncode_CloseBytes(&EC, 3);
3094 QCBOREncode_OpenArrayInMapN(&EC, 40);
3095 QCBOREncode_OpenBytes(&EC, &Place);
3096 QCBOREncode_CloseBytes(&EC, 0);
3097 QCBOREncode_CloseArray(&EC);
3098 QCBOREncode_CloseMap(&EC);
3099 uErr = QCBOREncode_Finish(&EC, &Encoded);
3100 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003101 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003102 }
3103 if(UsefulBuf_Compare(Encoded,
3104 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003105 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003106 }
3107
3108 return 0;
3109}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003110
3111
3112
3113int32_t
3114SortMapTest(void)
3115{
3116 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3117 QCBOREncodeContext EC;
3118 UsefulBufC EncodedAndSorted;
3119 QCBORError uErr;
3120 struct UBCompareDiagnostic CompareDiagnostics;
3121
3122
3123 /* --- Basic sort test case --- */
3124 QCBOREncode_Init(&EC, TestBuf);
3125 QCBOREncode_OpenMap(&EC);
3126 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3127 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3128 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3129 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3130 QCBOREncode_CloseAndSortMap(&EC);
3131 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3132 if(uErr) {
3133 return 11;
3134 }
3135
3136 static const uint8_t spBasic[] = {
3137 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3138
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003139 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003140 return 12;
3141 }
3142
3143 /* --- Empty map sort test case --- */
3144 QCBOREncode_Init(&EC, TestBuf);
3145 QCBOREncode_OpenMap(&EC);
3146 QCBOREncode_CloseAndSortMap(&EC);
3147 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3148 if(uErr) {
3149 return 21;
3150 }
3151
3152 static const uint8_t spEmpty[] = {0xA0};
3153 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3154 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3155 &CompareDiagnostics)) {
3156 return 22;
3157 }
3158
3159 /* --- Several levels of nested sorted maps --- */
3160 QCBOREncode_Init(&EC, TestBuf);
3161 QCBOREncode_OpenMap(&EC);
3162 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3163 QCBOREncode_OpenMapInMapN(&EC, 428);
3164 QCBOREncode_AddNULLToMap(&EC, "null");
3165 QCBOREncode_OpenArrayInMap(&EC, "array");
3166 QCBOREncode_AddSZString(&EC, "hi");
3167 QCBOREncode_AddSZString(&EC, "there");
3168 QCBOREncode_CloseArray(&EC);
3169 QCBOREncode_OpenMapInMap(&EC, "empty2");
3170 QCBOREncode_CloseAndSortMap(&EC);
3171 QCBOREncode_OpenMapInMap(&EC, "empty1");
3172 QCBOREncode_CloseAndSortMap(&EC);
3173 QCBOREncode_CloseAndSortMap(&EC);
3174 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3175 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3176 QCBOREncode_CloseAndSortMap(&EC);
3177 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3178 if(uErr) {
3179 return 31;
3180 }
3181 static const uint8_t spNested[] = {
3182 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3183 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3184 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3185 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3186 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3187 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3188 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3189 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3190
3191 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3192 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested),
3193 &CompareDiagnostics)) {
3194 return 32;
3195 }
3196
3197 /* --- Degenerate case of everything in order --- */
3198 QCBOREncode_Init(&EC, TestBuf);
3199 QCBOREncode_OpenMap(&EC);
3200 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3201 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3202 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3203 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3204 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3205 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3206 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3207 QCBOREncode_CloseAndSortMap(&EC);
3208 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3209 if(uErr) {
3210 return 41;
3211 }
3212
3213 static const uint8_t sp6Items[] = {
3214 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3215 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3216 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3217 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3218 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3219 &CompareDiagnostics)) {
3220 return 42;
3221 }
3222
3223 /* --- Degenerate case -- reverse order --- */
3224 QCBOREncode_Init(&EC, TestBuf);
3225 QCBOREncode_OpenMap(&EC);
3226 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3227 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3228 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3229 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3230 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3231 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3232 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3233 QCBOREncode_CloseAndSortMap(&EC);
3234 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3235 if(uErr) {
3236 return 51;
3237 }
3238
3239 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3240 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3241 &CompareDiagnostics)) {
3242 return 52;
3243 }
3244
3245 /* --- Same items, randomly out of order --- */
3246 QCBOREncode_Init(&EC, TestBuf);
3247 QCBOREncode_OpenMap(&EC);
3248 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3249 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3250 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3251 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3252 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3253 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3254 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3255 QCBOREncode_CloseAndSortMap(&EC);
3256 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3257 if(uErr) {
3258 return 61;
3259 }
3260
3261 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3263 &CompareDiagnostics)) {
3264 return 62;
3265 }
3266
3267 /* --- Stuff in front of and after array to sort --- */
3268 QCBOREncode_Init(&EC, TestBuf);
3269 QCBOREncode_OpenArray(&EC);
3270 QCBOREncode_AddInt64(&EC, 111);
3271 QCBOREncode_AddInt64(&EC, 222);
3272 QCBOREncode_OpenMap(&EC);
3273 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3274 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3275 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3276 QCBOREncode_CloseAndSortMap(&EC);
3277 QCBOREncode_AddInt64(&EC, 888);
3278 QCBOREncode_AddInt64(&EC, 999);
3279 QCBOREncode_CloseArray(&EC);
3280 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3281 if(uErr) {
3282 return 71;
3283 }
3284
3285 static const uint8_t spPreItems[] = {
3286 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3287 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3288 0x03, 0xE7};
3289 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3290 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3291 &CompareDiagnostics)) {
3292 return 72;
3293 }
3294
3295 /* --- map with labels of all CBOR major types and in reverse order --- */
3296 QCBOREncode_Init(&EC, TestBuf);
3297 QCBOREncode_OpenMap(&EC);
3298
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003299 /* Adding labels directly rather than AddToMap functions */
3300
3301#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003302 QCBOREncode_AddDouble(&EC, 8.77);
3303 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003304#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003305
3306 QCBOREncode_AddBool(&EC, true);
3307 QCBOREncode_AddInt64(&EC, 6);
3308
3309 QCBOREncode_AddDateEpoch(&EC, 88);
3310 QCBOREncode_AddInt64(&EC, 5);
3311
3312 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3313 QCBOREncode_AddInt64(&EC, 4);
3314
3315 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3316 QCBOREncode_AddInt64(&EC, 7);
3317
3318 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3319
3320 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3321 QCBOREncode_AddInt64(&EC, 2);
3322
3323 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3324 QCBOREncode_CloseAndSortMap(&EC);
3325
3326 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3327 if(uErr) {
3328 return 81;
3329 }
3330
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003331#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003332 static const uint8_t spLabelTypes[] = {
3333 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3334 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3335 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3336 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3337 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003338#else
3339 static const uint8_t spLabelTypes[] = {
3340 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3341 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3342 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3343#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3344
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003345 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3346 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3347 &CompareDiagnostics)) {
3348 return 82;
3349 }
3350
3351 /* --- labels are indefinitely encoded --- */
3352 QCBOREncode_Init(&EC, TestBuf);
3353 QCBOREncode_OpenMap(&EC);
3354
3355 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3356
3357 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3358
3359 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3360 QCBOREncode_AddInt64(&EC, 3);
3361
3362 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3363 QCBOREncode_AddInt64(&EC, 4);
3364
3365 QCBOREncode_CloseAndSortMap(&EC);
3366
3367 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3368 if(uErr) {
3369 return 91;
3370 }
3371
3372 static const uint8_t spIndefItems[] = {
3373 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3374 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3375 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3376 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3377 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3378 &CompareDiagnostics)) {
3379 return 92;
3380 }
3381
3382 /* --- Indefinitely encoded maps --- */
3383 QCBOREncode_Init(&EC, TestBuf);
3384 QCBOREncode_OpenMapIndefiniteLength(&EC);
3385
3386 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3387 QCBOREncode_CloseMapIndefiniteLength(&EC);
3388
3389 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3390 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3391
3392 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3393 QCBOREncode_CloseMapIndefiniteLength(&EC);
3394
3395 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3396 QCBOREncode_CloseMapIndefiniteLength(&EC);
3397
3398 QCBOREncode_CloseAndSortMapIndef(&EC);
3399 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3400 if(uErr) {
3401 return 101;
3402 }
3403
3404 static const uint8_t spIndeMaps[] = {
3405 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3406 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3407 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3408 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3409 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3410 &CompareDiagnostics)) {
3411 return 102;
3412 }
3413
3414 return 0;
3415}
Laurence Lundblade240ca822024-01-16 11:11:00 -07003416
3417
3418#include <math.h> /* For INFINITY and NAN and isnan() */
3419
3420
3421int32_t CDETest(void)
3422{
3423 QCBOREncodeContext EC;
3424 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003425 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003426
3427 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3428
3429 QCBOREncode_SerializationCDE(&EC);
3430
3431 /* Items added to test sorting and preferred encoding of numbers and floats */
3432 QCBOREncode_OpenMap(&EC);
3433 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3434 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3435 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3436 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3437 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3438 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3439
3440 QCBOREncode_CloseMap(&EC);
3441
Laurence Lundbladee75f5962024-02-15 17:51:32 -08003442 uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
3443 if(uExpectedErr != QCBOR_SUCCESS) {
3444 return 2;
3445 }
Laurence Lundblade240ca822024-01-16 11:11:00 -07003446
3447 static const uint8_t spExpectedCDE[] = {
3448 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3449 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3450 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3451 0x61, 0x78, 0xF9, 0x40, 0x00};
3452
3453 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3454 Encoded)) {
3455 return 1;
3456 }
3457
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003458
3459#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3460 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3461#else
3462 uExpectedErr = QCBOR_SUCCESS;
3463#endif
3464
3465
Laurence Lundblade240ca822024-01-16 11:11:00 -07003466 /* Next, make sure methods that encode non-CDE error out */
3467 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3468 QCBOREncode_SerializationCDE(&EC);
3469 QCBOREncode_OpenMapIndefiniteLength(&EC);
3470 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003471 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003472 return 100;
3473 }
3474
3475
3476 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3477 QCBOREncode_SerializationCDE(&EC);
3478 QCBOREncode_AddDoubleNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003479 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003480 return 101;
3481 }
3482
3483 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3484 QCBOREncode_SerializationCDE(&EC);
3485 QCBOREncode_AddFloatNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003486 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003487 return 101;
3488 }
3489
3490 return 0;
3491}
3492
3493
3494int32_t DCBORTest(void)
3495{
3496 QCBOREncodeContext EC;
3497 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003498 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003499
3500 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3501
3502 QCBOREncode_SerializationdCBOR(&EC);
3503
3504 /* Items added to test sorting and preferred encoding of numbers and floats */
3505 QCBOREncode_OpenMap(&EC);
3506 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3507 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3508 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3509 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3510 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3511
3512 QCBOREncode_CloseMap(&EC);
3513
3514 QCBOREncode_Finish(&EC, &Encoded);
3515
3516 static const uint8_t spExpecteddCBOR[] = {
3517 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3518 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3519 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3520
3521 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3522 Encoded)) {
3523 return 1;
3524 }
3525
3526
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003527#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3528 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3529#else
3530 uExpectedErr = QCBOR_SUCCESS;
3531#endif
3532
Laurence Lundblade240ca822024-01-16 11:11:00 -07003533 /* Next, make sure methods that encode non-CDE error out */
3534 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3535 QCBOREncode_SerializationdCBOR(&EC);
3536 QCBOREncode_OpenMapIndefiniteLength(&EC);
3537 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003538 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003539 return 100;
3540 }
3541
3542 /* Next, make sure methods that encode non-CDE error out */
3543 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3544 QCBOREncode_SerializationdCBOR(&EC);
3545 QCBOREncode_AddUndef(&EC);
3546 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003547 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003548 return 101;
3549 }
3550
3551
3552 return 0;
3553
3554}