blob: 8b41af33eb0e7488ab17a54809598511ce5fcb07 [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 Lundbladedd6e76e2021-03-10 01:54:01 -07003 Copyright (c) 2018-2021, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundbladeee851742020-01-08 08:37:05 -080051//#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
76 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladeee851742020-01-08 08:37:05 -080077 printf("Position: %d Actual: 0x%x Expected: 0x%x\n",
78 (uint32_t)i,
79 ((uint8_t *)U1.ptr)[i],
80 ((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 Lundbladedd6e76e2021-03-10 01:54:01 -070099#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -0800100/*
101 Returns 0 if UsefulBufs are equal
102 Returns 1000000 + offeset if they are not equal.
103
104
105
106*/
107struct UBCompareDiagnostic {
108 uint8_t uActual;
109 uint8_t uExpected;
110 size_t uOffset;
111};
112
Laurence Lundbladeee851742020-01-08 08:37:05 -0800113static int32_t
114UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
115 UsefulBufC Expected,
116 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800117 size_t i;
118 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800119 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800120 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800121 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
122 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800123 pDiag->uOffset = i;
124 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800125 // Cast to int is OK as this is only a diagnostic and the sizes
126 // here are never over a few KB.
127 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800128 }
129 }
130 return 0;
131
132}
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -0700133#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade59289e52019-12-30 13:44:37 -0800134
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700135
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530136// One big buffer that is used by all the tests to encode into
137// Putting it in uninitialized data is better than using a lot
138// of stack. The tests should run on small devices too.
139static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140
141
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142
143/*
144 Some very minimal tests.
145 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800146int32_t BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800147{
148 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800149 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530151 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530152
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800153 QCBOREncode_OpenMap(&EC);
154 QCBOREncode_AddBoolToMapN(&EC, 66, true);
155 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800157 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700158 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530159 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 // Decode it and see that is right
164 QCBORDecodeContext DC;
165 QCBORItem Item;
166 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
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_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 QCBORDecode_GetNext(&DC, &Item);
174 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530175 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800177
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800178 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530179 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800181
182
Laurence Lundbladeee851742020-01-08 08:37:05 -0800183 // Make another encoded message with the CBOR from the previous
184 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530185 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800186 QCBOREncode_Init(&EC, MemoryForEncoded2);
187 QCBOREncode_OpenArray(&EC);
188 QCBOREncode_AddUInt64(&EC, 451);
189 QCBOREncode_AddEncoded(&EC, Encoded);
190 QCBOREncode_OpenMap(&EC);
191 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
192 QCBOREncode_CloseMap(&EC);
193 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800195 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700196 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530197 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800198 }
199 /*
200 [ // 0 1:3
201 451, // 1 1:2
202 { // 1 1:2 2:1
203 66: true // 2 1:1
204 },
205 { // 1 1:1 2:1
206 -70000: { // 2 1:1 2:1 3:1
207 66: true // 3 XXXXXX
208 }
209 }
210 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
212
213
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214 83 # array(3)
215 19 01C3 # unsigned(451)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 A1 # map(1)
220 3A 0001116F # negative(69999)
221 A1 # map(1)
222 18 42 # unsigned(66)
223 F5 # primitive(21)
224 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800226 // Decode it and see if it is OK
227 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800229 // 0 1:3
230 QCBORDecode_GetNext(&DC, &Item);
231 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530232 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800235 // 1 1:2
236 QCBORDecode_GetNext(&DC, &Item);
237 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530238 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800241 // 1 1:2 2:1
242 QCBORDecode_GetNext(&DC, &Item);
243 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530244 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800247 // 2 1:1
248 QCBORDecode_GetNext(&DC, &Item);
249 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530250 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800253 // 1 1:1 2:1
254 QCBORDecode_GetNext(&DC, &Item);
255 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530256 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800259 // 2 1:1 2:1 3:1
260 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800261 if(Item.uDataType != QCBOR_TYPE_MAP ||
262 Item.val.uCount != 1 ||
263 Item.uLabelType != QCBOR_TYPE_INT64 ||
264 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530265 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800268 // 3 XXXXXX
269 QCBORDecode_GetNext(&DC, &Item);
270 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530271 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800273
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800274 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530275 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800277
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800278 return 0;
279}
280
281
282
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530283static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700284 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530285 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
286 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
287 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
288 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
289 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
290 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
291 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
292 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700293 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530294 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
295 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
296 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
297 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
298 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
299 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
300 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
301 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
302 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
303 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
304 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
305 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
306 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
307 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
308 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
309 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
310 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
311 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
312 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
313 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
314 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
315 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
316 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
317 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
318 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700319 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
320 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530321 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
322 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
323 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
324 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
325 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
326 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
327 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
328 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
329 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
330 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
331 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
332 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
333 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
334 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
335 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
336 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
337 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
338 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
339 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
340 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
341 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
342 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
343 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
344 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
345 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
346 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
347 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
348 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
349 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
350 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
351 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
352 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
353 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
354 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
355 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
356 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
357 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
358 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
359 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
360 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
361 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
362 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
363 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
364 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
365 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
366 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
367 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
368 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
369 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
370 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
371 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
372 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
373 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
374 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
375 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
376 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
377 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
378 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700379 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530380 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
381 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
382 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
383 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
384 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
385 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
386 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
387 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
388 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
389 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
390 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
391 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
392 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
393 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
394 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
395 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
396 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
397 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
398 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
399 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
400 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
401 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
402 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
403 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
404 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
405 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
406 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
407 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
408 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
409 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
410 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
411 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
412 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
413 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700414 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530415 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
416 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
417 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
418 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
419 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
420 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
421 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
422 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
423 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
424 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
425 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
426 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
427 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
428 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
429 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
430 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
431 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
432 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
433 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
434 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
435 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
436 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
437 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
438 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
439 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
440 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
441 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
442 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
443 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
444 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
445 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
446 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
447 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
448 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
449 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
450 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
451 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
452 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
453 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
454 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
455 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
456 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
457 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
458 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
459 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
460 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
461 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
462 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
463 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
464 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
465 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
466 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
467 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
468 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
469 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
470 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
471 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
472 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
473 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
474 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
475 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
476 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
477 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
478 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
479 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
480 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
481 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
482 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
483 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
484 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
486 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
487 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
488 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490};
491
492
493static const char *szMIME = "\
494MIME-Version: 1.0\n\
495Content-Type: multipart/mixed;\n\
496boundary=\"XXXXboundary text\"\n\
497\n\
498This is a multipart message in MIME format.\n\
499\n\
500--XXXXboundary text\n\
501Content-Type: text/plain\n\
502\n\
503this is the body text\n\
504\n\
505--XXXXboundary text\n\
506Content-Type: text/plain;\n\
507Content-Disposition: attachment;\n\
508filename=\"test.txt\"\n\
509\n\
510this is the attachment text\n\
511\n\
512--XXXXboundary text--";
513
514
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800515int32_t AllAddMethodsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800516{
Laurence Lundblade323f8a92020-09-06 19:43:09 -0700517 // Improvement: this test should be broken down into several so it is more
Laurence Lundbladeee851742020-01-08 08:37:05 -0800518 // managable. Tags and labels could be more sensible
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800519 QCBOREncodeContext ECtx;
520 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800521
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530522 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800523
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800524 QCBOREncode_OpenArray(&ECtx);
525
Laurence Lundbladeee851742020-01-08 08:37:05 -0800526 // Some ints that are tagged and have strings preceeding them
527 // (not labels becase it is not a map)
Laurence Lundblade067035b2018-11-28 17:35:25 -0800528 QCBOREncode_AddSZString(&ECtx, "UINT62");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700529 QCBOREncode_AddTag(&ECtx, 100);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800530 QCBOREncode_AddUInt64(&ECtx, 89989909);
531 QCBOREncode_AddSZString(&ECtx, "INT64");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700532 QCBOREncode_AddTag(&ECtx, 76);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800533 QCBOREncode_AddInt64(&ECtx, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 QCBOREncode_AddUInt64(&ECtx,0);
535 QCBOREncode_AddInt64(&ECtx, -44);
536
537 // ints that go in maps
538 QCBOREncode_OpenMap(&ECtx);
539 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
540 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
541 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
542 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
543 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800544
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800545 // Epoch Date
546 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800547
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800548 // Epoch date with labels
549 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800550 QCBOREncode_AddDateEpochToMap(&ECtx, "LongLiveDenisRitchie", 1400000000);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
552 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
553 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800554
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 // Binary blobs
556 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
557
558 // binary blobs in maps
559 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800560 QCBOREncode_AddSZString(&ECtx, "binbin");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700561 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800562 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
563 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
565 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800567 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530568 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundbladeee851742020-01-08 08:37:05 -0800570 const char *szURL =
571 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
572 QCBOREncode_AddURI(&ECtx, UsefulBuf_FromSZ(szURL));
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530573 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
574 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530575 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800577 // text blobs in maps
578 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530579 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade067035b2018-11-28 17:35:25 -0800580 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
581 QCBOREncode_AddSZString(&ECtx, "()()()");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700582 QCBOREncode_AddTag(&ECtx, 1000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800583 QCBOREncode_AddSZString(&ECtx, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530584 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
586 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundbladeee851742020-01-08 08:37:05 -0800587 QCBOREncode_AddURIToMap(&ECtx,
588 "RFC",
589 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530590 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
591 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
592 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
593 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
594 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530595 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
596 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 QCBOREncode_CloseMap(&ECtx);
598
599 // Date strings
600 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
601 QCBOREncode_OpenMap(&ECtx);
602 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
603 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
604 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 // true / false ...
607 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
608 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800609 QCBOREncode_AddSZString(&ECtx, "dare");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700610 QCBOREncode_AddTag(&ECtx, 66);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800611 QCBOREncode_AddBool(&ECtx, true);
612 QCBOREncode_AddBoolToMap(&ECtx, "uu", false);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800613 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
614 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800616 // opening an array
617 QCBOREncode_OpenArray(&ECtx);
618 QCBOREncode_CloseArray(&ECtx);
619
620 // opening arrays in a map
621 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800622 QCBOREncode_AddSZString(&ECtx, "label and tagged empty array");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700623 QCBOREncode_AddTag(&ECtx, 1093);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800624 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800625 QCBOREncode_CloseArray(&ECtx);
626 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
627 QCBOREncode_CloseArray(&ECtx);
628 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800629 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632 // opening maps with labels and tagging
633 QCBOREncode_OpenMap(&ECtx);
634 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
635 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800636 QCBOREncode_AddSZString(&ECtx, "in a in a in a");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700637 QCBOREncode_AddTag(&ECtx, 9087);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800638 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 QCBOREncode_CloseMap(&ECtx);
640 QCBOREncode_CloseMap(&ECtx);
641 QCBOREncode_CloseMap(&ECtx);
642 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundblade067035b2018-11-28 17:35:25 -0800644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800645 // Extended simple values (these are not standard...)
646 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800647 QCBOREncode_AddSZString(&ECtx, "s1");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700648 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800649 QCBOREncode_AddSimple(&ECtx, 255);
650 QCBOREncode_AddSimpleToMap(&ECtx, "s2", 0);
651 QCBOREncode_AddSZString(&ECtx, "s3");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700652 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800653 QCBOREncode_AddSimple(&ECtx, 33);
654 QCBOREncode_AddInt64(&ECtx, 88378374); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700655 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800656 QCBOREncode_AddSimple(&ECtx, 255);
657 QCBOREncode_AddInt64(&ECtx, 89); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700658 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800659 QCBOREncode_AddSimple(&ECtx, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800660 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800661
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 // UUIDs
Laurence Lundbladeee851742020-01-08 08:37:05 -0800663 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
664 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
665 0x49, 0x43, 0x41, 0x32};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800666 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
668 QCBOREncode_OpenMap(&ECtx);
669 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
670 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
671 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673 // Bool
674 QCBOREncode_AddBool(&ECtx, true);
675 QCBOREncode_AddBool(&ECtx, false);
676 QCBOREncode_OpenMap(&ECtx);
677 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
678 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
679 QCBOREncode_CloseMap(&ECtx);
680
681
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530682 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800683 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800684 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
685 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
686 QCBOREncode_OpenMap(&ECtx);
687 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
688 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
689 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
690 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
691 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 QCBOREncode_CloseArray(&ECtx);
694
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530695 UsefulBufC Enc;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade0595e932018-11-02 22:22:47 +0700697 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800698 nReturn = -1;
699 goto Done;
700 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530702 if(CheckResults(Enc, spExpectedEncodedAll))
703 nReturn = -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800705Done:
706 return nReturn;
707}
708
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530709/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200710 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530711 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
712 3B 0000000100000000 # negative(4294967296)
713 3A FFFFFFFF # negative(4294967295)
714 3A FFFFFFFE # negative(4294967294)
715 3A FFFFFFFD # negative(4294967293)
716 3A 7FFFFFFF # negative(2147483647)
717 3A 7FFFFFFE # negative(2147483646)
718 3A 00010001 # negative(65537)
719 3A 00010000 # negative(65536)
720 39 FFFF # negative(65535)
721 39 FFFE # negative(65534)
722 39 FFFD # negative(65533)
723 39 0100 # negative(256)
724 38 FF # negative(255)
725 38 FE # negative(254)
726 38 FD # negative(253)
727 38 18 # negative(24)
728 37 # negative(23)
729 36 # negative(22)
730 20 # negative(0)
731 00 # unsigned(0)
732 00 # unsigned(0)
733 01 # unsigned(1)
734 16 # unsigned(22)
735 17 # unsigned(23)
736 18 18 # unsigned(24)
737 18 19 # unsigned(25)
738 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200739 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530740 18 FE # unsigned(254)
741 18 FF # unsigned(255)
742 19 0100 # unsigned(256)
743 19 0101 # unsigned(257)
744 19 FFFE # unsigned(65534)
745 19 FFFF # unsigned(65535)
746 1A 00010000 # unsigned(65536)
747 1A 00010001 # unsigned(65537)
748 1A 00010002 # unsigned(65538)
749 1A 7FFFFFFF # unsigned(2147483647)
750 1A 7FFFFFFF # unsigned(2147483647)
751 1A 80000000 # unsigned(2147483648)
752 1A 80000001 # unsigned(2147483649)
753 1A FFFFFFFE # unsigned(4294967294)
754 1A FFFFFFFF # unsigned(4294967295)
755 1B 0000000100000000 # unsigned(4294967296)
756 1B 0000000100000001 # unsigned(4294967297)
757 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
758 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
759 */
760static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200761 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
763 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
764 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
765 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
766 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
767 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
768 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
769 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
770 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
771 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200772 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
773 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
774 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
775 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
776 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
777 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
778 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
779 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
780 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
781 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
782 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
783 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
784 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800785
786/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800787
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788 Test the generation of integers. This also ends up testing
789 encoding of all the different lengths. It encodes integers
790 of many lengths and values, especially around the boundaries
791 for different types of integers. It compares the output
792 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800793
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800795int32_t IntegerValuesTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796{
797 QCBOREncodeContext ECtx;
798 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800799
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530800 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800801 QCBOREncode_OpenArray(&ECtx);
802
803 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
804 QCBOREncode_AddInt64(&ECtx, -4294967297);
805 QCBOREncode_AddInt64(&ECtx, -4294967296);
806 QCBOREncode_AddInt64(&ECtx, -4294967295);
807 QCBOREncode_AddInt64(&ECtx, -4294967294);
808 QCBOREncode_AddInt64(&ECtx, -2147483648);
809 QCBOREncode_AddInt64(&ECtx, -2147483647);
810 QCBOREncode_AddInt64(&ECtx, -65538);
811 QCBOREncode_AddInt64(&ECtx, -65537);
812 QCBOREncode_AddInt64(&ECtx, -65536);
813 QCBOREncode_AddInt64(&ECtx, -65535);
814 QCBOREncode_AddInt64(&ECtx, -65534);
815 QCBOREncode_AddInt64(&ECtx, -257);
816 QCBOREncode_AddInt64(&ECtx, -256);
817 QCBOREncode_AddInt64(&ECtx, -255);
818 QCBOREncode_AddInt64(&ECtx, -254);
819 QCBOREncode_AddInt64(&ECtx, -25);
820 QCBOREncode_AddInt64(&ECtx, -24);
821 QCBOREncode_AddInt64(&ECtx, -23);
822 QCBOREncode_AddInt64(&ECtx, -1);
823 QCBOREncode_AddInt64(&ECtx, 0);
824 QCBOREncode_AddUInt64(&ECtx, 0ULL);
825 QCBOREncode_AddInt64(&ECtx, 1);
826 QCBOREncode_AddInt64(&ECtx, 22);
827 QCBOREncode_AddInt64(&ECtx, 23);
828 QCBOREncode_AddInt64(&ECtx, 24);
829 QCBOREncode_AddInt64(&ECtx, 25);
830 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200831 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800832 QCBOREncode_AddInt64(&ECtx, 254);
833 QCBOREncode_AddInt64(&ECtx, 255);
834 QCBOREncode_AddInt64(&ECtx, 256);
835 QCBOREncode_AddInt64(&ECtx, 257);
836 QCBOREncode_AddInt64(&ECtx, 65534);
837 QCBOREncode_AddInt64(&ECtx, 65535);
838 QCBOREncode_AddInt64(&ECtx, 65536);
839 QCBOREncode_AddInt64(&ECtx, 65537);
840 QCBOREncode_AddInt64(&ECtx, 65538);
841 QCBOREncode_AddInt64(&ECtx, 2147483647);
842 QCBOREncode_AddInt64(&ECtx, 2147483647);
843 QCBOREncode_AddInt64(&ECtx, 2147483648);
844 QCBOREncode_AddInt64(&ECtx, 2147483649);
845 QCBOREncode_AddInt64(&ECtx, 4294967294);
846 QCBOREncode_AddInt64(&ECtx, 4294967295);
847 QCBOREncode_AddInt64(&ECtx, 4294967296);
848 QCBOREncode_AddInt64(&ECtx, 4294967297);
849 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
850 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800851
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800852 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800853
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530854 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700855 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800856 nReturn = -1;
857 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800858
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530859 if(CheckResults(Enc, spExpectedEncodedInts))
860 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800861
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800862 return(nReturn);
863}
864
865
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530866/*
867 85 # array(5)
868 F5 # primitive(21)
869 F4 # primitive(20)
870 F6 # primitive(22)
871 F7 # primitive(23)
872 A1 # map(1)
873 65 # text(5)
874 554E446566 # "UNDef"
875 F7 # primitive(23)
876 */
877static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
879
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800880int32_t SimpleValuesTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800881{
882 QCBOREncodeContext ECtx;
883 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800884
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530885 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800886 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900887
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
889 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
890 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
891 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800892
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800893 QCBOREncode_OpenMap(&ECtx);
894
895 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
896 QCBOREncode_CloseMap(&ECtx);
897
898 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530900 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700901 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800902 nReturn = -1;
903 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800904
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530905 if(CheckResults(ECBOR, spExpectedEncodedSimple))
906 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800907
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908 return(nReturn);
909}
910
Jan Jongboom47d86c52019-07-25 08:54:16 +0200911/*
912 9F # array(5)
913 F5 # primitive(21)
914 F4 # primitive(20)
915 F6 # primitive(22)
916 F7 # primitive(23)
917 BF # map(1)
918 65 # text(5)
919 554E446566 # "UNDef"
920 F7 # primitive(23)
921 FF # break
922 FF # break
923 */
924static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
925 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
926
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800927int32_t SimpleValuesIndefiniteLengthTest1()
Jan Jongboom47d86c52019-07-25 08:54:16 +0200928{
929 QCBOREncodeContext ECtx;
930 int nReturn = 0;
931
932 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
933 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
934
935 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
936 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
937 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
938 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
939
940 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
941
942 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
943 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
944
945 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
946
947 UsefulBufC ECBOR;
948 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
949 nReturn = -1;
950 }
951
952 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
953 return -2;
954
955 return(nReturn);
956}
957
Jan Jongboom5d827882019-08-07 12:51:15 +0200958/*
959A5 # map(5)
960 63 # text(3)
961 617272 # "arr"
962 98 1F # array(31)
963 00 # unsigned(0)
964 01 # unsigned(1)
965 02 # unsigned(2)
966 03 # unsigned(3)
967 04 # unsigned(4)
968 05 # unsigned(5)
969 06 # unsigned(6)
970 07 # unsigned(7)
971 08 # unsigned(8)
972 09 # unsigned(9)
973 0A # unsigned(10)
974 0B # unsigned(11)
975 0C # unsigned(12)
976 0D # unsigned(13)
977 0E # unsigned(14)
978 0F # unsigned(15)
979 10 # unsigned(16)
980 11 # unsigned(17)
981 12 # unsigned(18)
982 13 # unsigned(19)
983 14 # unsigned(20)
984 15 # unsigned(21)
985 16 # unsigned(22)
986 17 # unsigned(23)
987 18 18 # unsigned(24)
988 18 19 # unsigned(25)
989 18 1A # unsigned(26)
990 18 1B # unsigned(27)
991 18 1C # unsigned(28)
992 18 1D # unsigned(29)
993 18 1E # unsigned(30)
994 63 # text(3)
995 6D6170 # "map"
996 B8 1F # map(31)
997 61 # text(1)
998 61 # "a"
999 00 # unsigned(0)
1000 61 # text(1)
1001 62 # "b"
1002 01 # unsigned(1)
1003 61 # text(1)
1004 63 # "c"
1005 02 # unsigned(2)
1006 61 # text(1)
1007 64 # "d"
1008 03 # unsigned(3)
1009 61 # text(1)
1010 65 # "e"
1011 04 # unsigned(4)
1012 61 # text(1)
1013 66 # "f"
1014 05 # unsigned(5)
1015 61 # text(1)
1016 67 # "g"
1017 06 # unsigned(6)
1018 61 # text(1)
1019 68 # "h"
1020 07 # unsigned(7)
1021 61 # text(1)
1022 69 # "i"
1023 08 # unsigned(8)
1024 61 # text(1)
1025 6A # "j"
1026 09 # unsigned(9)
1027 61 # text(1)
1028 6B # "k"
1029 0A # unsigned(10)
1030 61 # text(1)
1031 6C # "l"
1032 0B # unsigned(11)
1033 61 # text(1)
1034 6D # "m"
1035 0C # unsigned(12)
1036 61 # text(1)
1037 6E # "n"
1038 0D # unsigned(13)
1039 61 # text(1)
1040 6F # "o"
1041 0E # unsigned(14)
1042 61 # text(1)
1043 70 # "p"
1044 0F # unsigned(15)
1045 61 # text(1)
1046 71 # "q"
1047 10 # unsigned(16)
1048 61 # text(1)
1049 72 # "r"
1050 11 # unsigned(17)
1051 61 # text(1)
1052 73 # "s"
1053 12 # unsigned(18)
1054 61 # text(1)
1055 74 # "t"
1056 13 # unsigned(19)
1057 61 # text(1)
1058 75 # "u"
1059 14 # unsigned(20)
1060 61 # text(1)
1061 76 # "v"
1062 15 # unsigned(21)
1063 61 # text(1)
1064 77 # "w"
1065 16 # unsigned(22)
1066 61 # text(1)
1067 78 # "x"
1068 17 # unsigned(23)
1069 61 # text(1)
1070 79 # "y"
1071 18 18 # unsigned(24)
1072 61 # text(1)
1073 7A # "z"
1074 18 19 # unsigned(25)
1075 61 # text(1)
1076 41 # "A"
1077 18 1A # unsigned(26)
1078 61 # text(1)
1079 42 # "B"
1080 18 1B # unsigned(27)
1081 61 # text(1)
1082 43 # "C"
1083 18 1C # unsigned(28)
1084 61 # text(1)
1085 44 # "D"
1086 18 1D # unsigned(29)
1087 61 # text(1)
1088 45 # "E"
1089 18 1E # unsigned(30)
1090 65 # text(5)
1091 6D696E3331 # "min31"
1092 38 1E # negative(30)
1093 66 # text(6)
1094 706C75733331 # "plus31"
1095 18 1F # unsigned(31)
1096 63 # text(3)
1097 737472 # "str"
1098 78 1F # text(31)
1099 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1100 */
1101static const uint8_t EncodeLengthThirtyone[] = {
1102 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1103 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1104 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1105 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1106 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1107 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1108 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1109 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1110 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1111 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1112 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1113 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1114 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1115 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1116 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1117 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1118 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1119 0x31
1120};
1121
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001122int32_t EncodeLengthThirtyoneTest()
Jan Jongboom5d827882019-08-07 12:51:15 +02001123{
1124 QCBOREncodeContext ECtx;
1125 int nReturn = 0;
1126
1127 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1128 QCBOREncode_OpenMap(&ECtx);
1129
1130 // add array with 31 items
1131 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1132 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001133 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001134 }
1135 QCBOREncode_CloseArray(&ECtx);
1136
1137 // add map with 31 items
1138 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001139 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001140 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001141 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001142 if (ix < 26) c = c + ix;
1143 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001144 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001145 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1146 }
1147 QCBOREncode_CloseMap(&ECtx);
1148
1149 // add -31 and +31
1150 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1151 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1152
1153 // add string with length 31
1154 const char *str = "testtesttesttesttesttestqcbor11";
1155 UsefulBufC str_b = { str, 31 };
1156 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1157
1158 QCBOREncode_CloseMap(&ECtx);
1159
1160 UsefulBufC ECBOR;
1161 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1162 nReturn = -1;
1163 }
1164
1165 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1166 return -2;
1167
1168 return(nReturn);
1169}
1170
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301171
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301172/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001173 * [ "2013-03-21T20:04:00Z",
1174 * 0("2013-03-21T20:04:00Z"),
1175 * 1363896240,
1176 * 1(1363896240),
1177 * 100(-10676),
1178 * 3994,
1179 * 1004("1940-10-09"),
1180 * "1980-12-08",
1181 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1182 * "SD": 1(999),
1183 * "Sample Date from RFC 8943": "1985-04-12",
1184 * 42: 1004("1985-04-12T23:20:50.52Z"),
1185 * "SY": 100(-10676),
1186 * 45: 3994
1187 * }
1188 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301189 */
1190static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001191 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1192 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1193 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1194 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1195 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1196 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1197 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1198 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1199 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1200 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1201 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1202 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1203 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1204 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1205 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1206 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1207 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1208 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1209 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1210 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1211 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1212 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1213 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001215int32_t EncodeDateTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216{
1217 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001218
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301219 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 QCBOREncode_OpenArray(&ECtx);
1222
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001223 /* The values are taken from the CBOR RFCs */
1224 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1225 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1226 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1227 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1228 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1229 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1230 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1231 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001233 QCBOREncode_OpenMap(&ECtx);
1234
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001235 QCBOREncode_AddDateStringToMap(&ECtx,
1236 "Sample Date from RFC 3339",
1237 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001238 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001239 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1240 "Sample Date from RFC 8943",
1241 QCBOR_ENCODE_AS_BORROWED,
1242 "1985-04-12");
1243 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1244 42,
1245 QCBOR_ENCODE_AS_TAG,
1246 "1985-04-12T23:20:50.52Z");
1247 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1248 "SY",
1249 QCBOR_ENCODE_AS_TAG,
1250 -10676);
1251 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1252 45,
1253 QCBOR_ENCODE_AS_BORROWED,
1254 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001256 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 QCBOREncode_CloseArray(&ECtx);
1259
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301260 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001261 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001262 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001263 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001264
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301265 if(CheckResults(ECBOR, spExpectedEncodedDates))
1266 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001267
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001268 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001269}
1270
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301271
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001272int32_t ArrayNestingTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001273{
1274 QCBOREncodeContext ECtx;
1275 int i;
1276 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301278 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001279 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1280 QCBOREncode_OpenArray(&ECtx);
1281 }
1282 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1283 QCBOREncode_CloseArray(&ECtx);
1284 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001285 UsefulBufC Encoded;
1286 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001287 nReturn = -1;
1288 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001289
1290 return(nReturn);
1291}
1292
1293
1294
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001295int32_t ArrayNestingTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296{
1297 QCBOREncodeContext ECtx;
1298 int i;
1299 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001300
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301301 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1303 QCBOREncode_OpenArray(&ECtx);
1304 }
1305 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1306 QCBOREncode_CloseArray(&ECtx);
1307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001308
Laurence Lundblade0595e932018-11-02 22:22:47 +07001309 UsefulBufC Encoded;
1310 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 nReturn = -1;
1312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314 return(nReturn);
1315}
1316
1317
1318
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001319int32_t ArrayNestingTest3()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001320{
1321 QCBOREncodeContext ECtx;
1322 int i;
1323 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301325 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1327 QCBOREncode_OpenArray(&ECtx);
1328 }
1329 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1330 QCBOREncode_CloseArray(&ECtx);
1331 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001332 UsefulBufC Encoded;
1333 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 nReturn = -1;
1335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001337 return(nReturn);
1338}
1339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001340
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301341/*
1342 81 # array(1)
1343 81 # array(1)
1344 81 # array(1)
1345 81 # array(1)
1346 80 # array(0)
1347*/
1348static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301351/*
1352 82 # array(2)
1353 81 # array(1)
1354 81 # array(1)
1355 81 # array(1)
1356 81 # array(1)
1357 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001358 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301359 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1360 3B 0000000100000000 # negative(4294967296)
1361 3A FFFFFFFF # negative(4294967295)
1362 3A FFFFFFFE # negative(4294967294)
1363 3A FFFFFFFD # negative(4294967293)
1364 3A 7FFFFFFF # negative(2147483647)
1365 3A 7FFFFFFE # negative(2147483646)
1366 3A 00010001 # negative(65537)
1367 3A 00010000 # negative(65536)
1368 39 FFFF # negative(65535)
1369 39 FFFE # negative(65534)
1370 39 FFFD # negative(65533)
1371 39 0100 # negative(256)
1372 38 FF # negative(255)
1373 38 FE # negative(254)
1374 38 FD # negative(253)
1375 38 18 # negative(24)
1376 37 # negative(23)
1377 36 # negative(22)
1378 20 # negative(0)
1379 00 # unsigned(0)
1380 00 # unsigned(0)
1381 01 # unsigned(1)
1382 16 # unsigned(22)
1383 17 # unsigned(23)
1384 18 18 # unsigned(24)
1385 18 19 # unsigned(25)
1386 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001387 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301388 18 FE # unsigned(254)
1389 18 FF # unsigned(255)
1390 19 0100 # unsigned(256)
1391 19 0101 # unsigned(257)
1392 19 FFFE # unsigned(65534)
1393 19 FFFF # unsigned(65535)
1394 1A 00010000 # unsigned(65536)
1395 1A 00010001 # unsigned(65537)
1396 1A 00010002 # unsigned(65538)
1397 1A 7FFFFFFF # unsigned(2147483647)
1398 1A 7FFFFFFF # unsigned(2147483647)
1399 1A 80000000 # unsigned(2147483648)
1400 1A 80000001 # unsigned(2147483649)
1401 1A FFFFFFFE # unsigned(4294967294)
1402 1A FFFFFFFF # unsigned(4294967295)
1403 1B 0000000100000000 # unsigned(4294967296)
1404 1B 0000000100000001 # unsigned(4294967297)
1405 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1406 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1407 */
1408static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001409 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001410 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1411 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1412 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1413 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1414 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1415 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1416 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1417 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1418 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1419 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1420 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001421 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1422 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1423 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1424 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1425 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1426 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1427 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1428 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1429 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1430 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1431 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1432 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1433 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001434
1435
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001436int32_t EncodeRawTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001438 QCBOREncodeContext ECtx;
1439
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301440 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001441 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301442 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1443 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001445
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001446 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001447
Laurence Lundblade0595e932018-11-02 22:22:47 +07001448 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001449 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301452 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001453 return -5;
1454 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001455
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001456 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457}
1458
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301459/*
1460 This returns a pointer to spBigBuf
1461 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001462static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463{
1464 QCBOREncodeContext ECtx;
1465 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001466
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 *pEncoded = NULL;
1468 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301469 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001470
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 // loop runs CBOR encoding twice. First with no buffer to
1472 // calucate the length so buffer can be allocated correctly,
1473 // and last with the buffer to do the actual encoding
1474 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301475 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001476 QCBOREncode_OpenMap(&ECtx);
1477 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1478 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1479 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1480 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1481 QCBOREncode_CloseArray(&ECtx);
1482 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1483 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1484 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1485 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1486 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1487 QCBOREncode_CloseMap(&ECtx);
1488 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001489
Laurence Lundblade0595e932018-11-02 22:22:47 +07001490 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 goto Done;
1492 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301493 if(uFirstSizeEstimate != *pEncodedLen) {
1494 nReturn = 1;
1495 } else {
1496 nReturn = 0;
1497 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 goto Done;
1499 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301500 *pEncoded = spBigBuf;
1501 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001502
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001504
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 Done:
1506 return(nReturn);
1507}
1508
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301509/*
1510 A3 # map(3)
1511 6D # text(13)
1512 666972737420696E7465676572 # "first integer"
1513 18 2A # unsigned(42)
1514 77 # text(23)
1515 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1516 82 # array(2)
1517 67 # text(7)
1518 737472696E6731 # "string1"
1519 67 # text(7)
1520 737472696E6732 # "string2"
1521 6C # text(12)
1522 6D617020696E2061206D6170 # "map in a map"
1523 A4 # map(4)
1524 67 # text(7)
1525 62797465732031 # "bytes 1"
1526 44 # bytes(4)
1527 78787878 # "xxxx"
1528 67 # text(7)
1529 62797465732032 # "bytes 2"
1530 44 # bytes(4)
1531 79797979 # "yyyy"
1532 6B # text(11)
1533 616E6F7468657220696E74 # "another int"
1534 18 62 # unsigned(98)
1535 66 # text(6)
1536 746578742032 # "text 2"
1537 78 1E # text(30)
1538 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1539 */
1540static const uint8_t spValidMapEncoded[] = {
1541 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1542 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1543 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1544 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1545 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1546 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1547 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1548 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1549 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1550 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1551 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1552 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1553 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1554 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1555 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1556 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557
1558
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001559int32_t MapEncodeTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560{
1561 uint8_t *pEncodedMaps;
1562 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301565 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001566 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301569 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1570 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 return(nReturn);
1573}
1574
1575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576/*
1577 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001578
Laurence Lundbladeee851742020-01-08 08:37:05 -08001579 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1580 CBOR_SIMPLEV_NULL
1581 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 @param[in] szAlexString Diagnostic code.
1583 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001584 @param[in/out] pnLen Size of pOut buffer when called; length of data
1585 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 @return
1588 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 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 -08001591 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001593
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001594 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1595 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 */
1598
Laurence Lundbladeee851742020-01-08 08:37:05 -08001599static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001600FormatRTICResults(uint8_t uRResult,
1601 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001602 const char *szType,
1603 const char *szAlexString,
1604 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605{
1606 // Buffer that the result will be written in to
1607 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001608 // QCBOREncode will never write off the end of this buffer. If it won't
1609 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 // Context for the encoder
1612 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301613 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001615 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1616 // Contents are label / value pairs
1617 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001620
Laurence Lundbladeee851742020-01-08 08:37:05 -08001621 // The result: 0 if scan happened and found nothing; 1 if it happened and
1622 // found something wrong; 2 if it didn't happen
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001623 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624
1625 // Add the diagnostic code
1626 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1627
1628 // Add a time stamp
1629 if(time) {
1630 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1631 }
1632
1633 // Add the diagnostic code
1634 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1635
1636 // Open a subordinate map for telemtry data
1637 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1638
1639 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001641 // Add a few fake integers and buffers for now.
1642 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1643
1644 // Add a few fake integers and buffers for now.
1645 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1646
1647 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301648 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001649 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001650
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001651 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1652 }
1653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001655 // Close the telemetry map
1656 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001657
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001658 // Close the map
1659 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001660
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301661 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundblade0595e932018-11-02 22:22:47 +07001663 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001664
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301665 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001666}
1667
1668
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301669/*
1670 A5 # map(5)
1671 69 # text(9)
1672 696E74656772697479 # "integrity"
1673 F4 # primitive(20)
1674 64 # text(4)
1675 74797065 # "type"
1676 66 # text(6)
1677 726563656E74 # "recent"
1678 64 # text(4)
1679 74696D65 # "time"
1680 C1 # tag(1)
1681 1A 580D4172 # unsigned(1477263730)
1682 64 # text(4)
1683 64696167 # "diag"
1684 6A # text(10)
1685 30784131654335303031 # "0xA1eC5001"
1686 69 # text(9)
1687 74656C656D65747279 # "telemetry"
1688 A3 # map(3)
1689 69 # text(9)
1690 53686F652053697A65 # "Shoe Size"
1691 0C # unsigned(12)
1692 62 # text(2)
1693 4951 # "IQ"
1694 1A FFFFFFFF # unsigned(4294967295)
1695 77 # text(23)
1696 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1697 48 # bytes(8)
1698 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1699 */
1700static const uint8_t spExpectedRTIC[] = {
1701 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1702 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1703 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1704 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1705 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1706 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1707 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1708 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1709 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1710 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1711 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1712 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001713
1714
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001715int32_t RTICResultsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001716{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001717 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301718 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301719 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301720 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001721 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301722 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001723
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301724 if(CheckResults(Encoded, spExpectedRTIC)) {
1725 return -2;
1726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001727
1728 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001729}
1730
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301731
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301732/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001733 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001734
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301735 82 # array(2)
1736 19 01C3 # unsigned(451)
1737 43 # bytes(3)
1738 1901D2 # "\x19\x01\xD2"
1739*/
1740static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301741
Laurence Lundblade684aec22018-10-12 19:33:53 +08001742/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001743 81 #array(1)
1744 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1745 */
1746static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1747
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001748static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1749
Laurence Lundbladeda532272019-04-07 11:40:17 -07001750/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001751 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001752 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301753int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001754{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001755 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001756
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001757 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301758 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001759
Laurence Lundblade684aec22018-10-12 19:33:53 +08001760 QCBOREncode_OpenArray(&EC);
1761 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001762
Laurence Lundblade684aec22018-10-12 19:33:53 +08001763 QCBOREncode_BstrWrap(&EC);
1764 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001765
Laurence Lundblade684aec22018-10-12 19:33:53 +08001766 UsefulBufC Wrapped;
1767 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001768
Laurence Lundblade684aec22018-10-12 19:33:53 +08001769 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001770
Laurence Lundblade684aec22018-10-12 19:33:53 +08001771 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001772 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001773 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001774 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001775
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301776 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001777 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001778 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001779
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001780 // Second test - see if the length of the wrapped
1781 // bstr is correct. Also tests bstr wrapping
1782 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001783 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1784 QCBOREncode_OpenArray(&EC);
1785 QCBOREncode_BstrWrap(&EC);
1786 QCBOREncode_OpenArray(&EC);
1787 QCBOREncode_AddNULL(&EC);
1788 QCBOREncode_CloseArray(&EC);
1789 UsefulBufC BStr;
1790 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001791 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1792 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001793 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001794 return -5;
1795 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001796
Laurence Lundbladeda532272019-04-07 11:40:17 -07001797 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1798 // bstr wrapping use cases.
1799 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1800 QCBOREncode_Init(&EC, StuffBuf);
1801 QCBOREncode_OpenArray(&EC);
1802 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1803 QCBOREncode_CloseArray(&EC);
1804 if(QCBOREncode_Finish(&EC, &Encoded)) {
1805 return -6;
1806 }
1807 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1808 return -7;
1809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001810
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001811 // Fourth test, cancelling a byte string
1812 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1813
1814 QCBOREncode_OpenArray(&EC);
1815 QCBOREncode_AddUInt64(&EC, 451);
1816
1817 QCBOREncode_BstrWrap(&EC);
1818 QCBOREncode_CancelBstrWrap(&EC);
1819
1820
1821 QCBOREncode_AddUInt64(&EC, 42);
1822 QCBOREncode_CloseArray(&EC);
1823 if(QCBOREncode_Finish(&EC, &Encoded)) {
1824 return -8;
1825 }
1826 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1827 return -9;
1828 }
1829
1830 QCBORError uErr;
1831#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1832 // Fifth test, failed cancelling
1833 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1834
1835 QCBOREncode_OpenArray(&EC);
1836 QCBOREncode_AddUInt64(&EC, 451);
1837
1838 QCBOREncode_BstrWrap(&EC);
1839 QCBOREncode_AddUInt64(&EC, 99);
1840 QCBOREncode_CancelBstrWrap(&EC);
1841
1842 QCBOREncode_AddUInt64(&EC, 42);
1843 QCBOREncode_CloseArray(&EC);
1844 uErr = QCBOREncode_Finish(&EC, &Encoded);
1845 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1846 return -10;
1847 }
1848#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1849
1850 // Sixth test, another cancel, but the error is not caught
1851 // This use will produce unintended CBOR. The error
1852 // is not caught because it would require tracking state
1853 // for QCBOREncode_BstrWrapInMapN.
1854 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1855
1856 QCBOREncode_OpenMap(&EC);
1857 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1858
1859 QCBOREncode_BstrWrapInMapN(&EC, 55);
1860 QCBOREncode_CancelBstrWrap(&EC);
1861
1862 QCBOREncode_CloseMap(&EC);
1863 uErr = QCBOREncode_Finish(&EC, &Encoded);
1864 if(uErr != QCBOR_SUCCESS) {
1865 return -11;
1866 }
1867
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001868 return 0;
1869}
1870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001871
1872
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001873int32_t BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001874{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001875 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001876 UsefulBufC Wrapped;
1877 UsefulBufC Encoded2;
1878 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001879
1880#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1881 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001882
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301883 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001884
Laurence Lundblade684aec22018-10-12 19:33:53 +08001885 QCBOREncode_OpenArray(&EC);
1886 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001887
Laurence Lundblade684aec22018-10-12 19:33:53 +08001888 QCBOREncode_BstrWrap(&EC);
1889 QCBOREncode_AddUInt64(&EC, 466);
1890 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001891
Laurence Lundblade684aec22018-10-12 19:33:53 +08001892 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001893
Laurence Lundblade684aec22018-10-12 19:33:53 +08001894 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001895
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001896 uError = QCBOREncode_Finish(&EC, &Encoded2);
1897 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001898 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001899 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001900
Laurence Lundbladeee851742020-01-08 08:37:05 -08001901 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301902 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001903 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001904 uError = QCBOREncode_Finish(&EC, &Encoded2);
1905 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001906 return (int32_t)(200 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001907 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001908#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001909
Laurence Lundblade684aec22018-10-12 19:33:53 +08001910 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301911 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001912 for(int i = 1; i < 18; i++) {
1913 QCBOREncode_BstrWrap(&EC);
1914 }
1915 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001916
Laurence Lundblade684aec22018-10-12 19:33:53 +08001917 for(int i = 1; i < 18; i++) {
1918 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001920
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001921 uError = QCBOREncode_Finish(&EC, &Encoded2);
1922 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001923 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001924 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001925
Laurence Lundblade684aec22018-10-12 19:33:53 +08001926 return 0;
1927}
1928
1929
Laurence Lundblade684aec22018-10-12 19:33:53 +08001930/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001931 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001932
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001933 [
1934 h'82004E82014B8202488203458204428105',
1935 {
1936 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1937 8426568656C6C6F18234BA2131318436568656C6C6F'
1938 }
1939 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001940
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001941 Unwrapping the first byte string in the above gives
1942 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301943
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001944 Unwrapping again, the byte string immediately above gives
1945 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301946
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001947 ...
1948
1949 Unrapping the second byte string in the top-level CBOR above gives
1950 {16: 16,
1951 64: "hello",
1952 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1953 }
1954
1955 Unwrapping again, the byte string immediately above gives
1956 {17: 17,
1957 65: "hello",
1958 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
1959 }
1960
1961 ...
1962
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301963 */
1964static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001965{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001966 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
1967 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
1968 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
1969 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
1970 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
1971 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
1972 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
1973 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1974 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
1975 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08001976};
1977
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001978
1979/*
1980 Get an int64 out of the decoder or fail.
1981 */
1982static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001983{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001984 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001985 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001986
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001987 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001988 if(nReturn) {
1989 return nReturn;
1990 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08001991 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001992 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001993 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001994
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001995 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001996 return 0;
1997}
1998
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001999/*
2000 Get an array out of the decoder or fail.
2001 */
2002static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002003{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002004 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002005 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002006
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002007 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002008 if(nReturn) {
2009 return nReturn;
2010 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002011 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2012 return -1;
2013 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002014
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002015 *pInt = Item.val.uCount;
2016 return 0;
2017}
2018
2019/*
2020 Get a map out of the decoder or fail.
2021 */
2022static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2023{
2024 QCBORItem Item;
2025 int32_t nReturn;
2026
2027 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002028 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002029 return nReturn;
2030 }
2031 if(Item.uDataType != QCBOR_TYPE_MAP) {
2032 return -1;
2033 }
2034
2035 *pInt = Item.val.uCount;
2036 return 0;
2037}
2038
2039/*
2040 Get a byte string out of the decoder or fail.
2041 */
2042static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2043{
2044 QCBORItem Item;
2045 int32_t nReturn;
2046
2047 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2048 if(nReturn) {
2049 return nReturn;
2050 }
2051 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002052 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002053 }
2054
2055 *pBstr = Item.val.string;
2056 return 0;
2057}
2058
2059/*
2060 Get a byte string out of the decoder or fail.
2061 */
2062static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2063{
2064 QCBORItem Item;
2065 int nReturn;
2066
2067 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2068 if(nReturn) {
2069 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002070 }
2071 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002072 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002073 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002074
2075 *pTstr = Item.val.string;
2076 return 0;
2077}
2078
2079
2080/*
2081 Recursively decode array containing a little CBOR and a bstr wrapped array
2082 with a little CBOR and a bstr wrapped array...
2083
2084 Part of bstr_wrap_nest_test.
2085 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2086{
2087 int64_t nInt;
2088 UsefulBufC Bstr;
2089 uint16_t nArrayCount;
2090 QCBORDecodeContext DC;
2091 int32_t nResult;
2092
2093 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2094
2095 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2096 return -10;
2097 }
2098
2099 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002100 return -11;
2101 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002102
2103 nResult = GetByteString(&DC, &Bstr);
2104 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2105 if(nArrayCount != 1) {
2106 return -12;
2107 } else {
2108 // successful exit
2109 return 0;
2110 }
2111 }
2112 if(nResult) {
2113 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002114 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002115
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002116 // tail recursion; good compilers will reuse the stack frame
2117 return DecodeNextNested(Bstr);
2118}
2119
2120
2121/*
2122 Recursively decode map containing a little CBOR and a bstr wrapped map
2123 with a little CBOR and a bstr wrapped map...
2124
2125 Part of bstr_wrap_nest_test.
2126 */
2127static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2128{
2129 int32_t nResult;
2130 uint16_t nMapCount;
2131 int64_t nInt;
2132 UsefulBufC Bstr;
2133 QCBORDecodeContext DC;
2134
2135 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2136
2137 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2138 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002141 if(GetInt64(&DC, &nInt)) {
2142 return -21;
2143 }
2144
2145 // The "hello"
2146 if(GetTextString(&DC, &Bstr)) {
2147 return -22;
2148 }
2149
2150 nResult = GetByteString(&DC, &Bstr);
2151 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2152 if(nMapCount == 2) {
2153 // successful exit
2154 return 0;
2155 } else {
2156 return -23;
2157 }
2158 }
2159
2160 if(nResult) {
2161 return -24;
2162 }
2163
2164 // tail recursion; good compilers will reuse the stack frame
2165 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002166}
2167
2168
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002169int32_t BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002170{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002171 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302172 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002173
Laurence Lundblade684aec22018-10-12 19:33:53 +08002174 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002175 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002177 QCBOREncode_OpenArray(&EC);
2178
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002179 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002180 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002181 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002182 QCBOREncode_AddInt64(&EC, i);
2183 }
2184 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2185 QCBOREncode_CloseArray(&EC);
2186 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002188
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002189 QCBOREncode_OpenMap(&EC);
2190 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2191 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2192 QCBOREncode_OpenMap(&EC);
2193 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002194 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002195 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002196
2197 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2198 QCBOREncode_CloseMap(&EC);
2199 QCBOREncode_CloseBstrWrap(&EC, NULL);
2200 }
2201 QCBOREncode_CloseMap(&EC);
2202
Laurence Lundblade684aec22018-10-12 19:33:53 +08002203 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002204
Laurence Lundblade684aec22018-10-12 19:33:53 +08002205 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002206 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002207 return -1;
2208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundblade684aec22018-10-12 19:33:53 +08002210 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302211 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002212 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade684aec22018-10-12 19:33:53 +08002215 // ---- Decode it and see if it is OK ------
2216 QCBORDecodeContext DC;
2217 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002218
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002219 UsefulBufC Bstr;
2220 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002222 // Array surrounding the the whole thing
2223 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002224 return -3;
2225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002227 // Get the byte string wrapping some array stuff
2228 if(GetByteString(&DC, &Bstr)) {
2229 return -4;
2230 }
2231
2232 // Decode the wrapped nested structure
2233 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002234 if(nReturn) {
2235 return nReturn;
2236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002237
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002238 // A map enclosing some map-oriented bstr wraps
2239 if(GetMap(&DC, &nArrayCount)) {
2240 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002243 // Get the byte string wrapping some array stuff
2244 if(GetByteString(&DC, &Bstr)) {
2245 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002248 // Decode the wrapped nested structure
2249 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002250 if(nReturn) {
2251 return nReturn;
2252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002253
Laurence Lundblade684aec22018-10-12 19:33:53 +08002254 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002255 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002256 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002257
Laurence Lundblade684aec22018-10-12 19:33:53 +08002258 return 0;
2259}
2260
2261
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002262static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302263 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2264 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2265 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2266 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2267 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2268 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2269 0x45, 0xca, 0xcb, 0x36};
2270
2271/*
2272 D2 # tag(18)
2273 84 # array(4)
2274 43 # bytes(3)
2275 A10126 # "\xA1\x01&"
2276 A1 # map(1)
2277 04 # unsigned(4)
2278 42 # bytes(2)
2279 3131 # "11"
2280 54 # bytes(20)
2281 546869732069732074686520636F6E74656E742E # "This is the content."
2282 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002283 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2284 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2285 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2286 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2287 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2288 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2289 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2290 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302291 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002292static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302293 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2294 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2295 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2296 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2297 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2298 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2299 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2300 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2301 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2302 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2303
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002304static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2305
2306
Laurence Lundblade684aec22018-10-12 19:33:53 +08002307/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002308 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002309 C.2.1. This doesn't actually verify the signature (however
2310 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002311 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002312int32_t CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002313{
2314 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002315 const char *szKid = "11";
2316 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2317 const char *szPayload = "This is the content.";
2318 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2319 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2320 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002321
Laurence Lundblade684aec22018-10-12 19:33:53 +08002322 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002323
2324 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302325 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundblade684aec22018-10-12 19:33:53 +08002327 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002328 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002329 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002330
Laurence Lundblade684aec22018-10-12 19:33:53 +08002331 // Add protected headers
2332 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002333
Laurence Lundblade684aec22018-10-12 19:33:53 +08002334 // Empty map with unprotected headers
2335 QCBOREncode_OpenMap(&EC);
2336 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2337 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundblade684aec22018-10-12 19:33:53 +08002339 // The payload
2340 UsefulBufC WrappedPayload;
2341 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002342 // Payload is not actually CBOR in example C.2.1 like it would be
2343 // for a CWT or EAT. It is just a text string.
2344 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002345 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002346
Laurence Lundblade684aec22018-10-12 19:33:53 +08002347 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002348 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2349 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002350 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002351 return -1;
2352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002353
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002354/* if(UsefulBuf_Compare(WrappedPayload,
2355 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2356 return -1;
2357 } */
2358
Laurence Lundblade684aec22018-10-12 19:33:53 +08002359 // The signature
2360 QCBOREncode_AddBytes(&EC, Signature);
2361 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade684aec22018-10-12 19:33:53 +08002363 // Finish and check the results
2364 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002365 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002366 return -2;
2367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002368
Laurence Lundblade684aec22018-10-12 19:33:53 +08002369 // 98 is the size from RFC 8152 C.2.1
2370 if(COSE_Sign1.len != 98) {
2371 return -3;
2372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002373
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002374 // It would be good to compare this to the output from a COSE
2375 // implementation like COSE-C. This has been checked against the
2376 // CBOR playground.
2377 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002378 return -4;
2379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002381
2382 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2383 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2384
2385 // top level array for cose sign1, 18 is the tag for COSE sign
2386 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2387 QCBOREncode_OpenArray(&EC);
2388
2389 // Add protected headers
2390 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2391
2392 // Empty map with unprotected headers
2393 QCBOREncode_OpenMap(&EC);
2394 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2395 QCBOREncode_CloseMap(&EC);
2396
2397 // The payload
2398 QCBOREncode_BstrWrap(&EC);
2399 // Payload is not actually CBOR in example C.2.1 like it would be
2400 // for a CWT or EAT. It is just a text string.
2401 QCBOREncode_AddEncoded(&EC, Payload);
2402 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2403
2404 // Check we got back the actual payload expected
2405 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2406 if(UsefulBuf_Compare(WrappedPayload,
2407 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2408 return -11;
2409 }
2410
2411 // The signature
2412 QCBOREncode_AddBytes(&EC, Signature);
2413 QCBOREncode_CloseArray(&EC);
2414
2415 // Finish and check the results
2416 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2417 return -12;
2418 }
2419
2420 // 98 is the size from RFC 8152 C.2.1
2421 if(COSE_Sign1.len != 98) {
2422 return -13;
2423 }
2424
2425 // It would be good to compare this to the output from a COSE
2426 // implementation like COSE-C. This has been checked against the
2427 // CBOR playground.
2428 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2429 return -14;
2430 }
2431
Laurence Lundblade684aec22018-10-12 19:33:53 +08002432 return 0;
2433}
2434
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002435
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002436int32_t EncodeErrorTests()
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002437{
2438 QCBOREncodeContext EC;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002439
2440
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002441 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002442 // Do all of these tests with NULL buffers so no actual
2443 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002444 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002445
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002446 // First verify no error from a big buffer
2447 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002448 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002449 // 6 is the CBOR overhead for opening the array and encodng the length
2450 // This exactly fills the buffer.
2451 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002452 QCBOREncode_CloseArray(&EC);
2453 size_t xx;
2454 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2455 return -1;
2456 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002457
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002458 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002459 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002460 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002461 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002462 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002463 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002464 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2465 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002466 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002467 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002468 QCBOREncode_CloseArray(&EC);
2469 QCBOREncode_CloseArray(&EC);
2470 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2471 return -2;
2472 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002473
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002474 // Third, fit an array in exactly at max position allowed
2475 QCBOREncode_Init(&EC, Buffer);
2476 QCBOREncode_OpenArray(&EC);
2477 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2478 QCBOREncode_OpenArray(&EC);
2479 QCBOREncode_CloseArray(&EC);
2480 QCBOREncode_CloseArray(&EC);
2481 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2482 return -10;
2483 }
2484
2485
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002486 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2487 // Work close to the 4GB size limit for a better test
2488 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002489 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002490
2491 QCBOREncode_Init(&EC, Large);
2492 QCBOREncode_OpenArray(&EC);
2493 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2494 QCBOREncode_CloseArray(&EC);
2495 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2496 // Making sure it succeeds when it should first
2497 return -3;
2498 }
2499
2500 QCBOREncode_Init(&EC, Large);
2501 QCBOREncode_OpenArray(&EC);
2502 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2503 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2504 QCBOREncode_CloseArray(&EC);
2505 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2506 // Now just 1 byte over, see that it fails
2507 return -4;
2508 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002509
2510
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002511 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2512 QCBOREncode_Init(&EC, Large);
2513 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2514 QCBOREncode_OpenArray(&EC);
2515 }
2516 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2517 QCBOREncode_CloseArray(&EC);
2518 }
2519 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2520 // Making sure it succeeds when it should first
2521 return -5;
2522 }
2523
2524 QCBOREncode_Init(&EC, Large);
2525 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2526 QCBOREncode_OpenArray(&EC);
2527 }
2528 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2529 QCBOREncode_CloseArray(&EC);
2530 }
2531 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2532 // One more level to cause error
2533 return -6;
2534 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002535
2536
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002537#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002538 // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
2539 QCBOREncode_Init(&EC, Large);
2540 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2541 QCBOREncode_OpenArray(&EC);
2542 }
2543 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2544 QCBOREncode_CloseArray(&EC);
2545 }
2546 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
2547 // One more level to cause error
2548 return -7;
2549 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002550
2551
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002552 // ------ QCBOR_ERR_CLOSE_MISMATCH --------
2553 QCBOREncode_Init(&EC, Large);
2554 QCBOREncode_OpenArray(&EC);
2555 UsefulBufC Wrap;
2556 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
2557 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
2558 return -8;
2559 }
2560
2561
2562 // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
2563 QCBOREncode_Init(&EC, Large);
2564 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2565 QCBOREncode_OpenArray(&EC);
2566 }
2567 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2568 QCBOREncode_CloseArray(&EC);
2569 }
2570 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2571 // One more level to cause error
2572 return -9;
2573 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002574#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002575
Laurence Lundblade241705e2018-12-30 18:56:14 -08002576 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2577 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002578
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002579
2580 // ----- Test the check for NULL buffer ------
2581 QCBOREncode_Init(&EC, Buffer);
2582 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2583 return -11;
2584 }
2585
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002586#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002587 // ------ QCBOR_ERR_UNSUPPORTED --------
2588 QCBOREncode_Init(&EC, Large);
2589 QCBOREncode_OpenArray(&EC);
2590 QCBOREncode_AddSimple(&EC, 24); // CBOR_SIMPLEV_RESERVED_START
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002591 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002592 return -12;
2593 }
2594
2595 QCBOREncode_Init(&EC, Large);
2596 QCBOREncode_OpenArray(&EC);
2597 QCBOREncode_AddSimple(&EC, 31); // CBOR_SIMPLEV_RESERVED_END
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002598 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002599 return -13;
2600 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002601#endif /* #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2602
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002603
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002604 return 0;
2605}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002606
2607
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002608#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002609/*
2610 [
2611 4([-1, 3]),
2612 4([-20, 4759477275222530853136]),
2613 4([9223372036854775807, -4759477275222530853137]),
2614 5([300, 100]),
2615 5([-20, 4759477275222530853136]),
2616 5([-9223372036854775808, -4759477275222530853137])
2617 ]
2618 */
2619static const uint8_t spExpectedExponentAndMantissaArray[] = {
2620 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2621 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2622 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2623 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2624 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2625 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2626 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2627 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2628 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2629 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2630 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2631
2632
2633/*
2634 {
2635 "decimal fraction": 4([-1, 3]),
2636 300: 4([-1, 3]),
2637 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2638 400: 4([2147483647, 4759477275222530853136]),
2639 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2640 500: 4([9223372036854775807, -4759477275222530853137]),
2641 "big float": 5([300, 100]),
2642 600: 5([300, 100]),
2643 "big float bignum positive": 5([-20, 4759477275222530853136]),
2644 700: 5([-20, 4759477275222530853136]),
2645 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2646 800: 5([-9223372036854775808, -4759477275222530853137])
2647 }
2648 */
2649static const uint8_t spExpectedExponentAndMantissaMap[] = {
2650 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2651 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2652 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2653 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2654 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2655 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2656 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2657 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2658 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2659 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2660 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2661 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2662 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2663 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2664 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2665 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2666 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2667 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2668 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2669 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2670 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2671 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2672 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2673 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2674 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2675 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2676 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2677 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2678 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2679 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2680 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2681 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2682 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2683 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2684 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2685 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2686 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2687 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2688 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2689 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2690 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2691 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2692 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2693 0x09, 0x10
2694};
2695
2696
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002697int32_t ExponentAndMantissaEncodeTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08002698{
2699 QCBOREncodeContext EC;
2700 UsefulBufC EncodedExponentAndMantissa;
2701
2702 // Constant for the big number used in all the tests.
2703 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2704 0x06, 0x07, 0x08, 0x09, 0x010};
2705 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2706
2707 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2708 QCBOREncode_OpenArray(&EC);
2709 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2710 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2711 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2712 QCBOREncode_AddBigFloat(&EC, 100, 300);
2713 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2714 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2715 QCBOREncode_CloseArray(&EC);
2716
2717 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2718 return -2;
2719 }
2720
2721 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2722 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2723 NULL);
2724 if(nReturn) {
2725 return nReturn;
2726 }
2727
2728 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2729 QCBOREncode_OpenMap(&EC);
2730
2731 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2732
2733 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2734
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002735 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002736 "decimal fraction bignum postive",
2737 BigNum,
2738 false,
2739 -200);
2740
2741 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2742 400,
2743 BigNum,
2744 false,
2745 INT32_MAX);
2746
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002747 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002748 "decimal fraction bignum negative",
2749 BigNum,
2750 true,
2751 INT64_MAX);
2752
2753 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2754 500,
2755 BigNum,
2756 true,
2757 INT64_MAX);
2758
2759 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2760
2761 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2762
2763 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2764 "big float bignum positive",
2765 BigNum,
2766 false,
2767 -20);
2768
2769 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2770 700,
2771 BigNum,
2772 false,
2773 -20);
2774
2775 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2776 "big float bignum negative",
2777 BigNum,
2778 true,
2779 INT64_MIN);
2780
2781 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2782 800,
2783 BigNum,
2784 true,
2785 INT64_MIN);
2786
2787 QCBOREncode_CloseMap(&EC);
2788
2789 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2790 return -3;
2791 }
2792
2793
2794 struct UBCompareDiagnostic Diag;
2795
2796 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2797 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2798 &Diag);
2799 if(nReturn) {
2800 return nReturn + 1000000; // +1000000 to distinguish from first test above
2801 }
2802
2803 return 0;
2804}
2805
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002806#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002807
2808
2809int32_t QCBORHeadTest()
2810{
2811 /* This test doesn't have to be extensive, because just about every
2812 * other test exercises QCBOREncode_EncodeHead().
2813 */
2814 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002815 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002816
2817 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2818 CBOR_MAJOR_TYPE_POSITIVE_INT,
2819 0,
2820 0);
2821
2822 static const uint8_t expectedZero[] = {0x00};
2823
2824 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2825 return -1;
2826 }
2827
2828 // ---- Encode a zero padded out to an 8 byte integer ----
2829 encoded = QCBOREncode_EncodeHead(RightSize,
2830 CBOR_MAJOR_TYPE_POSITIVE_INT,
2831 8, // uMinSize is 8 bytes
2832 0);
2833
2834 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2835 0x00, 0x00, 0x00, 0x00};
2836
2837 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2838 return -2;
2839 }
2840
2841
2842 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002843 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002844
2845 encoded = QCBOREncode_EncodeHead(TooSmall,
2846 CBOR_MAJOR_TYPE_POSITIVE_INT,
2847 0,
2848 0);
2849
2850 if(!UsefulBuf_IsNULLC(encoded)) {
2851 return -3;
2852 }
2853
2854 return 0;
2855}