blob: cc3ad949a9a21bdad8152459b2aa2cc1506ac613 [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.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05305
Laurence Lundbladed92a6162018-11-01 11:38:35 +07006 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
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 Lundbladedc6e28e2018-10-11 19:19:27 +053019
Laurence Lundbladed92a6162018-11-01 11:38:35 +070020 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30 IF 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.
39
40 This is largely complete for the implemented.
41
42 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
47
48 */
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 Lundbladecafcfe12018-10-31 21:59:50 +070052#if 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;
79
80}
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;
108
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);
114
115 UsefulBufC Encoded;
116 if(QCBOREncode_Finish2(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530117 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800118 }
119
120
121 // Decode it and see that is right
122 QCBORDecodeContext DC;
123 QCBORItem Item;
124 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
125
126 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 }
130
131 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 }
135
136 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530137 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800138 }
139
140
141 // 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);
151
152 UsefulBufC Encoded2;
153 if(QCBOREncode_Finish2(&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 ]
168
169
170
171 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 */
182
183 // Decode it and see if it is OK
184 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
185
186 // 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 }
191
192 // 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 }
197
198 // 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 }
203
204 // 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 }
209
210 // 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 }
215
216 // 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 }
221
222 // 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 }
227
228 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530229 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 }
231
232 return 0;
233}
234
235
236
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530237static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530238 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
239 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,
247 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8,
248 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72,
249 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01,
250 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb,
251 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63,
252 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03,
253 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f,
254 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00,
255 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70,
256 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
257 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
258 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
259 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
260 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
261 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
262 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
263 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
264 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
265 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
266 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
267 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
268 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
269 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
270 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
271 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
272 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
273 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
274 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
275 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
276 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
277 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
278 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
279 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
280 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
281 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
282 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
283 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
284 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
285 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
286 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
287 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
288 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
289 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
290 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
291 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
292 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
293 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
294 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
295 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
296 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
297 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
298 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
299 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
300 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
301 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
302 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
303 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
304 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
305 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
306 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
307 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
308 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
309 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
310 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
311 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
312 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
313 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
314 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
315 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
316 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
317 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
318 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
319 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
320 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
321 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
322 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
323 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
324 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
325 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
326 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
327 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
328 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
329 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
330 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
331 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
332 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
333 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
334 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
335 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
336 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
337 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
338 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
339 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
340 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
341 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
342 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
343 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
344 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
345 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
346 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
347 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
348 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
349 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
350 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
351 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
352 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
353 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
354 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
355 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
356 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
357 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
358 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
359 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
360 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
361 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
362 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
363 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
364 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
365 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
366 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
367 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
368 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
369 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
370 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
371 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
372 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
373 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
374 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
375 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
376 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
377 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
378 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
379 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
380 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
381 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
382 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
383 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
384 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
385 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
386 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
387 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
388 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
389 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
390 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
391 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
392 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
393 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
394 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
395 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
396 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
397 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
398 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
399 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
400 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
401 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
402 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
403 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
404 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
405 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
406 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
407 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
408 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
409 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
410 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
411 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
412 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
413 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
414 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
415 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
416 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
417 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
418 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
419 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
420 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
421 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
422 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
423 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
424 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
425 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
426 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
427 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
428 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
429 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
430 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
431 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
432 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
433 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
434 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
435 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
436 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
437 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
438 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
439 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
440 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
441 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
442 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
443 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
444 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
445 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
446 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
447 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
448 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
449 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
450 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
451 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
452 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453};
454
455
456static const char *szMIME = "\
457MIME-Version: 1.0\n\
458Content-Type: multipart/mixed;\n\
459boundary=\"XXXXboundary text\"\n\
460\n\
461This is a multipart message in MIME format.\n\
462\n\
463--XXXXboundary text\n\
464Content-Type: text/plain\n\
465\n\
466this is the body text\n\
467\n\
468--XXXXboundary text\n\
469Content-Type: text/plain;\n\
470Content-Disposition: attachment;\n\
471filename=\"test.txt\"\n\
472\n\
473this is the attachment text\n\
474\n\
475--XXXXboundary text--";
476
477
478int AllAddMethodsTest()
479{
480 QCBOREncodeContext ECtx;
481 int nReturn = 0;
482
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530483 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484
485 QCBOREncode_OpenArray(&ECtx);
486
487 // Non-map ints
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700488 QCBOREncode_AddTag(&ECtx, 100);
489 QCBOREncode_AddUInt64_2(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 89989909);
490 QCBOREncode_AddTag(&ECtx, 76);
491 QCBOREncode_AddInt64_2(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800492 QCBOREncode_AddUInt64(&ECtx,0);
493 QCBOREncode_AddInt64(&ECtx, -44);
494
495 // ints that go in maps
496 QCBOREncode_OpenMap(&ECtx);
497 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
498 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
499 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
500 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
501 QCBOREncode_CloseMap(&ECtx);
502
503 // floats and doubles
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700504 QCBOREncode_AddTag(&ECtx, 88);
505 QCBOREncode_AddFloat_2(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 3.14159);
506 QCBOREncode_AddTag(&ECtx, 99);
507 QCBOREncode_AddDouble_2(&ECtx, "Street", QCBOR_NO_INT_LABEL, 8.654309);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508 QCBOREncode_AddFloat(&ECtx, 1);
509 QCBOREncode_AddDouble(&ECtx, 1);
510
511 // floats and doubles that go in map
512 QCBOREncode_OpenMap(&ECtx);
513 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
514 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
515 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
516 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
517 QCBOREncode_CloseMap(&ECtx);
518
519 // Epoch Date
520 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
521
522 // Epoch date with labels
523 QCBOREncode_OpenMap(&ECtx);
524 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
525 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
526 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
527 QCBOREncode_CloseMap(&ECtx);
528
529 // Binary blobs
530 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
531
532 // binary blobs in maps
533 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700534 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700535 QCBOREncode_AddBytes_2(&ECtx, "binbin", QCBOR_NO_INT_LABEL, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
537 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
538 QCBOREncode_CloseMap(&ECtx);
539
540 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530541 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530543 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"));
544 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
545 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530546 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547
548 // text blobs in maps
549 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530550 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade5164a9e2018-11-01 11:24:57 +0700551 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar")); // TODO _2?
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700552 QCBOREncode_AddTag(&ECtx, 1000);
553 QCBOREncode_AddSZString_2(&ECtx, "()()()", QCBOR_NO_INT_LABEL, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530554 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
556 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530557 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
558 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
559 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
560 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
561 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
562 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530563 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
564 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565 QCBOREncode_CloseMap(&ECtx);
566
567 // Date strings
568 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
569 QCBOREncode_OpenMap(&ECtx);
570 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
571 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
572 QCBOREncode_CloseMap(&ECtx);
573
574 // true / false ...
575 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
576 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700577 QCBOREncode_AddTag(&ECtx, 66);
578 QCBOREncode_AddSimple_2(&ECtx, "dare", QCBOR_NO_INT_LABEL, CBOR_SIMPLEV_TRUE);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
580 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
581 QCBOREncode_CloseMap(&ECtx);
582
583 // opening an array
584 QCBOREncode_OpenArray(&ECtx);
585 QCBOREncode_CloseArray(&ECtx);
586
587 // opening arrays in a map
588 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700589 QCBOREncode_AddTag(&ECtx, 1093);
590 QCBOREncode_OpenArray_2(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591 QCBOREncode_CloseArray(&ECtx);
592 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
593 QCBOREncode_CloseArray(&ECtx);
594 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
595 QCBOREncode_CloseArray(&ECtx);
596 QCBOREncode_CloseMap(&ECtx);
597
598 // opening maps with labels and tagging
599 QCBOREncode_OpenMap(&ECtx);
600 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
601 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700602 QCBOREncode_AddTag(&ECtx, 9087);
603 QCBOREncode_OpenMap_2(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 QCBOREncode_CloseMap(&ECtx);
605 QCBOREncode_CloseMap(&ECtx);
606 QCBOREncode_CloseMap(&ECtx);
607 QCBOREncode_CloseMap(&ECtx);
608
609 // Extended simple values (these are not standard...)
610 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700611 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700612 QCBOREncode_AddType7_2(&ECtx, "s1", QCBOR_NO_INT_LABEL, 0, 255);
613 QCBOREncode_AddType7_2(&ECtx, "s2", QCBOR_NO_INT_LABEL, 0, 0);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700614 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700615 QCBOREncode_AddType7_2(&ECtx, "s3", QCBOR_NO_INT_LABEL, 0, 33);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700616 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700617 QCBOREncode_AddType7_2(&ECtx, NULL, 88378374, 0, 255);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700618 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700619 QCBOREncode_AddType7_2(&ECtx, NULL, 89, 0, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 QCBOREncode_CloseMap(&ECtx);
621
622
623 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530624 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 +0530625 UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
627 QCBOREncode_OpenMap(&ECtx);
628 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
629 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
630 QCBOREncode_CloseMap(&ECtx);
631
632
633 // Bool
634 QCBOREncode_AddBool(&ECtx, true);
635 QCBOREncode_AddBool(&ECtx, false);
636 QCBOREncode_OpenMap(&ECtx);
637 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
638 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
639 QCBOREncode_CloseMap(&ECtx);
640
641
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530642 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530643 UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
645 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
646 QCBOREncode_OpenMap(&ECtx);
647 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
648 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
649 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
650 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
651 QCBOREncode_CloseMap(&ECtx);
652
653 QCBOREncode_CloseArray(&ECtx);
654
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530655 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656
657 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
658 nReturn = -1;
659 goto Done;
660 }
661
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530662 if(CheckResults(Enc, spExpectedEncodedAll))
663 nReturn = -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800664
665Done:
666 return nReturn;
667}
668
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530669/*
670 98 2F # array(47)
671 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
672 3B 0000000100000000 # negative(4294967296)
673 3A FFFFFFFF # negative(4294967295)
674 3A FFFFFFFE # negative(4294967294)
675 3A FFFFFFFD # negative(4294967293)
676 3A 7FFFFFFF # negative(2147483647)
677 3A 7FFFFFFE # negative(2147483646)
678 3A 00010001 # negative(65537)
679 3A 00010000 # negative(65536)
680 39 FFFF # negative(65535)
681 39 FFFE # negative(65534)
682 39 FFFD # negative(65533)
683 39 0100 # negative(256)
684 38 FF # negative(255)
685 38 FE # negative(254)
686 38 FD # negative(253)
687 38 18 # negative(24)
688 37 # negative(23)
689 36 # negative(22)
690 20 # negative(0)
691 00 # unsigned(0)
692 00 # unsigned(0)
693 01 # unsigned(1)
694 16 # unsigned(22)
695 17 # unsigned(23)
696 18 18 # unsigned(24)
697 18 19 # unsigned(25)
698 18 1A # unsigned(26)
699 18 FE # unsigned(254)
700 18 FF # unsigned(255)
701 19 0100 # unsigned(256)
702 19 0101 # unsigned(257)
703 19 FFFE # unsigned(65534)
704 19 FFFF # unsigned(65535)
705 1A 00010000 # unsigned(65536)
706 1A 00010001 # unsigned(65537)
707 1A 00010002 # unsigned(65538)
708 1A 7FFFFFFF # unsigned(2147483647)
709 1A 7FFFFFFF # unsigned(2147483647)
710 1A 80000000 # unsigned(2147483648)
711 1A 80000001 # unsigned(2147483649)
712 1A FFFFFFFE # unsigned(4294967294)
713 1A FFFFFFFF # unsigned(4294967295)
714 1B 0000000100000000 # unsigned(4294967296)
715 1B 0000000100000001 # unsigned(4294967297)
716 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
717 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
718 */
719static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800720 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
721 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
722 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
723 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
724 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
725 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
726 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
727 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
728 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
729 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
730 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
731 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
732 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
733 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
734 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
735 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
736 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
737 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
738 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
739 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
740 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
741 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
742 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
743 0xff, 0xff};
744
745/*
746
747 Test the generation of integers. This also ends up testing
748 encoding of all the different lengths. It encodes integers
749 of many lengths and values, especially around the boundaries
750 for different types of integers. It compares the output
751 to expected values generated from http://cbor.me.
752
753 */
754int IntegerValuesTest1()
755{
756 QCBOREncodeContext ECtx;
757 int nReturn = 0;
758
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530759 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760 QCBOREncode_OpenArray(&ECtx);
761
762 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
763 QCBOREncode_AddInt64(&ECtx, -4294967297);
764 QCBOREncode_AddInt64(&ECtx, -4294967296);
765 QCBOREncode_AddInt64(&ECtx, -4294967295);
766 QCBOREncode_AddInt64(&ECtx, -4294967294);
767 QCBOREncode_AddInt64(&ECtx, -2147483648);
768 QCBOREncode_AddInt64(&ECtx, -2147483647);
769 QCBOREncode_AddInt64(&ECtx, -65538);
770 QCBOREncode_AddInt64(&ECtx, -65537);
771 QCBOREncode_AddInt64(&ECtx, -65536);
772 QCBOREncode_AddInt64(&ECtx, -65535);
773 QCBOREncode_AddInt64(&ECtx, -65534);
774 QCBOREncode_AddInt64(&ECtx, -257);
775 QCBOREncode_AddInt64(&ECtx, -256);
776 QCBOREncode_AddInt64(&ECtx, -255);
777 QCBOREncode_AddInt64(&ECtx, -254);
778 QCBOREncode_AddInt64(&ECtx, -25);
779 QCBOREncode_AddInt64(&ECtx, -24);
780 QCBOREncode_AddInt64(&ECtx, -23);
781 QCBOREncode_AddInt64(&ECtx, -1);
782 QCBOREncode_AddInt64(&ECtx, 0);
783 QCBOREncode_AddUInt64(&ECtx, 0ULL);
784 QCBOREncode_AddInt64(&ECtx, 1);
785 QCBOREncode_AddInt64(&ECtx, 22);
786 QCBOREncode_AddInt64(&ECtx, 23);
787 QCBOREncode_AddInt64(&ECtx, 24);
788 QCBOREncode_AddInt64(&ECtx, 25);
789 QCBOREncode_AddInt64(&ECtx, 26);
790 QCBOREncode_AddInt64(&ECtx, 254);
791 QCBOREncode_AddInt64(&ECtx, 255);
792 QCBOREncode_AddInt64(&ECtx, 256);
793 QCBOREncode_AddInt64(&ECtx, 257);
794 QCBOREncode_AddInt64(&ECtx, 65534);
795 QCBOREncode_AddInt64(&ECtx, 65535);
796 QCBOREncode_AddInt64(&ECtx, 65536);
797 QCBOREncode_AddInt64(&ECtx, 65537);
798 QCBOREncode_AddInt64(&ECtx, 65538);
799 QCBOREncode_AddInt64(&ECtx, 2147483647);
800 QCBOREncode_AddInt64(&ECtx, 2147483647);
801 QCBOREncode_AddInt64(&ECtx, 2147483648);
802 QCBOREncode_AddInt64(&ECtx, 2147483649);
803 QCBOREncode_AddInt64(&ECtx, 4294967294);
804 QCBOREncode_AddInt64(&ECtx, 4294967295);
805 QCBOREncode_AddInt64(&ECtx, 4294967296);
806 QCBOREncode_AddInt64(&ECtx, 4294967297);
807 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
808 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
809
810 QCBOREncode_CloseArray(&ECtx);
811
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530812 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
814 nReturn = -1;
815 }
816
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530817 if(CheckResults(Enc, spExpectedEncodedInts))
818 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819
820 return(nReturn);
821}
822
823
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530824/*
825 85 # array(5)
826 F5 # primitive(21)
827 F4 # primitive(20)
828 F6 # primitive(22)
829 F7 # primitive(23)
830 A1 # map(1)
831 65 # text(5)
832 554E446566 # "UNDef"
833 F7 # primitive(23)
834 */
835static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
837
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800838int SimpleValuesTest1()
839{
840 QCBOREncodeContext ECtx;
841 int nReturn = 0;
842
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530843 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800844 QCBOREncode_OpenArray(&ECtx);
845
846 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
847 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
848 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
849 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
850
851 QCBOREncode_OpenMap(&ECtx);
852
853 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
854 QCBOREncode_CloseMap(&ECtx);
855
856 QCBOREncode_CloseArray(&ECtx);
857
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530858 UsefulBufC ECBOR;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
860 nReturn = -1;
861 }
862
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530863 if(CheckResults(ECBOR, spExpectedEncodedSimple))
864 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865
866 return(nReturn);
867}
868
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530869
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530870/*
871 83 # array(3)
872 C0 # tag(0)
873 74 # text(20)
874 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
875 C1 # tag(1)
876 1A 514B67B0 # unsigned(1363896240)
877 A2 # map(2)
878 78 19 # text(25)
879 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
880 C0 # tag(0)
881 77 # text(23)
882 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
883 62 # text(2)
884 5344 # "SD"
885 C1 # tag(1)
886 19 03E7 # unsigned(999)
887 */
888static const uint8_t spExpectedEncodedDates[] = {
889 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
890 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
891 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
892 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
893 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
894 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
895 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
896 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
897 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800898};
899
900int EncodeDateTest()
901{
902 QCBOREncodeContext ECtx;
903 int nReturn = 0;
904
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530905 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800906
907 QCBOREncode_OpenArray(&ECtx);
908
909
910 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
911 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
912
913
914 QCBOREncode_OpenMap(&ECtx);
915
916 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
917
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
919
920 QCBOREncode_CloseMap(&ECtx);
921
922 QCBOREncode_CloseArray(&ECtx);
923
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530924 UsefulBufC ECBOR;
925
926 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800927 nReturn = -1;
928 }
929
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530930 if(CheckResults(ECBOR, spExpectedEncodedDates))
931 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932
933 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934}
935
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530936
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800937int ArrayNestingTest1()
938{
939 QCBOREncodeContext ECtx;
940 int i;
941 int nReturn = 0;
942
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530943 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800944 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
945 QCBOREncode_OpenArray(&ECtx);
946 }
947 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
948 QCBOREncode_CloseArray(&ECtx);
949 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530950 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800951 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 nReturn = -1;
953 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954
955 return(nReturn);
956}
957
958
959
960int ArrayNestingTest2()
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+1; 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
974 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
976 nReturn = -1;
977 }
978
979 return(nReturn);
980}
981
982
983
984int ArrayNestingTest3()
985{
986 QCBOREncodeContext ECtx;
987 int i;
988 int nReturn = 0;
989
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530990 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800991 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
992 QCBOREncode_OpenArray(&ECtx);
993 }
994 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
995 QCBOREncode_CloseArray(&ECtx);
996 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530997 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
999 nReturn = -1;
1000 }
1001
1002 return(nReturn);
1003}
1004
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001005
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301006/*
1007 81 # array(1)
1008 81 # array(1)
1009 81 # array(1)
1010 81 # array(1)
1011 80 # array(0)
1012*/
1013static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001014
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301016/*
1017 82 # array(2)
1018 81 # array(1)
1019 81 # array(1)
1020 81 # array(1)
1021 81 # array(1)
1022 80 # array(0)
1023 98 2F # array(47)
1024 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1025 3B 0000000100000000 # negative(4294967296)
1026 3A FFFFFFFF # negative(4294967295)
1027 3A FFFFFFFE # negative(4294967294)
1028 3A FFFFFFFD # negative(4294967293)
1029 3A 7FFFFFFF # negative(2147483647)
1030 3A 7FFFFFFE # negative(2147483646)
1031 3A 00010001 # negative(65537)
1032 3A 00010000 # negative(65536)
1033 39 FFFF # negative(65535)
1034 39 FFFE # negative(65534)
1035 39 FFFD # negative(65533)
1036 39 0100 # negative(256)
1037 38 FF # negative(255)
1038 38 FE # negative(254)
1039 38 FD # negative(253)
1040 38 18 # negative(24)
1041 37 # negative(23)
1042 36 # negative(22)
1043 20 # negative(0)
1044 00 # unsigned(0)
1045 00 # unsigned(0)
1046 01 # unsigned(1)
1047 16 # unsigned(22)
1048 17 # unsigned(23)
1049 18 18 # unsigned(24)
1050 18 19 # unsigned(25)
1051 18 1A # unsigned(26)
1052 18 FE # unsigned(254)
1053 18 FF # unsigned(255)
1054 19 0100 # unsigned(256)
1055 19 0101 # unsigned(257)
1056 19 FFFE # unsigned(65534)
1057 19 FFFF # unsigned(65535)
1058 1A 00010000 # unsigned(65536)
1059 1A 00010001 # unsigned(65537)
1060 1A 00010002 # unsigned(65538)
1061 1A 7FFFFFFF # unsigned(2147483647)
1062 1A 7FFFFFFF # unsigned(2147483647)
1063 1A 80000000 # unsigned(2147483648)
1064 1A 80000001 # unsigned(2147483649)
1065 1A FFFFFFFE # unsigned(4294967294)
1066 1A FFFFFFFF # unsigned(4294967295)
1067 1B 0000000100000000 # unsigned(4294967296)
1068 1B 0000000100000001 # unsigned(4294967297)
1069 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1070 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1071 */
1072static const uint8_t spEncodeRawExpected[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1074 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1075 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1076 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1077 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1078 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1079 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1080 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1081 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1082 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1083 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1084 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1085 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1086 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1087 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1088 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1089 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1090 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1091 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1092 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1093 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1094 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1095 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1096 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1097
1098
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099int EncodeRawTest()
1100{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001101 QCBOREncodeContext ECtx;
1102
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301103 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001104 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301105 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1106 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001107 QCBOREncode_CloseArray(&ECtx);
1108
1109 UsefulBufC EncodedRawTest;
1110
1111 if(QCBOREncode_Finish2(&ECtx, &EncodedRawTest)) {
1112 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113 }
1114
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301115 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001116 return -5;
1117 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001118
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001119 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001120}
1121
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301122/*
1123 This returns a pointer to spBigBuf
1124 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1126{
1127 QCBOREncodeContext ECtx;
1128 int nReturn = -1;
1129
1130 *pEncoded = NULL;
1131 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301132 size_t uFirstSizeEstimate = 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133
1134 // loop runs CBOR encoding twice. First with no buffer to
1135 // calucate the length so buffer can be allocated correctly,
1136 // and last with the buffer to do the actual encoding
1137 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301138 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001139 QCBOREncode_OpenMap(&ECtx);
1140 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1141 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1142 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1143 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1144 QCBOREncode_CloseArray(&ECtx);
1145 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1146 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1147 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1148 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1149 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1150 QCBOREncode_CloseMap(&ECtx);
1151 QCBOREncode_CloseMap(&ECtx);
1152
1153
1154 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
1155 goto Done;
1156 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301157 if(uFirstSizeEstimate != *pEncodedLen) {
1158 nReturn = 1;
1159 } else {
1160 nReturn = 0;
1161 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 goto Done;
1163 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301164 *pEncoded = spBigBuf;
1165 uFirstSizeEstimate = *pEncodedLen;
1166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001167 } while(1);
1168
1169 Done:
1170 return(nReturn);
1171}
1172
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301173/*
1174 A3 # map(3)
1175 6D # text(13)
1176 666972737420696E7465676572 # "first integer"
1177 18 2A # unsigned(42)
1178 77 # text(23)
1179 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1180 82 # array(2)
1181 67 # text(7)
1182 737472696E6731 # "string1"
1183 67 # text(7)
1184 737472696E6732 # "string2"
1185 6C # text(12)
1186 6D617020696E2061206D6170 # "map in a map"
1187 A4 # map(4)
1188 67 # text(7)
1189 62797465732031 # "bytes 1"
1190 44 # bytes(4)
1191 78787878 # "xxxx"
1192 67 # text(7)
1193 62797465732032 # "bytes 2"
1194 44 # bytes(4)
1195 79797979 # "yyyy"
1196 6B # text(11)
1197 616E6F7468657220696E74 # "another int"
1198 18 62 # unsigned(98)
1199 66 # text(6)
1200 746578742032 # "text 2"
1201 78 1E # text(30)
1202 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1203 */
1204static const uint8_t spValidMapEncoded[] = {
1205 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1206 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1207 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1208 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1209 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1210 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1211 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1212 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1213 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1214 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1215 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1216 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1217 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1218 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1219 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1220 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221
1222
1223int MapEncodeTest()
1224{
1225 uint8_t *pEncodedMaps;
1226 size_t nEncodedMapLen;
1227
1228 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301229 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001230 }
1231
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001232 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301233 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1234 nReturn = 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235
1236 return(nReturn);
1237}
1238
1239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001240/*
1241 @brief Encode the RTIC results
1242
1243 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1244 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1245 @param[in] szAlexString Diagnostic code.
1246 @param[in[ pOut Buffer to put the result in
1247 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1248
1249 @return
1250 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1251
1252 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1253 short an error will be returned. This function will never write off the end
1254 of the buffer passed to it.
1255
1256 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1257 length of the encoded CBOR.
1258
1259 */
1260
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301261static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001262{
1263 // Buffer that the result will be written in to
1264 // It is fixed size and small that a stack variable will be fine
1265 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1266
1267 // Context for the encoder
1268 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301269 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001270
1271 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1272 // Contents are label / value pairs
1273 QCBOREncode_OpenMap(&ECtx);
1274
1275 { // Brace / indention just to show CBOR encoding nesting
1276
1277 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1278 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1279
1280 // Add the diagnostic code
1281 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1282
1283 // Add a time stamp
1284 if(time) {
1285 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1286 }
1287
1288 // Add the diagnostic code
1289 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1290
1291 // Open a subordinate map for telemtry data
1292 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1293
1294 { // Brace / indention just to show CBOR encoding nesting
1295
1296 // Add a few fake integers and buffers for now.
1297 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1298
1299 // Add a few fake integers and buffers for now.
1300 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1301
1302 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301303 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001304 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1305
1306 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1307 }
1308 }
1309
1310 // Close the telemetry map
1311 QCBOREncode_CloseMap(&ECtx);
1312
1313 // Close the map
1314 QCBOREncode_CloseMap(&ECtx);
1315
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301316 UsefulBufC Result;
1317
1318 QCBOREncode_Finish2(&ECtx, &Result);
1319
1320 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001321}
1322
1323
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301324/*
1325 A5 # map(5)
1326 69 # text(9)
1327 696E74656772697479 # "integrity"
1328 F4 # primitive(20)
1329 64 # text(4)
1330 74797065 # "type"
1331 66 # text(6)
1332 726563656E74 # "recent"
1333 64 # text(4)
1334 74696D65 # "time"
1335 C1 # tag(1)
1336 1A 580D4172 # unsigned(1477263730)
1337 64 # text(4)
1338 64696167 # "diag"
1339 6A # text(10)
1340 30784131654335303031 # "0xA1eC5001"
1341 69 # text(9)
1342 74656C656D65747279 # "telemetry"
1343 A3 # map(3)
1344 69 # text(9)
1345 53686F652053697A65 # "Shoe Size"
1346 0C # unsigned(12)
1347 62 # text(2)
1348 4951 # "IQ"
1349 1A FFFFFFFF # unsigned(4294967295)
1350 77 # text(23)
1351 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1352 48 # bytes(8)
1353 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1354 */
1355static const uint8_t spExpectedRTIC[] = {
1356 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1357 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1358 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1359 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1360 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1361 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1362 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1363 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1364 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1365 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1366 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1367 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368
1369
1370int RTICResultsTest()
1371{
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301372 UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
1373 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301374 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301375 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301379 if(CheckResults(Encoded, spExpectedRTIC)) {
1380 return -2;
1381 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382
1383 return 0;
1384}
1385
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301386
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301387/*
1388 82 # array(2)
1389 19 01C3 # unsigned(451)
1390 43 # bytes(3)
1391 1901D2 # "\x19\x01\xD2"
1392*/
1393static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301394
Laurence Lundblade684aec22018-10-12 19:33:53 +08001395/*
1396 Very basic bstr wrapping test
1397 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301398int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001400 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301402 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403
Laurence Lundblade684aec22018-10-12 19:33:53 +08001404 QCBOREncode_OpenArray(&EC);
1405 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406
Laurence Lundblade684aec22018-10-12 19:33:53 +08001407 QCBOREncode_BstrWrap(&EC);
1408 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001409
Laurence Lundblade684aec22018-10-12 19:33:53 +08001410 UsefulBufC Wrapped;
1411 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412
Laurence Lundblade684aec22018-10-12 19:33:53 +08001413 QCBOREncode_CloseArray(&EC);
1414
1415 UsefulBufC Encoded;
1416 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1417 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301420 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001421 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 }
1423
1424 return 0;
1425}
1426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001427
1428
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301429int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001430{
1431 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001432 QCBOREncodeContext EC;
1433
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301434 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001435
1436 QCBOREncode_OpenArray(&EC);
1437 QCBOREncode_AddUInt64(&EC, 451);
1438
1439 QCBOREncode_BstrWrap(&EC);
1440 QCBOREncode_AddUInt64(&EC, 466);
1441 QCBOREncode_OpenArray(&EC);
1442
1443 UsefulBufC Wrapped;
1444 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1445
1446 QCBOREncode_CloseArray(&EC);
1447
1448 UsefulBufC Encoded2;
1449 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
1450 return -1;
1451 }
1452
1453 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301454 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001455 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1456 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
1457 return -2;
1458 }
1459
1460 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301461 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001462 for(int i = 1; i < 18; i++) {
1463 QCBOREncode_BstrWrap(&EC);
1464 }
1465 QCBOREncode_AddBool(&EC, true);
1466
1467 for(int i = 1; i < 18; i++) {
1468 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1469 }
1470
1471 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1472 return -3;
1473 }
1474
1475 return 0;
1476}
1477
1478
1479
1480// Part of bstr_wrap_nest_test
1481/*
1482 83 array with three
1483 53 byte string with 19 bytes
1484 01 #1
1485 50 byte string with 16 bytes
1486 02
1487 4D byte string with 13 bytes
1488 03
1489 4A byte string with 10 bytes
1490 04
1491 47 byte string with 7 bytes
1492 05
1493 44 byte string with 4 bytes
1494 06
1495 41 byte string with 1 byte
1496 07
1497 01
1498 02
1499 03
1500 04
1501 05
1502 06
1503 07
1504 A2 map with two items
1505 18 20 label for byte string
1506 54 byte string of length 20
1507 82 Array with two items
1508 10 The integer value 10
1509 A2 map with two items
1510 18 21 label for byte string
1511 44 byte string with 4 bytes
1512 81 array with 1 item
1513 11 integer value 11
1514 18 30 integer value 30
1515 18 40 integer label 40
1516 65 68 65 6C 6C 6F text string hello
1517 18 31 integer value 31
1518 18 41 integer label 41
1519 65 68 65 6C 6C 6F text string hello
1520
1521
1522 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301523
1524
1525/*
1526 83 # array(3)
1527 56 # bytes(22)
1528 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1529 07 # unsigned(7)
1530 A2 # map(2)
1531 18 20 # unsigned(32)
1532 54 # bytes(20)
1533 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1534 18 41 # unsigned(65)
1535 65 # text(5)
1536 68656C6C6F # "hello"
1537 */
1538static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001539{
1540 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1541 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1542 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1543 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1544 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1545 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1546 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1547 0x6F
1548};
1549
1550// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301551static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001552{
1553 int nReturn;
1554 QCBORDecodeContext DC;
1555 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1556
1557 QCBORItem Item;
1558 nReturn = QCBORDecode_GetNext(&DC, &Item);
1559 if(nReturn) {
1560 return -11;
1561 }
1562 if(Item.uDataType != QCBOR_TYPE_INT64) {
1563 return -12;
1564 }
1565
1566 nReturn = QCBORDecode_GetNext(&DC, &Item);
1567 if(nReturn == QCBOR_ERR_HIT_END) {
1568 return 0;
1569 }
1570 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1571 return -13;
1572 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301573 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001574 if(nReturn) {
1575 return nReturn;
1576 }
1577
1578 nReturn = QCBORDecode_GetNext(&DC, &Item);
1579 if(nReturn) {
1580 return -14;
1581 }
1582 if(Item.uDataType != QCBOR_TYPE_INT64) {
1583 return -15;
1584 }
1585
1586 if(QCBORDecode_Finish(&DC)) {
1587 return -16;
1588 }
1589
1590 return 0;
1591}
1592
1593// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301594static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001595{
1596 int nReturn;
1597 QCBORDecodeContext DC;
1598 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1599
1600 QCBORItem Item;
1601 nReturn = QCBORDecode_GetNext(&DC, &Item);
1602 if(nReturn) {
1603 return -11;
1604 }
1605 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1606 return -12;
1607 }
1608
1609 nReturn = QCBORDecode_GetNext(&DC, &Item);
1610 if(nReturn) {
1611 return -11;
1612 }
1613 if(Item.uDataType != QCBOR_TYPE_INT64) {
1614 return -12;
1615 }
1616
1617 nReturn = QCBORDecode_GetNext(&DC, &Item);
1618 if(nReturn) {
1619 return -11;
1620 }
1621 if(Item.uDataType != QCBOR_TYPE_MAP) {
1622 return 0;
1623 }
1624
1625 nReturn = QCBORDecode_GetNext(&DC, &Item);
1626 if(nReturn) {
1627 return -11;
1628 }
1629 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1630 return -13;
1631 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301632 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001633 if(nReturn) {
1634 return nReturn;
1635 }
1636
1637 nReturn = QCBORDecode_GetNext(&DC, &Item);
1638 if(nReturn) {
1639 return -11;
1640 }
1641 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1642 return -12;
1643 }
1644 nReturn = QCBORDecode_GetNext(&DC, &Item);
1645 if(nReturn) {
1646 return -11;
1647 }
1648 if(Item.uDataType != QCBOR_TYPE_INT64) {
1649 return -12;
1650 }
1651
1652 if(QCBORDecode_Finish(&DC)) {
1653 return -16;
1654 }
1655
1656 return 0;
1657}
1658
1659
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301660int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001661{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001662 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301663 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001664
1665 // ---- Make a complicated nested CBOR structure ---
1666 QCBOREncode_OpenArray(&EC);
1667
1668 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1669 QCBOREncode_BstrWrap(&EC);
1670 QCBOREncode_AddUInt64(&EC, i);
1671 }
1672
1673 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1674 QCBOREncode_CloseBstrWrap(&EC, NULL);
1675 QCBOREncode_AddUInt64(&EC, i);
1676 }
1677
1678 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1679 QCBOREncode_OpenMap(&EC);
1680 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1681 QCBOREncode_OpenArray(&EC);
1682 QCBOREncode_AddUInt64(&EC, i+0x10);
1683 }
1684
1685 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1686 QCBOREncode_CloseArray(&EC);
1687 QCBOREncode_AddUInt64(&EC, i+0x30);
1688 QCBOREncode_CloseBstrWrap(&EC, NULL);
1689 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1690 QCBOREncode_CloseMap(&EC);
1691 }
1692 QCBOREncode_CloseArray(&EC);
1693
1694 UsefulBufC Encoded;
1695 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1696 return -1;
1697 }
1698
1699 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301700 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001701 return -25;
1702 }
1703
1704
1705 // ---- Decode it and see if it is OK ------
1706 QCBORDecodeContext DC;
1707 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1708
1709 QCBORItem Item;
1710 QCBORDecode_GetNext(&DC, &Item);
1711 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1712 return -2;
1713 }
1714
1715 QCBORDecode_GetNext(&DC, &Item);
1716 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1717 return -3;
1718 }
1719
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301720 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001721 if(nReturn) {
1722 return nReturn;
1723 }
1724
1725 nReturn = QCBORDecode_GetNext(&DC, &Item);
1726 if(nReturn) {
1727 return -11;
1728 }
1729 if(Item.uDataType != QCBOR_TYPE_INT64) {
1730 return -12;
1731 }
1732
1733 QCBORDecode_GetNext(&DC, &Item);
1734 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1735 return -2;
1736 }
1737
1738 QCBORDecode_GetNext(&DC, &Item);
1739 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1740 return -3;
1741 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301742 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001743 if(nReturn) {
1744 return nReturn;
1745 }
1746
1747 nReturn = QCBORDecode_GetNext(&DC, &Item);
1748 if(nReturn) {
1749 return -11;
1750 }
1751 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1752 return -12;
1753 }
1754
1755 if(QCBORDecode_Finish(&DC)) {
1756 return -16;
1757 }
1758
1759 return 0;
1760}
1761
1762
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301763static const uint8_t spSignature[] = {
1764 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1765 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1766 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1767 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1768 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1769 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1770 0x45, 0xca, 0xcb, 0x36};
1771
1772/*
1773 D2 # tag(18)
1774 84 # array(4)
1775 43 # bytes(3)
1776 A10126 # "\xA1\x01&"
1777 A1 # map(1)
1778 04 # unsigned(4)
1779 42 # bytes(2)
1780 3131 # "11"
1781 54 # bytes(20)
1782 546869732069732074686520636F6E74656E742E # "This is the content."
1783 58 40 # bytes(64)
1784 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"
1785 */
1786static const uint8_t spExpected[] = {
1787 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1788 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1789 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1790 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1791 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1792 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1793 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1794 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1795 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1796 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1797
Laurence Lundblade684aec22018-10-12 19:33:53 +08001798/*
1799 this corresponds exactly to the example in RFC 8152
1800 section C.2.1. This doesn't actually verify the signature
1801 though that would be nice as it would make the test
1802 really good. That would require bring in ECDSA crypto
1803 to this test.
1804 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301805int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001806{
1807 // All of this is from RFC 8152 C.2.1
1808 const char *szKid = "11";
1809 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1810 const char *szPayload = "This is the content.";
1811 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301812 static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301813 UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301814
Laurence Lundblade684aec22018-10-12 19:33:53 +08001815 // It would be good to compare this to the output from
1816 // a COSE implementation like COSE-C. It has been checked
1817 // against the CBOR playground.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301818 UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001819
Laurence Lundblade684aec22018-10-12 19:33:53 +08001820 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301821 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001822
1823 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladecafcfe12018-10-31 21:59:50 +07001824 QCBOREncode_AddTag(&EC, 18); // TODO: replace with constant
1825 QCBOREncode_OpenArray_2(&EC, NULL, QCBOR_NO_INT_LABEL); // TODO: _2
Laurence Lundblade684aec22018-10-12 19:33:53 +08001826
1827 // Add protected headers
1828 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1829
1830 // Empty map with unprotected headers
1831 QCBOREncode_OpenMap(&EC);
1832 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1833 QCBOREncode_CloseMap(&EC);
1834
1835 // The payload
1836 UsefulBufC WrappedPayload;
1837 QCBOREncode_BstrWrap(&EC);
1838 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1839 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1840
1841 // Check we got back the actual payload expected
1842 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1843 return -1;
1844 }
1845
1846 // The signature
1847 QCBOREncode_AddBytes(&EC, Signature);
1848 QCBOREncode_CloseArray(&EC);
1849
1850 // Finish and check the results
1851 UsefulBufC COSE_Sign1;
1852 if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) {
1853 return -2;
1854 }
1855
1856 // 98 is the size from RFC 8152 C.2.1
1857 if(COSE_Sign1.len != 98) {
1858 return -3;
1859 }
1860
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301861 if(CheckResults(COSE_Sign1, spExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001862 return -4;
1863 }
1864
1865 return 0;
1866}
1867