blob: 36187e3ddfca769bd39877bef545dae1ed8e8799 [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
1830
1831// Part of bstr_wrap_nest_test
1832/*
1833 83 array with three
1834 53 byte string with 19 bytes
1835 01 #1
1836 50 byte string with 16 bytes
1837 02
1838 4D byte string with 13 bytes
1839 03
1840 4A byte string with 10 bytes
1841 04
1842 47 byte string with 7 bytes
1843 05
1844 44 byte string with 4 bytes
1845 06
1846 41 byte string with 1 byte
1847 07
1848 01
1849 02
1850 03
1851 04
1852 05
1853 06
1854 07
1855 A2 map with two items
1856 18 20 label for byte string
1857 54 byte string of length 20
1858 82 Array with two items
1859 10 The integer value 10
1860 A2 map with two items
1861 18 21 label for byte string
1862 44 byte string with 4 bytes
1863 81 array with 1 item
1864 11 integer value 11
1865 18 30 integer value 30
1866 18 40 integer label 40
1867 65 68 65 6C 6C 6F text string hello
1868 18 31 integer value 31
1869 18 41 integer label 41
1870 65 68 65 6C 6C 6F text string hello
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001871
1872
Laurence Lundblade684aec22018-10-12 19:33:53 +08001873 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301874
1875
1876/*
1877 83 # array(3)
1878 56 # bytes(22)
1879 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1880 07 # unsigned(7)
1881 A2 # map(2)
1882 18 20 # unsigned(32)
1883 54 # bytes(20)
1884 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1885 18 41 # unsigned(65)
1886 65 # text(5)
1887 68656C6C6F # "hello"
1888 */
1889static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001890{
1891 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1892 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1893 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1894 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1895 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1896 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1897 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1898 0x6F
1899};
1900
1901// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301902static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001903{
1904 int nReturn;
1905 QCBORDecodeContext DC;
1906 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001907
Laurence Lundblade684aec22018-10-12 19:33:53 +08001908 QCBORItem Item;
1909 nReturn = QCBORDecode_GetNext(&DC, &Item);
1910 if(nReturn) {
1911 return -11;
1912 }
1913 if(Item.uDataType != QCBOR_TYPE_INT64) {
1914 return -12;
1915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001916
Laurence Lundblade684aec22018-10-12 19:33:53 +08001917 nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001918 if(nReturn == QCBOR_ERR_HIT_END || nReturn == QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001919 return 0;
1920 }
1921 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1922 return -13;
1923 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301924 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001925 if(nReturn) {
1926 return nReturn;
1927 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001928
Laurence Lundblade684aec22018-10-12 19:33:53 +08001929 nReturn = QCBORDecode_GetNext(&DC, &Item);
1930 if(nReturn) {
1931 return -14;
1932 }
1933 if(Item.uDataType != QCBOR_TYPE_INT64) {
1934 return -15;
1935 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001936
Laurence Lundblade684aec22018-10-12 19:33:53 +08001937 if(QCBORDecode_Finish(&DC)) {
1938 return -16;
1939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001940
Laurence Lundblade684aec22018-10-12 19:33:53 +08001941 return 0;
1942}
1943
1944// Part of bstr_wrap_nest_test
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001945static int32_t DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001946{
1947 int nReturn;
1948 QCBORDecodeContext DC;
1949 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade684aec22018-10-12 19:33:53 +08001951 QCBORItem Item;
1952 nReturn = QCBORDecode_GetNext(&DC, &Item);
1953 if(nReturn) {
1954 return -11;
1955 }
1956 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1957 return -12;
1958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade684aec22018-10-12 19:33:53 +08001960 nReturn = QCBORDecode_GetNext(&DC, &Item);
1961 if(nReturn) {
1962 return -11;
1963 }
1964 if(Item.uDataType != QCBOR_TYPE_INT64) {
1965 return -12;
1966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001967
Laurence Lundblade684aec22018-10-12 19:33:53 +08001968 nReturn = QCBORDecode_GetNext(&DC, &Item);
1969 if(nReturn) {
1970 return -11;
1971 }
1972 if(Item.uDataType != QCBOR_TYPE_MAP) {
1973 return 0;
1974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001975
Laurence Lundblade684aec22018-10-12 19:33:53 +08001976 nReturn = QCBORDecode_GetNext(&DC, &Item);
1977 if(nReturn) {
1978 return -11;
1979 }
1980 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1981 return -13;
1982 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301983 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001984 if(nReturn) {
1985 return nReturn;
1986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001987
Laurence Lundblade684aec22018-10-12 19:33:53 +08001988 nReturn = QCBORDecode_GetNext(&DC, &Item);
1989 if(nReturn) {
1990 return -11;
1991 }
1992 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1993 return -12;
1994 }
1995 nReturn = QCBORDecode_GetNext(&DC, &Item);
1996 if(nReturn) {
1997 return -11;
1998 }
1999 if(Item.uDataType != QCBOR_TYPE_INT64) {
2000 return -12;
2001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundblade684aec22018-10-12 19:33:53 +08002003 if(QCBORDecode_Finish(&DC)) {
2004 return -16;
2005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002006
Laurence Lundblade684aec22018-10-12 19:33:53 +08002007 return 0;
2008}
2009
2010
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002011int32_t BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002012{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002013 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302014 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002015
Laurence Lundblade684aec22018-10-12 19:33:53 +08002016 // ---- Make a complicated nested CBOR structure ---
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002017#define BSTR_TEST_DEPTH 10
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002018
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002019 QCBOREncode_OpenArray(&EC);
2020
2021 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002022 QCBOREncode_BstrWrap(&EC);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002023 QCBOREncode_AddInt64(&EC, i);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002025
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002026 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002027 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002028 QCBOREncode_AddInt64(&EC, i);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002030
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002031 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002032 QCBOREncode_OpenMap(&EC);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002033 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002034 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002035 QCBOREncode_AddInt64(&EC, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002036 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002037
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002038 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002039 QCBOREncode_CloseArray(&EC);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002040 QCBOREncode_AddInt64(&EC, i+0x30);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002041 QCBOREncode_CloseBstrWrap(&EC, NULL);
2042 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
2043 QCBOREncode_CloseMap(&EC);
2044 }
2045 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002046
Laurence Lundblade684aec22018-10-12 19:33:53 +08002047 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002048 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002049 return -1;
2050 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002051
Laurence Lundblade684aec22018-10-12 19:33:53 +08002052 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302053 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002054 return -25;
2055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002056
2057
Laurence Lundblade684aec22018-10-12 19:33:53 +08002058 // ---- Decode it and see if it is OK ------
2059 QCBORDecodeContext DC;
2060 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002061
Laurence Lundblade684aec22018-10-12 19:33:53 +08002062 QCBORItem Item;
2063 QCBORDecode_GetNext(&DC, &Item);
2064 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
2065 return -2;
2066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002067
Laurence Lundblade684aec22018-10-12 19:33:53 +08002068 QCBORDecode_GetNext(&DC, &Item);
2069 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2070 return -3;
2071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302073 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002074 if(nReturn) {
2075 return nReturn;
2076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002077
Laurence Lundblade684aec22018-10-12 19:33:53 +08002078 nReturn = QCBORDecode_GetNext(&DC, &Item);
2079 if(nReturn) {
2080 return -11;
2081 }
2082 if(Item.uDataType != QCBOR_TYPE_INT64) {
2083 return -12;
2084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002085
Laurence Lundblade684aec22018-10-12 19:33:53 +08002086 QCBORDecode_GetNext(&DC, &Item);
2087 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
2088 return -2;
2089 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002090
Laurence Lundblade684aec22018-10-12 19:33:53 +08002091 QCBORDecode_GetNext(&DC, &Item);
2092 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2093 return -3;
2094 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302095 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002096 if(nReturn) {
2097 return nReturn;
2098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002099
Laurence Lundblade684aec22018-10-12 19:33:53 +08002100 nReturn = QCBORDecode_GetNext(&DC, &Item);
2101 if(nReturn) {
2102 return -11;
2103 }
2104 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
2105 return -12;
2106 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002107
Laurence Lundblade684aec22018-10-12 19:33:53 +08002108 if(QCBORDecode_Finish(&DC)) {
2109 return -16;
2110 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002111
Laurence Lundblade684aec22018-10-12 19:33:53 +08002112 return 0;
2113}
2114
2115
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002116static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302117 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2118 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2119 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2120 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2121 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2122 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2123 0x45, 0xca, 0xcb, 0x36};
2124
2125/*
2126 D2 # tag(18)
2127 84 # array(4)
2128 43 # bytes(3)
2129 A10126 # "\xA1\x01&"
2130 A1 # map(1)
2131 04 # unsigned(4)
2132 42 # bytes(2)
2133 3131 # "11"
2134 54 # bytes(20)
2135 546869732069732074686520636F6E74656E742E # "This is the content."
2136 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002137 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2138 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2139 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2140 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2141 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2142 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2143 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2144 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302145 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002146static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302147 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2148 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2149 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2150 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2151 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2152 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2153 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2154 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2155 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2156 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2157
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002158static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2159
2160
Laurence Lundblade684aec22018-10-12 19:33:53 +08002161/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002162 This corresponds exactly to the example in RFC 8152 section
2163 C.2.1. This doesn't actually verify the signature though that would
2164 be nice as it would make the test really good. That would require
2165 bring in ECDSA crypto to this test.
Laurence Lundblade684aec22018-10-12 19:33:53 +08002166 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002167int32_t CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002168{
2169 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002170 const char *szKid = "11";
2171 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2172 const char *szPayload = "This is the content.";
2173 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2174 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2175 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade684aec22018-10-12 19:33:53 +08002177 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302178 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundblade684aec22018-10-12 19:33:53 +08002180 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002181 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002182 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002183
Laurence Lundblade684aec22018-10-12 19:33:53 +08002184 // Add protected headers
2185 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002186
Laurence Lundblade684aec22018-10-12 19:33:53 +08002187 // Empty map with unprotected headers
2188 QCBOREncode_OpenMap(&EC);
2189 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2190 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002191
Laurence Lundblade684aec22018-10-12 19:33:53 +08002192 // The payload
2193 UsefulBufC WrappedPayload;
2194 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002195 // Payload is not actually CBOR in example C.2.1 like it would be
2196 // for a CWT or EAT. It is just a text string.
2197 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002198 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundblade684aec22018-10-12 19:33:53 +08002200 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002201 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2202 if(UsefulBuf_Compare(WrappedPayload,
2203 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002204 return -1;
2205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002206
Laurence Lundblade684aec22018-10-12 19:33:53 +08002207 // The signature
2208 QCBOREncode_AddBytes(&EC, Signature);
2209 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002210
Laurence Lundblade684aec22018-10-12 19:33:53 +08002211 // Finish and check the results
2212 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002213 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002214 return -2;
2215 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002216
Laurence Lundblade684aec22018-10-12 19:33:53 +08002217 // 98 is the size from RFC 8152 C.2.1
2218 if(COSE_Sign1.len != 98) {
2219 return -3;
2220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002222 // It would be good to compare this to the output from a COSE
2223 // implementation like COSE-C. This has been checked against the
2224 // CBOR playground.
2225 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002226 return -4;
2227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002228
Laurence Lundblade684aec22018-10-12 19:33:53 +08002229 return 0;
2230}
2231
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002232
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002233int32_t EncodeErrorTests()
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002234{
2235 QCBOREncodeContext EC;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002236
2237
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002238 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002239 // Do all of these tests with NULL buffers so no actual
2240 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002241 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002242
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002243 // First verify no error from a big buffer
2244 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002245 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002246 // 6 is the CBOR overhead for opening the array and encodng the length
2247 // This exactly fills the buffer.
2248 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002249 QCBOREncode_CloseArray(&EC);
2250 size_t xx;
2251 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2252 return -1;
2253 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002254
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002255 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002256 // Also test fetching the error code before finish
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002257 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002258 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002259 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
2260 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002261 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2262 // Error fetch failed.
2263 return -12;
2264 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002265 QCBOREncode_CloseArray(&EC);
2266 QCBOREncode_CloseArray(&EC);
2267 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2268 return -2;
2269 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002270
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002271 // Third, fit an array in exactly at max position allowed
2272 QCBOREncode_Init(&EC, Buffer);
2273 QCBOREncode_OpenArray(&EC);
2274 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2275 QCBOREncode_OpenArray(&EC);
2276 QCBOREncode_CloseArray(&EC);
2277 QCBOREncode_CloseArray(&EC);
2278 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2279 return -10;
2280 }
2281
2282
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002283 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2284 // Work close to the 4GB size limit for a better test
2285 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002286 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002287
2288 QCBOREncode_Init(&EC, Large);
2289 QCBOREncode_OpenArray(&EC);
2290 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2291 QCBOREncode_CloseArray(&EC);
2292 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2293 // Making sure it succeeds when it should first
2294 return -3;
2295 }
2296
2297 QCBOREncode_Init(&EC, Large);
2298 QCBOREncode_OpenArray(&EC);
2299 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2300 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2301 QCBOREncode_CloseArray(&EC);
2302 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2303 // Now just 1 byte over, see that it fails
2304 return -4;
2305 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002306
2307
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002308 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2309 QCBOREncode_Init(&EC, Large);
2310 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2311 QCBOREncode_OpenArray(&EC);
2312 }
2313 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2314 QCBOREncode_CloseArray(&EC);
2315 }
2316 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2317 // Making sure it succeeds when it should first
2318 return -5;
2319 }
2320
2321 QCBOREncode_Init(&EC, Large);
2322 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2323 QCBOREncode_OpenArray(&EC);
2324 }
2325 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2326 QCBOREncode_CloseArray(&EC);
2327 }
2328 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2329 // One more level to cause error
2330 return -6;
2331 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002332
2333
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002334 // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
2335 QCBOREncode_Init(&EC, Large);
2336 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2337 QCBOREncode_OpenArray(&EC);
2338 }
2339 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2340 QCBOREncode_CloseArray(&EC);
2341 }
2342 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
2343 // One more level to cause error
2344 return -7;
2345 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002346
2347
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002348 // ------ QCBOR_ERR_CLOSE_MISMATCH --------
2349 QCBOREncode_Init(&EC, Large);
2350 QCBOREncode_OpenArray(&EC);
2351 UsefulBufC Wrap;
2352 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
2353 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
2354 return -8;
2355 }
2356
2357
2358 // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
2359 QCBOREncode_Init(&EC, Large);
2360 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2361 QCBOREncode_OpenArray(&EC);
2362 }
2363 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2364 QCBOREncode_CloseArray(&EC);
2365 }
2366 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2367 // One more level to cause error
2368 return -9;
2369 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002370
Laurence Lundblade241705e2018-12-30 18:56:14 -08002371 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2372 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002373
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002374
2375 // ----- Test the check for NULL buffer ------
2376 QCBOREncode_Init(&EC, Buffer);
2377 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2378 return -11;
2379 }
2380
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002381 // ------ QCBOR_ERR_UNSUPPORTED --------
2382 QCBOREncode_Init(&EC, Large);
2383 QCBOREncode_OpenArray(&EC);
2384 QCBOREncode_AddSimple(&EC, 24); // CBOR_SIMPLEV_RESERVED_START
2385 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2386 return -12;
2387 }
2388
2389 QCBOREncode_Init(&EC, Large);
2390 QCBOREncode_OpenArray(&EC);
2391 QCBOREncode_AddSimple(&EC, 31); // CBOR_SIMPLEV_RESERVED_END
2392 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2393 return -13;
2394 }
2395
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002396 return 0;
2397}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002398
2399
2400#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2401/*
2402 [
2403 4([-1, 3]),
2404 4([-20, 4759477275222530853136]),
2405 4([9223372036854775807, -4759477275222530853137]),
2406 5([300, 100]),
2407 5([-20, 4759477275222530853136]),
2408 5([-9223372036854775808, -4759477275222530853137])
2409 ]
2410 */
2411static const uint8_t spExpectedExponentAndMantissaArray[] = {
2412 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2413 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2414 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2415 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2416 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2417 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2418 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2419 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2420 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2421 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2422 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2423
2424
2425/*
2426 {
2427 "decimal fraction": 4([-1, 3]),
2428 300: 4([-1, 3]),
2429 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2430 400: 4([2147483647, 4759477275222530853136]),
2431 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2432 500: 4([9223372036854775807, -4759477275222530853137]),
2433 "big float": 5([300, 100]),
2434 600: 5([300, 100]),
2435 "big float bignum positive": 5([-20, 4759477275222530853136]),
2436 700: 5([-20, 4759477275222530853136]),
2437 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2438 800: 5([-9223372036854775808, -4759477275222530853137])
2439 }
2440 */
2441static const uint8_t spExpectedExponentAndMantissaMap[] = {
2442 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2443 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2444 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2445 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2446 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2447 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2448 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2449 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2450 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2451 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2452 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2453 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2454 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2455 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2456 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2457 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2458 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2459 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2460 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2461 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2462 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2463 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2464 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2465 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2466 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2467 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2468 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2469 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2470 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2471 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2472 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2473 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2474 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2475 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2476 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2477 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2478 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2479 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2480 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2481 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2482 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2483 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2484 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2485 0x09, 0x10
2486};
2487
2488
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002489int32_t ExponentAndMantissaEncodeTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08002490{
2491 QCBOREncodeContext EC;
2492 UsefulBufC EncodedExponentAndMantissa;
2493
2494 // Constant for the big number used in all the tests.
2495 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2496 0x06, 0x07, 0x08, 0x09, 0x010};
2497 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2498
2499 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2500 QCBOREncode_OpenArray(&EC);
2501 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2502 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2503 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2504 QCBOREncode_AddBigFloat(&EC, 100, 300);
2505 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2506 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2507 QCBOREncode_CloseArray(&EC);
2508
2509 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2510 return -2;
2511 }
2512
2513 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2514 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2515 NULL);
2516 if(nReturn) {
2517 return nReturn;
2518 }
2519
2520 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2521 QCBOREncode_OpenMap(&EC);
2522
2523 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2524
2525 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2526
2527 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2528 "decimal fraction bignum postive",
2529 BigNum,
2530 false,
2531 -200);
2532
2533 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2534 400,
2535 BigNum,
2536 false,
2537 INT32_MAX);
2538
2539 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2540 "decimal fraction bignum negative",
2541 BigNum,
2542 true,
2543 INT64_MAX);
2544
2545 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2546 500,
2547 BigNum,
2548 true,
2549 INT64_MAX);
2550
2551 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2552
2553 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2554
2555 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2556 "big float bignum positive",
2557 BigNum,
2558 false,
2559 -20);
2560
2561 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2562 700,
2563 BigNum,
2564 false,
2565 -20);
2566
2567 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2568 "big float bignum negative",
2569 BigNum,
2570 true,
2571 INT64_MIN);
2572
2573 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2574 800,
2575 BigNum,
2576 true,
2577 INT64_MIN);
2578
2579 QCBOREncode_CloseMap(&EC);
2580
2581 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2582 return -3;
2583 }
2584
2585
2586 struct UBCompareDiagnostic Diag;
2587
2588 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2589 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2590 &Diag);
2591 if(nReturn) {
2592 return nReturn + 1000000; // +1000000 to distinguish from first test above
2593 }
2594
2595 return 0;
2596}
2597
2598#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */