blob: b42002e2198b62f3511cb38810629d8850eeee0f [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05305
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor.h"
34#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035
36
Laurence Lundblade369b90a2018-10-22 02:04:37 +053037/*
38 This is the test set for CBOR encoding.
39
40 This is largely complete for the implemented.
41
42 A few more things to do include:
43 - Add a test for counting the top level items and adding it back in with AddRaw()
44 - Run on some different CPUs like 32-bit and maybe even 16-bit
45 - Test the large array count limit
46 - Add the CBOR diagnostic output for every expected
47
48 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049
Laurence Lundblade369b90a2018-10-22 02:04:37 +053050//#define PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +070052#if PRINT_FUNCTIONS_FOR_DEBUGGINGXX
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053053#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055// ifdef these out to not have compiler warnings
56static void printencoded(const uint8_t *pEncoded, size_t nLen)
57{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070058 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059 for(i = 0; i < nLen; i++) {
60 uint8_t Z = pEncoded[i];
61 printf("%02x ", Z);
62 }
63 printf("\n");
64
65 fflush(stdout);
66}
67
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080068
Laurence Lundblade369b90a2018-10-22 02:04:37 +053069// Do the comparison and print out where it fails
70static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070071 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053072 for(i = 0; i < U1.len; i++) {
73 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 printf("Position: %d Actual: 0x%x Expected: 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 return 1;
76 }
77 }
78 return 0;
79
80}
81
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070082#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053083 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053084
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070085#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080086
87#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080088 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
89
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070090#endif
91
92
93
Laurence Lundblade369b90a2018-10-22 02:04:37 +053094// One big buffer that is used by all the tests to encode into
95// Putting it in uninitialized data is better than using a lot
96// of stack. The tests should run on small devices too.
97static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098
99
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800100
101/*
102 Some very minimal tests.
103 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530104int BasicEncodeTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800105{
106 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800107 QCBOREncodeContext EC;
108
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530109 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530110
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800111 QCBOREncode_OpenMap(&EC);
112 QCBOREncode_AddBoolToMapN(&EC, 66, true);
113 QCBOREncode_CloseMap(&EC);
114
115 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700116 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530117 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800118 }
119
120
121 // Decode it and see that is right
122 QCBORDecodeContext DC;
123 QCBORItem Item;
124 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
125
126 QCBORDecode_GetNext(&DC, &Item);
127 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530128 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800129 }
130
131 QCBORDecode_GetNext(&DC, &Item);
132 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530133 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800134 }
135
136 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530137 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800138 }
139
140
141 // Make another encoded message with the CBOR from the previous put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530142 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800143 QCBOREncode_Init(&EC, MemoryForEncoded2);
144 QCBOREncode_OpenArray(&EC);
145 QCBOREncode_AddUInt64(&EC, 451);
146 QCBOREncode_AddEncoded(&EC, Encoded);
147 QCBOREncode_OpenMap(&EC);
148 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
149 QCBOREncode_CloseMap(&EC);
150 QCBOREncode_CloseArray(&EC);
151
152 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700153 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530154 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800155 }
156 /*
157 [ // 0 1:3
158 451, // 1 1:2
159 { // 1 1:2 2:1
160 66: true // 2 1:1
161 },
162 { // 1 1:1 2:1
163 -70000: { // 2 1:1 2:1 3:1
164 66: true // 3 XXXXXX
165 }
166 }
167 ]
168
169
170
171 83 # array(3)
172 19 01C3 # unsigned(451)
173 A1 # map(1)
174 18 42 # unsigned(66)
175 F5 # primitive(21)
176 A1 # map(1)
177 3A 0001116F # negative(69999)
178 A1 # map(1)
179 18 42 # unsigned(66)
180 F5 # primitive(21)
181 */
182
183 // Decode it and see if it is OK
184 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
185
186 // 0 1:3
187 QCBORDecode_GetNext(&DC, &Item);
188 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530189 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800190 }
191
192 // 1 1:2
193 QCBORDecode_GetNext(&DC, &Item);
194 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530195 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800196 }
197
198 // 1 1:2 2:1
199 QCBORDecode_GetNext(&DC, &Item);
200 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530201 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800202 }
203
204 // 2 1:1
205 QCBORDecode_GetNext(&DC, &Item);
206 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530207 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800208 }
209
210 // 1 1:1 2:1
211 QCBORDecode_GetNext(&DC, &Item);
212 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530213 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214 }
215
216 // 2 1:1 2:1 3:1
217 QCBORDecode_GetNext(&DC, &Item);
218 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530219 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800220 }
221
222 // 3 XXXXXX
223 QCBORDecode_GetNext(&DC, &Item);
224 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530225 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800226 }
227
228 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530229 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800230 }
231
232 return 0;
233}
234
235
236
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530237static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700238 0x98, 0x22, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530239 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
240 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
241 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
242 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
243 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
244 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
245 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
246 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700247 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530248 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
249 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
250 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
251 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
252 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
253 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
254 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
255 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
256 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
257 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
258 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
259 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
260 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
261 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
262 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
263 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
264 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
265 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
266 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
267 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
268 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
269 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
270 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
271 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
272 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
273 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
274 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
275 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
276 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
277 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
278 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
279 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
280 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
281 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
282 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
283 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
284 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
285 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
286 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
287 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
288 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
289 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
290 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
291 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
292 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
293 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
294 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
295 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
296 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
297 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
298 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
299 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
300 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
301 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
302 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
303 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
304 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
305 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
306 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
307 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
308 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
309 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
310 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
311 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
312 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
313 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
314 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
315 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
316 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
317 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
318 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
319 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
320 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
321 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
322 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
323 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
324 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
325 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
326 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
327 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
328 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
329 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
330 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
331 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
332 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
333 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
334 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
335 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
336 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
337 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
338 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
339 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
340 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
341 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
342 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
343 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
344 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
345 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
346 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
347 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
348 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
349 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
350 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
351 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
352 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
353 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
354 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
355 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
356 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
357 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
358 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
359 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
360 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
361 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
362 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
363 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
364 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
365 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
366 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
367 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
368 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
369 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
370 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
371 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
372 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
373 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
374 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
375 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
376 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
377 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
378 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
379 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
380 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
381 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
382 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
383 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
384 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
385 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
386 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
387 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
388 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
389 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
390 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
391 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
392 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
393 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
394 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
395 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
396 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
397 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
398 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
399 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
400 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
401 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
402 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
403 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
404 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
405 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
406 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
407 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
408 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
409 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
410 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
411 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
412 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
413 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
414 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
415 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
416 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
417 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
418 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
419 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
420 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
421 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
422 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
423 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
424 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
425 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
426 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
427 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
428 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
429 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
430 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
431 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
432 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
433 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
434 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
435 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
436 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
437 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
438 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
440 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
441 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
442 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
443 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800444};
445
446
447static const char *szMIME = "\
448MIME-Version: 1.0\n\
449Content-Type: multipart/mixed;\n\
450boundary=\"XXXXboundary text\"\n\
451\n\
452This is a multipart message in MIME format.\n\
453\n\
454--XXXXboundary text\n\
455Content-Type: text/plain\n\
456\n\
457this is the body text\n\
458\n\
459--XXXXboundary text\n\
460Content-Type: text/plain;\n\
461Content-Disposition: attachment;\n\
462filename=\"test.txt\"\n\
463\n\
464this is the attachment text\n\
465\n\
466--XXXXboundary text--";
467
468
469int AllAddMethodsTest()
470{
471 QCBOREncodeContext ECtx;
472 int nReturn = 0;
473
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530474 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475
476 QCBOREncode_OpenArray(&ECtx);
477
478 // Non-map ints
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700479 QCBOREncode_AddTag(&ECtx, 100);
480 QCBOREncode_AddUInt64_2(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 89989909);
481 QCBOREncode_AddTag(&ECtx, 76);
482 QCBOREncode_AddInt64_2(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 77689989909);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800483 QCBOREncode_AddUInt64(&ECtx,0);
484 QCBOREncode_AddInt64(&ECtx, -44);
485
486 // ints that go in maps
487 QCBOREncode_OpenMap(&ECtx);
488 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
489 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
490 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
491 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
492 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493
494 // Epoch Date
495 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
496
497 // Epoch date with labels
498 QCBOREncode_OpenMap(&ECtx);
499 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
500 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
501 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
502 QCBOREncode_CloseMap(&ECtx);
503
504 // Binary blobs
505 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
506
507 // binary blobs in maps
508 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700509 QCBOREncode_AddTag(&ECtx, 100000);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700510 QCBOREncode_AddBytes_2(&ECtx, "binbin", QCBOR_NO_INT_LABEL, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800511 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
512 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
513 QCBOREncode_CloseMap(&ECtx);
514
515 // text blobs
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530516 QCBOREncode_AddText(&ECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 QCBOREncode_AddSZString(&ECtx, "oof\n");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530518 QCBOREncode_AddURI(&ECtx, UsefulBuf_FROM_SZ_LITERAL("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
519 QCBOREncode_AddB64Text(&ECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
520 QCBOREncode_AddRegex(&ECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530521 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800522
523 // text blobs in maps
524 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530525 QCBOREncode_AddTextToMap(&ECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
Laurence Lundblade5164a9e2018-11-01 11:24:57 +0700526 QCBOREncode_AddTextToMap(&ECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar")); // TODO _2?
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700527 QCBOREncode_AddTag(&ECtx, 1000);
528 QCBOREncode_AddSZString_2(&ECtx, "()()()", QCBOR_NO_INT_LABEL, "rab rab oof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530529 QCBOREncode_AddTextToMapN(&ECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800530 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
531 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530532 QCBOREncode_AddURIToMap(&ECtx, "RFC", UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
533 QCBOREncode_AddURIToMapN(&ECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
534 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
535 QCBOREncode_AddB64TextToMapN(&ECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
536 QCBOREncode_AddRegexToMap(&ECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
537 QCBOREncode_AddRegexToMapN(&ECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530538 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
539 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800540 QCBOREncode_CloseMap(&ECtx);
541
542 // Date strings
543 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
544 QCBOREncode_OpenMap(&ECtx);
545 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
546 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
547 QCBOREncode_CloseMap(&ECtx);
548
549 // true / false ...
550 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
551 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700552 QCBOREncode_AddTag(&ECtx, 66);
553 QCBOREncode_AddSimple_2(&ECtx, "dare", QCBOR_NO_INT_LABEL, CBOR_SIMPLEV_TRUE);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800554 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
555 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
556 QCBOREncode_CloseMap(&ECtx);
557
558 // opening an array
559 QCBOREncode_OpenArray(&ECtx);
560 QCBOREncode_CloseArray(&ECtx);
561
562 // opening arrays in a map
563 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700564 QCBOREncode_AddTag(&ECtx, 1093);
565 QCBOREncode_OpenArray_2(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 QCBOREncode_CloseArray(&ECtx);
567 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
568 QCBOREncode_CloseArray(&ECtx);
569 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
570 QCBOREncode_CloseArray(&ECtx);
571 QCBOREncode_CloseMap(&ECtx);
572
573 // opening maps with labels and tagging
574 QCBOREncode_OpenMap(&ECtx);
575 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
576 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700577 QCBOREncode_AddTag(&ECtx, 9087);
578 QCBOREncode_OpenMap_2(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 QCBOREncode_CloseMap(&ECtx);
580 QCBOREncode_CloseMap(&ECtx);
581 QCBOREncode_CloseMap(&ECtx);
582 QCBOREncode_CloseMap(&ECtx);
583
584 // Extended simple values (these are not standard...)
585 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700586 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700587 QCBOREncode_AddType7_2(&ECtx, "s1", QCBOR_NO_INT_LABEL, 0, 255);
588 QCBOREncode_AddType7_2(&ECtx, "s2", QCBOR_NO_INT_LABEL, 0, 0);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700589 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700590 QCBOREncode_AddType7_2(&ECtx, "s3", QCBOR_NO_INT_LABEL, 0, 33);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700591 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700592 QCBOREncode_AddType7_2(&ECtx, NULL, 88378374, 0, 255);
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700593 QCBOREncode_AddTag(&ECtx, 88);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700594 QCBOREncode_AddType7_2(&ECtx, NULL, 89, 0, 19);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 QCBOREncode_CloseMap(&ECtx);
596
597
598 // UUIDs
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530599 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530600 UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800601 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
602 QCBOREncode_OpenMap(&ECtx);
603 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
604 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
605 QCBOREncode_CloseMap(&ECtx);
606
607
608 // Bool
609 QCBOREncode_AddBool(&ECtx, true);
610 QCBOREncode_AddBool(&ECtx, false);
611 QCBOREncode_OpenMap(&ECtx);
612 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
613 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
614 QCBOREncode_CloseMap(&ECtx);
615
616
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530617 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530618 UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800619 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
620 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
621 QCBOREncode_OpenMap(&ECtx);
622 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
623 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
624 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
625 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
626 QCBOREncode_CloseMap(&ECtx);
627
628 QCBOREncode_CloseArray(&ECtx);
629
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530630 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800631
Laurence Lundblade0595e932018-11-02 22:22:47 +0700632 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800633 nReturn = -1;
634 goto Done;
635 }
636
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530637 if(CheckResults(Enc, spExpectedEncodedAll))
638 nReturn = -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639
640Done:
641 return nReturn;
642}
643
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530644/*
645 98 2F # array(47)
646 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
647 3B 0000000100000000 # negative(4294967296)
648 3A FFFFFFFF # negative(4294967295)
649 3A FFFFFFFE # negative(4294967294)
650 3A FFFFFFFD # negative(4294967293)
651 3A 7FFFFFFF # negative(2147483647)
652 3A 7FFFFFFE # negative(2147483646)
653 3A 00010001 # negative(65537)
654 3A 00010000 # negative(65536)
655 39 FFFF # negative(65535)
656 39 FFFE # negative(65534)
657 39 FFFD # negative(65533)
658 39 0100 # negative(256)
659 38 FF # negative(255)
660 38 FE # negative(254)
661 38 FD # negative(253)
662 38 18 # negative(24)
663 37 # negative(23)
664 36 # negative(22)
665 20 # negative(0)
666 00 # unsigned(0)
667 00 # unsigned(0)
668 01 # unsigned(1)
669 16 # unsigned(22)
670 17 # unsigned(23)
671 18 18 # unsigned(24)
672 18 19 # unsigned(25)
673 18 1A # unsigned(26)
674 18 FE # unsigned(254)
675 18 FF # unsigned(255)
676 19 0100 # unsigned(256)
677 19 0101 # unsigned(257)
678 19 FFFE # unsigned(65534)
679 19 FFFF # unsigned(65535)
680 1A 00010000 # unsigned(65536)
681 1A 00010001 # unsigned(65537)
682 1A 00010002 # unsigned(65538)
683 1A 7FFFFFFF # unsigned(2147483647)
684 1A 7FFFFFFF # unsigned(2147483647)
685 1A 80000000 # unsigned(2147483648)
686 1A 80000001 # unsigned(2147483649)
687 1A FFFFFFFE # unsigned(4294967294)
688 1A FFFFFFFF # unsigned(4294967295)
689 1B 0000000100000000 # unsigned(4294967296)
690 1B 0000000100000001 # unsigned(4294967297)
691 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
692 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
693 */
694static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800695 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
696 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
697 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
698 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
699 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
700 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
701 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
702 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
703 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
704 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
705 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
706 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
707 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
708 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
709 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
710 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
711 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
712 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
713 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
714 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
715 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
716 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
717 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
718 0xff, 0xff};
719
720/*
721
722 Test the generation of integers. This also ends up testing
723 encoding of all the different lengths. It encodes integers
724 of many lengths and values, especially around the boundaries
725 for different types of integers. It compares the output
726 to expected values generated from http://cbor.me.
727
728 */
729int IntegerValuesTest1()
730{
731 QCBOREncodeContext ECtx;
732 int nReturn = 0;
733
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530734 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 QCBOREncode_OpenArray(&ECtx);
736
737 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
738 QCBOREncode_AddInt64(&ECtx, -4294967297);
739 QCBOREncode_AddInt64(&ECtx, -4294967296);
740 QCBOREncode_AddInt64(&ECtx, -4294967295);
741 QCBOREncode_AddInt64(&ECtx, -4294967294);
742 QCBOREncode_AddInt64(&ECtx, -2147483648);
743 QCBOREncode_AddInt64(&ECtx, -2147483647);
744 QCBOREncode_AddInt64(&ECtx, -65538);
745 QCBOREncode_AddInt64(&ECtx, -65537);
746 QCBOREncode_AddInt64(&ECtx, -65536);
747 QCBOREncode_AddInt64(&ECtx, -65535);
748 QCBOREncode_AddInt64(&ECtx, -65534);
749 QCBOREncode_AddInt64(&ECtx, -257);
750 QCBOREncode_AddInt64(&ECtx, -256);
751 QCBOREncode_AddInt64(&ECtx, -255);
752 QCBOREncode_AddInt64(&ECtx, -254);
753 QCBOREncode_AddInt64(&ECtx, -25);
754 QCBOREncode_AddInt64(&ECtx, -24);
755 QCBOREncode_AddInt64(&ECtx, -23);
756 QCBOREncode_AddInt64(&ECtx, -1);
757 QCBOREncode_AddInt64(&ECtx, 0);
758 QCBOREncode_AddUInt64(&ECtx, 0ULL);
759 QCBOREncode_AddInt64(&ECtx, 1);
760 QCBOREncode_AddInt64(&ECtx, 22);
761 QCBOREncode_AddInt64(&ECtx, 23);
762 QCBOREncode_AddInt64(&ECtx, 24);
763 QCBOREncode_AddInt64(&ECtx, 25);
764 QCBOREncode_AddInt64(&ECtx, 26);
765 QCBOREncode_AddInt64(&ECtx, 254);
766 QCBOREncode_AddInt64(&ECtx, 255);
767 QCBOREncode_AddInt64(&ECtx, 256);
768 QCBOREncode_AddInt64(&ECtx, 257);
769 QCBOREncode_AddInt64(&ECtx, 65534);
770 QCBOREncode_AddInt64(&ECtx, 65535);
771 QCBOREncode_AddInt64(&ECtx, 65536);
772 QCBOREncode_AddInt64(&ECtx, 65537);
773 QCBOREncode_AddInt64(&ECtx, 65538);
774 QCBOREncode_AddInt64(&ECtx, 2147483647);
775 QCBOREncode_AddInt64(&ECtx, 2147483647);
776 QCBOREncode_AddInt64(&ECtx, 2147483648);
777 QCBOREncode_AddInt64(&ECtx, 2147483649);
778 QCBOREncode_AddInt64(&ECtx, 4294967294);
779 QCBOREncode_AddInt64(&ECtx, 4294967295);
780 QCBOREncode_AddInt64(&ECtx, 4294967296);
781 QCBOREncode_AddInt64(&ECtx, 4294967297);
782 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
783 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
784
785 QCBOREncode_CloseArray(&ECtx);
786
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530787 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700788 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800789 nReturn = -1;
790 }
791
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530792 if(CheckResults(Enc, spExpectedEncodedInts))
793 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794
795 return(nReturn);
796}
797
798
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530799/*
800 85 # array(5)
801 F5 # primitive(21)
802 F4 # primitive(20)
803 F6 # primitive(22)
804 F7 # primitive(23)
805 A1 # map(1)
806 65 # text(5)
807 554E446566 # "UNDef"
808 F7 # primitive(23)
809 */
810static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
812
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813int SimpleValuesTest1()
814{
815 QCBOREncodeContext ECtx;
816 int nReturn = 0;
817
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530818 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819 QCBOREncode_OpenArray(&ECtx);
820
821 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
822 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
823 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
824 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
825
826 QCBOREncode_OpenMap(&ECtx);
827
828 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
829 QCBOREncode_CloseMap(&ECtx);
830
831 QCBOREncode_CloseArray(&ECtx);
832
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530833 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700834 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800835 nReturn = -1;
836 }
837
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530838 if(CheckResults(ECBOR, spExpectedEncodedSimple))
839 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800840
841 return(nReturn);
842}
843
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530844
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530845/*
846 83 # array(3)
847 C0 # tag(0)
848 74 # text(20)
849 323031332D30332D32315432303A30343A30305A # "2013-03-21T20:04:00Z"
850 C1 # tag(1)
851 1A 514B67B0 # unsigned(1363896240)
852 A2 # map(2)
853 78 19 # text(25)
854 53616D706C6520446174652066726F6D205246432033333339 # "Sample Date from RFC 3339"
855 C0 # tag(0)
856 77 # text(23)
857 313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"
858 62 # text(2)
859 5344 # "SD"
860 C1 # tag(1)
861 19 03E7 # unsigned(999)
862 */
863static const uint8_t spExpectedEncodedDates[] = {
864 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33,
865 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a,
866 0x30, 0x30, 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
867 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44,
868 0x61, 0x74, 0x65, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x52,
869 0x46, 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31,
870 0x39, 0x38, 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
871 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30, 0x2e, 0x35,
872 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1, 0x19, 0x03, 0xe7
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800873};
874
875int EncodeDateTest()
876{
877 QCBOREncodeContext ECtx;
878 int nReturn = 0;
879
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530880 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800881
882 QCBOREncode_OpenArray(&ECtx);
883
884
885 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
886 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
887
888
889 QCBOREncode_OpenMap(&ECtx);
890
891 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
892
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800893 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
894
895 QCBOREncode_CloseMap(&ECtx);
896
897 QCBOREncode_CloseArray(&ECtx);
898
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530899 UsefulBufC ECBOR;
900
Laurence Lundblade0595e932018-11-02 22:22:47 +0700901 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800902 nReturn = -1;
903 }
904
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530905 if(CheckResults(ECBOR, spExpectedEncodedDates))
906 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800907
908 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800909}
910
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912int ArrayNestingTest1()
913{
914 QCBOREncodeContext ECtx;
915 int i;
916 int nReturn = 0;
917
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530918 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
920 QCBOREncode_OpenArray(&ECtx);
921 }
922 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
923 QCBOREncode_CloseArray(&ECtx);
924 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700925 UsefulBufC Encoded;
926 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800927 nReturn = -1;
928 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929
930 return(nReturn);
931}
932
933
934
935int ArrayNestingTest2()
936{
937 QCBOREncodeContext ECtx;
938 int i;
939 int nReturn = 0;
940
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530941 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800942 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
943 QCBOREncode_OpenArray(&ECtx);
944 }
945 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
946 QCBOREncode_CloseArray(&ECtx);
947 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530948
Laurence Lundblade0595e932018-11-02 22:22:47 +0700949 UsefulBufC Encoded;
950 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800951 nReturn = -1;
952 }
953
954 return(nReturn);
955}
956
957
958
959int ArrayNestingTest3()
960{
961 QCBOREncodeContext ECtx;
962 int i;
963 int nReturn = 0;
964
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530965 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
967 QCBOREncode_OpenArray(&ECtx);
968 }
969 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
970 QCBOREncode_CloseArray(&ECtx);
971 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700972 UsefulBufC Encoded;
973 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800974 nReturn = -1;
975 }
976
977 return(nReturn);
978}
979
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530981/*
982 81 # array(1)
983 81 # array(1)
984 81 # array(1)
985 81 # array(1)
986 80 # array(0)
987*/
988static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800990// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530991/*
992 82 # array(2)
993 81 # array(1)
994 81 # array(1)
995 81 # array(1)
996 81 # array(1)
997 80 # array(0)
998 98 2F # array(47)
999 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1000 3B 0000000100000000 # negative(4294967296)
1001 3A FFFFFFFF # negative(4294967295)
1002 3A FFFFFFFE # negative(4294967294)
1003 3A FFFFFFFD # negative(4294967293)
1004 3A 7FFFFFFF # negative(2147483647)
1005 3A 7FFFFFFE # negative(2147483646)
1006 3A 00010001 # negative(65537)
1007 3A 00010000 # negative(65536)
1008 39 FFFF # negative(65535)
1009 39 FFFE # negative(65534)
1010 39 FFFD # negative(65533)
1011 39 0100 # negative(256)
1012 38 FF # negative(255)
1013 38 FE # negative(254)
1014 38 FD # negative(253)
1015 38 18 # negative(24)
1016 37 # negative(23)
1017 36 # negative(22)
1018 20 # negative(0)
1019 00 # unsigned(0)
1020 00 # unsigned(0)
1021 01 # unsigned(1)
1022 16 # unsigned(22)
1023 17 # unsigned(23)
1024 18 18 # unsigned(24)
1025 18 19 # unsigned(25)
1026 18 1A # unsigned(26)
1027 18 FE # unsigned(254)
1028 18 FF # unsigned(255)
1029 19 0100 # unsigned(256)
1030 19 0101 # unsigned(257)
1031 19 FFFE # unsigned(65534)
1032 19 FFFF # unsigned(65535)
1033 1A 00010000 # unsigned(65536)
1034 1A 00010001 # unsigned(65537)
1035 1A 00010002 # unsigned(65538)
1036 1A 7FFFFFFF # unsigned(2147483647)
1037 1A 7FFFFFFF # unsigned(2147483647)
1038 1A 80000000 # unsigned(2147483648)
1039 1A 80000001 # unsigned(2147483649)
1040 1A FFFFFFFE # unsigned(4294967294)
1041 1A FFFFFFFF # unsigned(4294967295)
1042 1B 0000000100000000 # unsigned(4294967296)
1043 1B 0000000100000001 # unsigned(4294967297)
1044 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1045 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1046 */
1047static const uint8_t spEncodeRawExpected[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001048 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
1049 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1050 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1051 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1052 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1053 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1054 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1055 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1056 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1057 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1058 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1059 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
1060 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
1061 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
1062 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
1063 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
1064 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
1065 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
1066 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
1067 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
1068 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
1069 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
1070 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
1071 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1072
1073
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074int EncodeRawTest()
1075{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001076 QCBOREncodeContext ECtx;
1077
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301078 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001079 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301080 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1081 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001082 QCBOREncode_CloseArray(&ECtx);
1083
1084 UsefulBufC EncodedRawTest;
1085
Laurence Lundblade0595e932018-11-02 22:22:47 +07001086 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001087 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001088 }
1089
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301090 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001091 return -5;
1092 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001094 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095}
1096
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301097/*
1098 This returns a pointer to spBigBuf
1099 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001100static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1101{
1102 QCBOREncodeContext ECtx;
1103 int nReturn = -1;
1104
1105 *pEncoded = NULL;
1106 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301107 size_t uFirstSizeEstimate = 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001108
1109 // loop runs CBOR encoding twice. First with no buffer to
1110 // calucate the length so buffer can be allocated correctly,
1111 // and last with the buffer to do the actual encoding
1112 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301113 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001114 QCBOREncode_OpenMap(&ECtx);
1115 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1116 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1117 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1118 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1119 QCBOREncode_CloseArray(&ECtx);
1120 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1121 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1122 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1123 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1124 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1125 QCBOREncode_CloseMap(&ECtx);
1126 QCBOREncode_CloseMap(&ECtx);
1127
Laurence Lundblade0595e932018-11-02 22:22:47 +07001128 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129 goto Done;
1130 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301131 if(uFirstSizeEstimate != *pEncodedLen) {
1132 nReturn = 1;
1133 } else {
1134 nReturn = 0;
1135 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 goto Done;
1137 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301138 *pEncoded = spBigBuf;
1139 uFirstSizeEstimate = *pEncodedLen;
1140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141 } while(1);
1142
1143 Done:
1144 return(nReturn);
1145}
1146
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301147/*
1148 A3 # map(3)
1149 6D # text(13)
1150 666972737420696E7465676572 # "first integer"
1151 18 2A # unsigned(42)
1152 77 # text(23)
1153 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1154 82 # array(2)
1155 67 # text(7)
1156 737472696E6731 # "string1"
1157 67 # text(7)
1158 737472696E6732 # "string2"
1159 6C # text(12)
1160 6D617020696E2061206D6170 # "map in a map"
1161 A4 # map(4)
1162 67 # text(7)
1163 62797465732031 # "bytes 1"
1164 44 # bytes(4)
1165 78787878 # "xxxx"
1166 67 # text(7)
1167 62797465732032 # "bytes 2"
1168 44 # bytes(4)
1169 79797979 # "yyyy"
1170 6B # text(11)
1171 616E6F7468657220696E74 # "another int"
1172 18 62 # unsigned(98)
1173 66 # text(6)
1174 746578742032 # "text 2"
1175 78 1E # text(30)
1176 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1177 */
1178static const uint8_t spValidMapEncoded[] = {
1179 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1180 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1181 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1182 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1183 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1184 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1185 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1186 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1187 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1188 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1189 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1190 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1191 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1192 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1193 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1194 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001195
1196
1197int MapEncodeTest()
1198{
1199 uint8_t *pEncodedMaps;
1200 size_t nEncodedMapLen;
1201
1202 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301203 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001204 }
1205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301207 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1208 nReturn = 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001209
1210 return(nReturn);
1211}
1212
1213
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214/*
1215 @brief Encode the RTIC results
1216
1217 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1218 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1219 @param[in] szAlexString Diagnostic code.
1220 @param[in[ pOut Buffer to put the result in
1221 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1222
1223 @return
1224 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1225
1226 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1227 short an error will be returned. This function will never write off the end
1228 of the buffer passed to it.
1229
1230 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1231 length of the encoded CBOR.
1232
1233 */
1234
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301235static UsefulBufC FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001236{
1237 // Buffer that the result will be written in to
1238 // It is fixed size and small that a stack variable will be fine
1239 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1240
1241 // Context for the encoder
1242 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301243 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244
1245 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1246 // Contents are label / value pairs
1247 QCBOREncode_OpenMap(&ECtx);
1248
1249 { // Brace / indention just to show CBOR encoding nesting
1250
1251 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1252 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1253
1254 // Add the diagnostic code
1255 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1256
1257 // Add a time stamp
1258 if(time) {
1259 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1260 }
1261
1262 // Add the diagnostic code
1263 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1264
1265 // Open a subordinate map for telemtry data
1266 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1267
1268 { // Brace / indention just to show CBOR encoding nesting
1269
1270 // Add a few fake integers and buffers for now.
1271 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1272
1273 // Add a few fake integers and buffers for now.
1274 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1275
1276 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301277 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001278 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1279
1280 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1281 }
1282 }
1283
1284 // Close the telemetry map
1285 QCBOREncode_CloseMap(&ECtx);
1286
1287 // Close the map
1288 QCBOREncode_CloseMap(&ECtx);
1289
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301290 UsefulBufC Result;
1291
Laurence Lundblade0595e932018-11-02 22:22:47 +07001292 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301293
1294 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295}
1296
1297
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301298/*
1299 A5 # map(5)
1300 69 # text(9)
1301 696E74656772697479 # "integrity"
1302 F4 # primitive(20)
1303 64 # text(4)
1304 74797065 # "type"
1305 66 # text(6)
1306 726563656E74 # "recent"
1307 64 # text(4)
1308 74696D65 # "time"
1309 C1 # tag(1)
1310 1A 580D4172 # unsigned(1477263730)
1311 64 # text(4)
1312 64696167 # "diag"
1313 6A # text(10)
1314 30784131654335303031 # "0xA1eC5001"
1315 69 # text(9)
1316 74656C656D65747279 # "telemetry"
1317 A3 # map(3)
1318 69 # text(9)
1319 53686F652053697A65 # "Shoe Size"
1320 0C # unsigned(12)
1321 62 # text(2)
1322 4951 # "IQ"
1323 1A FFFFFFFF # unsigned(4294967295)
1324 77 # text(23)
1325 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1326 48 # bytes(8)
1327 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1328 */
1329static const uint8_t spExpectedRTIC[] = {
1330 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1331 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1332 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1333 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1334 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1335 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1336 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1337 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1338 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1339 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1340 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1341 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342
1343
1344int RTICResultsTest()
1345{
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301346 UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
1347 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301348 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301349 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301351 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301353 if(CheckResults(Encoded, spExpectedRTIC)) {
1354 return -2;
1355 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001356
1357 return 0;
1358}
1359
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301360
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301361/*
1362 82 # array(2)
1363 19 01C3 # unsigned(451)
1364 43 # bytes(3)
1365 1901D2 # "\x19\x01\xD2"
1366*/
1367static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301368
Laurence Lundblade684aec22018-10-12 19:33:53 +08001369/*
1370 Very basic bstr wrapping test
1371 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301372int BstrWrapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001373{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001374 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001375
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301376 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001377
Laurence Lundblade684aec22018-10-12 19:33:53 +08001378 QCBOREncode_OpenArray(&EC);
1379 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380
Laurence Lundblade684aec22018-10-12 19:33:53 +08001381 QCBOREncode_BstrWrap(&EC);
1382 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383
Laurence Lundblade684aec22018-10-12 19:33:53 +08001384 UsefulBufC Wrapped;
1385 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386
Laurence Lundblade684aec22018-10-12 19:33:53 +08001387 QCBOREncode_CloseArray(&EC);
1388
1389 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001390 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001391 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001393
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301394 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001395 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001396 }
1397
1398 return 0;
1399}
1400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401
1402
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301403int BstrWrapErrorTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001404{
1405 // -------------- Test closing a bstrwrap when it is an array that is open -----------
Laurence Lundblade684aec22018-10-12 19:33:53 +08001406 QCBOREncodeContext EC;
1407
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301408 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001409
1410 QCBOREncode_OpenArray(&EC);
1411 QCBOREncode_AddUInt64(&EC, 451);
1412
1413 QCBOREncode_BstrWrap(&EC);
1414 QCBOREncode_AddUInt64(&EC, 466);
1415 QCBOREncode_OpenArray(&EC);
1416
1417 UsefulBufC Wrapped;
1418 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1419
1420 QCBOREncode_CloseArray(&EC);
1421
1422 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001423 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001424 return -1;
1425 }
1426
1427 // ----------- test closing a bstrwrap when nothing is open ---------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301428 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001429 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade0595e932018-11-02 22:22:47 +07001430 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001431 return -2;
1432 }
1433
1434 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301435 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001436 for(int i = 1; i < 18; i++) {
1437 QCBOREncode_BstrWrap(&EC);
1438 }
1439 QCBOREncode_AddBool(&EC, true);
1440
1441 for(int i = 1; i < 18; i++) {
1442 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1443 }
1444
Laurence Lundblade0595e932018-11-02 22:22:47 +07001445 if(QCBOREncode_Finish(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001446 return -3;
1447 }
1448
1449 return 0;
1450}
1451
1452
1453
1454// Part of bstr_wrap_nest_test
1455/*
1456 83 array with three
1457 53 byte string with 19 bytes
1458 01 #1
1459 50 byte string with 16 bytes
1460 02
1461 4D byte string with 13 bytes
1462 03
1463 4A byte string with 10 bytes
1464 04
1465 47 byte string with 7 bytes
1466 05
1467 44 byte string with 4 bytes
1468 06
1469 41 byte string with 1 byte
1470 07
1471 01
1472 02
1473 03
1474 04
1475 05
1476 06
1477 07
1478 A2 map with two items
1479 18 20 label for byte string
1480 54 byte string of length 20
1481 82 Array with two items
1482 10 The integer value 10
1483 A2 map with two items
1484 18 21 label for byte string
1485 44 byte string with 4 bytes
1486 81 array with 1 item
1487 11 integer value 11
1488 18 30 integer value 30
1489 18 40 integer label 40
1490 65 68 65 6C 6C 6F text string hello
1491 18 31 integer value 31
1492 18 41 integer label 41
1493 65 68 65 6C 6C 6F text string hello
1494
1495
1496 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301497
1498
1499/*
1500 83 # array(3)
1501 56 # bytes(22)
1502 00530150024D034A0447054406410700010203040506 # "\x00S\x01P\x02M\x03J\x04G\x05D\x06A\a\x00\x01\x02\x03\x04\x05\x06"
1503 07 # unsigned(7)
1504 A2 # map(2)
1505 18 20 # unsigned(32)
1506 54 # bytes(20)
1507 8210A21821448111183018406568656C6C6F1831 # "\x82\x10\xA2\x18!D\x81\x11\x180\x18@ehello\x181"
1508 18 41 # unsigned(65)
1509 65 # text(5)
1510 68656C6C6F # "hello"
1511 */
1512static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08001513{
1514 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1515 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1516 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1517 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1518 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1519 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1520 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1521 0x6F
1522};
1523
1524// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301525static int DecodeNextNested(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001526{
1527 int nReturn;
1528 QCBORDecodeContext DC;
1529 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1530
1531 QCBORItem Item;
1532 nReturn = QCBORDecode_GetNext(&DC, &Item);
1533 if(nReturn) {
1534 return -11;
1535 }
1536 if(Item.uDataType != QCBOR_TYPE_INT64) {
1537 return -12;
1538 }
1539
1540 nReturn = QCBORDecode_GetNext(&DC, &Item);
1541 if(nReturn == QCBOR_ERR_HIT_END) {
1542 return 0;
1543 }
1544 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1545 return -13;
1546 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301547 nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001548 if(nReturn) {
1549 return nReturn;
1550 }
1551
1552 nReturn = QCBORDecode_GetNext(&DC, &Item);
1553 if(nReturn) {
1554 return -14;
1555 }
1556 if(Item.uDataType != QCBOR_TYPE_INT64) {
1557 return -15;
1558 }
1559
1560 if(QCBORDecode_Finish(&DC)) {
1561 return -16;
1562 }
1563
1564 return 0;
1565}
1566
1567// Part of bstr_wrap_nest_test
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301568static int DecodeNextNested2(UsefulBufC Wrapped)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001569{
1570 int nReturn;
1571 QCBORDecodeContext DC;
1572 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1573
1574 QCBORItem Item;
1575 nReturn = QCBORDecode_GetNext(&DC, &Item);
1576 if(nReturn) {
1577 return -11;
1578 }
1579 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1580 return -12;
1581 }
1582
1583 nReturn = QCBORDecode_GetNext(&DC, &Item);
1584 if(nReturn) {
1585 return -11;
1586 }
1587 if(Item.uDataType != QCBOR_TYPE_INT64) {
1588 return -12;
1589 }
1590
1591 nReturn = QCBORDecode_GetNext(&DC, &Item);
1592 if(nReturn) {
1593 return -11;
1594 }
1595 if(Item.uDataType != QCBOR_TYPE_MAP) {
1596 return 0;
1597 }
1598
1599 nReturn = QCBORDecode_GetNext(&DC, &Item);
1600 if(nReturn) {
1601 return -11;
1602 }
1603 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1604 return -13;
1605 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301606 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001607 if(nReturn) {
1608 return nReturn;
1609 }
1610
1611 nReturn = QCBORDecode_GetNext(&DC, &Item);
1612 if(nReturn) {
1613 return -11;
1614 }
1615 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1616 return -12;
1617 }
1618 nReturn = QCBORDecode_GetNext(&DC, &Item);
1619 if(nReturn) {
1620 return -11;
1621 }
1622 if(Item.uDataType != QCBOR_TYPE_INT64) {
1623 return -12;
1624 }
1625
1626 if(QCBORDecode_Finish(&DC)) {
1627 return -16;
1628 }
1629
1630 return 0;
1631}
1632
1633
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301634int BstrWrapNestTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001635{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001636 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301637 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001638
1639 // ---- Make a complicated nested CBOR structure ---
1640 QCBOREncode_OpenArray(&EC);
1641
1642 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1643 QCBOREncode_BstrWrap(&EC);
1644 QCBOREncode_AddUInt64(&EC, i);
1645 }
1646
1647 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1648 QCBOREncode_CloseBstrWrap(&EC, NULL);
1649 QCBOREncode_AddUInt64(&EC, i);
1650 }
1651
1652 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1653 QCBOREncode_OpenMap(&EC);
1654 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1655 QCBOREncode_OpenArray(&EC);
1656 QCBOREncode_AddUInt64(&EC, i+0x10);
1657 }
1658
1659 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1660 QCBOREncode_CloseArray(&EC);
1661 QCBOREncode_AddUInt64(&EC, i+0x30);
1662 QCBOREncode_CloseBstrWrap(&EC, NULL);
1663 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1664 QCBOREncode_CloseMap(&EC);
1665 }
1666 QCBOREncode_CloseArray(&EC);
1667
1668 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001669 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001670 return -1;
1671 }
1672
1673 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301674 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001675 return -25;
1676 }
1677
1678
1679 // ---- Decode it and see if it is OK ------
1680 QCBORDecodeContext DC;
1681 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1682
1683 QCBORItem Item;
1684 QCBORDecode_GetNext(&DC, &Item);
1685 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1686 return -2;
1687 }
1688
1689 QCBORDecode_GetNext(&DC, &Item);
1690 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1691 return -3;
1692 }
1693
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301694 int nReturn = DecodeNextNested(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001695 if(nReturn) {
1696 return nReturn;
1697 }
1698
1699 nReturn = QCBORDecode_GetNext(&DC, &Item);
1700 if(nReturn) {
1701 return -11;
1702 }
1703 if(Item.uDataType != QCBOR_TYPE_INT64) {
1704 return -12;
1705 }
1706
1707 QCBORDecode_GetNext(&DC, &Item);
1708 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1709 return -2;
1710 }
1711
1712 QCBORDecode_GetNext(&DC, &Item);
1713 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1714 return -3;
1715 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301716 nReturn = DecodeNextNested2(Item.val.string);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001717 if(nReturn) {
1718 return nReturn;
1719 }
1720
1721 nReturn = QCBORDecode_GetNext(&DC, &Item);
1722 if(nReturn) {
1723 return -11;
1724 }
1725 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1726 return -12;
1727 }
1728
1729 if(QCBORDecode_Finish(&DC)) {
1730 return -16;
1731 }
1732
1733 return 0;
1734}
1735
1736
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301737static const uint8_t spSignature[] = {
1738 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1739 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1740 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1741 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1742 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1743 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1744 0x45, 0xca, 0xcb, 0x36};
1745
1746/*
1747 D2 # tag(18)
1748 84 # array(4)
1749 43 # bytes(3)
1750 A10126 # "\xA1\x01&"
1751 A1 # map(1)
1752 04 # unsigned(4)
1753 42 # bytes(2)
1754 3131 # "11"
1755 54 # bytes(20)
1756 546869732069732074686520636F6E74656E742E # "This is the content."
1757 58 40 # bytes(64)
1758 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4\xCCk#\xD5\x8F\xEF\\\b1\x06\xC4\xD2Z\x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91\xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01\xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA\xCB6"
1759 */
1760static const uint8_t spExpected[] = {
1761 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1762 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1763 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1764 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1765 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1766 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1767 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1768 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1769 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1770 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1771
Laurence Lundblade684aec22018-10-12 19:33:53 +08001772/*
1773 this corresponds exactly to the example in RFC 8152
1774 section C.2.1. This doesn't actually verify the signature
1775 though that would be nice as it would make the test
1776 really good. That would require bring in ECDSA crypto
1777 to this test.
1778 */
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301779int CoseSign1TBSTest()
Laurence Lundblade684aec22018-10-12 19:33:53 +08001780{
1781 // All of this is from RFC 8152 C.2.1
1782 const char *szKid = "11";
1783 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1784 const char *szPayload = "This is the content.";
1785 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301786 static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301787 UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301788
Laurence Lundblade684aec22018-10-12 19:33:53 +08001789 // It would be good to compare this to the output from
1790 // a COSE implementation like COSE-C. It has been checked
1791 // against the CBOR playground.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301792 UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSignature);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001793
Laurence Lundblade684aec22018-10-12 19:33:53 +08001794 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301795 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001796
1797 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladecafcfe12018-10-31 21:59:50 +07001798 QCBOREncode_AddTag(&EC, 18); // TODO: replace with constant
1799 QCBOREncode_OpenArray_2(&EC, NULL, QCBOR_NO_INT_LABEL); // TODO: _2
Laurence Lundblade684aec22018-10-12 19:33:53 +08001800
1801 // Add protected headers
1802 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1803
1804 // Empty map with unprotected headers
1805 QCBOREncode_OpenMap(&EC);
1806 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1807 QCBOREncode_CloseMap(&EC);
1808
1809 // The payload
1810 UsefulBufC WrappedPayload;
1811 QCBOREncode_BstrWrap(&EC);
1812 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1813 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1814
1815 // Check we got back the actual payload expected
1816 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1817 return -1;
1818 }
1819
1820 // The signature
1821 QCBOREncode_AddBytes(&EC, Signature);
1822 QCBOREncode_CloseArray(&EC);
1823
1824 // Finish and check the results
1825 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001826 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001827 return -2;
1828 }
1829
1830 // 98 is the size from RFC 8152 C.2.1
1831 if(COSE_Sign1.len != 98) {
1832 return -3;
1833 }
1834
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301835 if(CheckResults(COSE_Sign1, spExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001836 return -4;
1837 }
1838
1839 return 0;
1840}
1841