diff mbox series

[for_v23,09/16] selftests/x86/sgx: Add helper function and macros to assert results

Message ID 20191008044613.12350-10-sean.j.christopherson@intel.com (mailing list archive)
State New, archived
Headers show
Series x86/vdso: sgx: Major vDSO cleanup | expand

Commit Message

Sean Christopherson Oct. 8, 2019, 4:46 a.m. UTC
Borrow code and ideas from the KVM selftests for asserting and reporting
test results and failures.  Update the existing test assertions to use
the new functionality.  Defer other updates, e.g. error handling, to
future patches.

Change the license to GPL-2.0-only to accommodate the borrowed code.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 tools/testing/selftests/x86/sgx/main.c | 52 ++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/x86/sgx/main.c b/tools/testing/selftests/x86/sgx/main.c
index 3a0d76c40bcc..0c964bc1fca0 100644
--- a/tools/testing/selftests/x86/sgx/main.c
+++ b/tools/testing/selftests/x86/sgx/main.c
@@ -1,9 +1,10 @@ 
-// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+// SPDX-License-Identifier: GPL-2.0-only
 // Copyright(c) 2016-18 Intel Corporation.
 
 #include <elf.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -24,6 +25,47 @@ 
 #define PAGE_SIZE  4096
 
 static const uint64_t MAGIC = 0x1122334455667788ULL;
+
+void __attribute__((noinline)) test_assert(bool exp, const char *exp_str,
+					   const char *file, unsigned int line,
+					   const char *fmt, ...)
+{
+	va_list ap;
+
+	if (exp)
+		return;
+
+	va_start(ap, fmt);
+
+	fprintf(stderr, "==== SGX Selftest Assertion Failure ====\n");
+	if (exp_str)
+		fprintf(stderr, "  %s:%u: %s\n", file, line, exp_str);
+	if (fmt) {
+		if (exp_str)
+			fputs("  ", stderr);
+		else
+			fprintf(stderr, "  %s:%u: ", file, line);
+		vfprintf(stderr, fmt, ap);
+		fputs("\n", stderr);
+	}
+	va_end(ap);
+	exit(1);
+}
+
+#define TEST_ASSERT(e, fmt, ...) \
+	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
+
+#define ASSERT_EQ(a, b)							     \
+do {									     \
+	typeof(a) __a = (a);						     \
+	typeof(b) __b = (b);						     \
+	test_assert(__a == __b, NULL, __FILE__, __LINE__,		     \
+		    "%s == %s failed.\n"				     \
+		    "\t%s is %#lx\n"					     \
+		    "\t%s is %#lx",					     \
+		    #a, #b, #a, (unsigned long)__a, #b, (unsigned long)__b); \
+} while (0)
+
 void *eenter;
 
 struct vdso_symtab {
@@ -346,15 +388,11 @@  int main(int argc, char *argv[], char *envp[])
 	if (!encl_build(&secs, bin, bin_size, &sigstruct))
 		exit(1);
 
-	printf("Input: 0x%lx\n", MAGIC);
 	sgx_call((void *)&MAGIC, &result, 0, NULL, NULL, NULL,
 		 (void *)secs.base, &exception, NULL);
 
-	if (result != MAGIC) {
-		fprintf(stderr, "0x%lx != 0x%lx\n", result, MAGIC);
-		exit(1);
-	}
+	ASSERT_EQ(result, MAGIC);
 
-	printf("Output: 0x%lx\n", result);
+	printf("All tests passed!\n");
 	exit(0);
 }