tests: port from g_test_trap_subprocess() to g_test_trap_fork()
[platform/upstream/glib.git] / gio / tests / gdbus-non-socket.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include <stdlib.h>
28
29 #ifdef G_OS_UNIX
30 #include <gio/gunixinputstream.h>
31 #include <gio/gunixoutputstream.h>
32 #include <gio/gunixconnection.h>
33 #endif
34
35 #include "gdbus-tests.h"
36
37 static GMainLoop *loop = NULL;
38
39 /* ---------------------------------------------------------------------------------------------------- */
40 #ifdef G_OS_UNIX
41
42 #include "test-pipe-unix.h"
43 #include "test-io-stream.h"
44
45 /* ---------------------------------------------------------------------------------------------------- */
46
47 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
48   -1,   /* ref_count */
49   "result",
50   "s",
51   NULL  /* annotations */
52 };
53
54 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
55   &pokee_method_poke_out_arg0,
56   NULL,
57 };
58
59 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
60   -1,   /* ref_count */
61   "value",
62   "s",
63   NULL  /* annotations */
64 };
65
66 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
67   &pokee_method_poke_in_arg0,
68   NULL,
69 };
70
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 */
77 };
78
79 static const GDBusMethodInfo *pokee_methods[2] = {
80   &pokee_method_poke,
81   NULL
82 };
83
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 */
91 };
92
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)
102 {
103   const gchar *str;
104   gchar *ret;
105
106   g_assert_cmpstr (method_name, ==, "Poke");
107
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);
112 }
113
114 static const GDBusInterfaceVTable pokee_vtable = {
115   pokee_method_call,
116   NULL, /* get_property */
117   NULL  /* set_property */
118 };
119
120 /* Processes:
121  *
122  * parent
123  * \- first child (via fork()) is the pokee
124  * \- second child (via g_test_trap_fork()) is the poker
125  *
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.
129  */
130 static void
131 test_non_socket (void)
132 {
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, in_child;
141
142   error = NULL;
143
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])));
153
154   switch ((first_child = fork ()))
155     {
156     case -1:
157       g_assert_not_reached ();
158       break;
159
160     case 0:
161       /* first child */
162
163       /* we shouldn't do this in the parent, because we shouldn't use a
164        * GMainContext both before and after fork
165        */
166       loop = g_main_loop_new (NULL, FALSE);
167
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]);
172
173       guid = g_dbus_generate_guid ();
174       error = NULL;
175       /* We need to delay message processing to avoid the race
176        * described in
177        *
178        *  https://bugzilla.gnome.org/show_bug.cgi?id=627188
179        *
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
184        */
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]);
195
196       /* make sure we exit along with the parent */
197       g_dbus_connection_set_exit_on_close (connection, TRUE);
198
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);
208
209       /* and now start message processing */
210       g_dbus_connection_start_message_processing (connection);
211
212       g_main_loop_run (loop);
213
214       g_assert_not_reached ();
215       break;
216
217     default:
218       /* parent continues below */
219       break;
220     }
221
222   /* This is #ifdef G_OS_UNIX anyway, so just use g_test_trap_fork() */
223   G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
224   in_child = g_test_trap_fork (0, 0);
225   G_GNUC_END_IGNORE_DEPRECATIONS;
226
227   if (!in_child)
228     {
229       /* parent */
230       g_object_unref (streams[0]);
231       g_object_unref (streams[1]);
232
233       g_test_trap_assert_passed ();
234       g_assert_cmpint (kill (first_child, SIGTERM), ==, 0);
235       return;
236     }
237
238   /* second child */
239
240   /* we shouldn't do this in the parent, because we shouldn't use a
241    * GMainContext both before and after fork
242    */
243   loop = g_main_loop_new (NULL, FALSE);
244
245   ok = g_io_stream_close (streams[0], NULL, &error);
246   g_assert_no_error (error);
247   g_assert (ok);
248   g_object_unref (streams[0]);
249
250   connection = g_dbus_connection_new_sync (streams[1],
251                                            NULL, /* guid */
252                                            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
253                                            NULL, /* GDBusAuthObserver */
254                                            NULL,
255                                            &error);
256   g_assert_no_error (error);
257   g_object_unref (streams[1]);
258
259   /* poke the first child */
260   error = NULL;
261   ret = g_dbus_connection_call_sync (connection,
262                                      NULL, /* name */
263                                      "/pokee",
264                                      "org.gtk.GDBus.Pokee",
265                                      "Poke",
266                                      g_variant_new ("(s)", "I am the POKER!"),
267                                      G_VARIANT_TYPE ("(s)"), /* return type */
268                                      G_DBUS_CALL_FLAGS_NONE,
269                                      -1,
270                                      NULL, /* cancellable */
271                                      &error);
272   g_assert_no_error (error);
273   g_variant_get (ret, "(&s)", &str);
274   g_assert_cmpstr (str, ==, "You poked me with: `I am the POKER!'");
275   g_variant_unref (ret);
276
277   g_object_unref (connection);
278   g_main_loop_unref (loop);
279   exit (0);
280 }
281
282 #else /* G_OS_UNIX */
283
284 static void
285 test_non_socket (void)
286 {
287   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
288 }
289 #endif
290
291 /* ---------------------------------------------------------------------------------------------------- */
292
293 int
294 main (int   argc,
295       char *argv[])
296 {
297   gint ret;
298
299   g_test_init (&argc, &argv, NULL);
300
301   g_test_add_func ("/gdbus/non-socket", test_non_socket);
302
303   ret = g_test_run();
304
305   return ret;
306 }