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>
28 #include "gdbus-tests.h"
30 /* all tests rely on a shared mainloop */
31 static GMainLoop *loop = NULL;
33 /* ---------------------------------------------------------------------------------------------------- */
39 EXPLICITLY_FALSE = FALSE,
40 EXPLICITLY_TRUE = TRUE,
49 static const TestData cases[] = {
50 { "default", NULL, IMPLICITLY_TRUE, REMOTE },
51 { "true", NULL, EXPLICITLY_TRUE, REMOTE },
52 { "false", NULL, EXPLICITLY_FALSE, REMOTE },
53 { "we-close", "662100", EXPLICITLY_TRUE, LOCAL },
58 quit_later_cb (gpointer data G_GNUC_UNUSED)
60 g_main_loop_quit (loop);
65 #define VANISHED_PATTERN "*Remote peer vanished with error: Underlying GIOStream returned 0 bytes on an async read (g-io-error-quark, 0). Exiting.*"
68 closed_cb (GDBusConnection *c G_GNUC_UNUSED,
69 gboolean remote_peer_vanished,
73 const TestData *td = test_data;
75 g_assert_cmpint (remote_peer_vanished, ==, (td->who_closes == REMOTE));
76 g_assert_cmpint ((error == NULL), ==, (td->who_closes == LOCAL));
78 /* we delay this so that if exit-on-close was going to happen, it will
81 g_timeout_add (50, quit_later_cb, NULL);
85 close_async_cb (GObject *source G_GNUC_UNUSED,
86 GAsyncResult *res G_GNUC_UNUSED,
87 gpointer nil G_GNUC_UNUSED)
92 test_exit_on_close (gconstpointer test_data)
94 const TestData *td = test_data;
96 /* all the tests rely on a shared main loop */
97 loop = g_main_loop_new (NULL, FALSE);
99 /* all the tests use a session bus with a well-known address that we can bring up and down
100 * using session_bus_up() and session_bus_down().
102 g_unsetenv ("DISPLAY");
103 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
105 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
110 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
112 g_assert (c != NULL);
114 /* the default is meant to be TRUE */
115 if (td->exit_on_close != IMPLICITLY_TRUE)
116 g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
118 g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
119 (td->exit_on_close != EXPLICITLY_FALSE));
120 g_assert (!g_dbus_connection_is_closed (c));
122 g_timeout_add (50, quit_later_cb, NULL);
123 g_main_loop_run (loop);
125 g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
127 if (td->who_closes == LOCAL)
130 GError *error = NULL;
132 v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
133 "/org/freedesktop/DBus",
134 "org.freedesktop.DBus",
137 G_VARIANT_TYPE ("(as)"),
138 G_DBUS_CALL_FLAGS_NONE,
142 g_assert_no_error (error);
143 g_assert (v != NULL);
145 g_dbus_connection_close (c, NULL, close_async_cb, NULL);
152 g_main_loop_run (loop);
153 /* this is only reached when we turn off exit-on-close */
154 g_main_loop_unref (loop);
158 if (td->exit_on_close == EXPLICITLY_FALSE ||
159 td->who_closes == LOCAL)
161 g_test_trap_assert_stdout_unmatched (VANISHED_PATTERN);
162 g_test_trap_assert_passed ();
166 g_test_trap_assert_stdout (VANISHED_PATTERN);
167 g_test_trap_assert_failed();
171 /* ---------------------------------------------------------------------------------------------------- */
180 g_test_init (&argc, &argv, NULL);
182 for (i = 0; cases[i].name != NULL; i++)
184 gchar *name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
186 g_test_add_data_func (name, &cases[i], test_exit_on_close);