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