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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
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 */
123 * \- first child (via fork()) is the pokee
124 * \- second child (via g_test_trap_fork()) is the poker
126 * The second child only exists to avoid sharing a main context between several
127 * second-children if we run a test resembling this one multiple times.
128 * See https://bugzilla.gnome.org/show_bug.cgi?id=658999 for why that's bad.
131 test_non_socket (void)
133 GIOStream
*streams
[2];
134 GDBusConnection
*connection
;
144 ok
= test_bidi_pipe (&streams
[0], &streams
[1], &error
);
145 g_assert_no_error (error
);
147 g_assert (G_IS_IO_STREAM (streams
[0]));
148 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams
[0])));
149 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams
[0])));
150 g_assert (G_IS_IO_STREAM (streams
[1]));
151 g_assert (G_IS_INPUT_STREAM (g_io_stream_get_input_stream (streams
[1])));
152 g_assert (G_IS_OUTPUT_STREAM (g_io_stream_get_output_stream (streams
[1])));
154 switch ((first_child
= fork ()))
157 g_assert_not_reached ();
163 /* we shouldn't do this in the parent, because we shouldn't use a
164 * GMainContext both before and after fork
166 loop
= g_main_loop_new (NULL
, FALSE
);
168 ok
= g_io_stream_close (streams
[1], NULL
, &error
);
169 g_assert_no_error (error
);
171 g_object_unref (streams
[1]);
173 guid
= g_dbus_generate_guid ();
175 /* We need to delay message processing to avoid the race
178 * https://bugzilla.gnome.org/show_bug.cgi?id=627188
180 * This is because (early) dispatching is done on the IO thread
181 * (method_call() isn't called until we're in the right thread
182 * though) so in rare cases the parent sends the message before
183 * we (the first child) register the object
185 connection
= g_dbus_connection_new_sync (streams
[0],
187 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
|
188 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
,
189 NULL
, /* GDBusAuthObserver */
193 g_assert_no_error (error
);
194 g_object_unref (streams
[0]);
196 /* make sure we exit along with the parent */
197 g_dbus_connection_set_exit_on_close (connection
, TRUE
);
200 g_dbus_connection_register_object (connection
,
202 (GDBusInterfaceInfo
*) &pokee_object_info
,
204 NULL
, /* user_data */
205 NULL
, /* user_data_free_func */
207 g_assert_no_error (error
);
209 /* and now start message processing */
210 g_dbus_connection_start_message_processing (connection
);
212 g_main_loop_run (loop
);
214 g_assert_not_reached ();
218 /* parent continues below */
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);
235 /* we shouldn't do this in the parent, because we shouldn't use a
236 * GMainContext both before and after fork
238 loop
= g_main_loop_new (NULL
, FALSE
);
240 ok
= g_io_stream_close (streams
[0], NULL
, &error
);
241 g_assert_no_error (error
);
243 g_object_unref (streams
[0]);
245 connection
= g_dbus_connection_new_sync (streams
[1],
247 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
,
248 NULL
, /* GDBusAuthObserver */
251 g_assert_no_error (error
);
252 g_object_unref (streams
[1]);
254 /* poke the first child */
256 ret
= g_dbus_connection_call_sync (connection
,
259 "org.gtk.GDBus.Pokee",
261 g_variant_new ("(s)", "I am the POKER!"),
262 G_VARIANT_TYPE ("(s)"), /* return type */
263 G_DBUS_CALL_FLAGS_NONE
,
265 NULL
, /* cancellable */
267 g_assert_no_error (error
);
268 g_variant_get (ret
, "(&s)", &str
);
269 g_assert_cmpstr (str
, ==, "You poked me with: `I am the POKER!'");
270 g_variant_unref (ret
);
272 g_object_unref (connection
);
273 g_main_loop_unref (loop
);
277 #else /* G_OS_UNIX */
280 test_non_socket (void)
282 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
286 /* ---------------------------------------------------------------------------------------------------- */
295 g_test_init (&argc
, &argv
, NULL
);
297 g_test_add_func ("/gdbus/non-socket", test_non_socket
);