From 8abe12e86e9353fd8399e2e663bfc64a97ca5dfa Mon Sep 17 00:00:00 2001 From: Edward O'Callaghan Date: Thu, 24 Feb 2022 13:59:34 +1100 Subject: 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 Co-Author: Daniel Campello Signed-off-by: Daniel Campello Reviewed-on: https://review.coreboot.org/c/flashrom/+/62320 Tested-by: build bot (Jenkins) Reviewed-by: Anastasia Klimchuk --- tests/chip.c | 88 ++++++++++++++++++++++++++++++++++++++++++++----------- tests/lifecycle.c | 45 ++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/chip.c b/tests/chip.c index fe125332a..03d604fa0 100644 --- a/tests/chip.c +++ b/tests/chip.c @@ -102,8 +102,10 @@ int block_erase_chip(struct flashctx *flash, unsigned int blockaddr, unsigned in } 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 @@ static void teardown(struct flashrom_layout **layout) 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 erase_chip_test_success(void **state) { (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 erase_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 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 @@ void erase_chip_with_dummyflasher_test_success(void **state) */ 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 read_chip_test_success(void **state) { (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 read_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 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 @@ void read_chip_with_dummyflasher_test_success(void **state) */ 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 write_chip_test_success(void **state) { (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 write_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 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 @@ void write_chip_with_dummyflasher_test_success(void **state) */ 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 verify_chip_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_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 @@ void verify_chip_test_success(void **state) 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 @@ void verify_chip_with_dummyflasher_test_success(void **state) */ 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 @@ void verify_chip_with_dummyflasher_test_success(void **state) free(param_dup); free(newcontents); - - io_mock_register(NULL); } diff --git a/tests/lifecycle.c b/tests/lifecycle.c index 41cf7a820..825654f9c 100644 --- a/tests/lifecycle.c +++ b/tests/lifecycle.c @@ -83,7 +83,15 @@ static void run_probe_lifecycle(void **state, const struct io_mock *io, 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_basic_lifecycle_test_success(void **state) 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 dummy_probe_lifecycle_test_success(void **state) 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 @@ static void raiden_debug_libusb_free_config_descriptor(void *state, struct libus 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 @@ int dediprog_libusb_control_transfer(void *state, 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 @@ void linux_mtd_probe_lifecycle_test_success(void **state) { #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"); -- cgit v1.2.3