test-mempool-alloconly.c revision 8b2839664b4b1730d7d5fd5c1856e97e34934a3d
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen/* Copyright (c) 2007-2014 Dovecot authors, see the included COPYING file */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen#include "test-lib.h"
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic bool mem_has_bytes(const void *mem, size_t size, uint8_t b)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen{
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen const uint8_t *bytes = mem;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen unsigned int i;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (i = 0; i < size; i++) {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen if (bytes[i] != b)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FALSE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return TRUE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen}
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenvoid test_mempool_alloconly(void)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen{
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen#define PMALLOC_MAX_COUNT 128
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen pool_t pool;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen unsigned int i, j, k;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen void *mem[PMALLOC_MAX_COUNT + 1];
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen bool success = TRUE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (i = 0; i < 64; i++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (j = 1; j <= 128; j++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen pool = pool_alloconly_create(MEMPOOL_GROWING"test", i);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mem[0] = p_malloc(pool, j);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen memset(mem[0], j, j);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (k = 1; k <= PMALLOC_MAX_COUNT; k++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mem[k] = p_malloc(pool, k);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen memset(mem[k], k, k);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!mem_has_bytes(mem[0], j, j))
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen success = FALSE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (k = 1; k <= PMALLOC_MAX_COUNT; k++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!mem_has_bytes(mem[k], k, k))
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen success = FALSE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen pool_unref(&pool);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen test_out("mempool_alloconly", success);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen}
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenenum fatal_test_state fatal_mempool(int stage)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen{
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen static pool_t pool;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen switch(stage) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen case 0: /* forbidden size */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen test_begin("fatal_mempool");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen pool = pool_alloconly_create(MEMPOOL_GROWING"fatal", 1);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen (void)p_malloc(pool, 0);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FATAL_TEST_FAILURE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen case 1: /* logically impossible size */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen (void)p_malloc(pool, SSIZE_T_MAX + 1ULL);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FATAL_TEST_FAILURE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen case 2: /* physically impossible size */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen (void)p_malloc(pool, SSIZE_T_MAX - (size_t)MEM_ALIGN(1));
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FATAL_TEST_FAILURE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* Continue with other tests as follows:
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen case 3:
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen something_fatal();
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FATAL_TEST_FAILURE;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen }
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* Either our tests have finished, or the test suite has got confused. */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen pool_unref(&pool);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen test_end();
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen return FATAL_TEST_FINISHED;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen}
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen