Anastasia Klimchuk has uploaded this change for review.

View Change

tests: Split flashrom tests into two executables

The reason for splitting: some tests need to wrap spi_send_command,
some other tests (specifically the tests in the next patch in this
chain) need a real spi_send_command.

Cmocka offers a capability to call __real_spi_send_command to call a
real function and bypass the wrap. However, the function in question,
spi_send_command, is not called in tests explicitly. There is no
clear way to say "for this one test use the wrap, for other tests use
real".

There is one test file spi25.c which needs spi_send_command to be
wrapped, and this test file is extracted into a separate executable
in this patch. The rest of existing tests don't care about
spi_send_command. However in the next patch new tests are added,
and they need spi_send_command real. The approach used here is: use
real function unless it is required to mock, so all tests that don't
care go into the executable with the real spi_send_command.

BUG=b:181803212
TEST=builds and ninja test

Change-Id: I22945cce3d0f36adaa8032167a3ef4e54eccb611
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
---
M tests/meson.build
M tests/tests.c
M tests/tests.h
A tests/wrap_send_tests.c
A tests/wrap_send_tests.h
5 files changed, 128 insertions(+), 34 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/53/56753/1
diff --git a/tests/meson.build b/tests/meson.build
index 5776862..fb1f438 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -17,16 +17,30 @@
'tests.c',
'helpers.c',
'flashrom.c',
- 'spi25.c',
'init_shutdown.c',
'layout.c',
]

+srcs_wrap_send = [
+ 'wrap_send_tests.c',
+ 'spi25.c',
+]
+
+# These mocks are needed for all executables
+shared_mocks = [
+ '-Wl,--wrap=test_outb',
+ '-Wl,--wrap=test_inb',
+ '-Wl,--wrap=test_outw',
+ '-Wl,--wrap=test_inw',
+ '-Wl,--wrap=test_outl',
+ '-Wl,--wrap=test_inl',
+]
+
mocks = [
+ shared_mocks,
'-Wl,--wrap=strdup',
'-Wl,--wrap=physunmap',
'-Wl,--wrap=physmap',
- '-Wl,--wrap=spi_send_command',
'-Wl,--wrap=sio_write',
'-Wl,--wrap=sio_read',
'-Wl,--wrap=open',
@@ -35,12 +49,6 @@
'-Wl,--wrap=fopen',
'-Wl,--wrap=fopen64',
'-Wl,--wrap=rget_io_perms',
- '-Wl,--wrap=test_outb',
- '-Wl,--wrap=test_inb',
- '-Wl,--wrap=test_outw',
- '-Wl,--wrap=test_inw',
- '-Wl,--wrap=test_outl',
- '-Wl,--wrap=test_inl',
'-Wl,--wrap=usb_dev_get_by_vid_pid_number',
'-Wl,--wrap=libusb_set_configuration',
'-Wl,--wrap=libusb_claim_interface',
@@ -51,6 +59,11 @@
'-Wl,--gc-sections',
]

+mocks_wrap_send = [
+ shared_mocks,
+ '-Wl,--wrap=spi_send_command',
+]
+
flashrom_tests = executable('flashrom_unit_tests',
srcs,
include_directories : root_includes,
@@ -65,3 +78,18 @@
dependencies : [cmocka_dep, flashrom_test_dep],
)
test('cmocka test flashrom', flashrom_tests)
+
+flashrom_wrap_send_tests = executable('flashrom_wrap_send_unit_tests',
+ srcs_wrap_send,
+ include_directories : root_includes,
+ c_args : [
+ cargs,
+ '-ffunction-sections',
+ '-fdata-sections',
+ # '-DSTANDALONE',
+ ],
+ export_dynamic : true,
+ link_args : mocks_wrap_send,
+ dependencies : [cmocka_dep, flashrom_test_dep],
+)
+test('cmocka wrap send test flashrom', flashrom_wrap_send_tests)
diff --git a/tests/tests.c b/tests/tests.c
index 00c987c..3c40faa 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -213,20 +213,6 @@
};
ret |= cmocka_run_group_tests_name("flashrom.c tests", flashrom_tests, NULL, NULL);

