Fix compiler warnings when cross-compiling dbus-daemon test for mingw32
[platform/upstream/dbus.git] / test / dbus-daemon.c
1 /* Integration tests for the dbus-daemon
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2010-2011 Nokia Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <glib.h>
30
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33
34 #include <string.h>
35
36 #ifdef DBUS_WIN
37 # include <io.h>
38 # include <windows.h>
39 #else
40 # include <signal.h>
41 # include <unistd.h>
42 #endif
43
44 typedef struct {
45     DBusError e;
46     GError *ge;
47
48     GPid daemon_pid;
49
50     DBusConnection *left_conn;
51
52     DBusConnection *right_conn;
53     gboolean right_conn_echo;
54 } Fixture;
55
56 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
57 static void
58 _assert_no_error (const DBusError *e,
59     const char *file,
60     int line)
61 {
62   if (G_UNLIKELY (dbus_error_is_set (e)))
63     g_error ("%s:%d: expected success but got error: %s: %s",
64         file, line, e->name, e->message);
65 }
66
67 static gchar *
68 spawn_dbus_daemon (gchar *binary,
69     gchar *configuration,
70     GPid *daemon_pid)
71 {
72   GError *error = NULL;
73   GString *address;
74   gint address_fd;
75   gchar *argv[] = {
76       binary,
77       configuration,
78       "--nofork",
79       "--print-address=1", /* stdout */
80       NULL
81   };
82
83   g_spawn_async_with_pipes (NULL, /* working directory */
84       argv,
85       NULL, /* envp */
86       G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
87       NULL, /* child_setup */
88       NULL, /* user data */
89       daemon_pid,
90       NULL, /* child's stdin = /dev/null */
91       &address_fd,
92       NULL, /* child's stderr = our stderr */
93       &error);
94   g_assert_no_error (error);
95
96   address = g_string_new (NULL);
97
98   /* polling until the dbus-daemon writes out its address is a bit stupid,
99    * but at least it's simple, unlike dbus-launch... in principle we could
100    * use select() here, but life's too short */
101   while (1)
102     {
103       gssize bytes;
104       gchar buf[4096];
105       gchar *newline;
106
107       bytes = read (address_fd, buf, sizeof (buf));
108
109       if (bytes > 0)
110         g_string_append_len (address, buf, bytes);
111
112       newline = strchr (address->str, '\n');
113
114       if (newline != NULL)
115         {
116           g_string_truncate (address, newline - address->str);
117           break;
118         }
119
120       g_usleep (G_USEC_PER_SEC / 10);
121     }
122
123   return g_string_free (address, FALSE);
124 }
125
126 static DBusConnection *
127 connect_to_bus (const gchar *address)
128 {
129   DBusConnection *conn;
130   DBusError error = DBUS_ERROR_INIT;
131   dbus_bool_t ok;
132
133   conn = dbus_connection_open_private (address, &error);
134   assert_no_error (&error);
135   g_assert (conn != NULL);
136
137   ok = dbus_bus_register (conn, &error);
138   assert_no_error (&error);
139   g_assert (ok);
140   g_assert (dbus_bus_get_unique_name (conn) != NULL);
141
142   dbus_connection_setup_with_g_main (conn, NULL);
143   return conn;
144 }
145
146 static DBusHandlerResult
147 echo_filter (DBusConnection *connection,
148     DBusMessage *message,
149     void *user_data)
150 {
151   DBusMessage *reply;
152   DBusError error = DBUS_ERROR_INIT;
153   int *sleep_ms = user_data;
154
155   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
156     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
157
158   reply = dbus_message_new_method_return (message);
159
160   if (reply == NULL)
161     g_error ("OOM");
162
163   if (!dbus_connection_send (connection, reply, NULL))
164     g_error ("OOM");
165
166   dbus_message_unref (reply);
167
168   return DBUS_HANDLER_RESULT_HANDLED;
169 }
170
171 static void
172 setup (Fixture *f,
173     gconstpointer context G_GNUC_UNUSED)
174 {
175   gchar *dbus_daemon;
176   gchar *config;
177   gchar *address;
178
179   f->ge = NULL;
180   dbus_error_init (&f->e);
181
182   dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
183
184   if (dbus_daemon == NULL)
185     dbus_daemon = g_strdup ("dbus-daemon");
186
187   if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
188     {
189       config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
190           g_getenv ("DBUS_TEST_SYSCONFDIR"));
191     }
192   else if (g_getenv ("DBUS_TEST_DATA") != NULL)
193     {
194       config = g_strdup_printf (
195           "--config-file=%s/valid-config-files/session.conf",
196           g_getenv ("DBUS_TEST_DATA"));
197     }
198   else
199     {
200       config = g_strdup ("--session");
201     }
202
203   address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
204
205   g_free (dbus_daemon);
206   g_free (config);
207
208   f->left_conn = connect_to_bus (address);
209   f->right_conn = connect_to_bus (address);
210   g_free (address);
211 }
212
213 static void
214 add_echo_filter (Fixture *f)
215 {
216   if (!dbus_connection_add_filter (f->right_conn, echo_filter, NULL, NULL))
217     g_error ("OOM");
218
219   f->right_conn_echo = TRUE;
220 }
221
222 static void
223 pc_count (DBusPendingCall *pc,
224     void *data)
225 {
226   guint *received_p = data;
227
228   (*received_p)++;
229 }
230
231 static void
232 test_echo (Fixture *f,
233     gconstpointer context G_GNUC_UNUSED)
234 {
235   guint count = 2000;
236   guint sent;
237   guint received = 0;
238   double elapsed;
239
240   if (g_test_perf ())
241     count = 100000;
242
243   add_echo_filter (f);
244
245   g_test_timer_start ();
246
247   for (sent = 0; sent < count; sent++)
248     {
249       DBusMessage *m = dbus_message_new_method_call (
250           dbus_bus_get_unique_name (f->right_conn), "/",
251           "com.example", "Spam");
252       DBusPendingCall *pc;
253
254       if (m == NULL)
255         g_error ("OOM");
256
257       if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
258                                             DBUS_TIMEOUT_INFINITE) ||
259           pc == NULL)
260         g_error ("OOM");
261
262       if (dbus_pending_call_get_completed (pc))
263         pc_count (pc, &received);
264       else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
265             NULL))
266         g_error ("OOM");
267
268       dbus_pending_call_unref (pc);
269       dbus_message_unref (m);
270     }
271
272   while (received < count)
273     g_main_context_iteration (NULL, TRUE);
274
275   elapsed = g_test_timer_elapsed ();
276
277   g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
278       count, elapsed);
279 }
280
281 static void
282 teardown (Fixture *f,
283     gconstpointer context G_GNUC_UNUSED)
284 {
285   dbus_error_free (&f->e);
286   g_clear_error (&f->ge);
287
288   if (f->left_conn != NULL)
289     {
290       dbus_connection_close (f->left_conn);
291       dbus_connection_unref (f->left_conn);
292       f->left_conn = NULL;
293     }
294
295   if (f->right_conn != NULL)
296     {
297       if (f->right_conn_echo)
298         {
299           dbus_connection_remove_filter (f->right_conn, echo_filter, NULL);
300           f->right_conn_echo = FALSE;
301         }
302
303       dbus_connection_close (f->right_conn);
304       dbus_connection_unref (f->right_conn);
305       f->right_conn = NULL;
306     }
307
308 #ifdef DBUS_WIN
309   TerminateProcess (f->daemon_pid, 1);
310 #else
311   kill (f->daemon_pid, SIGTERM);
312 #endif
313
314   g_spawn_close_pid (f->daemon_pid);
315 }
316
317 int
318 main (int argc,
319     char **argv)
320 {
321   g_test_init (&argc, &argv, NULL);
322   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
323
324   g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
325
326   return g_test_run ();
327 }