1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include "manifest-moran3_decode.h"
9 #include "manifest-moran4_decode.h"
10
11
12 uint8_t test_vector2[] = {
13 0xa2, 0x01, 0xf6, 0x02, 0x58, 0x3a, 0xa3, 0x01, 0x01, 0x02,
14 0x02, 0x05, 0x81, 0xa3, 0x01, 0x81, 0x41, 0x30,
15 0x02, 0x18, 0x25, 0x03, 0x84, 0x44, 0xa1, 0x01,
16 0x18, 0x29, 0xa0, 0xf6, 0x58, 0x20, 0x8c, 0xaf,
17 0x92, 0x83, 0xb1, 0x36, 0x66, 0xca, 0x4e, 0x50,
18 0xf7, 0xa1, 0xee, 0xe8, 0x6b, 0xa4, 0x0b, 0x5e,
19 0x6a, 0x1d, 0x2c, 0xa3, 0x9f, 0x74, 0x98, 0xb6,
20 0xa6, 0xa7, 0xbe, 0x8d, 0x8d, 0x67
21 };
22
23 uint8_t test_vector3[] = {
24 0xa3, 0x01, 0x84, 0x44, 0xa1, 0x03, 0x18, 0x2a,
25 0xa0, 0xf6, 0x81, 0x83, 0x43, 0xa1, 0x01, 0x26, 0xa1, 0x04,
26 0x58, 0x20, 0x53, 0x7a, 0xc9, 0x3a, 0xc9, 0x09, 0xe7, 0x99,
27 0x90, 0x91, 0x4c, 0xaa, 0x00, 0xfe, 0x87, 0xee, 0xea, 0x63,
28 0x7e, 0xf8, 0x9b, 0x55, 0x12, 0xe5, 0xcb, 0x6e, 0x55, 0x8a,
29 0x13, 0x6f, 0xf9, 0x8d, 0x58, 0x47, 0x30, 0x45, 0x02, 0x21,
30 0x00, 0x83, 0x0c, 0xf1, 0x42, 0xcc, 0x4a, 0xdf, 0x56, 0x33,
31 0x92, 0xdc, 0x7e, 0x04, 0x30, 0x00, 0x01, 0x58, 0xbf, 0x37,
32 0x20, 0xb2, 0x8b, 0x7c, 0xea, 0x38, 0x8b, 0x0f, 0x1a, 0x5f,
33 0x89, 0x18, 0xa8, 0x02, 0x20, 0x1d, 0xef, 0x2d, 0xf3, 0x4d,
34 0x6a, 0xbd, 0x3b, 0x17, 0xc3, 0x42, 0x55, 0x73, 0xff, 0x2b,
35 0x7c, 0xac, 0xae, 0x3d, 0xd0, 0x85, 0xe1, 0x1d, 0xfc, 0x23,
36 0xbf, 0x0c, 0x60, 0xbe, 0x51, 0xb7, 0xda, 0x02, 0x58, 0xb9,
37 0xa6, 0x01, 0x01,
38 0x02, 0x02, 0x03, 0xa1, 0x01, 0x82, 0x82, 0x01, 0x50, 0xfa,
39 0x6b, 0x4a, 0x53, 0xd5, 0xad, 0x5f, 0xdf, 0xbe, 0x9d, 0xe6,
40 0x63, 0xe4, 0xd4, 0x1f, 0xfe, 0x82, 0x02, 0x50, 0x6e, 0x04,
41 0xd3, 0xc2, 0x48, 0x87, 0x59, 0xe4, 0xa5, 0x97, 0xb5, 0xe7,
42 0xcd, 0x49, 0x76, 0x53, 0x05, 0x81, 0xa3, 0x01, 0x81, 0x41,
43 0x30, 0x02, 0x18, 0x25, 0x03, 0x84, 0x44, 0xa1, 0x01, 0x18,
44 0x29, 0xa0, 0xf6, 0x58, 0x20, 0x8c, 0xaf, 0x92, 0x83, 0xb1,
45 0x36, 0x66, 0xca, 0x4e, 0x50, 0xf7, 0xa1, 0xee, 0xe8, 0x6b,
46 0xa4, 0x0b, 0x5e, 0x6a, 0x1d, 0x2c, 0xa3, 0x9f, 0x74, 0x98,
47 0xb6, 0xa6, 0xa7, 0xbe, 0x8d, 0x8d, 0x67, 0x06, 0xa1, 0x01,
48 0x81, 0xa2, 0x01, 0x81, 0x41, 0x30, 0x02, 0x81, 0xa2, 0x01,
49 0x82, 0x01, 0x01, 0x03, 0x82, 0x00, 0x76, 0x68, 0x74, 0x74,
50 0x70, 0x3a, 0x2f, 0x2f, 0x66, 0x6f, 0x6f, 0x2e, 0x62, 0x61,
51 0x72, 0x2f, 0x62, 0x61, 0x7a, 0x2e, 0x62, 0x69, 0x6e, 0x08,
52 0x84, 0x44, 0xa1, 0x01, 0x18, 0x29, 0xa0, 0xf6, 0x58, 0x20,
53 0x4e, 0x27, 0x14, 0x59, 0x84, 0x79, 0xd8, 0xb6, 0x63, 0x48,
54 0x05, 0xdf, 0x50, 0x19, 0xef, 0x34, 0x20, 0xed, 0xff, 0x03,
55 0x29, 0x89, 0x4a, 0xcc, 0x91, 0xde, 0x8c, 0x8d, 0xe1, 0x6f,
56 0xb0, 0xcf, 0x06, 0x58, 0xcc,
57 0xa1, 0x01, 0x78, 0xc8, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20,
58 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f,
59 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74,
60 0x2c, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65,
61 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73,
62 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74, 0x2e,
63 0x20, 0x4e, 0x75, 0x6e, 0x63, 0x20, 0x73, 0x65, 0x64, 0x20,
64 0x74, 0x69, 0x6e, 0x63, 0x69, 0x64, 0x75, 0x6e, 0x74, 0x20,
65 0x61, 0x6e, 0x74, 0x65, 0x2c, 0x20, 0x61, 0x20, 0x73, 0x6f,
66 0x64, 0x61, 0x6c, 0x65, 0x73, 0x20, 0x6c, 0x69, 0x67, 0x75,
67 0x6c, 0x61, 0x2e, 0x20, 0x50, 0x68, 0x61, 0x73, 0x65, 0x6c,
68 0x6c, 0x75, 0x73, 0x20, 0x75, 0x6c, 0x6c, 0x61, 0x6d, 0x63,
69 0x6f, 0x72, 0x70, 0x65, 0x72, 0x20, 0x6f, 0x64, 0x69, 0x6f,
70 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x64, 0x6f, 0x20, 0x69,
71 0x70, 0x73, 0x75, 0x6d, 0x20, 0x65, 0x67, 0x65, 0x73, 0x74,
72 0x61, 0x73, 0x2c, 0x20, 0x76, 0x69, 0x74, 0x61, 0x65, 0x20,
73 0x6c, 0x61, 0x63, 0x69, 0x6e, 0x69, 0x61, 0x20, 0x6c, 0x65,
74 0x6f, 0x20, 0x6f, 0x72, 0x6e, 0x61, 0x72, 0x65, 0x2e, 0x20,
75 0x53, 0x75, 0x73, 0x70, 0x65, 0x6e, 0x64, 0x69, 0x73, 0x73,
76 0x65, 0x20, 0x70, 0x6f, 0x73, 0x75, 0x65, 0x72, 0x65, 0x20,
77 0x73, 0x65, 0x64, 0x2e,
78 };
79
80 uint8_t test_vector4[] = {
81 0xa2, 0x01, 0xf6, 0x02, 0x58, 0x6d, 0xa4, 0x01,
82 0x01, 0x02, 0x02, 0x04, 0x81, 0xa3, 0x01, 0x82,
83 0x45, 0x46, 0x6c, 0x61, 0x73, 0x68, 0x43, 0x00,
84 0x34, 0x01, 0x02, 0x19, 0x87, 0xd0, 0x03, 0x82,
85 0x01, 0x58, 0x20, 0x00, 0x11, 0x22, 0x33, 0x44,
86 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
87 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67, 0x89,
88 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76,
89 0x54, 0x32, 0x10, 0x09, 0x58, 0x2d, 0x83, 0xa1,
90 0x0b, 0x00, 0xa1, 0x10, 0xa1, 0x06, 0x58, 0x20,
91 0x81, 0x82, 0x00, 0x78, 0x1b, 0x68, 0x74, 0x74,
92 0x70, 0x3a, 0x2f, 0x2f, 0x65, 0x78, 0x61, 0x6d,
93 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
94 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x62, 0x69, 0x6e,
95 0xa1, 0x14, 0xf6
96 };
97
98 static struct OuterWrapper outerwrapper = {0};
99 static struct SUIT_Outer_Wrapper outerwrapper4 = {0};
100
101 // Example 9.1 from draft-moran-suit-manifest-03
ZTEST(cbor_decode_test,test_1)102 ZTEST(cbor_decode_test, test_1)
103 {
104 size_t decode_len;
105 uint8_t expected_tag[] = {
106 0x8c, 0xaf, 0x92, 0x83, 0xb1, 0x36, 0x66, 0xca,
107 0x4e, 0x50, 0xf7, 0xa1, 0xee, 0xe8, 0x6b, 0xa4,
108 0x0b, 0x5e, 0x6a, 0x1d, 0x2c, 0xa3, 0x9f, 0x74,
109 0x98, 0xb6, 0xa6, 0xa7, 0xbe, 0x8d, 0x8d, 0x67,
110 };
111 zassert_equal(ZCBOR_SUCCESS, cbor_decode_OuterWrapper(test_vector2,
112 sizeof(test_vector2),
113 &outerwrapper, &decode_len),
114 "test_vector2 failed");
115 zassert_equal(sizeof(test_vector2), decode_len, NULL);
116 zassert_equal(2, outerwrapper
117 .OuterWrapper_manifest_cbor
118 .Manifest_sequence,
119 "Sequence number incorrect (%d)", outerwrapper.OuterWrapper_manifest_cbor.Manifest_sequence);
120 zassert_true(outerwrapper
121 .OuterWrapper_manifest_cbor
122 .Manifest_payloads_present,
123 "Expected payloads entry");
124 zassert_equal(1, outerwrapper
125 .OuterWrapper_manifest_cbor
126 .Manifest_payloads_present,
127 "Expected payloads present");
128 zassert_equal(1, outerwrapper
129 .OuterWrapper_manifest_cbor
130 .Manifest_payloads
131 .Manifest_payloads_PayloadInfo_m_count,
132 "Expected single payloads entry, was %d",
133 outerwrapper
134 .OuterWrapper_manifest_cbor
135 .Manifest_payloads
136 .Manifest_payloads_PayloadInfo_m_count);
137 zassert_mem_equal(expected_tag,
138 outerwrapper
139 .OuterWrapper_manifest_cbor
140 .Manifest_payloads
141 .Manifest_payloads_PayloadInfo_m[0]
142 .PayloadInfo_payloadDigest
143 .COSE_Mac0_tag
144 .value,
145 sizeof(expected_tag),
146 "Expected a certain payloads tag");
147 }
148
149 // Example 9.3 from draft-moran-suit-manifest-03
ZTEST(cbor_decode_test,test_2)150 ZTEST(cbor_decode_test, test_2)
151 {
152 size_t decode_len;
153 memset(&outerwrapper, 0, sizeof(struct OuterWrapper));
154 char expected_updateDescription[] =
155 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc "
156 "sed tincidunt ante, a sodales ligula. Phasellus ullamcorper "
157 "odio commodo ipsum egestas, vitae lacinia leo ornare. "
158 "Suspendisse posuere sed.";
159 zassert_equal(ZCBOR_SUCCESS, cbor_decode_OuterWrapper(test_vector3,
160 sizeof(test_vector3),
161 &outerwrapper, &decode_len),
162 "test_vector3 failed");
163 zassert_equal(sizeof(test_vector3), decode_len, NULL);
164 zassert_equal(2, outerwrapper
165 .OuterWrapper_manifest_cbor
166 .Manifest_sequence,
167 "Sequence number incorrect (%d)", outerwrapper.OuterWrapper_manifest_cbor.Manifest_sequence);
168 zassert_equal(1, outerwrapper
169 .OuterWrapper_textExt_present,
170 "Expected text present");
171 zassert_equal(1, outerwrapper
172 .OuterWrapper_textExt
173 .OuterWrapper_textExt_cbor_count,
174 "Expected single text entry");
175 zassert_equal(1, outerwrapper
176 .OuterWrapper_textExt
177 .OuterWrapper_textExt_cbor_count,
178 "Expected single text entry");
179 zassert_equal(1, outerwrapper
180 .OuterWrapper_manifest_cbor
181 .Manifest_payloads
182 .Manifest_payloads_PayloadInfo_m_count,
183 "Expected single payloads entry, was %d",
184 outerwrapper
185 .OuterWrapper_manifest_cbor
186 .Manifest_payloads
187 .Manifest_payloads_PayloadInfo_m_count);
188 zassert_mem_equal(expected_updateDescription,
189 outerwrapper
190 .OuterWrapper_textExt
191 .OuterWrapper_textExt_cbor[0]
192 .Text_inttstr[0]
193 .Text_inttstr
194 .value,
195 sizeof(expected_updateDescription) - 1,
196 "Expected lorem ipsum text.");
197 }
198
199 // Example 1 from draft-moran-suit-manifest-04
ZTEST(cbor_decode_test,test_3)200 ZTEST(cbor_decode_test, test_3)
201 {
202 size_t decode_len;
203 char expected_uri[] = "http://example.com/file.bin";
204 memset(&outerwrapper4, 0, sizeof(struct SUIT_Outer_Wrapper));
205 zassert_equal(ZCBOR_SUCCESS, cbor_decode_SUIT_Outer_Wrapper(test_vector4,
206 sizeof(test_vector4), &outerwrapper4, &decode_len), "test_vector failed");
207 zassert_equal(sizeof(test_vector4), decode_len, NULL);
208 zassert_equal(2,
209 outerwrapper4
210 .SUIT_Outer_Wrapper_suit_manifest_cbor
211 .SUIT_Manifest_suit_manifest_sequence_number,
212 "Sequence number incorrect");
213 zassert_equal(1,
214 outerwrapper4
215 .SUIT_Outer_Wrapper_suit_manifest_cbor
216 .SUIT_Manifest_suit_install_present,
217 "Expected install present");
218 zassert_equal(SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_c,
219 outerwrapper4
220 .SUIT_Outer_Wrapper_suit_manifest_cbor
221 .SUIT_Manifest_suit_install
222 .SUIT_Manifest_suit_install
223 .SUIT_Severable_Command_Sequence3_choice,
224 "Expected install present");
225 zassert_equal(3,
226 outerwrapper4
227 .SUIT_Outer_Wrapper_suit_manifest_cbor
228 .SUIT_Manifest_suit_install
229 .SUIT_Manifest_suit_install
230 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
231 .SUIT_Command_Sequence_SUIT_Command_m_count,
232 "Expected x commands.");
233 zassert_equal(SUIT_Command_union_SUIT_Directive_m_c,
234 outerwrapper4
235 .SUIT_Outer_Wrapper_suit_manifest_cbor
236 .SUIT_Manifest_suit_install
237 .SUIT_Manifest_suit_install
238 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
239 .SUIT_Command_Sequence_SUIT_Command_m[0]
240 .SUIT_Command_union_choice,
241 "Expected directive");
242 zassert_equal(SUIT_Directive_Set_Component_Index_c,
243 outerwrapper4
244 .SUIT_Outer_Wrapper_suit_manifest_cbor
245 .SUIT_Manifest_suit_install
246 .SUIT_Manifest_suit_install
247 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
248 .SUIT_Command_Sequence_SUIT_Command_m[0]
249 .SUIT_Command_union_SUIT_Directive_m
250 .SUIT_Directive_choice,
251 "Expected Component_Index");
252 zassert_equal(SUIT_Directive_Set_Component_Index_uint_c,
253 outerwrapper4
254 .SUIT_Outer_Wrapper_suit_manifest_cbor
255 .SUIT_Manifest_suit_install
256 .SUIT_Manifest_suit_install
257 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
258 .SUIT_Command_Sequence_SUIT_Command_m[0]
259 .SUIT_Command_union_SUIT_Directive_m
260 .SUIT_Directive_Set_Component_Index_choice,
261 NULL);
262 zassert_equal(SUIT_Command_union_SUIT_Directive_m_c,
263 outerwrapper4
264 .SUIT_Outer_Wrapper_suit_manifest_cbor
265 .SUIT_Manifest_suit_install
266 .SUIT_Manifest_suit_install
267 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
268 .SUIT_Command_Sequence_SUIT_Command_m[1]
269 .SUIT_Command_union_choice,
270 "Expected directive");
271 zassert_equal(SUIT_Directive_Set_Parameters_c,
272 outerwrapper4
273 .SUIT_Outer_Wrapper_suit_manifest_cbor
274 .SUIT_Manifest_suit_install
275 .SUIT_Manifest_suit_install
276 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
277 .SUIT_Command_Sequence_SUIT_Command_m[1]
278 .SUIT_Command_union_SUIT_Directive_m
279 .SUIT_Directive_choice,
280 "Expected Set_Parameters");
281 zassert_equal(1,
282 outerwrapper4
283 .SUIT_Outer_Wrapper_suit_manifest_cbor
284 .SUIT_Manifest_suit_install
285 .SUIT_Manifest_suit_install
286 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
287 .SUIT_Command_Sequence_SUIT_Command_m[1]
288 .SUIT_Command_union_SUIT_Directive_m
289 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m_count,
290 "Expected 1 parameter");
291 zassert_equal(SUIT_Parameters_SUIT_Parameter_URI_List_c,
292 outerwrapper4
293 .SUIT_Outer_Wrapper_suit_manifest_cbor
294 .SUIT_Manifest_suit_install
295 .SUIT_Manifest_suit_install
296 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
297 .SUIT_Command_Sequence_SUIT_Command_m[1]
298 .SUIT_Command_union_SUIT_Directive_m
299 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m[0]
300 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m
301 .SUIT_Parameters_choice,
302 "Expected uri list parameter");
303 zassert_equal(1,
304 outerwrapper4
305 .SUIT_Outer_Wrapper_suit_manifest_cbor
306 .SUIT_Manifest_suit_install
307 .SUIT_Manifest_suit_install
308 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
309 .SUIT_Command_Sequence_SUIT_Command_m[1]
310 .SUIT_Command_union_SUIT_Directive_m
311 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m[0]
312 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m
313 .SUIT_Parameters_SUIT_Parameter_URI_List_cbor
314 .SUIT_URI_List_SUIT_Prioritized_URI_m_count,
315 "Expected 1 uri");
316 zassert_equal(0,
317 outerwrapper4
318 .SUIT_Outer_Wrapper_suit_manifest_cbor
319 .SUIT_Manifest_suit_install
320 .SUIT_Manifest_suit_install
321 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
322 .SUIT_Command_Sequence_SUIT_Command_m[1]
323 .SUIT_Command_union_SUIT_Directive_m
324 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m[0]
325 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m
326 .SUIT_Parameters_SUIT_Parameter_URI_List_cbor
327 .SUIT_URI_List_SUIT_Prioritized_URI_m[0]
328 .SUIT_Prioritized_URI_priority,
329 "Expected priority x");
330 zassert_mem_equal(expected_uri,
331 outerwrapper4
332 .SUIT_Outer_Wrapper_suit_manifest_cbor
333 .SUIT_Manifest_suit_install
334 .SUIT_Manifest_suit_install
335 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
336 .SUIT_Command_Sequence_SUIT_Command_m[1]
337 .SUIT_Command_union_SUIT_Directive_m
338 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m[0]
339 .SUIT_Directive_Set_Parameters_SUIT_Parameters_m
340 .SUIT_Parameters_SUIT_Parameter_URI_List_cbor
341 .SUIT_URI_List_SUIT_Prioritized_URI_m[0]
342 .SUIT_Prioritized_URI_uri.value,
343 sizeof(expected_uri) - 1,
344 "Expected example.com uri");
345 zassert_equal(SUIT_Command_union_SUIT_Directive_m_c,
346 outerwrapper4
347 .SUIT_Outer_Wrapper_suit_manifest_cbor
348 .SUIT_Manifest_suit_install
349 .SUIT_Manifest_suit_install
350 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
351 .SUIT_Command_Sequence_SUIT_Command_m[2]
352 .SUIT_Command_union_choice,
353 "Expected directive");
354 zassert_equal(SUIT_Directive_Fetch_c,
355 outerwrapper4
356 .SUIT_Outer_Wrapper_suit_manifest_cbor
357 .SUIT_Manifest_suit_install
358 .SUIT_Manifest_suit_install
359 .SUIT_Severable_Command_Sequence3_SUIT_Command_Sequence_bstr_cbor
360 .SUIT_Command_Sequence_SUIT_Command_m[2]
361 .SUIT_Command_union_SUIT_Directive_m
362 .SUIT_Directive_choice,
363 "Expected Directive_Fetch");
364 }
365
366
367 ZTEST_SUITE(cbor_decode_test, NULL, NULL, NULL, NULL, NULL);
368