From afe7498eb6212e05c35f437a202824093c369060 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Sat, 28 Jan 2017 11:54:30 +0100 Subject: [PATCH] Add glib based test case for testing dbus auth configuration. This patch contains an endless loop fix in spawn_dbus_daemon() in case the spawned process exits unexpected. 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 | 15 ++ .../valid-config-files/debug-allow-sha1.conf.in | 13 ++ test/test-auth.c | 185 +++++++++++++++++++++ test/test-utils-glib.c | 80 +++++++-- test/test-utils-glib.h | 6 + 10 files changed, 338 insertions(+), 13 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 b354a8a..e93c960 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -111,6 +111,7 @@ if(DBUS_WITH_GLIB) add_test_executable(test-syslog ${CMAKE_SOURCE_DIR}/../test/internals/syslog.c ${TEST_LIBRARIES}) add_test_executable(test-uid-permissions ${CMAKE_SOURCE_DIR}/../test/uid-permissions.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 df0ab36..df1e881 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/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..cbba6b1 --- /dev/null +++ b/test/data/valid-config-files/debug-allow-invalid.conf.in @@ -0,0 +1,15 @@ + + + + + @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..86fd47e --- /dev/null +++ b/test/test-auth.c @@ -0,0 +1,185 @@ +/* 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; + f->address = NULL; + + if (!_test_get_dbus_daemon (config ? config->config_file : NULL, + TEST_USER_ME, + &f->daemon_pid, &f->address)) + { + f->skip = TRUE; + } + f->client_conn = 0; +} + +static dbus_bool_t +_test_connect_to_bus (Fixture *f) +{ + DBusError error = DBUS_ERROR_INIT; + dbus_bool_t ok; + + f->client_conn = dbus_connection_open_private (f->address, &error); + if (dbus_error_is_set (&error)) + return FALSE; + + if (f->client_conn == NULL) + return FALSE; + + ok = dbus_bus_register (f->client_conn, &error); + if (dbus_error_is_set (&error)) + return FALSE; + + if (!ok) + return FALSE; + + if (dbus_bus_get_unique_name (f->client_conn) == NULL) + return FALSE; + + test_connection_setup (f->ctx, f->client_conn); + return TRUE; +} + +static void +test_invalid (Fixture *f, gconstpointer context) +{ + const Config *config = context; + + if (f->skip) + return; + if (f->address != NULL) + g_error ("Was unexpectedly able to start a dbus-daemon for %s", + config->config_file); +} + +static void +test_valid (Fixture *f, gconstpointer context) +{ + const Config *config = context; + + if (f->skip) + return; + if (f->address == NULL) + { + g_error ("Unable to run dbus-daemon with %s", config->config_file); + return; + } + if (!_test_connect_to_bus (f)) + g_error ("Unable to connect to %s", f->address); +} + +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_valid, teardown); + g_test_add ("/auth/sha1", Fixture, &config_sha1, setup, test_valid, teardown); + g_test_add ("/auth/anonymous", Fixture, &config_anonymous, setup, test_valid, teardown); + g_test_add ("/auth/any", Fixture, &config_any, setup, test_valid, teardown); + + return g_test_run (); + + return 0; +} diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index ee286b9..258257b 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -104,6 +104,7 @@ spawn_dbus_daemon (const gchar *binary, #ifdef DBUS_UNIX const struct passwd *pwd = NULL; #endif + gboolean is_running = TRUE; if (user != TEST_USER_ME) { @@ -196,7 +197,7 @@ spawn_dbus_daemon (const gchar *binary, /* polling until the dbus-daemon writes out its address is a bit stupid, * but at least it's simple, unlike dbus-launch... in principle we could * use select() here, but life's too short */ - while (1) + while (is_running) { gssize bytes; gchar buf[4096]; @@ -218,11 +219,24 @@ spawn_dbus_daemon (const gchar *binary, } g_usleep (G_USEC_PER_SEC / 10); + int state = test_check_pid (*daemon_pid, 1); + if (state == 1) + is_running = TRUE; + else if (state == 0) + is_running = FALSE; + else + g_error ("unexpected return from test_check_pid()") ; } g_close (address_fd, NULL); - return g_string_free (address, FALSE); + if (is_running) + return g_string_free (address, FALSE); + else + { + *daemon_pid = 0; + return g_string_free (address, TRUE); + } } gchar * @@ -230,10 +244,23 @@ 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 @@ -242,13 +269,12 @@ test_get_dbus_daemon (const gchar *config_file, if (config_file != NULL) { - if (g_getenv ("DBUS_TEST_DATA") == NULL) { 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 out; } arg = g_strdup_printf ( @@ -286,22 +312,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 out; } 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; +out: + if (dbus_daemon) + g_free (dbus_daemon); + if (arg) + g_free (arg); + return result; } DBusConnection * @@ -420,6 +451,29 @@ test_kill_pid (GPid pid) kill (pid, SIGTERM); #endif } +/** + * return process state + * @param pid + * @return 1 - process is still running + * @return 0 - process has been exited with the request exit code + * @return -1 - process has been exited in an unexpected was + */ +int +test_check_pid (GPid pid, int exit_code) +{ +#ifdef DBUS_WIN + DWORD ret = WaitForSingleObject(pid, 0); + return ret == WAIT_TIMEOUT; +#else + int wstatus; + int result = waitpid (pid, &wstatus, WNOHANG); + if (result == 0) + return 1; + else if (result > 0 && WIFEXITED (wstatus) && WEXITSTATUS (wstatus) == exit_code) + return 0; + return -1; +#endif +} static gboolean time_out (gpointer data) diff --git a/test/test-utils-glib.h b/test/test-utils-glib.h index acacee0..608437b 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, @@ -78,6 +83,7 @@ DBusConnection *test_connect_to_bus_as_user (TestMainContext *ctx, TestUser user); void test_kill_pid (GPid pid); +int test_check_pid (GPid pid, int exit_code); void test_init (int *argcp, char ***argvp); -- 2.6.6