From 5aeff879e1d9fec7cb6f023e31ccfb3d73bb1ef3 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 5 Feb 2015 18:30:36 +0000 Subject: [PATCH 5/7] Add a regression test for connecting to XDG_RUNTIME_DIR/bus by default This test requires the unix:runtime=yes sub-transport from Bug #61303. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61301 --- test/Makefile.am | 1 + .../valid-config-files/listen-unix-runtime.conf | 11 +++ test/dbus-daemon.c | 78 +++++++++++++++++++++- 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 test/data/valid-config-files/listen-unix-runtime.conf diff --git a/test/Makefile.am b/test/Makefile.am index c2a55c9..0b98c03 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -398,6 +398,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 4516091..edd8d0b 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,6 +27,14 @@ #include +#include +#include + +#include + +#include +#include + #include "test-utils-glib.h" #ifdef DBUS_UNIX @@ -73,6 +82,9 @@ typedef struct { DBusConnection *right_conn; gboolean right_conn_echo; gboolean wait_forever_called; + + gchar *tmp_runtime_dir; + gchar *saved_runtime_dir; } Fixture; static DBusHandlerResult @@ -110,6 +122,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 @@ -123,6 +136,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); + } + address = test_get_dbus_daemon (config ? config->config_file : NULL, TEST_USER_ME, &f->daemon_pid); @@ -134,7 +158,22 @@ setup (Fixture *f, } f->left_conn = test_connect_to_bus (f->ctx, address); - f->right_conn = test_connect_to_bus (f->ctx, address); + + if (config != NULL && config->connect_mode == RELY_ON_DEFAULT) + { + /* use the default bus for the echo service ("right"), to check that + * it ends up on the same bus as the client ("left") */ + f->right_conn = dbus_bus_get_private (DBUS_BUS_SESSION, &f->e); + test_assert_no_error (&f->e); + + if (!test_connection_setup (f->ctx, f->right_conn)) + g_error ("OOM"); + } + else + { + f->right_conn = test_connect_to_bus (f->ctx, address); + } + g_free (address); } @@ -613,17 +652,43 @@ 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, char **argv) @@ -641,6 +706,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