Anastasia Klimchuk has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/56501 )
Change subject: tests: Add test to erase a chip ......................................................................
tests: Add test to erase a chip
The test covers the code which parforms do_erase operation. It works with fake chip and dummy programmer. Fake chip has all operations defined, they all log and return 0. Read operation always initialises buffer with one and the same value.
BUG=b:181803212 TEST=builds and ninja test
Change-Id: I6f74bfe4e02244d24d6c837cc3d551251e7b4898 Signed-off-by: Anastasia Klimchuk aklm@chromium.org --- A tests/chip.c M tests/meson.build M tests/tests.c M tests/tests.h 4 files changed, 124 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/01/56501/1
diff --git a/tests/chip.c b/tests/chip.c new file mode 100644 index 0000000..98758b7 --- /dev/null +++ b/tests/chip.c @@ -0,0 +1,115 @@ +/* + * This file is part of the flashrom project. + * + * Copyright 2021 Google LLC + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <include/test.h> +#include <stdio.h> +#include <string.h> + +#include "flash.h" +#include "programmer.h" + +int read_chip(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) +{ + printf("Read chip called with start=0x%x, len=0x%x\n", start, len); + /* + * Populate buf with the value used for erase operation, this allows to pass + * verification checks and also emulates successful erase. + */ + buf = memset(buf, 0xff, len); + return 0; +} + +int write_chip(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) +{ + printf("Write chip called with start=0x%x, len=0x%x\n", start, len); + return 0; +} + +int unlock_chip(struct flashctx *flash) +{ + printf("Unlock chip called\n"); + return 0; +} + +int block_erase_chip(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen) +{ + printf("Block erase called with blockaddr=0x%x, blocklen=0x%x\n", blockaddr, blocklen); + return 0; +} + +void erase_chip_test_success(void **state) +{ + (void) state; /* unused */ + + /* Programmer param. Test is happy with default params. */ + char *param_dup = strdup(""); + + struct flashchip chip = { + .vendor = "aklm", + /* + * Total size less than 16 * 1024 to skip some steps + * in flashrom.c#prepare_flash_access. + */ + .total_size = 8 * 1024, + .tested = TEST_OK_PREW, + .read = read_chip, + .write = write_chip, + .unlock = unlock_chip, + .block_erasers = + {{ + /* All blocks within total size of the chip. */ + .eraseblocks = { {2 * 1024, 3} }, + .block_erase = block_erase_chip, + }}, + }; + struct flashrom_flashctx flash = { + .chip = &chip, + }; + + struct flashrom_layout *layout; + + printf("Creating layout with one included region... "); + assert_int_equal(0, flashrom_layout_new(&layout)); + /* One region which covers total size of chip. */ + assert_int_equal(0, flashrom_layout_add_region(layout, 0x00000000, 0x00002000, "region")); + assert_int_equal(0, flashrom_layout_include_region(layout, "region")); + + flashrom_layout_set(&flash, layout); + printf("done\n"); + + /* + * We need some programmer (any), and dummy is a very good one, + * because it doesn't need any mocking. So no extra complexity + * from a programmer side, and test can focus on the code which + * erases the chip. + */ + printf("Dummyflasher initialising... "); + assert_int_equal(0, programmer_init(&programmer_dummy, param_dup)); + printf("done\n"); + + printf("Erase chip operation started.\n"); + assert_int_equal(0, do_erase(&flash)); + printf("Erase chip operation done.\n"); + + printf("Dummyflasher shutdown... "); + assert_int_equal(0, programmer_shutdown()); + printf("done\n"); + + printf("Releasing layout... "); + flashrom_layout_release(layout); + printf("done\n"); + + free(param_dup); +} diff --git a/tests/meson.build b/tests/meson.build index 5776862..1bca6ec 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -20,6 +20,7 @@ 'spi25.c', 'init_shutdown.c', 'layout.c', + 'chip.c', ]
mocks = [ diff --git a/tests/tests.c b/tests/tests.c index 00c987c..3f77598 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -246,5 +246,10 @@ }; ret |= cmocka_run_group_tests_name("layout.c tests", layout_tests, NULL, NULL);
+ const struct CMUnitTest chip_tests[] = { + cmocka_unit_test(erase_chip_test_success), + }; + ret |= cmocka_run_group_tests_name("chip.c tests", chip_tests, NULL, NULL); + return ret; } diff --git a/tests/tests.h b/tests/tests.h index 14cfee0..c99b095 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -55,4 +55,7 @@ void layout_region_invalid_address_test_success(void **state); void layout_region_invalid_range_test_success(void **state);
+/* chip.c */ +void erase_chip_test_success(void **state); + #endif /* TESTS_H */