Name of user not set #1003143 has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/47642 )
Change subject: tests: Add lib/cbmem_console-test test case ......................................................................
tests: Add lib/cbmem_console-test test case
Add test case executed twice, once for ROMSTAGE and once RAMSTAGE. Each test is named and visible in cmocka output with stage in its name.
Signed-off-by: Jakub Czapiga jacz@semihalf.com Change-Id: I464eee61f538188427bec730d2e004c7b76cca67 --- M tests/lib/Makefile.inc A tests/lib/cbmem_console-test.c 2 files changed, 114 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/42/47642/1
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 219f7e7..c498289 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -6,6 +6,8 @@ tests-y += imd-test tests-y += timestamp-test tests-y += edid-test +tests-y += cbmem_console-romstage-test +tests-y += cbmem_console-ramstage-test
string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -30,3 +32,10 @@ edid-test-srcs += src/lib/edid.c edid-test-srcs += tests/stubs/console.c
+cbmem_console-romstage-test-stage := romstage +cbmem_console-romstage-test-srcs += tests/lib/cbmem_console-test.c +cbmem_console-romstage-test-srcs += tests/stubs/console.c + +cbmem_console-ramstage-test-stage := ramstage +cbmem_console-ramstage-test-srcs += tests/lib/cbmem_console-test.c +cbmem_console-ramstage-test-srcs += tests/stubs/console.c \ No newline at end of file diff --git a/tests/lib/cbmem_console-test.c b/tests/lib/cbmem_console-test.c new file mode 100644 index 0000000..831f963 --- /dev/null +++ b/tests/lib/cbmem_console-test.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include "../lib/cbmem_console.c" + +#include <inttypes.h> +#include <tests/test.h> + + +#if ENV_ROMSTAGE_OR_BEFORE + /* Weak references have to be here, so TEST_REGION macro will work properly */ + __weak extern u8 _preram_cbmem_console[]; + __weak extern u8 _epreram_cbmem_console[]; + + #define PRERAM_CBMEM_CONSOLE_SIZE (1 * KiB) + TEST_REGION(preram_cbmem_console, PRERAM_CBMEM_CONSOLE_SIZE); +#endif + +/* Disable init hooks. This test does not need them. */ +void cbmem_run_init_hooks(int is_recovery) +{ + (void)is_recovery; +} + +int setup_cbmemc(void **state) +{ + cbmemc_init(); + return 0; +} + +int teardown_cbmemc(void **state) +{ + current_console->size = 0; + return 0; +} + +void test_cbmemc_init(void **state) +{ + cbmemc_init(); + + /* Check if console structure was created. */ + assert_non_null(current_console); +} + +void test_cbmemc_tx_byte(void **state) +{ + int i; + u32 cursor; + const unsigned char data[] = "Random testing string\n" + "`1234567890-=~!@#$%^&*()_+\n" + "abcdefghijklmnopqrstuvwxyz\n" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; + + for (i = 0; i < ARRAY_SIZE(data); ++i) + cbmemc_tx_byte(data[i]); + + cursor = current_console->cursor & CURSOR_MASK; + + assert_int_equal(ARRAY_SIZE(data), cursor); + + /* Check if all characters were added correctly. */ + assert_string_equal(data, current_console->body); +} + +void test_cbmemc_tx_byte_overflow(void **state) +{ + int i; + u32 cursor; + u32 flags; + const uint32_t console_size = current_console->size; + const unsigned char data[] = "Another random string\n" + "abcdefghijklmnopqrstuvwxyz\n" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" + "`1234567890-=~!@#$%^&*()_+\n"; + + for (i = 0; i < console_size + ARRAY_SIZE(data); ++i) + cbmemc_tx_byte(data[i % ARRAY_SIZE(data)]); + + cursor = current_console->cursor & CURSOR_MASK; + flags = current_console->cursor & ~CURSOR_MASK; + + /* Check if there was a overflow in buffer */ + assert_int_equal(OVERFLOW, flags & OVERFLOW); + + /* Check if cursor got back to the beginning of a buffer */ + assert_int_equal(ARRAY_SIZE(data), cursor); +} + +int main(void) +{ +#if ENV_ROMSTAGE_OR_BEFORE + const char *test_name = "cbmem_console-test-romstage"; +#else + const char *test_name = "cbmem_console-test-ramstage"; +#endif + + const struct CMUnitTest tests[] = { + cmocka_unit_test_teardown(test_cbmemc_init, teardown_cbmemc), + cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte, + setup_cbmemc, teardown_cbmemc), + cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte_overflow, + setup_cbmemc, teardown_cbmemc), + }; + + return cmocka_run_group_tests_name(test_name, tests, NULL, NULL); +}