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 add_client_timeout (DBusTimeout *timeout,
61 DBusConnection *connection = data;
63 return _dbus_loop_add_timeout (client_loop, timeout);
67 remove_client_timeout (DBusTimeout *timeout,
70 DBusConnection *connection = data;
72 _dbus_loop_remove_timeout (client_loop, timeout);
75 static DBusHandlerResult
76 client_disconnect_filter (DBusConnection *connection,
80 if (!dbus_message_is_signal (message,
83 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
85 _dbus_verbose ("Removing client %p in disconnect handler\n",
88 _dbus_list_remove (&clients, connection);
90 dbus_connection_unref (connection);
94 _dbus_loop_unref (client_loop);
98 return DBUS_HANDLER_RESULT_HANDLED;
102 bus_setup_debug_client (DBusConnection *connection)
106 if (!dbus_connection_add_filter (connection,
107 client_disconnect_filter,
113 if (client_loop == NULL)
115 client_loop = _dbus_loop_new ();
116 if (client_loop == NULL)
120 if (!dbus_connection_set_watch_functions (connection,
128 if (!dbus_connection_set_timeout_functions (connection,
130 remove_client_timeout,
135 if (!_dbus_list_append (&clients, connection))
143 dbus_connection_remove_filter (connection,
144 client_disconnect_filter,
147 dbus_connection_set_watch_functions (connection,
148 NULL, NULL, NULL, NULL, NULL);
149 dbus_connection_set_timeout_functions (connection,
150 NULL, NULL, NULL, NULL, NULL);
152 _dbus_list_remove_last (&clients, connection);
156 _dbus_loop_unref (client_loop);
165 bus_test_clients_foreach (BusConnectionForeachFunction function,
170 link = _dbus_list_get_first_link (&clients);
173 DBusConnection *connection = link->data;
174 DBusList *next = _dbus_list_get_next_link (&clients, link);
176 if (!(* function) (connection, data))
184 bus_test_client_listed (DBusConnection *connection)
188 link = _dbus_list_get_first_link (&clients);
191 DBusConnection *c = link->data;
192 DBusList *next = _dbus_list_get_next_link (&clients, link);
204 bus_test_run_clients_loop (dbus_bool_t block_once)
206 if (client_loop == NULL)
209 _dbus_verbose ("---> Dispatching on \"client side\"\n");
211 /* dispatch before we block so pending dispatches
212 * won't make our block return early
214 _dbus_loop_dispatch (client_loop);
216 /* Do one blocking wait, since we're expecting data */
219 _dbus_verbose ("---> blocking on \"client side\"\n");
220 _dbus_loop_iterate (client_loop, TRUE);
223 /* Then mop everything up */
224 while (_dbus_loop_iterate (client_loop, FALSE))
227 _dbus_verbose ("---> Done dispatching on \"client side\"\n");
231 bus_test_run_bus_loop (BusContext *context,
232 dbus_bool_t block_once)
234 _dbus_verbose ("---> Dispatching on \"server side\"\n");
236 /* dispatch before we block so pending dispatches
237 * won't make our block return early
239 _dbus_loop_dispatch (bus_context_get_loop (context));
241 /* Do one blocking wait, since we're expecting data */
244 _dbus_verbose ("---> blocking on \"server side\"\n");
245 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
248 /* Then mop everything up */
249 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
252 _dbus_verbose ("---> Done dispatching on \"server side\"\n");
256 bus_test_run_everything (BusContext *context)
258 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
259 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
264 bus_context_new_test (const DBusString *test_data_dir,
265 const char *filename)
268 DBusString config_file;
272 if (!_dbus_string_init (&config_file))
274 _dbus_warn ("No memory\n");
278 if (!_dbus_string_copy (test_data_dir, 0,
281 _dbus_warn ("No memory\n");
282 _dbus_string_free (&config_file);
286 _dbus_string_init_const (&relative, filename);
288 if (!_dbus_concat_dir_and_file (&config_file, &relative))
290 _dbus_warn ("No memory\n");
291 _dbus_string_free (&config_file);
295 dbus_error_init (&error);
296 context = bus_context_new (&config_file, FALSE, NULL, NULL, NULL, FALSE, &error);
299 _DBUS_ASSERT_ERROR_IS_SET (&error);
301 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
302 filename, error.message);
304 dbus_error_free (&error);
306 _dbus_string_free (&config_file);
311 _dbus_string_free (&config_file);