From 9a52d20b34b39df6402d3f342ae4d19dbd358df7 Mon Sep 17 00:00:00 2001 From: Anastasia Klimchuk Date: Fri, 12 Nov 2021 16:36:18 +1100 Subject: 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 Reviewed-on: https://review.coreboot.org/c/flashrom/+/59239 Tested-by: build bot (Jenkins) Reviewed-by: Edward O'Callaghan Reviewed-by: Felix Singer --- tests/chip.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.c | 2 ++ tests/tests.h | 2 ++ 3 files changed, 100 insertions(+) (limited to 'tests') diff --git a/tests/chip.c b/tests/chip.c index 798199c3c..fe125332a 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 @@ void write_chip_with_dummyflasher_test_success(void **state) 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 06dc12fae..1dda46703 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -390,6 +390,8 @@ int main(void) 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 16974af21..f1b4e9b2a 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -64,5 +64,7 @@ void read_chip_test_success(void **state); 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 */ -- cgit v1.2.3