1/* GDBus regression test - close a stream when a message remains to be written
2 *
3 * Copyright © 2006-2010 Red Hat, Inc.
4 * Copyright © 2011 Nokia Corporation
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
20 */
21
22#include <config.h>
23
24#include <stdlib.h>
25#include <string.h>
26
27#include <gio/gio.h>
28
29#ifdef G_OS_UNIX
30# include <unistd.h>
31
32# include <glib/glib-unix.h>
33# include <gio/gunixinputstream.h>
34# include <gio/gunixoutputstream.h>
35# include <gio/gunixconnection.h>
36#else
37# error This test is currently Unix-specific due to use of g_unix_open_pipe()
38#endif
39
40#include "gdbus-tests.h"
41
42#define CLOSE_TIME_MS 1
43#define N_REPEATS_SLOW 5000
44#define N_REPEATS 100
45
46/* ---------- MyIOStream ------------------------------------------------- */
47
48#define MY_TYPE_IO_STREAM (my_io_stream_get_type ())
49#define MY_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_IO_STREAM, MyIOStream))
50#define MY_IS_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_IO_STREAM))
51
52typedef struct
53{
54 GIOStream parent_instance;
55 GInputStream *input_stream;
56 GOutputStream *output_stream;
57} MyIOStream;
58
59typedef struct
60{
61 GIOStreamClass parent_class;
62} MyIOStreamClass;
63
64static GType my_io_stream_get_type (void) G_GNUC_CONST;
65
66G_DEFINE_TYPE (MyIOStream, my_io_stream, G_TYPE_IO_STREAM)
67
68static void
69my_io_stream_finalize (GObject *object)
70{
71 MyIOStream *stream = MY_IO_STREAM (object);
72 g_object_unref (object: stream->input_stream);
73 g_object_unref (object: stream->output_stream);
74 G_OBJECT_CLASS (my_io_stream_parent_class)->finalize (object);
75}
76
77static void
78my_io_stream_init (MyIOStream *stream)
79{
80}
81
82static GInputStream *
83my_io_stream_get_input_stream (GIOStream *_stream)
84{
85 MyIOStream *stream = MY_IO_STREAM (_stream);
86 return stream->input_stream;
87}
88
89static GOutputStream *
90my_io_stream_get_output_stream (GIOStream *_stream)
91{
92 MyIOStream *stream = MY_IO_STREAM (_stream);
93 return stream->output_stream;
94}
95
96static void
97my_io_stream_class_init (MyIOStreamClass *klass)
98{
99 GObjectClass *gobject_class;
100 GIOStreamClass *giostream_class;
101
102 gobject_class = G_OBJECT_CLASS (klass);
103 gobject_class->finalize = my_io_stream_finalize;
104
105 giostream_class = G_IO_STREAM_CLASS (klass);
106 giostream_class->get_input_stream = my_io_stream_get_input_stream;
107 giostream_class->get_output_stream = my_io_stream_get_output_stream;
108}
109
110static GIOStream *
111my_io_stream_new (GInputStream *input_stream,
112 GOutputStream *output_stream)
113{
114 MyIOStream *stream;
115 g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), NULL);
116 g_return_val_if_fail (G_IS_OUTPUT_STREAM (output_stream), NULL);
117 stream = MY_IO_STREAM (g_object_new (MY_TYPE_IO_STREAM, NULL));
118 stream->input_stream = g_object_ref (input_stream);
119 stream->output_stream = g_object_ref (output_stream);
120 return G_IO_STREAM (stream);
121}
122
123/* ---------- MySlowCloseOutputStream ------------------------------------ */
124
125typedef struct
126{
127 GFilterOutputStream parent_instance;
128} MySlowCloseOutputStream;
129
130typedef struct
131{
132 GFilterOutputStreamClass parent_class;
133} MySlowCloseOutputStreamClass;
134
135#define MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM \
136 (my_slow_close_output_stream_get_type ())
137#define MY_OUTPUT_STREAM(o) \
138 (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM, \
139 MySlowCloseOutputStream))
140#define MY_IS_SLOW_CLOSE_OUTPUT_STREAM(o) \
141 (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM))
142
143static GType my_slow_close_output_stream_get_type (void) G_GNUC_CONST;
144
145G_DEFINE_TYPE (MySlowCloseOutputStream, my_slow_close_output_stream,
146 G_TYPE_FILTER_OUTPUT_STREAM)
147
148static void
149my_slow_close_output_stream_init (MySlowCloseOutputStream *stream)
150{
151}
152
153static gboolean
154my_slow_close_output_stream_close (GOutputStream *stream,
155 GCancellable *cancellable,
156 GError **error)
157{
158 g_usleep (CLOSE_TIME_MS * 1000);
159 return G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
160 close_fn (stream, cancellable, error);
161}
162
163typedef struct {
164 GOutputStream *stream;
165 gint io_priority;
166 GCancellable *cancellable;
167 GAsyncReadyCallback callback;
168 gpointer user_data;
169} DelayedClose;
170
171static void
172delayed_close_free (gpointer data)
173{
174 DelayedClose *df = data;
175
176 g_object_unref (object: df->stream);
177 if (df->cancellable)
178 g_object_unref (object: df->cancellable);
179 g_free (mem: df);
180}
181
182static gboolean
183delayed_close_cb (gpointer data)
184{
185 DelayedClose *df = data;
186
187 G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
188 close_async (df->stream, df->io_priority, df->cancellable, df->callback,
189 df->user_data);
190
191 return G_SOURCE_REMOVE;
192}
193
194static void
195my_slow_close_output_stream_close_async (GOutputStream *stream,
196 int io_priority,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
199 gpointer user_data)
200{
201 GSource *later;
202 DelayedClose *df;
203
204 df = g_new0 (DelayedClose, 1);
205 df->stream = g_object_ref (stream);
206 df->io_priority = io_priority;
207 df->cancellable = (cancellable != NULL ? g_object_ref (cancellable) : NULL);
208 df->callback = callback;
209 df->user_data = user_data;
210
211 later = g_timeout_source_new (CLOSE_TIME_MS);
212 g_source_set_callback (source: later, func: delayed_close_cb, data: df, notify: delayed_close_free);
213 g_source_attach (source: later, context: g_main_context_get_thread_default ());
214}
215
216static gboolean
217my_slow_close_output_stream_close_finish (GOutputStream *stream,
218 GAsyncResult *result,
219 GError **error)
220{
221 return G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
222 close_finish (stream, result, error);
223}
224
225static void
226my_slow_close_output_stream_class_init (MySlowCloseOutputStreamClass *klass)
227{
228 GOutputStreamClass *ostream_class;
229
230 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
231 ostream_class->close_fn = my_slow_close_output_stream_close;
232 ostream_class->close_async = my_slow_close_output_stream_close_async;
233 ostream_class->close_finish = my_slow_close_output_stream_close_finish;
234}
235
236static GIOStream *
237my_io_stream_new_for_fds (gint fd_in, gint fd_out)
238{
239 GIOStream *stream;
240 GInputStream *input_stream;
241 GOutputStream *real_output_stream;
242 GOutputStream *output_stream;
243
244 input_stream = g_unix_input_stream_new (fd: fd_in, TRUE);
245 real_output_stream = g_unix_output_stream_new (fd: fd_out, TRUE);
246 output_stream = g_object_new (MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM,
247 first_property_name: "base-stream", real_output_stream,
248 NULL);
249 stream = my_io_stream_new (input_stream, output_stream);
250 g_object_unref (object: input_stream);
251 g_object_unref (object: output_stream);
252 g_object_unref (object: real_output_stream);
253 return stream;
254}
255
256/* ---------- Tests ------------------------------------------------------ */
257
258typedef struct {
259 gint server_to_client[2];
260 gint client_to_server[2];
261 GIOStream *server_iostream;
262 GDBusConnection *server_conn;
263 GIOStream *iostream;
264 GDBusConnection *connection;
265 gchar *guid;
266 GError *error;
267} Fixture;
268
269static void
270setup (Fixture *f,
271 gconstpointer context)
272{
273 f->guid = g_dbus_generate_guid ();
274}
275
276static void
277teardown (Fixture *f,
278 gconstpointer context)
279{
280 g_clear_object (&f->server_iostream);
281 g_clear_object (&f->server_conn);
282 g_clear_object (&f->iostream);
283 g_clear_object (&f->connection);
284 g_clear_error (err: &f->error);
285 g_free (mem: f->guid);
286}
287
288static void
289on_new_conn (GObject *source,
290 GAsyncResult *res,
291 gpointer user_data)
292{
293 GDBusConnection **connection = user_data;
294 GError *error = NULL;
295
296 *connection = g_dbus_connection_new_for_address_finish (res, error: &error);
297 g_assert_no_error (error);
298}
299
300static void
301test_once (Fixture *f,
302 gconstpointer context)
303{
304 GDBusMessage *message;
305 gboolean pipe_res;
306
307 pipe_res = g_unix_open_pipe (fds: f->server_to_client, FD_CLOEXEC, error: &f->error);
308 g_assert (pipe_res);
309 pipe_res = g_unix_open_pipe (fds: f->client_to_server, FD_CLOEXEC, error: &f->error);
310 g_assert (pipe_res);
311
312 f->server_iostream = my_io_stream_new_for_fds (fd_in: f->client_to_server[0],
313 fd_out: f->server_to_client[1]);
314 f->iostream = my_io_stream_new_for_fds (fd_in: f->server_to_client[0],
315 fd_out: f->client_to_server[1]);
316
317 g_dbus_connection_new (stream: f->server_iostream,
318 guid: f->guid,
319 flags: (G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
320 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS),
321 NULL /* auth observer */,
322 NULL /* cancellable */,
323 callback: on_new_conn, user_data: &f->server_conn);
324
325 g_dbus_connection_new (stream: f->iostream,
326 NULL,
327 flags: G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
328 NULL /* auth observer */,
329 NULL /* cancellable */,
330 callback: on_new_conn, user_data: &f->connection);
331
332 while (f->server_conn == NULL || f->connection == NULL)
333 g_main_context_iteration (NULL, TRUE);
334
335 /*
336 * queue a message - it'll sometimes be sent while the close is pending,
337 * triggering the bug
338 */
339 message = g_dbus_message_new_signal (path: "/", interface_: "com.example.Foo", signal: "Bar");
340 g_dbus_connection_send_message (connection: f->connection, message, flags: 0, NULL, error: &f->error);
341 g_assert_no_error (f->error);
342 g_object_unref (object: message);
343
344 /* close the connection (deliberately or via last-unref) */
345 if (g_strcmp0 (str1: context, str2: "unref") == 0)
346 {
347 g_clear_object (&f->connection);
348 }
349 else
350 {
351 g_dbus_connection_close_sync (connection: f->connection, NULL, error: &f->error);
352 g_assert_no_error (f->error);
353 }
354
355 /* either way, wait for the connection to close */
356 while (!g_dbus_connection_is_closed (connection: f->server_conn))
357 g_main_context_iteration (NULL, TRUE);
358
359 /* clean up before the next run */
360 g_clear_object (&f->iostream);
361 g_clear_object (&f->server_iostream);
362 g_clear_object (&f->connection);
363 g_clear_object (&f->server_conn);
364 g_clear_error (err: &f->error);
365}
366
367static void
368test_many_times (Fixture *f,
369 gconstpointer context)
370{
371 guint i, n_repeats;
372
373 if (g_test_slow ())
374 n_repeats = N_REPEATS_SLOW;
375 else
376 n_repeats = N_REPEATS;
377
378 for (i = 0; i < n_repeats; i++)
379 test_once (f, context);
380}
381
382int
383main (int argc,
384 char *argv[])
385{
386 g_test_init (argc: &argc, argv: &argv, NULL);
387
388 g_test_add ("/gdbus/close-pending", Fixture, "close",
389 setup, test_many_times, teardown);
390 g_test_add ("/gdbus/unref-pending", Fixture, "unref",
391 setup, test_many_times, teardown);
392
393 return g_test_run();
394}
395

source code of gtk/subprojects/glib/gio/tests/gdbus-close-pending.c