From 4eed5c84325008642287c7a9ed461d956abc3643 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 8 Jan 2015 14:49:27 +0000 Subject: [PATCH 2/2] Add regression test for unix:runtime=1 Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61303 --- test/loopback.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/test/loopback.c b/test/loopback.c index 7526d8d..aea313b 100644 --- a/test/loopback.c +++ b/test/loopback.c @@ -27,9 +27,11 @@ #include #include +#include #include +#include #include #include "test-utils.h" @@ -44,6 +46,9 @@ typedef struct { GQueue server_messages; DBusConnection *client_conn; + + gchar *tmp_runtime_dir; + gchar *saved_runtime_dir; } Fixture; static void @@ -100,6 +105,56 @@ setup (Fixture *f, test_server_setup (f->ctx, f->server); } +#ifdef DBUS_UNIX +static void +setup_runtime (Fixture *f, + gconstpointer addr) +{ + char *listening_at; + GError *error = NULL; + + /* this is chosen to be something needing escaping */ + f->tmp_runtime_dir = g_dir_make_tmp ("dbus=daemon=test.XXXXXX", &error); + g_assert_no_error (error); + + /* 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); + + setup (f, addr); + + listening_at = dbus_server_get_address (f->server); + g_message ("listening at %s", listening_at); + g_assert (g_str_has_prefix (listening_at, "unix:path=")); + g_assert (strstr (listening_at, "dbus%3ddaemon%3dtest.") != NULL); + g_assert (strstr (listening_at, "/bus,") != NULL || + g_str_has_suffix (listening_at, "/bus")); + + dbus_free (listening_at); +} + +static void +setup_no_runtime (Fixture *f, + gconstpointer addr) +{ + char *listening_at; + + /* we're relying on being single-threaded for this to be safe */ + f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR")); + g_unsetenv ("XDG_RUNTIME_DIR"); + + setup (f, addr); + + listening_at = dbus_server_get_address (f->server); + g_message ("listening at %s", listening_at); + /* we have fallen back to something in /tmp, either abstract or not */ + g_assert (g_str_has_prefix (listening_at, "unix:")); + g_assert (strstr (listening_at, "=/tmp/") != NULL); + + dbus_free (listening_at); +} +#endif + static void test_connect (Fixture *f, gconstpointer addr G_GNUC_UNUSED) @@ -251,6 +306,40 @@ teardown (Fixture *f, test_main_context_unref (f->ctx); } +#ifdef DBUS_UNIX +static void +teardown_no_runtime (Fixture *f, + gconstpointer addr) +{ + teardown (f, addr); + + /* 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); +} + +static void +teardown_runtime (Fixture *f, + gconstpointer addr) +{ + gchar *path; + + teardown (f, addr); + + /* the socket exists */ + 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); +} +#endif + int main (int argc, char **argv) @@ -273,6 +362,13 @@ main (int argc, test_connect, teardown); g_test_add ("/message/unix", Fixture, "unix:tmpdir=/tmp", setup, test_message, teardown); + + g_test_add ("/connect/unix/runtime", Fixture, + "unix:runtime=1;unix:tmpdir=/tmp", setup_runtime, test_connect, + teardown_runtime); + g_test_add ("/connect/unix/no-runtime", Fixture, + "unix:runtime=1;unix:tmpdir=/tmp", setup_no_runtime, test_connect, + teardown_no_runtime); #endif g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup, -- 2.1.4