Skip to content

Commit

Permalink
auth: Organize code into two unit tests
Browse files Browse the repository at this point in the history
auth-cache needs special setup
  • Loading branch information
cmouse authored and GitLab committed Apr 28, 2017
1 parent cca398c commit 332e3fd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 63 deletions.
28 changes: 12 additions & 16 deletions src/auth/Makefile.am
Expand Up @@ -224,33 +224,29 @@ libstats_auth_la_SOURCES = auth-stats.c

test_programs = \
test-auth-cache \
test-auth-request-var-expand \
test-username-filter \
test-db-dict
test-auth

noinst_PROGRAMS = $(test_programs)

noinst_HEADERS = test-auth.h

test_libs = \
../lib-test/libtest.la \
../lib/liblib.la
../lib-dovecot/libdovecot.la

test_auth_cache_SOURCES = auth-cache.c test-auth-cache.c
test_auth_cache_LDADD = $(test_libs)
test_auth_cache_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs)
# this is needed to force auth-cache.c recompilation
test_auth_cache_CPPFLAGS = $(AM_CPPFLAGS)

test_auth_request_var_expand_SOURCES = test-auth-request-var-expand.c
test_auth_request_var_expand_LDADD = $(test_libs) libauth.la
test_auth_request_var_expand_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs)

test_username_filter_SOURCES = test-username-filter.c
test_username_filter_LDADD = $(test_libs) $(auth_libs) $(AUTH_LIBS) $(LIBDOVECOT)
test_username_filter_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs) $(LIBDOVECOT_DEPS)

test_db_dict_SOURCES = test-db-dict.c
test_db_dict_LDADD = $(test_libs) libauth.la
test_db_dict_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs)
test_auth_SOURCES = \
test-auth-request-var-expand.c \
test-username-filter.c \
test-db-dict.c \
test-mock.c \
test-main.c
test_auth_LDADD = $(test_libs) $(auth_libs) $(AUTH_LIBS)
test_auth_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs)

check: check-am check-test
check-test: all-am
Expand Down
21 changes: 8 additions & 13 deletions src/auth/test-auth-request-var-expand.c
@@ -1,12 +1,11 @@
/* Copyright (c) 2015-2017 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "test-auth.h"
#include "str.h"
#include "auth.h"
#include "passdb.h"
#include "userdb.h"
#include "auth-request.h"
#include "test-common.h"

static struct passdb_module test_passdb = {
.id = 40
Expand Down Expand Up @@ -235,22 +234,18 @@ static void test_auth_request_var_expand_funcs(void)
test_end();
}

int main(void)
void test_auth_request_var_expand(void)
{
static void (*const test_functions[])(void) = {
test_auth_request_var_expand_shortlong,
test_auth_request_var_expand_flags,
test_auth_request_var_expand_long,
test_auth_request_var_expand_usernames,
test_auth_request_var_expand_funcs,
NULL
};

default_test_request.local_ip.u.ip4.s_addr = htonl(123456789);
default_test_request.remote_ip.u.ip4.s_addr = htonl(1234567890);
default_test_request.real_local_ip.u.ip4.s_addr = htonl(223456788);
default_test_request.real_remote_ip.u.ip4.s_addr = htonl(223456789);

test_request = default_test_request;
return test_run(test_functions);

test_auth_request_var_expand_shortlong();
test_auth_request_var_expand_flags();
test_auth_request_var_expand_long();
test_auth_request_var_expand_usernames();
test_auth_request_var_expand_funcs();
}
14 changes: 14 additions & 0 deletions src/auth/test-auth.h
@@ -0,0 +1,14 @@
/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */

#ifndef TEST_AUTH_H
#define TEST_AUTH_H 1

#include "lib.h"
#include "test-common.h"

void test_auth_request_var_expand(void);
void test_db_dict_parse_cache_key(void);
void test_username_filter(void);

#endif

14 changes: 2 additions & 12 deletions src/auth/test-db-dict.c
@@ -1,11 +1,10 @@
/* Copyright (c) 2013-2017 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "test-auth.h"
#include "array.h"
#include "db-dict.h"
#include "test-common.h"

static void test_db_dict_parse_cache_key(void)
void test_db_dict_parse_cache_key(void)
{
struct db_dict_key keys[] = {
{ "key0", "%d and %n", NULL, NULL, 0 },
Expand Down Expand Up @@ -40,12 +39,3 @@ static void test_db_dict_parse_cache_key(void)
"\t%d and %n\t%l\t%{foo}%r%{bar}\t%{test1}/path\t%{extra}\tpath2/%{test2}\t%{plop}") == 0);
test_end();
}

int main(void)
{
static void (*const test_functions[])(void) = {
test_db_dict_parse_cache_key,
NULL
};
return test_run(test_functions);
}
19 changes: 19 additions & 0 deletions src/auth/test-main.c
@@ -0,0 +1,19 @@
#include "lib.h"
#include "test-common.h"
#include "test-auth.h"

int main(int argc, const char *argv[])
{
const char *match = "";
static const struct named_test test_functions[] = {
TEST_NAMED(test_auth_request_var_expand)
TEST_NAMED(test_db_dict_parse_cache_key)
TEST_NAMED(test_username_filter)
{ NULL, NULL }
};

if (argc > 2 && strcasecmp(argv[1], "--match") == 0)
match = argv[2];

return test_run_named(test_functions, match);
}
11 changes: 11 additions & 0 deletions src/auth/test-mock.c
@@ -0,0 +1,11 @@
#include "lib.h"
#include "auth-common.h"

struct auth_penalty *auth_penalty;
time_t process_start_time;
bool worker, worker_restart_request;
void auth_module_load(const char *names ATTR_UNUSED)
{
}
void auth_refresh_proctitle(void) {
}
24 changes: 2 additions & 22 deletions src/auth/test-username-filter.c
@@ -1,19 +1,9 @@
/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "test-common.h"
#include "auth-common.h"
#include "test-auth.h"
#include "auth-request.h"
struct auth_penalty *auth_penalty;
time_t process_start_time;
bool worker, worker_restart_request;
void auth_module_load(const char *names ATTR_UNUSED)
{
}
void auth_refresh_proctitle(void) {
}

static void test_username_filter(void)
void test_username_filter(void)
{
const struct {
const char *filter;
Expand Down Expand Up @@ -56,13 +46,3 @@ static void test_username_filter(void)

test_end();
}

int main(void)
{
static void (*const test_functions[])(void) = {
test_username_filter,
NULL
};

return test_run(test_functions);
}

0 comments on commit 332e3fd

Please sign in to comment.