blob: bd7a7ae102e912e042381467875b363609d42c39 [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
93
Laurence Lundblade369b90a2018-10-22 02:04:37 +053094// One big buffer that is used by all the tests to encode into
95// Putting it in uninitialized data is better than using a lot
96// of stack. The tests should run on small devices too.
97static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098
99
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800100
101/*
102 Some very minimal tests.
103 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530104int BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800105{
106 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800107 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800108
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530109 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530110
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800111 QCBOREncode_OpenMap(&EC);
112 QCBOREncode_AddBoolToMapN(&EC, 66, true);
113 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800114
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800115 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700116 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530117 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800119
120
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800121 // Decode it and see that is right
122 QCBORDecodeContext DC;
123 QCBORItem Item;
124 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800125
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800126 QCBORDecode_GetNext(&DC, &Item);
127 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530128 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800131 QCBORDecode_GetNext(&DC, &Item);
132 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530133 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800135
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800136 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530137 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800139
140
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800141 // Make another encoded message with the CBOR from the previous put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530142 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800143 QCBOREncode_Init(&EC, MemoryForEncoded2);
144 QCBOREncode_OpenArray(&EC);
145 QCBOREncode_AddUInt64(&EC, 451);
146 QCBOREncode_AddEncoded(&EC, Encoded);
147 QCBOREncode_OpenMap(&EC);
148 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
149 QCBOREncode_CloseMap(&EC);
150 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
156 /*
157 [ // 0 1:3
158 451, // 1 1:2
159 { // 1 1:2 2:1
160 66: true // 2 1:1
161 },
162 { // 1 1:1 2:1
163 -70000: { // 2 1:1 2:1 3:1
164 66: true // 3 XXXXXX
165 }
166 }
167 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
169
170
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 83 # array(3)
172 19 01C3 # unsigned(451)
173 A1 # map(1)
174 18 42 # unsigned(66)
175 F5 # primitive(21)
176 A1 # map(1)
177 3A 0001116F # negative(69999)
178 A1 # map(1)
179 18 42 # unsigned(66)
180 F5 # primitive(21)
181 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800183 // Decode it and see if it is OK
184 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800185
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800186 // 0 1:3
187 QCBORDecode_GetNext(&DC, &Item);
188 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530189 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800192 // 1 1:2
193 QCBORDecode_GetNext(&DC, &Item);
194 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530195 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800198 // 1 1:2 2:1
199 QCBORDecode_GetNext(&DC, &Item);
200 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530201 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800204 // 2 1:1
205 QCBORDecode_GetNext(&DC, &Item);
206 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530207 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800210 // 1 1:1 2:1
211 QCBORDecode_GetNext(&DC, &Item);
212 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530213 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800216 // 2 1:1 2:1 3:1
217 QCBORDecode_GetNext(&DC, &Item);
218 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 +0530219 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800222 // 3 XXXXXX
223 QCBORDecode_GetNext(&DC, &Item);
224 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530225 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800227
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800228 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530229 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800231
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800232 return 0;
233}
234
235
236
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530237static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700238 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530239 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
240 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
241 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
242 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
243 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
244 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
245 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
246 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700247 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530248 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
249 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
250 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
251 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
252 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
253 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
254 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
255 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
256 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
257 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
258 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
259 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
260 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
261 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
262 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
263 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
264 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
265 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
266 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
267 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
268 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
269 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
270 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
271 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
272 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
273 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
274 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
275 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
276 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
277 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
278 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
279 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
280 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
281 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
282 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
283 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
284 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
285 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
286 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
287 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
288 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
289 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
290 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
291 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
292 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
293 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
294 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
295 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
296 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
297 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
298 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
299 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
300 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
301 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
302 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
303 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
304 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
305 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
306 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
307 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
308 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
309 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
310 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
311 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
312 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
313 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
314 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
315 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
316 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
317 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
318 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
319 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
320 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
321 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
322 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
323 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
324 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
325 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
326 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
327 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
328 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
329 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
330 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
331 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
332 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
333 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
334 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
335 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
336 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
337 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
338 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
339 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
340 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
341 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
342 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
343 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
344 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
345 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
346 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
347 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
348 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
349 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
350 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
351 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
352 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
353 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
354 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
355 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
356 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
357 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
358 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
359 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
360 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
361 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
362 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
363 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
364 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
365 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
366 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
367 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
368 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
369 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
370 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
371 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
372 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
373 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
374 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
375 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
376 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
377 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
378 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
379 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
380 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
381 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
383 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
384 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
385 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
386 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
387 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
388 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
389 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
390 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
391 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
392 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
393 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
394 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
395 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
396 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
397 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
398 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
399 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
400 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
401 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
402 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
403 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
404 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
405 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
406 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
407 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
408 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
409 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
410 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
411 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
412 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
413 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
414 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
415 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
416 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
417 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
418 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
419 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
420 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
421 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
422 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
423 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
424 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
425 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
426 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
427 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
428 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
429 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
430 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
431 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
432 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
433 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
434 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
435 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
436 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
437 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
438 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
440 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
441 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
442 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
443 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800444};
445
446
447static const char *szMIME = "\
448MIME-Version: 1.0\n\
449Content-Type: multipart/mixed;\n\
450boundary=\"XXXXboundary text\"\n\
451\n\
452This is a multipart message in MIME format.\n\
453\n\
454--XXXXboundary text\n\
455Content-Type: text/plain\n\
456\n\
457this is the body text\n\
458\n\
459--XXXXboundary text\n\
460Content-Type: text/plain;\n\
461Content-Disposition: attachment;\n\
462filename=\"test.txt\"\n\
463\n\
464this is the attachment text\n\
465\n\
466--XXXXboundary text--";
467
468
469int AllAddMethodsTest()
470{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800471 // 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 +0800472 QCBOREncodeContext ECtx;
473 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800474
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530475 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 QCBOREncode_OpenArray(&ECtx);
478
Laurence Lundblade067035b2018-11-28 17:35:25 -0800479 // Some ints that are tagged and have strings preceeding them (not labels becase it is not a map)
480 QCBOREncode_AddSZString(&ECtx, "UINT62");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700481 QCBOREncode_AddTag(&ECtx, 100);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800482 QCBOREncode_AddUInt64(&ECtx, 89989909);
483 QCBOREncode_AddSZString(&ECtx, "INT64");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700484 QCBOREncode_AddTag(&ECtx, 76);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800485 QCBOREncode_AddInt64(&ECtx, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 QCBOREncode_AddUInt64(&ECtx,0);
487 QCBOREncode_AddInt64(&ECtx, -44);
488
489 // ints that go in maps
490 QCBOREncode_OpenMap(&ECtx);
491 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
492 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
493 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
494 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
495 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800496
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800497 // Epoch Date
498 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800499
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800500 // Epoch date with labels
501 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800502 QCBOREncode_AddDateEpochToMap(&ECtx, "LongLiveDenisRitchie", 1400000000);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800503 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
504 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
505 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800506
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800507 // Binary blobs
508 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
509
510 // binary blobs in maps
511 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800512 QCBOREncode_AddSZString(&ECtx, "binbin");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700513 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800514 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
515 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800516 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
517 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800519 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530520 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530522 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"));
523 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
524 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530525 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 // text blobs in maps
528 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530529 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade067035b2018-11-28 17:35:25 -0800530 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
531 QCBOREncode_AddSZString(&ECtx, "()()()");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700532 QCBOREncode_AddTag(&ECtx, 1000);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800533 QCBOREncode_AddSZString(&ECtx, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530534 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800535 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
536 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530537 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
538 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
539 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
540 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
541 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
542 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530543 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
544 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800545 QCBOREncode_CloseMap(&ECtx);
546
547 // Date strings
548 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
549 QCBOREncode_OpenMap(&ECtx);
550 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
551 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
552 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800553
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800554 // true / false ...
555 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
556 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800557 QCBOREncode_AddSZString(&ECtx, "dare");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700558 QCBOREncode_AddTag(&ECtx, 66);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800559 QCBOREncode_AddBool(&ECtx, true);
560 QCBOREncode_AddBoolToMap(&ECtx, "uu", false);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800561 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
562 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 // opening an array
565 QCBOREncode_OpenArray(&ECtx);
566 QCBOREncode_CloseArray(&ECtx);
567
568 // opening arrays in a map
569 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800570 QCBOREncode_AddSZString(&ECtx, "label and tagged empty array");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700571 QCBOREncode_AddTag(&ECtx, 1093);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800572 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800573 QCBOREncode_CloseArray(&ECtx);
574 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
575 QCBOREncode_CloseArray(&ECtx);
576 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800577 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // opening maps with labels and tagging
581 QCBOREncode_OpenMap(&ECtx);
582 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
583 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800584 QCBOREncode_AddSZString(&ECtx, "in a in a in a");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700585 QCBOREncode_AddTag(&ECtx, 9087);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800586 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 QCBOREncode_CloseMap(&ECtx);
588 QCBOREncode_CloseMap(&ECtx);
589 QCBOREncode_CloseMap(&ECtx);
590 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800591
Laurence Lundblade067035b2018-11-28 17:35:25 -0800592
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800593 // Extended simple values (these are not standard...)
594 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800595 QCBOREncode_AddSZString(&ECtx, "s1");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700596 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800597 QCBOREncode_AddSimple(&ECtx, 255);
598 QCBOREncode_AddSimpleToMap(&ECtx, "s2", 0);
599 QCBOREncode_AddSZString(&ECtx, "s3");
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700600 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800601 QCBOREncode_AddSimple(&ECtx, 33);
602 QCBOREncode_AddInt64(&ECtx, 88378374); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700603 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800604 QCBOREncode_AddSimple(&ECtx, 255);
605 QCBOREncode_AddInt64(&ECtx, 89); // label before tag
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700606 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800607 QCBOREncode_AddSimple(&ECtx, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800608 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530611 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 -0800612 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800613 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
614 QCBOREncode_OpenMap(&ECtx);
615 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
616 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
617 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800619 // Bool
620 QCBOREncode_AddBool(&ECtx, true);
621 QCBOREncode_AddBool(&ECtx, false);
622 QCBOREncode_OpenMap(&ECtx);
623 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
624 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
625 QCBOREncode_CloseMap(&ECtx);
626
627
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530628 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800629 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
631 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
632 QCBOREncode_OpenMap(&ECtx);
633 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
634 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
635 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
636 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
637 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800638
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 QCBOREncode_CloseArray(&ECtx);
640
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530641 UsefulBufC Enc;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800642
Laurence Lundblade0595e932018-11-02 22:22:47 +0700643 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 nReturn = -1;
645 goto Done;
646 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530648 if(CheckResults(Enc, spExpectedEncodedAll))
649 nReturn = -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800650
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800651Done:
652 return nReturn;
653}
654
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530655/*
656 98 2F # array(47)
657 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
658 3B 0000000100000000 # negative(4294967296)
659 3A FFFFFFFF # negative(4294967295)
660 3A FFFFFFFE # negative(4294967294)
661 3A FFFFFFFD # negative(4294967293)
662 3A 7FFFFFFF # negative(2147483647)
663 3A 7FFFFFFE # negative(2147483646)
664 3A 00010001 # negative(65537)
665 3A 00010000 # negative(65536)
666 39 FFFF # negative(65535)
667 39 FFFE # negative(65534)
668 39 FFFD # negative(65533)
669 39 0100 # negative(256)
670 38 FF # negative(255)
671 38 FE # negative(254)
672 38 FD # negative(253)
673 38 18 # negative(24)
674 37 # negative(23)
675 36 # negative(22)
676 20 # negative(0)
677 00 # unsigned(0)
678 00 # unsigned(0)
679 01 # unsigned(1)
680 16 # unsigned(22)
681 17 # unsigned(23)
682 18 18 # unsigned(24)
683 18 19 # unsigned(25)
684 18 1A # unsigned(26)
685 18 FE # unsigned(254)
686 18 FF # unsigned(255)
687 19 0100 # unsigned(256)
688 19 0101 # unsigned(257)
689 19 FFFE # unsigned(65534)
690 19 FFFF # unsigned(65535)
691 1A 00010000 # unsigned(65536)
692 1A 00010001 # unsigned(65537)
693 1A 00010002 # unsigned(65538)
694 1A 7FFFFFFF # unsigned(2147483647)
695 1A 7FFFFFFF # unsigned(2147483647)
696 1A 80000000 # unsigned(2147483648)
697 1A 80000001 # unsigned(2147483649)
698 1A FFFFFFFE # unsigned(4294967294)
699 1A FFFFFFFF # unsigned(4294967295)
700 1B 0000000100000000 # unsigned(4294967296)
701 1B 0000000100000001 # unsigned(4294967297)
702 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
703 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
704 */
705static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
707 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
708 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
709 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
710 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
711 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
712 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
713 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
714 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
715 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
716 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
717 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
718 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
719 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
720 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
721 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
722 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
723 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
724 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
725 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
726 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
727 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
728 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
729 0xff, 0xff};
730
731/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800732
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 Test the generation of integers. This also ends up testing
734 encoding of all the different lengths. It encodes integers
735 of many lengths and values, especially around the boundaries
736 for different types of integers. It compares the output
737 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800738
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 */
740int IntegerValuesTest1()
741{
742 QCBOREncodeContext ECtx;
743 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800744
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530745 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800746 QCBOREncode_OpenArray(&ECtx);
747
748 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
749 QCBOREncode_AddInt64(&ECtx, -4294967297);
750 QCBOREncode_AddInt64(&ECtx, -4294967296);
751 QCBOREncode_AddInt64(&ECtx, -4294967295);
752 QCBOREncode_AddInt64(&ECtx, -4294967294);
753 QCBOREncode_AddInt64(&ECtx, -2147483648);
754 QCBOREncode_AddInt64(&ECtx, -2147483647);
755 QCBOREncode_AddInt64(&ECtx, -65538);
756 QCBOREncode_AddInt64(&ECtx, -65537);
757 QCBOREncode_AddInt64(&ECtx, -65536);
758 QCBOREncode_AddInt64(&ECtx, -65535);
759 QCBOREncode_AddInt64(&ECtx, -65534);
760 QCBOREncode_AddInt64(&ECtx, -257);
761 QCBOREncode_AddInt64(&ECtx, -256);
762 QCBOREncode_AddInt64(&ECtx, -255);
763 QCBOREncode_AddInt64(&ECtx, -254);
764 QCBOREncode_AddInt64(&ECtx, -25);
765 QCBOREncode_AddInt64(&ECtx, -24);
766 QCBOREncode_AddInt64(&ECtx, -23);
767 QCBOREncode_AddInt64(&ECtx, -1);
768 QCBOREncode_AddInt64(&ECtx, 0);
769 QCBOREncode_AddUInt64(&ECtx, 0ULL);
770 QCBOREncode_AddInt64(&ECtx, 1);
771 QCBOREncode_AddInt64(&ECtx, 22);
772 QCBOREncode_AddInt64(&ECtx, 23);
773 QCBOREncode_AddInt64(&ECtx, 24);
774 QCBOREncode_AddInt64(&ECtx, 25);
775 QCBOREncode_AddInt64(&ECtx, 26);
776 QCBOREncode_AddInt64(&ECtx, 254);
777 QCBOREncode_AddInt64(&ECtx, 255);
778 QCBOREncode_AddInt64(&ECtx, 256);
779 QCBOREncode_AddInt64(&ECtx, 257);
780 QCBOREncode_AddInt64(&ECtx, 65534);
781 QCBOREncode_AddInt64(&ECtx, 65535);
782 QCBOREncode_AddInt64(&ECtx, 65536);
783 QCBOREncode_AddInt64(&ECtx, 65537);
784 QCBOREncode_AddInt64(&ECtx, 65538);
785 QCBOREncode_AddInt64(&ECtx, 2147483647);
786 QCBOREncode_AddInt64(&ECtx, 2147483647);
787 QCBOREncode_AddInt64(&ECtx, 2147483648);
788 QCBOREncode_AddInt64(&ECtx, 2147483649);
789 QCBOREncode_AddInt64(&ECtx, 4294967294);
790 QCBOREncode_AddInt64(&ECtx, 4294967295);
791 QCBOREncode_AddInt64(&ECtx, 4294967296);
792 QCBOREncode_AddInt64(&ECtx, 4294967297);
793 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
794 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800795
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800797
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530798 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700799 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 nReturn = -1;
801 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800802
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530803 if(CheckResults(Enc, spExpectedEncodedInts))
804 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800805
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800806 return(nReturn);
807}
808
809
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530810/*
811 85 # array(5)
812 F5 # primitive(21)
813 F4 # primitive(20)
814 F6 # primitive(22)
815 F7 # primitive(23)
816 A1 # map(1)
817 65 # text(5)
818 554E446566 # "UNDef"
819 F7 # primitive(23)
820 */
821static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800822 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
823
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800824int SimpleValuesTest1()
825{
826 QCBOREncodeContext ECtx;
827 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530829 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800830 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900831
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800832 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
833 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
834 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
835 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800836
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800837 QCBOREncode_OpenMap(&ECtx);
838
839 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
840 QCBOREncode_CloseMap(&ECtx);
841
842 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800843
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530844 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700845 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800846 nReturn = -1;
847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800848
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530849 if(CheckResults(ECBOR, spExpectedEncodedSimple))
850 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800851
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800852 return(nReturn);
853}
854
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530855
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530856/*
857 83 # array(3)
858 C0 # tag(0)
859 74 # text(20)
860 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
861 C1 # tag(1)
862 1A 514B67B0 # unsigned(1363896240)
863 A2 # map(2)
864 78 19 # text(25)
865 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
866 C0 # tag(0)
867 77 # text(23)
868 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
869 62 # text(2)
870 5344 # "SD"
871 C1 # tag(1)
872 19 03E7 # unsigned(999)
873 */
874static const uint8_t spExpectedEncodedDates[] = {
875 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
876 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
877 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
878 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
879 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
880 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
881 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
882 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
883 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800884};
885
886int EncodeDateTest()
887{
888 QCBOREncodeContext ECtx;
889 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800890
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530891 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800892
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800893 QCBOREncode_OpenArray(&ECtx);
894
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800896 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
897 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
898
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800900 QCBOREncode_OpenMap(&ECtx);
901
902 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800903
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800904 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800905
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800906 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800907
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908 QCBOREncode_CloseArray(&ECtx);
909
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530910 UsefulBufC ECBOR;
911
Laurence Lundblade0595e932018-11-02 22:22:47 +0700912 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800913 nReturn = -1;
914 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800915
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530916 if(CheckResults(ECBOR, spExpectedEncodedDates))
917 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800918
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800920}
921
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530922
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800923int ArrayNestingTest1()
924{
925 QCBOREncodeContext ECtx;
926 int i;
927 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530929 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
931 QCBOREncode_OpenArray(&ECtx);
932 }
933 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
934 QCBOREncode_CloseArray(&ECtx);
935 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700936 UsefulBufC Encoded;
937 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 nReturn = -1;
939 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800940
941 return(nReturn);
942}
943
944
945
946int ArrayNestingTest2()
947{
948 QCBOREncodeContext ECtx;
949 int i;
950 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530952 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
954 QCBOREncode_OpenArray(&ECtx);
955 }
956 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
957 QCBOREncode_CloseArray(&ECtx);
958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundblade0595e932018-11-02 22:22:47 +0700960 UsefulBufC Encoded;
961 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800962 nReturn = -1;
963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800964
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 return(nReturn);
966}
967
968
969
970int ArrayNestingTest3()
971{
972 QCBOREncodeContext ECtx;
973 int i;
974 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800975
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530976 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
978 QCBOREncode_OpenArray(&ECtx);
979 }
980 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
981 QCBOREncode_CloseArray(&ECtx);
982 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700983 UsefulBufC Encoded;
984 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800985 nReturn = -1;
986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800987
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800988 return(nReturn);
989}
990
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800991
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530992/*
993 81 # array(1)
994 81 # array(1)
995 81 # array(1)
996 81 # array(1)
997 80 # array(0)
998*/
999static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301002/*
1003 82 # array(2)
1004 81 # array(1)
1005 81 # array(1)
1006 81 # array(1)
1007 81 # array(1)
1008 80 # array(0)
1009 98 2F # array(47)
1010 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1011 3B 0000000100000000 # negative(4294967296)
1012 3A FFFFFFFF # negative(4294967295)
1013 3A FFFFFFFE # negative(4294967294)
1014 3A FFFFFFFD # negative(4294967293)
1015 3A 7FFFFFFF # negative(2147483647)
1016 3A 7FFFFFFE # negative(2147483646)
1017 3A 00010001 # negative(65537)
1018 3A 00010000 # negative(65536)
1019 39 FFFF # negative(65535)
1020 39 FFFE # negative(65534)
1021 39 FFFD # negative(65533)
1022 39 0100 # negative(256)
1023 38 FF # negative(255)
1024 38 FE # negative(254)
1025 38 FD # negative(253)
1026 38 18 # negative(24)
1027 37 # negative(23)
1028 36 # negative(22)
1029 20 # negative(0)
1030 00 # unsigned(0)
1031 00 # unsigned(0)
1032 01 # unsigned(1)
1033 16 # unsigned(22)
1034 17 # unsigned(23)
1035 18 18 # unsigned(24)
1036 18 19 # unsigned(25)
1037 18 1A # unsigned(26)
1038 18 FE # unsigned(254)
1039 18 FF # unsigned(255)
1040 19 0100 # unsigned(256)
1041 19 0101 # unsigned(257)
1042 19 FFFE # unsigned(65534)
1043 19 FFFF # unsigned(65535)
1044 1A 00010000 # unsigned(65536)
1045 1A 00010001 # unsigned(65537)
1046 1A 00010002 # unsigned(65538)
1047 1A 7FFFFFFF # unsigned(2147483647)
1048 1A 7FFFFFFF # unsigned(2147483647)
1049 1A 80000000 # unsigned(2147483648)
1050 1A 80000001 # unsigned(2147483649)
1051 1A FFFFFFFE # unsigned(4294967294)
1052 1A FFFFFFFF # unsigned(4294967295)
1053 1B 0000000100000000 # unsigned(4294967296)
1054 1B 0000000100000001 # unsigned(4294967297)
1055 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1056 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1057 */
1058static const uint8_t spEncodeRawExpected[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1060 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1061 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1062 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1063 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1064 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1065 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1066 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1067 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1068 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1069 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1070 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1071 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1072 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1073 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1074 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1075 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1076 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1077 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1078 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1079 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1080 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1081 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1082 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1083
1084
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085int EncodeRawTest()
1086{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001087 QCBOREncodeContext ECtx;
1088
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301089 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001090 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301091 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1092 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001093 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001094
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001095 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundblade0595e932018-11-02 22:22:47 +07001097 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001098 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001100
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301101 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001102 return -5;
1103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001104
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001105 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001106}
1107
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301108/*
1109 This returns a pointer to spBigBuf
1110 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1112{
1113 QCBOREncodeContext ECtx;
1114 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001115
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 *pEncoded = NULL;
1117 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301118 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001120 // loop runs CBOR encoding twice. First with no buffer to
1121 // calucate the length so buffer can be allocated correctly,
1122 // and last with the buffer to do the actual encoding
1123 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301124 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 QCBOREncode_OpenMap(&ECtx);
1126 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1127 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1128 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1129 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1130 QCBOREncode_CloseArray(&ECtx);
1131 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1132 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1133 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1134 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1135 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1136 QCBOREncode_CloseMap(&ECtx);
1137 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001138
Laurence Lundblade0595e932018-11-02 22:22:47 +07001139 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001140 goto Done;
1141 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301142 if(uFirstSizeEstimate != *pEncodedLen) {
1143 nReturn = 1;
1144 } else {
1145 nReturn = 0;
1146 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 goto Done;
1148 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301149 *pEncoded = spBigBuf;
1150 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001152 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001154 Done:
1155 return(nReturn);
1156}
1157
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301158/*
1159 A3 # map(3)
1160 6D # text(13)
1161 666972737420696E7465676572 # "first integer"
1162 18 2A # unsigned(42)
1163 77 # text(23)
1164 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1165 82 # array(2)
1166 67 # text(7)
1167 737472696E6731 # "string1"
1168 67 # text(7)
1169 737472696E6732 # "string2"
1170 6C # text(12)
1171 6D617020696E2061206D6170 # "map in a map"
1172 A4 # map(4)
1173 67 # text(7)
1174 62797465732031 # "bytes 1"
1175 44 # bytes(4)
1176 78787878 # "xxxx"
1177 67 # text(7)
1178 62797465732032 # "bytes 2"
1179 44 # bytes(4)
1180 79797979 # "yyyy"
1181 6B # text(11)
1182 616E6F7468657220696E74 # "another int"
1183 18 62 # unsigned(98)
1184 66 # text(6)
1185 746578742032 # "text 2"
1186 78 1E # text(30)
1187 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1188 */
1189static const uint8_t spValidMapEncoded[] = {
1190 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1191 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1192 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1193 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1194 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1195 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1196 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1197 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1198 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1199 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1200 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1201 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1202 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1203 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1204 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1205 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206
1207
1208int MapEncodeTest()
1209{
1210 uint8_t *pEncodedMaps;
1211 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301214 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001216
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301218 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1219 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 return(nReturn);
1222}
1223
1224
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001225/*
1226 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001228 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1229 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1230 @param[in] szAlexString Diagnostic code.
1231 @param[in[ pOut Buffer to put the result in
1232 @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 -08001233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001234 @return
1235 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237 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 -08001238 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001239 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1242 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244 */
1245
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301246static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001247{
1248 // Buffer that the result will be written in to
1249 // It is fixed size and small that a stack variable will be fine
1250 // 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 -08001251
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001252 // Context for the encoder
1253 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301254 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001256 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1257 // Contents are label / value pairs
1258 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001262 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1263 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1264
1265 // Add the diagnostic code
1266 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1267
1268 // Add a time stamp
1269 if(time) {
1270 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1271 }
1272
1273 // Add the diagnostic code
1274 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1275
1276 // Open a subordinate map for telemtry data
1277 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1278
1279 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 // Add a few fake integers and buffers for now.
1282 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1283
1284 // Add a few fake integers and buffers for now.
1285 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1286
1287 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301288 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001289 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001291 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1292 }
1293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295 // Close the telemetry map
1296 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298 // Close the map
1299 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001300
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301301 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001302
Laurence Lundblade0595e932018-11-02 22:22:47 +07001303 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301305 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306}
1307
1308
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301309/*
1310 A5 # map(5)
1311 69 # text(9)
1312 696E74656772697479 # "integrity"
1313 F4 # primitive(20)
1314 64 # text(4)
1315 74797065 # "type"
1316 66 # text(6)
1317 726563656E74 # "recent"
1318 64 # text(4)
1319 74696D65 # "time"
1320 C1 # tag(1)
1321 1A 580D4172 # unsigned(1477263730)
1322 64 # text(4)
1323 64696167 # "diag"
1324 6A # text(10)
1325 30784131654335303031 # "0xA1eC5001"
1326 69 # text(9)
1327 74656C656D65747279 # "telemetry"
1328 A3 # map(3)
1329 69 # text(9)
1330 53686F652053697A65 # "Shoe Size"
1331 0C # unsigned(12)
1332 62 # text(2)
1333 4951 # "IQ"
1334 1A FFFFFFFF # unsigned(4294967295)
1335 77 # text(23)
1336 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1337 48 # bytes(8)
1338 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1339 */
1340static const uint8_t spExpectedRTIC[] = {
1341 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1342 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1343 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1344 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1345 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1346 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1347 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1348 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1349 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1350 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1351 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1352 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353
1354
1355int RTICResultsTest()
1356{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001357 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301358 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301359 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301360 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301364 if(CheckResults(Encoded, spExpectedRTIC)) {
1365 return -2;
1366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001367
1368 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369}
1370
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301371
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301372/*
1373 82 # array(2)
1374 19 01C3 # unsigned(451)
1375 43 # bytes(3)
1376 1901D2 # "\x19\x01\xD2"
1377*/
1378static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301379
Laurence Lundblade684aec22018-10-12 19:33:53 +08001380/*
1381 Very basic bstr wrapping test
1382 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301383int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001384{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001385 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001386
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301387 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001388
Laurence Lundblade684aec22018-10-12 19:33:53 +08001389 QCBOREncode_OpenArray(&EC);
1390 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade684aec22018-10-12 19:33:53 +08001392 QCBOREncode_BstrWrap(&EC);
1393 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001394
Laurence Lundblade684aec22018-10-12 19:33:53 +08001395 UsefulBufC Wrapped;
1396 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001397
Laurence Lundblade684aec22018-10-12 19:33:53 +08001398 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundblade684aec22018-10-12 19:33:53 +08001400 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001401 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001402 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001404
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301405 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001406 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001407 }
Laurence Lundblade7412f812019-01-01 18:49:36 -08001408
1409 /* Another test; see about handling length calculation */
1410 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1411 QCBOREncode_OpenArray(&EC);
1412 QCBOREncode_BstrWrap(&EC);
1413 QCBOREncode_OpenArray(&EC);
1414 QCBOREncode_AddNULL(&EC);
1415 QCBOREncode_CloseArray(&EC);
1416 UsefulBufC BStr;
1417 QCBOREncode_CloseBstrWrap(&EC, &BStr);
1418 // 2 is one byte for an array of length 1 and 1 byte for a NULL
1419 if(BStr.ptr != NULL || BStr.len != 2) {
1420 return -5;
1421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423 return 0;
1424}
1425
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001426
1427
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301428int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001429{
1430 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001431 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001432
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301433 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundblade684aec22018-10-12 19:33:53 +08001435 QCBOREncode_OpenArray(&EC);
1436 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001437
Laurence Lundblade684aec22018-10-12 19:33:53 +08001438 QCBOREncode_BstrWrap(&EC);
1439 QCBOREncode_AddUInt64(&EC, 466);
1440 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001441
Laurence Lundblade684aec22018-10-12 19:33:53 +08001442 UsefulBufC Wrapped;
1443 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001444
Laurence Lundblade684aec22018-10-12 19:33:53 +08001445 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundblade684aec22018-10-12 19:33:53 +08001447 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001448 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001449 return -1;
1450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundblade684aec22018-10-12 19:33:53 +08001452 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301453 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001454 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade0595e932018-11-02 22:22:47 +07001455 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001456 return -2;
1457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001458
Laurence Lundblade684aec22018-10-12 19:33:53 +08001459 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301460 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001461 for(int i = 1; i < 18; i++) {
1462 QCBOREncode_BstrWrap(&EC);
1463 }
1464 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001465
Laurence Lundblade684aec22018-10-12 19:33:53 +08001466 for(int i = 1; i < 18; i++) {
1467 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundblade0595e932018-11-02 22:22:47 +07001470 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001471 return -3;
1472 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001473
Laurence Lundblade684aec22018-10-12 19:33:53 +08001474 return 0;
1475}
1476
1477
1478
1479// Part of bstr_wrap_nest_test
1480/*
1481 83 array with three
1482 53 byte string with 19 bytes
1483 01 #1
1484 50 byte string with 16 bytes
1485 02
1486 4D byte string with 13 bytes
1487 03
1488 4A byte string with 10 bytes
1489 04
1490 47 byte string with 7 bytes
1491 05
1492 44 byte string with 4 bytes
1493 06
1494 41 byte string with 1 byte
1495 07
1496 01
1497 02
1498 03
1499 04
1500 05
1501 06
1502 07
1503 A2 map with two items
1504 18 20 label for byte string
1505 54 byte string of length 20
1506 82 Array with two items
1507 10 The integer value 10
1508 A2 map with two items
1509 18 21 label for byte string
1510 44 byte string with 4 bytes
1511 81 array with 1 item
1512 11 integer value 11
1513 18 30 integer value 30
1514 18 40 integer label 40
1515 65 68 65 6C 6C 6F text string hello
1516 18 31 integer value 31
1517 18 41 integer label 41
1518 65 68 65 6C 6C 6F text string hello
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001519
1520
Laurence Lundblade684aec22018-10-12 19:33:53 +08001521 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301522
1523
1524/*
1525 83 # array(3)
1526 56 # bytes(22)
1527 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1528 07 # unsigned(7)
1529 A2 # map(2)
1530 18 20 # unsigned(32)
1531 54 # bytes(20)
1532 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1533 18 41 # unsigned(65)
1534 65 # text(5)
1535 68656C6C6F # "hello"
1536 */
1537static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001538{
1539 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1540 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1541 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1542 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1543 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1544 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1545 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1546 0x6F
1547};
1548
1549// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301550static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001551{
1552 int nReturn;
1553 QCBORDecodeContext DC;
1554 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001555
Laurence Lundblade684aec22018-10-12 19:33:53 +08001556 QCBORItem Item;
1557 nReturn = QCBORDecode_GetNext(&DC, &Item);
1558 if(nReturn) {
1559 return -11;
1560 }
1561 if(Item.uDataType != QCBOR_TYPE_INT64) {
1562 return -12;
1563 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001564
Laurence Lundblade684aec22018-10-12 19:33:53 +08001565 nReturn = QCBORDecode_GetNext(&DC, &Item);
1566 if(nReturn == QCBOR_ERR_HIT_END) {
1567 return 0;
1568 }
1569 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1570 return -13;
1571 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301572 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001573 if(nReturn) {
1574 return nReturn;
1575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundblade684aec22018-10-12 19:33:53 +08001577 nReturn = QCBORDecode_GetNext(&DC, &Item);
1578 if(nReturn) {
1579 return -14;
1580 }
1581 if(Item.uDataType != QCBOR_TYPE_INT64) {
1582 return -15;
1583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundblade684aec22018-10-12 19:33:53 +08001585 if(QCBORDecode_Finish(&DC)) {
1586 return -16;
1587 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001588
Laurence Lundblade684aec22018-10-12 19:33:53 +08001589 return 0;
1590}
1591
1592// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301593static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001594{
1595 int nReturn;
1596 QCBORDecodeContext DC;
1597 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001598
Laurence Lundblade684aec22018-10-12 19:33:53 +08001599 QCBORItem Item;
1600 nReturn = QCBORDecode_GetNext(&DC, &Item);
1601 if(nReturn) {
1602 return -11;
1603 }
1604 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1605 return -12;
1606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001607
Laurence Lundblade684aec22018-10-12 19:33:53 +08001608 nReturn = QCBORDecode_GetNext(&DC, &Item);
1609 if(nReturn) {
1610 return -11;
1611 }
1612 if(Item.uDataType != QCBOR_TYPE_INT64) {
1613 return -12;
1614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001615
Laurence Lundblade684aec22018-10-12 19:33:53 +08001616 nReturn = QCBORDecode_GetNext(&DC, &Item);
1617 if(nReturn) {
1618 return -11;
1619 }
1620 if(Item.uDataType != QCBOR_TYPE_MAP) {
1621 return 0;
1622 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade684aec22018-10-12 19:33:53 +08001624 nReturn = QCBORDecode_GetNext(&DC, &Item);
1625 if(nReturn) {
1626 return -11;
1627 }
1628 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1629 return -13;
1630 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301631 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001632 if(nReturn) {
1633 return nReturn;
1634 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001635
Laurence Lundblade684aec22018-10-12 19:33:53 +08001636 nReturn = QCBORDecode_GetNext(&DC, &Item);
1637 if(nReturn) {
1638 return -11;
1639 }
1640 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1641 return -12;
1642 }
1643 nReturn = QCBORDecode_GetNext(&DC, &Item);
1644 if(nReturn) {
1645 return -11;
1646 }
1647 if(Item.uDataType != QCBOR_TYPE_INT64) {
1648 return -12;
1649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001650
Laurence Lundblade684aec22018-10-12 19:33:53 +08001651 if(QCBORDecode_Finish(&DC)) {
1652 return -16;
1653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001654
Laurence Lundblade684aec22018-10-12 19:33:53 +08001655 return 0;
1656}
1657
1658
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301659int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001660{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001661 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301662 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001663
Laurence Lundblade684aec22018-10-12 19:33:53 +08001664 // ---- Make a complicated nested CBOR structure ---
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001665#define BSTR_TEST_DEPTH 10
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001666
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001667 QCBOREncode_OpenArray(&EC);
1668
1669 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001670 QCBOREncode_BstrWrap(&EC);
1671 QCBOREncode_AddUInt64(&EC, i);
1672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001673
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001674 for(int i = 0; i < BSTR_TEST_DEPTH-2; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001675 QCBOREncode_CloseBstrWrap(&EC, NULL);
1676 QCBOREncode_AddUInt64(&EC, i);
1677 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001678
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001679 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001680 QCBOREncode_OpenMap(&EC);
Laurence Lundblade067035b2018-11-28 17:35:25 -08001681 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001682 QCBOREncode_OpenArray(&EC);
1683 QCBOREncode_AddUInt64(&EC, i+0x10);
1684 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001685
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001686 for(int i = 0; i < (BSTR_TEST_DEPTH-2)/3; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001687 QCBOREncode_CloseArray(&EC);
1688 QCBOREncode_AddUInt64(&EC, i+0x30);
1689 QCBOREncode_CloseBstrWrap(&EC, NULL);
1690 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1691 QCBOREncode_CloseMap(&EC);
1692 }
1693 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001694
Laurence Lundblade684aec22018-10-12 19:33:53 +08001695 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001696 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001697 return -1;
1698 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001699
Laurence Lundblade684aec22018-10-12 19:33:53 +08001700 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301701 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001702 return -25;
1703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
1705
Laurence Lundblade684aec22018-10-12 19:33:53 +08001706 // ---- Decode it and see if it is OK ------
1707 QCBORDecodeContext DC;
1708 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001709
Laurence Lundblade684aec22018-10-12 19:33:53 +08001710 QCBORItem Item;
1711 QCBORDecode_GetNext(&DC, &Item);
1712 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1713 return -2;
1714 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001715
Laurence Lundblade684aec22018-10-12 19:33:53 +08001716 QCBORDecode_GetNext(&DC, &Item);
1717 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1718 return -3;
1719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001720
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301721 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001722 if(nReturn) {
1723 return nReturn;
1724 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001725
Laurence Lundblade684aec22018-10-12 19:33:53 +08001726 nReturn = QCBORDecode_GetNext(&DC, &Item);
1727 if(nReturn) {
1728 return -11;
1729 }
1730 if(Item.uDataType != QCBOR_TYPE_INT64) {
1731 return -12;
1732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001733
Laurence Lundblade684aec22018-10-12 19:33:53 +08001734 QCBORDecode_GetNext(&DC, &Item);
1735 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1736 return -2;
1737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001738
Laurence Lundblade684aec22018-10-12 19:33:53 +08001739 QCBORDecode_GetNext(&DC, &Item);
1740 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1741 return -3;
1742 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301743 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001744 if(nReturn) {
1745 return nReturn;
1746 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001747
Laurence Lundblade684aec22018-10-12 19:33:53 +08001748 nReturn = QCBORDecode_GetNext(&DC, &Item);
1749 if(nReturn) {
1750 return -11;
1751 }
1752 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1753 return -12;
1754 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001755
Laurence Lundblade684aec22018-10-12 19:33:53 +08001756 if(QCBORDecode_Finish(&DC)) {
1757 return -16;
1758 }
Laurence Lundblade7412f812019-01-01 18:49:36 -08001759
Laurence Lundblade684aec22018-10-12 19:33:53 +08001760 return 0;
1761}
1762
1763
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301764static const uint8_t spSignature[] = {
1765 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1766 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1767 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1768 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1769 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1770 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1771 0x45, 0xca, 0xcb, 0x36};
1772
1773/*
1774 D2 # tag(18)
1775 84 # array(4)
1776 43 # bytes(3)
1777 A10126 # "\xA1\x01&"
1778 A1 # map(1)
1779 04 # unsigned(4)
1780 42 # bytes(2)
1781 3131 # "11"
1782 54 # bytes(20)
1783 546869732069732074686520636F6E74656E742E # "This is the content."
1784 58 40 # bytes(64)
1785 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4\xCCk#\xD5\x8F\xEF\\\b1\x06\xC4\xD2Z\x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91\xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01\xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA\xCB6"
1786 */
1787static const uint8_t spExpected[] = {
1788 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1789 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1790 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1791 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1792 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1793 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1794 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1795 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1796 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1797 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1798
Laurence Lundblade684aec22018-10-12 19:33:53 +08001799/*
1800 this corresponds exactly to the example in RFC 8152
1801 section C.2.1. This doesn't actually verify the signature
1802 though that would be nice as it would make the test
1803 really good. That would require bring in ECDSA crypto
1804 to this test.
1805 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301806int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001807{
1808 // All of this is from RFC 8152 C.2.1
1809 const char *szKid = "11";
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001810 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001811 const char *szPayload = "This is the content.";
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001812 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301813 static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001814 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301815
Laurence Lundblade684aec22018-10-12 19:33:53 +08001816 // It would be good to compare this to the output from
1817 // a COSE implementation like COSE-C. It has been checked
1818 // against the CBOR playground.
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001819 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001820
Laurence Lundblade684aec22018-10-12 19:33:53 +08001821 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301822 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001823
Laurence Lundblade684aec22018-10-12 19:33:53 +08001824 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09001825 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08001826 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001827
Laurence Lundblade684aec22018-10-12 19:33:53 +08001828 // Add protected headers
1829 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001830
Laurence Lundblade684aec22018-10-12 19:33:53 +08001831 // Empty map with unprotected headers
1832 QCBOREncode_OpenMap(&EC);
1833 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1834 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001835
Laurence Lundblade684aec22018-10-12 19:33:53 +08001836 // The payload
1837 UsefulBufC WrappedPayload;
1838 QCBOREncode_BstrWrap(&EC);
1839 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1840 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001841
Laurence Lundblade684aec22018-10-12 19:33:53 +08001842 // Check we got back the actual payload expected
1843 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1844 return -1;
1845 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001846
Laurence Lundblade684aec22018-10-12 19:33:53 +08001847 // The signature
1848 QCBOREncode_AddBytes(&EC, Signature);
1849 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001850
Laurence Lundblade684aec22018-10-12 19:33:53 +08001851 // Finish and check the results
1852 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001853 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001854 return -2;
1855 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001856
Laurence Lundblade684aec22018-10-12 19:33:53 +08001857 // 98 is the size from RFC 8152 C.2.1
1858 if(COSE_Sign1.len != 98) {
1859 return -3;
1860 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001861
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301862 if(CheckResults(COSE_Sign1, spExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001863 return -4;
1864 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001865
Laurence Lundblade684aec22018-10-12 19:33:53 +08001866 return 0;
1867}
1868
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08001869
1870int EncodeErrorTests()
1871{
1872 QCBOREncodeContext EC;
1873
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08001874
1875 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08001876 // Do all of these tests with NULL buffers as buffers would have to 4GB
1877 UsefulBuf Giant = (UsefulBuf){NULL, (uint64_t)UINT32_MAX + ((uint64_t)UINT32_MAX / 2)};
1878
1879 // First verify no error from a really big buffer
1880 QCBOREncode_Init(&EC, Giant);
1881 QCBOREncode_OpenArray(&EC);
1882 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, (uint64_t)UINT32_MAX + 10000ULL});
1883 QCBOREncode_CloseArray(&EC);
1884 size_t xx;
1885 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
1886 return -1;
1887 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08001888
1889 // second verify error from an array in encoded output too large
1890 QCBOREncode_Init(&EC, Giant);
1891 QCBOREncode_OpenArray(&EC);
1892 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, (uint64_t)UINT32_MAX+10000ULL});
1893 QCBOREncode_OpenArray(&EC);
1894 QCBOREncode_CloseArray(&EC);
1895 QCBOREncode_CloseArray(&EC);
1896 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
1897 return -2;
1898 }
1899
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08001900
1901 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
1902 // Work close to the 4GB size limit for a better test
1903 const uint32_t uLargeSize = UINT32_MAX - 1024;
1904 UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
1905
1906 QCBOREncode_Init(&EC, Large);
1907 QCBOREncode_OpenArray(&EC);
1908 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
1909 QCBOREncode_CloseArray(&EC);
1910 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
1911 // Making sure it succeeds when it should first
1912 return -3;
1913 }
1914
1915 QCBOREncode_Init(&EC, Large);
1916 QCBOREncode_OpenArray(&EC);
1917 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
1918 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
1919 QCBOREncode_CloseArray(&EC);
1920 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
1921 // Now just 1 byte over, see that it fails
1922 return -4;
1923 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08001924
1925
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08001926 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
1927 QCBOREncode_Init(&EC, Large);
1928 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
1929 QCBOREncode_OpenArray(&EC);
1930 }
1931 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
1932 QCBOREncode_CloseArray(&EC);
1933 }
1934 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
1935 // Making sure it succeeds when it should first
1936 return -5;
1937 }
1938
1939 QCBOREncode_Init(&EC, Large);
1940 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
1941 QCBOREncode_OpenArray(&EC);
1942 }
1943 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
1944 QCBOREncode_CloseArray(&EC);
1945 }
1946 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1947 // One more level to cause error
1948 return -6;
1949 }
1950
1951
1952 // ------ QCBOR_ERR_TOO_MANY_CLOSES --------
1953 QCBOREncode_Init(&EC, Large);
1954 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
1955 QCBOREncode_OpenArray(&EC);
1956 }
1957 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
1958 QCBOREncode_CloseArray(&EC);
1959 }
1960 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_TOO_MANY_CLOSES) {
1961 // One more level to cause error
1962 return -7;
1963 }
1964
1965
1966 // ------ QCBOR_ERR_CLOSE_MISMATCH --------
1967 QCBOREncode_Init(&EC, Large);
1968 QCBOREncode_OpenArray(&EC);
1969 UsefulBufC Wrap;
1970 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
1971 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_CLOSE_MISMATCH) {
1972 return -8;
1973 }
1974
1975
1976 // ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN ---------
1977 QCBOREncode_Init(&EC, Large);
1978 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
1979 QCBOREncode_OpenArray(&EC);
1980 }
1981 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
1982 QCBOREncode_CloseArray(&EC);
1983 }
1984 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1985 // One more level to cause error
1986 return -9;
1987 }
1988
Laurence Lundblade241705e2018-12-30 18:56:14 -08001989 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
1990 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08001991
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08001992 return 0;
1993}