From: Markus Valentin Date: Mon, 17 Jun 2019 09:22:45 +0000 (+0200) Subject: lib-auth: add unit test for auth_user_info_export X-Git-Tag: 2.3.8~215 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c66144b3251136b0642fda612762173fef523a66;p=thirdparty%2Fdovecot%2Fcore.git lib-auth: add unit test for auth_user_info_export The test verifies that fields are ending up in the auth request as expected given a corresponding auth_user_info struct. --- diff --git a/src/lib-auth/Makefile.am b/src/lib-auth/Makefile.am index 13c42a0314..4aff5f8442 100644 --- a/src/lib-auth/Makefile.am +++ b/src/lib-auth/Makefile.am @@ -2,7 +2,8 @@ noinst_LTLIBRARIES = libauth.la AM_CPPFLAGS = \ -I$(top_srcdir)/src/lib \ - -I$(top_srcdir)/src/lib-master + -I$(top_srcdir)/src/lib-master \ + -I$(top_srcdir)/src/lib-test libauth_la_SOURCES = \ auth-client.c \ @@ -18,3 +19,28 @@ headers = \ pkginc_libdir=$(pkgincludedir) pkginc_lib_HEADERS = $(headers) + +test_programs = \ + test-auth-master + +noinst_PROGRAMS = $(test_programs) + +test_libs = \ + $(noinst_LTLIBRARIES) \ + ../lib-test/libtest.la \ + ../lib/liblib.la \ + $(MODULE_LIBS) + +test_deps = \ + $(noinst_LTLIBRARIES) \ + ../lib-test/libtest.la \ + ../lib/liblib.la + +test_auth_master_SOURCES = test-auth-master.c +test_auth_master_LDADD = $(test_libs) +test_auth_master_DEPENDENCIES = $(test_deps) + +check-local: + for bin in $(test_programs); do \ + if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \ + done diff --git a/src/lib-auth/test-auth-master.c b/src/lib-auth/test-auth-master.c new file mode 100644 index 0000000000..93ff41751f --- /dev/null +++ b/src/lib-auth/test-auth-master.c @@ -0,0 +1,74 @@ +/* Copyright (c) 2019 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "auth-master.h" +#include "net.h" +#include "test-common.h" +#include "str.h" + +static void test_auth_user_info_export(void) +{ + string_t *str; + struct auth_user_info info; + + i_zero(&info); + + test_begin("auth_user_info_export()"); + + /* Setup info for auth_user_info_export call where the + * resulting auth request string should contain all + * real_ variables. */ + net_addr2ip("192.168.1.1", &info.local_ip); + net_addr2ip("192.23.42.9", &info.real_local_ip); + net_addr2ip("10.42.3.223", &info.remote_ip); + net_addr2ip("192.168.1.2", &info.real_remote_ip); + info.local_port = 57035; + info.remote_port = 53075; + info.real_remote_port = 64385; + info.real_local_port = 57391; + + str = t_str_new(128); + auth_user_info_export(str, &info); + + test_assert(strstr(str_c(str), "real_rip=192.168.1.2") != NULL); + test_assert(strstr(str_c(str), "real_lip=192.23.42.9") != NULL); + test_assert(strstr(str_c(str), "rip=10.42.3.223") != NULL); + test_assert(strstr(str_c(str), "lip=192.168.1.1") != NULL); + test_assert(strstr(str_c(str), "real_rport=64385") != NULL); + test_assert(strstr(str_c(str), "rport=53075") != NULL); + test_assert(strstr(str_c(str), "real_lport=57391") != NULL); + test_assert(strstr(str_c(str), "lport=57035") != NULL); + + /* Setup info for auth_user_info_export call where the + * resulting auth request string should not contain any + * real_ variables. */ + net_addr2ip("10.42.3.223", &info.real_remote_ip); + net_addr2ip("192.168.1.1", &info.real_local_ip); + info.real_remote_port = 53075; + info.real_local_port = 57035; + + str_truncate(str, 0); + auth_user_info_export(str, &info); + + test_assert(strstr(str_c(str), "rip=10.42.3.223") != NULL); + test_assert(strstr(str_c(str), "lip=192.168.1.1") != NULL); + test_assert(strstr(str_c(str), "lport=57035") != NULL); + test_assert(strstr(str_c(str), "rport=53075") != NULL); + /* The following fields should not be part of the string as + * they are matching with their non-real counterparts */ + test_assert(strstr(str_c(str), "real_lport") == NULL); + test_assert(strstr(str_c(str), "real_rport") == NULL); + test_assert(strstr(str_c(str), "real_rip") == NULL); + test_assert(strstr(str_c(str), "real_lip") == NULL); + + test_end(); +} + +int main(void) +{ + static void (*const test_functions[])(void) = { + test_auth_user_info_export, + NULL + }; + return test_run(test_functions); +}