Skip to content

Commit

Permalink
lib-test: Add test_expect_fatal_string()
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Nov 27, 2017
1 parent a9a4210 commit 37bf0a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/lib-test/test-common.c
Expand Up @@ -20,7 +20,7 @@ static bool test_success;
static unsigned int failure_count;
static unsigned int total_count;
static unsigned int expected_errors;
static char *expected_error_str;
static char *expected_error_str, *expected_fatal_str;

void test_begin(const char *name)
{
Expand Down Expand Up @@ -144,7 +144,7 @@ test_expect_no_more_errors(void)
expected_errors = 0;
}

static bool
static bool ATTR_FORMAT(2, 0)
expect_error_check(char **error_strp, const char *format, va_list args)
{
if (*error_strp == NULL)
Expand Down Expand Up @@ -182,15 +182,31 @@ test_error_handler(const struct failure_context *ctx,
}
}

void test_expect_fatal_string(const char *substr)
{
i_free(expected_fatal_str);
expected_fatal_str = i_strdup(substr);
}

static void ATTR_FORMAT(2, 0) ATTR_NORETURN
test_fatal_handler(const struct failure_context *ctx ATTR_UNUSED,
const char *format ATTR_UNUSED, va_list args ATTR_UNUSED)
test_fatal_handler(const struct failure_context *ctx,
const char *format, va_list args)
{
/* Prevent recursion, we can't handle our own errors */
i_set_fatal_handler(default_fatal_handler);
i_assert(expecting_fatal); /* if not at the right time, bail */
i_set_fatal_handler(test_fatal_handler);
longjmp(fatal_jmpbuf, 1);

va_list args2;
VA_COPY(args2, args);
bool suppress = expect_error_check(&expected_fatal_str, format, args2);
va_end(args);

if (suppress) {
i_set_fatal_handler(test_fatal_handler);
longjmp(fatal_jmpbuf, 1);
} else {
default_fatal_handler(ctx, format, args);
}
i_unreached(); /* we simply can't get here */
}

Expand Down Expand Up @@ -328,6 +344,7 @@ void ATTR_NORETURN
test_exit(int status)
{
i_free_and_null(expected_error_str);
i_free_and_null(expected_fatal_str);
i_free_and_null(test_prefix);
t_pop_last_unsafe(); /* as we were within a T_BEGIN { tests[i].func(); } T_END */
lib_deinit();
Expand Down
3 changes: 3 additions & 0 deletions src/lib-test/test-common.h
Expand Up @@ -83,6 +83,9 @@ int test_run_with_fatals(void (*const test_functions[])(void),
int test_run_named_with_fatals(const char *match, const struct named_test tests[],
const struct named_fatal fatals[]);

/* Require the Fatal/Panic string to match this or the fatal test fails. */
void test_expect_fatal_string(const char *substr);

#define FATAL_DECL(x) enum fatal_test_state x(unsigned int);
#define FATAL_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */
#define FATAL_NAMED(x) { .name = #x , .func = x },
Expand Down

0 comments on commit 37bf0a2

Please sign in to comment.