diff mbox

[5/8] msgb/test: Add functions to catch and check exceptions

Message ID 1448627180-10603-6-git-send-email-jerlbeck@sysmocom.de
State Accepted
Headers show

Commit Message

Jacob Erlbeck Nov. 27, 2015, 12:26 p.m. UTC
Currently the msgb error handling cannot be fully tested, since in
many cases osmo_panic will be called. This will in turn call abort().
Using an osmo_panic_handler that just returns will not help, since
many msgb functions rely on MSGB_ABORT to not return at all.

This commit uses an alternative osmo_panic_raise handler that just
calls longjmp to return to the test function.

Since some of this activity is logged to stderr where the strings may
contain variable parts like pointer addresses, stderr checking is
disabled in testsuite.at.

Sponsored-by: On-Waves ehf
---
 tests/msgb/msgb_test.c | 31 +++++++++++++++++++++++++++++++
 tests/testsuite.at     |  2 +-
 2 files changed, 32 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c
index 7592509..260aca5 100644
--- a/tests/msgb/msgb_test.c
+++ b/tests/msgb/msgb_test.c
@@ -23,6 +23,7 @@ 
 #include <osmocom/core/logging.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/core/msgb.h>
+#include <setjmp.h>
 
 #include <errno.h>
 
@@ -34,6 +35,36 @@ 
 		abort(); \
 	}
 
+static jmp_buf jmp_env;
+static int jmp_env_valid = 0;
+static void osmo_panic_raise(const char *fmt, va_list args)
+{
+	/*
+	 * The args can include pointer values which are not suitable for
+	 * regression testing. So just write the (hopefully constant) format
+	 * string to stdout and write the full message to stderr.
+	 */
+	printf("%s", fmt);
+	vfprintf(stderr, fmt, args);
+	if (!jmp_env_valid)
+		abort();
+	longjmp(jmp_env, 1);
+}
+
+/* Note that this does not nest */
+#define OSMO_PANIC_TRY(pE) (osmo_panic_try(pE, setjmp(jmp_env)))
+
+static int osmo_panic_try(volatile int *exception, int setjmp_result)
+{
+	jmp_env_valid = setjmp_result == 0;
+	*exception = setjmp_result;
+
+	if (setjmp_result)
+		fprintf(stderr, "Exception caught: %d\n", setjmp_result);
+
+	return *exception == 0;
+}
+
 static void test_msgb_api()
 {
 	struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 12199e3..bd3fa1b 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -30,7 +30,7 @@  AT_CLEANUP
 AT_SETUP([msgb])
 AT_KEYWORDS([msgb])
 cat $abs_srcdir/msgb/msgb_test.ok > expout
-AT_CHECK([$abs_top_builddir/tests/msgb/msgb_test], [0], [expout])
+AT_CHECK([$abs_top_builddir/tests/msgb/msgb_test], [0], [expout], [ignore])
 AT_CLEANUP
 
 if ENABLE_MSGFILE