1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* test.c unit test routines
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #ifdef DBUS_BUILD_TESTS
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-list.h>
30 #include <dbus/dbus-sysdeps.h>
32 /* The "debug client" watch/timeout handlers don't dispatch messages,
33 * as we manually pull them in order to verify them. This is why they
34 * are different from the real handlers in connection.c
36 static DBusList *clients = NULL;
37 static DBusLoop *client_loop = NULL;
40 client_watch_callback (DBusWatch *watch,
41 unsigned int condition,
44 /* FIXME this can be done in dbus-mainloop.c
45 * if the code in activation.c for the babysitter
46 * watch handler is fixed.
49 return dbus_watch_handle (watch, condition);
53 add_client_watch (DBusWatch *watch,
56 DBusConnection *connection = data;
58 return _dbus_loop_add_watch (client_loop,
59 watch, client_watch_callback, connection,
64 remove_client_watch (DBusWatch *watch,
67 DBusConnection *connection = data;
69 _dbus_loop_remove_watch (client_loop,
70 watch, client_watch_callback, connection);
74 client_timeout_callback (DBusTimeout *timeout,
77 DBusConnection *connection = data;
79 dbus_connection_ref (connection);
81 /* can return FALSE on OOM but we just let it fire again later */
82 dbus_timeout_handle (timeout);
84 dbus_connection_unref (connection);
88 add_client_timeout (DBusTimeout *timeout,
91 DBusConnection *connection = data;
93 return _dbus_loop_add_timeout (client_loop, timeout, client_timeout_callback, connection, NULL);
97 remove_client_timeout (DBusTimeout *timeout,
100 DBusConnection *connection = data;
102 _dbus_loop_remove_timeout (client_loop, timeout, client_timeout_callback, connection);
105 static DBusHandlerResult
106 client_disconnect_filter (DBusConnection *connection,
107 DBusMessage *message,
110 if (!dbus_message_is_signal (message,
111 DBUS_INTERFACE_LOCAL,
113 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
115 _dbus_verbose ("Removing client %p in disconnect handler\n",
118 _dbus_list_remove (&clients, connection);
120 dbus_connection_unref (connection);
124 _dbus_loop_unref (client_loop);
128 return DBUS_HANDLER_RESULT_HANDLED;
132 bus_setup_debug_client (DBusConnection *connection)
136 if (!dbus_connection_add_filter (connection,
137 client_disconnect_filter,
143 if (client_loop == NULL)
145 client_loop = _dbus_loop_new ();
146 if (client_loop == NULL)
150 if (!dbus_connection_set_watch_functions (connection,
158 if (!dbus_connection_set_timeout_functions (connection,
160 remove_client_timeout,
165 if (!_dbus_list_append (&clients, connection))
173 dbus_connection_remove_filter (connection,
174 client_disconnect_filter,
177 dbus_connection_set_watch_functions (connection,
178 NULL, NULL, NULL, NULL, NULL);
179 dbus_connection_set_timeout_functions (connection,
180 NULL, NULL, NULL, NULL, NULL);
182 _dbus_list_remove_last (&clients, connection);
186 _dbus_loop_unref (client_loop);
195 bus_test_clients_foreach (BusConnectionForeachFunction function,
200 link = _dbus_list_get_first_link (&clients);
203 DBusConnection *connection = link->data;
204 DBusList *next = _dbus_list_get_next_link (&clients, link);
206 if (!(* function) (connection, data))
214 bus_test_client_listed (DBusConnection *connection)
218 link = _dbus_list_get_first_link (&clients);
221 DBusConnection *c = link->data;
222 DBusList *next = _dbus_list_get_next_link (&clients, link);
234 bus_test_run_clients_loop (dbus_bool_t block_once)
236 if (client_loop == NULL)
239 _dbus_verbose ("---> Dispatching on \"client side\"\n");
241 /* dispatch before we block so pending dispatches
242 * won't make our block return early
244 _dbus_loop_dispatch (client_loop);
246 /* Do one blocking wait, since we're expecting data */
249 _dbus_verbose ("---> blocking on \"client side\"\n");
250 _dbus_loop_iterate (client_loop, TRUE);
253 /* Then mop everything up */
254 while (_dbus_loop_iterate (client_loop, FALSE))
257 _dbus_verbose ("---> Done dispatching on \"client side\"\n");
261 bus_test_run_bus_loop (BusContext *context,
262 dbus_bool_t block_once)
264 _dbus_verbose ("---> Dispatching on \"server side\"\n");
266 /* dispatch before we block so pending dispatches
267 * won't make our block return early
269 _dbus_loop_dispatch (bus_context_get_loop (context));
271 /* Do one blocking wait, since we're expecting data */
274 _dbus_verbose ("---> blocking on \"server side\"\n");
275 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
278 /* Then mop everything up */
279 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
282 _dbus_verbose ("---> Done dispatching on \"server side\"\n");
286 bus_test_run_everything (BusContext *context)
288 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
289 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
294 bus_context_new_test (const DBusString *test_data_dir,
295 const char *filename)
298 DBusString config_file;
302 if (!_dbus_string_init (&config_file))
304 _dbus_warn ("No memory\n");
308 if (!_dbus_string_copy (test_data_dir, 0,
311 _dbus_warn ("No memory\n");
312 _dbus_string_free (&config_file);
316 _dbus_string_init_const (&relative, filename);
318 if (!_dbus_concat_dir_and_file (&config_file, &relative))
320 _dbus_warn ("No memory\n");
321 _dbus_string_free (&config_file);
325 dbus_error_init (&error);
326 context = bus_context_new (&config_file, FALSE, NULL, NULL, &error);
329 _DBUS_ASSERT_ERROR_IS_SET (&error);
331 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
332 filename, error.message);
334 dbus_error_free (&error);
336 _dbus_string_free (&config_file);
341 _dbus_string_free (&config_file);