]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Use test_expect_fatal_string() for all fatal unit tests
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 27 Nov 2017 13:14:36 +0000 (15:14 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 27 Nov 2017 17:37:10 +0000 (19:37 +0200)
src/lib/test-array.c
src/lib/test-data-stack.c
src/lib/test-malloc-overflow.c
src/lib/test-mempool-allocfree.c
src/lib/test-mempool-alloconly.c
src/lib/test-mempool.c
src/lib/test-printf-format-fix.c

index 52f2f6f80d905be5ca845062aec83821f1ee7373..187c8958a7df5b6441df287b3cf647b32eaa146a 100644 (file)
@@ -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;
        }
index 40eab77d1633b42cdcd48d868aba5ce5c3db1875..fb29e711a4b8da13ba89a06a78928b1bf2027caa 100644 (file)
@@ -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;
index f1786dad75e17a6e102b15a562f7d47e9d394360..07f13d905b03456d38529378894aa4a5e19515e5 100644 (file)
@@ -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;
index ab1e664390a786fd1ff7dc69bcd6aa529037db27..856c18418b39a899f892dcd55b90176ab6ab146b 100644 (file)
@@ -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;
 
index 1f2094db18067b3b6dc25c659ffa9d59d53025e6..d77f2435a4b75e5285a415c8df4d239ebccf4fb8 100644 (file)
@@ -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;
 
index f909f59304d235f23a3c171d60d684f87253cdac..fd98d9943c07f32dce57de225993cfcd27c9209d 100644 (file)
@@ -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;
 }
index fa63da3dbc4b4f9730a6cb3e46882be6826c1c2e..05ea88b6295d8bd5f2ac14bfa4f8391a5c61f566 100644 (file)
@@ -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;
 }