Anastasia Klimchuk has uploaded this change for review.

View Change

tests: Use regular cmocka wraps for hwaccess functions

hwaccess functions used to be static inline functions and needed
a special treatment so that they could be mocked for unit tests.

This has changed, see include/hwaccess_x86_io.h now the functions
are not static inline anymore, and it is possible to use regular
cmocka wraps.

Fixes https://ticket.coreboot.org/issues/385

BUG=b:181803212
TEST=ninja test

Change-Id: Iafce071ea7ad5bcfdebbba968699d5743705f8e0
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
---
M meson.build
D tests/hwaccess_x86_io_unittest.h
M tests/meson.build
M tests/tests.c
M tests/wraps.h
5 files changed, 29 insertions(+), 83 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/81/64881/1
diff --git a/meson.build b/meson.build
index a8e5a7b..abaed4e 100644
--- a/meson.build
+++ b/meson.build
@@ -512,7 +512,6 @@
compile_args : [
'-includestdlib.h',
'-includeunittest_env.h',
- '-includehwaccess_x86_io_unittest.h'
],
dependencies : [
deps,
diff --git a/tests/hwaccess_x86_io_unittest.h b/tests/hwaccess_x86_io_unittest.h
deleted file mode 100644
index 14f3caa..0000000
--- a/tests/hwaccess_x86_io_unittest.h
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * This file is part of the flashrom project.
- *
- * Copyright 2021 Google LLC
- *
- * 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; version 2 of the License.
- *
- * 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.
- */
-
-/*
- * This header is used instead of hwaccess_x86_io.h for unit tests
- * (see flashrom_test_dep in meson.build).
- *
- * There is no hardware in unit test environment and all hardware operations
- * need to be mocked.
- */
-
-/*
- * The same guard is used intentionally for hwaccess_x86_io.h and
- * hwaccess_x86_io_unittest.h. When build is made for the test environment,
- * hwaccess_x86_io_unittest.h is included first, and it effectively
- * replaces hwaccess_x86_io.h.
- */
-#ifndef __HWACCESS_X86_IO_H__
-#define __HWACCESS_X86_IO_H__ 1
-
-#define OUTB(v, p) test_outb(v, p)
-#define OUTW(v, p) test_outw(v, p)
-#define OUTL(v, p) test_outl(v, p)
-#define INB(p) test_inb(p)
-#define INW(p) test_inw(p)
-#define INL(p) test_inl(p)
-
-#include <stdint.h>
-
-int rget_io_perms(void);
-
-/*
- * Dummy implementation of iopl from sys/io.h.
- * sys/io.h by itself is platform-specific, so instead of including
- * the header we just have this dummy function, which is sufficient
- * for test purposes.
- */
-static inline int iopl(int level)
-{
- return 0;
-}
-
-/* All functions below are mocked in unit tests. */
-
-void test_outb(uint8_t value, uint16_t port);
-uint8_t test_inb(uint16_t port);
-void test_outw(uint16_t value, uint16_t port);
-uint16_t test_inw(uint16_t port);
-void test_outl(uint32_t value, uint16_t port);
-uint32_t test_inl(uint16_t port);
-
-#endif /* !__HWACCESS_X86_IO_H__ */
diff --git a/tests/meson.build b/tests/meson.build
index 588ac17..38f5be5 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -67,12 +67,12 @@
'-Wl,--wrap=clearerr',
'-Wl,--wrap=setvbuf',
'-Wl,--wrap=rget_io_perms',
- '-Wl,--wrap=test_outb',
- '-Wl,--wrap=test_inb',
- '-Wl,--wrap=test_outw',
- '-Wl,--wrap=test_inw',
- '-Wl,--wrap=test_outl',
- '-Wl,--wrap=test_inl',
+ '-Wl,--wrap=OUTB',
+ '-Wl,--wrap=INB',
+ '-Wl,--wrap=OUTW',
+ '-Wl,--wrap=INW',
+ '-Wl,--wrap=OUTL',
+ '-Wl,--wrap=INL',
'-Wl,--wrap=usb_dev_get_by_vid_pid_number',
'-Wl,--wrap=libusb_init',
'-Wl,--wrap=libusb_open',
diff --git a/tests/tests.c b/tests/tests.c
index a719a8f..8f002ab 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -321,14 +321,14 @@
return 0;
}

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

-unsigned char __wrap_test_inb(unsigned short port)
+unsigned char __wrap_INB(unsigned short port)
{
/* LOG_ME; */
if (get_io() && get_io()->inb)
@@ -336,14 +336,14 @@
return 0;
}

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

-unsigned short __wrap_test_inw(unsigned short port)
+unsigned short __wrap_INW(unsigned short port)
{
/* LOG_ME; */
if (get_io() && get_io()->inw)
@@ -351,14 +351,14 @@
return 0;
}

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

-unsigned int __wrap_test_inl(unsigned short port)
+unsigned int __wrap_INL(unsigned short port)
{
/* LOG_ME; */
if (get_io() && get_io()->inl)
@@ -366,6 +366,17 @@
return 0;
}

+/*
+ * Dummy implementation of iopl from sys/io.h.
+ * sys/io.h by itself is platform-specific, so instead of including
+ * the header we just have this dummy function, which is sufficient
+ * for test purposes.
+ */
+static inline int iopl(int level)
+{
+ return 0;
+}
+
int main(void)
{
int ret = 0;
diff --git a/tests/wraps.h b/tests/wraps.h
index c088d98..bfa8a41 100644
--- a/tests/wraps.h
+++ b/tests/wraps.h
@@ -58,12 +58,12 @@
int __wrap_ferror(FILE *fp);
void __wrap_clearerr(FILE *fp);
int __wrap_rget_io_perms(void);
-void __wrap_test_outb(unsigned char value, unsigned short port);
-unsigned char __wrap_test_inb(unsigned short port);
-void __wrap_test_outw(unsigned short value, unsigned short port);
-unsigned short __wrap_test_inw(unsigned short port);
-void __wrap_test_outl(unsigned int value, unsigned short port);
-unsigned int __wrap_test_inl(unsigned short port);
+void __wrap_OUTB(unsigned char value, unsigned short port);
+unsigned char __wrap_INB(unsigned short port);
+void __wrap_OUTW(unsigned short value, unsigned short port);
+unsigned short __wrap_INW(unsigned short port);
+void __wrap_OUTL(unsigned int value, unsigned short port);
+unsigned int __wrap_INL(unsigned short port);
int __wrap_spi_send_command(const struct flashctx *flash,
unsigned int writecnt, unsigned int readcnt,
const unsigned char *writearr, unsigned char *readarr);

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Iafce071ea7ad5bcfdebbba968699d5743705f8e0
Gerrit-Change-Number: 64881
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange