summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorPeter Jones <pjones@redhat.com>2021-02-14 14:50:44 -0500
committerPeter Jones <pjones@redhat.com>2021-02-25 10:15:14 -0500
commit9ca8e9a633501c3ec3c95d2576b795eee8d9b1cc (patch)
tree00bb38ceba093b70cb1f54a85dac69c4e3de7532 /include
parent73322ba087d10d06b0656816bf4b7ba80b02c751 (diff)
downloadefi-boot-shim-9ca8e9a633501c3ec3c95d2576b795eee8d9b1cc.tar.gz
efi-boot-shim-9ca8e9a633501c3ec3c95d2576b795eee8d9b1cc.zip
make 'make test' able to run unit test harnesses
This adds a couple of make targets to do unit tests that are linked to libc: test-FOO : builds and runs test-FOO for any test-FOO.c test : builds and runs all test-FOO tests Note that building and running this test does not quite work yet /on this branch/. In order to do that, we need some cleanups and reorganizing that I don't want to push just yet, which can be found on https://github.com/rhboot/shim/tree/test-reorg Signed-off-by: Peter Jones <pjones@redhat.com>
Diffstat (limited to 'include')
-rw-r--r--include/test.h123
-rw-r--r--include/test.mk45
2 files changed, 168 insertions, 0 deletions
diff --git a/include/test.h b/include/test.h
new file mode 100644
index 00000000..6fc178ba
--- /dev/null
+++ b/include/test.h
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/*
+ * test.h - fake a bunch of EFI types so we can build test harnesses with libc
+ * Copyright Peter Jones <pjones@redhat.com>
+ */
+
+#ifdef SHIM_UNIT_TEST
+#ifndef TEST_H_
+#define TEST_H_
+
+#include <stdarg.h>
+
+#if defined(__aarch64__)
+#include <aa64/efibind.h>
+#elif defined(__arm__)
+#include <arm/efibind.h>
+#elif defined(__i386__) || defined(__i486__) || defined(__i686__)
+#include <ia32/efibind.h>
+#elif defined(__x86_64__)
+#include <x64/efibind.h>
+#else
+#error what arch is this
+#endif
+
+#include <efidef.h>
+
+#include <efidevp.h>
+#include <efiprot.h>
+#include <eficon.h>
+#include <efiapi.h>
+#include <efierr.h>
+
+#include <efipxebc.h>
+#include <efinet.h>
+#include <efiip.h>
+
+#include <stdlib.h>
+
+#define ZeroMem(buf, sz) memset(buf, 0, sz)
+#define SetMem(buf, sz, value) memset(buf, value, sz)
+#define CopyMem(dest, src, len) memcpy(dest, src, len)
+#define CompareMem(dest, src, len) memcmp(dest, src, len)
+
+#include <assert.h>
+
+#define AllocateZeroPool(x) calloc(1, (x))
+#define AllocatePool(x) malloc(x)
+#define FreePool(x) free(x)
+#define ReallocatePool(old, oldsz, newsz) realloc(old, newsz)
+
+extern int debug;
+#ifdef dprint
+#undef dprint
+#define dprint(fmt, ...) {( if (debug) printf("%s:%d:" fmt, __func__, __LINE__, ##__VA_ARGS__); })
+#endif
+
+#define eassert(cond, fmt, ...) \
+ ({ \
+ if (!(cond)) { \
+ printf("%s:%d:" fmt, __func__, __LINE__, \
+ ##__VA_ARGS__); \
+ } \
+ assert(cond); \
+ })
+
+#define assert_equal_return(a, b, status, fmt, ...) \
+ ({ \
+ if (!((a) == (b))) { \
+ printf("%s:%d:" fmt, __func__, __LINE__, (a), (b), \
+ ##__VA_ARGS__); \
+ printf("%s:%d:Assertion `%s' failed.\n", __func__, \
+ __LINE__, __stringify(a == b)); \
+ return status; \
+ } \
+ })
+
+#define assert_return(cond, status, fmt, ...) \
+ ({ \
+ if (!(cond)) { \
+ printf("%s:%d:" fmt, __func__, __LINE__, \
+ ##__VA_ARGS__); \
+ printf("%s:%d:Assertion `%s' failed.\n", __func__, \
+ __LINE__, __stringify(cond)); \
+ return status; \
+ } \
+ })
+
+#define assert_goto(cond, label, fmt, ...) \
+ ({ \
+ if (!(cond)) { \
+ printf("%s:%d:" fmt, __func__, __LINE__, \
+ ##__VA_ARGS__); \
+ printf("%s:%d:Assertion `%s' failed.\n", __func__, \
+ __LINE__, __stringify(cond)); \
+ goto label; \
+ } \
+ })
+
+#define assert_equal_goto(a, b, label, fmt, ...) \
+ ({ \
+ if (!((a) == (b))) { \
+ printf("%s:%d:" fmt, __func__, __LINE__, (a), (b), \
+ ##__VA_ARGS__); \
+ printf("%s:%d:Assertion `%s' failed.\n", __func__, \
+ __LINE__, __stringify(a == b)); \
+ goto label; \
+ } \
+ })
+
+#define test(x, ...) \
+ ({ \
+ int rc; \
+ printf("running %s\n", __stringify(x)); \
+ rc = x(__VA_ARGS__); \
+ if (rc < 0) \
+ status = 1; \
+ printf("%s: %s\n", __stringify(x), \
+ rc < 0 ? "failed" : "passed"); \
+ })
+
+#endif /* !TEST_H_ */
+#endif /* SHIM_UNIT_TEST */
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/test.mk b/include/test.mk
new file mode 100644
index 00000000..f70fdaa9
--- /dev/null
+++ b/include/test.mk
@@ -0,0 +1,45 @@
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+# test.mk - makefile to make local test programs
+#
+
+.SUFFIXES:
+
+CC = gcc
+VALGRIND ?=
+DEBUG_PRINTS ?= 0
+CFLAGS = -O2 -ggdb -std=gnu11 \
+ -isystem $(TOPDIR)/include/system \
+ $(EFI_INCLUDES) \
+ -Iinclude -iquote . \
+ -fshort-wchar -flto -fno-builtin \
+ -Wall \
+ -Wextra \
+ -Wsign-compare \
+ -Wno-deprecated-declarations \
+ -Wno-pointer-sign \
+ -Wno-unused \
+ -Werror \
+ -Werror=nonnull \
+ -Werror=nonnull-compare \
+ $(ARCH_DEFINES) \
+ -DEFI_FUNCTION_WRAPPER \
+ -DGNU_EFI_USE_MS_ABI -DPAGE_SIZE=4096 \
+ -DSHIM_UNIT_TEST \
+ "-DDEFAULT_DEBUG_PRINT_STATE=$(DEBUG_PRINTS)"
+
+tests := $(patsubst %.c,%,$(wildcard test-*.c))
+
+$(tests) :: test-% : test.c test-%.c $(test-%_FILES)
+ $(CC) $(CFLAGS) -o $@ $^ $(wildcard $*.c) $(test-$*_FILES)
+ $(VALGRIND) ./$@
+
+test : $(tests)
+
+all : test
+
+clean :
+
+.PHONY: $(tests) all test clean
+
+# vim:ft=make