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