# HG changeset patch # User Aki Tuomi # Date 1493372977 -10800 # Node ID 220da21b7f8030ba0c70e88e6fe1abe344e4cca0 # Parent 2d5018651d64eb1eef5b620623189dc23bdc58e1 auth: Add test suite for username filter diff -r 2d5018651d64 -r 220da21b7f80 src/auth/Makefile.am --- a/src/auth/Makefile.am Fri Apr 28 10:25:14 2017 +0300 +++ b/src/auth/Makefile.am Fri Apr 28 12:49:37 2017 +0300 @@ -226,6 +226,7 @@ test_programs = \ test-auth-cache \ test-auth-request-var-expand \ + test-username-filter \ test-db-dict noinst_PROGRAMS = $(test_programs) @@ -244,6 +245,10 @@ 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) diff -r 2d5018651d64 -r 220da21b7f80 src/auth/test-username-filter.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/auth/test-username-filter.c Fri Apr 28 12:49:37 2017 +0300 @@ -0,0 +1,68 @@ +/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "test-common.h" +#include "auth-common.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) +{ + const struct { + const char *filter; + const char *input; + bool accepted; + } cases[] = { + { "", "", TRUE }, + { "*", "", TRUE }, + { "", "testuser1", TRUE }, + { "*", "testuser1", TRUE }, + { "!*", "testuser1", FALSE }, + { "!*", "", FALSE }, + { "*@*", "", FALSE }, + { "*@*", "@", TRUE }, + { "!*@*", "@", FALSE }, + { "!*@*", "", TRUE }, + { "*@*", "testuser1", FALSE }, + { "!*@*", "testuser1", TRUE }, + { "*@*", "testuser1@testdomain", TRUE }, + { "!*@*", "testuser1@testdomain", FALSE }, + { "*@testdomain *@testdomain2", "testuser1@testdomain", TRUE }, + { "*@testdomain *@testdomain2", "testuser1@testdomain2", TRUE }, + { "*@testdomain *@testdomain2", "testuser1@testdomain3", FALSE }, + { "!testuser@testdomain *@testdomain", "testuser@testdomain", FALSE }, + { "!testuser@testdomain *@testdomain", "testuser2@testdomain", TRUE }, + { "*@testdomain !testuser@testdomain !testuser2@testdomain", "testuser@testdomain", FALSE }, + { "*@testdomain !testuser@testdomain !testuser2@testdomain", "testuser3@testdomain", TRUE }, + { "!testuser@testdomain !testuser2@testdomain", "testuser", TRUE }, + { "!testuser@testdomain !testuser2@testdomain", "testuser@testdomain", FALSE }, + { "!testuser@testdomain *@testdomain !testuser2@testdomain", "testuser3@testdomain", TRUE }, + { "!testuser@testdomain *@testdomain !testuser2@testdomain", "testuser@testdomain", FALSE }, + }; + + test_begin("test username_filter"); + + for(size_t i = 0; i < N_ELEMENTS(cases); i++) { + const char *const *filter = t_strsplit_spaces(cases[i].filter, " ,"); + test_assert_idx(auth_request_username_accepted(filter, cases[i].input) == cases[i].accepted, i); + } + + test_end(); +} + +int main(void) +{ + static void (*test_functions[])(void) = { + test_username_filter, + NULL + }; + + return test_run(test_functions); +}