Updated FSF's address
[platform/upstream/glib.git] / gio / tests / gdbus-exit-on-close.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include <gio/gio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include "gdbus-tests.h"
27
28 /* all tests rely on a shared mainloop */
29 static GMainLoop *loop = NULL;
30
31 /* ---------------------------------------------------------------------------------------------------- */
32
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;
46
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 }
53 };
54
55 static gboolean
56 quit_later_cb (gpointer data G_GNUC_UNUSED)
57 {
58   g_main_loop_quit (loop);
59
60   return FALSE;
61 }
62
63 #define VANISHED_PATTERN "*Remote peer vanished with error: Underlying GIOStream returned 0 bytes on an async read (g-io-error-quark, 0). Exiting.*"
64
65 static void
66 closed_cb (GDBusConnection  *c G_GNUC_UNUSED,
67            gboolean          remote_peer_vanished,
68            GError           *error,
69            gpointer          test_data)
70 {
71   const TestData *td = test_data;
72
73   if (error == NULL)
74     g_debug ("closed (%d, no error)", remote_peer_vanished);
75   else
76     g_debug ("closed (%d, %s %d \"%s\")", remote_peer_vanished,
77              g_quark_to_string (error->domain), error->code, error->message);
78
79   g_assert_cmpint (remote_peer_vanished, ==, (td->who_closes == REMOTE));
80   g_assert_cmpint ((error == NULL), ==, (td->who_closes == LOCAL));
81
82   /* we delay this so that if exit-on-close was going to happen, it will
83    * win the race
84    */
85   g_timeout_add (50, quit_later_cb, NULL);
86 }
87
88 static void
89 close_async_cb (GObject *source G_GNUC_UNUSED,
90                 GAsyncResult *res G_GNUC_UNUSED,
91                 gpointer nil G_GNUC_UNUSED)
92 {
93   GError *error = NULL;
94
95   if (g_dbus_connection_close_finish (G_DBUS_CONNECTION (source),
96                                       res,
97                                       &error))
98     {
99       g_debug ("closed connection");
100     }
101   else
102     {
103       g_warning ("failed to close connection: %s (%s #%d)",
104                  error->message, g_quark_to_string (error->domain),
105                  error->code);
106     }
107 }
108
109 static void
110 test_exit_on_close_subprocess (gconstpointer test_data)
111 {
112   const TestData *td = test_data;
113   GDBusConnection *c;
114
115   loop = g_main_loop_new (NULL, FALSE);
116
117   session_bus_up ();
118   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
119
120   g_assert (c != NULL);
121
122   /* the default is meant to be TRUE */
123   if (td->exit_on_close != IMPLICITLY_TRUE)
124     g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
125
126   g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
127                    (td->exit_on_close != EXPLICITLY_FALSE));
128   g_assert (!g_dbus_connection_is_closed (c));
129
130   g_timeout_add (50, quit_later_cb, NULL);
131   g_main_loop_run (loop);
132
133   g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
134
135   if (td->who_closes == LOCAL)
136     {
137       GVariant *v;
138       GError *error = NULL;
139
140       v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
141                                        "/org/freedesktop/DBus",
142                                        "org.freedesktop.DBus",
143                                        "ListNames",
144                                        NULL,
145                                        G_VARIANT_TYPE ("(as)"),
146                                        G_DBUS_CALL_FLAGS_NONE,
147                                        -1,
148                                        NULL,
149                                        &error);
150       g_assert_no_error (error);
151       g_assert (v != NULL);
152       g_variant_unref (v);
153
154       g_dbus_connection_close (c, NULL, close_async_cb, NULL);
155     }
156   else
157     {
158       session_bus_stop ();
159     }
160
161   g_main_loop_run (loop);
162   /* this is only reached when we turn off exit-on-close */
163   g_main_loop_unref (loop);
164   g_object_unref (c);
165
166   session_bus_down ();
167
168   exit (0);
169 }
170
171 static void
172 test_exit_on_close (gconstpointer test_data)
173 {
174   const TestData *td = test_data;
175   GTestSubprocessFlags flags;
176   char *child_name;
177
178   g_test_dbus_unset ();
179
180   if (g_test_verbose ())
181     flags = G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;
182   else
183     flags = 0;
184
185   child_name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", td->name);
186   g_test_trap_subprocess (child_name, 0, flags);
187   g_free (child_name);
188
189   if (td->exit_on_close == EXPLICITLY_FALSE ||
190       td->who_closes == LOCAL)
191     {
192       g_test_trap_assert_stderr_unmatched (VANISHED_PATTERN);
193       g_test_trap_assert_passed ();
194     }
195   else
196     {
197       g_test_trap_assert_stderr (VANISHED_PATTERN);
198       g_test_trap_assert_failed();
199     }
200 }
201
202 /* ---------------------------------------------------------------------------------------------------- */
203
204 int
205 main (int   argc,
206       char *argv[])
207 {
208   gint i;
209
210   g_test_init (&argc, &argv, NULL);
211
212   for (i = 0; cases[i].name != NULL; i++)
213     {
214       gchar *name;
215
216       name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
217       g_test_add_data_func (name, &cases[i], test_exit_on_close);
218       g_free (name);
219
220       name = g_strdup_printf ("/gdbus/exit-on-close/%s/subprocess", cases[i].name);
221       g_test_add_data_func (name, &cases[i], test_exit_on_close_subprocess);
222       g_free (name);
223     }
224
225   return g_test_run();
226 }