Fix make check with builddir != srcdir
[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
29 #include "gdbus-tests.h"
30
31 const gchar *srcdir;
32 const gchar *builddir;
33
34 /* all tests rely on a shared mainloop */
35 static GMainLoop *loop = NULL;
36
37 #if 0
38 G_GNUC_UNUSED static void
39 _log (const gchar *format, ...)
40 {
41   GTimeVal now;
42   time_t now_time;
43   struct tm *now_tm;
44   gchar time_buf[128];
45   gchar *str;
46   va_list var_args;
47
48   va_start (var_args, format);
49   str = g_strdup_vprintf (format, var_args);
50   va_end (var_args);
51
52   g_get_current_time (&now);
53   now_time = (time_t) now.tv_sec;
54   now_tm = localtime (&now_time);
55   strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
56
57   g_print ("%s.%06d: %s\n",
58            time_buf, (gint) now.tv_usec / 1000,
59            str);
60   g_free (str);
61 }
62 #else
63 #define _log(...)
64 #endif
65
66 static gboolean
67 test_connection_quit_mainloop (gpointer user_data)
68 {
69   volatile gboolean *quit_mainloop_fired = user_data;
70   _log ("quit_mainloop_fired");
71   *quit_mainloop_fired = TRUE;
72   g_main_loop_quit (loop);
73   return TRUE;
74 }
75
76 /* ---------------------------------------------------------------------------------------------------- */
77 /* Connection life-cycle testing */
78 /* ---------------------------------------------------------------------------------------------------- */
79
80 static const GDBusInterfaceInfo boo_interface_info =
81 {
82   -1,
83   "org.example.Boo",
84   (GDBusMethodInfo **) NULL,
85   (GDBusSignalInfo **) NULL,
86   (GDBusPropertyInfo **) NULL,
87   NULL,
88 };
89
90 static const GDBusInterfaceVTable boo_vtable =
91 {
92   NULL, /* _method_call */
93   NULL, /* _get_property */
94   NULL  /* _set_property */
95 };
96
97 static GDBusMessage *
98 some_filter_func (GDBusConnection *connection,
99                   GDBusMessage    *message,
100                   gboolean         incoming,
101                   gpointer         user_data)
102 {
103   return message;
104 }
105
106 static void
107 on_name_owner_changed (GDBusConnection *connection,
108                        const gchar     *sender_name,
109                        const gchar     *object_path,
110                        const gchar     *interface_name,
111                        const gchar     *signal_name,
112                        GVariant        *parameters,
113                        gpointer         user_data)
114 {
115 }
116
117 static void
118 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
119 {
120   volatile gboolean *val = user_data;
121   *val = TRUE;
122   _log ("destroynotify fired for %p", val);
123   g_main_loop_quit (loop);
124 }
125
126 static void
127 test_connection_life_cycle (void)
128 {
129   gboolean ret;
130   GDBusConnection *c;
131   GDBusConnection *c2;
132   GError *error;
133   volatile gboolean on_signal_registration_freed_called;
134   volatile gboolean on_filter_freed_called;
135   volatile gboolean on_register_object_freed_called;
136   volatile gboolean quit_mainloop_fired;
137   guint quit_mainloop_id;
138   guint registration_id;
139
140   error = NULL;
141
142   /*
143    * Check for correct behavior when no bus is present
144    *
145    */
146   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
147   g_assert (error != NULL);
148   g_assert (!g_dbus_error_is_remote_error (error));
149   g_assert (c == NULL);
150   g_error_free (error);
151
152   /*
153    *  Check for correct behavior when a bus is present
154    */
155   session_bus_up ();
156   /* case 1 */
157   error = NULL;
158   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
159   g_assert_no_error (error);
160   g_assert (c != NULL);
161   g_assert (!g_dbus_connection_is_closed (c));
162
163   /*
164    * Check that singleton handling work
165    */
166   error = NULL;
167   c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
168   g_assert_no_error (error);
169   g_assert (c2 != NULL);
170   g_assert (c == c2);
171   g_object_unref (c2);
172
173   /*
174    * Check that private connections work
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 (c != c2);
180   g_object_unref (c2);
181
182   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
183   g_assert_no_error (error);
184   g_assert (c2 != NULL);
185   g_assert (!g_dbus_connection_is_closed (c2));
186   ret = g_dbus_connection_close_sync (c2, NULL, &error);
187   g_assert_no_error (error);
188   g_assert (ret);
189   _g_assert_signal_received (c2, "closed");
190   g_assert (g_dbus_connection_is_closed (c2));
191   ret = g_dbus_connection_close_sync (c2, NULL, &error);
192   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
193   g_error_free (error);
194   g_assert (!ret);
195   g_object_unref (c2);
196
197   /*
198    * Check that the finalization code works
199    *
200    * (and that the GDestroyNotify for filters and objects and signal
201    * registrations are run as expected)
202    */
203   error = NULL;
204   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
205   g_assert_no_error (error);
206   g_assert (c2 != NULL);
207   /* signal registration */
208   on_signal_registration_freed_called = FALSE;
209   g_dbus_connection_signal_subscribe (c2,
210                                       "org.freedesktop.DBus", /* bus name */
211                                       "org.freedesktop.DBus", /* interface */
212                                       "NameOwnerChanged",     /* member */
213                                       "/org/freesktop/DBus",  /* path */
214                                       NULL,                   /* arg0 */
215                                       G_DBUS_SIGNAL_FLAGS_NONE,
216                                       on_name_owner_changed,
217                                       (gpointer) &on_signal_registration_freed_called,
218                                       a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
219   /* filter func */
220   on_filter_freed_called = FALSE;
221   g_dbus_connection_add_filter (c2,
222                                 some_filter_func,
223                                 (gpointer) &on_filter_freed_called,
224                                 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
225   /* object registration */
226   on_register_object_freed_called = FALSE;
227   error = NULL;
228   registration_id = g_dbus_connection_register_object (c2,
229                                                        "/foo",
230                                                        (GDBusInterfaceInfo *) &boo_interface_info,
231                                                        &boo_vtable,
232                                                        (gpointer) &on_register_object_freed_called,
233                                                        a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
234                                                        &error);
235   g_assert_no_error (error);
236   g_assert (registration_id > 0);
237   /* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
238   g_object_unref (c2);
239   quit_mainloop_fired = FALSE;
240   quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
241   _log ("destroynotifies for\n"
242         " register_object %p\n"
243         " filter          %p\n"
244         " signal          %p",
245         &on_register_object_freed_called,
246         &on_filter_freed_called,
247         &on_signal_registration_freed_called);
248   while (TRUE)
249     {
250       if (on_signal_registration_freed_called &&
251           on_filter_freed_called &&
252           on_register_object_freed_called)
253         break;
254       if (quit_mainloop_fired)
255         break;
256       _log ("entering loop");
257       g_main_loop_run (loop);
258       _log ("exiting loop");
259     }
260   g_source_remove (quit_mainloop_id);
261   g_assert (on_signal_registration_freed_called);
262   g_assert (on_filter_freed_called);
263   g_assert (on_register_object_freed_called);
264   g_assert (!quit_mainloop_fired);
265
266   /*
267    *  Check for correct behavior when the bus goes away
268    *
269    */
270   g_assert (!g_dbus_connection_is_closed (c));
271   g_dbus_connection_set_exit_on_close (c, FALSE);
272   session_bus_stop ();
273   _g_assert_signal_received (c, "closed");
274   g_assert (g_dbus_connection_is_closed (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_unref (c);
492
493   session_bus_down ();
494 }
495
496 /* ---------------------------------------------------------------------------------------------------- */
497 /* Connection signal tests */
498 /* ---------------------------------------------------------------------------------------------------- */
499
500 static void
501 test_connection_signal_handler (GDBusConnection  *connection,
502                                 const gchar      *sender_name,
503                                 const gchar      *object_path,
504                                 const gchar      *interface_name,
505                                 const gchar      *signal_name,
506                                 GVariant         *parameters,
507                                 gpointer         user_data)
508 {
509   gint *counter = user_data;
510   *counter += 1;
511
512   /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
513            sender_name,
514            object_path,
515            interface_name,
516            signal_name);*/
517
518   g_main_loop_quit (loop);
519 }
520
521 static void
522 test_connection_signals (void)
523 {
524   GDBusConnection *c1;
525   GDBusConnection *c2;
526   GDBusConnection *c3;
527   guint s1;
528   guint s1b;
529   guint s2;
530   guint s3;
531   gint count_s1;
532   gint count_s1b;
533   gint count_s2;
534   gint count_name_owner_changed;
535   GError *error;
536   gboolean ret;
537   GVariant *result;
538   gboolean quit_mainloop_fired;
539   guint quit_mainloop_id;
540
541   error = NULL;
542
543   /*
544    * Bring up first separate connections
545    */
546   session_bus_up ();
547   /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
548    * emulate this
549    */
550   if (g_getenv ("G_DBUS_MONITOR") == NULL)
551     {
552       c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
553       g_assert (c1 != NULL);
554       g_assert (!g_dbus_connection_is_closed (c1));
555       g_object_unref (c1);
556     }
557   c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
558   g_assert (c1 != NULL);
559   g_assert (!g_dbus_connection_is_closed (c1));
560   g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
561
562   /*
563    * Install two signal handlers for the first connection
564    *
565    *  - Listen to the signal "Foo" from :1.2 (e.g. c2)
566    *  - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
567    *
568    * and then count how many times this signal handler was invoked.
569    */
570   s1 = g_dbus_connection_signal_subscribe (c1,
571                                            ":1.2",
572                                            "org.gtk.GDBus.ExampleInterface",
573                                            "Foo",
574                                            "/org/gtk/GDBus/ExampleInterface",
575                                            NULL,
576                                            G_DBUS_SIGNAL_FLAGS_NONE,
577                                            test_connection_signal_handler,
578                                            &count_s1,
579                                            NULL);
580   s2 = g_dbus_connection_signal_subscribe (c1,
581                                            NULL, /* match any sender */
582                                            "org.gtk.GDBus.ExampleInterface",
583                                            "Foo",
584                                            "/org/gtk/GDBus/ExampleInterface",
585                                            NULL,
586                                            G_DBUS_SIGNAL_FLAGS_NONE,
587                                            test_connection_signal_handler,
588                                            &count_s2,
589                                            NULL);
590   s3 = g_dbus_connection_signal_subscribe (c1,
591                                            "org.freedesktop.DBus",  /* sender */
592                                            "org.freedesktop.DBus",  /* interface */
593                                            "NameOwnerChanged",      /* member */
594                                            "/org/freedesktop/DBus", /* path */
595                                            NULL,
596                                            G_DBUS_SIGNAL_FLAGS_NONE,
597                                            test_connection_signal_handler,
598                                            &count_name_owner_changed,
599                                            NULL);
600   /* Note that s1b is *just like* s1 - this is to catch a bug where N
601    * subscriptions of the same rule causes N calls to each of the N
602    * subscriptions instead of just 1 call to each of the N subscriptions.
603    */
604   s1b = g_dbus_connection_signal_subscribe (c1,
605                                             ":1.2",
606                                             "org.gtk.GDBus.ExampleInterface",
607                                             "Foo",
608                                             "/org/gtk/GDBus/ExampleInterface",
609                                             NULL,
610                                             G_DBUS_SIGNAL_FLAGS_NONE,
611                                             test_connection_signal_handler,
612                                             &count_s1b,
613                                             NULL);
614   g_assert (s1 != 0);
615   g_assert (s1b != 0);
616   g_assert (s2 != 0);
617   g_assert (s3 != 0);
618
619   count_s1 = 0;
620   count_s1b = 0;
621   count_s2 = 0;
622   count_name_owner_changed = 0;
623
624   /*
625    * Make c2 emit "Foo" - we should catch it twice
626    *
627    * Note that there is no way to be sure that the signal subscriptions
628    * on c1 are effective yet - for all we know, the AddMatch() messages
629    * could sit waiting in a buffer somewhere between this process and
630    * the message bus. And emitting signals on c2 (a completely other
631    * socket!) will not necessarily change this.
632    *
633    * To ensure this is not the case, do a synchronous call on c1.
634    */
635   result = g_dbus_connection_call_sync (c1,
636                                         "org.freedesktop.DBus",  /* bus name */
637                                         "/org/freedesktop/DBus", /* object path */
638                                         "org.freedesktop.DBus",  /* interface name */
639                                         "GetId",                 /* method name */
640                                         NULL,                    /* parameters */
641                                         NULL,                    /* return type */
642                                         G_DBUS_CALL_FLAGS_NONE,
643                                         -1,
644                                         NULL,
645                                         &error);
646   g_assert_no_error (error);
647   g_assert (result != NULL);
648   g_variant_unref (result);
649
650   /*
651    * Bring up two other connections
652    */
653   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
654   g_assert (c2 != NULL);
655   g_assert (!g_dbus_connection_is_closed (c2));
656   g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
657   c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
658   g_assert (c3 != NULL);
659   g_assert (!g_dbus_connection_is_closed (c3));
660   g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
661
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 static void
720 test_match_rule (GDBusConnection  *connection,
721                  GDBusSignalFlags  flags,
722                  gchar            *arg0_rule,
723                  gchar            *arg0,
724                  gboolean          should_match)
725 {
726   guint subscription_ids[2];
727   gint emissions = 0;
728   gint matches = 0;
729   GError *error = NULL;
730
731   subscription_ids[0] = g_dbus_connection_signal_subscribe (connection,
732                                                             NULL, "org.gtk.ExampleInterface", "Foo", "/",
733                                                             NULL,
734                                                             G_DBUS_SIGNAL_FLAGS_NONE,
735                                                             test_connection_signal_handler,
736                                                             &emissions, NULL);
737   subscription_ids[1] = g_dbus_connection_signal_subscribe (connection,
738                                                             NULL, "org.gtk.ExampleInterface", "Foo", "/",
739                                                             arg0_rule,
740                                                             flags,
741                                                             test_connection_signal_handler,
742                                                             &matches, NULL);
743   g_assert_cmpint (subscription_ids[0], !=, 0);
744   g_assert_cmpint (subscription_ids[1], !=, 0);
745
746   g_dbus_connection_emit_signal (connection,
747                                  NULL, "/", "org.gtk.ExampleInterface",
748                                  "Foo", g_variant_new ("(s)", arg0),
749                                  &error);
750   g_assert_no_error (error);
751
752   /* synchronously ping a non-existent method to make sure the signals are dispatched */
753   g_dbus_connection_call_sync (connection, "org.gtk.ExampleInterface", "/", "org.gtk.ExampleInterface",
754                                "Bar", g_variant_new ("()"), G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE,
755                                -1, NULL, NULL);
756
757   while (g_main_context_iteration (NULL, FALSE))
758     ;
759
760   g_assert_cmpint (emissions, ==, 1);
761   g_assert_cmpint (matches, ==, should_match ? 1 : 0);
762
763   g_dbus_connection_signal_unsubscribe (connection, subscription_ids[0]);
764   g_dbus_connection_signal_unsubscribe (connection, subscription_ids[1]);
765 }
766
767 static void
768 test_connection_signal_match_rules (void)
769 {
770   GDBusConnection *con;
771
772   session_bus_up ();
773   con = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
774
775   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "foo", TRUE);
776   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "bar", FALSE);
777
778   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "", FALSE);
779   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org", FALSE);
780   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk", TRUE);
781   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk.Example", TRUE);
782   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk+", FALSE);
783
784   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "/", TRUE);
785   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "", FALSE);
786   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/Example", TRUE);
787   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/", "/org/gtk/Example", TRUE);
788   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/", TRUE);
789   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk", FALSE);
790   test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk+", "/org/gtk", FALSE);
791
792   g_object_unref (con);
793   session_bus_down ();
794 }
795
796 /* ---------------------------------------------------------------------------------------------------- */
797
798 typedef struct
799 {
800   guint num_handled;
801   guint num_outgoing;
802   guint32 serial;
803 } FilterData;
804
805 static GDBusMessage *
806 filter_func (GDBusConnection *connection,
807              GDBusMessage    *message,
808              gboolean         incoming,
809              gpointer         user_data)
810 {
811   FilterData *data = user_data;
812   guint32 reply_serial;
813
814   if (incoming)
815     {
816       reply_serial = g_dbus_message_get_reply_serial (message);
817       if (reply_serial == data->serial)
818         data->num_handled += 1;
819     }
820   else
821     {
822       data->num_outgoing += 1;
823     }
824
825   return message;
826 }
827
828
829 typedef struct
830 {
831   gboolean alter_incoming;
832   gboolean alter_outgoing;
833 } FilterEffects;
834
835 static GDBusMessage *
836 other_filter_func (GDBusConnection *connection,
837                    GDBusMessage    *message,
838                    gboolean         incoming,
839                    gpointer         user_data)
840 {
841   FilterEffects *effects = user_data;
842   GDBusMessage *ret;
843   gboolean alter;
844
845   if (incoming)
846     alter = effects->alter_incoming;
847   else
848     alter = effects->alter_outgoing;
849
850   if (alter)
851     {
852       GDBusMessage *copy;
853       GVariant *body;
854       gchar *s;
855       gchar *s2;
856
857       copy = g_dbus_message_copy (message, NULL);
858       g_object_unref (message);
859
860       body = g_dbus_message_get_body (copy);
861       g_variant_get (body, "(s)", &s);
862       s2 = g_strdup_printf ("MOD: %s", s);
863       g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
864       g_free (s2);
865       g_free (s);
866
867       ret = copy;
868     }
869   else
870     {
871       ret = message;
872     }
873
874   return ret;
875 }
876
877 static void
878 test_connection_filter_name_owner_changed_signal_handler (GDBusConnection  *connection,
879                                                           const gchar      *sender_name,
880                                                           const gchar      *object_path,
881                                                           const gchar      *interface_name,
882                                                           const gchar      *signal_name,
883                                                           GVariant         *parameters,
884                                                           gpointer         user_data)
885 {
886   const gchar *name;
887   const gchar *old_owner;
888   const gchar *new_owner;
889
890   g_variant_get (parameters,
891                  "(&s&s&s)",
892                  &name,
893                  &old_owner,
894                  &new_owner);
895
896   if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
897     {
898       g_main_loop_quit (loop);
899     }
900 }
901
902 static gboolean
903 test_connection_filter_on_timeout (gpointer user_data)
904 {
905   g_printerr ("Timeout waiting 30 sec on service\n");
906   g_assert_not_reached ();
907   return FALSE;
908 }
909
910 static void
911 test_connection_filter (void)
912 {
913   GDBusConnection *c;
914   FilterData data;
915   GDBusMessage *m;
916   GDBusMessage *m2;
917   GDBusMessage *r;
918   GError *error;
919   guint filter_id;
920   guint timeout_mainloop_id;
921   guint signal_handler_id;
922   FilterEffects effects;
923   GVariant *result;
924   const gchar *s;
925   gchar *path;
926
927   memset (&data, '\0', sizeof (FilterData));
928
929   session_bus_up ();
930
931   error = NULL;
932   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
933   g_assert_no_error (error);
934   g_assert (c != NULL);
935
936   filter_id = g_dbus_connection_add_filter (c,
937                                             filter_func,
938                                             &data,
939                                             NULL);
940
941   m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
942                                       "/org/freedesktop/DBus", /* path */
943                                       "org.freedesktop.DBus", /* interface */
944                                       "GetNameOwner");
945   g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
946   error = NULL;
947   g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
948   g_assert_no_error (error);
949
950   while (data.num_handled == 0)
951     g_thread_yield ();
952
953   m2 = g_dbus_message_copy (m, &error);
954   g_assert_no_error (error);
955   g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
956   g_object_unref (m2);
957   g_assert_no_error (error);
958
959   while (data.num_handled == 1)
960     g_thread_yield ();
961
962   m2 = g_dbus_message_copy (m, &error);
963   g_assert_no_error (error);
964   g_dbus_message_set_serial (m2, data.serial);
965   /* lock the message to test PRESERVE_SERIAL flag. */
966   g_dbus_message_lock (m2);
967   g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
968   g_object_unref (m2);
969   g_assert_no_error (error);
970
971   while (data.num_handled == 2)
972     g_thread_yield ();
973
974   m2 = g_dbus_message_copy (m, &error);
975   g_assert_no_error (error);
976   r = g_dbus_connection_send_message_with_reply_sync (c,
977                                                       m2,
978                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
979                                                       -1,
980                                                       &data.serial,
981                                                       NULL, /* GCancellable */
982                                                       &error);
983   g_object_unref (m2);
984   g_assert_no_error (error);
985   g_assert (r != NULL);
986   g_object_unref (r);
987   g_assert_cmpint (data.num_handled, ==, 4);
988
989   g_dbus_connection_remove_filter (c, filter_id);
990
991   m2 = g_dbus_message_copy (m, &error);
992   g_assert_no_error (error);
993   r = g_dbus_connection_send_message_with_reply_sync (c,
994                                                       m2,
995                                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
996                                                       -1,
997                                                       &data.serial,
998                                                       NULL, /* GCancellable */
999                                                       &error);
1000   g_object_unref (m2);
1001   g_assert_no_error (error);
1002   g_assert (r != NULL);
1003   g_object_unref (r);
1004   g_assert_cmpint (data.num_handled, ==, 4);
1005   g_assert_cmpint (data.num_outgoing, ==, 4);
1006
1007   /* this is safe; testserver will exit once the bus goes away */
1008   path = g_build_filename (builddir, "gdbus-testserver", NULL);
1009   g_assert (g_spawn_command_line_async (path, NULL));
1010   g_free (path);
1011
1012   /* wait for service to be available */
1013   signal_handler_id = g_dbus_connection_signal_subscribe (c,
1014                                                           "org.freedesktop.DBus", /* sender */
1015                                                           "org.freedesktop.DBus",
1016                                                           "NameOwnerChanged",
1017                                                           "/org/freedesktop/DBus",
1018                                                           NULL, /* arg0 */
1019                                                           G_DBUS_SIGNAL_FLAGS_NONE,
1020                                                           test_connection_filter_name_owner_changed_signal_handler,
1021                                                           NULL,
1022                                                           NULL);
1023   g_assert_cmpint (signal_handler_id, !=, 0);
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 }
1101
1102 static gpointer
1103 serials_thread_func (GDBusConnection *c)
1104 {
1105   guint32 message_serial;
1106
1107   /* No calls on this thread yet */
1108   g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
1109
1110   /* Send a bogus message and store its serial */
1111   message_serial = 0;
1112   send_bogus_message (c, &message_serial);
1113
1114   /* Give it some time to actually send the message out */
1115   g_usleep (250000);
1116
1117   g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
1118   g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
1119
1120   return NULL;
1121 }
1122
1123 static void
1124 test_connection_serials (void)
1125 {
1126   GDBusConnection *c;
1127   GError *error;
1128   GThread *pool[NUM_THREADS];
1129   int i;
1130
1131   session_bus_up ();
1132
1133   error = NULL;
1134   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1135   g_assert_no_error (error);
1136   g_assert (c != NULL);
1137
1138   /* Status after initialization */
1139   g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
1140
1141   /* Send a bogus message */
1142   send_bogus_message (c, NULL);
1143   g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1144
1145   /* Start the threads */
1146   for (i = 0; i < NUM_THREADS; i++)
1147     pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
1148
1149   /* Wait until threads are finished */
1150   for (i = 0; i < NUM_THREADS; i++)
1151     {
1152       g_thread_join (pool[i]);
1153       g_thread_unref (pool[i]);
1154     }
1155
1156   /* No calls in between on this thread, should be the last value */
1157   g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1158
1159   send_bogus_message (c, NULL);
1160
1161   /* All above calls + calls in threads */
1162   g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
1163
1164   g_object_unref (c);
1165
1166   session_bus_down ();
1167 }
1168
1169 /* ---------------------------------------------------------------------------------------------------- */
1170
1171 static void
1172 test_connection_basic (void)
1173 {
1174   GDBusConnection *connection;
1175   GError *error;
1176   GDBusCapabilityFlags flags;
1177   gchar *guid;
1178   gchar *name;
1179   gboolean closed;
1180   gboolean exit_on_close;
1181   GIOStream *stream;
1182   GCredentials *credentials;
1183
1184   session_bus_up ();
1185
1186   error = NULL;
1187   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1188   g_assert_no_error (error);
1189   g_assert (connection != NULL);
1190
1191   flags = g_dbus_connection_get_capabilities (connection);
1192   g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1193             flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1194
1195   credentials = g_dbus_connection_get_peer_credentials (connection);
1196   g_assert (credentials == NULL);
1197
1198   g_object_get (connection,
1199                 "stream", &stream,
1200                 "guid", &guid,
1201                 "unique-name", &name,
1202                 "closed", &closed,
1203                 "exit-on-close", &exit_on_close,
1204                 "capabilities", &flags,
1205                 NULL);
1206
1207   g_assert (G_IS_IO_STREAM (stream));
1208   g_assert (g_dbus_is_guid (guid));
1209   g_assert (g_dbus_is_unique_name (name));
1210   g_assert (!closed);
1211   g_assert (exit_on_close);
1212   g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1213             flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1214   g_object_unref (stream);
1215   g_free (name);
1216   g_free (guid);
1217
1218   g_object_unref (connection);
1219
1220   session_bus_down ();
1221 }
1222
1223 /* ---------------------------------------------------------------------------------------------------- */
1224
1225 int
1226 main (int   argc,
1227       char *argv[])
1228 {
1229   g_test_init (&argc, &argv, NULL);
1230
1231   if (g_getenv ("G_TEST_DATA"))
1232     srcdir = builddir = g_getenv ("G_TEST_DATA");
1233   else
1234     {
1235       srcdir = SRCDIR;
1236       builddir = BUILDDIR;
1237     }
1238
1239   /* all the tests rely on a shared main loop */
1240   loop = g_main_loop_new (NULL, FALSE);
1241
1242   g_test_dbus_unset ();
1243
1244   g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
1245   g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
1246   g_test_add_func ("/gdbus/connection/send", test_connection_send);
1247   g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
1248   g_test_add_func ("/gdbus/connection/signal-match-rules", test_connection_signal_match_rules);
1249   g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
1250   g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
1251   return g_test_run();
1252 }