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 #define MY_TYPE_IO_STREAM (my_io_stream_get_type ())
43 #define MY_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_IO_STREAM, MyIOStream))
44 #define MY_IS_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_IO_STREAM))
48 GIOStream parent_instance;
49 GInputStream *input_stream;
50 GOutputStream *output_stream;
55 GIOStreamClass parent_class;
58 static GType my_io_stream_get_type (void) G_GNUC_CONST;
60 G_DEFINE_TYPE (MyIOStream, my_io_stream, G_TYPE_IO_STREAM);
63 my_io_stream_finalize (GObject *object)
65 MyIOStream *stream = MY_IO_STREAM (object);
66 g_object_unref (stream->input_stream);
67 g_object_unref (stream->output_stream);
68 G_OBJECT_CLASS (my_io_stream_parent_class)->finalize (object);
72 my_io_stream_init (MyIOStream *stream)
77 my_io_stream_get_input_stream (GIOStream *_stream)
79 MyIOStream *stream = MY_IO_STREAM (_stream);
80 return stream->input_stream;
83 static GOutputStream *
84 my_io_stream_get_output_stream (GIOStream *_stream)
86 MyIOStream *stream = MY_IO_STREAM (_stream);
87 return stream->output_stream;
91 my_io_stream_class_init (MyIOStreamClass *klass)
93 GObjectClass *gobject_class;
94 GIOStreamClass *giostream_class;
96 gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->finalize = my_io_stream_finalize;
99 giostream_class = G_IO_STREAM_CLASS (klass);
100 giostream_class->get_input_stream = my_io_stream_get_input_stream;
101 giostream_class->get_output_stream = my_io_stream_get_output_stream;
105 my_io_stream_new (GInputStream *input_stream,
106 GOutputStream *output_stream)
109 g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), NULL);
110 g_return_val_if_fail (G_IS_OUTPUT_STREAM (output_stream), NULL);
111 stream = MY_IO_STREAM (g_object_new (MY_TYPE_IO_STREAM, NULL));
112 stream->input_stream = g_object_ref (input_stream);
113 stream->output_stream = g_object_ref (output_stream);
114 return G_IO_STREAM (stream);
118 my_io_stream_new_for_fds (gint fd_in, gint fd_out)
121 GInputStream *input_stream;
122 GOutputStream *output_stream;
124 input_stream = g_unix_input_stream_new (fd_in, TRUE);
125 output_stream = g_unix_output_stream_new (fd_out, TRUE);
126 stream = my_io_stream_new (input_stream, output_stream);
127 g_object_unref (input_stream);
128 g_object_unref (output_stream);
132 /* ---------------------------------------------------------------------------------------------------- */
134 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
138 NULL /* annotations */
141 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
142 &pokee_method_poke_out_arg0,
146 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
150 NULL /* annotations */
153 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
154 &pokee_method_poke_in_arg0,
158 static const GDBusMethodInfo pokee_method_poke = {
161 (GDBusArgInfo**) pokee_method_poke_in_args,
162 (GDBusArgInfo**) pokee_method_poke_out_args,
163 NULL /* annotations */
166 static const GDBusMethodInfo *pokee_methods[2] = {
171 static const GDBusInterfaceInfo pokee_object_info = {
173 "org.gtk.GDBus.Pokee",
174 (GDBusMethodInfo**) pokee_methods,
176 NULL, /* properties */
177 NULL /* annotations */
181 pokee_method_call (GDBusConnection *connection,
183 const gchar *object_path,
184 const gchar *interface_name,
185 const gchar *method_name,
186 GVariant *parameters,
187 GDBusMethodInvocation *invocation,
193 g_assert_cmpstr (method_name, ==, "Poke");
195 g_variant_get (parameters, "(&s)", &str);
196 ret = g_strdup_printf ("You poked me with: `%s'", str);
197 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ret));
201 static const GDBusInterfaceVTable pokee_vtable = {
203 NULL, /* get_property */
204 NULL /* set_property */
210 * \- first child (via fork()) is the pokee
211 * \- second child (via g_test_trap_fork()) is the poker
213 * The second child only exists to avoid sharing a main context between several
214 * second-children if we run a test resembling this one multiple times.
215 * See https://bugzilla.gnome.org/show_bug.cgi?id=658999 for why that's bad.
218 test_non_socket (void)
223 GDBusConnection *connection;
232 g_assert_cmpint (pipe (in_pipes), ==, 0);
233 g_assert_cmpint (pipe (out_pipes), ==, 0);
235 switch ((first_child = fork ()))
238 g_assert_not_reached ();
244 /* we shouldn't do this in the parent, because we shouldn't use a
245 * GMainContext both before and after fork
247 loop = g_main_loop_new (NULL, FALSE);
249 read_fd = in_pipes[0];
250 write_fd = out_pipes[1];
251 g_assert_cmpint (close ( in_pipes[1]), ==, 0); /* close unused write end */
252 g_assert_cmpint (close (out_pipes[0]), ==, 0); /* close unused read end */
253 stream = my_io_stream_new_for_fds (read_fd, write_fd);
254 guid = g_dbus_generate_guid ();
256 /* We need to delay message processing to avoid the race
259 * https://bugzilla.gnome.org/show_bug.cgi?id=627188
261 * This is because (early) dispatching is done on the IO thread
262 * (method_call() isn't called until we're in the right thread
263 * though) so in rare cases the parent sends the message before
264 * we (the first child) register the object
266 connection = g_dbus_connection_new_sync (stream,
268 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
269 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
270 NULL, /* GDBusAuthObserver */
274 g_assert_no_error (error);
275 g_object_unref (stream);
277 /* make sure we exit along with the parent */
278 g_dbus_connection_set_exit_on_close (connection, TRUE);
281 g_dbus_connection_register_object (connection,
283 (GDBusInterfaceInfo *) &pokee_object_info,
285 NULL, /* user_data */
286 NULL, /* user_data_free_func */
288 g_assert_no_error (error);
290 /* and now start message processing */
291 g_dbus_connection_start_message_processing (connection);
293 g_main_loop_run (loop);
295 g_assert_not_reached ();
299 /* parent continues below */
303 if (!g_test_trap_fork (0, 0))
306 g_test_trap_assert_passed ();
307 g_assert_cmpint (kill (first_child, SIGTERM), ==, 0);
313 /* we shouldn't do this in the parent, because we shouldn't use a
314 * GMainContext both before and after fork
316 loop = g_main_loop_new (NULL, FALSE);
318 read_fd = out_pipes[0];
319 write_fd = in_pipes[1];
320 g_assert_cmpint (close (out_pipes[1]), ==, 0); /* close unused write end */
321 g_assert_cmpint (close ( in_pipes[0]), ==, 0); /* close unused read end */
322 stream = my_io_stream_new_for_fds (read_fd, write_fd);
324 connection = g_dbus_connection_new_sync (stream,
326 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
327 NULL, /* GDBusAuthObserver */
330 g_assert_no_error (error);
331 g_object_unref (stream);
333 /* poke the first child */
335 ret = g_dbus_connection_call_sync (connection,
338 "org.gtk.GDBus.Pokee",
340 g_variant_new ("(s)", "I am the POKER!"),
341 G_VARIANT_TYPE ("(s)"), /* return type */
342 G_DBUS_CALL_FLAGS_NONE,
344 NULL, /* cancellable */
346 g_assert_no_error (error);
347 g_variant_get (ret, "(&s)", &str);
348 g_assert_cmpstr (str, ==, "You poked me with: `I am the POKER!'");
349 g_variant_unref (ret);
351 g_object_unref (connection);
352 g_main_loop_unref (loop);
356 #else /* G_OS_UNIX */
359 test_non_socket (void)
361 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
365 /* ---------------------------------------------------------------------------------------------------- */
374 g_test_init (&argc, &argv, NULL);
376 g_test_add_func ("/gdbus/non-socket", test_non_socket);