Name of user not set #1003143 has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/46217 )
Change subject: tests: Add lib/imd_cbmem-test test case ......................................................................
tests: Add lib/imd_cbmem-test test case
Signed-off-by: Jakub Czapiga jacz@semihalf.com Change-Id: I5d84c0b589a9360af93d6215ad35abe6d8ee1559 --- M tests/lib/Makefile.inc A tests/lib/imd_cbmem-test.c 2 files changed, 309 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/17/46217/1
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 3062bca..2f381fe 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -4,6 +4,7 @@ tests-y += b64_decode-test tests-y += hexstrtobin-test tests-y += imd-test +tests-y += imd_cbmem-test
string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -17,4 +18,11 @@
imd-test-srcs += tests/lib/imd-test.c imd-test-srcs += tests/stubs/console.c -imd-test-srcs += src/lib/imd.c \ No newline at end of file +imd-test-srcs += src/lib/imd.c + +imd_cbmem-test-srcs += tests/lib/imd_cbmem-test.c +# imd_cbmem-test-srcs += src/lib/imd_cbmem.c +imd_cbmem-test-srcs += tests/stubs/console.c +imd_cbmem-test-srcs += src/lib/imd.c +imd_cbmem-test-srcs += src/lib/bootmem.c +imd_cbmem-test-srcs += src/lib/memrange.c diff --git a/tests/lib/imd_cbmem-test.c b/tests/lib/imd_cbmem-test.c new file mode 100644 index 0000000..2ad0941 --- /dev/null +++ b/tests/lib/imd_cbmem-test.c @@ -0,0 +1,300 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* Include UUT source code directly instead of linking it. This will allow + * access to internal structures and data without having to extract them to + * another header file + */ +#include "../lib/imd_cbmem.c" + +#include <imd.h> +#include <cbmem.h> +#include <stdlib.h> +#include <tests/test.h> +#include <stdio.h> +#include <commonlib/bsd/helpers.h> +#include <imd_private.h> + +#define LIMIT_SZ (LIMIT_ALIGN + 1) + +#define CBMEM_ENTRY_ID 0xA001 +#define CBMEM_SM_ENTRY_ID 0xB001 + +#define CBMEM_SIZE (4 * MiB) + +void reset_imd(void) +{ + imd.lg.limit = (uintptr_t) NULL; + imd.lg.r = NULL; + imd.sm.limit = (uintptr_t) NULL; + imd.sm.r = NULL; +} + +void cbmem_run_init_hooks(int is_recovery) +{ + (void) is_recovery; +} + +static void test_cbmem_top(void **state) +{ + cbmem_top_init_once(); + + if (ENV_ROMSTAGE) + assert_ptr_equal(cbmem_top_chipset(), cbmem_top()); + + if (ENV_POSTCAR || ENV_RAMSTAGE) + assert_ptr_equal((void *)_cbmem_top_ptr, cbmem_top()); +} + +static void test_cbmem_initialize_empty(void **state) +{ + const struct cbmem_entry *found; + + cbmem_initialize_empty(); + + found = cbmem_entry_find(SMALL_REGION_ID); + assert_non_null(found); +} + +static void test_cbmem_initialize_empty_id_size(void **state) +{ + const struct cbmem_entry *found_1, *found_2; + + cbmem_initialize_empty_id_size(CBMEM_ENTRY_ID, CBMEM_ROOT_SIZE); + + found_1 = cbmem_entry_find(SMALL_REGION_ID); + assert_non_null(found_1); + + found_2 = cbmem_entry_find(CBMEM_ENTRY_ID); + assert_non_null(found_2); +} + +static void test_cbmem_initialize(void **state) +{ + int res; + const struct cbmem_entry *found; + + res = cbmem_initialize(); + assert_int_equal(0, res); + + cbmem_initialize_empty(); + + res = cbmem_initialize(); + assert_int_equal(0, res); + + found = cbmem_entry_find(SMALL_REGION_ID); + assert_non_null(found); +} + +static void test_cbmem_initialize_id_size(void **state) +{ + int res; + const struct cbmem_entry *found_1, *found_2; + + res = cbmem_initialize_id_size(0, 0); + assert_int_equal(0, res); + + cbmem_initialize_empty(); + + res = cbmem_initialize_id_size(CBMEM_ENTRY_ID, CBMEM_ROOT_SIZE); + assert_int_equal(0, res); + + found_1 = cbmem_entry_find(SMALL_REGION_ID); + assert_non_null(found_1); + + found_2 = cbmem_entry_find(CBMEM_ENTRY_ID); + assert_non_null(found_2); +} + +static void test_cbmem_recovery(void **state) +{ + int is_wakeup = 1; + assert_int_equal(0, cbmem_recovery(is_wakeup)); + + is_wakeup = 0; + assert_int_equal(0, cbmem_recovery(is_wakeup)); +} + +static void test_cbmem_entry_add(void **state) +{ + int id1 = 0x1; + int id2 = 0x2; + + assert_null(cbmem_entry_find(id1)); + assert_null(cbmem_entry_find(id2)); + + cbmem_initialize_empty_id_size(id1, CBMEM_ROOT_SIZE); + cbmem_entry_add(id2, CBMEM_ROOT_SIZE); + + assert_non_null(cbmem_entry_find(id1)); + assert_non_null(cbmem_entry_find(id2)); +} + +static void test_cbmem_add(void **state) +{ + int id1 = 0x3; + int id2 = 0x4; + + assert_null(cbmem_find(id1)); + assert_null(cbmem_find(id2)); + + cbmem_initialize_empty_id_size(id1, CBMEM_ROOT_SIZE); + cbmem_add(id2, CBMEM_ROOT_SIZE); + + assert_non_null(cbmem_find(id1)); + assert_non_null(cbmem_find(id2)); +} + +static void test_cbmem_entry_find(void **state) +{ + int id1 = 0x6; + int id2 = 0x7; + const struct cbmem_entry *cbm_e1, *cbm_e2, *found_1, *found_2; + + assert_null(cbmem_entry_find(id1)); + + cbmem_initialize_empty(); + cbm_e1 = cbmem_entry_add(id1, CBMEM_ROOT_SIZE); + cbm_e2 = cbmem_entry_add(id2, CBMEM_ROOT_SIZE); + + found_1 = cbmem_entry_find(id1); + assert_non_null(found_1); + assert_ptr_equal(cbm_e1, found_1); + + found_2 = cbmem_entry_find(id2); + assert_non_null(found_2); + assert_ptr_equal(cbm_e2, found_2); +} + +static void test_cbmem_find(void **state) +{ + int id1 = 0x8; + int id2 = 0x9; + void *cbm_e1, *cbm_e2, *found_1, *found_2; + + assert_null(cbmem_find(id1)); + + cbmem_initialize_empty(); + cbm_e1 = cbmem_add(id1, CBMEM_ROOT_SIZE); + cbm_e2 = cbmem_add(id2, CBMEM_ROOT_SIZE); + + found_1 = cbmem_find(id1); + assert_non_null(found_1); + assert_ptr_equal(cbm_e1, found_1); + + found_2 = cbmem_find(id2); + assert_non_null(found_2); + assert_ptr_equal(cbm_e2, found_2); +} + +static void test_cbmem_entry_remove(void **state) +{ + int id1 = 0x10; + int id2 = 0x11; + const struct cbmem_entry *cbm_e1, *cbm_e2; + + assert_int_equal(-1, cbmem_entry_remove(NULL)); + + cbmem_initialize_empty(); + cbm_e1 = cbmem_entry_add(id1, CBMEM_ROOT_SIZE); + cbm_e2 = cbmem_entry_add(id2, CBMEM_ROOT_SIZE); + + assert_int_equal(-1, cbmem_entry_remove(cbm_e1)); + assert_int_equal(0, cbmem_entry_remove(cbm_e2)); + assert_int_equal(0, cbmem_entry_remove(cbm_e1)); +} + +static void test_cbmem_entry_size(void **state) +{ + struct imd_entry i_e = { .size = CBMEM_ROOT_SIZE }; + const struct cbmem_entry *cbm_e = imd_to_cbmem(&i_e); + + assert_int_equal(CBMEM_ROOT_SIZE, cbmem_entry_size(cbm_e)); + + i_e.size = 0; + assert_int_equal(0, cbmem_entry_size(cbm_e)); +} + +static void test_cbmem_entry_start(void **state) +{ + const struct cbmem_entry *cbm_e = NULL; + + assert_null(cbmem_entry_start(cbm_e)); + + cbmem_initialize_empty_id_size(CBMEM_ENTRY_ID, CBMEM_ROOT_SIZE); + cbm_e = cbmem_entry_find(CBMEM_ENTRY_ID); + assert_non_null(cbmem_entry_start(cbm_e)); +} + +static void test_cbmem_add_bootmem(void **state) +{ + void *baseptr = NULL; + size_t size = 0; + + cbmem_get_region(&baseptr, &size); + assert_null(baseptr); + assert_int_equal(0, size); + + cbmem_initialize_empty_id_size(CBMEM_ENTRY_ID, CBMEM_ROOT_SIZE); + + cbmem_add_bootmem(); + + assert_non_null(&baseptr); + assert_int_not_equal(0, &size); +} + +static void test_cbmem_get_region(void **state) +{ + void *baseptr = NULL; + size_t size = 0; + + cbmem_get_region(&baseptr, &size); + assert_null(baseptr); + assert_int_equal(0, size); + + cbmem_initialize_empty_id_size(CBMEM_ENTRY_ID, CBMEM_ROOT_SIZE); + + cbmem_get_region(&baseptr, &size); + assert_non_null(&baseptr); + assert_int_not_equal(0, size); +} + +static int teardown_test_imd_cbmem(void **state) +{ + reset_imd(); + return 0; +} + +static int setup_group_imd_cbmem(void **status) +{ + _cbmem_top_ptr = (uintptr_t)malloc(CBMEM_SIZE) + CBMEM_SIZE; + return 0; +} + +static int teardown_group_imd_cbmem(void **status) +{ + free((void *)_cbmem_top_ptr - CBMEM_SIZE); + return 0; +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_cbmem_top), + cmocka_unit_test_teardown(test_cbmem_initialize_empty, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_initialize_empty_id_size, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_initialize, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_initialize_id_size, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_recovery, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_entry_add, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_add, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_entry_find, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_find, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_entry_remove, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_entry_size, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_entry_start, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_add_bootmem, teardown_test_imd_cbmem), + cmocka_unit_test_teardown(test_cbmem_get_region, teardown_test_imd_cbmem), + }; + + return cmocka_run_group_tests(tests, setup_group_imd_cbmem, teardown_group_imd_cbmem); +}