Edward O'Callaghan has submitted this change. ( 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 cover 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 Reviewed-on: https://review.coreboot.org/c/flashrom/+/59239 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Edward O'Callaghan quasisec@chromium.org Reviewed-by: Felix Singer felixsinger@posteo.net --- M tests/chip.c M tests/tests.c M tests/tests.h 3 files changed, 100 insertions(+), 0 deletions(-)
Approvals: build bot (Jenkins): Verified Felix Singer: Looks good to me, approved Edward O'Callaghan: Looks good to me, approved
diff --git a/tests/chip.c b/tests/chip.c index 798199c..fe12533 100644 --- a/tests/chip.c +++ b/tests/chip.c @@ -31,6 +31,7 @@
#include "chipdrivers.h" #include "flash.h" +#include "io_mock.h" #include "libflashrom.h" #include "programmer.h"
@@ -350,3 +351,98 @@ free(param_dup); free(newcontents); } + +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 = "-"; + unsigned long size = mock_chip.total_size * 1024; + uint8_t *const newcontents = malloc(size); + + printf("Verify chip operation started.\n"); + assert_int_equal(0, read_buf_from_file(newcontents, size, filename)); + assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size)); + printf("Verify chip operation done.\n"); + + teardown(&layout); + + free(newcontents); + + 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 = "-"; + unsigned long size = mock_chip.total_size * 1024; + uint8_t *const newcontents = malloc(size); + + /* + * 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, read_buf_from_file(newcontents, size, filename)); + assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL)); + printf("Write chip operation done.\n"); + + printf("Verify chip operation started.\n"); + assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size)); + printf("Verify chip operation done.\n"); + + teardown(&layout); + + free(param_dup); + free(newcontents); + + io_mock_register(NULL); +} diff --git a/tests/tests.c b/tests/tests.c index 06dc12f..1dda467 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -390,6 +390,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 16974af..f1b4e9b 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -64,5 +64,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 */