test-user-directory.c revision e34d170f8f0e084bd94bfbc1a7085ece67e508df
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen/* Copyright (c) 2012 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 "user-directory.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include "test-common.h"
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#include <stdlib.h>
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen#define USER_DIR_TIMEOUT 1000000
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainenunsigned int mail_user_hash(const char *username ATTR_UNUSED,
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen const char *format ATTR_UNUSED) { return 0; }
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
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen iter = user_directory_iter_init(dir);
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");
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, "%u");
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");
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, "%u");
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;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen unsigned int i, count = 10000 + rand()%10000;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen test_begin("user directory random");
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen dir = user_directory_init(USER_DIR_TIMEOUT, "%u");
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen for (i = 0; i < count; i++) {
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen if (rand() % 10 == 0)
32a93320fd2b6ada5ac8027166819463c1a007b6Timo Sirainen timestamp = ioloop_time;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen else
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen timestamp = ioloop_time-rand()%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{
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen static void (*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 };
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen ioloop_time = 1234567890;
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen return test_run(test_functions);
447bf65ddb82ec279e7386828748ef47e199a6afTimo Sirainen}