blob: ab4fd7a8c6d0b86d6b75b1d1b5a935236889c324 [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 Lundblade6d926af2019-01-01 19:01:43 -08003 Copyright (c) 2018-2019, 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 Lundbladedc6e28e2018-10-11 19:19:27 +053031 ==============================================================================*/
32
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 Lundblade369b90a2018-10-22 02:04:37 +053050//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051
Laurence Lundblade067035b2018-11-28 17:35:25 -080052#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053053#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055// ifdef these out to not have compiler warnings
56static void printencoded(const uint8_t *pEncoded, size_t nLen)
57{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070058 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059 for(i = 0; i < nLen; i++) {
60 uint8_t Z = pEncoded[i];
61 printf("%02x ", Z);
62 }
63 printf("\n");
64
65 fflush(stdout);
66}
67
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080068
Laurence Lundblade369b90a2018-10-22 02:04:37 +053069// Do the comparison and print out where it fails
70static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070071 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053072 for(i = 0; i < U1.len; i++) {
73 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 printf("Position: %d Actual: 0x%x Expected: 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 return 1;
76 }
77 }
78 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080079
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053080}
81
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070082#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053083 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053084
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070085#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080086
87#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080088 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
89
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070090#endif
91
92
Laurence Lundblade59289e52019-12-30 13:44:37 -080093#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
94/*
95 Returns 0 if UsefulBufs are equal
96 Returns 1000000 + offeset if they are not equal.
97
98
99
100*/
101struct UBCompareDiagnostic {
102 uint8_t uActual;
103 uint8_t uExpected;
104 size_t uOffset;
105};
106
107static int32_t UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual, UsefulBufC Expected, struct UBCompareDiagnostic *pDiag) {
108 size_t i;
109 for(i = 0; i < Actual.len; i++) {
110 if(((uint8_t *)Actual.ptr)[i] != ((uint8_t *)Expected.ptr)[i]) {
111 if(pDiag) {
112 pDiag->uActual = ((uint8_t *)Actual.ptr)[i];
113 pDiag->uExpected = ((uint8_t *)Expected.ptr)[i];
114 pDiag->uOffset = i;
115 }
116 return (int32_t)i + 1000000; // Cast to int is OK as this is only a diagnostic and the sizes here are never over a few KB.
117 }
118 }
119 return 0;
120
121}
122#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
123
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700124
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530125// One big buffer that is used by all the tests to encode into
126// Putting it in uninitialized data is better than using a lot
127// of stack. The tests should run on small devices too.
128static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129
130
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800131
132/*
133 Some very minimal tests.
134 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530135int BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800136{
137 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800138 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800139
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530140 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530141
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142 QCBOREncode_OpenMap(&EC);
143 QCBOREncode_AddBoolToMapN(&EC, 66, true);
144 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800145
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800146 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700147 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530148 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800149 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 // Decode it and see that is right
153 QCBORDecodeContext DC;
154 QCBORItem Item;
155 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800157 QCBORDecode_GetNext(&DC, &Item);
158 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530159 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800162 QCBORDecode_GetNext(&DC, &Item);
163 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530164 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800167 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530168 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800170
171
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800172 // Make another encoded message with the CBOR from the previous put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530173 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800174 QCBOREncode_Init(&EC, MemoryForEncoded2);
175 QCBOREncode_OpenArray(&EC);
176 QCBOREncode_AddUInt64(&EC, 451);
177 QCBOREncode_AddEncoded(&EC, Encoded);
178 QCBOREncode_OpenMap(&EC);
179 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
180 QCBOREncode_CloseMap(&EC);
181 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800183 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700184 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530185 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800186 }
187 /*
188 [ // 0 1:3
189 451, // 1 1:2
190 { // 1 1:2 2:1
191 66: true // 2 1:1
192 },
193 { // 1 1:1 2:1
194 -70000: { // 2 1:1 2:1 3:1
195 66: true // 3 XXXXXX
196 }
197 }
198 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
200
201
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800202 83 # array(3)
203 19 01C3 # unsigned(451)
204 A1 # map(1)
205 18 42 # unsigned(66)
206 F5 # primitive(21)
207 A1 # map(1)
208 3A 0001116F # negative(69999)
209 A1 # map(1)
210 18 42 # unsigned(66)
211 F5 # primitive(21)
212 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800213
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214 // Decode it and see if it is OK
215 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800216
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800217 // 0 1:3
218 QCBORDecode_GetNext(&DC, &Item);
219 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530220 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800223 // 1 1:2
224 QCBORDecode_GetNext(&DC, &Item);
225 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530226 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800229 // 1 1:2 2:1
230 QCBORDecode_GetNext(&DC, &Item);
231 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530232 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800235 // 2 1:1
236 QCBORDecode_GetNext(&DC, &Item);
237 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530238 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800241 // 1 1:1 2:1
242 QCBORDecode_GetNext(&DC, &Item);
243 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530244 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800247 // 2 1:1 2:1 3:1
248 QCBORDecode_GetNext(&DC, &Item);
249 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530250 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800253 // 3 XXXXXX
254 QCBORDecode_GetNext(&DC, &Item);
255 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530256 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800259 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800263 return 0;
264}
265
266
267
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530268static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700269 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530270 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
271 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
272 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
273 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
274 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
275 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
276 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
277 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700278 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530279 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
280 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
281 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
282 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
283 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
284 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
285 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
286 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
287 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
288 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
289 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
290 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
291 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
292 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
293 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
294 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
295 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
296 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
297 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
298 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
299 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
300 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
301 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
302 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
303 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
304 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
305 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
306 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
307 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
308 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
309 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
310 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
311 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
312 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
313 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
314 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
315 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
316 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
317 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
318 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
319 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
320 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
321 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
322 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
323 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
324 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
325 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
326 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
327 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
328 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
329 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
330 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
331 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
332 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
333 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
334 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
335 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
336 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
337 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
338 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
339 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
340 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
341 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
342 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
343 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
344 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
345 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
346 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
347 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
348 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
349 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
350 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
351 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
352 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
353 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
354 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
355 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
356 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
357 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
358 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
359 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
360 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
361 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
362 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
363 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
364 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
365 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
366 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
367 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
368 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
369 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
370 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
371 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
372 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
373 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
374 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
375 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
376 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
377 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
378 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
379 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
380 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
381 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
382 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
383 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
384 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
385 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
386 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
387 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
388 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
389 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
390 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
391 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
392 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
393 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
394 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
395 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
396 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
397 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
398 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
399 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
400 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
401 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
402 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
403 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
404 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
405 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
406 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
407 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
408 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
409 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
410 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
411 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
412 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
413 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
414 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
415 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
416 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
417 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
418 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
419 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
420 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
421 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
422 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
423 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
424 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
425 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
426 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
427 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
428 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
429 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
430 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
431 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
432 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
433 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
434 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
435 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
436 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
437 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
438 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
439 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
440 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
441 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
442 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
443 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
444 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
445 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
446 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
447 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
448 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
449 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
450 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
451 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
452 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
453 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
454 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
455 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
456 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
457 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
458 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
459 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
460 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
461 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
462 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
463 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
464 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
465 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
466 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
467 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
468 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
469 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
471 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
472 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
473 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
474 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475};
476
477
478static const char *szMIME = "\
479MIME-Version: 1.0\n\
480Content-Type: multipart/mixed;\n\
481boundary=\"XXXXboundary text\"\n\
482\n\
483This is a multipart message in MIME format.\n\
484\n\
485--XXXXboundary text\n\
486Content-Type: text/plain\n\
487\n\
488this is the body text\n\
489\n\
490--XXXXboundary text\n\
491Content-Type: text/plain;\n\
492Content-Disposition: attachment;\n\
493filename=\"test.txt\"\n\
494\n\
495this is the attachment text\n\
496\n\
497--XXXXboundary text--";
498
499
500int AllAddMethodsTest()
501{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800502 // TODO: this test should be broken down into several so it is more managable. Tags and labels could be more sensible
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800503 QCBOREncodeContext ECtx;
504 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530506 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508 QCBOREncode_OpenArray(&ECtx);
509
Laurence Lundblade067035b2018-11-28 17:35:25 -0800510 // Some ints that are tagged and have strings preceeding them (not labels becase it is not a map)
511 QCBOREncode_AddSZString(&ECtx, "UINT62");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700512 QCBOREncode_AddTag(&ECtx, 100);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800513 QCBOREncode_AddUInt64(&ECtx, 89989909);
514 QCBOREncode_AddSZString(&ECtx, "INT64");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700515 QCBOREncode_AddTag(&ECtx, 76);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800516 QCBOREncode_AddInt64(&ECtx, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 QCBOREncode_AddUInt64(&ECtx,0);
518 QCBOREncode_AddInt64(&ECtx, -44);
519
520 // ints that go in maps
521 QCBOREncode_OpenMap(&ECtx);
522 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
523 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
524 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
525 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
526 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800528 // Epoch Date
529 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531 // Epoch date with labels
532 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800533 QCBOREncode_AddDateEpochToMap(&ECtx, "LongLiveDenisRitchie", 1400000000);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
535 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
536 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 // Binary blobs
539 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
540
541 // binary blobs in maps
542 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800543 QCBOREncode_AddSZString(&ECtx, "binbin");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700544 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800545 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
546 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
548 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800549
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530551 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800552 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530553 QCBOREncode_AddURI(&ECtx, UsefulBuf_FROM_SZ_LITERAL("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
554 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
555 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530556 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558 // text blobs in maps
559 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530560 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade067035b2018-11-28 17:35:25 -0800561 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
562 QCBOREncode_AddSZString(&ECtx, "()()()");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700563 QCBOREncode_AddTag(&ECtx, 1000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800564 QCBOREncode_AddSZString(&ECtx, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530565 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
567 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530568 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
569 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
570 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
571 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
572 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
573 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530574 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
575 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 QCBOREncode_CloseMap(&ECtx);
577
578 // Date strings
579 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
580 QCBOREncode_OpenMap(&ECtx);
581 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
582 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
583 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 // true / false ...
586 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
587 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800588 QCBOREncode_AddSZString(&ECtx, "dare");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700589 QCBOREncode_AddTag(&ECtx, 66);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800590 QCBOREncode_AddBool(&ECtx, true);
591 QCBOREncode_AddBoolToMap(&ECtx, "uu", false);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800592 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
593 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 // opening an array
596 QCBOREncode_OpenArray(&ECtx);
597 QCBOREncode_CloseArray(&ECtx);
598
599 // opening arrays in a map
600 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800601 QCBOREncode_AddSZString(&ECtx, "label and tagged empty array");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700602 QCBOREncode_AddTag(&ECtx, 1093);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800603 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 QCBOREncode_CloseArray(&ECtx);
605 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
606 QCBOREncode_CloseArray(&ECtx);
607 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 // opening maps with labels and tagging
612 QCBOREncode_OpenMap(&ECtx);
613 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
614 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800615 QCBOREncode_AddSZString(&ECtx, "in a in a in a");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700616 QCBOREncode_AddTag(&ECtx, 9087);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800617 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 QCBOREncode_CloseMap(&ECtx);
619 QCBOREncode_CloseMap(&ECtx);
620 QCBOREncode_CloseMap(&ECtx);
621 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800622
Laurence Lundblade067035b2018-11-28 17:35:25 -0800623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624 // Extended simple values (these are not standard...)
625 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800626 QCBOREncode_AddSZString(&ECtx, "s1");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700627 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800628 QCBOREncode_AddSimple(&ECtx, 255);
629 QCBOREncode_AddSimpleToMap(&ECtx, "s2", 0);
630 QCBOREncode_AddSZString(&ECtx, "s3");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700631 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800632 QCBOREncode_AddSimple(&ECtx, 33);
633 QCBOREncode_AddInt64(&ECtx, 88378374); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700634 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800635 QCBOREncode_AddSimple(&ECtx, 255);
636 QCBOREncode_AddInt64(&ECtx, 89); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700637 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800638 QCBOREncode_AddSimple(&ECtx, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800641 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530642 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800643 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
645 QCBOREncode_OpenMap(&ECtx);
646 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
647 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
648 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800649
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800650 // Bool
651 QCBOREncode_AddBool(&ECtx, true);
652 QCBOREncode_AddBool(&ECtx, false);
653 QCBOREncode_OpenMap(&ECtx);
654 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
655 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
656 QCBOREncode_CloseMap(&ECtx);
657
658
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530659 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800660 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
662 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
663 QCBOREncode_OpenMap(&ECtx);
664 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
665 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
666 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
667 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
668 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800670 QCBOREncode_CloseArray(&ECtx);
671
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530672 UsefulBufC Enc;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800673
Laurence Lundblade0595e932018-11-02 22:22:47 +0700674 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800675 nReturn = -1;
676 goto Done;
677 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800678
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530679 if(CheckResults(Enc, spExpectedEncodedAll))
680 nReturn = -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800681
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800682Done:
683 return nReturn;
684}
685
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530686/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200687 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530688 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
689 3B 0000000100000000 # negative(4294967296)
690 3A FFFFFFFF # negative(4294967295)
691 3A FFFFFFFE # negative(4294967294)
692 3A FFFFFFFD # negative(4294967293)
693 3A 7FFFFFFF # negative(2147483647)
694 3A 7FFFFFFE # negative(2147483646)
695 3A 00010001 # negative(65537)
696 3A 00010000 # negative(65536)
697 39 FFFF # negative(65535)
698 39 FFFE # negative(65534)
699 39 FFFD # negative(65533)
700 39 0100 # negative(256)
701 38 FF # negative(255)
702 38 FE # negative(254)
703 38 FD # negative(253)
704 38 18 # negative(24)
705 37 # negative(23)
706 36 # negative(22)
707 20 # negative(0)
708 00 # unsigned(0)
709 00 # unsigned(0)
710 01 # unsigned(1)
711 16 # unsigned(22)
712 17 # unsigned(23)
713 18 18 # unsigned(24)
714 18 19 # unsigned(25)
715 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200716 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530717 18 FE # unsigned(254)
718 18 FF # unsigned(255)
719 19 0100 # unsigned(256)
720 19 0101 # unsigned(257)
721 19 FFFE # unsigned(65534)
722 19 FFFF # unsigned(65535)
723 1A 00010000 # unsigned(65536)
724 1A 00010001 # unsigned(65537)
725 1A 00010002 # unsigned(65538)
726 1A 7FFFFFFF # unsigned(2147483647)
727 1A 7FFFFFFF # unsigned(2147483647)
728 1A 80000000 # unsigned(2147483648)
729 1A 80000001 # unsigned(2147483649)
730 1A FFFFFFFE # unsigned(4294967294)
731 1A FFFFFFFF # unsigned(4294967295)
732 1B 0000000100000000 # unsigned(4294967296)
733 1B 0000000100000001 # unsigned(4294967297)
734 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
735 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
736 */
737static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200738 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
740 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
741 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
742 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
743 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
744 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
745 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
746 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
747 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
748 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200749 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
750 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
751 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
752 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
753 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
754 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
755 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
756 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
757 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
758 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
759 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
760 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
761 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762
763/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800764
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800765 Test the generation of integers. This also ends up testing
766 encoding of all the different lengths. It encodes integers
767 of many lengths and values, especially around the boundaries
768 for different types of integers. It compares the output
769 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800770
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800771 */
772int IntegerValuesTest1()
773{
774 QCBOREncodeContext ECtx;
775 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800776
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530777 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 QCBOREncode_OpenArray(&ECtx);
779
780 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
781 QCBOREncode_AddInt64(&ECtx, -4294967297);
782 QCBOREncode_AddInt64(&ECtx, -4294967296);
783 QCBOREncode_AddInt64(&ECtx, -4294967295);
784 QCBOREncode_AddInt64(&ECtx, -4294967294);
785 QCBOREncode_AddInt64(&ECtx, -2147483648);
786 QCBOREncode_AddInt64(&ECtx, -2147483647);
787 QCBOREncode_AddInt64(&ECtx, -65538);
788 QCBOREncode_AddInt64(&ECtx, -65537);
789 QCBOREncode_AddInt64(&ECtx, -65536);
790 QCBOREncode_AddInt64(&ECtx, -65535);
791 QCBOREncode_AddInt64(&ECtx, -65534);
792 QCBOREncode_AddInt64(&ECtx, -257);
793 QCBOREncode_AddInt64(&ECtx, -256);
794 QCBOREncode_AddInt64(&ECtx, -255);
795 QCBOREncode_AddInt64(&ECtx, -254);
796 QCBOREncode_AddInt64(&ECtx, -25);
797 QCBOREncode_AddInt64(&ECtx, -24);
798 QCBOREncode_AddInt64(&ECtx, -23);
799 QCBOREncode_AddInt64(&ECtx, -1);
800 QCBOREncode_AddInt64(&ECtx, 0);
801 QCBOREncode_AddUInt64(&ECtx, 0ULL);
802 QCBOREncode_AddInt64(&ECtx, 1);
803 QCBOREncode_AddInt64(&ECtx, 22);
804 QCBOREncode_AddInt64(&ECtx, 23);
805 QCBOREncode_AddInt64(&ECtx, 24);
806 QCBOREncode_AddInt64(&ECtx, 25);
807 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200808 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800809 QCBOREncode_AddInt64(&ECtx, 254);
810 QCBOREncode_AddInt64(&ECtx, 255);
811 QCBOREncode_AddInt64(&ECtx, 256);
812 QCBOREncode_AddInt64(&ECtx, 257);
813 QCBOREncode_AddInt64(&ECtx, 65534);
814 QCBOREncode_AddInt64(&ECtx, 65535);
815 QCBOREncode_AddInt64(&ECtx, 65536);
816 QCBOREncode_AddInt64(&ECtx, 65537);
817 QCBOREncode_AddInt64(&ECtx, 65538);
818 QCBOREncode_AddInt64(&ECtx, 2147483647);
819 QCBOREncode_AddInt64(&ECtx, 2147483647);
820 QCBOREncode_AddInt64(&ECtx, 2147483648);
821 QCBOREncode_AddInt64(&ECtx, 2147483649);
822 QCBOREncode_AddInt64(&ECtx, 4294967294);
823 QCBOREncode_AddInt64(&ECtx, 4294967295);
824 QCBOREncode_AddInt64(&ECtx, 4294967296);
825 QCBOREncode_AddInt64(&ECtx, 4294967297);
826 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
827 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800830
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530831 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700832 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800833 nReturn = -1;
834 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800835
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530836 if(CheckResults(Enc, spExpectedEncodedInts))
837 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800839 return(nReturn);
840}
841
842
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530843/*
844 85 # array(5)
845 F5 # primitive(21)
846 F4 # primitive(20)
847 F6 # primitive(22)
848 F7 # primitive(23)
849 A1 # map(1)
850 65 # text(5)
851 554E446566 # "UNDef"
852 F7 # primitive(23)
853 */
854static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800855 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
856
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800857int SimpleValuesTest1()
858{
859 QCBOREncodeContext ECtx;
860 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800861
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530862 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800863 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
866 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
867 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
868 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 QCBOREncode_OpenMap(&ECtx);
871
872 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
873 QCBOREncode_CloseMap(&ECtx);
874
875 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800876
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530877 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700878 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800879 nReturn = -1;
880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530882 if(CheckResults(ECBOR, spExpectedEncodedSimple))
883 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 return(nReturn);
886}
887
Jan Jongboom47d86c52019-07-25 08:54:16 +0200888/*
889 9F # array(5)
890 F5 # primitive(21)
891 F4 # primitive(20)
892 F6 # primitive(22)
893 F7 # primitive(23)
894 BF # map(1)
895 65 # text(5)
896 554E446566 # "UNDef"
897 F7 # primitive(23)
898 FF # break
899 FF # break
900 */
901static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
902 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
903
904int SimpleValuesIndefiniteLengthTest1()
905{
906 QCBOREncodeContext ECtx;
907 int nReturn = 0;
908
909 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
910 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
911
912 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
913 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
914 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
915 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
916
917 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
918
919 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
920 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
921
922 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
923
924 UsefulBufC ECBOR;
925 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
926 nReturn = -1;
927 }
928
929 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
930 return -2;
931
932 return(nReturn);
933}
934
Jan Jongboom5d827882019-08-07 12:51:15 +0200935/*
936A5 # map(5)
937 63 # text(3)
938 617272 # "arr"
939 98 1F # array(31)
940 00 # unsigned(0)
941 01 # unsigned(1)
942 02 # unsigned(2)
943 03 # unsigned(3)
944 04 # unsigned(4)
945 05 # unsigned(5)
946 06 # unsigned(6)
947 07 # unsigned(7)
948 08 # unsigned(8)
949 09 # unsigned(9)
950 0A # unsigned(10)
951 0B # unsigned(11)
952 0C # unsigned(12)
953 0D # unsigned(13)
954 0E # unsigned(14)
955 0F # unsigned(15)
956 10 # unsigned(16)
957 11 # unsigned(17)
958 12 # unsigned(18)
959 13 # unsigned(19)
960 14 # unsigned(20)
961 15 # unsigned(21)
962 16 # unsigned(22)
963 17 # unsigned(23)
964 18 18 # unsigned(24)
965 18 19 # unsigned(25)
966 18 1A # unsigned(26)
967 18 1B # unsigned(27)
968 18 1C # unsigned(28)
969 18 1D # unsigned(29)
970 18 1E # unsigned(30)
971 63 # text(3)
972 6D6170 # "map"
973 B8 1F # map(31)
974 61 # text(1)
975 61 # "a"
976 00 # unsigned(0)
977 61 # text(1)
978 62 # "b"
979 01 # unsigned(1)
980 61 # text(1)
981 63 # "c"
982 02 # unsigned(2)
983 61 # text(1)
984 64 # "d"
985 03 # unsigned(3)
986 61 # text(1)
987 65 # "e"
988 04 # unsigned(4)
989 61 # text(1)
990 66 # "f"
991 05 # unsigned(5)
992 61 # text(1)
993 67 # "g"
994 06 # unsigned(6)
995 61 # text(1)
996 68 # "h"
997 07 # unsigned(7)
998 61 # text(1)
999 69 # "i"
1000 08 # unsigned(8)
1001 61 # text(1)
1002 6A # "j"
1003 09 # unsigned(9)
1004 61 # text(1)
1005 6B # "k"
1006 0A # unsigned(10)
1007 61 # text(1)
1008 6C # "l"
1009 0B # unsigned(11)
1010 61 # text(1)
1011 6D # "m"
1012 0C # unsigned(12)
1013 61 # text(1)
1014 6E # "n"
1015 0D # unsigned(13)
1016 61 # text(1)
1017 6F # "o"
1018 0E # unsigned(14)
1019 61 # text(1)
1020 70 # "p"
1021 0F # unsigned(15)
1022 61 # text(1)
1023 71 # "q"
1024 10 # unsigned(16)
1025 61 # text(1)
1026 72 # "r"
1027 11 # unsigned(17)
1028 61 # text(1)
1029 73 # "s"
1030 12 # unsigned(18)
1031 61 # text(1)
1032 74 # "t"
1033 13 # unsigned(19)
1034 61 # text(1)
1035 75 # "u"
1036 14 # unsigned(20)
1037 61 # text(1)
1038 76 # "v"
1039 15 # unsigned(21)
1040 61 # text(1)
1041 77 # "w"
1042 16 # unsigned(22)
1043 61 # text(1)
1044 78 # "x"
1045 17 # unsigned(23)
1046 61 # text(1)
1047 79 # "y"
1048 18 18 # unsigned(24)
1049 61 # text(1)
1050 7A # "z"
1051 18 19 # unsigned(25)
1052 61 # text(1)
1053 41 # "A"
1054 18 1A # unsigned(26)
1055 61 # text(1)
1056 42 # "B"
1057 18 1B # unsigned(27)
1058 61 # text(1)
1059 43 # "C"
1060 18 1C # unsigned(28)
1061 61 # text(1)
1062 44 # "D"
1063 18 1D # unsigned(29)
1064 61 # text(1)
1065 45 # "E"
1066 18 1E # unsigned(30)
1067 65 # text(5)
1068 6D696E3331 # "min31"
1069 38 1E # negative(30)
1070 66 # text(6)
1071 706C75733331 # "plus31"
1072 18 1F # unsigned(31)
1073 63 # text(3)
1074 737472 # "str"
1075 78 1F # text(31)
1076 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1077 */
1078static const uint8_t EncodeLengthThirtyone[] = {
1079 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1080 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1081 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1082 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1083 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1084 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1085 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1086 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1087 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1088 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1089 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1090 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1091 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1092 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1093 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1094 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1095 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1096 0x31
1097};
1098
1099int EncodeLengthThirtyoneTest()
1100{
1101 QCBOREncodeContext ECtx;
1102 int nReturn = 0;
1103
1104 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1105 QCBOREncode_OpenMap(&ECtx);
1106
1107 // add array with 31 items
1108 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1109 for (size_t ix = 0; ix < 31; ix++) {
1110 QCBOREncode_AddInt64(&ECtx, ix);
1111 }
1112 QCBOREncode_CloseArray(&ECtx);
1113
1114 // add map with 31 items
1115 QCBOREncode_OpenMapInMap(&ECtx, "map");
1116 for (size_t ix = 0; ix < 31; ix++) {
1117 // make sure we have unique keys in the map (a-z then follow by A-Z)
1118 char c = 'a';
1119 if (ix < 26) c = c + ix;
1120 else c = 'A' + (ix - 26);
1121 char buffer[2] = { c, 0 };
1122 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1123 }
1124 QCBOREncode_CloseMap(&ECtx);
1125
1126 // add -31 and +31
1127 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1128 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1129
1130 // add string with length 31
1131 const char *str = "testtesttesttesttesttestqcbor11";
1132 UsefulBufC str_b = { str, 31 };
1133 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1134
1135 QCBOREncode_CloseMap(&ECtx);
1136
1137 UsefulBufC ECBOR;
1138 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1139 nReturn = -1;
1140 }
1141
1142 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1143 return -2;
1144
1145 return(nReturn);
1146}
1147
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301148
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301149/*
1150 83 # array(3)
1151 C0 # tag(0)
1152 74 # text(20)
1153 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
1154 C1 # tag(1)
1155 1A 514B67B0 # unsigned(1363896240)
1156 A2 # map(2)
1157 78 19 # text(25)
1158 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
1159 C0 # tag(0)
1160 77 # text(23)
1161 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
1162 62 # text(2)
1163 5344 # "SD"
1164 C1 # tag(1)
1165 19 03E7 # unsigned(999)
1166 */
1167static const uint8_t spExpectedEncodedDates[] = {
1168 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
1169 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
1170 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
1171 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
1172 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
1173 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
1174 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
1175 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
1176 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001177};
1178
1179int EncodeDateTest()
1180{
1181 QCBOREncodeContext ECtx;
1182 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001183
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301184 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186 QCBOREncode_OpenArray(&ECtx);
1187
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
1190 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
1191
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001192
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001193 QCBOREncode_OpenMap(&ECtx);
1194
1195 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 QCBOREncode_CloseArray(&ECtx);
1202
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301203 UsefulBufC ECBOR;
1204
Laurence Lundblade0595e932018-11-02 22:22:47 +07001205 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 nReturn = -1;
1207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301209 if(CheckResults(ECBOR, spExpectedEncodedDates))
1210 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213}
1214
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216int ArrayNestingTest1()
1217{
1218 QCBOREncodeContext ECtx;
1219 int i;
1220 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001221
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301222 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1224 QCBOREncode_OpenArray(&ECtx);
1225 }
1226 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1227 QCBOREncode_CloseArray(&ECtx);
1228 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001229 UsefulBufC Encoded;
1230 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001231 nReturn = -1;
1232 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001233
1234 return(nReturn);
1235}
1236
1237
1238
1239int ArrayNestingTest2()
1240{
1241 QCBOREncodeContext ECtx;
1242 int i;
1243 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001244
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301245 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001246 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1247 QCBOREncode_OpenArray(&ECtx);
1248 }
1249 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1250 QCBOREncode_CloseArray(&ECtx);
1251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
Laurence Lundblade0595e932018-11-02 22:22:47 +07001253 UsefulBufC Encoded;
1254 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255 nReturn = -1;
1256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 return(nReturn);
1259}
1260
1261
1262
1263int ArrayNestingTest3()
1264{
1265 QCBOREncodeContext ECtx;
1266 int i;
1267 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001268
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301269 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001270 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1271 QCBOREncode_OpenArray(&ECtx);
1272 }
1273 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1274 QCBOREncode_CloseArray(&ECtx);
1275 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001276 UsefulBufC Encoded;
1277 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001278 nReturn = -1;
1279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 return(nReturn);
1282}
1283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301285/*
1286 81 # array(1)
1287 81 # array(1)
1288 81 # array(1)
1289 81 # array(1)
1290 80 # array(0)
1291*/
1292static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001294// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301295/*
1296 82 # array(2)
1297 81 # array(1)
1298 81 # array(1)
1299 81 # array(1)
1300 81 # array(1)
1301 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001302 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301303 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1304 3B 0000000100000000 # negative(4294967296)
1305 3A FFFFFFFF # negative(4294967295)
1306 3A FFFFFFFE # negative(4294967294)
1307 3A FFFFFFFD # negative(4294967293)
1308 3A 7FFFFFFF # negative(2147483647)
1309 3A 7FFFFFFE # negative(2147483646)
1310 3A 00010001 # negative(65537)
1311 3A 00010000 # negative(65536)
1312 39 FFFF # negative(65535)
1313 39 FFFE # negative(65534)
1314 39 FFFD # negative(65533)
1315 39 0100 # negative(256)
1316 38 FF # negative(255)
1317 38 FE # negative(254)
1318 38 FD # negative(253)
1319 38 18 # negative(24)
1320 37 # negative(23)
1321 36 # negative(22)
1322 20 # negative(0)
1323 00 # unsigned(0)
1324 00 # unsigned(0)
1325 01 # unsigned(1)
1326 16 # unsigned(22)
1327 17 # unsigned(23)
1328 18 18 # unsigned(24)
1329 18 19 # unsigned(25)
1330 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001331 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301332 18 FE # unsigned(254)
1333 18 FF # unsigned(255)
1334 19 0100 # unsigned(256)
1335 19 0101 # unsigned(257)
1336 19 FFFE # unsigned(65534)
1337 19 FFFF # unsigned(65535)
1338 1A 00010000 # unsigned(65536)
1339 1A 00010001 # unsigned(65537)
1340 1A 00010002 # unsigned(65538)
1341 1A 7FFFFFFF # unsigned(2147483647)
1342 1A 7FFFFFFF # unsigned(2147483647)
1343 1A 80000000 # unsigned(2147483648)
1344 1A 80000001 # unsigned(2147483649)
1345 1A FFFFFFFE # unsigned(4294967294)
1346 1A FFFFFFFF # unsigned(4294967295)
1347 1B 0000000100000000 # unsigned(4294967296)
1348 1B 0000000100000001 # unsigned(4294967297)
1349 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1350 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1351 */
1352static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001353 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1355 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1356 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1357 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1358 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1359 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1360 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1361 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1362 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1363 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1364 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001365 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1366 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1367 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1368 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1369 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1370 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1371 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1372 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1373 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1374 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1375 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1376 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1377 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378
1379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380int EncodeRawTest()
1381{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001382 QCBOREncodeContext ECtx;
1383
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301384 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001385 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301386 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1387 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001388 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001389
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001390 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade0595e932018-11-02 22:22:47 +07001392 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001395
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301396 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001397 return -5;
1398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001400 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401}
1402
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301403/*
1404 This returns a pointer to spBigBuf
1405 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1407{
1408 QCBOREncodeContext ECtx;
1409 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411 *pEncoded = NULL;
1412 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301413 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 // loop runs CBOR encoding twice. First with no buffer to
1416 // calucate the length so buffer can be allocated correctly,
1417 // and last with the buffer to do the actual encoding
1418 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301419 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 QCBOREncode_OpenMap(&ECtx);
1421 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1422 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1423 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1424 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1425 QCBOREncode_CloseArray(&ECtx);
1426 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1427 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1428 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1429 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1430 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1431 QCBOREncode_CloseMap(&ECtx);
1432 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundblade0595e932018-11-02 22:22:47 +07001434 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001435 goto Done;
1436 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301437 if(uFirstSizeEstimate != *pEncodedLen) {
1438 nReturn = 1;
1439 } else {
1440 nReturn = 0;
1441 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442 goto Done;
1443 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301444 *pEncoded = spBigBuf;
1445 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001448
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 Done:
1450 return(nReturn);
1451}
1452
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301453/*
1454 A3 # map(3)
1455 6D # text(13)
1456 666972737420696E7465676572 # "first integer"
1457 18 2A # unsigned(42)
1458 77 # text(23)
1459 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1460 82 # array(2)
1461 67 # text(7)
1462 737472696E6731 # "string1"
1463 67 # text(7)
1464 737472696E6732 # "string2"
1465 6C # text(12)
1466 6D617020696E2061206D6170 # "map in a map"
1467 A4 # map(4)
1468 67 # text(7)
1469 62797465732031 # "bytes 1"
1470 44 # bytes(4)
1471 78787878 # "xxxx"
1472 67 # text(7)
1473 62797465732032 # "bytes 2"
1474 44 # bytes(4)
1475 79797979 # "yyyy"
1476 6B # text(11)
1477 616E6F7468657220696E74 # "another int"
1478 18 62 # unsigned(98)
1479 66 # text(6)
1480 746578742032 # "text 2"
1481 78 1E # text(30)
1482 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1483 */
1484static const uint8_t spValidMapEncoded[] = {
1485 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1486 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1487 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1488 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1489 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1490 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1491 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1492 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1493 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1494 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1495 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1496 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1497 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1498 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1499 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1500 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501
1502
1503int MapEncodeTest()
1504{
1505 uint8_t *pEncodedMaps;
1506 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301509 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001511
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301513 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1514 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 return(nReturn);
1517}
1518
1519
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520/*
1521 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001522
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1524 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1525 @param[in] szAlexString Diagnostic code.
1526 @param[in[ pOut Buffer to put the result in
1527 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 @return
1530 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532 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 -08001533 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001534 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1537 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001538
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539 */
1540
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301541static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001542{
1543 // Buffer that the result will be written in to
1544 // It is fixed size and small that a stack variable will be fine
1545 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 // Context for the encoder
1548 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301549 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1552 // Contents are label / value pairs
1553 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001554
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1558 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1559
1560 // Add the diagnostic code
1561 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1562
1563 // Add a time stamp
1564 if(time) {
1565 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1566 }
1567
1568 // Add the diagnostic code
1569 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1570
1571 // Open a subordinate map for telemtry data
1572 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1573
1574 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 // Add a few fake integers and buffers for now.
1577 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1578
1579 // Add a few fake integers and buffers for now.
1580 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1581
1582 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301583 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001584 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1587 }
1588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 // Close the telemetry map
1591 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001592
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 // Close the map
1594 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001595
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301596 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001597
Laurence Lundblade0595e932018-11-02 22:22:47 +07001598 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001599
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301600 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601}
1602
1603
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301604/*
1605 A5 # map(5)
1606 69 # text(9)
1607 696E74656772697479 # "integrity"
1608 F4 # primitive(20)
1609 64 # text(4)
1610 74797065 # "type"
1611 66 # text(6)
1612 726563656E74 # "recent"
1613 64 # text(4)
1614 74696D65 # "time"
1615 C1 # tag(1)
1616 1A 580D4172 # unsigned(1477263730)
1617 64 # text(4)
1618 64696167 # "diag"
1619 6A # text(10)
1620 30784131654335303031 # "0xA1eC5001"
1621 69 # text(9)
1622 74656C656D65747279 # "telemetry"
1623 A3 # map(3)
1624 69 # text(9)
1625 53686F652053697A65 # "Shoe Size"
1626 0C # unsigned(12)
1627 62 # text(2)
1628 4951 # "IQ"
1629 1A FFFFFFFF # unsigned(4294967295)
1630 77 # text(23)
1631 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1632 48 # bytes(8)
1633 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1634 */
1635static const uint8_t spExpectedRTIC[] = {
1636 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1637 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1638 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1639 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1640 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1641 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1642 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1643 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1644 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1645 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1646 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1647 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001648
1649
1650int RTICResultsTest()
1651{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001652 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301653 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301654 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301655 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001656 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001658
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301659 if(CheckResults(Encoded, spExpectedRTIC)) {
1660 return -2;
1661 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
1663 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001664}
1665
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301666
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301667/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001668 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001669
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301670 82 # array(2)
1671 19 01C3 # unsigned(451)
1672 43 # bytes(3)
1673 1901D2 # "\x19\x01\xD2"
1674*/
1675static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301676
Laurence Lundblade684aec22018-10-12 19:33:53 +08001677/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001678 81 #array(1)
1679 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1680 */
1681static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1682
1683/*
Laurence Lundblade684aec22018-10-12 19:33:53 +08001684 Very basic bstr wrapping test
1685 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301686int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001688 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001690 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301691 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001692
Laurence Lundblade684aec22018-10-12 19:33:53 +08001693 QCBOREncode_OpenArray(&EC);
1694 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001695
Laurence Lundblade684aec22018-10-12 19:33:53 +08001696 QCBOREncode_BstrWrap(&EC);
1697 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001698
Laurence Lundblade684aec22018-10-12 19:33:53 +08001699 UsefulBufC Wrapped;
1700 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001701
Laurence Lundblade684aec22018-10-12 19:33:53 +08001702 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001703
Laurence Lundblade684aec22018-10-12 19:33:53 +08001704 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001705 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001706 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001708
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301709 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001710 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001711 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001712
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001713 // Second test - see if the length of the wrapped
1714 // bstr is correct. Also tests bstr wrapping
1715 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001716 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1717 QCBOREncode_OpenArray(&EC);
1718 QCBOREncode_BstrWrap(&EC);
1719 QCBOREncode_OpenArray(&EC);
1720 QCBOREncode_AddNULL(&EC);
1721 QCBOREncode_CloseArray(&EC);
1722 UsefulBufC BStr;
1723 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001724 // 3 is one byte for the wrapping bstr, 1 for an array of length 1, and 1 byte for a NULL
1725 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001726 return -5;
1727 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001728
Laurence Lundbladeda532272019-04-07 11:40:17 -07001729 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1730 // bstr wrapping use cases.
1731 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1732 QCBOREncode_Init(&EC, StuffBuf);
1733 QCBOREncode_OpenArray(&EC);
1734 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1735 QCBOREncode_CloseArray(&EC);
1736 if(QCBOREncode_Finish(&EC, &Encoded)) {
1737 return -6;
1738 }
1739 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1740 return -7;
1741 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001742
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001743 return 0;
1744}
1745
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001746
1747
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301748int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001749{
1750 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001751 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001752
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301753 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001754
Laurence Lundblade684aec22018-10-12 19:33:53 +08001755 QCBOREncode_OpenArray(&EC);
1756 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001757
Laurence Lundblade684aec22018-10-12 19:33:53 +08001758 QCBOREncode_BstrWrap(&EC);
1759 QCBOREncode_AddUInt64(&EC, 466);
1760 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001761
Laurence Lundblade684aec22018-10-12 19:33:53 +08001762 UsefulBufC Wrapped;
1763 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001764
Laurence Lundblade684aec22018-10-12 19:33:53 +08001765 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001766
Laurence Lundblade684aec22018-10-12 19:33:53 +08001767 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001768 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001769 return -1;
1770 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001771
Laurence Lundblade684aec22018-10-12 19:33:53 +08001772 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301773 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001774 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade0595e932018-11-02 22:22:47 +07001775 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001776 return -2;
1777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001778
Laurence Lundblade684aec22018-10-12 19:33:53 +08001779 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301780 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001781 for(int i = 1; i < 18; i++) {
1782 QCBOREncode_BstrWrap(&EC);
1783 }
1784 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001785
Laurence Lundblade684aec22018-10-12 19:33:53 +08001786 for(int i = 1; i < 18; i++) {
1787 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1788 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001789
Laurence Lundblade0595e932018-11-02 22:22:47 +07001790 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001791 return -3;
1792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001793
Laurence Lundblade684aec22018-10-12 19:33:53 +08001794 return 0;
1795}
1796
1797
1798
1799// Part of bstr_wrap_nest_test
1800/*
1801 83 array with three
1802 53 byte string with 19 bytes
1803 01 #1
1804 50 byte string with 16 bytes
1805 02
1806 4D byte string with 13 bytes
1807 03
1808 4A byte string with 10 bytes
1809 04
1810 47 byte string with 7 bytes
1811 05
1812 44 byte string with 4 bytes
1813 06
1814 41 byte string with 1 byte
1815 07
1816 01
1817 02
1818 03
1819 04
1820 05
1821 06
1822 07
1823 A2 map with two items
1824 18 20 label for byte string
1825 54 byte string of length 20
1826 82 Array with two items
1827 10 The integer value 10
1828 A2 map with two items
1829 18 21 label for byte string
1830 44 byte string with 4 bytes
1831 81 array with 1 item
1832 11 integer value 11
1833 18 30 integer value 30
1834 18 40 integer label 40
1835 65 68 65 6C 6C 6F text string hello
1836 18 31 integer value 31
1837 18 41 integer label 41
1838 65 68 65 6C 6C 6F text string hello
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001839
1840
Laurence Lundblade684aec22018-10-12 19:33:53 +08001841 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301842
1843
1844/*
1845 83 # array(3)
1846 56 # bytes(22)
1847 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1848 07 # unsigned(7)
1849 A2 # map(2)
1850 18 20 # unsigned(32)
1851 54 # bytes(20)
1852 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1853 18 41 # unsigned(65)
1854 65 # text(5)
1855 68656C6C6F # "hello"
1856 */
1857static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001858{
1859 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1860 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1861 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1862 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1863 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1864 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1865 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1866 0x6F
1867};
1868
1869// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301870static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001871{
1872 int nReturn;
1873 QCBORDecodeContext DC;
1874 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001875
Laurence Lundblade684aec22018-10-12 19:33:53 +08001876 QCBORItem Item;
1877 nReturn = QCBORDecode_GetNext(&DC, &Item);
1878 if(nReturn) {
1879 return -11;
1880 }
1881 if(Item.uDataType != QCBOR_TYPE_INT64) {
1882 return -12;
1883 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001884
Laurence Lundblade684aec22018-10-12 19:33:53 +08001885 nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001886 if(nReturn == QCBOR_ERR_HIT_END || nReturn == QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001887 return 0;
1888 }
1889 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1890 return -13;
1891 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301892 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001893 if(nReturn) {
1894 return nReturn;
1895 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001896
Laurence Lundblade684aec22018-10-12 19:33:53 +08001897 nReturn = QCBORDecode_GetNext(&DC, &Item);
1898 if(nReturn) {
1899 return -14;
1900 }
1901 if(Item.uDataType != QCBOR_TYPE_INT64) {
1902 return -15;
1903 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001904
Laurence Lundblade684aec22018-10-12 19:33:53 +08001905 if(QCBORDecode_Finish(&DC)) {
1906 return -16;
1907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001908
Laurence Lundblade684aec22018-10-12 19:33:53 +08001909 return 0;
1910}
1911
1912// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301913static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001914{
1915 int nReturn;
1916 QCBORDecodeContext DC;
1917 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001918
Laurence Lundblade684aec22018-10-12 19:33:53 +08001919 QCBORItem Item;
1920 nReturn = QCBORDecode_GetNext(&DC, &Item);
1921 if(nReturn) {
1922 return -11;
1923 }
1924 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1925 return -12;
1926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001927
Laurence Lundblade684aec22018-10-12 19:33:53 +08001928 nReturn = QCBORDecode_GetNext(&DC, &Item);
1929 if(nReturn) {
1930 return -11;
1931 }
1932 if(Item.uDataType != QCBOR_TYPE_INT64) {
1933 return -12;
1934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001935
Laurence Lundblade684aec22018-10-12 19:33:53 +08001936 nReturn = QCBORDecode_GetNext(&DC, &Item);
1937 if(nReturn) {
1938 return -11;
1939 }
1940 if(Item.uDataType != QCBOR_TYPE_MAP) {
1941 return 0;
1942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundblade684aec22018-10-12 19:33:53 +08001944 nReturn = QCBORDecode_GetNext(&DC, &Item);
1945 if(nReturn) {
1946 return -11;
1947 }
1948 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1949 return -13;
1950 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301951 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001952 if(nReturn) {
1953 return nReturn;
1954 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundblade684aec22018-10-12 19:33:53 +08001956 nReturn = QCBORDecode_GetNext(&DC, &Item);
1957 if(nReturn) {
1958 return -11;
1959 }
1960 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1961 return -12;
1962 }
1963 nReturn = QCBORDecode_GetNext(&DC, &Item);
1964 if(nReturn) {
1965 return -11;
1966 }
1967 if(Item.uDataType != QCBOR_TYPE_INT64) {
1968 return -12;
1969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001970
Laurence Lundblade684aec22018-10-12 19:33:53 +08001971 if(QCBORDecode_Finish(&DC)) {
1972 return -16;
1973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001974
Laurence Lundblade684aec22018-10-12 19:33:53 +08001975 return 0;
1976}
1977
1978
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301979int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001980{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001981 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301982 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001983
Laurence Lundblade684aec22018-10-12 19:33:53 +08001984 // ---- Make a complicated nested CBOR structure ---
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001985#define BSTR_TEST_DEPTH 10
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001986
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001987 QCBOREncode_OpenArray(&EC);
1988
1989 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001990 QCBOREncode_BstrWrap(&EC);
1991 QCBOREncode_AddUInt64(&EC, i);
1992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001993
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001994 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001995 QCBOREncode_CloseBstrWrap(&EC, NULL);
1996 QCBOREncode_AddUInt64(&EC, i);
1997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001998
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001999 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002000 QCBOREncode_OpenMap(&EC);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002001 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002002 QCBOREncode_OpenArray(&EC);
2003 QCBOREncode_AddUInt64(&EC, i+0x10);
2004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002006 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002007 QCBOREncode_CloseArray(&EC);
2008 QCBOREncode_AddUInt64(&EC, i+0x30);
2009 QCBOREncode_CloseBstrWrap(&EC, NULL);
2010 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
2011 QCBOREncode_CloseMap(&EC);
2012 }
2013 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002014
Laurence Lundblade684aec22018-10-12 19:33:53 +08002015 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002016 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002017 return -1;
2018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundblade684aec22018-10-12 19:33:53 +08002020 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302021 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002022 return -25;
2023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002024
2025
Laurence Lundblade684aec22018-10-12 19:33:53 +08002026 // ---- Decode it and see if it is OK ------
2027 QCBORDecodeContext DC;
2028 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002029
Laurence Lundblade684aec22018-10-12 19:33:53 +08002030 QCBORItem Item;
2031 QCBORDecode_GetNext(&DC, &Item);
2032 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
2033 return -2;
2034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundblade684aec22018-10-12 19:33:53 +08002036 QCBORDecode_GetNext(&DC, &Item);
2037 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2038 return -3;
2039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002040
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302041 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002042 if(nReturn) {
2043 return nReturn;
2044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002045
Laurence Lundblade684aec22018-10-12 19:33:53 +08002046 nReturn = QCBORDecode_GetNext(&DC, &Item);
2047 if(nReturn) {
2048 return -11;
2049 }
2050 if(Item.uDataType != QCBOR_TYPE_INT64) {
2051 return -12;
2052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002053
Laurence Lundblade684aec22018-10-12 19:33:53 +08002054 QCBORDecode_GetNext(&DC, &Item);
2055 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
2056 return -2;
2057 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002058
Laurence Lundblade684aec22018-10-12 19:33:53 +08002059 QCBORDecode_GetNext(&DC, &Item);
2060 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2061 return -3;
2062 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302063 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002064 if(nReturn) {
2065 return nReturn;
2066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002067
Laurence Lundblade684aec22018-10-12 19:33:53 +08002068 nReturn = QCBORDecode_GetNext(&DC, &Item);
2069 if(nReturn) {
2070 return -11;
2071 }
2072 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
2073 return -12;
2074 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002075
Laurence Lundblade684aec22018-10-12 19:33:53 +08002076 if(QCBORDecode_Finish(&DC)) {
2077 return -16;
2078 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002079
Laurence Lundblade684aec22018-10-12 19:33:53 +08002080 return 0;
2081}
2082
2083
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002084static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302085 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2086 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2087 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2088 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2089 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2090 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2091 0x45, 0xca, 0xcb, 0x36};
2092
2093/*
2094 D2 # tag(18)
2095 84 # array(4)
2096 43 # bytes(3)
2097 A10126 # "\xA1\x01&"
2098 A1 # map(1)
2099 04 # unsigned(4)
2100 42 # bytes(2)
2101 3131 # "11"
2102 54 # bytes(20)
2103 546869732069732074686520636F6E74656E742E # "This is the content."
2104 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002105 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2106 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2107 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2108 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2109 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2110 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2111 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2112 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302113 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002114static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302115 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2116 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2117 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2118 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2119 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2120 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2121 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2122 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2123 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2124 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2125
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002126static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2127
2128
Laurence Lundblade684aec22018-10-12 19:33:53 +08002129/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002130 This corresponds exactly to the example in RFC 8152 section
2131 C.2.1. This doesn't actually verify the signature though that would
2132 be nice as it would make the test really good. That would require
2133 bring in ECDSA crypto to this test.
Laurence Lundblade684aec22018-10-12 19:33:53 +08002134 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302135int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08002136{
2137 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002138 const char *szKid = "11";
2139 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2140 const char *szPayload = "This is the content.";
2141 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2142 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2143 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002144
Laurence Lundblade684aec22018-10-12 19:33:53 +08002145 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302146 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002147
Laurence Lundblade684aec22018-10-12 19:33:53 +08002148 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002149 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002150 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundblade684aec22018-10-12 19:33:53 +08002152 // Add protected headers
2153 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002154
Laurence Lundblade684aec22018-10-12 19:33:53 +08002155 // Empty map with unprotected headers
2156 QCBOREncode_OpenMap(&EC);
2157 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2158 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002159
Laurence Lundblade684aec22018-10-12 19:33:53 +08002160 // The payload
2161 UsefulBufC WrappedPayload;
2162 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002163 // Payload is not actually CBOR in example C.2.1 like it would be
2164 // for a CWT or EAT. It is just a text string.
2165 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002166 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundblade684aec22018-10-12 19:33:53 +08002168 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002169 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2170 if(UsefulBuf_Compare(WrappedPayload,
2171 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002172 return -1;
2173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002174
Laurence Lundblade684aec22018-10-12 19:33:53 +08002175 // The signature
2176 QCBOREncode_AddBytes(&EC, Signature);
2177 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002178
Laurence Lundblade684aec22018-10-12 19:33:53 +08002179 // Finish and check the results
2180 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002181 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002182 return -2;
2183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002184
Laurence Lundblade684aec22018-10-12 19:33:53 +08002185 // 98 is the size from RFC 8152 C.2.1
2186 if(COSE_Sign1.len != 98) {
2187 return -3;
2188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002189
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002190 // It would be good to compare this to the output from a COSE
2191 // implementation like COSE-C. This has been checked against the
2192 // CBOR playground.
2193 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002194 return -4;
2195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002196
Laurence Lundblade684aec22018-10-12 19:33:53 +08002197 return 0;
2198}
2199
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002200
2201int EncodeErrorTests()
2202{
2203 QCBOREncodeContext EC;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002204
2205
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002206 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002207 // Do all of these tests with NULL buffers so no actual large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002208 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002209
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002210 // First verify no error from a big buffer
2211 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002212 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002213 // 6 is the CBOR overhead for opening the array and encodng the length
2214 // This exactly fills the buffer.
2215 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002216 QCBOREncode_CloseArray(&EC);
2217 size_t xx;
2218 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2219 return -1;
2220 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002221
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002222 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002223 // Also test fetching the error code before finish
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002224 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002225 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002226 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
2227 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002228 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2229 // Error fetch failed.
2230 return -12;
2231 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002232 QCBOREncode_CloseArray(&EC);
2233 QCBOREncode_CloseArray(&EC);
2234 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2235 return -2;
2236 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002237
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002238 // Third, fit an array in exactly at max position allowed
2239 QCBOREncode_Init(&EC, Buffer);
2240 QCBOREncode_OpenArray(&EC);
2241 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2242 QCBOREncode_OpenArray(&EC);
2243 QCBOREncode_CloseArray(&EC);
2244 QCBOREncode_CloseArray(&EC);
2245 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2246 return -10;
2247 }
2248
2249
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002250 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2251 // Work close to the 4GB size limit for a better test
2252 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002253 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002254
2255 QCBOREncode_Init(&EC, Large);
2256 QCBOREncode_OpenArray(&EC);
2257 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2258 QCBOREncode_CloseArray(&EC);
2259 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2260 // Making sure it succeeds when it should first
2261 return -3;
2262 }
2263
2264 QCBOREncode_Init(&EC, Large);
2265 QCBOREncode_OpenArray(&EC);
2266 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2267 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2268 QCBOREncode_CloseArray(&EC);
2269 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2270 // Now just 1 byte over, see that it fails
2271 return -4;
2272 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002273
2274
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002275 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2276 QCBOREncode_Init(&EC, Large);
2277 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2278 QCBOREncode_OpenArray(&EC);
2279 }
2280 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2281 QCBOREncode_CloseArray(&EC);
2282 }
2283 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2284 // Making sure it succeeds when it should first
2285 return -5;
2286 }
2287
2288 QCBOREncode_Init(&EC, Large);
2289 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2290 QCBOREncode_OpenArray(&EC);
2291 }
2292 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2293 QCBOREncode_CloseArray(&EC);
2294 }
2295 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2296 // One more level to cause error
2297 return -6;
2298 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002299
2300
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002301 // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
2302 QCBOREncode_Init(&EC, Large);
2303 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2304 QCBOREncode_OpenArray(&EC);
2305 }
2306 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2307 QCBOREncode_CloseArray(&EC);
2308 }
2309 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
2310 // One more level to cause error
2311 return -7;
2312 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002313
2314
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002315 // ------ QCBOR_ERR_CLOSE_MISMATCH --------
2316 QCBOREncode_Init(&EC, Large);
2317 QCBOREncode_OpenArray(&EC);
2318 UsefulBufC Wrap;
2319 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
2320 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
2321 return -8;
2322 }
2323
2324
2325 // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
2326 QCBOREncode_Init(&EC, Large);
2327 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2328 QCBOREncode_OpenArray(&EC);
2329 }
2330 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2331 QCBOREncode_CloseArray(&EC);
2332 }
2333 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2334 // One more level to cause error
2335 return -9;
2336 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002337
Laurence Lundblade241705e2018-12-30 18:56:14 -08002338 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2339 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002340
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002341
2342 // ----- Test the check for NULL buffer ------
2343 QCBOREncode_Init(&EC, Buffer);
2344 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2345 return -11;
2346 }
2347
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002348 // ------ QCBOR_ERR_UNSUPPORTED --------
2349 QCBOREncode_Init(&EC, Large);
2350 QCBOREncode_OpenArray(&EC);
2351 QCBOREncode_AddSimple(&EC, 24); // CBOR_SIMPLEV_RESERVED_START
2352 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2353 return -12;
2354 }
2355
2356 QCBOREncode_Init(&EC, Large);
2357 QCBOREncode_OpenArray(&EC);
2358 QCBOREncode_AddSimple(&EC, 31); // CBOR_SIMPLEV_RESERVED_END
2359 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_UNSUPPORTED) {
2360 return -13;
2361 }
2362
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002363 return 0;
2364}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002365
2366
2367#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2368/*
2369 [
2370 4([-1, 3]),
2371 4([-20, 4759477275222530853136]),
2372 4([9223372036854775807, -4759477275222530853137]),
2373 5([300, 100]),
2374 5([-20, 4759477275222530853136]),
2375 5([-9223372036854775808, -4759477275222530853137])
2376 ]
2377 */
2378static const uint8_t spExpectedExponentAndMantissaArray[] = {
2379 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2380 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2381 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2382 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2383 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2384 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2385 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2386 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2387 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2388 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2389 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2390
2391
2392/*
2393 {
2394 "decimal fraction": 4([-1, 3]),
2395 300: 4([-1, 3]),
2396 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2397 400: 4([2147483647, 4759477275222530853136]),
2398 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2399 500: 4([9223372036854775807, -4759477275222530853137]),
2400 "big float": 5([300, 100]),
2401 600: 5([300, 100]),
2402 "big float bignum positive": 5([-20, 4759477275222530853136]),
2403 700: 5([-20, 4759477275222530853136]),
2404 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2405 800: 5([-9223372036854775808, -4759477275222530853137])
2406 }
2407 */
2408static const uint8_t spExpectedExponentAndMantissaMap[] = {
2409 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2410 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2411 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2412 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2413 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2414 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2415 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2416 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2417 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2418 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2419 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2420 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2421 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2422 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2423 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2424 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2425 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2426 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2427 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2428 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2429 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2430 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2431 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2432 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2433 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2434 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2435 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2436 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2437 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2438 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2439 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2440 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2441 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2442 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2443 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2444 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2445 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2446 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2447 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2448 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2449 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2450 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2451 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2452 0x09, 0x10
2453};
2454
2455
2456int ExponentAndMantissaEncodeTests()
2457{
2458 QCBOREncodeContext EC;
2459 UsefulBufC EncodedExponentAndMantissa;
2460
2461 // Constant for the big number used in all the tests.
2462 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2463 0x06, 0x07, 0x08, 0x09, 0x010};
2464 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2465
2466 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2467 QCBOREncode_OpenArray(&EC);
2468 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2469 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2470 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2471 QCBOREncode_AddBigFloat(&EC, 100, 300);
2472 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2473 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2474 QCBOREncode_CloseArray(&EC);
2475
2476 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2477 return -2;
2478 }
2479
2480 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2481 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2482 NULL);
2483 if(nReturn) {
2484 return nReturn;
2485 }
2486
2487 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2488 QCBOREncode_OpenMap(&EC);
2489
2490 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2491
2492 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2493
2494 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2495 "decimal fraction bignum postive",
2496 BigNum,
2497 false,
2498 -200);
2499
2500 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2501 400,
2502 BigNum,
2503 false,
2504 INT32_MAX);
2505
2506 QCBOREncode_AddDecimalFractionBigNumToMap(&EC,
2507 "decimal fraction bignum negative",
2508 BigNum,
2509 true,
2510 INT64_MAX);
2511
2512 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2513 500,
2514 BigNum,
2515 true,
2516 INT64_MAX);
2517
2518 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2519
2520 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2521
2522 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2523 "big float bignum positive",
2524 BigNum,
2525 false,
2526 -20);
2527
2528 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2529 700,
2530 BigNum,
2531 false,
2532 -20);
2533
2534 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2535 "big float bignum negative",
2536 BigNum,
2537 true,
2538 INT64_MIN);
2539
2540 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2541 800,
2542 BigNum,
2543 true,
2544 INT64_MIN);
2545
2546 QCBOREncode_CloseMap(&EC);
2547
2548 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2549 return -3;
2550 }
2551
2552
2553 struct UBCompareDiagnostic Diag;
2554
2555 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2556 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2557 &Diag);
2558 if(nReturn) {
2559 return nReturn + 1000000; // +1000000 to distinguish from first test above
2560 }
2561
2562 return 0;
2563}
2564
2565#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */