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>
52 DBusConnection *left_conn;
54 DBusConnection *right_conn;
55 gboolean right_conn_echo;
58 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
60 _assert_no_error (const DBusError *e,
64 if (G_UNLIKELY (dbus_error_is_set (e)))
65 g_error ("%s:%d: expected success but got error: %s: %s",
66 file, line, e->name, e->message);
70 spawn_dbus_daemon (gchar *binary,
81 "--print-address=1", /* stdout */
85 g_spawn_async_with_pipes (NULL, /* working directory */
88 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
89 NULL, /* child_setup */
92 NULL, /* child's stdin = /dev/null */
94 NULL, /* child's stderr = our stderr */
96 g_assert_no_error (error);
98 address = g_string_new (NULL);
100 /* polling until the dbus-daemon writes out its address is a bit stupid,
101 * but at least it's simple, unlike dbus-launch... in principle we could
102 * use select() here, but life's too short */
109 bytes = read (address_fd, buf, sizeof (buf));
112 g_string_append_len (address, buf, bytes);
114 newline = strchr (address->str, '\n');
118 g_string_truncate (address, newline - address->str);
122 g_usleep (G_USEC_PER_SEC / 10);
125 return g_string_free (address, FALSE);
128 static DBusConnection *
129 connect_to_bus (const gchar *address)
131 DBusConnection *conn;
132 DBusError error = DBUS_ERROR_INIT;
135 conn = dbus_connection_open_private (address, &error);
136 assert_no_error (&error);
137 g_assert (conn != NULL);
139 ok = dbus_bus_register (conn, &error);
140 assert_no_error (&error);
142 g_assert (dbus_bus_get_unique_name (conn) != NULL);
144 dbus_connection_setup_with_g_main (conn, NULL);
148 static DBusHandlerResult
149 echo_filter (DBusConnection *connection,
150 DBusMessage *message,
155 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
156 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
158 reply = dbus_message_new_method_return (message);
163 if (!dbus_connection_send (connection, reply, NULL))
166 dbus_message_unref (reply);
168 return DBUS_HANDLER_RESULT_HANDLED;
174 const char *config_file;
179 gconstpointer context)
181 const Config *config = context;
187 dbus_error_init (&f->e);
189 if (config != NULL && config->config_file != NULL)
191 if (g_getenv ("DBUS_TEST_DATA") == NULL)
193 g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
194 config->config_file);
199 arg = g_strdup_printf (
200 "--config-file=%s/%s",
201 g_getenv ("DBUS_TEST_DATA"), config->config_file);
203 else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
205 arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
206 g_getenv ("DBUS_TEST_SYSCONFDIR"));
208 else if (g_getenv ("DBUS_TEST_DATA") != NULL)
210 arg = g_strdup_printf (
211 "--config-file=%s/valid-config-files/session.conf",
212 g_getenv ("DBUS_TEST_DATA"));
216 arg = g_strdup ("--session");
219 dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
221 if (dbus_daemon == NULL)
222 dbus_daemon = g_strdup ("dbus-daemon");
224 address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid);
226 g_free (dbus_daemon);
229 f->left_conn = connect_to_bus (address);
230 f->right_conn = connect_to_bus (address);
235 add_echo_filter (Fixture *f)
237 if (!dbus_connection_add_filter (f->right_conn, echo_filter, NULL, NULL))
240 f->right_conn_echo = TRUE;
244 pc_count (DBusPendingCall *pc,
247 guint *received_p = data;
253 test_echo (Fixture *f,
254 gconstpointer context)
256 const Config *config = context;
265 if (config != NULL && config->bug_ref != NULL)
266 g_test_bug (config->bug_ref);
272 count = MAX (config->min_messages, count);
276 g_test_timer_start ();
278 for (sent = 0; sent < count; sent++)
280 DBusMessage *m = dbus_message_new_method_call (
281 dbus_bus_get_unique_name (f->right_conn), "/",
282 "com.example", "Spam");
288 if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
289 DBUS_TIMEOUT_INFINITE) ||
293 if (dbus_pending_call_get_completed (pc))
294 pc_count (pc, &received);
295 else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
299 dbus_pending_call_unref (pc);
300 dbus_message_unref (m);
303 while (received < count)
304 g_main_context_iteration (NULL, TRUE);
306 elapsed = g_test_timer_elapsed ();
308 g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
313 teardown (Fixture *f,
314 gconstpointer context G_GNUC_UNUSED)
316 dbus_error_free (&f->e);
317 g_clear_error (&f->ge);
319 if (f->left_conn != NULL)
321 dbus_connection_close (f->left_conn);
322 dbus_connection_unref (f->left_conn);
326 if (f->right_conn != NULL)
328 if (f->right_conn_echo)
330 dbus_connection_remove_filter (f->right_conn, echo_filter, NULL);
331 f->right_conn_echo = FALSE;
334 dbus_connection_close (f->right_conn);
335 dbus_connection_unref (f->right_conn);
336 f->right_conn = NULL;
339 if (f->daemon_pid != 0)
342 TerminateProcess (f->daemon_pid, 1);
344 kill (f->daemon_pid, SIGTERM);
347 g_spawn_close_pid (f->daemon_pid);
352 static Config limited_config = {
353 "34393", 10000, "valid-config-files/incoming-limit.conf"
360 g_test_init (&argc, &argv, NULL);
361 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
363 g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
364 g_test_add ("/echo/limited", Fixture, &limited_config,
365 setup, test_echo, teardown);
367 return g_test_run ();