blob: 1c0902b97d1ee0c95e4009e1cf914a640f66dda6 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundblade70fc1252024-05-31 10:57:28 -070051#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070076 if(((const uint8_t *)U1.ptr)[i] != ((const uint8_t *)U2.ptr)[i]) {
escherstairb5563422023-02-13 18:36:45 +010077 printf("Position: %u Actual: 0x%x Expected: 0x%x\n",
Laurence Lundbladeee851742020-01-08 08:37:05 -080078 (uint32_t)i,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070079 ((const uint8_t *)U1.ptr)[i],
80 ((const uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081 return 1;
82 }
83 }
84 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086}
87
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070088#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053089 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070091#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080094 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
95
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070096#endif
97
98
Laurence Lundblade59289e52019-12-30 13:44:37 -080099/*
100 Returns 0 if UsefulBufs are equal
101 Returns 1000000 + offeset if they are not equal.
Laurence Lundblade59289e52019-12-30 13:44:37 -0800102*/
103struct UBCompareDiagnostic {
104 uint8_t uActual;
105 uint8_t uExpected;
106 size_t uOffset;
107};
108
Laurence Lundbladeee851742020-01-08 08:37:05 -0800109static int32_t
110UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
111 UsefulBufC Expected,
112 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800113 size_t i;
114 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800115 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800116 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800117 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
118 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800119 pDiag->uOffset = i;
120 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800121 // Cast to int is OK as this is only a diagnostic and the sizes
122 // here are never over a few KB.
123 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800124 }
125 }
126 return 0;
127
128}
Laurence Lundblade59289e52019-12-30 13:44:37 -0800129
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700130
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530131// One big buffer that is used by all the tests to encode into
132// Putting it in uninitialized data is better than using a lot
133// of stack. The tests should run on small devices too.
134static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135
136
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800137
138/*
139 Some very minimal tests.
140 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300141int32_t BasicEncodeTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142{
143 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800144 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800145
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530146 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530147
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800148 QCBOREncode_OpenMap(&EC);
149 QCBOREncode_AddBoolToMapN(&EC, 66, true);
150 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
157
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800158 // Decode it and see that is right
159 QCBORDecodeContext DC;
160 QCBORItem Item;
161 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 QCBORDecode_GetNext(&DC, &Item);
164 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530165 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800168 QCBORDecode_GetNext(&DC, &Item);
169 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530174 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
177
Laurence Lundbladeee851742020-01-08 08:37:05 -0800178 // Make another encoded message with the CBOR from the previous
179 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530180 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800181 QCBOREncode_Init(&EC, MemoryForEncoded2);
182 QCBOREncode_OpenArray(&EC);
183 QCBOREncode_AddUInt64(&EC, 451);
184 QCBOREncode_AddEncoded(&EC, Encoded);
185 QCBOREncode_OpenMap(&EC);
186 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
187 QCBOREncode_CloseMap(&EC);
188 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700191 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530192 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800193 }
194 /*
195 [ // 0 1:3
196 451, // 1 1:2
197 { // 1 1:2 2:1
198 66: true // 2 1:1
199 },
200 { // 1 1:1 2:1
201 -70000: { // 2 1:1 2:1 3:1
202 66: true // 3 XXXXXX
203 }
204 }
205 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800209 83 # array(3)
210 19 01C3 # unsigned(451)
211 A1 # map(1)
212 18 42 # unsigned(66)
213 F5 # primitive(21)
214 A1 # map(1)
215 3A 0001116F # negative(69999)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800220
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221 // Decode it and see if it is OK
222 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800224 // 0 1:3
225 QCBORDecode_GetNext(&DC, &Item);
226 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530227 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 // 1 1:2
231 QCBORDecode_GetNext(&DC, &Item);
232 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530233 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800236 // 1 1:2 2:1
237 QCBORDecode_GetNext(&DC, &Item);
238 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530239 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800242 // 2 1:1
243 QCBORDecode_GetNext(&DC, &Item);
244 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530245 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800248 // 1 1:1 2:1
249 QCBORDecode_GetNext(&DC, &Item);
250 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530251 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800254 // 2 1:1 2:1 3:1
255 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800256 if(Item.uDataType != QCBOR_TYPE_MAP ||
257 Item.val.uCount != 1 ||
258 Item.uLabelType != QCBOR_TYPE_INT64 ||
259 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800263 // 3 XXXXXX
264 QCBORDecode_GetNext(&DC, &Item);
265 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530266 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800269 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530270 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800273 return 0;
274}
275
276
277
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530278static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade2d493002024-02-01 11:09:17 -0700279 0x98, 0x24, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530280 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
281 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
Laurence Lundblade2d493002024-02-01 11:09:17 -0700282 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0x20, 0xa4, 0x63, 0x4c, 0x42,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530283 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
284 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
285 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
286 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
287 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700288 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
290 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
291 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
292 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
293 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700294 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
295 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00,
296 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40,
297 0x66, 0x62,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530298 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
299 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
300 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700301 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530302 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
303 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
304 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
305 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
306 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
307 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
308 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
309 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
310 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
311 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
312 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
313 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
314 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
315 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700316 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
317 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530318 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
319 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
320 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
321 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
322 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
323 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
324 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
325 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
326 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
327 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
328 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
329 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
330 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
331 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
332 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
333 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
334 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
335 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
336 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
337 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
338 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
339 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
340 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
341 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
342 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
343 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
344 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
345 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
346 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
347 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
348 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
349 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
350 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
351 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
352 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
353 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
354 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
355 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
356 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
357 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
358 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
359 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
360 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
361 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
362 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
363 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
364 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
365 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
366 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
367 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
368 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
369 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
370 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
371 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
372 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
373 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
374 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
375 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700376 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530377 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
378 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
379 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
380 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
381 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
383 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
384 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
385 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
386 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
387 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
388 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
389 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
390 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
391 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
392 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
393 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
394 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
395 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
396 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
397 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
398 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
399 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
400 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
401 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
402 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
403 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
404 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
405 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
406 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
407 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
408 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
409 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
410 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700411 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530412 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
413 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
414 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
415 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
416 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
417 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
418 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
419 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
420 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
421 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
422 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
423 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
424 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
425 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
426 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
427 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
428 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
429 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
430 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
431 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
432 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
433 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
434 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
435 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
436 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
437 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
438 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
439 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
440 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
441 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
442 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
443 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
444 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
445 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
446 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
447 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
448 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
449 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
450 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
451 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
452 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
453 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
454 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
455 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
456 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
457 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
458 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
459 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
460 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
461 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
462 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
463 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
464 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
465 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
466 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
467 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
468 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
469 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
470 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
471 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
472 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
473 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
474 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
475 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
476 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
477 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
478 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
479 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
480 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
481 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
484 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
485 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487};
488
489
490static const char *szMIME = "\
491MIME-Version: 1.0\n\
492Content-Type: multipart/mixed;\n\
493boundary=\"XXXXboundary text\"\n\
494\n\
495This is a multipart message in MIME format.\n\
496\n\
497--XXXXboundary text\n\
498Content-Type: text/plain\n\
499\n\
500this is the body text\n\
501\n\
502--XXXXboundary text\n\
503Content-Type: text/plain;\n\
504Content-Disposition: attachment;\n\
505filename=\"test.txt\"\n\
506\n\
507this is the attachment text\n\
508\n\
509--XXXXboundary text--";
510
511
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700512static void AddAll(QCBOREncodeContext *pECtx)
513{
514 QCBOREncode_OpenArray(pECtx);
515
516 /* Some ints that are tagged and have strings preceeding them
517 * (not labels becase it is not a map) */
518 QCBOREncode_AddSZString(pECtx, "UINT62");
519 QCBOREncode_AddTag(pECtx, 100);
520 QCBOREncode_AddUInt64(pECtx, 89989909);
521 QCBOREncode_AddSZString(pECtx, "INT64");
522 QCBOREncode_AddTag(pECtx, 76);
523 QCBOREncode_AddInt64(pECtx, 77689989909);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700524 QCBOREncode_AddUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700525 QCBOREncode_AddInt64(pECtx, -44);
Laurence Lundblade2d493002024-02-01 11:09:17 -0700526 QCBOREncode_AddNegativeUInt64(pECtx, 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700527
528 /* ints that go in maps */
529 QCBOREncode_OpenMap(pECtx);
530 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
531 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
532 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
533 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
534 QCBOREncode_CloseMap(pECtx);
535
536 /* Epoch Date */
537 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
538
539 /* Epoch date with labels */
540 QCBOREncode_OpenMap(pECtx);
541 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
542 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
543 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
544 QCBOREncode_CloseMap(pECtx);
545
546 /* Binary blobs */
547 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
548
549 /* binary blobs in maps */
550 QCBOREncode_OpenMap(pECtx);
551 QCBOREncode_AddSZString(pECtx, "binbin");
552 QCBOREncode_AddTag(pECtx, 100000);
553 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700554 QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700555 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
556 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
557 QCBOREncode_CloseMap(pECtx);
558
559 /* text blobs */
560 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
561 QCBOREncode_AddSZString(pECtx, "oof\n");
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700562 QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string
563
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700564 const char *szURL =
565 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
566 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
567 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
568 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
569 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
570
571 /* text blobs in maps */
572 QCBOREncode_OpenMap(pECtx);
573 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
574 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
575 QCBOREncode_AddSZString(pECtx, "()()()");
576 QCBOREncode_AddTag(pECtx, 1000);
577 QCBOREncode_AddSZString(pECtx, "rab rab oof");
578 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
579 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
580 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
581 QCBOREncode_AddURIToMap(pECtx,
582 "RFC",
583 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
584 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
585 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
586 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
587 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
588 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
589 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
590 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
591 QCBOREncode_CloseMap(pECtx);
592
593 /* Date strings */
594 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
595 QCBOREncode_OpenMap(pECtx);
596 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
597 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
598 QCBOREncode_CloseMap(pECtx);
599
600 /* true / false ... */
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700601 QCBOREncode_AddUndef(pECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700602 QCBOREncode_OpenMap(pECtx);
603 QCBOREncode_AddSZString(pECtx, "dare");
604 QCBOREncode_AddTag(pECtx, 66);
605 QCBOREncode_AddBool(pECtx, true);
606 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700607 QCBOREncode_AddNULLToMapN(pECtx, 737634);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700608 QCBOREncode_CloseMap(pECtx);
609
610 /* opening an array */
611 QCBOREncode_OpenArray(pECtx);
612 QCBOREncode_CloseArray(pECtx);
613
614 /* opening arrays in a map */
615 QCBOREncode_OpenMap(pECtx);
616 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
617 QCBOREncode_AddTag(pECtx, 1093);
618 QCBOREncode_OpenArray(pECtx);
619 QCBOREncode_CloseArray(pECtx);
620 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
621 QCBOREncode_CloseArray(pECtx);
622 QCBOREncode_OpenArrayInMapN(pECtx, 42);
623 QCBOREncode_CloseArray(pECtx);
624 QCBOREncode_CloseMap(pECtx);
625
626 /* opening maps with labels and tagging */
627 QCBOREncode_OpenMap(pECtx);
628 QCBOREncode_OpenMapInMap(pECtx, "in a map");
629 QCBOREncode_OpenMapInMapN(pECtx, 5556);
630 QCBOREncode_AddSZString(pECtx, "in a in a in a");
631 QCBOREncode_AddTag(pECtx, 9087);
632 QCBOREncode_OpenMap(pECtx);
633 QCBOREncode_CloseMap(pECtx);
634 QCBOREncode_CloseMap(pECtx);
635 QCBOREncode_CloseMap(pECtx);
636 QCBOREncode_CloseMap(pECtx);
637
638 /* Extended simple values (these are not standard...) */
639 QCBOREncode_OpenMap(pECtx);
640 QCBOREncode_AddSZString(pECtx, "s1");
641 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700642 QCBOREncode_AddSimple(pECtx, 255);
643 QCBOREncode_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700644 QCBOREncode_AddSZString(pECtx, "s3");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700646 QCBOREncode_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700647 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
648 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700649 QCBOREncode_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700650 QCBOREncode_AddInt64(pECtx, 89); // label before tag
651 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700652 QCBOREncode_AddSimple(pECtx, 19);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700653 QCBOREncode_CloseMap(pECtx);
654
655 /* UUIDs */
656 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
657 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
658 0x49, 0x43, 0x41, 0x32};
659 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
660 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
661 QCBOREncode_OpenMap(pECtx);
662 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
663 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
664 QCBOREncode_CloseMap(pECtx);
665
666 /* Bool */
667 QCBOREncode_AddBool(pECtx, true);
668 QCBOREncode_AddBool(pECtx, false);
669 QCBOREncode_OpenMap(pECtx);
670 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
671 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
672 QCBOREncode_CloseMap(pECtx);
673
674 /* Big numbers */
675 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
676 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
677 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
678 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
679 QCBOREncode_OpenMap(pECtx);
680 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
681 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
682 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
683 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
684 QCBOREncode_CloseMap(pECtx);
685
686 QCBOREncode_CloseArray(pECtx);
687}
688
689
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300690int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700692 /* Improvement: this test should be broken down into several so it is more
693 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 QCBOREncodeContext ECtx;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800695 UsefulBufC Enc;
696 size_t size;
697 int nReturn;
698 QCBORError uExpectedErr;
699
700 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530702 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800703 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2d493002024-02-01 11:09:17 -0700705 AddAll(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706
Laurence Lundblade0595e932018-11-02 22:22:47 +0700707 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800708 nReturn = -1;
709 goto Done;
710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800711
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700712 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530713 nReturn = -2;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800714 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700715 }
716
717
718 /* Also test size calculation */
719 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800720 QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700721
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800722 AddAll(&ECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700723
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700724 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
725 nReturn = -10;
726 goto Done;
727 }
728
729 if(size != sizeof(spExpectedEncodedAll)) {
730 nReturn = -11;
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800731 goto Done;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800733
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800734#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
735 uExpectedErr = QCBOR_ERR_NOT_ALLOWED;
736#else
737 uExpectedErr = QCBOR_SUCCESS;
738#endif
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
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800757
758 /* 0x7ffc000000000000ULL is a NaN with a payload. */
759 QCBOREncode_AddDouble(&ECtx, UsefulBufUtil_CopyUint64ToDouble(0x7ff8000000000001ULL));
760 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
761 nReturn = -23;
762 goto Done;
763 }
764
765 /* 0x7ff80001UL is a NaN with a payload. */
766 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ff80001UL));
767 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
768 nReturn = -24;
769 goto Done;
770 }
771
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800772 /* 0x7ffc0000UL is a NaN with a payload. */
773 QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
774 if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
775 nReturn = -25;
776 goto Done;
777 }
778#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */
779
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800780Done:
781 return nReturn;
782}
783
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700784
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530785/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200786 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530787 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
788 3B 0000000100000000 # negative(4294967296)
789 3A FFFFFFFF # negative(4294967295)
790 3A FFFFFFFE # negative(4294967294)
791 3A FFFFFFFD # negative(4294967293)
792 3A 7FFFFFFF # negative(2147483647)
793 3A 7FFFFFFE # negative(2147483646)
794 3A 00010001 # negative(65537)
795 3A 00010000 # negative(65536)
796 39 FFFF # negative(65535)
797 39 FFFE # negative(65534)
798 39 FFFD # negative(65533)
799 39 0100 # negative(256)
800 38 FF # negative(255)
801 38 FE # negative(254)
802 38 FD # negative(253)
803 38 18 # negative(24)
804 37 # negative(23)
805 36 # negative(22)
806 20 # negative(0)
807 00 # unsigned(0)
808 00 # unsigned(0)
809 01 # unsigned(1)
810 16 # unsigned(22)
811 17 # unsigned(23)
812 18 18 # unsigned(24)
813 18 19 # unsigned(25)
814 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200815 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530816 18 FE # unsigned(254)
817 18 FF # unsigned(255)
818 19 0100 # unsigned(256)
819 19 0101 # unsigned(257)
820 19 FFFE # unsigned(65534)
821 19 FFFF # unsigned(65535)
822 1A 00010000 # unsigned(65536)
823 1A 00010001 # unsigned(65537)
824 1A 00010002 # unsigned(65538)
825 1A 7FFFFFFF # unsigned(2147483647)
826 1A 7FFFFFFF # unsigned(2147483647)
827 1A 80000000 # unsigned(2147483648)
828 1A 80000001 # unsigned(2147483649)
829 1A FFFFFFFE # unsigned(4294967294)
830 1A FFFFFFFF # unsigned(4294967295)
831 1B 0000000100000000 # unsigned(4294967296)
832 1B 0000000100000001 # unsigned(4294967297)
833 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
834 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
835 */
836static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200837 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800838 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
839 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
840 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
841 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
842 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
843 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
844 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
845 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
846 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
847 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200848 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
849 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
850 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
851 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
852 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
853 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
854 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
855 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
856 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
857 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
858 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
859 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
860 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800861
862/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800863
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800864 Test the generation of integers. This also ends up testing
865 encoding of all the different lengths. It encodes integers
866 of many lengths and values, especially around the boundaries
867 for different types of integers. It compares the output
868 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300871int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800872{
873 QCBOREncodeContext ECtx;
874 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800875
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530876 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800877 QCBOREncode_OpenArray(&ECtx);
878
879 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
880 QCBOREncode_AddInt64(&ECtx, -4294967297);
881 QCBOREncode_AddInt64(&ECtx, -4294967296);
882 QCBOREncode_AddInt64(&ECtx, -4294967295);
883 QCBOREncode_AddInt64(&ECtx, -4294967294);
884 QCBOREncode_AddInt64(&ECtx, -2147483648);
885 QCBOREncode_AddInt64(&ECtx, -2147483647);
886 QCBOREncode_AddInt64(&ECtx, -65538);
887 QCBOREncode_AddInt64(&ECtx, -65537);
888 QCBOREncode_AddInt64(&ECtx, -65536);
889 QCBOREncode_AddInt64(&ECtx, -65535);
890 QCBOREncode_AddInt64(&ECtx, -65534);
891 QCBOREncode_AddInt64(&ECtx, -257);
892 QCBOREncode_AddInt64(&ECtx, -256);
893 QCBOREncode_AddInt64(&ECtx, -255);
894 QCBOREncode_AddInt64(&ECtx, -254);
895 QCBOREncode_AddInt64(&ECtx, -25);
896 QCBOREncode_AddInt64(&ECtx, -24);
897 QCBOREncode_AddInt64(&ECtx, -23);
898 QCBOREncode_AddInt64(&ECtx, -1);
899 QCBOREncode_AddInt64(&ECtx, 0);
900 QCBOREncode_AddUInt64(&ECtx, 0ULL);
901 QCBOREncode_AddInt64(&ECtx, 1);
902 QCBOREncode_AddInt64(&ECtx, 22);
903 QCBOREncode_AddInt64(&ECtx, 23);
904 QCBOREncode_AddInt64(&ECtx, 24);
905 QCBOREncode_AddInt64(&ECtx, 25);
906 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200907 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908 QCBOREncode_AddInt64(&ECtx, 254);
909 QCBOREncode_AddInt64(&ECtx, 255);
910 QCBOREncode_AddInt64(&ECtx, 256);
911 QCBOREncode_AddInt64(&ECtx, 257);
912 QCBOREncode_AddInt64(&ECtx, 65534);
913 QCBOREncode_AddInt64(&ECtx, 65535);
914 QCBOREncode_AddInt64(&ECtx, 65536);
915 QCBOREncode_AddInt64(&ECtx, 65537);
916 QCBOREncode_AddInt64(&ECtx, 65538);
917 QCBOREncode_AddInt64(&ECtx, 2147483647);
918 QCBOREncode_AddInt64(&ECtx, 2147483647);
919 QCBOREncode_AddInt64(&ECtx, 2147483648);
920 QCBOREncode_AddInt64(&ECtx, 2147483649);
921 QCBOREncode_AddInt64(&ECtx, 4294967294);
922 QCBOREncode_AddInt64(&ECtx, 4294967295);
923 QCBOREncode_AddInt64(&ECtx, 4294967296);
924 QCBOREncode_AddInt64(&ECtx, 4294967297);
925 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
926 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800927
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800928 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800929
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530930 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700931 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 nReturn = -1;
933 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530935 if(CheckResults(Enc, spExpectedEncodedInts))
936 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800937
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 return(nReturn);
939}
940
941
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530942/*
943 85 # array(5)
944 F5 # primitive(21)
945 F4 # primitive(20)
946 F6 # primitive(22)
947 F7 # primitive(23)
948 A1 # map(1)
949 65 # text(5)
950 554E446566 # "UNDef"
951 F7 # primitive(23)
952 */
953static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
955
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300956int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957{
958 QCBOREncodeContext ECtx;
959 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800960
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530961 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800962 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900963
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700964 QCBOREncode_AddBool(&ECtx, true);
965 QCBOREncode_AddBool(&ECtx, false);
966 QCBOREncode_AddNULL(&ECtx);
967 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800968
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800969 QCBOREncode_OpenMap(&ECtx);
970
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700971 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800972 QCBOREncode_CloseMap(&ECtx);
973
974 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800975
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530976 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700977 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 nReturn = -1;
979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800980
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530981 if(CheckResults(ECBOR, spExpectedEncodedSimple))
982 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984 return(nReturn);
985}
986
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700987#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Jan Jongboom47d86c52019-07-25 08:54:16 +0200988/*
989 9F # array(5)
990 F5 # primitive(21)
991 F4 # primitive(20)
992 F6 # primitive(22)
993 F7 # primitive(23)
994 BF # map(1)
995 65 # text(5)
996 554E446566 # "UNDef"
997 F7 # primitive(23)
998 FF # break
999 FF # break
1000 */
1001static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
1002 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
1003
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001004int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +02001005{
1006 QCBOREncodeContext ECtx;
1007 int nReturn = 0;
1008
1009 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1010 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
1011
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001012 QCBOREncode_AddBool(&ECtx, true);
1013 QCBOREncode_AddBool(&ECtx, false);
1014 QCBOREncode_AddNULL(&ECtx);
1015 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +02001016
1017 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
1018
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001019 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +02001020 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
1021
1022 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
1023
1024 UsefulBufC ECBOR;
1025 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1026 nReturn = -1;
1027 }
1028
1029 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
1030 return -2;
1031
1032 return(nReturn);
1033}
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001034#endif
Jan Jongboom47d86c52019-07-25 08:54:16 +02001035
Jan Jongboom5d827882019-08-07 12:51:15 +02001036/*
1037A5 # map(5)
1038 63 # text(3)
1039 617272 # "arr"
1040 98 1F # array(31)
1041 00 # unsigned(0)
1042 01 # unsigned(1)
1043 02 # unsigned(2)
1044 03 # unsigned(3)
1045 04 # unsigned(4)
1046 05 # unsigned(5)
1047 06 # unsigned(6)
1048 07 # unsigned(7)
1049 08 # unsigned(8)
1050 09 # unsigned(9)
1051 0A # unsigned(10)
1052 0B # unsigned(11)
1053 0C # unsigned(12)
1054 0D # unsigned(13)
1055 0E # unsigned(14)
1056 0F # unsigned(15)
1057 10 # unsigned(16)
1058 11 # unsigned(17)
1059 12 # unsigned(18)
1060 13 # unsigned(19)
1061 14 # unsigned(20)
1062 15 # unsigned(21)
1063 16 # unsigned(22)
1064 17 # unsigned(23)
1065 18 18 # unsigned(24)
1066 18 19 # unsigned(25)
1067 18 1A # unsigned(26)
1068 18 1B # unsigned(27)
1069 18 1C # unsigned(28)
1070 18 1D # unsigned(29)
1071 18 1E # unsigned(30)
1072 63 # text(3)
1073 6D6170 # "map"
1074 B8 1F # map(31)
1075 61 # text(1)
1076 61 # "a"
1077 00 # unsigned(0)
1078 61 # text(1)
1079 62 # "b"
1080 01 # unsigned(1)
1081 61 # text(1)
1082 63 # "c"
1083 02 # unsigned(2)
1084 61 # text(1)
1085 64 # "d"
1086 03 # unsigned(3)
1087 61 # text(1)
1088 65 # "e"
1089 04 # unsigned(4)
1090 61 # text(1)
1091 66 # "f"
1092 05 # unsigned(5)
1093 61 # text(1)
1094 67 # "g"
1095 06 # unsigned(6)
1096 61 # text(1)
1097 68 # "h"
1098 07 # unsigned(7)
1099 61 # text(1)
1100 69 # "i"
1101 08 # unsigned(8)
1102 61 # text(1)
1103 6A # "j"
1104 09 # unsigned(9)
1105 61 # text(1)
1106 6B # "k"
1107 0A # unsigned(10)
1108 61 # text(1)
1109 6C # "l"
1110 0B # unsigned(11)
1111 61 # text(1)
1112 6D # "m"
1113 0C # unsigned(12)
1114 61 # text(1)
1115 6E # "n"
1116 0D # unsigned(13)
1117 61 # text(1)
1118 6F # "o"
1119 0E # unsigned(14)
1120 61 # text(1)
1121 70 # "p"
1122 0F # unsigned(15)
1123 61 # text(1)
1124 71 # "q"
1125 10 # unsigned(16)
1126 61 # text(1)
1127 72 # "r"
1128 11 # unsigned(17)
1129 61 # text(1)
1130 73 # "s"
1131 12 # unsigned(18)
1132 61 # text(1)
1133 74 # "t"
1134 13 # unsigned(19)
1135 61 # text(1)
1136 75 # "u"
1137 14 # unsigned(20)
1138 61 # text(1)
1139 76 # "v"
1140 15 # unsigned(21)
1141 61 # text(1)
1142 77 # "w"
1143 16 # unsigned(22)
1144 61 # text(1)
1145 78 # "x"
1146 17 # unsigned(23)
1147 61 # text(1)
1148 79 # "y"
1149 18 18 # unsigned(24)
1150 61 # text(1)
1151 7A # "z"
1152 18 19 # unsigned(25)
1153 61 # text(1)
1154 41 # "A"
1155 18 1A # unsigned(26)
1156 61 # text(1)
1157 42 # "B"
1158 18 1B # unsigned(27)
1159 61 # text(1)
1160 43 # "C"
1161 18 1C # unsigned(28)
1162 61 # text(1)
1163 44 # "D"
1164 18 1D # unsigned(29)
1165 61 # text(1)
1166 45 # "E"
1167 18 1E # unsigned(30)
1168 65 # text(5)
1169 6D696E3331 # "min31"
1170 38 1E # negative(30)
1171 66 # text(6)
1172 706C75733331 # "plus31"
1173 18 1F # unsigned(31)
1174 63 # text(3)
1175 737472 # "str"
1176 78 1F # text(31)
1177 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1178 */
1179static const uint8_t EncodeLengthThirtyone[] = {
1180 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1181 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1182 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1183 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1184 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1185 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1186 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1187 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1188 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1189 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1190 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1191 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1192 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1193 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1194 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1195 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1196 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1197 0x31
1198};
1199
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001200int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001201{
1202 QCBOREncodeContext ECtx;
1203 int nReturn = 0;
1204
1205 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1206 QCBOREncode_OpenMap(&ECtx);
1207
1208 // add array with 31 items
1209 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1210 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001211 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001212 }
1213 QCBOREncode_CloseArray(&ECtx);
1214
1215 // add map with 31 items
1216 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001217 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001218 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001219 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001220 if (ix < 26) c = c + ix;
1221 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001222 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001223 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1224 }
1225 QCBOREncode_CloseMap(&ECtx);
1226
1227 // add -31 and +31
1228 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1229 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1230
1231 // add string with length 31
1232 const char *str = "testtesttesttesttesttestqcbor11";
1233 UsefulBufC str_b = { str, 31 };
1234 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1235
1236 QCBOREncode_CloseMap(&ECtx);
1237
1238 UsefulBufC ECBOR;
1239 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1240 nReturn = -1;
1241 }
1242
1243 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1244 return -2;
1245
1246 return(nReturn);
1247}
1248
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301249
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301250/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001251 * [ "2013-03-21T20:04:00Z",
1252 * 0("2013-03-21T20:04:00Z"),
1253 * 1363896240,
1254 * 1(1363896240),
1255 * 100(-10676),
1256 * 3994,
1257 * 1004("1940-10-09"),
1258 * "1980-12-08",
1259 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1260 * "SD": 1(999),
1261 * "Sample Date from RFC 8943": "1985-04-12",
1262 * 42: 1004("1985-04-12T23:20:50.52Z"),
1263 * "SY": 100(-10676),
1264 * 45: 3994
1265 * }
1266 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301267 */
1268static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001269 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1270 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1271 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1272 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1273 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1274 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1275 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1276 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1277 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1278 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1279 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1280 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1281 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1282 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1283 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1284 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1285 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1286 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1287 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1288 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1289 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1290 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1291 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001292
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001293int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001294{
1295 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301297 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001299 QCBOREncode_OpenArray(&ECtx);
1300
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001301 /* The values are taken from the CBOR RFCs */
1302 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1303 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1304 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1305 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1306 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1307 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1308 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1309 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 QCBOREncode_OpenMap(&ECtx);
1312
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001313 QCBOREncode_AddDateStringToMap(&ECtx,
1314 "Sample Date from RFC 3339",
1315 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001317 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1318 "Sample Date from RFC 8943",
1319 QCBOR_ENCODE_AS_BORROWED,
1320 "1985-04-12");
1321 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1322 42,
1323 QCBOR_ENCODE_AS_TAG,
1324 "1985-04-12T23:20:50.52Z");
1325 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1326 "SY",
1327 QCBOR_ENCODE_AS_TAG,
1328 -10676);
1329 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1330 45,
1331 QCBOR_ENCODE_AS_BORROWED,
1332 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 QCBOREncode_CloseArray(&ECtx);
1337
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301338 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001339 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001340 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001342
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301343 if(CheckResults(ECBOR, spExpectedEncodedDates))
1344 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001345
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001346 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001347}
1348
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301349
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001350int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351{
1352 QCBOREncodeContext ECtx;
1353 int i;
1354 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301356 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1358 QCBOREncode_OpenArray(&ECtx);
1359 }
1360 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1361 QCBOREncode_CloseArray(&ECtx);
1362 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001363 UsefulBufC Encoded;
1364 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001365 nReturn = -1;
1366 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367
1368 return(nReturn);
1369}
1370
1371
1372
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001373int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001374{
1375 QCBOREncodeContext ECtx;
1376 int i;
1377 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001378
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301379 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1381 QCBOREncode_OpenArray(&ECtx);
1382 }
1383 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1384 QCBOREncode_CloseArray(&ECtx);
1385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001386
Laurence Lundblade0595e932018-11-02 22:22:47 +07001387 UsefulBufC Encoded;
1388 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 nReturn = -1;
1390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392 return(nReturn);
1393}
1394
1395
1396
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001397int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398{
1399 QCBOREncodeContext ECtx;
1400 int i;
1401 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001402
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301403 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1405 QCBOREncode_OpenArray(&ECtx);
1406 }
1407 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1408 QCBOREncode_CloseArray(&ECtx);
1409 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001410 UsefulBufC Encoded;
1411 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412 nReturn = -1;
1413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 return(nReturn);
1416}
1417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301419/*
1420 81 # array(1)
1421 81 # array(1)
1422 81 # array(1)
1423 81 # array(1)
1424 80 # array(0)
1425*/
1426static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001427
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301429/*
1430 82 # array(2)
1431 81 # array(1)
1432 81 # array(1)
1433 81 # array(1)
1434 81 # array(1)
1435 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001436 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301437 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1438 3B 0000000100000000 # negative(4294967296)
1439 3A FFFFFFFF # negative(4294967295)
1440 3A FFFFFFFE # negative(4294967294)
1441 3A FFFFFFFD # negative(4294967293)
1442 3A 7FFFFFFF # negative(2147483647)
1443 3A 7FFFFFFE # negative(2147483646)
1444 3A 00010001 # negative(65537)
1445 3A 00010000 # negative(65536)
1446 39 FFFF # negative(65535)
1447 39 FFFE # negative(65534)
1448 39 FFFD # negative(65533)
1449 39 0100 # negative(256)
1450 38 FF # negative(255)
1451 38 FE # negative(254)
1452 38 FD # negative(253)
1453 38 18 # negative(24)
1454 37 # negative(23)
1455 36 # negative(22)
1456 20 # negative(0)
1457 00 # unsigned(0)
1458 00 # unsigned(0)
1459 01 # unsigned(1)
1460 16 # unsigned(22)
1461 17 # unsigned(23)
1462 18 18 # unsigned(24)
1463 18 19 # unsigned(25)
1464 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001465 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301466 18 FE # unsigned(254)
1467 18 FF # unsigned(255)
1468 19 0100 # unsigned(256)
1469 19 0101 # unsigned(257)
1470 19 FFFE # unsigned(65534)
1471 19 FFFF # unsigned(65535)
1472 1A 00010000 # unsigned(65536)
1473 1A 00010001 # unsigned(65537)
1474 1A 00010002 # unsigned(65538)
1475 1A 7FFFFFFF # unsigned(2147483647)
1476 1A 7FFFFFFF # unsigned(2147483647)
1477 1A 80000000 # unsigned(2147483648)
1478 1A 80000001 # unsigned(2147483649)
1479 1A FFFFFFFE # unsigned(4294967294)
1480 1A FFFFFFFF # unsigned(4294967295)
1481 1B 0000000100000000 # unsigned(4294967296)
1482 1B 0000000100000001 # unsigned(4294967297)
1483 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1484 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1485 */
1486static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001487 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1489 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1490 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1491 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1492 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1493 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1494 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1495 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1496 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1497 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1498 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001499 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1500 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1501 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1502 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1503 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1504 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1505 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1506 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1507 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1508 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1509 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1510 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1511 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512
1513
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001514int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001515{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001516 QCBOREncodeContext ECtx;
1517
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301518 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001519 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301520 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1521 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001522 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001523
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001524 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundblade0595e932018-11-02 22:22:47 +07001526 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001527 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301530 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001531 return -5;
1532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001533
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001534 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535}
1536
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301537/*
1538 This returns a pointer to spBigBuf
1539 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541{
1542 QCBOREncodeContext ECtx;
1543 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001544
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 *pEncoded = NULL;
1546 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301547 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001548
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 // loop runs CBOR encoding twice. First with no buffer to
1550 // calucate the length so buffer can be allocated correctly,
1551 // and last with the buffer to do the actual encoding
1552 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301553 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 QCBOREncode_OpenMap(&ECtx);
1555 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1556 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1557 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1558 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1559 QCBOREncode_CloseArray(&ECtx);
1560 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1561 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1562 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1563 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1564 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1565 QCBOREncode_CloseMap(&ECtx);
1566 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
Laurence Lundblade0595e932018-11-02 22:22:47 +07001568 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 goto Done;
1570 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301571 if(uFirstSizeEstimate != *pEncodedLen) {
1572 nReturn = 1;
1573 } else {
1574 nReturn = 0;
1575 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 goto Done;
1577 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301578 *pEncoded = spBigBuf;
1579 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001581 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 Done:
1584 return(nReturn);
1585}
1586
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301587/*
1588 A3 # map(3)
1589 6D # text(13)
1590 666972737420696E7465676572 # "first integer"
1591 18 2A # unsigned(42)
1592 77 # text(23)
1593 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1594 82 # array(2)
1595 67 # text(7)
1596 737472696E6731 # "string1"
1597 67 # text(7)
1598 737472696E6732 # "string2"
1599 6C # text(12)
1600 6D617020696E2061206D6170 # "map in a map"
1601 A4 # map(4)
1602 67 # text(7)
1603 62797465732031 # "bytes 1"
1604 44 # bytes(4)
1605 78787878 # "xxxx"
1606 67 # text(7)
1607 62797465732032 # "bytes 2"
1608 44 # bytes(4)
1609 79797979 # "yyyy"
1610 6B # text(11)
1611 616E6F7468657220696E74 # "another int"
1612 18 62 # unsigned(98)
1613 66 # text(6)
1614 746578742032 # "text 2"
1615 78 1E # text(30)
1616 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1617 */
1618static const uint8_t spValidMapEncoded[] = {
1619 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1620 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1621 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1622 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1623 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1624 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1625 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1626 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1627 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1628 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1629 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1630 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1631 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1632 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1633 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1634 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001635
1636
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001637int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638{
1639 uint8_t *pEncodedMaps;
1640 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001642 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301643 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301647 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1648 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001649
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001650 return(nReturn);
1651}
1652
1653
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001654/*
1655 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001656
Laurence Lundbladeee851742020-01-08 08:37:05 -08001657 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1658 CBOR_SIMPLEV_NULL
1659 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660 @param[in] szAlexString Diagnostic code.
1661 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001662 @param[in/out] pnLen Size of pOut buffer when called; length of data
1663 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001665 @return
1666 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001668 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 -08001669 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001671
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001672 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1673 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001674
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001675 */
1676
Laurence Lundbladeee851742020-01-08 08:37:05 -08001677static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001678FormatRTICResults(uint8_t uRResult,
1679 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001680 const char *szType,
1681 const char *szAlexString,
1682 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001683{
1684 // Buffer that the result will be written in to
1685 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001686 // QCBOREncode will never write off the end of this buffer. If it won't
1687 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001689 // Context for the encoder
1690 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301691 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001693 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1694 // Contents are label / value pairs
1695 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001697 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001698
Laurence Lundbladeee851742020-01-08 08:37:05 -08001699 // The result: 0 if scan happened and found nothing; 1 if it happened and
1700 // found something wrong; 2 if it didn't happen
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001701 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001702
1703 // Add the diagnostic code
1704 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1705
1706 // Add a time stamp
1707 if(time) {
1708 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1709 }
1710
1711 // Add the diagnostic code
1712 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1713
1714 // Open a subordinate map for telemtry data
1715 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1716
1717 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001718
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001719 // Add a few fake integers and buffers for now.
1720 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1721
1722 // Add a few fake integers and buffers for now.
1723 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1724
1725 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301726 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001727 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001728
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001729 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1730 }
1731 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001732
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001733 // Close the telemetry map
1734 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001735
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001736 // Close the map
1737 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001738
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301739 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001740
Laurence Lundblade0595e932018-11-02 22:22:47 +07001741 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001742
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301743 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001744}
1745
1746
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301747/*
1748 A5 # map(5)
1749 69 # text(9)
1750 696E74656772697479 # "integrity"
1751 F4 # primitive(20)
1752 64 # text(4)
1753 74797065 # "type"
1754 66 # text(6)
1755 726563656E74 # "recent"
1756 64 # text(4)
1757 74696D65 # "time"
1758 C1 # tag(1)
1759 1A 580D4172 # unsigned(1477263730)
1760 64 # text(4)
1761 64696167 # "diag"
1762 6A # text(10)
1763 30784131654335303031 # "0xA1eC5001"
1764 69 # text(9)
1765 74656C656D65747279 # "telemetry"
1766 A3 # map(3)
1767 69 # text(9)
1768 53686F652053697A65 # "Shoe Size"
1769 0C # unsigned(12)
1770 62 # text(2)
1771 4951 # "IQ"
1772 1A FFFFFFFF # unsigned(4294967295)
1773 77 # text(23)
1774 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1775 48 # bytes(8)
1776 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1777 */
1778static const uint8_t spExpectedRTIC[] = {
1779 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1780 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1781 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1782 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1783 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1784 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1785 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1786 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1787 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1788 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1789 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1790 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001791
1792
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001793int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001794{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001795 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301796 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301797 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301798 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001799 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301800 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001801
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301802 if(CheckResults(Encoded, spExpectedRTIC)) {
1803 return -2;
1804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001805
1806 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001807}
1808
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301809
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301810/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001811 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001812
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301813 82 # array(2)
1814 19 01C3 # unsigned(451)
1815 43 # bytes(3)
1816 1901D2 # "\x19\x01\xD2"
1817*/
1818static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301819
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001820static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1821
Laurence Lundbladeda532272019-04-07 11:40:17 -07001822/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001823 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001824 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001825int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001826{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001827 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001828
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001829 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301830 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001831
Laurence Lundblade684aec22018-10-12 19:33:53 +08001832 QCBOREncode_OpenArray(&EC);
1833 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001834
Laurence Lundblade684aec22018-10-12 19:33:53 +08001835 QCBOREncode_BstrWrap(&EC);
1836 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001837
Laurence Lundblade684aec22018-10-12 19:33:53 +08001838 UsefulBufC Wrapped;
1839 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001840
Laurence Lundblade684aec22018-10-12 19:33:53 +08001841 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001842
Laurence Lundblade684aec22018-10-12 19:33:53 +08001843 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001844 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001845 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001846 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001847
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301848 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001849 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001850 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001851
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001852 // Second test - see if the length of the wrapped
1853 // bstr is correct. Also tests bstr wrapping
1854 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001855 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1856 QCBOREncode_OpenArray(&EC);
1857 QCBOREncode_BstrWrap(&EC);
1858 QCBOREncode_OpenArray(&EC);
1859 QCBOREncode_AddNULL(&EC);
1860 QCBOREncode_CloseArray(&EC);
1861 UsefulBufC BStr;
1862 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001863 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1864 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001865 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001866 return -5;
1867 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001868
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001869
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001870 // Fourth test, cancelling a byte string
1871 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1872
1873 QCBOREncode_OpenArray(&EC);
1874 QCBOREncode_AddUInt64(&EC, 451);
1875
1876 QCBOREncode_BstrWrap(&EC);
1877 QCBOREncode_CancelBstrWrap(&EC);
1878
1879
1880 QCBOREncode_AddUInt64(&EC, 42);
1881 QCBOREncode_CloseArray(&EC);
1882 if(QCBOREncode_Finish(&EC, &Encoded)) {
1883 return -8;
1884 }
1885 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1886 return -9;
1887 }
1888
1889 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001890 // Fifth test, failed cancelling
1891 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1892
1893 QCBOREncode_OpenArray(&EC);
1894 QCBOREncode_AddUInt64(&EC, 451);
1895
1896 QCBOREncode_BstrWrap(&EC);
1897 QCBOREncode_AddUInt64(&EC, 99);
1898 QCBOREncode_CancelBstrWrap(&EC);
1899
1900 QCBOREncode_AddUInt64(&EC, 42);
1901 QCBOREncode_CloseArray(&EC);
1902 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001903#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001904 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1905 return -10;
1906 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001907#else
1908 if(uErr != QCBOR_SUCCESS) {
1909 return -110;
1910 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001911#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1912
1913 // Sixth test, another cancel, but the error is not caught
1914 // This use will produce unintended CBOR. The error
1915 // is not caught because it would require tracking state
1916 // for QCBOREncode_BstrWrapInMapN.
1917 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1918
1919 QCBOREncode_OpenMap(&EC);
1920 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1921
1922 QCBOREncode_BstrWrapInMapN(&EC, 55);
1923 QCBOREncode_CancelBstrWrap(&EC);
1924
1925 QCBOREncode_CloseMap(&EC);
1926 uErr = QCBOREncode_Finish(&EC, &Encoded);
1927 if(uErr != QCBOR_SUCCESS) {
1928 return -11;
1929 }
1930
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001931 return 0;
1932}
1933
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001934
1935
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001936int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001937{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001938 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001939 UsefulBufC Wrapped;
1940 UsefulBufC Encoded2;
1941 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001942
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001943 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001944
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301945 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001946
Laurence Lundblade684aec22018-10-12 19:33:53 +08001947 QCBOREncode_OpenArray(&EC);
1948 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001949
Laurence Lundblade684aec22018-10-12 19:33:53 +08001950 QCBOREncode_BstrWrap(&EC);
1951 QCBOREncode_AddUInt64(&EC, 466);
1952 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001953
Laurence Lundblade684aec22018-10-12 19:33:53 +08001954 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundblade684aec22018-10-12 19:33:53 +08001956 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001957
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001958 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001959#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001960 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001961 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001962 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001963#else
1964 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1965 * is set and not to be sure to excerice all the relavant code in
1966 * both conditions. When the guards are disabled, there is no
1967 * error returned, but the code path is still covered.
1968 */
1969 if(uError != QCBOR_SUCCESS) {
1970 return (int32_t)(600 + uError);
1971 }
1972#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001973
Laurence Lundbladeee851742020-01-08 08:37:05 -08001974 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301975 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001976 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001977 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001978#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001979 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001980 return (int32_t)(700 + uError);
1981 }
1982#else
1983 if(uError != QCBOR_SUCCESS) {
1984 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001985 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001986#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001987
Laurence Lundblade684aec22018-10-12 19:33:53 +08001988 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301989 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001990 for(int i = 1; i < 18; i++) {
1991 QCBOREncode_BstrWrap(&EC);
1992 }
1993 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001994
Laurence Lundblade684aec22018-10-12 19:33:53 +08001995 for(int i = 1; i < 18; i++) {
1996 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001998
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001999 uError = QCBOREncode_Finish(&EC, &Encoded2);
2000 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08002001 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002002 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002003
Laurence Lundblade684aec22018-10-12 19:33:53 +08002004 return 0;
2005}
2006
2007
Laurence Lundblade684aec22018-10-12 19:33:53 +08002008/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002009 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002010
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002011 [
2012 h'82004E82014B8202488203458204428105',
2013 {
2014 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
2015 8426568656C6C6F18234BA2131318436568656C6C6F'
2016 }
2017 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002018
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002019 Unwrapping the first byte string in the above gives
2020 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302021
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002022 Unwrapping again, the byte string immediately above gives
2023 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302024
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002025 ...
2026
2027 Unrapping the second byte string in the top-level CBOR above gives
2028 {16: 16,
2029 64: "hello",
2030 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2031 }
2032
2033 Unwrapping again, the byte string immediately above gives
2034 {17: 17,
2035 65: "hello",
2036 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2037 }
2038
2039 ...
2040
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302041 */
2042static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002043{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002044 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2045 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2046 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2047 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2048 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2049 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2050 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2051 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2052 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2053 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002054};
2055
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002056
2057/*
2058 Get an int64 out of the decoder or fail.
2059 */
2060static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002061{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002062 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002063 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002065 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002066 if(nReturn) {
2067 return nReturn;
2068 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002069 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002070 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002073 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002074 return 0;
2075}
2076
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002077/*
2078 Get an array out of the decoder or fail.
2079 */
2080static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002081{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002082 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002083 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002085 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002086 if(nReturn) {
2087 return nReturn;
2088 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002089 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2090 return -1;
2091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002092
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002093 *pInt = Item.val.uCount;
2094 return 0;
2095}
2096
2097/*
2098 Get a map out of the decoder or fail.
2099 */
2100static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2101{
2102 QCBORItem Item;
2103 int32_t nReturn;
2104
2105 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002106 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002107 return nReturn;
2108 }
2109 if(Item.uDataType != QCBOR_TYPE_MAP) {
2110 return -1;
2111 }
2112
2113 *pInt = Item.val.uCount;
2114 return 0;
2115}
2116
2117/*
2118 Get a byte string out of the decoder or fail.
2119 */
2120static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2121{
2122 QCBORItem Item;
2123 int32_t nReturn;
2124
2125 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2126 if(nReturn) {
2127 return nReturn;
2128 }
2129 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002130 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002131 }
2132
2133 *pBstr = Item.val.string;
2134 return 0;
2135}
2136
2137/*
2138 Get a byte string out of the decoder or fail.
2139 */
2140static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2141{
2142 QCBORItem Item;
2143 int nReturn;
2144
2145 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2146 if(nReturn) {
2147 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002148 }
2149 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002150 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002151 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002152
2153 *pTstr = Item.val.string;
2154 return 0;
2155}
2156
2157
2158/*
2159 Recursively decode array containing a little CBOR and a bstr wrapped array
2160 with a little CBOR and a bstr wrapped array...
2161
2162 Part of bstr_wrap_nest_test.
2163 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2164{
2165 int64_t nInt;
2166 UsefulBufC Bstr;
2167 uint16_t nArrayCount;
2168 QCBORDecodeContext DC;
2169 int32_t nResult;
2170
2171 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2172
2173 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2174 return -10;
2175 }
2176
2177 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002178 return -11;
2179 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002180
2181 nResult = GetByteString(&DC, &Bstr);
2182 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2183 if(nArrayCount != 1) {
2184 return -12;
2185 } else {
2186 // successful exit
2187 return 0;
2188 }
2189 }
2190 if(nResult) {
2191 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002193
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002194 // tail recursion; good compilers will reuse the stack frame
2195 return DecodeNextNested(Bstr);
2196}
2197
2198
2199/*
2200 Recursively decode map containing a little CBOR and a bstr wrapped map
2201 with a little CBOR and a bstr wrapped map...
2202
2203 Part of bstr_wrap_nest_test.
2204 */
2205static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2206{
2207 int32_t nResult;
2208 uint16_t nMapCount;
2209 int64_t nInt;
2210 UsefulBufC Bstr;
2211 QCBORDecodeContext DC;
2212
2213 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2214
2215 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2216 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002218
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002219 if(GetInt64(&DC, &nInt)) {
2220 return -21;
2221 }
2222
2223 // The "hello"
2224 if(GetTextString(&DC, &Bstr)) {
2225 return -22;
2226 }
2227
2228 nResult = GetByteString(&DC, &Bstr);
2229 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2230 if(nMapCount == 2) {
2231 // successful exit
2232 return 0;
2233 } else {
2234 return -23;
2235 }
2236 }
2237
2238 if(nResult) {
2239 return -24;
2240 }
2241
2242 // tail recursion; good compilers will reuse the stack frame
2243 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002244}
2245
2246
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002247int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002248{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002249 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302250 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
Laurence Lundblade684aec22018-10-12 19:33:53 +08002252 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002253 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002254
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002255 QCBOREncode_OpenArray(&EC);
2256
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002257 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002258 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002259 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002260 QCBOREncode_AddInt64(&EC, i);
2261 }
2262 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2263 QCBOREncode_CloseArray(&EC);
2264 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002267 QCBOREncode_OpenMap(&EC);
2268 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2269 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2270 QCBOREncode_OpenMap(&EC);
2271 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002272 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002273 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002274
2275 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2276 QCBOREncode_CloseMap(&EC);
2277 QCBOREncode_CloseBstrWrap(&EC, NULL);
2278 }
2279 QCBOREncode_CloseMap(&EC);
2280
Laurence Lundblade684aec22018-10-12 19:33:53 +08002281 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade684aec22018-10-12 19:33:53 +08002283 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002284 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002285 return -1;
2286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundblade684aec22018-10-12 19:33:53 +08002288 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302289 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002290 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002292
Laurence Lundblade684aec22018-10-12 19:33:53 +08002293 // ---- Decode it and see if it is OK ------
2294 QCBORDecodeContext DC;
2295 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002296
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002297 UsefulBufC Bstr;
2298 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002300 // Array surrounding the the whole thing
2301 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002302 return -3;
2303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002304
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002305 // Get the byte string wrapping some array stuff
2306 if(GetByteString(&DC, &Bstr)) {
2307 return -4;
2308 }
2309
2310 // Decode the wrapped nested structure
2311 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002312 if(nReturn) {
2313 return nReturn;
2314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002316 // A map enclosing some map-oriented bstr wraps
2317 if(GetMap(&DC, &nArrayCount)) {
2318 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002320
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002321 // Get the byte string wrapping some array stuff
2322 if(GetByteString(&DC, &Bstr)) {
2323 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002326 // Decode the wrapped nested structure
2327 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002328 if(nReturn) {
2329 return nReturn;
2330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002331
Laurence Lundblade684aec22018-10-12 19:33:53 +08002332 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002333 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002334 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002335
Laurence Lundblade684aec22018-10-12 19:33:53 +08002336 return 0;
2337}
2338
2339
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002340static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302341 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2342 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2343 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2344 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2345 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2346 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2347 0x45, 0xca, 0xcb, 0x36};
2348
2349/*
2350 D2 # tag(18)
2351 84 # array(4)
2352 43 # bytes(3)
2353 A10126 # "\xA1\x01&"
2354 A1 # map(1)
2355 04 # unsigned(4)
2356 42 # bytes(2)
2357 3131 # "11"
2358 54 # bytes(20)
2359 546869732069732074686520636F6E74656E742E # "This is the content."
2360 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002361 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2362 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2363 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2364 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2365 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2366 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2367 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2368 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302369 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002370static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302371 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2372 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2373 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2374 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2375 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2376 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2377 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2378 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2379 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2380 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2381
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002382static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2383
2384
Laurence Lundblade684aec22018-10-12 19:33:53 +08002385/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002386 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002387 C.2.1. This doesn't actually verify the signature (however
2388 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002389 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002390int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002391{
2392 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002393 const char *szKid = "11";
2394 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2395 const char *szPayload = "This is the content.";
2396 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2397 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2398 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002399
Laurence Lundblade684aec22018-10-12 19:33:53 +08002400 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002401
2402 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302403 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002404
Laurence Lundblade684aec22018-10-12 19:33:53 +08002405 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002406 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002407 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002408
Laurence Lundblade684aec22018-10-12 19:33:53 +08002409 // Add protected headers
2410 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002411
Laurence Lundblade684aec22018-10-12 19:33:53 +08002412 // Empty map with unprotected headers
2413 QCBOREncode_OpenMap(&EC);
2414 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2415 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002416
Laurence Lundblade684aec22018-10-12 19:33:53 +08002417 // The payload
2418 UsefulBufC WrappedPayload;
2419 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002420 // Payload is not actually CBOR in example C.2.1 like it would be
2421 // for a CWT or EAT. It is just a text string.
2422 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002423 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002424
Laurence Lundblade684aec22018-10-12 19:33:53 +08002425 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002426 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2427 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002428 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002429 return -1;
2430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002431
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002432/* if(UsefulBuf_Compare(WrappedPayload,
2433 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2434 return -1;
2435 } */
2436
Laurence Lundblade684aec22018-10-12 19:33:53 +08002437 // The signature
2438 QCBOREncode_AddBytes(&EC, Signature);
2439 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002440
Laurence Lundblade684aec22018-10-12 19:33:53 +08002441 // Finish and check the results
2442 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002443 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002444 return -2;
2445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade684aec22018-10-12 19:33:53 +08002447 // 98 is the size from RFC 8152 C.2.1
2448 if(COSE_Sign1.len != 98) {
2449 return -3;
2450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002451
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002452 // It would be good to compare this to the output from a COSE
2453 // implementation like COSE-C. This has been checked against the
2454 // CBOR playground.
2455 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002456 return -4;
2457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002458
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002459
2460 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2461 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2462
2463 // top level array for cose sign1, 18 is the tag for COSE sign
2464 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2465 QCBOREncode_OpenArray(&EC);
2466
2467 // Add protected headers
2468 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2469
2470 // Empty map with unprotected headers
2471 QCBOREncode_OpenMap(&EC);
2472 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2473 QCBOREncode_CloseMap(&EC);
2474
2475 // The payload
2476 QCBOREncode_BstrWrap(&EC);
2477 // Payload is not actually CBOR in example C.2.1 like it would be
2478 // for a CWT or EAT. It is just a text string.
2479 QCBOREncode_AddEncoded(&EC, Payload);
2480 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2481
2482 // Check we got back the actual payload expected
2483 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2484 if(UsefulBuf_Compare(WrappedPayload,
2485 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2486 return -11;
2487 }
2488
2489 // The signature
2490 QCBOREncode_AddBytes(&EC, Signature);
2491 QCBOREncode_CloseArray(&EC);
2492
2493 // Finish and check the results
2494 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2495 return -12;
2496 }
2497
2498 // 98 is the size from RFC 8152 C.2.1
2499 if(COSE_Sign1.len != 98) {
2500 return -13;
2501 }
2502
2503 // It would be good to compare this to the output from a COSE
2504 // implementation like COSE-C. This has been checked against the
2505 // CBOR playground.
2506 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2507 return -14;
2508 }
2509
Laurence Lundblade684aec22018-10-12 19:33:53 +08002510 return 0;
2511}
2512
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002513
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002514int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002515{
2516 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002517 QCBORError uErr;
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002518 UsefulBufC EncodedResult;
2519 MakeUsefulBufOnStack(SmallBuffer, 4);
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002520
2521
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002522 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002523 // Do all of these tests with NULL buffers so no actual
2524 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002525 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002526
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002527 // First verify no error from a big buffer
2528 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002529 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002530 // 6 is the CBOR overhead for opening the array and encodng the length
2531 // This exactly fills the buffer.
2532 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002533 QCBOREncode_CloseArray(&EC);
2534 size_t xx;
2535 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2536 return -1;
2537 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002538
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002539 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002540 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002541 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002542 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002543 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002544 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002545 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2546 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002547 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002548 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002549 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002550 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2551 return -2;
2552 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002553
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002554 // Third, fit an array in exactly at max position allowed
2555 QCBOREncode_Init(&EC, Buffer);
2556 QCBOREncode_OpenArray(&EC);
2557 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2558 QCBOREncode_OpenArray(&EC);
2559 QCBOREncode_CloseArray(&EC);
2560 QCBOREncode_CloseArray(&EC);
2561 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2562 return -10;
2563 }
2564
2565
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002566 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2567 // Work close to the 4GB size limit for a better test
2568 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002569 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002570
2571 QCBOREncode_Init(&EC, Large);
2572 QCBOREncode_OpenArray(&EC);
2573 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2574 QCBOREncode_CloseArray(&EC);
2575 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2576 // Making sure it succeeds when it should first
2577 return -3;
2578 }
2579
2580 QCBOREncode_Init(&EC, Large);
2581 QCBOREncode_OpenArray(&EC);
2582 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2583 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2584 QCBOREncode_CloseArray(&EC);
2585 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2586 // Now just 1 byte over, see that it fails
2587 return -4;
2588 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002589
2590
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002591 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2592 QCBOREncode_Init(&EC, Large);
2593 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2594 QCBOREncode_OpenArray(&EC);
2595 }
2596 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2597 QCBOREncode_CloseArray(&EC);
2598 }
2599 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2600 // Making sure it succeeds when it should first
2601 return -5;
2602 }
2603
2604 QCBOREncode_Init(&EC, Large);
2605 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2606 QCBOREncode_OpenArray(&EC);
2607 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002608 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002609 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2610 QCBOREncode_CloseArray(&EC);
2611 }
2612 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002613 return -6;
2614 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002615
2616
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002617 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002618 QCBOREncode_Init(&EC, Large);
2619 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2620 QCBOREncode_OpenArray(&EC);
2621 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002622 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002623 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2624 QCBOREncode_CloseArray(&EC);
2625 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002626 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2627#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2628 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002629 return -7;
2630 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002631#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2632 if(uErr != QCBOR_SUCCESS) {
2633 return -107;
2634 }
2635#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002636
2637
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002638 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002639 QCBOREncode_Init(&EC, Large);
2640 QCBOREncode_OpenArray(&EC);
2641 UsefulBufC Wrap;
2642 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002643 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2644#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2645 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002646 return -8;
2647 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002648#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2649 if(uErr != QCBOR_SUCCESS) {
2650 return -108;
2651 }
2652#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002653
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002654 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002655 QCBOREncode_Init(&EC, Large);
2656 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2657 QCBOREncode_OpenArray(&EC);
2658 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002659 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002660 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2661 QCBOREncode_CloseArray(&EC);
2662 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002663
2664 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2665#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2666 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002667 return -9;
2668 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002669#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2670 if(uErr != QCBOR_SUCCESS) {
2671 return -109;
2672 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002673#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002674
Laurence Lundblade241705e2018-12-30 18:56:14 -08002675 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2676 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002677
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002678
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002679 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002680 QCBOREncode_Init(&EC, Buffer);
2681 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2682 return -11;
2683 }
2684
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002685 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002686 QCBOREncode_Init(&EC, Large);
2687 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002688 QCBOREncode_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002689 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2690#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2691 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002692 return -12;
2693 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002694#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2695 if(uErr != QCBOR_SUCCESS) {
2696 return -112;
2697 }
2698#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2699
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002700
2701 QCBOREncode_Init(&EC, Large);
2702 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002703 QCBOREncode_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002704 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2705#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2706 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002707 return -13;
2708 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002709#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2710 if(uErr != QCBOR_SUCCESS) {
2711 return -113;
2712 }
2713#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002714
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002715 /* Test that still-open error sticks */
2716 QCBOREncode_Init(&EC, Large);
2717 QCBOREncode_OpenArray(&EC);
2718 QCBOREncode_Finish(&EC, &EncodedResult);
2719#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2720 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2721 return -120;
2722 }
2723#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2724 if(QCBOREncode_GetErrorState(&EC) != QCBOR_SUCCESS) {
2725 return -122;
2726 }
2727#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2728
2729 /* Test that too-small error is sticky */
2730 QCBOREncode_Init(&EC, SmallBuffer);
2731 QCBOREncode_OpenArray(&EC);
2732 QCBOREncode_AddInt64(&EC, INT64_MAX);
2733 QCBOREncode_AddInt64(&EC, INT64_MAX);
2734 QCBOREncode_AddInt64(&EC, INT64_MAX);
2735 QCBOREncode_CloseArray(&EC);
2736 QCBOREncode_Finish(&EC, &EncodedResult);
2737 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2738 return -130;
2739 }
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002740
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002741 return 0;
2742}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002743
2744
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002745#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002746/*
2747 [
2748 4([-1, 3]),
2749 4([-20, 4759477275222530853136]),
2750 4([9223372036854775807, -4759477275222530853137]),
2751 5([300, 100]),
2752 5([-20, 4759477275222530853136]),
2753 5([-9223372036854775808, -4759477275222530853137])
2754 ]
2755 */
2756static const uint8_t spExpectedExponentAndMantissaArray[] = {
2757 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2758 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2759 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2760 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2761 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2762 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2763 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2764 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2765 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2766 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2767 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2768
2769
2770/*
2771 {
2772 "decimal fraction": 4([-1, 3]),
2773 300: 4([-1, 3]),
2774 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2775 400: 4([2147483647, 4759477275222530853136]),
2776 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2777 500: 4([9223372036854775807, -4759477275222530853137]),
2778 "big float": 5([300, 100]),
2779 600: 5([300, 100]),
2780 "big float bignum positive": 5([-20, 4759477275222530853136]),
2781 700: 5([-20, 4759477275222530853136]),
2782 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2783 800: 5([-9223372036854775808, -4759477275222530853137])
2784 }
2785 */
2786static const uint8_t spExpectedExponentAndMantissaMap[] = {
2787 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2788 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2789 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2790 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2791 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2792 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2793 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2794 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2795 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2796 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2797 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2798 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2799 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2800 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2801 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2802 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2803 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2804 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2805 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2806 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2807 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2808 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2809 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2810 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2811 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2812 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2813 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2814 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2815 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2816 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2817 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2818 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2819 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2820 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2821 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2822 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2823 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2824 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2825 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2826 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2827 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2828 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2829 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2830 0x09, 0x10
2831};
2832
2833
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002834int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002835{
2836 QCBOREncodeContext EC;
2837 UsefulBufC EncodedExponentAndMantissa;
2838
2839 // Constant for the big number used in all the tests.
2840 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2841 0x06, 0x07, 0x08, 0x09, 0x010};
2842 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2843
2844 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2845 QCBOREncode_OpenArray(&EC);
2846 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2847 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2848 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2849 QCBOREncode_AddBigFloat(&EC, 100, 300);
2850 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2851 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2852 QCBOREncode_CloseArray(&EC);
2853
2854 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2855 return -2;
2856 }
2857
2858 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2859 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2860 NULL);
2861 if(nReturn) {
2862 return nReturn;
2863 }
2864
2865 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2866 QCBOREncode_OpenMap(&EC);
2867
2868 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2869
2870 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2871
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002872 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002873 "decimal fraction bignum postive",
2874 BigNum,
2875 false,
2876 -200);
2877
2878 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2879 400,
2880 BigNum,
2881 false,
2882 INT32_MAX);
2883
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002884 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002885 "decimal fraction bignum negative",
2886 BigNum,
2887 true,
2888 INT64_MAX);
2889
2890 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2891 500,
2892 BigNum,
2893 true,
2894 INT64_MAX);
2895
2896 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2897
2898 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2899
2900 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2901 "big float bignum positive",
2902 BigNum,
2903 false,
2904 -20);
2905
2906 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2907 700,
2908 BigNum,
2909 false,
2910 -20);
2911
2912 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2913 "big float bignum negative",
2914 BigNum,
2915 true,
2916 INT64_MIN);
2917
2918 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2919 800,
2920 BigNum,
2921 true,
2922 INT64_MIN);
2923
2924 QCBOREncode_CloseMap(&EC);
2925
2926 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2927 return -3;
2928 }
2929
2930
2931 struct UBCompareDiagnostic Diag;
2932
2933 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2934 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2935 &Diag);
2936 if(nReturn) {
2937 return nReturn + 1000000; // +1000000 to distinguish from first test above
2938 }
2939
2940 return 0;
2941}
2942
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002943#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002944
2945
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002946int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002947{
2948 /* This test doesn't have to be extensive, because just about every
2949 * other test exercises QCBOREncode_EncodeHead().
2950 */
2951 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002952 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002953
2954 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2955 CBOR_MAJOR_TYPE_POSITIVE_INT,
2956 0,
2957 0);
2958
2959 static const uint8_t expectedZero[] = {0x00};
2960
2961 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2962 return -1;
2963 }
2964
2965 // ---- Encode a zero padded out to an 8 byte integer ----
2966 encoded = QCBOREncode_EncodeHead(RightSize,
2967 CBOR_MAJOR_TYPE_POSITIVE_INT,
2968 8, // uMinSize is 8 bytes
2969 0);
2970
2971 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2972 0x00, 0x00, 0x00, 0x00};
2973
2974 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2975 return -2;
2976 }
2977
2978
2979 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002980 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002981
2982 encoded = QCBOREncode_EncodeHead(TooSmall,
2983 CBOR_MAJOR_TYPE_POSITIVE_INT,
2984 0,
2985 0);
2986
2987 if(!UsefulBuf_IsNULLC(encoded)) {
2988 return -3;
2989 }
2990
2991 return 0;
2992}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002993
2994
2995static const uint8_t spExpectedForOpenBytes[] = {
2996 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2997 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2998 0x78
2999};
3000
3001static const uint8_t spExpectedForOpenBytes2[] = {
3002 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
3003 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
3004 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
3005};
3006
3007int32_t
3008OpenCloseBytesTest(void)
3009{
3010 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
3011 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
3012 QCBOREncodeContext EC;
3013 UsefulBuf Place;
3014 UsefulBufC Encoded;
3015 QCBORError uErr;
3016
3017 /* Normal use case -- add a byte string that fits */
3018 QCBOREncode_Init(&EC, TestBuf);
3019 QCBOREncode_OpenBytes(&EC, &Place);
3020 if(Place.ptr != TestBuf.ptr ||
3021 Place.len != TestBuf.len) {
3022 return 1;
3023 }
3024 Place.len -= 4;
3025 UsefulBuf_Set(Place, 'x');
3026 QCBOREncode_CloseBytes(&EC, Place.len);
3027 QCBOREncode_Finish(&EC, &Encoded);
3028 if(UsefulBuf_Compare(Encoded,
3029 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
3030 return 2;
3031 }
3032
Paul Liétar79789772022-07-26 20:33:18 +01003033 /* Run the same test but with a NULL buffer */
3034 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3035 QCBOREncode_OpenBytes(&EC, &Place);
3036 if(!UsefulBuf_IsNULL(Place)) {
3037 return 3;
3038 }
3039 Place.len -= 4;
3040 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3041 QCBOREncode_CloseBytes(&EC, Place.len);
3042 uErr = QCBOREncode_Finish(&EC, &Encoded);
3043 if(uErr != QCBOR_SUCCESS ||
3044 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3045 return 4;
3046 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003047
3048 /* Open a byte string with no room left */
3049 QCBOREncode_Init(&EC, TestBuf);
3050 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3051 QCBOREncode_OpenBytes(&EC, &Place);
3052 if(Place.ptr != NULL ||
3053 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003054 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003055 }
3056
3057 /* Try to extend byte string past end of encoding output buffer */
3058 QCBOREncode_Init(&EC, TestBuf);
3059 QCBOREncode_AddSZString(&EC, "012345678901234567");
3060 QCBOREncode_OpenBytes(&EC, &Place);
3061 /* Don't bother to write any bytes*/
3062 QCBOREncode_CloseBytes(&EC, Place.len+1);
3063 uErr = QCBOREncode_GetErrorState(&EC);
3064 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003065 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003066 }
3067
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003068 /* Close a byte string without opening one. */
3069 QCBOREncode_Init(&EC, TestBuf);
3070 QCBOREncode_AddSZString(&EC, "012345678");
3071 QCBOREncode_CloseBytes(&EC, 1);
3072 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003073#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003074 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003075 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003076 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003077#else
3078 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003079 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003080 }
3081#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003082
3083 /* Forget to close a byte string */
3084 QCBOREncode_Init(&EC, TestBuf);
3085 QCBOREncode_AddSZString(&EC, "012345678");
3086 QCBOREncode_OpenBytes(&EC, &Place);
3087 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003088#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003089 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003090 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003091 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003092#else
3093 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003094 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003095 }
3096#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003097
3098 /* Try to open a byte string in a byte string */
3099 QCBOREncode_Init(&EC, TestBuf);
3100 QCBOREncode_AddSZString(&EC, "012345678");
3101 QCBOREncode_OpenBytes(&EC, &Place);
3102 QCBOREncode_OpenBytes(&EC, &Place);
3103 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003104#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003105 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003106 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003107 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003108#else
3109 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003110 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003111 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003112#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3113
3114 /* A successful case with a little complexity */
3115 QCBOREncode_Init(&EC, TestBuf2);
3116 QCBOREncode_OpenMap(&EC);
3117 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3118 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3119 Place.len = 2;
3120 UsefulBuf_Set(Place, 'x');
3121 QCBOREncode_CloseBytes(&EC, 2);
3122 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3123 Place.len = 3;
3124 UsefulBuf_Set(Place, 'y');
3125 QCBOREncode_CloseBytes(&EC, 3);
3126 QCBOREncode_OpenArrayInMapN(&EC, 40);
3127 QCBOREncode_OpenBytes(&EC, &Place);
3128 QCBOREncode_CloseBytes(&EC, 0);
3129 QCBOREncode_CloseArray(&EC);
3130 QCBOREncode_CloseMap(&EC);
3131 uErr = QCBOREncode_Finish(&EC, &Encoded);
3132 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003133 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003134 }
3135 if(UsefulBuf_Compare(Encoded,
3136 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003137 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003138 }
3139
3140 return 0;
3141}
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003142
3143
3144
3145int32_t
3146SortMapTest(void)
3147{
3148 UsefulBuf_MAKE_STACK_UB( TestBuf, 200);
3149 QCBOREncodeContext EC;
3150 UsefulBufC EncodedAndSorted;
3151 QCBORError uErr;
3152 struct UBCompareDiagnostic CompareDiagnostics;
3153
3154
3155 /* --- Basic sort test case --- */
3156 QCBOREncode_Init(&EC, TestBuf);
3157 QCBOREncode_OpenMap(&EC);
3158 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3159 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3160 QCBOREncode_AddInt64ToMapN(&EC, 4, 4);
3161 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3162 QCBOREncode_CloseAndSortMap(&EC);
3163 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3164 if(uErr) {
3165 return 11;
3166 }
3167
3168 static const uint8_t spBasic[] = {
3169 0xA4, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04};
3170
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003171 if(UsefulBuf_Compare(EncodedAndSorted, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBasic))) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003172 return 12;
3173 }
3174
3175 /* --- Empty map sort test case --- */
3176 QCBOREncode_Init(&EC, TestBuf);
3177 QCBOREncode_OpenMap(&EC);
3178 QCBOREncode_CloseAndSortMap(&EC);
3179 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3180 if(uErr) {
3181 return 21;
3182 }
3183
3184 static const uint8_t spEmpty[] = {0xA0};
3185 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3186 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmpty),
3187 &CompareDiagnostics)) {
3188 return 22;
3189 }
3190
3191 /* --- Several levels of nested sorted maps --- */
3192 QCBOREncode_Init(&EC, TestBuf);
3193 QCBOREncode_OpenMap(&EC);
3194 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3195 QCBOREncode_OpenMapInMapN(&EC, 428);
3196 QCBOREncode_AddNULLToMap(&EC, "null");
3197 QCBOREncode_OpenArrayInMap(&EC, "array");
3198 QCBOREncode_AddSZString(&EC, "hi");
3199 QCBOREncode_AddSZString(&EC, "there");
3200 QCBOREncode_CloseArray(&EC);
3201 QCBOREncode_OpenMapInMap(&EC, "empty2");
3202 QCBOREncode_CloseAndSortMap(&EC);
3203 QCBOREncode_OpenMapInMap(&EC, "empty1");
3204 QCBOREncode_CloseAndSortMap(&EC);
3205 QCBOREncode_CloseAndSortMap(&EC);
3206 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3207 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3208 QCBOREncode_CloseAndSortMap(&EC);
3209 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3210 if(uErr) {
3211 return 31;
3212 }
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003213
3214 /* Correctly sorted.
3215 * {
3216 * 88: 1(888888),
3217 * 428: {
3218 * "null": null,
3219 * "array": [
3220 * "hi",
3221 * "there"
3222 * ],
3223 * "empty1": {},
3224 * "empty2": {}
3225 * },
3226 * "boo": true,
3227 * "three": 3
3228 * }
3229 */
3230 static const uint8_t spSorted[] = {
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003231 0xA4, 0x18, 0x58, 0xC1, 0x1A, 0x00, 0x0D, 0x90,
3232 0x38, 0x19, 0x01, 0xAC, 0xA4, 0x64, 0x6E, 0x75,
3233 0x6C, 0x6C, 0xF6, 0x65, 0x61, 0x72, 0x72, 0x61,
3234 0x79, 0x82, 0x62, 0x68, 0x69, 0x65, 0x74, 0x68,
3235 0x65, 0x72, 0x65, 0x66, 0x65, 0x6D, 0x70, 0x74,
3236 0x79, 0x31, 0xA0, 0x66, 0x65, 0x6D, 0x70, 0x74,
3237 0x79, 0x32, 0xA0, 0x63, 0x62, 0x6F, 0x6F, 0xF5,
3238 0x65, 0x74, 0x68, 0x72, 0x65, 0x65, 0x03};
3239
3240 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003241 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003242 &CompareDiagnostics)) {
3243 return 32;
3244 }
3245
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003246
3247 /* Same data items, but added in a different order */
3248 QCBOREncode_Init(&EC, TestBuf);
3249 QCBOREncode_OpenMap(&EC);
3250 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3251 QCBOREncode_OpenMapInMapN(&EC, 428);
3252 QCBOREncode_OpenMapInMap(&EC, "empty1");
3253 QCBOREncode_CloseAndSortMap(&EC);
3254 QCBOREncode_OpenArrayInMap(&EC, "array");
3255 QCBOREncode_AddSZString(&EC, "hi");
3256 QCBOREncode_AddSZString(&EC, "there");
3257 QCBOREncode_CloseArray(&EC);
3258 QCBOREncode_OpenMapInMap(&EC, "empty2");
3259 QCBOREncode_CloseAndSortMap(&EC);
3260 QCBOREncode_AddNULLToMap(&EC, "null");
3261 QCBOREncode_CloseAndSortMap(&EC);
3262 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3263 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3264 QCBOREncode_CloseAndSortMap(&EC);
3265 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3266 if(uErr) {
3267 return 31;
3268 }
3269
3270 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3271 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3272 &CompareDiagnostics)) {
3273 return 32;
3274 }
3275
3276 /* Same data items, but added in a different order */
3277 QCBOREncode_Init(&EC, TestBuf);
3278 QCBOREncode_OpenMap(&EC);
3279 QCBOREncode_AddBoolToMap(&EC, "boo", true);
3280 QCBOREncode_OpenMapInMapN(&EC, 428);
3281 QCBOREncode_OpenMapInMap(&EC, "empty1");
3282 QCBOREncode_CloseAndSortMap(&EC);
3283 QCBOREncode_OpenArrayInMap(&EC, "array");
3284 QCBOREncode_AddSZString(&EC, "hi");
3285 QCBOREncode_AddSZString(&EC, "there");
3286 QCBOREncode_CloseArray(&EC);
3287 QCBOREncode_OpenMapInMap(&EC, "empty2");
3288 QCBOREncode_CloseAndSortMap(&EC);
3289 QCBOREncode_AddNULLToMap(&EC, "null");
3290 QCBOREncode_CloseAndSortMap(&EC);
3291 QCBOREncode_AddDateEpochToMapN(&EC, 88, 888888);
3292 QCBOREncode_AddInt64ToMap(&EC, "three", 3);
3293 QCBOREncode_CloseAndSortMap(&EC);
3294 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3295 if(uErr) {
3296 return 31;
3297 }
3298
3299 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3300 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSorted),
3301 &CompareDiagnostics)) {
3302 return 32;
3303 }
3304
3305
3306
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003307 /* --- Degenerate case of everything in order --- */
3308 QCBOREncode_Init(&EC, TestBuf);
3309 QCBOREncode_OpenMap(&EC);
3310 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3311 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3312 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3313 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3314 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3315 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3316 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3317 QCBOREncode_CloseAndSortMap(&EC);
3318 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3319 if(uErr) {
3320 return 41;
3321 }
3322
3323 static const uint8_t sp6Items[] = {
3324 0xA7, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x61,
3325 0x61, 0x03, 0x61, 0x62, 0x04, 0x62, 0x61, 0x61,
3326 0x05, 0x63, 0x61, 0x61, 0x61, 0x06};
3327 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3328 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3329 &CompareDiagnostics)) {
3330 return 42;
3331 }
3332
3333 /* --- Degenerate case -- reverse order --- */
3334 QCBOREncode_Init(&EC, TestBuf);
3335 QCBOREncode_OpenMap(&EC);
3336 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3337 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3338 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3339 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3340 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3341 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3342 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3343 QCBOREncode_CloseAndSortMap(&EC);
3344 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3345 if(uErr) {
3346 return 51;
3347 }
3348
3349 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3350 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3351 &CompareDiagnostics)) {
3352 return 52;
3353 }
3354
3355 /* --- Same items, randomly out of order --- */
3356 QCBOREncode_Init(&EC, TestBuf);
3357 QCBOREncode_OpenMap(&EC);
3358 QCBOREncode_AddInt64ToMap(&EC, "aa", 5);
3359 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3360 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3361 QCBOREncode_AddInt64ToMap(&EC, "b", 4);
3362 QCBOREncode_AddInt64ToMap(&EC, "aaa", 6);
3363 QCBOREncode_AddInt64ToMap(&EC, "a", 3);
3364 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3365 QCBOREncode_CloseAndSortMap(&EC);
3366 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3367 if(uErr) {
3368 return 61;
3369 }
3370
3371 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3372 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sp6Items),
3373 &CompareDiagnostics)) {
3374 return 62;
3375 }
3376
3377 /* --- Stuff in front of and after array to sort --- */
3378 QCBOREncode_Init(&EC, TestBuf);
3379 QCBOREncode_OpenArray(&EC);
3380 QCBOREncode_AddInt64(&EC, 111);
3381 QCBOREncode_AddInt64(&EC, 222);
3382 QCBOREncode_OpenMap(&EC);
3383 QCBOREncode_AddInt64ToMapN(&EC, 0, 0);
3384 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3385 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3386 QCBOREncode_CloseAndSortMap(&EC);
3387 QCBOREncode_AddInt64(&EC, 888);
3388 QCBOREncode_AddInt64(&EC, 999);
3389 QCBOREncode_CloseArray(&EC);
3390 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3391 if(uErr) {
3392 return 71;
3393 }
3394
3395 static const uint8_t spPreItems[] = {
3396 0x85, 0x18, 0x6F, 0x18, 0xDE, 0xA3, 0x00, 0x00,
3397 0x01, 0x01, 0x02, 0x02, 0x19, 0x03, 0x78, 0x19,
3398 0x03, 0xE7};
3399 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3400 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spPreItems),
3401 &CompareDiagnostics)) {
3402 return 72;
3403 }
3404
3405 /* --- map with labels of all CBOR major types and in reverse order --- */
3406 QCBOREncode_Init(&EC, TestBuf);
3407 QCBOREncode_OpenMap(&EC);
3408
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003409 /* Adding labels directly rather than AddToMap functions */
3410
3411#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003412 QCBOREncode_AddDouble(&EC, 8.77);
3413 QCBOREncode_AddInt64(&EC, 7);
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003414#endif /* QCBOR_DISABLE_ALL_FLOAT */
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003415
3416 QCBOREncode_AddBool(&EC, true);
3417 QCBOREncode_AddInt64(&EC, 6);
3418
3419 QCBOREncode_AddDateEpoch(&EC, 88);
3420 QCBOREncode_AddInt64(&EC, 5);
3421
3422 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\xa0"));
3423 QCBOREncode_AddInt64(&EC, 4);
3424
3425 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x80"));
3426 QCBOREncode_AddInt64(&EC, 7);
3427
3428 QCBOREncode_AddInt64ToMap(&EC, "text", 3);
3429
3430 QCBOREncode_AddBytes(&EC, UsefulBuf_FromSZ("xx"));
3431 QCBOREncode_AddInt64(&EC, 2);
3432
3433 QCBOREncode_AddInt64ToMapN(&EC, 1, 1); /* Integer */
3434 QCBOREncode_CloseAndSortMap(&EC);
3435
3436 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3437 if(uErr) {
3438 return 81;
3439 }
3440
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003441#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003442 static const uint8_t spLabelTypes[] = {
3443 0xA8, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3444 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3445 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06, 0xFB,
3446 0x40, 0x21, 0x8A, 0x3D, 0x70, 0xA3, 0xD7, 0x0A,
3447 0x07};
Laurence Lundblade3b703b82024-01-27 20:17:20 -07003448#else
3449 static const uint8_t spLabelTypes[] = {
3450 0xA7, 0x01, 0x01, 0x42, 0x78, 0x78, 0x02, 0x64,
3451 0x74, 0x65, 0x78, 0x74, 0x03, 0x80, 0x07, 0xA0,
3452 0x04, 0xC1, 0x18, 0x58, 0x05, 0xF5, 0x06};
3453#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
3454
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003455 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3456 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLabelTypes),
3457 &CompareDiagnostics)) {
3458 return 82;
3459 }
3460
3461 /* --- labels are indefinitely encoded --- */
3462 QCBOREncode_Init(&EC, TestBuf);
3463 QCBOREncode_OpenMap(&EC);
3464
3465 QCBOREncode_AddInt64ToMap(&EC, "aaaa", 1);
3466
3467 QCBOREncode_AddInt64ToMap(&EC, "bb", 2);
3468
3469 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f\x61" "a" "\x61" "a" "\xff"));
3470 QCBOREncode_AddInt64(&EC, 3);
3471
3472 QCBOREncode_AddEncoded(&EC, UsefulBuf_FromSZ("\x7f" "\x61" "c" "\xff"));
3473 QCBOREncode_AddInt64(&EC, 4);
3474
3475 QCBOREncode_CloseAndSortMap(&EC);
3476
3477 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3478 if(uErr) {
3479 return 91;
3480 }
3481
3482 static const uint8_t spIndefItems[] = {
3483 0xA4, 0x62, 0x62, 0x62, 0x02, 0x64, 0x61, 0x61,
3484 0x61, 0x61, 0x01, 0x7F, 0x61, 0x61, 0x61, 0x61,
3485 0xFF, 0x03, 0x7F, 0x61, 0x63, 0xFF, 0x04};
3486 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3487 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefItems),
3488 &CompareDiagnostics)) {
3489 return 92;
3490 }
3491
3492 /* --- Indefinitely encoded maps --- */
3493 QCBOREncode_Init(&EC, TestBuf);
3494 QCBOREncode_OpenMapIndefiniteLength(&EC);
3495
3496 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "aa");
3497 QCBOREncode_CloseMapIndefiniteLength(&EC);
3498
3499 QCBOREncode_OpenArrayIndefiniteLengthInMap(&EC, "ff");
3500 QCBOREncode_CloseArrayIndefiniteLength(&EC);
3501
3502 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "zz");
3503 QCBOREncode_CloseMapIndefiniteLength(&EC);
3504
3505 QCBOREncode_OpenMapIndefiniteLengthInMap(&EC, "bb");
3506 QCBOREncode_CloseMapIndefiniteLength(&EC);
3507
3508 QCBOREncode_CloseAndSortMapIndef(&EC);
3509 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3510 if(uErr) {
3511 return 101;
3512 }
3513
3514 static const uint8_t spIndeMaps[] = {
3515 0xBF, 0x62, 0x61, 0x61, 0xBF, 0xFF, 0x62, 0x62,
3516 0x62, 0xBF, 0xFF, 0x62, 0x66, 0x66, 0x9F, 0xFF,
3517 0x62, 0x7A, 0x7A, 0xBF, 0xFF, 0xFF, 0x06, 0xFB};
3518 if(UsefulBuf_CompareWithDiagnostic(EncodedAndSorted,
3519 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndeMaps),
3520 &CompareDiagnostics)) {
3521 return 102;
3522 }
3523
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07003524
3525 /* --- Duplicate label test --- */
3526 QCBOREncode_Init(&EC, TestBuf);
3527 QCBOREncode_OpenMap(&EC);
3528 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3529 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3530 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3531 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3532 QCBOREncode_CloseAndSortMap(&EC);
3533 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3534 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3535 return 114;
3536 }
3537
3538 QCBOREncode_Init(&EC, TestBuf);
3539 QCBOREncode_OpenMap(&EC);
3540 QCBOREncode_AddInt64ToMapN(&EC, 3, 3);
3541 QCBOREncode_AddInt64ToMapN(&EC, 1, 1);
3542 QCBOREncode_AddInt64ToMapN(&EC, 1, 2);
3543 QCBOREncode_AddInt64ToMapN(&EC, 2, 2);
3544 QCBOREncode_CloseAndSortMap(&EC);
3545 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3546 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3547 return 115;
3548 }
3549
3550 QCBOREncode_Init(&EC, TestBuf);
3551 QCBOREncode_OpenMap(&EC);
3552 QCBOREncode_AddInt64ToMap(&EC, "abc", 3);
3553 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3554 QCBOREncode_AddInt64ToMap(&EC, "def", 1);
3555 QCBOREncode_AddInt64ToMap(&EC, "def", 2);
3556 QCBOREncode_CloseAndSortMap(&EC);
3557 uErr = QCBOREncode_Finish(&EC, &EncodedAndSorted);
3558 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
3559 return 116;
3560 }
3561
Laurence Lundbladed6e13022023-11-26 10:14:02 -07003562 return 0;
3563}
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003564
3565
3566#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
3567
3568#include <math.h> /* For INFINITY and NAN and isnan() */
3569
3570
3571/* Public function. See qcbor_encode_tests.h */
3572int32_t CDETest(void)
3573{
3574 QCBOREncodeContext EC;
3575 UsefulBufC Encoded;
3576 QCBORError uExpectedErr;
3577
3578 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3579
3580 QCBOREncode_SerializationCDE(&EC);
3581
3582 /* Items added to test sorting and preferred encoding of numbers and floats */
3583 QCBOREncode_OpenMap(&EC);
3584 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3585 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3586 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3587 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3588 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3589 QCBOREncode_AddUndefToMap(&EC, "t"); /* Test because dCBOR disallows */
3590
3591 QCBOREncode_CloseMap(&EC);
3592
3593 uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
3594 if(uExpectedErr != QCBOR_SUCCESS) {
3595 return 2;
3596 }
3597
3598 static const uint8_t spExpectedCDE[] = {
3599 0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3600 0x00, 0x61, 0x6B, 0xF9, 0x3C, 0x00, 0x61, 0x72,
3601 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x61, 0x74, 0xF7,
3602 0x61, 0x78, 0xF9, 0x40, 0x00};
3603
3604 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedCDE),
3605 Encoded)) {
3606 return 1;
3607 }
3608
3609
3610#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3611 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3612#else
3613 uExpectedErr = QCBOR_SUCCESS;
3614#endif
3615
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003616 /* Next, make sure methods that encode non-CDE error out */
3617 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3618 QCBOREncode_SerializationCDE(&EC);
3619 QCBOREncode_OpenMapIndefiniteLength(&EC);
3620 QCBOREncode_CloseMap(&EC);
3621 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3622 return 100;
3623 }
3624
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003625 return 0;
3626}
3627
3628/* Public function. See qcbor_encode_tests.h */
3629int32_t DCBORTest(void)
3630{
3631 QCBOREncodeContext EC;
3632 UsefulBufC Encoded;
3633 QCBORError uExpectedErr;
3634
3635 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3636
3637 QCBOREncode_SerializationdCBOR(&EC);
3638
3639 /* Items added to test sorting and preferred encoding of numbers and floats */
3640 QCBOREncode_OpenMap(&EC);
3641 QCBOREncode_AddFloatToMap(&EC, "k", 1.0f);
3642 QCBOREncode_AddInt64ToMap(&EC, "a", 1);
3643 QCBOREncode_AddDoubleToMap(&EC, "x", 2.0);
3644 QCBOREncode_AddDoubleToMap(&EC, "r", 3.4028234663852886E+38);
3645 QCBOREncode_AddDoubleToMap(&EC, "b", NAN);
3646
3647 QCBOREncode_CloseMap(&EC);
3648
3649 QCBOREncode_Finish(&EC, &Encoded);
3650
3651 static const uint8_t spExpecteddCBOR[] = {
3652 0xA5, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,
3653 0x00, 0x61, 0x6B, 0x01, 0x61, 0x72, 0xFA, 0x7F,
3654 0x7F, 0xFF, 0xFF, 0x61, 0x78, 0x02};
3655
3656 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpecteddCBOR),
3657 Encoded)) {
3658 return 1;
3659 }
3660
3661
3662#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3663 uExpectedErr = QCBOR_ERR_NOT_PREFERRED;
3664#else
3665 uExpectedErr = QCBOR_SUCCESS;
3666#endif
3667
3668 /* Next, make sure methods that encode of non-CDE error out */
3669
3670 /* Indefinite-length map */
3671 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3672 QCBOREncode_SerializationdCBOR(&EC);
3673 QCBOREncode_OpenMapIndefiniteLength(&EC);
3674 QCBOREncode_CloseMap(&EC);
3675 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3676 return 100;
3677 }
3678
3679 /* Indefinite-length array */
3680 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3681 QCBOREncode_SerializationdCBOR(&EC);
3682 QCBOREncode_OpenArrayIndefiniteLength(&EC);
3683 QCBOREncode_CloseMap(&EC);
3684 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3685 return 101;
3686 }
3687
3688 /* The "undef" special value */
3689 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3690 QCBOREncode_SerializationdCBOR(&EC);
3691 QCBOREncode_AddUndef(&EC);
3692 QCBOREncode_CloseMap(&EC);
3693 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3694 return 102;
3695 }
3696
3697 /* 65-bit negative integers */
3698 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
3699 QCBOREncode_SerializationdCBOR(&EC);
3700 QCBOREncode_AddNegativeUInt64(&EC, 1);
3701 if(QCBOREncode_GetErrorState(&EC) != uExpectedErr) {
3702 return 103;
3703 }
3704
3705 /* Improvement: when indefinite length string encoding is supported
3706 * test it here too. */
3707
3708 return 0;
3709
3710}
3711#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */