Anastasia Klimchuk has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/57915 )
Change subject: tests: Move current_io to io_mock.c be visible across tests ......................................................................
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 --- M tests/io_mock.c M tests/io_mock.h M tests/tests.c 3 files changed, 48 insertions(+), 41 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/15/57915/1
diff --git a/tests/io_mock.c b/tests/io_mock.c index a47c87d..3a2c0f3 100644 --- a/tests/io_mock.c +++ b/tests/io_mock.c @@ -16,6 +16,18 @@
#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; +} + void *not_null(void) { return (void *)NON_ZERO; diff --git a/tests/io_mock.h b/tests/io_mock.h index b3626ef..3ef61a5 100644 --- a/tests/io_mock.h +++ b/tests/io_mock.h @@ -100,4 +100,6 @@
void io_mock_register(const struct io_mock *io);
+const struct io_mock *get_io(void); + #endif diff --git a/tests/tests.c b/tests/tests.c index 72beb83..ccafa89 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -21,13 +21,6 @@ #include <string.h> #include <stdint.h>
-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) { @@ -79,27 +72,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; } @@ -109,32 +102,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(); }
@@ -153,16 +146,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; }
@@ -175,8 +168,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; }
@@ -206,45 +199,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; }
@@ -272,8 +265,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; }