blob: 8da104fc06a12c6c3ac855b81017d4664c4034d2 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056#include "qcbor.h"
57#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058
59
Laurence Lundblade369b90a2018-10-22 02:04:37 +053060/*
61 This is the test set for CBOR encoding.
62
63 This is largely complete for the implemented.
64
65 A few more things to do include:
66 - Add a test for counting the top level items and adding it back in with AddRaw()
67 - Run on some different CPUs like 32-bit and maybe even 16-bit
68 - Test the large array count limit
69 - Add the CBOR diagnostic output for every expected
70
71 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080072
Laurence Lundblade369b90a2018-10-22 02:04:37 +053073//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080074
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070075#if PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053076#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080077
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080078// ifdef these out to not have compiler warnings
79static void printencoded(const uint8_t *pEncoded, size_t nLen)
80{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070081 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080082 for(i = 0; i < nLen; i++) {
83 uint8_t Z = pEncoded[i];
84 printf("%02x ", Z);
85 }
86 printf("\n");
87
88 fflush(stdout);
89}
90
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091
Laurence Lundblade369b90a2018-10-22 02:04:37 +053092// Do the comparison and print out where it fails
93static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070094 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053095 for(i = 0; i < U1.len; i++) {
96 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070097 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 +053098 return 1;
99 }
100 }
101 return 0;
102
103}
104
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700105#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530106 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530107
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700108#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109
110#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800111 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
112
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700113#endif
114
115
116
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530117// One big buffer that is used by all the tests to encode into
118// Putting it in uninitialized data is better than using a lot
119// of stack. The tests should run on small devices too.
120static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121
122
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800123
124/*
125 Some very minimal tests.
126 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530127int BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800128{
129 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800130 QCBOREncodeContext EC;
131
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530132 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530133
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800134 QCBOREncode_OpenMap(&EC);
135 QCBOREncode_AddBoolToMapN(&EC, 66, true);
136 QCBOREncode_CloseMap(&EC);
137
138 UsefulBufC Encoded;
139 if(QCBOREncode_Finish2(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530140 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800141 }
142
143
144 // Decode it and see that is right
145 QCBORDecodeContext DC;
146 QCBORItem Item;
147 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
148
149 QCBORDecode_GetNext(&DC, &Item);
150 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530151 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800152 }
153
154 QCBORDecode_GetNext(&DC, &Item);
155 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530156 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800157 }
158
159 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530160 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800161 }
162
163
164 // Make another encoded message with the CBOR from the previous put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530165 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800166 QCBOREncode_Init(&EC, MemoryForEncoded2);
167 QCBOREncode_OpenArray(&EC);
168 QCBOREncode_AddUInt64(&EC, 451);
169 QCBOREncode_AddEncoded(&EC, Encoded);
170 QCBOREncode_OpenMap(&EC);
171 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
172 QCBOREncode_CloseMap(&EC);
173 QCBOREncode_CloseArray(&EC);
174
175 UsefulBufC Encoded2;
176 if(QCBOREncode_Finish2(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530177 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800178 }
179 /*
180 [ // 0 1:3
181 451, // 1 1:2
182 { // 1 1:2 2:1
183 66: true // 2 1:1
184 },
185 { // 1 1:1 2:1
186 -70000: { // 2 1:1 2:1 3:1
187 66: true // 3 XXXXXX
188 }
189 }
190 ]
191
192
193
194 83 # array(3)
195 19 01C3 # unsigned(451)
196 A1 # map(1)
197 18 42 # unsigned(66)
198 F5 # primitive(21)
199 A1 # map(1)
200 3A 0001116F # negative(69999)
201 A1 # map(1)
202 18 42 # unsigned(66)
203 F5 # primitive(21)
204 */
205
206 // Decode it and see if it is OK
207 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
208
209 // 0 1:3
210 QCBORDecode_GetNext(&DC, &Item);
211 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530212 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800213 }
214
215 // 1 1:2
216 QCBORDecode_GetNext(&DC, &Item);
217 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530218 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800219 }
220
221 // 1 1:2 2:1
222 QCBORDecode_GetNext(&DC, &Item);
223 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530224 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800225 }
226
227 // 2 1:1
228 QCBORDecode_GetNext(&DC, &Item);
229 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530230 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800231 }
232
233 // 1 1:1 2:1
234 QCBORDecode_GetNext(&DC, &Item);
235 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530236 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800237 }
238
239 // 2 1:1 2:1 3:1
240 QCBORDecode_GetNext(&DC, &Item);
241 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 +0530242 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800243 }
244
245 // 3 XXXXXX
246 QCBORDecode_GetNext(&DC, &Item);
247 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530248 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800249 }
250
251 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530252 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800253 }
254
255 return 0;
256}
257
258
259
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530260static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530261 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
262 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
263 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
264 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
265 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
266 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
267 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
268 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
269 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
270 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8,
271 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72,
272 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01,
273 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb,
274 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63,
275 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03,
276 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f,
277 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00,
278 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70,
279 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
280 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
281 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
282 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
283 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
284 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
285 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
286 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
287 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
288 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
289 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
290 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
291 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
292 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
293 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
294 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
295 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
296 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
297 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
298 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
299 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
300 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
301 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
302 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
303 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
304 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
305 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
306 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
307 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
308 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
309 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
310 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
311 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
312 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
313 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
314 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
315 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
316 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
317 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
318 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
319 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
320 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
321 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
322 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
323 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
324 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
325 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
326 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
327 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
328 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
329 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
330 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
331 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
332 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
333 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
334 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
335 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
336 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
337 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
338 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
339 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
340 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
341 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
342 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
343 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
344 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
345 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
346 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
347 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
348 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
349 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
350 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
351 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
352 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
353 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
354 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
355 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
356 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
357 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
358 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
359 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
360 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
361 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
362 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
363 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
364 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
365 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
366 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
367 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
368 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
369 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
370 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
371 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
372 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
373 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
374 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
375 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
376 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
377 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
378 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
379 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
380 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
381 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
382 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
383 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
384 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
385 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
386 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
387 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
388 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
389 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
390 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
391 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
392 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
393 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
394 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
395 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
396 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
397 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
398 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
399 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
400 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
401 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
402 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
403 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
404 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
405 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
406 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
407 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
408 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
409 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
410 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
411 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
412 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
413 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
414 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
415 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
416 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
417 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
418 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
419 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
420 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
421 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
422 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
423 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
424 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
425 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
426 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
427 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
428 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
429 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
430 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
431 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
432 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
433 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
434 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
435 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
436 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
437 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
438 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
439 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
440 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
441 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
442 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
443 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
444 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
445 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
446 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
447 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
448 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
449 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
450 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
451 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
452 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
453 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
454 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
455 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
456 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
457 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
458 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
459 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
460 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
461 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
462 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
463 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
464 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
465 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
466 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
467 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
468 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
469 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
470 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
471 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
472 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
473 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
474 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
475 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476};
477
478
479static const char *szMIME = "\
480MIME-Version: 1.0\n\
481Content-Type: multipart/mixed;\n\
482boundary=\"XXXXboundary text\"\n\
483\n\
484This is a multipart message in MIME format.\n\
485\n\
486--XXXXboundary text\n\
487Content-Type: text/plain\n\
488\n\
489this is the body text\n\
490\n\
491--XXXXboundary text\n\
492Content-Type: text/plain;\n\
493Content-Disposition: attachment;\n\
494filename=\"test.txt\"\n\
495\n\
496this is the attachment text\n\
497\n\
498--XXXXboundary text--";
499
500
501int AllAddMethodsTest()
502{
503 QCBOREncodeContext ECtx;
504 int nReturn = 0;
505
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530506 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800507
508 QCBOREncode_OpenArray(&ECtx);
509
510 // Non-map ints
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700511 QCBOREncode_AddTag(&ECtx, 100);
512 QCBOREncode_AddUInt64_2(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 89989909);
513 QCBOREncode_AddTag(&ECtx, 76);
514 QCBOREncode_AddInt64_2(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515 QCBOREncode_AddUInt64(&ECtx,0);
516 QCBOREncode_AddInt64(&ECtx, -44);
517
518 // ints that go in maps
519 QCBOREncode_OpenMap(&ECtx);
520 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
521 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
522 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
523 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
524 QCBOREncode_CloseMap(&ECtx);
525
526 // floats and doubles
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700527 QCBOREncode_AddTag(&ECtx, 88);
528 QCBOREncode_AddFloat_2(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 3.14159);
529 QCBOREncode_AddTag(&ECtx, 99);
530 QCBOREncode_AddDouble_2(&ECtx, "Street", QCBOR_NO_INT_LABEL, 8.654309);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531 QCBOREncode_AddFloat(&ECtx, 1);
532 QCBOREncode_AddDouble(&ECtx, 1);
533
534 // floats and doubles that go in map
535 QCBOREncode_OpenMap(&ECtx);
536 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
537 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
538 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
539 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
540 QCBOREncode_CloseMap(&ECtx);
541
542 // Epoch Date
543 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
544
545 // Epoch date with labels
546 QCBOREncode_OpenMap(&ECtx);
547 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
548 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
549 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
550 QCBOREncode_CloseMap(&ECtx);
551
552 // Binary blobs
553 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
554
555 // binary blobs in maps
556 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700557 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700558 QCBOREncode_AddBytes_2(&ECtx, "binbin", QCBOR_NO_INT_LABEL, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
560 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
561 QCBOREncode_CloseMap(&ECtx);
562
563 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530564 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530566 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"));
567 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
568 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530569 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800570
571 // text blobs in maps
572 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530573 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade5164a9e2018-11-01 11:24:57 +0700574 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar")); // TODO _2?
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700575 QCBOREncode_AddTag(&ECtx, 1000);
576 QCBOREncode_AddSZString_2(&ECtx, "()()()", QCBOR_NO_INT_LABEL, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530577 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
579 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530580 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
581 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
582 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
583 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
584 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
585 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530586 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
587 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588 QCBOREncode_CloseMap(&ECtx);
589
590 // Date strings
591 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
592 QCBOREncode_OpenMap(&ECtx);
593 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
594 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
595 QCBOREncode_CloseMap(&ECtx);
596
597 // true / false ...
598 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
599 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700600 QCBOREncode_AddTag(&ECtx, 66);
601 QCBOREncode_AddSimple_2(&ECtx, "dare", QCBOR_NO_INT_LABEL, CBOR_SIMPLEV_TRUE);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
603 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
604 QCBOREncode_CloseMap(&ECtx);
605
606 // opening an array
607 QCBOREncode_OpenArray(&ECtx);
608 QCBOREncode_CloseArray(&ECtx);
609
610 // opening arrays in a map
611 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700612 QCBOREncode_AddTag(&ECtx, 1093);
613 QCBOREncode_OpenArray_2(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800614 QCBOREncode_CloseArray(&ECtx);
615 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
616 QCBOREncode_CloseArray(&ECtx);
617 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
618 QCBOREncode_CloseArray(&ECtx);
619 QCBOREncode_CloseMap(&ECtx);
620
621 // opening maps with labels and tagging
622 QCBOREncode_OpenMap(&ECtx);
623 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
624 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700625 QCBOREncode_AddTag(&ECtx, 9087);
626 QCBOREncode_OpenMap_2(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800627 QCBOREncode_CloseMap(&ECtx);
628 QCBOREncode_CloseMap(&ECtx);
629 QCBOREncode_CloseMap(&ECtx);
630 QCBOREncode_CloseMap(&ECtx);
631
632 // Extended simple values (these are not standard...)
633 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700634 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700635 QCBOREncode_AddType7_2(&ECtx, "s1", QCBOR_NO_INT_LABEL, 0, 255);
636 QCBOREncode_AddType7_2(&ECtx, "s2", QCBOR_NO_INT_LABEL, 0, 0);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700637 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700638 QCBOREncode_AddType7_2(&ECtx, "s3", QCBOR_NO_INT_LABEL, 0, 33);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700639 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700640 QCBOREncode_AddType7_2(&ECtx, NULL, 88378374, 0, 255);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700641 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700642 QCBOREncode_AddType7_2(&ECtx, NULL, 89, 0, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800643 QCBOREncode_CloseMap(&ECtx);
644
645
646 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530647 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530648 UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
650 QCBOREncode_OpenMap(&ECtx);
651 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
652 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
653 QCBOREncode_CloseMap(&ECtx);
654
655
656 // Bool
657 QCBOREncode_AddBool(&ECtx, true);
658 QCBOREncode_AddBool(&ECtx, false);
659 QCBOREncode_OpenMap(&ECtx);
660 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
661 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
662 QCBOREncode_CloseMap(&ECtx);
663
664
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530665 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530666 UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
668 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
669 QCBOREncode_OpenMap(&ECtx);
670 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
671 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
672 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
673 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
674 QCBOREncode_CloseMap(&ECtx);
675
676 QCBOREncode_CloseArray(&ECtx);
677
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530678 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800679
680 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
681 nReturn = -1;
682 goto Done;
683 }
684
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530685 if(CheckResults(Enc, spExpectedEncodedAll))
686 nReturn = -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800687
688Done:
689 return nReturn;
690}
691
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530692/*
693 98 2F # array(47)
694 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
695 3B 0000000100000000 # negative(4294967296)
696 3A FFFFFFFF # negative(4294967295)
697 3A FFFFFFFE # negative(4294967294)
698 3A FFFFFFFD # negative(4294967293)
699 3A 7FFFFFFF # negative(2147483647)
700 3A 7FFFFFFE # negative(2147483646)
701 3A 00010001 # negative(65537)
702 3A 00010000 # negative(65536)
703 39 FFFF # negative(65535)
704 39 FFFE # negative(65534)
705 39 FFFD # negative(65533)
706 39 0100 # negative(256)
707 38 FF # negative(255)
708 38 FE # negative(254)
709 38 FD # negative(253)
710 38 18 # negative(24)
711 37 # negative(23)
712 36 # negative(22)
713 20 # negative(0)
714 00 # unsigned(0)
715 00 # unsigned(0)
716 01 # unsigned(1)
717 16 # unsigned(22)
718 17 # unsigned(23)
719 18 18 # unsigned(24)
720 18 19 # unsigned(25)
721 18 1A # unsigned(26)
722 18 FE # unsigned(254)
723 18 FF # unsigned(255)
724 19 0100 # unsigned(256)
725 19 0101 # unsigned(257)
726 19 FFFE # unsigned(65534)
727 19 FFFF # unsigned(65535)
728 1A 00010000 # unsigned(65536)
729 1A 00010001 # unsigned(65537)
730 1A 00010002 # unsigned(65538)
731 1A 7FFFFFFF # unsigned(2147483647)
732 1A 7FFFFFFF # unsigned(2147483647)
733 1A 80000000 # unsigned(2147483648)
734 1A 80000001 # unsigned(2147483649)
735 1A FFFFFFFE # unsigned(4294967294)
736 1A FFFFFFFF # unsigned(4294967295)
737 1B 0000000100000000 # unsigned(4294967296)
738 1B 0000000100000001 # unsigned(4294967297)
739 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
740 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
741 */
742static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800743 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
744 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
745 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
746 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
747 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
748 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
749 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
750 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
751 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
752 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
753 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
754 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
755 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
756 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
757 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
758 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
759 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
760 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
761 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
762 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
763 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
764 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
765 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
766 0xff, 0xff};
767
768/*
769
770 Test the generation of integers. This also ends up testing
771 encoding of all the different lengths. It encodes integers
772 of many lengths and values, especially around the boundaries
773 for different types of integers. It compares the output
774 to expected values generated from http://cbor.me.
775
776 */
777int IntegerValuesTest1()
778{
779 QCBOREncodeContext ECtx;
780 int nReturn = 0;
781
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530782 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 QCBOREncode_OpenArray(&ECtx);
784
785 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
786 QCBOREncode_AddInt64(&ECtx, -4294967297);
787 QCBOREncode_AddInt64(&ECtx, -4294967296);
788 QCBOREncode_AddInt64(&ECtx, -4294967295);
789 QCBOREncode_AddInt64(&ECtx, -4294967294);
790 QCBOREncode_AddInt64(&ECtx, -2147483648);
791 QCBOREncode_AddInt64(&ECtx, -2147483647);
792 QCBOREncode_AddInt64(&ECtx, -65538);
793 QCBOREncode_AddInt64(&ECtx, -65537);
794 QCBOREncode_AddInt64(&ECtx, -65536);
795 QCBOREncode_AddInt64(&ECtx, -65535);
796 QCBOREncode_AddInt64(&ECtx, -65534);
797 QCBOREncode_AddInt64(&ECtx, -257);
798 QCBOREncode_AddInt64(&ECtx, -256);
799 QCBOREncode_AddInt64(&ECtx, -255);
800 QCBOREncode_AddInt64(&ECtx, -254);
801 QCBOREncode_AddInt64(&ECtx, -25);
802 QCBOREncode_AddInt64(&ECtx, -24);
803 QCBOREncode_AddInt64(&ECtx, -23);
804 QCBOREncode_AddInt64(&ECtx, -1);
805 QCBOREncode_AddInt64(&ECtx, 0);
806 QCBOREncode_AddUInt64(&ECtx, 0ULL);
807 QCBOREncode_AddInt64(&ECtx, 1);
808 QCBOREncode_AddInt64(&ECtx, 22);
809 QCBOREncode_AddInt64(&ECtx, 23);
810 QCBOREncode_AddInt64(&ECtx, 24);
811 QCBOREncode_AddInt64(&ECtx, 25);
812 QCBOREncode_AddInt64(&ECtx, 26);
813 QCBOREncode_AddInt64(&ECtx, 254);
814 QCBOREncode_AddInt64(&ECtx, 255);
815 QCBOREncode_AddInt64(&ECtx, 256);
816 QCBOREncode_AddInt64(&ECtx, 257);
817 QCBOREncode_AddInt64(&ECtx, 65534);
818 QCBOREncode_AddInt64(&ECtx, 65535);
819 QCBOREncode_AddInt64(&ECtx, 65536);
820 QCBOREncode_AddInt64(&ECtx, 65537);
821 QCBOREncode_AddInt64(&ECtx, 65538);
822 QCBOREncode_AddInt64(&ECtx, 2147483647);
823 QCBOREncode_AddInt64(&ECtx, 2147483647);
824 QCBOREncode_AddInt64(&ECtx, 2147483648);
825 QCBOREncode_AddInt64(&ECtx, 2147483649);
826 QCBOREncode_AddInt64(&ECtx, 4294967294);
827 QCBOREncode_AddInt64(&ECtx, 4294967295);
828 QCBOREncode_AddInt64(&ECtx, 4294967296);
829 QCBOREncode_AddInt64(&ECtx, 4294967297);
830 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
831 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
832
833 QCBOREncode_CloseArray(&ECtx);
834
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530835 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
837 nReturn = -1;
838 }
839
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530840 if(CheckResults(Enc, spExpectedEncodedInts))
841 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800842
843 return(nReturn);
844}
845
846
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530847/*
848 85 # array(5)
849 F5 # primitive(21)
850 F4 # primitive(20)
851 F6 # primitive(22)
852 F7 # primitive(23)
853 A1 # map(1)
854 65 # text(5)
855 554E446566 # "UNDef"
856 F7 # primitive(23)
857 */
858static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
860
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800861int SimpleValuesTest1()
862{
863 QCBOREncodeContext ECtx;
864 int nReturn = 0;
865
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530866 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 QCBOREncode_OpenArray(&ECtx);
868
869 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
870 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
871 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
872 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
873
874 QCBOREncode_OpenMap(&ECtx);
875
876 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
877 QCBOREncode_CloseMap(&ECtx);
878
879 QCBOREncode_CloseArray(&ECtx);
880
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530881 UsefulBufC ECBOR;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800882 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
883 nReturn = -1;
884 }
885
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530886 if(CheckResults(ECBOR, spExpectedEncodedSimple))
887 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888
889 return(nReturn);
890}
891
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530892
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530893/*
894 83 # array(3)
895 C0 # tag(0)
896 74 # text(20)
897 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
898 C1 # tag(1)
899 1A 514B67B0 # unsigned(1363896240)
900 A2 # map(2)
901 78 19 # text(25)
902 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
903 C0 # tag(0)
904 77 # text(23)
905 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
906 62 # text(2)
907 5344 # "SD"
908 C1 # tag(1)
909 19 03E7 # unsigned(999)
910 */
911static const uint8_t spExpectedEncodedDates[] = {
912 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
913 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
914 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
915 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
916 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
917 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
918 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
919 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
920 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921};
922
923int EncodeDateTest()
924{
925 QCBOREncodeContext ECtx;
926 int nReturn = 0;
927
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530928 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929
930 QCBOREncode_OpenArray(&ECtx);
931
932
933 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
934 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
935
936
937 QCBOREncode_OpenMap(&ECtx);
938
939 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
940
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800941 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
942
943 QCBOREncode_CloseMap(&ECtx);
944
945 QCBOREncode_CloseArray(&ECtx);
946
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530947 UsefulBufC ECBOR;
948
949 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 nReturn = -1;
951 }
952
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530953 if(CheckResults(ECBOR, spExpectedEncodedDates))
954 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800955
956 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957}
958
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960int ArrayNestingTest1()
961{
962 QCBOREncodeContext ECtx;
963 int i;
964 int nReturn = 0;
965
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530966 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
968 QCBOREncode_OpenArray(&ECtx);
969 }
970 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
971 QCBOREncode_CloseArray(&ECtx);
972 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530973 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800974 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 nReturn = -1;
976 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977
978 return(nReturn);
979}
980
981
982
983int ArrayNestingTest2()
984{
985 QCBOREncodeContext ECtx;
986 int i;
987 int nReturn = 0;
988
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530989 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800990 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
991 QCBOREncode_OpenArray(&ECtx);
992 }
993 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
994 QCBOREncode_CloseArray(&ECtx);
995 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530996
997 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
999 nReturn = -1;
1000 }
1001
1002 return(nReturn);
1003}
1004
1005
1006
1007int ArrayNestingTest3()
1008{
1009 QCBOREncodeContext ECtx;
1010 int i;
1011 int nReturn = 0;
1012
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301013 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001014 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1015 QCBOREncode_OpenArray(&ECtx);
1016 }
1017 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1018 QCBOREncode_CloseArray(&ECtx);
1019 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301020 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001021 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
1022 nReturn = -1;
1023 }
1024
1025 return(nReturn);
1026}
1027
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301029/*
1030 81 # array(1)
1031 81 # array(1)
1032 81 # array(1)
1033 81 # array(1)
1034 80 # array(0)
1035*/
1036static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001037
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301039/*
1040 82 # array(2)
1041 81 # array(1)
1042 81 # array(1)
1043 81 # array(1)
1044 81 # array(1)
1045 80 # array(0)
1046 98 2F # array(47)
1047 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1048 3B 0000000100000000 # negative(4294967296)
1049 3A FFFFFFFF # negative(4294967295)
1050 3A FFFFFFFE # negative(4294967294)
1051 3A FFFFFFFD # negative(4294967293)
1052 3A 7FFFFFFF # negative(2147483647)
1053 3A 7FFFFFFE # negative(2147483646)
1054 3A 00010001 # negative(65537)
1055 3A 00010000 # negative(65536)
1056 39 FFFF # negative(65535)
1057 39 FFFE # negative(65534)
1058 39 FFFD # negative(65533)
1059 39 0100 # negative(256)
1060 38 FF # negative(255)
1061 38 FE # negative(254)
1062 38 FD # negative(253)
1063 38 18 # negative(24)
1064 37 # negative(23)
1065 36 # negative(22)
1066 20 # negative(0)
1067 00 # unsigned(0)
1068 00 # unsigned(0)
1069 01 # unsigned(1)
1070 16 # unsigned(22)
1071 17 # unsigned(23)
1072 18 18 # unsigned(24)
1073 18 19 # unsigned(25)
1074 18 1A # unsigned(26)
1075 18 FE # unsigned(254)
1076 18 FF # unsigned(255)
1077 19 0100 # unsigned(256)
1078 19 0101 # unsigned(257)
1079 19 FFFE # unsigned(65534)
1080 19 FFFF # unsigned(65535)
1081 1A 00010000 # unsigned(65536)
1082 1A 00010001 # unsigned(65537)
1083 1A 00010002 # unsigned(65538)
1084 1A 7FFFFFFF # unsigned(2147483647)
1085 1A 7FFFFFFF # unsigned(2147483647)
1086 1A 80000000 # unsigned(2147483648)
1087 1A 80000001 # unsigned(2147483649)
1088 1A FFFFFFFE # unsigned(4294967294)
1089 1A FFFFFFFF # unsigned(4294967295)
1090 1B 0000000100000000 # unsigned(4294967296)
1091 1B 0000000100000001 # unsigned(4294967297)
1092 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1093 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1094 */
1095static const uint8_t spEncodeRawExpected[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1097 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1098 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1099 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1100 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1101 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1102 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1103 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1104 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1105 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1106 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1107 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1108 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1109 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1110 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1111 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1112 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1113 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1114 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1115 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1116 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1117 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1120
1121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001122int EncodeRawTest()
1123{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001124 QCBOREncodeContext ECtx;
1125
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301126 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001127 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301128 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1129 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001130 QCBOREncode_CloseArray(&ECtx);
1131
1132 UsefulBufC EncodedRawTest;
1133
1134 if(QCBOREncode_Finish2(&ECtx, &EncodedRawTest)) {
1135 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 }
1137
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301138 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001139 return -5;
1140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001142 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143}
1144
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301145/*
1146 This returns a pointer to spBigBuf
1147 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001148static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1149{
1150 QCBOREncodeContext ECtx;
1151 int nReturn = -1;
1152
1153 *pEncoded = NULL;
1154 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301155 size_t uFirstSizeEstimate = 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001156
1157 // loop runs CBOR encoding twice. First with no buffer to
1158 // calucate the length so buffer can be allocated correctly,
1159 // and last with the buffer to do the actual encoding
1160 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301161 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 QCBOREncode_OpenMap(&ECtx);
1163 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1164 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1165 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1166 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1167 QCBOREncode_CloseArray(&ECtx);
1168 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1169 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1170 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1171 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1172 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1173 QCBOREncode_CloseMap(&ECtx);
1174 QCBOREncode_CloseMap(&ECtx);
1175
1176
1177 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
1178 goto Done;
1179 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301180 if(uFirstSizeEstimate != *pEncodedLen) {
1181 nReturn = 1;
1182 } else {
1183 nReturn = 0;
1184 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 goto Done;
1186 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301187 *pEncoded = spBigBuf;
1188 uFirstSizeEstimate = *pEncodedLen;
1189
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001190 } while(1);
1191
1192 Done:
1193 return(nReturn);
1194}
1195
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301196/*
1197 A3 # map(3)
1198 6D # text(13)
1199 666972737420696E7465676572 # "first integer"
1200 18 2A # unsigned(42)
1201 77 # text(23)
1202 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1203 82 # array(2)
1204 67 # text(7)
1205 737472696E6731 # "string1"
1206 67 # text(7)
1207 737472696E6732 # "string2"
1208 6C # text(12)
1209 6D617020696E2061206D6170 # "map in a map"
1210 A4 # map(4)
1211 67 # text(7)
1212 62797465732031 # "bytes 1"
1213 44 # bytes(4)
1214 78787878 # "xxxx"
1215 67 # text(7)
1216 62797465732032 # "bytes 2"
1217 44 # bytes(4)
1218 79797979 # "yyyy"
1219 6B # text(11)
1220 616E6F7468657220696E74 # "another int"
1221 18 62 # unsigned(98)
1222 66 # text(6)
1223 746578742032 # "text 2"
1224 78 1E # text(30)
1225 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1226 */
1227static const uint8_t spValidMapEncoded[] = {
1228 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1229 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1230 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1231 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1232 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1233 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1234 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1235 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1236 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1237 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1238 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1239 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1240 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1241 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1242 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1243 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244
1245
1246int MapEncodeTest()
1247{
1248 uint8_t *pEncodedMaps;
1249 size_t nEncodedMapLen;
1250
1251 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301252 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001253 }
1254
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301256 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1257 nReturn = 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258
1259 return(nReturn);
1260}
1261
1262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001263/*
1264 @brief Encode the RTIC results
1265
1266 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1267 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1268 @param[in] szAlexString Diagnostic code.
1269 @param[in[ pOut Buffer to put the result in
1270 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1271
1272 @return
1273 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1274
1275 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1276 short an error will be returned. This function will never write off the end
1277 of the buffer passed to it.
1278
1279 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1280 length of the encoded CBOR.
1281
1282 */
1283
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301284static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285{
1286 // Buffer that the result will be written in to
1287 // It is fixed size and small that a stack variable will be fine
1288 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1289
1290 // Context for the encoder
1291 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301292 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293
1294 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1295 // Contents are label / value pairs
1296 QCBOREncode_OpenMap(&ECtx);
1297
1298 { // Brace / indention just to show CBOR encoding nesting
1299
1300 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1301 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1302
1303 // Add the diagnostic code
1304 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1305
1306 // Add a time stamp
1307 if(time) {
1308 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1309 }
1310
1311 // Add the diagnostic code
1312 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1313
1314 // Open a subordinate map for telemtry data
1315 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1316
1317 { // Brace / indention just to show CBOR encoding nesting
1318
1319 // Add a few fake integers and buffers for now.
1320 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1321
1322 // Add a few fake integers and buffers for now.
1323 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1324
1325 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301326 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001327 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1328
1329 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1330 }
1331 }
1332
1333 // Close the telemetry map
1334 QCBOREncode_CloseMap(&ECtx);
1335
1336 // Close the map
1337 QCBOREncode_CloseMap(&ECtx);
1338
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301339 UsefulBufC Result;
1340
1341 QCBOREncode_Finish2(&ECtx, &Result);
1342
1343 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344}
1345
1346
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301347/*
1348 A5 # map(5)
1349 69 # text(9)
1350 696E74656772697479 # "integrity"
1351 F4 # primitive(20)
1352 64 # text(4)
1353 74797065 # "type"
1354 66 # text(6)
1355 726563656E74 # "recent"
1356 64 # text(4)
1357 74696D65 # "time"
1358 C1 # tag(1)
1359 1A 580D4172 # unsigned(1477263730)
1360 64 # text(4)
1361 64696167 # "diag"
1362 6A # text(10)
1363 30784131654335303031 # "0xA1eC5001"
1364 69 # text(9)
1365 74656C656D65747279 # "telemetry"
1366 A3 # map(3)
1367 69 # text(9)
1368 53686F652053697A65 # "Shoe Size"
1369 0C # unsigned(12)
1370 62 # text(2)
1371 4951 # "IQ"
1372 1A FFFFFFFF # unsigned(4294967295)
1373 77 # text(23)
1374 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1375 48 # bytes(8)
1376 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1377 */
1378static const uint8_t spExpectedRTIC[] = {
1379 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1380 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1381 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1382 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1383 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1384 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1385 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1386 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1387 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1388 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1389 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1390 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001391
1392
1393int RTICResultsTest()
1394{
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301395 UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
1396 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301397 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301398 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301400 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301402 if(CheckResults(Encoded, spExpectedRTIC)) {
1403 return -2;
1404 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405
1406 return 0;
1407}
1408
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301409
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301410/*
1411 82 # array(2)
1412 19 01C3 # unsigned(451)
1413 43 # bytes(3)
1414 1901D2 # "\x19\x01\xD2"
1415*/
1416static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301417
Laurence Lundblade684aec22018-10-12 19:33:53 +08001418/*
1419 Very basic bstr wrapping test
1420 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301421int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001423 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301425 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001426
Laurence Lundblade684aec22018-10-12 19:33:53 +08001427 QCBOREncode_OpenArray(&EC);
1428 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429
Laurence Lundblade684aec22018-10-12 19:33:53 +08001430 QCBOREncode_BstrWrap(&EC);
1431 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432
Laurence Lundblade684aec22018-10-12 19:33:53 +08001433 UsefulBufC Wrapped;
1434 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001435
Laurence Lundblade684aec22018-10-12 19:33:53 +08001436 QCBOREncode_CloseArray(&EC);
1437
1438 UsefulBufC Encoded;
1439 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1440 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301443 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001444 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445 }
1446
1447 return 0;
1448}
1449
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450
1451
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301452int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001453{
1454 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001455 QCBOREncodeContext EC;
1456
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301457 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001458
1459 QCBOREncode_OpenArray(&EC);
1460 QCBOREncode_AddUInt64(&EC, 451);
1461
1462 QCBOREncode_BstrWrap(&EC);
1463 QCBOREncode_AddUInt64(&EC, 466);
1464 QCBOREncode_OpenArray(&EC);
1465
1466 UsefulBufC Wrapped;
1467 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1468
1469 QCBOREncode_CloseArray(&EC);
1470
1471 UsefulBufC Encoded2;
1472 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
1473 return -1;
1474 }
1475
1476 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301477 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001478 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1479 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
1480 return -2;
1481 }
1482
1483 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301484 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001485 for(int i = 1; i < 18; i++) {
1486 QCBOREncode_BstrWrap(&EC);
1487 }
1488 QCBOREncode_AddBool(&EC, true);
1489
1490 for(int i = 1; i < 18; i++) {
1491 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1492 }
1493
1494 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1495 return -3;
1496 }
1497
1498 return 0;
1499}
1500
1501
1502
1503// Part of bstr_wrap_nest_test
1504/*
1505 83 array with three
1506 53 byte string with 19 bytes
1507 01 #1
1508 50 byte string with 16 bytes
1509 02
1510 4D byte string with 13 bytes
1511 03
1512 4A byte string with 10 bytes
1513 04
1514 47 byte string with 7 bytes
1515 05
1516 44 byte string with 4 bytes
1517 06
1518 41 byte string with 1 byte
1519 07
1520 01
1521 02
1522 03
1523 04
1524 05
1525 06
1526 07
1527 A2 map with two items
1528 18 20 label for byte string
1529 54 byte string of length 20
1530 82 Array with two items
1531 10 The integer value 10
1532 A2 map with two items
1533 18 21 label for byte string
1534 44 byte string with 4 bytes
1535 81 array with 1 item
1536 11 integer value 11
1537 18 30 integer value 30
1538 18 40 integer label 40
1539 65 68 65 6C 6C 6F text string hello
1540 18 31 integer value 31
1541 18 41 integer label 41
1542 65 68 65 6C 6C 6F text string hello
1543
1544
1545 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301546
1547
1548/*
1549 83 # array(3)
1550 56 # bytes(22)
1551 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1552 07 # unsigned(7)
1553 A2 # map(2)
1554 18 20 # unsigned(32)
1555 54 # bytes(20)
1556 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1557 18 41 # unsigned(65)
1558 65 # text(5)
1559 68656C6C6F # "hello"
1560 */
1561static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001562{
1563 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1564 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1565 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1566 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1567 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1568 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1569 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1570 0x6F
1571};
1572
1573// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301574static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001575{
1576 int nReturn;
1577 QCBORDecodeContext DC;
1578 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1579
1580 QCBORItem Item;
1581 nReturn = QCBORDecode_GetNext(&DC, &Item);
1582 if(nReturn) {
1583 return -11;
1584 }
1585 if(Item.uDataType != QCBOR_TYPE_INT64) {
1586 return -12;
1587 }
1588
1589 nReturn = QCBORDecode_GetNext(&DC, &Item);
1590 if(nReturn == QCBOR_ERR_HIT_END) {
1591 return 0;
1592 }
1593 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1594 return -13;
1595 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301596 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001597 if(nReturn) {
1598 return nReturn;
1599 }
1600
1601 nReturn = QCBORDecode_GetNext(&DC, &Item);
1602 if(nReturn) {
1603 return -14;
1604 }
1605 if(Item.uDataType != QCBOR_TYPE_INT64) {
1606 return -15;
1607 }
1608
1609 if(QCBORDecode_Finish(&DC)) {
1610 return -16;
1611 }
1612
1613 return 0;
1614}
1615
1616// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301617static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001618{
1619 int nReturn;
1620 QCBORDecodeContext DC;
1621 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1622
1623 QCBORItem Item;
1624 nReturn = QCBORDecode_GetNext(&DC, &Item);
1625 if(nReturn) {
1626 return -11;
1627 }
1628 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1629 return -12;
1630 }
1631
1632 nReturn = QCBORDecode_GetNext(&DC, &Item);
1633 if(nReturn) {
1634 return -11;
1635 }
1636 if(Item.uDataType != QCBOR_TYPE_INT64) {
1637 return -12;
1638 }
1639
1640 nReturn = QCBORDecode_GetNext(&DC, &Item);
1641 if(nReturn) {
1642 return -11;
1643 }
1644 if(Item.uDataType != QCBOR_TYPE_MAP) {
1645 return 0;
1646 }
1647
1648 nReturn = QCBORDecode_GetNext(&DC, &Item);
1649 if(nReturn) {
1650 return -11;
1651 }
1652 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1653 return -13;
1654 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301655 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001656 if(nReturn) {
1657 return nReturn;
1658 }
1659
1660 nReturn = QCBORDecode_GetNext(&DC, &Item);
1661 if(nReturn) {
1662 return -11;
1663 }
1664 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1665 return -12;
1666 }
1667 nReturn = QCBORDecode_GetNext(&DC, &Item);
1668 if(nReturn) {
1669 return -11;
1670 }
1671 if(Item.uDataType != QCBOR_TYPE_INT64) {
1672 return -12;
1673 }
1674
1675 if(QCBORDecode_Finish(&DC)) {
1676 return -16;
1677 }
1678
1679 return 0;
1680}
1681
1682
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301683int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001684{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001685 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301686 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001687
1688 // ---- Make a complicated nested CBOR structure ---
1689 QCBOREncode_OpenArray(&EC);
1690
1691 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1692 QCBOREncode_BstrWrap(&EC);
1693 QCBOREncode_AddUInt64(&EC, i);
1694 }
1695
1696 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1697 QCBOREncode_CloseBstrWrap(&EC, NULL);
1698 QCBOREncode_AddUInt64(&EC, i);
1699 }
1700
1701 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1702 QCBOREncode_OpenMap(&EC);
1703 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1704 QCBOREncode_OpenArray(&EC);
1705 QCBOREncode_AddUInt64(&EC, i+0x10);
1706 }
1707
1708 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1709 QCBOREncode_CloseArray(&EC);
1710 QCBOREncode_AddUInt64(&EC, i+0x30);
1711 QCBOREncode_CloseBstrWrap(&EC, NULL);
1712 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1713 QCBOREncode_CloseMap(&EC);
1714 }
1715 QCBOREncode_CloseArray(&EC);
1716
1717 UsefulBufC Encoded;
1718 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1719 return -1;
1720 }
1721
1722 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301723 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001724 return -25;
1725 }
1726
1727
1728 // ---- Decode it and see if it is OK ------
1729 QCBORDecodeContext DC;
1730 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1731
1732 QCBORItem Item;
1733 QCBORDecode_GetNext(&DC, &Item);
1734 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1735 return -2;
1736 }
1737
1738 QCBORDecode_GetNext(&DC, &Item);
1739 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1740 return -3;
1741 }
1742
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301743 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001744 if(nReturn) {
1745 return nReturn;
1746 }
1747
1748 nReturn = QCBORDecode_GetNext(&DC, &Item);
1749 if(nReturn) {
1750 return -11;
1751 }
1752 if(Item.uDataType != QCBOR_TYPE_INT64) {
1753 return -12;
1754 }
1755
1756 QCBORDecode_GetNext(&DC, &Item);
1757 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1758 return -2;
1759 }
1760
1761 QCBORDecode_GetNext(&DC, &Item);
1762 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1763 return -3;
1764 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301765 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001766 if(nReturn) {
1767 return nReturn;
1768 }
1769
1770 nReturn = QCBORDecode_GetNext(&DC, &Item);
1771 if(nReturn) {
1772 return -11;
1773 }
1774 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1775 return -12;
1776 }
1777
1778 if(QCBORDecode_Finish(&DC)) {
1779 return -16;
1780 }
1781
1782 return 0;
1783}
1784
1785
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301786static const uint8_t spSignature[] = {
1787 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1788 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1789 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1790 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1791 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1792 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1793 0x45, 0xca, 0xcb, 0x36};
1794
1795/*
1796 D2 # tag(18)
1797 84 # array(4)
1798 43 # bytes(3)
1799 A10126 # "\xA1\x01&"
1800 A1 # map(1)
1801 04 # unsigned(4)
1802 42 # bytes(2)
1803 3131 # "11"
1804 54 # bytes(20)
1805 546869732069732074686520636F6E74656E742E # "This is the content."
1806 58 40 # bytes(64)
1807 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"
1808 */
1809static const uint8_t spExpected[] = {
1810 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1811 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1812 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1813 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1814 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1815 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1816 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1817 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1818 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1819 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1820
Laurence Lundblade684aec22018-10-12 19:33:53 +08001821/*
1822 this corresponds exactly to the example in RFC 8152
1823 section C.2.1. This doesn't actually verify the signature
1824 though that would be nice as it would make the test
1825 really good. That would require bring in ECDSA crypto
1826 to this test.
1827 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301828int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001829{
1830 // All of this is from RFC 8152 C.2.1
1831 const char *szKid = "11";
1832 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1833 const char *szPayload = "This is the content.";
1834 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301835 static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301836 UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301837
Laurence Lundblade684aec22018-10-12 19:33:53 +08001838 // It would be good to compare this to the output from
1839 // a COSE implementation like COSE-C. It has been checked
1840 // against the CBOR playground.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301841 UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001842
Laurence Lundblade684aec22018-10-12 19:33:53 +08001843 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301844 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001845
1846 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladecafcfe12018-10-31 21:59:50 +07001847 QCBOREncode_AddTag(&EC, 18); // TODO: replace with constant
1848 QCBOREncode_OpenArray_2(&EC, NULL, QCBOR_NO_INT_LABEL); // TODO: _2
Laurence Lundblade684aec22018-10-12 19:33:53 +08001849
1850 // Add protected headers
1851 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1852
1853 // Empty map with unprotected headers
1854 QCBOREncode_OpenMap(&EC);
1855 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1856 QCBOREncode_CloseMap(&EC);
1857
1858 // The payload
1859 UsefulBufC WrappedPayload;
1860 QCBOREncode_BstrWrap(&EC);
1861 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1862 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1863
1864 // Check we got back the actual payload expected
1865 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1866 return -1;
1867 }
1868
1869 // The signature
1870 QCBOREncode_AddBytes(&EC, Signature);
1871 QCBOREncode_CloseArray(&EC);
1872
1873 // Finish and check the results
1874 UsefulBufC COSE_Sign1;
1875 if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) {
1876 return -2;
1877 }
1878
1879 // 98 is the size from RFC 8152 C.2.1
1880 if(COSE_Sign1.len != 98) {
1881 return -3;
1882 }
1883
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301884 if(CheckResults(COSE_Sign1, spExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001885 return -4;
1886 }
1887
1888 return 0;
1889}
1890