1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
25 #include <sys/types.h>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
33 G_GNUC_UNUSED static void
34 _log (const gchar *format, ...)
43 va_start (var_args, format);
44 str = g_strdup_vprintf (format, var_args);
47 g_get_current_time (&now);
48 now_time = (time_t) now.tv_sec;
49 now_tm = localtime (&now_time);
50 strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
52 g_print ("%s.%06d: %s\n",
53 time_buf, (gint) now.tv_usec / 1000,
62 test_connection_quit_mainloop (gpointer user_data)
64 volatile gboolean *quit_mainloop_fired = user_data;
65 _log ("quit_mainloop_fired");
66 *quit_mainloop_fired = TRUE;
67 g_main_loop_quit (loop);
71 /* ---------------------------------------------------------------------------------------------------- */
72 /* Connection life-cycle testing */
73 /* ---------------------------------------------------------------------------------------------------- */
75 static const GDBusInterfaceInfo boo_interface_info =
79 (GDBusMethodInfo **) NULL,
80 (GDBusSignalInfo **) NULL,
81 (GDBusPropertyInfo **) NULL,
85 static const GDBusInterfaceVTable boo_vtable =
87 NULL, /* _method_call */
88 NULL, /* _get_property */
89 NULL /* _set_property */
93 some_filter_func (GDBusConnection *connection,
94 GDBusMessage *message,
102 on_name_owner_changed (GDBusConnection *connection,
103 const gchar *sender_name,
104 const gchar *object_path,
105 const gchar *interface_name,
106 const gchar *signal_name,
107 GVariant *parameters,
113 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
115 volatile gboolean *val = user_data;
117 _log ("destroynotify fired for %p", val);
118 g_main_loop_quit (loop);
122 test_connection_bus_failure (void)
125 GError *error = NULL;
128 * Check for correct behavior when no bus is present
131 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
132 g_assert (error != NULL);
133 g_assert (!g_dbus_error_is_remote_error (error));
134 g_assert (c == NULL);
135 g_error_free (error);
139 test_connection_life_cycle (void)
145 volatile gboolean on_signal_registration_freed_called;
146 volatile gboolean on_filter_freed_called;
147 volatile gboolean on_register_object_freed_called;
148 volatile gboolean quit_mainloop_fired;
149 guint quit_mainloop_id;
150 guint registration_id;
155 * Check for correct behavior when a bus is present
160 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
161 g_assert_no_error (error);
162 g_assert (c != NULL);
163 g_assert (!g_dbus_connection_is_closed (c));
166 * Check that singleton handling work
169 c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
170 g_assert_no_error (error);
171 g_assert (c2 != NULL);
176 * Check that private connections work
178 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
179 g_assert_no_error (error);
180 g_assert (c2 != NULL);
184 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
185 g_assert_no_error (error);
186 g_assert (c2 != NULL);
187 g_assert (!g_dbus_connection_is_closed (c2));
188 ret = g_dbus_connection_close_sync (c2, NULL, &error);
189 g_assert_no_error (error);
191 _g_assert_signal_received (c2, "closed");
192 g_assert (g_dbus_connection_is_closed (c2));
193 ret = g_dbus_connection_close_sync (c2, NULL, &error);
194 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
195 g_error_free (error);
200 * Check that the finalization code works
202 * (and that the GDestroyNotify for filters and objects and signal
203 * registrations are run as expected)
206 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
207 g_assert_no_error (error);
208 g_assert (c2 != NULL);
209 /* signal registration */
210 on_signal_registration_freed_called = FALSE;
211 g_dbus_connection_signal_subscribe (c2,
212 "org.freedesktop.DBus", /* bus name */
213 "org.freedesktop.DBus", /* interface */
214 "NameOwnerChanged", /* member */
215 "/org/freesktop/DBus", /* path */
217 G_DBUS_SIGNAL_FLAGS_NONE,
218 on_name_owner_changed,
219 (gpointer) &on_signal_registration_freed_called,
220 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
222 on_filter_freed_called = FALSE;
223 g_dbus_connection_add_filter (c2,
225 (gpointer) &on_filter_freed_called,
226 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
227 /* object registration */
228 on_register_object_freed_called = FALSE;
230 registration_id = g_dbus_connection_register_object (c2,
232 (GDBusInterfaceInfo *) &boo_interface_info,
234 (gpointer) &on_register_object_freed_called,
235 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
237 g_assert_no_error (error);
238 g_assert (registration_id > 0);
239 /* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
241 quit_mainloop_fired = FALSE;
242 quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
243 _log ("destroynotifies for\n"
244 " register_object %p\n"
247 &on_register_object_freed_called,
248 &on_filter_freed_called,
249 &on_signal_registration_freed_called);
252 if (on_signal_registration_freed_called &&
253 on_filter_freed_called &&
254 on_register_object_freed_called)
256 if (quit_mainloop_fired)
258 _log ("entering loop");
259 g_main_loop_run (loop);
260 _log ("exiting loop");
262 g_source_remove (quit_mainloop_id);
263 g_assert (on_signal_registration_freed_called);
264 g_assert (on_filter_freed_called);
265 g_assert (on_register_object_freed_called);
266 g_assert (!quit_mainloop_fired);
269 * Check for correct behavior when the bus goes away
272 g_assert (!g_dbus_connection_is_closed (c));
273 g_dbus_connection_set_exit_on_close (c, FALSE);
275 _g_assert_signal_received (c, "closed");
276 g_assert (g_dbus_connection_is_closed (c));
282 /* ---------------------------------------------------------------------------------------------------- */
283 /* Test that sending and receiving messages work as expected */
284 /* ---------------------------------------------------------------------------------------------------- */
287 msg_cb_expect_error_disconnected (GDBusConnection *connection,
295 result = g_dbus_connection_call_finish (connection,
298 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
299 g_assert (!g_dbus_error_is_remote_error (error));
300 g_error_free (error);
301 g_assert (result == NULL);
303 g_main_loop_quit (loop);
307 msg_cb_expect_error_unknown_method (GDBusConnection *connection,
315 result = g_dbus_connection_call_finish (connection,
318 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
319 g_assert (g_dbus_error_is_remote_error (error));
320 g_error_free (error);
321 g_assert (result == NULL);
323 g_main_loop_quit (loop);
327 msg_cb_expect_success (GDBusConnection *connection,
335 result = g_dbus_connection_call_finish (connection,
338 g_assert_no_error (error);
339 g_assert (result != NULL);
340 g_variant_unref (result);
342 g_main_loop_quit (loop);
346 msg_cb_expect_error_cancelled (GDBusConnection *connection,
354 result = g_dbus_connection_call_finish (connection,
357 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
358 g_assert (!g_dbus_error_is_remote_error (error));
359 g_error_free (error);
360 g_assert (result == NULL);
362 g_main_loop_quit (loop);
366 msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
374 result = g_dbus_connection_call_finish (connection,
377 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
378 g_assert (!g_dbus_error_is_remote_error (error));
379 g_error_free (error);
380 g_assert (result == NULL);
382 g_main_loop_quit (loop);
385 /* ---------------------------------------------------------------------------------------------------- */
388 test_connection_send (void)
395 /* First, get an unopened connection */
396 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
397 g_assert (c != NULL);
398 g_assert (!g_dbus_connection_is_closed (c));
401 * Check that we never actually send a message if the GCancellable
402 * is already cancelled - i.e. we should get #G_IO_ERROR_CANCELLED
403 * when the actual connection is not up.
405 ca = g_cancellable_new ();
406 g_cancellable_cancel (ca);
407 g_dbus_connection_call (c,
408 "org.freedesktop.DBus", /* bus_name */
409 "/org/freedesktop/DBus", /* object path */
410 "org.freedesktop.DBus", /* interface name */
411 "GetId", /* method name */
413 G_DBUS_CALL_FLAGS_NONE,
416 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
418 g_main_loop_run (loop);
422 * Check that we get a reply to the GetId() method call.
424 g_dbus_connection_call (c,
425 "org.freedesktop.DBus", /* bus_name */
426 "/org/freedesktop/DBus", /* object path */
427 "org.freedesktop.DBus", /* interface name */
428 "GetId", /* method name */
430 G_DBUS_CALL_FLAGS_NONE,
433 (GAsyncReadyCallback) msg_cb_expect_success,
435 g_main_loop_run (loop);
438 * Check that we get an error reply to the NonExistantMethod() method call.
440 g_dbus_connection_call (c,
441 "org.freedesktop.DBus", /* bus_name */
442 "/org/freedesktop/DBus", /* object path */
443 "org.freedesktop.DBus", /* interface name */
444 "NonExistantMethod", /* method name */
446 G_DBUS_CALL_FLAGS_NONE,
449 (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
451 g_main_loop_run (loop);
454 * Check that cancellation works when the message is already in flight.
456 ca = g_cancellable_new ();
457 g_dbus_connection_call (c,
458 "org.freedesktop.DBus", /* bus_name */
459 "/org/freedesktop/DBus", /* object path */
460 "org.freedesktop.DBus", /* interface name */
461 "GetId", /* method name */
463 G_DBUS_CALL_FLAGS_NONE,
466 (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
468 g_cancellable_cancel (ca);
469 g_main_loop_run (loop);
473 * Check that we get an error when sending to a connection that is disconnected.
475 g_dbus_connection_set_exit_on_close (c, FALSE);
477 _g_assert_signal_received (c, "closed");
478 g_assert (g_dbus_connection_is_closed (c));
480 g_dbus_connection_call (c,
481 "org.freedesktop.DBus", /* bus_name */
482 "/org/freedesktop/DBus", /* object path */
483 "org.freedesktop.DBus", /* interface name */
484 "GetId", /* method name */
486 G_DBUS_CALL_FLAGS_NONE,
489 (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
491 g_main_loop_run (loop);
498 /* ---------------------------------------------------------------------------------------------------- */
499 /* Connection signal tests */
500 /* ---------------------------------------------------------------------------------------------------- */
503 test_connection_signal_handler (GDBusConnection *connection,
504 const gchar *sender_name,
505 const gchar *object_path,
506 const gchar *interface_name,
507 const gchar *signal_name,
508 GVariant *parameters,
511 gint *counter = user_data;
514 /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
520 g_main_loop_quit (loop);
524 test_connection_signals (void)
536 gint count_name_owner_changed;
540 gboolean quit_mainloop_fired;
541 guint quit_mainloop_id;
546 * Bring up first separate connections
549 /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
552 if (g_getenv ("G_DBUS_MONITOR") == NULL)
554 c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
555 g_assert (c1 != NULL);
556 g_assert (!g_dbus_connection_is_closed (c1));
559 c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
560 g_assert (c1 != NULL);
561 g_assert (!g_dbus_connection_is_closed (c1));
562 g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
565 * Install two signal handlers for the first connection
567 * - Listen to the signal "Foo" from :1.2 (e.g. c2)
568 * - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
570 * and then count how many times this signal handler was invoked.
572 s1 = g_dbus_connection_signal_subscribe (c1,
574 "org.gtk.GDBus.ExampleInterface",
576 "/org/gtk/GDBus/ExampleInterface",
578 G_DBUS_SIGNAL_FLAGS_NONE,
579 test_connection_signal_handler,
582 s2 = g_dbus_connection_signal_subscribe (c1,
583 NULL, /* match any sender */
584 "org.gtk.GDBus.ExampleInterface",
586 "/org/gtk/GDBus/ExampleInterface",
588 G_DBUS_SIGNAL_FLAGS_NONE,
589 test_connection_signal_handler,
592 s3 = g_dbus_connection_signal_subscribe (c1,
593 "org.freedesktop.DBus", /* sender */
594 "org.freedesktop.DBus", /* interface */
595 "NameOwnerChanged", /* member */
596 "/org/freedesktop/DBus", /* path */
598 G_DBUS_SIGNAL_FLAGS_NONE,
599 test_connection_signal_handler,
600 &count_name_owner_changed,
602 /* Note that s1b is *just like* s1 - this is to catch a bug where N
603 * subscriptions of the same rule causes N calls to each of the N
604 * subscriptions instead of just 1 call to each of the N subscriptions.
606 s1b = g_dbus_connection_signal_subscribe (c1,
608 "org.gtk.GDBus.ExampleInterface",
610 "/org/gtk/GDBus/ExampleInterface",
612 G_DBUS_SIGNAL_FLAGS_NONE,
613 test_connection_signal_handler,
624 count_name_owner_changed = 0;
627 * Make c2 emit "Foo" - we should catch it twice
629 * Note that there is no way to be sure that the signal subscriptions
630 * on c1 are effective yet - for all we know, the AddMatch() messages
631 * could sit waiting in a buffer somewhere between this process and
632 * the message bus. And emitting signals on c2 (a completely other
633 * socket!) will not necessarily change this.
635 * To ensure this is not the case, do a synchronous call on c1.
637 result = g_dbus_connection_call_sync (c1,
638 "org.freedesktop.DBus", /* bus name */
639 "/org/freedesktop/DBus", /* object path */
640 "org.freedesktop.DBus", /* interface name */
641 "GetId", /* method name */
642 NULL, /* parameters */
643 NULL, /* return type */
644 G_DBUS_CALL_FLAGS_NONE,
648 g_assert_no_error (error);
649 g_assert (result != NULL);
650 g_variant_unref (result);
653 * Bring up two other connections
655 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
656 g_assert (c2 != NULL);
657 g_assert (!g_dbus_connection_is_closed (c2));
658 g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
659 c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
660 g_assert (c3 != NULL);
661 g_assert (!g_dbus_connection_is_closed (c3));
662 g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
664 /* now, emit the signal on c2 */
665 ret = g_dbus_connection_emit_signal (c2,
666 NULL, /* destination bus name */
667 "/org/gtk/GDBus/ExampleInterface",
668 "org.gtk.GDBus.ExampleInterface",
672 g_assert_no_error (error);
674 while (!(count_s1 >= 1 && count_s2 >= 1))
675 g_main_loop_run (loop);
676 g_assert_cmpint (count_s1, ==, 1);
677 g_assert_cmpint (count_s2, ==, 1);
680 * Make c3 emit "Foo" - we should catch it only once
682 ret = g_dbus_connection_emit_signal (c3,
683 NULL, /* destination bus name */
684 "/org/gtk/GDBus/ExampleInterface",
685 "org.gtk.GDBus.ExampleInterface",
689 g_assert_no_error (error);
691 while (!(count_s1 == 1 && count_s2 == 2))
692 g_main_loop_run (loop);
693 g_assert_cmpint (count_s1, ==, 1);
694 g_assert_cmpint (count_s2, ==, 2);
697 * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
698 * to avoid spinning forever
700 quit_mainloop_fired = FALSE;
701 quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, &quit_mainloop_fired);
702 while (count_name_owner_changed < 2 && !quit_mainloop_fired)
703 g_main_loop_run (loop);
704 g_source_remove (quit_mainloop_id);
705 g_assert_cmpint (count_s1, ==, 1);
706 g_assert_cmpint (count_s2, ==, 2);
707 g_assert_cmpint (count_name_owner_changed, ==, 2);
709 g_dbus_connection_signal_unsubscribe (c1, s1);
710 g_dbus_connection_signal_unsubscribe (c1, s2);
711 g_dbus_connection_signal_unsubscribe (c1, s3);
712 g_dbus_connection_signal_unsubscribe (c1, s1b);
722 test_match_rule (GDBusConnection *connection,
723 GDBusSignalFlags flags,
726 gboolean should_match)
728 guint subscription_ids[2];
731 GError *error = NULL;
733 subscription_ids[0] = g_dbus_connection_signal_subscribe (connection,
734 NULL, "org.gtk.ExampleInterface", "Foo", "/",
736 G_DBUS_SIGNAL_FLAGS_NONE,
737 test_connection_signal_handler,
739 subscription_ids[1] = g_dbus_connection_signal_subscribe (connection,
740 NULL, "org.gtk.ExampleInterface", "Foo", "/",
743 test_connection_signal_handler,
745 g_assert_cmpint (subscription_ids[0], !=, 0);
746 g_assert_cmpint (subscription_ids[1], !=, 0);
748 g_dbus_connection_emit_signal (connection,
749 NULL, "/", "org.gtk.ExampleInterface",
750 "Foo", g_variant_new ("(s)", arg0),
752 g_assert_no_error (error);
754 /* synchronously ping a non-existent method to make sure the signals are dispatched */
755 g_dbus_connection_call_sync (connection, "org.gtk.ExampleInterface", "/", "org.gtk.ExampleInterface",
756 "Bar", g_variant_new ("()"), G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE,
759 while (g_main_context_iteration (NULL, FALSE))
762 g_assert_cmpint (emissions, ==, 1);
763 g_assert_cmpint (matches, ==, should_match ? 1 : 0);
765 g_dbus_connection_signal_unsubscribe (connection, subscription_ids[0]);
766 g_dbus_connection_signal_unsubscribe (connection, subscription_ids[1]);
770 test_connection_signal_match_rules (void)
772 GDBusConnection *con;
775 con = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
777 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "foo", TRUE);
778 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "bar", FALSE);
780 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "", FALSE);
781 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org", FALSE);
782 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk", TRUE);
783 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk.Example", TRUE);
784 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk+", FALSE);
786 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "/", TRUE);
787 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "", FALSE);
788 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/Example", TRUE);
789 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/", "/org/gtk/Example", TRUE);
790 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/", TRUE);
791 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk", FALSE);
792 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk+", "/org/gtk", FALSE);
794 g_object_unref (con);
798 /* ---------------------------------------------------------------------------------------------------- */
807 static GDBusMessage *
808 filter_func (GDBusConnection *connection,
809 GDBusMessage *message,
813 FilterData *data = user_data;
814 guint32 reply_serial;
818 reply_serial = g_dbus_message_get_reply_serial (message);
819 if (reply_serial == data->serial)
820 data->num_handled += 1;
824 data->num_outgoing += 1;
833 gboolean alter_incoming;
834 gboolean alter_outgoing;
837 static GDBusMessage *
838 other_filter_func (GDBusConnection *connection,
839 GDBusMessage *message,
843 FilterEffects *effects = user_data;
848 alter = effects->alter_incoming;
850 alter = effects->alter_outgoing;
859 copy = g_dbus_message_copy (message, NULL);
860 g_object_unref (message);
862 body = g_dbus_message_get_body (copy);
863 g_variant_get (body, "(s)", &s);
864 s2 = g_strdup_printf ("MOD: %s", s);
865 g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
880 test_connection_filter_name_owner_changed_signal_handler (GDBusConnection *connection,
881 const gchar *sender_name,
882 const gchar *object_path,
883 const gchar *interface_name,
884 const gchar *signal_name,
885 GVariant *parameters,
889 const gchar *old_owner;
890 const gchar *new_owner;
892 g_variant_get (parameters,
898 if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
900 g_main_loop_quit (loop);
905 test_connection_filter_on_timeout (gpointer user_data)
907 g_printerr ("Timeout waiting 30 sec on service\n");
908 g_assert_not_reached ();
913 test_connection_filter (void)
922 guint timeout_mainloop_id;
923 guint signal_handler_id;
924 FilterEffects effects;
928 memset (&data, '\0', sizeof (FilterData));
933 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
934 g_assert_no_error (error);
935 g_assert (c != NULL);
937 filter_id = g_dbus_connection_add_filter (c,
942 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
943 "/org/freedesktop/DBus", /* path */
944 "org.freedesktop.DBus", /* interface */
946 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
948 g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
949 g_assert_no_error (error);
951 while (data.num_handled == 0)
954 m2 = g_dbus_message_copy (m, &error);
955 g_assert_no_error (error);
956 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
958 g_assert_no_error (error);
960 while (data.num_handled == 1)
963 m2 = g_dbus_message_copy (m, &error);
964 g_assert_no_error (error);
965 g_dbus_message_set_serial (m2, data.serial);
966 /* lock the message to test PRESERVE_SERIAL flag. */
967 g_dbus_message_lock (m2);
968 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
970 g_assert_no_error (error);
972 while (data.num_handled == 2)
975 m2 = g_dbus_message_copy (m, &error);
976 g_assert_no_error (error);
977 r = g_dbus_connection_send_message_with_reply_sync (c,
979 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
982 NULL, /* GCancellable */
985 g_assert_no_error (error);
986 g_assert (r != NULL);
988 g_assert_cmpint (data.num_handled, ==, 4);
990 g_dbus_connection_remove_filter (c, filter_id);
992 m2 = g_dbus_message_copy (m, &error);
993 g_assert_no_error (error);
994 r = g_dbus_connection_send_message_with_reply_sync (c,
996 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
999 NULL, /* GCancellable */
1001 g_object_unref (m2);
1002 g_assert_no_error (error);
1003 g_assert (r != NULL);
1005 g_assert_cmpint (data.num_handled, ==, 4);
1006 g_assert_cmpint (data.num_outgoing, ==, 4);
1008 /* wait for service to be available */
1009 signal_handler_id = g_dbus_connection_signal_subscribe (c,
1010 "org.freedesktop.DBus", /* sender */
1011 "org.freedesktop.DBus",
1013 "/org/freedesktop/DBus",
1015 G_DBUS_SIGNAL_FLAGS_NONE,
1016 test_connection_filter_name_owner_changed_signal_handler,
1019 g_assert_cmpint (signal_handler_id, !=, 0);
1021 /* this is safe; testserver will exit once the bus goes away */
1022 g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
1024 timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
1025 g_main_loop_run (loop);
1026 g_source_remove (timeout_mainloop_id);
1027 g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
1029 /* now test some combinations... */
1030 filter_id = g_dbus_connection_add_filter (c,
1035 effects.alter_incoming = FALSE;
1036 effects.alter_outgoing = FALSE;
1038 result = g_dbus_connection_call_sync (c,
1039 "com.example.TestService", /* bus name */
1040 "/com/example/TestObject", /* object path */
1041 "com.example.Frob", /* interface name */
1042 "HelloWorld", /* method name */
1043 g_variant_new ("(s)", "Cat"), /* parameters */
1044 G_VARIANT_TYPE ("(s)"), /* return type */
1045 G_DBUS_CALL_FLAGS_NONE,
1049 g_assert_no_error (error);
1050 g_variant_get (result, "(&s)", &s);
1051 g_assert_cmpstr (s, ==, "You greeted me with 'Cat'. Thanks!");
1052 g_variant_unref (result);
1054 effects.alter_incoming = TRUE;
1055 effects.alter_outgoing = TRUE;
1057 result = g_dbus_connection_call_sync (c,
1058 "com.example.TestService", /* bus name */
1059 "/com/example/TestObject", /* object path */
1060 "com.example.Frob", /* interface name */
1061 "HelloWorld", /* method name */
1062 g_variant_new ("(s)", "Cat"), /* parameters */
1063 G_VARIANT_TYPE ("(s)"), /* return type */
1064 G_DBUS_CALL_FLAGS_NONE,
1068 g_assert_no_error (error);
1069 g_variant_get (result, "(&s)", &s);
1070 g_assert_cmpstr (s, ==, "MOD: You greeted me with 'MOD: Cat'. Thanks!");
1071 g_variant_unref (result);
1074 g_dbus_connection_remove_filter (c, filter_id);
1079 session_bus_down ();
1082 /* ---------------------------------------------------------------------------------------------------- */
1084 #define NUM_THREADS 50
1087 send_bogus_message (GDBusConnection *c, guint32 *out_serial)
1092 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
1093 "/org/freedesktop/DBus", /* path */
1094 "org.freedesktop.DBus", /* interface */
1096 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
1098 g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, out_serial, &error);
1099 g_assert_no_error (error);
1104 serials_thread_func (GDBusConnection *c)
1106 guint32 message_serial;
1108 /* No calls on this thread yet */
1109 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
1111 /* Send a bogus message and store its serial */
1113 send_bogus_message (c, &message_serial);
1115 /* Give it some time to actually send the message out */
1118 g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
1119 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
1125 test_connection_serials (void)
1129 GThread *pool[NUM_THREADS];
1135 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1136 g_assert_no_error (error);
1137 g_assert (c != NULL);
1139 /* Status after initialization */
1140 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
1142 /* Send a bogus message */
1143 send_bogus_message (c, NULL);
1144 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1146 /* Start the threads */
1147 for (i = 0; i < NUM_THREADS; i++)
1148 pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
1150 /* Wait until threads are finished */
1151 for (i = 0; i < NUM_THREADS; i++)
1152 g_thread_join (pool[i]);
1154 /* No calls in between on this thread, should be the last value */
1155 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1157 send_bogus_message (c, NULL);
1159 /* All above calls + calls in threads */
1160 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
1164 session_bus_down ();
1167 /* ---------------------------------------------------------------------------------------------------- */
1170 test_connection_basic (void)
1172 GDBusConnection *connection;
1174 GDBusCapabilityFlags flags;
1178 gboolean exit_on_close;
1180 GCredentials *credentials;
1185 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1186 g_assert_no_error (error);
1187 g_assert (connection != NULL);
1189 flags = g_dbus_connection_get_capabilities (connection);
1190 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1191 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1193 credentials = g_dbus_connection_get_peer_credentials (connection);
1194 g_assert (credentials == NULL);
1196 g_object_get (connection,
1199 "unique-name", &name,
1201 "exit-on-close", &exit_on_close,
1202 "capabilities", &flags,
1205 g_assert (G_IS_IO_STREAM (stream));
1206 g_assert (g_dbus_is_guid (guid));
1207 g_assert (g_dbus_is_unique_name (name));
1209 g_assert (exit_on_close);
1210 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1211 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1212 g_object_unref (stream);
1216 g_object_unref (connection);
1218 session_bus_down ();
1221 /* ---------------------------------------------------------------------------------------------------- */
1228 g_test_init (&argc, &argv, NULL);
1230 /* all the tests rely on a shared main loop */
1231 loop = g_main_loop_new (NULL, FALSE);
1233 g_test_dbus_unset ();
1235 /* gdbus cleanup is pretty racy due to worker threads, so always do this test first */
1236 g_test_add_func ("/gdbus/connection/bus-failure", test_connection_bus_failure);
1238 g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
1239 g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
1240 g_test_add_func ("/gdbus/connection/send", test_connection_send);
1241 g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
1242 g_test_add_func ("/gdbus/connection/signal-match-rules", test_connection_signal_match_rules);
1243 g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
1244 g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
1247 g_main_loop_unref (loop);