summaryrefslogtreecommitdiffstats
path: root/src/director/test-user-directory.c
blob: 88f40a895996e0db0f07d2715a8266b64add8e75 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "ioloop.h"
#include "mail-user-hash.h"
#include "mail-host.h"
#include "test-common.h"


#define USER_DIR_TIMEOUT 1000000

bool mail_user_hash(const char *username ATTR_UNUSED,
		    const char *format ATTR_UNUSED,
		    unsigned int *hash_r, const char **error_r ATTR_UNUSED)
{
	*hash_r = 0;
	return TRUE;
}

static void
verify_user_directory(struct user_directory *dir, unsigned int user_count)
{
	struct user_directory_iter *iter;
	struct user *user, *prev = NULL;
	unsigned int prev_stamp = 0, iter_count = 0;

	iter = user_directory_iter_init(dir, FALSE);
	while ((user = user_directory_iter_next(iter)) != NULL) {
		test_assert(prev_stamp <= user->timestamp);
		test_assert(user->prev == prev);
		test_assert(prev == NULL || user->prev->next == user);

		iter_count++;
		prev = user;
	}
	test_assert(prev == NULL || prev->next == NULL);
	user_directory_iter_deinit(&iter);
	test_assert(iter_count == user_count);
}

static void test_user_directory_ascending(void)
{
	const unsigned int count = 100000;
	struct user_directory *dir;
	struct mail_host *host = t_new(struct mail_host, 1);
	unsigned int i;

	test_begin("user directory ascending");
	dir = user_directory_init(NULL, USER_DIR_TIMEOUT, NULL);
	(void)user_directory_add(dir, 1, host, ioloop_time + count+1);

	for (i = 0; i < count; i++)
		(void)user_directory_add(dir, i+2, host, ioloop_time + i);
	verify_user_directory(dir, count+1);
	user_directory_deinit(&dir);
	test_end();
}

static void test_user_directory_descending(void)
{
	const unsigned int count = 1000;
	struct user_directory *dir;
	struct mail_host *host = t_new(struct mail_host, 1);
	unsigned int i;

	test_begin("user directory descending");
	dir = user_directory_init(NULL, USER_DIR_TIMEOUT, NULL);

	for (i = 0; i < count; i++)
		(void)user_directory_add(dir, i+1, host, ioloop_time - i);
	verify_user_directory(dir, count);
	user_directory_deinit(&dir);
	test_end();
}

static void test_user_directory_random(void)
{
	struct user_directory *dir;
	struct mail_host *host = t_new(struct mail_host, 1);
	time_t timestamp;
	unsigned int i, count = i_rand_minmax(10000, 19999);

	test_begin("user directory random");
	dir = user_directory_init(NULL, USER_DIR_TIMEOUT, NULL);
	for (i = 0; i < count; i++) {
		if (i_rand_limit(10) == 0)
			timestamp = ioloop_time;
		else
			timestamp = ioloop_time-i_rand_limit(100);
		(void)user_directory_add(dir, i+1, host, timestamp);
	}
	verify_user_directory(dir, count);
	user_directory_deinit(&dir);
	test_end();
}

int main(void)
{
	static void (*const test_functions[])(void) = {
		test_user_directory_ascending,
		test_user_directory_descending,
		test_user_directory_random,
		NULL
	};
	struct ioloop *ioloop = io_loop_create();
	int ret = test_run(test_functions);
	io_loop_destroy(&ioloop);
	return ret;
}