From 999bbb32ae6e9b9a339442615a61b4505d7ed1d9 Mon Sep 17 00:00:00 2001 From: Edward O'Callaghan Date: Thu, 31 Mar 2022 10:23:59 +1100 Subject: tests/lifecycle.c: Deduce out io-setup-teardown do-pattern The following do-block is quite error prone to do manually, ``` io_mock_register(&XXX_io); run_probe_lifecycle(state, &XXX, "", ".."); io_mock_register(NULL); ```. Hence, deduce out the common pattern and fold up into the common worker function to handle state machine setup and teardown in a consistent way. BUG=b:227521116 TEST=`ninja test`. Change-Id: Icc00acd980a027337acb079f5afc3cccdfe4c765 Signed-off-by: Edward O'Callaghan Reviewed-on: https://review.coreboot.org/c/flashrom/+/63231 Tested-by: build bot (Jenkins) Reviewed-by: Anastasia Klimchuk Reviewed-by: Daniel Campello --- tests/lifecycle.c | 52 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) (limited to 'tests/lifecycle.c') diff --git a/tests/lifecycle.c b/tests/lifecycle.c index 1a8f21895..41cf7a820 100644 --- a/tests/lifecycle.c +++ b/tests/lifecycle.c @@ -35,7 +35,7 @@ static void probe_chip(const struct programmer_entry *prog, flashrom_flash_release(flashctx); /* cleanup */ } -static void run_lifecycle(void **state, const struct programmer_entry *prog, +static void run_lifecycle(void **state, const struct io_mock *io, const struct programmer_entry *prog, const char *param, const char *const chip_name, void (*action)(const struct programmer_entry *prog, struct flashrom_programmer *flashprog, @@ -43,6 +43,8 @@ static void run_lifecycle(void **state, const struct programmer_entry *prog, { (void) state; /* unused */ + io_mock_register(io); + struct flashrom_programmer *flashprog; char *param_dup = strdup(param); @@ -58,27 +60,30 @@ static void run_lifecycle(void **state, const struct programmer_entry *prog, printf("... flashrom_programmer_shutdown for programmer=%s successful\n", prog->name); free(param_dup); + + io_mock_register(NULL); } -static void run_basic_lifecycle(void **state, const struct programmer_entry *prog, const char *param) +static void run_basic_lifecycle(void **state, const struct io_mock *io, + const struct programmer_entry *prog, const char *param) { /* Basic lifecycle only does init and shutdown, * so neither chip name nor action is needed. */ - run_lifecycle(state, prog, param, NULL /* chip_name */, NULL /* action */); + run_lifecycle(state, io, prog, param, NULL /* chip_name */, NULL /* action */); } -static void run_probe_lifecycle(void **state, const struct programmer_entry *prog, - const char *param, const char *const chip_name) +static void run_probe_lifecycle(void **state, const struct io_mock *io, + const struct programmer_entry *prog, const char *param, const char *const chip_name) { /* Each probe lifecycle should run independently, without cache. */ clear_spi_id_cache(); - run_lifecycle(state, prog, param, chip_name, &probe_chip); + run_lifecycle(state, io, prog, param, chip_name, &probe_chip); } void dummy_basic_lifecycle_test_success(void **state) { #if CONFIG_DUMMY == 1 - run_basic_lifecycle(state, &programmer_dummy, "bus=parallel+lpc+fwh+spi"); + run_basic_lifecycle(state, NULL, &programmer_dummy, "bus=parallel+lpc+fwh+spi"); #else skip(); #endif @@ -87,7 +92,7 @@ void dummy_basic_lifecycle_test_success(void **state) void dummy_probe_lifecycle_test_success(void **state) { #if CONFIG_DUMMY == 1 - run_probe_lifecycle(state, &programmer_dummy, "bus=spi,emulate=W25Q128FV", "W25Q128.V"); + run_probe_lifecycle(state, NULL, &programmer_dummy, "bus=spi,emulate=W25Q128FV", "W25Q128.V"); #else skip(); #endif @@ -96,7 +101,7 @@ void dummy_probe_lifecycle_test_success(void **state) void nicrealtek_basic_lifecycle_test_success(void **state) { #if CONFIG_NICREALTEK == 1 - run_basic_lifecycle(state, &programmer_nicrealtek, ""); + run_basic_lifecycle(state, NULL, &programmer_nicrealtek, ""); #else skip(); #endif @@ -188,11 +193,7 @@ void raiden_debug_basic_lifecycle_test_success(void **state) char raiden_debug_param[12]; snprintf(raiden_debug_param, 12, "address=%d", USB_DEVICE_ADDRESS); - io_mock_register(&raiden_debug_io); - - run_basic_lifecycle(state, &programmer_raiden_debug_spi, raiden_debug_param); - - io_mock_register(NULL); + run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); #else skip(); #endif @@ -229,11 +230,7 @@ void dediprog_basic_lifecycle_test_success(void **state) .libusb_control_transfer = dediprog_libusb_control_transfer, }; - io_mock_register(&dediprog_io); - - run_basic_lifecycle(state, &programmer_dediprog, "voltage=3.5V"); - - io_mock_register(NULL); + run_basic_lifecycle(state, &dediprog_io, &programmer_dediprog, "voltage=3.5V"); #else skip(); #endif @@ -306,11 +303,7 @@ void linux_mtd_probe_lifecycle_test_success(void **state) .fclose = linux_mtd_fclose, }; - io_mock_register(&linux_mtd_io); - - run_probe_lifecycle(state, &programmer_linux_mtd, "", "Opaque flash chip"); - - io_mock_register(NULL); + run_probe_lifecycle(state, &linux_mtd_io, &programmer_linux_mtd, "", "Opaque flash chip"); #else skip(); #endif @@ -366,11 +359,7 @@ void linux_spi_probe_lifecycle_test_success(void **state) .fallback_open_state = &linux_spi_fallback_open_state, }; - io_mock_register(&linux_spi_io); - - run_probe_lifecycle(state, &programmer_linux_spi, "dev=/dev/null", "W25Q128.V"); - - io_mock_register(NULL); + run_probe_lifecycle(state, &linux_spi_io, &programmer_linux_spi, "dev=/dev/null", "W25Q128.V"); #else skip(); #endif @@ -416,11 +405,8 @@ void realtek_mst_basic_lifecycle_test_success(void **state) .write = realtek_mst_write, .fallback_open_state = &realtek_mst_fallback_open_state, }; - io_mock_register(&realtek_mst_io); - run_basic_lifecycle(state, &programmer_realtek_mst_i2c_spi, "bus=254,enter-isp=0"); - - io_mock_register(NULL); + run_basic_lifecycle(state, &realtek_mst_io, &programmer_realtek_mst_i2c_spi, "bus=254,enter-isp=0"); #else skip(); #endif /* CONFIG_REALTEK_I2C_SPI */ -- cgit v1.2.3