blob: 4a79bbd17a595e7b608aed6a2ad1a0c5adc46c84 [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#if 0
690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691
692static uint8_t pExpectedEncodedFloat[] = {
693 0x98, 0x1e, 0xfa, 0x00, 0x00, 0x00, 0x00, 0xfa,
694 0x3f, 0x80, 0x00, 0x00, 0xfa, 0x3f, 0x8c, 0xcc,
695 0xcd, 0xfa, 0x3f, 0xc0, 0x00, 0x00, 0xfa, 0x47,
696 0x7f, 0xe0, 0x00, 0xfa, 0x47, 0xc3, 0x50, 0x00,
697 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0xfa, 0x7f, 0x80,
698 0x00, 0x00, 0xfa, 0x33, 0x80, 0x00, 0x00, 0xfa,
699 0x38, 0x80, 0x00, 0x00, 0xfa, 0xc0, 0x80, 0x00,
700 0x00, 0xfa, 0xc0, 0x83, 0x33, 0x33, 0xfa, 0x7f,
701 0xc0, 0x00, 0x00, 0xfa, 0x7f, 0x80, 0x00, 0x00,
702 0xfa, 0xff, 0x80, 0x00, 0x00, 0xfb, 0x00, 0x00,
703 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
704 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
705 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
706 0xfb, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
707 0x00, 0xfb, 0x40, 0xef, 0xfc, 0x00, 0x00, 0x00,
708 0x00, 0x00, 0xfb, 0x40, 0xf8, 0x6a, 0x00, 0x00,
709 0x00, 0x00, 0x00, 0xfb, 0x47, 0xef, 0xff, 0xff,
710 0xe0, 0x00, 0x00, 0x00, 0xfb, 0x7e, 0x37, 0xe4,
711 0x3c, 0x88, 0x00, 0x75, 0x9c, 0xfb, 0x3e, 0x70,
712 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x3f,
713 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
714 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
715 0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66,
716 0x66, 0xfb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00,
717 0x00, 0x00, 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00,
718 0x00, 0x00, 0x00, 0xfb, 0xff, 0xf0, 0x00, 0x00,
719 0x00, 0x00, 0x00, 0x00};
720
721
722int FloatValuesTest1()
723{
724 QCBOREncodeContext ECtx;
725 int nReturn = 0;
726
727 uint8_t pEncoded[1000];
728 size_t nEncodedLen = sizeof(pEncoded);
729
730 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
731 QCBOREncode_OpenArray(&ECtx);
732
733 // These are all samples published
734 // in RFC 7049.
735 QCBOREncode_AddFloat(&ECtx, 0.0);
736 QCBOREncode_AddFloat(&ECtx, 1.0);
737 QCBOREncode_AddFloat(&ECtx, 1.1); // appx
738 QCBOREncode_AddFloat(&ECtx, 1.5);
739 QCBOREncode_AddFloat(&ECtx, 65504.0);
740 QCBOREncode_AddFloat(&ECtx, 100000.0);
741 QCBOREncode_AddFloat(&ECtx, 3.4028234663852886e+38);
742 QCBOREncode_AddFloat(&ECtx, 1.0e+300); // Infinity?
743 QCBOREncode_AddFloat(&ECtx, 5.960464477539063e-8);
744 QCBOREncode_AddFloat(&ECtx, 0.00006103515625);
745 QCBOREncode_AddFloat(&ECtx, -4.0);
746 QCBOREncode_AddFloat(&ECtx, -4.1); // appx
747
748 QCBOREncode_AddFloat(&ECtx, NAN);
749 QCBOREncode_AddFloat(&ECtx, INFINITY);
750 QCBOREncode_AddFloat(&ECtx, -INFINITY);
751
752
753 QCBOREncode_AddDouble(&ECtx, 0.0);
754 QCBOREncode_AddDouble(&ECtx, 1.0);
755 QCBOREncode_AddDouble(&ECtx, 1.1); // appx
756 QCBOREncode_AddDouble(&ECtx, 1.5);
757 QCBOREncode_AddDouble(&ECtx, 65504.0);
758 QCBOREncode_AddDouble(&ECtx, 100000.0);
759 QCBOREncode_AddDouble(&ECtx, 3.4028234663852886e+38);
760 QCBOREncode_AddDouble(&ECtx, 1.0e+300); // Infinity?
761 QCBOREncode_AddDouble(&ECtx, 5.960464477539063e-8);
762 QCBOREncode_AddDouble(&ECtx, 0.00006103515625);
763 QCBOREncode_AddDouble(&ECtx, -4.0);
764 QCBOREncode_AddDouble(&ECtx, -4.1); // appx
765
766 QCBOREncode_AddDouble(&ECtx, NAN);
767 QCBOREncode_AddDouble(&ECtx, INFINITY);
768 QCBOREncode_AddDouble(&ECtx, -INFINITY);
769
770 QCBOREncode_CloseArray(&ECtx);
771 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
772 nReturn = -1;
773 }
774
775 if(nEncodedLen != sizeof(pExpectedEncodedFloat) || bcmp(pEncoded, pExpectedEncodedFloat, nEncodedLen))
776 nReturn = -1;
777
778 //printencoded(pEncoded, nEncodedLen);
779
780 return(nReturn);
781}
782
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530783#endif
784
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800785static uint8_t pExpectedEncodedDates[] = {
786 0x83, 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54,
787 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30,
788 0x5a, 0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0, 0xa2,
789 0x78, 0x19, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65,
790 0x20, 0x44, 0x61, 0x74, 0x65, 0x20, 0x66, 0x72,
791 0x6f, 0x6d, 0x20, 0x52, 0x46, 0x43, 0x20, 0x33,
792 0x33, 0x33, 0x39, 0xc0, 0x77, 0x31, 0x39, 0x38,
793 0x35, 0x2d, 0x30, 0x34, 0x2d, 0x31, 0x32, 0x54,
794 0x32, 0x33, 0x3a, 0x32, 0x30, 0x3a, 0x35, 0x30,
795 0x2e, 0x35, 0x32, 0x5a, 0x62, 0x53, 0x44, 0xc1,
796 0x19, 0x03, 0xe7
797};
798
799int EncodeDateTest()
800{
801 QCBOREncodeContext ECtx;
802 int nReturn = 0;
803
804 uint8_t pEncoded[1000];
805 size_t nEncodedLen = sizeof(pEncoded);
806
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530807 QCBOREncode_Init(&ECtx, (UsefulBuf){pEncoded, nEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800808
809 QCBOREncode_OpenArray(&ECtx);
810
811
812 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z"); // from CBOR RFC
813 QCBOREncode_AddDateEpoch(&ECtx, 1363896240); // from CBOR RFC
814
815
816 QCBOREncode_OpenMap(&ECtx);
817
818 QCBOREncode_AddDateStringToMap(&ECtx, "Sample Date from RFC 3339", "1985-04-12T23:20:50.52Z");
819
820
821 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
822
823 QCBOREncode_CloseMap(&ECtx);
824
825 QCBOREncode_CloseArray(&ECtx);
826
827
828 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
829 nReturn = -1;
830 }
831
832 if(nEncodedLen != sizeof(pExpectedEncodedDates) || bcmp(pEncoded, pExpectedEncodedDates, nEncodedLen))
833 nReturn = -1;
834
835 //printencoded(pEncoded, nEncodedLen);
836
837 return(nReturn);
838
839}
840
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800841int ArrayNestingTest1()
842{
843 QCBOREncodeContext ECtx;
844 int i;
845 int nReturn = 0;
846
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530847 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800848
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530849 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800850 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
851 QCBOREncode_OpenArray(&ECtx);
852 }
853 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
854 QCBOREncode_CloseArray(&ECtx);
855 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530856 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800857 if(QCBOREncode_Finish(&ECtx, &nEncodedLen)) {
858 printf("ArrayNestingTest1 Failed\n");
859 nReturn = -1;
860 }
861 //printencoded(pEncoded, nEncodedLen);
862
863 return(nReturn);
864}
865
866
867
868int ArrayNestingTest2()
869{
870 QCBOREncodeContext ECtx;
871 int i;
872 int nReturn = 0;
873
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530874 UsefulBuf_MakeStackUB(Encode, 100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530876 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800877 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
878 QCBOREncode_OpenArray(&ECtx);
879 }
880 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
881 QCBOREncode_CloseArray(&ECtx);
882 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530883
884 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
886 nReturn = -1;
887 }
888
889 return(nReturn);
890}
891
892
893
894int ArrayNestingTest3()
895{
896 QCBOREncodeContext ECtx;
897 int i;
898 int nReturn = 0;
899
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530900 UsefulBuf_MakeStackUB(Encode, 100);
901
902 QCBOREncode_Init(&ECtx, Encode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
904 QCBOREncode_OpenArray(&ECtx);
905 }
906 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
907 QCBOREncode_CloseArray(&ECtx);
908 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530909 size_t nEncodedLen;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 if(QCBOREncode_Finish(&ECtx, &nEncodedLen) != QCBOR_ERR_TOO_MANY_CLOSES) {
911 nReturn = -1;
912 }
913
914 return(nReturn);
915}
916
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530917#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918
919
920static uint8_t s_pFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
921
922static int EncodeRaw(uint8_t **pEncoded, size_t *pEncodedLen)
923{
924 QCBOREncodeContext ECtx;
925 int nReturn = -1;
926
927 *pEncoded = NULL;
928 *pEncodedLen = INT32_MAX; // largest buffer this CBOR implementation will deal with
929
930 // loop runs CBOR encoding twice. First with no buffer to
931 // calucate the length so buffer can be allocated correctly,
932 // and last with the buffer to do the actual encoding
933 do {
934 QCBOREncode_Init(&ECtx, *pEncoded, *pEncodedLen);
935 QCBOREncode_OpenArray(&ECtx);
936 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {s_pFiveArrarys, 5}), 1});
937 QCBOREncode_AddRaw(&ECtx, (EncodedCBORC){((UsefulBufC) {pExpectedEncodedInts, sizeof(pExpectedEncodedInts)}), 1});
938 QCBOREncode_CloseArray(&ECtx);
939
940 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
941 goto Done;
942 if(*pEncoded != NULL) {
943 nReturn = 0;
944 goto Done;
945 }
946 *pEncoded = malloc(*pEncodedLen);
947 } while(1);
948
949Done:
950 return(nReturn);
951}
952
953
954// Validated at http://cbor.me and by manually examining its output
955static uint8_t s_pEncodeRawExpected[] = {
956 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x2f,
957 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
958 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
959 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
960 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
961 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
962 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
963 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
964 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
965 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
966 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
967 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
968 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00, 0x19, 0x01,
969 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff, 0xff, 0x1a,
970 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00,
971 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02, 0x1a, 0x7f,
972 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff, 0xff, 0xff,
973 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a, 0x80, 0x00,
974 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff, 0xfe, 0x1a,
975 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
976 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00,
977 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1b, 0x7f,
978 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b,
979 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
980
981
982
983int EncodeRawTest()
984{
985 uint8_t *pEncodedRaw;
986 size_t nEncodedRawLen;
987
988 if(EncodeRaw(&pEncodedRaw, &nEncodedRawLen)) {
989 return(-1);
990 }
991
992 //printencoded(pEncodedRaw, nEncodedRawLen);
993
994 int nReturn = 0;
995 if(nEncodedRawLen != sizeof(s_pEncodeRawExpected) || memcmp(s_pEncodeRawExpected, pEncodedRaw, sizeof(s_pEncodeRawExpected)))
996 nReturn = 1;
997
998 return(nReturn);
999}
1000
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301001#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002
1003
1004
1005static int CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
1006{
1007 QCBOREncodeContext ECtx;
1008 int nReturn = -1;
1009
1010 *pEncoded = NULL;
1011 *pEncodedLen = INT32_MAX;
1012
1013 // loop runs CBOR encoding twice. First with no buffer to
1014 // calucate the length so buffer can be allocated correctly,
1015 // and last with the buffer to do the actual encoding
1016 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301017 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001018 QCBOREncode_OpenMap(&ECtx);
1019 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1020 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1021 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1022 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1023 QCBOREncode_CloseArray(&ECtx);
1024 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1025 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1026 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1027 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1028 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1029 QCBOREncode_CloseMap(&ECtx);
1030 QCBOREncode_CloseMap(&ECtx);
1031
1032
1033 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
1034 goto Done;
1035 if(*pEncoded != NULL) {
1036 nReturn = 0;
1037 goto Done;
1038 }
1039 *pEncoded = malloc(*pEncodedLen);
1040 } while(1);
1041
1042 Done:
1043 return(nReturn);
1044}
1045
1046
1047static uint8_t pValidMapEncoded[] = {
1048 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a,
1049 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77, 0x6f, 0x20,
1050 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x67,
1051 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20,
1052 0x6d, 0x61, 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31, 0x44, 0x78, 0x78, 0x78, 0x78,
1053 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f,
1054 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
1055 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
1056 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 } ;
1057
1058
1059
1060int MapEncodeTest()
1061{
1062 uint8_t *pEncodedMaps;
1063 size_t nEncodedMapLen;
1064
1065 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
1066 return(-1);
1067 }
1068
1069 //printencoded(pEncodedMaps, nEncodedMapLen);
1070
1071 int nReturn = 0;
1072 if(memcmp(pValidMapEncoded, pEncodedMaps, sizeof(pValidMapEncoded)))
1073 nReturn = 1;
1074
1075 return(nReturn);
1076}
1077
1078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079/*
1080 @brief Encode the RTIC results
1081
1082 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or CBOR_SIMPLEV_NULL
1083 @param[in] time Time stamp in UNIX epoch time or 0 for no time stamp
1084 @param[in] szAlexString Diagnostic code.
1085 @param[in[ pOut Buffer to put the result in
1086 @param[in/out] pnLen Size of pOut buffer when called; length of data output in buffer on return
1087
1088 @return
1089 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
1090
1091 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
1092 short an error will be returned. This function will never write off the end
1093 of the buffer passed to it.
1094
1095 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1096 length of the encoded CBOR.
1097
1098 */
1099
1100int FormatRTICResults(int nRResult, uint64_t time, const char *szType, const char *szAlexString, uint8_t *pOut, size_t *pnLen)
1101{
1102 // Buffer that the result will be written in to
1103 // It is fixed size and small that a stack variable will be fine
1104 // QCBOREncode will never write off the end of this buffer. If it won't fit QCBOREncode_Finish will return an error.
1105
1106 // Context for the encoder
1107 QCBOREncodeContext ECtx;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301108 QCBOREncode_Init(&ECtx, (UsefulBuf){pOut, *pnLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109
1110 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1111 // Contents are label / value pairs
1112 QCBOREncode_OpenMap(&ECtx);
1113
1114 { // Brace / indention just to show CBOR encoding nesting
1115
1116 // The result: 0 if scan happened and found nothing; 1 if it happened and found something wrong; 2 if it didn't happen
1117 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", nRResult);
1118
1119 // Add the diagnostic code
1120 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1121
1122 // Add a time stamp
1123 if(time) {
1124 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1125 }
1126
1127 // Add the diagnostic code
1128 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1129
1130 // Open a subordinate map for telemtry data
1131 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1132
1133 { // Brace / indention just to show CBOR encoding nesting
1134
1135 // Add a few fake integers and buffers for now.
1136 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1137
1138 // Add a few fake integers and buffers for now.
1139 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1140
1141 // Add a few fake integers and buffers for now.
1142 const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
1143 UsefulBufC WSPV = {pPV, sizeof(pPV)};
1144
1145 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1146 }
1147 }
1148
1149 // Close the telemetry map
1150 QCBOREncode_CloseMap(&ECtx);
1151
1152 // Close the map
1153 QCBOREncode_CloseMap(&ECtx);
1154
1155 return QCBOREncode_Finish(&ECtx, pnLen);
1156}
1157
1158
1159static 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};
1160
1161
1162int RTICResultsTest()
1163{
1164 uint8_t out[200];
1165 size_t len = sizeof(out);
1166
1167 int nResult = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730, "recent", "0xA1eC5001", out, &len);
1168 if(nResult)
1169 return -1;
1170
1171 if(memcmp(pExpectedRTIC, out, sizeof(pExpectedRTIC)))
1172 return 1;
1173
1174 //printencoded(out, len);
1175
1176 return 0;
1177}
1178
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301179
1180#if 0
1181
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182static const uint8_t pBStrArrayExpected[] = { 0x45, 0x81, 0x63, 0x66, 0x6F, 0x6F };
1183
1184static const uint8_t pBStrMapExpected[] = {
1185 0x81, // array of 1
1186 0x58, 0x1e, // bstr wrapper
1187 0xA1, // map of 1
1188 0x13, // label 19
1189 0x78, 0x1a, // text string of 26
1190 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
1191 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a};
1192
1193int BStrWrapTests()
1194{
1195 QCBOREncodeContext ECtx;
1196
1197 uint8_t pEncoded[100];
1198 size_t nEncodedLen = sizeof(pEncoded);
1199
1200 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
1201
1202 QCBOREncode_OpenArray_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
1203 QCBOREncode_AddSZString(&ECtx, "foo");
1204 QCBOREncode_CloseArray(&ECtx);
1205
1206 EncodedCBOR E;
1207 QCBOREncode_Finish2(&ECtx, &E);
1208
1209 if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrArrayExpected) )) {
1210 return 1;
1211 }
1212
1213
1214 QCBOREncode_Init(&ECtx, pEncoded, nEncodedLen);
1215
1216 QCBOREncode_OpenArray(&ECtx);
1217
1218 QCBOREncode_OpenMap_3(&ECtx, NULL, QCBOR_NO_INT_LABEL, CBOR_TAG_NONE, 1);
1219 QCBOREncode_AddSZStringToMapN(&ECtx, 19, "abcdefghijklmnopqrstuvwxyz");
1220 QCBOREncode_CloseMap(&ECtx);
1221 QCBOREncode_CloseArray(&ECtx);
1222
1223 QCBOREncode_Finish2(&ECtx, &E);
1224
1225 if(UsefulBuf_Compare(UsefulBuf_Const(E.Bytes), ByteArrayLiteralToUsefulBufC(pBStrMapExpected) )) {
1226 return 2;
1227 }
1228
1229 return 0;
1230}
1231
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301232#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001233
1234