arch/arm64: Support FEAT_CCIDX
[coreboot2.git] / tests / lib / cbfs-verification-test.c
blobb1a39bcaacb9a326ab0e54f893571055d57301e3
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <cbfs.h>
4 #include <commonlib/bsd/cbfs_private.h>
5 #include <commonlib/region.h>
6 #include <string.h>
7 #include <tests/lib/cbfs_util.h>
8 #include <tests/test.h>
11 /* Mocks */
13 static struct cbfs_boot_device cbd;
15 const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
17 check_expected(force_ro);
18 return &cbd;
21 size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg)
23 if (hash_alg != VB2_HASH_SHA256) {
24 fail_msg("Unsupported hash algorithm: %d\n", hash_alg);
25 return 0;
28 return VB2_SHA256_DIGEST_SIZE;
31 vb2_error_t vb2_hash_verify(bool allow_hwcrypto, const void *buf, uint32_t size,
32 const struct vb2_hash *hash)
34 assert_true(allow_hwcrypto);
35 check_expected_ptr(buf);
36 check_expected(size);
37 assert_int_equal(hash->algo, VB2_HASH_SHA256);
39 if (!memcmp(hash->sha256, good_hash, sizeof(good_hash)))
40 return VB2_SUCCESS;
42 if (!memcmp(hash->sha256, bad_hash, sizeof(bad_hash)))
43 return VB2_ERROR_SHA_MISMATCH;
45 fail_msg("%s called with bad hash", __func__);
46 return VB2_ERROR_SHA_MISMATCH;
49 size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
51 fail_msg("Unexpected call to %s", __func__);
52 return 0;
55 size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
57 fail_msg("Unexpected call to %s", __func__);
58 return 0;
61 vb2_error_t vb2_digest_init(struct vb2_digest_context *dc, bool allow_hwcrypto,
62 enum vb2_hash_algorithm hash_alg, uint32_t data_size)
64 if (hash_alg != VB2_HASH_SHA256) {
65 fail_msg("Unsupported hash algorithm: %d\n", hash_alg);
66 return VB2_ERROR_SHA_INIT_ALGORITHM;
69 return VB2_SUCCESS;
72 vb2_error_t vb2_digest_extend(struct vb2_digest_context *dc, const uint8_t *buf, uint32_t size)
74 check_expected(buf);
75 check_expected(size);
76 return VB2_SUCCESS;
79 vb2_error_t vb2_digest_finalize(struct vb2_digest_context *dc, uint8_t *digest, uint32_t size)
81 memcpy(digest, mock_ptr_type(void *), size);
82 return VB2_SUCCESS;
85 /* Original function alias created by test framework. Used for call wrapping in mock below. */
86 enum cb_err __real_cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
87 size_t *data_offset_out, struct vb2_hash *metadata_hash);
89 enum cb_err cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
90 size_t *data_offset_out, struct vb2_hash *metadata_hash)
92 const enum cb_err err =
93 __real_cbfs_lookup(dev, name, mdata_out, data_offset_out, metadata_hash);
94 assert_int_equal(mock_type(enum cb_err), err);
95 return err;
98 /* Tests */
100 static int setup_test_cbfs(void **state)
102 memset(&cbd, 0, sizeof(cbd));
103 return 0;
106 static void test_cbfs_map_no_hash(void **state)
108 void *mapping = NULL;
109 assert_int_equal(0, rdev_chain_mem(&cbd.rdev, &file_no_hash, sizeof(file_no_hash)));
111 if (CONFIG(CBFS_VERIFICATION)) {
112 /* File with no hash. No hash causes hash mismatch by default,
113 so mapping will not be completed successfully. */
114 expect_value(cbfs_get_boot_device, force_ro, false);
115 will_return(cbfs_lookup, CB_SUCCESS);
116 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
117 assert_null(mapping);
118 } else {
119 expect_value(cbfs_get_boot_device, force_ro, false);
120 will_return(cbfs_lookup, CB_SUCCESS);
121 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
122 assert_ptr_equal(mapping, file_no_hash.attrs_and_data);
126 static void test_cbfs_map_valid_hash(void **state)
128 void *mapping = NULL;
129 assert_int_equal(0,
130 rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash)));
132 if (CONFIG(CBFS_VERIFICATION)) {
133 expect_value(cbfs_get_boot_device, force_ro, false);
134 expect_value(vb2_hash_verify, buf,
135 &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
136 expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
137 will_return(cbfs_lookup, CB_SUCCESS);
138 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
139 assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
140 } else {
141 expect_value(cbfs_get_boot_device, force_ro, false);
142 will_return(cbfs_lookup, CB_SUCCESS);
143 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
144 assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
148 static void test_cbfs_map_invalid_hash(void **state)
150 void *mapping = NULL;
151 assert_int_equal(
152 0, rdev_chain_mem(&cbd.rdev, &file_broken_hash, sizeof(file_broken_hash)));
154 if (CONFIG(CBFS_VERIFICATION)) {
155 expect_value(cbfs_get_boot_device, force_ro, false);
156 expect_value(vb2_hash_verify, buf,
157 &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]);
158 expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
159 will_return(cbfs_lookup, CB_SUCCESS);
160 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
161 assert_null(mapping);
162 } else {
163 expect_value(cbfs_get_boot_device, force_ro, false);
164 will_return(cbfs_lookup, CB_SUCCESS);
165 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
166 assert_ptr_equal(mapping, &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]);
170 void test_init_boot_device_verify(void **state)
172 struct vb2_hash hash = {.algo = VB2_HASH_SHA256};
173 const uint8_t hash_value[VB2_SHA256_DIGEST_SIZE] = {0};
174 memset(&cbd, 0, sizeof(cbd));
175 assert_int_equal(0,
176 rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash)));
178 if (CONFIG(CBFS_VERIFICATION)) {
179 expect_memory(vb2_digest_extend, buf, &file_valid_hash,
180 be32_to_cpu(file_valid_hash.header.offset));
181 expect_value(vb2_digest_extend, size,
182 be32_to_cpu(file_valid_hash.header.offset));
183 will_return(vb2_digest_finalize, hash_value);
186 assert_int_equal(CB_SUCCESS, cbfs_init_boot_device(&cbd, &hash));
189 int main(void)
191 const struct CMUnitTest tests[] = {
192 cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs),
193 cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs),
194 cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs),
196 cmocka_unit_test(test_init_boot_device_verify),
199 return cb_run_group_tests(tests, NULL, NULL);