1 #include <config.h>
2 #include "test-utils.h"
3
4 typedef struct
5 {
6 DBusLoop *loop;
7 DBusConnection *connection;
8
9 } CData;
10
11 static dbus_bool_t
add_watch(DBusWatch * watch,void * data)12 add_watch (DBusWatch *watch,
13 void *data)
14 {
15 CData *cd = data;
16
17 return _dbus_loop_add_watch (cd->loop, watch);
18 }
19
20 static void
remove_watch(DBusWatch * watch,void * data)21 remove_watch (DBusWatch *watch,
22 void *data)
23 {
24 CData *cd = data;
25
26 _dbus_loop_remove_watch (cd->loop, watch);
27 }
28
29 static void
toggle_watch(DBusWatch * watch,void * data)30 toggle_watch (DBusWatch *watch,
31 void *data)
32 {
33 CData *cd = data;
34
35 _dbus_loop_toggle_watch (cd->loop, watch);
36 }
37
38 static dbus_bool_t
add_timeout(DBusTimeout * timeout,void * data)39 add_timeout (DBusTimeout *timeout,
40 void *data)
41 {
42 CData *cd = data;
43
44 return _dbus_loop_add_timeout (cd->loop, timeout);
45 }
46
47 static void
remove_timeout(DBusTimeout * timeout,void * data)48 remove_timeout (DBusTimeout *timeout,
49 void *data)
50 {
51 CData *cd = data;
52
53 _dbus_loop_remove_timeout (cd->loop, timeout);
54 }
55
56 static void
dispatch_status_function(DBusConnection * connection,DBusDispatchStatus new_status,void * data)57 dispatch_status_function (DBusConnection *connection,
58 DBusDispatchStatus new_status,
59 void *data)
60 {
61 DBusLoop *loop = data;
62
63 if (new_status != DBUS_DISPATCH_COMPLETE)
64 {
65 while (!_dbus_loop_queue_dispatch (loop, connection))
66 _dbus_wait_for_memory ();
67 }
68 }
69
70 static void
cdata_free(void * data)71 cdata_free (void *data)
72 {
73 CData *cd = data;
74
75 dbus_connection_unref (cd->connection);
76 _dbus_loop_unref (cd->loop);
77
78 dbus_free (cd);
79 }
80
81 static CData*
cdata_new(DBusLoop * loop,DBusConnection * connection)82 cdata_new (DBusLoop *loop,
83 DBusConnection *connection)
84 {
85 CData *cd;
86
87 cd = dbus_new0 (CData, 1);
88 if (cd == NULL)
89 return NULL;
90
91 cd->loop = loop;
92 cd->connection = connection;
93
94 dbus_connection_ref (cd->connection);
95 _dbus_loop_ref (cd->loop);
96
97 return cd;
98 }
99
100 dbus_bool_t
test_connection_setup(DBusLoop * loop,DBusConnection * connection)101 test_connection_setup (DBusLoop *loop,
102 DBusConnection *connection)
103 {
104 CData *cd;
105
106 cd = NULL;
107
108 dbus_connection_set_dispatch_status_function (connection, dispatch_status_function,
109 loop, NULL);
110
111 cd = cdata_new (loop, connection);
112 if (cd == NULL)
113 goto nomem;
114
115 if (!dbus_connection_set_watch_functions (connection,
116 add_watch,
117 remove_watch,
118 toggle_watch,
119 cd, cdata_free))
120 goto nomem;
121
122
123 cd = cdata_new (loop, connection);
124 if (cd == NULL)
125 goto nomem;
126
127 if (!dbus_connection_set_timeout_functions (connection,
128 add_timeout,
129 remove_timeout,
130 NULL,
131 cd, cdata_free))
132 goto nomem;
133
134 if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
135 {
136 if (!_dbus_loop_queue_dispatch (loop, connection))
137 goto nomem;
138 }
139
140 return TRUE;
141
142 nomem:
143 if (cd)
144 cdata_free (cd);
145
146 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
147 dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL);
148 dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL);
149
150 return FALSE;
151 }
152
153 void
test_connection_shutdown(DBusLoop * loop,DBusConnection * connection)154 test_connection_shutdown (DBusLoop *loop,
155 DBusConnection *connection)
156 {
157 if (!dbus_connection_set_watch_functions (connection,
158 NULL,
159 NULL,
160 NULL,
161 NULL, NULL))
162 _dbus_assert_not_reached ("setting watch functions to NULL failed");
163
164 if (!dbus_connection_set_timeout_functions (connection,
165 NULL,
166 NULL,
167 NULL,
168 NULL, NULL))
169 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
170
171 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
172 }
173
174 typedef struct
175 {
176 DBusLoop *loop;
177 DBusServer *server;
178 } ServerData;
179
180 static void
serverdata_free(void * data)181 serverdata_free (void *data)
182 {
183 ServerData *sd = data;
184
185 dbus_server_unref (sd->server);
186 _dbus_loop_unref (sd->loop);
187
188 dbus_free (sd);
189 }
190
191 static ServerData*
serverdata_new(DBusLoop * loop,DBusServer * server)192 serverdata_new (DBusLoop *loop,
193 DBusServer *server)
194 {
195 ServerData *sd;
196
197 sd = dbus_new0 (ServerData, 1);
198 if (sd == NULL)
199 return NULL;
200
201 sd->loop = loop;
202 sd->server = server;
203
204 dbus_server_ref (sd->server);
205 _dbus_loop_ref (sd->loop);
206
207 return sd;
208 }
209
210 static dbus_bool_t
add_server_watch(DBusWatch * watch,void * data)211 add_server_watch (DBusWatch *watch,
212 void *data)
213 {
214 ServerData *context = data;
215
216 return _dbus_loop_add_watch (context->loop, watch);
217 }
218
219 static void
toggle_server_watch(DBusWatch * watch,void * data)220 toggle_server_watch (DBusWatch *watch,
221 void *data)
222 {
223 ServerData *context = data;
224
225 _dbus_loop_toggle_watch (context->loop, watch);
226 }
227
228 static void
remove_server_watch(DBusWatch * watch,void * data)229 remove_server_watch (DBusWatch *watch,
230 void *data)
231 {
232 ServerData *context = data;
233
234 _dbus_loop_remove_watch (context->loop, watch);
235 }
236
237 static dbus_bool_t
add_server_timeout(DBusTimeout * timeout,void * data)238 add_server_timeout (DBusTimeout *timeout,
239 void *data)
240 {
241 ServerData *context = data;
242
243 return _dbus_loop_add_timeout (context->loop, timeout);
244 }
245
246 static void
remove_server_timeout(DBusTimeout * timeout,void * data)247 remove_server_timeout (DBusTimeout *timeout,
248 void *data)
249 {
250 ServerData *context = data;
251
252 _dbus_loop_remove_timeout (context->loop, timeout);
253 }
254
255 dbus_bool_t
test_server_setup(DBusLoop * loop,DBusServer * server)256 test_server_setup (DBusLoop *loop,
257 DBusServer *server)
258 {
259 ServerData *sd;
260
261 sd = serverdata_new (loop, server);
262 if (sd == NULL)
263 goto nomem;
264
265 if (!dbus_server_set_watch_functions (server,
266 add_server_watch,
267 remove_server_watch,
268 toggle_server_watch,
269 sd,
270 serverdata_free))
271 {
272 goto nomem;
273 }
274
275 sd = serverdata_new (loop, server);
276 if (sd == NULL)
277 goto nomem;
278
279 if (!dbus_server_set_timeout_functions (server,
280 add_server_timeout,
281 remove_server_timeout,
282 NULL,
283 sd, serverdata_free))
284 {
285 goto nomem;
286 }
287 return TRUE;
288
289 nomem:
290 if (sd)
291 serverdata_free (sd);
292
293 test_server_shutdown (loop, server);
294
295 return FALSE;
296 }
297
298 void
test_server_shutdown(DBusLoop * loop,DBusServer * server)299 test_server_shutdown (DBusLoop *loop,
300 DBusServer *server)
301 {
302 dbus_server_disconnect (server);
303
304 if (!dbus_server_set_watch_functions (server,
305 NULL, NULL, NULL,
306 NULL,
307 NULL))
308 _dbus_assert_not_reached ("setting watch functions to NULL failed");
309
310 if (!dbus_server_set_timeout_functions (server,
311 NULL, NULL, NULL,
312 NULL,
313 NULL))
314 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
315 }
316