1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
30 #include <gio/gunixinputstream.h>
31 #include <gio/gunixoutputstream.h>
32 #include <gio/gunixconnection.h>
35 #include "gdbus-tests.h"
37 static GMainLoop *loop = NULL;
39 /* ---------------------------------------------------------------------------------------------------- */
42 #include "test-pipe-unix.h"
43 #include "test-io-stream.h"
45 /* ---------------------------------------------------------------------------------------------------- */
47 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
51 NULL /* annotations */
54 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
55 &pokee_method_poke_out_arg0,
59 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
63 NULL /* annotations */
66 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
67 &pokee_method_poke_in_arg0,
71 static const GDBusMethodInfo pokee_method_poke = {
74 (GDBusArgInfo**) pokee_method_poke_in_args,
75 (GDBusArgInfo**) pokee_method_poke_out_args,
76 NULL /* annotations */
79 static const GDBusMethodInfo *pokee_methods[2] = {
84 static const GDBusInterfaceInfo pokee_object_info = {
86 "org.gtk.GDBus.Pokee",
87 (GDBusMethodInfo**) pokee_methods,
89 NULL, /* properties */
90 NULL /* annotations */
94 pokee_method_call (GDBusConnection *connection,
96 const gchar *object_path,
97 const gchar *interface_name,
98 const gchar *method_name,
100 GDBusMethodInvocation *invocation,
106 g_assert_cmpstr (method_name, ==, "Poke");
108 g_variant_get (parameters, "(&s)", &str);
109 ret = g_strdup_printf ("You poked me with: '%s'", str);
110 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ret));
114 static const GDBusInterfaceVTable pokee_vtable = {
116 NULL, /* get_property */
117 NULL, /* set_property */
124 * \- first child (via fork()) is the pokee
125 * \- second child (via g_test_trap_fork()) is the poker
127 * The second child only exists to avoid sharing a main context between several
128 * second-children if we run a test resembling this one multiple times.
129 * See https://bugzilla.gnome.org/show_bug.cgi?id=658999 for why that's bad.
132 test_non_socket (void)
134 GIOStream *streams[2];
135 GDBusConnection *connection;
145 ok = test_bidi_pipe (&streams[0], &streams[1], &error);
146 g_assert_no_error (error);
148 g_assert (G_IS_IO_STREAM (streams[0]));
149 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[0])));
150 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[0])));
151 g_assert (G_IS_IO_STREAM (streams[1]));
152 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[1])));
153 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[1])));
155 switch ((first_child = fork ()))
158 g_assert_not_reached ();
164 /* we shouldn't do this in the parent, because we shouldn't use a
165 * GMainContext both before and after fork
167 loop = g_main_loop_new (NULL, FALSE);
169 ok = g_io_stream_close (streams[1], NULL, &error);
170 g_assert_no_error (error);
172 g_object_unref (streams[1]);
174 guid = g_dbus_generate_guid ();
176 /* We need to delay message processing to avoid the race
179 * https://bugzilla.gnome.org/show_bug.cgi?id=627188
181 * This is because (early) dispatching is done on the IO thread
182 * (method_call() isn't called until we're in the right thread
183 * though) so in rare cases the parent sends the message before
184 * we (the first child) register the object
186 connection = g_dbus_connection_new_sync (streams[0],
188 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
189 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
190 NULL, /* GDBusAuthObserver */
194 g_assert_no_error (error);
195 g_object_unref (streams[0]);
197 /* make sure we exit along with the parent */
198 g_dbus_connection_set_exit_on_close (connection, TRUE);
201 g_dbus_connection_register_object (connection,
203 (GDBusInterfaceInfo *) &pokee_object_info,
205 NULL, /* user_data */
206 NULL, /* user_data_free_func */
208 g_assert_no_error (error);
210 /* and now start message processing */
211 g_dbus_connection_start_message_processing (connection);
213 g_main_loop_run (loop);
215 g_assert_not_reached ();
219 /* parent continues below */
223 /* This is #ifdef G_OS_UNIX anyway, so just use g_test_trap_fork() */
224 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
225 if (!g_test_trap_fork (0, G_TEST_TRAP_DEFAULT))
228 g_object_unref (streams[0]);
229 g_object_unref (streams[1]);
231 g_test_trap_assert_passed ();
232 g_assert_cmpint (kill (first_child, SIGTERM), ==, 0);
235 G_GNUC_END_IGNORE_DEPRECATIONS;
239 /* we shouldn't do this in the parent, because we shouldn't use a
240 * GMainContext both before and after fork
242 loop = g_main_loop_new (NULL, FALSE);
244 ok = g_io_stream_close (streams[0], NULL, &error);
245 g_assert_no_error (error);
247 g_object_unref (streams[0]);
249 connection = g_dbus_connection_new_sync (streams[1],
251 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
252 NULL, /* GDBusAuthObserver */
255 g_assert_no_error (error);
256 g_object_unref (streams[1]);
258 /* poke the first child */
260 ret = g_dbus_connection_call_sync (connection,
263 "org.gtk.GDBus.Pokee",
265 g_variant_new ("(s)", "I am the POKER!"),
266 G_VARIANT_TYPE ("(s)"), /* return type */
267 G_DBUS_CALL_FLAGS_NONE,
269 NULL, /* cancellable */
271 g_assert_no_error (error);
272 g_variant_get (ret, "(&s)", &str);
273 g_assert_cmpstr (str, ==, "You poked me with: 'I am the POKER!'");
274 g_variant_unref (ret);
276 g_object_unref (connection);
277 g_main_loop_unref (loop);
281 #else /* G_OS_UNIX */
284 test_non_socket (void)
286 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
290 /* ---------------------------------------------------------------------------------------------------- */
298 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
300 g_test_add_func ("/gdbus/non-socket", test_non_socket);