1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <commonlib/bsd/cbfs_private.h>
5 #include <commonlib/region.h>
7 #include <tests/lib/cbfs_util.h>
8 #include <tests/test.h>
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
);
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
);
28 return VB2_SHA256_DIGEST_SIZE
;
31 vb2_error_t
vb2_hash_verify(const void *buf
, uint32_t size
, const struct vb2_hash
*hash
)
33 check_expected_ptr(buf
);
35 assert_int_equal(hash
->algo
, VB2_HASH_SHA256
);
37 if (!memcmp(hash
->sha256
, good_hash
, sizeof(good_hash
)))
40 if (!memcmp(hash
->sha256
, bad_hash
, sizeof(bad_hash
)))
41 return VB2_ERROR_SHA_MISMATCH
;
43 fail_msg("%s called with bad hash", __func__
);
44 return VB2_ERROR_SHA_MISMATCH
;
47 size_t ulzman(const void *src
, size_t srcn
, void *dst
, size_t dstn
)
49 fail_msg("Unexpected call to %s", __func__
);
53 size_t ulz4fn(const void *src
, size_t srcn
, void *dst
, size_t dstn
)
55 fail_msg("Unexpected call to %s", __func__
);
59 vb2_error_t
vb2_digest_init(struct vb2_digest_context
*dc
, enum vb2_hash_algorithm hash_alg
)
61 if (hash_alg
!= VB2_HASH_SHA256
) {
62 fail_msg("Unsupported hash algorithm: %d\n", hash_alg
);
63 return VB2_ERROR_SHA_INIT_ALGORITHM
;
69 vb2_error_t
vb2_digest_extend(struct vb2_digest_context
*dc
, const uint8_t *buf
, uint32_t size
)
76 vb2_error_t
vb2_digest_finalize(struct vb2_digest_context
*dc
, uint8_t *digest
, uint32_t size
)
78 memcpy(digest
, mock_ptr_type(void *), size
);
82 /* Original function alias created by test framework. Used for call wrapping in mock below. */
83 enum cb_err
__real_cbfs_lookup(cbfs_dev_t dev
, const char *name
, union cbfs_mdata
*mdata_out
,
84 size_t *data_offset_out
, struct vb2_hash
*metadata_hash
);
86 enum cb_err
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 const enum cb_err err
=
90 __real_cbfs_lookup(dev
, name
, mdata_out
, data_offset_out
, metadata_hash
);
91 assert_int_equal(mock_type(enum cb_err
), err
);
97 static int setup_test_cbfs(void **state
)
99 memset(&cbd
, 0, sizeof(cbd
));
103 static void test_cbfs_map_no_hash(void **state
)
105 void *mapping
= NULL
;
106 assert_int_equal(0, rdev_chain_mem(&cbd
.rdev
, &file_no_hash
, sizeof(file_no_hash
)));
108 if (CONFIG(CBFS_VERIFICATION
)) {
109 /* File with no hash. No hash causes hash mismatch by default,
110 so mapping will not be completed successfully. */
111 expect_value(cbfs_get_boot_device
, force_ro
, false);
112 will_return(cbfs_lookup
, CB_SUCCESS
);
113 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
114 assert_null(mapping
);
116 expect_value(cbfs_get_boot_device
, force_ro
, false);
117 will_return(cbfs_lookup
, CB_SUCCESS
);
118 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
119 assert_ptr_equal(mapping
, file_no_hash
.attrs_and_data
);
123 static void test_cbfs_map_valid_hash(void **state
)
125 void *mapping
= NULL
;
127 rdev_chain_mem(&cbd
.rdev
, &file_valid_hash
, sizeof(file_valid_hash
)));
129 if (CONFIG(CBFS_VERIFICATION
)) {
130 expect_value(cbfs_get_boot_device
, force_ro
, false);
131 expect_value(vb2_hash_verify
, buf
,
132 &file_valid_hash
.attrs_and_data
[HASH_ATTR_SIZE
]);
133 expect_value(vb2_hash_verify
, size
, TEST_DATA_1_SIZE
);
134 will_return(cbfs_lookup
, CB_SUCCESS
);
135 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
136 assert_ptr_equal(mapping
, &file_valid_hash
.attrs_and_data
[HASH_ATTR_SIZE
]);
138 expect_value(cbfs_get_boot_device
, force_ro
, false);
139 will_return(cbfs_lookup
, CB_SUCCESS
);
140 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
141 assert_ptr_equal(mapping
, &file_valid_hash
.attrs_and_data
[HASH_ATTR_SIZE
]);
145 static void test_cbfs_map_invalid_hash(void **state
)
147 void *mapping
= NULL
;
149 0, rdev_chain_mem(&cbd
.rdev
, &file_broken_hash
, sizeof(file_broken_hash
)));
151 if (CONFIG(CBFS_VERIFICATION
)) {
152 expect_value(cbfs_get_boot_device
, force_ro
, false);
153 expect_value(vb2_hash_verify
, buf
,
154 &file_broken_hash
.attrs_and_data
[HASH_ATTR_SIZE
]);
155 expect_value(vb2_hash_verify
, size
, TEST_DATA_1_SIZE
);
156 will_return(cbfs_lookup
, CB_SUCCESS
);
157 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
158 assert_null(mapping
);
160 expect_value(cbfs_get_boot_device
, force_ro
, false);
161 will_return(cbfs_lookup
, CB_SUCCESS
);
162 mapping
= cbfs_map(TEST_DATA_1_FILENAME
, NULL
);
163 assert_ptr_equal(mapping
, &file_broken_hash
.attrs_and_data
[HASH_ATTR_SIZE
]);
167 void test_init_boot_device_verify(void **state
)
169 struct vb2_hash hash
= {.algo
= VB2_HASH_SHA256
};
170 const uint8_t hash_value
[VB2_SHA256_DIGEST_SIZE
] = {0};
171 memset(&cbd
, 0, sizeof(cbd
));
173 rdev_chain_mem(&cbd
.rdev
, &file_valid_hash
, sizeof(file_valid_hash
)));
175 if (CONFIG(CBFS_VERIFICATION
)) {
176 expect_memory(vb2_digest_extend
, buf
, &file_valid_hash
,
177 be32_to_cpu(file_valid_hash
.header
.offset
));
178 expect_value(vb2_digest_extend
, size
,
179 be32_to_cpu(file_valid_hash
.header
.offset
));
180 will_return(vb2_digest_finalize
, hash_value
);
183 assert_int_equal(CB_SUCCESS
, cbfs_init_boot_device(&cbd
, &hash
));
188 const struct CMUnitTest tests
[] = {
189 cmocka_unit_test_setup(test_cbfs_map_no_hash
, setup_test_cbfs
),
190 cmocka_unit_test_setup(test_cbfs_map_valid_hash
, setup_test_cbfs
),
191 cmocka_unit_test_setup(test_cbfs_map_invalid_hash
, setup_test_cbfs
),
193 cmocka_unit_test(test_init_boot_device_verify
),
196 return cb_run_group_tests(tests
, NULL
, NULL
);