Jakub Czapiga has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/58327 )
Change subject: libpayload/tests: Add libcbfs/cbfs_core-test test case ......................................................................
libpayload/tests: Add libcbfs/cbfs_core-test test case
Change-Id: Ib246a891242f5a514f2f142699800eeb407de4f9 Signed-off-by: Jakub Czapiga jacz@semihalf.com --- A payloads/libpayload/tests/include/tests/libcbfs/cbfs_util.h A payloads/libpayload/tests/libcbfs/Makefile.inc A payloads/libpayload/tests/libcbfs/cbfs_core-test.c A payloads/libpayload/tests/mocks/cbfs_file_mock.c 4 files changed, 472 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/27/58327/1
diff --git a/payloads/libpayload/tests/include/tests/libcbfs/cbfs_util.h b/payloads/libpayload/tests/include/tests/libcbfs/cbfs_util.h new file mode 100644 index 0000000..bbf27f9 --- /dev/null +++ b/payloads/libpayload/tests/include/tests/libcbfs/cbfs_util.h @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef TESTS_LIB_CBFS_UTIL_H +#define TESTS_LIB_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)) + +#if CONFIG(LP_LITTLE_ENDIAN) + +#define BYTES32(b32) LE32(b32) +#define BYTES64(b64) LE64(b64) + +#elif CONFIG(LP_BIG_ENDIAN) + +#define BYTES32(b32) BE32(b32) +#define BYTES64(b64) BE64(b64) + +#endif + +#define FILENAME_SIZE 16 + +struct cbfs_test_file { + struct cbfs_file header; + u8 filename[FILENAME_SIZE]; + u8 attrs_and_data[200]; +}; + +#define TEST_CBFS_CACHE_SIZE (2 * MiB) +#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 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 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 /* TESTS_LIB_CBFS_UTIL_H */ diff --git a/payloads/libpayload/tests/libcbfs/Makefile.inc b/payloads/libpayload/tests/libcbfs/Makefile.inc new file mode 100644 index 0000000..ccf8c91 --- /dev/null +++ b/payloads/libpayload/tests/libcbfs/Makefile.inc @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0-only + +tests-y += cbfs_core-test +tests-y += cbfs_core-x86-test + +cbfs_core-test-srcs += tests/libcbfs/cbfs_core-test.c +cbfs_core-test-srcs += libcbfs/cbfs.c +cbfs_core-test-srcs += tests/mocks/cbfs_file_mock.c +cbfs_core-test-cflags += -ggdb -Og + +$(call copy-test,cbfs_core-test,cbfs_core-x86-test) +cbfs_core-x86-test-config += CONFIG_LP_ARCH_X86=1 + diff --git a/payloads/libpayload/tests/libcbfs/cbfs_core-test.c b/payloads/libpayload/tests/libcbfs/cbfs_core-test.c new file mode 100644 index 0000000..5908e5b --- /dev/null +++ b/payloads/libpayload/tests/libcbfs/cbfs_core-test.c @@ -0,0 +1,282 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <libpayload.h> +#include <cbfs.h> +#include <tests/libcbfs/cbfs_util.h> + +/* MOCKS */ + +int mock_cbfs_media_open(struct cbfs_media *media) +{ + return mock_type(int); +} + +void setup_mock_cbfs_media_open(int retval) +{ + will_return(mock_cbfs_media_open, retval); +} + +size_t mock_cbfs_media_read(struct cbfs_media *media, void *dest, size_t offset, size_t count) +{ + u8 *buf = mock_ptr_type(u8 *); + if (buf) + memcpy(dest, buf, count); + return mock_type(int); +} + +void setup_mock_cbfs_media_read(void *buf, int retval) +{ + will_return(mock_cbfs_media_read, buf); + will_return(mock_cbfs_media_read, retval); +} + +void *mock_cbfs_media_map(struct cbfs_media *media, size_t offset, size_t count) +{ + return mock_ptr_type(void *); +} + +void setup_mock_cbfs_media_map(void *retmap) +{ + will_return(mock_cbfs_media_map, retmap); +} + +void *mock_cbfs_media_unmap(struct cbfs_media *media, const void *mapping) +{ + return NULL; +} + +int mock_cbfs_media_close(struct cbfs_media *media) +{ + return mock_type(int); +} + +void setup_mock_cbfs_media_close(int retval) +{ + will_return(mock_cbfs_media_close, retval); +} + +struct cbfs_media mock_media = { + .context = NULL, + .open = mock_cbfs_media_open, + .read = mock_cbfs_media_read, + .map = mock_cbfs_media_map, + .unmap = mock_cbfs_media_unmap, + .close = mock_cbfs_media_close, +}; + +#define MOCK_MEDIA_INIT ((struct cbfs_media) { \ + .context = mock_media.context, \ + .open = mock_media.open, \ + .read = mock_media.read, \ + .map = mock_media.map, \ + .unmap = mock_media.unmap, \ + .close = mock_media.close, \ +}) + +int init_default_cbfs_media(struct cbfs_media *media) +{ + struct cbfs_media *mocked_media = mock_ptr_type(struct cbfs_media *); + + if (mocked_media) + memcpy(media, mocked_media, sizeof(*media)); + + return mock_type(int); +} + +static void setup_mock_init_default_cbfs_media(struct cbfs_media *media, int retval) +{ + will_return(init_default_cbfs_media, media); + will_return(init_default_cbfs_media, retval); +} + +/* TESTS */ + +static void test_cbfs_get_header_default_media_init_failure(void **state) +{ + setup_mock_init_default_cbfs_media(NULL, -1); + assert_ptr_equal(CBFS_HEADER_INVALID_ADDRESS, cbfs_get_header(CBFS_DEFAULT_MEDIA)); +} + +static void test_cbfs_get_header_read_failure(void **state) +{ + struct cbfs_media *test_media = *state; + struct cbfs_media local_media = MOCK_MEDIA_INIT; + + if (test_media == CBFS_DEFAULT_MEDIA) + setup_mock_init_default_cbfs_media(&local_media, 0); + else + test_media = &local_media; + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(NULL, 0); + assert_ptr_equal(CBFS_HEADER_INVALID_ADDRESS, cbfs_get_header(test_media)); +} + +static void test_cbfs_get_header_invalid_map_address(void **state) +{ + struct cbfs_media *test_media = *state; + struct cbfs_media local_media = MOCK_MEDIA_INIT; + u8 master_header_offset[] = { BYTES32(0) }; + + if (test_media == CBFS_DEFAULT_MEDIA) + setup_mock_init_default_cbfs_media(&local_media, 0); + else + test_media = &local_media; + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(master_header_offset, ARRAY_SIZE(master_header_offset)); + setup_mock_cbfs_media_map(CBFS_MEDIA_INVALID_MAP_ADDRESS); + setup_mock_cbfs_media_close(0); + + assert_ptr_equal(CBFS_HEADER_INVALID_ADDRESS, cbfs_get_header(test_media)); +} + +static void test_cbfs_get_header_invalid_magic(void **state) +{ + struct cbfs_media *test_media = *state; + struct cbfs_media local_media = MOCK_MEDIA_INIT; + struct cbfs_header master_header = { .magic = htobe32(0xBADBA61C) }; + u8 master_header_offset[] = { BYTES32((intptr_t)&master_header & 0xFFFFFFFF) }; + + if (test_media == CBFS_DEFAULT_MEDIA) + setup_mock_init_default_cbfs_media(&local_media, 0); + else + test_media = &local_media; + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(master_header_offset, ARRAY_SIZE(master_header_offset)); + setup_mock_cbfs_media_map(&master_header); + setup_mock_cbfs_media_close(0); + + assert_ptr_equal(CBFS_HEADER_INVALID_ADDRESS, cbfs_get_header(test_media)); +} + +static void test_cbfs_get_header_all_ok(void **state) +{ + struct cbfs_media *test_media = *state; + struct cbfs_media local_media = MOCK_MEDIA_INIT; + struct cbfs_header master_header = { .magic = htobe32(CBFS_HEADER_MAGIC) }; + u8 master_header_offset[] = { BYTES32(0) }; /* mock does not care */ + + if (test_media == CBFS_DEFAULT_MEDIA) + setup_mock_init_default_cbfs_media(&local_media, 0); + else + test_media = &local_media; + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(master_header_offset, ARRAY_SIZE(master_header_offset)); + setup_mock_cbfs_media_map(&master_header); + setup_mock_cbfs_media_close(0); + + assert_ptr_equal(&master_header, cbfs_get_header(test_media)); +} + +struct sysinfo_t lib_sysinfo; + +/* If cbfs_get_header() fails (returns CBFS_HEADER_INVALID_ADDRESS), then cbfs_get_range(), + and thus cbfs_get_handle() should fail() */ +static void test_cbfs_get_handle_get_range_failure_get_handler_failure(void **state) +{ + struct cbfs_media *test_media = *state; + struct cbfs_media local_media = MOCK_MEDIA_INIT; + + if (test_media == CBFS_DEFAULT_MEDIA) + setup_mock_init_default_cbfs_media(&local_media, 0); + else + test_media = &local_media; + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(NULL, 0); + assert_null(cbfs_get_handle(test_media, "should-be-ignored")); +} + +static void test_cbfs_get_handle_init_default_cbfs_media_failure(void **state) +{ + struct cbfs_header master_header = { .magic = htobe32(CBFS_HEADER_MAGIC) }; + + /* cbfs_get_range() should succeed */ + lib_sysinfo.cbfs_offset = (uintptr_t)&master_header; + lib_sysinfo.cbfs_size = 4 * MiB; + setup_mock_init_default_cbfs_media(NULL, -1); + + assert_null(cbfs_get_handle(CBFS_DEFAULT_MEDIA, "this-call-should-fail")); +} + +static void test_cbfs_get_handle_cbfs_default_media_success_no_files(void **state) +{ + struct cbfs_media local_media = MOCK_MEDIA_INIT; + + lib_sysinfo.cbfs_offset = 64; + lib_sysinfo.cbfs_size = 1 * KiB - 64; /* Does not matter. Has to be positive por this test */ + + /* cbfs_get_range() should use lib_sysinfo.cbfs_* and return to call + init_default_cbfs_media() */ + setup_mock_init_default_cbfs_media(&local_media, 0); + + /* cbfs_get_handle() should open default media, see that it fails to read cbfs_file + (buffer too small), and return with error (NULL) after closing media. */ + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(NULL, 0); + setup_mock_cbfs_media_close(0); + + assert_null(cbfs_get_handle(CBFS_DEFAULT_MEDIA, "does_not_care")); +} + +static void test_cbfs_get_handle_cbfs_custom_media_success_no_files(void **state) +{ + struct cbfs_media local_media = MOCK_MEDIA_INIT; + struct cbfs_header master_header = { + .magic = htobe32(CBFS_HEADER_MAGIC), + .offset = htonl(64), + .romsize = htonl(1 * KiB - 64), + .bootblocksize = ntohl(128), /* Only on LP_ARCH_X86 */ + }; + u8 master_header_offset[] = { BYTES32(0) }; /* mock does not care */ + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(master_header_offset, ARRAY_SIZE(master_header_offset)); + setup_mock_cbfs_media_map(&master_header); + setup_mock_cbfs_media_close(0); + + setup_mock_cbfs_media_open(0); + setup_mock_cbfs_media_read(NULL, 0); + setup_mock_cbfs_media_close(0); + + assert_null(cbfs_get_handle(&local_media, "does_not_care")); + +} + +#define CBFS_GET_HEADER_OR_HANDLE_NAME_MEDIA_TEST(fn, test_name, media) ((struct CMUnitTest) { \ + .name = (test_name), \ + .test_func = (fn), \ + .setup_func = NULL, \ + .teardown_func = NULL, \ + .initial_state = (media), \ + }) + +#define CBFS_GET_HEADER_OR_HANDLE_TEST(fn) EMPTY_WRAP( \ + CBFS_GET_HEADER_OR_HANDLE_NAME_MEDIA_TEST(fn, #fn ", CBFS_DEFAULT_MEDIA", \ + CBFS_DEFAULT_MEDIA), \ + CBFS_GET_HEADER_OR_HANDLE_NAME_MEDIA_TEST(fn, #fn ", custom media", NULL) \ +) + +int main(void) +{ + const struct CMUnitTest tests[] = { + /* get_cbfs_header() */ + cmocka_unit_test(test_cbfs_get_header_default_media_init_failure), + CBFS_GET_HEADER_OR_HANDLE_TEST(test_cbfs_get_header_read_failure), + CBFS_GET_HEADER_OR_HANDLE_TEST(test_cbfs_get_header_invalid_map_address), + CBFS_GET_HEADER_OR_HANDLE_TEST(test_cbfs_get_header_invalid_magic), + CBFS_GET_HEADER_OR_HANDLE_TEST(test_cbfs_get_header_all_ok), + + /* cbfs_get_handle() */ + CBFS_GET_HEADER_OR_HANDLE_TEST( + test_cbfs_get_handle_get_range_failure_get_handler_failure), + cmocka_unit_test(test_cbfs_get_handle_init_default_cbfs_media_failure), + cmocka_unit_test(test_cbfs_get_handle_cbfs_default_media_success_no_files), + cmocka_unit_test(test_cbfs_get_handle_cbfs_custom_media_success_no_files), + }; + + 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 0000000..47a3055 --- /dev/null +++ b/payloads/libpayload/tests/mocks/cbfs_file_mock.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <tests/libcbfs/cbfs_util.h> + +TEST_REGION(cbfs_cache, TEST_CBFS_CACHE_SIZE); + +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 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), + }, +};