1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* test.c unit test routines
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifdef DBUS_BUILD_TESTS
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-list.h>
31 /* The "debug client" watch/timeout handlers don't dispatch messages,
32 * as we manually pull them in order to verify them. This is why they
33 * are different from the real handlers in connection.c
35 static DBusList *clients = NULL;
36 static DBusLoop *client_loop = NULL;
39 client_watch_callback (DBusWatch *watch,
40 unsigned int condition,
43 /* FIXME this can be done in dbus-mainloop.c
44 * if the code in activation.c for the babysitter
45 * watch handler is fixed.
48 return dbus_watch_handle (watch, condition);
52 add_client_watch (DBusWatch *watch,
55 DBusConnection *connection = data;
57 return _dbus_loop_add_watch (client_loop,
58 watch, client_watch_callback, connection,
63 remove_client_watch (DBusWatch *watch,
66 DBusConnection *connection = data;
68 _dbus_loop_remove_watch (client_loop,
69 watch, client_watch_callback, connection);
73 client_timeout_callback (DBusTimeout *timeout,
76 DBusConnection *connection = data;
78 dbus_connection_ref (connection);
80 /* can return FALSE on OOM but we just let it fire again later */
81 dbus_timeout_handle (timeout);
83 dbus_connection_unref (connection);
87 add_client_timeout (DBusTimeout *timeout,
90 DBusConnection *connection = data;
92 return _dbus_loop_add_timeout (client_loop, timeout, client_timeout_callback, connection, NULL);
96 remove_client_timeout (DBusTimeout *timeout,
99 DBusConnection *connection = data;
101 _dbus_loop_remove_timeout (client_loop, timeout, client_timeout_callback, connection);
104 static DBusHandlerResult
105 client_disconnect_filter (DBusConnection *connection,
106 DBusMessage *message,
109 if (!dbus_message_is_signal (message,
110 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
112 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
114 _dbus_verbose ("Removing client %p in disconnect handler\n",
117 _dbus_list_remove (&clients, connection);
119 dbus_connection_unref (connection);
123 _dbus_loop_unref (client_loop);
127 return DBUS_HANDLER_RESULT_HANDLED;
131 bus_setup_debug_client (DBusConnection *connection)
135 if (!dbus_connection_add_filter (connection,
136 client_disconnect_filter,
142 if (client_loop == NULL)
144 client_loop = _dbus_loop_new ();
145 if (client_loop == NULL)
149 if (!dbus_connection_set_watch_functions (connection,
157 if (!dbus_connection_set_timeout_functions (connection,
159 remove_client_timeout,
164 if (!_dbus_list_append (&clients, connection))
172 dbus_connection_remove_filter (connection,
173 client_disconnect_filter,
176 dbus_connection_set_watch_functions (connection,
177 NULL, NULL, NULL, NULL, NULL);
178 dbus_connection_set_timeout_functions (connection,
179 NULL, NULL, NULL, NULL, NULL);
181 _dbus_list_remove_last (&clients, connection);
185 _dbus_loop_unref (client_loop);
194 bus_test_clients_foreach (BusConnectionForeachFunction function,
199 link = _dbus_list_get_first_link (&clients);
202 DBusConnection *connection = link->data;
203 DBusList *next = _dbus_list_get_next_link (&clients, link);
205 if (!(* function) (connection, data))
213 bus_test_client_listed (DBusConnection *connection)
217 link = _dbus_list_get_first_link (&clients);
220 DBusConnection *c = link->data;
221 DBusList *next = _dbus_list_get_next_link (&clients, link);
233 bus_test_run_clients_loop (dbus_bool_t block_once)
235 if (client_loop == NULL)
238 /* dispatch before we block so pending dispatches
239 * won't make our block return early
241 _dbus_loop_dispatch (client_loop);
243 /* Do one blocking wait, since we're expecting data */
246 _dbus_verbose ("---> blocking on \"client side\"\n");
247 _dbus_loop_iterate (client_loop, TRUE);
250 /* Then mop everything up */
251 while (_dbus_loop_iterate (client_loop, FALSE))
256 bus_test_run_bus_loop (BusContext *context,
257 dbus_bool_t block_once)
259 /* dispatch before we block so pending dispatches
260 * won't make our block return early
262 _dbus_loop_dispatch (bus_context_get_loop (context));
264 /* Do one blocking wait, since we're expecting data */
267 _dbus_verbose ("---> blocking on \"server side\"\n");
268 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
271 /* Then mop everything up */
272 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
277 bus_test_run_everything (BusContext *context)
279 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
280 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
285 bus_context_new_test (const DBusString *test_data_dir,
286 const char *filename)
289 DBusString config_file;
293 if (!_dbus_string_init (&config_file))
295 _dbus_warn ("No memory\n");
299 if (!_dbus_string_copy (test_data_dir, 0,
302 _dbus_warn ("No memory\n");
303 _dbus_string_free (&config_file);
307 _dbus_string_init_const (&relative, filename);
309 if (!_dbus_concat_dir_and_file (&config_file, &relative))
311 _dbus_warn ("No memory\n");
312 _dbus_string_free (&config_file);
316 dbus_error_init (&error);
317 context = bus_context_new (&config_file, FALSE, -1, -1, &error);
320 _DBUS_ASSERT_ERROR_IS_SET (&error);
322 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
323 filename, error.message);
325 dbus_error_free (&error);
327 _dbus_string_free (&config_file);
332 _dbus_string_free (&config_file);