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
29 #include <dbus/dbus-internals.h>
30 #include <dbus/dbus-list.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 BusLoop *client_loop = NULL;
40 client_watch_callback (DBusWatch *watch,
41 unsigned int condition,
44 DBusConnection *connection = data;
47 dbus_connection_ref (connection);
49 retval = dbus_connection_handle_watch (connection, watch, condition);
51 dbus_connection_unref (connection);
57 add_client_watch (DBusWatch *watch,
60 DBusConnection *connection = data;
62 return bus_loop_add_watch (client_loop,
63 watch, client_watch_callback, connection,
68 remove_client_watch (DBusWatch *watch,
71 DBusConnection *connection = data;
73 bus_loop_remove_watch (client_loop,
74 watch, client_watch_callback, connection);
78 client_timeout_callback (DBusTimeout *timeout,
81 DBusConnection *connection = data;
83 dbus_connection_ref (connection);
85 /* can return FALSE on OOM but we just let it fire again later */
86 dbus_timeout_handle (timeout);
88 dbus_connection_unref (connection);
92 add_client_timeout (DBusTimeout *timeout,
95 DBusConnection *connection = data;
97 return bus_loop_add_timeout (client_loop, timeout, client_timeout_callback, connection, NULL);
101 remove_client_timeout (DBusTimeout *timeout,
104 DBusConnection *connection = data;
106 bus_loop_remove_timeout (client_loop, timeout, client_timeout_callback, connection);
109 static DBusHandlerResult
110 client_disconnect_handler (DBusMessageHandler *handler,
111 DBusConnection *connection,
112 DBusMessage *message,
115 _dbus_verbose ("Removing client %p in disconnect handler\n",
118 _dbus_list_remove (&clients, connection);
120 dbus_connection_unref (connection);
124 bus_loop_unref (client_loop);
128 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
131 static int handler_slot = -1;
132 static int handler_slot_refcount = 0;
135 handler_slot_ref (void)
137 if (handler_slot < 0)
139 handler_slot = dbus_connection_allocate_data_slot ();
141 if (handler_slot < 0)
144 _dbus_assert (handler_slot_refcount == 0);
147 handler_slot_refcount += 1;
154 handler_slot_unref (void)
156 _dbus_assert (handler_slot_refcount > 0);
158 handler_slot_refcount -= 1;
160 if (handler_slot_refcount == 0)
162 dbus_connection_free_data_slot (handler_slot);
168 free_handler (void *data)
170 DBusMessageHandler *handler = data;
172 dbus_message_handler_unref (handler);
173 handler_slot_unref ();
177 bus_setup_debug_client (DBusConnection *connection)
179 DBusMessageHandler *disconnect_handler;
180 const char *to_handle[] = { DBUS_MESSAGE_LOCAL_DISCONNECT };
183 disconnect_handler = dbus_message_handler_new (client_disconnect_handler,
186 if (disconnect_handler == NULL)
189 if (!dbus_connection_register_handler (connection,
192 _DBUS_N_ELEMENTS (to_handle)))
194 dbus_message_handler_unref (disconnect_handler);
200 if (client_loop == NULL)
202 client_loop = bus_loop_new ();
203 if (client_loop == NULL)
207 if (!dbus_connection_set_watch_functions (connection,
215 if (!dbus_connection_set_timeout_functions (connection,
217 remove_client_timeout,
222 if (!_dbus_list_append (&clients, connection))
225 if (!handler_slot_ref ())
228 /* Set up handler to be destroyed */
229 if (!dbus_connection_set_data (connection, handler_slot,
233 handler_slot_unref ();
242 dbus_message_handler_unref (disconnect_handler); /* unregisters it */
244 dbus_connection_set_watch_functions (connection,
245 NULL, NULL, NULL, NULL, NULL);
246 dbus_connection_set_timeout_functions (connection,
247 NULL, NULL, NULL, NULL, NULL);
249 _dbus_list_remove_last (&clients, connection);
253 bus_loop_unref (client_loop);
262 bus_test_clients_foreach (BusConnectionForeachFunction function,
267 link = _dbus_list_get_first_link (&clients);
270 DBusConnection *connection = link->data;
271 DBusList *next = _dbus_list_get_next_link (&clients, link);
273 if (!(* function) (connection, data))
281 bus_test_client_listed (DBusConnection *connection)
285 link = _dbus_list_get_first_link (&clients);
288 DBusConnection *c = link->data;
289 DBusList *next = _dbus_list_get_next_link (&clients, link);
301 bus_test_run_clients_loop (void)
303 if (client_loop == NULL)
306 /* Do one blocking wait, since we're expecting data */
307 bus_loop_iterate (client_loop, TRUE);
309 /* Then mop everything up */
310 while (bus_loop_iterate (client_loop, FALSE))
315 bus_test_run_bus_loop (BusContext *context)
317 /* Do one blocking wait, since we're expecting data */
318 bus_loop_iterate (bus_context_get_loop (context), TRUE);
320 /* Then mop everything up */
321 while (bus_loop_iterate (bus_context_get_loop (context), FALSE))
326 bus_test_run_everything (BusContext *context)
333 while (bus_loop_iterate (bus_context_get_loop (context), FALSE) ||
334 (client_loop == NULL || bus_loop_iterate (client_loop, FALSE)))
341 bus_context_new_test (const DBusString *test_data_dir,
342 const char *filename)
345 DBusString config_file;
349 if (!_dbus_string_init (&config_file))
351 _dbus_warn ("No memory\n");
355 if (!_dbus_string_copy (test_data_dir, 0,
358 _dbus_warn ("No memory\n");
359 _dbus_string_free (&config_file);
363 _dbus_string_init_const (&relative, filename);
365 if (!_dbus_concat_dir_and_file (&config_file, &relative))
367 _dbus_warn ("No memory\n");
368 _dbus_string_free (&config_file);
372 dbus_error_init (&error);
373 context = bus_context_new (&config_file, -1, &error);
376 _DBUS_ASSERT_ERROR_IS_SET (&error);
378 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
379 filename, error.message);
381 dbus_error_free (&error);
383 _dbus_string_free (&config_file);
388 _dbus_string_free (&config_file);