]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-auth: add unit test for auth_user_info_export
authorMarkus Valentin <markus.valentin@open-xchange.com>
Mon, 17 Jun 2019 09:22:45 +0000 (11:22 +0200)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Tue, 10 Sep 2019 07:01:42 +0000 (10:01 +0300)
The test verifies that fields are ending up in the auth request as
expected given a corresponding auth_user_info struct.

src/lib-auth/Makefile.am
src/lib-auth/test-auth-master.c [new file with mode: 0644]

index 13c42a031405c9e17d118407ecb21dc3ae063023..4aff5f8442ea40b075ef2ae342866e540d84c191 100644 (file)
@@ -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 (file)
index 0000000..93ff417
--- /dev/null
@@ -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);
+}