From 97acc374e453233fed65b397c4195c21dd5a0a57 Mon Sep 17 00:00:00 2001 From: Anastasia Klimchuk Date: Fri, 17 Sep 2021 15:31:01 +1000 Subject: 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 Reviewed-on: https://review.coreboot.org/c/flashrom/+/57915 Tested-by: build bot (Jenkins) Reviewed-by: Nico Huber --- tests/io_mock.c | 29 +++++++++++++++++++++ tests/io_mock.h | 2 ++ tests/meson.build | 1 + tests/tests.c | 75 +++++++++++++++++++++++++------------------------------ 4 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 tests/io_mock.c (limited to 'tests') diff --git a/tests/io_mock.c b/tests/io_mock.c new file mode 100644 index 00000000..51dde927 --- /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 a285e534..7c235d82 100644 --- a/tests/io_mock.h +++ b/tests/io_mock.h @@ -95,4 +95,6 @@ struct io_mock { 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 8019bd37..df563580 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 329e798a..afba5f36 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -26,13 +26,6 @@ void *not_null(void) 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 @@ uint8_t __wrap_sio_read(uint16_t port, uint8_t reg) 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_ioctl(int fd, unsigned long int request, ...) 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 @@ int __wrap_fstat64(int fd, void *buf) 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_setvbuf(FILE *fp, char *buf, int type, size_t size) 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 @@ int __wrap_rget_io_perms(void) 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 @@ int __wrap_libusb_control_transfer(libusb_device_handle *devh, uint8_t bmRequest 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; } -- cgit v1.2.3