From 9ca8e9a633501c3ec3c95d2576b795eee8d9b1cc Mon Sep 17 00:00:00 2001 From: Peter Jones Date: Sun, 14 Feb 2021 14:50:44 -0500 Subject: 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 --- include/test.h | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 include/test.h (limited to 'include/test.h') 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 + */ + +#ifdef SHIM_UNIT_TEST +#ifndef TEST_H_ +#define TEST_H_ + +#include + +#if defined(__aarch64__) +#include +#elif defined(__arm__) +#include +#elif defined(__i386__) || defined(__i486__) || defined(__i686__) +#include +#elif defined(__x86_64__) +#include +#else +#error what arch is this +#endif + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#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 + +#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 -- cgit v1.2.3