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