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 add_client_timeout (DBusTimeout *timeout,
57 return _dbus_loop_add_timeout (client_loop, timeout);
61 remove_client_timeout (DBusTimeout *timeout,
64 _dbus_loop_remove_timeout (client_loop, timeout);
67 static DBusHandlerResult
68 client_disconnect_filter (DBusConnection *connection,
72 if (!dbus_message_is_signal (message,
75 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
77 _dbus_verbose ("Removing client %p in disconnect handler\n",
80 _dbus_list_remove (&clients, connection);
82 dbus_connection_unref (connection);
86 _dbus_loop_unref (client_loop);
90 return DBUS_HANDLER_RESULT_HANDLED;
94 bus_setup_debug_client (DBusConnection *connection)
98 if (!dbus_connection_add_filter (connection,
99 client_disconnect_filter,
105 if (client_loop == NULL)
107 client_loop = _dbus_loop_new ();
108 if (client_loop == NULL)
112 if (!dbus_connection_set_watch_functions (connection,
120 if (!dbus_connection_set_timeout_functions (connection,
122 remove_client_timeout,
127 if (!_dbus_list_append (&clients, connection))
135 dbus_connection_remove_filter (connection,
136 client_disconnect_filter,
139 dbus_connection_set_watch_functions (connection,
140 NULL, NULL, NULL, NULL, NULL);
141 dbus_connection_set_timeout_functions (connection,
142 NULL, NULL, NULL, NULL, NULL);
144 _dbus_list_remove_last (&clients, connection);
148 _dbus_loop_unref (client_loop);
157 bus_test_clients_foreach (BusConnectionForeachFunction function,
162 link = _dbus_list_get_first_link (&clients);
165 DBusConnection *connection = link->data;
166 DBusList *next = _dbus_list_get_next_link (&clients, link);
168 if (!(* function) (connection, data))
176 bus_test_client_listed (DBusConnection *connection)
180 link = _dbus_list_get_first_link (&clients);
183 DBusConnection *c = link->data;
184 DBusList *next = _dbus_list_get_next_link (&clients, link);
196 bus_test_run_clients_loop (dbus_bool_t block_once)
198 if (client_loop == NULL)
201 _dbus_verbose ("---> Dispatching on \"client side\"\n");
203 /* dispatch before we block so pending dispatches
204 * won't make our block return early
206 _dbus_loop_dispatch (client_loop);
208 /* Do one blocking wait, since we're expecting data */
211 _dbus_verbose ("---> blocking on \"client side\"\n");
212 _dbus_loop_iterate (client_loop, TRUE);
215 /* Then mop everything up */
216 while (_dbus_loop_iterate (client_loop, FALSE))
219 _dbus_verbose ("---> Done dispatching on \"client side\"\n");
223 bus_test_run_bus_loop (BusContext *context,
224 dbus_bool_t block_once)
226 _dbus_verbose ("---> Dispatching on \"server side\"\n");
228 /* dispatch before we block so pending dispatches
229 * won't make our block return early
231 _dbus_loop_dispatch (bus_context_get_loop (context));
233 /* Do one blocking wait, since we're expecting data */
236 _dbus_verbose ("---> blocking on \"server side\"\n");
237 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
240 /* Then mop everything up */
241 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
244 _dbus_verbose ("---> Done dispatching on \"server side\"\n");
248 bus_test_run_everything (BusContext *context)
250 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
251 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
256 bus_context_new_test (const DBusString *test_data_dir,
257 const char *filename)
260 DBusString config_file;
264 if (!_dbus_string_init (&config_file))
266 _dbus_warn ("No memory\n");
270 if (!_dbus_string_copy (test_data_dir, 0,
273 _dbus_warn ("No memory\n");
274 _dbus_string_free (&config_file);
278 _dbus_string_init_const (&relative, filename);
280 if (!_dbus_concat_dir_and_file (&config_file, &relative))
282 _dbus_warn ("No memory\n");
283 _dbus_string_free (&config_file);
287 dbus_error_init (&error);
288 context = bus_context_new (&config_file, FALSE, NULL, NULL, NULL, FALSE, &error);
291 _DBUS_ASSERT_ERROR_IS_SET (&error);
293 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
294 filename, error.message);
296 dbus_error_free (&error);
298 _dbus_string_free (&config_file);
303 _dbus_string_free (&config_file);