summaryrefslogtreecommitdiffstats
path: root/src/lib/test-mempool-alloconly.c
blob: c3b0001b7955b11dae5e114970614afbd51f627c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"

static bool mem_has_bytes(const void *mem, size_t size, uint8_t b)
{
	const uint8_t *bytes = mem;
	unsigned int i;

	for (i = 0; i < size; i++) {
		if (bytes[i] != b)
			return FALSE;
	}
	return TRUE;
}

void test_mempool_alloconly(void)
{
#define SENTRY_SIZE 32
#define SENTRY_CHAR 0xDE
#define PMALLOC_MAX_COUNT 128
	pool_t pool;
	unsigned int i, j, k;
	void *mem[PMALLOC_MAX_COUNT + 1];
	char *sentry;

	test_begin("mempool_alloconly");
	for (i = 0; i < 64; i++) {
		for (j = 1; j <= 128; j++) {
			pool = pool_alloconly_create(MEMPOOL_GROWING"test", i);
			/* make sure p_malloc() doesn't overwrite unallocated
			   data in data stack. parts of the code relies on
			   this. */
			sentry = t_buffer_get(SENTRY_SIZE);
			memset(sentry, SENTRY_CHAR, SENTRY_SIZE);

			mem[0] = p_malloc(pool, j);
			memset(mem[0], j, j);

			for (k = 1; k <= PMALLOC_MAX_COUNT; k++) {
				mem[k] = p_malloc(pool, k);
				memset(mem[k], k, k);
			}
			test_assert(mem_has_bytes(sentry, SENTRY_SIZE, SENTRY_CHAR));
			test_assert(t_buffer_get(SENTRY_SIZE) == sentry);

			test_assert(mem_has_bytes(mem[0], j, j));
			for (k = 1; k <= PMALLOC_MAX_COUNT; k++)
				test_assert(mem_has_bytes(mem[k], k, k));
			pool_unref(&pool);
		}
	}
	test_end();
}

enum fatal_test_state fatal_mempool_alloconly(unsigned int stage)
{
	static pool_t pool;

	if (pool == NULL && stage != 0)
		return FATAL_TEST_FAILURE;

	switch(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, POOL_MAX_ALLOC_SIZE + 1ULL);
		return FATAL_TEST_FAILURE;

#ifdef _LP64 /* malloc(POOL_MAX_ALLOC_SIZE) may succeed with 32bit */
	case 2: /* physically impossible size */
		test_expect_fatal_string("Out of memory");
		(void)p_malloc(pool, POOL_MAX_ALLOC_SIZE);
		return FATAL_TEST_FAILURE;
#endif

	/* Continue with other tests as follows:
	case 3:
		something_fatal();
		return FATAL_TEST_FAILURE;
	*/
	}

	/* Either our tests have finished, or the test suite has got confused. */
	pool_unref(&pool);
	test_end();
	return FATAL_TEST_FINISHED;
}