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;
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,
57 DBusConnection *connection)
59 return bus_loop_add_watch (watch, client_watch_callback, connection,
64 remove_client_watch (DBusWatch *watch,
65 DBusConnection *connection)
67 bus_loop_remove_watch (watch, client_watch_callback, connection);
71 client_timeout_callback (DBusTimeout *timeout,
74 DBusConnection *connection = data;
76 dbus_connection_ref (connection);
78 /* can return FALSE on OOM but we just let it fire again later */
79 dbus_timeout_handle (timeout);
81 dbus_connection_unref (connection);
85 add_client_timeout (DBusTimeout *timeout,
86 DBusConnection *connection)
88 return bus_loop_add_timeout (timeout, client_timeout_callback, connection, NULL);
92 remove_client_timeout (DBusTimeout *timeout,
93 DBusConnection *connection)
95 bus_loop_remove_timeout (timeout, client_timeout_callback, connection);
98 static DBusHandlerResult
99 client_disconnect_handler (DBusMessageHandler *handler,
100 DBusConnection *connection,
101 DBusMessage *message,
104 _dbus_verbose ("Removing client %p in disconnect handler\n",
107 _dbus_list_remove (&clients, connection);
109 dbus_connection_unref (connection);
111 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
114 static int handler_slot = -1;
115 static int handler_slot_refcount = 0;
118 handler_slot_ref (void)
120 if (handler_slot < 0)
122 handler_slot = dbus_connection_allocate_data_slot ();
124 if (handler_slot < 0)
127 _dbus_assert (handler_slot_refcount == 0);
130 handler_slot_refcount += 1;
137 handler_slot_unref (void)
139 _dbus_assert (handler_slot_refcount > 0);
141 handler_slot_refcount -= 1;
143 if (handler_slot_refcount == 0)
145 dbus_connection_free_data_slot (handler_slot);
151 free_handler (void *data)
153 DBusMessageHandler *handler = data;
155 dbus_message_handler_unref (handler);
156 handler_slot_unref ();
160 bus_setup_debug_client (DBusConnection *connection)
162 DBusMessageHandler *disconnect_handler;
163 const char *to_handle[] = { DBUS_MESSAGE_LOCAL_DISCONNECT };
166 disconnect_handler = dbus_message_handler_new (client_disconnect_handler,
169 if (disconnect_handler == NULL)
172 if (!dbus_connection_register_handler (connection,
175 _DBUS_N_ELEMENTS (to_handle)))
177 dbus_message_handler_unref (disconnect_handler);
183 if (!dbus_connection_set_watch_functions (connection,
184 (DBusAddWatchFunction) add_client_watch,
185 (DBusRemoveWatchFunction) remove_client_watch,
191 if (!dbus_connection_set_timeout_functions (connection,
192 (DBusAddTimeoutFunction) add_client_timeout,
193 (DBusRemoveTimeoutFunction) remove_client_timeout,
198 if (!_dbus_list_append (&clients, connection))
201 if (!handler_slot_ref ())
204 /* Set up handler to be destroyed */
205 if (!dbus_connection_set_data (connection, handler_slot,
209 handler_slot_unref ();
218 dbus_message_handler_unref (disconnect_handler); /* unregisters it */
220 dbus_connection_set_watch_functions (connection,
221 NULL, NULL, NULL, NULL, NULL);
222 dbus_connection_set_timeout_functions (connection,
223 NULL, NULL, NULL, NULL, NULL);
225 _dbus_list_remove_last (&clients, connection);
232 bus_test_clients_foreach (BusConnectionForeachFunction function,
237 link = _dbus_list_get_first_link (&clients);
240 DBusConnection *connection = link->data;
241 DBusList *next = _dbus_list_get_next_link (&clients, link);
243 if (!(* function) (connection, data))
251 bus_test_client_listed (DBusConnection *connection)
255 link = _dbus_list_get_first_link (&clients);
258 DBusConnection *c = link->data;
259 DBusList *next = _dbus_list_get_next_link (&clients, link);