Edward O'Callaghan has submitted this change. ( https://review.coreboot.org/c/flashrom/+/62320 )
Change subject: tests/: Add file path and flags validation to open() calls ......................................................................
tests/: Add file path and flags validation to open() calls
With this change we add path and flag validation to many tests that do not call open. Expected path is set to NULL, if the code indead calls open then the assertion for non-NULL will make the test fail.
BUG=b:217629892,b:215255210 TEST=`ninja test`.
Change-Id: I892fa1ecee26ebce9640893edbb228fa9aa7b0b6 Signed-off-by: Edward O'Callaghan quasisec@google.com Co-Author: Daniel Campello campello@chromium.org Signed-off-by: Daniel Campello campello@chromium.org Reviewed-on: https://review.coreboot.org/c/flashrom/+/62320 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Anastasia Klimchuk aklm@chromium.org --- M tests/chip.c M tests/lifecycle.c 2 files changed, 113 insertions(+), 20 deletions(-)
Approvals: build bot (Jenkins): Verified Anastasia Klimchuk: Looks good to me, approved
diff --git a/tests/chip.c b/tests/chip.c index fe12533..03d604f 100644 --- a/tests/chip.c +++ b/tests/chip.c @@ -102,8 +102,10 @@ }
static void setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout, - struct flashchip *chip, const char *programmer_param) + struct flashchip *chip, const char *programmer_param, const struct io_mock *io) { + io_mock_register(io); + flashctx->chip = chip;
g_chip_state.unlock_calls = 0; @@ -139,6 +141,8 @@ printf("Releasing layout... "); flashrom_layout_release(*layout); printf("done\n"); + + io_mock_register(NULL); }
static const struct flashchip chip_8MiB = { @@ -190,12 +194,20 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_8MiB; const char *param = ""; /* Default values for all params. */
- setup_chip(&flashctx, &layout, &mock_chip, param); + setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
printf("Erase chip operation started.\n"); assert_int_equal(0, flashrom_flash_erase(&flashctx)); @@ -208,6 +220,14 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_W25Q128_V; @@ -217,7 +237,7 @@ */ char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flashctx, &layout, &mock_chip, param_dup); + setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
printf("Erase chip operation started.\n"); assert_int_equal(0, flashrom_flash_erase(&flashctx)); @@ -232,12 +252,20 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_8MiB; const char *param = ""; /* Default values for all params. */
- setup_chip(&flashctx, &layout, &mock_chip, param); + setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
const char *const filename = "read_chip.test"; unsigned long size = mock_chip.total_size * 1024; @@ -257,6 +285,14 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_W25Q128_V; @@ -266,7 +302,7 @@ */ char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flashctx, &layout, &mock_chip, param_dup); + setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
const char *const filename = "read_chip.test"; unsigned long size = mock_chip.total_size * 1024; @@ -287,12 +323,20 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_8MiB; const char *param = ""; /* Default values for all params. */
- setup_chip(&flashctx, &layout, &mock_chip, param); + setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
/* * Providing filename "-" means content is taken from standard input. @@ -325,6 +369,14 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock chip_io = { + .fallback_open_state = &data, + }; + struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_W25Q128_V; @@ -334,7 +386,7 @@ */ char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flashctx, &layout, &mock_chip, param_dup); + setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
/* See comment in write_chip_test_success */ const char *const filename = "-"; @@ -367,18 +419,21 @@ { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; const struct io_mock verify_chip_io = { .fread = verify_chip_fread, + .fallback_open_state = &data, };
- io_mock_register(&verify_chip_io); - struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_8MiB; const char *param = ""; /* Default values for all params. */
- setup_chip(&flashctx, &layout, &mock_chip, param); + setup_chip(&flashctx, &layout, &mock_chip, param, &verify_chip_io);
/* See comment in write_chip_test_success */ const char *const filename = "-"; @@ -393,20 +448,21 @@ teardown(&layout);
free(newcontents); - - io_mock_register(NULL); }
void verify_chip_with_dummyflasher_test_success(void **state) { (void) state; /* unused */
+ static struct io_mock_fallback_open_state data = { + .noc = 0, + .paths = { NULL }, + }; const struct io_mock verify_chip_io = { .fread = verify_chip_fread, + .fallback_open_state = &data, };
- io_mock_register(&verify_chip_io); - struct flashrom_flashctx flashctx = { 0 }; struct flashrom_layout *layout; struct flashchip mock_chip = chip_W25Q128_V; @@ -416,7 +472,7 @@ */ char *param_dup = strdup("bus=spi,emulate=W25Q128FV");
- setup_chip(&flashctx, &layout, &mock_chip, param_dup); + setup_chip(&flashctx, &layout, &mock_chip, param_dup, &verify_chip_io);
/* See comment in write_chip_test_success */ const char *const filename = "-"; @@ -443,6 +499,4 @@
free(param_dup); free(newcontents); - - io_mock_register(NULL); } diff --git a/tests/lifecycle.c b/tests/lifecycle.c index 41cf7a8..825654f 100644 --- a/tests/lifecycle.c +++ b/tests/lifecycle.c @@ -83,7 +83,15 @@ void dummy_basic_lifecycle_test_success(void **state) { #if CONFIG_DUMMY == 1 - run_basic_lifecycle(state, NULL, &programmer_dummy, "bus=parallel+lpc+fwh+spi"); + static struct io_mock_fallback_open_state dummy_fallback_open_state = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock dummy_io = { + .fallback_open_state = &dummy_fallback_open_state, + }; + + run_basic_lifecycle(state, &dummy_io, &programmer_dummy, "bus=parallel+lpc+fwh+spi"); #else skip(); #endif @@ -92,7 +100,15 @@ void dummy_probe_lifecycle_test_success(void **state) { #if CONFIG_DUMMY == 1 - run_probe_lifecycle(state, NULL, &programmer_dummy, "bus=spi,emulate=W25Q128FV", "W25Q128.V"); + static struct io_mock_fallback_open_state dummy_fallback_open_state = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock dummy_io = { + .fallback_open_state = &dummy_fallback_open_state, + }; + + run_probe_lifecycle(state, &dummy_io, &programmer_dummy, "bus=spi,emulate=W25Q128FV", "W25Q128.V"); #else skip(); #endif @@ -101,7 +117,15 @@ void nicrealtek_basic_lifecycle_test_success(void **state) { #if CONFIG_NICREALTEK == 1 - run_basic_lifecycle(state, NULL, &programmer_nicrealtek, ""); + static struct io_mock_fallback_open_state nicrealtek_fallback_open_state = { + .noc = 0, + .paths = { NULL }, + }; + const struct io_mock nicrealtek_io = { + .fallback_open_state = &nicrealtek_fallback_open_state, + }; + + run_basic_lifecycle(state, &nicrealtek_io, &programmer_nicrealtek, ""); #else skip(); #endif @@ -178,12 +202,17 @@ void raiden_debug_basic_lifecycle_test_success(void **state) { #if CONFIG_RAIDEN_DEBUG_SPI == 1 + static struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { + .noc = 0, + .paths = { NULL }, + }; const struct io_mock raiden_debug_io = { .libusb_get_device_list = raiden_debug_libusb_get_device_list, .libusb_free_device_list = raiden_debug_libusb_free_device_list, .libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, .libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, .libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, + .fallback_open_state = &raiden_debug_fallback_open_state, };
/* @@ -225,9 +254,14 @@ void dediprog_basic_lifecycle_test_success(void **state) { #if CONFIG_DEDIPROG == 1 + static struct io_mock_fallback_open_state dediprog_fallback_open_state = { + .noc = 0, + .paths = { NULL }, + }; const struct io_mock dediprog_io = { .libusb_init = dediprog_libusb_init, .libusb_control_transfer = dediprog_libusb_control_transfer, + .fallback_open_state = &dediprog_fallback_open_state, };
run_basic_lifecycle(state, &dediprog_io, &programmer_dediprog, "voltage=3.5V"); @@ -296,11 +330,16 @@ { #if CONFIG_LINUX_MTD == 1 struct linux_mtd_io_state linux_mtd_io_state = { NULL }; + static struct io_mock_fallback_open_state linux_mtd_fallback_open_state = { + .noc = 0, + .paths = { 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, + .fallback_open_state = &linux_mtd_fallback_open_state, };
run_probe_lifecycle(state, &linux_mtd_io, &programmer_linux_mtd, "", "Opaque flash chip");