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