1 /* Integration tests for the dbus-daemon
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2010-2011 Nokia Corporation
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
51 DBusConnection *left_conn;
53 DBusConnection *right_conn;
54 gboolean right_conn_echo;
57 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
59 _assert_no_error (const DBusError *e,
63 if (G_UNLIKELY (dbus_error_is_set (e)))
64 g_error ("%s:%d: expected success but got error: %s: %s",
65 file, line, e->name, e->message);
69 spawn_dbus_daemon (gchar *binary,
80 "--print-address=1", /* stdout */
84 g_spawn_async_with_pipes (NULL, /* working directory */
87 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
88 NULL, /* child_setup */
91 NULL, /* child's stdin = /dev/null */
93 NULL, /* child's stderr = our stderr */
95 g_assert_no_error (error);
97 address = g_string_new (NULL);
99 /* polling until the dbus-daemon writes out its address is a bit stupid,
100 * but at least it's simple, unlike dbus-launch... in principle we could
101 * use select() here, but life's too short */
108 bytes = read (address_fd, buf, sizeof (buf));
111 g_string_append_len (address, buf, bytes);
113 newline = strchr (address->str, '\n');
117 g_string_truncate (address, newline - address->str);
121 g_usleep (G_USEC_PER_SEC / 10);
124 return g_string_free (address, FALSE);
127 static DBusConnection *
128 connect_to_bus (const gchar *address)
130 DBusConnection *conn;
131 DBusError error = DBUS_ERROR_INIT;
134 conn = dbus_connection_open_private (address, &error);
135 assert_no_error (&error);
136 g_assert (conn != NULL);
138 ok = dbus_bus_register (conn, &error);
139 assert_no_error (&error);
141 g_assert (dbus_bus_get_unique_name (conn) != NULL);
143 dbus_connection_setup_with_g_main (conn, NULL);
147 static DBusHandlerResult
148 echo_filter (DBusConnection *connection,
149 DBusMessage *message,
153 DBusError error = DBUS_ERROR_INIT;
154 int *sleep_ms = user_data;
156 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
157 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
159 reply = dbus_message_new_method_return (message);
164 if (!dbus_connection_send (connection, reply, NULL))
167 dbus_message_unref (reply);
169 return DBUS_HANDLER_RESULT_HANDLED;
175 const char *config_file;
180 gconstpointer context)
182 const Config *config = context;
188 dbus_error_init (&f->e);
190 if (config != NULL && config->config_file != NULL)
192 if (g_getenv ("DBUS_TEST_DATA") == NULL)
194 g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
195 config->config_file);
200 arg = g_strdup_printf (
201 "--config-file=%s/%s",
202 g_getenv ("DBUS_TEST_DATA"), config->config_file);
204 else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
206 arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
207 g_getenv ("DBUS_TEST_SYSCONFDIR"));
209 else if (g_getenv ("DBUS_TEST_DATA") != NULL)
211 arg = g_strdup_printf (
212 "--config-file=%s/valid-config-files/session.conf",
213 g_getenv ("DBUS_TEST_DATA"));
217 arg = g_strdup ("--session");
220 dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
222 if (dbus_daemon == NULL)
223 dbus_daemon = g_strdup ("dbus-daemon");
225 address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid);
227 g_free (dbus_daemon);
230 f->left_conn = connect_to_bus (address);
231 f->right_conn = connect_to_bus (address);
236 add_echo_filter (Fixture *f)
238 if (!dbus_connection_add_filter (f->right_conn, echo_filter, NULL, NULL))
241 f->right_conn_echo = TRUE;
245 pc_count (DBusPendingCall *pc,
248 guint *received_p = data;
254 test_echo (Fixture *f,
255 gconstpointer context)
257 const Config *config = context;
266 if (config != NULL && config->bug_ref != NULL)
267 g_test_bug (config->bug_ref);
273 count = MAX (config->min_messages, count);
277 g_test_timer_start ();
279 for (sent = 0; sent < count; sent++)
281 DBusMessage *m = dbus_message_new_method_call (
282 dbus_bus_get_unique_name (f->right_conn), "/",
283 "com.example", "Spam");
289 if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
290 DBUS_TIMEOUT_INFINITE) ||
294 if (dbus_pending_call_get_completed (pc))
295 pc_count (pc, &received);
296 else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
300 dbus_pending_call_unref (pc);
301 dbus_message_unref (m);
304 while (received < count)
305 g_main_context_iteration (NULL, TRUE);
307 elapsed = g_test_timer_elapsed ();
309 g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
314 teardown (Fixture *f,
315 gconstpointer context G_GNUC_UNUSED)
317 dbus_error_free (&f->e);
318 g_clear_error (&f->ge);
320 if (f->left_conn != NULL)
322 dbus_connection_close (f->left_conn);
323 dbus_connection_unref (f->left_conn);
327 if (f->right_conn != NULL)
329 if (f->right_conn_echo)
331 dbus_connection_remove_filter (f->right_conn, echo_filter, NULL);
332 f->right_conn_echo = FALSE;
335 dbus_connection_close (f->right_conn);
336 dbus_connection_unref (f->right_conn);
337 f->right_conn = NULL;
340 if (f->daemon_pid != 0)
343 TerminateProcess (f->daemon_pid, 1);
345 kill (f->daemon_pid, SIGTERM);
348 g_spawn_close_pid (f->daemon_pid);
353 static Config limited_config = {
354 "34393", 10000, "valid-config-files/incoming-limit.conf"
361 g_test_init (&argc, &argv, NULL);
362 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
364 g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
365 g_test_add ("/echo/limited", Fixture, &limited_config,
366 setup, test_echo, teardown);
368 return g_test_run ();