From: Timo Sirainen Date: Mon, 27 Nov 2017 13:14:36 +0000 (+0200) Subject: lib: Use test_expect_fatal_string() for all fatal unit tests X-Git-Tag: 2.3.0.rc1~328 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba1a5db879b08d2fefcb42160af67853cdfe7687;p=thirdparty%2Fdovecot%2Fcore.git lib: Use test_expect_fatal_string() for all fatal unit tests --- diff --git a/src/lib/test-array.c b/src/lib/test-array.c index 52f2f6f80d..187c8958a7 100644 --- a/src/lib/test-array.c +++ b/src/lib/test-array.c @@ -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; } @@ -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; } @@ -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; } diff --git a/src/lib/test-data-stack.c b/src/lib/test-data-stack.c index 40eab77d16..fb29e711a4 100644 --- a/src/lib/test-data-stack.c +++ b/src/lib/test-data-stack.c @@ -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; } @@ -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; @@ -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; diff --git a/src/lib/test-malloc-overflow.c b/src/lib/test-malloc-overflow.c index f1786dad75..07f13d905b 100644 --- a/src/lib/test-malloc-overflow.c +++ b/src/lib/test-malloc-overflow.c @@ -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"); @@ -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; @@ -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"); @@ -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; diff --git a/src/lib/test-mempool-allocfree.c b/src/lib/test-mempool-allocfree.c index ab1e664390..856c18418b 100644 --- a/src/lib/test-mempool-allocfree.c +++ b/src/lib/test-mempool-allocfree.c @@ -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; diff --git a/src/lib/test-mempool-alloconly.c b/src/lib/test-mempool-alloconly.c index 1f2094db18..d77f2435a4 100644 --- a/src/lib/test-mempool-alloconly.c +++ b/src/lib/test-mempool-alloconly.c @@ -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; diff --git a/src/lib/test-mempool.c b/src/lib/test-mempool.c index f909f59304..fd98d9943c 100644 --- a/src/lib/test-mempool.c +++ b/src/lib/test-mempool.c @@ -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: @@ -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; } diff --git a/src/lib/test-printf-format-fix.c b/src/lib/test-printf-format-fix.c index fa63da3dbc..05ea88b629 100644 --- a/src/lib/test-printf-format-fix.c +++ b/src/lib/test-printf-format-fix.c @@ -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)) { @@ -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; }