diff options
author | Jakub Czapiga <jacz@semihalf.com> | 2021-11-15 08:36:07 +0000 |
---|---|---|
committer | Felix Held <felix-coreboot@felixheld.de> | 2022-01-10 14:30:04 +0000 |
commit | 63e54275f684da6f6db8289561726adab5254b39 (patch) | |
tree | a01a1b40170f88ef0036585564eb30ac8b57ef48 /payloads/libpayload/tests | |
parent | 1fa3da4d9b30ad7e63b79b6966794ee179dc6501 (diff) |
libpayload: Implement new CBFS access API
This commit adds new CBFS API, which is based on the one available in
the main coreboot source tree. Libpayload implementation supports RO/RW
file lookups and file contents verification.
Change-Id: I00da0658dbac0cddf92ad55611def947932d23c7
Signed-off-by: Jakub Czapiga <jacz@semihalf.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/59497
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'payloads/libpayload/tests')
-rw-r--r-- | payloads/libpayload/tests/Makefile.inc | 1 | ||||
-rw-r--r-- | payloads/libpayload/tests/include/mocks/cbfs_util.h | 114 | ||||
-rw-r--r-- | payloads/libpayload/tests/libcbfs/Makefile.inc | 33 | ||||
-rw-r--r-- | payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c | 642 | ||||
-rw-r--r-- | payloads/libpayload/tests/libcbfs/cbfs-verification-test.c | 247 | ||||
-rw-r--r-- | payloads/libpayload/tests/mocks/cbfs_file_mock.c | 95 | ||||
-rw-r--r-- | payloads/libpayload/tests/mocks/die.c | 16 |
7 files changed, 1148 insertions, 0 deletions
diff --git a/payloads/libpayload/tests/Makefile.inc b/payloads/libpayload/tests/Makefile.inc index 55c455cf40..e1c2a232d4 100644 --- a/payloads/libpayload/tests/Makefile.inc +++ b/payloads/libpayload/tests/Makefile.inc @@ -34,6 +34,7 @@ TEST_CFLAGS := -include include/kconfig.h -include include/compiler.h TEST_CFLAGS += -Iinclude -Iinclude/mock TEST_CFLAGS += -I$(coreboottop)/src/commonlib/bsd/include TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) +TEST_CFLAGS += -I$(coreboottop)/3rdparty/vboot/firmware/include # Test specific includes TEST_CFLAGS += -I$(testsrc)/include diff --git a/payloads/libpayload/tests/include/mocks/cbfs_util.h b/payloads/libpayload/tests/include/mocks/cbfs_util.h new file mode 100644 index 0000000000..b0a1e79831 --- /dev/null +++ b/payloads/libpayload/tests/include/mocks/cbfs_util.h @@ -0,0 +1,114 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef MOCKS_CBFS_UTIL_H +#define MOCKS_CBFS_UTIL_H + +#include <cbfs.h> +#include <stddef.h> +#include <tests/test.h> + +#define BE32(be32) EMPTY_WRAP(\ + ((be32) >> 24) & 0xff, ((be32) >> 16) & 0xff, \ + ((be32) >> 8) & 0xff, ((be32) >> 0) & 0xff) + +#define BE64(be64) EMPTY_WRAP( \ + BE32(((be64) >> 32) & 0xFFFFFFFF), \ + BE32(((be64) >> 0) & 0xFFFFFFFF)) + +#define LE32(val32) EMPTY_WRAP(\ + ((val32) >> 0) & 0xff, ((val32) >> 8) & 0xff, \ + ((val32) >> 16) & 0xff, ((val32) >> 24) & 0xff) + +#define LE64(val64) EMPTY_WRAP( \ + BE32(((val64) >> 0) & 0xFFFFFFFF), \ + BE32(((val64) >> 32) & 0xFFFFFFFF)) + +#define FILENAME_SIZE 16 + +struct cbfs_test_file { + struct cbfs_file header; + u8 filename[FILENAME_SIZE]; + u8 attrs_and_data[200]; +}; + +#define TEST_MCACHE_SIZE (2 * MiB) + +#define HEADER_INITIALIZER(ftype, attr_len, file_len) { \ + .magic = CBFS_FILE_MAGIC, \ + .len = htobe32(file_len), \ + .type = htobe32(ftype), \ + .attributes_offset = \ + htobe32(attr_len ? sizeof(struct cbfs_file) + FILENAME_SIZE : 0), \ + .offset = htobe32(sizeof(struct cbfs_file) + FILENAME_SIZE + attr_len), \ +} + +#define HASH_ATTR_SIZE (offsetof(struct cbfs_file_attr_hash, hash.raw) + VB2_SHA256_DIGEST_SIZE) + +/* This macro basically does nothing but suppresses linter messages */ +#define EMPTY_WRAP(...) __VA_ARGS__ + +#define TEST_DATA_1_FILENAME "test/data/1" +#define TEST_DATA_1_SIZE sizeof((u8[]){TEST_DATA_1}) +#define TEST_DATA_1 EMPTY_WRAP( \ + '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', \ + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', \ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', \ + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', \ + '[', '\\', ']', '^', '_', '`', \ + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', \ + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') + +#define TEST_DATA_2_FILENAME "test/data/2" +#define TEST_DATA_2_SIZE sizeof((u8[]){TEST_DATA_2}) +#define TEST_DATA_2 EMPTY_WRAP( \ + 0x9d, 0xa9, 0x91, 0xac, 0x5d, 0xb2, 0x70, 0x76, 0x37, 0x94, 0x94, 0xa8, 0x8b, 0x78, \ + 0xb9, 0xaa, 0x1a, 0x8e, 0x9a, 0x16, 0xbe, 0xdc, 0x29, 0x42, 0x46, 0x58, 0xd4, 0x37, \ + 0x94, 0xca, 0x05, 0xdb, 0x54, 0xfa, 0xd8, 0x6e, 0x54, 0xd8, 0x30, 0x46, 0x5d, 0x62, \ + 0xc2, 0xce, 0xd8, 0x74, 0x60, 0xaf, 0x83, 0x8f, 0xfa, 0x97, 0xdd, 0x6e, 0xcb, 0x60, \ + 0xfa, 0xed, 0x8b, 0x55, 0x9e, 0xc1, 0xc2, 0x18, 0x4f, 0xe2, 0x28, 0x7e, 0xd7, 0x2f, \ + 0xa2, 0x86, 0xfb, 0x4d, 0x3e, 0x00, 0x5a, 0xf7, 0xc2, 0xad, 0x0e, 0xa7, 0xa2, 0xf7, \ + 0x38, 0x66, 0xe6, 0x5c, 0x76, 0x98, 0x89, 0x63, 0xeb, 0xc5, 0xf5, 0xb7, 0xa7, 0x58, \ + 0xe0, 0xf0, 0x2e, 0x2f, 0xb0, 0x95, 0xb7, 0x43, 0x28, 0x19, 0x2d, 0xef, 0x1a, 0xb3, \ + 0x42, 0x31, 0x55, 0x0f, 0xbc, 0xcd, 0x01, 0xe5, 0x39, 0x18, 0x88, 0x83, 0xb2, 0xc5, \ + 0x4b, 0x3b, 0x38, 0xe7) + +#define TEST_DATA_INT_1_FILENAME "test-int-1" +#define TEST_DATA_INT_1_SIZE 8 +#define TEST_DATA_INT_1 0xFEDCBA9876543210ULL + +#define TEST_DATA_INT_2_FILENAME "test-int-2" +#define TEST_DATA_INT_2_SIZE 8 +#define TEST_DATA_INT_2 0x10FE32DC54A97698ULL + +#define TEST_DATA_INT_3_FILENAME "test-int-3" +#define TEST_DATA_INT_3_SIZE 8 +#define TEST_DATA_INT_3 0xFA57F003B0036667ULL + +#define TEST_SHA256 \ + EMPTY_WRAP(0xef, 0xc7, 0xb1, 0x0a, 0xbf, 0x54, 0x2f, 0xaa, 0x12, 0xa6, 0xeb, 0xf, \ + 0xff, 0xf4, 0x19, 0xc1, 0x63, 0xf4, 0x60, 0x50, 0xc5, 0xb0, 0xbe, 0x37, \ + 0x32, 0x11, 0x19, 0x63, 0x61, 0xe0, 0x53, 0xe0) + +#define INVALID_SHA256 \ + EMPTY_WRAP('T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'n', 'o', 't', ' ', 'a', ' ', 'v', \ + 'a', 'l', 'i', 'd', ' ', 'S', 'H', 'A', '2', '5', '6', '!', '!', '!', '!', \ + '!', '!') + +extern const u8 test_data_1[TEST_DATA_1_SIZE]; +extern const u8 test_data_2[TEST_DATA_2_SIZE]; +extern const u8 test_data_int_1[TEST_DATA_INT_1_SIZE]; +extern const u8 test_data_int_2[TEST_DATA_INT_2_SIZE]; +extern const u8 test_data_int_3[TEST_DATA_INT_3_SIZE]; + +extern const u8 good_hash[VB2_SHA256_DIGEST_SIZE]; +extern const u8 bad_hash[VB2_SHA256_DIGEST_SIZE]; + +extern const struct cbfs_test_file file_no_hash; +extern const struct cbfs_test_file file_valid_hash; +extern const struct cbfs_test_file file_broken_hash; +extern const struct cbfs_test_file test_file_1; +extern const struct cbfs_test_file test_file_2; +extern const struct cbfs_test_file test_file_int_1; +extern const struct cbfs_test_file test_file_int_2; +extern const struct cbfs_test_file test_file_int_3; + +#endif /* MOCKS_CBFS_UTIL_H */ diff --git a/payloads/libpayload/tests/libcbfs/Makefile.inc b/payloads/libpayload/tests/libcbfs/Makefile.inc new file mode 100644 index 0000000000..ad4efedcba --- /dev/null +++ b/payloads/libpayload/tests/libcbfs/Makefile.inc @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: GPL-2.0-only + +tests-y += cbfs-lookup-no-fallback-test +tests-y += cbfs-lookup-has-fallback-test +tests-y += cbfs-verification-no-sha512-test +tests-y += cbfs-verification-has-sha512-test +tests-y += cbfs-no-verification-no-sha512-test +tests-y += cbfs-no-verification-has-sha512-test + + +cbfs-lookup-no-fallback-test-srcs += tests/libcbfs/cbfs-lookup-test.c +cbfs-lookup-no-fallback-test-srcs += tests/mocks/cbfs_file_mock.c +cbfs-lookup-no-fallback-test-config += CONFIG_LP_ENABLE_CBFS_FALLBACK=0 +cbfs-lookup-no-fallback-test-config += CONFIG_LP_LZ4=1 +cbfs-lookup-no-fallback-test-config += CONFIG_LP_LZMA=1 + +$(call copy-test,cbfs-lookup-no-fallback-test,cbfs-lookup-has-fallback-test) +cbfs-lookup-has-fallback-test-config += CONFIG_LP_ENABLE_CBFS_FALLBACK=1 + +cbfs-verification-no-sha512-test-srcs += tests/libcbfs/cbfs-verification-test.c +cbfs-verification-no-sha512-test-srcs += tests/mocks/cbfs_file_mock.c +cbfs-verification-no-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=1 +cbfs-verification-no-sha512-test-config += VB2_SUPPORT_SHA512=0 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-verification-has-sha512-test) +cbfs-verification-has-sha512-test-config += VB2_SUPPORT_SHA512=1 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-no-sha512-test) +cbfs-verification-has-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=0 + +$(call copy-test,cbfs-verification-no-sha512-test,cbfs-no-verification-has-sha512-test) +cbfs-verification-has-sha512-test-config += CONFIG_LP_CBFS_VERIFICATION=0 +cbfs-verification-has-sha512-test-config += VB2_SUPPORT_SHA512=1 diff --git a/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c new file mode 100644 index 0000000000..0f7cdc7108 --- /dev/null +++ b/payloads/libpayload/tests/libcbfs/cbfs-lookup-test.c @@ -0,0 +1,642 @@ +/* SPDX-License-Identifier: GPL-2.0.-only */ + +#include <libpayload-config.h> +#include <cbfs.h> +#include <cbfs_glue.h> +#include <commonlib/bsd/cb_err.h> +#include <commonlib/bsd/cbfs_mdata.h> +#include <endian.h> +#include <mocks/cbfs_util.h> +#include <stdlib.h> +#include <string.h> +#include <sysinfo.h> +#include <tests/test.h> + +#include "../libcbfs/cbfs.c" + +/* Mocks */ + +unsigned long virtual_offset = 0; +struct sysinfo_t lib_sysinfo; + +unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst, + unsigned long dstn) +{ + assert_true(dstn != 0); + check_expected(srcn); + check_expected(dstn); + memcpy(dst, src, dstn); + return dstn; +} + +size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) +{ + assert_non_null(dstn); + check_expected(srcn); + check_expected(dstn); + memcpy(dst, src, dstn); + return dstn; +} + +static size_t test_fmap_offset = 0; +static size_t test_fmap_size = 0; +static cb_err_t test_fmap_result = CB_SUCCESS; + +static void set_fmap_locate_area_results(size_t offset, size_t size, size_t result) +{ + test_fmap_offset = offset; + test_fmap_size = size; + test_fmap_result = result; +} + +cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size) +{ + *offset = test_fmap_offset; + *size = test_fmap_size; + return test_fmap_result; +} + +cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name, + union cbfs_mdata *mdata_out, size_t *data_offset_out) +{ + assert_non_null(mcache); + assert_true(mcache_size > 0 && mcache_size % CBFS_MCACHE_ALIGNMENT == 0); + assert_non_null(mdata_out); + assert_non_null(data_offset_out); + + check_expected(name); + + cb_err_t ret = mock_type(cb_err_t); + if (ret != CB_SUCCESS) + return ret; + + memcpy(mdata_out, mock_ptr_type(const union cbfs_mdata *), sizeof(union cbfs_mdata)); + *data_offset_out = mock_type(size_t); + return CB_SUCCESS; +} + +static void expect_cbfs_mcache_lookup(const char *name, cb_err_t err, + const union cbfs_mdata *mdata, size_t data_offset_out) +{ + expect_string(cbfs_mcache_lookup, name, name); + will_return(cbfs_mcache_lookup, err); + + if (err == CB_SUCCESS) { + will_return(cbfs_mcache_lookup, mdata); + will_return(cbfs_mcache_lookup, data_offset_out); + } +} + +cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out, + size_t *data_offset_out, struct vb2_hash *metadata_hash) +{ + assert_non_null(dev); + check_expected(name); + + cb_err_t ret = mock_type(cb_err_t); + if (ret != CB_SUCCESS) + return ret; + + memcpy(mdata_out, mock_ptr_type(const union cbfS_mdata *), sizeof(union cbfs_mdata)); + *data_offset_out = mock_type(size_t); + return CB_SUCCESS; +} + +static void expect_cbfs_lookup(const char *name, cb_err_t err, const union cbfs_mdata *mdata, + size_t data_offset_out) +{ + expect_string(cbfs_lookup, name, name); + will_return(cbfs_lookup, err); + + if (err == CB_SUCCESS) { + will_return(cbfs_lookup, mdata); + will_return(cbfs_lookup, data_offset_out); + } +} + +const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check) +{ + return mock_ptr_type(void *); +} + +static bool force_single_boot_device_size_failure = false; + +ssize_t boot_device_read(void *buf, size_t offset, size_t size) +{ + memcpy(buf, (void *)offset, size); + if (force_single_boot_device_size_failure) { + force_single_boot_device_size_failure = false; + return CB_ERR; + } + return size; +} + +/* Utils */ + +static size_t get_cbfs_file_size(const void *file_ptr) +{ + const struct cbfs_file *f = file_ptr; + return be32toh(f->offset) + be32toh(f->len); +} + +static void create_cbfs(const struct cbfs_test_file *files[], const size_t nfiles, + uint8_t *buffer, const size_t buffer_size) +{ + uint8_t *data_ptr = buffer; + size_t file_size = 0; + memset(buffer, 0, buffer_size); + for (size_t i = 0; i < nfiles; ++i) { + if (files[i] == NULL) { + file_size = CBFS_ALIGNMENT; + assert_true(&data_ptr[file_size] < &buffer[buffer_size]); + } else { + file_size = get_cbfs_file_size(files[i]); + assert_true(&data_ptr[file_size] < &buffer[buffer_size]); + memcpy(data_ptr, files[i], file_size); + } + data_ptr = &data_ptr[file_size]; + data_ptr = &buffer[ALIGN_UP((uintptr_t)data_ptr - (uintptr_t)buffer, + CBFS_ALIGNMENT)]; + } +} + +static size_t get_created_cbfs_file_start_offset(const struct cbfs_test_file *files[], + const size_t nfile) +{ + size_t offset_out = 0; + size_t offset = 0; + for (size_t i = 0; i < nfile; ++i) { + offset = files[i] ? get_cbfs_file_size(files[i]) : CBFS_ALIGNMENT; + offset_out = ALIGN_UP(offset_out + offset, CBFS_ALIGNMENT); + } + return offset_out; +} + +/* Setup */ + +static uint8_t + aligned_cbfs_ro_buffer[(sizeof(struct cbfs_test_file) + CBFS_ALIGNMENT * 50)] __aligned( + CBFS_ALIGNMENT); +static const size_t aligned_cbfs_ro_buffer_size = sizeof(aligned_cbfs_ro_buffer); +static uint8_t + aligned_cbfs_rw_buffer[(sizeof(struct cbfs_test_file) + CBFS_ALIGNMENT * 50)] __aligned( + CBFS_ALIGNMENT); +static const size_t aligned_cbfs_rw_buffer_size = sizeof(aligned_cbfs_rw_buffer); + +static uint8_t *unaligned_cbfs_ro_buffer = &aligned_cbfs_ro_buffer[5]; +static const size_t unaligned_cbfs_ro_buffer_size = aligned_cbfs_ro_buffer_size - 5; +static uint8_t *unaligned_cbfs_rw_buffer = &aligned_cbfs_rw_buffer[5]; +static const size_t unaligned_cbfs_rw_buffer_size = aligned_cbfs_rw_buffer_size - 5; + +struct cbfs_test_state { + uint8_t *cbfs_ro_buf; + uint64_t cbfs_ro_size; + uint8_t *cbfs_rw_buf; + uint64_t cbfs_rw_size; + + size_t mcache_ro_offset; + size_t mcache_ro_size; + size_t mcache_rw_offset; + size_t mcache_rw_size; + + struct cbfs_test_setup { + bool unaligned; + bool init_ro; + bool init_rw; + } ex; +}; + + +/* Because of how CMocka works, it should be called in the test function, or in the setup + function only if CBFS API capable of initializing RO CBFS boot device is called. */ +static void setup_cbfs_boot_device(struct cbfs_test_state *s) +{ + set_fmap_locate_area_results(0, 0, CB_SUCCESS); + lib_sysinfo.cbfs_ro_mcache_offset = 0; + lib_sysinfo.cbfs_ro_mcache_size = 0; + memset((void *)cbfs_get_boot_device(true), 0, sizeof(struct cbfs_boot_device)); + if (s->ex.init_ro) { + set_fmap_locate_area_results((size_t)s->cbfs_ro_buf, s->cbfs_ro_size, + CB_SUCCESS); + lib_sysinfo.cbfs_ro_mcache_offset = s->mcache_ro_offset; + lib_sysinfo.cbfs_ro_mcache_size = s->mcache_ro_size; + } + + lib_sysinfo.cbfs_offset = 0; + lib_sysinfo.cbfs_size = 0; + lib_sysinfo.cbfs_rw_mcache_offset = 0; + lib_sysinfo.cbfs_rw_mcache_size = 0; + memset((void *)cbfs_get_boot_device(false), 0, sizeof(struct cbfs_boot_device)); + if (s->ex.init_rw) { + lib_sysinfo.cbfs_offset = (uint64_t)s->cbfs_rw_buf; + lib_sysinfo.cbfs_size = s->cbfs_rw_size; + lib_sysinfo.cbfs_rw_mcache_offset = s->mcache_rw_offset; + lib_sysinfo.cbfs_rw_mcache_size = s->mcache_rw_size; + } +} + +static int setup_cbfs_test(void **state) +{ + struct cbfs_test_state *s = calloc(1, sizeof(*s)); + + if (!s) + return 1; + + if (*state) + memcpy(&s->ex, *state, sizeof(s->ex)); + + if (s->ex.init_ro) { + if (s->ex.unaligned) { + s->cbfs_ro_buf = unaligned_cbfs_ro_buffer; + s->cbfs_ro_size = unaligned_cbfs_ro_buffer_size; + } else { + s->cbfs_ro_buf = aligned_cbfs_ro_buffer; + s->cbfs_ro_size = aligned_cbfs_ro_buffer_size; + } + } + + if (s->ex.init_rw) { + if (s->ex.unaligned) { + s->cbfs_rw_buf = unaligned_cbfs_rw_buffer; + s->cbfs_rw_size = unaligned_cbfs_rw_buffer_size; + } else { + s->cbfs_rw_buf = aligned_cbfs_rw_buffer; + s->cbfs_rw_size = aligned_cbfs_rw_buffer_size; + } + } + + *state = s; + + return 0; +} + +static int teardown_cbfs_test(void **state) +{ + if (*state) + free(*state); + + return 0; +} + +/* Tests */ + +static void test_cbfs_boot_device_init(void **state) +{ + const struct cbfs_boot_device *cbd = NULL; + + /* No valid RO, should fail */ + set_fmap_locate_area_results(0, 0, CB_ERR); + lib_sysinfo.cbfs_offset = 0; + lib_sysinfo.cbfs_size = 0; + lib_sysinfo.cbfs_rw_mcache_size = 0; + lib_sysinfo.cbfs_rw_mcache_offset = 0; + lib_sysinfo.cbfs_ro_mcache_offset = 0; + lib_sysinfo.cbfs_ro_mcache_size = 0; + assert_int_equal(NULL, cbfs_get_boot_device(true)); + assert_null(cbfs_ro_map("file", NULL)); + + /* Valid RO */ + set_fmap_locate_area_results(0x12345678, 0x90ABCDEF, CB_SUCCESS); + lib_sysinfo.cbfs_ro_mcache_offset = 0x600D41C3; + lib_sysinfo.cbfs_ro_mcache_size = 0xBADBEEFF; + cbd = cbfs_get_boot_device(true); + assert_non_null(cbd); + assert_int_equal(0x12345678, cbd->dev.offset); + assert_int_equal(0x90ABCDEF, cbd->dev.size); + assert_int_equal(0xBADBEEFF, cbd->mcache_size); + assert_int_equal(0x600D41C3, cbd->mcache); + + lib_sysinfo.cbfs_offset = 0xAABBCCDD; + lib_sysinfo.cbfs_size = 0x1000; + lib_sysinfo.cbfs_rw_mcache_offset = 0x8F8F8F8F; + lib_sysinfo.cbfs_rw_mcache_size = 0x500; + cbd = cbfs_get_boot_device(false); + assert_non_null(cbd); + assert_int_equal(0xAABBCCDD, cbd->dev.offset); + assert_int_equal(0x1000, cbd->dev.size); + assert_int_equal(0x8F8F8F8F, cbd->mcache); + assert_int_equal(0x500, cbd->mcache_size); +} + +/* This test checks cbfs_map() basic cases and covers only RW CBFS. */ +void test_cbfs_map(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_int_1, &test_file_2, NULL, &test_file_int_3, + &test_file_int_2, NULL, NULL, &test_file_1, + }; + uint8_t *cbfs_buf = NULL; + size_t foffset = 0; + + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_rw_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 0); + expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_1.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_map(TEST_DATA_INT_1_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_1_SIZE, size_out); + assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 1); + expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_2.header.offset)); + will_return(cbfs_find_attr, &test_file_2.attrs_and_data); + expect_value(ulzman, srcn, TEST_DATA_2_SIZE); + expect_value(ulzman, dstn, TEST_DATA_2_SIZE); + mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_2_SIZE, size_out); + assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 3); + expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_3.header.offset)); + will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data); + expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE); + expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE); + mapping = cbfs_map(TEST_DATA_INT_3_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_3_SIZE, size_out); + assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 4); + expect_cbfs_lookup(TEST_DATA_INT_2_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_2.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_map(TEST_DATA_INT_2_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_2_SIZE, size_out); + assert_memory_equal(test_data_int_2, mapping, TEST_DATA_INT_2_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 7); + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_1.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size_out); + assert_memory_equal(test_data_1, mapping, TEST_DATA_1_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0); + if (s->ex.init_rw && CONFIG(LP_ENABLE_CBFS_FALLBACK)) + expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0); + mapping = cbfs_map("invalid_file", &size_out); + assert_null(mapping); +} + +static void test_cbfs_invalid_compression_algo(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + uint8_t *cbfs_buf = NULL; + struct cbfs_test_file *f; + struct cbfs_file_attr_compression *comp; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_2, + }; + + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_rw_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + f = (struct cbfs_test_file *)cbfs_buf; + comp = (struct cbfs_file_attr_compression *)&f->attrs_and_data[0]; + comp->compression = 0xFFFFFFF0; + + size_out = 0; + expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS, (const union cbfs_mdata *)cbfs_buf, + be32toh(test_file_1.header.offset)); + will_return(cbfs_find_attr, comp); + mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out); + assert_null(mapping); +} + +static void test_cbfs_io_error(void **state) +{ + struct cbfs_test_state *s = *state; + setup_cbfs_boot_device(s); + + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_CBFS_IO, 0, 0); + assert_null(cbfs_map(TEST_DATA_1_FILENAME, NULL)); +} + +static void test_cbfs_successful_fallback_to_ro(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_1, &test_file_2, &test_file_int_1, + &test_file_int_1, &test_file_int_2, &test_file_int_3, + }; + uint8_t *cbfs_buf = NULL; + size_t foffset = 0; + + if (!CONFIG(LP_ENABLE_CBFS_FALLBACK)) { + print_message("Skipping test, because LP_ENABLE_CBFS_FALLBACK == 0\n"); + skip(); + } + + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_ro_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_ro_buf, s->cbfs_ro_size); + if (s->ex.init_rw) + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files) - 2, s->cbfs_rw_buf, + s->cbfs_rw_size); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 1); + expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_CBFS_NOT_FOUND, 0, 0); + expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_2.header.offset)); + will_return(cbfs_find_attr, &test_file_2.attrs_and_data); + expect_value(ulzman, srcn, TEST_DATA_2_SIZE); + expect_value(ulzman, dstn, TEST_DATA_2_SIZE); + mapping = cbfs_map(TEST_DATA_2_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_2_SIZE, size_out); + assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE); + cbfs_unmap(mapping); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 5); + expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_CBFS_NOT_FOUND, 0, 0); + expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_3.header.offset)); + will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data); + expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE); + expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE); + mapping = cbfs_map(TEST_DATA_INT_3_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_3_SIZE, size_out); + assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE); + cbfs_unmap(mapping); +} + +static void test_cbfs_load(void **state) +{ + struct cbfs_test_state *s = *state; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_int_1, &test_file_2, NULL, &test_file_int_3, + &test_file_int_2, NULL, NULL, &test_file_1, + }; + uint8_t *cbfs_buf = NULL; + uint8_t load_buf[1 * KiB]; + size_t foffset = 0; + + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_rw_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + /* Successful load */ + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 0); + expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_1.header.offset)); + will_return(cbfs_find_attr, NULL); + size_out = cbfs_load(TEST_DATA_INT_1_FILENAME, load_buf, sizeof(load_buf)); + assert_int_equal(TEST_DATA_INT_1_SIZE, size_out); + assert_memory_equal(test_data_int_1, load_buf, TEST_DATA_INT_1_SIZE); + + /* Buffer too small */ + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 7); + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_1.header.offset)); + will_return(cbfs_find_attr, NULL); + size_out = cbfs_load(TEST_DATA_1_FILENAME, load_buf, TEST_DATA_1_SIZE / 2); + assert_int_equal(0, size_out); +} + +static void test_cbfs_map_with_mcache(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_int_2, &test_file_1, NULL, + &test_file_int_3, &test_file_int_1, &test_file_2, + }; + uint8_t *cbfs_buf = NULL; + size_t foffset = 0; + + /* Will not be accessed, just needs to be valid. */ + s->mcache_ro_offset = ALIGN_UP(0x1000, CBFS_MCACHE_ALIGNMENT); + s->mcache_ro_size = ALIGN_UP(0x500, CBFS_MCACHE_ALIGNMENT); + s->mcache_rw_offset = ALIGN_UP(0x3000, CBFS_MCACHE_ALIGNMENT); + s->mcache_rw_size = ALIGN_UP(0x600, CBFS_MCACHE_ALIGNMENT); + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_rw_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 4); + expect_cbfs_mcache_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_int_1.header.offset)); + will_return(cbfs_find_attr, NULL); + mapping = cbfs_map(TEST_DATA_INT_1_FILENAME, &size_out); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_INT_1_SIZE, size_out); + assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE); + cbfs_unmap(mapping); +} + +static void test_cbfs_boot_device_read_failure(void **state) +{ + struct cbfs_test_state *s = *state; + void *mapping = NULL; + size_t size_out = 0; + const struct cbfs_test_file *cbfs_files[] = { + &test_file_int_3, &test_file_1, NULL, + &test_file_int_3, &test_file_int_1, &test_file_2, + }; + uint8_t *cbfs_buf = NULL; + size_t foffset = 0; + + setup_cbfs_boot_device(s); + cbfs_buf = s->cbfs_rw_buf; + create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size); + + size_out = 0; + foffset = get_created_cbfs_file_start_offset(cbfs_files, 1); + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&cbfs_buf[foffset], + foffset + be32toh(test_file_1.header.offset)); + will_return(cbfs_find_attr, NULL); + force_single_boot_device_size_failure = true; + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size_out); + assert_null(mapping); +} + + +#define TEST_CBFS_NAME_ALIGN_RO_RW(fn, test_name, enable_unaligned, enable_init_ro, \ + enable_init_rw) \ + ((struct CMUnitTest){ \ + .name = (test_name), \ + .test_func = (fn), \ + .setup_func = setup_cbfs_test, \ + .teardown_func = teardown_cbfs_test, \ + .initial_state = \ + &(struct cbfs_test_setup){ \ + .unaligned = enable_unaligned, \ + .init_ro = enable_init_ro, \ + .init_rw = enable_init_rw, \ + }, \ + }) + +#define TEST_CBFS_LOOKUP(fn) \ + EMPTY_WRAP(TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW, aligned", false, false, true), \ + TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW, unaligned", true, false, true)) + +#define TEST_CBFS_RO_FALLBACK(fn) \ + EMPTY_WRAP(TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW+RO, aligned", false, true, true), \ + TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RW+RO, unaligned", true, true, true), \ + TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RO, aligned", false, true, false), \ + TEST_CBFS_NAME_ALIGN_RO_RW(fn, #fn ", RO, unaligned", true, true, false)) + + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_cbfs_boot_device_init), + TEST_CBFS_LOOKUP(test_cbfs_map), + TEST_CBFS_LOOKUP(test_cbfs_invalid_compression_algo), + TEST_CBFS_LOOKUP(test_cbfs_io_error), + TEST_CBFS_RO_FALLBACK(test_cbfs_successful_fallback_to_ro), + TEST_CBFS_LOOKUP(test_cbfs_load), + TEST_CBFS_LOOKUP(test_cbfs_map_with_mcache), + TEST_CBFS_LOOKUP(test_cbfs_boot_device_read_failure), + }; + + return lp_run_group_tests(tests, NULL, NULL); +} diff --git a/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c b/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c new file mode 100644 index 0000000000..2ab3d5302d --- /dev/null +++ b/payloads/libpayload/tests/libcbfs/cbfs-verification-test.c @@ -0,0 +1,247 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <cbfs.h> +#include <cbfs_glue.h> +#include <string.h> +#include <mocks/cbfs_util.h> +#include <tests/test.h> + +#include "../libcbfs/cbfs.c" + +/* Mocks */ + +unsigned long virtual_offset = 0; +struct sysinfo_t lib_sysinfo; + +size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg) +{ + if (hash_alg != VB2_HASH_SHA256) { + fail_msg("Unsupported hash algorithm: %d\n", hash_alg); + return 0; + } + + return VB2_SHA256_DIGEST_SIZE; +} + +vb2_error_t vb2_hash_verify(const void *buf, uint32_t size, const struct vb2_hash *hash) +{ + check_expected_ptr(buf); + check_expected(size); + + assert_int_equal(hash->algo, VB2_HASH_SHA256); + + if (!memcmp(hash->sha256, good_hash, sizeof(good_hash))) + return VB2_SUCCESS; + + if (!memcmp(hash->sha256, bad_hash, sizeof(bad_hash))) + return VB2_ERROR_SHA_MISMATCH; + + fail_msg("%s called with bad hash", __func__); + return VB2_ERROR_SHA_MISMATCH; +} + +unsigned long ulzman(const unsigned char *src, unsigned long srcn, unsigned char *dst, + unsigned long dstn) +{ + fail_msg("Unexpected call to %s", __func__); + return 0; +} + +size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) +{ + fail_msg("Unexpected call to %s", __func__); + return 0; +} + +cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *name, + union cbfs_mdata *mdata_out, size_t *data_offset_out) +{ + return CB_CBFS_CACHE_FULL; +} + +cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_out, + size_t *data_offset_out, struct vb2_hash *metadata_hash) +{ + assert_non_null(dev); + check_expected(name); + + cb_err_t ret = mock_type(cb_err_t); + if (ret != CB_SUCCESS) + return ret; + + memcpy(mdata_out, mock_ptr_type(const union cbfs_mdata *), sizeof(union cbfs_mdata)); + *data_offset_out = mock_type(size_t); + return CB_SUCCESS; +} + +static void expect_cbfs_lookup(const char *name, cb_err_t err, const union cbfs_mdata *mdata, + size_t data_offset_out) +{ + expect_string(cbfs_lookup, name, name); + will_return(cbfs_lookup, err); + + if (err == CB_SUCCESS) { + will_return(cbfs_lookup, mdata); + will_return(cbfs_lookup, data_offset_out); + } +} + +const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check) +{ + return mock_ptr_type(void *); +} + +cb_err_t fmap_locate_area(const char *name, size_t *offset, size_t *size) +{ + *offset = 0; + *size = 0; + return CB_SUCCESS; +} + +ssize_t boot_device_read(void *buf, size_t offset, size_t size) +{ + /* Offset should be based on an address from lib_sysinfo.cbfs_offset */ + memcpy(buf, (void *)offset, size); + + return size; +} + +const struct vb2_hash *cbfs_file_hash(const union cbfs_mdata *mdata) +{ + return mock_ptr_type(const struct vb2_hash *); +} + +/* Utils */ + +static void clear_cbfs_boot_devices(void) +{ + lib_sysinfo.cbfs_ro_mcache_offset = 0; + lib_sysinfo.cbfs_ro_mcache_size = 0; + lib_sysinfo.cbfs_offset = 0; + lib_sysinfo.cbfs_size = 0; + lib_sysinfo.cbfs_rw_mcache_offset = 0; + lib_sysinfo.cbfs_rw_mcache_size = 0; + memset((void *)cbfs_get_boot_device(true), 0, sizeof(struct cbfs_boot_device)); + memset((void *)cbfs_get_boot_device(false), 0, sizeof(struct cbfs_boot_device)); +} + +void set_cbfs(uint64_t offset, size_t size) +{ + clear_cbfs_boot_devices(); + lib_sysinfo.cbfs_offset = offset; + lib_sysinfo.cbfs_size = size; +} + +/* Tests */ + +static int setup_test_cbfs(void **state) +{ + clear_cbfs_boot_devices(); + return 0; +} + +static void test_cbfs_map_no_hash(void **state) +{ + void *mapping = NULL; + size_t size = 0; + + set_cbfs((uint64_t)&file_no_hash, sizeof(file_no_hash)); + + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&file_no_hash, + be32toh(file_no_hash.header.offset)); + will_return(cbfs_find_attr, NULL); + + if (CONFIG(LP_CBFS_VERIFICATION)) { + /* File with no hash. No hash causes hash mismatch by default, + so mapping will not be completed successfully. */ + will_return(cbfs_file_hash, NULL); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_null(mapping); + } else { + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size); + assert_memory_equal(test_data_1, mapping, size); + cbfs_unmap(mapping); + } +} + +static void test_cbfs_map_valid_hash(void **state) +{ + void *mapping = NULL; + size_t size = 0; + struct vb2_hash hash = { + .algo = VB2_HASH_SHA256, + }; + memcpy(&hash.sha256, good_hash, VB2_SHA256_DIGEST_SIZE); + + set_cbfs((uint64_t)&file_valid_hash, sizeof(file_valid_hash)); + + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&file_valid_hash, + be32toh(file_valid_hash.header.offset)); + will_return(cbfs_find_attr, NULL); + + + if (CONFIG(LP_CBFS_VERIFICATION)) { + will_return(cbfs_file_hash, &hash); + expect_memory(vb2_hash_verify, buf, + &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE], HASH_ATTR_SIZE); + expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE); + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size); + assert_memory_equal(mapping, &file_valid_hash.attrs_and_data[HASH_ATTR_SIZE], + size); + } else { + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size); + assert_memory_equal(test_data_1, mapping, size); + cbfs_unmap(mapping); + } +} + +static void test_cbfs_map_invalid_hash(void **state) +{ + void *mapping = NULL; + size_t size = 0; + struct vb2_hash hash = { + .algo = VB2_HASH_SHA256, + }; + memcpy(&hash.sha256, bad_hash, VB2_SHA256_DIGEST_SIZE); + + set_cbfs((uint64_t)&file_broken_hash, sizeof(file_broken_hash)); + + expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS, + (const union cbfs_mdata *)&file_broken_hash, + be32toh(file_broken_hash.header.offset)); + will_return(cbfs_find_attr, NULL); + + if (CONFIG(LP_CBFS_VERIFICATION)) { + will_return(cbfs_file_hash, &hash); + expect_memory(vb2_hash_verify, buf, + &file_broken_hash.attrs_and_data[HASH_ATTR_SIZE], HASH_ATTR_SIZE); + expect_value(vb2_hash_verify, size, TEST_DATA_1_SIZE); + mapping = cbfs_map(TEST_DATA_1_FILENAME, NULL); + assert_null(mapping); + } else { + mapping = cbfs_map(TEST_DATA_1_FILENAME, &size); + assert_non_null(mapping); + assert_int_equal(TEST_DATA_1_SIZE, size); + assert_memory_equal(test_data_1, mapping, size); + cbfs_unmap(mapping); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup(test_cbfs_map_no_hash, setup_test_cbfs), + cmocka_unit_test_setup(test_cbfs_map_valid_hash, setup_test_cbfs), + cmocka_unit_test_setup(test_cbfs_map_invalid_hash, setup_test_cbfs), + }; + + return lp_run_group_tests(tests, NULL, NULL); +} diff --git a/payloads/libpayload/tests/mocks/cbfs_file_mock.c b/payloads/libpayload/tests/mocks/cbfs_file_mock.c new file mode 100644 index 0000000000..3ad078c7b0 --- /dev/null +++ b/payloads/libpayload/tests/mocks/cbfs_file_mock.c @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <mocks/cbfs_util.h> + + +const u8 test_data_1[TEST_DATA_1_SIZE] = { TEST_DATA_1 }; +const u8 test_data_2[TEST_DATA_2_SIZE] = { TEST_DATA_2 }; +const u8 test_data_int_1[TEST_DATA_INT_1_SIZE] = { LE64(TEST_DATA_INT_1) }; +const u8 test_data_int_2[TEST_DATA_INT_2_SIZE] = { LE64(TEST_DATA_INT_2) }; +const u8 test_data_int_3[TEST_DATA_INT_3_SIZE] = { LE64(TEST_DATA_INT_3) }; + +const u8 good_hash[VB2_SHA256_DIGEST_SIZE] = { TEST_SHA256 }; +const u8 bad_hash[VB2_SHA256_DIGEST_SIZE] = { INVALID_SHA256 }; + +const struct cbfs_test_file file_no_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file file_valid_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_HASH), + BE32(HASH_ATTR_SIZE), + BE32(VB2_HASH_SHA256), + TEST_SHA256, + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file file_broken_hash = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, HASH_ATTR_SIZE, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_HASH), + BE32(HASH_ATTR_SIZE), + BE32(VB2_HASH_SHA256), + INVALID_SHA256, + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file test_file_1 = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_1_SIZE), + .filename = TEST_DATA_1_FILENAME, + .attrs_and_data = { + TEST_DATA_1, + }, +}; + +const struct cbfs_test_file test_file_2 = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, sizeof(struct cbfs_file_attr_compression), + TEST_DATA_2_SIZE), + .filename = TEST_DATA_2_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_COMPRESSION), + BE32(sizeof(struct cbfs_file_attr_compression)), + BE32(CBFS_COMPRESS_LZMA), + BE32(TEST_DATA_2_SIZE), + TEST_DATA_2, + }, +}; + +const struct cbfs_test_file test_file_int_1 = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_INT_1_SIZE), + .filename = TEST_DATA_INT_1_FILENAME, + .attrs_and_data = { + LE64(TEST_DATA_INT_1), + }, +}; + +const struct cbfs_test_file test_file_int_2 = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, 0, TEST_DATA_INT_2_SIZE), + .filename = TEST_DATA_INT_2_FILENAME, + .attrs_and_data = { + LE64(TEST_DATA_INT_2), + }, +}; + +const struct cbfs_test_file test_file_int_3 = { + .header = HEADER_INITIALIZER(CBFS_TYPE_RAW, sizeof(struct cbfs_file_attr_compression), + TEST_DATA_INT_3_SIZE), + .filename = TEST_DATA_INT_3_FILENAME, + .attrs_and_data = { + BE32(CBFS_FILE_ATTR_TAG_COMPRESSION), + BE32(sizeof(struct cbfs_file_attr_compression)), + BE32(CBFS_COMPRESS_LZ4), + BE32(TEST_DATA_INT_3_SIZE), + LE64(TEST_DATA_INT_3), + }, +}; diff --git a/payloads/libpayload/tests/mocks/die.c b/payloads/libpayload/tests/mocks/die.c new file mode 100644 index 0000000000..a67105a12b --- /dev/null +++ b/payloads/libpayload/tests/mocks/die.c @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <tests/test.h> +#include <stdbool.h> + +void die_work(const char *file, const char *func, int line, const char *fmt, ...) +{ + /* Failing asserts are jumping to the user code (test) if expect_assert_failed() was + previously called. Otherwise it jumps to the cmocka code and fails the test. */ + mock_assert(false, "Mock assetion called", file, line); + + /* Should never be reached */ + print_error("%s() called...\n", __func__); + while (1) + ; +} |