Skip to content

Commit

Permalink
lib: Use test_expect_fatal_string() for all fatal unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Nov 27, 2017
1 parent a7d1bd5 commit ba1a5db
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/lib/test-array.c
Expand Up @@ -304,6 +304,7 @@ enum fatal_test_state fatal_array(unsigned int stage)
test_begin("fatal_array");
t_array_init(&ad, 3);
/* allocation big enough, but memory not initialised */
test_expect_fatal_string("(array_idx_i): assertion failed: (idx * array->element_size < array->buffer->used)");
useless_ptr = array_idx(&ad, 0);
return FATAL_TEST_FAILURE;
}
Expand All @@ -313,6 +314,7 @@ enum fatal_test_state fatal_array(unsigned int stage)
t_array_init(&ad, 2);
array_append(&ad, tmpd, 2);
/* actual out of range address requested */
test_expect_fatal_string("(array_idx_i): assertion failed: (idx * array->element_size < array->buffer->used)");
useless_ptr = array_idx(&ad, 2);
return FATAL_TEST_FAILURE;
}
Expand All @@ -323,6 +325,8 @@ enum fatal_test_state fatal_array(unsigned int stage)
t_array_init(&ad, 2);
t_array_init(&as, 8);
array_append(&as, tmps, 2);
/* can't copy different array sizes */
test_expect_fatal_string("(array_copy): assertion failed: (dest->element_size == src->element_size)");
array_copy(&ad.arr, 1, &as.arr, 0, 4);
return FATAL_TEST_FAILURE;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/test-data-stack.c
Expand Up @@ -193,6 +193,7 @@ enum fatal_test_state fatal_data_stack(unsigned int stage)
undo_data = *undo_ptr;
*undo_ptr = '*';
/* t_malloc_no0 will panic block header corruption */
test_expect_fatal_string("Corrupted data stack canary");
(void)t_malloc_no0(10);
return FATAL_TEST_FAILURE;
}
Expand All @@ -205,6 +206,7 @@ enum fatal_test_state fatal_data_stack(unsigned int stage)
undo_data = *undo_ptr;
*undo_ptr = '*';
/* t_pop will now fail */
test_expect_fatal_string("buffer overflow");
(void)t_pop(&t_id);
t_id = NONEXISTENT_STACK_FRAME_ID; /* We're FUBAR, mustn't pop next entry */
return FATAL_TEST_FAILURE;
Expand All @@ -218,6 +220,7 @@ enum fatal_test_state fatal_data_stack(unsigned int stage)
undo_data = *undo_ptr;
*undo_ptr = '*';
/* t_pop will now fail */
test_expect_fatal_string("buffer overflow");
(void)t_pop(&t_id);
t_id = NONEXISTENT_STACK_FRAME_ID; /* We're FUBAR, mustn't pop next entry */
return FATAL_TEST_FAILURE;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/test-malloc-overflow.c
Expand Up @@ -55,6 +55,7 @@ static enum fatal_test_state fatal_malloc_overflow_multiply(unsigned int *stage)
};
unsigned int i;

test_expect_fatal_string("memory allocation overflow");
switch (*stage) {
case 0:
test_begin("MALLOC_MULTIPLY() overflows");
Expand All @@ -70,6 +71,7 @@ static enum fatal_test_state fatal_malloc_overflow_multiply(unsigned int *stage)
*stage -= N_ELEMENTS(mul_tests)*2;
if (*stage == 0)
test_end();
test_expect_fatal_string(NULL);
return FATAL_TEST_FINISHED;
}
i = *stage / 2;
Expand All @@ -91,6 +93,7 @@ static enum fatal_test_state fatal_malloc_overflow_add(unsigned int *stage)
};
unsigned int i;

test_expect_fatal_string("memory allocation overflow");
switch (*stage) {
case 0:
test_begin("MALLOC_ADD() overflows");
Expand All @@ -106,6 +109,7 @@ static enum fatal_test_state fatal_malloc_overflow_add(unsigned int *stage)
*stage -= N_ELEMENTS(add_tests)*2;
if (*stage == 0)
test_end();
test_expect_fatal_string(NULL);
return FATAL_TEST_FINISHED;
}
i = *stage / 2;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/test-mempool-allocfree.c
Expand Up @@ -105,14 +105,17 @@ enum fatal_test_state fatal_mempool_allocfree(unsigned int stage)
case 0: /* forbidden size */
test_begin("fatal_mempool_allocfree");
pool = pool_allocfree_create("fatal");
test_expect_fatal_string("Trying to allocate 0 bytes");
(void)p_malloc(pool, 0);
return FATAL_TEST_FAILURE;

