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 DBusConnection *connection = data;
46 dbus_connection_ref (connection);
48 retval = dbus_connection_handle_watch (connection, watch, condition);
50 dbus_connection_unref (connection);
56 add_client_watch (DBusWatch *watch,
59 DBusConnection *connection = data;
61 return _dbus_loop_add_watch (client_loop,
62 watch, client_watch_callback, connection,
67 remove_client_watch (DBusWatch *watch,
70 DBusConnection *connection = data;
72 _dbus_loop_remove_watch (client_loop,
73 watch, client_watch_callback, connection);
77 client_timeout_callback (DBusTimeout *timeout,
80 DBusConnection *connection = data;
82 dbus_connection_ref (connection);
84 /* can return FALSE on OOM but we just let it fire again later */
85 dbus_timeout_handle (timeout);
87 dbus_connection_unref (connection);
91 add_client_timeout (DBusTimeout *timeout,
94 DBusConnection *connection = data;
96 return _dbus_loop_add_timeout (client_loop, timeout, client_timeout_callback, connection, NULL);
100 remove_client_timeout (DBusTimeout *timeout,
103 DBusConnection *connection = data;
105 _dbus_loop_remove_timeout (client_loop, timeout, client_timeout_callback, connection);
108 static DBusHandlerResult
109 client_disconnect_handler (DBusMessageHandler *handler,
110 DBusConnection *connection,
111 DBusMessage *message,
114 _dbus_verbose ("Removing client %p in disconnect handler\n",
117 _dbus_list_remove (&clients, connection);
119 dbus_connection_unref (connection);
123 _dbus_loop_unref (client_loop);
127 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
130 static int handler_slot = -1;
131 static int handler_slot_refcount = 0;
134 handler_slot_ref (void)
136 if (handler_slot < 0)
138 handler_slot = dbus_connection_allocate_data_slot ();
140 if (handler_slot < 0)
143 _dbus_assert (handler_slot_refcount == 0);
146 handler_slot_refcount += 1;
153 handler_slot_unref (void)
155 _dbus_assert (handler_slot_refcount > 0);
157 handler_slot_refcount -= 1;
159 if (handler_slot_refcount == 0)
161 dbus_connection_free_data_slot (handler_slot);
167 free_handler (void *data)
169 DBusMessageHandler *handler = data;
171 dbus_message_handler_unref (handler);
172 handler_slot_unref ();
176 bus_setup_debug_client (DBusConnection *connection)
178 DBusMessageHandler *disconnect_handler;
179 const char *to_handle[] = { DBUS_MESSAGE_LOCAL_DISCONNECT };
182 disconnect_handler = dbus_message_handler_new (client_disconnect_handler,
185 if (disconnect_handler == NULL)
188 if (!dbus_connection_register_handler (connection,
191 _DBUS_N_ELEMENTS (to_handle)))
193 dbus_message_handler_unref (disconnect_handler);
199 if (client_loop == NULL)
201 client_loop = _dbus_loop_new ();
202 if (client_loop == NULL)
206 if (!dbus_connection_set_watch_functions (connection,
214 if (!dbus_connection_set_timeout_functions (connection,
216 remove_client_timeout,
221 if (!_dbus_list_append (&clients, connection))
224 if (!handler_slot_ref ())
227 /* Set up handler to be destroyed */
228 if (!dbus_connection_set_data (connection, handler_slot,
232 handler_slot_unref ();
241 dbus_message_handler_unref (disconnect_handler); /* unregisters it */
243 dbus_connection_set_watch_functions (connection,
244 NULL, NULL, NULL, NULL, NULL);
245 dbus_connection_set_timeout_functions (connection,
246 NULL, NULL, NULL, NULL, NULL);
248 _dbus_list_remove_last (&clients, connection);
252 _dbus_loop_unref (client_loop);
261 bus_test_clients_foreach (BusConnectionForeachFunction function,
266 link = _dbus_list_get_first_link (&clients);
269 DBusConnection *connection = link->data;
270 DBusList *next = _dbus_list_get_next_link (&clients, link);
272 if (!(* function) (connection, data))
280 bus_test_client_listed (DBusConnection *connection)
284 link = _dbus_list_get_first_link (&clients);
287 DBusConnection *c = link->data;
288 DBusList *next = _dbus_list_get_next_link (&clients, link);
300 bus_test_run_clients_loop (void)
302 if (client_loop == NULL)
305 /* Do one blocking wait, since we're expecting data */
306 _dbus_loop_iterate (client_loop, TRUE);
308 /* Then mop everything up */
309 while (_dbus_loop_iterate (client_loop, FALSE))
314 bus_test_run_bus_loop (BusContext *context)
316 /* Do one blocking wait, since we're expecting data */
317 _dbus_loop_iterate (bus_context_get_loop (context), TRUE);
319 /* Then mop everything up */
320 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE))
325 bus_test_run_everything (BusContext *context)
332 while (_dbus_loop_iterate (bus_context_get_loop (context), FALSE) ||
333 (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);