From: Aki Tuomi Date: Fri, 16 Feb 2024 12:35:45 +0000 (+0200) Subject: auth: Add auth-client-connection test X-Git-Tag: 2.4.0~1726 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9d16fcf7ec893d2501c6df02fd9ca2c650b9c683;p=thirdparty%2Fdovecot%2Fcore.git auth: Add auth-client-connection test --- diff --git a/src/auth/Makefile.am b/src/auth/Makefile.am index e6e4cd7ed8..ca558d8ff6 100644 --- a/src/auth/Makefile.am +++ b/src/auth/Makefile.am @@ -208,6 +208,8 @@ pkginc_lib_HEADERS = $(headers) test_programs = \ test-auth-cache \ + test-auth-client \ + test-auth-master \ test-auth \ test-mech @@ -245,6 +247,14 @@ test_mech_SOURCES = \ test_mech_LDADD = $(test_libs) $(auth_libs) $(AUTH_LIBS) $(LUA_LIBS) test_mech_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs) +test_auth_client_SOURCES = \ + test-auth.c \ + test-mock.c \ + test-auth-client.c + +test_auth_client_LDADD = $(test_libs) $(auth_libs) $(AUTH_LIBS) $(LUA_LIBS) +test_auth_client_DEPENDENCIES = $(pkglibexec_PROGRAMS) $(test_libs) + check-local: for bin in $(test_programs); do \ if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \ diff --git a/src/auth/test-auth-client.c b/src/auth/test-auth-client.c new file mode 100644 index 0000000000..2254d208f2 --- /dev/null +++ b/src/auth/test-auth-client.c @@ -0,0 +1,99 @@ +/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */ + +#include "test-auth.h" +#include "ioloop.h" +#include "net.h" +#include "master-service.h" +#include "auth-common.h" +#include "auth-client-connection.h" +#include "auth-client.h" + +#define TEST_AUTH_CLIENT_SOCKET "test-auth-client-socket" + +static void auth_client_connected(int *server_fd) +{ + struct auth *auth = auth_default_service(); + int fd = net_accept(*server_fd, NULL, NULL); + auth_client_connection_create(auth, fd, TEST_AUTH_CLIENT_SOCKET, FALSE, FALSE); +} + +static void +test_callback(struct auth_client_request *req, enum auth_request_status status, + const char *b64, const char *const *args, void *context) +{ + struct auth_client *client ATTR_UNUSED = context; + if (status == AUTH_REQUEST_STATUS_CONTINUE) { + test_assert_strcmp(b64, ""); + /* continue with \0testuser\0testpass */ + auth_client_request_continue(req, "AHRlc3R1c2VyAHRlc3RwYXNz"); + return; + } + test_assert_strcmp(args[0], "user=testuser"); + test_assert_cmp(status, ==, AUTH_REQUEST_STATUS_OK); + io_loop_stop(current_ioloop); +} + +static void test_auth_client(void) +{ + test_begin("auth client"); + struct ioloop *loop = io_loop_create(); + test_auth_init(); + + i_unlink_if_exists(TEST_AUTH_CLIENT_SOCKET); + int fd = net_listen_unix(TEST_AUTH_CLIENT_SOCKET, 10); + struct io *io = io_add(fd, IO_READ, auth_client_connected, &fd); + + struct auth_client *client = + auth_client_init(TEST_AUTH_CLIENT_SOCKET, getpid(), FALSE); + auth_client_connect(client); + + while (!auth_client_is_connected(client)) { + io_loop_set_running(current_ioloop); + io_loop_handler_run(current_ioloop); + } + + struct auth_request_info reqinfo = { + .mech = "plain", + .client_id = "1", + .service = "default", + .session_id = "1", + }; + (void)auth_client_request_new(client, &reqinfo, test_callback, client); + + io_loop_run(current_ioloop); + + auth_client_deinit(&client); + io_remove(&io); + auth_client_connections_destroy_all(); + test_auth_deinit(); + io_loop_destroy(&loop); + i_unlink_if_exists(TEST_AUTH_CLIENT_SOCKET); + test_end(); +} + +int main(int argc, char *argv[]) +{ + static void (*const test_functions[])(void) = { + test_auth_client, + NULL + }; + const enum master_service_flags service_flags = + MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS | + MASTER_SERVICE_FLAG_STANDALONE | + MASTER_SERVICE_FLAG_STD_CLIENT | + MASTER_SERVICE_FLAG_DONT_SEND_STATS; + int ret; + + master_service = master_service_init("test-auth-client", + service_flags, &argc, &argv, ""); + + master_service_init_finish(master_service); + + struct ioloop *ioloop = io_loop_create(); + io_loop_set_current(ioloop); + ret = test_run(test_functions); + io_loop_destroy(&ioloop); + + master_service_deinit(&master_service); + return ret; +} diff --git a/src/auth/test-auth.c b/src/auth/test-auth.c index 0ba5f8f49d..5fb4eee7a0 100644 --- a/src/auth/test-auth.c +++ b/src/auth/test-auth.c @@ -5,6 +5,7 @@ #include "settings-parser.h" #include "auth-settings.h" #include "auth-token.h" +#include "auth-penalty.h" #include "mech.h" #include "otp.h" #include "mech-otp-common.h" @@ -60,10 +61,13 @@ void test_auth_init(void) auths_preinit(&test_auth_set, mech_reg, protocols); auths_init(); auth_token_init(); + + auth_penalty = auth_penalty_init("missing"); } void test_auth_deinit(void) { + auth_penalty_deinit(&auth_penalty); mech_otp_deinit(); auths_deinit(); auth_token_deinit();