Update German translation
[glib.git] / gio / tests / gdbus-unix-addresses.c
blobe0832871196bc28552d8716977c06928b8d3030c
1 /* GLib testing framework examples and tests
3 * Copyright © 2015 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <glib.h>
21 #ifndef G_OS_UNIX
22 #error This is a Unix-specific test
23 #endif
25 #include <errno.h>
27 #include <glib/gstdio.h>
28 #include <gio/gio.h>
29 #include <gio/gunixsocketaddress.h>
31 static void
32 print_address (void)
34 GError *error = NULL;
35 gchar *addr;
37 addr = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, NULL,
38 &error);
40 g_assert_no_error (error);
41 g_assert_nonnull (addr);
42 g_print ("%s\n", addr);
43 g_free (addr);
46 static GSocket *mock_bus = NULL;
47 static gchar *mock_bus_path = NULL;
48 /* this is deliberately something that needs escaping */
49 static gchar tmpdir[] = "/tmp/gdbus,unix,test.XXXXXX";
51 static void
52 set_up_mock_xdg_runtime_dir (void)
54 GError *error = NULL;
55 GSocketAddress *addr;
57 mock_bus = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0,
58 &error);
59 g_assert_no_error (error);
60 g_assert_true (G_IS_SOCKET (mock_bus));
62 /* alters tmpdir in-place */
63 if (g_mkdtemp_full (tmpdir, 0700) == NULL)
64 g_error ("g_mkdtemp_full: %s", g_strerror (errno));
66 mock_bus_path = g_strconcat (tmpdir, "/bus", NULL);
67 addr = g_unix_socket_address_new (mock_bus_path);
68 g_socket_bind (mock_bus, addr, FALSE, &error);
69 g_assert_no_error (error);
70 g_object_unref (addr);
72 g_setenv ("XDG_RUNTIME_DIR", tmpdir, TRUE);
75 static void
76 tear_down_mock_xdg_runtime_dir (void)
78 GError *error = NULL;
80 g_socket_close (mock_bus, &error);
81 g_assert_no_error (error);
83 if (g_unlink (mock_bus_path) < 0)
84 g_error ("g_unlink(\"%s\"): %s", mock_bus_path, g_strerror (errno));
86 if (g_rmdir (tmpdir) < 0)
87 g_error ("g_rmdir(\"%s\"): %s", tmpdir, g_strerror (errno));
89 g_clear_object (&mock_bus);
90 g_clear_pointer (&mock_bus_path, g_free);
93 static gchar *path = NULL;
95 static void
96 set_up_mock_dbus_launch (void)
98 path = g_strconcat (g_test_get_dir (G_TEST_BUILT), ":",
99 g_getenv ("PATH"), NULL);
100 g_debug ("PATH=%s", path);
101 g_setenv ("PATH", path, TRUE);
103 /* libdbus won't even try X11 autolaunch if DISPLAY is unset; GDBus
104 * does the same in Debian derivatives (proposed upstream in
105 * GNOME#723506) */
106 g_setenv ("DISPLAY", "an unrealistic mock X11 display", TRUE);
109 static void
110 tear_down_mock_dbus_launch (void)
112 g_clear_pointer (&path, g_free);
115 static void
116 test_x11_autolaunch (void)
118 if (g_test_subprocess ())
120 g_unsetenv ("DISPLAY");
121 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
122 g_unsetenv ("XDG_RUNTIME_DIR");
123 set_up_mock_dbus_launch ();
125 print_address ();
127 tear_down_mock_dbus_launch ();
128 return;
131 g_test_trap_subprocess (NULL, 0, 0);
132 g_test_trap_assert_stderr_unmatched ("?*");
133 g_test_trap_assert_stdout ("hello:this=address-is-from-the,mock=dbus-launch\n");
134 g_test_trap_assert_passed ();
137 static void
138 test_xdg_runtime (void)
140 if (g_test_subprocess ())
142 g_unsetenv ("DISPLAY");
143 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
144 set_up_mock_xdg_runtime_dir ();
145 set_up_mock_dbus_launch ();
147 print_address ();
149 tear_down_mock_dbus_launch ();
150 tear_down_mock_xdg_runtime_dir ();
151 return;
154 g_test_trap_subprocess (NULL, 0, 0);
155 g_test_trap_assert_stderr_unmatched ("?*");
156 g_test_trap_assert_stdout ("unix:path=/tmp/gdbus%2Cunix%2Ctest.*/bus\n");
157 g_test_trap_assert_passed ();
161 main (int argc,
162 char *argv[])
164 g_test_init (&argc, &argv, NULL);
166 g_test_add_func ("/gdbus/x11-autolaunch", test_x11_autolaunch);
167 g_test_add_func ("/gdbus/xdg-runtime", test_xdg_runtime);
169 return g_test_run();