Evan Benn has uploaded this change for review.
tests: Convert selfcheck to unit tests
Add unit tests for programmer_table, flashchips, and board_matches
structs. The tests are derived from the selfcheck function, checking
that the required fields have been filled in.
BUG=b:140595239
BRANCH=None
TEST=meson test
Change-Id: I41cd014d9bf909296b6c28e3e00548e6883ff41a
Signed-off-by: Evan Benn <evanbenn@chromium.org>
---
M board_enable.c
M include/programmer.h
M tests/meson.build
M tests/tests.c
M tests/tests.h
A tests/well_formed_tables.c
6 files changed, 109 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/20/69620/1
diff --git a/board_enable.c b/board_enable.c
index 4903c0f..bf26173 100644
--- a/board_enable.c
+++ b/board_enable.c
@@ -2524,6 +2524,7 @@
#endif
{ 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, P3, NULL, NULL, 0, NT, NULL}, /* end marker */
};
+const size_t board_matches_size = ARRAY_SIZE(board_matches);
int selfcheck_board_enables(void)
{
diff --git a/include/programmer.h b/include/programmer.h
index 386e6ec..f979c25 100644
--- a/include/programmer.h
+++ b/include/programmer.h
@@ -193,6 +193,7 @@
};
extern const struct board_match board_matches[];
+extern const size_t board_matches_size;
struct board_info {
const char *vendor;
diff --git a/tests/meson.build b/tests/meson.build
index 66adb92..86d2f30 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -31,6 +31,7 @@
'layout.c',
'chip.c',
'chip_wp.c',
+ 'well_formed_tables.c',
)
if not programmer.get('dummy').get('active')
diff --git a/tests/tests.c b/tests/tests.c
index ea416b8..0b1752e 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -381,6 +381,13 @@
};
ret |= cmocka_run_group_tests_name("helpers.c tests", helpers_tests, NULL, NULL);
+ const struct CMUnitTest well_formed_tables[] = {
+ cmocka_unit_test(well_formed_programmer_table),
+ cmocka_unit_test(well_formed_flashchips_table),
+ cmocka_unit_test(well_formed_board_matches_table),
+ };
+ ret |= cmocka_run_group_tests_name("well_formed_tables.c tests", well_formed_tables, NULL, NULL);
+
const struct CMUnitTest flashrom_tests[] = {
cmocka_unit_test(flashbuses_to_text_test_success),
};
diff --git a/tests/tests.h b/tests/tests.h
index 7d84101..230c042 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -87,4 +87,9 @@
void full_chip_erase_with_wp_dummyflasher_test_success(void **state);
void partial_chip_erase_with_wp_dummyflasher_test_success(void **state);
+/* well_formed_tables.c */
+void well_formed_programmer_table(void **state);
+void well_formed_flashchips_table(void **state);
+void well_formed_board_matches_table(void **state);
+
#endif /* TESTS_H */
diff --git a/tests/well_formed_tables.c b/tests/well_formed_tables.c
new file mode 100644
index 0000000..67593d8
--- /dev/null
+++ b/tests/well_formed_tables.c
@@ -0,0 +1,76 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2022 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 "string.h"
+
+#include "include/test.h"
+#include "programmer.h"
+#include "tests.h"
+
+
+#define assert_table(assertion, index, name) do { \
+ if (!(assertion)) \
+ fail_msg(#assertion " is false for index:%zu name:%s", \
+ (index), \
+ (name) ? (name) : "unknown" \
+ ); \
+} while (0)
+
+
+void well_formed_programmer_table(void **state) {
+ (void) state; /* unused */
+ size_t i;
+ for (i = 0; i < programmer_table_size; i++) {
+ const struct programmer_entry *const p = programmer_table[i];
+ assert_table(p, i, programmer_table[i]->name);
+ assert_table(p->name, i, programmer_table[i]->name);
+ assert_table(p->type, i, programmer_table[i]->name);
+ if (strcmp("internal", p->name) != 0) { /* This one has its device list stored separately. */
+ assert_table(p->devs.note, i, programmer_table[i]->name);
+ }
+ assert_table(p->init, i, programmer_table[i]->name);
+ }
+}
+
+void well_formed_flashchips_table(void **state) {
+ (void) state; /* unused */
+ size_t i;
+ assert_false(flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL);
+ for (i = 0; i < flashchips_size - 1; i++) {
+ const struct flashchip *chip = &flashchips[i];
+ assert_table(chip->vendor, i, flashchips[i].name);
+ assert_table(chip->name, i, flashchips[i].name);
+ assert_table(chip->bustype != BUS_NONE, i, flashchips[i].name);
+ }
+}
+
+void well_formed_board_matches_table(void **state) {
+ (void) state; /* unused */
+ size_t i;
+ assert_false(board_matches_size <= 1 || board_matches[board_matches_size - 1].vendor_name != NULL);
+ for (i = 0; i < board_matches_size - 1; i++) {
+ const struct board_match *b = &board_matches[i];
+ assert_table(b->vendor_name, i, b->board_name);
+ assert_table(b->board_name, i, b->board_name);
+ assert_table(b->board_name, i, b->board_name);
+ if ((b->first_vendor == 0 || b->first_device == 0 ||
+ b->second_vendor == 0 || b->second_device == 0) ||
+ ((b->lb_vendor == NULL) ^ (b->lb_part == NULL)) ||
+ (b->max_rom_decode_parallel == 0 && b->enable == NULL)) {
+ fail_msg("Board enable for %s %s is misdefined.\n",
+ b->vendor_name, b->board_name);
+ }
+ }
+}
To view, visit change 69620. To unsubscribe, or for help writing mail filters, visit settings.