7de1d8112f82591f634f405e83732ef2354ebaab
[platform/upstream/dbus.git] / bus / test.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* test.c  unit test routines
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include <config.h>
25
26 #ifdef DBUS_BUILD_TESTS
27 #include "test.h"
28 #include "loop.h"
29 #include <dbus/dbus-internals.h>
30 #include <dbus/dbus-list.h>
31
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
35  */
36 static DBusList *clients = NULL;
37
38 static dbus_bool_t
39 client_watch_callback (DBusWatch     *watch,
40                            unsigned int   condition,
41                            void          *data)
42 {
43   DBusConnection *connection = data;
44   dbus_bool_t retval;
45   
46   dbus_connection_ref (connection);
47   
48   retval = dbus_connection_handle_watch (connection, watch, condition);
49
50   dbus_connection_unref (connection);
51
52   return retval;
53 }
54
55 static dbus_bool_t
56 add_client_watch (DBusWatch      *watch,
57                       DBusConnection *connection)
58 {
59   return bus_loop_add_watch (watch, client_watch_callback, connection,
60                              NULL);
61 }
62
63 static void
64 remove_client_watch (DBusWatch      *watch,
65                          DBusConnection *connection)
66 {
67   bus_loop_remove_watch (watch, client_watch_callback, connection);
68 }
69
70 static void
71 client_timeout_callback (DBusTimeout   *timeout,
72                              void          *data)
73 {
74   DBusConnection *connection = data;
75
76   dbus_connection_ref (connection);
77
78   /* can return FALSE on OOM but we just let it fire again later */
79   dbus_timeout_handle (timeout);
80
81   dbus_connection_unref (connection);
82 }
83
84 static dbus_bool_t
85 add_client_timeout (DBusTimeout    *timeout,
86                         DBusConnection *connection)
87 {
88   return bus_loop_add_timeout (timeout, client_timeout_callback, connection, NULL);
89 }
90
91 static void
92 remove_client_timeout (DBusTimeout    *timeout,
93                            DBusConnection *connection)
94 {
95   bus_loop_remove_timeout (timeout, client_timeout_callback, connection);
96 }
97
98 static DBusHandlerResult
99 client_disconnect_handler (DBusMessageHandler *handler,
100                            DBusConnection     *connection,
101                            DBusMessage        *message,
102                            void               *user_data)
103 {
104   _dbus_verbose ("Removing client %p in disconnect handler\n",
105                  connection);
106   
107   _dbus_list_remove (&clients, connection);
108   
109   dbus_connection_unref (connection);
110   
111   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
112 }
113
114 static int handler_slot = -1;
115
116 dbus_bool_t
117 bus_setup_debug_client (DBusConnection *connection)
118 {
119   DBusMessageHandler *disconnect_handler;
120   const char *to_handle[] = { DBUS_MESSAGE_LOCAL_DISCONNECT };
121   dbus_bool_t retval;
122
123   if (handler_slot < 0)
124     handler_slot = dbus_connection_allocate_data_slot ();
125   if (handler_slot < 0)
126     return FALSE;
127   
128   disconnect_handler = dbus_message_handler_new (client_disconnect_handler,
129                                                  NULL, NULL);
130
131   if (disconnect_handler == NULL)
132     return FALSE;
133
134   if (!dbus_connection_register_handler (connection,
135                                          disconnect_handler,
136                                          to_handle,
137                                          _DBUS_N_ELEMENTS (to_handle)))
138     {
139       dbus_message_handler_unref (disconnect_handler);
140       return FALSE;
141     }
142
143   retval = FALSE;
144   
145   if (!dbus_connection_set_watch_functions (connection,
146                                             (DBusAddWatchFunction) add_client_watch,
147                                             (DBusRemoveWatchFunction) remove_client_watch,
148                                             NULL,
149                                             connection,
150                                             NULL))
151     goto out;
152       
153   if (!dbus_connection_set_timeout_functions (connection,
154                                               (DBusAddTimeoutFunction) add_client_timeout,
155                                               (DBusRemoveTimeoutFunction) remove_client_timeout,
156                                               NULL,
157                                               connection, NULL))
158     goto out;
159
160   if (!_dbus_list_append (&clients, connection))
161     goto out;
162
163   /* Set up handler to be destroyed */
164   if (!dbus_connection_set_data (connection, handler_slot,
165                                  disconnect_handler,
166                                  (DBusFreeFunction)
167                                  dbus_message_handler_unref))
168     goto out;
169   
170   retval = TRUE;
171   
172  out:
173   if (!retval)
174     {
175       dbus_message_handler_unref (disconnect_handler); /* unregisters it */
176       
177       dbus_connection_set_watch_functions (connection,
178                                            NULL, NULL, NULL, NULL, NULL);
179       dbus_connection_set_timeout_functions (connection,
180                                              NULL, NULL, NULL, NULL, NULL);
181
182       _dbus_list_remove_last (&clients, connection);
183     }
184       
185   return retval;
186 }
187
188 void
189 bus_test_clients_foreach (BusConnectionForeachFunction  function,
190                           void                         *data)
191 {
192   DBusList *link;
193   
194   link = _dbus_list_get_first_link (&clients);
195   while (link != NULL)
196     {
197       DBusConnection *connection = link->data;
198       DBusList *next = _dbus_list_get_next_link (&clients, link);
199
200       if (!(* function) (connection, data))
201         break;
202       
203       link = next;
204     }
205 }
206
207 dbus_bool_t
208 bus_test_client_listed (DBusConnection *connection)
209 {
210   DBusList *link;
211   
212   link = _dbus_list_get_first_link (&clients);
213   while (link != NULL)
214     {
215       DBusConnection *c = link->data;
216       DBusList *next = _dbus_list_get_next_link (&clients, link);
217
218       if (c == connection)
219         return TRUE;
220       
221       link = next;
222     }
223
224   return FALSE;
225 }
226
227 #endif