diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-10-20 20:49:13 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-10-20 20:49:13 +0100 |
commit | 7a30c726b71293d50b8473da524c2fe4f9056337 (patch) | |
tree | 3c82dc0f7276f73f65ac89eef749a7576049c236 | |
parent | e471d201ee5b2423eb6cb4855b7b7c8ff0483119 (diff) | |
download | dynstr-7a30c726b71293d50b8473da524c2fe4f9056337.tar.gz |
Add test to verify dynstr_malloc_error_abort actually calls abort.
-rw-r--r-- | dynstr.c | 1 | ||||
-rw-r--r-- | unittests.c | 38 |
2 files changed, 39 insertions, 0 deletions
@@ -34,4 +34,5 @@ void dynstr_malloc_error_indicate(int error, size_t size, void *oldptr) void dynstr_malloc_error_abort(int error, size_t size, void *oldptr) { + abort(); } diff --git a/unittests.c b/unittests.c index fd30081..28a9e9d 100644 --- a/unittests.c +++ b/unittests.c @@ -1,3 +1,7 @@ +#define _POSIX_C_SOURCE 200112L + +#include <setjmp.h> +#include <signal.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -5,6 +9,13 @@ #include "dynstr.h" +#define FAIL(msg) \ + do { \ + printf("FAIL: %s\n", msg); \ + return false; \ + } while (0) + + #define FAIL_UNLESS_EQUAL(a,b) \ do { \ if ((a) != (b)) { \ @@ -31,6 +42,32 @@ static int test_sets_error_handler(void) } +static jmp_buf env; +static void abort_handler(int signo) +{ + longjmp(env, 1); +} + + +static int test_abort_handler_calls_abort(void) +{ + struct sigaction act; + struct sigaction oldact; + + if (setjmp(env) == 0) { + act.sa_handler = abort_handler; + act.sa_flags = 0; + sigaction(SIGABRT, &act, &oldact); + dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); + dynstr_malloc_error_abort(0, 0, 0); + FAIL("dynstr_malloc_error_abort returned"); + } else { + sigaction(SIGABRT, &oldact, NULL); + } + return true; +} + + struct test { const char *name; int (*test)(void); @@ -42,6 +79,7 @@ struct test { static const struct test tests[] = { TEST(test_default_error_handler_is_indicate), TEST(test_sets_error_handler), + TEST(test_abort_handler_calls_abort), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); |