docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / tests / gdbus-non-socket.c
blob238b3031843a4103e29b13b0ada24a8c3a4eb226
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>
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
27 #include <stdlib.h>
29 #ifdef G_OS_UNIX
30 #include <gio/gunixinputstream.h>
31 #include <gio/gunixoutputstream.h>
32 #include <gio/gunixconnection.h>
33 #endif
35 #include "gdbus-tests.h"
37 static GMainLoop *loop = NULL;
39 /* ---------------------------------------------------------------------------------------------------- */
40 #ifdef G_OS_UNIX
42 #include "test-pipe-unix.h"
43 #include "test-io-stream.h"
45 /* ---------------------------------------------------------------------------------------------------- */
47 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
48 -1, /* ref_count */
49 "result",
50 "s",
51 NULL /* annotations */
54 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
55 &pokee_method_poke_out_arg0,
56 NULL,
59 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
60 -1, /* ref_count */
61 "value",
62 "s",
63 NULL /* annotations */
66 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
67 &pokee_method_poke_in_arg0,
68 NULL,
71 static const GDBusMethodInfo pokee_method_poke = {
72 -1, /* ref_count */
73 "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] = {
80 &pokee_method_poke,
81 NULL
84 static const GDBusInterfaceInfo pokee_object_info = {
85 -1, /* ref_count */
86 "org.gtk.GDBus.Pokee",
87 (GDBusMethodInfo**) pokee_methods,
88 NULL, /* signals */
89 NULL, /* properties */
90 NULL /* annotations */
93 static void
94 pokee_method_call (GDBusConnection *connection,
95 const gchar *sender,
96 const gchar *object_path,
97 const gchar *interface_name,
98 const gchar *method_name,
99 GVariant *parameters,
100 GDBusMethodInvocation *invocation,
101 gpointer user_data)
103 const gchar *str;
104 gchar *ret;
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));
111 g_free (ret);
114 static const GDBusInterfaceVTable pokee_vtable = {
115 pokee_method_call,
116 NULL, /* get_property */
117 NULL /* set_property */
120 /* Processes:
122 * parent
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.
130 static void
131 test_non_socket (void)
133 GIOStream *streams[2];
134 GDBusConnection *connection;
135 GError *error;
136 gchar *guid;
137 pid_t first_child;
138 GVariant *ret;
139 const gchar *str;
140 gboolean ok;
142 error = NULL;
144 ok = test_bidi_pipe (&streams[0], &streams[1], &error);
145 g_assert_no_error (error);
146 g_assert (ok);
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 ()))
156 case -1:
157 g_assert_not_reached ();
158 break;
160 case 0:
161 /* first child */
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);
170 g_assert (ok);
171 g_object_unref (streams[1]);
173 guid = g_dbus_generate_guid ();
174 error = NULL;
175 /* We need to delay message processing to avoid the race
176 * described in
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],
186 guid,
187 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
188 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
189 NULL, /* GDBusAuthObserver */
190 NULL,
191 &error);
192 g_free (guid);
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);
199 error = NULL;
200 g_dbus_connection_register_object (connection,
201 "/pokee",
202 (GDBusInterfaceInfo *) &pokee_object_info,
203 &pokee_vtable,
204 NULL, /* user_data */
205 NULL, /* user_data_free_func */
206 &error);
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 ();
215 break;
217 default:
218 /* parent continues below */
219 break;
222 if (!g_test_trap_fork (0, 0))
224 /* parent */
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);
230 return;
233 /* second child */
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);
242 g_assert (ok);
243 g_object_unref (streams[0]);
245 connection = g_dbus_connection_new_sync (streams[1],
246 NULL, /* guid */
247 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
248 NULL, /* GDBusAuthObserver */
249 NULL,
250 &error);
251 g_assert_no_error (error);
252 g_object_unref (streams[1]);
254 /* poke the first child */
255 error = NULL;
256 ret = g_dbus_connection_call_sync (connection,
257 NULL, /* name */
258 "/pokee",
259 "org.gtk.GDBus.Pokee",
260 "Poke",
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 */
266 &error);
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);
274 exit (0);
277 #else /* G_OS_UNIX */
279 static void
280 test_non_socket (void)
282 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
284 #endif
286 /* ---------------------------------------------------------------------------------------------------- */
289 main (int argc,
290 char *argv[])
292 gint ret;
294 g_type_init ();
295 g_test_init (&argc, &argv, NULL);
297 g_test_add_func ("/gdbus/non-socket", test_non_socket);
299 ret = g_test_run();
301 return ret;