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/>.
22 #error This is a Unix-specific test
27 #include <glib/gstdio.h>
29 #include <gio/gunixsocketaddress.h>
37 addr
= g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION
, NULL
,
40 g_assert_no_error (error
);
41 g_assert_nonnull (addr
);
42 g_print ("%s\n", 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";
52 set_up_mock_xdg_runtime_dir (void)
57 mock_bus
= g_socket_new (G_SOCKET_FAMILY_UNIX
, G_SOCKET_TYPE_STREAM
, 0,
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
)
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
);
79 tear_down_mock_xdg_runtime_dir (void)
83 g_socket_close (mock_bus
, &error
);
84 g_assert_no_error (error
);
86 if (g_unlink (mock_bus_path
) < 0)
89 g_error ("g_unlink(\"%s\"): %s", mock_bus_path
, g_strerror (errsv
));
92 if (g_rmdir (tmpdir
) < 0)
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
;
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
114 g_setenv ("DISPLAY", "an unrealistic mock X11 display", TRUE
);
118 tear_down_mock_dbus_launch (void)
120 g_clear_pointer (&path
, g_free
);
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 ();
136 tear_down_mock_dbus_launch ();
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 ();
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 ();
158 tear_down_mock_dbus_launch ();
159 tear_down_mock_xdg_runtime_dir ();
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 ();
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
);