- const struct CMUnitTest spi25_tests[] = {
- cmocka_unit_test(spi_write_enable_test_success),
- cmocka_unit_test(spi_write_disable_test_success),
- cmocka_unit_test(probe_spi_rdid_test_success),
- cmocka_unit_test(probe_spi_rdid4_test_success),
- cmocka_unit_test(probe_spi_rems_test_success),
- cmocka_unit_test(probe_spi_res1_test_success),
- cmocka_unit_test(probe_spi_res2_test_success),
- cmocka_unit_test(probe_spi_res3_test_success),
- cmocka_unit_test(probe_spi_at25f_test_success),
- cmocka_unit_test(probe_spi_st95_test_success), /* spi95.c */
- };
- ret |= cmocka_run_group_tests_name("spi25.c tests", spi25_tests, NULL, NULL);
-
const struct CMUnitTest init_shutdown_tests[] = {
cmocka_unit_test(dummy_init_and_shutdown_test_success),
cmocka_unit_test(mec1308_init_and_shutdown_test_success),
diff --git a/tests/tests.h b/tests/tests.h
index 14cfee0..65d3769 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -28,18 +28,6 @@
/* flashrom.c */
void flashbuses_to_text_test_success(void **state);

-/* spi25.c */
-void spi_write_enable_test_success(void **state);
-void spi_write_disable_test_success(void **state);
-void probe_spi_rdid_test_success(void **state);
-void probe_spi_rdid4_test_success(void **state);
-void probe_spi_rems_test_success(void **state);
-void probe_spi_res1_test_success(void **state);
-void probe_spi_res2_test_success(void **state);
-void probe_spi_res3_test_success(void **state);
-void probe_spi_at25f_test_success(void **state);
-void probe_spi_st95_test_success(void **state); /* spi95.c */
-
/* init_shutdown.c */
void dummy_init_and_shutdown_test_success(void **state);
void mec1308_init_and_shutdown_test_success(void **state);
diff --git a/tests/wrap_send_tests.c b/tests/wrap_send_tests.c
new file mode 100644
index 0000000..33da308
--- /dev/null
+++ b/tests/wrap_send_tests.c
@@ -0,0 +1,61 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2020 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 "wrap_send_tests.h"
+
+void __wrap_test_outb(unsigned char value, unsigned short port) {}
+
+unsigned char __wrap_test_inb(unsigned short port)
+{
+ return 0;
+}
+
+void __wrap_test_outw(unsigned short value, unsigned short port) {}
+
+unsigned short __wrap_test_inw(unsigned short port)
+{
+ return 0;
+}
+
+void __wrap_test_outl(unsigned int value, unsigned short port) {}
+
+unsigned int __wrap_test_inl(unsigned short port)
+{
+ return 0;
+}
+
+int main(void)
+{
+ int ret = 0;
+
+ cmocka_set_message_output(CM_OUTPUT_STDOUT);
+
+ const struct CMUnitTest spi25_tests[] = {
+ cmocka_unit_test(spi_write_enable_test_success),
+ cmocka_unit_test(spi_write_disable_test_success),
+ cmocka_unit_test(probe_spi_rdid_test_success),
+ cmocka_unit_test(probe_spi_rdid4_test_success),
+ cmocka_unit_test(probe_spi_rems_test_success),
+ cmocka_unit_test(probe_spi_res1_test_success),
+ cmocka_unit_test(probe_spi_res2_test_success),
+ cmocka_unit_test(probe_spi_res3_test_success),
+ cmocka_unit_test(probe_spi_at25f_test_success),
+ cmocka_unit_test(probe_spi_st95_test_success), /* spi95.c */
+ };
+ ret |= cmocka_run_group_tests_name("spi25.c tests", spi25_tests, NULL, NULL);
+
+ return ret;
+}
diff --git a/tests/wrap_send_tests.h b/tests/wrap_send_tests.h
new file mode 100644
index 0000000..69cdeae
--- /dev/null
+++ b/tests/wrap_send_tests.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2020 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.
+ */
+
+#ifndef WRAP_SEND_TESTS_H
+#define WRAP_SEND_TESTS_H
+
+/* spi25.c */
+void spi_write_enable_test_success(void **state);
+void spi_write_disable_test_success(void **state);
+void probe_spi_rdid_test_success(void **state);
+void probe_spi_rdid4_test_success(void **state);
+void probe_spi_rems_test_success(void **state);
+void probe_spi_res1_test_success(void **state);
+void probe_spi_res2_test_success(void **state);
+void probe_spi_res3_test_success(void **state);
+void probe_spi_at25f_test_success(void **state);
+void probe_spi_st95_test_success(void **state); /* spi95.c */
+
+#endif /* WRAP_SEND_TESTS_H */

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I22945cce3d0f36adaa8032167a3ef4e54eccb611
Gerrit-Change-Number: 56753
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange