From fb51f2ac6131a4413cc55162a5b9890e28993a96 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 29 Jan 2015 13:34:20 +0000 Subject: [PATCH 4/5] Add a regression test for connecting to XDG_RUNTIME_DIR/bus by default Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61303 --- test/Makefile.am | 1 + .../valid-config-files/listen-unix-runtime.conf | 11 +++ test/dbus-daemon.c | 95 +++++++++++++++++++--- 3 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 test/data/valid-config-files/listen-unix-runtime.conf diff --git a/test/Makefile.am b/test/Makefile.am index 173df74..4da3efe 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -327,6 +327,7 @@ static_data = \ data/valid-config-files/basic.conf \ data/valid-config-files/basic.d/basic.conf \ data/valid-config-files/entities.conf \ + data/valid-config-files/listen-unix-runtime.conf \ data/valid-config-files/many-rules.conf \ data/valid-config-files/system.d/test.conf \ data/valid-messages/array-of-array-of-uint32.message \ diff --git a/test/data/valid-config-files/listen-unix-runtime.conf b/test/data/valid-config-files/listen-unix-runtime.conf new file mode 100644 index 0000000..169de2c --- /dev/null +++ b/test/data/valid-config-files/listen-unix-runtime.conf @@ -0,0 +1,11 @@ + + + session + unix:runtime=yes + + + + + + diff --git a/test/dbus-daemon.c b/test/dbus-daemon.c index 1cd51d2..b7b5dbf 100644 --- a/test/dbus-daemon.c +++ b/test/dbus-daemon.c @@ -2,6 +2,7 @@ * * Author: Simon McVittie * Copyright © 2010-2011 Nokia Corporation + * Copyright © 2015 Collabora Ltd. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files @@ -26,12 +27,14 @@ #include +#include +#include + #include +#include #include -#include - #ifdef DBUS_WIN # include # include @@ -83,6 +86,9 @@ typedef struct { DBusConnection *right_conn; gboolean right_conn_echo; gboolean wait_forever_called; + + gchar *tmp_runtime_dir; + gchar *saved_runtime_dir; } Fixture; #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__) @@ -165,14 +171,23 @@ connect_to_bus (Fixture *f, DBusError error = DBUS_ERROR_INIT; dbus_bool_t ok; - conn = dbus_connection_open_private (address, &error); - assert_no_error (&error); - g_assert (conn != NULL); + if (address == NULL) + { + conn = dbus_bus_get_private (DBUS_BUS_SESSION, &error); + assert_no_error (&error); + g_assert (conn != NULL); + } + else + { + conn = dbus_connection_open_private (address, &error); + assert_no_error (&error); + g_assert (conn != NULL); - ok = dbus_bus_register (conn, &error); - assert_no_error (&error); - g_assert (ok); - g_assert (dbus_bus_get_unique_name (conn) != NULL); + ok = dbus_bus_register (conn, &error); + assert_no_error (&error); + g_assert (ok); + g_assert (dbus_bus_get_unique_name (conn) != NULL); + } test_connection_setup (f->ctx, conn); return conn; @@ -213,6 +228,7 @@ 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 @@ -228,6 +244,17 @@ setup (Fixture *f, f->ge = NULL; dbus_error_init (&f->e); + if (config != NULL && config->connect_mode == RELY_ON_DEFAULT) + { + /* this is chosen to be something needing escaping */ + f->tmp_runtime_dir = g_dir_make_tmp ("dbus=daemon=test.XXXXXX", &f->ge); + g_assert_no_error (f->ge); + + /* we're relying on being single-threaded for this to be safe */ + f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR")); + g_setenv ("XDG_RUNTIME_DIR", f->tmp_runtime_dir, TRUE); + } + if (config != NULL && config->config_file != NULL) { if (g_getenv ("DBUS_TEST_DAEMON_ADDRESS") != NULL) @@ -283,8 +310,17 @@ setup (Fixture *f, g_free (dbus_daemon); g_free (arg); - f->left_conn = connect_to_bus (f, address); - f->right_conn = connect_to_bus (f, address); + if (config != NULL && config->connect_mode == RELY_ON_DEFAULT) + { + f->left_conn = connect_to_bus (f, NULL); + f->right_conn = connect_to_bus (f, NULL); + } + else + { + f->left_conn = connect_to_bus (f, address); + f->right_conn = connect_to_bus (f, address); + } + g_free (address); } @@ -758,16 +794,42 @@ teardown (Fixture *f, f->daemon_pid = 0; } + if (f->tmp_runtime_dir != NULL) + { + gchar *path; + + /* the socket may exist */ + path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir); + g_assert (g_remove (path) == 0 || errno == ENOENT); + g_free (path); + /* there shouldn't be anything else in there */ + g_assert_cmpint (g_rmdir (f->tmp_runtime_dir), ==, 0); + + /* we're relying on being single-threaded for this to be safe */ + g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE); + g_free (f->saved_runtime_dir); + g_free (f->tmp_runtime_dir); + } + test_main_context_unref (f->ctx); } static Config limited_config = { - "34393", 10000, "valid-config-files/incoming-limit.conf" + "34393", 10000, "valid-config-files/incoming-limit.conf", + SPECIFY_ADDRESS }; static Config finite_timeout_config = { - NULL, 1, "valid-config-files/finite-timeout.conf" + NULL, 1, "valid-config-files/finite-timeout.conf", + SPECIFY_ADDRESS +}; + +#ifdef DBUS_UNIX +static Config listen_unix_runtime_config = { + "61303", 1, "valid-config-files/listen-unix-runtime.conf", + RELY_ON_DEFAULT }; +#endif int main (int argc, @@ -787,6 +849,13 @@ main (int argc, g_test_add ("/processid", Fixture, NULL, setup, test_processid, teardown); g_test_add ("/canonical-path/uae", Fixture, NULL, setup, test_canonical_path_uae, teardown); +#ifdef DBUS_UNIX + /* We can't test this in loopback.c with the rest of unix:runtime=yes, + * because dbus_bus_get[_private] is the only way to use the default, + * and that blocks on a round-trip to the dbus-daemon */ + g_test_add ("/unix-runtime-is-default", Fixture, &listen_unix_runtime_config, + setup, test_echo, teardown); +#endif return g_test_run (); } -- 2.1.4