case 1: /* logically impossible size */
test_expect_fatal_string("Trying to allocate");
(void)p_malloc(pool, SSIZE_T_MAX + 1ULL);
return FATAL_TEST_FAILURE;

case 2: /* physically impossible size */
test_expect_fatal_string("Out of memory");
(void)p_malloc(pool, SSIZE_T_MAX - 1024);
return FATAL_TEST_FAILURE;

Expand Down
3 changes: 3 additions & 0 deletions src/lib/test-mempool-alloconly.c
Expand Up @@ -64,14 +64,17 @@ enum fatal_test_state fatal_mempool_alloconly(unsigned int stage)
case 0: /* forbidden size */
test_begin("fatal_mempool_alloconly");
pool = pool_alloconly_create(MEMPOOL_GROWING"fatal", 1);
test_expect_fatal_string("Trying to allocate 0 bytes");
(void)p_malloc(pool, 0);
return FATAL_TEST_FAILURE;

case 1: /* logically impossible size */
test_expect_fatal_string("Trying to allocate");
(void)p_malloc(pool, SSIZE_T_MAX + 1ULL);
return FATAL_TEST_FAILURE;

case 2: /* physically impossible size */
test_expect_fatal_string("Out of memory");
(void)p_malloc(pool, SSIZE_T_MAX - 1024);
return FATAL_TEST_FAILURE;

Expand Down
2 changes: 2 additions & 0 deletions src/lib/test-mempool.c
Expand Up @@ -58,6 +58,7 @@ enum fatal_test_state fatal_mempool(unsigned int stage)
static uint32max_array_t *m1;
static uint32_t *m2;

test_expect_fatal_string("memory allocation overflow");
#if SIZEOF_VOID_P == 8
switch(stage) {
case 0:
Expand Down Expand Up @@ -105,6 +106,7 @@ enum fatal_test_state fatal_mempool(unsigned int stage)
#else
# error unsupported pointer size
#endif
test_expect_fatal_string(NULL);
test_end();
return FATAL_TEST_FINISHED;
}
Expand Down
36 changes: 20 additions & 16 deletions src/lib/test-printf-format-fix.c
Expand Up @@ -106,21 +106,24 @@ void test_printf_format_fix()
/* Want to test the panics too? go for it! */
enum fatal_test_state fatal_printf_format_fix(unsigned int stage)
{
static const char *fatals[] = {
"no no no %n's",
"no no no %-1234567890123n's with extra stuff",
"%m allowed once, but not twice: %m",
"%m must not obscure a later %n",
"definitely can't have a tailing %",
"Evil %**%n",
"Evil %*#%99999$s",
"No weird %% with %0%",
"No duplicate modifiers %00s",
"Minimum length can't be too long %10000s",
"Minimum length doesn't support %*1$s",
"Precision can't be too long %.10000s",
"Precision can't be too long %1.10000s",
"Precision doesn't support %1.-1s",
static const struct {
const char *format;
const char *expected_fatal;
} fatals[] = {
{ "no no no %n's", "%n modifier used" },
{ "no no no %-1234567890123n's with extra stuff", "Too large minimum field width" },
{ "%m allowed once, but not twice: %m", "%m used twice" },
{ "%m must not obscure a later %n", "%n modifier used" },
{ "definitely can't have a tailing %", "Missing % specifier" },
{ "Evil %**%n", "Unsupported 0x2a specifier" },
{ "Evil %*#%99999$s", "Unsupported 0x23 specifier" },
{ "No weird %% with %0%", "Unsupported 0x25 specifier" },
{ "No duplicate modifiers %00s", "Duplicate % flag '0'" },
{ "Minimum length can't be too long %10000s", "Too large minimum field width" },
{ "Minimum length doesn't support %*1$s", "Unsupported 0x31 specifier" },
{ "Precision can't be too long %.10000s", "Too large precision" },
{ "Precision can't be too long %1.10000s", "Too large precision" },
{ "Precision doesn't support %1.-1s", "Unsupported 0x2d specifier" },
};

if(stage >= N_ELEMENTS(fatals)) {
Expand All @@ -132,6 +135,7 @@ enum fatal_test_state fatal_printf_format_fix(unsigned int stage)
test_begin("fatal_printf_format_fix");

/* let's crash! */
(void)printf_format_fix(fatals[stage]);
test_expect_fatal_string(fatals[stage].expected_fatal);
(void)printf_format_fix(fatals[stage].format);
return FATAL_TEST_FAILURE;
}

0 comments on commit ba1a5db

Please sign in to comment.