Edward O'Callaghan submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Edward O'Callaghan: Looks good to me, approved Anastasia Klimchuk: Looks good to me, approved
tests: add fprintf() mock with redirection to io_mock

We've seen somewhat obscure test failures where the real fprintf()
function was passed a fake file returned by the fopen() mock.

Although the code that caused the specific failure was cros-specific,
adding an fprintf() mock should help avoid future debugging.

TEST=ninja test
BRANCH=none
BUG=b:217661133

Change-Id: I3f8594ea24d17436a7932732d9d05416b804dc93
Signed-off-by: Nikolai Artemiev <nartemiev@google.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/61708
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
M tests/io_mock.h
M tests/meson.build
M tests/tests.c
3 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/tests/io_mock.h b/tests/io_mock.h
index 854ddd9..8734774 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -102,6 +102,7 @@
FILE* (*fopen)(void *state, const char *pathname, const char *mode);
char* (*fgets)(void *state, char *buf, int len, FILE *fp);
size_t (*fread)(void *state, void *buf, size_t size, size_t len, FILE *fp);
+ int (*fprintf)(void *state, FILE *fp, const char *fmt, va_list args);
int (*fclose)(void *state, FILE *fp);
};

diff --git a/tests/meson.build b/tests/meson.build
index 5bb6ac9..2f0fc67 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -56,6 +56,7 @@
'-Wl,--wrap=fsync',
'-Wl,--wrap=fread',
'-Wl,--wrap=fgets',
+ '-Wl,--wrap=fprintf',
'-Wl,--wrap=fclose',
'-Wl,--wrap=feof',
'-Wl,--wrap=ferror',
diff --git a/tests/tests.c b/tests/tests.c
index a4a312e..06dc12f 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -236,6 +236,20 @@
return 0;
}

+int __wrap_fprintf(FILE *fp, const char *fmt, ...)
+{
+ LOG_ME;
+ if (get_io() && get_io()->fprintf) {
+ va_list args;
+ int out;
+ va_start(args, fmt);
+ out = get_io()->fprintf(get_io()->state, fp, fmt, args);
+ va_end(args);
+ return out;
+ }
+ return 0;
+}
+
int __wrap_fclose(FILE *fp)
{
LOG_ME;

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I3f8594ea24d17436a7932732d9d05416b804dc93
Gerrit-Change-Number: 61708
Gerrit-PatchSet: 3
Gerrit-Owner: Nikolai Artemiev <nartemiev@google.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@mailbox.org>
Gerrit-MessageType: merged