From 9c4b01ab3de9ebf365f2bc524a0968998ac108c9 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Tue, 24 Jan 2017 09:33:13 +0100 Subject: [PATCH] Add glib based test case for testing dbus auth configuration. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=99512 --- cmake/test/CMakeLists.txt | 1 + test/Makefile.am | 7 + .../data/invalid-config-files/auth-invalid.conf.in | 15 +++ test/data/valid-config-files/auth-all.conf.in | 14 ++ .../data/valid-config-files/auth-anonymous.conf.in | 15 +++ test/data/valid-config-files/auth-external.conf.in | 15 +++ test/test-auth.c | 149 +++++++++++++++++++++ 7 files changed, 216 insertions(+) create mode 100644 test/data/invalid-config-files/auth-invalid.conf.in create mode 100644 test/data/valid-config-files/auth-all.conf.in create mode 100644 test/data/valid-config-files/auth-anonymous.conf.in create mode 100644 test/data/valid-config-files/auth-external.conf.in create mode 100644 test/test-auth.c diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt index 58eed09..2dfa33f 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -109,6 +109,7 @@ if(DBUS_WITH_GLIB) add_test_executable(test-relay ${CMAKE_SOURCE_DIR}/../test/relay.c ${TEST_LIBRARIES}) add_test_executable(test-syntax ${CMAKE_SOURCE_DIR}/../test/syntax.c ${TEST_LIBRARIES}) add_test_executable(test-syslog ${CMAKE_SOURCE_DIR}/../test/internals/syslog.c ${TEST_LIBRARIES}) + add_test_executable(test-auth ${CMAKE_SOURCE_DIR}/../test/test-auth.c ${TEST_LIBRARIES}) add_helper_executable(manual-authz ${CMAKE_SOURCE_DIR}/../test/manual-authz.c ${TEST_LIBRARIES}) endif() diff --git a/test/Makefile.am b/test/Makefile.am index df0ab36..62679cd 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -161,6 +161,7 @@ endif if DBUS_WITH_GLIB installable_tests += \ + test-auth \ test-corrupt \ test-dbus-daemon \ test-dbus-daemon-eavesdrop \ @@ -268,6 +269,12 @@ test_apparmor_activation_LDADD = \ $(NULL) endif +test_auth_SOURCES = test-auth.c +test_auth_LDADD = \ + libdbus-testutils.la \ + $(GLIB_LIBS) \ + $(NULL) + test_corrupt_SOURCES = corrupt.c test_corrupt_LDADD = \ libdbus-testutils.la \ diff --git a/test/data/invalid-config-files/auth-invalid.conf.in b/test/data/invalid-config-files/auth-invalid.conf.in new file mode 100644 index 0000000..19a5112 --- /dev/null +++ b/test/data/invalid-config-files/auth-invalid.conf.in @@ -0,0 +1,15 @@ + + + + + @TEST_LISTEN@ + INVALID + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/auth-all.conf.in b/test/data/valid-config-files/auth-all.conf.in new file mode 100644 index 0000000..adc3aa5 --- /dev/null +++ b/test/data/valid-config-files/auth-all.conf.in @@ -0,0 +1,14 @@ + + + + + @TEST_LISTEN@ + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/auth-anonymous.conf.in b/test/data/valid-config-files/auth-anonymous.conf.in new file mode 100644 index 0000000..4f41c52 --- /dev/null +++ b/test/data/valid-config-files/auth-anonymous.conf.in @@ -0,0 +1,15 @@ + + + + + @TEST_LISTEN@ + ANONYMOUS + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/auth-external.conf.in b/test/data/valid-config-files/auth-external.conf.in new file mode 100644 index 0000000..248263e --- /dev/null +++ b/test/data/valid-config-files/auth-external.conf.in @@ -0,0 +1,15 @@ + + + + + @TEST_LISTEN@ + EXTERNAL + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/test-auth.c b/test/test-auth.c new file mode 100644 index 0000000..9681cd5 --- /dev/null +++ b/test/test-auth.c @@ -0,0 +1,149 @@ +#include + +#include + +#include + +#include +#ifdef G_OS_UNIX +#include +#include +#endif + +#include "test-utils-glib.h" + +typedef struct { + gboolean skip; + DBusError e; + TestMainContext *ctx; + + GPid daemon_pid; + DBusServer *server; + + gchar *address; + + DBusConnection *client_conn; +} Fixture; + +typedef struct { + const char *bug_ref; + guint min_messages; + const char *config_file; + enum { SPECIFY_ADDRESS = 0, RELY_ON_DEFAULT } connect_mode; +} Config; + +static void +setup (Fixture *f, + gconstpointer context) +{ + const Config *config = context; + + f->ctx = test_main_context_get (); + dbus_error_init (&f->e); + + f->address = test_get_dbus_daemon (config ? config->config_file : NULL, + TEST_USER_ME, + &f->daemon_pid); + f->client_conn = 0; +} + +static void +test_invalid (Fixture *f, + gconstpointer context) +{ + g_assert (f->address == NULL); + if (f->address) + f->client_conn = test_connect_to_bus (f->ctx, f->address); + g_assert (f->client_conn == NULL); +} + +static void +test_anonymous (Fixture *f, + gconstpointer context) +{ + g_assert (f->address != NULL); + if (f->address) + f->client_conn = test_connect_to_bus (f->ctx, f->address); + g_assert(f->client_conn != NULL); +} + +static void +test_external (Fixture *f, + gconstpointer context) +{ + g_assert (f->address != NULL); + if (f->address) + f->client_conn = test_connect_to_bus (f->ctx, f->address); + g_assert(f->client_conn != NULL); +} + +static void +test_all (Fixture *f, + gconstpointer context) +{ + g_assert (f->address != NULL); + if (f->address) + f->client_conn = test_connect_to_bus (f->ctx, f->address); + g_assert(f->client_conn != NULL); +} + + +static void +teardown (Fixture *f, + gconstpointer context G_GNUC_UNUSED) +{ + dbus_error_free (&f->e); + + if (f->client_conn != NULL) + { + dbus_connection_close (f->client_conn); + dbus_connection_unref (f->client_conn); + f->client_conn = NULL; + } + + if (f->daemon_pid != 0) + { + test_kill_pid (f->daemon_pid); + g_spawn_close_pid (f->daemon_pid); + f->daemon_pid = 0; + } + + test_main_context_unref (f->ctx); + g_free (f->address); +} + +static Config config_invalid = { + "99512", 1, "invalid-config-files/auth-invalid.conf", + SPECIFY_ADDRESS +}; + +static Config config_external = { + "99512", 1, "valid-config-files/auth-external.conf", + SPECIFY_ADDRESS +}; + +static Config config_anonymous = { + "99512", 1, "valid-config-files/auth-anonymous.conf", + SPECIFY_ADDRESS +}; + +static Config config_all = { + "99512", 1, "valid-config-files/auth-all.conf", + SPECIFY_ADDRESS +}; + +int +main (int argc, + char **argv) +{ + test_init (&argc, &argv); + + g_test_add ("/auth/invalid", Fixture, &config_invalid, setup, test_invalid, teardown); + g_test_add ("/auth/anonymous", Fixture, &config_anonymous, setup, test_anonymous, teardown); + g_test_add ("/auth/external", Fixture, &config_external, setup, test_external, teardown); + g_test_add ("/auth/all", Fixture, &config_all, setup, test_all, teardown); + + return g_test_run (); + + return 0; +} -- 1.8.4.5