From f5b0bcc02f3868832c8989a523e6dc786c57210f Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 26 Jan 2017 22:40:11 +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 ++ .../debug-allow-anonymous.conf.in | 14 ++ .../valid-config-files/debug-allow-any.conf.in | 12 ++ .../debug-allow-external.conf.in | 13 ++ .../valid-config-files/debug-allow-invalid.conf.in | 13 ++ .../valid-config-files/debug-allow-sha1.conf.in | 13 ++ test/test-auth.c | 175 +++++++++++++++++++++ test/test-utils-glib.c | 39 +++-- test/test-utils-glib.h | 5 + 10 files changed, 287 insertions(+), 10 deletions(-) create mode 100644 test/data/valid-config-files/debug-allow-anonymous.conf.in create mode 100644 test/data/valid-config-files/debug-allow-any.conf.in create mode 100644 test/data/valid-config-files/debug-allow-external.conf.in create mode 100644 test/data/valid-config-files/debug-allow-invalid.conf.in create mode 100644 test/data/valid-config-files/debug-allow-sha1.conf.in create mode 100644 test/test-auth.c diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt index 58eed09..4111c18 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -110,6 +110,7 @@ if(DBUS_WITH_GLIB) 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_helper_executable(manual-authz ${CMAKE_SOURCE_DIR}/../test/manual-authz.c ${TEST_LIBRARIES}) + add_test_executable(test-auth ${CMAKE_SOURCE_DIR}/../test/test-auth.c ${TEST_LIBRARIES}) endif() ### keep these in creation order, i.e. uppermost dirs first diff --git a/test/Makefile.am b/test/Makefile.am index eaf2df8..797fd9d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -156,6 +156,7 @@ endif if DBUS_WITH_GLIB installable_tests += \ + test-auth \ test-corrupt \ test-dbus-daemon \ test-dbus-daemon-eavesdrop \ @@ -231,6 +232,12 @@ manual_authz_LDADD = \ $(GLIB_LIBS) \ $(NULL) +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 \ @@ -344,6 +351,11 @@ endif DBUS_ENABLE_INSTALLED_TESTS in_data = \ data/valid-config-files-system/debug-allow-all-fail.conf.in \ data/valid-config-files-system/debug-allow-all-pass.conf.in \ + data/valid-config-files/debug-allow-invalid.conf.in \ + data/valid-config-files/debug-allow-anonymous.conf.in \ + data/valid-config-files/debug-allow-external.conf.in \ + data/valid-config-files/debug-allow-sha1.conf.in \ + data/valid-config-files/debug-allow-any.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/debug-allow-anonymous.conf.in b/test/data/valid-config-files/debug-allow-anonymous.conf.in new file mode 100644 index 0000000..e433391 --- /dev/null +++ b/test/data/valid-config-files/debug-allow-anonymous.conf.in @@ -0,0 +1,14 @@ + + + @TEST_LISTEN@ + ANONYMOUS + + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/debug-allow-any.conf.in b/test/data/valid-config-files/debug-allow-any.conf.in new file mode 100644 index 0000000..b4bab47 --- /dev/null +++ b/test/data/valid-config-files/debug-allow-any.conf.in @@ -0,0 +1,12 @@ + + + @TEST_LISTEN@ + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/debug-allow-external.conf.in b/test/data/valid-config-files/debug-allow-external.conf.in new file mode 100644 index 0000000..6c4e8e5 --- /dev/null +++ b/test/data/valid-config-files/debug-allow-external.conf.in @@ -0,0 +1,13 @@ + + + @TEST_LISTEN@ + EXTERNAL + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/debug-allow-invalid.conf.in b/test/data/valid-config-files/debug-allow-invalid.conf.in new file mode 100644 index 0000000..1ca544c --- /dev/null +++ b/test/data/valid-config-files/debug-allow-invalid.conf.in @@ -0,0 +1,13 @@ + + + @TEST_LISTEN@ + INVALID + @DBUS_TEST_DATA@/valid-service-files + + + + + + + diff --git a/test/data/valid-config-files/debug-allow-sha1.conf.in b/test/data/valid-config-files/debug-allow-sha1.conf.in new file mode 100644 index 0000000..1dc57ed --- /dev/null +++ b/test/data/valid-config-files/debug-allow-sha1.conf.in @@ -0,0 +1,13 @@ + + + @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..b82c62d --- /dev/null +++ b/test/test-auth.c @@ -0,0 +1,175 @@ +/* test case for dbus daemon auth configuration + * + * Copyright © 2017 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; + TestMainContext *ctx; + + GPid daemon_pid; + gchar *address; + + DBusConnection *client_conn; +} Fixture; + +typedef struct { + 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 (); + f->skip = FALSE; + + if (!_test_get_dbus_daemon (config ? config->config_file : NULL, + TEST_USER_ME, + &f->daemon_pid, &f->address)) + { + g_test_skip ("could not start dbus daemon"); + f->skip = TRUE; + } + f->client_conn = 0; +} + +static void +test_invalid (Fixture *f, + gconstpointer context) +{ + if (f->skip) + return; + 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) +{ + if (f->skip) + return; + g_assert (f->address != NULL); + 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) +{ + if (f->skip) + return; + g_assert (f->address != NULL); + f->client_conn = test_connect_to_bus (f->ctx, f->address); + g_assert (f->client_conn != NULL); +} + +static void +test_any (Fixture *f, + gconstpointer context) +{ + if (f->skip) + return; + g_assert (f->address != NULL); + 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) +{ + 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 = { + "valid-config-files/debug-allow-invalid.conf", + SPECIFY_ADDRESS +}; + +static Config config_external = { + "valid-config-files/debug-allow-external.conf", + SPECIFY_ADDRESS +}; + +static Config config_sha1 = { + "valid-config-files/debug-allow-sha1.conf", + SPECIFY_ADDRESS +}; + +static Config config_anonymous = { + "valid-config-files/debug-allow-anonymous.conf", + SPECIFY_ADDRESS +}; + +static Config config_any = { + "valid-config-files/debug-allow-any.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/any", Fixture, &config_any, setup, test_any, teardown); + + return g_test_run (); + + return 0; +} diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index e084977..e6a2965 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -225,15 +225,29 @@ spawn_dbus_daemon (const gchar *binary, return g_string_free (address, FALSE); } + gchar * test_get_dbus_daemon (const gchar *config_file, TestUser user, GPid *daemon_pid) { - gchar *dbus_daemon; - gchar *arg; + gchar **address = 0; + if (_test_get_dbus_daemon (config_file, user, daemon_pid, address)) + return *address; + else + return NULL; +} + +dbus_bool_t +_test_get_dbus_daemon (const gchar *config_file, + TestUser user, + GPid *daemon_pid, + gchar **address) +{ + gchar *dbus_daemon = NULL; + gchar *arg = NULL; const gchar *listen_address = NULL; - gchar *address; + dbus_bool_t result = FALSE; /* we often have to override this because on Windows, the default may be * autolaunch:, which is globally-scoped and hence unsuitable for @@ -248,7 +262,7 @@ test_get_dbus_daemon (const gchar *config_file, g_test_message ("set DBUS_TEST_DATA to a directory containing %s", config_file); g_test_skip ("DBUS_TEST_DATA not set"); - return NULL; + goto fail; } arg = g_strdup_printf ( @@ -286,22 +300,27 @@ test_get_dbus_daemon (const gchar *config_file, { g_test_skip ("cannot use DBUS_TEST_DAEMON_ADDRESS for " "unusally-configured dbus-daemon"); - address = NULL; + goto fail; } else { - address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS")); + *address = g_strdup (g_getenv ("DBUS_TEST_DAEMON_ADDRESS")); + result = TRUE; } } else { - address = spawn_dbus_daemon (dbus_daemon, arg, + *address = spawn_dbus_daemon (dbus_daemon, arg, listen_address, user, daemon_pid); + result = TRUE; } - g_free (dbus_daemon); - g_free (arg); - return address; +fail: + if (dbus_daemon) + g_free (dbus_daemon); + if (arg) + g_free (arg); + return result; } DBusConnection * diff --git a/test/test-utils-glib.h b/test/test-utils-glib.h index acacee0..d8900b1 100644 --- a/test/test-utils-glib.h +++ b/test/test-utils-glib.h @@ -71,6 +71,11 @@ gchar *test_get_dbus_daemon (const gchar *config_file, TestUser user, GPid *daemon_pid); +dbus_bool_t _test_get_dbus_daemon (const gchar *config_file, + TestUser user, + GPid *daemon_pid, + gchar **address); + DBusConnection *test_connect_to_bus (TestMainContext *ctx, const gchar *address); DBusConnection *test_connect_to_bus_as_user (TestMainContext *ctx, -- 2.6.6