Edward O'Callaghan has uploaded this change for review.

View Change

tests/: Add subregion alignment unit-test

Change-Id: Id3ce5cd1936f0f348d34a6c77cee15e27a5c353f
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
---
M tests/chip.c
M tests/tests.c
M tests/tests.h
3 files changed, 113 insertions(+), 0 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/59/71659/1
diff --git a/tests/chip.c b/tests/chip.c
index 1d9cdc9..5f89d05 100644
--- a/tests/chip.c
+++ b/tests/chip.c
@@ -102,6 +102,29 @@
return 0;
}

+static int unittest_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap)
+{
+ int ret = 0;
+ FILE *output_type = stdout;
+
+ va_list logfile_args;
+ va_copy(logfile_args, ap);
+
+ if (level < FLASHROM_MSG_INFO)
+ output_type = stderr;
+
+ if (level <= verbose_screen) {
+ ret = vfprintf(output_type, fmt, ap);
+ /* msg_*spew often happens inside chip accessors in possibly
+ * time-critical operations. Don't slow them down by flushing. */
+ if (level != FLASHROM_MSG_SPEW)
+ fflush(output_type);
+ }
+
+ va_end(logfile_args);
+ return ret;
+}
+
static void setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout,
struct flashchip *chip, const char *programmer_param, const struct io_mock *io)
{
@@ -418,6 +441,84 @@
free(newcontents);
}

+void write_chip_subregion_with_dummyflasher_test_success(void **state) // XXX
+{
+ (void) state; /* unused */
+
+ static struct io_mock_fallback_open_state data = {
+ .noc = 0,
+ .paths = { NULL },
+ };
+ const struct io_mock chip_io = {
+ .fallback_open_state = &data,
+ };
+
+ struct flashrom_flashctx flashctx = { 0 };
+ struct flashrom_layout *layout;
+ struct flashchip mock_chip = chip_W25Q128_V;
+ const uint32_t mock_chip_size = mock_chip.total_size * KiB;
+ /*
+ * 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");
+
+#define MOCK_CHIP_SUBREGION_CONTENTS 0xCC
+ /**
+ * Prepare newcontents as follows:
+ * {MOCK_CHIP_SUBREGION_CONTENTS} [..] {MOCK_CHIP_SUBREGION_CONTENTS}.
+ */
+ uint8_t *newcontents = calloc(1, mock_chip_size);
+ assert_non_null(newcontents);
+ memset(newcontents, MOCK_CHIP_SUBREGION_CONTENTS, mock_chip_size);
+
+ setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
+ flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_AFTER_WRITE, true);
+ flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, false);
+ flashrom_set_log_callback((flashrom_log_callback *)&unittest_print_cb);
+
+ /**
+ * Prepare mock chip content and release setup_chip() layout for our
+ * custom ones.
+ */
+ assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, mock_chip_size, NULL));
+ flashrom_layout_release(layout);
+
+ /**
+ * Create subregion smaller than erase granularity of chip.
+ */
+ printf("Creating custom subregion layout... ");
+ assert_int_equal(0, flashrom_layout_new(&layout));
+ printf("Adding and including subregion0... ");
+ assert_int_equal(0, flashrom_layout_add_region(layout, 0, (1 * KiB), "subregion0"));
+ assert_int_equal(0, flashrom_layout_include_region(layout, "subregion0"));
+ flashrom_layout_set(&flashctx, layout);
+ printf("Subregion layout configuration done.\n");
+
+ /**
+ * Prepare newcontents as follows:
+ * 0xAA 0xAA {MOCK_CHIP_SUBREGION_CONTENTS} [..] {MOCK_CHIP_SUBREGION_CONTENTS}.
+ */
+ printf("Subregion chip W op..\n");
+ memset(newcontents, 0xAA, 2);
+ assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, mock_chip_size, NULL));
+ printf("Subregion chip W op done.\n");
+
+ printf("Entire chip V op..\n");
+ //flashrom_layout_set(&flashctx, NULL); // use default layout.
+ flashrom_layout_release(layout);
+ assert_int_equal(0, flashrom_layout_new(&layout));
+ assert_int_equal(0, flashrom_layout_add_region(layout, 0, mock_chip_size - 1, "entire"));
+ assert_int_equal(0, flashrom_layout_include_region(layout, "entire"));
+ flashrom_layout_set(&flashctx, layout);
+ assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, mock_chip_size));
+ printf("Entire chip V op done.\n");
+
+ teardown(&layout);
+ free(param_dup);
+ free(newcontents);
+}
+
static size_t verify_chip_fread(void *state, void *buf, size_t size, size_t len, FILE *fp)
{
/*
diff --git a/tests/tests.c b/tests/tests.c
index a1dcaca..b96dee5 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -480,6 +480,7 @@
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(write_chip_subregion_with_dummyflasher_test_success),
cmocka_unit_test(verify_chip_test_success),
cmocka_unit_test(verify_chip_with_dummyflasher_test_success),
};
diff --git a/tests/tests.h b/tests/tests.h
index 6a12fdb..ee583f1 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -80,6 +80,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 write_chip_subregion_with_dummyflasher_test_success(void **state);
void verify_chip_test_success(void **state);
void verify_chip_with_dummyflasher_test_success(void **state);


To view, visit change 71659. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Id3ce5cd1936f0f348d34a6c77cee15e27a5c353f
Gerrit-Change-Number: 71659
Gerrit-PatchSet: 1
Gerrit-Owner: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-MessageType: newchange