blob: 638a9b9e76b7eeef7fcf957c3c225c2e16f403ed [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 Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, 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 Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor.h"
34#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035
36
Laurence Lundblade369b90a2018-10-22 02:04:37 +053037/*
38 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080039
Laurence Lundblade369b90a2018-10-22 02:04:37 +053040 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080041
Laurence Lundblade369b90a2018-10-22 02:04:37 +053042 A few more things to do include:
43 - Add a test for counting the top level items and adding it back in with AddRaw()
44 - Run on some different CPUs like 32-bit and maybe even 16-bit
45 - Test the large array count limit
46 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080047
Laurence Lundblade369b90a2018-10-22 02:04:37 +053048 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049
Laurence Lundbladeee851742020-01-08 08:37:05 -080050//#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051
Laurence Lundbladeee851742020-01-08 08:37:05 -080052#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053053#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054
Laurence Lundbladeee851742020-01-08 08:37:05 -080055#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056// ifdef these out to not have compiler warnings
57static void printencoded(const uint8_t *pEncoded, size_t nLen)
58{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070059 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060 for(i = 0; i < nLen; i++) {
61 uint8_t Z = pEncoded[i];
62 printf("%02x ", Z);
63 }
64 printf("\n");
65
66 fflush(stdout);
67}
Laurence Lundbladeee851742020-01-08 08:37:05 -080068#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade369b90a2018-10-22 02:04:37 +053071// Do the comparison and print out where it fails
72static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070073 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053074 for(i = 0; i < U1.len; i++) {
75 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladeee851742020-01-08 08:37:05 -080076 printf("Position: %d Actual: 0x%x Expected: 0x%x\n",
77 (uint32_t)i,
78 ((uint8_t *)U1.ptr)[i],
79 ((uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053080 return 1;
81 }
82 }
83 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080084
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053085}
86
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070087#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053088 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053089
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070090#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091
92#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080093 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
94
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070095#endif
96
97
Laurence Lundblade59289e52019-12-30 13:44:37 -080098#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
99/*
100 Returns 0 if UsefulBufs are equal
101 Returns 1000000 + offeset if they are not equal.
102
103
104
105*/
106struct UBCompareDiagnostic {
107 uint8_t uActual;
108 uint8_t uExpected;
109 size_t uOffset;
110};
111
Laurence Lundbladeee851742020-01-08 08:37:05 -0800112static int32_t
113UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
114 UsefulBufC Expected,
115 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800116 size_t i;
117 for(i = 0; i < Actual.len; i++) {
118 if(((uint8_t *)Actual.ptr)[i] != ((uint8_t *)Expected.ptr)[i]) {
119 if(pDiag) {
120 pDiag->uActual = ((uint8_t *)Actual.ptr)[i];
121 pDiag->uExpected = ((uint8_t *)Expected.ptr)[i];
122 pDiag->uOffset = i;
123 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800124 // Cast to int is OK as this is only a diagnostic and the sizes
125 // here are never over a few KB.
126 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800127 }
128 }
129 return 0;
130
131}
132#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
133
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700134
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530135// One big buffer that is used by all the tests to encode into
136// Putting it in uninitialized data is better than using a lot
137// of stack. The tests should run on small devices too.
138static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139
140
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800141
142/*
143 Some very minimal tests.
144 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800145int32_t BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800146{
147 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800148 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530150 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 QCBOREncode_OpenMap(&EC);
153 QCBOREncode_AddBoolToMapN(&EC, 66, true);
154 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800156 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700157 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530158 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800160
161
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800162 // Decode it and see that is right
163 QCBORDecodeContext DC;
164 QCBORItem Item;
165 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800167 QCBORDecode_GetNext(&DC, &Item);
168 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530169 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800171
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800172 QCBORDecode_GetNext(&DC, &Item);
173 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530174 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800177 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530178 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
181
Laurence Lundbladeee851742020-01-08 08:37:05 -0800182 // Make another encoded message with the CBOR from the previous
183 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530184 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800185 QCBOREncode_Init(&EC, MemoryForEncoded2);
186 QCBOREncode_OpenArray(&EC);
187 QCBOREncode_AddUInt64(&EC, 451);
188 QCBOREncode_AddEncoded(&EC, Encoded);
189 QCBOREncode_OpenMap(&EC);
190 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
191 QCBOREncode_CloseMap(&EC);
192 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800194 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700195 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530196 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800197 }
198 /*
199 [ // 0 1:3
200 451, // 1 1:2
201 { // 1 1:2 2:1
202 66: true // 2 1:1
203 },
204 { // 1 1:1 2:1
205 -70000: { // 2 1:1 2:1 3:1
206 66: true // 3 XXXXXX
207 }
208 }
209 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800210
211
212
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800213 83 # array(3)
214 19 01C3 # unsigned(451)
215 A1 # map(1)
216 18 42 # unsigned(66)
217 F5 # primitive(21)
218 A1 # map(1)
219 3A 0001116F # negative(69999)
220 A1 # map(1)
221 18 42 # unsigned(66)
222 F5 # primitive(21)
223 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800224
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800225 // Decode it and see if it is OK
226 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800227
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 // 0 1:3
229 QCBORDecode_GetNext(&DC, &Item);
230 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530231 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800234 // 1 1:2
235 QCBORDecode_GetNext(&DC, &Item);
236 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530237 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800240 // 1 1:2 2:1
241 QCBORDecode_GetNext(&DC, &Item);
242 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530243 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800245
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800246 // 2 1:1
247 QCBORDecode_GetNext(&DC, &Item);
248 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530249 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800251
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800252 // 1 1:1 2:1
253 QCBORDecode_GetNext(&DC, &Item);
254 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530255 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800257
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800258 // 2 1:1 2:1 3:1
259 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800260 if(Item.uDataType != QCBOR_TYPE_MAP ||
261 Item.val.uCount != 1 ||
262 Item.uLabelType != QCBOR_TYPE_INT64 ||
263 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530264 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800267 // 3 XXXXXX
268 QCBORDecode_GetNext(&DC, &Item);
269 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530270 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800273 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530274 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800276
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800277 return 0;
278}
279
280
281
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530282static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700283 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530284 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
285 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
286 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
287 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
288 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
289 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
290 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
291 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700292 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530293 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
294 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
295 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
296 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
297 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
298 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
299 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
300 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
301 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
302 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
303 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
304 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
305 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
306 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
307 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
308 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
309 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
310 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
311 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
312 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
313 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
314 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
315 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
316 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
317 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
318 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
319 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
320 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
321 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
322 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
323 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
324 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
325 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
326 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
327 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
328 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
329 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
330 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
331 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
332 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
333 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
334 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
335 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
336 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
337 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
338 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
339 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
340 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
341 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
342 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
343 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
344 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
345 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
346 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
347 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
348 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
349 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
350 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
351 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
352 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
353 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
354 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
355 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
356 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
357 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
358 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
359 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
360 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
361 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
362 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
363 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
364 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
365 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
366 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
367 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
368 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
369 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
370 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
371 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
372 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
373 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
374 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
375 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
376 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
377 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
378 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
379 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
380 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
381 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
382 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
383 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
384 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
385 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
386 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
387 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
388 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
389 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
390 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
391 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
392 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
393 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
394 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
395 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
396 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
397 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
398 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
399 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
400 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
401 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
402 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
403 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
404 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
405 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
406 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
407 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
408 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
409 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
410 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
411 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
412 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
413 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
414 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
415 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
416 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
417 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
418 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
419 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
420 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
421 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
422 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
423 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
424 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
425 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
426 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
427 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
428 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
429 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
430 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
431 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
432 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
433 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
434 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
435 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
436 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
437 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
438 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
439 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
440 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
441 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
442 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
443 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
444 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
445 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
446 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
447 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
448 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
449 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
450 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
451 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
452 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
453 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
454 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
455 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
456 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
457 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
458 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
459 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
460 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
461 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
462 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
463 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
464 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
465 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
466 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
467 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
468 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
469 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
470 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
471 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
472 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
473 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
474 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
475 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
476 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
477 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
478 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
479 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
480 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
481 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
482 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
483 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
486 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
487 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489};
490
491
492static const char *szMIME = "\
493MIME-Version: 1.0\n\
494Content-Type: multipart/mixed;\n\
495boundary=\"XXXXboundary text\"\n\
496\n\
497This is a multipart message in MIME format.\n\
498\n\
499--XXXXboundary text\n\
500Content-Type: text/plain\n\
501\n\
502this is the body text\n\
503\n\
504--XXXXboundary text\n\
505Content-Type: text/plain;\n\
506Content-Disposition: attachment;\n\
507filename=\"test.txt\"\n\
508\n\
509this is the attachment text\n\
510\n\
511--XXXXboundary text--";
512
513
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800514int32_t AllAddMethodsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515{
Laurence Lundbladeee851742020-01-08 08:37:05 -0800516 // TODO: this test should be broken down into several so it is more
517 // managable. Tags and labels could be more sensible
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800518 QCBOREncodeContext ECtx;
519 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800520
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530521 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800522
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 QCBOREncode_OpenArray(&ECtx);
524
Laurence Lundbladeee851742020-01-08 08:37:05 -0800525 // Some ints that are tagged and have strings preceeding them
526 // (not labels becase it is not a map)
Laurence Lundblade067035b2018-11-28 17:35:25 -0800527 QCBOREncode_AddSZString(&ECtx, "UINT62");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700528 QCBOREncode_AddTag(&ECtx, 100);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800529 QCBOREncode_AddUInt64(&ECtx, 89989909);
530 QCBOREncode_AddSZString(&ECtx, "INT64");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700531 QCBOREncode_AddTag(&ECtx, 76);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800532 QCBOREncode_AddInt64(&ECtx, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800533 QCBOREncode_AddUInt64(&ECtx,0);
534 QCBOREncode_AddInt64(&ECtx, -44);
535
536 // ints that go in maps
537 QCBOREncode_OpenMap(&ECtx);
538 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
539 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
540 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
541 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
542 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800543
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800544 // Epoch Date
545 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547 // Epoch date with labels
548 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800549 QCBOREncode_AddDateEpochToMap(&ECtx, "LongLiveDenisRitchie", 1400000000);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
551 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
552 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800553
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800554 // Binary blobs
555 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
556
557 // binary blobs in maps
558 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800559 QCBOREncode_AddSZString(&ECtx, "binbin");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700560 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800561 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
562 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
564 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530567 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800568 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundbladeee851742020-01-08 08:37:05 -0800569 const char *szURL =
570 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
571 QCBOREncode_AddURI(&ECtx, UsefulBuf_FromSZ(szURL));
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530572 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
573 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530574 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 // text blobs in maps
577 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530578 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade067035b2018-11-28 17:35:25 -0800579 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
580 QCBOREncode_AddSZString(&ECtx, "()()()");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700581 QCBOREncode_AddTag(&ECtx, 1000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800582 QCBOREncode_AddSZString(&ECtx, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530583 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800584 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
585 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundbladeee851742020-01-08 08:37:05 -0800586 QCBOREncode_AddURIToMap(&ECtx,
587 "RFC",
588 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530589 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
590 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
591 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
592 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
593 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530594 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
595 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596 QCBOREncode_CloseMap(&ECtx);
597
598 // Date strings
599 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
600 QCBOREncode_OpenMap(&ECtx);
601 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
602 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
603 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 // true / false ...
606 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
607 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800608 QCBOREncode_AddSZString(&ECtx, "dare");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700609 QCBOREncode_AddTag(&ECtx, 66);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800610 QCBOREncode_AddBool(&ECtx, true);
611 QCBOREncode_AddBoolToMap(&ECtx, "uu", false);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
613 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 // opening an array
616 QCBOREncode_OpenArray(&ECtx);
617 QCBOREncode_CloseArray(&ECtx);
618
619 // opening arrays in a map
620 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800621 QCBOREncode_AddSZString(&ECtx, "label and tagged empty array");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700622 QCBOREncode_AddTag(&ECtx, 1093);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800623 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624 QCBOREncode_CloseArray(&ECtx);
625 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
626 QCBOREncode_CloseArray(&ECtx);
627 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800628 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800629 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800631 // opening maps with labels and tagging
632 QCBOREncode_OpenMap(&ECtx);
633 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
634 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800635 QCBOREncode_AddSZString(&ECtx, "in a in a in a");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700636 QCBOREncode_AddTag(&ECtx, 9087);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800637 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800638 QCBOREncode_CloseMap(&ECtx);
639 QCBOREncode_CloseMap(&ECtx);
640 QCBOREncode_CloseMap(&ECtx);
641 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800642
Laurence Lundblade067035b2018-11-28 17:35:25 -0800643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 // Extended simple values (these are not standard...)
645 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800646 QCBOREncode_AddSZString(&ECtx, "s1");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700647 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800648 QCBOREncode_AddSimple(&ECtx, 255);
649 QCBOREncode_AddSimpleToMap(&ECtx, "s2", 0);
650 QCBOREncode_AddSZString(&ECtx, "s3");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700651 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800652 QCBOREncode_AddSimple(&ECtx, 33);
653 QCBOREncode_AddInt64(&ECtx, 88378374); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700654 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800655 QCBOREncode_AddSimple(&ECtx, 255);
656 QCBOREncode_AddInt64(&ECtx, 89); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700657 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800658 QCBOREncode_AddSimple(&ECtx, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800659 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 // UUIDs
Laurence Lundbladeee851742020-01-08 08:37:05 -0800662 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
663 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
664 0x49, 0x43, 0x41, 0x32};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800665 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
667 QCBOREncode_OpenMap(&ECtx);
668 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
669 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
670 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800671
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800672 // Bool
673 QCBOREncode_AddBool(&ECtx, true);
674 QCBOREncode_AddBool(&ECtx, false);
675 QCBOREncode_OpenMap(&ECtx);
676 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
677 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
678 QCBOREncode_CloseMap(&ECtx);
679
680
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530681 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800682 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800683 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
684 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
685 QCBOREncode_OpenMap(&ECtx);
686 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
687 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
688 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
689 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
690 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800692 QCBOREncode_CloseArray(&ECtx);
693
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530694 UsefulBufC Enc;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800695
Laurence Lundblade0595e932018-11-02 22:22:47 +0700696 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800697 nReturn = -1;
698 goto Done;
699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800700
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530701 if(CheckResults(Enc, spExpectedEncodedAll))
702 nReturn = -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704Done:
705 return nReturn;
706}
707
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530708/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200709 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530710 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
711 3B 0000000100000000 # negative(4294967296)
712 3A FFFFFFFF # negative(4294967295)
713 3A FFFFFFFE # negative(4294967294)
714 3A FFFFFFFD # negative(4294967293)
715 3A 7FFFFFFF # negative(2147483647)
716 3A 7FFFFFFE # negative(2147483646)
717 3A 00010001 # negative(65537)
718 3A 00010000 # negative(65536)
719 39 FFFF # negative(65535)
720 39 FFFE # negative(65534)
721 39 FFFD # negative(65533)
722 39 0100 # negative(256)
723 38 FF # negative(255)
724 38 FE # negative(254)
725 38 FD # negative(253)
726 38 18 # negative(24)
727 37 # negative(23)
728 36 # negative(22)
729 20 # negative(0)
730 00 # unsigned(0)
731 00 # unsigned(0)
732 01 # unsigned(1)
733 16 # unsigned(22)
734 17 # unsigned(23)
735 18 18 # unsigned(24)
736 18 19 # unsigned(25)
737 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200738 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530739 18 FE # unsigned(254)
740 18 FF # unsigned(255)
741 19 0100 # unsigned(256)
742 19 0101 # unsigned(257)
743 19 FFFE # unsigned(65534)
744 19 FFFF # unsigned(65535)
745 1A 00010000 # unsigned(65536)
746 1A 00010001 # unsigned(65537)
747 1A 00010002 # unsigned(65538)
748 1A 7FFFFFFF # unsigned(2147483647)
749 1A 7FFFFFFF # unsigned(2147483647)
750 1A 80000000 # unsigned(2147483648)
751 1A 80000001 # unsigned(2147483649)
752 1A FFFFFFFE # unsigned(4294967294)
753 1A FFFFFFFF # unsigned(4294967295)
754 1B 0000000100000000 # unsigned(4294967296)
755 1B 0000000100000001 # unsigned(4294967297)
756 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
757 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
758 */
759static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200760 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800761 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
762 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
763 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
764 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
765 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
766 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
767 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
768 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
769 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
770 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200771 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
772 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
773 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
774 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
775 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
776 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
777 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
778 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
779 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
780 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
781 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
782 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
783 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784
785/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800786
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800787 Test the generation of integers. This also ends up testing
788 encoding of all the different lengths. It encodes integers
789 of many lengths and values, especially around the boundaries
790 for different types of integers. It compares the output
791 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800793 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800794int32_t IntegerValuesTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795{
796 QCBOREncodeContext ECtx;
797 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800798
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530799 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 QCBOREncode_OpenArray(&ECtx);
801
802 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
803 QCBOREncode_AddInt64(&ECtx, -4294967297);
804 QCBOREncode_AddInt64(&ECtx, -4294967296);
805 QCBOREncode_AddInt64(&ECtx, -4294967295);
806 QCBOREncode_AddInt64(&ECtx, -4294967294);
807 QCBOREncode_AddInt64(&ECtx, -2147483648);
808 QCBOREncode_AddInt64(&ECtx, -2147483647);
809 QCBOREncode_AddInt64(&ECtx, -65538);
810 QCBOREncode_AddInt64(&ECtx, -65537);
811 QCBOREncode_AddInt64(&ECtx, -65536);
812 QCBOREncode_AddInt64(&ECtx, -65535);
813 QCBOREncode_AddInt64(&ECtx, -65534);
814 QCBOREncode_AddInt64(&ECtx, -257);
815 QCBOREncode_AddInt64(&ECtx, -256);
816 QCBOREncode_AddInt64(&ECtx, -255);
817 QCBOREncode_AddInt64(&ECtx, -254);
818 QCBOREncode_AddInt64(&ECtx, -25);
819 QCBOREncode_AddInt64(&ECtx, -24);
820 QCBOREncode_AddInt64(&ECtx, -23);
821 QCBOREncode_AddInt64(&ECtx, -1);
822 QCBOREncode_AddInt64(&ECtx, 0);
823 QCBOREncode_AddUInt64(&ECtx, 0ULL);
824 QCBOREncode_AddInt64(&ECtx, 1);
825 QCBOREncode_AddInt64(&ECtx, 22);
826 QCBOREncode_AddInt64(&ECtx, 23);
827 QCBOREncode_AddInt64(&ECtx, 24);
828 QCBOREncode_AddInt64(&ECtx, 25);
829 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200830 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800831 QCBOREncode_AddInt64(&ECtx, 254);
832 QCBOREncode_AddInt64(&ECtx, 255);
833 QCBOREncode_AddInt64(&ECtx, 256);
834 QCBOREncode_AddInt64(&ECtx, 257);
835 QCBOREncode_AddInt64(&ECtx, 65534);
836 QCBOREncode_AddInt64(&ECtx, 65535);
837 QCBOREncode_AddInt64(&ECtx, 65536);
838 QCBOREncode_AddInt64(&ECtx, 65537);
839 QCBOREncode_AddInt64(&ECtx, 65538);
840 QCBOREncode_AddInt64(&ECtx, 2147483647);
841 QCBOREncode_AddInt64(&ECtx, 2147483647);
842 QCBOREncode_AddInt64(&ECtx, 2147483648);
843 QCBOREncode_AddInt64(&ECtx, 2147483649);
844 QCBOREncode_AddInt64(&ECtx, 4294967294);
845 QCBOREncode_AddInt64(&ECtx, 4294967295);
846 QCBOREncode_AddInt64(&ECtx, 4294967296);
847 QCBOREncode_AddInt64(&ECtx, 4294967297);
848 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
849 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800850
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800851 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800852
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530853 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700854 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800855 nReturn = -1;
856 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800857
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530858 if(CheckResults(Enc, spExpectedEncodedInts))
859 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800860
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800861 return(nReturn);
862}
863
864
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530865/*
866 85 # array(5)
867 F5 # primitive(21)
868 F4 # primitive(20)
869 F6 # primitive(22)
870 F7 # primitive(23)
871 A1 # map(1)
872 65 # text(5)
873 554E446566 # "UNDef"
874 F7 # primitive(23)
875 */
876static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800877 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
878
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800879int32_t SimpleValuesTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800880{
881 QCBOREncodeContext ECtx;
882 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800883
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530884 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900886
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800887 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
888 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
889 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
890 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800891
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892 QCBOREncode_OpenMap(&ECtx);
893
894 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
895 QCBOREncode_CloseMap(&ECtx);
896
897 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530899 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700900 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 nReturn = -1;
902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800903
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530904 if(CheckResults(ECBOR, spExpectedEncodedSimple))
905 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800907 return(nReturn);
908}
909
Jan Jongboom47d86c52019-07-25 08:54:16 +0200910/*
911 9F # array(5)
912 F5 # primitive(21)
913 F4 # primitive(20)
914 F6 # primitive(22)
915 F7 # primitive(23)
916 BF # map(1)
917 65 # text(5)
918 554E446566 # "UNDef"
919 F7 # primitive(23)
920 FF # break
921 FF # break
922 */
923static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
924 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
925
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800926int32_t SimpleValuesIndefiniteLengthTest1()
Jan Jongboom47d86c52019-07-25 08:54:16 +0200927{
928 QCBOREncodeContext ECtx;
929 int nReturn = 0;
930
931 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
932 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
933
934 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
935 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
936 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
937 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
938
939 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
940
941 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
942 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
943
944 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
945
946 UsefulBufC ECBOR;
947 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
948 nReturn = -1;
949 }
950
951 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
952 return -2;
953
954 return(nReturn);
955}
956
Jan Jongboom5d827882019-08-07 12:51:15 +0200957/*
958A5 # map(5)
959 63 # text(3)
960 617272 # "arr"
961 98 1F # array(31)
962 00 # unsigned(0)
963 01 # unsigned(1)
964 02 # unsigned(2)
965 03 # unsigned(3)
966 04 # unsigned(4)
967 05 # unsigned(5)
968 06 # unsigned(6)
969 07 # unsigned(7)
970 08 # unsigned(8)
971 09 # unsigned(9)
972 0A # unsigned(10)
973 0B # unsigned(11)
974 0C # unsigned(12)
975 0D # unsigned(13)
976 0E # unsigned(14)
977 0F # unsigned(15)
978 10 # unsigned(16)
979 11 # unsigned(17)
980 12 # unsigned(18)
981 13 # unsigned(19)
982 14 # unsigned(20)
983 15 # unsigned(21)
984 16 # unsigned(22)
985 17 # unsigned(23)
986 18 18 # unsigned(24)
987 18 19 # unsigned(25)
988 18 1A # unsigned(26)
989 18 1B # unsigned(27)
990 18 1C # unsigned(28)
991 18 1D # unsigned(29)
992 18 1E # unsigned(30)
993 63 # text(3)
994 6D6170 # "map"
995 B8 1F # map(31)
996 61 # text(1)
997 61 # "a"
998 00 # unsigned(0)
999 61 # text(1)
1000 62 # "b"
1001 01 # unsigned(1)
1002 61 # text(1)
1003 63 # "c"
1004 02 # unsigned(2)
1005 61 # text(1)
1006 64 # "d"
1007 03 # unsigned(3)
1008 61 # text(1)
1009 65 # "e"
1010 04 # unsigned(4)
1011 61 # text(1)
1012 66 # "f"
1013 05 # unsigned(5)
1014 61 # text(1)
1015 67 # "g"
1016 06 # unsigned(6)
1017 61 # text(1)
1018 68 # "h"
1019 07 # unsigned(7)
1020 61 # text(1)
1021 69 # "i"
1022 08 # unsigned(8)
1023 61 # text(1)
1024 6A # "j"
1025 09 # unsigned(9)
1026 61 # text(1)
1027 6B # "k"
1028 0A # unsigned(10)
1029 61 # text(1)
1030 6C # "l"
1031 0B # unsigned(11)
1032 61 # text(1)
1033 6D # "m"
1034 0C # unsigned(12)
1035 61 # text(1)
1036 6E # "n"
1037 0D # unsigned(13)
1038 61 # text(1)
1039 6F # "o"
1040 0E # unsigned(14)
1041 61 # text(1)
1042 70 # "p"
1043 0F # unsigned(15)
1044 61 # text(1)
1045 71 # "q"
1046 10 # unsigned(16)
1047 61 # text(1)
1048 72 # "r"
1049 11 # unsigned(17)
1050 61 # text(1)
1051 73 # "s"
1052 12 # unsigned(18)
1053 61 # text(1)
1054 74 # "t"
1055 13 # unsigned(19)
1056 61 # text(1)
1057 75 # "u"
1058 14 # unsigned(20)
1059 61 # text(1)
1060 76 # "v"
1061 15 # unsigned(21)
1062 61 # text(1)
1063 77 # "w"
1064 16 # unsigned(22)
1065 61 # text(1)
1066 78 # "x"
1067 17 # unsigned(23)
1068 61 # text(1)
1069 79 # "y"
1070 18 18 # unsigned(24)
1071 61 # text(1)
1072 7A # "z"
1073 18 19 # unsigned(25)
1074 61 # text(1)
1075 41 # "A"
1076 18 1A # unsigned(26)
1077 61 # text(1)
1078 42 # "B"
1079 18 1B # unsigned(27)
1080 61 # text(1)
1081 43 # "C"
1082 18 1C # unsigned(28)
1083 61 # text(1)
1084 44 # "D"
1085 18 1D # unsigned(29)
1086 61 # text(1)
1087 45 # "E"
1088 18 1E # unsigned(30)
1089 65 # text(5)
1090 6D696E3331 # "min31"
1091 38 1E # negative(30)
1092 66 # text(6)
1093 706C75733331 # "plus31"
1094 18 1F # unsigned(31)
1095 63 # text(3)
1096 737472 # "str"
1097 78 1F # text(31)
1098 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1099 */
1100static const uint8_t EncodeLengthThirtyone[] = {
1101 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1102 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1103 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1104 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1105 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1106 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1107 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1108 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1109 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1110 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1111 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1112 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1113 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1114 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1115 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1116 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1117 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1118 0x31
1119};
1120
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001121int32_t EncodeLengthThirtyoneTest()
Jan Jongboom5d827882019-08-07 12:51:15 +02001122{
1123 QCBOREncodeContext ECtx;
1124 int nReturn = 0;
1125
1126 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1127 QCBOREncode_OpenMap(&ECtx);
1128
1129 // add array with 31 items
1130 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1131 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001132 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001133 }
1134 QCBOREncode_CloseArray(&ECtx);
1135
1136 // add map with 31 items
1137 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001138 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001139 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001140 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001141 if (ix < 26) c = c + ix;
1142 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001143 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001144 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1145 }
1146 QCBOREncode_CloseMap(&ECtx);
1147
1148 // add -31 and +31
1149 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1150 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1151
1152 // add string with length 31
1153 const char *str = "testtesttesttesttesttestqcbor11";
1154 UsefulBufC str_b = { str, 31 };
1155 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1156
1157 QCBOREncode_CloseMap(&ECtx);
1158
1159 UsefulBufC ECBOR;
1160 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1161 nReturn = -1;
1162 }
1163
1164 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1165 return -2;
1166
1167 return(nReturn);
1168}
1169
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301170
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301171/*
1172 83 # array(3)
1173 C0 # tag(0)
1174 74 # text(20)
1175 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
1176 C1 # tag(1)
1177 1A 514B67B0 # unsigned(1363896240)
1178 A2 # map(2)
1179 78 19 # text(25)
1180 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
1181 C0 # tag(0)
1182 77 # text(23)
1183 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
1184 62 # text(2)
1185 5344 # "SD"
1186 C1 # tag(1)
1187 19 03E7 # unsigned(999)
1188 */
1189static const uint8_t spExpectedEncodedDates[] = {
1190 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
1191 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
1192 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
1193 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
1194 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
1195 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
1196 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
1197 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
1198 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199};
1200
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001201int32_t EncodeDateTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202{
1203 QCBOREncodeContext ECtx;
1204 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001205
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301206 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001208 QCBOREncode_OpenArray(&ECtx);
1209
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001210
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
1212 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
1213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 QCBOREncode_OpenMap(&ECtx);
1216
1217 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001222
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 QCBOREncode_CloseArray(&ECtx);
1224
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301225 UsefulBufC ECBOR;
1226
Laurence Lundblade0595e932018-11-02 22:22:47 +07001227 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001228 nReturn = -1;
1229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301231 if(CheckResults(ECBOR, spExpectedEncodedDates))
1232 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001234 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235}
1236
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301237
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001238int32_t ArrayNestingTest1()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001239{
1240 QCBOREncodeContext ECtx;
1241 int i;
1242 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301244 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001245 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1246 QCBOREncode_OpenArray(&ECtx);
1247 }
1248 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1249 QCBOREncode_CloseArray(&ECtx);
1250 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001251 UsefulBufC Encoded;
1252 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001253 nReturn = -1;
1254 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255
1256 return(nReturn);
1257}
1258
1259
1260
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001261int32_t ArrayNestingTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001262{
1263 QCBOREncodeContext ECtx;
1264 int i;
1265 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001266
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301267 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1269 QCBOREncode_OpenArray(&ECtx);
1270 }
1271 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1272 QCBOREncode_CloseArray(&ECtx);
1273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
Laurence Lundblade0595e932018-11-02 22:22:47 +07001275 UsefulBufC Encoded;
1276 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001277 nReturn = -1;
1278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001279
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280 return(nReturn);
1281}
1282
1283
1284
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001285int32_t ArrayNestingTest3()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001286{
1287 QCBOREncodeContext ECtx;
1288 int i;
1289 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001290
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301291 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001292 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1293 QCBOREncode_OpenArray(&ECtx);
1294 }
1295 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1296 QCBOREncode_CloseArray(&ECtx);
1297 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001298 UsefulBufC Encoded;
1299 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300 nReturn = -1;
1301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001302
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001303 return(nReturn);
1304}
1305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301307/*
1308 81 # array(1)
1309 81 # array(1)
1310 81 # array(1)
1311 81 # array(1)
1312 80 # array(0)
1313*/
1314static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301317/*
1318 82 # array(2)
1319 81 # array(1)
1320 81 # array(1)
1321 81 # array(1)
1322 81 # array(1)
1323 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001324 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301325 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1326 3B 0000000100000000 # negative(4294967296)
1327 3A FFFFFFFF # negative(4294967295)
1328 3A FFFFFFFE # negative(4294967294)
1329 3A FFFFFFFD # negative(4294967293)
1330 3A 7FFFFFFF # negative(2147483647)
1331 3A 7FFFFFFE # negative(2147483646)
1332 3A 00010001 # negative(65537)
1333 3A 00010000 # negative(65536)
1334 39 FFFF # negative(65535)
1335 39 FFFE # negative(65534)
1336 39 FFFD # negative(65533)
1337 39 0100 # negative(256)
1338 38 FF # negative(255)
1339 38 FE # negative(254)
1340 38 FD # negative(253)
1341 38 18 # negative(24)
1342 37 # negative(23)
1343 36 # negative(22)
1344 20 # negative(0)
1345 00 # unsigned(0)
1346 00 # unsigned(0)
1347 01 # unsigned(1)
1348 16 # unsigned(22)
1349 17 # unsigned(23)
1350 18 18 # unsigned(24)
1351 18 19 # unsigned(25)
1352 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001353 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301354 18 FE # unsigned(254)
1355 18 FF # unsigned(255)
1356 19 0100 # unsigned(256)
1357 19 0101 # unsigned(257)
1358 19 FFFE # unsigned(65534)
1359 19 FFFF # unsigned(65535)
1360 1A 00010000 # unsigned(65536)
1361 1A 00010001 # unsigned(65537)
1362 1A 00010002 # unsigned(65538)
1363 1A 7FFFFFFF # unsigned(2147483647)
1364 1A 7FFFFFFF # unsigned(2147483647)
1365 1A 80000000 # unsigned(2147483648)
1366 1A 80000001 # unsigned(2147483649)
1367 1A FFFFFFFE # unsigned(4294967294)
1368 1A FFFFFFFF # unsigned(4294967295)
1369 1B 0000000100000000 # unsigned(4294967296)
1370 1B 0000000100000001 # unsigned(4294967297)
1371 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1372 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1373 */
1374static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001375 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1377 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1378 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1379 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1380 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1381 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1382 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1383 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1384 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1385 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1386 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001387 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1388 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1389 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1390 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1391 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1392 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1393 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1394 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1395 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1396 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1397 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1398 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1399 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001400
1401
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001402int32_t EncodeRawTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001404 QCBOREncodeContext ECtx;
1405
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301406 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001407 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301408 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1409 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001410 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001411
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001412 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade0595e932018-11-02 22:22:47 +07001414 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001415 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001417
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301418 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001419 return -5;
1420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001421
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001422 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423}
1424
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301425/*
1426 This returns a pointer to spBigBuf
1427 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001428static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429{
1430 QCBOREncodeContext ECtx;
1431 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433 *pEncoded = NULL;
1434 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301435 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 // loop runs CBOR encoding twice. First with no buffer to
1438 // calucate the length so buffer can be allocated correctly,
1439 // and last with the buffer to do the actual encoding
1440 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301441 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442 QCBOREncode_OpenMap(&ECtx);
1443 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1444 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1445 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1446 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1447 QCBOREncode_CloseArray(&ECtx);
1448 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1449 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1450 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1451 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1452 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1453 QCBOREncode_CloseMap(&ECtx);
1454 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001455
Laurence Lundblade0595e932018-11-02 22:22:47 +07001456 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 goto Done;
1458 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301459 if(uFirstSizeEstimate != *pEncodedLen) {
1460 nReturn = 1;
1461 } else {
1462 nReturn = 0;
1463 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001464 goto Done;
1465 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301466 *pEncoded = spBigBuf;
1467 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001470
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 Done:
1472 return(nReturn);
1473}
1474
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301475/*
1476 A3 # map(3)
1477 6D # text(13)
1478 666972737420696E7465676572 # "first integer"
1479 18 2A # unsigned(42)
1480 77 # text(23)
1481 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1482 82 # array(2)
1483 67 # text(7)
1484 737472696E6731 # "string1"
1485 67 # text(7)
1486 737472696E6732 # "string2"
1487 6C # text(12)
1488 6D617020696E2061206D6170 # "map in a map"
1489 A4 # map(4)
1490 67 # text(7)
1491 62797465732031 # "bytes 1"
1492 44 # bytes(4)
1493 78787878 # "xxxx"
1494 67 # text(7)
1495 62797465732032 # "bytes 2"
1496 44 # bytes(4)
1497 79797979 # "yyyy"
1498 6B # text(11)
1499 616E6F7468657220696E74 # "another int"
1500 18 62 # unsigned(98)
1501 66 # text(6)
1502 746578742032 # "text 2"
1503 78 1E # text(30)
1504 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1505 */
1506static const uint8_t spValidMapEncoded[] = {
1507 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1508 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1509 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1510 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1511 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1512 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1513 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1514 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1515 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1516 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1517 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1518 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1519 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1520 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1521 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1522 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523
1524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001525int32_t MapEncodeTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526{
1527 uint8_t *pEncodedMaps;
1528 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301531 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001534 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301535 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1536 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001538 return(nReturn);
1539}
1540
1541
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001542/*
1543 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001544
Laurence Lundbladeee851742020-01-08 08:37:05 -08001545 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1546 CBOR_SIMPLEV_NULL
1547 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 @param[in] szAlexString Diagnostic code.
1549 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001550 @param[in/out] pnLen Size of pOut buffer when called; length of data
1551 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001552
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001553 @return
1554 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556 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 -08001557 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1561 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 */
1564
Laurence Lundbladeee851742020-01-08 08:37:05 -08001565static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001566FormatRTICResults(uint8_t uRResult,
1567 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001568 const char *szType,
1569 const char *szAlexString,
1570 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571{
1572 // Buffer that the result will be written in to
1573 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001574 // QCBOREncode will never write off the end of this buffer. If it won't
1575 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001577 // Context for the encoder
1578 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301579 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001581 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1582 // Contents are label / value pairs
1583 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundbladeee851742020-01-08 08:37:05 -08001587 // The result: 0 if scan happened and found nothing; 1 if it happened and
1588 // found something wrong; 2 if it didn't happen
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001589 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590
1591 // Add the diagnostic code
1592 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1593
1594 // Add a time stamp
1595 if(time) {
1596 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1597 }
1598
1599 // Add the diagnostic code
1600 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1601
1602 // Open a subordinate map for telemtry data
1603 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1604
1605 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 // Add a few fake integers and buffers for now.
1608 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1609
1610 // Add a few fake integers and buffers for now.
1611 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1612
1613 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301614 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001615 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1618 }
1619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 // Close the telemetry map
1622 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 // Close the map
1625 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001626
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301627 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001628
Laurence Lundblade0595e932018-11-02 22:22:47 +07001629 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001630
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301631 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001632}
1633
1634
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301635/*
1636 A5 # map(5)
1637 69 # text(9)
1638 696E74656772697479 # "integrity"
1639 F4 # primitive(20)
1640 64 # text(4)
1641 74797065 # "type"
1642 66 # text(6)
1643 726563656E74 # "recent"
1644 64 # text(4)
1645 74696D65 # "time"
1646 C1 # tag(1)
1647 1A 580D4172 # unsigned(1477263730)
1648 64 # text(4)
1649 64696167 # "diag"
1650 6A # text(10)
1651 30784131654335303031 # "0xA1eC5001"
1652 69 # text(9)
1653 74656C656D65747279 # "telemetry"
1654 A3 # map(3)
1655 69 # text(9)
1656 53686F652053697A65 # "Shoe Size"
1657 0C # unsigned(12)
1658 62 # text(2)
1659 4951 # "IQ"
1660 1A FFFFFFFF # unsigned(4294967295)
1661 77 # text(23)
1662 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1663 48 # bytes(8)
1664 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1665 */
1666static const uint8_t spExpectedRTIC[] = {
1667 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1668 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1669 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1670 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1671 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1672 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1673 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1674 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1675 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1676 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1677 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1678 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001679
1680
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001681int32_t RTICResultsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001682{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001683 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301684 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301685 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301686 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301688 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301690 if(CheckResults(Encoded, spExpectedRTIC)) {
1691 return -2;
1692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001693
1694 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001695}
1696
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301697
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301698/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001699 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001700
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301701 82 # array(2)
1702 19 01C3 # unsigned(451)
1703 43 # bytes(3)
1704 1901D2 # "\x19\x01\xD2"
1705*/
1706static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301707
Laurence Lundblade684aec22018-10-12 19:33:53 +08001708/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001709 81 #array(1)
1710 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1711 */
1712static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1713
1714/*
Laurence Lundblade684aec22018-10-12 19:33:53 +08001715 Very basic bstr wrapping test
1716 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301717int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001718{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001719 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001720
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001721 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301722 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001723
Laurence Lundblade684aec22018-10-12 19:33:53 +08001724 QCBOREncode_OpenArray(&EC);
1725 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001726
Laurence Lundblade684aec22018-10-12 19:33:53 +08001727 QCBOREncode_BstrWrap(&EC);
1728 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001729
Laurence Lundblade684aec22018-10-12 19:33:53 +08001730 UsefulBufC Wrapped;
1731 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001732
Laurence Lundblade684aec22018-10-12 19:33:53 +08001733 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001734
Laurence Lundblade684aec22018-10-12 19:33:53 +08001735 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001736 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001737 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001739
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301740 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001741 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001743
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001744 // Second test - see if the length of the wrapped
1745 // bstr is correct. Also tests bstr wrapping
1746 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001747 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1748 QCBOREncode_OpenArray(&EC);
1749 QCBOREncode_BstrWrap(&EC);
1750 QCBOREncode_OpenArray(&EC);
1751 QCBOREncode_AddNULL(&EC);
1752 QCBOREncode_CloseArray(&EC);
1753 UsefulBufC BStr;
1754 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001755 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1756 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001757 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001758 return -5;
1759 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001760
Laurence Lundbladeda532272019-04-07 11:40:17 -07001761 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1762 // bstr wrapping use cases.
1763 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1764 QCBOREncode_Init(&EC, StuffBuf);
1765 QCBOREncode_OpenArray(&EC);
1766 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1767 QCBOREncode_CloseArray(&EC);
1768 if(QCBOREncode_Finish(&EC, &Encoded)) {
1769 return -6;
1770 }
1771 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1772 return -7;
1773 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001774
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001775 return 0;
1776}
1777
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001778
1779
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001780int32_t BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001781{
Laurence Lundbladeee851742020-01-08 08:37:05 -08001782 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001783 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001784
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301785 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001786
Laurence Lundblade684aec22018-10-12 19:33:53 +08001787 QCBOREncode_OpenArray(&EC);
1788 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001789
Laurence Lundblade684aec22018-10-12 19:33:53 +08001790 QCBOREncode_BstrWrap(&EC);
1791 QCBOREncode_AddUInt64(&EC, 466);
1792 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001793
Laurence Lundblade684aec22018-10-12 19:33:53 +08001794 UsefulBufC Wrapped;
1795 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001796
Laurence Lundblade684aec22018-10-12 19:33:53 +08001797 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001798
Laurence Lundblade684aec22018-10-12 19:33:53 +08001799 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001800 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001801 return -1;
1802 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001803
Laurence Lundbladeee851742020-01-08 08:37:05 -08001804 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301805 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001806 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade0595e932018-11-02 22:22:47 +07001807 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001808 return -2;
1809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001810
Laurence Lundblade684aec22018-10-12 19:33:53 +08001811 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301812 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001813 for(int i = 1; i < 18; i++) {
1814 QCBOREncode_BstrWrap(&EC);
1815 }
1816 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001817
Laurence Lundblade684aec22018-10-12 19:33:53 +08001818 for(int i = 1; i < 18; i++) {
1819 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1820 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001821
Laurence Lundblade0595e932018-11-02 22:22:47 +07001822 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001823 return -3;
1824 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001825
Laurence Lundblade684aec22018-10-12 19:33:53 +08001826 return 0;
1827}
1828
1829
Laurence Lundblade684aec22018-10-12 19:33:53 +08001830/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001831 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001832
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001833 [
1834 h'82004E82014B8202488203458204428105',
1835 {
1836 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1837 8426568656C6C6F18234BA2131318436568656C6C6F'
1838 }
1839 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001840
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001841 Unwrapping the first byte string in the above gives
1842 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301843
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001844 Unwrapping again, the byte string immediately above gives
1845 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301846
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001847 ...
1848
1849 Unrapping the second byte string in the top-level CBOR above gives
1850 {16: 16,
1851 64: "hello",
1852 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1853 }
1854
1855 Unwrapping again, the byte string immediately above gives
1856 {17: 17,
1857 65: "hello",
1858 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
1859 }
1860
1861 ...
1862
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301863 */
1864static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001865{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001866 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
1867 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
1868 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
1869 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
1870 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
1871 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
1872 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
1873 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1874 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
1875 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08001876};
1877
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001878
1879/*
1880 Get an int64 out of the decoder or fail.
1881 */
1882static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001883{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001884 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001885 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001886
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001887 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001888 if(nReturn) {
1889 return nReturn;
1890 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08001891 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001892 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001894
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001895 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001896 return 0;
1897}
1898
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001899/*
1900 Get an array out of the decoder or fail.
1901 */
1902static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001903{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001904 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001905 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001906
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001907 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001908 if(nReturn) {
1909 return nReturn;
1910 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001911 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1912 return -1;
1913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001914
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001915 *pInt = Item.val.uCount;
1916 return 0;
1917}
1918
1919/*
1920 Get a map out of the decoder or fail.
1921 */
1922static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
1923{
1924 QCBORItem Item;
1925 int32_t nReturn;
1926
1927 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001928 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001929 return nReturn;
1930 }
1931 if(Item.uDataType != QCBOR_TYPE_MAP) {
1932 return -1;
1933 }
1934
1935 *pInt = Item.val.uCount;
1936 return 0;
1937}
1938
1939/*
1940 Get a byte string out of the decoder or fail.
1941 */
1942static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
1943{
1944 QCBORItem Item;
1945 int32_t nReturn;
1946
1947 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
1948 if(nReturn) {
1949 return nReturn;
1950 }
1951 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1952 return -1; // TODO: better type?
1953 }
1954
1955 *pBstr = Item.val.string;
1956 return 0;
1957}
1958
1959/*
1960 Get a byte string out of the decoder or fail.
1961 */
1962static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
1963{
1964 QCBORItem Item;
1965 int nReturn;
1966
1967 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
1968 if(nReturn) {
1969 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001970 }
1971 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001972 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08001973 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001974
1975 *pTstr = Item.val.string;
1976 return 0;
1977}
1978
1979
1980/*
1981 Recursively decode array containing a little CBOR and a bstr wrapped array
1982 with a little CBOR and a bstr wrapped array...
1983
1984 Part of bstr_wrap_nest_test.
1985 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
1986{
1987 int64_t nInt;
1988 UsefulBufC Bstr;
1989 uint16_t nArrayCount;
1990 QCBORDecodeContext DC;
1991 int32_t nResult;
1992
1993 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1994
1995 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
1996 return -10;
1997 }
1998
1999 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002000 return -11;
2001 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002002
2003 nResult = GetByteString(&DC, &Bstr);
2004 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2005 if(nArrayCount != 1) {
2006 return -12;
2007 } else {
2008 // successful exit
2009 return 0;
2010 }
2011 }
2012 if(nResult) {
2013 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002014 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002015
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002016 // tail recursion; good compilers will reuse the stack frame
2017 return DecodeNextNested(Bstr);
2018}
2019
2020
2021/*
2022 Recursively decode map containing a little CBOR and a bstr wrapped map
2023 with a little CBOR and a bstr wrapped map...
2024
2025 Part of bstr_wrap_nest_test.
2026 */
2027static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2028{
2029 int32_t nResult;
2030 uint16_t nMapCount;
2031 int64_t nInt;
2032 UsefulBufC Bstr;
2033 QCBORDecodeContext DC;
2034
2035 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2036
2037 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2038 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002040
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002041 if(GetInt64(&DC, &nInt)) {
2042 return -21;
2043 }
2044
2045 // The "hello"
2046 if(GetTextString(&DC, &Bstr)) {
2047 return -22;
2048 }
2049
2050 nResult = GetByteString(&DC, &Bstr);
2051 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2052 if(nMapCount == 2) {
2053 // successful exit
2054 return 0;
2055 } else {
2056 return -23;
2057 }
2058 }
2059
2060 if(nResult) {
2061 return -24;
2062 }
2063
2064 // tail recursion; good compilers will reuse the stack frame
2065 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002066}
2067
2068
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002069int32_t BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002070{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002071 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302072 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002073
Laurence Lundblade684aec22018-10-12 19:33:53 +08002074 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002075 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002076
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002077 QCBOREncode_OpenArray(&EC);
2078
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002079 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002080 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002081 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002082 QCBOREncode_AddInt64(&EC, i);
2083 }
2084 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2085 QCBOREncode_CloseArray(&EC);
2086 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002088
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002089 QCBOREncode_OpenMap(&EC);
2090 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2091 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2092 QCBOREncode_OpenMap(&EC);
2093 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002094 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002095 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002096
2097 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2098 QCBOREncode_CloseMap(&EC);
2099 QCBOREncode_CloseBstrWrap(&EC, NULL);
2100 }
2101 QCBOREncode_CloseMap(&EC);
2102
Laurence Lundblade684aec22018-10-12 19:33:53 +08002103 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002104
Laurence Lundblade684aec22018-10-12 19:33:53 +08002105 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002106 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002107 return -1;
2108 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002109
Laurence Lundblade684aec22018-10-12 19:33:53 +08002110 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302111 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002112 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002114
Laurence Lundblade684aec22018-10-12 19:33:53 +08002115 // ---- Decode it and see if it is OK ------
2116 QCBORDecodeContext DC;
2117 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002118
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002119 UsefulBufC Bstr;
2120 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002122 // Array surrounding the the whole thing
2123 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002124 return -3;
2125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002126
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002127 // Get the byte string wrapping some array stuff
2128 if(GetByteString(&DC, &Bstr)) {
2129 return -4;
2130 }
2131
2132 // Decode the wrapped nested structure
2133 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002134 if(nReturn) {
2135 return nReturn;
2136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002137
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002138 // A map enclosing some map-oriented bstr wraps
2139 if(GetMap(&DC, &nArrayCount)) {
2140 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002142
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002143 // Get the byte string wrapping some array stuff
2144 if(GetByteString(&DC, &Bstr)) {
2145 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002147
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002148 // Decode the wrapped nested structure
2149 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002150 if(nReturn) {
2151 return nReturn;
2152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002153
Laurence Lundblade684aec22018-10-12 19:33:53 +08002154 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002155 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002156 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002157
Laurence Lundblade684aec22018-10-12 19:33:53 +08002158 return 0;
2159}
2160
2161
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002162static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302163 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2164 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2165 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2166 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2167 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2168 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2169 0x45, 0xca, 0xcb, 0x36};
2170
2171/*
2172 D2 # tag(18)
2173 84 # array(4)
2174 43 # bytes(3)
2175 A10126 # "\xA1\x01&"
2176 A1 # map(1)
2177 04 # unsigned(4)
2178 42 # bytes(2)
2179 3131 # "11"
2180 54 # bytes(20)
2181 546869732069732074686520636F6E74656E742E # "This is the content."
2182 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002183 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2184 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2185 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2186 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2187 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2188 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2189 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2190 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302191 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002192static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302193 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2194 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2195 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2196 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2197 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2198 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2199 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2200 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2201 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2202 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2203
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002204static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2205
2206
Laurence Lundblade684aec22018-10-12 19:33:53 +08002207/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002208 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002209 C.2.1. This doesn't actually verify the signature (however
2210 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002211 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002212int32_t CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002213{
2214 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002215 const char *szKid = "11";
2216 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2217 const char *szPayload = "This is the content.";
2218 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2219 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2220 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221
Laurence Lundblade684aec22018-10-12 19:33:53 +08002222 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002223
2224 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302225 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundblade684aec22018-10-12 19:33:53 +08002227 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002228 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002229 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundblade684aec22018-10-12 19:33:53 +08002231 // Add protected headers
2232 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002233
Laurence Lundblade684aec22018-10-12 19:33:53 +08002234 // Empty map with unprotected headers
2235 QCBOREncode_OpenMap(&EC);
2236 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2237 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002238
Laurence Lundblade684aec22018-10-12 19:33:53 +08002239 // The payload
2240 UsefulBufC WrappedPayload;
2241 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002242 // Payload is not actually CBOR in example C.2.1 like it would be
2243 // for a CWT or EAT. It is just a text string.
2244 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002245 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundblade684aec22018-10-12 19:33:53 +08002247 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002248 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2249 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002250 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002251 return -1;
2252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002253
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002254/* if(UsefulBuf_Compare(WrappedPayload,
2255 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2256 return -1;
2257 } */
2258
Laurence Lundblade684aec22018-10-12 19:33:53 +08002259 // The signature
2260 QCBOREncode_AddBytes(&EC, Signature);
2261 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundblade684aec22018-10-12 19:33:53 +08002263 // Finish and check the results
2264 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002265 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002266 return -2;
2267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundblade684aec22018-10-12 19:33:53 +08002269 // 98 is the size from RFC 8152 C.2.1
2270 if(COSE_Sign1.len != 98) {
2271 return -3;
2272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002273
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002274 // It would be good to compare this to the output from a COSE
2275 // implementation like COSE-C. This has been checked against the
2276 // CBOR playground.
2277 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002278 return -4;
2279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002280
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002281
2282 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2283 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2284
2285 // top level array for cose sign1, 18 is the tag for COSE sign
2286 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2287 QCBOREncode_OpenArray(&EC);
2288
2289 // Add protected headers
2290 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2291
2292 // Empty map with unprotected headers
2293 QCBOREncode_OpenMap(&EC);
2294 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2295 QCBOREncode_CloseMap(&EC);
2296
2297 // The payload
2298 QCBOREncode_BstrWrap(&EC);
2299 // Payload is not actually CBOR in example C.2.1 like it would be
2300 // for a CWT or EAT. It is just a text string.
2301 QCBOREncode_AddEncoded(&EC, Payload);
2302 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2303
2304 // Check we got back the actual payload expected
2305 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2306 if(UsefulBuf_Compare(WrappedPayload,
2307 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2308 return -11;
2309 }
2310
2311 // The signature
2312 QCBOREncode_AddBytes(&EC, Signature);
2313 QCBOREncode_CloseArray(&EC);
2314
2315 // Finish and check the results
2316 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2317 return -12;
2318 }
2319
2320 // 98 is the size from RFC 8152 C.2.1
2321 if(COSE_Sign1.len != 98) {
2322 return -13;
2323 }
2324
2325 // It would be good to compare this to the output from a COSE
2326 // implementation like COSE-C. This has been checked against the
2327 // CBOR playground.
2328 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2329 return -14;
2330 }
2331
Laurence Lundblade684aec22018-10-12 19:33:53 +08002332 return 0;
2333}
2334
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002335
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002336int32_t EncodeErrorTests()
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002337{
2338 QCBOREncodeContext EC;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002339
2340
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002341 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002342 // Do all of these tests with NULL buffers so no actual
2343 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002344 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002345
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002346 // First verify no error from a big buffer
2347 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002348 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002349 // 6 is the CBOR overhead for opening the array and encodng the length
2350 // This exactly fills the buffer.
2351 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002352 QCBOREncode_CloseArray(&EC);
2353 size_t xx;
2354 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2355 return -1;
2356 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002357
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002358 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002359 // Also test fetching the error code before finish
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002360 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002361 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002362 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
2363 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002364 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2365 // Error fetch failed.
2366 return -12;
2367 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002368 QCBOREncode_CloseArray(&EC);
2369 QCBOREncode_CloseArray(&EC);
2370 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2371 return -2;
2372 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002373
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002374 // Third, fit an array in exactly at max position allowed
2375 QCBOREncode_Init(&EC, Buffer);
2376 QCBOREncode_OpenArray(&EC);
2377 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2378 QCBOREncode_OpenArray(&EC);
2379 QCBOREncode_CloseArray(&EC);
2380 QCBOREncode_CloseArray(&EC);
2381 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2382 return -10;
2383 }
2384
2385
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002386 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2387 // Work close to the 4GB size limit for a better test
2388 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002389 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002390
2391 QCBOREncode_Init(&EC, Large);
2392 QCBOREncode_OpenArray(&EC);
2393 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2394 QCBOREncode_CloseArray(&EC);
2395 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2396 // Making sure it succeeds when it should first
2397 return -3;
2398 }
2399
2400 QCBOREncode_Init(&EC, Large);
2401 QCBOREncode_OpenArray(&EC);
2402 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2403 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2404 QCBOREncode_CloseArray(&EC);
2405 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2406 // Now just 1 byte over, see that it fails
2407 return -4;
2408 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002409
2410
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002411 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2412 QCBOREncode_Init(&EC, Large);
2413 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2414 QCBOREncode_OpenArray(&EC);
2415 }
2416 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2417 QCBOREncode_CloseArray(&EC);
2418 }
2419 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2420 // Making sure it succeeds when it should first
2421 return -5;
2422 }
2423
2424 QCBOREncode_Init(&EC, Large);
2425 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2426 QCBOREncode_OpenArray(&EC);
2427 }
2428 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2429 QCBOREncode_CloseArray(&EC);
2430 }
2431 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2432 // One more level to cause error
2433 return -6;
2434 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002435
2436
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002437 // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
2438 QCBOREncode_Init(&EC, Large);
2439 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2440 QCBOREncode_OpenArray(&EC);
2441 }
2442 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2443 QCBOREncode_CloseArray(&EC);
2444 }
2445 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
2446 // One more level to cause error
2447 return -7;
2448 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002449
2450
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002451 // ------ QCBOR_ERR_CLOSE_MISMATCH --------
2452 QCBOREncode_Init(&EC, Large);
2453 QCBOREncode_OpenArray(&EC);
2454 UsefulBufC Wrap;
2455 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
2456 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
2457 return -8;
2458 }
2459
2460
2461 // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
2462 QCBOREncode_Init(&EC, Large);
2463 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2464 QCBOREncode_OpenArray(&EC);
2465 }
2466 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2467 QCBOREncode_CloseArray(&EC);
2468 }
2469 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2470 // One more level to cause error
2471 return -9;
2472 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002473
Laurence Lundblade241705e2018-12-30 18:56:14 -08002474 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2475 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002476
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002477
2478 // ----- Test the check for NULL buffer ------
2479 QCBOREncode_Init(&EC, Buffer);
2480 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2481 return -11;
2482 }
2483
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002484 // ------ QCBOR_ERR_UNSUPPORTED --------
2485 QCBOREncode_Init(&EC, Large);
2486 QCBOREncode_OpenArray(&EC);
2487 QCBOREncode_AddSimple(&EC, 24); // CBOR_SIMPLEV_RESERVED_START
2488 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2489 return -12;
2490 }
2491
2492 QCBOREncode_Init(&EC, Large);
2493 QCBOREncode_OpenArray(&EC);
2494 QCBOREncode_AddSimple(&EC, 31); // CBOR_SIMPLEV_RESERVED_END
2495 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2496 return -13;
2497 }
2498
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002499 return 0;
2500}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002501
2502
2503#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2504/*
2505 [
2506 4([-1, 3]),
2507 4([-20, 4759477275222530853136]),
2508 4([9223372036854775807, -4759477275222530853137]),
2509 5([300, 100]),
2510 5([-20, 4759477275222530853136]),
2511 5([-9223372036854775808, -4759477275222530853137])
2512 ]
2513 */
2514static const uint8_t spExpectedExponentAndMantissaArray[] = {
2515 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2516 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2517 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2518 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2519 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2520 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2521 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2522 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2523 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2524 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2525 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2526
2527
2528/*
2529 {
2530 "decimal fraction": 4([-1, 3]),
2531 300: 4([-1, 3]),
2532 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2533 400: 4([2147483647, 4759477275222530853136]),
2534 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2535 500: 4([9223372036854775807, -4759477275222530853137]),
2536 "big float": 5([300, 100]),
2537 600: 5([300, 100]),
2538 "big float bignum positive": 5([-20, 4759477275222530853136]),
2539 700: 5([-20, 4759477275222530853136]),
2540 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2541 800: 5([-9223372036854775808, -4759477275222530853137])
2542 }
2543 */
2544static const uint8_t spExpectedExponentAndMantissaMap[] = {
2545 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2546 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2547 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2548 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2549 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2550 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2551 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2552 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2553 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2554 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2555 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2556 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2557 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2558 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2559 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2560 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2561 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2562 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2563 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2564 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2565 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2566 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2567 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2568 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2569 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2570 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2571 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2572 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2573 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2574 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2575 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2576 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2577 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2578 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2579 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2580 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2581 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2582 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2583 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2584 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2585 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2586 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2587 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2588 0x09, 0x10
2589};
2590
2591
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002592int32_t ExponentAndMantissaEncodeTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08002593{
2594 QCBOREncodeContext EC;
2595 UsefulBufC EncodedExponentAndMantissa;
2596
2597 // Constant for the big number used in all the tests.
2598 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2599 0x06, 0x07, 0x08, 0x09, 0x010};
2600 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2601
2602 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2603 QCBOREncode_OpenArray(&EC);
2604 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2605 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2606 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2607 QCBOREncode_AddBigFloat(&EC, 100, 300);
2608 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2609 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2610 QCBOREncode_CloseArray(&EC);
2611
2612 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2613 return -2;
2614 }
2615
2616 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2617 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2618 NULL);
2619 if(nReturn) {
2620 return nReturn;
2621 }
2622
2623 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2624 QCBOREncode_OpenMap(&EC);
2625
2626 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2627
2628 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2629
2630 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2631 "decimal fraction bignum postive",
2632 BigNum,
2633 false,
2634 -200);
2635
2636 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2637 400,
2638 BigNum,
2639 false,
2640 INT32_MAX);
2641
2642 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2643 "decimal fraction bignum negative",
2644 BigNum,
2645 true,
2646 INT64_MAX);
2647
2648 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2649 500,
2650 BigNum,
2651 true,
2652 INT64_MAX);
2653
2654 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2655
2656 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2657
2658 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2659 "big float bignum positive",
2660 BigNum,
2661 false,
2662 -20);
2663
2664 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2665 700,
2666 BigNum,
2667 false,
2668 -20);
2669
2670 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2671 "big float bignum negative",
2672 BigNum,
2673 true,
2674 INT64_MIN);
2675
2676 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2677 800,
2678 BigNum,
2679 true,
2680 INT64_MIN);
2681
2682 QCBOREncode_CloseMap(&EC);
2683
2684 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2685 return -3;
2686 }
2687
2688
2689 struct UBCompareDiagnostic Diag;
2690
2691 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2692 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2693 &Diag);
2694 if(nReturn) {
2695 return nReturn + 1000000; // +1000000 to distinguish from first test above
2696 }
2697
2698 return 0;
2699}
2700
2701#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002702
2703
2704int32_t QCBORHeadTest()
2705{
2706 /* This test doesn't have to be extensive, because just about every
2707 * other test exercises QCBOREncode_EncodeHead().
2708 */
2709 // ---- basic test to encode a zero ----
2710 MakeUsefulBufOnStack(RightSize, QCBOR_HEAD_BUFFER_SIZE);
2711
2712 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2713 CBOR_MAJOR_TYPE_POSITIVE_INT,
2714 0,
2715 0);
2716
2717 static const uint8_t expectedZero[] = {0x00};
2718
2719 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2720 return -1;
2721 }
2722
2723 // ---- Encode a zero padded out to an 8 byte integer ----
2724 encoded = QCBOREncode_EncodeHead(RightSize,
2725 CBOR_MAJOR_TYPE_POSITIVE_INT,
2726 8, // uMinSize is 8 bytes
2727 0);
2728
2729 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2730 0x00, 0x00, 0x00, 0x00};
2731
2732 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2733 return -2;
2734 }
2735
2736
2737 // ---- Try to encode into too-small a buffer ----
2738 MakeUsefulBufOnStack(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
2739
2740 encoded = QCBOREncode_EncodeHead(TooSmall,
2741 CBOR_MAJOR_TYPE_POSITIVE_INT,
2742 0,
2743 0);
2744
2745 if(!UsefulBuf_IsNULLC(encoded)) {
2746 return -3;
2747 }
2748
2749 return 0;
2750}