blob: ae94a7f60fdc2fd65030e50f3c00e9a116490f61 [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#include <strings.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059#include <stdlib.h>
60
61
62
63// TODO: -- test on a 32-bit machine)
64
65// TODO: test QCBOR_MAX_ITEMS_IN_ARRAY (this is very large...)
66
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053067#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
68#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070// ifdef these out to not have compiler warnings
71static void printencoded(const uint8_t *pEncoded, size_t nLen)
72{
73 int i;
74 for(i = 0; i < nLen; i++) {
75 uint8_t Z = pEncoded[i];
76 printf("%02x ", Z);
77 }
78 printf("\n");
79
80 fflush(stdout);
81}
82
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080083#endif
84
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053085#include <stdio.h>
86
87int Compare(UsefulBufC U1, UsefulBufC U2) {
88 int i;
89 for(i = 0; i < U1.len; i++) {
90 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
91 printf("%d 0x%x 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
92 return 1;
93 }
94 }
95 return 0;
96
97}
98
99
100/*#define CheckResults(Enc, Expected) \
101 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)}) */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102
103#define CheckResults(Enc, Expected) \
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530104 Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105
106
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800107
108/*
109 Some very minimal tests.
110 */
111int basic_encode_test()
112{
113 // Very simple CBOR, a map with one boolean that is true in it
114 UsefulBuf_MakeStackUB(MemoryForEncoded, 100);
115 QCBOREncodeContext EC;
116
117 QCBOREncode_Init(&EC, MemoryForEncoded);
118
119 QCBOREncode_OpenMap(&EC);
120 QCBOREncode_AddBoolToMapN(&EC, 66, true);
121 QCBOREncode_CloseMap(&EC);
122
123 UsefulBufC Encoded;
124 if(QCBOREncode_Finish2(&EC, &Encoded)) {
125 return -3;
126 }
127
128
129 // Decode it and see that is right
130 QCBORDecodeContext DC;
131 QCBORItem Item;
132 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
133
134 QCBORDecode_GetNext(&DC, &Item);
135 if(Item.uDataType != QCBOR_TYPE_MAP) {
136 return -1;
137 }
138
139 QCBORDecode_GetNext(&DC, &Item);
140 if(Item.uDataType != QCBOR_TYPE_TRUE) {
141 return -1;
142 }
143
144 if(QCBORDecode_Finish(&DC)) {
145 return -2;
146 }
147
148
149 // Make another encoded message with the CBOR from the previous put into this one
150 UsefulBuf_MakeStackUB(MemoryForEncoded2, 100);
151 QCBOREncode_Init(&EC, MemoryForEncoded2);
152 QCBOREncode_OpenArray(&EC);
153 QCBOREncode_AddUInt64(&EC, 451);
154 QCBOREncode_AddEncoded(&EC, Encoded);
155 QCBOREncode_OpenMap(&EC);
156 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
157 QCBOREncode_CloseMap(&EC);
158 QCBOREncode_CloseArray(&EC);
159
160 UsefulBufC Encoded2;
161 if(QCBOREncode_Finish2(&EC, &Encoded2)) {
162 return -3;
163 }
164 /*
165 [ // 0 1:3
166 451, // 1 1:2
167 { // 1 1:2 2:1
168 66: true // 2 1:1
169 },
170 { // 1 1:1 2:1
171 -70000: { // 2 1:1 2:1 3:1
172 66: true // 3 XXXXXX
173 }
174 }
175 ]
176
177
178
179 83 # array(3)
180 19 01C3 # unsigned(451)
181 A1 # map(1)
182 18 42 # unsigned(66)
183 F5 # primitive(21)
184 A1 # map(1)
185 3A 0001116F # negative(69999)
186 A1 # map(1)
187 18 42 # unsigned(66)
188 F5 # primitive(21)
189 */
190
191 // Decode it and see if it is OK
192 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
193
194 // 0 1:3
195 QCBORDecode_GetNext(&DC, &Item);
196 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
197 return -1;
198 }
199
200 // 1 1:2
201 QCBORDecode_GetNext(&DC, &Item);
202 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
203 return -1;
204 }
205
206 // 1 1:2 2:1
207 QCBORDecode_GetNext(&DC, &Item);
208 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
209 return -1;
210 }
211
212 // 2 1:1
213 QCBORDecode_GetNext(&DC, &Item);
214 if(Item.uDataType != QCBOR_TYPE_TRUE) {
215 return -1;
216 }
217
218 // 1 1:1 2:1
219 QCBORDecode_GetNext(&DC, &Item);
220 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
221 return -1;
222 }
223
224 // 2 1:1 2:1 3:1
225 QCBORDecode_GetNext(&DC, &Item);
226 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
227 return -1;
228 }
229
230 // 3 XXXXXX
231 QCBORDecode_GetNext(&DC, &Item);
232 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
233 return -1;
234 }
235
236 if(QCBORDecode_Finish(&DC)) {
237 return -2;
238 }
239
240 return 0;
241}
242
243
244
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245static const uint8_t pExpectedEncodedAll[] = {
246
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530247 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
248 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
249 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
250 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
251 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
252 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
253 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
254 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
255 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
256 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8,
257 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72,
258 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01,
259 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb,
260 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63,
261 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03,
262 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f,
263 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00,
264 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70,
265 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
266 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
267 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
268 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
269 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
270 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
271 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
272 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
273 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
274 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
275 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
276 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
277 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
278 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
279 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
280 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
281 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
282 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
283 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
284 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
285 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
286 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
287 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
288 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
289 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
290 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
291 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
292 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
293 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
294 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
295 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
296 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
297 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
298 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
299 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
300 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
301 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
302 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
303 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
304 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
305 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
306 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
307 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
308 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
309 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
310 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
311 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
312 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
313 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
314 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
315 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
316 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
317 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
318 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
319 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
320 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
321 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
322 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
323 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
324 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
325 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
326 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
327 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
328 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
329 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
330 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
331 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
332 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
333 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
334 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
335 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
336 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
337 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
338 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
339 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
340 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
341 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
342 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
343 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
344 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
345 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
346 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
347 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
348 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
349 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
350 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
351 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
352 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
353 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
354 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
355 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
356 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
357 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
358 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
359 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
360 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
361 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
362 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
363 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
364 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
365 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
366 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
367 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
368 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
369 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
370 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
371 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
372 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
373 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
374 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
375 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
376 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
377 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
378 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
379 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
380 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
381 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
382 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
383 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
384 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
385 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
386 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
387 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
388 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
389 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
390 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
391 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
392 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
393 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
394 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
395 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
396 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
397 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
398 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
399 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
400 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
401 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
402 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
403 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
404 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
405 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
406 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
407 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
408 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
409 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
410 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
411 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
412 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
413 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
414 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
415 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
416 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
417 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
418 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
419 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
420 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
421 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
422 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
423 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
424 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
425 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
426 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
427 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
428 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
429 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
430 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
431 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
432 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
433 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
434 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
435 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
436 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
437 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
438 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
439 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
440 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
441 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
442 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
443 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
444 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
445 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
446 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
447 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
448 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
449 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
450 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
451 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
452 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
453 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
454 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
455 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
456 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
457 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
458 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
459 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
460 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
461 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462};
463
464
465static const char *szMIME = "\
466MIME-Version: 1.0\n\
467Content-Type: multipart/mixed;\n\
468boundary=\"XXXXboundary text\"\n\
469\n\
470This is a multipart message in MIME format.\n\
471\n\
472--XXXXboundary text\n\
473Content-Type: text/plain\n\
474\n\
475this is the body text\n\
476\n\
477--XXXXboundary text\n\
478Content-Type: text/plain;\n\
479Content-Disposition: attachment;\n\
480filename=\"test.txt\"\n\
481\n\
482this is the attachment text\n\
483\n\
484--XXXXboundary text--";
485
486
487int AllAddMethodsTest()
488{
489 QCBOREncodeContext ECtx;
490 int nReturn = 0;
491
492 uint8_t pEncoded[3000];
493 size_t nEncodedLen = sizeof(pEncoded);
494
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530495 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800496
497 QCBOREncode_OpenArray(&ECtx);
498
499 // Non-map ints
500 QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
501 QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 77689989909);
502 QCBOREncode_AddUInt64(&ECtx,0);
503 QCBOREncode_AddInt64(&ECtx, -44);
504
505 // ints that go in maps
506 QCBOREncode_OpenMap(&ECtx);
507 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
508 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
509 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
510 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
511 QCBOREncode_CloseMap(&ECtx);
512
513 // floats and doubles
514 QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
515 QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
516 QCBOREncode_AddFloat(&ECtx, 1);
517 QCBOREncode_AddDouble(&ECtx, 1);
518
519 // floats and doubles that go in map
520 QCBOREncode_OpenMap(&ECtx);
521 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
522 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
523 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
524 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
525 QCBOREncode_CloseMap(&ECtx);
526
527 // Epoch Date
528 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
529
530 // Epoch date with labels
531 QCBOREncode_OpenMap(&ECtx);
532 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
533 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
534 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
535 QCBOREncode_CloseMap(&ECtx);
536
537 // Binary blobs
538 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
539
540 // binary blobs in maps
541 QCBOREncode_OpenMap(&ECtx);
542 QCBOREncode_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
543 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
544 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
545 QCBOREncode_CloseMap(&ECtx);
546
547 // text blobs
548 QCBOREncode_AddText(&ECtx, SZLiteralToUsefulBufC("bar bar foo bar"));
549 QCBOREncode_AddSZString(&ECtx, "oof\n");
550 QCBOREncode_AddURI(&ECtx, SZLiteralToUsefulBufC("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
551 QCBOREncode_AddB64Text(&ECtx, SZLiteralToUsefulBufC("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
552 QCBOREncode_AddRegex(&ECtx, SZLiteralToUsefulBufC("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530553 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800554
555 // text blobs in maps
556 QCBOREncode_OpenMap(&ECtx);
557 QCBOREncode_AddTextToMap(&ECtx, "#####", SZLiteralToUsefulBufC("foo bar foo foo"));
558 QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, SZLiteralToUsefulBufC("foo bar"));
559 QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
560 QCBOREncode_AddTextToMapN(&ECtx,22, SZLiteralToUsefulBufC("foo foo foo foo"));
561 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
562 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
563 QCBOREncode_AddURIToMap(&ECtx, "RFC", SZLiteralToUsefulBufC("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
564 QCBOREncode_AddURIToMapN(&ECtx, 0x89, SZLiteralToUsefulBufC("http://cbor.me/"));
565 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", SZLiteralToUsefulBufC("cGxlYXN1cmUu"));
566 QCBOREncode_AddB64TextToMapN(&ECtx, 64, SZLiteralToUsefulBufC("c3VyZS4="));
567 QCBOREncode_AddRegexToMap(&ECtx, "popo", SZLiteralToUsefulBufC("100\\s*mk")); // x code string literal bug
568 QCBOREncode_AddRegexToMapN(&ECtx, -51, SZLiteralToUsefulBufC("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530569 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
570 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800571 QCBOREncode_CloseMap(&ECtx);
572
573 // Date strings
574 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
575 QCBOREncode_OpenMap(&ECtx);
576 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
577 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
578 QCBOREncode_CloseMap(&ECtx);
579
580 // true / false ...
581 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
582 QCBOREncode_OpenMap(&ECtx);
583 QCBOREncode_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
584 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
585 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
586 QCBOREncode_CloseMap(&ECtx);
587
588 // opening an array
589 QCBOREncode_OpenArray(&ECtx);
590 QCBOREncode_CloseArray(&ECtx);
591
592 // opening arrays in a map
593 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530594 QCBOREncode_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 QCBOREncode_CloseArray(&ECtx);
596 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
597 QCBOREncode_CloseArray(&ECtx);
598 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
599 QCBOREncode_CloseArray(&ECtx);
600 QCBOREncode_CloseMap(&ECtx);
601
602 // opening maps with labels and tagging
603 QCBOREncode_OpenMap(&ECtx);
604 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
605 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530606 QCBOREncode_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 QCBOREncode_CloseMap(&ECtx);
608 QCBOREncode_CloseMap(&ECtx);
609 QCBOREncode_CloseMap(&ECtx);
610 QCBOREncode_CloseMap(&ECtx);
611
612 // Extended simple values (these are not standard...)
613 QCBOREncode_OpenMap(&ECtx);
614 QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
615 QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
616 QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
617 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
618 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
619 QCBOREncode_CloseMap(&ECtx);
620
621
622 // UUIDs
623 static uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
624 UsefulBufC XXUUID = ByteArrayLiteralToUsefulBufC(ppppUUID);
625 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
626 QCBOREncode_OpenMap(&ECtx);
627 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
628 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
629 QCBOREncode_CloseMap(&ECtx);
630
631
632 // Bool
633 QCBOREncode_AddBool(&ECtx, true);
634 QCBOREncode_AddBool(&ECtx, false);
635 QCBOREncode_OpenMap(&ECtx);
636 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
637 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
638 QCBOREncode_CloseMap(&ECtx);
639
640
641 static uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
642 UsefulBufC BIGNUM = ByteArrayLiteralToUsefulBufC(pBignum);
643 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
644 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
645 QCBOREncode_OpenMap(&ECtx);
646 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
647 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
648 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
649 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
650 QCBOREncode_CloseMap(&ECtx);
651
652 QCBOREncode_CloseArray(&ECtx);
653
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530654 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655
656 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
657 nReturn = -1;
658 goto Done;
659 }
660
661 //printencodedE(Enc);
662
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530663 if(CheckResults(Enc, pExpectedEncodedAll))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800664 nReturn = -1;
665
666Done:
667 return nReturn;
668}
669
670// todo -- add a test for counting the top level items and adding it back in with AddRaw()
671
672
673static const uint8_t pExpectedEncodedInts[] = {
674 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
675 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
676 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
677 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
678 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
679 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
680 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
681 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
682 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
683 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
684 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
685 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
686 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
687 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
688 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
689 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
690 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
691 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
692 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
693 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
694 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
695 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
696 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
697 0xff, 0xff};
698
699/*
700
701 Test the generation of integers. This also ends up testing
702 encoding of all the different lengths. It encodes integers
703 of many lengths and values, especially around the boundaries
704 for different types of integers. It compares the output
705 to expected values generated from http://cbor.me.
706
707 */
708int IntegerValuesTest1()
709{
710 QCBOREncodeContext ECtx;
711 int nReturn = 0;
712
713 uint8_t pEncoded[1000];
714 size_t nEncodedLen = sizeof(pEncoded);
715
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530716 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800717 QCBOREncode_OpenArray(&ECtx);
718
719 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
720 QCBOREncode_AddInt64(&ECtx, -4294967297);
721 QCBOREncode_AddInt64(&ECtx, -4294967296);
722 QCBOREncode_AddInt64(&ECtx, -4294967295);
723 QCBOREncode_AddInt64(&ECtx, -4294967294);
724 QCBOREncode_AddInt64(&ECtx, -2147483648);
725 QCBOREncode_AddInt64(&ECtx, -2147483647);
726 QCBOREncode_AddInt64(&ECtx, -65538);
727 QCBOREncode_AddInt64(&ECtx, -65537);
728 QCBOREncode_AddInt64(&ECtx, -65536);
729 QCBOREncode_AddInt64(&ECtx, -65535);
730 QCBOREncode_AddInt64(&ECtx, -65534);
731 QCBOREncode_AddInt64(&ECtx, -257);
732 QCBOREncode_AddInt64(&ECtx, -256);
733 QCBOREncode_AddInt64(&ECtx, -255);
734 QCBOREncode_AddInt64(&ECtx, -254);
735 QCBOREncode_AddInt64(&ECtx, -25);
736 QCBOREncode_AddInt64(&ECtx, -24);
737 QCBOREncode_AddInt64(&ECtx, -23);
738 QCBOREncode_AddInt64(&ECtx, -1);
739 QCBOREncode_AddInt64(&ECtx, 0);
740 QCBOREncode_AddUInt64(&ECtx, 0ULL);
741 QCBOREncode_AddInt64(&ECtx, 1);
742 QCBOREncode_AddInt64(&ECtx, 22);
743 QCBOREncode_AddInt64(&ECtx, 23);
744 QCBOREncode_AddInt64(&ECtx, 24);
745 QCBOREncode_AddInt64(&ECtx, 25);
746 QCBOREncode_AddInt64(&ECtx, 26);
747 QCBOREncode_AddInt64(&ECtx, 254);
748 QCBOREncode_AddInt64(&ECtx, 255);
749 QCBOREncode_AddInt64(&ECtx, 256);
750 QCBOREncode_AddInt64(&ECtx, 257);
751 QCBOREncode_AddInt64(&ECtx, 65534);
752 QCBOREncode_AddInt64(&ECtx, 65535);
753 QCBOREncode_AddInt64(&ECtx, 65536);
754 QCBOREncode_AddInt64(&ECtx, 65537);
755 QCBOREncode_AddInt64(&ECtx, 65538);
756 QCBOREncode_AddInt64(&ECtx, 2147483647);
757 QCBOREncode_AddInt64(&ECtx, 2147483647);
758 QCBOREncode_AddInt64(&ECtx, 2147483648);
759 QCBOREncode_AddInt64(&ECtx, 2147483649);
760 QCBOREncode_AddInt64(&ECtx, 4294967294);
761 QCBOREncode_AddInt64(&ECtx, 4294967295);
762 QCBOREncode_AddInt64(&ECtx, 4294967296);
763 QCBOREncode_AddInt64(&ECtx, 4294967297);
764 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
765 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
766
767 QCBOREncode_CloseArray(&ECtx);
768
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530769 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
771 nReturn = -1;
772 }
773
774 if(CheckResults(Enc, pExpectedEncodedInts))
775 return -1;
776
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530777 if(Enc.len != sizeof(pExpectedEncodedInts) || bcmp(pEncoded, pExpectedEncodedInts, Enc.len))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 nReturn = -1;
779
780 //printencoded(pEncoded, nEncodedLen);
781
782 return(nReturn);
783}
784
785
786
787static uint8_t pExpectedEncodedSimple[] = {
788 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
789
790
791int SimpleValuesTest1()
792{
793 QCBOREncodeContext ECtx;
794 int nReturn = 0;
795
796 uint8_t pEncoded[100];
797 size_t nEncodedLen = sizeof(pEncoded);
798
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530799 QCBOREncode_Init(&ECtx, (UsefulBuf) {pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 QCBOREncode_OpenArray(&ECtx);
801
802 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
803 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
804 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
805 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
806
807 QCBOREncode_OpenMap(&ECtx);
808
809 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
810 QCBOREncode_CloseMap(&ECtx);
811
812 QCBOREncode_CloseArray(&ECtx);
813
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530814 UsefulBufC ECBOR;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
816 nReturn = -1;
817 }
818
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530819 if(ECBOR.len != sizeof(pExpectedEncodedSimple) || bcmp(pEncoded, pExpectedEncodedSimple, ECBOR.len))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800820 nReturn = -1;
821
822 // printencoded(pEncoded, nEncodedLen);
823
824 return(nReturn);
825}
826
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829static uint8_t pExpectedEncodedDates[] = {
830 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54,
831 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30,
832 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
833 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65,
834 0x20, 0x44, 0x61, 0x74, 0x65, 0x20, 0x66, 0x72,
835 0x6f, 0x6d, 0x20, 0x52, 0x46, 0x43, 0x20, 0x33,
836 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, 0x39, 0x38,
837 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
838 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30,
839 0x2e, 0x35, 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1,
840 0x19, 0x03, 0xe7
841};
842
843int EncodeDateTest()
844{
845 QCBOREncodeContext ECtx;
846 int nReturn = 0;
847
848 uint8_t pEncoded[1000];
849 size_t nEncodedLen = sizeof(pEncoded);
850
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530851 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800852
853 QCBOREncode_OpenArray(&ECtx);
854
855
856 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
857 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
858
859
860 QCBOREncode_OpenMap(&ECtx);
861
862 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
863
864
865 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
866
867 QCBOREncode_CloseMap(&ECtx);
868
869 QCBOREncode_CloseArray(&ECtx);
870
871
872 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
873 nReturn = -1;
874 }
875
876 if(nEncodedLen != sizeof(pExpectedEncodedDates) || bcmp(pEncoded, pExpectedEncodedDates, nEncodedLen))
877 nReturn = -1;
878
879 //printencoded(pEncoded, nEncodedLen);
880
881 return(nReturn);
882
883}
884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885int ArrayNestingTest1()
886{
887 QCBOREncodeContext ECtx;
888 int i;
889 int nReturn = 0;
890
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530891 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530893 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800894 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
895 QCBOREncode_OpenArray(&ECtx);
896 }
897 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
898 QCBOREncode_CloseArray(&ECtx);
899 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530900 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
902 printf("ArrayNestingTest1 Failed\n");
903 nReturn = -1;
904 }
905 //printencoded(pEncoded, nEncodedLen);
906
907 return(nReturn);
908}
909
910
911
912int ArrayNestingTest2()
913{
914 QCBOREncodeContext ECtx;
915 int i;
916 int nReturn = 0;
917
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530918 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530920 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
922 QCBOREncode_OpenArray(&ECtx);
923 }
924 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
925 QCBOREncode_CloseArray(&ECtx);
926 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530927
928 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
930 nReturn = -1;
931 }
932
933 return(nReturn);
934}
935
936
937
938int ArrayNestingTest3()
939{
940 QCBOREncodeContext ECtx;
941 int i;
942 int nReturn = 0;
943
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530944 UsefulBuf_MakeStackUB(Encode, 100);
945
946 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
948 QCBOREncode_OpenArray(&ECtx);
949 }
950 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
951 QCBOREncode_CloseArray(&ECtx);
952 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530953 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
955 nReturn = -1;
956 }
957
958 return(nReturn);
959}
960
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530961#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800962
963
964static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
965
966static int EncodeRaw(uint8_t **pEncoded, size_t *pEncodedLen)
967{
968 QCBOREncodeContext ECtx;
969 int nReturn = -1;
970
971 *pEncoded = NULL;
972 *pEncodedLen = INT32_MAX; // largest buffer this CBOR implementation will deal with
973
974 // loop runs CBOR encoding twice. First with no buffer to
975 // calucate the length so buffer can be allocated correctly,
976 // and last with the buffer to do the actual encoding
977 do {
978 QCBOREncode_Init(&ECtx, *pEncoded, *pEncodedLen);
979 QCBOREncode_OpenArray(&ECtx);
980 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {s_pFiveArrarys, 5}), 1});
981 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}), 1});
982 QCBOREncode_CloseArray(&ECtx);
983
984 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
985 goto Done;
986 if(*pEncoded != NULL) {
987 nReturn = 0;
988 goto Done;
989 }
990 *pEncoded = malloc(*pEncodedLen);
991 } while(1);
992
993Done:
994 return(nReturn);
995}
996
997
998// Validated at http://cbor.me and by manually examining its output
999static uint8_t s_pEncodeRawExpected[] = {
1000 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1001 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1002 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1003 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1004 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1005 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1006 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1007 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1008 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1009 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1010 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1011 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1012 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1013 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1014 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1015 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1016 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1017 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1018 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1019 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1020 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1021 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1022 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1023 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1024
1025
1026
1027int EncodeRawTest()
1028{
1029 uint8_t *pEncodedRaw;
1030 size_t nEncodedRawLen;
1031
1032 if(EncodeRaw(&pEncodedRaw, &nEncodedRawLen)) {
1033 return(-1);
1034 }
1035
1036 //printencoded(pEncodedRaw, nEncodedRawLen);
1037
1038 int nReturn = 0;
1039 if(nEncodedRawLen != sizeof(s_pEncodeRawExpected) || memcmp(s_pEncodeRawExpected, pEncodedRaw, sizeof(s_pEncodeRawExpected)))
1040 nReturn = 1;
1041
1042 return(nReturn);
1043}
1044
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301045#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046
1047
1048
1049static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1050{
1051 QCBOREncodeContext ECtx;
1052 int nReturn = -1;
1053
1054 *pEncoded = NULL;
1055 *pEncodedLen = INT32_MAX;
1056
1057 // loop runs CBOR encoding twice. First with no buffer to
1058 // calucate the length so buffer can be allocated correctly,
1059 // and last with the buffer to do the actual encoding
1060 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301061 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 QCBOREncode_OpenMap(&ECtx);
1063 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1064 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1065 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1066 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1067 QCBOREncode_CloseArray(&ECtx);
1068 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1069 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1070 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1071 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1072 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1073 QCBOREncode_CloseMap(&ECtx);
1074 QCBOREncode_CloseMap(&ECtx);
1075
1076
1077 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
1078 goto Done;
1079 if(*pEncoded != NULL) {
1080 nReturn = 0;
1081 goto Done;
1082 }
1083 *pEncoded = malloc(*pEncodedLen);
1084 } while(1);
1085
1086 Done:
1087 return(nReturn);
1088}
1089
1090
1091static uint8_t pValidMapEncoded[] = {
1092 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a,
1093 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, 0x6f, 0x20,
1094 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x67,
1095 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20,
1096 0x6d, 0x61, 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, 0x44, 0x78, 0x78, 0x78, 0x78,
1097 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f,
1098 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
1099 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
1100 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 } ;
1101
1102
1103
1104int MapEncodeTest()
1105{
1106 uint8_t *pEncodedMaps;
1107 size_t nEncodedMapLen;
1108
1109 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
1110 return(-1);
1111 }
1112
1113 //printencoded(pEncodedMaps, nEncodedMapLen);
1114
1115 int nReturn = 0;
1116 if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded)))
1117 nReturn = 1;
1118
1119 return(nReturn);
1120}
1121
1122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001123/*
1124 @brief Encode the RTIC results
1125
1126 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1127 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1128 @param[in] szAlexString Diagnostic code.
1129 @param[in[ pOut Buffer to put the result in
1130 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1131
1132 @return
1133 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1134
1135 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1136 short an error will be returned. This function will never write off the end
1137 of the buffer passed to it.
1138
1139 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1140 length of the encoded CBOR.
1141
1142 */
1143
1144int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen)
1145{
1146 // Buffer that the result will be written in to
1147 // It is fixed size and small that a stack variable will be fine
1148 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1149
1150 // Context for the encoder
1151 QCBOREncodeContext ECtx;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301152 QCBOREncode_Init(&ECtx, (UsefulBuf){pOut, *pnLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001153
1154 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1155 // Contents are label / value pairs
1156 QCBOREncode_OpenMap(&ECtx);
1157
1158 { // Brace / indention just to show CBOR encoding nesting
1159
1160 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1161 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1162
1163 // Add the diagnostic code
1164 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1165
1166 // Add a time stamp
1167 if(time) {
1168 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1169 }
1170
1171 // Add the diagnostic code
1172 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1173
1174 // Open a subordinate map for telemtry data
1175 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1176
1177 { // Brace / indention just to show CBOR encoding nesting
1178
1179 // Add a few fake integers and buffers for now.
1180 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1181
1182 // Add a few fake integers and buffers for now.
1183 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1184
1185 // Add a few fake integers and buffers for now.
1186 const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
1187 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1188
1189 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1190 }
1191 }
1192
1193 // Close the telemetry map
1194 QCBOREncode_CloseMap(&ECtx);
1195
1196 // Close the map
1197 QCBOREncode_CloseMap(&ECtx);
1198
1199 return QCBOREncode_Finish(&ECtx, pnLen);
1200}
1201
1202
1203static const uint8_t pExpectedRTIC[] = {0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74, 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1, 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67, 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30, 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69, 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61, 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
1204
1205
1206int RTICResultsTest()
1207{
1208 uint8_t out[200];
1209 size_t len = sizeof(out);
1210
1211 int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len);
1212 if(nResult)
1213 return -1;
1214
1215 if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC)))
1216 return 1;
1217
1218 //printencoded(out, len);
1219
1220 return 0;
1221}
1222
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301223
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301224
Laurence Lundblade684aec22018-10-12 19:33:53 +08001225/*
1226 Very basic bstr wrapping test
1227 */
1228int bstrwraptest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001229{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001230 UsefulBuf_MakeStackUB(MemoryForEncoded, 100);
1231 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001232
Laurence Lundblade684aec22018-10-12 19:33:53 +08001233 QCBOREncode_Init(&EC, MemoryForEncoded);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001234
Laurence Lundblade684aec22018-10-12 19:33:53 +08001235 QCBOREncode_OpenArray(&EC);
1236 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237
Laurence Lundblade684aec22018-10-12 19:33:53 +08001238 QCBOREncode_BstrWrap(&EC);
1239 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001240
Laurence Lundblade684aec22018-10-12 19:33:53 +08001241 UsefulBufC Wrapped;
1242 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243
Laurence Lundblade684aec22018-10-12 19:33:53 +08001244 QCBOREncode_CloseArray(&EC);
1245
1246 UsefulBufC Encoded;
1247 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1248 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001249 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250
Laurence Lundblade684aec22018-10-12 19:33:53 +08001251 const uint8_t pExpected[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
1252 if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(pExpected), Encoded)) {
1253 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001254 }
1255
1256 return 0;
1257}
1258
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001259
1260
Laurence Lundblade684aec22018-10-12 19:33:53 +08001261int bstr_wrap_error_test()
1262{
1263 // -------------- Test closing a bstrwrap when it is an array that is open -----------
1264 UsefulBuf_MakeStackUB(MemoryForEncoded, 100);
1265 QCBOREncodeContext EC;
1266
1267 QCBOREncode_Init(&EC, MemoryForEncoded);
1268
1269 QCBOREncode_OpenArray(&EC);
1270 QCBOREncode_AddUInt64(&EC, 451);
1271
1272 QCBOREncode_BstrWrap(&EC);
1273 QCBOREncode_AddUInt64(&EC, 466);
1274 QCBOREncode_OpenArray(&EC);
1275
1276 UsefulBufC Wrapped;
1277 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1278
1279 QCBOREncode_CloseArray(&EC);
1280
1281 UsefulBufC Encoded2;
1282 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
1283 return -1;
1284 }
1285
1286 // ----------- test closing a bstrwrap when nothing is open ---------------------
1287 QCBOREncode_Init(&EC, MemoryForEncoded);
1288 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1289 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
1290 return -2;
1291 }
1292
1293 // --------------- test nesting too deep ----------------------------------
1294 QCBOREncode_Init(&EC, MemoryForEncoded);
1295 for(int i = 1; i < 18; i++) {
1296 QCBOREncode_BstrWrap(&EC);
1297 }
1298 QCBOREncode_AddBool(&EC, true);
1299
1300 for(int i = 1; i < 18; i++) {
1301 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1302 }
1303
1304 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1305 return -3;
1306 }
1307
1308 return 0;
1309}
1310
1311
1312
1313// Part of bstr_wrap_nest_test
1314/*
1315 83 array with three
1316 53 byte string with 19 bytes
1317 01 #1
1318 50 byte string with 16 bytes
1319 02
1320 4D byte string with 13 bytes
1321 03
1322 4A byte string with 10 bytes
1323 04
1324 47 byte string with 7 bytes
1325 05
1326 44 byte string with 4 bytes
1327 06
1328 41 byte string with 1 byte
1329 07
1330 01
1331 02
1332 03
1333 04
1334 05
1335 06
1336 07
1337 A2 map with two items
1338 18 20 label for byte string
1339 54 byte string of length 20
1340 82 Array with two items
1341 10 The integer value 10
1342 A2 map with two items
1343 18 21 label for byte string
1344 44 byte string with 4 bytes
1345 81 array with 1 item
1346 11 integer value 11
1347 18 30 integer value 30
1348 18 40 integer label 40
1349 65 68 65 6C 6C 6F text string hello
1350 18 31 integer value 31
1351 18 41 integer label 41
1352 65 68 65 6C 6C 6F text string hello
1353
1354
1355 */
1356static const uint8_t sExpectedDeepBstr[] =
1357{
1358 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1359 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1360 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1361 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1362 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1363 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1364 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1365 0x6F
1366};
1367
1368// Part of bstr_wrap_nest_test
1369static int decode_next_nested(UsefulBufC Wrapped)
1370{
1371 int nReturn;
1372 QCBORDecodeContext DC;
1373 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1374
1375 QCBORItem Item;
1376 nReturn = QCBORDecode_GetNext(&DC, &Item);
1377 if(nReturn) {
1378 return -11;
1379 }
1380 if(Item.uDataType != QCBOR_TYPE_INT64) {
1381 return -12;
1382 }
1383
1384 nReturn = QCBORDecode_GetNext(&DC, &Item);
1385 if(nReturn == QCBOR_ERR_HIT_END) {
1386 return 0;
1387 }
1388 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1389 return -13;
1390 }
1391 nReturn = decode_next_nested(Item.val.string);
1392 if(nReturn) {
1393 return nReturn;
1394 }
1395
1396 nReturn = QCBORDecode_GetNext(&DC, &Item);
1397 if(nReturn) {
1398 return -14;
1399 }
1400 if(Item.uDataType != QCBOR_TYPE_INT64) {
1401 return -15;
1402 }
1403
1404 if(QCBORDecode_Finish(&DC)) {
1405 return -16;
1406 }
1407
1408 return 0;
1409}
1410
1411// Part of bstr_wrap_nest_test
1412static int decode_next_nested2(UsefulBufC Wrapped)
1413{
1414 int nReturn;
1415 QCBORDecodeContext DC;
1416 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1417
1418 QCBORItem Item;
1419 nReturn = QCBORDecode_GetNext(&DC, &Item);
1420 if(nReturn) {
1421 return -11;
1422 }
1423 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1424 return -12;
1425 }
1426
1427 nReturn = QCBORDecode_GetNext(&DC, &Item);
1428 if(nReturn) {
1429 return -11;
1430 }
1431 if(Item.uDataType != QCBOR_TYPE_INT64) {
1432 return -12;
1433 }
1434
1435 nReturn = QCBORDecode_GetNext(&DC, &Item);
1436 if(nReturn) {
1437 return -11;
1438 }
1439 if(Item.uDataType != QCBOR_TYPE_MAP) {
1440 return 0;
1441 }
1442
1443 nReturn = QCBORDecode_GetNext(&DC, &Item);
1444 if(nReturn) {
1445 return -11;
1446 }
1447 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1448 return -13;
1449 }
1450 nReturn = decode_next_nested2(Item.val.string);
1451 if(nReturn) {
1452 return nReturn;
1453 }
1454
1455 nReturn = QCBORDecode_GetNext(&DC, &Item);
1456 if(nReturn) {
1457 return -11;
1458 }
1459 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1460 return -12;
1461 }
1462 nReturn = QCBORDecode_GetNext(&DC, &Item);
1463 if(nReturn) {
1464 return -11;
1465 }
1466 if(Item.uDataType != QCBOR_TYPE_INT64) {
1467 return -12;
1468 }
1469
1470 if(QCBORDecode_Finish(&DC)) {
1471 return -16;
1472 }
1473
1474 return 0;
1475}
1476
1477
1478int bstr_wrap_nest_test()
1479{
1480 UsefulBuf_MakeStackUB(MemoryForEncoded, 300);
1481 QCBOREncodeContext EC;
1482 QCBOREncode_Init(&EC, MemoryForEncoded);
1483
1484 // ---- Make a complicated nested CBOR structure ---
1485 QCBOREncode_OpenArray(&EC);
1486
1487 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1488 QCBOREncode_BstrWrap(&EC);
1489 QCBOREncode_AddUInt64(&EC, i);
1490 }
1491
1492 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1493 QCBOREncode_CloseBstrWrap(&EC, NULL);
1494 QCBOREncode_AddUInt64(&EC, i);
1495 }
1496
1497 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1498 QCBOREncode_OpenMap(&EC);
1499 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1500 QCBOREncode_OpenArray(&EC);
1501 QCBOREncode_AddUInt64(&EC, i+0x10);
1502 }
1503
1504 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1505 QCBOREncode_CloseArray(&EC);
1506 QCBOREncode_AddUInt64(&EC, i+0x30);
1507 QCBOREncode_CloseBstrWrap(&EC, NULL);
1508 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1509 QCBOREncode_CloseMap(&EC);
1510 }
1511 QCBOREncode_CloseArray(&EC);
1512
1513 UsefulBufC Encoded;
1514 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1515 return -1;
1516 }
1517
1518 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
1519 if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(sExpectedDeepBstr), Encoded)) {
1520 return -25;
1521 }
1522
1523
1524 // ---- Decode it and see if it is OK ------
1525 QCBORDecodeContext DC;
1526 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1527
1528 QCBORItem Item;
1529 QCBORDecode_GetNext(&DC, &Item);
1530 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1531 return -2;
1532 }
1533
1534 QCBORDecode_GetNext(&DC, &Item);
1535 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1536 return -3;
1537 }
1538
1539 int nReturn = decode_next_nested(Item.val.string);
1540 if(nReturn) {
1541 return nReturn;
1542 }
1543
1544 nReturn = QCBORDecode_GetNext(&DC, &Item);
1545 if(nReturn) {
1546 return -11;
1547 }
1548 if(Item.uDataType != QCBOR_TYPE_INT64) {
1549 return -12;
1550 }
1551
1552 QCBORDecode_GetNext(&DC, &Item);
1553 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1554 return -2;
1555 }
1556
1557 QCBORDecode_GetNext(&DC, &Item);
1558 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1559 return -3;
1560 }
1561 nReturn = decode_next_nested2(Item.val.string);
1562 if(nReturn) {
1563 return nReturn;
1564 }
1565
1566 nReturn = QCBORDecode_GetNext(&DC, &Item);
1567 if(nReturn) {
1568 return -11;
1569 }
1570 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1571 return -12;
1572 }
1573
1574 if(QCBORDecode_Finish(&DC)) {
1575 return -16;
1576 }
1577
1578 return 0;
1579}
1580
1581
1582/*
1583 this corresponds exactly to the example in RFC 8152
1584 section C.2.1. This doesn't actually verify the signature
1585 though that would be nice as it would make the test
1586 really good. That would require bring in ECDSA crypto
1587 to this test.
1588 */
1589int cose_sign1_tbs_test()
1590{
1591 // All of this is from RFC 8152 C.2.1
1592 const char *szKid = "11";
1593 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1594 const char *szPayload = "This is the content.";
1595 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
1596 const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
1597 UsefulBufC ProtectedHeaders = UsefulBuf_FromByteArrayLiteral(pProtectedHeaders);
1598 const uint8_t sSignature[] = {
1599 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1600 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1601 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1602 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1603 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1604 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1605 0x45, 0xca, 0xcb, 0x36};
1606 // It would be good to compare this to the output from
1607 // a COSE implementation like COSE-C. It has been checked
1608 // against the CBOR playground.
1609 UsefulBufC Signature = UsefulBuf_FromByteArrayLiteral(sSignature);
1610 const uint8_t sExpected[] = {
1611 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1612 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1613 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1614 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1615 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1616 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1617 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1618 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1619 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1620 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1621 UsefulBufC Expected = UsefulBuf_FromByteArrayLiteral(sExpected);
1622
1623 UsefulBuf_MakeStackUB(MemoryForEncoded, 98);
1624 QCBOREncodeContext EC;
1625 QCBOREncode_Init(&EC, MemoryForEncoded);
1626
1627 // top level array for cose sign1, 18 is the tag for COSE sign
1628 QCBOREncode_OpenArray_3(&EC, NULL, QCBOR_NO_INT_LABEL, 18);
1629
1630 // Add protected headers
1631 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1632
1633 // Empty map with unprotected headers
1634 QCBOREncode_OpenMap(&EC);
1635 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1636 QCBOREncode_CloseMap(&EC);
1637
1638 // The payload
1639 UsefulBufC WrappedPayload;
1640 QCBOREncode_BstrWrap(&EC);
1641 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1642 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1643
1644 // Check we got back the actual payload expected
1645 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1646 return -1;
1647 }
1648
1649 // The signature
1650 QCBOREncode_AddBytes(&EC, Signature);
1651 QCBOREncode_CloseArray(&EC);
1652
1653 // Finish and check the results
1654 UsefulBufC COSE_Sign1;
1655 if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) {
1656 return -2;
1657 }
1658
1659 // 98 is the size from RFC 8152 C.2.1
1660 if(COSE_Sign1.len != 98) {
1661 return -3;
1662 }
1663
1664 if(UsefulBuf_Compare(COSE_Sign1, Expected)) {
1665 return -4;
1666 }
1667
1668 return 0;
1669}
1670