Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / gio / tests / gdbus-unix-addresses.c
blob531ce7a85a51d5df40c9711404184bf4d148fdb5
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.1 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)
65 int errsv = errno;
66 g_error ("g_mkdtemp_full: %s", g_strerror (errsv));
69 mock_bus_path = g_strconcat (tmpdir, "/bus", NULL);
70 addr = g_unix_socket_address_new (mock_bus_path);
71 g_socket_bind (mock_bus, addr, FALSE, &error);
72 g_assert_no_error (error);
73 g_object_unref (addr);
75 g_setenv ("XDG_RUNTIME_DIR", tmpdir, TRUE);
78 static void
79 tear_down_mock_xdg_runtime_dir (void)
81 GError *error = NULL;
83 g_socket_close (mock_bus, &error);
84 g_assert_no_error (error);
86 if (g_unlink (mock_bus_path) < 0)
88 int errsv = errno;
89 g_error ("g_unlink(\"%s\"): %s", mock_bus_path, g_strerror (errsv));
92 if (g_rmdir (tmpdir) < 0)
94 int errsv = errno;
95 g_error ("g_rmdir(\"%s\"): %s", tmpdir, g_strerror (errsv));
98 g_clear_object (&mock_bus);
99 g_clear_pointer (&mock_bus_path, g_free);
102 static gchar *path = NULL;
104 static void
105 set_up_mock_dbus_launch (void)
107 path = g_strconcat (g_test_get_dir (G_TEST_BUILT), ":",
108 g_getenv ("PATH"), NULL);
109 g_setenv ("PATH", path, TRUE);
111 /* libdbus won't even try X11 autolaunch if DISPLAY is unset; GDBus
112 * does the same in Debian derivatives (proposed upstream in
113 * GNOME#723506) */
114 g_setenv ("DISPLAY", "an unrealistic mock X11 display", TRUE);
117 static void
118 tear_down_mock_dbus_launch (void)
120 g_clear_pointer (&path, g_free);
123 static void
124 test_x11_autolaunch (void)
126 if (g_test_subprocess ())
128 g_unsetenv ("DISPLAY");
129 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
130 g_unsetenv ("XDG_RUNTIME_DIR");
131 g_unsetenv ("G_MESSAGES_DEBUG");
132 set_up_mock_dbus_launch ();
134 print_address ();
136 tear_down_mock_dbus_launch ();
137 return;
140 g_test_trap_subprocess (NULL, 0, 0);
141 g_test_trap_assert_stderr_unmatched ("?*");
142 g_test_trap_assert_stdout ("hello:this=address-is-from-the,mock=dbus-launch\n");
143 g_test_trap_assert_passed ();
146 static void
147 test_xdg_runtime (void)
149 if (g_test_subprocess ())
151 g_unsetenv ("DISPLAY");
152 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
153 set_up_mock_xdg_runtime_dir ();
154 set_up_mock_dbus_launch ();
156 print_address ();
158 tear_down_mock_dbus_launch ();
159 tear_down_mock_xdg_runtime_dir ();
160 return;
163 g_test_trap_subprocess (NULL, 0, 0);
164 g_test_trap_assert_stderr_unmatched ("?*");
165 g_test_trap_assert_stdout ("unix:path=/tmp/gdbus%2Cunix%2Ctest.*/bus\n");
166 g_test_trap_assert_passed ();
170 main (int argc,
171 char *argv[])
173 g_test_init (&argc, &argv, NULL);
175 g_test_add_func ("/gdbus/x11-autolaunch", test_x11_autolaunch);
176 g_test_add_func ("/gdbus/xdg-runtime", test_xdg_runtime);
178 return g_test_run();