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 return _dbus_loop_add_watch (client_loop, watch);
47 remove_client_watch (DBusWatch *watch,
50 _dbus_loop_remove_watch (client_loop, watch);
54 toggle_client_watch (DBusWatch *watch,
57 _dbus_loop_toggle_watch (client_loop, watch);
61 add_client_timeout (DBusTimeout *timeout,
64 return _dbus_loop_add_timeout (client_loop, timeout);
68 remove_client_timeout (DBusTimeout *timeout,
71 _dbus_loop_remove_timeout (client_loop, timeout);
74 static DBusHandlerResult
75 client_disconnect_filter (DBusConnection *connection,
79 if (!dbus_message_is_signal (message,
82 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
84 _dbus_verbose ("Removing client %p in disconnect handler\n",
87 _dbus_list_remove (&clients, connection);
89 dbus_connection_unref (connection);
93 _dbus_loop_unref (client_loop);
97 return DBUS_HANDLER_RESULT_HANDLED;
101 bus_setup_debug_client (DBusConnection *connection)
105 if (!dbus_connection_add_filter (connection,
106 client_disconnect_filter,
112 if (client_loop == NULL)
114 client_loop = _dbus_loop_new ();
115 if (client_loop == NULL)
119 if (!dbus_connection_set_watch_functions (connection,
127 if (!dbus_connection_set_timeout_functions (connection,
129 remove_client_timeout,
134 if (!_dbus_list_append (&clients, connection))
142 dbus_connection_remove_filter (connection,
143 client_disconnect_filter,
146 dbus_connection_set_watch_functions (connection,
147 NULL, NULL, NULL, NULL, NULL);
148 dbus_connection_set_timeout_functions (connection,
149 NULL, NULL, NULL, NULL, NULL);
151 _dbus_list_remove_last (&clients, connection);
155 _dbus_loop_unref (client_loop);
164 bus_test_clients_foreach (BusConnectionForeachFunction function,
169 link = _dbus_list_get_first_link (&clients);
172 DBusConnection *connection = link->data;
173 DBusList *next = _dbus_list_get_next_link (&clients, link);
175 if (!(* function) (connection, data))
183 bus_test_client_listed (DBusConnection *connection)
187 link = _dbus_list_get_first_link (&clients);
190 DBusConnection *c = link->data;
191 DBusList *next = _dbus_list_get_next_link (&clients, link);
203 bus_test_run_clients_loop (dbus_bool_t block_once)
205 if (client_loop == NULL)
208 _dbus_verbose ("---> Dispatching on \"client side\"\n");
210 /* dispatch before we block so pending dispatches
211 * won't make our block return early
213 _dbus_loop_dispatch (client_loop);
215 /* Do one blocking wait, since we're expecting data */
218 _dbus_verbose ("---> blocking on \"client side\"\n");
219 _dbus_loop_iterate (client_loop, TRUE);
222 /* Then mop everything up */
223 while (_dbus_loop_iterate (client_loop, FALSE))
226 _dbus_verbose ("---> Done dispatching on \"client side\"\n");
230 bus_test_run_bus_loop (BusContext *context,
231 dbus_bool_t block_once)
233 _dbus_verbose ("---> Dispatching on \"server side\"\n");
235 /* dispatch before we block so pending dispatches
236 * won't make our block return early
238 _dbus_loop_dispatch (bus_context_get_loop (context));
240 /* Do one blocking wait, since we're expecting data */
243 _dbus_verbose ("---> blocking on \"server side\"\n");
244 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
247 /* Then mop everything up */
248 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
251 _dbus_verbose ("---> Done dispatching on \"server side\"\n");
255 bus_test_run_everything (BusContext *context)
257 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
258 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
263 bus_context_new_test (const DBusString *test_data_dir,
264 const char *filename)
267 DBusString config_file;
271 if (!_dbus_string_init (&config_file))
273 _dbus_warn ("No memory\n");
277 if (!_dbus_string_copy (test_data_dir, 0,
280 _dbus_warn ("No memory\n");
281 _dbus_string_free (&config_file);
285 _dbus_string_init_const (&relative, filename);
287 if (!_dbus_concat_dir_and_file (&config_file, &relative))
289 _dbus_warn ("No memory\n");
290 _dbus_string_free (&config_file);
294 dbus_error_init (&error);
295 context = bus_context_new (&config_file, BUS_CONTEXT_FLAG_NONE, NULL, NULL, NULL, &error);
298 _DBUS_ASSERT_ERROR_IS_SET (&error);
300 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
301 filename, error.message);
303 dbus_error_free (&error);
305 _dbus_string_free (&config_file);
310 _dbus_string_free (&config_file);