Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / gdbus-connection.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 <sys/types.h>
28 #include <sys/wait.h>
29
30 #include "gdbus-tests.h"
31
32 /* all tests rely on a shared mainloop */
33 static GMainLoop *loop = NULL;
34
35 G_GNUC_UNUSED static void
36 _log (const gchar *format, ...)
37 {
38   GTimeVal now;
39   time_t now_time;
40   struct tm *now_tm;
41   gchar time_buf[128];
42   gchar *str;
43   va_list var_args;
44
45   va_start (var_args, format);
46   str = g_strdup_vprintf (format, var_args);
47   va_end (var_args);
48
49   g_get_current_time (&now);
50   now_time = (time_t) now.tv_sec;
51   now_tm = localtime (&now_time);
52   strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
53
54   g_print ("%s.%06d: %s\n",
55            time_buf, (gint) now.tv_usec / 1000,
56            str);
57   g_free (str);
58 }
59
60 static gboolean
61 test_connection_quit_mainloop (gpointer user_data)
62 {
63   volatile gboolean *quit_mainloop_fired = user_data;
64   //_log ("quit_mainloop_fired");
65   *quit_mainloop_fired = TRUE;
66   g_main_loop_quit (loop);
67   return TRUE;
68 }
69
70 /* ---------------------------------------------------------------------------------------------------- */
71 /* Connection life-cycle testing */
72 /* ---------------------------------------------------------------------------------------------------- */
73
74 static const GDBusInterfaceInfo boo_interface_info =
75 {
76   -1,
77   "org.example.Boo",
78   (GDBusMethodInfo **) NULL,
79   (GDBusSignalInfo **) NULL,
80   (GDBusPropertyInfo **) NULL,
81   NULL,
82 };
83
84 static const GDBusInterfaceVTable boo_vtable =
85 {
86   NULL, /* _method_call */
87   NULL, /* _get_property */
88   NULL  /* _set_property */
89 };
90
91 static GDBusMessage *
92 some_filter_func (GDBusConnection *connection,
93                   GDBusMessage    *message,
94                   gboolean         incoming,
95                   gpointer         user_data)
96 {
97   return message;
98 }
99
100 static void
101 on_name_owner_changed (GDBusConnection *connection,
102                        const gchar     *sender_name,
103                        const gchar     *object_path,
104                        const gchar     *interface_name,
105                        const gchar     *signal_name,
106                        GVariant        *parameters,
107                        gpointer         user_data)
108 {
109 }
110
111 static void
112 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
113 {
114   volatile gboolean *val = user_data;
115   *val = TRUE;
116   //_log ("destroynotify fired for %p", val);
117   g_main_loop_quit (loop);
118 }
119
120 static void
121 test_connection_life_cycle (void)
122 {
123   gboolean ret;
124   GDBusConnection *c;
125   GDBusConnection *c2;
126   GError *error;
127   volatile gboolean on_signal_registration_freed_called;
128   volatile gboolean on_filter_freed_called;
129   volatile gboolean on_register_object_freed_called;
130   volatile gboolean quit_mainloop_fired;
131   guint quit_mainloop_id;
132   guint registration_id;
133
134   error = NULL;
135
136   /*
137    * Check for correct behavior when no bus is present
138    *
139    */
140   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
141   _g_assert_error_domain (error, G_IO_ERROR);
142   g_assert (!g_dbus_error_is_remote_error (error));
143   g_assert (c == NULL);
144   g_error_free (error);
145
146   /*
147    *  Check for correct behavior when a bus is present
148    */
149   session_bus_up ();
150   /* case 1 */
151   error = NULL;
152   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
153   g_assert_no_error (error);
154   g_assert (c != NULL);
155   g_assert (!g_dbus_connection_is_closed (c));
156
157   /*
158    * Check that singleton handling work
159    */
160   error = NULL;
161   c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
162   g_assert_no_error (error);
163   g_assert (c2 != NULL);
164   g_assert (c == c2);
165   g_object_unref (c2);
166
167   /*
168    * Check that private connections work
169    */
170   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
171   g_assert_no_error (error);
172   g_assert (c2 != NULL);
173   g_assert (c != c2);
174   g_object_unref (c2);
175
176   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
177   g_assert_no_error (error);
178   g_assert (c2 != NULL);
179   g_assert (!g_dbus_connection_is_closed (c2));
180   ret = g_dbus_connection_close_sync (c2, NULL, &error);
181   g_assert_no_error (error);
182   g_assert (ret);
183   _g_assert_signal_received (c2, "closed");
184   g_assert (g_dbus_connection_is_closed (c2));
185   ret = g_dbus_connection_close_sync (c2, NULL, &error);
186   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
187   g_error_free (error);
188   g_assert (!ret);
189   g_object_unref (c2);
190
191   /*
192    * Check that the finalization code works
193    *
194    * (and that the GDestroyNotify for filters and objects and signal
195    * registrations are run as expected)
196    */
197   error = NULL;
198   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
199   g_assert_no_error (error);
200   g_assert (c2 != NULL);
201   /* signal registration */
202   on_signal_registration_freed_called = FALSE;
203   g_dbus_connection_signal_subscribe (c2,
204                                       "org.freedesktop.DBus", /* bus name */
205                                       "org.freedesktop.DBus", /* interface */
206                                       "NameOwnerChanged",     /* member */
207                                       "/org/freesktop/DBus",  /* path */
208                                       NULL,                   /* arg0 */
209                                       G_DBUS_SIGNAL_FLAGS_NONE,
210                                       on_name_owner_changed,
211                                       (gpointer) &on_signal_registration_freed_called,
212                                       a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
213   /* filter func */
214   on_filter_freed_called = FALSE;
215   g_dbus_connection_add_filter (c2,
216                                 some_filter_func,
217                                 (gpointer) &on_filter_freed_called,
218                                 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
219   /* object registration */
220   on_register_object_freed_called = FALSE;
221   error = NULL;
222   registration_id = g_dbus_connection_register_object (c2,
223                                                        "/foo",
224                                                        (GDBusInterfaceInfo *) &boo_interface_info,
225                                                        &boo_vtable,
226                                                        (gpointer) &on_register_object_freed_called,
227                                                        a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
228                                                        &error);
229   g_assert_no_error (error);
230   g_assert (registration_id > 0);
231   /* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
232   g_object_unref (c2);
233   quit_mainloop_fired = FALSE;
234   quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
235   //_log ("destroynotifies for\n"
236   //      " register_object %p\n"
237   //      " filter          %p\n"
238   //      " signal          %p",
239   //      &on_register_object_freed_called,
240   //      &on_filter_freed_called,
241   //      &on_signal_registration_freed_called);
242   while (TRUE)
243     {
244       if (on_signal_registration_freed_called &&
245           on_filter_freed_called &&
246           on_register_object_freed_called)
247         break;
248       if (quit_mainloop_fired)
249         break;
250       //_log ("entering loop");
251       g_main_loop_run (loop);
252       //_log ("exiting loop");
253     }
254   g_source_remove (quit_mainloop_id);
255   g_assert (on_signal_registration_freed_called);
256   g_assert (on_filter_freed_called);
257   g_assert (on_register_object_freed_called);
258   g_assert (!quit_mainloop_fired);
259
260   /*
261    *  Check for correct behavior when the bus goes away
262    *
263    */
264   g_assert (!g_dbus_connection_is_closed (c));
265   g_dbus_connection_set_exit_on_close (c, FALSE);
266   session_bus_down ();
267   if (!g_dbus_connection_is_closed (c))
268     _g_assert_signal_received (c, "closed");
269   g_assert (g_dbus_connection_is_closed (c));
270
271   _g_object_wait_for_single_ref (c);
272   g_object_unref (c);
273 }
274
275 /* ---------------------------------------------------------------------------------------------------- */
276 /* Test that sending and receiving messages work as expected */
277 /* ---------------------------------------------------------------------------------------------------- */
278
279 static void
280 msg_cb_expect_error_disconnected (GDBusConnection *connection,
281                                   GAsyncResult    *res,
282                                   gpointer         user_data)
283 {
284   GError *error;
285   GVariant *result;
286
287   error = NULL;
288   result = g_dbus_connection_call_finish (connection,
289                                           res,
290                                           &error);
291   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
292   g_assert (!g_dbus_error_is_remote_error (error));
293   g_error_free (error);
294   g_assert (result == NULL);
295
296   g_main_loop_quit (loop);
297 }
298
299 static void
300 msg_cb_expect_error_unknown_method (GDBusConnection *connection,
301                                     GAsyncResult    *res,
302                                     gpointer         user_data)
303 {
304   GError *error;
305   GVariant *result;
306
307   error = NULL;
308   result = g_dbus_connection_call_finish (connection,
309                                           res,
310                                           &error);
311   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
312   g_assert (g_dbus_error_is_remote_error (error));
313   g_error_free (error);
314   g_assert (result == NULL);
315
316   g_main_loop_quit (loop);
317 }
318
319 static void
320 msg_cb_expect_success (GDBusConnection *connection,
321                        GAsyncResult    *res,
322                        gpointer         user_data)
323 {
324   GError *error;
325   GVariant *result;
326
327   error = NULL;
328   result = g_dbus_connection_call_finish (connection,
329                                           res,
330                                           &error);
331   g_assert_no_error (error);
332   g_assert (result != NULL);
333   g_variant_unref (result);
334
335   g_main_loop_quit (loop);
336 }
337
338 static void
339 msg_cb_expect_error_cancelled (GDBusConnection *connection,
340                                GAsyncResult    *res,
341                                gpointer         user_data)
342 {
343   GError *error;
344   GVariant *result;
345
346   error = NULL;
347   result = g_dbus_connection_call_finish (connection,
348                                           res,
349                                           &error);
350   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
351   g_assert (!g_dbus_error_is_remote_error (error));
352   g_error_free (error);
353   g_assert (result == NULL);
354
355   g_main_loop_quit (loop);
356 }
357
358 static void
359 msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
360                                  GAsyncResult    *res,
361                                  gpointer         user_data)
362 {
363   GError *error;
364   GVariant *result;
365
366   error = NULL;
367   result = g_dbus_connection_call_finish (connection,
368                                           res,
369                                           &error);
370   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
371   g_assert (!g_dbus_error_is_remote_error (error));
372   g_error_free (error);
373   g_assert (result == NULL);
374
375   g_main_loop_quit (loop);
376 }
377
378 /* ---------------------------------------------------------------------------------------------------- */
379
380 static void
381 test_connection_send (void)
382 {
383   GDBusConnection *c;
384   GCancellable *ca;
385
386   session_bus_up ();
387
388   /* First, get an unopened connection */
389   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
390   g_assert (c != NULL);
391   g_assert (!g_dbus_connection_is_closed (c));
392
393   /*
394    * Check that we never actually send a message if the GCancellable
395    * is already cancelled - i.e.  we should get #G_IO_ERROR_CANCELLED
396    * when the actual connection is not up.
397    */
398   ca = g_cancellable_new ();
399   g_cancellable_cancel (ca);
400   g_dbus_connection_call (c,
401                           "org.freedesktop.DBus",  /* bus_name */
402                           "/org/freedesktop/DBus", /* object path */
403                           "org.freedesktop.DBus",  /* interface name */
404                           "GetId",                 /* method name */
405                           NULL, NULL,
406                           G_DBUS_CALL_FLAGS_NONE,
407                           -1,
408                           ca,
409                           (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
410                           NULL);
411   g_main_loop_run (loop);
412   g_object_unref (ca);
413
414   /*
415    * Check that we get a reply to the GetId() method call.
416    */
417   g_dbus_connection_call (c,
418                           "org.freedesktop.DBus",  /* bus_name */
419                           "/org/freedesktop/DBus", /* object path */
420                           "org.freedesktop.DBus",  /* interface name */
421                           "GetId",                 /* method name */
422                           NULL, NULL,
423                           G_DBUS_CALL_FLAGS_NONE,
424                           -1,
425                           NULL,
426                           (GAsyncReadyCallback) msg_cb_expect_success,
427                           NULL);
428   g_main_loop_run (loop);
429
430   /*
431    * Check that we get an error reply to the NonExistantMethod() method call.
432    */
433   g_dbus_connection_call (c,
434                           "org.freedesktop.DBus",  /* bus_name */
435                           "/org/freedesktop/DBus", /* object path */
436                           "org.freedesktop.DBus",  /* interface name */
437                           "NonExistantMethod",     /* method name */
438                           NULL, NULL,
439                           G_DBUS_CALL_FLAGS_NONE,
440                           -1,
441                           NULL,
442                           (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
443                           NULL);
444   g_main_loop_run (loop);
445
446   /*
447    * Check that cancellation works when the message is already in flight.
448    */
449   ca = g_cancellable_new ();
450   g_dbus_connection_call (c,
451                           "org.freedesktop.DBus",  /* bus_name */
452                           "/org/freedesktop/DBus", /* object path */
453                           "org.freedesktop.DBus",  /* interface name */
454                           "GetId",                 /* method name */
455                           NULL, NULL,
456                           G_DBUS_CALL_FLAGS_NONE,
457                           -1,
458                           ca,
459                           (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
460                           NULL);
461   g_cancellable_cancel (ca);
462   g_main_loop_run (loop);
463   g_object_unref (ca);
464
465   /*
466    * Check that we get an error when sending to a connection that is disconnected.
467    */
468   g_dbus_connection_set_exit_on_close (c, FALSE);
469   session_bus_down ();
470   _g_assert_signal_received (c, "closed");
471   g_assert (g_dbus_connection_is_closed (c));
472
473   g_dbus_connection_call (c,
474                           "org.freedesktop.DBus",  /* bus_name */
475                           "/org/freedesktop/DBus", /* object path */
476                           "org.freedesktop.DBus",  /* interface name */
477                           "GetId",                 /* method name */
478                           NULL, NULL,
479                           G_DBUS_CALL_FLAGS_NONE,
480                           -1,
481                           NULL,
482                           (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
483                           NULL);
484   g_main_loop_run (loop);
485
486   _g_object_wait_for_single_ref (c);
487   g_object_unref (c);
488 }
489
490 /* ---------------------------------------------------------------------------------------------------- */
491 /* Connection signal tests */
492 /* ---------------------------------------------------------------------------------------------------- */
493
494 static void
495 test_connection_signal_handler (GDBusConnection  *connection,
496                                 const gchar      *sender_name,
497                                 const gchar      *object_path,
498                                 const gchar      *interface_name,
499                                 const gchar      *signal_name,
500                                 GVariant         *parameters,
501                                 gpointer         user_data)
502 {
503   gint *counter = user_data;
504   *counter += 1;
505
506   /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
507            sender_name,
508            object_path,
509            interface_name,
510            signal_name);*/
511
512   g_main_loop_quit (loop);
513 }
514
515 static void
516 test_connection_signals (void)
517 {
518   GDBusConnection *c1;
519   GDBusConnection *c2;
520   GDBusConnection *c3;
521   guint s1;
522   guint s1b;
523   guint s2;
524   guint s3;
525   gint count_s1;
526   gint count_s1b;
527   gint count_s2;
528   gint count_name_owner_changed;
529   GError *error;
530   gboolean ret;
531   GVariant *result;
532   gboolean quit_mainloop_fired;
533   guint quit_mainloop_id;
534
535   error = NULL;
536
537   /*
538    * Bring up first separate connections
539    */
540   session_bus_up ();
541   /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
542    * emulate this
543    */
544   if (g_getenv ("G_DBUS_MONITOR") == NULL)
545     {
546       c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
547       g_assert (c1 != NULL);
548       g_assert (!g_dbus_connection_is_closed (c1));
549       g_object_unref (c1);
550     }
551   c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
552   g_assert (c1 != NULL);
553   g_assert (!g_dbus_connection_is_closed (c1));
554   g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
555
556   /*
557    * Install two signal handlers for the first connection
558    *
559    *  - Listen to the signal "Foo" from :1.2 (e.g. c2)
560    *  - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
561    *
562    * and then count how many times this signal handler was invoked.
563    */
564   s1 = g_dbus_connection_signal_subscribe (c1,
565                                            ":1.2",
566                                            "org.gtk.GDBus.ExampleInterface",
567                                            "Foo",
568                                            "/org/gtk/GDBus/ExampleInterface",
569                                            NULL,
570                                            G_DBUS_SIGNAL_FLAGS_NONE,
571                                            test_connection_signal_handler,
572                                            &count_s1,
573                                            NULL);
574   s2 = g_dbus_connection_signal_subscribe (c1,
575                                            NULL, /* match any sender */
576                                            "org.gtk.GDBus.ExampleInterface",
577                                            "Foo",
578                                            "/org/gtk/GDBus/ExampleInterface",
579                                            NULL,
580                                            G_DBUS_SIGNAL_FLAGS_NONE,
581                                            test_connection_signal_handler,
582                                            &count_s2,
583                                            NULL);
584   s3 = g_dbus_connection_signal_subscribe (c1,
585                                            "org.freedesktop.DBus",  /* sender */
586                                            "org.freedesktop.DBus",  /* interface */
587                                            "NameOwnerChanged",      /* member */
588                                            "/org/freedesktop/DBus", /* path */
589                                            NULL,
590                                            G_DBUS_SIGNAL_FLAGS_NONE,
591                                            test_connection_signal_handler,
592                                            &count_name_owner_changed,
593                                            NULL);
594   /* Note that s1b is *just like* s1 - this is to catch a bug where N
595    * subscriptions of the same rule causes N calls to each of the N
596    * subscriptions instead of just 1 call to each of the N subscriptions.
597    */
598   s1b = g_dbus_connection_signal_subscribe (c1,
599                                             ":1.2",
600                                             "org.gtk.GDBus.ExampleInterface",
601                                             "Foo",
602                                             "/org/gtk/GDBus/ExampleInterface",
603                                             NULL,
604                                             G_DBUS_SIGNAL_FLAGS_NONE,
605                                             test_connection_signal_handler,
606                                             &count_s1b,
607                                             NULL);
608   g_assert (s1 != 0);
609   g_assert (s1b != 0);
610   g_assert (s2 != 0);
611   g_assert (s3 != 0);
612
613   count_s1 = 0;
614   count_s1b = 0;
615   count_s2 = 0;
616   count_name_owner_changed = 0;
617
618   /*
619    * Bring up two other connections
620    */
621   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
622   g_assert (c2 != NULL);
623   g_assert (!g_dbus_connection_is_closed (c2));
624   g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
625   c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
626   g_assert (c3 != NULL);
627   g_assert (!g_dbus_connection_is_closed (c3));
628   g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
629
630   /*
631    * Make c2 emit "Foo" - we should catch it twice
632    *
633    * Note that there is no way to be sure that the signal subscriptions
634    * on c1 are effective yet - for all we know, the AddMatch() messages
635    * could sit waiting in a buffer somewhere between this process and
636    * the message bus. And emitting signals on c2 (a completely other
637    * socket!) will not necessarily change this.
638    *
639    * To ensure this is not the case, do a synchronous call on c1.
640    */
641   result = g_dbus_connection_call_sync (c1,
642                                         "org.freedesktop.DBus",  /* bus name */
643                                         "/org/freedesktop/DBus", /* object path */
644                                         "org.freedesktop.DBus",  /* interface name */
645                                         "GetId",                 /* method name */
646                                         NULL,                    /* parameters */
647                                         NULL,                    /* return type */
648                                         G_DBUS_CALL_FLAGS_NONE,
649                                         -1,
650                                         NULL,
651                                         &error);
652   g_assert_no_error (error);
653   g_assert (result != NULL);
654   g_variant_unref (result);
655   /* now, emit the signal on c2 */
656   ret = g_dbus_connection_emit_signal (c2,
657                                        NULL, /* destination bus name */
658                                        "/org/gtk/GDBus/ExampleInterface",
659                                        "org.gtk.GDBus.ExampleInterface",
660                                        "Foo",
661                                        NULL,
662                                        &error);
663   g_assert_no_error (error);
664   g_assert (ret);
665   while (!(count_s1 >= 1 && count_s2 >= 1))
666     g_main_loop_run (loop);
667   g_assert_cmpint (count_s1, ==, 1);
668   g_assert_cmpint (count_s2, ==, 1);
669
670   /*
671    * Make c3 emit "Foo" - we should catch it only once
672    */
673   ret = g_dbus_connection_emit_signal (c3,
674                                        NULL, /* destination bus name */
675                                        "/org/gtk/GDBus/ExampleInterface",
676                                        "org.gtk.GDBus.ExampleInterface",
677                                        "Foo",
678                                        NULL,
679                                        &error);
680   g_assert_no_error (error);
681   g_assert (ret);
682   while (!(count_s1 == 1 && count_s2 == 2))
683     g_main_loop_run (loop);
684   g_assert_cmpint (count_s1, ==, 1);
685   g_assert_cmpint (count_s2, ==, 2);
686
687   /*
688    * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
689    * to avoid spinning forever
690    */
691   quit_mainloop_fired = FALSE;
692   quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, &quit_mainloop_fired);
693   while (count_name_owner_changed < 2 && !quit_mainloop_fired)
694     g_main_loop_run (loop);
695   g_source_remove (quit_mainloop_id);
696   g_assert_cmpint (count_s1, ==, 1);
697   g_assert_cmpint (count_s2, ==, 2);
698   g_assert_cmpint (count_name_owner_changed, ==, 2);
699
700   g_dbus_connection_signal_unsubscribe (c1, s1);
701   g_dbus_connection_signal_unsubscribe (c1, s2);
702   g_dbus_connection_signal_unsubscribe (c1, s3);
703   g_dbus_connection_signal_unsubscribe (c1, s1b);
704
705   _g_object_wait_for_single_ref (c1);
706   _g_object_wait_for_single_ref (c2);
707   _g_object_wait_for_single_ref (c3);
708
709   g_object_unref (c1);
710   g_object_unref (c2);
711   g_object_unref (c3);
712
713   session_bus_down ();
714 }
715
716 /* ---------------------------------------------------------------------------------------------------- */
717
718 typedef struct
719 {
720   guint num_handled;
721   guint num_outgoing;
722   guint32 serial;
723 } FilterData;
724
725 static GDBusMessage *
726 filter_func (GDBusConnection *connection,
727              GDBusMessage    *message,
728              gboolean         incoming,
729              gpointer         user_data)
730 {
731   FilterData *data = user_data;
732   guint32 reply_serial;
733
734   if (incoming)
735     {
736       reply_serial = g_dbus_message_get_reply_serial (message);
737       if (reply_serial == data->serial)
738         data->num_handled += 1;
739     }
740   else
741     {
742       data->num_outgoing += 1;
743     }
744
745   return message;
746 }
747
748
749 typedef struct
750 {
751   gboolean alter_incoming;
752   gboolean alter_outgoing;
753 } FilterEffects;
754
755 static GDBusMessage *
756 other_filter_func (GDBusConnection *connection,
757                    GDBusMessage    *message,
758                    gboolean         incoming,
759                    gpointer         user_data)
760 {
761   FilterEffects *effects = user_data;
762   GDBusMessage *ret;
763   gboolean alter;
764
765   if (incoming)
766     alter = effects->alter_incoming;
767   else
768     alter = effects->alter_outgoing;
769
770   if (alter)
771     {
772       GDBusMessage *copy;
773       GVariant *body;
774       gchar *s;
775       gchar *s2;
776
777       copy = g_dbus_message_copy (message, NULL);
778       g_object_unref (message);
779
780       body = g_dbus_message_get_body (copy);
781       g_variant_get (body, "(s)", &s);
782       s2 = g_strdup_printf ("MOD: %s", s);
783       g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
784       g_free (s2);
785       g_free (s);
786
787       ret = copy;
788     }
789   else
790     {
791       ret = message;
792     }
793
794   return ret;
795 }
796
797 static void
798 test_connection_filter_name_owner_changed_signal_handler (GDBusConnection  *connection,
799                                                           const gchar      *sender_name,
800                                                           const gchar      *object_path,
801                                                           const gchar      *interface_name,
802                                                           const gchar      *signal_name,
803                                                           GVariant         *parameters,
804                                                           gpointer         user_data)
805 {
806   const gchar *name;
807   const gchar *old_owner;
808   const gchar *new_owner;
809
810   g_variant_get (parameters,
811                  "(&s&s&s)",
812                  &name,
813                  &old_owner,
814                  &new_owner);
815
816   if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
817     {
818       g_main_loop_quit (loop);
819     }
820 }
821
822 static gboolean
823 test_connection_filter_on_timeout (gpointer user_data)
824 {
825   g_printerr ("Timeout waiting 30 sec on service\n");
826   g_assert_not_reached ();
827   return FALSE;
828 }
829
830 static void
831 test_connection_filter (void)
832 {
833   GDBusConnection *c;
834   FilterData data;
835   GDBusMessage *m;
836   GDBusMessage *m2;
837   GDBusMessage *r;
838   GError *error;
839   guint filter_id;
840   guint timeout_mainloop_id;
841   guint signal_handler_id;
842   FilterEffects effects;
843   GVariant *result;
844   const gchar *s;
845
846   memset (&data, '\0', sizeof (FilterData));
847
848   session_bus_up ();
849
850   error = NULL;
851   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
852   g_assert_no_error (error);
853   g_assert (c != NULL);
854
855   filter_id = g_dbus_connection_add_filter (c,
856                                             filter_func,
857                                             &data,
858                                             NULL);
859
860   m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
861                                       "/org/freedesktop/DBus", /* path */
862                                       "org.freedesktop.DBus", /* interface */
863                                       "GetNameOwner");
864   g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
865   error = NULL;
866   g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
867   g_assert_no_error (error);
868
869   while (data.num_handled == 0)
870     g_thread_yield ();
871
872   m2 = g_dbus_message_copy (m, &error);
873   g_assert_no_error (error);
874   g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
875   g_object_unref (m2);
876   g_assert_no_error (error);
877
878   while (data.num_handled == 1)
879     g_thread_yield ();
880
881   m2 = g_dbus_message_copy (m, &error);
882   g_assert_no_error (error);
883   g_dbus_message_set_serial (m2, data.serial);
884   /* lock the message to test PRESERVE_SERIAL flag. */
885   g_dbus_message_lock (m2);
886   g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
887   g_object_unref (m2);
888   g_assert_no_error (error);
889
890   while (data.num_handled == 2)
891     g_thread_yield ();
892
893   m2 = g_dbus_message_copy (m, &error);
894   g_assert_no_error (error);
895   r = g_dbus_connection_send_message_with_reply_sync (c,
896                                                       m2,
897                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
898                                                       -1,
899                                                       &data.serial,
900                                                       NULL, /* GCancellable */
901                                                       &error);
902   g_object_unref (m2);
903   g_assert_no_error (error);
904   g_assert (r != NULL);
905   g_object_unref (r);
906   g_assert_cmpint (data.num_handled, ==, 4);
907
908   g_dbus_connection_remove_filter (c, filter_id);
909
910   m2 = g_dbus_message_copy (m, &error);
911   g_assert_no_error (error);
912   r = g_dbus_connection_send_message_with_reply_sync (c,
913                                                       m2,
914                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
915                                                       -1,
916                                                       &data.serial,
917                                                       NULL, /* GCancellable */
918                                                       &error);
919   g_object_unref (m2);
920   g_assert_no_error (error);
921   g_assert (r != NULL);
922   g_object_unref (r);
923   g_assert_cmpint (data.num_handled, ==, 4);
924   g_assert_cmpint (data.num_outgoing, ==, 4);
925
926   /* this is safe; testserver will exit once the bus goes away */
927   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
928   /* wait for service to be available */
929   signal_handler_id = g_dbus_connection_signal_subscribe (c,
930                                                           "org.freedesktop.DBus", /* sender */
931                                                           "org.freedesktop.DBus",
932                                                           "NameOwnerChanged",
933                                                           "/org/freedesktop/DBus",
934                                                           NULL, /* arg0 */
935                                                           G_DBUS_SIGNAL_FLAGS_NONE,
936                                                           test_connection_filter_name_owner_changed_signal_handler,
937                                                           NULL,
938                                                           NULL);
939   g_assert_cmpint (signal_handler_id, !=, 0);
940   timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
941   g_main_loop_run (loop);
942   g_source_remove (timeout_mainloop_id);
943   g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
944
945   /* now test some combinations... */
946   filter_id = g_dbus_connection_add_filter (c,
947                                             other_filter_func,
948                                             &effects,
949                                             NULL);
950   /* -- */
951   effects.alter_incoming = FALSE;
952   effects.alter_outgoing = FALSE;
953   error = NULL;
954   result = g_dbus_connection_call_sync (c,
955                                         "com.example.TestService",      /* bus name */
956                                         "/com/example/TestObject",      /* object path */
957                                         "com.example.Frob",             /* interface name */
958                                         "HelloWorld",                   /* method name */
959                                         g_variant_new ("(s)", "Cat"),   /* parameters */
960                                         G_VARIANT_TYPE ("(s)"),         /* return type */
961                                         G_DBUS_CALL_FLAGS_NONE,
962                                         -1,
963                                         NULL,
964                                         &error);
965   g_assert_no_error (error);
966   g_variant_get (result, "(&s)", &s);
967   g_assert_cmpstr (s, ==, "You greeted me with 'Cat'. Thanks!");
968   g_variant_unref (result);
969   /* -- */
970   effects.alter_incoming = TRUE;
971   effects.alter_outgoing = TRUE;
972   error = NULL;
973   result = g_dbus_connection_call_sync (c,
974                                         "com.example.TestService",      /* bus name */
975                                         "/com/example/TestObject",      /* object path */
976                                         "com.example.Frob",             /* interface name */
977                                         "HelloWorld",                   /* method name */
978                                         g_variant_new ("(s)", "Cat"),   /* parameters */
979                                         G_VARIANT_TYPE ("(s)"),         /* return type */
980                                         G_DBUS_CALL_FLAGS_NONE,
981                                         -1,
982                                         NULL,
983                                         &error);
984   g_assert_no_error (error);
985   g_variant_get (result, "(&s)", &s);
986   g_assert_cmpstr (s, ==, "MOD: You greeted me with 'MOD: Cat'. Thanks!");
987   g_variant_unref (result);
988
989
990   g_dbus_connection_remove_filter (c, filter_id);
991
992   _g_object_wait_for_single_ref (c);
993   g_object_unref (c);
994   g_object_unref (m);
995
996   session_bus_down ();
997 }
998
999 static void
1000 test_connection_basic (void)
1001 {
1002   GDBusConnection *connection;
1003   GError *error;
1004   GDBusCapabilityFlags flags;
1005   gchar *guid;
1006   gchar *name;
1007   gboolean closed;
1008   gboolean exit_on_close;
1009   GIOStream *stream;
1010   GCredentials *credentials;
1011
1012   session_bus_up ();
1013
1014   error = NULL;
1015   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1016   g_assert_no_error (error);
1017   g_assert (connection != NULL);
1018
1019   flags = g_dbus_connection_get_capabilities (connection);
1020   g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1021             flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1022
1023   credentials = g_dbus_connection_get_peer_credentials (connection);
1024   g_assert (credentials == NULL);
1025
1026   g_object_get (connection,
1027                 "stream", &stream,
1028                 "guid", &guid,
1029                 "unique-name", &name,
1030                 "closed", &closed,
1031                 "exit-on-close", &exit_on_close,
1032                 "capabilities", &flags,
1033                 NULL);
1034
1035   g_assert (G_IS_IO_STREAM (stream));
1036   g_assert (g_dbus_is_guid (guid));
1037   g_assert (g_dbus_is_unique_name (name));
1038   g_assert (!closed);
1039   g_assert (exit_on_close);
1040   g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1041             flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1042   g_object_unref (stream);
1043   g_free (name);
1044   g_free (guid);
1045
1046   _g_object_wait_for_single_ref (connection);
1047   g_object_unref (connection);
1048
1049   session_bus_down ();
1050 }
1051
1052 /* ---------------------------------------------------------------------------------------------------- */
1053
1054 int
1055 main (int   argc,
1056       char *argv[])
1057 {
1058   g_type_init ();
1059   g_test_init (&argc, &argv, NULL);
1060
1061   /* all the tests rely on a shared main loop */
1062   loop = g_main_loop_new (NULL, FALSE);
1063
1064   /* all the tests use a session bus with a well-known address that we can bring up and down
1065    * using session_bus_up() and session_bus_down().
1066    */
1067   g_unsetenv ("DISPLAY");
1068   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1069
1070   g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
1071   g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
1072   g_test_add_func ("/gdbus/connection/send", test_connection_send);
1073   g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
1074   g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
1075   return g_test_run();
1076 }