blob: 6cc9d08cbc3910218ca231ab75136ee46755b271 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundbladeee851742020-01-08 08:37:05 -080051//#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070076 if(((const uint8_t *)U1.ptr)[i] != ((const uint8_t *)U2.ptr)[i]) {
escherstairb5563422023-02-13 18:36:45 +010077 printf("Position: %u Actual: 0x%x Expected: 0x%x\n",
Laurence Lundbladeee851742020-01-08 08:37:05 -080078 (uint32_t)i,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070079 ((const uint8_t *)U1.ptr)[i],
80 ((const uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081 return 1;
82 }
83 }
84 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086}
87
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070088#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053089 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070091#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080094 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
95
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070096#endif
97
98
Laurence Lundblade59289e52019-12-30 13:44:37 -080099/*
100 Returns 0 if UsefulBufs are equal
101 Returns 1000000 + offeset if they are not equal.
Laurence Lundblade59289e52019-12-30 13:44:37 -0800102*/
103struct UBCompareDiagnostic {
104 uint8_t uActual;
105 uint8_t uExpected;
106 size_t uOffset;
107};
108
Laurence Lundbladeee851742020-01-08 08:37:05 -0800109static int32_t
110UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
111 UsefulBufC Expected,
112 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800113 size_t i;
114 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800115 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800116 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800117 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
118 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800119 pDiag->uOffset = i;
120 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800121 // Cast to int is OK as this is only a diagnostic and the sizes
122 // here are never over a few KB.
123 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800124 }
125 }
126 return 0;
127
128}
Laurence Lundblade59289e52019-12-30 13:44:37 -0800129
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700130
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530131// One big buffer that is used by all the tests to encode into
132// Putting it in uninitialized data is better than using a lot
133// of stack. The tests should run on small devices too.
134static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135
136
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800137
138/*
139 Some very minimal tests.
140 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300141int32_t BasicEncodeTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142{
143 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800144 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800145
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530146 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530147
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800148 QCBOREncode_OpenMap(&EC);
149 QCBOREncode_AddBoolToMapN(&EC, 66, true);
150 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
157
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800158 // Decode it and see that is right
159 QCBORDecodeContext DC;
160 QCBORItem Item;
161 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 QCBORDecode_GetNext(&DC, &Item);
164 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530165 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800168 QCBORDecode_GetNext(&DC, &Item);
169 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530174 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
177
Laurence Lundbladeee851742020-01-08 08:37:05 -0800178 // Make another encoded message with the CBOR from the previous
179 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530180 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800181 QCBOREncode_Init(&EC, MemoryForEncoded2);
182 QCBOREncode_OpenArray(&EC);
183 QCBOREncode_AddUInt64(&EC, 451);
184 QCBOREncode_AddEncoded(&EC, Encoded);
185 QCBOREncode_OpenMap(&EC);
186 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
187 QCBOREncode_CloseMap(&EC);
188 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700191 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530192 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800193 }
194 /*
195 [ // 0 1:3
196 451, // 1 1:2
197 { // 1 1:2 2:1
198 66: true // 2 1:1
199 },
200 { // 1 1:1 2:1
201 -70000: { // 2 1:1 2:1 3:1
202 66: true // 3 XXXXXX
203 }
204 }
205 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800209 83 # array(3)
210 19 01C3 # unsigned(451)
211 A1 # map(1)
212 18 42 # unsigned(66)
213 F5 # primitive(21)
214 A1 # map(1)
215 3A 0001116F # negative(69999)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800220
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221 // Decode it and see if it is OK
222 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800224 // 0 1:3
225 QCBORDecode_GetNext(&DC, &Item);
226 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530227 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 // 1 1:2
231 QCBORDecode_GetNext(&DC, &Item);
232 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530233 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800236 // 1 1:2 2:1
237 QCBORDecode_GetNext(&DC, &Item);
238 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530239 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800242 // 2 1:1
243 QCBORDecode_GetNext(&DC, &Item);
244 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530245 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800248 // 1 1:1 2:1
249 QCBORDecode_GetNext(&DC, &Item);
250 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530251 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800254 // 2 1:1 2:1 3:1
255 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800256 if(Item.uDataType != QCBOR_TYPE_MAP ||
257 Item.val.uCount != 1 ||
258 Item.uLabelType != QCBOR_TYPE_INT64 ||
259 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800263 // 3 XXXXXX
264 QCBORDecode_GetNext(&DC, &Item);
265 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530266 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800269 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530270 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800273 return 0;
274}
275
276
277
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530278static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade2d493002024-02-01 11:09:17 -0700279 0x98, 0x24, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530280 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
281 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
Laurence Lundblade2d493002024-02-01 11:09:17 -0700282 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0x20, 0xa4, 0x63, 0x4c, 0x42,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530283 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
284 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
285 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
286 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
287 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700288 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
290 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
291 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
292 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
293 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700294 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
295 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00,
296 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40,
297 0x66, 0x62,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530298 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
299 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
300 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700301 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530302 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
303 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
304 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
305 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
306 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
307 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
308 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
309 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
310 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
311 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
312 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
313 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
314 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
315 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700316 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
317 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530318 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
319 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
320 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
321 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
322 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
323 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
324 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
325 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
326 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
327 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
328 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
329 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
330 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
331 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
332 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
333 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
334 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
335 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
336 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
337 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
338 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
339 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
340 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
341 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
342 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
343 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
344 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
345 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
346 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
347 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
348 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
349 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
350 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
351 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
352 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
353 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
354 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
355 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
356 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
357 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
358 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
359 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
360 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
361 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
362 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
363 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
364 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
365 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
366 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
367 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
368 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
369 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
370 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
371 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
372 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
373 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
374 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
375 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700376 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530377 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
378 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
379 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
380 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
381 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
383 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
384 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
385 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
386 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
387 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
388 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
389 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
390 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
391 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
392 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
393 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
394 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
395 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
396 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
397 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
398 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
399 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
400 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
401 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
402 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
403 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
404 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
405 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
406 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
407 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
408 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
409 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
410 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700411 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530412 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
413 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
414 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
415 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
416 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
417 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
418 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
419 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
420 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
421 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
422 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
423 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
424 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
425 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
426 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
427 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
428 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
429 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
430 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
431 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
432 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
433 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
434 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
435 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
436 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
437 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
438 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
439 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
440 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
441 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
442 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
443 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
444 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
445 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
446 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
447 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
448 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
449 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
450 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
451 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
452 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
453 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
454 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
455 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
456 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
457 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
458 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
459 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
460 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
461 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
462 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
463 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
464 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
465 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
466 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
467 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
468 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
469 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
470 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
471 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
472 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
473 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
474 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
475 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
476 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
477 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
478 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
479 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
480 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
481 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
484 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
485 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487};
488
489
490static const char *szMIME = "\
491MIME-Version: 1.0\n\
492Content-Type: multipart/mixed;\n\
493boundary=\"XXXXboundary text\"\n\
494\n\
495This is a multipart message in MIME format.\n\
496\n\
497--XXXXboundary text\n\
498Content-Type: text/plain\n\
499\n\
500this is the body text\n\
501\n\
502--XXXXboundary text\n\
503Content-Type: text/plain;\n\
504Content-Disposition: attachment;\n\
505filename=\"test.txt\"\n\
506\n\
507this is the attachment text\n\
508\n\
509--XXXXboundary text--";
510
511
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700512static void AddAll(QCBOREncodeContext *pECtx)
513{
514 QCBOREncode_OpenArray(pECtx);
515
516 /* Some ints that are tagged and have strings preceeding them
517 * (not labels becase it is not a map) */
518 QCBOREncode_AddSZString(pECtx, "UINT62");
519 QCBOREncode_AddTag(pECtx, 100);
520 QCBOREncode_AddUInt64(pECtx, 89989909);
521 QCBOREncode_AddSZString(pECtx, "INT64");
522 QCBOREncode_AddTag(pECtx, 76);
523 QCBOREncode_AddInt64(pECtx, 77689989909);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700524 QCBOREncode_AddUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700525 QCBOREncode_AddInt64(pECtx, -44);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700526 QCBOREncode_AddNegativeUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700527
528 /* ints that go in maps */
529 QCBOREncode_OpenMap(pECtx);
530 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
531 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
532 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
533 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
534 QCBOREncode_CloseMap(pECtx);
535
536 /* Epoch Date */
537 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
538
539 /* Epoch date with labels */
540 QCBOREncode_OpenMap(pECtx);
541 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
542 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
543 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
544 QCBOREncode_CloseMap(pECtx);
545
546 /* Binary blobs */
547 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
548
549 /* binary blobs in maps */
550 QCBOREncode_OpenMap(pECtx);
551 QCBOREncode_AddSZString(pECtx, "binbin");
552 QCBOREncode_AddTag(pECtx, 100000);
553 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700554 QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700555 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
556 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
557 QCBOREncode_CloseMap(pECtx);
558
559 /* text blobs */
560 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
561 QCBOREncode_AddSZString(pECtx, "oof\n");
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700562 QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string
563
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700564 const char *szURL =
565 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
566 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
567 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
568 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
569 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
570
571 /* text blobs in maps */
572 QCBOREncode_OpenMap(pECtx);
573 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
574 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
575 QCBOREncode_AddSZString(pECtx, "()()()");
576 QCBOREncode_AddTag(pECtx, 1000);
577 QCBOREncode_AddSZString(pECtx, "rab rab oof");
578 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
579 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
580 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
581 QCBOREncode_AddURIToMap(pECtx,
582 "RFC",
583 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
584 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
585 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
586 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
587 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
588 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
589 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
590 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
591 QCBOREncode_CloseMap(pECtx);
592
593 /* Date strings */
594 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
595 QCBOREncode_OpenMap(pECtx);
596 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
597 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
598 QCBOREncode_CloseMap(pECtx);
599
600 /* true / false ... */
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700601 QCBOREncode_AddUndef(pECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700602 QCBOREncode_OpenMap(pECtx);
603 QCBOREncode_AddSZString(pECtx, "dare");
604 QCBOREncode_AddTag(pECtx, 66);
605 QCBOREncode_AddBool(pECtx, true);
606 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700607 QCBOREncode_AddNULLToMapN(pECtx, 737634);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700608 QCBOREncode_CloseMap(pECtx);
609
610 /* opening an array */
611 QCBOREncode_OpenArray(pECtx);
612 QCBOREncode_CloseArray(pECtx);
613
614 /* opening arrays in a map */
615 QCBOREncode_OpenMap(pECtx);
616 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
617 QCBOREncode_AddTag(pECtx, 1093);
618 QCBOREncode_OpenArray(pECtx);
619 QCBOREncode_CloseArray(pECtx);
620 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
621 QCBOREncode_CloseArray(pECtx);
622 QCBOREncode_OpenArrayInMapN(pECtx, 42);
623 QCBOREncode_CloseArray(pECtx);
624 QCBOREncode_CloseMap(pECtx);
625
626 /* opening maps with labels and tagging */
627 QCBOREncode_OpenMap(pECtx);
628 QCBOREncode_OpenMapInMap(pECtx, "in a map");
629 QCBOREncode_OpenMapInMapN(pECtx, 5556);
630 QCBOREncode_AddSZString(pECtx, "in a in a in a");
631 QCBOREncode_AddTag(pECtx, 9087);
632 QCBOREncode_OpenMap(pECtx);
633 QCBOREncode_CloseMap(pECtx);
634 QCBOREncode_CloseMap(pECtx);
635 QCBOREncode_CloseMap(pECtx);
636 QCBOREncode_CloseMap(pECtx);
637
638 /* Extended simple values (these are not standard...) */
639 QCBOREncode_OpenMap(pECtx);
640 QCBOREncode_AddSZString(pECtx, "s1");
641 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700642 QCBOREncode_Private_AddSimple(pECtx, 255);
643 QCBOREncode_Private_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700644 QCBOREncode_AddSZString(pECtx, "s3");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700646 QCBOREncode_Private_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700647 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
648 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700649 QCBOREncode_Private_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700650 QCBOREncode_AddInt64(pECtx, 89); // label before tag
651 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700652 QCBOREncode_Private_AddSimple(pECtx, 19);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700653 QCBOREncode_CloseMap(pECtx);
654
655 /* UUIDs */
656 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
657 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
658 0x49, 0x43, 0x41, 0x32};
659 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
660 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
661 QCBOREncode_OpenMap(pECtx);
662 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
663 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
664 QCBOREncode_CloseMap(pECtx);
665
666 /* Bool */
667 QCBOREncode_AddBool(pECtx, true);
668 QCBOREncode_AddBool(pECtx, false);
669 QCBOREncode_OpenMap(pECtx);
670 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
671 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
672 QCBOREncode_CloseMap(pECtx);
673
674 /* Big numbers */
675 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
676 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
677 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
678 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
679 QCBOREncode_OpenMap(pECtx);
680 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
681 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
682 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
683 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
684 QCBOREncode_CloseMap(pECtx);
685
686 QCBOREncode_CloseArray(pECtx);
687}
688
689
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300690int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700692 /* Improvement: this test should be broken down into several so it is more
693 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 QCBOREncodeContext ECtx;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800695 UsefulBufC Enc;
696 size_t size;
697 int nReturn;
698 QCBORError uExpectedErr;
699
700 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530702 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800703 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2d493002024-02-01 11:09:17 -0700705 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706
Laurence Lundblade0595e932018-11-02 22:22:47 +0700707 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800708 nReturn = -1;
709 goto Done;
710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800711
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700712 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530713 nReturn = -2;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800714 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700715 }
716
717
718 /* Also test size calculation */
719 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800720 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700721
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800722 AddAll(&ECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700723
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700724 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
725 nReturn = -10;
726 goto Done;
727 }
728
729 if(size != sizeof(spExpectedEncodedAll)) {
730 nReturn = -11;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800731 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800733
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800734#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
735 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
736#else
737 uExpectedErr = QCBOR_SUCCESS;
738#endif
739
740 /* Test the QCBOR_ERR_NOT_ALLOWED error codes */
741 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
742 QCBOREncode_AddNegativeUInt64(&ECtx, 1);
743 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
744 nReturn = -21;
745 goto Done;
746 }
747
748#ifndef USEFULBUF_DISABLE_ALL_FLOAT
749 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
757 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
766 /* 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
780 /* 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
787 /* 0x7ffc0000UL is a NaN with a payload. */
788 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
789 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
790 nReturn = -25;
791 goto Done;
792 }
793#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */
794
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 Lundbladef2f0c3f2024-04-12 13:01:54 -07002531 UsefulBufC EncodedResult;
2532 MakeUsefulBufOnStack(SmallBuffer, 4);
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002533
2534
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002535 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002536 // Do all of these tests with NULL buffers so no actual
2537 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002538 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002539
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002540 // First verify no error from a big buffer
2541 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002542 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002543 // 6 is the CBOR overhead for opening the array and encodng the length
2544 // This exactly fills the buffer.
2545 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002546 QCBOREncode_CloseArray(&EC);
2547 size_t xx;
2548 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2549 return -1;
2550 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002551
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002552 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002553 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002554 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002555 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002556 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002557 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002558 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2559 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002560 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002561 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002562 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002563 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2564 return -2;
2565 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002566
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002567 // Third, fit an array in exactly at max position allowed
2568 QCBOREncode_Init(&EC, Buffer);
2569 QCBOREncode_OpenArray(&EC);
2570 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2571 QCBOREncode_OpenArray(&EC);
2572 QCBOREncode_CloseArray(&EC);
2573 QCBOREncode_CloseArray(&EC);
2574 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2575 return -10;
2576 }
2577
2578
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002579 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2580 // Work close to the 4GB size limit for a better test
2581 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002582 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002583
2584 QCBOREncode_Init(&EC, Large);
2585 QCBOREncode_OpenArray(&EC);
2586 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2587 QCBOREncode_CloseArray(&EC);
2588 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2589 // Making sure it succeeds when it should first
2590 return -3;
2591 }
2592
2593 QCBOREncode_Init(&EC, Large);
2594 QCBOREncode_OpenArray(&EC);
2595 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2596 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2597 QCBOREncode_CloseArray(&EC);
2598 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2599 // Now just 1 byte over, see that it fails
2600 return -4;
2601 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002602
2603
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002604 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2605 QCBOREncode_Init(&EC, Large);
2606 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2607 QCBOREncode_OpenArray(&EC);
2608 }
2609 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2610 QCBOREncode_CloseArray(&EC);
2611 }
2612 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2613 // Making sure it succeeds when it should first
2614 return -5;
2615 }
2616
2617 QCBOREncode_Init(&EC, Large);
2618 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2619 QCBOREncode_OpenArray(&EC);
2620 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002621 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002622 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2623 QCBOREncode_CloseArray(&EC);
2624 }
2625 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002626 return -6;
2627 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002628
2629
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002630 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002631 QCBOREncode_Init(&EC, Large);
2632 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2633 QCBOREncode_OpenArray(&EC);
2634 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002635 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002636 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2637 QCBOREncode_CloseArray(&EC);
2638 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002639 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2640#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2641 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002642 return -7;
2643 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002644#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2645 if(uErr != QCBOR_SUCCESS) {
2646 return -107;
2647 }
2648#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002649
2650
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002651 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002652 QCBOREncode_Init(&EC, Large);
2653 QCBOREncode_OpenArray(&EC);
2654 UsefulBufC Wrap;
2655 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002656 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2657#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2658 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002659 return -8;
2660 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002661#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2662 if(uErr != QCBOR_SUCCESS) {
2663 return -108;
2664 }
2665#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002666
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002667 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002668 QCBOREncode_Init(&EC, Large);
2669 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2670 QCBOREncode_OpenArray(&EC);
2671 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002672 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002673 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2674 QCBOREncode_CloseArray(&EC);
2675 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002676
2677 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2678#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2679 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002680 return -9;
2681 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002682#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2683 if(uErr != QCBOR_SUCCESS) {
2684 return -109;
2685 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002686#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002687
Laurence Lundblade241705e2018-12-30 18:56:14 -08002688 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2689 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002690
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002691
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002692 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002693 QCBOREncode_Init(&EC, Buffer);
2694 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2695 return -11;
2696 }
2697
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002698 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002699 QCBOREncode_Init(&EC, Large);
2700 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002701 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002702 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2703#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2704 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002705 return -12;
2706 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002707#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2708 if(uErr != QCBOR_SUCCESS) {
2709 return -112;
2710 }
2711#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2712
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002713
2714 QCBOREncode_Init(&EC, Large);
2715 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002716 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002717 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2718#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2719 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002720 return -13;
2721 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002722#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2723 if(uErr != QCBOR_SUCCESS) {
2724 return -113;
2725 }
2726#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002727
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002728 /* Test that still-open error sticks */
2729 QCBOREncode_Init(&EC, Large);
2730 QCBOREncode_OpenArray(&EC);
2731 QCBOREncode_Finish(&EC, &EncodedResult);
2732#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2733 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2734 return -120;
2735 }
2736#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2737 if(QCBOREncode_GetErrorState(&EC) != QCBOR_SUCCESS) {
2738 return -122;
2739 }
2740#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2741
2742 /* Test that too-small error is sticky */
2743 QCBOREncode_Init(&EC, SmallBuffer);
2744 QCBOREncode_OpenArray(&EC);
2745 QCBOREncode_AddInt64(&EC, INT64_MAX);
2746 QCBOREncode_AddInt64(&EC, INT64_MAX);
2747 QCBOREncode_AddInt64(&EC, INT64_MAX);
2748 QCBOREncode_CloseArray(&EC);
2749 QCBOREncode_Finish(&EC, &EncodedResult);
2750 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2751 return -130;
2752 }
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002753
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002754 return 0;
2755}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002756
2757
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002758#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002759/*
2760 [
2761 4([-1, 3]),
2762 4([-20, 4759477275222530853136]),
2763 4([9223372036854775807, -4759477275222530853137]),
2764 5([300, 100]),
2765 5([-20, 4759477275222530853136]),
2766 5([-9223372036854775808, -4759477275222530853137])
2767 ]
2768 */
2769static const uint8_t spExpectedExponentAndMantissaArray[] = {
2770 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2771 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2772 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2773 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2774 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2775 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2776 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2777 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2778 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2779 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2780 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2781
2782
2783/*
2784 {
2785 "decimal fraction": 4([-1, 3]),
2786 300: 4([-1, 3]),
2787 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2788 400: 4([2147483647, 4759477275222530853136]),
2789 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2790 500: 4([9223372036854775807, -4759477275222530853137]),
2791 "big float": 5([300, 100]),
2792 600: 5([300, 100]),
2793 "big float bignum positive": 5([-20, 4759477275222530853136]),
2794 700: 5([-20, 4759477275222530853136]),
2795 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2796 800: 5([-9223372036854775808, -4759477275222530853137])
2797 }
2798 */
2799static const uint8_t spExpectedExponentAndMantissaMap[] = {
2800 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2801 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2802 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2803 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2804 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2805 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2806 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2807 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2808 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2809 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2810 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2811 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2812 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2813 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2814 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2815 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2816 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2817 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2818 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2819 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2820 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2821 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2822 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2823 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2824 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2825 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2826 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2827 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2828 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2829 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2830 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2831 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2832 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2833 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2834 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2835 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2836 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2837 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2838 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2839 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2840 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2841 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2842 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2843 0x09, 0x10
2844};
2845
2846
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002847int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002848{
2849 QCBOREncodeContext EC;
2850 UsefulBufC EncodedExponentAndMantissa;
2851
2852 // Constant for the big number used in all the tests.
2853 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2854 0x06, 0x07, 0x08, 0x09, 0x010};
2855 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2856
2857 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2858 QCBOREncode_OpenArray(&EC);
2859 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2860 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2861 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2862 QCBOREncode_AddBigFloat(&EC, 100, 300);
2863 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2864 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2865 QCBOREncode_CloseArray(&EC);
2866
2867 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2868 return -2;
2869 }
2870
2871 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2872 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2873 NULL);
2874 if(nReturn) {
2875 return nReturn;
2876 }
2877
2878 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2879 QCBOREncode_OpenMap(&EC);
2880
2881 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2882
2883 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2884
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002885 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002886 "decimal fraction bignum postive",
2887 BigNum,
2888 false,
2889 -200);
2890
2891 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2892 400,
2893 BigNum,
2894 false,
2895 INT32_MAX);
2896
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002897 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002898 "decimal fraction bignum negative",
2899 BigNum,
2900 true,
2901 INT64_MAX);
2902
2903 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2904 500,
2905 BigNum,
2906 true,
2907 INT64_MAX);
2908
2909 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2910
2911 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2912
2913 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2914 "big float bignum positive",
2915 BigNum,
2916 false,
2917 -20);
2918
2919 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2920 700,
2921 BigNum,
2922 false,
2923 -20);
2924
2925 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2926 "big float bignum negative",
2927 BigNum,
2928 true,
2929 INT64_MIN);
2930
2931 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2932 800,
2933 BigNum,
2934 true,
2935 INT64_MIN);
2936
2937 QCBOREncode_CloseMap(&EC);
2938
2939 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2940 return -3;
2941 }
2942
2943
2944 struct UBCompareDiagnostic Diag;
2945
2946 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2947 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2948 &Diag);
2949 if(nReturn) {
2950 return nReturn + 1000000; // +1000000 to distinguish from first test above
2951 }
2952
2953 return 0;
2954}
2955
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002956#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002957
2958
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002959int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002960{
2961 /* This test doesn't have to be extensive, because just about every
2962 * other test exercises QCBOREncode_EncodeHead().
2963 */
2964 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002965 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002966
2967 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2968 CBOR_MAJOR_TYPE_POSITIVE_INT,
2969 0,
2970 0);
2971
2972 static const uint8_t expectedZero[] = {0x00};
2973
2974 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2975 return -1;
2976 }
2977
2978 // ---- Encode a zero padded out to an 8 byte integer ----
2979 encoded = QCBOREncode_EncodeHead(RightSize,
2980 CBOR_MAJOR_TYPE_POSITIVE_INT,
2981 8, // uMinSize is 8 bytes
2982 0);
2983
2984 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2985 0x00, 0x00, 0x00, 0x00};
2986
2987 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2988 return -2;
2989 }
2990
2991
2992 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002993 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002994
2995 encoded = QCBOREncode_EncodeHead(TooSmall,
2996 CBOR_MAJOR_TYPE_POSITIVE_INT,
2997 0,
2998 0);
2999
3000 if(!UsefulBuf_IsNULLC(encoded)) {
3001 return -3;
3002 }
3003
3004 return 0;
3005}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003006
3007
3008static const uint8_t spExpectedForOpenBytes[] = {
3009 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
3010 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
3011 0x78
3012};
3013
3014static const uint8_t spExpectedForOpenBytes2[] = {
3015 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
3016 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
3017 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
3018};
3019
3020int32_t
3021OpenCloseBytesTest(void)
3022{
3023 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
3024 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
3025 QCBOREncodeContext EC;
3026 UsefulBuf Place;
3027 UsefulBufC Encoded;
3028 QCBORError uErr;
3029
3030 /* Normal use case -- add a byte string that fits */
3031 QCBOREncode_Init(&EC, TestBuf);
3032 QCBOREncode_OpenBytes(&EC, &Place);
3033 if(Place.ptr != TestBuf.ptr ||
3034 Place.len != TestBuf.len) {
3035 return 1;
3036 }
3037 Place.len -= 4;
3038 UsefulBuf_Set(Place, 'x');
3039 QCBOREncode_CloseBytes(&EC, Place.len);
3040 QCBOREncode_Finish(&EC, &Encoded);
3041 if(UsefulBuf_Compare(Encoded,
3042 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
3043 return 2;
3044 }
3045
Paul Liétar79789772022-07-26 20:33:18 +01003046 /* Run the same test but with a NULL buffer */
3047 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3048 QCBOREncode_OpenBytes(&EC, &Place);
3049 if(!UsefulBuf_IsNULL(Place)) {
3050 return 3;
3051 }
3052 Place.len -= 4;
3053 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3054 QCBOREncode_CloseBytes(&EC, Place.len);
3055 uErr = QCBOREncode_Finish(&EC, &Encoded);
3056 if(uErr != QCBOR_SUCCESS ||
3057 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3058 return 4;
3059 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003060
3061 /* Open a byte string with no room left */
3062 QCBOREncode_Init(&EC, TestBuf);
3063 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3064 QCBOREncode_OpenBytes(&EC, &Place);
3065 if(Place.ptr != NULL ||
3066 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003067 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003068 }
3069
3070 /* Try to extend byte string past end of encoding output buffer */
3071 QCBOREncode_Init(&EC, TestBuf);
3072 QCBOREncode_AddSZString(&EC, "012345678901234567");
3073 QCBOREncode_OpenBytes(&EC, &Place);
3074 /* Don't bother to write any bytes*/
3075 QCBOREncode_CloseBytes(&EC, Place.len+1);
3076 uErr = QCBOREncode_GetErrorState(&EC);
3077 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003078 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003079 }
3080
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003081 /* Close a byte string without opening one. */
3082 QCBOREncode_Init(&EC, TestBuf);
3083 QCBOREncode_AddSZString(&EC, "012345678");
3084 QCBOREncode_CloseBytes(&EC, 1);
3085 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003086#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003087 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003088 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003089 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003090#else
3091 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003092 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003093 }
3094#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003095
3096 /* Forget to close a byte string */
3097 QCBOREncode_Init(&EC, TestBuf);
3098 QCBOREncode_AddSZString(&EC, "012345678");
3099 QCBOREncode_OpenBytes(&EC, &Place);
3100 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003101#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003102 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003103 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003104 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003105#else
3106 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003107 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003108 }
3109#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003110
3111 /* Try to open a byte string in a byte string */
3112 QCBOREncode_Init(&EC, TestBuf);
3113 QCBOREncode_AddSZString(&EC, "012345678");
3114 QCBOREncode_OpenBytes(&EC, &Place);
3115 QCBOREncode_OpenBytes(&EC, &Place);
3116 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003117#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003118 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003119 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003120 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003121#else
3122 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003123 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003124 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003125#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3126
3127 /* A successful case with a little complexity */
3128 QCBOREncode_Init(&EC, TestBuf2);
3129 QCBOREncode_OpenMap(&EC);
3130 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3131 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3132 Place.len = 2;
3133 UsefulBuf_Set(Place, 'x');
3134 QCBOREncode_CloseBytes(&EC, 2);
3135 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3136 Place.len = 3;
3137 UsefulBuf_Set(Place, 'y');
3138 QCBOREncode_CloseBytes(&EC, 3);
3139 QCBOREncode_OpenArrayInMapN(&EC, 40);
3140 QCBOREncode_OpenBytes(&EC, &Place);
3141 QCBOREncode_CloseBytes(&EC, 0);
3142 QCBOREncode_CloseArray(&EC);
3143 QCBOREncode_CloseMap(&EC);
3144 uErr = QCBOREncode_Finish(&EC, &Encoded);
3145 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003146 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003147 }
3148 if(UsefulBuf_Compare(Encoded,
3149 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003150 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003151 }
3152
3153 return 0;
3154}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003155
3156
3157
3158int32_t
3159SortMapTest(void)
3160{
3161 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3162 QCBOREncodeContext EC;
3163 UsefulBufC EncodedAndSorted;
3164 QCBORError uErr;
3165 struct UBCompareDiagnostic CompareDiagnostics;
3166
3167
3168 /* --- Basic sort test case --- */
3169 QCBOREncode_Init(&EC, TestBuf);
3170 QCBOREncode_OpenMap(&EC);
3171 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3172 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3173 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3174 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3175 QCBOREncode_CloseAndSortMap(&EC);
3176 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3177 if(uErr) {
3178 return 11;
3179 }
3180
3181 static const uint8_t spBasic[] = {
3182 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3183
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003184 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003185 return 12;
3186 }
3187
3188 /* --- Empty map sort test case --- */
3189 QCBOREncode_Init(&EC, TestBuf);
3190 QCBOREncode_OpenMap(&EC);
3191 QCBOREncode_CloseAndSortMap(&EC);
3192 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3193 if(uErr) {
3194 return 21;
3195 }
3196
3197 static const uint8_t spEmpty[] = {0xA0};
3198 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3199 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3200 &CompareDiagnostics)) {
3201 return 22;
3202 }
3203
3204 /* --- Several levels of nested sorted maps --- */
3205 QCBOREncode_Init(&EC, TestBuf);
3206 QCBOREncode_OpenMap(&EC);
3207 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3208 QCBOREncode_OpenMapInMapN(&EC, 428);
3209 QCBOREncode_AddNULLToMap(&EC, "null");
3210 QCBOREncode_OpenArrayInMap(&EC, "array");
3211 QCBOREncode_AddSZString(&EC, "hi");
3212 QCBOREncode_AddSZString(&EC, "there");
3213 QCBOREncode_CloseArray(&EC);
3214 QCBOREncode_OpenMapInMap(&EC, "empty2");
3215 QCBOREncode_CloseAndSortMap(&EC);
3216 QCBOREncode_OpenMapInMap(&EC, "empty1");
3217 QCBOREncode_CloseAndSortMap(&EC);
3218 QCBOREncode_CloseAndSortMap(&EC);
3219 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3220 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3221 QCBOREncode_CloseAndSortMap(&EC);
3222 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3223 if(uErr) {
3224 return 31;
3225 }
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003226
3227 /* Correctly sorted.
3228 * {
3229 * 88: 1(888888),
3230 * 428: {
3231 * "null": null,
3232 * "array": [
3233 * "hi",
3234 * "there"
3235 * ],
3236 * "empty1": {},
3237 * "empty2": {}
3238 * },
3239 * "boo": true,
3240 * "three": 3
3241 * }
3242 */
3243 static const uint8_t spSorted[] = {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003244 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3245 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3246 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3247 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3248 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3249 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3250 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3251 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3252
3253 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003254 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003255 &CompareDiagnostics)) {
3256 return 32;
3257 }
3258
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003259
3260 /* Same data items, but added in a different order */
3261 QCBOREncode_Init(&EC, TestBuf);
3262 QCBOREncode_OpenMap(&EC);
3263 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3264 QCBOREncode_OpenMapInMapN(&EC, 428);
3265 QCBOREncode_OpenMapInMap(&EC, "empty1");
3266 QCBOREncode_CloseAndSortMap(&EC);
3267 QCBOREncode_OpenArrayInMap(&EC, "array");
3268 QCBOREncode_AddSZString(&EC, "hi");
3269 QCBOREncode_AddSZString(&EC, "there");
3270 QCBOREncode_CloseArray(&EC);
3271 QCBOREncode_OpenMapInMap(&EC, "empty2");
3272 QCBOREncode_CloseAndSortMap(&EC);
3273 QCBOREncode_AddNULLToMap(&EC, "null");
3274 QCBOREncode_CloseAndSortMap(&EC);
3275 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3276 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3277 QCBOREncode_CloseAndSortMap(&EC);
3278 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3279 if(uErr) {
3280 return 31;
3281 }
3282
3283 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3284 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3285 &CompareDiagnostics)) {
3286 return 32;
3287 }
3288
3289 /* Same data items, but added in a different order */
3290 QCBOREncode_Init(&EC, TestBuf);
3291 QCBOREncode_OpenMap(&EC);
3292 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3293 QCBOREncode_OpenMapInMapN(&EC, 428);
3294 QCBOREncode_OpenMapInMap(&EC, "empty1");
3295 QCBOREncode_CloseAndSortMap(&EC);
3296 QCBOREncode_OpenArrayInMap(&EC, "array");
3297 QCBOREncode_AddSZString(&EC, "hi");
3298 QCBOREncode_AddSZString(&EC, "there");
3299 QCBOREncode_CloseArray(&EC);
3300 QCBOREncode_OpenMapInMap(&EC, "empty2");
3301 QCBOREncode_CloseAndSortMap(&EC);
3302 QCBOREncode_AddNULLToMap(&EC, "null");
3303 QCBOREncode_CloseAndSortMap(&EC);
3304 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3305 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3306 QCBOREncode_CloseAndSortMap(&EC);
3307 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3308 if(uErr) {
3309 return 31;
3310 }
3311
3312 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3313 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3314 &CompareDiagnostics)) {
3315 return 32;
3316 }
3317
3318
3319
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003320 /* --- Degenerate case of everything in order --- */
3321 QCBOREncode_Init(&EC, TestBuf);
3322 QCBOREncode_OpenMap(&EC);
3323 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3324 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3325 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3326 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3327 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3328 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3329 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3330 QCBOREncode_CloseAndSortMap(&EC);
3331 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3332 if(uErr) {
3333 return 41;
3334 }
3335
3336 static const uint8_t sp6Items[] = {
3337 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3338 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3339 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3340 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3341 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3342 &CompareDiagnostics)) {
3343 return 42;
3344 }
3345
3346 /* --- Degenerate case -- reverse order --- */
3347 QCBOREncode_Init(&EC, TestBuf);
3348 QCBOREncode_OpenMap(&EC);
3349 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3350 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3351 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3352 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3353 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3354 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3355 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3356 QCBOREncode_CloseAndSortMap(&EC);
3357 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3358 if(uErr) {
3359 return 51;
3360 }
3361
3362 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3363 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3364 &CompareDiagnostics)) {
3365 return 52;
3366 }
3367
3368 /* --- Same items, randomly out of order --- */
3369 QCBOREncode_Init(&EC, TestBuf);
3370 QCBOREncode_OpenMap(&EC);
3371 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3372 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3373 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3374 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3375 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3376 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3377 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3378 QCBOREncode_CloseAndSortMap(&EC);
3379 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3380 if(uErr) {
3381 return 61;
3382 }
3383
3384 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3385 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3386 &CompareDiagnostics)) {
3387 return 62;
3388 }
3389
3390 /* --- Stuff in front of and after array to sort --- */
3391 QCBOREncode_Init(&EC, TestBuf);
3392 QCBOREncode_OpenArray(&EC);
3393 QCBOREncode_AddInt64(&EC, 111);
3394 QCBOREncode_AddInt64(&EC, 222);
3395 QCBOREncode_OpenMap(&EC);
3396 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3397 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3398 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3399 QCBOREncode_CloseAndSortMap(&EC);
3400 QCBOREncode_AddInt64(&EC, 888);
3401 QCBOREncode_AddInt64(&EC, 999);
3402 QCBOREncode_CloseArray(&EC);
3403 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3404 if(uErr) {
3405 return 71;
3406 }
3407
3408 static const uint8_t spPreItems[] = {
3409 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3410 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3411 0x03, 0xE7};
3412 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3413 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3414 &CompareDiagnostics)) {
3415 return 72;
3416 }
3417
3418 /* --- map with labels of all CBOR major types and in reverse order --- */
3419 QCBOREncode_Init(&EC, TestBuf);
3420 QCBOREncode_OpenMap(&EC);
3421
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003422 /* Adding labels directly rather than AddToMap functions */
3423
3424#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003425 QCBOREncode_AddDouble(&EC, 8.77);
3426 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003427#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003428
3429 QCBOREncode_AddBool(&EC, true);
3430 QCBOREncode_AddInt64(&EC, 6);
3431
3432 QCBOREncode_AddDateEpoch(&EC, 88);
3433 QCBOREncode_AddInt64(&EC, 5);
3434
3435 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3436 QCBOREncode_AddInt64(&EC, 4);
3437
3438 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3439 QCBOREncode_AddInt64(&EC, 7);
3440
3441 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3442
3443 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3444 QCBOREncode_AddInt64(&EC, 2);
3445
3446 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3447 QCBOREncode_CloseAndSortMap(&EC);
3448
3449 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3450 if(uErr) {
3451 return 81;
3452 }
3453
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003454#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003455 static const uint8_t spLabelTypes[] = {
3456 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3457 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3458 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3459 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3460 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003461#else
3462 static const uint8_t spLabelTypes[] = {
3463 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3464 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3465 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3466#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3467
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003468 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3469 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3470 &CompareDiagnostics)) {
3471 return 82;
3472 }
3473
3474 /* --- labels are indefinitely encoded --- */
3475 QCBOREncode_Init(&EC, TestBuf);
3476 QCBOREncode_OpenMap(&EC);
3477
3478 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3479
3480 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3481
3482 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3483 QCBOREncode_AddInt64(&EC, 3);
3484
3485 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3486 QCBOREncode_AddInt64(&EC, 4);
3487
3488 QCBOREncode_CloseAndSortMap(&EC);
3489
3490 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3491 if(uErr) {
3492 return 91;
3493 }
3494
3495 static const uint8_t spIndefItems[] = {
3496 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3497 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3498 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3499 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3500 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3501 &CompareDiagnostics)) {
3502 return 92;
3503 }
3504
3505 /* --- Indefinitely encoded maps --- */
3506 QCBOREncode_Init(&EC, TestBuf);
3507 QCBOREncode_OpenMapIndefiniteLength(&EC);
3508
3509 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3510 QCBOREncode_CloseMapIndefiniteLength(&EC);
3511
3512 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3513 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3514
3515 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3516 QCBOREncode_CloseMapIndefiniteLength(&EC);
3517
3518 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3519 QCBOREncode_CloseMapIndefiniteLength(&EC);
3520
3521 QCBOREncode_CloseAndSortMapIndef(&EC);
3522 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3523 if(uErr) {
3524 return 101;
3525 }
3526
3527 static const uint8_t spIndeMaps[] = {
3528 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3529 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3530 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3531 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3532 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3533 &CompareDiagnostics)) {
3534 return 102;
3535 }
3536
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003537
3538 /* --- Duplicate label test --- */
3539 QCBOREncode_Init(&EC, TestBuf);
3540 QCBOREncode_OpenMap(&EC);
3541 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3542 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3543 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3544 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3545 QCBOREncode_CloseAndSortMap(&EC);
3546 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3547 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3548 return 114;
3549 }
3550
3551 QCBOREncode_Init(&EC, TestBuf);
3552 QCBOREncode_OpenMap(&EC);
3553 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3554 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3555 QCBOREncode_AddInt64ToMapN(&EC, 1, 2);
3556 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3557 QCBOREncode_CloseAndSortMap(&EC);
3558 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3559 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3560 return 115;
3561 }
3562
3563 QCBOREncode_Init(&EC, TestBuf);
3564 QCBOREncode_OpenMap(&EC);
3565 QCBOREncode_AddInt64ToMap(&EC, "abc", 3);
3566 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3567 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3568 QCBOREncode_AddInt64ToMap(&EC, "def", 2);
3569 QCBOREncode_CloseAndSortMap(&EC);
3570 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3571 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3572 return 116;
3573 }
3574
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003575 return 0;
3576}
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003577
3578
3579#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
3580
3581#include <math.h> /* For INFINITY and NAN and isnan() */
3582
3583
3584/* Public function. See qcbor_encode_tests.h */
3585int32_t CDETest(void)
3586{
3587 QCBOREncodeContext EC;
3588 UsefulBufC Encoded;
3589 QCBORError uExpectedErr;
3590
3591 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3592
3593 QCBOREncode_SerializationCDE(&EC);
3594
3595 /* Items added to test sorting and preferred encoding of numbers and floats */
3596 QCBOREncode_OpenMap(&EC);
3597 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3598 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3599 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3600 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3601 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3602 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3603
3604 QCBOREncode_CloseMap(&EC);
3605
3606 uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
3607 if(uExpectedErr != QCBOR_SUCCESS) {
3608 return 2;
3609 }
3610
3611 static const uint8_t spExpectedCDE[] = {
3612 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3613 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3614 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3615 0x61, 0x78, 0xF9, 0x40, 0x00};
3616
3617 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3618 Encoded)) {
3619 return 1;
3620 }
3621
3622
3623#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3624 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3625#else
3626 uExpectedErr = QCBOR_SUCCESS;
3627#endif
3628
3629
3630 /* Next, make sure methods that encode non-CDE error out */
3631 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3632 QCBOREncode_SerializationCDE(&EC);
3633 QCBOREncode_OpenMapIndefiniteLength(&EC);
3634 QCBOREncode_CloseMap(&EC);
3635 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3636 return 100;
3637 }
3638
3639
3640 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3641 QCBOREncode_SerializationCDE(&EC);
3642 QCBOREncode_AddDoubleNoPreferred(&EC, 0);
3643 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3644 return 101;
3645 }
3646
3647 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3648 QCBOREncode_SerializationCDE(&EC);
3649 QCBOREncode_AddFloatNoPreferred(&EC, 0);
3650 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3651 return 101;
3652 }
3653
3654 return 0;
3655}
3656
3657/* Public function. See qcbor_encode_tests.h */
3658int32_t DCBORTest(void)
3659{
3660 QCBOREncodeContext EC;
3661 UsefulBufC Encoded;
3662 QCBORError uExpectedErr;
3663
3664 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3665
3666 QCBOREncode_SerializationdCBOR(&EC);
3667
3668 /* Items added to test sorting and preferred encoding of numbers and floats */
3669 QCBOREncode_OpenMap(&EC);
3670 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3671 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3672 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3673 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3674 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3675
3676 QCBOREncode_CloseMap(&EC);
3677
3678 QCBOREncode_Finish(&EC, &Encoded);
3679
3680 static const uint8_t spExpecteddCBOR[] = {
3681 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3682 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3683 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3684
3685 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3686 Encoded)) {
3687 return 1;
3688 }
3689
3690
3691#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3692 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3693#else
3694 uExpectedErr = QCBOR_SUCCESS;
3695#endif
3696
3697 /* Next, make sure methods that encode of non-CDE error out */
3698
3699 /* Indefinite-length map */
3700 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3701 QCBOREncode_SerializationdCBOR(&EC);
3702 QCBOREncode_OpenMapIndefiniteLength(&EC);
3703 QCBOREncode_CloseMap(&EC);
3704 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3705 return 100;
3706 }
3707
3708 /* Indefinite-length array */
3709 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3710 QCBOREncode_SerializationdCBOR(&EC);
3711 QCBOREncode_OpenArrayIndefiniteLength(&EC);
3712 QCBOREncode_CloseMap(&EC);
3713 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3714 return 101;
3715 }
3716
3717 /* The "undef" special value */
3718 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3719 QCBOREncode_SerializationdCBOR(&EC);
3720 QCBOREncode_AddUndef(&EC);
3721 QCBOREncode_CloseMap(&EC);
3722 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3723 return 102;
3724 }
3725
3726 /* 65-bit negative integers */
3727 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3728 QCBOREncode_SerializationdCBOR(&EC);
3729 QCBOREncode_AddNegativeUInt64(&EC, 1);
3730 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3731 return 103;
3732 }
3733
3734 /* Improvement: when indefinite length string encoding is supported
3735 * test it here too. */
3736
3737 return 0;
3738
3739}
3740#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */