summaryrefslogtreecommitdiffstats
path: root/tests/msgb
diff options
context:
space:
mode:
authorJacob Erlbeck <jerlbeck@sysmocom.de>2015-11-27 13:26:17 +0100
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2016-01-15 18:10:05 +0100
commitcbefa085593128c5f5e6da255edeae6b4f952032 (patch)
tree7e3f410db92986341128511a431cef104e28e335 /tests/msgb
parent7cd8a1b06387427c6db4fd9f6dee116212ec304a (diff)
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
Diffstat (limited to 'tests/msgb')
-rw-r--r--tests/msgb/msgb_test.c31
1 files changed, 31 insertions, 0 deletions
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 <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");