bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "lib.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "ioloop.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "mail-user-hash.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "mail-host.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "test-common.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#define USER_DIR_TIMEOUT 1000000
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainenbool mail_user_hash(const char *username ATTR_UNUSED,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char *format ATTR_UNUSED,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen unsigned int *hash_r, const char **error_r ATTR_UNUSED)
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen{
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen *hash_r = 0;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return TRUE;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen}
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenstatic void
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenverify_user_directory(struct user_directory *dir, unsigned int user_count)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen{
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct user_directory_iter *iter;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct user *user, *prev = NULL;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen unsigned int prev_stamp = 0, iter_count = 0;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
14660f677e16a5c36f3c43e9e64f5e021fda627bTimo Sirainen iter = user_directory_iter_init(dir, FALSE);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen while ((user = user_directory_iter_next(iter)) != NULL) {
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_assert(prev_stamp <= user->timestamp);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_assert(user->prev == prev);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_assert(prev == NULL || user->prev->next == user);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen iter_count++;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen prev = user;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen }
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_assert(prev == NULL || prev->next == NULL);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen user_directory_iter_deinit(&iter);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_assert(iter_count == user_count);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenstatic void test_user_directory_ascending(void)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen{
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen const unsigned int count = 100000;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct user_directory *dir;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct mail_host *host = t_new(struct mail_host, 1);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen unsigned int i;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_begin("user directory ascending");
de92873c366becfaea1554642f89b9169d7955e2Timo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)user_directory_add(dir, 1, host, ioloop_time + count+1);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen for (i = 0; i < count; i++)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)user_directory_add(dir, i+2, host, ioloop_time + i);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen verify_user_directory(dir, count+1);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen user_directory_deinit(&dir);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_end();
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenstatic void test_user_directory_descending(void)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen{
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen const unsigned int count = 1000;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct user_directory *dir;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct mail_host *host = t_new(struct mail_host, 1);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen unsigned int i;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_begin("user directory descending");
de92873c366becfaea1554642f89b9169d7955e2Timo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen for (i = 0; i < count; i++)
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)user_directory_add(dir, i+1, host, ioloop_time - i);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen verify_user_directory(dir, count);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen user_directory_deinit(&dir);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_end();
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenstatic void test_user_directory_random(void)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen{
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct user_directory *dir;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen struct mail_host *host = t_new(struct mail_host, 1);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen time_t timestamp;
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek unsigned int i, count = i_rand_minmax(10000, 19999);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_begin("user directory random");
de92873c366becfaea1554642f89b9169d7955e2Timo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen for (i = 0; i < count; i++) {
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek if (i_rand_limit(10) == 0)
32a93320fd2b6ada5ac8027166819463c1a007b6Timo Sirainen timestamp = ioloop_time;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen else
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek timestamp = ioloop_time-i_rand_limit(100);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen (void)user_directory_add(dir, i+1, host, timestamp);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen }
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen verify_user_directory(dir, count);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen user_directory_deinit(&dir);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_end();
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenint main(void)
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_user_directory_ascending,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_user_directory_descending,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_user_directory_random,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen NULL
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen };
8017d6c67481d2bcfa1ba1fe229939a0ba8b56e9Timo Sirainen struct ioloop *ioloop = io_loop_create();
8017d6c67481d2bcfa1ba1fe229939a0ba8b56e9Timo Sirainen int ret = test_run(test_functions);
8017d6c67481d2bcfa1ba1fe229939a0ba8b56e9Timo Sirainen io_loop_destroy(&ioloop);
8017d6c67481d2bcfa1ba1fe229939a0ba8b56e9Timo Sirainen return ret;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}