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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include <sys/types.h>
29 #include "gdbus-tests.h"
32 const gchar *builddir;
34 /* all tests rely on a shared mainloop */
35 static GMainLoop *loop = NULL;
38 G_GNUC_UNUSED static void
39 _log (const gchar *format, ...)
48 va_start (var_args, format);
49 str = g_strdup_vprintf (format, var_args);
52 g_get_current_time (&now);
53 now_time = (time_t) now.tv_sec;
54 now_tm = localtime (&now_time);
55 strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
57 g_print ("%s.%06d: %s\n",
58 time_buf, (gint) now.tv_usec / 1000,
67 test_connection_quit_mainloop (gpointer user_data)
69 volatile gboolean *quit_mainloop_fired = user_data;
70 _log ("quit_mainloop_fired");
71 *quit_mainloop_fired = TRUE;
72 g_main_loop_quit (loop);
76 /* ---------------------------------------------------------------------------------------------------- */
77 /* Connection life-cycle testing */
78 /* ---------------------------------------------------------------------------------------------------- */
80 static const GDBusInterfaceInfo boo_interface_info =
84 (GDBusMethodInfo **) NULL,
85 (GDBusSignalInfo **) NULL,
86 (GDBusPropertyInfo **) NULL,
90 static const GDBusInterfaceVTable boo_vtable =
92 NULL, /* _method_call */
93 NULL, /* _get_property */
94 NULL /* _set_property */
98 some_filter_func (GDBusConnection *connection,
99 GDBusMessage *message,
107 on_name_owner_changed (GDBusConnection *connection,
108 const gchar *sender_name,
109 const gchar *object_path,
110 const gchar *interface_name,
111 const gchar *signal_name,
112 GVariant *parameters,
118 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
120 volatile gboolean *val = user_data;
122 _log ("destroynotify fired for %p", val);
123 g_main_loop_quit (loop);
127 test_connection_life_cycle (void)
133 volatile gboolean on_signal_registration_freed_called;
134 volatile gboolean on_filter_freed_called;
135 volatile gboolean on_register_object_freed_called;
136 volatile gboolean quit_mainloop_fired;
137 guint quit_mainloop_id;
138 guint registration_id;
143 * Check for correct behavior when no bus is present
146 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
147 g_assert (error != NULL);
148 g_assert (!g_dbus_error_is_remote_error (error));
149 g_assert (c == NULL);
150 g_error_free (error);
153 * Check for correct behavior when a bus is present
158 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
159 g_assert_no_error (error);
160 g_assert (c != NULL);
161 g_assert (!g_dbus_connection_is_closed (c));
164 * Check that singleton handling work
167 c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
168 g_assert_no_error (error);
169 g_assert (c2 != NULL);
174 * Check that private connections work
176 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
177 g_assert_no_error (error);
178 g_assert (c2 != NULL);
182 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
183 g_assert_no_error (error);
184 g_assert (c2 != NULL);
185 g_assert (!g_dbus_connection_is_closed (c2));
186 ret = g_dbus_connection_close_sync (c2, NULL, &error);
187 g_assert_no_error (error);
189 _g_assert_signal_received (c2, "closed");
190 g_assert (g_dbus_connection_is_closed (c2));
191 ret = g_dbus_connection_close_sync (c2, NULL, &error);
192 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
193 g_error_free (error);
198 * Check that the finalization code works
200 * (and that the GDestroyNotify for filters and objects and signal
201 * registrations are run as expected)
204 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
205 g_assert_no_error (error);
206 g_assert (c2 != NULL);
207 /* signal registration */
208 on_signal_registration_freed_called = FALSE;
209 g_dbus_connection_signal_subscribe (c2,
210 "org.freedesktop.DBus", /* bus name */
211 "org.freedesktop.DBus", /* interface */
212 "NameOwnerChanged", /* member */
213 "/org/freesktop/DBus", /* path */
215 G_DBUS_SIGNAL_FLAGS_NONE,
216 on_name_owner_changed,
217 (gpointer) &on_signal_registration_freed_called,
218 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
220 on_filter_freed_called = FALSE;
221 g_dbus_connection_add_filter (c2,
223 (gpointer) &on_filter_freed_called,
224 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
225 /* object registration */
226 on_register_object_freed_called = FALSE;
228 registration_id = g_dbus_connection_register_object (c2,
230 (GDBusInterfaceInfo *) &boo_interface_info,
232 (gpointer) &on_register_object_freed_called,
233 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
235 g_assert_no_error (error);
236 g_assert (registration_id > 0);
237 /* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
239 quit_mainloop_fired = FALSE;
240 quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
241 _log ("destroynotifies for\n"
242 " register_object %p\n"
245 &on_register_object_freed_called,
246 &on_filter_freed_called,
247 &on_signal_registration_freed_called);
250 if (on_signal_registration_freed_called &&
251 on_filter_freed_called &&
252 on_register_object_freed_called)
254 if (quit_mainloop_fired)
256 _log ("entering loop");
257 g_main_loop_run (loop);
258 _log ("exiting loop");
260 g_source_remove (quit_mainloop_id);
261 g_assert (on_signal_registration_freed_called);
262 g_assert (on_filter_freed_called);
263 g_assert (on_register_object_freed_called);
264 g_assert (!quit_mainloop_fired);
267 * Check for correct behavior when the bus goes away
270 g_assert (!g_dbus_connection_is_closed (c));
271 g_dbus_connection_set_exit_on_close (c, FALSE);
273 _g_assert_signal_received (c, "closed");
274 g_assert (g_dbus_connection_is_closed (c));
280 /* ---------------------------------------------------------------------------------------------------- */
281 /* Test that sending and receiving messages work as expected */
282 /* ---------------------------------------------------------------------------------------------------- */
285 msg_cb_expect_error_disconnected (GDBusConnection *connection,
293 result = g_dbus_connection_call_finish (connection,
296 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
297 g_assert (!g_dbus_error_is_remote_error (error));
298 g_error_free (error);
299 g_assert (result == NULL);
301 g_main_loop_quit (loop);
305 msg_cb_expect_error_unknown_method (GDBusConnection *connection,
313 result = g_dbus_connection_call_finish (connection,
316 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
317 g_assert (g_dbus_error_is_remote_error (error));
318 g_error_free (error);
319 g_assert (result == NULL);
321 g_main_loop_quit (loop);
325 msg_cb_expect_success (GDBusConnection *connection,
333 result = g_dbus_connection_call_finish (connection,
336 g_assert_no_error (error);
337 g_assert (result != NULL);
338 g_variant_unref (result);
340 g_main_loop_quit (loop);
344 msg_cb_expect_error_cancelled (GDBusConnection *connection,
352 result = g_dbus_connection_call_finish (connection,
355 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
356 g_assert (!g_dbus_error_is_remote_error (error));
357 g_error_free (error);
358 g_assert (result == NULL);
360 g_main_loop_quit (loop);
364 msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
372 result = g_dbus_connection_call_finish (connection,
375 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
376 g_assert (!g_dbus_error_is_remote_error (error));
377 g_error_free (error);
378 g_assert (result == NULL);
380 g_main_loop_quit (loop);
383 /* ---------------------------------------------------------------------------------------------------- */
386 test_connection_send (void)
393 /* First, get an unopened connection */
394 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
395 g_assert (c != NULL);
396 g_assert (!g_dbus_connection_is_closed (c));
399 * Check that we never actually send a message if the GCancellable
400 * is already cancelled - i.e. we should get #G_IO_ERROR_CANCELLED
401 * when the actual connection is not up.
403 ca = g_cancellable_new ();
404 g_cancellable_cancel (ca);
405 g_dbus_connection_call (c,
406 "org.freedesktop.DBus", /* bus_name */
407 "/org/freedesktop/DBus", /* object path */
408 "org.freedesktop.DBus", /* interface name */
409 "GetId", /* method name */
411 G_DBUS_CALL_FLAGS_NONE,
414 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
416 g_main_loop_run (loop);
420 * Check that we get a reply to the GetId() method call.
422 g_dbus_connection_call (c,
423 "org.freedesktop.DBus", /* bus_name */
424 "/org/freedesktop/DBus", /* object path */
425 "org.freedesktop.DBus", /* interface name */
426 "GetId", /* method name */
428 G_DBUS_CALL_FLAGS_NONE,
431 (GAsyncReadyCallback) msg_cb_expect_success,
433 g_main_loop_run (loop);
436 * Check that we get an error reply to the NonExistantMethod() method call.
438 g_dbus_connection_call (c,
439 "org.freedesktop.DBus", /* bus_name */
440 "/org/freedesktop/DBus", /* object path */
441 "org.freedesktop.DBus", /* interface name */
442 "NonExistantMethod", /* method name */
444 G_DBUS_CALL_FLAGS_NONE,
447 (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
449 g_main_loop_run (loop);
452 * Check that cancellation works when the message is already in flight.
454 ca = g_cancellable_new ();
455 g_dbus_connection_call (c,
456 "org.freedesktop.DBus", /* bus_name */
457 "/org/freedesktop/DBus", /* object path */
458 "org.freedesktop.DBus", /* interface name */
459 "GetId", /* method name */
461 G_DBUS_CALL_FLAGS_NONE,
464 (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
466 g_cancellable_cancel (ca);
467 g_main_loop_run (loop);
471 * Check that we get an error when sending to a connection that is disconnected.
473 g_dbus_connection_set_exit_on_close (c, FALSE);
475 _g_assert_signal_received (c, "closed");
476 g_assert (g_dbus_connection_is_closed (c));
478 g_dbus_connection_call (c,
479 "org.freedesktop.DBus", /* bus_name */
480 "/org/freedesktop/DBus", /* object path */
481 "org.freedesktop.DBus", /* interface name */
482 "GetId", /* method name */
484 G_DBUS_CALL_FLAGS_NONE,
487 (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
489 g_main_loop_run (loop);
496 /* ---------------------------------------------------------------------------------------------------- */
497 /* Connection signal tests */
498 /* ---------------------------------------------------------------------------------------------------- */
501 test_connection_signal_handler (GDBusConnection *connection,
502 const gchar *sender_name,
503 const gchar *object_path,
504 const gchar *interface_name,
505 const gchar *signal_name,
506 GVariant *parameters,
509 gint *counter = user_data;
512 /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
518 g_main_loop_quit (loop);
522 test_connection_signals (void)
534 gint count_name_owner_changed;
538 gboolean quit_mainloop_fired;
539 guint quit_mainloop_id;
544 * Bring up first separate connections
547 /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
550 if (g_getenv ("G_DBUS_MONITOR") == NULL)
552 c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
553 g_assert (c1 != NULL);
554 g_assert (!g_dbus_connection_is_closed (c1));
557 c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
558 g_assert (c1 != NULL);
559 g_assert (!g_dbus_connection_is_closed (c1));
560 g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
563 * Install two signal handlers for the first connection
565 * - Listen to the signal "Foo" from :1.2 (e.g. c2)
566 * - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
568 * and then count how many times this signal handler was invoked.
570 s1 = g_dbus_connection_signal_subscribe (c1,
572 "org.gtk.GDBus.ExampleInterface",
574 "/org/gtk/GDBus/ExampleInterface",
576 G_DBUS_SIGNAL_FLAGS_NONE,
577 test_connection_signal_handler,
580 s2 = g_dbus_connection_signal_subscribe (c1,
581 NULL, /* match any sender */
582 "org.gtk.GDBus.ExampleInterface",
584 "/org/gtk/GDBus/ExampleInterface",
586 G_DBUS_SIGNAL_FLAGS_NONE,
587 test_connection_signal_handler,
590 s3 = g_dbus_connection_signal_subscribe (c1,
591 "org.freedesktop.DBus", /* sender */
592 "org.freedesktop.DBus", /* interface */
593 "NameOwnerChanged", /* member */
594 "/org/freedesktop/DBus", /* path */
596 G_DBUS_SIGNAL_FLAGS_NONE,
597 test_connection_signal_handler,
598 &count_name_owner_changed,
600 /* Note that s1b is *just like* s1 - this is to catch a bug where N
601 * subscriptions of the same rule causes N calls to each of the N
602 * subscriptions instead of just 1 call to each of the N subscriptions.
604 s1b = g_dbus_connection_signal_subscribe (c1,
606 "org.gtk.GDBus.ExampleInterface",
608 "/org/gtk/GDBus/ExampleInterface",
610 G_DBUS_SIGNAL_FLAGS_NONE,
611 test_connection_signal_handler,
622 count_name_owner_changed = 0;
625 * Make c2 emit "Foo" - we should catch it twice
627 * Note that there is no way to be sure that the signal subscriptions
628 * on c1 are effective yet - for all we know, the AddMatch() messages
629 * could sit waiting in a buffer somewhere between this process and
630 * the message bus. And emitting signals on c2 (a completely other
631 * socket!) will not necessarily change this.
633 * To ensure this is not the case, do a synchronous call on c1.
635 result = g_dbus_connection_call_sync (c1,
636 "org.freedesktop.DBus", /* bus name */
637 "/org/freedesktop/DBus", /* object path */
638 "org.freedesktop.DBus", /* interface name */
639 "GetId", /* method name */
640 NULL, /* parameters */
641 NULL, /* return type */
642 G_DBUS_CALL_FLAGS_NONE,
646 g_assert_no_error (error);
647 g_assert (result != NULL);
648 g_variant_unref (result);
651 * Bring up two other connections
653 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
654 g_assert (c2 != NULL);
655 g_assert (!g_dbus_connection_is_closed (c2));
656 g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
657 c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
658 g_assert (c3 != NULL);
659 g_assert (!g_dbus_connection_is_closed (c3));
660 g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
662 /* now, emit the signal on c2 */
663 ret = g_dbus_connection_emit_signal (c2,
664 NULL, /* destination bus name */
665 "/org/gtk/GDBus/ExampleInterface",
666 "org.gtk.GDBus.ExampleInterface",
670 g_assert_no_error (error);
672 while (!(count_s1 >= 1 && count_s2 >= 1))
673 g_main_loop_run (loop);
674 g_assert_cmpint (count_s1, ==, 1);
675 g_assert_cmpint (count_s2, ==, 1);
678 * Make c3 emit "Foo" - we should catch it only once
680 ret = g_dbus_connection_emit_signal (c3,
681 NULL, /* destination bus name */
682 "/org/gtk/GDBus/ExampleInterface",
683 "org.gtk.GDBus.ExampleInterface",
687 g_assert_no_error (error);
689 while (!(count_s1 == 1 && count_s2 == 2))
690 g_main_loop_run (loop);
691 g_assert_cmpint (count_s1, ==, 1);
692 g_assert_cmpint (count_s2, ==, 2);
695 * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
696 * to avoid spinning forever
698 quit_mainloop_fired = FALSE;
699 quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, &quit_mainloop_fired);
700 while (count_name_owner_changed < 2 && !quit_mainloop_fired)
701 g_main_loop_run (loop);
702 g_source_remove (quit_mainloop_id);
703 g_assert_cmpint (count_s1, ==, 1);
704 g_assert_cmpint (count_s2, ==, 2);
705 g_assert_cmpint (count_name_owner_changed, ==, 2);
707 g_dbus_connection_signal_unsubscribe (c1, s1);
708 g_dbus_connection_signal_unsubscribe (c1, s2);
709 g_dbus_connection_signal_unsubscribe (c1, s3);
710 g_dbus_connection_signal_unsubscribe (c1, s1b);
720 test_match_rule (GDBusConnection *connection,
721 GDBusSignalFlags flags,
724 gboolean should_match)
726 guint subscription_ids[2];
729 GError *error = NULL;
731 subscription_ids[0] = g_dbus_connection_signal_subscribe (connection,
732 NULL, "org.gtk.ExampleInterface", "Foo", "/",
734 G_DBUS_SIGNAL_FLAGS_NONE,
735 test_connection_signal_handler,
737 subscription_ids[1] = g_dbus_connection_signal_subscribe (connection,
738 NULL, "org.gtk.ExampleInterface", "Foo", "/",
741 test_connection_signal_handler,
743 g_assert_cmpint (subscription_ids[0], !=, 0);
744 g_assert_cmpint (subscription_ids[1], !=, 0);
746 g_dbus_connection_emit_signal (connection,
747 NULL, "/", "org.gtk.ExampleInterface",
748 "Foo", g_variant_new ("(s)", arg0),
750 g_assert_no_error (error);
752 /* synchronously ping a non-existent method to make sure the signals are dispatched */
753 g_dbus_connection_call_sync (connection, "org.gtk.ExampleInterface", "/", "org.gtk.ExampleInterface",
754 "Bar", g_variant_new ("()"), G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE,
757 while (g_main_context_iteration (NULL, FALSE))
760 g_assert_cmpint (emissions, ==, 1);
761 g_assert_cmpint (matches, ==, should_match ? 1 : 0);
763 g_dbus_connection_signal_unsubscribe (connection, subscription_ids[0]);
764 g_dbus_connection_signal_unsubscribe (connection, subscription_ids[1]);
768 test_connection_signal_match_rules (void)
770 GDBusConnection *con;
773 con = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
775 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "foo", TRUE);
776 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "bar", FALSE);
778 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "", FALSE);
779 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org", FALSE);
780 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk", TRUE);
781 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk.Example", TRUE);
782 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk+", FALSE);
784 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "/", TRUE);
785 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "", FALSE);
786 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/Example", TRUE);
787 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/", "/org/gtk/Example", TRUE);
788 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/", TRUE);
789 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk", FALSE);
790 test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk+", "/org/gtk", FALSE);
792 g_object_unref (con);
796 /* ---------------------------------------------------------------------------------------------------- */
805 static GDBusMessage *
806 filter_func (GDBusConnection *connection,
807 GDBusMessage *message,
811 FilterData *data = user_data;
812 guint32 reply_serial;
816 reply_serial = g_dbus_message_get_reply_serial (message);
817 if (reply_serial == data->serial)
818 data->num_handled += 1;
822 data->num_outgoing += 1;
831 gboolean alter_incoming;
832 gboolean alter_outgoing;
835 static GDBusMessage *
836 other_filter_func (GDBusConnection *connection,
837 GDBusMessage *message,
841 FilterEffects *effects = user_data;
846 alter = effects->alter_incoming;
848 alter = effects->alter_outgoing;
857 copy = g_dbus_message_copy (message, NULL);
858 g_object_unref (message);
860 body = g_dbus_message_get_body (copy);
861 g_variant_get (body, "(s)", &s);
862 s2 = g_strdup_printf ("MOD: %s", s);
863 g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
878 test_connection_filter_name_owner_changed_signal_handler (GDBusConnection *connection,
879 const gchar *sender_name,
880 const gchar *object_path,
881 const gchar *interface_name,
882 const gchar *signal_name,
883 GVariant *parameters,
887 const gchar *old_owner;
888 const gchar *new_owner;
890 g_variant_get (parameters,
896 if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
898 g_main_loop_quit (loop);
903 test_connection_filter_on_timeout (gpointer user_data)
905 g_printerr ("Timeout waiting 30 sec on service\n");
906 g_assert_not_reached ();
911 test_connection_filter (void)
920 guint timeout_mainloop_id;
921 guint signal_handler_id;
922 FilterEffects effects;
927 memset (&data, '\0', sizeof (FilterData));
932 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
933 g_assert_no_error (error);
934 g_assert (c != NULL);
936 filter_id = g_dbus_connection_add_filter (c,
941 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
942 "/org/freedesktop/DBus", /* path */
943 "org.freedesktop.DBus", /* interface */
945 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
947 g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
948 g_assert_no_error (error);
950 while (data.num_handled == 0)
953 m2 = g_dbus_message_copy (m, &error);
954 g_assert_no_error (error);
955 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
957 g_assert_no_error (error);
959 while (data.num_handled == 1)
962 m2 = g_dbus_message_copy (m, &error);
963 g_assert_no_error (error);
964 g_dbus_message_set_serial (m2, data.serial);
965 /* lock the message to test PRESERVE_SERIAL flag. */
966 g_dbus_message_lock (m2);
967 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
969 g_assert_no_error (error);
971 while (data.num_handled == 2)
974 m2 = g_dbus_message_copy (m, &error);
975 g_assert_no_error (error);
976 r = g_dbus_connection_send_message_with_reply_sync (c,
978 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
981 NULL, /* GCancellable */
984 g_assert_no_error (error);
985 g_assert (r != NULL);
987 g_assert_cmpint (data.num_handled, ==, 4);
989 g_dbus_connection_remove_filter (c, filter_id);
991 m2 = g_dbus_message_copy (m, &error);
992 g_assert_no_error (error);
993 r = g_dbus_connection_send_message_with_reply_sync (c,
995 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
998 NULL, /* GCancellable */
1000 g_object_unref (m2);
1001 g_assert_no_error (error);
1002 g_assert (r != NULL);
1004 g_assert_cmpint (data.num_handled, ==, 4);
1005 g_assert_cmpint (data.num_outgoing, ==, 4);
1007 /* this is safe; testserver will exit once the bus goes away */
1008 path = g_build_filename (builddir, "gdbus-testserver", NULL);
1009 g_assert (g_spawn_command_line_async (path, NULL));
1012 /* wait for service to be available */
1013 signal_handler_id = g_dbus_connection_signal_subscribe (c,
1014 "org.freedesktop.DBus", /* sender */
1015 "org.freedesktop.DBus",
1017 "/org/freedesktop/DBus",
1019 G_DBUS_SIGNAL_FLAGS_NONE,
1020 test_connection_filter_name_owner_changed_signal_handler,
1023 g_assert_cmpint (signal_handler_id, !=, 0);
1024 timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
1025 g_main_loop_run (loop);
1026 g_source_remove (timeout_mainloop_id);
1027 g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
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);
1103 serials_thread_func (GDBusConnection *c)
1105 guint32 message_serial;
1107 /* No calls on this thread yet */
1108 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
1110 /* Send a bogus message and store its serial */
1112 send_bogus_message (c, &message_serial);
1114 /* Give it some time to actually send the message out */
1117 g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
1118 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
1124 test_connection_serials (void)
1128 GThread *pool[NUM_THREADS];
1134 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1135 g_assert_no_error (error);
1136 g_assert (c != NULL);
1138 /* Status after initialization */
1139 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
1141 /* Send a bogus message */
1142 send_bogus_message (c, NULL);
1143 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1145 /* Start the threads */
1146 for (i = 0; i < NUM_THREADS; i++)
1147 pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
1149 /* Wait until threads are finished */
1150 for (i = 0; i < NUM_THREADS; i++)
1152 g_thread_join (pool[i]);
1153 g_thread_unref (pool[i]);
1156 /* No calls in between on this thread, should be the last value */
1157 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1159 send_bogus_message (c, NULL);
1161 /* All above calls + calls in threads */
1162 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
1166 session_bus_down ();
1169 /* ---------------------------------------------------------------------------------------------------- */
1172 test_connection_basic (void)
1174 GDBusConnection *connection;
1176 GDBusCapabilityFlags flags;
1180 gboolean exit_on_close;
1182 GCredentials *credentials;
1187 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1188 g_assert_no_error (error);
1189 g_assert (connection != NULL);
1191 flags = g_dbus_connection_get_capabilities (connection);
1192 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1193 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1195 credentials = g_dbus_connection_get_peer_credentials (connection);
1196 g_assert (credentials == NULL);
1198 g_object_get (connection,
1201 "unique-name", &name,
1203 "exit-on-close", &exit_on_close,
1204 "capabilities", &flags,
1207 g_assert (G_IS_IO_STREAM (stream));
1208 g_assert (g_dbus_is_guid (guid));
1209 g_assert (g_dbus_is_unique_name (name));
1211 g_assert (exit_on_close);
1212 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1213 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1214 g_object_unref (stream);
1218 g_object_unref (connection);
1220 session_bus_down ();
1223 /* ---------------------------------------------------------------------------------------------------- */
1229 g_test_init (&argc, &argv, NULL);
1231 if (g_getenv ("G_TEST_DATA"))
1232 srcdir = builddir = g_getenv ("G_TEST_DATA");
1239 /* all the tests rely on a shared main loop */
1240 loop = g_main_loop_new (NULL, FALSE);
1242 g_test_dbus_unset ();
1244 g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
1245 g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
1246 g_test_add_func ("/gdbus/connection/send", test_connection_send);
1247 g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
1248 g_test_add_func ("/gdbus/connection/signal-match-rules", test_connection_signal_match_rules);
1249 g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
1250 g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
1251 return g_test_run();