Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/63231 )
Change subject: tests/lifecycle.c: Deduce out io-setup-teardown do-pattern ......................................................................
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 quasisec@google.com --- M tests/lifecycle.c 1 file changed, 19 insertions(+), 33 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/31/63231/1
diff --git a/tests/lifecycle.c b/tests/lifecycle.c index 282a6b5..4f7f248 100644 --- a/tests/lifecycle.c +++ b/tests/lifecycle.c @@ -35,7 +35,7 @@ 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 @@ { (void) state; /* unused */
+ io_mock_register(io); + struct flashrom_programmer *flashprog; char *param_dup = strdup(param);
@@ -58,27 +60,30 @@ 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_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 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 @@ 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 @@ .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 @@ .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 @@ -360,11 +353,7 @@ .ioctl = linux_spi_ioctl, };
- 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 @@ -414,11 +403,8 @@ .read = realtek_mst_read, .write = realtek_mst_write, }; - 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 */