blob: 3f3573961f3213d853bed7cbdba1f9c5b6b9ba0c [file] [log] [blame]
Jakub Czapiga6f3fd632021-07-22 08:52:46 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <cbfs.h>
4#include <commonlib/region.h>
5#include <string.h>
6#include <tests/lib/cbfs_util.h>
7#include <tests/test.h>
8
9
10/* Mocks */
11
12static struct cbfs_boot_device cbd;
13
14const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro)
15{
16 check_expected(force_ro);
17 return &cbd;
18}
19
20size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg)
21{
22 if (hash_alg != VB2_HASH_SHA256) {
23 fail_msg("Unsupported hash algorithm: %d\n", hash_alg);
24 return 0;
25 }
26
27 return VB2_SHA256_DIGEST_SIZE;
28}
29
30vb2_error_t vb2_hash_verify(const void *buf, uint32_t size, const struct vb2_hash *hash)
31{
32 check_expected_ptr(buf);
33 check_expected(size);
34 assert_int_equal(hash->algo, VB2_HASH_SHA256);
35
36 if (!memcmp(hash->sha256, good_hash, sizeof(good_hash)))
37 return VB2_SUCCESS;
38
39 if (!memcmp(hash->sha256, bad_hash, sizeof(bad_hash)))
40 return VB2_ERROR_SHA_MISMATCH;
41
42 fail_msg("%s called with bad hash", __func__);
43 return VB2_ERROR_SHA_MISMATCH;
44}
45
46size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
47{
48 fail_msg("Unexpected call to %s", __func__);
49 return 0;
50}
51
52size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
53{
54 fail_msg("Unexpected call to %s", __func__);
55 return 0;
56}
57
58vb2_error_t vb2_digest_init(struct vb2_digest_context *dc, enum vb2_hash_algorithm hash_alg)
59{
60 if (hash_alg != VB2_HASH_SHA256) {
61 fail_msg("Unsupported hash algorithm: %d\n", hash_alg);
62 return VB2_ERROR_SHA_INIT_ALGORITHM;
63 }
64
65 return VB2_SUCCESS;
66}
67
68vb2_error_t vb2_digest_extend(struct vb2_digest_context *dc, const uint8_t *buf, uint32_t size)
69{
70 check_expected(buf);
71 check_expected(size);
72 return VB2_SUCCESS;
73}
74
75vb2_error_t vb2_digest_finalize(struct vb2_digest_context *dc, uint8_t *digest, uint32_t size)
76{
77 memcpy(digest, mock_ptr_type(void *), size);
78 return VB2_SUCCESS;
79}
80
81/* Original function alias created by test framework. Used for call wrapping in mock below. */
82cb_err_t __real_cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
83 size_t *data_offset_out, struct vb2_hash *metadata_hash);
84
85cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out,
86 size_t *data_offset_out, struct vb2_hash *metadata_hash)
87{
88 const cb_err_t err =
89 __real_cbfs_lookup(dev, name, mdata_out, data_offset_out, metadata_hash);
90 assert_int_equal(mock_type(cb_err_t), err);
91 return err;
92}
93
94/* Tests */
95
96static int setup_test_cbfs(void **state)
97{
98 memset(&cbd, 0, sizeof(cbd));
99 return 0;
100}
101
102static void test_cbfs_map_no_hash(void **state)
103{
104 void *mapping = NULL;
105 assert_int_equal(0, rdev_chain_mem(&cbd.rdev, &file_no_hash, sizeof(file_no_hash)));
106
107 if (CONFIG(CBFS_VERIFICATION)) {
108 /* File with no hash. No hash causes hash mismatch by default,
109 so mapping will not be completed successfully. */
110 expect_value(cbfs_get_boot_device, force_ro, false);
111 will_return(cbfs_lookup, CB_SUCCESS);
112 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
113 assert_null(mapping);
114 } else {
115 expect_value(cbfs_get_boot_device, force_ro, false);
116 will_return(cbfs_lookup, CB_SUCCESS);
117 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
118 assert_ptr_equal(mapping, file_no_hash.attrs_and_data);
119 }
120}
121
122static void test_cbfs_map_valid_hash(void **state)
123{
124 void *mapping = NULL;
125 assert_int_equal(0,
126 rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash)));
127
128 if (CONFIG(CBFS_VERIFICATION)) {
129 expect_value(cbfs_get_boot_device, force_ro, false);
130 expect_value(vb2_hash_verify, buf,
131 &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
132 expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
133 will_return(cbfs_lookup, CB_SUCCESS);
134 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
135 assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
136 } else {
137 expect_value(cbfs_get_boot_device, force_ro, false);
138 will_return(cbfs_lookup, CB_SUCCESS);
139 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
140 assert_ptr_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE]);
141 }
142}
143
144static void test_cbfs_map_invalid_hash(void **state)
145{
146 void *mapping = NULL;
147 assert_int_equal(
148 0, rdev_chain_mem(&cbd.rdev, &file_broken_hash, sizeof(file_broken_hash)));
149
150 if (CONFIG(CBFS_VERIFICATION)) {
151 expect_value(cbfs_get_boot_device, force_ro, false);
152 expect_value(vb2_hash_verify, buf,
153 &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]);
154 expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE);
155 will_return(cbfs_lookup, CB_SUCCESS);
156 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
157 assert_null(mapping);
158 } else {
159 expect_value(cbfs_get_boot_device, force_ro, false);
160 will_return(cbfs_lookup, CB_SUCCESS);
161 mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL);
162 assert_ptr_equal(mapping, &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE]);
163 }
164}
165
166void test_init_boot_device_verify(void **state)
167{
168 struct vb2_hash hash = {.algo = VB2_HASH_SHA256};
169 const uint8_t hash_value[VB2_SHA256_DIGEST_SIZE] = {0};
170 memset(&cbd, 0, sizeof(cbd));
171 assert_int_equal(0,
172 rdev_chain_mem(&cbd.rdev, &file_valid_hash, sizeof(file_valid_hash)));
173
174 if (CONFIG(CBFS_VERIFICATION)) {
175 expect_memory(vb2_digest_extend, buf, &file_valid_hash,
176 be32_to_cpu(file_valid_hash.header.offset));
177 expect_value(vb2_digest_extend, size,
178 be32_to_cpu(file_valid_hash.header.offset));
179 will_return(vb2_digest_finalize, hash_value);
180 }
181
182 assert_int_equal(CB_SUCCESS, cbfs_init_boot_device(&cbd, &hash));
183}
184
185int main(void)
186{
187 const struct CMUnitTest tests[] = {
188 cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs),
189 cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs),
190 cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs),
191
192 cmocka_unit_test(test_init_boot_device_verify),
193 };
194
195 return cb_run_group_tests(tests, NULL, NULL);
196}