1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* GLib testing framework examples and tests
*
* Copyright (C) 2008-2010 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <gio/gio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "gdbus-tests.h"
/* all tests rely on a shared mainloop */
static GMainLoop *loop = NULL;
/* ---------------------------------------------------------------------------------------------------- */
typedef struct {
const gchar *name;
const gchar *bug;
enum {
EXPLICITLY_FALSE = FALSE,
EXPLICITLY_TRUE = TRUE,
IMPLICITLY_TRUE
} exit_on_close;
enum {
LOCAL,
REMOTE
} who_closes;
} TestData;
static const TestData cases[] = {
{ "default", NULL, IMPLICITLY_TRUE, REMOTE },
{ "true", NULL, EXPLICITLY_TRUE, REMOTE },
{ "false", NULL, EXPLICITLY_FALSE, REMOTE },
{ "we-close", "662100", EXPLICITLY_TRUE, LOCAL },
{ NULL }
};
static gboolean
quit_later_cb (gpointer data G_GNUC_UNUSED)
{
g_main_loop_quit (loop);
return FALSE;
}
#define VANISHED_PATTERN "*Remote peer vanished with error: Underlying GIOStream returned 0 bytes on an async read (g-io-error-quark, 0). Exiting.*"
static void
closed_cb (GDBusConnection *c G_GNUC_UNUSED,
gboolean remote_peer_vanished,
GError *error,
gpointer test_data)
{
const TestData *td = test_data;
if (error == NULL)
g_debug ("closed (%d, no error)", remote_peer_vanished);
else
g_debug ("closed (%d, %s %d \"%s\")", remote_peer_vanished,
g_quark_to_string (error->domain), error->code, error->message);
g_assert_cmpint (remote_peer_vanished, ==, (td->who_closes == REMOTE));
g_assert_cmpint ((error == NULL), ==, (td->who_closes == LOCAL));
/* we delay this so that if exit-on-close was going to happen, it will
* win the race
*/
g_timeout_add (50, quit_later_cb, NULL);
}
static void
close_async_cb (GObject *source G_GNUC_UNUSED,
GAsyncResult *res G_GNUC_UNUSED,
gpointer nil G_GNUC_UNUSED)
{
GError *error = NULL;
if (g_dbus_connection_close_finish (G_DBUS_CONNECTION (source),
res,
&error))
{
g_debug ("closed connection");
}
else
{
g_warning ("failed to close connection: %s (%s #%d)",
error->message, g_quark_to_string (error->domain),
error->code);
}
}
static void
test_exit_on_close (gconstpointer test_data)
{
const TestData *td = test_data;
GTestTrapFlags silence;
/* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset ();
if (g_test_verbose ())
silence = 0;
else
silence = G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
if (g_test_trap_fork (0, silence))
{
GDBusConnection *c;
session_bus_up ();
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert (c != NULL);
/* the default is meant to be TRUE */
if (td->exit_on_close != IMPLICITLY_TRUE)
g_dbus_connection_set_exit_on_close (c, td->exit_on_close);
g_assert_cmpint (g_dbus_connection_get_exit_on_close (c), ==,
(td->exit_on_close != EXPLICITLY_FALSE));
g_assert (!g_dbus_connection_is_closed (c));
g_timeout_add (50, quit_later_cb, NULL);
g_main_loop_run (loop);
g_signal_connect (c, "closed", G_CALLBACK (closed_cb), (gpointer) td);
if (td->who_closes == LOCAL)
{
GVariant *v;
GError *error = NULL;
v = g_dbus_connection_call_sync (c, "org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"ListNames",
NULL,
G_VARIANT_TYPE ("(as)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_assert (v != NULL);
g_variant_unref (v);
g_dbus_connection_close (c, NULL, close_async_cb, NULL);
}
else
{
session_bus_stop ();
}
g_main_loop_run (loop);
/* this is only reached when we turn off exit-on-close */
g_main_loop_unref (loop);
g_object_unref (c);
session_bus_down ();
exit (0);
}
if (td->exit_on_close == EXPLICITLY_FALSE ||
td->who_closes == LOCAL)
{
g_test_trap_assert_stdout_unmatched (VANISHED_PATTERN);
g_test_trap_assert_passed ();
}
else
{
g_test_trap_assert_stdout (VANISHED_PATTERN);
g_test_trap_assert_failed();
}
}
/* ---------------------------------------------------------------------------------------------------- */
int
main (int argc,
char *argv[])
{
gint i;
g_test_init (&argc, &argv, NULL);
for (i = 0; cases[i].name != NULL; i++)
{
gchar *name = g_strdup_printf ("/gdbus/exit-on-close/%s", cases[i].name);
g_test_add_data_func (name, &cases[i], test_exit_on_close);
g_free (name);
}
return g_test_run();
}
|