1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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/>.
18 * Author: David Zeuthen <davidz@redhat.com>
28 #include <gio/gunixinputstream.h>
29 #include <gio/gunixoutputstream.h>
30 #include <gio/gunixconnection.h>
33 #include "gdbus-tests.h"
35 static GMainLoop *loop = NULL;
37 /* ---------------------------------------------------------------------------------------------------- */
40 #include "test-pipe-unix.h"
41 #include "test-io-stream.h"
43 /* ---------------------------------------------------------------------------------------------------- */
45 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
49 NULL /* annotations */
52 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
53 &pokee_method_poke_out_arg0,
57 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
61 NULL /* annotations */
64 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
65 &pokee_method_poke_in_arg0,
69 static const GDBusMethodInfo pokee_method_poke = {
72 (GDBusArgInfo**) pokee_method_poke_in_args,
73 (GDBusArgInfo**) pokee_method_poke_out_args,
74 NULL /* annotations */
77 static const GDBusMethodInfo *pokee_methods[2] = {
82 static const GDBusInterfaceInfo pokee_object_info = {
84 "org.gtk.GDBus.Pokee",
85 (GDBusMethodInfo**) pokee_methods,
87 NULL, /* properties */
88 NULL /* annotations */
92 pokee_method_call (GDBusConnection *connection,
94 const gchar *object_path,
95 const gchar *interface_name,
96 const gchar *method_name,
98 GDBusMethodInvocation *invocation,
104 g_assert_cmpstr (method_name, ==, "Poke");
106 g_variant_get (parameters, "(&s)", &str);
107 ret = g_strdup_printf ("You poked me with: '%s'", str);
108 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ret));
112 static const GDBusInterfaceVTable pokee_vtable = {
114 NULL, /* get_property */
115 NULL /* set_property */
121 * \- first child (via fork()) is the pokee
122 * \- second child (via g_test_trap_fork()) is the poker
124 * The second child only exists to avoid sharing a main context between several
125 * second-children if we run a test resembling this one multiple times.
126 * See https://bugzilla.gnome.org/show_bug.cgi?id=658999 for why that's bad.
129 test_non_socket (void)
131 GIOStream *streams[2];
132 GDBusConnection *connection;
142 ok = test_bidi_pipe (&streams[0], &streams[1], &error);
143 g_assert_no_error (error);
145 g_assert (G_IS_IO_STREAM (streams[0]));
146 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[0])));
147 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[0])));
148 g_assert (G_IS_IO_STREAM (streams[1]));
149 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams[1])));
150 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams[1])));
152 switch ((first_child = fork ()))
155 g_assert_not_reached ();
161 /* we shouldn't do this in the parent, because we shouldn't use a
162 * GMainContext both before and after fork
164 loop = g_main_loop_new (NULL, FALSE);
166 ok = g_io_stream_close (streams[1], NULL, &error);
167 g_assert_no_error (error);
169 g_object_unref (streams[1]);
171 guid = g_dbus_generate_guid ();
173 /* We need to delay message processing to avoid the race
176 * https://bugzilla.gnome.org/show_bug.cgi?id=627188
178 * This is because (early) dispatching is done on the IO thread
179 * (method_call() isn't called until we're in the right thread
180 * though) so in rare cases the parent sends the message before
181 * we (the first child) register the object
183 connection = g_dbus_connection_new_sync (streams[0],
185 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
186 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
187 NULL, /* GDBusAuthObserver */
191 g_assert_no_error (error);
192 g_object_unref (streams[0]);
194 /* make sure we exit along with the parent */
195 g_dbus_connection_set_exit_on_close (connection, TRUE);
198 g_dbus_connection_register_object (connection,
200 (GDBusInterfaceInfo *) &pokee_object_info,
202 NULL, /* user_data */
203 NULL, /* user_data_free_func */
205 g_assert_no_error (error);
207 /* and now start message processing */
208 g_dbus_connection_start_message_processing (connection);
210 g_main_loop_run (loop);
212 g_assert_not_reached ();
216 /* parent continues below */
220 /* This is #ifdef G_OS_UNIX anyway, so just use g_test_trap_fork() */
221 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
222 if (!g_test_trap_fork (0, 0))
225 g_object_unref (streams[0]);
226 g_object_unref (streams[1]);
228 g_test_trap_assert_passed ();
229 g_assert_cmpint (kill (first_child, SIGTERM), ==, 0);
232 G_GNUC_END_IGNORE_DEPRECATIONS;
236 /* we shouldn't do this in the parent, because we shouldn't use a
237 * GMainContext both before and after fork
239 loop = g_main_loop_new (NULL, FALSE);
241 ok = g_io_stream_close (streams[0], NULL, &error);
242 g_assert_no_error (error);
244 g_object_unref (streams[0]);
246 connection = g_dbus_connection_new_sync (streams[1],
248 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
249 NULL, /* GDBusAuthObserver */
252 g_assert_no_error (error);
253 g_object_unref (streams[1]);
255 /* poke the first child */
257 ret = g_dbus_connection_call_sync (connection,
260 "org.gtk.GDBus.Pokee",
262 g_variant_new ("(s)", "I am the POKER!"),
263 G_VARIANT_TYPE ("(s)"), /* return type */
264 G_DBUS_CALL_FLAGS_NONE,
266 NULL, /* cancellable */
268 g_assert_no_error (error);
269 g_variant_get (ret, "(&s)", &str);
270 g_assert_cmpstr (str, ==, "You poked me with: 'I am the POKER!'");
271 g_variant_unref (ret);
273 g_object_unref (connection);
274 g_main_loop_unref (loop);
278 #else /* G_OS_UNIX */
281 test_non_socket (void)
283 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
287 /* ---------------------------------------------------------------------------------------------------- */
295 g_test_init (&argc, &argv, NULL);
297 g_test_add_func ("/gdbus/non-socket", test_non_socket);