Blame gio/tests/gdbus-unix-addresses.c

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