From 5b613a5cc318c14c1f85b1d46cc3569680420dc1 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 26 Jan 2017 12:45:17 +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 | 12 ++ 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/data/valid-config-files/auth-invalid.conf.in | 15 ++ test/data/valid-config-files/auth-sha1.conf.in | 15 ++ test/test-auth.c | 173 +++++++++++++++++++++ 8 files changed, 260 insertions(+) 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/data/valid-config-files/auth-invalid.conf.in create mode 100644 test/data/valid-config-files/auth-sha1.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..c29a379 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 \ @@ -389,6 +396,11 @@ in_data = \ data/systemd-activation/com.example.SystemdActivatable3.service.in \ data/valid-config-files-system/debug-allow-all-fail.conf.in \ data/valid-config-files-system/debug-allow-all-pass.conf.in \ + data/invalid-config-files/auth-invalid.conf.in \ + data/valid-config-files/auth-sha1.conf.in \ + data/valid-config-files/auth-anonymous.conf.in \ + data/valid-config-files/auth-external.conf.in \ + data/valid-config-files/auth-all.conf.in \ data/valid-config-files/debug-allow-all-sha1.conf.in \ data/valid-config-files/debug-allow-all.conf.in \ data/valid-config-files/finite-timeout.conf.in \ 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/data/valid-config-files/auth-invalid.conf.in b/test/data/valid-config-files/auth-invalid.conf.in new file mode 100644 index 0000000..34d3d18 --- /dev/null +++ b/test/data/valid-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-sha1.conf.in b/test/data/valid-config-files/auth-sha1.conf.in new file mode 100644 index 0000000..6d63d84 --- /dev/null +++ b/test/data/valid-config-files/auth-sha1.conf.in @@ -0,0 +1,15 @@ + + + + + @TEST_LISTEN@ + DBUS_COOKIE_SHA1 + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/test-auth.c b/test/test-auth.c new file mode 100644 index 0000000..7b59685 --- /dev/null +++ b/test/test-auth.c @@ -0,0 +1,173 @@ +/* test case for dbus daemon auth configuration + * + * Author: Ralf Habacker + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include + +#include + +#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, "valid-config-files/auth-invalid.conf", + SPECIFY_ADDRESS +}; + +static Config config_external = { + "99512", 1, "valid-config-files/auth-external.conf", + SPECIFY_ADDRESS +}; + +static Config config_sha1 = { + "99512", 1, "valid-config-files/auth-sha1.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/external", Fixture, &config_external, setup, test_external, teardown); + g_test_add ("/auth/sha1", Fixture, &config_sha1, setup, test_anonymous, teardown); + g_test_add ("/auth/anonymous", Fixture, &config_anonymous, setup, test_anonymous, teardown); + g_test_add ("/auth/all", Fixture, &config_all, setup, test_all, teardown); + + return g_test_run (); + + return 0; +} -- 1.8.4.5