Update German translation
[glib.git] / gio / tests / gdbus-exit-on-close.c
blob291cc8f58eca874483bbadf675dfd105150b5209
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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include <gio/gio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
26 #include "gdbus-tests.h"
28 /* all tests rely on a shared mainloop */
29 static GMainLoop *loop = NULL;
31 /* ---------------------------------------------------------------------------------------------------- */
33 typedef struct {
34 const gchar *name;
35 const gchar *bug;
36 enum {
37 EXPLICITLY_FALSE = FALSE,
38 EXPLICITLY_TRUE = TRUE,
39 IMPLICITLY_TRUE
40 } exit_on_close;
41 enum {
42 LOCAL,
43 REMOTE
44 } who_closes;
45 } TestData;
47 static const TestData cases[] = {
48 { "default", NULL, IMPLICITLY_TRUE, REMOTE },
49 { "true", NULL, EXPLICITLY_TRUE, REMOTE },
50 { "false", NULL, EXPLICITLY_FALSE, REMOTE },
51 { "we-close", "662100", EXPLICITLY_TRUE, LOCAL },
52 { NULL }
55 static gboolean
56 quit_later_cb (gpointer data G_GNUC_UNUSED)
58 g_main_loop_quit (loop);
60 return FALSE;
63 static void
64 closed_cb (GDBusConnection *c G_GNUC_UNUSED,
65 gboolean remote_peer_vanished,
66 GError *error,
67 gpointer test_data)
69 const TestData *td = test_data;
71 if (error == NULL)
72 g_debug ("closed (%d, no error)", remote_peer_vanished);
73 else
74 g_debug ("closed (%d, %s %d \"%s\")", remote_peer_vanished,
75 g_quark_to_string (error->domain), error->code, error->message);
77 g_assert_cmpint (remote_peer_vanished, ==, (td->who_closes == REMOTE));
78 g_assert_cmpint ((error == NULL), ==, (td->who_closes == LOCAL));
80 /* we delay this so that if exit-on-close was going to happen, it will
81 * win the race
83 g_timeout_add (50, quit_later_cb, NULL);
86 static void
87 close_async_cb (GObject *source G_GNUC_UNUSED,
88 GAsyncResult *res G_GNUC_UNUSED,
89 gpointer nil G_GNUC_UNUSED)
91 GError *error = NULL;
93 if (g_dbus_connection_close_finish (G_DBUS_CONNECTION (source),
94 res,
95 &error))
97 g_debug ("closed connection");
99 else
101 g_warning ("failed to close connection: %s (%s #%d)",
102 error->message, g_quark_to_string (error->domain),
103 error->code);
107 static void
108 test_exit_on_close_subprocess (gconstpointer test_data)
110 const TestData *td = test_data;
111 GDBusConnection *c;
113 loop = g_main_loop_new (NULL, FALSE);
115 session_bus_up ();
116 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
118 g_assert (c != NULL);
120 /* the default is meant to be TRUE */
121 if (td->exit_on_close != IMPLICITLY_TRUE)
122 g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
124 g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
125 (td->exit_on_close != EXPLICITLY_FALSE));
126 g_assert (!g_dbus_connection_is_closed (c));
128 g_timeout_add (50, quit_later_cb, NULL);
129 g_main_loop_run (loop);
131 g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
133 if (td->who_closes == LOCAL)
135 GVariant *v;
136 GError *error = NULL;
138 v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
139 "/org/freedesktop/DBus",
140 "org.freedesktop.DBus",
141 "ListNames",
142 NULL,
143 G_VARIANT_TYPE ("(as)"),
144 G_DBUS_CALL_FLAGS_NONE,
146 NULL,
147 &error);
148 g_assert_no_error (error);
149 g_assert (v != NULL);
150 g_variant_unref (v);
152 g_dbus_connection_close (c, NULL, close_async_cb, NULL);
154 else
156 session_bus_stop ();
159 g_main_loop_run (loop);
160 /* this is only reached when we turn off exit-on-close */
161 g_main_loop_unref (loop);
162 g_object_unref (c);
164 session_bus_down ();
166 exit (0);
169 static void
170 test_exit_on_close (gconstpointer test_data)
172 const TestData *td = test_data;
173 GTestSubprocessFlags flags;
174 char *child_name;
176 g_test_dbus_unset ();
178 if (g_test_verbose ())
179 flags = G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
180 else
181 flags = 0;
183 child_name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", td->name);
184 g_test_trap_subprocess (child_name, 0, flags);
185 g_free (child_name);
187 if (td->exit_on_close == EXPLICITLY_FALSE ||
188 td->who_closes == LOCAL)
189 g_test_trap_assert_passed ();
190 else
191 g_test_trap_assert_failed();
194 /* ---------------------------------------------------------------------------------------------------- */
197 main (int argc,
198 char *argv[])
200 gint i;
202 g_test_init (&argc, &argv, NULL);
204 for (i = 0; cases[i].name != NULL; i++)
206 gchar *name;
208 name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
209 g_test_add_data_func (name, &cases[i], test_exit_on_close);
210 g_free (name);
212 name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", cases[i].name);
213 g_test_add_data_func (name, &cases[i], test_exit_on_close_subprocess);
214 g_free (name);
217 return g_test_run();