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 "manifest2_decode.h"
9 #include "zcbor_print.h"
10 
11 
12 /* draft-ietf-suit-manifest-02 Example 0 */
13 uint8_t test_vector1[] = {
14 	0xa2, 0x01, 0x58, 0x54, 0xd2, 0x84, 0x43, 0xa1, 0x01,
15 	0x26, 0xa1, 0x04, 0x48, 0x74, 0x65, 0x73, 0x74, 0x20,
16 	0x6b, 0x65, 0x79, 0xf6, 0x58, 0x40, 0xeb, 0xec, 0xb6,
17 	0x6c, 0xbe, 0xcb, 0x19, 0xdc, 0xed, 0xac, 0xf8, 0x45,
18 	0x9c, 0x1a, 0x22, 0xa1, 0x45, 0x37, 0x81, 0xba, 0x98,
19 	0xd8, 0xff, 0xb9, 0xd4, 0xe2, 0x91, 0x2f, 0x29, 0xd2,
20 	0x3b, 0xac, 0x5a, 0xe3, 0xd5, 0x1f, 0x1f, 0xf0, 0xc1,
21 	0xb1, 0xdf, 0x05, 0xe2, 0x07, 0xca, 0x17, 0x48, 0x3a,
22 	0x57, 0xed, 0xe9, 0x14, 0xcf, 0x82, 0x6b, 0x73, 0x59,
23 	0x91, 0x37, 0x88, 0x1c, 0x83, 0x64, 0xc8, 0x02, 0x58,
24 	0x51, 0xa4, 0x01, 0x01, 0x02, 0x01, 0x03, 0x58, 0x40,
25 	0xa2, 0x02, 0x4c, 0x81, 0x82, 0x45, 0x46, 0x6c, 0x61,
26 	0x73, 0x68, 0x43, 0x00, 0x34, 0x01, 0x04, 0x58, 0x2e,
27 	0x82, 0x13, 0xa2, 0x0b, 0x58, 0x24, 0x82, 0x02, 0x58,
28 	0x20, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
29 	0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01,
30 	0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc,
31 	0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x0c, 0x19, 0x87,
32 	0xd0, 0x0c, 0x47, 0x86, 0x0c, 0x00, 0x03, 0xf6, 0x17,
33 	0xf6
34 };
35 
36 static struct SUIT_Outer_Wrapper outerwrapper1;
37 static struct SUIT_Command_Sequence sequence;
38 
ZTEST(cbor_decode_test2,test_5)39 ZTEST(cbor_decode_test2, test_5)
40 {
41 	struct SUIT_Manifest *manifest;
42 	size_t decode_len;
43 	struct SUIT_Component_Identifier *component;
44 	uint8_t expected_component0[] = {0x46, 0x6c, 0x61, 0x73, 0x68};
45 	uint8_t expected_component1[] = {0x00, 0x34, 0x01};
46 	int res;
47 
48 	zcbor_log("test_vector at: 0x%zu\r\n", (size_t)test_vector1);
49 	zcbor_log("test_vector end at: 0x%zu\r\n",
50 				((size_t)test_vector1) + sizeof(test_vector1));
51 	memset(&outerwrapper1, 0, sizeof(outerwrapper1));
52 	res = cbor_decode_SUIT_Outer_Wrapper(test_vector1, sizeof(test_vector1),
53 						&outerwrapper1, &decode_len);
54 	zassert_equal(ZCBOR_SUCCESS, res, "top-level decoding failed.");
55 	zassert_equal(sizeof(test_vector1), decode_len, NULL);
56 	zassert_equal(SUIT_Manifest_Wrapped_suit_manifest_c, outerwrapper1
57 			.SUIT_Outer_Wrapper_SUIT_Manifest_Wrapped_m
58 			.SUIT_Manifest_Wrapped_choice,
59 			"wrong manifest variant");
60 	manifest = &outerwrapper1
61 			.SUIT_Outer_Wrapper_SUIT_Manifest_Wrapped_m
62 			.SUIT_Manifest_Wrapped_suit_manifest_cbor;
63 	zassert_equal(1, manifest
64 			->SUIT_Manifest_suit_manifest_sequence_number,
65 			"wrong sequence number");
66 	zassert_equal(1, manifest
67 			->SUIT_Manifest_suit_common_present,
68 			"common should be present");
69 	zassert_equal(0, manifest
70 			->SUIT_Manifest_suit_dependency_resolution_present,
71 			"should not be present");
72 	zassert_equal(0, manifest
73 			->SUIT_Manifest_suit_payload_fetch_present,
74 			"should not be present");
75 	zassert_equal(0, manifest
76 			->SUIT_Manifest_suit_install_present,
77 			"should not be present");
78 	zassert_equal(0, manifest
79 			->SUIT_Manifest_suit_validate_present,
80 			"should not be present");
81 	zassert_equal(0, manifest
82 			->SUIT_Manifest_suit_load_present,
83 			"should not be present");
84 	zassert_equal(1, manifest
85 			->SUIT_Manifest_suit_run_present,
86 			"should not be present");
87 	zassert_equal(0, manifest
88 			->SUIT_Manifest_suit_text_present,
89 			"should not be present");
90 	zassert_equal(0, manifest
91 			->SUIT_Manifest_suit_coswid_present,
92 			"should not be present");
93 	zassert_equal(1, manifest
94 			->SUIT_Manifest_suit_common
95 			.SUIT_Manifest_suit_common_cbor
96 			.SUIT_Common_suit_components
97 			.SUIT_Common_suit_components_cbor
98 			.SUIT_Components_SUIT_Component_Identifier_m_count,
99 			"Wrong number of common components");
100 	component = &manifest
101 			->SUIT_Manifest_suit_common
102 			.SUIT_Manifest_suit_common_cbor
103 			.SUIT_Common_suit_components
104 			.SUIT_Common_suit_components_cbor
105 			.SUIT_Components_SUIT_Component_Identifier_m[0];
106 	zassert_equal(2, component
107 			->SUIT_Component_Identifier_bstr_count,
108 			"Wrong number of elements in component");
109 	zassert_equal(sizeof(expected_component0), component
110 			->SUIT_Component_Identifier_bstr[0]
111 			.len,
112 			"component elem 0 len doesn't match.");
113 	zassert_mem_equal(expected_component0, component
114 			->SUIT_Component_Identifier_bstr[0]
115 			.value,
116 			sizeof(expected_component0),
117 			"component elem 0 doesn't match.");
118 	zassert_equal(sizeof(expected_component1), component
119 			->SUIT_Component_Identifier_bstr[1]
120 			.len,
121 			"component elem 1 len doesn't match.");
122 	zassert_mem_equal(expected_component1, component
123 			->SUIT_Component_Identifier_bstr[1]
124 			.value,
125 			sizeof(expected_component1),
126 			"component elem 1 doesn't match.");
127 
128 	zcbor_log("\r\n");
129 	zassert_equal(1, manifest
130 			->SUIT_Manifest_suit_common
131 			.SUIT_Manifest_suit_common_cbor
132 			.SUIT_Common_suit_common_sequence_present,
133 			"common sequence should be present.");
134 	memset(&sequence, 0, sizeof(sequence));
135 	res = cbor_decode_SUIT_Command_Sequence(
136 			manifest
137 			->SUIT_Manifest_suit_common
138 			.SUIT_Manifest_suit_common_cbor
139 			.SUIT_Common_suit_common_sequence
140 			.SUIT_Common_suit_common_sequence
141 			.value,
142 			manifest
143 			->SUIT_Manifest_suit_common
144 			.SUIT_Manifest_suit_common_cbor
145 			.SUIT_Common_suit_common_sequence
146 			.SUIT_Common_suit_common_sequence
147 			.len,
148 			&sequence, &decode_len);
149 	zassert_equal(ZCBOR_SUCCESS, res, "Parsing common sequence failed.");
150 	zassert_equal(manifest
151 			->SUIT_Manifest_suit_common
152 			.SUIT_Manifest_suit_common_cbor
153 			.SUIT_Common_suit_common_sequence
154 			.SUIT_Common_suit_common_sequence
155 			.len, decode_len, NULL);
156 	zassert_equal(1, sequence
157 			.SUIT_Command_Sequence_SUIT_Command_m_count,
158 			"Should be one command (was %d).", sequence
159 			.SUIT_Command_Sequence_SUIT_Command_m_count);
160 	zassert_equal(SUIT_Command_SUIT_Directive_m_c, sequence
161 			.SUIT_Command_Sequence_SUIT_Command_m[0]
162 			.SUIT_Command_choice,
163 			"Should be a directive.");
164 	zassert_equal(2, sequence
165 			.SUIT_Command_Sequence_SUIT_Command_m[0]
166 			.SUIT_Command_SUIT_Directive_m
167 			.suit_directive_set_parameters_m_l_map_SUIT_Parameters_m_count,
168 			"Should be two vars (parameters).");
169 	zcbor_log("\r\n");
170 
171 	memset(&sequence, 0, sizeof(sequence));
172 	res = cbor_decode_SUIT_Command_Sequence(
173 			manifest
174 			->SUIT_Manifest_suit_run
175 			.SUIT_Manifest_suit_run
176 			.value,
177 			manifest
178 			->SUIT_Manifest_suit_run
179 			.SUIT_Manifest_suit_run
180 			.len,
181 			&sequence, &decode_len);
182 	zassert_equal(ZCBOR_SUCCESS, res, "Parsing run command sequence failed.");
183 	zassert_equal(manifest
184 			->SUIT_Manifest_suit_run
185 			.SUIT_Manifest_suit_run
186 			.len, decode_len, NULL);
187 }
188 
189 ZTEST_SUITE(cbor_decode_test2, NULL, NULL, NULL, NULL, NULL);
190