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"
31 /* all tests rely on a shared mainloop */
32 static GMainLoop *loop = NULL;
35 G_GNUC_UNUSED static void
36 _log (const gchar *format, ...)
45 va_start (var_args, format);
46 str = g_strdup_vprintf (format, var_args);
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);
54 g_print ("%s.%06d: %s\n",
55 time_buf, (gint) now.tv_usec / 1000,
64 test_connection_quit_mainloop (gpointer user_data)
66 volatile gboolean *quit_mainloop_fired = user_data;
67 _log ("quit_mainloop_fired");
68 *quit_mainloop_fired = TRUE;
69 g_main_loop_quit (loop);
73 /* ---------------------------------------------------------------------------------------------------- */
74 /* Connection life-cycle testing */
75 /* ---------------------------------------------------------------------------------------------------- */
77 static const GDBusInterfaceInfo boo_interface_info =
81 (GDBusMethodInfo **) NULL,
82 (GDBusSignalInfo **) NULL,
83 (GDBusPropertyInfo **) NULL,
87 static const GDBusInterfaceVTable boo_vtable =
89 NULL, /* _method_call */
90 NULL, /* _get_property */
91 NULL /* _set_property */
95 some_filter_func (GDBusConnection *connection,
96 GDBusMessage *message,
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,
115 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
117 volatile gboolean *val = user_data;
119 _log ("destroynotify fired for %p", val);
120 g_main_loop_quit (loop);
124 test_connection_life_cycle (void)
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;
140 * Check for correct behavior when no bus is present
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);
150 * Check for correct behavior when a bus is present
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));
161 * Check that singleton handling work
164 c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
165 g_assert_no_error (error);
166 g_assert (c2 != NULL);
171 * Check that private connections work
173 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
174 g_assert_no_error (error);
175 g_assert (c2 != NULL);
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);
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);
195 * Check that the finalization code works
197 * (and that the GDestroyNotify for filters and objects and signal
198 * registrations are run as expected)
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 */
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);
217 on_filter_freed_called = FALSE;
218 g_dbus_connection_add_filter (c2,
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;
225 registration_id = g_dbus_connection_register_object (c2,
227 (GDBusInterfaceInfo *) &boo_interface_info,
229 (gpointer) &on_register_object_freed_called,
230 a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
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 */
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"
242 &on_register_object_freed_called,
243 &on_filter_freed_called,
244 &on_signal_registration_freed_called);
247 if (on_signal_registration_freed_called &&
248 on_filter_freed_called &&
249 on_register_object_freed_called)
251 if (quit_mainloop_fired)
253 _log ("entering loop");
254 g_main_loop_run (loop);
255 _log ("exiting loop");
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);
264 * Check for correct behavior when the bus goes away
267 g_assert (!g_dbus_connection_is_closed (c));
268 g_dbus_connection_set_exit_on_close (c, FALSE);
270 _g_assert_signal_received (c, "closed");
271 g_assert (g_dbus_connection_is_closed (c));
277 /* ---------------------------------------------------------------------------------------------------- */
278 /* Test that sending and receiving messages work as expected */
279 /* ---------------------------------------------------------------------------------------------------- */
282 msg_cb_expect_error_disconnected (GDBusConnection *connection,
290 result = g_dbus_connection_call_finish (connection,
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);
298 g_main_loop_quit (loop);
302 msg_cb_expect_error_unknown_method (GDBusConnection *connection,
310 result = g_dbus_connection_call_finish (connection,
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);
318 g_main_loop_quit (loop);
322 msg_cb_expect_success (GDBusConnection *connection,
330 result = g_dbus_connection_call_finish (connection,
333 g_assert_no_error (error);
334 g_assert (result != NULL);
335 g_variant_unref (result);
337 g_main_loop_quit (loop);
341 msg_cb_expect_error_cancelled (GDBusConnection *connection,
349 result = g_dbus_connection_call_finish (connection,
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);
357 g_main_loop_quit (loop);
361 msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
369 result = g_dbus_connection_call_finish (connection,
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);
377 g_main_loop_quit (loop);
380 /* ---------------------------------------------------------------------------------------------------- */
383 test_connection_send (void)
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));
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.
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 */
408 G_DBUS_CALL_FLAGS_NONE,
411 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
413 g_main_loop_run (loop);
417 * Check that we get a reply to the GetId() method call.
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 */
425 G_DBUS_CALL_FLAGS_NONE,
428 (GAsyncReadyCallback) msg_cb_expect_success,
430 g_main_loop_run (loop);
433 * Check that we get an error reply to the NonExistantMethod() method call.
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 */
441 G_DBUS_CALL_FLAGS_NONE,
444 (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
446 g_main_loop_run (loop);
449 * Check that cancellation works when the message is already in flight.
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 */
458 G_DBUS_CALL_FLAGS_NONE,
461 (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
463 g_cancellable_cancel (ca);
464 g_main_loop_run (loop);
468 * Check that we get an error when sending to a connection that is disconnected.
470 g_dbus_connection_set_exit_on_close (c, FALSE);
472 _g_assert_signal_received (c, "closed");
473 g_assert (g_dbus_connection_is_closed (c));
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 */
481 G_DBUS_CALL_FLAGS_NONE,
484 (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
486 g_main_loop_run (loop);
493 /* ---------------------------------------------------------------------------------------------------- */
494 /* Connection signal tests */
495 /* ---------------------------------------------------------------------------------------------------- */
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,
506 gint *counter = user_data;
509 /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
515 g_main_loop_quit (loop);
519 test_connection_signals (void)
531 gint count_name_owner_changed;
535 gboolean quit_mainloop_fired;
536 guint quit_mainloop_id;
541 * Bring up first separate connections
544 /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
547 if (g_getenv ("G_DBUS_MONITOR") == NULL)
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));
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");
560 * Install two signal handlers for the first connection
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)
565 * and then count how many times this signal handler was invoked.
567 s1 = g_dbus_connection_signal_subscribe (c1,
569 "org.gtk.GDBus.ExampleInterface",
571 "/org/gtk/GDBus/ExampleInterface",
573 G_DBUS_SIGNAL_FLAGS_NONE,
574 test_connection_signal_handler,
577 s2 = g_dbus_connection_signal_subscribe (c1,
578 NULL, /* match any sender */
579 "org.gtk.GDBus.ExampleInterface",
581 "/org/gtk/GDBus/ExampleInterface",
583 G_DBUS_SIGNAL_FLAGS_NONE,
584 test_connection_signal_handler,
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 */
593 G_DBUS_SIGNAL_FLAGS_NONE,
594 test_connection_signal_handler,
595 &count_name_owner_changed,
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.
601 s1b = g_dbus_connection_signal_subscribe (c1,
603 "org.gtk.GDBus.ExampleInterface",
605 "/org/gtk/GDBus/ExampleInterface",
607 G_DBUS_SIGNAL_FLAGS_NONE,
608 test_connection_signal_handler,
619 count_name_owner_changed = 0;
622 * Make c2 emit "Foo" - we should catch it twice
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.
630 * To ensure this is not the case, do a synchronous call on c1.
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,
643 g_assert_no_error (error);
644 g_assert (result != NULL);
645 g_variant_unref (result);
648 * Bring up two other connections
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");
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",
667 g_assert_no_error (error);
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);
675 * Make c3 emit "Foo" - we should catch it only once
677 ret = g_dbus_connection_emit_signal (c3,
678 NULL, /* destination bus name */
679 "/org/gtk/GDBus/ExampleInterface",
680 "org.gtk.GDBus.ExampleInterface",
684 g_assert_no_error (error);
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);
692 * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
693 * to avoid spinning forever
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);
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);
716 /* ---------------------------------------------------------------------------------------------------- */
725 static GDBusMessage *
726 filter_func (GDBusConnection *connection,
727 GDBusMessage *message,
731 FilterData *data = user_data;
732 guint32 reply_serial;
736 reply_serial = g_dbus_message_get_reply_serial (message);
737 if (reply_serial == data->serial)
738 data->num_handled += 1;
742 data->num_outgoing += 1;
751 gboolean alter_incoming;
752 gboolean alter_outgoing;
755 static GDBusMessage *
756 other_filter_func (GDBusConnection *connection,
757 GDBusMessage *message,
761 FilterEffects *effects = user_data;
766 alter = effects->alter_incoming;
768 alter = effects->alter_outgoing;
777 copy = g_dbus_message_copy (message, NULL);
778 g_object_unref (message);
780 body = g_dbus_message_get_body (copy);
781 g_variant_get (body, "(s)", &s);
782 s2 = g_strdup_printf ("MOD: %s", s);
783 g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
798 test_connection_filter_name_owner_changed_signal_handler (GDBusConnection *connection,
799 const gchar *sender_name,
800 const gchar *object_path,
801 const gchar *interface_name,
802 const gchar *signal_name,
803 GVariant *parameters,
807 const gchar *old_owner;
808 const gchar *new_owner;
810 g_variant_get (parameters,
816 if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
818 g_main_loop_quit (loop);
823 test_connection_filter_on_timeout (gpointer user_data)
825 g_printerr ("Timeout waiting 30 sec on service\n");
826 g_assert_not_reached ();
831 test_connection_filter (void)
840 guint timeout_mainloop_id;
841 guint signal_handler_id;
842 FilterEffects effects;
846 memset (&data, '\0', sizeof (FilterData));
851 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
852 g_assert_no_error (error);
853 g_assert (c != NULL);
855 filter_id = g_dbus_connection_add_filter (c,
860 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
861 "/org/freedesktop/DBus", /* path */
862 "org.freedesktop.DBus", /* interface */
864 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
866 g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
867 g_assert_no_error (error);
869 while (data.num_handled == 0)
872 m2 = g_dbus_message_copy (m, &error);
873 g_assert_no_error (error);
874 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &data.serial, &error);
876 g_assert_no_error (error);
878 while (data.num_handled == 1)
881 m2 = g_dbus_message_copy (m, &error);
882 g_assert_no_error (error);
883 g_dbus_message_set_serial (m2, data.serial);
884 /* lock the message to test PRESERVE_SERIAL flag. */
885 g_dbus_message_lock (m2);
886 g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &data.serial, &error);
888 g_assert_no_error (error);
890 while (data.num_handled == 2)
893 m2 = g_dbus_message_copy (m, &error);
894 g_assert_no_error (error);
895 r = g_dbus_connection_send_message_with_reply_sync (c,
897 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
900 NULL, /* GCancellable */
903 g_assert_no_error (error);
904 g_assert (r != NULL);
906 g_assert_cmpint (data.num_handled, ==, 4);
908 g_dbus_connection_remove_filter (c, filter_id);
910 m2 = g_dbus_message_copy (m, &error);
911 g_assert_no_error (error);
912 r = g_dbus_connection_send_message_with_reply_sync (c,
914 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
917 NULL, /* GCancellable */
920 g_assert_no_error (error);
921 g_assert (r != NULL);
923 g_assert_cmpint (data.num_handled, ==, 4);
924 g_assert_cmpint (data.num_outgoing, ==, 4);
926 /* this is safe; testserver will exit once the bus goes away */
927 g_assert (g_spawn_command_line_async ("./gdbus-testserver", NULL));
928 /* wait for service to be available */
929 signal_handler_id = g_dbus_connection_signal_subscribe (c,
930 "org.freedesktop.DBus", /* sender */
931 "org.freedesktop.DBus",
933 "/org/freedesktop/DBus",
935 G_DBUS_SIGNAL_FLAGS_NONE,
936 test_connection_filter_name_owner_changed_signal_handler,
939 g_assert_cmpint (signal_handler_id, !=, 0);
940 timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
941 g_main_loop_run (loop);
942 g_source_remove (timeout_mainloop_id);
943 g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
945 /* now test some combinations... */
946 filter_id = g_dbus_connection_add_filter (c,
951 effects.alter_incoming = FALSE;
952 effects.alter_outgoing = FALSE;
954 result = g_dbus_connection_call_sync (c,
955 "com.example.TestService", /* bus name */
956 "/com/example/TestObject", /* object path */
957 "com.example.Frob", /* interface name */
958 "HelloWorld", /* method name */
959 g_variant_new ("(s)", "Cat"), /* parameters */
960 G_VARIANT_TYPE ("(s)"), /* return type */
961 G_DBUS_CALL_FLAGS_NONE,
965 g_assert_no_error (error);
966 g_variant_get (result, "(&s)", &s);
967 g_assert_cmpstr (s, ==, "You greeted me with 'Cat'. Thanks!");
968 g_variant_unref (result);
970 effects.alter_incoming = TRUE;
971 effects.alter_outgoing = TRUE;
973 result = g_dbus_connection_call_sync (c,
974 "com.example.TestService", /* bus name */
975 "/com/example/TestObject", /* object path */
976 "com.example.Frob", /* interface name */
977 "HelloWorld", /* method name */
978 g_variant_new ("(s)", "Cat"), /* parameters */
979 G_VARIANT_TYPE ("(s)"), /* return type */
980 G_DBUS_CALL_FLAGS_NONE,
984 g_assert_no_error (error);
985 g_variant_get (result, "(&s)", &s);
986 g_assert_cmpstr (s, ==, "MOD: You greeted me with 'MOD: Cat'. Thanks!");
987 g_variant_unref (result);
990 g_dbus_connection_remove_filter (c, filter_id);
998 /* ---------------------------------------------------------------------------------------------------- */
1000 #define NUM_THREADS 50
1003 send_bogus_message (GDBusConnection *c, guint32 *out_serial)
1008 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
1009 "/org/freedesktop/DBus", /* path */
1010 "org.freedesktop.DBus", /* interface */
1012 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
1014 g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, out_serial, &error);
1015 g_assert_no_error (error);
1019 serials_thread_func (GDBusConnection *c)
1021 guint32 message_serial;
1023 /* No calls on this thread yet */
1024 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
1026 /* Send a bogus message and store its serial */
1028 send_bogus_message (c, &message_serial);
1030 /* Give it some time to actually send the message out */
1033 g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
1034 g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
1040 test_connection_serials (void)
1044 GThread *pool[NUM_THREADS];
1050 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1051 g_assert_no_error (error);
1052 g_assert (c != NULL);
1054 /* Status after initialization */
1055 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
1057 /* Send a bogus message */
1058 send_bogus_message (c, NULL);
1059 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1061 /* Start the threads */
1062 for (i = 0; i < NUM_THREADS; i++)
1063 pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
1065 /* Wait until threads are finished */
1066 for (i = 0; i < NUM_THREADS; i++)
1068 g_thread_join (pool[i]);
1069 g_thread_unref (pool[i]);
1072 /* No calls in between on this thread, should be the last value */
1073 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
1075 send_bogus_message (c, NULL);
1077 /* All above calls + calls in threads */
1078 g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
1082 session_bus_down ();
1085 /* ---------------------------------------------------------------------------------------------------- */
1088 test_connection_basic (void)
1090 GDBusConnection *connection;
1092 GDBusCapabilityFlags flags;
1096 gboolean exit_on_close;
1098 GCredentials *credentials;
1103 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1104 g_assert_no_error (error);
1105 g_assert (connection != NULL);
1107 flags = g_dbus_connection_get_capabilities (connection);
1108 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1109 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1111 credentials = g_dbus_connection_get_peer_credentials (connection);
1112 g_assert (credentials == NULL);
1114 g_object_get (connection,
1117 "unique-name", &name,
1119 "exit-on-close", &exit_on_close,
1120 "capabilities", &flags,
1123 g_assert (G_IS_IO_STREAM (stream));
1124 g_assert (g_dbus_is_guid (guid));
1125 g_assert (g_dbus_is_unique_name (name));
1127 g_assert (exit_on_close);
1128 g_assert (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
1129 flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
1130 g_object_unref (stream);
1134 g_object_unref (connection);
1136 session_bus_down ();
1139 /* ---------------------------------------------------------------------------------------------------- */
1145 g_test_init (&argc, &argv, NULL);
1147 /* all the tests rely on a shared main loop */
1148 loop = g_main_loop_new (NULL, FALSE);
1150 g_test_dbus_unset ();
1152 g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
1153 g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
1154 g_test_add_func ("/gdbus/connection/send", test_connection_send);
1155 g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
1156 g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
1157 g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
1158 return g_test_run();