blob: ed8f73402afe6359222ddf3ddbfaf110e0fa5e08 [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
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800748#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800749 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
750 /* 0x7ff8000000000001ULL is a NaN with a payload. */
751 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
752 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
753 nReturn = -22;
754 goto Done;
755 }
756
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800757 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
758 /* 0x7ff8000000000001ULL is a NaN with a payload. */
759 QCBOREncode_AddDoubleNoPreferred(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
760 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
761 nReturn = -22;
762 goto Done;
763 }
764
765
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800766 /* 0x7ffc000000000000ULL is a NaN with a payload. */
767 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
768 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
769 nReturn = -23;
770 goto Done;
771 }
772
773 /* 0x7ff80001UL is a NaN with a payload. */
774 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
775 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
776 nReturn = -24;
777 goto Done;
778 }
779
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800780 /* 0x7ff80001UL is a NaN with a payload. */
781 QCBOREncode_AddFloatNoPreferred(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
782 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
783 nReturn = -24;
784 goto Done;
785 }
786
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800787 /* 0x7ffc0000UL is a NaN with a payload. */
788 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
789 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
Laurence Lundbladee75f5962024-02-15 17:51:32 -0800790 nReturn = -25;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800791 goto Done;
792 }
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800793#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795Done:
796 return nReturn;
797}
798
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700799
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530800/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200801 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530802 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
803 3B 0000000100000000 # negative(4294967296)
804 3A FFFFFFFF # negative(4294967295)
805 3A FFFFFFFE # negative(4294967294)
806 3A FFFFFFFD # negative(4294967293)
807 3A 7FFFFFFF # negative(2147483647)
808 3A 7FFFFFFE # negative(2147483646)
809 3A 00010001 # negative(65537)
810 3A 00010000 # negative(65536)
811 39 FFFF # negative(65535)
812 39 FFFE # negative(65534)
813 39 FFFD # negative(65533)
814 39 0100 # negative(256)
815 38 FF # negative(255)
816 38 FE # negative(254)
817 38 FD # negative(253)
818 38 18 # negative(24)
819 37 # negative(23)
820 36 # negative(22)
821 20 # negative(0)
822 00 # unsigned(0)
823 00 # unsigned(0)
824 01 # unsigned(1)
825 16 # unsigned(22)
826 17 # unsigned(23)
827 18 18 # unsigned(24)
828 18 19 # unsigned(25)
829 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200830 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530831 18 FE # unsigned(254)
832 18 FF # unsigned(255)
833 19 0100 # unsigned(256)
834 19 0101 # unsigned(257)
835 19 FFFE # unsigned(65534)
836 19 FFFF # unsigned(65535)
837 1A 00010000 # unsigned(65536)
838 1A 00010001 # unsigned(65537)
839 1A 00010002 # unsigned(65538)
840 1A 7FFFFFFF # unsigned(2147483647)
841 1A 7FFFFFFF # unsigned(2147483647)
842 1A 80000000 # unsigned(2147483648)
843 1A 80000001 # unsigned(2147483649)
844 1A FFFFFFFE # unsigned(4294967294)
845 1A FFFFFFFF # unsigned(4294967295)
846 1B 0000000100000000 # unsigned(4294967296)
847 1B 0000000100000001 # unsigned(4294967297)
848 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
849 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
850 */
851static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200852 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800853 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
854 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
855 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
856 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
857 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
858 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
859 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
860 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
861 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
862 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200863 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
864 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
865 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
866 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
867 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
868 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
869 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
870 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
871 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
872 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
873 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
874 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
875 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800876
877/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800878
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800879 Test the generation of integers. This also ends up testing
880 encoding of all the different lengths. It encodes integers
881 of many lengths and values, especially around the boundaries
882 for different types of integers. It compares the output
883 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300886int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800887{
888 QCBOREncodeContext ECtx;
889 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800890
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530891 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892 QCBOREncode_OpenArray(&ECtx);
893
894 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
895 QCBOREncode_AddInt64(&ECtx, -4294967297);
896 QCBOREncode_AddInt64(&ECtx, -4294967296);
897 QCBOREncode_AddInt64(&ECtx, -4294967295);
898 QCBOREncode_AddInt64(&ECtx, -4294967294);
899 QCBOREncode_AddInt64(&ECtx, -2147483648);
900 QCBOREncode_AddInt64(&ECtx, -2147483647);
901 QCBOREncode_AddInt64(&ECtx, -65538);
902 QCBOREncode_AddInt64(&ECtx, -65537);
903 QCBOREncode_AddInt64(&ECtx, -65536);
904 QCBOREncode_AddInt64(&ECtx, -65535);
905 QCBOREncode_AddInt64(&ECtx, -65534);
906 QCBOREncode_AddInt64(&ECtx, -257);
907 QCBOREncode_AddInt64(&ECtx, -256);
908 QCBOREncode_AddInt64(&ECtx, -255);
909 QCBOREncode_AddInt64(&ECtx, -254);
910 QCBOREncode_AddInt64(&ECtx, -25);
911 QCBOREncode_AddInt64(&ECtx, -24);
912 QCBOREncode_AddInt64(&ECtx, -23);
913 QCBOREncode_AddInt64(&ECtx, -1);
914 QCBOREncode_AddInt64(&ECtx, 0);
915 QCBOREncode_AddUInt64(&ECtx, 0ULL);
916 QCBOREncode_AddInt64(&ECtx, 1);
917 QCBOREncode_AddInt64(&ECtx, 22);
918 QCBOREncode_AddInt64(&ECtx, 23);
919 QCBOREncode_AddInt64(&ECtx, 24);
920 QCBOREncode_AddInt64(&ECtx, 25);
921 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200922 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800923 QCBOREncode_AddInt64(&ECtx, 254);
924 QCBOREncode_AddInt64(&ECtx, 255);
925 QCBOREncode_AddInt64(&ECtx, 256);
926 QCBOREncode_AddInt64(&ECtx, 257);
927 QCBOREncode_AddInt64(&ECtx, 65534);
928 QCBOREncode_AddInt64(&ECtx, 65535);
929 QCBOREncode_AddInt64(&ECtx, 65536);
930 QCBOREncode_AddInt64(&ECtx, 65537);
931 QCBOREncode_AddInt64(&ECtx, 65538);
932 QCBOREncode_AddInt64(&ECtx, 2147483647);
933 QCBOREncode_AddInt64(&ECtx, 2147483647);
934 QCBOREncode_AddInt64(&ECtx, 2147483648);
935 QCBOREncode_AddInt64(&ECtx, 2147483649);
936 QCBOREncode_AddInt64(&ECtx, 4294967294);
937 QCBOREncode_AddInt64(&ECtx, 4294967295);
938 QCBOREncode_AddInt64(&ECtx, 4294967296);
939 QCBOREncode_AddInt64(&ECtx, 4294967297);
940 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
941 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800942
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530945 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700946 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 nReturn = -1;
948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530950 if(CheckResults(Enc, spExpectedEncodedInts))
951 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800952
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953 return(nReturn);
954}
955
956
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530957/*
958 85 # array(5)
959 F5 # primitive(21)
960 F4 # primitive(20)
961 F6 # primitive(22)
962 F7 # primitive(23)
963 A1 # map(1)
964 65 # text(5)
965 554E446566 # "UNDef"
966 F7 # primitive(23)
967 */
968static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800969 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
970
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300971int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800972{
973 QCBOREncodeContext ECtx;
974 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800975
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530976 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900978
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700979 QCBOREncode_AddBool(&ECtx, true);
980 QCBOREncode_AddBool(&ECtx, false);
981 QCBOREncode_AddNULL(&ECtx);
982 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984 QCBOREncode_OpenMap(&ECtx);
985
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700986 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 QCBOREncode_CloseMap(&ECtx);
988
989 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530991 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700992 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800993 nReturn = -1;
994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800995
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530996 if(CheckResults(ECBOR, spExpectedEncodedSimple))
997 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800998
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800999 return(nReturn);
1000}
1001
Jan Jongboom47d86c52019-07-25 08:54:16 +02001002/*
1003 9F # array(5)
1004 F5 # primitive(21)
1005 F4 # primitive(20)
1006 F6 # primitive(22)
1007 F7 # primitive(23)
1008 BF # map(1)
1009 65 # text(5)
1010 554E446566 # "UNDef"
1011 F7 # primitive(23)
1012 FF # break
1013 FF # break
1014 */
1015static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
1016 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
1017
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001018int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +02001019{
1020 QCBOREncodeContext ECtx;
1021 int nReturn = 0;
1022
1023 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1024 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
1025
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001026 QCBOREncode_AddBool(&ECtx, true);
1027 QCBOREncode_AddBool(&ECtx, false);
1028 QCBOREncode_AddNULL(&ECtx);
1029 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +02001030
1031 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
1032
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001033 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +02001034 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
1035
1036 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
1037
1038 UsefulBufC ECBOR;
1039 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1040 nReturn = -1;
1041 }
1042
1043 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
1044 return -2;
1045
1046 return(nReturn);
1047}
1048
Jan Jongboom5d827882019-08-07 12:51:15 +02001049/*
1050A5 # map(5)
1051 63 # text(3)
1052 617272 # "arr"
1053 98 1F # array(31)
1054 00 # unsigned(0)
1055 01 # unsigned(1)
1056 02 # unsigned(2)
1057 03 # unsigned(3)
1058 04 # unsigned(4)
1059 05 # unsigned(5)
1060 06 # unsigned(6)
1061 07 # unsigned(7)
1062 08 # unsigned(8)
1063 09 # unsigned(9)
1064 0A # unsigned(10)
1065 0B # unsigned(11)
1066 0C # unsigned(12)
1067 0D # unsigned(13)
1068 0E # unsigned(14)
1069 0F # unsigned(15)
1070 10 # unsigned(16)
1071 11 # unsigned(17)
1072 12 # unsigned(18)
1073 13 # unsigned(19)
1074 14 # unsigned(20)
1075 15 # unsigned(21)
1076 16 # unsigned(22)
1077 17 # unsigned(23)
1078 18 18 # unsigned(24)
1079 18 19 # unsigned(25)
1080 18 1A # unsigned(26)
1081 18 1B # unsigned(27)
1082 18 1C # unsigned(28)
1083 18 1D # unsigned(29)
1084 18 1E # unsigned(30)
1085 63 # text(3)
1086 6D6170 # "map"
1087 B8 1F # map(31)
1088 61 # text(1)
1089 61 # "a"
1090 00 # unsigned(0)
1091 61 # text(1)
1092 62 # "b"
1093 01 # unsigned(1)
1094 61 # text(1)
1095 63 # "c"
1096 02 # unsigned(2)
1097 61 # text(1)
1098 64 # "d"
1099 03 # unsigned(3)
1100 61 # text(1)
1101 65 # "e"
1102 04 # unsigned(4)
1103 61 # text(1)
1104 66 # "f"
1105 05 # unsigned(5)
1106 61 # text(1)
1107 67 # "g"
1108 06 # unsigned(6)
1109 61 # text(1)
1110 68 # "h"
1111 07 # unsigned(7)
1112 61 # text(1)
1113 69 # "i"
1114 08 # unsigned(8)
1115 61 # text(1)
1116 6A # "j"
1117 09 # unsigned(9)
1118 61 # text(1)
1119 6B # "k"
1120 0A # unsigned(10)
1121 61 # text(1)
1122 6C # "l"
1123 0B # unsigned(11)
1124 61 # text(1)
1125 6D # "m"
1126 0C # unsigned(12)
1127 61 # text(1)
1128 6E # "n"
1129 0D # unsigned(13)
1130 61 # text(1)
1131 6F # "o"
1132 0E # unsigned(14)
1133 61 # text(1)
1134 70 # "p"
1135 0F # unsigned(15)
1136 61 # text(1)
1137 71 # "q"
1138 10 # unsigned(16)
1139 61 # text(1)
1140 72 # "r"
1141 11 # unsigned(17)
1142 61 # text(1)
1143 73 # "s"
1144 12 # unsigned(18)
1145 61 # text(1)
1146 74 # "t"
1147 13 # unsigned(19)
1148 61 # text(1)
1149 75 # "u"
1150 14 # unsigned(20)
1151 61 # text(1)
1152 76 # "v"
1153 15 # unsigned(21)
1154 61 # text(1)
1155 77 # "w"
1156 16 # unsigned(22)
1157 61 # text(1)
1158 78 # "x"
1159 17 # unsigned(23)
1160 61 # text(1)
1161 79 # "y"
1162 18 18 # unsigned(24)
1163 61 # text(1)
1164 7A # "z"
1165 18 19 # unsigned(25)
1166 61 # text(1)
1167 41 # "A"
1168 18 1A # unsigned(26)
1169 61 # text(1)
1170 42 # "B"
1171 18 1B # unsigned(27)
1172 61 # text(1)
1173 43 # "C"
1174 18 1C # unsigned(28)
1175 61 # text(1)
1176 44 # "D"
1177 18 1D # unsigned(29)
1178 61 # text(1)
1179 45 # "E"
1180 18 1E # unsigned(30)
1181 65 # text(5)
1182 6D696E3331 # "min31"
1183 38 1E # negative(30)
1184 66 # text(6)
1185 706C75733331 # "plus31"
1186 18 1F # unsigned(31)
1187 63 # text(3)
1188 737472 # "str"
1189 78 1F # text(31)
1190 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1191 */
1192static const uint8_t EncodeLengthThirtyone[] = {
1193 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1194 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1195 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1196 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1197 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1198 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1199 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1200 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1201 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1202 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1203 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1204 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1205 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1206 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1207 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1208 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1209 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1210 0x31
1211};
1212
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001213int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001214{
1215 QCBOREncodeContext ECtx;
1216 int nReturn = 0;
1217
1218 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1219 QCBOREncode_OpenMap(&ECtx);
1220
1221 // add array with 31 items
1222 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1223 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001224 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001225 }
1226 QCBOREncode_CloseArray(&ECtx);
1227
1228 // add map with 31 items
1229 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001230 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001231 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001232 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001233 if (ix < 26) c = c + ix;
1234 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001235 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001236 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1237 }
1238 QCBOREncode_CloseMap(&ECtx);
1239
1240 // add -31 and +31
1241 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1242 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1243
1244 // add string with length 31
1245 const char *str = "testtesttesttesttesttestqcbor11";
1246 UsefulBufC str_b = { str, 31 };
1247 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1248
1249 QCBOREncode_CloseMap(&ECtx);
1250
1251 UsefulBufC ECBOR;
1252 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1253 nReturn = -1;
1254 }
1255
1256 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1257 return -2;
1258
1259 return(nReturn);
1260}
1261
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301262
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301263/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001264 * [ "2013-03-21T20:04:00Z",
1265 * 0("2013-03-21T20:04:00Z"),
1266 * 1363896240,
1267 * 1(1363896240),
1268 * 100(-10676),
1269 * 3994,
1270 * 1004("1940-10-09"),
1271 * "1980-12-08",
1272 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1273 * "SD": 1(999),
1274 * "Sample Date from RFC 8943": "1985-04-12",
1275 * 42: 1004("1985-04-12T23:20:50.52Z"),
1276 * "SY": 100(-10676),
1277 * 45: 3994
1278 * }
1279 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301280 */
1281static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001282 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1283 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1284 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1285 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1286 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1287 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1288 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1289 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1290 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1291 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1292 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1293 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1294 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1295 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1296 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1297 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1298 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1299 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1300 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1301 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1302 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1303 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1304 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001305
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001306int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001307{
1308 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001309
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301310 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 QCBOREncode_OpenArray(&ECtx);
1313
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001314 /* The values are taken from the CBOR RFCs */
1315 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1316 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1317 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1318 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1319 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1320 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1321 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1322 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001324 QCBOREncode_OpenMap(&ECtx);
1325
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001326 QCBOREncode_AddDateStringToMap(&ECtx,
1327 "Sample Date from RFC 3339",
1328 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001330 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1331 "Sample Date from RFC 8943",
1332 QCBOR_ENCODE_AS_BORROWED,
1333 "1985-04-12");
1334 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1335 42,
1336 QCBOR_ENCODE_AS_TAG,
1337 "1985-04-12T23:20:50.52Z");
1338 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1339 "SY",
1340 QCBOR_ENCODE_AS_TAG,
1341 -10676);
1342 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1343 45,
1344 QCBOR_ENCODE_AS_BORROWED,
1345 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001347 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 QCBOREncode_CloseArray(&ECtx);
1350
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301351 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001352 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001353 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301356 if(CheckResults(ECBOR, spExpectedEncodedDates))
1357 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001358
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001359 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001360}
1361
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301362
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001363int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001364{
1365 QCBOREncodeContext ECtx;
1366 int i;
1367 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001368
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301369 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1371 QCBOREncode_OpenArray(&ECtx);
1372 }
1373 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1374 QCBOREncode_CloseArray(&ECtx);
1375 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001376 UsefulBufC Encoded;
1377 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378 nReturn = -1;
1379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380
1381 return(nReturn);
1382}
1383
1384
1385
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001386int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387{
1388 QCBOREncodeContext ECtx;
1389 int i;
1390 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301392 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001393 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1394 QCBOREncode_OpenArray(&ECtx);
1395 }
1396 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1397 QCBOREncode_CloseArray(&ECtx);
1398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundblade0595e932018-11-02 22:22:47 +07001400 UsefulBufC Encoded;
1401 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001402 nReturn = -1;
1403 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001404
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 return(nReturn);
1406}
1407
1408
1409
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001410int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411{
1412 QCBOREncodeContext ECtx;
1413 int i;
1414 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001415
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301416 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001417 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1418 QCBOREncode_OpenArray(&ECtx);
1419 }
1420 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1421 QCBOREncode_CloseArray(&ECtx);
1422 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001423 UsefulBufC Encoded;
1424 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001425 nReturn = -1;
1426 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001427
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428 return(nReturn);
1429}
1430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301432/*
1433 81 # array(1)
1434 81 # array(1)
1435 81 # array(1)
1436 81 # array(1)
1437 80 # array(0)
1438*/
1439static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301442/*
1443 82 # array(2)
1444 81 # array(1)
1445 81 # array(1)
1446 81 # array(1)
1447 81 # array(1)
1448 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001449 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301450 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1451 3B 0000000100000000 # negative(4294967296)
1452 3A FFFFFFFF # negative(4294967295)
1453 3A FFFFFFFE # negative(4294967294)
1454 3A FFFFFFFD # negative(4294967293)
1455 3A 7FFFFFFF # negative(2147483647)
1456 3A 7FFFFFFE # negative(2147483646)
1457 3A 00010001 # negative(65537)
1458 3A 00010000 # negative(65536)
1459 39 FFFF # negative(65535)
1460 39 FFFE # negative(65534)
1461 39 FFFD # negative(65533)
1462 39 0100 # negative(256)
1463 38 FF # negative(255)
1464 38 FE # negative(254)
1465 38 FD # negative(253)
1466 38 18 # negative(24)
1467 37 # negative(23)
1468 36 # negative(22)
1469 20 # negative(0)
1470 00 # unsigned(0)
1471 00 # unsigned(0)
1472 01 # unsigned(1)
1473 16 # unsigned(22)
1474 17 # unsigned(23)
1475 18 18 # unsigned(24)
1476 18 19 # unsigned(25)
1477 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001478 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301479 18 FE # unsigned(254)
1480 18 FF # unsigned(255)
1481 19 0100 # unsigned(256)
1482 19 0101 # unsigned(257)
1483 19 FFFE # unsigned(65534)
1484 19 FFFF # unsigned(65535)
1485 1A 00010000 # unsigned(65536)
1486 1A 00010001 # unsigned(65537)
1487 1A 00010002 # unsigned(65538)
1488 1A 7FFFFFFF # unsigned(2147483647)
1489 1A 7FFFFFFF # unsigned(2147483647)
1490 1A 80000000 # unsigned(2147483648)
1491 1A 80000001 # unsigned(2147483649)
1492 1A FFFFFFFE # unsigned(4294967294)
1493 1A FFFFFFFF # unsigned(4294967295)
1494 1B 0000000100000000 # unsigned(4294967296)
1495 1B 0000000100000001 # unsigned(4294967297)
1496 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1497 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1498 */
1499static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001500 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1502 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1503 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1504 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1505 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1506 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1507 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1508 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1509 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1510 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1511 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001512 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1513 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1514 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1515 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1516 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1517 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1518 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1519 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1520 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1521 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1522 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1523 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1524 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525
1526
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001527int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001529 QCBOREncodeContext ECtx;
1530
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301531 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001532 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301533 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1534 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001535 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001536
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001537 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001538
Laurence Lundblade0595e932018-11-02 22:22:47 +07001539 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001540 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001542
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301543 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001544 return -5;
1545 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001547 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548}
1549
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301550/*
1551 This returns a pointer to spBigBuf
1552 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001553static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554{
1555 QCBOREncodeContext ECtx;
1556 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 *pEncoded = NULL;
1559 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301560 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001561
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001562 // loop runs CBOR encoding twice. First with no buffer to
1563 // calucate the length so buffer can be allocated correctly,
1564 // and last with the buffer to do the actual encoding
1565 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301566 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001567 QCBOREncode_OpenMap(&ECtx);
1568 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1569 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1570 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1571 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1572 QCBOREncode_CloseArray(&ECtx);
1573 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1574 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1575 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1576 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1577 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1578 QCBOREncode_CloseMap(&ECtx);
1579 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001580
Laurence Lundblade0595e932018-11-02 22:22:47 +07001581 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 goto Done;
1583 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301584 if(uFirstSizeEstimate != *pEncodedLen) {
1585 nReturn = 1;
1586 } else {
1587 nReturn = 0;
1588 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 goto Done;
1590 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301591 *pEncoded = spBigBuf;
1592 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001593
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001594 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001595
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001596 Done:
1597 return(nReturn);
1598}
1599
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301600/*
1601 A3 # map(3)
1602 6D # text(13)
1603 666972737420696E7465676572 # "first integer"
1604 18 2A # unsigned(42)
1605 77 # text(23)
1606 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1607 82 # array(2)
1608 67 # text(7)
1609 737472696E6731 # "string1"
1610 67 # text(7)
1611 737472696E6732 # "string2"
1612 6C # text(12)
1613 6D617020696E2061206D6170 # "map in a map"
1614 A4 # map(4)
1615 67 # text(7)
1616 62797465732031 # "bytes 1"
1617 44 # bytes(4)
1618 78787878 # "xxxx"
1619 67 # text(7)
1620 62797465732032 # "bytes 2"
1621 44 # bytes(4)
1622 79797979 # "yyyy"
1623 6B # text(11)
1624 616E6F7468657220696E74 # "another int"
1625 18 62 # unsigned(98)
1626 66 # text(6)
1627 746578742032 # "text 2"
1628 78 1E # text(30)
1629 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1630 */
1631static const uint8_t spValidMapEncoded[] = {
1632 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1633 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1634 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1635 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1636 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1637 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1638 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1639 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1640 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1641 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1642 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1643 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1644 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1645 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1646 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1647 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001648
1649
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001650int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001651{
1652 uint8_t *pEncodedMaps;
1653 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001655 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301656 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001658
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001659 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301660 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1661 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001663 return(nReturn);
1664}
1665
1666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001667/*
1668 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001669
Laurence Lundbladeee851742020-01-08 08:37:05 -08001670 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1671 CBOR_SIMPLEV_NULL
1672 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673 @param[in] szAlexString Diagnostic code.
1674 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001675 @param[in/out] pnLen Size of pOut buffer when called; length of data
1676 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678 @return
1679 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001681 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 -08001682 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001683 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001685 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1686 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001688 */
1689
Laurence Lundbladeee851742020-01-08 08:37:05 -08001690static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001691FormatRTICResults(uint8_t uRResult,
1692 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001693 const char *szType,
1694 const char *szAlexString,
1695 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001696{
1697 // Buffer that the result will be written in to
1698 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001699 // QCBOREncode will never write off the end of this buffer. If it won't
1700 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001701
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001702 // Context for the encoder
1703 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301704 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001705
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001706 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1707 // Contents are label / value pairs
1708 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001709
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001710 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001711
Laurence Lundbladeee851742020-01-08 08:37:05 -08001712 // The result: 0 if scan happened and found nothing; 1 if it happened and
1713 // found something wrong; 2 if it didn't happen
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001714 QCBOREncode_Private_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715
1716 // Add the diagnostic code
1717 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1718
1719 // Add a time stamp
1720 if(time) {
1721 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1722 }
1723
1724 // Add the diagnostic code
1725 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1726
1727 // Open a subordinate map for telemtry data
1728 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1729
1730 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001731
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001732 // Add a few fake integers and buffers for now.
1733 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1734
1735 // Add a few fake integers and buffers for now.
1736 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1737
1738 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301739 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001740 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001741
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1743 }
1744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001745
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001746 // Close the telemetry map
1747 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001748
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001749 // Close the map
1750 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001751
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301752 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001753
Laurence Lundblade0595e932018-11-02 22:22:47 +07001754 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001755
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301756 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001757}
1758
1759
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301760/*
1761 A5 # map(5)
1762 69 # text(9)
1763 696E74656772697479 # "integrity"
1764 F4 # primitive(20)
1765 64 # text(4)
1766 74797065 # "type"
1767 66 # text(6)
1768 726563656E74 # "recent"
1769 64 # text(4)
1770 74696D65 # "time"
1771 C1 # tag(1)
1772 1A 580D4172 # unsigned(1477263730)
1773 64 # text(4)
1774 64696167 # "diag"
1775 6A # text(10)
1776 30784131654335303031 # "0xA1eC5001"
1777 69 # text(9)
1778 74656C656D65747279 # "telemetry"
1779 A3 # map(3)
1780 69 # text(9)
1781 53686F652053697A65 # "Shoe Size"
1782 0C # unsigned(12)
1783 62 # text(2)
1784 4951 # "IQ"
1785 1A FFFFFFFF # unsigned(4294967295)
1786 77 # text(23)
1787 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1788 48 # bytes(8)
1789 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1790 */
1791static const uint8_t spExpectedRTIC[] = {
1792 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1793 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1794 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1795 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1796 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1797 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1798 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1799 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1800 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1801 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1802 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1803 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001804
1805
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001806int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001807{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001808 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301809 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301810 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301811 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001812 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301813 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001814
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301815 if(CheckResults(Encoded, spExpectedRTIC)) {
1816 return -2;
1817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001818
1819 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001820}
1821
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301822
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301823/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001824 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001825
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301826 82 # array(2)
1827 19 01C3 # unsigned(451)
1828 43 # bytes(3)
1829 1901D2 # "\x19\x01\xD2"
1830*/
1831static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301832
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001833static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1834
Laurence Lundbladeda532272019-04-07 11:40:17 -07001835/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001836 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001837 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001838int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001839{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001840 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001841
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001842 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301843 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001844
Laurence Lundblade684aec22018-10-12 19:33:53 +08001845 QCBOREncode_OpenArray(&EC);
1846 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001847
Laurence Lundblade684aec22018-10-12 19:33:53 +08001848 QCBOREncode_BstrWrap(&EC);
1849 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001850
Laurence Lundblade684aec22018-10-12 19:33:53 +08001851 UsefulBufC Wrapped;
1852 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001853
Laurence Lundblade684aec22018-10-12 19:33:53 +08001854 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001855
Laurence Lundblade684aec22018-10-12 19:33:53 +08001856 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001857 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001858 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001859 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001860
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301861 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001862 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001863 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001864
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001865 // Second test - see if the length of the wrapped
1866 // bstr is correct. Also tests bstr wrapping
1867 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001868 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1869 QCBOREncode_OpenArray(&EC);
1870 QCBOREncode_BstrWrap(&EC);
1871 QCBOREncode_OpenArray(&EC);
1872 QCBOREncode_AddNULL(&EC);
1873 QCBOREncode_CloseArray(&EC);
1874 UsefulBufC BStr;
1875 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001876 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1877 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001878 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001879 return -5;
1880 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001881
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001882
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001883 // Fourth test, cancelling a byte string
1884 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1885
1886 QCBOREncode_OpenArray(&EC);
1887 QCBOREncode_AddUInt64(&EC, 451);
1888
1889 QCBOREncode_BstrWrap(&EC);
1890 QCBOREncode_CancelBstrWrap(&EC);
1891
1892
1893 QCBOREncode_AddUInt64(&EC, 42);
1894 QCBOREncode_CloseArray(&EC);
1895 if(QCBOREncode_Finish(&EC, &Encoded)) {
1896 return -8;
1897 }
1898 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1899 return -9;
1900 }
1901
1902 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001903 // Fifth test, failed cancelling
1904 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1905
1906 QCBOREncode_OpenArray(&EC);
1907 QCBOREncode_AddUInt64(&EC, 451);
1908
1909 QCBOREncode_BstrWrap(&EC);
1910 QCBOREncode_AddUInt64(&EC, 99);
1911 QCBOREncode_CancelBstrWrap(&EC);
1912
1913 QCBOREncode_AddUInt64(&EC, 42);
1914 QCBOREncode_CloseArray(&EC);
1915 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001916#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001917 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1918 return -10;
1919 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001920#else
1921 if(uErr != QCBOR_SUCCESS) {
1922 return -110;
1923 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001924#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1925
1926 // Sixth test, another cancel, but the error is not caught
1927 // This use will produce unintended CBOR. The error
1928 // is not caught because it would require tracking state
1929 // for QCBOREncode_BstrWrapInMapN.
1930 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1931
1932 QCBOREncode_OpenMap(&EC);
1933 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1934
1935 QCBOREncode_BstrWrapInMapN(&EC, 55);
1936 QCBOREncode_CancelBstrWrap(&EC);
1937
1938 QCBOREncode_CloseMap(&EC);
1939 uErr = QCBOREncode_Finish(&EC, &Encoded);
1940 if(uErr != QCBOR_SUCCESS) {
1941 return -11;
1942 }
1943
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001944 return 0;
1945}
1946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001947
1948
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001949int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001950{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001951 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001952 UsefulBufC Wrapped;
1953 UsefulBufC Encoded2;
1954 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001955
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001956 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001957
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301958 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade684aec22018-10-12 19:33:53 +08001960 QCBOREncode_OpenArray(&EC);
1961 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001962
Laurence Lundblade684aec22018-10-12 19:33:53 +08001963 QCBOREncode_BstrWrap(&EC);
1964 QCBOREncode_AddUInt64(&EC, 466);
1965 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001966
Laurence Lundblade684aec22018-10-12 19:33:53 +08001967 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001968
Laurence Lundblade684aec22018-10-12 19:33:53 +08001969 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001970
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001971 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001972#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001973 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001974 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001975 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001976#else
1977 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1978 * is set and not to be sure to excerice all the relavant code in
1979 * both conditions. When the guards are disabled, there is no
1980 * error returned, but the code path is still covered.
1981 */
1982 if(uError != QCBOR_SUCCESS) {
1983 return (int32_t)(600 + uError);
1984 }
1985#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001986
Laurence Lundbladeee851742020-01-08 08:37:05 -08001987 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301988 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001989 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001990 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001991#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001992 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001993 return (int32_t)(700 + uError);
1994 }
1995#else
1996 if(uError != QCBOR_SUCCESS) {
1997 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001998 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001999#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundblade684aec22018-10-12 19:33:53 +08002001 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302002 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08002003 for(int i = 1; i < 18; i++) {
2004 QCBOREncode_BstrWrap(&EC);
2005 }
2006 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002007
Laurence Lundblade684aec22018-10-12 19:33:53 +08002008 for(int i = 1; i < 18; i++) {
2009 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
2010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002011
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002012 uError = QCBOREncode_Finish(&EC, &Encoded2);
2013 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08002014 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002015 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002016
Laurence Lundblade684aec22018-10-12 19:33:53 +08002017 return 0;
2018}
2019
2020
Laurence Lundblade684aec22018-10-12 19:33:53 +08002021/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002022 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002023
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002024 [
2025 h'82004E82014B8202488203458204428105',
2026 {
2027 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
2028 8426568656C6C6F18234BA2131318436568656C6C6F'
2029 }
2030 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002031
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002032 Unwrapping the first byte string in the above gives
2033 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302034
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002035 Unwrapping again, the byte string immediately above gives
2036 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302037
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002038 ...
2039
2040 Unrapping the second byte string in the top-level CBOR above gives
2041 {16: 16,
2042 64: "hello",
2043 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2044 }
2045
2046 Unwrapping again, the byte string immediately above gives
2047 {17: 17,
2048 65: "hello",
2049 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2050 }
2051
2052 ...
2053
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302054 */
2055static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002056{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002057 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2058 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2059 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2060 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2061 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2062 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2063 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2064 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2065 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2066 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002067};
2068
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002069
2070/*
2071 Get an int64 out of the decoder or fail.
2072 */
2073static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002074{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002075 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002076 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002077
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002078 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002079 if(nReturn) {
2080 return nReturn;
2081 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002082 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002083 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002085
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002086 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002087 return 0;
2088}
2089
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002090/*
2091 Get an array out of the decoder or fail.
2092 */
2093static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002094{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002095 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002096 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002098 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002099 if(nReturn) {
2100 return nReturn;
2101 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002102 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2103 return -1;
2104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002105
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002106 *pInt = Item.val.uCount;
2107 return 0;
2108}
2109
2110/*
2111 Get a map out of the decoder or fail.
2112 */
2113static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2114{
2115 QCBORItem Item;
2116 int32_t nReturn;
2117
2118 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002119 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002120 return nReturn;
2121 }
2122 if(Item.uDataType != QCBOR_TYPE_MAP) {
2123 return -1;
2124 }
2125
2126 *pInt = Item.val.uCount;
2127 return 0;
2128}
2129
2130/*
2131 Get a byte string out of the decoder or fail.
2132 */
2133static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2134{
2135 QCBORItem Item;
2136 int32_t nReturn;
2137
2138 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2139 if(nReturn) {
2140 return nReturn;
2141 }
2142 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002143 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002144 }
2145
2146 *pBstr = Item.val.string;
2147 return 0;
2148}
2149
2150/*
2151 Get a byte string out of the decoder or fail.
2152 */
2153static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2154{
2155 QCBORItem Item;
2156 int nReturn;
2157
2158 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2159 if(nReturn) {
2160 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002161 }
2162 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002163 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002164 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002165
2166 *pTstr = Item.val.string;
2167 return 0;
2168}
2169
2170
2171/*
2172 Recursively decode array containing a little CBOR and a bstr wrapped array
2173 with a little CBOR and a bstr wrapped array...
2174
2175 Part of bstr_wrap_nest_test.
2176 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2177{
2178 int64_t nInt;
2179 UsefulBufC Bstr;
2180 uint16_t nArrayCount;
2181 QCBORDecodeContext DC;
2182 int32_t nResult;
2183
2184 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2185
2186 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2187 return -10;
2188 }
2189
2190 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002191 return -11;
2192 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002193
2194 nResult = GetByteString(&DC, &Bstr);
2195 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2196 if(nArrayCount != 1) {
2197 return -12;
2198 } else {
2199 // successful exit
2200 return 0;
2201 }
2202 }
2203 if(nResult) {
2204 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002206
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002207 // tail recursion; good compilers will reuse the stack frame
2208 return DecodeNextNested(Bstr);
2209}
2210
2211
2212/*
2213 Recursively decode map containing a little CBOR and a bstr wrapped map
2214 with a little CBOR and a bstr wrapped map...
2215
2216 Part of bstr_wrap_nest_test.
2217 */
2218static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2219{
2220 int32_t nResult;
2221 uint16_t nMapCount;
2222 int64_t nInt;
2223 UsefulBufC Bstr;
2224 QCBORDecodeContext DC;
2225
2226 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2227
2228 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2229 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002232 if(GetInt64(&DC, &nInt)) {
2233 return -21;
2234 }
2235
2236 // The "hello"
2237 if(GetTextString(&DC, &Bstr)) {
2238 return -22;
2239 }
2240
2241 nResult = GetByteString(&DC, &Bstr);
2242 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2243 if(nMapCount == 2) {
2244 // successful exit
2245 return 0;
2246 } else {
2247 return -23;
2248 }
2249 }
2250
2251 if(nResult) {
2252 return -24;
2253 }
2254
2255 // tail recursion; good compilers will reuse the stack frame
2256 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002257}
2258
2259
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002260int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002261{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002262 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302263 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundblade684aec22018-10-12 19:33:53 +08002265 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002266 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002268 QCBOREncode_OpenArray(&EC);
2269
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002270 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002271 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002272 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002273 QCBOREncode_AddInt64(&EC, i);
2274 }
2275 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2276 QCBOREncode_CloseArray(&EC);
2277 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002279
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002280 QCBOREncode_OpenMap(&EC);
2281 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2282 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2283 QCBOREncode_OpenMap(&EC);
2284 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002285 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002286 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002287
2288 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2289 QCBOREncode_CloseMap(&EC);
2290 QCBOREncode_CloseBstrWrap(&EC, NULL);
2291 }
2292 QCBOREncode_CloseMap(&EC);
2293
Laurence Lundblade684aec22018-10-12 19:33:53 +08002294 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade684aec22018-10-12 19:33:53 +08002296 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002297 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002298 return -1;
2299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002300
Laurence Lundblade684aec22018-10-12 19:33:53 +08002301 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302302 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002303 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002305
Laurence Lundblade684aec22018-10-12 19:33:53 +08002306 // ---- Decode it and see if it is OK ------
2307 QCBORDecodeContext DC;
2308 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002310 UsefulBufC Bstr;
2311 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002312
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002313 // Array surrounding the the whole thing
2314 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002315 return -3;
2316 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002317
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002318 // Get the byte string wrapping some array stuff
2319 if(GetByteString(&DC, &Bstr)) {
2320 return -4;
2321 }
2322
2323 // Decode the wrapped nested structure
2324 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002325 if(nReturn) {
2326 return nReturn;
2327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002328
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002329 // A map enclosing some map-oriented bstr wraps
2330 if(GetMap(&DC, &nArrayCount)) {
2331 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002333
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002334 // Get the byte string wrapping some array stuff
2335 if(GetByteString(&DC, &Bstr)) {
2336 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002339 // Decode the wrapped nested structure
2340 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002341 if(nReturn) {
2342 return nReturn;
2343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundblade684aec22018-10-12 19:33:53 +08002345 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002346 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002347 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002348
Laurence Lundblade684aec22018-10-12 19:33:53 +08002349 return 0;
2350}
2351
2352
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002353static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302354 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2355 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2356 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2357 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2358 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2359 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2360 0x45, 0xca, 0xcb, 0x36};
2361
2362/*
2363 D2 # tag(18)
2364 84 # array(4)
2365 43 # bytes(3)
2366 A10126 # "\xA1\x01&"
2367 A1 # map(1)
2368 04 # unsigned(4)
2369 42 # bytes(2)
2370 3131 # "11"
2371 54 # bytes(20)
2372 546869732069732074686520636F6E74656E742E # "This is the content."
2373 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002374 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2375 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2376 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2377 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2378 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2379 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2380 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2381 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302382 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002383static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302384 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2385 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2386 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2387 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2388 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2389 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2390 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2391 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2392 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2393 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2394
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002395static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2396
2397
Laurence Lundblade684aec22018-10-12 19:33:53 +08002398/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002399 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002400 C.2.1. This doesn't actually verify the signature (however
2401 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002402 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002403int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002404{
2405 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002406 const char *szKid = "11";
2407 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2408 const char *szPayload = "This is the content.";
2409 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2410 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2411 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002412
Laurence Lundblade684aec22018-10-12 19:33:53 +08002413 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002414
2415 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302416 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002417
Laurence Lundblade684aec22018-10-12 19:33:53 +08002418 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002419 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002420 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002421
Laurence Lundblade684aec22018-10-12 19:33:53 +08002422 // Add protected headers
2423 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002424
Laurence Lundblade684aec22018-10-12 19:33:53 +08002425 // Empty map with unprotected headers
2426 QCBOREncode_OpenMap(&EC);
2427 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2428 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002429
Laurence Lundblade684aec22018-10-12 19:33:53 +08002430 // The payload
2431 UsefulBufC WrappedPayload;
2432 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002433 // Payload is not actually CBOR in example C.2.1 like it would be
2434 // for a CWT or EAT. It is just a text string.
2435 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002436 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002437
Laurence Lundblade684aec22018-10-12 19:33:53 +08002438 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002439 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2440 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002441 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002442 return -1;
2443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002444
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002445/* if(UsefulBuf_Compare(WrappedPayload,
2446 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2447 return -1;
2448 } */
2449
Laurence Lundblade684aec22018-10-12 19:33:53 +08002450 // The signature
2451 QCBOREncode_AddBytes(&EC, Signature);
2452 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundblade684aec22018-10-12 19:33:53 +08002454 // Finish and check the results
2455 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002456 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002457 return -2;
2458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade684aec22018-10-12 19:33:53 +08002460 // 98 is the size from RFC 8152 C.2.1
2461 if(COSE_Sign1.len != 98) {
2462 return -3;
2463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002465 // It would be good to compare this to the output from a COSE
2466 // implementation like COSE-C. This has been checked against the
2467 // CBOR playground.
2468 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002469 return -4;
2470 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002471
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002472
2473 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2474 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2475
2476 // top level array for cose sign1, 18 is the tag for COSE sign
2477 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2478 QCBOREncode_OpenArray(&EC);
2479
2480 // Add protected headers
2481 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2482
2483 // Empty map with unprotected headers
2484 QCBOREncode_OpenMap(&EC);
2485 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2486 QCBOREncode_CloseMap(&EC);
2487
2488 // The payload
2489 QCBOREncode_BstrWrap(&EC);
2490 // Payload is not actually CBOR in example C.2.1 like it would be
2491 // for a CWT or EAT. It is just a text string.
2492 QCBOREncode_AddEncoded(&EC, Payload);
2493 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2494
2495 // Check we got back the actual payload expected
2496 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2497 if(UsefulBuf_Compare(WrappedPayload,
2498 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2499 return -11;
2500 }
2501
2502 // The signature
2503 QCBOREncode_AddBytes(&EC, Signature);
2504 QCBOREncode_CloseArray(&EC);
2505
2506 // Finish and check the results
2507 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2508 return -12;
2509 }
2510
2511 // 98 is the size from RFC 8152 C.2.1
2512 if(COSE_Sign1.len != 98) {
2513 return -13;
2514 }
2515
2516 // It would be good to compare this to the output from a COSE
2517 // implementation like COSE-C. This has been checked against the
2518 // CBOR playground.
2519 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2520 return -14;
2521 }
2522
Laurence Lundblade684aec22018-10-12 19:33:53 +08002523 return 0;
2524}
2525
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002526
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002527int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002528{
2529 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002530 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002531
2532
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002533 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002534 // Do all of these tests with NULL buffers so no actual
2535 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002536 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002537
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002538 // First verify no error from a big buffer
2539 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002540 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002541 // 6 is the CBOR overhead for opening the array and encodng the length
2542 // This exactly fills the buffer.
2543 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002544 QCBOREncode_CloseArray(&EC);
2545 size_t xx;
2546 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2547 return -1;
2548 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002549
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002550 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002551 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002552 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002553 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002554 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002555 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002556 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2557 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002558 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002559 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002560 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002561 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2562 return -2;
2563 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002564
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002565 // Third, fit an array in exactly at max position allowed
2566 QCBOREncode_Init(&EC, Buffer);
2567 QCBOREncode_OpenArray(&EC);
2568 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2569 QCBOREncode_OpenArray(&EC);
2570 QCBOREncode_CloseArray(&EC);
2571 QCBOREncode_CloseArray(&EC);
2572 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2573 return -10;
2574 }
2575
2576
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002577 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2578 // Work close to the 4GB size limit for a better test
2579 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002580 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002581
2582 QCBOREncode_Init(&EC, Large);
2583 QCBOREncode_OpenArray(&EC);
2584 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2585 QCBOREncode_CloseArray(&EC);
2586 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2587 // Making sure it succeeds when it should first
2588 return -3;
2589 }
2590
2591 QCBOREncode_Init(&EC, Large);
2592 QCBOREncode_OpenArray(&EC);
2593 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2594 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2595 QCBOREncode_CloseArray(&EC);
2596 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2597 // Now just 1 byte over, see that it fails
2598 return -4;
2599 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002600
2601
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002602 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2603 QCBOREncode_Init(&EC, Large);
2604 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2605 QCBOREncode_OpenArray(&EC);
2606 }
2607 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2608 QCBOREncode_CloseArray(&EC);
2609 }
2610 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2611 // Making sure it succeeds when it should first
2612 return -5;
2613 }
2614
2615 QCBOREncode_Init(&EC, Large);
2616 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2617 QCBOREncode_OpenArray(&EC);
2618 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002619 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002620 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2621 QCBOREncode_CloseArray(&EC);
2622 }
2623 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002624 return -6;
2625 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002626
2627
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002628 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002629 QCBOREncode_Init(&EC, Large);
2630 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2631 QCBOREncode_OpenArray(&EC);
2632 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002633 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002634 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2635 QCBOREncode_CloseArray(&EC);
2636 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002637 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2638#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2639 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002640 return -7;
2641 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002642#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2643 if(uErr != QCBOR_SUCCESS) {
2644 return -107;
2645 }
2646#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002647
2648
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002649 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002650 QCBOREncode_Init(&EC, Large);
2651 QCBOREncode_OpenArray(&EC);
2652 UsefulBufC Wrap;
2653 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002654 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2655#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2656 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002657 return -8;
2658 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002659#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2660 if(uErr != QCBOR_SUCCESS) {
2661 return -108;
2662 }
2663#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002664
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002665 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002666 QCBOREncode_Init(&EC, Large);
2667 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2668 QCBOREncode_OpenArray(&EC);
2669 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002670 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002671 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2672 QCBOREncode_CloseArray(&EC);
2673 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002674
2675 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2676#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2677 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002678 return -9;
2679 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002680#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2681 if(uErr != QCBOR_SUCCESS) {
2682 return -109;
2683 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002684#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002685
Laurence Lundblade241705e2018-12-30 18:56:14 -08002686 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2687 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002688
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002689
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002690 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002691 QCBOREncode_Init(&EC, Buffer);
2692 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2693 return -11;
2694 }
2695
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002696 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002697 QCBOREncode_Init(&EC, Large);
2698 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002699 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002700 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2701#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2702 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002703 return -12;
2704 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002705#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2706 if(uErr != QCBOR_SUCCESS) {
2707 return -112;
2708 }
2709#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2710
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002711
2712 QCBOREncode_Init(&EC, Large);
2713 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002714 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002715 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2716#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2717 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002718 return -13;
2719 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002720#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2721 if(uErr != QCBOR_SUCCESS) {
2722 return -113;
2723 }
2724#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002725
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002726
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002727 return 0;
2728}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002729
2730
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002731#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002732/*
2733 [
2734 4([-1, 3]),
2735 4([-20, 4759477275222530853136]),
2736 4([9223372036854775807, -4759477275222530853137]),
2737 5([300, 100]),
2738 5([-20, 4759477275222530853136]),
2739 5([-9223372036854775808, -4759477275222530853137])
2740 ]
2741 */
2742static const uint8_t spExpectedExponentAndMantissaArray[] = {
2743 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2744 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2745 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2746 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2747 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2748 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2749 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2750 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2751 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2752 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2753 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2754
2755
2756/*
2757 {
2758 "decimal fraction": 4([-1, 3]),
2759 300: 4([-1, 3]),
2760 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2761 400: 4([2147483647, 4759477275222530853136]),
2762 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2763 500: 4([9223372036854775807, -4759477275222530853137]),
2764 "big float": 5([300, 100]),
2765 600: 5([300, 100]),
2766 "big float bignum positive": 5([-20, 4759477275222530853136]),
2767 700: 5([-20, 4759477275222530853136]),
2768 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2769 800: 5([-9223372036854775808, -4759477275222530853137])
2770 }
2771 */
2772static const uint8_t spExpectedExponentAndMantissaMap[] = {
2773 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2774 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2775 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2776 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2777 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2778 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2779 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2780 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2781 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2782 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2783 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2784 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2785 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2786 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2787 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2788 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2789 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2790 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2791 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2792 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2793 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2794 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2795 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2796 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2797 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2798 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2799 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2800 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2801 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2802 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2803 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2804 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2805 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2806 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2807 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2808 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2809 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2810 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2811 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2812 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2813 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2814 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2815 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2816 0x09, 0x10
2817};
2818
2819
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002820int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002821{
2822 QCBOREncodeContext EC;
2823 UsefulBufC EncodedExponentAndMantissa;
2824
2825 // Constant for the big number used in all the tests.
2826 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2827 0x06, 0x07, 0x08, 0x09, 0x010};
2828 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2829
2830 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2831 QCBOREncode_OpenArray(&EC);
2832 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2833 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2834 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2835 QCBOREncode_AddBigFloat(&EC, 100, 300);
2836 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2837 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2838 QCBOREncode_CloseArray(&EC);
2839
2840 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2841 return -2;
2842 }
2843
2844 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2845 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2846 NULL);
2847 if(nReturn) {
2848 return nReturn;
2849 }
2850
2851 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2852 QCBOREncode_OpenMap(&EC);
2853
2854 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2855
2856 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2857
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002858 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002859 "decimal fraction bignum postive",
2860 BigNum,
2861 false,
2862 -200);
2863
2864 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2865 400,
2866 BigNum,
2867 false,
2868 INT32_MAX);
2869
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002870 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002871 "decimal fraction bignum negative",
2872 BigNum,
2873 true,
2874 INT64_MAX);
2875
2876 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2877 500,
2878 BigNum,
2879 true,
2880 INT64_MAX);
2881
2882 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2883
2884 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2885
2886 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2887 "big float bignum positive",
2888 BigNum,
2889 false,
2890 -20);
2891
2892 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2893 700,
2894 BigNum,
2895 false,
2896 -20);
2897
2898 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2899 "big float bignum negative",
2900 BigNum,
2901 true,
2902 INT64_MIN);
2903
2904 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2905 800,
2906 BigNum,
2907 true,
2908 INT64_MIN);
2909
2910 QCBOREncode_CloseMap(&EC);
2911
2912 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2913 return -3;
2914 }
2915
2916
2917 struct UBCompareDiagnostic Diag;
2918
2919 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2920 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2921 &Diag);
2922 if(nReturn) {
2923 return nReturn + 1000000; // +1000000 to distinguish from first test above
2924 }
2925
2926 return 0;
2927}
2928
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002929#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002930
2931
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002932int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002933{
2934 /* This test doesn't have to be extensive, because just about every
2935 * other test exercises QCBOREncode_EncodeHead().
2936 */
2937 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002938 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002939
2940 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2941 CBOR_MAJOR_TYPE_POSITIVE_INT,
2942 0,
2943 0);
2944
2945 static const uint8_t expectedZero[] = {0x00};
2946
2947 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2948 return -1;
2949 }
2950
2951 // ---- Encode a zero padded out to an 8 byte integer ----
2952 encoded = QCBOREncode_EncodeHead(RightSize,
2953 CBOR_MAJOR_TYPE_POSITIVE_INT,
2954 8, // uMinSize is 8 bytes
2955 0);
2956
2957 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2958 0x00, 0x00, 0x00, 0x00};
2959
2960 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2961 return -2;
2962 }
2963
2964
2965 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002966 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002967
2968 encoded = QCBOREncode_EncodeHead(TooSmall,
2969 CBOR_MAJOR_TYPE_POSITIVE_INT,
2970 0,
2971 0);
2972
2973 if(!UsefulBuf_IsNULLC(encoded)) {
2974 return -3;
2975 }
2976
2977 return 0;
2978}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002979
2980
2981static const uint8_t spExpectedForOpenBytes[] = {
2982 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2983 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2984 0x78
2985};
2986
2987static const uint8_t spExpectedForOpenBytes2[] = {
2988 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2989 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2990 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2991};
2992
2993int32_t
2994OpenCloseBytesTest(void)
2995{
2996 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2997 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2998 QCBOREncodeContext EC;
2999 UsefulBuf Place;
3000 UsefulBufC Encoded;
3001 QCBORError uErr;
3002
3003 /* Normal use case -- add a byte string that fits */
3004 QCBOREncode_Init(&EC, TestBuf);
3005 QCBOREncode_OpenBytes(&EC, &Place);
3006 if(Place.ptr != TestBuf.ptr ||
3007 Place.len != TestBuf.len) {
3008 return 1;
3009 }
3010 Place.len -= 4;
3011 UsefulBuf_Set(Place, 'x');
3012 QCBOREncode_CloseBytes(&EC, Place.len);
3013 QCBOREncode_Finish(&EC, &Encoded);
3014 if(UsefulBuf_Compare(Encoded,
3015 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
3016 return 2;
3017 }
3018
Paul Liétar79789772022-07-26 20:33:18 +01003019 /* Run the same test but with a NULL buffer */
3020 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3021 QCBOREncode_OpenBytes(&EC, &Place);
3022 if(!UsefulBuf_IsNULL(Place)) {
3023 return 3;
3024 }
3025 Place.len -= 4;
3026 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3027 QCBOREncode_CloseBytes(&EC, Place.len);
3028 uErr = QCBOREncode_Finish(&EC, &Encoded);
3029 if(uErr != QCBOR_SUCCESS ||
3030 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3031 return 4;
3032 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003033
3034 /* Open a byte string with no room left */
3035 QCBOREncode_Init(&EC, TestBuf);
3036 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3037 QCBOREncode_OpenBytes(&EC, &Place);
3038 if(Place.ptr != NULL ||
3039 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003040 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003041 }
3042
3043 /* Try to extend byte string past end of encoding output buffer */
3044 QCBOREncode_Init(&EC, TestBuf);
3045 QCBOREncode_AddSZString(&EC, "012345678901234567");
3046 QCBOREncode_OpenBytes(&EC, &Place);
3047 /* Don't bother to write any bytes*/
3048 QCBOREncode_CloseBytes(&EC, Place.len+1);
3049 uErr = QCBOREncode_GetErrorState(&EC);
3050 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003051 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003052 }
3053
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003054 /* Close a byte string without opening one. */
3055 QCBOREncode_Init(&EC, TestBuf);
3056 QCBOREncode_AddSZString(&EC, "012345678");
3057 QCBOREncode_CloseBytes(&EC, 1);
3058 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003059#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003060 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003061 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003062 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003063#else
3064 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003065 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003066 }
3067#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003068
3069 /* Forget to close a byte string */
3070 QCBOREncode_Init(&EC, TestBuf);
3071 QCBOREncode_AddSZString(&EC, "012345678");
3072 QCBOREncode_OpenBytes(&EC, &Place);
3073 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003074#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003075 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003076 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003077 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003078#else
3079 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003080 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003081 }
3082#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003083
3084 /* Try to open a byte string in a byte string */
3085 QCBOREncode_Init(&EC, TestBuf);
3086 QCBOREncode_AddSZString(&EC, "012345678");
3087 QCBOREncode_OpenBytes(&EC, &Place);
3088 QCBOREncode_OpenBytes(&EC, &Place);
3089 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003090#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003091 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003092 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003093 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003094#else
3095 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003096 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003097 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003098#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3099
3100 /* A successful case with a little complexity */
3101 QCBOREncode_Init(&EC, TestBuf2);
3102 QCBOREncode_OpenMap(&EC);
3103 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3104 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3105 Place.len = 2;
3106 UsefulBuf_Set(Place, 'x');
3107 QCBOREncode_CloseBytes(&EC, 2);
3108 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3109 Place.len = 3;
3110 UsefulBuf_Set(Place, 'y');
3111 QCBOREncode_CloseBytes(&EC, 3);
3112 QCBOREncode_OpenArrayInMapN(&EC, 40);
3113 QCBOREncode_OpenBytes(&EC, &Place);
3114 QCBOREncode_CloseBytes(&EC, 0);
3115 QCBOREncode_CloseArray(&EC);
3116 QCBOREncode_CloseMap(&EC);
3117 uErr = QCBOREncode_Finish(&EC, &Encoded);
3118 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003119 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003120 }
3121 if(UsefulBuf_Compare(Encoded,
3122 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003123 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003124 }
3125
3126 return 0;
3127}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003128
3129
3130
3131int32_t
3132SortMapTest(void)
3133{
3134 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3135 QCBOREncodeContext EC;
3136 UsefulBufC EncodedAndSorted;
3137 QCBORError uErr;
3138 struct UBCompareDiagnostic CompareDiagnostics;
3139
3140
3141 /* --- Basic sort test case --- */
3142 QCBOREncode_Init(&EC, TestBuf);
3143 QCBOREncode_OpenMap(&EC);
3144 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3145 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3146 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3147 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3148 QCBOREncode_CloseAndSortMap(&EC);
3149 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3150 if(uErr) {
3151 return 11;
3152 }
3153
3154 static const uint8_t spBasic[] = {
3155 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3156
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003157 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003158 return 12;
3159 }
3160
3161 /* --- Empty map sort test case --- */
3162 QCBOREncode_Init(&EC, TestBuf);
3163 QCBOREncode_OpenMap(&EC);
3164 QCBOREncode_CloseAndSortMap(&EC);
3165 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3166 if(uErr) {
3167 return 21;
3168 }
3169
3170 static const uint8_t spEmpty[] = {0xA0};
3171 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3172 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3173 &CompareDiagnostics)) {
3174 return 22;
3175 }
3176
3177 /* --- Several levels of nested sorted maps --- */
3178 QCBOREncode_Init(&EC, TestBuf);
3179 QCBOREncode_OpenMap(&EC);
3180 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3181 QCBOREncode_OpenMapInMapN(&EC, 428);
3182 QCBOREncode_AddNULLToMap(&EC, "null");
3183 QCBOREncode_OpenArrayInMap(&EC, "array");
3184 QCBOREncode_AddSZString(&EC, "hi");
3185 QCBOREncode_AddSZString(&EC, "there");
3186 QCBOREncode_CloseArray(&EC);
3187 QCBOREncode_OpenMapInMap(&EC, "empty2");
3188 QCBOREncode_CloseAndSortMap(&EC);
3189 QCBOREncode_OpenMapInMap(&EC, "empty1");
3190 QCBOREncode_CloseAndSortMap(&EC);
3191 QCBOREncode_CloseAndSortMap(&EC);
3192 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3193 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3194 QCBOREncode_CloseAndSortMap(&EC);
3195 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3196 if(uErr) {
3197 return 31;
3198 }
3199 static const uint8_t spNested[] = {
3200 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3201 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3202 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3203 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3204 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3205 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3206 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3207 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3208
3209 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3210 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested),
3211 &CompareDiagnostics)) {
3212 return 32;
3213 }
3214
3215 /* --- Degenerate case of everything in order --- */
3216 QCBOREncode_Init(&EC, TestBuf);
3217 QCBOREncode_OpenMap(&EC);
3218 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3219 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3220 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3221 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3222 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3223 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3224 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3225 QCBOREncode_CloseAndSortMap(&EC);
3226 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3227 if(uErr) {
3228 return 41;
3229 }
3230
3231 static const uint8_t sp6Items[] = {
3232 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3233 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3234 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3235 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3236 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3237 &CompareDiagnostics)) {
3238 return 42;
3239 }
3240
3241 /* --- Degenerate case -- reverse order --- */
3242 QCBOREncode_Init(&EC, TestBuf);
3243 QCBOREncode_OpenMap(&EC);
3244 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3245 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3246 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3247 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3248 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3249 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3250 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3251 QCBOREncode_CloseAndSortMap(&EC);
3252 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3253 if(uErr) {
3254 return 51;
3255 }
3256
3257 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3258 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3259 &CompareDiagnostics)) {
3260 return 52;
3261 }
3262
3263 /* --- Same items, randomly out of order --- */
3264 QCBOREncode_Init(&EC, TestBuf);
3265 QCBOREncode_OpenMap(&EC);
3266 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3267 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3268 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3269 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3270 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3271 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3272 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3273 QCBOREncode_CloseAndSortMap(&EC);
3274 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3275 if(uErr) {
3276 return 61;
3277 }
3278
3279 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3280 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3281 &CompareDiagnostics)) {
3282 return 62;
3283 }
3284
3285 /* --- Stuff in front of and after array to sort --- */
3286 QCBOREncode_Init(&EC, TestBuf);
3287 QCBOREncode_OpenArray(&EC);
3288 QCBOREncode_AddInt64(&EC, 111);
3289 QCBOREncode_AddInt64(&EC, 222);
3290 QCBOREncode_OpenMap(&EC);
3291 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3292 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3293 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3294 QCBOREncode_CloseAndSortMap(&EC);
3295 QCBOREncode_AddInt64(&EC, 888);
3296 QCBOREncode_AddInt64(&EC, 999);
3297 QCBOREncode_CloseArray(&EC);
3298 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3299 if(uErr) {
3300 return 71;
3301 }
3302
3303 static const uint8_t spPreItems[] = {
3304 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3305 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3306 0x03, 0xE7};
3307 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3308 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3309 &CompareDiagnostics)) {
3310 return 72;
3311 }
3312
3313 /* --- map with labels of all CBOR major types and in reverse order --- */
3314 QCBOREncode_Init(&EC, TestBuf);
3315 QCBOREncode_OpenMap(&EC);
3316
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003317 /* Adding labels directly rather than AddToMap functions */
3318
3319#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003320 QCBOREncode_AddDouble(&EC, 8.77);
3321 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003322#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003323
3324 QCBOREncode_AddBool(&EC, true);
3325 QCBOREncode_AddInt64(&EC, 6);
3326
3327 QCBOREncode_AddDateEpoch(&EC, 88);
3328 QCBOREncode_AddInt64(&EC, 5);
3329
3330 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3331 QCBOREncode_AddInt64(&EC, 4);
3332
3333 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3334 QCBOREncode_AddInt64(&EC, 7);
3335
3336 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3337
3338 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3339 QCBOREncode_AddInt64(&EC, 2);
3340
3341 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3342 QCBOREncode_CloseAndSortMap(&EC);
3343
3344 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3345 if(uErr) {
3346 return 81;
3347 }
3348
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003349#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003350 static const uint8_t spLabelTypes[] = {
3351 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3352 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3353 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3354 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3355 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003356#else
3357 static const uint8_t spLabelTypes[] = {
3358 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3359 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3360 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3361#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3362
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003363 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3364 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3365 &CompareDiagnostics)) {
3366 return 82;
3367 }
3368
3369 /* --- labels are indefinitely encoded --- */
3370 QCBOREncode_Init(&EC, TestBuf);
3371 QCBOREncode_OpenMap(&EC);
3372
3373 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3374
3375 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3376
3377 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3378 QCBOREncode_AddInt64(&EC, 3);
3379
3380 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3381 QCBOREncode_AddInt64(&EC, 4);
3382
3383 QCBOREncode_CloseAndSortMap(&EC);
3384
3385 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3386 if(uErr) {
3387 return 91;
3388 }
3389
3390 static const uint8_t spIndefItems[] = {
3391 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3392 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3393 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3394 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3396 &CompareDiagnostics)) {
3397 return 92;
3398 }
3399
3400 /* --- Indefinitely encoded maps --- */
3401 QCBOREncode_Init(&EC, TestBuf);
3402 QCBOREncode_OpenMapIndefiniteLength(&EC);
3403
3404 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3405 QCBOREncode_CloseMapIndefiniteLength(&EC);
3406
3407 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3408 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3409
3410 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3411 QCBOREncode_CloseMapIndefiniteLength(&EC);
3412
3413 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3414 QCBOREncode_CloseMapIndefiniteLength(&EC);
3415
3416 QCBOREncode_CloseAndSortMapIndef(&EC);
3417 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3418 if(uErr) {
3419 return 101;
3420 }
3421
3422 static const uint8_t spIndeMaps[] = {
3423 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3424 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3425 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3426 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3427 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3428 &CompareDiagnostics)) {
3429 return 102;
3430 }
3431
3432 return 0;
3433}
Laurence Lundblade240ca822024-01-16 11:11:00 -07003434
3435
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003436#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
3437
Laurence Lundblade240ca822024-01-16 11:11:00 -07003438#include <math.h> /* For INFINITY and NAN and isnan() */
3439
3440
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003441/* Public function. See qcbor_encode_tests.h */
Laurence Lundblade240ca822024-01-16 11:11:00 -07003442int32_t CDETest(void)
3443{
3444 QCBOREncodeContext EC;
3445 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003446 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003447
3448 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3449
3450 QCBOREncode_SerializationCDE(&EC);
3451
3452 /* Items added to test sorting and preferred encoding of numbers and floats */
3453 QCBOREncode_OpenMap(&EC);
3454 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3455 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3456 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3457 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3458 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3459 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3460
3461 QCBOREncode_CloseMap(&EC);
3462
Laurence Lundbladee75f5962024-02-15 17:51:32 -08003463 uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
3464 if(uExpectedErr != QCBOR_SUCCESS) {
3465 return 2;
3466 }
Laurence Lundblade240ca822024-01-16 11:11:00 -07003467
3468 static const uint8_t spExpectedCDE[] = {
3469 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3470 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3471 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3472 0x61, 0x78, 0xF9, 0x40, 0x00};
3473
3474 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3475 Encoded)) {
3476 return 1;
3477 }
3478
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003479
3480#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3481 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3482#else
3483 uExpectedErr = QCBOR_SUCCESS;
3484#endif
3485
3486
Laurence Lundblade240ca822024-01-16 11:11:00 -07003487 /* Next, make sure methods that encode non-CDE error out */
3488 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3489 QCBOREncode_SerializationCDE(&EC);
3490 QCBOREncode_OpenMapIndefiniteLength(&EC);
3491 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003492 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003493 return 100;
3494 }
3495
3496
3497 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3498 QCBOREncode_SerializationCDE(&EC);
3499 QCBOREncode_AddDoubleNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003500 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003501 return 101;
3502 }
3503
3504 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3505 QCBOREncode_SerializationCDE(&EC);
3506 QCBOREncode_AddFloatNoPreferred(&EC, 0);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003507 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003508 return 101;
3509 }
3510
3511 return 0;
3512}
3513
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003514/* Public function. See qcbor_encode_tests.h */
Laurence Lundblade240ca822024-01-16 11:11:00 -07003515int32_t DCBORTest(void)
3516{
3517 QCBOREncodeContext EC;
3518 UsefulBufC Encoded;
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003519 QCBORError uExpectedErr;
Laurence Lundblade240ca822024-01-16 11:11:00 -07003520
3521 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3522
3523 QCBOREncode_SerializationdCBOR(&EC);
3524
3525 /* Items added to test sorting and preferred encoding of numbers and floats */
3526 QCBOREncode_OpenMap(&EC);
3527 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3528 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3529 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3530 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3531 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3532
3533 QCBOREncode_CloseMap(&EC);
3534
3535 QCBOREncode_Finish(&EC, &Encoded);
3536
3537 static const uint8_t spExpecteddCBOR[] = {
3538 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3539 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3540 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3541
3542 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3543 Encoded)) {
3544 return 1;
3545 }
3546
3547
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003548#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3549 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3550#else
3551 uExpectedErr = QCBOR_SUCCESS;
3552#endif
3553
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003554 /* Next, make sure methods that encode of non-CDE error out */
3555
3556 /* Indefinite-length map */
Laurence Lundblade240ca822024-01-16 11:11:00 -07003557 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3558 QCBOREncode_SerializationdCBOR(&EC);
3559 QCBOREncode_OpenMapIndefiniteLength(&EC);
3560 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003561 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003562 return 100;
3563 }
3564
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003565 /* Indefinite-length array */
Laurence Lundblade240ca822024-01-16 11:11:00 -07003566 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3567 QCBOREncode_SerializationdCBOR(&EC);
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003568 QCBOREncode_OpenArrayIndefiniteLength(&EC);
Laurence Lundblade240ca822024-01-16 11:11:00 -07003569 QCBOREncode_CloseMap(&EC);
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -08003570 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
Laurence Lundblade240ca822024-01-16 11:11:00 -07003571 return 101;
3572 }
3573
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003574 /* The "undef" special value */
3575 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3576 QCBOREncode_SerializationdCBOR(&EC);
3577 QCBOREncode_AddUndef(&EC);
3578 QCBOREncode_CloseMap(&EC);
3579 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3580 return 102;
3581 }
3582
3583 /* 65-bit negative integers */
3584 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3585 QCBOREncode_SerializationdCBOR(&EC);
3586 QCBOREncode_AddNegativeUInt64(&EC, 1);
3587 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3588 return 103;
3589 }
3590
3591 /* Improvement: when indefinite length string encoding is supported
3592 * test it here too. */
Laurence Lundblade240ca822024-01-16 11:11:00 -07003593
3594 return 0;
3595
3596}
Laurence Lundblade6bef4a62024-02-16 14:59:23 -08003597#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */