1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <mocks/cbfs_util.h>
7 #include <tests/test.h>
9 #include "../libcbfs/cbfs.c"
13 unsigned long virtual_offset
= 0;
14 struct sysinfo_t lib_sysinfo
;
16 size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg
)
18 if (hash_alg
!= VB2_HASH_SHA256
) {
19 fail_msg("Unsupported hash algorithm: %d\n", hash_alg
);
23 return VB2_SHA256_DIGEST_SIZE
;
26 vb2_error_t
vb2_hash_verify(bool allow_hwcrypto
, const void *buf
, uint32_t size
,
27 const struct vb2_hash
*hash
)
29 assert_true(allow_hwcrypto
);
30 check_expected_ptr(buf
);
33 assert_int_equal(hash
->algo
, VB2_HASH_SHA256
);
35 if (!memcmp(hash
->sha256
, good_hash
, sizeof(good_hash
)))
38 if (!memcmp(hash
->sha256
, bad_hash
, sizeof(bad_hash
)))
39 return VB2_ERROR_SHA_MISMATCH
;
41 fail_msg("%s called with bad hash", __func__
);
42 return VB2_ERROR_SHA_MISMATCH
;
45 bool vb2api_hwcrypto_allowed(struct vb2_context
*ctx
)
50 struct vb2_context
*vboot_get_context(void)
55 unsigned long ulzman(const unsigned char *src
, unsigned long srcn
, unsigned char *dst
,
58 size_t copy_size
= MIN(srcn
, dstn
);
60 memcpy(dst
, src
, copy_size
);
64 size_t ulz4fn(const void *src
, size_t srcn
, void *dst
, size_t dstn
)
66 size_t copy_size
= MIN(srcn
, dstn
);
68 memcpy(dst
, src
, copy_size
);
72 enum cb_err
cbfs_mcache_lookup(const void *mcache
, size_t mcache_size
, const char *name
,
73 union cbfs_mdata
*mdata_out
, size_t *data_offset_out
)
75 return CB_CBFS_CACHE_FULL
;
78 enum cb_err
cbfs_lookup(cbfs_dev_t dev
, const char *name
, union cbfs_mdata
*mdata_out
,
79 size_t *data_offset_out
, struct vb2_hash
*metadata_hash
)
84 enum cb_err ret
= mock_type(enum cb_err
);
85 if (ret
!= CB_SUCCESS
)
88 memcpy(mdata_out
, mock_ptr_type(const union cbfs_mdata
*), sizeof(union cbfs_mdata
));
89 *data_offset_out
= mock_type(size_t);
93 static void expect_cbfs_lookup(const char *name
, enum cb_err err
, const union cbfs_mdata
*mdata
,
94 size_t data_offset_out
)
96 expect_string(cbfs_lookup
, name
, name
);
97 will_return(cbfs_lookup
, err
);
99 if (err
== CB_SUCCESS
) {
100 will_return(cbfs_lookup
, mdata
);
101 will_return(cbfs_lookup
, data_offset_out
);
105 const void *cbfs_find_attr(const union cbfs_mdata
*mdata
, uint32_t attr_tag
, size_t size_check
)
107 return mock_ptr_type(void *);
110 enum cb_err
fmap_locate_area(const char *name
, size_t *offset
, size_t *size
)
117 ssize_t
boot_device_read(void *buf
, size_t offset
, size_t size
)
119 /* Offset should be based on an address from lib_sysinfo.cbfs_offset */
120 memcpy(buf
, (void *)offset
, size
);
125 const struct vb2_hash
*cbfs_file_hash(const union cbfs_mdata
*mdata
)
127 return mock_ptr_type(const struct vb2_hash
*);
132 static void clear_cbfs_boot_devices(void)
134 lib_sysinfo
.cbfs_ro_mcache_offset
= 0;
135 lib_sysinfo
.cbfs_ro_mcache_size
= 0;
136 lib_sysinfo
.cbfs_offset
= 0;
137 lib_sysinfo
.cbfs_size
= 0;
138 lib_sysinfo
.cbfs_rw_mcache_offset
= 0;
139 lib_sysinfo
.cbfs_rw_mcache_size
= 0;
140 memset((void *)cbfs_get_boot_device(true), 0, sizeof(struct cbfs_boot_device
));
141 memset((void *)cbfs_get_boot_device(false), 0, sizeof(struct cbfs_boot_device
));
144 void set_cbfs(uint64_t offset
, size_t size
)
146 clear_cbfs_boot_devices();
147 lib_sysinfo
.cbfs_offset
= offset
;
148 lib_sysinfo
.cbfs_size
= size
;
153 static int setup_test_cbfs(void **state
)
155 clear_cbfs_boot_devices();
159 static void test_cbfs_map_no_hash(void **state
)
161 void *mapping
= NULL
;
164 set_cbfs((uint64_t)&file_no_hash
, sizeof(file_no_hash
));
166 expect_cbfs_lookup(TEST_DATA_1_FILENAME
, CB_SUCCESS
,
167 (const union cbfs_mdata
*)&file_no_hash
,
168 be32toh(file_no_hash
.header
.offset
));
169 will_return(cbfs_find_attr
, NULL
);
171 if (CONFIG(LP_CBFS_VERIFICATION
)) {
172 /* File with no hash. No hash causes hash mismatch by default,
173 so mapping will not be completed successfully. */
174 will_return(cbfs_file_hash
, NULL
);
175 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
176 assert_null(mapping
);
178 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, &size
);
179 assert_non_null(mapping
);
180 assert_int_equal(TEST_DATA_1_SIZE
, size
);
181 assert_memory_equal(test_data_1
, mapping
, size
);
186 static void test_cbfs_map_valid_hash_impl(void **state
, bool lz4_compressed
)
188 void *mapping
= NULL
;
190 struct vb2_hash hash
= {
191 .algo
= VB2_HASH_SHA256
,
193 memcpy(&hash
.sha256
, good_hash
, VB2_SHA256_DIGEST_SIZE
);
195 set_cbfs((uint64_t)&file_valid_hash
, sizeof(file_valid_hash
));
197 expect_cbfs_lookup(TEST_DATA_1_FILENAME
, CB_SUCCESS
,
198 (const union cbfs_mdata
*)&file_valid_hash
,
199 be32toh(file_valid_hash
.header
.offset
));
201 if (lz4_compressed
) {
202 struct cbfs_file_attr_compression cattr
= {
203 .compression
= htobe32(CBFS_COMPRESS_LZ4
),
204 .decompressed_size
= htobe32(TEST_DATA_1_SIZE
),
206 will_return(cbfs_find_attr
, &cattr
);
207 expect_function_call(ulz4fn
);
209 will_return(cbfs_find_attr
, NULL
);
212 if (CONFIG(LP_CBFS_VERIFICATION
)) {
213 will_return(cbfs_file_hash
, &hash
);
214 expect_memory(vb2_hash_verify
, buf
,
215 &file_valid_hash
.attrs_and_data
[HASH_ATTR_SIZE
], HASH_ATTR_SIZE
);
216 expect_value(vb2_hash_verify
, size
, TEST_DATA_1_SIZE
);
217 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, &size
);
218 assert_non_null(mapping
);
219 assert_int_equal(TEST_DATA_1_SIZE
, size
);
220 assert_memory_equal(mapping
, &file_valid_hash
.attrs_and_data
[HASH_ATTR_SIZE
],
223 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, &size
);
224 assert_non_null(mapping
);
225 assert_int_equal(TEST_DATA_1_SIZE
, size
);
226 assert_memory_equal(test_data_1
, mapping
, size
);
231 static void test_cbfs_map_valid_hash(void **state
)
233 test_cbfs_map_valid_hash_impl(state
, false);
236 static void test_cbfs_map_valid_hash_with_lz4(void **state
)
238 test_cbfs_map_valid_hash_impl(state
, true);
241 static void test_cbfs_map_invalid_hash(void **state
)
243 void *mapping
= NULL
;
245 struct vb2_hash hash
= {
246 .algo
= VB2_HASH_SHA256
,
248 memcpy(&hash
.sha256
, bad_hash
, VB2_SHA256_DIGEST_SIZE
);
250 set_cbfs((uint64_t)&file_broken_hash
, sizeof(file_broken_hash
));
252 expect_cbfs_lookup(TEST_DATA_1_FILENAME
, CB_SUCCESS
,
253 (const union cbfs_mdata
*)&file_broken_hash
,
254 be32toh(file_broken_hash
.header
.offset
));
255 will_return(cbfs_find_attr
, NULL
);
257 if (CONFIG(LP_CBFS_VERIFICATION
)) {
258 will_return(cbfs_file_hash
, &hash
);
259 expect_memory(vb2_hash_verify
, buf
,
260 &file_broken_hash
.attrs_and_data
[HASH_ATTR_SIZE
], HASH_ATTR_SIZE
);
261 expect_value(vb2_hash_verify
, size
, TEST_DATA_1_SIZE
);
262 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
263 assert_null(mapping
);
265 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, &size
);
266 assert_non_null(mapping
);
267 assert_int_equal(TEST_DATA_1_SIZE
, size
);
268 assert_memory_equal(test_data_1
, mapping
, size
);
275 const struct CMUnitTest tests
[] = {
276 cmocka_unit_test_setup(test_cbfs_map_no_hash
, setup_test_cbfs
),
277 cmocka_unit_test_setup(test_cbfs_map_valid_hash
, setup_test_cbfs
),
278 cmocka_unit_test_setup(test_cbfs_map_valid_hash_with_lz4
, setup_test_cbfs
),
279 cmocka_unit_test_setup(test_cbfs_map_invalid_hash
, setup_test_cbfs
),
282 return lp_run_group_tests(tests
, NULL
, NULL
);