blob: 50531c2789bbe6d4272c401bccf85e78d85e03ca [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 Lundbladedc6e28e2018-10-11 19:19:27 +053075#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
76#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{
81 int i;
82 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 Lundbladedc6e28e2018-10-11 19:19:27 +053094 int i;
95 for(i = 0; i < U1.len; i++) {
96 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
97 printf("%d 0x%x 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
98 return 1;
99 }
100 }
101 return 0;
102
103}
104
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530105#define CheckResults(Enc, Expected) \
106 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530107
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530108#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 Lundblade369b90a2018-10-22 02:04:37 +0530113#endif
114
115
116
117// 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
511 QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
512 QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 77689989909);
513 QCBOREncode_AddUInt64(&ECtx,0);
514 QCBOREncode_AddInt64(&ECtx, -44);
515
516 // ints that go in maps
517 QCBOREncode_OpenMap(&ECtx);
518 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
519 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
520 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
521 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
522 QCBOREncode_CloseMap(&ECtx);
523
524 // floats and doubles
525 QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
526 QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
527 QCBOREncode_AddFloat(&ECtx, 1);
528 QCBOREncode_AddDouble(&ECtx, 1);
529
530 // floats and doubles that go in map
531 QCBOREncode_OpenMap(&ECtx);
532 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
533 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
534 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
535 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
536 QCBOREncode_CloseMap(&ECtx);
537
538 // Epoch Date
539 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
540
541 // Epoch date with labels
542 QCBOREncode_OpenMap(&ECtx);
543 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
544 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
545 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
546 QCBOREncode_CloseMap(&ECtx);
547
548 // Binary blobs
549 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
550
551 // binary blobs in maps
552 QCBOREncode_OpenMap(&ECtx);
553 QCBOREncode_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
554 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
555 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
556 QCBOREncode_CloseMap(&ECtx);
557
558 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530559 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530561 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"));
562 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
563 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530564 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565
566 // text blobs in maps
567 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530568 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
569 QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, UsefulBuf_FROM_SZ_LITERAL("foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800570 QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530571 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
573 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530574 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
575 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
576 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
577 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
578 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
579 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530580 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
581 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582 QCBOREncode_CloseMap(&ECtx);
583
584 // Date strings
585 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
586 QCBOREncode_OpenMap(&ECtx);
587 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
588 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
589 QCBOREncode_CloseMap(&ECtx);
590
591 // true / false ...
592 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
593 QCBOREncode_OpenMap(&ECtx);
594 QCBOREncode_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
595 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
596 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
597 QCBOREncode_CloseMap(&ECtx);
598
599 // opening an array
600 QCBOREncode_OpenArray(&ECtx);
601 QCBOREncode_CloseArray(&ECtx);
602
603 // opening arrays in a map
604 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530605 QCBOREncode_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 QCBOREncode_CloseArray(&ECtx);
607 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
608 QCBOREncode_CloseArray(&ECtx);
609 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
610 QCBOREncode_CloseArray(&ECtx);
611 QCBOREncode_CloseMap(&ECtx);
612
613 // opening maps with labels and tagging
614 QCBOREncode_OpenMap(&ECtx);
615 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
616 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530617 QCBOREncode_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 QCBOREncode_CloseMap(&ECtx);
619 QCBOREncode_CloseMap(&ECtx);
620 QCBOREncode_CloseMap(&ECtx);
621 QCBOREncode_CloseMap(&ECtx);
622
623 // Extended simple values (these are not standard...)
624 QCBOREncode_OpenMap(&ECtx);
625 QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
626 QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
627 QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
628 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
629 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
630 QCBOREncode_CloseMap(&ECtx);
631
632
633 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530634 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 +0530635 UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800636 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
637 QCBOREncode_OpenMap(&ECtx);
638 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
639 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
640 QCBOREncode_CloseMap(&ECtx);
641
642
643 // Bool
644 QCBOREncode_AddBool(&ECtx, true);
645 QCBOREncode_AddBool(&ECtx, false);
646 QCBOREncode_OpenMap(&ECtx);
647 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
648 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
649 QCBOREncode_CloseMap(&ECtx);
650
651
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530652 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530653 UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
655 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
656 QCBOREncode_OpenMap(&ECtx);
657 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
658 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
659 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
660 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
661 QCBOREncode_CloseMap(&ECtx);
662
663 QCBOREncode_CloseArray(&ECtx);
664
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530665 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666
667 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
668 nReturn = -1;
669 goto Done;
670 }
671
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530672 if(CheckResults(Enc, spExpectedEncodedAll))
673 nReturn = -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674
675Done:
676 return nReturn;
677}
678
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530679/*
680 98 2F # array(47)
681 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
682 3B 0000000100000000 # negative(4294967296)
683 3A FFFFFFFF # negative(4294967295)
684 3A FFFFFFFE # negative(4294967294)
685 3A FFFFFFFD # negative(4294967293)
686 3A 7FFFFFFF # negative(2147483647)
687 3A 7FFFFFFE # negative(2147483646)
688 3A 00010001 # negative(65537)
689 3A 00010000 # negative(65536)
690 39 FFFF # negative(65535)
691 39 FFFE # negative(65534)
692 39 FFFD # negative(65533)
693 39 0100 # negative(256)
694 38 FF # negative(255)
695 38 FE # negative(254)
696 38 FD # negative(253)
697 38 18 # negative(24)
698 37 # negative(23)
699 36 # negative(22)
700 20 # negative(0)
701 00 # unsigned(0)
702 00 # unsigned(0)
703 01 # unsigned(1)
704 16 # unsigned(22)
705 17 # unsigned(23)
706 18 18 # unsigned(24)
707 18 19 # unsigned(25)
708 18 1A # unsigned(26)
709 18 FE # unsigned(254)
710 18 FF # unsigned(255)
711 19 0100 # unsigned(256)
712 19 0101 # unsigned(257)
713 19 FFFE # unsigned(65534)
714 19 FFFF # unsigned(65535)
715 1A 00010000 # unsigned(65536)
716 1A 00010001 # unsigned(65537)
717 1A 00010002 # unsigned(65538)
718 1A 7FFFFFFF # unsigned(2147483647)
719 1A 7FFFFFFF # unsigned(2147483647)
720 1A 80000000 # unsigned(2147483648)
721 1A 80000001 # unsigned(2147483649)
722 1A FFFFFFFE # unsigned(4294967294)
723 1A FFFFFFFF # unsigned(4294967295)
724 1B 0000000100000000 # unsigned(4294967296)
725 1B 0000000100000001 # unsigned(4294967297)
726 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
727 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
728 */
729static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800730 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
731 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
732 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
733 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
734 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
735 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
736 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
737 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
738 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
739 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
740 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
741 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
742 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
743 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
744 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
745 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
746 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
747 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
748 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
749 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
750 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
751 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
752 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
753 0xff, 0xff};
754
755/*
756
757 Test the generation of integers. This also ends up testing
758 encoding of all the different lengths. It encodes integers
759 of many lengths and values, especially around the boundaries
760 for different types of integers. It compares the output
761 to expected values generated from http://cbor.me.
762
763 */
764int IntegerValuesTest1()
765{
766 QCBOREncodeContext ECtx;
767 int nReturn = 0;
768
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530769 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 QCBOREncode_OpenArray(&ECtx);
771
772 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
773 QCBOREncode_AddInt64(&ECtx, -4294967297);
774 QCBOREncode_AddInt64(&ECtx, -4294967296);
775 QCBOREncode_AddInt64(&ECtx, -4294967295);
776 QCBOREncode_AddInt64(&ECtx, -4294967294);
777 QCBOREncode_AddInt64(&ECtx, -2147483648);
778 QCBOREncode_AddInt64(&ECtx, -2147483647);
779 QCBOREncode_AddInt64(&ECtx, -65538);
780 QCBOREncode_AddInt64(&ECtx, -65537);
781 QCBOREncode_AddInt64(&ECtx, -65536);
782 QCBOREncode_AddInt64(&ECtx, -65535);
783 QCBOREncode_AddInt64(&ECtx, -65534);
784 QCBOREncode_AddInt64(&ECtx, -257);
785 QCBOREncode_AddInt64(&ECtx, -256);
786 QCBOREncode_AddInt64(&ECtx, -255);
787 QCBOREncode_AddInt64(&ECtx, -254);
788 QCBOREncode_AddInt64(&ECtx, -25);
789 QCBOREncode_AddInt64(&ECtx, -24);
790 QCBOREncode_AddInt64(&ECtx, -23);
791 QCBOREncode_AddInt64(&ECtx, -1);
792 QCBOREncode_AddInt64(&ECtx, 0);
793 QCBOREncode_AddUInt64(&ECtx, 0ULL);
794 QCBOREncode_AddInt64(&ECtx, 1);
795 QCBOREncode_AddInt64(&ECtx, 22);
796 QCBOREncode_AddInt64(&ECtx, 23);
797 QCBOREncode_AddInt64(&ECtx, 24);
798 QCBOREncode_AddInt64(&ECtx, 25);
799 QCBOREncode_AddInt64(&ECtx, 26);
800 QCBOREncode_AddInt64(&ECtx, 254);
801 QCBOREncode_AddInt64(&ECtx, 255);
802 QCBOREncode_AddInt64(&ECtx, 256);
803 QCBOREncode_AddInt64(&ECtx, 257);
804 QCBOREncode_AddInt64(&ECtx, 65534);
805 QCBOREncode_AddInt64(&ECtx, 65535);
806 QCBOREncode_AddInt64(&ECtx, 65536);
807 QCBOREncode_AddInt64(&ECtx, 65537);
808 QCBOREncode_AddInt64(&ECtx, 65538);
809 QCBOREncode_AddInt64(&ECtx, 2147483647);
810 QCBOREncode_AddInt64(&ECtx, 2147483647);
811 QCBOREncode_AddInt64(&ECtx, 2147483648);
812 QCBOREncode_AddInt64(&ECtx, 2147483649);
813 QCBOREncode_AddInt64(&ECtx, 4294967294);
814 QCBOREncode_AddInt64(&ECtx, 4294967295);
815 QCBOREncode_AddInt64(&ECtx, 4294967296);
816 QCBOREncode_AddInt64(&ECtx, 4294967297);
817 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
818 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
819
820 QCBOREncode_CloseArray(&ECtx);
821
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530822 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800823 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
824 nReturn = -1;
825 }
826
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530827 if(CheckResults(Enc, spExpectedEncodedInts))
828 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829
830 return(nReturn);
831}
832
833
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530834/*
835 85 # array(5)
836 F5 # primitive(21)
837 F4 # primitive(20)
838 F6 # primitive(22)
839 F7 # primitive(23)
840 A1 # map(1)
841 65 # text(5)
842 554E446566 # "UNDef"
843 F7 # primitive(23)
844 */
845static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800846 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
847
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800848int SimpleValuesTest1()
849{
850 QCBOREncodeContext ECtx;
851 int nReturn = 0;
852
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530853 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800854 QCBOREncode_OpenArray(&ECtx);
855
856 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
857 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
858 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
859 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
860
861 QCBOREncode_OpenMap(&ECtx);
862
863 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
864 QCBOREncode_CloseMap(&ECtx);
865
866 QCBOREncode_CloseArray(&ECtx);
867
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530868 UsefulBufC ECBOR;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
870 nReturn = -1;
871 }
872
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530873 if(CheckResults(ECBOR, spExpectedEncodedSimple))
874 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875
876 return(nReturn);
877}
878
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530879
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530880/*
881 83 # array(3)
882 C0 # tag(0)
883 74 # text(20)
884 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
885 C1 # tag(1)
886 1A 514B67B0 # unsigned(1363896240)
887 A2 # map(2)
888 78 19 # text(25)
889 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
890 C0 # tag(0)
891 77 # text(23)
892 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
893 62 # text(2)
894 5344 # "SD"
895 C1 # tag(1)
896 19 03E7 # unsigned(999)
897 */
898static const uint8_t spExpectedEncodedDates[] = {
899 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
900 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
901 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
902 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
903 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
904 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
905 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
906 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
907 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908};
909
910int EncodeDateTest()
911{
912 QCBOREncodeContext ECtx;
913 int nReturn = 0;
914
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530915 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916
917 QCBOREncode_OpenArray(&ECtx);
918
919
920 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
921 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
922
923
924 QCBOREncode_OpenMap(&ECtx);
925
926 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
927
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800928 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
929
930 QCBOREncode_CloseMap(&ECtx);
931
932 QCBOREncode_CloseArray(&ECtx);
933
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530934 UsefulBufC ECBOR;
935
936 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800937 nReturn = -1;
938 }
939
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530940 if(CheckResults(ECBOR, spExpectedEncodedDates))
941 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800942
943 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800944}
945
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947int ArrayNestingTest1()
948{
949 QCBOREncodeContext ECtx;
950 int i;
951 int nReturn = 0;
952
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530953 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
955 QCBOREncode_OpenArray(&ECtx);
956 }
957 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
958 QCBOREncode_CloseArray(&ECtx);
959 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530960 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800962 nReturn = -1;
963 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964
965 return(nReturn);
966}
967
968
969
970int ArrayNestingTest2()
971{
972 QCBOREncodeContext ECtx;
973 int i;
974 int nReturn = 0;
975
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530976 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
978 QCBOREncode_OpenArray(&ECtx);
979 }
980 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
981 QCBOREncode_CloseArray(&ECtx);
982 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530983
984 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800985 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
986 nReturn = -1;
987 }
988
989 return(nReturn);
990}
991
992
993
994int ArrayNestingTest3()
995{
996 QCBOREncodeContext ECtx;
997 int i;
998 int nReturn = 0;
999
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301000 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1002 QCBOREncode_OpenArray(&ECtx);
1003 }
1004 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1005 QCBOREncode_CloseArray(&ECtx);
1006 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301007 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001008 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
1009 nReturn = -1;
1010 }
1011
1012 return(nReturn);
1013}
1014
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301016/*
1017 81 # array(1)
1018 81 # array(1)
1019 81 # array(1)
1020 81 # array(1)
1021 80 # array(0)
1022*/
1023static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001025// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301026/*
1027 82 # array(2)
1028 81 # array(1)
1029 81 # array(1)
1030 81 # array(1)
1031 81 # array(1)
1032 80 # array(0)
1033 98 2F # array(47)
1034 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1035 3B 0000000100000000 # negative(4294967296)
1036 3A FFFFFFFF # negative(4294967295)
1037 3A FFFFFFFE # negative(4294967294)
1038 3A FFFFFFFD # negative(4294967293)
1039 3A 7FFFFFFF # negative(2147483647)
1040 3A 7FFFFFFE # negative(2147483646)
1041 3A 00010001 # negative(65537)
1042 3A 00010000 # negative(65536)
1043 39 FFFF # negative(65535)
1044 39 FFFE # negative(65534)
1045 39 FFFD # negative(65533)
1046 39 0100 # negative(256)
1047 38 FF # negative(255)
1048 38 FE # negative(254)
1049 38 FD # negative(253)
1050 38 18 # negative(24)
1051 37 # negative(23)
1052 36 # negative(22)
1053 20 # negative(0)
1054 00 # unsigned(0)
1055 00 # unsigned(0)
1056 01 # unsigned(1)
1057 16 # unsigned(22)
1058 17 # unsigned(23)
1059 18 18 # unsigned(24)
1060 18 19 # unsigned(25)
1061 18 1A # unsigned(26)
1062 18 FE # unsigned(254)
1063 18 FF # unsigned(255)
1064 19 0100 # unsigned(256)
1065 19 0101 # unsigned(257)
1066 19 FFFE # unsigned(65534)
1067 19 FFFF # unsigned(65535)
1068 1A 00010000 # unsigned(65536)
1069 1A 00010001 # unsigned(65537)
1070 1A 00010002 # unsigned(65538)
1071 1A 7FFFFFFF # unsigned(2147483647)
1072 1A 7FFFFFFF # unsigned(2147483647)
1073 1A 80000000 # unsigned(2147483648)
1074 1A 80000001 # unsigned(2147483649)
1075 1A FFFFFFFE # unsigned(4294967294)
1076 1A FFFFFFFF # unsigned(4294967295)
1077 1B 0000000100000000 # unsigned(4294967296)
1078 1B 0000000100000001 # unsigned(4294967297)
1079 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1080 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1081 */
1082static const uint8_t spEncodeRawExpected[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1084 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1085 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1086 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1087 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1088 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1089 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1090 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1091 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1092 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1093 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1094 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1095 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1096 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1097 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1098 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1099 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1100 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1101 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1102 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1103 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1104 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1107
1108
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109int EncodeRawTest()
1110{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001111 QCBOREncodeContext ECtx;
1112
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301113 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001114 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301115 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1116 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001117 QCBOREncode_CloseArray(&ECtx);
1118
1119 UsefulBufC EncodedRawTest;
1120
1121 if(QCBOREncode_Finish2(&ECtx, &EncodedRawTest)) {
1122 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001123 }
1124
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301125 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001126 return -5;
1127 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001128
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001129 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001130}
1131
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301132/*
1133 This returns a pointer to spBigBuf
1134 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001135static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1136{
1137 QCBOREncodeContext ECtx;
1138 int nReturn = -1;
1139
1140 *pEncoded = NULL;
1141 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301142 size_t uFirstSizeEstimate = 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143
1144 // loop runs CBOR encoding twice. First with no buffer to
1145 // calucate the length so buffer can be allocated correctly,
1146 // and last with the buffer to do the actual encoding
1147 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301148 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149 QCBOREncode_OpenMap(&ECtx);
1150 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1151 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1152 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1153 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1154 QCBOREncode_CloseArray(&ECtx);
1155 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1156 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1157 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1158 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1159 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1160 QCBOREncode_CloseMap(&ECtx);
1161 QCBOREncode_CloseMap(&ECtx);
1162
1163
1164 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
1165 goto Done;
1166 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301167 if(uFirstSizeEstimate != *pEncodedLen) {
1168 nReturn = 1;
1169 } else {
1170 nReturn = 0;
1171 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001172 goto Done;
1173 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301174 *pEncoded = spBigBuf;
1175 uFirstSizeEstimate = *pEncodedLen;
1176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001177 } while(1);
1178
1179 Done:
1180 return(nReturn);
1181}
1182
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301183/*
1184 A3 # map(3)
1185 6D # text(13)
1186 666972737420696E7465676572 # "first integer"
1187 18 2A # unsigned(42)
1188 77 # text(23)
1189 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1190 82 # array(2)
1191 67 # text(7)
1192 737472696E6731 # "string1"
1193 67 # text(7)
1194 737472696E6732 # "string2"
1195 6C # text(12)
1196 6D617020696E2061206D6170 # "map in a map"
1197 A4 # map(4)
1198 67 # text(7)
1199 62797465732031 # "bytes 1"
1200 44 # bytes(4)
1201 78787878 # "xxxx"
1202 67 # text(7)
1203 62797465732032 # "bytes 2"
1204 44 # bytes(4)
1205 79797979 # "yyyy"
1206 6B # text(11)
1207 616E6F7468657220696E74 # "another int"
1208 18 62 # unsigned(98)
1209 66 # text(6)
1210 746578742032 # "text 2"
1211 78 1E # text(30)
1212 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1213 */
1214static const uint8_t spValidMapEncoded[] = {
1215 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1216 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1217 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1218 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1219 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1220 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1221 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1222 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1223 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1224 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1225 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1226 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1227 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1228 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1229 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1230 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001231
1232
1233int MapEncodeTest()
1234{
1235 uint8_t *pEncodedMaps;
1236 size_t nEncodedMapLen;
1237
1238 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301239 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001240 }
1241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301243 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1244 nReturn = 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001245
1246 return(nReturn);
1247}
1248
1249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250/*
1251 @brief Encode the RTIC results
1252
1253 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1254 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1255 @param[in] szAlexString Diagnostic code.
1256 @param[in[ pOut Buffer to put the result in
1257 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1258
1259 @return
1260 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1261
1262 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1263 short an error will be returned. This function will never write off the end
1264 of the buffer passed to it.
1265
1266 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1267 length of the encoded CBOR.
1268
1269 */
1270
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301271static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001272{
1273 // Buffer that the result will be written in to
1274 // It is fixed size and small that a stack variable will be fine
1275 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1276
1277 // Context for the encoder
1278 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301279 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280
1281 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1282 // Contents are label / value pairs
1283 QCBOREncode_OpenMap(&ECtx);
1284
1285 { // Brace / indention just to show CBOR encoding nesting
1286
1287 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1288 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1289
1290 // Add the diagnostic code
1291 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1292
1293 // Add a time stamp
1294 if(time) {
1295 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1296 }
1297
1298 // Add the diagnostic code
1299 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1300
1301 // Open a subordinate map for telemtry data
1302 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1303
1304 { // Brace / indention just to show CBOR encoding nesting
1305
1306 // Add a few fake integers and buffers for now.
1307 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1308
1309 // Add a few fake integers and buffers for now.
1310 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1311
1312 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301313 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1315
1316 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1317 }
1318 }
1319
1320 // Close the telemetry map
1321 QCBOREncode_CloseMap(&ECtx);
1322
1323 // Close the map
1324 QCBOREncode_CloseMap(&ECtx);
1325
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301326 UsefulBufC Result;
1327
1328 QCBOREncode_Finish2(&ECtx, &Result);
1329
1330 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331}
1332
1333
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301334/*
1335 A5 # map(5)
1336 69 # text(9)
1337 696E74656772697479 # "integrity"
1338 F4 # primitive(20)
1339 64 # text(4)
1340 74797065 # "type"
1341 66 # text(6)
1342 726563656E74 # "recent"
1343 64 # text(4)
1344 74696D65 # "time"
1345 C1 # tag(1)
1346 1A 580D4172 # unsigned(1477263730)
1347 64 # text(4)
1348 64696167 # "diag"
1349 6A # text(10)
1350 30784131654335303031 # "0xA1eC5001"
1351 69 # text(9)
1352 74656C656D65747279 # "telemetry"
1353 A3 # map(3)
1354 69 # text(9)
1355 53686F652053697A65 # "Shoe Size"
1356 0C # unsigned(12)
1357 62 # text(2)
1358 4951 # "IQ"
1359 1A FFFFFFFF # unsigned(4294967295)
1360 77 # text(23)
1361 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1362 48 # bytes(8)
1363 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1364 */
1365static const uint8_t spExpectedRTIC[] = {
1366 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1367 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1368 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1369 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1370 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1371 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1372 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1373 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1374 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1375 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1376 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1377 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378
1379
1380int RTICResultsTest()
1381{
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301382 UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
1383 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301384 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301385 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301387 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301389 if(CheckResults(Encoded, spExpectedRTIC)) {
1390 return -2;
1391 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392
1393 return 0;
1394}
1395
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301396
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301397/*
1398 82 # array(2)
1399 19 01C3 # unsigned(451)
1400 43 # bytes(3)
1401 1901D2 # "\x19\x01\xD2"
1402*/
1403static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301404
Laurence Lundblade684aec22018-10-12 19:33:53 +08001405/*
1406 Very basic bstr wrapping test
1407 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301408int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001409{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001410 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301412 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001413
Laurence Lundblade684aec22018-10-12 19:33:53 +08001414 QCBOREncode_OpenArray(&EC);
1415 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001416
Laurence Lundblade684aec22018-10-12 19:33:53 +08001417 QCBOREncode_BstrWrap(&EC);
1418 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419
Laurence Lundblade684aec22018-10-12 19:33:53 +08001420 UsefulBufC Wrapped;
1421 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422
Laurence Lundblade684aec22018-10-12 19:33:53 +08001423 QCBOREncode_CloseArray(&EC);
1424
1425 UsefulBufC Encoded;
1426 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1427 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301430 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001431 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 }
1433
1434 return 0;
1435}
1436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437
1438
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301439int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001440{
1441 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001442 QCBOREncodeContext EC;
1443
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301444 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001445
1446 QCBOREncode_OpenArray(&EC);
1447 QCBOREncode_AddUInt64(&EC, 451);
1448
1449 QCBOREncode_BstrWrap(&EC);
1450 QCBOREncode_AddUInt64(&EC, 466);
1451 QCBOREncode_OpenArray(&EC);
1452
1453 UsefulBufC Wrapped;
1454 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1455
1456 QCBOREncode_CloseArray(&EC);
1457
1458 UsefulBufC Encoded2;
1459 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
1460 return -1;
1461 }
1462
1463 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301464 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001465 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1466 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
1467 return -2;
1468 }
1469
1470 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301471 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001472 for(int i = 1; i < 18; i++) {
1473 QCBOREncode_BstrWrap(&EC);
1474 }
1475 QCBOREncode_AddBool(&EC, true);
1476
1477 for(int i = 1; i < 18; i++) {
1478 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1479 }
1480
1481 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1482 return -3;
1483 }
1484
1485 return 0;
1486}
1487
1488
1489
1490// Part of bstr_wrap_nest_test
1491/*
1492 83 array with three
1493 53 byte string with 19 bytes
1494 01 #1
1495 50 byte string with 16 bytes
1496 02
1497 4D byte string with 13 bytes
1498 03
1499 4A byte string with 10 bytes
1500 04
1501 47 byte string with 7 bytes
1502 05
1503 44 byte string with 4 bytes
1504 06
1505 41 byte string with 1 byte
1506 07
1507 01
1508 02
1509 03
1510 04
1511 05
1512 06
1513 07
1514 A2 map with two items
1515 18 20 label for byte string
1516 54 byte string of length 20
1517 82 Array with two items
1518 10 The integer value 10
1519 A2 map with two items
1520 18 21 label for byte string
1521 44 byte string with 4 bytes
1522 81 array with 1 item
1523 11 integer value 11
1524 18 30 integer value 30
1525 18 40 integer label 40
1526 65 68 65 6C 6C 6F text string hello
1527 18 31 integer value 31
1528 18 41 integer label 41
1529 65 68 65 6C 6C 6F text string hello
1530
1531
1532 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301533
1534
1535/*
1536 83 # array(3)
1537 56 # bytes(22)
1538 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1539 07 # unsigned(7)
1540 A2 # map(2)
1541 18 20 # unsigned(32)
1542 54 # bytes(20)
1543 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1544 18 41 # unsigned(65)
1545 65 # text(5)
1546 68656C6C6F # "hello"
1547 */
1548static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001549{
1550 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1551 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1552 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1553 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1554 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1555 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1556 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1557 0x6F
1558};
1559
1560// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301561static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001562{
1563 int nReturn;
1564 QCBORDecodeContext DC;
1565 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1566
1567 QCBORItem Item;
1568 nReturn = QCBORDecode_GetNext(&DC, &Item);
1569 if(nReturn) {
1570 return -11;
1571 }
1572 if(Item.uDataType != QCBOR_TYPE_INT64) {
1573 return -12;
1574 }
1575
1576 nReturn = QCBORDecode_GetNext(&DC, &Item);
1577 if(nReturn == QCBOR_ERR_HIT_END) {
1578 return 0;
1579 }
1580 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1581 return -13;
1582 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301583 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001584 if(nReturn) {
1585 return nReturn;
1586 }
1587
1588 nReturn = QCBORDecode_GetNext(&DC, &Item);
1589 if(nReturn) {
1590 return -14;
1591 }
1592 if(Item.uDataType != QCBOR_TYPE_INT64) {
1593 return -15;
1594 }
1595
1596 if(QCBORDecode_Finish(&DC)) {
1597 return -16;
1598 }
1599
1600 return 0;
1601}
1602
1603// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301604static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001605{
1606 int nReturn;
1607 QCBORDecodeContext DC;
1608 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1609
1610 QCBORItem Item;
1611 nReturn = QCBORDecode_GetNext(&DC, &Item);
1612 if(nReturn) {
1613 return -11;
1614 }
1615 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1616 return -12;
1617 }
1618
1619 nReturn = QCBORDecode_GetNext(&DC, &Item);
1620 if(nReturn) {
1621 return -11;
1622 }
1623 if(Item.uDataType != QCBOR_TYPE_INT64) {
1624 return -12;
1625 }
1626
1627 nReturn = QCBORDecode_GetNext(&DC, &Item);
1628 if(nReturn) {
1629 return -11;
1630 }
1631 if(Item.uDataType != QCBOR_TYPE_MAP) {
1632 return 0;
1633 }
1634
1635 nReturn = QCBORDecode_GetNext(&DC, &Item);
1636 if(nReturn) {
1637 return -11;
1638 }
1639 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1640 return -13;
1641 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301642 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001643 if(nReturn) {
1644 return nReturn;
1645 }
1646
1647 nReturn = QCBORDecode_GetNext(&DC, &Item);
1648 if(nReturn) {
1649 return -11;
1650 }
1651 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1652 return -12;
1653 }
1654 nReturn = QCBORDecode_GetNext(&DC, &Item);
1655 if(nReturn) {
1656 return -11;
1657 }
1658 if(Item.uDataType != QCBOR_TYPE_INT64) {
1659 return -12;
1660 }
1661
1662 if(QCBORDecode_Finish(&DC)) {
1663 return -16;
1664 }
1665
1666 return 0;
1667}
1668
1669
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301670int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001671{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001672 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301673 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001674
1675 // ---- Make a complicated nested CBOR structure ---
1676 QCBOREncode_OpenArray(&EC);
1677
1678 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1679 QCBOREncode_BstrWrap(&EC);
1680 QCBOREncode_AddUInt64(&EC, i);
1681 }
1682
1683 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1684 QCBOREncode_CloseBstrWrap(&EC, NULL);
1685 QCBOREncode_AddUInt64(&EC, i);
1686 }
1687
1688 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1689 QCBOREncode_OpenMap(&EC);
1690 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1691 QCBOREncode_OpenArray(&EC);
1692 QCBOREncode_AddUInt64(&EC, i+0x10);
1693 }
1694
1695 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1696 QCBOREncode_CloseArray(&EC);
1697 QCBOREncode_AddUInt64(&EC, i+0x30);
1698 QCBOREncode_CloseBstrWrap(&EC, NULL);
1699 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1700 QCBOREncode_CloseMap(&EC);
1701 }
1702 QCBOREncode_CloseArray(&EC);
1703
1704 UsefulBufC Encoded;
1705 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1706 return -1;
1707 }
1708
1709 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301710 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001711 return -25;
1712 }
1713
1714
1715 // ---- Decode it and see if it is OK ------
1716 QCBORDecodeContext DC;
1717 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1718
1719 QCBORItem Item;
1720 QCBORDecode_GetNext(&DC, &Item);
1721 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1722 return -2;
1723 }
1724
1725 QCBORDecode_GetNext(&DC, &Item);
1726 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1727 return -3;
1728 }
1729
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301730 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001731 if(nReturn) {
1732 return nReturn;
1733 }
1734
1735 nReturn = QCBORDecode_GetNext(&DC, &Item);
1736 if(nReturn) {
1737 return -11;
1738 }
1739 if(Item.uDataType != QCBOR_TYPE_INT64) {
1740 return -12;
1741 }
1742
1743 QCBORDecode_GetNext(&DC, &Item);
1744 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1745 return -2;
1746 }
1747
1748 QCBORDecode_GetNext(&DC, &Item);
1749 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1750 return -3;
1751 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301752 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001753 if(nReturn) {
1754 return nReturn;
1755 }
1756
1757 nReturn = QCBORDecode_GetNext(&DC, &Item);
1758 if(nReturn) {
1759 return -11;
1760 }
1761 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1762 return -12;
1763 }
1764
1765 if(QCBORDecode_Finish(&DC)) {
1766 return -16;
1767 }
1768
1769 return 0;
1770}
1771
1772
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301773static const uint8_t spSignature[] = {
1774 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1775 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1776 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1777 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1778 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1779 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1780 0x45, 0xca, 0xcb, 0x36};
1781
1782/*
1783 D2 # tag(18)
1784 84 # array(4)
1785 43 # bytes(3)
1786 A10126 # "\xA1\x01&"
1787 A1 # map(1)
1788 04 # unsigned(4)
1789 42 # bytes(2)
1790 3131 # "11"
1791 54 # bytes(20)
1792 546869732069732074686520636F6E74656E742E # "This is the content."
1793 58 40 # bytes(64)
1794 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"
1795 */
1796static const uint8_t spExpected[] = {
1797 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1798 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1799 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1800 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1801 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1802 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1803 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1804 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1805 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1806 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1807
Laurence Lundblade684aec22018-10-12 19:33:53 +08001808/*
1809 this corresponds exactly to the example in RFC 8152
1810 section C.2.1. This doesn't actually verify the signature
1811 though that would be nice as it would make the test
1812 really good. That would require bring in ECDSA crypto
1813 to this test.
1814 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301815int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001816{
1817 // All of this is from RFC 8152 C.2.1
1818 const char *szKid = "11";
1819 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1820 const char *szPayload = "This is the content.";
1821 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301822 static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301823 UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301824
Laurence Lundblade684aec22018-10-12 19:33:53 +08001825 // It would be good to compare this to the output from
1826 // a COSE implementation like COSE-C. It has been checked
1827 // against the CBOR playground.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301828 UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001829
Laurence Lundblade684aec22018-10-12 19:33:53 +08001830 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301831 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001832
1833 // top level array for cose sign1, 18 is the tag for COSE sign
1834 QCBOREncode_OpenArray_3(&EC, NULL, QCBOR_NO_INT_LABEL, 18);
1835
1836 // Add protected headers
1837 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1838
1839 // Empty map with unprotected headers
1840 QCBOREncode_OpenMap(&EC);
1841 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1842 QCBOREncode_CloseMap(&EC);
1843
1844 // The payload
1845 UsefulBufC WrappedPayload;
1846 QCBOREncode_BstrWrap(&EC);
1847 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1848 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1849
1850 // Check we got back the actual payload expected
1851 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1852 return -1;
1853 }
1854
1855 // The signature
1856 QCBOREncode_AddBytes(&EC, Signature);
1857 QCBOREncode_CloseArray(&EC);
1858
1859 // Finish and check the results
1860 UsefulBufC COSE_Sign1;
1861 if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) {
1862 return -2;
1863 }
1864
1865 // 98 is the size from RFC 8152 C.2.1
1866 if(COSE_Sign1.len != 98) {
1867 return -3;
1868 }
1869
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301870 if(CheckResults(COSE_Sign1, spExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001871 return -4;
1872 }
1873
1874 return 0;
1875}
1876