aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAnastasia Klimchuk <aklm@chromium.org>2021-06-11 16:07:33 +1000
committerNico Huber <nico.h@gmx.de>2021-06-22 20:58:40 +0000
commitd3e778f8ab1694d9c93c2c290fdcd8929c7151f9 (patch)
treec46c43a9d4c9488ab02d3e0e9210204106dff096 /tests
parentf9f9c9d2c618c494f0d8ba9f4a1da4aea725d003 (diff)
downloadflashrom-d3e778f8ab1694d9c93c2c290fdcd8929c7151f9.tar.gz
flashrom-d3e778f8ab1694d9c93c2c290fdcd8929c7151f9.tar.bz2
flashrom-d3e778f8ab1694d9c93c2c290fdcd8929c7151f9.zip
tests: Move test environment header files into tests directory
Both of these headers are only used in test builds, so they should live in tests/ directory. No changes to meson.build and tests/meson.build files are needed because tests/meson.build adds current directory to search for include files. BUG=b:181803212 TEST=ninja test -> all tests pass nm builddir/tests/flashrom_unit_tests.p/.._it85spi.c.o -> has symbols _test_calloc, _test_free, test_inb, test_outb nm builddir/flashrom.p/it85spi.c.o -> has symbols calloc free inb outb Change-Id: Ia42773b98b1eb6c65241aa559c0c8b4926bd0814 Signed-off-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/55408 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/hwaccess_x86_io_unittest.h52
-rw-r--r--tests/unittest_env.h48
2 files changed, 100 insertions, 0 deletions
diff --git a/tests/hwaccess_x86_io_unittest.h b/tests/hwaccess_x86_io_unittest.h
new file mode 100644
index 00000000..7bffc963
--- /dev/null
+++ b/tests/hwaccess_x86_io_unittest.h
@@ -0,0 +1,52 @@
+/*
+ * 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>
+#include <sys/io.h>
+
+/* 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/unittest_env.h b/tests/unittest_env.h
new file mode 100644
index 00000000..9bd7509c
--- /dev/null
+++ b/tests/unittest_env.h
@@ -0,0 +1,48 @@
+/*
+ * 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 included in all files when they are built for unit test
+ * environment, for all the files to be covered by dynamic memory allocation
+ * checks (checks for memory leaks, buffer overflows and underflows).
+ *
+ * See flashrom_test_dep in meson.build for more details.
+ *
+ * https://api.cmocka.org/group__cmocka__alloc.html
+ */
+
+extern void* _test_malloc(const size_t size, const char* file, const int line);
+extern void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
+extern void* _test_calloc(const size_t number_of_elements, const size_t size,
+ const char* file, const int line);
+extern void _test_free(void* const ptr, const char* file, const int line);
+
+#ifdef malloc
+#undef malloc
+#endif
+#ifdef calloc
+#undef calloc
+#endif
+#ifdef realloc
+#undef realloc
+#endif
+#ifdef free
+#undef free
+#endif
+
+#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
+#define realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
+#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
+#define free(ptr) _test_free(ptr, __FILE__, __LINE__)