Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-non-socket.c
blobc0eae5ec7c5d0ffe588b2f1f7dc66f7f3caa89c1
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>
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
25 #include <stdlib.h>
27 #ifdef G_OS_UNIX
28 #include <gio/gunixinputstream.h>
29 #include <gio/gunixoutputstream.h>
30 #include <gio/gunixconnection.h>
31 #endif
33 #include "gdbus-tests.h"
35 static GMainLoop *loop = NULL;
37 /* ---------------------------------------------------------------------------------------------------- */
38 #ifdef G_OS_UNIX
40 #include "test-pipe-unix.h"
41 #include "test-io-stream.h"
43 /* ---------------------------------------------------------------------------------------------------- */
45 static const GDBusArgInfo pokee_method_poke_out_arg0 = {
46 -1, /* ref_count */
47 "result",
48 "s",
49 NULL /* annotations */
52 static const GDBusArgInfo *pokee_method_poke_out_args[2] = {
53 &pokee_method_poke_out_arg0,
54 NULL,
57 static const GDBusArgInfo pokee_method_poke_in_arg0 = {
58 -1, /* ref_count */
59 "value",
60 "s",
61 NULL /* annotations */
64 static const GDBusArgInfo *pokee_method_poke_in_args[2] = {
65 &pokee_method_poke_in_arg0,
66 NULL,
69 static const GDBusMethodInfo pokee_method_poke = {
70 -1, /* ref_count */
71 "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] = {
78 &pokee_method_poke,
79 NULL
82 static const GDBusInterfaceInfo pokee_object_info = {
83 -1, /* ref_count */
84 "org.gtk.GDBus.Pokee",
85 (GDBusMethodInfo**) pokee_methods,
86 NULL, /* signals */
87 NULL, /* properties */
88 NULL /* annotations */
91 static void
92 pokee_method_call (GDBusConnection *connection,
93 const gchar *sender,
94 const gchar *object_path,
95 const gchar *interface_name,
96 const gchar *method_name,
97 GVariant *parameters,
98 GDBusMethodInvocation *invocation,
99 gpointer user_data)
101 const gchar *str;
102 gchar *ret;
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));
109 g_free (ret);
112 static const GDBusInterfaceVTable pokee_vtable = {
113 pokee_method_call,
114 NULL, /* get_property */
115 NULL /* set_property */
118 /* Processes:
120 * parent
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.
128 static void
129 test_non_socket (void)
131 GIOStream *streams[2];
132 GDBusConnection *connection;
133 GError *error;
134 gchar *guid;
135 pid_t first_child;
136 GVariant *ret;
137 const gchar *str;
138 gboolean ok;
140 error = NULL;
142 ok = test_bidi_pipe (&streams[0], &streams[1], &error);
143 g_assert_no_error (error);
144 g_assert (ok);
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 ()))
154 case -1:
155 g_assert_not_reached ();
156 break;
158 case 0:
159 /* first child */
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);
168 g_assert (ok);
169 g_object_unref (streams[1]);
171 guid = g_dbus_generate_guid ();
172 error = NULL;
173 /* We need to delay message processing to avoid the race
174 * described in
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],
184 guid,
185 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
186 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
187 NULL, /* GDBusAuthObserver */
188 NULL,
189 &error);
190 g_free (guid);
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);
197 error = NULL;
198 g_dbus_connection_register_object (connection,
199 "/pokee",
200 (GDBusInterfaceInfo *) &pokee_object_info,
201 &pokee_vtable,
202 NULL, /* user_data */
203 NULL, /* user_data_free_func */
204 &error);
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 ();
213 break;
215 default:
216 /* parent continues below */
217 break;
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))
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;
232 G_GNUC_END_IGNORE_DEPRECATIONS;
234 /* second child */
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);
243 g_assert (ok);
244 g_object_unref (streams[0]);
246 connection = g_dbus_connection_new_sync (streams[1],
247 NULL, /* guid */
248 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
249 NULL, /* GDBusAuthObserver */
250 NULL,
251 &error);
252 g_assert_no_error (error);
253 g_object_unref (streams[1]);
255 /* poke the first child */
256 error = NULL;
257 ret = g_dbus_connection_call_sync (connection,
258 NULL, /* name */
259 "/pokee",
260 "org.gtk.GDBus.Pokee",
261 "Poke",
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 */
267 &error);
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);
275 exit (0);
278 #else /* G_OS_UNIX */
280 static void
281 test_non_socket (void)
283 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
285 #endif
287 /* ---------------------------------------------------------------------------------------------------- */
290 main (int argc,
291 char *argv[])
293 gint ret;
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;