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
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-list.h>
31 /* The "debug client" watch/timeout handlers don't dispatch messages,
32 * as we manually pull them in order to verify them. This is why they
33 * are different from the real handlers in connection.c
35 static DBusList *clients = NULL;
36 static DBusLoop *client_loop = NULL;
39 client_watch_callback (DBusWatch *watch,
40 unsigned int condition,
43 /* FIXME this can be done in dbus-mainloop.c
44 * if the code in activation.c for the babysitter
45 * watch handler is fixed.
48 return dbus_watch_handle (watch, condition);
52 add_client_watch (DBusWatch *watch,
55 DBusConnection *connection = data;
57 return _dbus_loop_add_watch (client_loop,
58 watch, client_watch_callback, connection,
63 remove_client_watch (DBusWatch *watch,
66 DBusConnection *connection = data;
68 _dbus_loop_remove_watch (client_loop,
69 watch, client_watch_callback, connection);
73 client_timeout_callback (DBusTimeout *timeout,
76 DBusConnection *connection = data;
78 dbus_connection_ref (connection);
80 /* can return FALSE on OOM but we just let it fire again later */
81 dbus_timeout_handle (timeout);
83 dbus_connection_unref (connection);
87 add_client_timeout (DBusTimeout *timeout,
90 DBusConnection *connection = data;
92 return _dbus_loop_add_timeout (client_loop, timeout, client_timeout_callback, connection, NULL);
96 remove_client_timeout (DBusTimeout *timeout,
99 DBusConnection *connection = data;
101 _dbus_loop_remove_timeout (client_loop, timeout, client_timeout_callback, connection);
104 static DBusHandlerResult
105 client_disconnect_handler (DBusMessageHandler *handler,
106 DBusConnection *connection,
107 DBusMessage *message,
110 _dbus_verbose ("Removing client %p in disconnect handler\n",
113 _dbus_list_remove (&clients, connection);
115 dbus_connection_unref (connection);
119 _dbus_loop_unref (client_loop);
123 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
126 static int handler_slot = -1;
127 static int handler_slot_refcount = 0;
130 handler_slot_ref (void)
132 if (handler_slot < 0)
134 handler_slot = dbus_connection_allocate_data_slot ();
136 if (handler_slot < 0)
139 _dbus_assert (handler_slot_refcount == 0);
142 handler_slot_refcount += 1;
149 handler_slot_unref (void)
151 _dbus_assert (handler_slot_refcount > 0);
153 handler_slot_refcount -= 1;
155 if (handler_slot_refcount == 0)
157 dbus_connection_free_data_slot (handler_slot);
163 free_handler (void *data)
165 DBusMessageHandler *handler = data;
167 dbus_message_handler_unref (handler);
168 handler_slot_unref ();
172 bus_setup_debug_client (DBusConnection *connection)
174 DBusMessageHandler *disconnect_handler;
175 const char *to_handle[] = { DBUS_MESSAGE_LOCAL_DISCONNECT };
178 disconnect_handler = dbus_message_handler_new (client_disconnect_handler,
181 if (disconnect_handler == NULL)
184 if (!dbus_connection_register_handler (connection,
187 _DBUS_N_ELEMENTS (to_handle)))
189 dbus_message_handler_unref (disconnect_handler);
195 if (client_loop == NULL)
197 client_loop = _dbus_loop_new ();
198 if (client_loop == NULL)
202 if (!dbus_connection_set_watch_functions (connection,
210 if (!dbus_connection_set_timeout_functions (connection,
212 remove_client_timeout,
217 if (!_dbus_list_append (&clients, connection))
220 if (!handler_slot_ref ())
223 /* Set up handler to be destroyed */
224 if (!dbus_connection_set_data (connection, handler_slot,
228 handler_slot_unref ();
237 dbus_message_handler_unref (disconnect_handler); /* unregisters it */
239 dbus_connection_set_watch_functions (connection,
240 NULL, NULL, NULL, NULL, NULL);
241 dbus_connection_set_timeout_functions (connection,
242 NULL, NULL, NULL, NULL, NULL);
244 _dbus_list_remove_last (&clients, connection);
248 _dbus_loop_unref (client_loop);
257 bus_test_clients_foreach (BusConnectionForeachFunction function,
262 link = _dbus_list_get_first_link (&clients);
265 DBusConnection *connection = link->data;
266 DBusList *next = _dbus_list_get_next_link (&clients, link);
268 if (!(* function) (connection, data))
276 bus_test_client_listed (DBusConnection *connection)
280 link = _dbus_list_get_first_link (&clients);
283 DBusConnection *c = link->data;
284 DBusList *next = _dbus_list_get_next_link (&clients, link);
296 bus_test_run_clients_loop (dbus_bool_t block_once)
298 if (client_loop == NULL)
301 /* dispatch before we block so pending dispatches
302 * won't make our block return early
304 _dbus_loop_dispatch (client_loop);
306 /* Do one blocking wait, since we're expecting data */
307 _dbus_loop_iterate (client_loop, block_once);
309 /* Then mop everything up */
310 while (_dbus_loop_iterate (client_loop, FALSE))
315 bus_test_run_bus_loop (BusContext *context,
316 dbus_bool_t block_once)
318 /* dispatch before we block so pending dispatches
319 * won't make our block return early
321 _dbus_loop_dispatch (bus_context_get_loop (context));
323 /* Do one blocking wait, since we're expecting data */
324 _dbus_loop_iterate (bus_context_get_loop (context), block_once);
326 /* Then mop everything up */
327 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
332 bus_test_run_everything (BusContext *context)
334 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
335 (client_loop == NULL || _dbus_loop_iterate (client_loop, FALSE)))
340 bus_context_new_test (const DBusString *test_data_dir,
341 const char *filename)
344 DBusString config_file;
348 if (!_dbus_string_init (&config_file))
350 _dbus_warn ("No memory\n");
354 if (!_dbus_string_copy (test_data_dir, 0,
357 _dbus_warn ("No memory\n");
358 _dbus_string_free (&config_file);
362 _dbus_string_init_const (&relative, filename);
364 if (!_dbus_concat_dir_and_file (&config_file, &relative))
366 _dbus_warn ("No memory\n");
367 _dbus_string_free (&config_file);
371 dbus_error_init (&error);
372 context = bus_context_new (&config_file, -1, &error);
375 _DBUS_ASSERT_ERROR_IS_SET (&error);
377 _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n",
378 filename, error.message);
380 dbus_error_free (&error);
382 _dbus_string_free (&config_file);
387 _dbus_string_free (&config_file);