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>
30 #include "gdbus-tests.h"
32 /* all tests rely on a shared mainloop */
33 static GMainLoop *loop = NULL;
35 /* ---------------------------------------------------------------------------------------------------- */
36 /* Connection life-cycle testing */
37 /* ---------------------------------------------------------------------------------------------------- */
40 test_connection_life_cycle (void)
50 * Check for correct behavior when no bus is present
53 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
54 _g_assert_error_domain (error, G_IO_ERROR);
55 g_assert (!g_dbus_error_is_remote_error (error));
61 * Check for correct behavior when a bus is present
65 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
66 g_assert_no_error (error);
68 g_assert (!g_dbus_connection_is_closed (c));
71 * Check that singleton handling work
73 c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
74 g_assert_no_error (error);
75 g_assert (c2 != NULL);
80 * Check that private connections work
82 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
83 g_assert_no_error (error);
84 g_assert (c2 != NULL);
88 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
89 g_assert_no_error (error);
90 g_assert (c2 != NULL);
91 g_assert (!g_dbus_connection_is_closed (c2));
92 ret = g_dbus_connection_close_sync (c2, NULL, &error);
93 g_assert_no_error (error);
95 _g_assert_signal_received (c2, "closed");
96 g_assert (g_dbus_connection_is_closed (c2));
97 ret = g_dbus_connection_close_sync (c2, NULL, &error);
98 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
103 * Check for correct behavior when the bus goes away
106 g_assert (!g_dbus_connection_is_closed (c));
107 g_dbus_connection_set_exit_on_close (c, FALSE);
109 if (!g_dbus_connection_is_closed (c))
110 _g_assert_signal_received (c, "closed");
111 g_assert (g_dbus_connection_is_closed (c));
113 _g_object_wait_for_single_ref (c);
117 /* ---------------------------------------------------------------------------------------------------- */
118 /* Test that sending and receiving messages work as expected */
119 /* ---------------------------------------------------------------------------------------------------- */
122 msg_cb_expect_error_disconnected (GDBusConnection *connection,
130 result = g_dbus_connection_call_finish (connection,
133 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
134 g_assert (!g_dbus_error_is_remote_error (error));
135 g_error_free (error);
136 g_assert (result == NULL);
138 g_main_loop_quit (loop);
142 msg_cb_expect_error_unknown_method (GDBusConnection *connection,
150 result = g_dbus_connection_call_finish (connection,
153 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
154 g_assert (g_dbus_error_is_remote_error (error));
155 g_assert (result == NULL);
157 g_main_loop_quit (loop);
161 msg_cb_expect_success (GDBusConnection *connection,
169 result = g_dbus_connection_call_finish (connection,
172 g_assert_no_error (error);
173 g_assert (result != NULL);
174 g_variant_unref (result);
176 g_main_loop_quit (loop);
180 msg_cb_expect_error_cancelled (GDBusConnection *connection,
188 result = g_dbus_connection_call_finish (connection,
191 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
192 g_assert (!g_dbus_error_is_remote_error (error));
193 g_error_free (error);
194 g_assert (result == NULL);
196 g_main_loop_quit (loop);
200 msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
208 result = g_dbus_connection_call_finish (connection,
211 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
212 g_assert (!g_dbus_error_is_remote_error (error));
213 g_error_free (error);
214 g_assert (result == NULL);
216 g_main_loop_quit (loop);
219 /* ---------------------------------------------------------------------------------------------------- */
222 test_connection_send (void)
229 /* First, get an unopened connection */
230 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
231 g_assert (c != NULL);
232 g_assert (!g_dbus_connection_is_closed (c));
235 * Check that we never actually send a message if the GCancellable
236 * is already cancelled - i.e. we should get #G_IO_ERROR_CANCELLED
237 * when the actual connection is not up.
239 ca = g_cancellable_new ();
240 g_cancellable_cancel (ca);
241 g_dbus_connection_call (c,
242 "org.freedesktop.DBus", /* bus_name */
243 "/org/freedesktop/DBus", /* object path */
244 "org.freedesktop.DBus", /* interface name */
245 "GetId", /* method name */
247 G_DBUS_CALL_FLAGS_NONE,
250 (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
252 g_main_loop_run (loop);
256 * Check that we get a reply to the GetId() method call.
258 g_dbus_connection_call (c,
259 "org.freedesktop.DBus", /* bus_name */
260 "/org/freedesktop/DBus", /* object path */
261 "org.freedesktop.DBus", /* interface name */
262 "GetId", /* method name */
264 G_DBUS_CALL_FLAGS_NONE,
267 (GAsyncReadyCallback) msg_cb_expect_success,
269 g_main_loop_run (loop);
272 * Check that we get an error reply to the NonExistantMethod() method call.
274 g_dbus_connection_call (c,
275 "org.freedesktop.DBus", /* bus_name */
276 "/org/freedesktop/DBus", /* object path */
277 "org.freedesktop.DBus", /* interface name */
278 "NonExistantMethod", /* method name */
280 G_DBUS_CALL_FLAGS_NONE,
283 (GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
285 g_main_loop_run (loop);
288 * Check that cancellation works when the message is already in flight.
290 ca = g_cancellable_new ();
291 g_dbus_connection_call (c,
292 "org.freedesktop.DBus", /* bus_name */
293 "/org/freedesktop/DBus", /* object path */
294 "org.freedesktop.DBus", /* interface name */
295 "GetId", /* method name */
297 G_DBUS_CALL_FLAGS_NONE,
300 (GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
302 g_cancellable_cancel (ca);
303 g_main_loop_run (loop);
307 * Check that we get an error when sending to a connection that is disconnected.
309 g_dbus_connection_set_exit_on_close (c, FALSE);
311 _g_assert_signal_received (c, "closed");
312 g_assert (g_dbus_connection_is_closed (c));
314 g_dbus_connection_call (c,
315 "org.freedesktop.DBus", /* bus_name */
316 "/org/freedesktop/DBus", /* object path */
317 "org.freedesktop.DBus", /* interface name */
318 "GetId", /* method name */
320 G_DBUS_CALL_FLAGS_NONE,
323 (GAsyncReadyCallback) msg_cb_expect_error_disconnected,
325 g_main_loop_run (loop);
327 _g_object_wait_for_single_ref (c);
331 /* ---------------------------------------------------------------------------------------------------- */
332 /* Connection signal tests */
333 /* ---------------------------------------------------------------------------------------------------- */
336 test_connection_signal_handler (GDBusConnection *connection,
337 const gchar *sender_name,
338 const gchar *object_path,
339 const gchar *interface_name,
340 const gchar *signal_name,
341 GVariant *parameters,
344 gint *counter = user_data;
347 /*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
353 g_main_loop_quit (loop);
357 test_connection_signal_quit_mainloop (gpointer user_data)
359 gboolean *quit_mainloop_fired = user_data;
360 *quit_mainloop_fired = TRUE;
361 g_main_loop_quit (loop);
366 test_connection_signals (void)
378 gint count_name_owner_changed;
386 * Bring up first separate connections
389 /* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
392 if (g_getenv ("G_DBUS_MONITOR") == NULL)
394 c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
395 g_assert (c1 != NULL);
396 g_assert (!g_dbus_connection_is_closed (c1));
399 c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
400 g_assert (c1 != NULL);
401 g_assert (!g_dbus_connection_is_closed (c1));
402 g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
405 * Install two signal handlers for the first connection
407 * - Listen to the signal "Foo" from :1.2 (e.g. c2)
408 * - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
410 * and then count how many times this signal handler was invoked.
412 s1 = g_dbus_connection_signal_subscribe (c1,
414 "org.gtk.GDBus.ExampleInterface",
416 "/org/gtk/GDBus/ExampleInterface",
418 G_DBUS_SIGNAL_FLAGS_NONE,
419 test_connection_signal_handler,
422 s2 = g_dbus_connection_signal_subscribe (c1,
423 NULL, /* match any sender */
424 "org.gtk.GDBus.ExampleInterface",
426 "/org/gtk/GDBus/ExampleInterface",
428 G_DBUS_SIGNAL_FLAGS_NONE,
429 test_connection_signal_handler,
432 s3 = g_dbus_connection_signal_subscribe (c1,
433 "org.freedesktop.DBus", /* sender */
434 "org.freedesktop.DBus", /* interface */
435 "NameOwnerChanged", /* member */
436 "/org/freedesktop/DBus", /* path */
438 G_DBUS_SIGNAL_FLAGS_NONE,
439 test_connection_signal_handler,
440 &count_name_owner_changed,
442 /* Note that s1b is *just like* s1 - this is to catch a bug where N
443 * subscriptions of the same rule causes N calls to each of the N
444 * subscriptions instead of just 1 call to each of the N subscriptions.
446 s1b = g_dbus_connection_signal_subscribe (c1,
448 "org.gtk.GDBus.ExampleInterface",
450 "/org/gtk/GDBus/ExampleInterface",
452 G_DBUS_SIGNAL_FLAGS_NONE,
453 test_connection_signal_handler,
464 count_name_owner_changed = 0;
467 * Bring up two other connections
469 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
470 g_assert (c2 != NULL);
471 g_assert (!g_dbus_connection_is_closed (c2));
472 g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
473 c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
474 g_assert (c3 != NULL);
475 g_assert (!g_dbus_connection_is_closed (c3));
476 g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
479 * Make c2 emit "Foo" - we should catch it twice
481 * Note that there is no way to be sure that the signal subscriptions
482 * on c1 are effective yet - for all we know, the AddMatch() messages
483 * could sit waiting in a buffer somewhere between this process and
484 * the message bus. And emitting signals on c2 (a completely other
485 * socket!) will not necessarily change this.
487 * To ensure this is not the case, do a synchronous call on c1.
489 result = g_dbus_connection_call_sync (c1,
490 "org.freedesktop.DBus", /* bus name */
491 "/org/freedesktop/DBus", /* object path */
492 "org.freedesktop.DBus", /* interface name */
493 "GetId", /* method name */
494 NULL, /* parameters */
495 NULL, /* return type */
496 G_DBUS_CALL_FLAGS_NONE,
500 g_assert_no_error (error);
501 g_assert (result != NULL);
502 g_variant_unref (result);
503 /* now, emit the signal on c2 */
504 ret = g_dbus_connection_emit_signal (c2,
505 NULL, /* destination bus name */
506 "/org/gtk/GDBus/ExampleInterface",
507 "org.gtk.GDBus.ExampleInterface",
511 g_assert_no_error (error);
513 while (!(count_s1 >= 1 && count_s2 >= 1))
514 g_main_loop_run (loop);
515 g_assert_cmpint (count_s1, ==, 1);
516 g_assert_cmpint (count_s2, ==, 1);
519 * Make c3 emit "Foo" - we should catch it only once
521 ret = g_dbus_connection_emit_signal (c3,
522 NULL, /* destination bus name */
523 "/org/gtk/GDBus/ExampleInterface",
524 "org.gtk.GDBus.ExampleInterface",
528 g_assert_no_error (error);
530 while (!(count_s1 == 1 && count_s2 == 2))
531 g_main_loop_run (loop);
532 g_assert_cmpint (count_s1, ==, 1);
533 g_assert_cmpint (count_s2, ==, 2);
536 * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
537 * to avoid spinning forever
539 gboolean quit_mainloop_fired;
540 guint quit_mainloop_id;
541 quit_mainloop_fired = FALSE;
542 quit_mainloop_id = g_timeout_add (5000, test_connection_signal_quit_mainloop, &quit_mainloop_fired);
543 while (count_name_owner_changed < 2 && !quit_mainloop_fired)
544 g_main_loop_run (loop);
545 g_source_remove (quit_mainloop_id);
546 g_assert_cmpint (count_s1, ==, 1);
547 g_assert_cmpint (count_s2, ==, 2);
548 g_assert_cmpint (count_name_owner_changed, ==, 2);
550 g_dbus_connection_signal_unsubscribe (c1, s1);
551 g_dbus_connection_signal_unsubscribe (c1, s2);
552 g_dbus_connection_signal_unsubscribe (c1, s3);
553 g_dbus_connection_signal_unsubscribe (c1, s1b);
555 _g_object_wait_for_single_ref (c1);
556 _g_object_wait_for_single_ref (c2);
557 _g_object_wait_for_single_ref (c3);
566 /* ---------------------------------------------------------------------------------------------------- */
576 filter_func (GDBusConnection *connection,
577 GDBusMessage *message,
581 FilterData *data = user_data;
582 guint32 reply_serial;
586 reply_serial = g_dbus_message_get_reply_serial (message);
587 if (reply_serial == data->serial)
588 data->num_handled += 1;
592 data->num_outgoing += 1;
599 test_connection_filter (void)
608 memset (&data, '\0', sizeof (FilterData));
613 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
614 g_assert_no_error (error);
615 g_assert (c != NULL);
617 filter_id = g_dbus_connection_add_filter (c,
622 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
623 "/org/freedesktop/DBus", /* path */
624 "org.freedesktop.DBus", /* interface */
626 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
628 g_dbus_connection_send_message (c, m, &data.serial, &error);
629 g_assert_no_error (error);
631 while (data.num_handled == 0)
634 g_dbus_message_set_serial (m, 0);
635 g_dbus_connection_send_message (c, m, &data.serial, &error);
636 g_assert_no_error (error);
638 while (data.num_handled == 1)
641 g_dbus_message_set_serial (m, 0);
642 r = g_dbus_connection_send_message_with_reply_sync (c,
646 NULL, /* GCancellable */
648 g_assert_no_error (error);
649 g_assert (r != NULL);
651 g_assert_cmpint (data.num_handled, ==, 3);
653 g_dbus_connection_remove_filter (c, filter_id);
655 g_dbus_message_set_serial (m, 0);
656 r = g_dbus_connection_send_message_with_reply_sync (c,
660 NULL, /* GCancellable */
662 g_assert_no_error (error);
663 g_assert (r != NULL);
665 g_assert_cmpint (data.num_handled, ==, 3);
666 g_assert_cmpint (data.num_outgoing, ==, 3);
668 _g_object_wait_for_single_ref (c);
675 /* ---------------------------------------------------------------------------------------------------- */
678 test_connection_flush_signal_handler (GDBusConnection *connection,
679 const gchar *sender_name,
680 const gchar *object_path,
681 const gchar *interface_name,
682 const gchar *signal_name,
683 GVariant *parameters,
686 g_main_loop_quit (loop);
690 test_connection_flush_on_timeout (gpointer user_data)
692 guint iteration = GPOINTER_TO_UINT (user_data);
693 g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
694 g_assert_not_reached ();
699 test_connection_flush (void)
701 GDBusConnection *connection;
704 guint signal_handler_id;
709 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
710 g_assert_no_error (error);
711 g_assert (connection != NULL);
713 signal_handler_id = g_dbus_connection_signal_subscribe (connection,
715 "org.gtk.GDBus.FlushInterface",
717 "/org/gtk/GDBus/FlushObject",
719 G_DBUS_SIGNAL_FLAGS_NONE,
720 test_connection_flush_signal_handler,
723 g_assert_cmpint (signal_handler_id, !=, 0);
725 for (n = 0; n < 50; n++)
729 guint timeout_mainloop_id;
732 ret = g_spawn_command_line_sync ("./gdbus-connection-flush-helper",
737 g_assert_no_error (error);
738 g_assert (WIFEXITED (exit_status));
739 g_assert_cmpint (WEXITSTATUS (exit_status), ==, 0);
742 timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
743 g_main_loop_run (loop);
744 g_source_remove (timeout_mainloop_id);
747 g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
748 _g_object_wait_for_single_ref (connection);
749 g_object_unref (connection);
754 /* ---------------------------------------------------------------------------------------------------- */
761 g_test_init (&argc, &argv, NULL);
763 /* all the tests rely on a shared main loop */
764 loop = g_main_loop_new (NULL, FALSE);
766 /* all the tests use a session bus with a well-known address that we can bring up and down
767 * using session_bus_up() and session_bus_down().
769 g_unsetenv ("DISPLAY");
770 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
772 g_test_add_func ("/gdbus/connection-life-cycle", test_connection_life_cycle);
773 g_test_add_func ("/gdbus/connection-send", test_connection_send);
774 g_test_add_func ("/gdbus/connection-signals", test_connection_signals);
775 g_test_add_func ("/gdbus/connection-filter", test_connection_filter);
776 g_test_add_func ("/gdbus/connection-flush", test_connection_flush);