Nico Huber submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved
tests: Move current_io to io_mock.c be visible across tests

tests.c is growing and needs to be split, specifically all
libusb wraps need to be extracted into their own file. See later
in the chain a lot more wraps are added for libusb functions. To
be able to split it, current_io needs to be moved one level up,
to be visible across tests. This allows having multiple files with
wraps, and all the wraps can use current_io.

BUG=b:181803212
TEST=builds and ninja test

Change-Id: I5327b5de430afe13a8cc931c8b4b188dcb8c8cf6
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/57915
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
---
A tests/io_mock.c
M tests/io_mock.h
M tests/meson.build
M tests/tests.c
4 files changed, 66 insertions(+), 41 deletions(-)

diff --git a/tests/io_mock.c b/tests/io_mock.c
new file mode 100644
index 0000000..51dde92
--- /dev/null
+++ b/tests/io_mock.c
@@ -0,0 +1,29 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2021, Google Inc. All rights reserved.
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 "io_mock.h"
+
+static const struct io_mock *current_io = NULL;
+
+void io_mock_register(const struct io_mock *io)
+{
+ current_io = io;
+}
+
+const struct io_mock *get_io(void)
+{
+ return current_io;
+}
diff --git a/tests/io_mock.h b/tests/io_mock.h
index a285e53..7c235d8 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -95,4 +95,6 @@

void io_mock_register(const struct io_mock *io);

+const struct io_mock *get_io(void);
+
#endif
diff --git a/tests/meson.build b/tests/meson.build
index 8019bd3..df56358 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -14,6 +14,7 @@
root_includes = include_directories('../subprojects')

srcs = [
+ 'io_mock.c',
'tests.c',
'helpers.c',
'flashrom.c',
diff --git a/tests/tests.c b/tests/tests.c
index 329e798..afba5f3 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -26,13 +26,6 @@
return (void *)NON_ZERO;
}

-static const struct io_mock *current_io = NULL;
-
-void io_mock_register(const struct io_mock *io)
-{
- current_io = io;
-}
-
/* Workaround for https://github.com/clibs/cmocka/issues/17 */
char *__wrap_strdup(const char *s)
{
@@ -84,27 +77,27 @@
int __wrap_open(const char *pathname, int flags)
{
LOG_ME;
- if (current_io && current_io->open)
- return current_io->open(current_io->state, pathname, flags);
+ if (get_io() && get_io()->open)
+ return get_io()->open(get_io()->state, pathname, flags);
return NON_ZERO;
}

int __wrap_open64(const char *pathname, int flags)
{
LOG_ME;
- if (current_io && current_io->open)
- return current_io->open(current_io->state, pathname, flags);
+ if (get_io() && get_io()->open)
+ return get_io()->open(get_io()->state, pathname, flags);
return NON_ZERO;
}

int __wrap_ioctl(int fd, unsigned long int request, ...)
{
LOG_ME;
- if (current_io && current_io->ioctl) {
+ if (get_io() && get_io()->ioctl) {
va_list args;
int out;
va_start(args, request);
- out = current_io->ioctl(current_io->state, fd, request, args);
+ out = get_io()->ioctl(get_io()->state, fd, request, args);
va_end(args);
return out;
}
@@ -114,32 +107,32 @@
int __wrap_write(int fd, const void *buf, size_t sz)
{
LOG_ME;
- if (current_io && current_io->write)
- return current_io->write(current_io->state, fd, buf, sz);
+ if (get_io() && get_io()->write)
+ return get_io()->write(get_io()->state, fd, buf, sz);
return sz;
}

int __wrap_read(int fd, void *buf, size_t sz)
{
LOG_ME;
- if (current_io && current_io->read)
- return current_io->read(current_io->state, fd, buf, sz);
+ if (get_io() && get_io()->read)
+ return get_io()->read(get_io()->state, fd, buf, sz);
return sz;
}

FILE *__wrap_fopen(const char *pathname, const char *mode)
{
LOG_ME;
- if (current_io && current_io->fopen)
- return current_io->fopen(current_io->state, pathname, mode);
+ if (get_io() && get_io()->fopen)
+ return get_io()->fopen(get_io()->state, pathname, mode);
return not_null();
}

FILE *__wrap_fopen64(const char *pathname, const char *mode)
{
LOG_ME;
- if (current_io && current_io->fopen)
- return current_io->fopen(current_io->state, pathname, mode);
+ if (get_io() && get_io()->fopen)
+ return get_io()->fopen(get_io()->state, pathname, mode);
return not_null();
}

@@ -170,16 +163,16 @@
char *__wrap_fgets(char *buf, int len, FILE *fp)
{
LOG_ME;
- if (current_io && current_io->fgets)
- return current_io->fgets(current_io->state, buf, len, fp);
+ if (get_io() && get_io()->fgets)
+ return get_io()->fgets(get_io()->state, buf, len, fp);
return NULL;
}

size_t __wrap_fread(void *ptr, size_t size, size_t len, FILE *fp)
{
LOG_ME;
- if (current_io && current_io->fread)
- return current_io->fread(current_io->state, ptr, size, len, fp);
+ if (get_io() && get_io()->fread)
+ return get_io()->fread(get_io()->state, ptr, size, len, fp);
return 0;
}

@@ -216,8 +209,8 @@
int __wrap_fclose(FILE *fp)
{
LOG_ME;
- if (current_io && current_io->fclose)
- return current_io->fclose(current_io->state, fp);
+ if (get_io() && get_io()->fclose)
+ return get_io()->fclose(get_io()->state, fp);
return 0;
}

@@ -247,45 +240,45 @@
void __wrap_test_outb(unsigned char value, unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->outb)
- current_io->outb(current_io->state, value, port);
+ if (get_io() && get_io()->outb)
+ get_io()->outb(get_io()->state, value, port);
}

unsigned char __wrap_test_inb(unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->inb)
- return current_io->inb(current_io->state, port);
+ if (get_io() && get_io()->inb)
+ return get_io()->inb(get_io()->state, port);
return 0;
}

void __wrap_test_outw(unsigned short value, unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->outw)
- current_io->outw(current_io->state, value, port);
+ if (get_io() && get_io()->outw)
+ get_io()->outw(get_io()->state, value, port);
}

unsigned short __wrap_test_inw(unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->inw)
- return current_io->inw(current_io->state, port);
+ if (get_io() && get_io()->inw)
+ return get_io()->inw(get_io()->state, port);
return 0;
}

void __wrap_test_outl(unsigned int value, unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->outl)
- current_io->outl(current_io->state, value, port);
+ if (get_io() && get_io()->outl)
+ get_io()->outl(get_io()->state, value, port);
}

unsigned int __wrap_test_inl(unsigned short port)
{
/* LOG_ME; */
- if (current_io && current_io->inl)
- return current_io->inl(current_io->state, port);
+ if (get_io() && get_io()->inl)
+ return get_io()->inl(get_io()->state, port);
return 0;
}

@@ -313,8 +306,8 @@
uint16_t wLength, unsigned int timeout)
{
LOG_ME;
- if (current_io && current_io->libusb_control_transfer)
- return current_io->libusb_control_transfer(current_io->state,
+ if (get_io() && get_io()->libusb_control_transfer)
+ return get_io()->libusb_control_transfer(get_io()->state,
devh, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
return 0;
}

4 is the latest approved patch-set. No files were changed between the latest approved patch-set and the submitted one.

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I5327b5de430afe13a8cc931c8b4b188dcb8c8cf6
Gerrit-Change-Number: 57915
Gerrit-PatchSet: 6
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@mailbox.org>
Gerrit-MessageType: merged