From cbefa085593128c5f5e6da255edeae6b4f952032 Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Fri, 27 Nov 2015 13:26:17 +0100 Subject: msgb/test: Add functions to catch and check exceptions 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 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/msgb') diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c index 75925095..260aca54 100644 --- a/tests/msgb/msgb_test.c +++ b/tests/msgb/msgb_test.c @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -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"); -- cgit v1.2.3