1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-29 16:03:47 +02:00

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>
This commit is contained in:
Anastasia Klimchuk 2021-09-17 15:31:01 +10:00 committed by Nico Huber
parent 83b5191399
commit 97acc374e4
4 changed files with 66 additions and 41 deletions

29
tests/io_mock.c Normal file
View File

@ -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;
}

View File

@ -95,4 +95,6 @@ struct io_mock {
void io_mock_register(const struct io_mock *io); void io_mock_register(const struct io_mock *io);
const struct io_mock *get_io(void);
#endif #endif

View File

@ -14,6 +14,7 @@
root_includes = include_directories('../subprojects') root_includes = include_directories('../subprojects')
srcs = [ srcs = [
'io_mock.c',
'tests.c', 'tests.c',
'helpers.c', 'helpers.c',
'flashrom.c', 'flashrom.c',

View File

@ -26,13 +26,6 @@ void *not_null(void)
return (void *)NON_ZERO; 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 */ /* Workaround for https://github.com/clibs/cmocka/issues/17 */
char *__wrap_strdup(const char *s) 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) int __wrap_open(const char *pathname, int flags)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->open) if (get_io() && get_io()->open)
return current_io->open(current_io->state, pathname, flags); return get_io()->open(get_io()->state, pathname, flags);
return NON_ZERO; return NON_ZERO;
} }
int __wrap_open64(const char *pathname, int flags) int __wrap_open64(const char *pathname, int flags)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->open) if (get_io() && get_io()->open)
return current_io->open(current_io->state, pathname, flags); return get_io()->open(get_io()->state, pathname, flags);
return NON_ZERO; return NON_ZERO;
} }
int __wrap_ioctl(int fd, unsigned long int request, ...) int __wrap_ioctl(int fd, unsigned long int request, ...)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->ioctl) { if (get_io() && get_io()->ioctl) {
va_list args; va_list args;
int out; int out;
va_start(args, request); 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); va_end(args);
return out; 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) int __wrap_write(int fd, const void *buf, size_t sz)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->write) if (get_io() && get_io()->write)
return current_io->write(current_io->state, fd, buf, sz); return get_io()->write(get_io()->state, fd, buf, sz);
return sz; return sz;
} }
int __wrap_read(int fd, void *buf, size_t sz) int __wrap_read(int fd, void *buf, size_t sz)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->read) if (get_io() && get_io()->read)
return current_io->read(current_io->state, fd, buf, sz); return get_io()->read(get_io()->state, fd, buf, sz);
return sz; return sz;
} }
FILE *__wrap_fopen(const char *pathname, const char *mode) FILE *__wrap_fopen(const char *pathname, const char *mode)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->fopen) if (get_io() && get_io()->fopen)
return current_io->fopen(current_io->state, pathname, mode); return get_io()->fopen(get_io()->state, pathname, mode);
return not_null(); return not_null();
} }
FILE *__wrap_fopen64(const char *pathname, const char *mode) FILE *__wrap_fopen64(const char *pathname, const char *mode)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->fopen) if (get_io() && get_io()->fopen)
return current_io->fopen(current_io->state, pathname, mode); return get_io()->fopen(get_io()->state, pathname, mode);
return not_null(); return not_null();
} }
@ -170,16 +163,16 @@ int __wrap_fstat64(int fd, void *buf)
char *__wrap_fgets(char *buf, int len, FILE *fp) char *__wrap_fgets(char *buf, int len, FILE *fp)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->fgets) if (get_io() && get_io()->fgets)
return current_io->fgets(current_io->state, buf, len, fp); return get_io()->fgets(get_io()->state, buf, len, fp);
return NULL; return NULL;
} }
size_t __wrap_fread(void *ptr, size_t size, size_t len, FILE *fp) size_t __wrap_fread(void *ptr, size_t size, size_t len, FILE *fp)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->fread) if (get_io() && get_io()->fread)
return current_io->fread(current_io->state, ptr, size, len, fp); return get_io()->fread(get_io()->state, ptr, size, len, fp);
return 0; return 0;
} }
@ -216,8 +209,8 @@ int __wrap_setvbuf(FILE *fp, char *buf, int type, size_t size)
int __wrap_fclose(FILE *fp) int __wrap_fclose(FILE *fp)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->fclose) if (get_io() && get_io()->fclose)
return current_io->fclose(current_io->state, fp); return get_io()->fclose(get_io()->state, fp);
return 0; return 0;
} }
@ -247,45 +240,45 @@ int __wrap_rget_io_perms(void)
void __wrap_test_outb(unsigned char value, unsigned short port) void __wrap_test_outb(unsigned char value, unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->outb) if (get_io() && get_io()->outb)
current_io->outb(current_io->state, value, port); get_io()->outb(get_io()->state, value, port);
} }
unsigned char __wrap_test_inb(unsigned short port) unsigned char __wrap_test_inb(unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->inb) if (get_io() && get_io()->inb)
return current_io->inb(current_io->state, port); return get_io()->inb(get_io()->state, port);
return 0; return 0;
} }
void __wrap_test_outw(unsigned short value, unsigned short port) void __wrap_test_outw(unsigned short value, unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->outw) if (get_io() && get_io()->outw)
current_io->outw(current_io->state, value, port); get_io()->outw(get_io()->state, value, port);
} }
unsigned short __wrap_test_inw(unsigned short port) unsigned short __wrap_test_inw(unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->inw) if (get_io() && get_io()->inw)
return current_io->inw(current_io->state, port); return get_io()->inw(get_io()->state, port);
return 0; return 0;
} }
void __wrap_test_outl(unsigned int value, unsigned short port) void __wrap_test_outl(unsigned int value, unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->outl) if (get_io() && get_io()->outl)
current_io->outl(current_io->state, value, port); get_io()->outl(get_io()->state, value, port);
} }
unsigned int __wrap_test_inl(unsigned short port) unsigned int __wrap_test_inl(unsigned short port)
{ {
/* LOG_ME; */ /* LOG_ME; */
if (current_io && current_io->inl) if (get_io() && get_io()->inl)
return current_io->inl(current_io->state, port); return get_io()->inl(get_io()->state, port);
return 0; return 0;
} }
@ -313,8 +306,8 @@ int __wrap_libusb_control_transfer(libusb_device_handle *devh, uint8_t bmRequest
uint16_t wLength, unsigned int timeout) uint16_t wLength, unsigned int timeout)
{ {
LOG_ME; LOG_ME;
if (current_io && current_io->libusb_control_transfer) if (get_io() && get_io()->libusb_control_transfer)
return current_io->libusb_control_transfer(current_io->state, return get_io()->libusb_control_transfer(get_io()->state,
devh, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout); devh, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
return 0; return 0;
} }