diff options
author | Anastasia Klimchuk <aklm@chromium.org> | 2021-08-19 15:15:19 +1000 |
---|---|---|
committer | Edward O'Callaghan <quasisec@chromium.org> | 2021-08-30 02:44:17 +0000 |
commit | 05b59d2ca30548f8460d87dbf8353ab1437cb204 (patch) | |
tree | 5298405692008e8230849f60ffc4887c90a00a62 /tests/init_shutdown.c | |
parent | 099e3785126437abafb4f4784f05ae469deb2d1d (diff) | |
download | flashrom-05b59d2ca30548f8460d87dbf8353ab1437cb204.tar.gz flashrom-05b59d2ca30548f8460d87dbf8353ab1437cb204.tar.bz2 flashrom-05b59d2ca30548f8460d87dbf8353ab1437cb204.zip |
tests: Mock file i/o for linux_mtd and linux_spi tests
This patch adds an init-shutdown test for linux_mtd. Since
linux_mtd is using file i/o operations, those are added to the
framework and mocked.
Another driver linux_spi which is also using file i/o, got an upgrade
in this patch, and it is now reading max buffer size from sysfs (using
mocked file i/o).
A good side-effect is that linux_mtd is the first test for opaque
masters, which is great to have in preparation for a change like
CB:56103 but for opaque masters.
BUG=b:181803212
TEST=builds and ninja test
Change-Id: I73f0d6ff2ad5074add7a721ed3416230d3647e3f
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/56413
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'tests/init_shutdown.c')
-rw-r--r-- | tests/init_shutdown.c | 99 |
1 files changed, 96 insertions, 3 deletions
diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c index d1d0c7f30..3236b22c9 100644 --- a/tests/init_shutdown.c +++ b/tests/init_shutdown.c @@ -19,6 +19,8 @@ #include "io_mock.h" #include "programmer.h" +#define NOT_NULL ((void *)0xf000baaa) + static void run_lifecycle(void **state, const struct programmer_entry *prog, const char *param) { (void) state; /* unused */ @@ -195,16 +197,107 @@ void ene_lpc_init_and_shutdown_test_success(void **state) #endif } +struct linux_mtd_io_state { + char *fopen_path; +}; + +FILE *linux_mtd_fopen(void *state, const char *pathname, const char *mode) +{ + struct linux_mtd_io_state *io_state = state; + + io_state->fopen_path = strdup(pathname); + + return NOT_NULL; +} + +size_t linux_mtd_fread(void *state, void *buf, size_t size, size_t len, FILE *fp) +{ + struct linux_mtd_fread_mock_entry { + const char *path; + const char *data; + }; + const struct linux_mtd_fread_mock_entry fread_mock_map[] = { + { "/sys/class/mtd/mtd0//type", "nor" }, + { "/sys/class/mtd/mtd0//name", "Device" }, + { "/sys/class/mtd/mtd0//flags", "" }, + { "/sys/class/mtd/mtd0//size", "1024" }, + { "/sys/class/mtd/mtd0//erasesize", "512" }, + { "/sys/class/mtd/mtd0//numeraseregions", "0" }, + }; + + struct linux_mtd_io_state *io_state = state; + unsigned int i; + + if (!io_state->fopen_path) + return 0; + + for (i = 0; i < ARRAY_SIZE(fread_mock_map); i++) { + const struct linux_mtd_fread_mock_entry *entry = &fread_mock_map[i]; + + if (!strcmp(io_state->fopen_path, entry->path)) { + size_t data_len = min(size * len, strlen(entry->data)); + memcpy(buf, entry->data, data_len); + return data_len; + } + } + + return 0; +} + +int linux_mtd_fclose(void *state, FILE *fp) +{ + struct linux_mtd_io_state *io_state = state; + + free(io_state->fopen_path); + + return 0; +} + +void linux_mtd_init_and_shutdown_test_success(void **state) +{ +#if CONFIG_LINUX_MTD == 1 + struct linux_mtd_io_state linux_mtd_io_state = { NULL }; + const struct io_mock linux_mtd_io = { + .state = &linux_mtd_io_state, + .fopen = linux_mtd_fopen, + .fread = linux_mtd_fread, + .fclose = linux_mtd_fclose, + }; + + io_mock_register(&linux_mtd_io); + + run_lifecycle(state, &programmer_linux_mtd, ""); + + io_mock_register(NULL); +#else + skip(); +#endif +} + +char *linux_spi_fgets(void *state, char *buf, int len, FILE *fp) +{ + /* Emulate reading max buffer size from sysfs. */ + const char *max_buf_size = "1048576"; + + return memcpy(buf, max_buf_size, min(len, strlen(max_buf_size) + 1)); +} + void linux_spi_init_and_shutdown_test_success(void **state) { /* * Current implementation tests a particular path of the init procedure. - * There are two ways for it to succeed: reading the buffer size from sysfs - * and the fallback to getpagesize(). This test does the latter (fallback to - * getpagesize). + * Specifically, it is reading the buffer size from sysfs. */ #if CONFIG_LINUX_SPI == 1 + const struct io_mock linux_spi_io = { + .fgets = linux_spi_fgets, + }; + + io_mock_register(&linux_spi_io); + run_lifecycle(state, &programmer_linux_spi, "dev=/dev/null"); + + io_mock_register(NULL); #else skip(); #endif |