Anastasia Klimchuk has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/59239 )
Change subject: tests: Add tests for verify operation ......................................................................
tests: Add tests for verify operation
This patch adds two tests which run do_verify operation, and adds io_mock for fread.
BUG=b:181803212 TEST=ninja test
Change-Id: I1cc6f73f9b1e385eb963adccf20759c13a40ed3b Signed-off-by: Anastasia Klimchuk aklm@chromium.org --- M tests/chip.c M tests/tests.c M tests/tests.h 3 files changed, 91 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/39/59239/1
diff --git a/tests/chip.c b/tests/chip.c index f5110c0..888b7db 100644 --- a/tests/chip.c +++ b/tests/chip.c @@ -19,6 +19,7 @@
#include "chipdrivers.h" #include "flash.h" +#include "io_mock.h" #include "programmer.h"
#define MOCK_CHIP_SIZE (8*MiB) @@ -340,3 +341,89 @@
free(param_dup); } + +size_t verify_chip_fread(void *state, void *buf, size_t size, size_t len, FILE *fp) +{ + /* + * Verify operation compares contents of the file vs contents on the chip. + * To emulate successful verification we emulate file contents to be the + * same as what is on the chip. + */ + memset(buf, MOCK_CHIP_CONTENT, len); + return len; +} + +void verify_chip_test_success(void **state) +{ + (void) state; /* unused */ + + const struct io_mock verify_chip_io = { + .fread = verify_chip_fread, + }; + + 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); + + /* See comment in write_chip_test_success */ + const char *const filename = "-"; + + printf("Verify chip operation started.\n"); + assert_int_equal(0, do_verify(&flashctx, filename)); + printf("Verify chip operation done.\n"); + + teardown(&layout); + + io_mock_register(NULL); +} + +void verify_chip_with_dummyflasher_test_success(void **state) +{ + (void) state; /* unused */ + + const struct io_mock verify_chip_io = { + .fread = verify_chip_fread, + }; + + io_mock_register(&verify_chip_io); + + struct flashrom_flashctx flashctx = { 0 }; + struct flashrom_layout *layout; + struct flashchip mock_chip = chip_W25Q128_V; + /* + * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this. + * Nothing to mock, dummy is taking care of this already. + */ + char *param_dup = strdup("bus=spi,emulate=W25Q128FV"); + + setup_chip(&flashctx, &layout, &mock_chip, param_dup); + + /* See comment in write_chip_test_success */ + const char *const filename = "-"; + + /* + * Dummyflasher controls chip state and fully emulates reads and writes, + * so to set up initial chip state we need to write on chip. Write + * operation takes content from file and writes on chip. File content is + * emulated in verify_chip_fread mock. + */ + + printf("Write chip operation started.\n"); + assert_int_equal(0, do_write(&flashctx, filename, NULL)); + printf("Write chip operation done.\n"); + + printf("Verify chip operation started.\n"); + assert_int_equal(0, do_verify(&flashctx, filename)); + printf("Verify chip operation done.\n"); + + teardown(&layout); + + free(param_dup); + + io_mock_register(NULL); +} diff --git a/tests/tests.c b/tests/tests.c index e3ad241..5262076 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -351,6 +351,8 @@ cmocka_unit_test(read_chip_with_dummyflasher_test_success), cmocka_unit_test(write_chip_test_success), cmocka_unit_test(write_chip_with_dummyflasher_test_success), + cmocka_unit_test(verify_chip_test_success), + cmocka_unit_test(verify_chip_with_dummyflasher_test_success), }; ret |= cmocka_run_group_tests_name("chip.c tests", chip_tests, NULL, NULL);
diff --git a/tests/tests.h b/tests/tests.h index 732920a..1dab411 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -63,5 +63,7 @@ void read_chip_with_dummyflasher_test_success(void **state); void write_chip_test_success(void **state); void write_chip_with_dummyflasher_test_success(void **state); +void verify_chip_test_success(void **state); +void verify_chip_with_dummyflasher_test_success(void **state);
#endif /* TESTS_H */