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 add_client_watch (DBusWatch *watch,
43 DBusConnection *connection = data;
45 return _dbus_loop_add_watch (client_loop, watch);
49 remove_client_watch (DBusWatch *watch,
52 DBusConnection *connection = data;
54 _dbus_loop_remove_watch (client_loop, watch);
58 toggle_client_watch (DBusWatch *watch,
61 _dbus_loop_toggle_watch (client_loop, watch);
65 add_client_timeout (DBusTimeout *timeout,
68 DBusConnection *connection = data;
70 return _dbus_loop_add_timeout (client_loop, timeout);
74 remove_client_timeout (DBusTimeout *timeout,
77 DBusConnection *connection = data;
79 _dbus_loop_remove_timeout (client_loop, timeout);
82 static DBusHandlerResult
83 client_disconnect_filter (DBusConnection *connection,
87 if (!dbus_message_is_signal (message,
90 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
92 _dbus_verbose ("Removing client %p in disconnect handler\n",
95 _dbus_list_remove (&clients, connection);
97 dbus_connection_unref (connection);
101 _dbus_loop_unref (client_loop);
105 return DBUS_HANDLER_RESULT_HANDLED;
109 bus_setup_debug_client (DBusConnection *connection)
113 if (!dbus_connection_add_filter (connection,
114 client_disconnect_filter,
120 if (client_loop == NULL)
122 client_loop = _dbus_loop_new ();
123 if (client_loop == NULL)
127 if (!dbus_connection_set_watch_functions (connection,
135 if (!dbus_connection_set_timeout_functions (connection,
137 remove_client_timeout,
142 if (!_dbus_list_append (&clients, connection))
150 dbus_connection_remove_filter (connection,
151 client_disconnect_filter,
154 dbus_connection_set_watch_functions (connection,
155 NULL, NULL, NULL, NULL, NULL);
156 dbus_connection_set_timeout_functions (connection,
157 NULL, NULL, NULL, NULL, NULL);
159 _dbus_list_remove_last (&clients, connection);
163 _dbus_loop_unref (client_loop);
172 bus_test_clients_foreach (BusConnectionForeachFunction function,
177 link = _dbus_list_get_first_link (&clients);
180 DBusConnection *connection = link->data;
181 DBusList *next = _dbus_list_get_next_link (&clients, link);
183 if (!(* function) (connection, data))
191 bus_test_client_listed (DBusConnection *connection)
195 link = _dbus_list_get_first_link (&clients);
198 DBusConnection *c = link->data;
199 DBusList *next = _dbus_list_get_next_link (&clients, link);
211 bus_test_run_clients_loop (dbus_bool_t block_once)
213 if (client_loop == NULL)
216 _dbus_verbose ("---> Dispatching on \"client side\"\n");
218 /* dispatch before we block so pending dispatches
219 * won't make our block return early
221 _dbus_loop_dispatch (client_loop);
223 /* Do one blocking wait, since we're expecting data */
226 _dbus_verbose ("---> blocking on \"client side\"\n");
227 _dbus_loop_iterate (client_loop, TRUE);
230 /* Then mop everything up */
231 while (_dbus_loop_iterate (client_loop, FALSE))
234 _dbus_verbose ("---> Done dispatching on \"client side\"\n");
238 bus_test_run_bus_loop (BusContext *context,
239 dbus_bool_t block_once)
241 _dbus_verbose ("---> Dispatching on \"server side\"\n");
243 /* dispatch before we block so pending dispatches
244 * won't make our block return early
246 _dbus_loop_dispatch (bus_context_get_loop (context));
248 /* Do one blocking wait, since we're expecting data */
251 _dbus_verbose ("---> blocking on \"server side\"\n");
252 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
255 /* Then mop everything up */
256 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
259 _dbus_verbose ("---> Done dispatching on \"server side\"\n");
263 bus_test_run_everything (BusContext *context)
265 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
266 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
271 bus_context_new_test (const DBusString *test_data_dir,
272 const char *filename)
275 DBusString config_file;
279 if (!_dbus_string_init (&config_file))
281 _dbus_warn ("No memory\n");
285 if (!_dbus_string_copy (test_data_dir, 0,
288 _dbus_warn ("No memory\n");
289 _dbus_string_free (&config_file);
293 _dbus_string_init_const (&relative, filename);
295 if (!_dbus_concat_dir_and_file (&config_file, &relative))
297 _dbus_warn ("No memory\n");
298 _dbus_string_free (&config_file);
302 dbus_error_init (&error);
303 context = bus_context_new (&config_file, FALSE, NULL, NULL, NULL, FALSE, &error);
306 _DBUS_ASSERT_ERROR_IS_SET (&error);
308 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
309 filename, error.message);
311 dbus_error_free (&error);
313 _dbus_string_free (&config_file);
318 _dbus_string_free (&config_file);