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 test_connection_signal_handler,
421 s2 = g_dbus_connection_signal_subscribe (c1,
422 NULL, /* match any sender */
423 "org.gtk.GDBus.ExampleInterface",
425 "/org/gtk/GDBus/ExampleInterface",
427 test_connection_signal_handler,
430 s3 = g_dbus_connection_signal_subscribe (c1,
431 "org.freedesktop.DBus", /* sender */
432 "org.freedesktop.DBus", /* interface */
433 "NameOwnerChanged", /* member */
434 "/org/freedesktop/DBus", /* path */
436 test_connection_signal_handler,
437 &count_name_owner_changed,
439 /* Note that s1b is *just like* s1 - this is to catch a bug where N
440 * subscriptions of the same rule causes N calls to each of the N
441 * subscriptions instead of just 1 call to each of the N subscriptions.
443 s1b = g_dbus_connection_signal_subscribe (c1,
445 "org.gtk.GDBus.ExampleInterface",
447 "/org/gtk/GDBus/ExampleInterface",
449 test_connection_signal_handler,
460 count_name_owner_changed = 0;
463 * Bring up two other connections
465 c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
466 g_assert (c2 != NULL);
467 g_assert (!g_dbus_connection_is_closed (c2));
468 g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
469 c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
470 g_assert (c3 != NULL);
471 g_assert (!g_dbus_connection_is_closed (c3));
472 g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
475 * Make c2 emit "Foo" - we should catch it twice
477 * Note that there is no way to be sure that the signal subscriptions
478 * on c1 are effective yet - for all we know, the AddMatch() messages
479 * could sit waiting in a buffer somewhere between this process and
480 * the message bus. And emitting signals on c2 (a completely other
481 * socket!) will not necessarily change this.
483 * To ensure this is not the case, do a synchronous call on c1.
485 result = g_dbus_connection_call_sync (c1,
486 "org.freedesktop.DBus", /* bus name */
487 "/org/freedesktop/DBus", /* object path */
488 "org.freedesktop.DBus", /* interface name */
489 "GetId", /* method name */
490 NULL, /* parameters */
491 NULL, /* return type */
492 G_DBUS_CALL_FLAGS_NONE,
496 g_assert_no_error (error);
497 g_assert (result != NULL);
498 g_variant_unref (result);
499 /* now, emit the signal on c2 */
500 ret = g_dbus_connection_emit_signal (c2,
501 NULL, /* destination bus name */
502 "/org/gtk/GDBus/ExampleInterface",
503 "org.gtk.GDBus.ExampleInterface",
507 g_assert_no_error (error);
509 while (!(count_s1 >= 1 && count_s2 >= 1))
510 g_main_loop_run (loop);
511 g_assert_cmpint (count_s1, ==, 1);
512 g_assert_cmpint (count_s2, ==, 1);
515 * Make c3 emit "Foo" - we should catch it only once
517 ret = g_dbus_connection_emit_signal (c3,
518 NULL, /* destination bus name */
519 "/org/gtk/GDBus/ExampleInterface",
520 "org.gtk.GDBus.ExampleInterface",
524 g_assert_no_error (error);
526 while (!(count_s1 == 1 && count_s2 == 2))
527 g_main_loop_run (loop);
528 g_assert_cmpint (count_s1, ==, 1);
529 g_assert_cmpint (count_s2, ==, 2);
532 * Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
533 * to avoid spinning forever
535 gboolean quit_mainloop_fired;
536 guint quit_mainloop_id;
537 quit_mainloop_fired = FALSE;
538 quit_mainloop_id = g_timeout_add (5000, test_connection_signal_quit_mainloop, &quit_mainloop_fired);
539 while (count_name_owner_changed < 2 && !quit_mainloop_fired)
540 g_main_loop_run (loop);
541 g_source_remove (quit_mainloop_id);
542 g_assert_cmpint (count_s1, ==, 1);
543 g_assert_cmpint (count_s2, ==, 2);
544 g_assert_cmpint (count_name_owner_changed, ==, 2);
546 g_dbus_connection_signal_unsubscribe (c1, s1);
547 g_dbus_connection_signal_unsubscribe (c1, s2);
548 g_dbus_connection_signal_unsubscribe (c1, s3);
549 g_dbus_connection_signal_unsubscribe (c1, s1b);
551 _g_object_wait_for_single_ref (c1);
552 _g_object_wait_for_single_ref (c2);
553 _g_object_wait_for_single_ref (c3);
562 /* ---------------------------------------------------------------------------------------------------- */
572 filter_func (GDBusConnection *connection,
573 GDBusMessage *message,
577 FilterData *data = user_data;
578 guint32 reply_serial;
582 reply_serial = g_dbus_message_get_reply_serial (message);
583 if (reply_serial == data->serial)
584 data->num_handled += 1;
588 data->num_outgoing += 1;
595 test_connection_filter (void)
604 memset (&data, '\0', sizeof (FilterData));
609 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
610 g_assert_no_error (error);
611 g_assert (c != NULL);
613 filter_id = g_dbus_connection_add_filter (c,
618 m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
619 "/org/freedesktop/DBus", /* path */
620 "org.freedesktop.DBus", /* interface */
622 g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
624 g_dbus_connection_send_message (c, m, &data.serial, &error);
625 g_assert_no_error (error);
627 while (data.num_handled == 0)
630 g_dbus_message_set_serial (m, 0);
631 g_dbus_connection_send_message (c, m, &data.serial, &error);
632 g_assert_no_error (error);
634 while (data.num_handled == 1)
637 g_dbus_message_set_serial (m, 0);
638 r = g_dbus_connection_send_message_with_reply_sync (c,
642 NULL, /* GCancellable */
644 g_assert_no_error (error);
645 g_assert (r != NULL);
647 g_assert_cmpint (data.num_handled, ==, 3);
649 g_dbus_connection_remove_filter (c, filter_id);
651 g_dbus_message_set_serial (m, 0);
652 r = g_dbus_connection_send_message_with_reply_sync (c,
656 NULL, /* GCancellable */
658 g_assert_no_error (error);
659 g_assert (r != NULL);
661 g_assert_cmpint (data.num_handled, ==, 3);
662 g_assert_cmpint (data.num_outgoing, ==, 3);
664 _g_object_wait_for_single_ref (c);
671 /* ---------------------------------------------------------------------------------------------------- */
674 test_connection_flush_signal_handler (GDBusConnection *connection,
675 const gchar *sender_name,
676 const gchar *object_path,
677 const gchar *interface_name,
678 const gchar *signal_name,
679 GVariant *parameters,
682 g_main_loop_quit (loop);
686 test_connection_flush_on_timeout (gpointer user_data)
688 guint iteration = GPOINTER_TO_UINT (user_data);
689 g_printerr ("Timeout waiting 1000 msec on iteration %d\n", iteration);
690 g_assert_not_reached ();
695 test_connection_flush (void)
697 GDBusConnection *connection;
700 guint signal_handler_id;
705 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
706 g_assert_no_error (error);
707 g_assert (connection != NULL);
709 signal_handler_id = g_dbus_connection_signal_subscribe (connection,
711 "org.gtk.GDBus.FlushInterface",
713 "/org/gtk/GDBus/FlushObject",
715 test_connection_flush_signal_handler,
718 g_assert_cmpint (signal_handler_id, !=, 0);
720 for (n = 0; n < 50; n++)
724 guint timeout_mainloop_id;
727 ret = g_spawn_command_line_sync ("./gdbus-connection-flush-helper",
732 g_assert_no_error (error);
733 g_assert (WIFEXITED (exit_status));
734 g_assert_cmpint (WEXITSTATUS (exit_status), ==, 0);
737 timeout_mainloop_id = g_timeout_add (1000, test_connection_flush_on_timeout, GUINT_TO_POINTER (n));
738 g_main_loop_run (loop);
739 g_source_remove (timeout_mainloop_id);
742 g_dbus_connection_signal_unsubscribe (connection, signal_handler_id);
743 _g_object_wait_for_single_ref (connection);
744 g_object_unref (connection);
749 /* ---------------------------------------------------------------------------------------------------- */
756 g_test_init (&argc, &argv, NULL);
758 /* all the tests rely on a shared main loop */
759 loop = g_main_loop_new (NULL, FALSE);
761 /* all the tests use a session bus with a well-known address that we can bring up and down
762 * using session_bus_up() and session_bus_down().
764 g_unsetenv ("DISPLAY");
765 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
767 g_test_add_func ("/gdbus/connection-life-cycle", test_connection_life_cycle);
768 g_test_add_func ("/gdbus/connection-send", test_connection_send);
769 g_test_add_func ("/gdbus/connection-signals", test_connection_signals);
770 g_test_add_func ("/gdbus/connection-filter", test_connection_filter);
771 g_test_add_func ("/gdbus/connection-flush", test_connection_flush);