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