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