blob: 000d416f77ba07c77fee5985b3bd7d97a61dc1b1 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056#include "qcbor.h"
57#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058#include <strings.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059#include <stdlib.h>
60
61
62
63// TODO: -- test on a 32-bit machine)
64
65// TODO: test QCBOR_MAX_ITEMS_IN_ARRAY (this is very large...)
66
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053067#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
68#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070// ifdef these out to not have compiler warnings
71static void printencoded(const uint8_t *pEncoded, size_t nLen)
72{
73 int i;
74 for(i = 0; i < nLen; i++) {
75 uint8_t Z = pEncoded[i];
76 printf("%02x ", Z);
77 }
78 printf("\n");
79
80 fflush(stdout);
81}
82
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080083#endif
84
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053085#include <stdio.h>
86
87int Compare(UsefulBufC U1, UsefulBufC U2) {
88 int i;
89 for(i = 0; i < U1.len; i++) {
90 if(((uint8_t *)U1.ptr)[i] != ((uint8_t *)U2.ptr)[i]) {
91 printf("%d 0x%x 0x%x\n", i, ((uint8_t *)U1.ptr)[i], ((uint8_t *)U2.ptr)[i]);
92 return 1;
93 }
94 }
95 return 0;
96
97}
98
99
100/*#define CheckResults(Enc, Expected) \
101 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)}) */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102
103#define CheckResults(Enc, Expected) \
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530104 Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105
106
107static const uint8_t pExpectedEncodedAll[] = {
108
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530109 0x98, 0x29, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
110 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
111 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
112 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
113 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
114 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
115 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
116 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
117 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
118 0xaf, 0x07, 0xff, 0x65, 0x4a, 0x61, 0x69, 0x6d, 0x65, 0xd8,
119 0x58, 0xfa, 0x40, 0x49, 0x0f, 0xd0, 0x66, 0x53, 0x74, 0x72,
120 0x65, 0x65, 0x74, 0xd8, 0x63, 0xfb, 0x40, 0x21, 0x4f, 0x01,
121 0x96, 0xd8, 0xf4, 0xf9, 0xfa, 0x3f, 0x80, 0x00, 0x00, 0xfb,
122 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x63,
123 0x66, 0x6f, 0x6f, 0xfa, 0x45, 0x98, 0xb8, 0x00, 0x39, 0x03,
124 0xe6, 0xfa, 0x44, 0x79, 0xc0, 0x00, 0x66, 0x66, 0x6f, 0x6f,
125 0x66, 0x6f, 0x6f, 0xfb, 0x41, 0x58, 0xf7, 0x7d, 0xc0, 0x00,
126 0x00, 0x00, 0x39, 0xaf, 0xc6, 0xfb, 0x40, 0xf5, 0xba, 0x70,
127 0x00, 0x00, 0x00, 0x00, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
128 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
129 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
130 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
131 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
132 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
133 0x42, 0xff, 0x00, 0xa3, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
134 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00, 0x66, 0x62,
135 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
136 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
137 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
138 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0xd8, 0x20, 0x78, 0x6b,
139 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
140 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
141 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
142 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
143 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
144 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
145 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
146 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
147 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
148 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
149 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
150 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
151 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
152 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
153 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd8,
154 0x24, 0x79, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
155 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
156 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
157 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
158 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
159 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
160 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
161 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
162 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
163 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
164 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
165 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
166 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
167 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
168 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
169 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
170 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
171 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
172 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
173 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
174 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
175 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
176 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
177 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
178 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
179 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
180 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
181 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
182 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
183 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
184 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
185 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
186 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
187 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
188 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
189 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
190 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
191 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
192 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
193 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
194 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
195 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
196 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
197 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
198 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
199 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
200 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
201 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
202 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
203 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
204 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
205 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
206 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
207 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
208 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
209 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
210 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
211 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
212 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
213 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd8, 0x24, 0x79, 0x01,
214 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
215 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
216 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
217 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
218 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
219 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
220 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
221 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
222 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
223 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
224 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
225 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
226 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
227 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
228 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
229 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
230 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
231 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
232 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
233 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
234 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
235 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
236 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
237 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
238 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
239 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
240 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
241 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
242 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
243 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
244 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
245 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
246 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
247 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
248 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd8, 0x24, 0x79, 0x01, 0x57,
249 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
250 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
251 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
252 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
253 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
254 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
255 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
256 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
257 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
258 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
259 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
260 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
261 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
262 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
263 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
264 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
265 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
266 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
267 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
268 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
269 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
270 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
271 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
272 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
273 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
274 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
275 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
276 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
277 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
278 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
279 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
280 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
281 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
282 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
283 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
284 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
285 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
286 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
287 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
288 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
289 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
290 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
291 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
292 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
293 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
294 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
295 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
296 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
297 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
298 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
299 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
300 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
301 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
302 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
303 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
304 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
305 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
306 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
307 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
308 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
309 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
310 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
311 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
312 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
313 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
314 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
315 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
317 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
318 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
321 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
322 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324};
325
326
327static const char *szMIME = "\
328MIME-Version: 1.0\n\
329Content-Type: multipart/mixed;\n\
330boundary=\"XXXXboundary text\"\n\
331\n\
332This is a multipart message in MIME format.\n\
333\n\
334--XXXXboundary text\n\
335Content-Type: text/plain\n\
336\n\
337this is the body text\n\
338\n\
339--XXXXboundary text\n\
340Content-Type: text/plain;\n\
341Content-Disposition: attachment;\n\
342filename=\"test.txt\"\n\
343\n\
344this is the attachment text\n\
345\n\
346--XXXXboundary text--";
347
348
349int AllAddMethodsTest()
350{
351 QCBOREncodeContext ECtx;
352 int nReturn = 0;
353
354 uint8_t pEncoded[3000];
355 size_t nEncodedLen = sizeof(pEncoded);
356
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530357 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358
359 QCBOREncode_OpenArray(&ECtx);
360
361 // Non-map ints
362 QCBOREncode_AddUInt64_3(&ECtx, "UINT62", QCBOR_NO_INT_LABEL, 100, 89989909);
363 QCBOREncode_AddInt64_3(&ECtx, "INT64", QCBOR_NO_INT_LABEL, 76, 77689989909);
364 QCBOREncode_AddUInt64(&ECtx,0);
365 QCBOREncode_AddInt64(&ECtx, -44);
366
367 // ints that go in maps
368 QCBOREncode_OpenMap(&ECtx);
369 QCBOREncode_AddUInt64ToMap(&ECtx, "LBL", 77);
370 QCBOREncode_AddUInt64ToMapN(&ECtx, -4, 88);
371 QCBOREncode_AddInt64ToMap(&ECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
372 QCBOREncode_AddInt64ToMapN(&ECtx, -100000000, -800000000);
373 QCBOREncode_CloseMap(&ECtx);
374
375 // floats and doubles
376 QCBOREncode_AddFloat_3(&ECtx, "Jaime", QCBOR_NO_INT_LABEL, 88, 3.14159);
377 QCBOREncode_AddDouble_3(&ECtx, "Street", QCBOR_NO_INT_LABEL, 99, 8.654309);
378 QCBOREncode_AddFloat(&ECtx, 1);
379 QCBOREncode_AddDouble(&ECtx, 1);
380
381 // floats and doubles that go in map
382 QCBOREncode_OpenMap(&ECtx);
383 QCBOREncode_AddFloatToMap(&ECtx, "foo", 4887);
384 QCBOREncode_AddFloatToMapN(&ECtx, -999, 999);
385 QCBOREncode_AddDoubleToMap(&ECtx, "foofoo", 6544887);
386 QCBOREncode_AddDoubleToMapN(&ECtx, -44999, 88999);
387 QCBOREncode_CloseMap(&ECtx);
388
389 // Epoch Date
390 QCBOREncode_AddDateEpoch(&ECtx, 2383748234);
391
392 // Epoch date with labels
393 QCBOREncode_OpenMap(&ECtx);
394 QCBOREncode_AddDateEpoch_2(&ECtx, "LongLiveDenisRitchie", QCBOR_NO_INT_LABEL, 1400000000);
395 QCBOREncode_AddDateEpochToMap(&ECtx, "time()", 1477263730);
396 QCBOREncode_AddDateEpochToMapN(&ECtx, -1969, 1477263222);
397 QCBOREncode_CloseMap(&ECtx);
398
399 // Binary blobs
400 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
401
402 // binary blobs in maps
403 QCBOREncode_OpenMap(&ECtx);
404 QCBOREncode_AddBytes_3(&ECtx, "binbin", QCBOR_NO_INT_LABEL, 100000, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
405 QCBOREncode_AddBytesToMap(&ECtx, "blabel", ((UsefulBufC){(uint8_t []){0x01, 0x02, 0x03}, 3}));
406 QCBOREncode_AddBytesToMapN(&ECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
407 QCBOREncode_CloseMap(&ECtx);
408
409 // text blobs
410 QCBOREncode_AddText(&ECtx, SZLiteralToUsefulBufC("bar bar foo bar"));
411 QCBOREncode_AddSZString(&ECtx, "oof\n");
412 QCBOREncode_AddURI(&ECtx, SZLiteralToUsefulBufC("http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8"));
413 QCBOREncode_AddB64Text(&ECtx, SZLiteralToUsefulBufC("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
414 QCBOREncode_AddRegex(&ECtx, SZLiteralToUsefulBufC("[^abc]+"));
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530415 QCBOREncode_AddMIMEData(&ECtx, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800416
417 // text blobs in maps
418 QCBOREncode_OpenMap(&ECtx);
419 QCBOREncode_AddTextToMap(&ECtx, "#####", SZLiteralToUsefulBufC("foo bar foo foo"));
420 QCBOREncode_AddText_3(&ECtx, "____", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, SZLiteralToUsefulBufC("foo bar"));
421 QCBOREncode_AddSZString_3(&ECtx, "()()()", QCBOR_NO_INT_LABEL, 1000, "rab rab oof");
422 QCBOREncode_AddTextToMapN(&ECtx,22, SZLiteralToUsefulBufC("foo foo foo foo"));
423 QCBOREncode_AddSZStringToMap(&ECtx, "^^", "oooooooof");
424 QCBOREncode_AddSZStringToMapN(&ECtx, 99, "ffffoooooooof");
425 QCBOREncode_AddURIToMap(&ECtx, "RFC", SZLiteralToUsefulBufC("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
426 QCBOREncode_AddURIToMapN(&ECtx, 0x89, SZLiteralToUsefulBufC("http://cbor.me/"));
427 QCBOREncode_AddB64TextToMap(&ECtx, "whenim64", SZLiteralToUsefulBufC("cGxlYXN1cmUu"));
428 QCBOREncode_AddB64TextToMapN(&ECtx, 64, SZLiteralToUsefulBufC("c3VyZS4="));
429 QCBOREncode_AddRegexToMap(&ECtx, "popo", SZLiteralToUsefulBufC("100\\s*mk")); // x code string literal bug
430 QCBOREncode_AddRegexToMapN(&ECtx, -51, SZLiteralToUsefulBufC("perl\\B")); // x code string literal bug
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530431 QCBOREncode_AddMIMEDataToMap(&ECtx, "Ned", UsefulBuf_FromSZ(szMIME));
432 QCBOREncode_AddMIMEDataToMapN(&ECtx, 10, UsefulBuf_FromSZ(szMIME));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 QCBOREncode_CloseMap(&ECtx);
434
435 // Date strings
436 QCBOREncode_AddDateString(&ECtx, "2003-12-13T18:30:02Z");
437 QCBOREncode_OpenMap(&ECtx);
438 QCBOREncode_AddDateStringToMap(&ECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
439 QCBOREncode_AddDateStringToMapN(&ECtx, 88, "2003-12-13T18:30:02.25+01:00");
440 QCBOREncode_CloseMap(&ECtx);
441
442 // true / false ...
443 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
444 QCBOREncode_OpenMap(&ECtx);
445 QCBOREncode_AddSimple_3(&ECtx, "dare", QCBOR_NO_INT_LABEL, 66, CBOR_SIMPLEV_TRUE);
446 QCBOREncode_AddSimpleToMap(&ECtx, "uu", CBOR_SIMPLEV_FALSE);
447 QCBOREncode_AddSimpleToMapN(&ECtx, 737634, CBOR_SIMPLEV_NULL);
448 QCBOREncode_CloseMap(&ECtx);
449
450 // opening an array
451 QCBOREncode_OpenArray(&ECtx);
452 QCBOREncode_CloseArray(&ECtx);
453
454 // opening arrays in a map
455 QCBOREncode_OpenMap(&ECtx);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530456 QCBOREncode_OpenArray_3(&ECtx, "label and tagged empty array", QCBOR_NO_INT_LABEL, 1093);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800457 QCBOREncode_CloseArray(&ECtx);
458 QCBOREncode_OpenArrayInMap(&ECtx, "alabl");
459 QCBOREncode_CloseArray(&ECtx);
460 QCBOREncode_OpenArrayInMapN(&ECtx, 42);
461 QCBOREncode_CloseArray(&ECtx);
462 QCBOREncode_CloseMap(&ECtx);
463
464 // opening maps with labels and tagging
465 QCBOREncode_OpenMap(&ECtx);
466 QCBOREncode_OpenMapInMap(&ECtx, "in a map");
467 QCBOREncode_OpenMapInMapN(&ECtx, 5556);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530468 QCBOREncode_OpenMap_3(&ECtx, "in a in a in a", QCBOR_NO_INT_LABEL, 9087);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469 QCBOREncode_CloseMap(&ECtx);
470 QCBOREncode_CloseMap(&ECtx);
471 QCBOREncode_CloseMap(&ECtx);
472 QCBOREncode_CloseMap(&ECtx);
473
474 // Extended simple values (these are not standard...)
475 QCBOREncode_OpenMap(&ECtx);
476 QCBOREncode_AddRawSimple_3(&ECtx, "s1", QCBOR_NO_INT_LABEL, 88, 255);
477 QCBOREncode_AddRawSimple_3(&ECtx, "s2", QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 0);
478 QCBOREncode_AddRawSimple_3(&ECtx, "s3", QCBOR_NO_INT_LABEL, 88, 33);
479 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 88378374, 88, 255);
480 QCBOREncode_AddRawSimple_3(&ECtx, NULL, 89, 88, 19);
481 QCBOREncode_CloseMap(&ECtx);
482
483
484 // UUIDs
485 static uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32};
486 UsefulBufC XXUUID = ByteArrayLiteralToUsefulBufC(ppppUUID);
487 QCBOREncode_AddBinaryUUID(&ECtx, XXUUID);
488 QCBOREncode_OpenMap(&ECtx);
489 QCBOREncode_AddBinaryUUIDToMap(&ECtx, "UUUU", XXUUID);
490 QCBOREncode_AddBinaryUUIDToMapN(&ECtx, 99, XXUUID);
491 QCBOREncode_CloseMap(&ECtx);
492
493
494 // Bool
495 QCBOREncode_AddBool(&ECtx, true);
496 QCBOREncode_AddBool(&ECtx, false);
497 QCBOREncode_OpenMap(&ECtx);
498 QCBOREncode_AddBoolToMap(&ECtx, "George is the man", true);
499 QCBOREncode_AddBoolToMapN(&ECtx, 010101, true);
500 QCBOREncode_CloseMap(&ECtx);
501
502
503 static uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
504 UsefulBufC BIGNUM = ByteArrayLiteralToUsefulBufC(pBignum);
505 QCBOREncode_AddPositiveBignum(&ECtx, BIGNUM);
506 QCBOREncode_AddNegativeBignum(&ECtx, BIGNUM);
507 QCBOREncode_OpenMap(&ECtx);
508 QCBOREncode_AddPositiveBignumToMap(&ECtx, "BN+", BIGNUM);
509 QCBOREncode_AddPositiveBignumToMapN(&ECtx, 64, BIGNUM);
510 QCBOREncode_AddNegativeBignumToMap(&ECtx, "BN-", BIGNUM);
511 QCBOREncode_AddNegativeBignumToMapN(&ECtx, -64, BIGNUM);
512 QCBOREncode_CloseMap(&ECtx);
513
514 QCBOREncode_CloseArray(&ECtx);
515
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530516 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517
518 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
519 nReturn = -1;
520 goto Done;
521 }
522
523 //printencodedE(Enc);
524
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530525 if(CheckResults(Enc, pExpectedEncodedAll))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800526 nReturn = -1;
527
528Done:
529 return nReturn;
530}
531
532// todo -- add a test for counting the top level items and adding it back in with AddRaw()
533
534
535static const uint8_t pExpectedEncodedInts[] = {
536 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
537 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
538 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
539 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
540 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
541 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
542 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
543 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
544 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
545 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
546 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
547 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
548 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
549 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
550 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
551 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
552 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
553 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
554 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
555 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
556 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
557 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
558 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
559 0xff, 0xff};
560
561/*
562
563 Test the generation of integers. This also ends up testing
564 encoding of all the different lengths. It encodes integers
565 of many lengths and values, especially around the boundaries
566 for different types of integers. It compares the output
567 to expected values generated from http://cbor.me.
568
569 */
570int IntegerValuesTest1()
571{
572 QCBOREncodeContext ECtx;
573 int nReturn = 0;
574
575 uint8_t pEncoded[1000];
576 size_t nEncodedLen = sizeof(pEncoded);
577
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530578 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 QCBOREncode_OpenArray(&ECtx);
580
581 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
582 QCBOREncode_AddInt64(&ECtx, -4294967297);
583 QCBOREncode_AddInt64(&ECtx, -4294967296);
584 QCBOREncode_AddInt64(&ECtx, -4294967295);
585 QCBOREncode_AddInt64(&ECtx, -4294967294);
586 QCBOREncode_AddInt64(&ECtx, -2147483648);
587 QCBOREncode_AddInt64(&ECtx, -2147483647);
588 QCBOREncode_AddInt64(&ECtx, -65538);
589 QCBOREncode_AddInt64(&ECtx, -65537);
590 QCBOREncode_AddInt64(&ECtx, -65536);
591 QCBOREncode_AddInt64(&ECtx, -65535);
592 QCBOREncode_AddInt64(&ECtx, -65534);
593 QCBOREncode_AddInt64(&ECtx, -257);
594 QCBOREncode_AddInt64(&ECtx, -256);
595 QCBOREncode_AddInt64(&ECtx, -255);
596 QCBOREncode_AddInt64(&ECtx, -254);
597 QCBOREncode_AddInt64(&ECtx, -25);
598 QCBOREncode_AddInt64(&ECtx, -24);
599 QCBOREncode_AddInt64(&ECtx, -23);
600 QCBOREncode_AddInt64(&ECtx, -1);
601 QCBOREncode_AddInt64(&ECtx, 0);
602 QCBOREncode_AddUInt64(&ECtx, 0ULL);
603 QCBOREncode_AddInt64(&ECtx, 1);
604 QCBOREncode_AddInt64(&ECtx, 22);
605 QCBOREncode_AddInt64(&ECtx, 23);
606 QCBOREncode_AddInt64(&ECtx, 24);
607 QCBOREncode_AddInt64(&ECtx, 25);
608 QCBOREncode_AddInt64(&ECtx, 26);
609 QCBOREncode_AddInt64(&ECtx, 254);
610 QCBOREncode_AddInt64(&ECtx, 255);
611 QCBOREncode_AddInt64(&ECtx, 256);
612 QCBOREncode_AddInt64(&ECtx, 257);
613 QCBOREncode_AddInt64(&ECtx, 65534);
614 QCBOREncode_AddInt64(&ECtx, 65535);
615 QCBOREncode_AddInt64(&ECtx, 65536);
616 QCBOREncode_AddInt64(&ECtx, 65537);
617 QCBOREncode_AddInt64(&ECtx, 65538);
618 QCBOREncode_AddInt64(&ECtx, 2147483647);
619 QCBOREncode_AddInt64(&ECtx, 2147483647);
620 QCBOREncode_AddInt64(&ECtx, 2147483648);
621 QCBOREncode_AddInt64(&ECtx, 2147483649);
622 QCBOREncode_AddInt64(&ECtx, 4294967294);
623 QCBOREncode_AddInt64(&ECtx, 4294967295);
624 QCBOREncode_AddInt64(&ECtx, 4294967296);
625 QCBOREncode_AddInt64(&ECtx, 4294967297);
626 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
627 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
628
629 QCBOREncode_CloseArray(&ECtx);
630
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530631 UsefulBufC Enc;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632 if(QCBOREncode_Finish2(&ECtx, &Enc)) {
633 nReturn = -1;
634 }
635
636 if(CheckResults(Enc, pExpectedEncodedInts))
637 return -1;
638
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530639 if(Enc.len != sizeof(pExpectedEncodedInts) || bcmp(pEncoded, pExpectedEncodedInts, Enc.len))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640 nReturn = -1;
641
642 //printencoded(pEncoded, nEncodedLen);
643
644 return(nReturn);
645}
646
647
648
649static uint8_t pExpectedEncodedSimple[] = {
650 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
651
652
653int SimpleValuesTest1()
654{
655 QCBOREncodeContext ECtx;
656 int nReturn = 0;
657
658 uint8_t pEncoded[100];
659 size_t nEncodedLen = sizeof(pEncoded);
660
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530661 QCBOREncode_Init(&ECtx, (UsefulBuf) {pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 QCBOREncode_OpenArray(&ECtx);
663
664 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_TRUE);
665 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_FALSE);
666 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_NULL);
667 QCBOREncode_AddSimple(&ECtx, CBOR_SIMPLEV_UNDEF);
668
669 QCBOREncode_OpenMap(&ECtx);
670
671 QCBOREncode_AddSimpleToMap(&ECtx, "UNDef", CBOR_SIMPLEV_UNDEF);
672 QCBOREncode_CloseMap(&ECtx);
673
674 QCBOREncode_CloseArray(&ECtx);
675
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530676 UsefulBufC ECBOR;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800677 if(QCBOREncode_Finish2(&ECtx, &ECBOR)) {
678 nReturn = -1;
679 }
680
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530681 if(ECBOR.len != sizeof(pExpectedEncodedSimple) || bcmp(pEncoded, pExpectedEncodedSimple, ECBOR.len))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800682 nReturn = -1;
683
684 // printencoded(pEncoded, nEncodedLen);
685
686 return(nReturn);
687}
688
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530689
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691static uint8_t pExpectedEncodedDates[] = {
692 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54,
693 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30,
694 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
695 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65,
696 0x20, 0x44, 0x61, 0x74, 0x65, 0x20, 0x66, 0x72,
697 0x6f, 0x6d, 0x20, 0x52, 0x46, 0x43, 0x20, 0x33,
698 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, 0x39, 0x38,
699 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
700 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30,
701 0x2e, 0x35, 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1,
702 0x19, 0x03, 0xe7
703};
704
705int EncodeDateTest()
706{
707 QCBOREncodeContext ECtx;
708 int nReturn = 0;
709
710 uint8_t pEncoded[1000];
711 size_t nEncodedLen = sizeof(pEncoded);
712
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530713 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800714
715 QCBOREncode_OpenArray(&ECtx);
716
717
718 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
719 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
720
721
722 QCBOREncode_OpenMap(&ECtx);
723
724 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
725
726
727 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
728
729 QCBOREncode_CloseMap(&ECtx);
730
731 QCBOREncode_CloseArray(&ECtx);
732
733
734 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
735 nReturn = -1;
736 }
737
738 if(nEncodedLen != sizeof(pExpectedEncodedDates) || bcmp(pEncoded, pExpectedEncodedDates, nEncodedLen))
739 nReturn = -1;
740
741 //printencoded(pEncoded, nEncodedLen);
742
743 return(nReturn);
744
745}
746
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800747int ArrayNestingTest1()
748{
749 QCBOREncodeContext ECtx;
750 int i;
751 int nReturn = 0;
752
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530753 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530755 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
757 QCBOREncode_OpenArray(&ECtx);
758 }
759 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
760 QCBOREncode_CloseArray(&ECtx);
761 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530762 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800763 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
764 printf("ArrayNestingTest1 Failed\n");
765 nReturn = -1;
766 }
767 //printencoded(pEncoded, nEncodedLen);
768
769 return(nReturn);
770}
771
772
773
774int ArrayNestingTest2()
775{
776 QCBOREncodeContext ECtx;
777 int i;
778 int nReturn = 0;
779
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530780 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800781
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530782 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
784 QCBOREncode_OpenArray(&ECtx);
785 }
786 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
787 QCBOREncode_CloseArray(&ECtx);
788 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530789
790 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800791 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
792 nReturn = -1;
793 }
794
795 return(nReturn);
796}
797
798
799
800int ArrayNestingTest3()
801{
802 QCBOREncodeContext ECtx;
803 int i;
804 int nReturn = 0;
805
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530806 UsefulBuf_MakeStackUB(Encode, 100);
807
808 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800809 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
810 QCBOREncode_OpenArray(&ECtx);
811 }
812 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
813 QCBOREncode_CloseArray(&ECtx);
814 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530815 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800816 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
817 nReturn = -1;
818 }
819
820 return(nReturn);
821}
822
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530823#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800824
825
826static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
827
828static int EncodeRaw(uint8_t **pEncoded, size_t *pEncodedLen)
829{
830 QCBOREncodeContext ECtx;
831 int nReturn = -1;
832
833 *pEncoded = NULL;
834 *pEncodedLen = INT32_MAX; // largest buffer this CBOR implementation will deal with
835
836 // loop runs CBOR encoding twice. First with no buffer to
837 // calucate the length so buffer can be allocated correctly,
838 // and last with the buffer to do the actual encoding
839 do {
840 QCBOREncode_Init(&ECtx, *pEncoded, *pEncodedLen);
841 QCBOREncode_OpenArray(&ECtx);
842 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {s_pFiveArrarys, 5}), 1});
843 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}), 1});
844 QCBOREncode_CloseArray(&ECtx);
845
846 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
847 goto Done;
848 if(*pEncoded != NULL) {
849 nReturn = 0;
850 goto Done;
851 }
852 *pEncoded = malloc(*pEncodedLen);
853 } while(1);
854
855Done:
856 return(nReturn);
857}
858
859
860// Validated at http://cbor.me and by manually examining its output
861static uint8_t s_pEncodeRawExpected[] = {
862 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
863 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
864 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
865 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
866 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
867 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
868 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
869 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
870 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
871 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
872 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
873 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
874 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
875 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
876 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
877 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
878 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
879 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
880 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
881 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
882 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
883 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
884 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
885 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
886
887
888
889int EncodeRawTest()
890{
891 uint8_t *pEncodedRaw;
892 size_t nEncodedRawLen;
893
894 if(EncodeRaw(&pEncodedRaw, &nEncodedRawLen)) {
895 return(-1);
896 }
897
898 //printencoded(pEncodedRaw, nEncodedRawLen);
899
900 int nReturn = 0;
901 if(nEncodedRawLen != sizeof(s_pEncodeRawExpected) || memcmp(s_pEncodeRawExpected, pEncodedRaw, sizeof(s_pEncodeRawExpected)))
902 nReturn = 1;
903
904 return(nReturn);
905}
906
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530907#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908
909
910
911static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
912{
913 QCBOREncodeContext ECtx;
914 int nReturn = -1;
915
916 *pEncoded = NULL;
917 *pEncodedLen = INT32_MAX;
918
919 // loop runs CBOR encoding twice. First with no buffer to
920 // calucate the length so buffer can be allocated correctly,
921 // and last with the buffer to do the actual encoding
922 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530923 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 QCBOREncode_OpenMap(&ECtx);
925 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
926 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
927 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
928 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
929 QCBOREncode_CloseArray(&ECtx);
930 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
931 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
932 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
933 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
934 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
935 QCBOREncode_CloseMap(&ECtx);
936 QCBOREncode_CloseMap(&ECtx);
937
938
939 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
940 goto Done;
941 if(*pEncoded != NULL) {
942 nReturn = 0;
943 goto Done;
944 }
945 *pEncoded = malloc(*pEncodedLen);
946 } while(1);
947
948 Done:
949 return(nReturn);
950}
951
952
953static uint8_t pValidMapEncoded[] = {
954 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a,
955 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, 0x6f, 0x20,
956 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x67,
957 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20,
958 0x6d, 0x61, 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, 0x44, 0x78, 0x78, 0x78, 0x78,
959 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f,
960 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
961 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
962 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 } ;
963
964
965
966int MapEncodeTest()
967{
968 uint8_t *pEncodedMaps;
969 size_t nEncodedMapLen;
970
971 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
972 return(-1);
973 }
974
975 //printencoded(pEncodedMaps, nEncodedMapLen);
976
977 int nReturn = 0;
978 if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded)))
979 nReturn = 1;
980
981 return(nReturn);
982}
983
984
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800985/*
986 @brief Encode the RTIC results
987
988 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
989 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
990 @param[in] szAlexString Diagnostic code.
991 @param[in[ pOut Buffer to put the result in
992 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
993
994 @return
995 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
996
997 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
998 short an error will be returned. This function will never write off the end
999 of the buffer passed to it.
1000
1001 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1002 length of the encoded CBOR.
1003
1004 */
1005
1006int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen)
1007{
1008 // Buffer that the result will be written in to
1009 // It is fixed size and small that a stack variable will be fine
1010 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1011
1012 // Context for the encoder
1013 QCBOREncodeContext ECtx;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301014 QCBOREncode_Init(&ECtx, (UsefulBuf){pOut, *pnLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015
1016 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1017 // Contents are label / value pairs
1018 QCBOREncode_OpenMap(&ECtx);
1019
1020 { // Brace / indention just to show CBOR encoding nesting
1021
1022 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1023 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1024
1025 // Add the diagnostic code
1026 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1027
1028 // Add a time stamp
1029 if(time) {
1030 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1031 }
1032
1033 // Add the diagnostic code
1034 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1035
1036 // Open a subordinate map for telemtry data
1037 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1038
1039 { // Brace / indention just to show CBOR encoding nesting
1040
1041 // Add a few fake integers and buffers for now.
1042 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1043
1044 // Add a few fake integers and buffers for now.
1045 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1046
1047 // Add a few fake integers and buffers for now.
1048 const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
1049 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1050
1051 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1052 }
1053 }
1054
1055 // Close the telemetry map
1056 QCBOREncode_CloseMap(&ECtx);
1057
1058 // Close the map
1059 QCBOREncode_CloseMap(&ECtx);
1060
1061 return QCBOREncode_Finish(&ECtx, pnLen);
1062}
1063
1064
1065static 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};
1066
1067
1068int RTICResultsTest()
1069{
1070 uint8_t out[200];
1071 size_t len = sizeof(out);
1072
1073 int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len);
1074 if(nResult)
1075 return -1;
1076
1077 if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC)))
1078 return 1;
1079
1080 //printencoded(out, len);
1081
1082 return 0;
1083}
1084
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301086
Laurence Lundblade684aec22018-10-12 19:33:53 +08001087/*
1088 Very basic bstr wrapping test
1089 */
1090int bstrwraptest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001092 UsefulBuf_MakeStackUB(MemoryForEncoded, 100);
1093 QCBOREncodeContext EC;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094
Laurence Lundblade684aec22018-10-12 19:33:53 +08001095 QCBOREncode_Init(&EC, MemoryForEncoded);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096
Laurence Lundblade684aec22018-10-12 19:33:53 +08001097 QCBOREncode_OpenArray(&EC);
1098 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099
Laurence Lundblade684aec22018-10-12 19:33:53 +08001100 QCBOREncode_BstrWrap(&EC);
1101 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001102
Laurence Lundblade684aec22018-10-12 19:33:53 +08001103 UsefulBufC Wrapped;
1104 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001105
Laurence Lundblade684aec22018-10-12 19:33:53 +08001106 QCBOREncode_CloseArray(&EC);
1107
1108 UsefulBufC Encoded;
1109 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1110 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001112
Laurence Lundblade684aec22018-10-12 19:33:53 +08001113 const uint8_t pExpected[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
1114 if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(pExpected), Encoded)) {
1115 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 }
1117
1118 return 0;
1119}
1120
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001121
1122
Laurence Lundblade684aec22018-10-12 19:33:53 +08001123int bstr_wrap_error_test()
1124{
1125 // -------------- Test closing a bstrwrap when it is an array that is open -----------
1126 UsefulBuf_MakeStackUB(MemoryForEncoded, 100);
1127 QCBOREncodeContext EC;
1128
1129 QCBOREncode_Init(&EC, MemoryForEncoded);
1130
1131 QCBOREncode_OpenArray(&EC);
1132 QCBOREncode_AddUInt64(&EC, 451);
1133
1134 QCBOREncode_BstrWrap(&EC);
1135 QCBOREncode_AddUInt64(&EC, 466);
1136 QCBOREncode_OpenArray(&EC);
1137
1138 UsefulBufC Wrapped;
1139 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1140
1141 QCBOREncode_CloseArray(&EC);
1142
1143 UsefulBufC Encoded2;
1144 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_CLOSE_MISMATCH) {
1145 return -1;
1146 }
1147
1148 // ----------- test closing a bstrwrap when nothing is open ---------------------
1149 QCBOREncode_Init(&EC, MemoryForEncoded);
1150 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1151 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_TOO_MANY_CLOSES) {
1152 return -2;
1153 }
1154
1155 // --------------- test nesting too deep ----------------------------------
1156 QCBOREncode_Init(&EC, MemoryForEncoded);
1157 for(int i = 1; i < 18; i++) {
1158 QCBOREncode_BstrWrap(&EC);
1159 }
1160 QCBOREncode_AddBool(&EC, true);
1161
1162 for(int i = 1; i < 18; i++) {
1163 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1164 }
1165
1166 if(QCBOREncode_Finish2(&EC, &Encoded2) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1167 return -3;
1168 }
1169
1170 return 0;
1171}
1172
1173
1174
1175// Part of bstr_wrap_nest_test
1176/*
1177 83 array with three
1178 53 byte string with 19 bytes
1179 01 #1
1180 50 byte string with 16 bytes
1181 02
1182 4D byte string with 13 bytes
1183 03
1184 4A byte string with 10 bytes
1185 04
1186 47 byte string with 7 bytes
1187 05
1188 44 byte string with 4 bytes
1189 06
1190 41 byte string with 1 byte
1191 07
1192 01
1193 02
1194 03
1195 04
1196 05
1197 06
1198 07
1199 A2 map with two items
1200 18 20 label for byte string
1201 54 byte string of length 20
1202 82 Array with two items
1203 10 The integer value 10
1204 A2 map with two items
1205 18 21 label for byte string
1206 44 byte string with 4 bytes
1207 81 array with 1 item
1208 11 integer value 11
1209 18 30 integer value 30
1210 18 40 integer label 40
1211 65 68 65 6C 6C 6F text string hello
1212 18 31 integer value 31
1213 18 41 integer label 41
1214 65 68 65 6C 6C 6F text string hello
1215
1216
1217 */
1218static const uint8_t sExpectedDeepBstr[] =
1219{
1220 0x83, 0x56, 0x00, 0x53, 0x01, 0x50, 0x02, 0x4D,
1221 0x03, 0x4A, 0x04, 0x47, 0x05, 0x44, 0x06, 0x41,
1222 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1223 0x07, 0xA2, 0x18, 0x20, 0x54, 0x82, 0x10, 0xA2,
1224 0x18, 0x21, 0x44, 0x81, 0x11, 0x18, 0x30, 0x18,
1225 0x40, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x18,
1226 0x31, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C, 0x6C,
1227 0x6F
1228};
1229
1230// Part of bstr_wrap_nest_test
1231static int decode_next_nested(UsefulBufC Wrapped)
1232{
1233 int nReturn;
1234 QCBORDecodeContext DC;
1235 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1236
1237 QCBORItem Item;
1238 nReturn = QCBORDecode_GetNext(&DC, &Item);
1239 if(nReturn) {
1240 return -11;
1241 }
1242 if(Item.uDataType != QCBOR_TYPE_INT64) {
1243 return -12;
1244 }
1245
1246 nReturn = QCBORDecode_GetNext(&DC, &Item);
1247 if(nReturn == QCBOR_ERR_HIT_END) {
1248 return 0;
1249 }
1250 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1251 return -13;
1252 }
1253 nReturn = decode_next_nested(Item.val.string);
1254 if(nReturn) {
1255 return nReturn;
1256 }
1257
1258 nReturn = QCBORDecode_GetNext(&DC, &Item);
1259 if(nReturn) {
1260 return -14;
1261 }
1262 if(Item.uDataType != QCBOR_TYPE_INT64) {
1263 return -15;
1264 }
1265
1266 if(QCBORDecode_Finish(&DC)) {
1267 return -16;
1268 }
1269
1270 return 0;
1271}
1272
1273// Part of bstr_wrap_nest_test
1274static int decode_next_nested2(UsefulBufC Wrapped)
1275{
1276 int nReturn;
1277 QCBORDecodeContext DC;
1278 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
1279
1280 QCBORItem Item;
1281 nReturn = QCBORDecode_GetNext(&DC, &Item);
1282 if(nReturn) {
1283 return -11;
1284 }
1285 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1286 return -12;
1287 }
1288
1289 nReturn = QCBORDecode_GetNext(&DC, &Item);
1290 if(nReturn) {
1291 return -11;
1292 }
1293 if(Item.uDataType != QCBOR_TYPE_INT64) {
1294 return -12;
1295 }
1296
1297 nReturn = QCBORDecode_GetNext(&DC, &Item);
1298 if(nReturn) {
1299 return -11;
1300 }
1301 if(Item.uDataType != QCBOR_TYPE_MAP) {
1302 return 0;
1303 }
1304
1305 nReturn = QCBORDecode_GetNext(&DC, &Item);
1306 if(nReturn) {
1307 return -11;
1308 }
1309 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1310 return -13;
1311 }
1312 nReturn = decode_next_nested2(Item.val.string);
1313 if(nReturn) {
1314 return nReturn;
1315 }
1316
1317 nReturn = QCBORDecode_GetNext(&DC, &Item);
1318 if(nReturn) {
1319 return -11;
1320 }
1321 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1322 return -12;
1323 }
1324 nReturn = QCBORDecode_GetNext(&DC, &Item);
1325 if(nReturn) {
1326 return -11;
1327 }
1328 if(Item.uDataType != QCBOR_TYPE_INT64) {
1329 return -12;
1330 }
1331
1332 if(QCBORDecode_Finish(&DC)) {
1333 return -16;
1334 }
1335
1336 return 0;
1337}
1338
1339
1340int bstr_wrap_nest_test()
1341{
1342 UsefulBuf_MakeStackUB(MemoryForEncoded, 300);
1343 QCBOREncodeContext EC;
1344 QCBOREncode_Init(&EC, MemoryForEncoded);
1345
1346 // ---- Make a complicated nested CBOR structure ---
1347 QCBOREncode_OpenArray(&EC);
1348
1349 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1350 QCBOREncode_BstrWrap(&EC);
1351 QCBOREncode_AddUInt64(&EC, i);
1352 }
1353
1354 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING-2; i++) {
1355 QCBOREncode_CloseBstrWrap(&EC, NULL);
1356 QCBOREncode_AddUInt64(&EC, i);
1357 }
1358
1359 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1360 QCBOREncode_OpenMap(&EC);
1361 QCBOREncode_BstrWrapMapN(&EC, i+0x20);
1362 QCBOREncode_OpenArray(&EC);
1363 QCBOREncode_AddUInt64(&EC, i+0x10);
1364 }
1365
1366 for(int i = 0; i < (QCBOR_MAX_ARRAY_NESTING-2)/3; i++) {
1367 QCBOREncode_CloseArray(&EC);
1368 QCBOREncode_AddUInt64(&EC, i+0x30);
1369 QCBOREncode_CloseBstrWrap(&EC, NULL);
1370 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
1371 QCBOREncode_CloseMap(&EC);
1372 }
1373 QCBOREncode_CloseArray(&EC);
1374
1375 UsefulBufC Encoded;
1376 if(QCBOREncode_Finish2(&EC, &Encoded)) {
1377 return -1;
1378 }
1379
1380 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
1381 if(UsefulBuf_Compare(UsefulBuf_FromByteArrayLiteral(sExpectedDeepBstr), Encoded)) {
1382 return -25;
1383 }
1384
1385
1386 // ---- Decode it and see if it is OK ------
1387 QCBORDecodeContext DC;
1388 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
1389
1390 QCBORItem Item;
1391 QCBORDecode_GetNext(&DC, &Item);
1392 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
1393 return -2;
1394 }
1395
1396 QCBORDecode_GetNext(&DC, &Item);
1397 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1398 return -3;
1399 }
1400
1401 int nReturn = decode_next_nested(Item.val.string);
1402 if(nReturn) {
1403 return nReturn;
1404 }
1405
1406 nReturn = QCBORDecode_GetNext(&DC, &Item);
1407 if(nReturn) {
1408 return -11;
1409 }
1410 if(Item.uDataType != QCBOR_TYPE_INT64) {
1411 return -12;
1412 }
1413
1414 QCBORDecode_GetNext(&DC, &Item);
1415 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 2) {
1416 return -2;
1417 }
1418
1419 QCBORDecode_GetNext(&DC, &Item);
1420 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
1421 return -3;
1422 }
1423 nReturn = decode_next_nested2(Item.val.string);
1424 if(nReturn) {
1425 return nReturn;
1426 }
1427
1428 nReturn = QCBORDecode_GetNext(&DC, &Item);
1429 if(nReturn) {
1430 return -11;
1431 }
1432 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1433 return -12;
1434 }
1435
1436 if(QCBORDecode_Finish(&DC)) {
1437 return -16;
1438 }
1439
1440 return 0;
1441}
1442
1443
1444/*
1445 this corresponds exactly to the example in RFC 8152
1446 section C.2.1. This doesn't actually verify the signature
1447 though that would be nice as it would make the test
1448 really good. That would require bring in ECDSA crypto
1449 to this test.
1450 */
1451int cose_sign1_tbs_test()
1452{
1453 // All of this is from RFC 8152 C.2.1
1454 const char *szKid = "11";
1455 UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
1456 const char *szPayload = "This is the content.";
1457 UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
1458 const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
1459 UsefulBufC ProtectedHeaders = UsefulBuf_FromByteArrayLiteral(pProtectedHeaders);
1460 const uint8_t sSignature[] = {
1461 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
1462 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
1463 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
1464 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
1465 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
1466 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
1467 0x45, 0xca, 0xcb, 0x36};
1468 // It would be good to compare this to the output from
1469 // a COSE implementation like COSE-C. It has been checked
1470 // against the CBOR playground.
1471 UsefulBufC Signature = UsefulBuf_FromByteArrayLiteral(sSignature);
1472 const uint8_t sExpected[] = {
1473 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
1474 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
1475 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
1476 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
1477 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
1478 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
1479 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
1480 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
1481 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
1482 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
1483 UsefulBufC Expected = UsefulBuf_FromByteArrayLiteral(sExpected);
1484
1485 UsefulBuf_MakeStackUB(MemoryForEncoded, 98);
1486 QCBOREncodeContext EC;
1487 QCBOREncode_Init(&EC, MemoryForEncoded);
1488
1489 // top level array for cose sign1, 18 is the tag for COSE sign
1490 QCBOREncode_OpenArray_3(&EC, NULL, QCBOR_NO_INT_LABEL, 18);
1491
1492 // Add protected headers
1493 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
1494
1495 // Empty map with unprotected headers
1496 QCBOREncode_OpenMap(&EC);
1497 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
1498 QCBOREncode_CloseMap(&EC);
1499
1500 // The payload
1501 UsefulBufC WrappedPayload;
1502 QCBOREncode_BstrWrap(&EC);
1503 QCBOREncode_AddEncoded(&EC, Payload); // Payload is not actually CBOR in example C.2.1
1504 QCBOREncode_CloseBstrWrap(&EC, &WrappedPayload);
1505
1506 // Check we got back the actual payload expected
1507 if(UsefulBuf_Compare(WrappedPayload, Payload)) {
1508 return -1;
1509 }
1510
1511 // The signature
1512 QCBOREncode_AddBytes(&EC, Signature);
1513 QCBOREncode_CloseArray(&EC);
1514
1515 // Finish and check the results
1516 UsefulBufC COSE_Sign1;
1517 if(QCBOREncode_Finish2(&EC, &COSE_Sign1)) {
1518 return -2;
1519 }
1520
1521 // 98 is the size from RFC 8152 C.2.1
1522 if(COSE_Sign1.len != 98) {
1523 return -3;
1524 }
1525
1526 if(UsefulBuf_Compare(COSE_Sign1, Expected)) {
1527 return -4;
1528 }
1529
1530 return 0;
1531}
1532