dbus-daemon test: sanity-check the just-installed config file when using DESTDIR
[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 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 <windows.h>
38 #else
39 # include <signal.h>
40 # include <unistd.h>
41 #endif
42
43 typedef struct {
44     DBusError e;
45     GError *ge;
46
47     gint daemon_pid;
48
49     DBusConnection *left_conn;
50
51     DBusConnection *right_conn;
52     gboolean right_conn_echo;
53 } Fixture;
54
55 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
56 static void
57 _assert_no_error (const DBusError *e,
58     const char *file,
59     int line)
60 {
61   if (G_UNLIKELY (dbus_error_is_set (e)))
62     g_error ("%s:%d: expected success but got error: %s: %s",
63         file, line, e->name, e->message);
64 }
65
66 static gchar *
67 spawn_dbus_daemon (gchar *binary,
68     gchar *configuration,
69     gint *daemon_pid)
70 {
71   GError *error = NULL;
72   GString *address;
73   gint address_fd;
74   gchar *argv[] = {
75       binary,
76       configuration,
77       "--nofork",
78       "--print-address=1", /* stdout */
79       NULL
80   };
81
82   g_spawn_async_with_pipes (NULL, /* working directory */
83       argv,
84       NULL, /* envp */
85       G_SPAWN_DO_NOT_REAP_CHILD,
86       NULL, /* child_setup */
87       NULL, /* user data */
88       daemon_pid,
89       NULL, /* child's stdin = /dev/null */
90       &address_fd,
91       NULL, /* child's stderr = our stderr */
92       &error);
93   g_assert_no_error (error);
94
95   address = g_string_new (NULL);
96
97   /* polling until the dbus-daemon writes out its address is a bit stupid,
98    * but at least it's simple, unlike dbus-launch... in principle we could
99    * use select() here, but life's too short */
100   while (1)
101     {
102       gssize bytes;
103       gchar buf[4096];
104       gchar *newline;
105
106       bytes = read (address_fd, buf, sizeof (buf));
107
108       if (bytes > 0)
109         g_string_append_len (address, buf, bytes);
110
111       newline = strchr (address->str, '\n');
112
113       if (newline != NULL)
114         {
115           g_string_truncate (address, newline - address->str);
116           break;
117         }
118
119       g_usleep (G_USEC_PER_SEC / 10);
120     }
121
122   return g_string_free (address, FALSE);
123 }
124
125 static DBusConnection *
126 connect_to_bus (const gchar *address)
127 {
128   DBusConnection *conn;
129   DBusError error = DBUS_ERROR_INIT;
130   dbus_bool_t ok;
131
132   conn = dbus_connection_open_private (address, &error);
133   assert_no_error (&error);
134   g_assert (conn != NULL);
135
136   ok = dbus_bus_register (conn, &error);
137   assert_no_error (&error);
138   g_assert (ok);
139   g_assert (dbus_bus_get_unique_name (conn) != NULL);
140
141   dbus_connection_setup_with_g_main (conn, NULL);
142   return conn;
143 }
144
145 static DBusHandlerResult
146 echo_filter (DBusConnection *connection,
147     DBusMessage *message,
148     void *user_data)
149 {
150   DBusMessage *reply;
151   DBusError error = DBUS_ERROR_INIT;
152   int *sleep_ms = user_data;
153
154   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
155     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
156
157   reply = dbus_message_new_method_return (message);
158
159   if (reply == NULL)
160     g_error ("OOM");
161
162   if (!dbus_connection_send (connection, reply, NULL))
163     g_error ("OOM");
164
165   dbus_message_unref (reply);
166
167   return DBUS_HANDLER_RESULT_HANDLED;
168 }
169
170 static void
171 setup (Fixture *f,
172     gconstpointer context G_GNUC_UNUSED)
173 {
174   gchar *dbus_daemon;
175   gchar *config;
176   gchar *address;
177
178   f->ge = NULL;
179   dbus_error_init (&f->e);
180
181   g_assert (g_getenv ("DBUS_TEST_DAEMON") != NULL);
182
183   dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
184
185   if (g_getenv ("DBUS_TEST_USE_INSTALLED") != NULL)
186     {
187       /* we strdup this because it might be clobbered by a subsequent
188        * g_getenv */
189       gchar *destdir = g_strdup (g_getenv ("DESTDIR"));
190
191       if (destdir != NULL && *destdir != '\0')
192         config = g_strdup_printf ("--config-file=%s%s/dbus-1/session.conf",
193             destdir, g_getenv ("DBUS_TEST_SYSCONFDIR"));
194       else
195         config = g_strdup ("--session");
196
197       g_free (destdir);
198     }
199   else
200     {
201       g_assert (g_getenv ("DBUS_TEST_DATA") != NULL);
202       config = g_strdup_printf (
203           "--config-file=%s/valid-config-files/session.conf",
204           g_getenv ("DBUS_TEST_DATA"));
205     }
206
207   address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
208
209   g_free (dbus_daemon);
210   g_free (config);
211
212   f->left_conn = connect_to_bus (address);
213   f->right_conn = connect_to_bus (address);
214   g_free (address);
215 }
216
217 static void
218 add_echo_filter (Fixture *f)
219 {
220   if (!dbus_connection_add_filter (f->right_conn, echo_filter, NULL, NULL))
221     g_error ("OOM");
222
223   f->right_conn_echo = TRUE;
224 }
225
226 static void
227 pc_count (DBusPendingCall *pc,
228     void *data)
229 {
230   guint *received_p = data;
231
232   (*received_p)++;
233 }
234
235 static void
236 test_echo (Fixture *f,
237     gconstpointer context G_GNUC_UNUSED)
238 {
239   guint count = 2000;
240   guint sent;
241   guint received = 0;
242   double elapsed;
243
244   if (g_test_perf ())
245     count = 100000;
246
247   add_echo_filter (f);
248
249   g_test_timer_start ();
250
251   for (sent = 0; sent < count; sent++)
252     {
253       DBusMessage *m = dbus_message_new_method_call (
254           dbus_bus_get_unique_name (f->right_conn), "/",
255           "com.example", "Spam");
256       DBusPendingCall *pc;
257
258       if (m == NULL)
259         g_error ("OOM");
260
261       if (!dbus_connection_send_with_reply (f->left_conn, m, &pc, 0x7FFFFFFF)
262           || pc == NULL)
263         g_error ("OOM");
264
265       if (dbus_pending_call_get_completed (pc))
266         pc_count (pc, &received);
267       else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
268             NULL))
269         g_error ("OOM");
270
271       dbus_pending_call_unref (pc);
272       dbus_message_unref (m);
273     }
274
275   while (received < count)
276     g_main_context_iteration (NULL, TRUE);
277
278   elapsed = g_test_timer_elapsed ();
279
280   g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
281       count, elapsed);
282 }
283
284 static void
285 teardown (Fixture *f,
286     gconstpointer context G_GNUC_UNUSED)
287 {
288   dbus_error_free (&f->e);
289   g_clear_error (&f->ge);
290
291   if (f->left_conn != NULL)
292     {
293       dbus_connection_close (f->left_conn);
294       dbus_connection_unref (f->left_conn);
295       f->left_conn = NULL;
296     }
297
298   if (f->right_conn != NULL)
299     {
300       if (f->right_conn_echo)
301         {
302           dbus_connection_remove_filter (f->right_conn, echo_filter, NULL);
303           f->right_conn_echo = FALSE;
304         }
305
306       dbus_connection_close (f->right_conn);
307       dbus_connection_unref (f->right_conn);
308       f->right_conn = NULL;
309     }
310
311 #ifdef DBUS_WIN
312   TerminateProcess (f->daemon_pid, 1);
313 #else
314   kill (f->daemon_pid, SIGTERM);
315 #endif
316
317   g_spawn_close_pid (f->daemon_pid);
318 }
319
320 int
321 main (int argc,
322     char **argv)
323 {
324   g_test_init (&argc, &argv, NULL);
325   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
326
327   g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
328
329   return g_test_run ();
330 }