Regression test for falling back to autolaunch: and XDG_RUNTIME_DIR/bus

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=747941
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: Philip Withnall <philip.withnall@collabora.co.uk>
This commit is contained in:
Simon McVittie 2015-04-27 16:26:33 +01:00
parent 32492c6ab0
commit b701c3c608
3 changed files with 249 additions and 0 deletions

View File

@ -244,6 +244,7 @@ test_programs += \
contenttype \
file \
gdbus-peer-object-manager \
gdbus-unix-addresses \
live-g-file \
socket-address \
stream-rw_all \
@ -253,6 +254,7 @@ test_programs += \
test_extra_programs += \
basic-application \
dbus-launch \
$(NULL)
# Uninstalled because of the check-for-executable logic in DesktopAppInfo unable to find the installed executable

77
gio/tests/dbus-launch.c Normal file
View File

@ -0,0 +1,77 @@
/*
* Mock version of dbus-launch, for gdbus-unix-addresses test
*
* Copyright © 2015 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#ifndef G_OS_UNIX
#error This is a Unix-specific test helper
#endif
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define ME "GDBus mock version of dbus-launch"
static void
write_all (const void *ptr,
size_t len)
{
const char *p = ptr;
while (len > 0)
{
ssize_t done = write (STDOUT_FILENO, p, len);
if (done == 0)
{
g_error ("%s: write: EOF", ME);
}
else if (done < 0)
{
if (errno == EINTR)
continue;
g_error ("%s: write: %s", ME, g_strerror (errno));
}
else
{
if (len < (size_t) done)
g_error ("%s: wrote too many bytes?", ME);
len -= done;
p += done;
}
}
}
int
main (int argc,
char *argv[])
{
pid_t pid = 0x2323;
long window_id = 0x42424242;
const char *addr = "hello:this=address-is-from-the,mock=dbus-launch";
write_all (addr, strlen (addr) + 1);
write_all (&pid, sizeof (pid));
write_all (&window_id, sizeof (window_id));
return 0;
}

View File

@ -0,0 +1,170 @@
/* GLib testing framework examples and tests
*
* Copyright © 2015 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#ifndef G_OS_UNIX
#error This is a Unix-specific test
#endif
#include <errno.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
static void
print_address (void)
{
GError *error = NULL;
gchar *addr;
addr = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, NULL,
&error);
g_assert_no_error (error);
g_assert_nonnull (addr);
g_print ("%s\n", addr);
g_free (addr);
}
static GSocket *mock_bus = NULL;
static gchar *mock_bus_path = NULL;
/* this is deliberately something that needs escaping */
static gchar tmpdir[] = "/tmp/gdbus,unix,test.XXXXXX";
static void
set_up_mock_xdg_runtime_dir (void)
{
GError *error = NULL;
GSocketAddress *addr;
mock_bus = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0,
&error);
g_assert_no_error (error);
g_assert_true (G_IS_SOCKET (mock_bus));
/* alters tmpdir in-place */
if (g_mkdtemp_full (tmpdir, 0700) == NULL)
g_error ("g_mkdtemp_full: %s", g_strerror (errno));
mock_bus_path = g_strconcat (tmpdir, "/bus", NULL);
addr = g_unix_socket_address_new (mock_bus_path);
g_socket_bind (mock_bus, addr, FALSE, &error);
g_assert_no_error (error);
g_object_unref (addr);
g_setenv ("XDG_RUNTIME_DIR", tmpdir, TRUE);
}
static void
tear_down_mock_xdg_runtime_dir (void)
{
GError *error = NULL;
g_socket_close (mock_bus, &error);
g_assert_no_error (error);
if (g_unlink (mock_bus_path) < 0)
g_error ("g_unlink(\"%s\"): %s", mock_bus_path, g_strerror (errno));
if (g_rmdir (tmpdir) < 0)
g_error ("g_rmdir(\"%s\"): %s", tmpdir, g_strerror (errno));
g_clear_object (&mock_bus);
g_clear_pointer (&mock_bus_path, g_free);
}
static gchar *path = NULL;
static void
set_up_mock_dbus_launch (void)
{
path = g_strconcat (g_test_get_dir (G_TEST_BUILT), ":",
g_getenv ("PATH"), NULL);
g_debug ("PATH=%s", path);
g_setenv ("PATH", path, TRUE);
/* libdbus won't even try X11 autolaunch if DISPLAY is unset; GDBus
* does the same in Debian derivatives (proposed upstream in
* GNOME#723506) */
g_setenv ("DISPLAY", "an unrealistic mock X11 display", TRUE);
}
static void
tear_down_mock_dbus_launch (void)
{
g_clear_pointer (&path, g_free);
}
static void
test_x11_autolaunch (void)
{
if (g_test_subprocess ())
{
g_unsetenv ("DISPLAY");
g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
g_unsetenv ("XDG_RUNTIME_DIR");
set_up_mock_dbus_launch ();
print_address ();
tear_down_mock_dbus_launch ();
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_stderr_unmatched ("?*");
g_test_trap_assert_stdout ("hello:this=address-is-from-the,mock=dbus-launch\n");
g_test_trap_assert_passed ();
}
static void
test_xdg_runtime (void)
{
if (g_test_subprocess ())
{
g_unsetenv ("DISPLAY");
g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
set_up_mock_xdg_runtime_dir ();
set_up_mock_dbus_launch ();
print_address ();
tear_down_mock_dbus_launch ();
tear_down_mock_xdg_runtime_dir ();
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_stderr_unmatched ("?*");
g_test_trap_assert_stdout ("unix:path=/tmp/gdbus%2Cunix%2Ctest.*/bus\n");
g_test_trap_assert_passed ();
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gdbus/x11-autolaunch", test_x11_autolaunch);
g_test_add_func ("/gdbus/xdg-runtime", test_xdg_runtime);
return g_test_run();
}