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>
41 # include <sys/types.h>
44 #include "test-utils.h"
56 DBusConnection *left_conn;
58 DBusConnection *right_conn;
59 gboolean right_conn_echo;
62 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
64 _assert_no_error (const DBusError *e,
68 if (G_UNLIKELY (dbus_error_is_set (e)))
69 g_error ("%s:%d: expected success but got error: %s: %s",
70 file, line, e->name, e->message);
74 spawn_dbus_daemon (gchar *binary,
85 "--print-address=1", /* stdout */
89 g_spawn_async_with_pipes (NULL, /* working directory */
92 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
93 NULL, /* child_setup */
96 NULL, /* child's stdin = /dev/null */
98 NULL, /* child's stderr = our stderr */
100 g_assert_no_error (error);
102 address = g_string_new (NULL);
104 /* polling until the dbus-daemon writes out its address is a bit stupid,
105 * but at least it's simple, unlike dbus-launch... in principle we could
106 * use select() here, but life's too short */
113 bytes = read (address_fd, buf, sizeof (buf));
116 g_string_append_len (address, buf, bytes);
118 newline = strchr (address->str, '\n');
122 g_string_truncate (address, newline - address->str);
126 g_usleep (G_USEC_PER_SEC / 10);
129 return g_string_free (address, FALSE);
132 static DBusConnection *
133 connect_to_bus (Fixture *f,
134 const gchar *address)
136 DBusConnection *conn;
137 DBusError error = DBUS_ERROR_INIT;
140 conn = dbus_connection_open_private (address, &error);
141 assert_no_error (&error);
142 g_assert (conn != NULL);
144 ok = dbus_bus_register (conn, &error);
145 assert_no_error (&error);
147 g_assert (dbus_bus_get_unique_name (conn) != NULL);
149 test_connection_setup (f->ctx, conn);
153 static DBusHandlerResult
154 echo_filter (DBusConnection *connection,
155 DBusMessage *message,
160 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
161 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
163 reply = dbus_message_new_method_return (message);
168 if (!dbus_connection_send (connection, reply, NULL))
171 dbus_message_unref (reply);
173 return DBUS_HANDLER_RESULT_HANDLED;
179 const char *config_file;
184 gconstpointer context)
186 const Config *config = context;
191 f->ctx = test_main_context_get ();
193 dbus_error_init (&f->e);
195 if (config != NULL && config->config_file != NULL)
197 if (g_getenv ("DBUS_TEST_DATA") == NULL)
199 g_message ("SKIP: set DBUS_TEST_DATA to a directory containing %s",
200 config->config_file);
205 arg = g_strdup_printf (
206 "--config-file=%s/%s",
207 g_getenv ("DBUS_TEST_DATA"), config->config_file);
209 else if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
211 arg = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
212 g_getenv ("DBUS_TEST_SYSCONFDIR"));
214 else if (g_getenv ("DBUS_TEST_DATA") != NULL)
216 arg = g_strdup_printf (
217 "--config-file=%s/valid-config-files/session.conf",
218 g_getenv ("DBUS_TEST_DATA"));
222 arg = g_strdup ("--session");
225 dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
227 if (dbus_daemon == NULL)
228 dbus_daemon = g_strdup ("dbus-daemon");
230 address = spawn_dbus_daemon (dbus_daemon, arg, &f->daemon_pid);
232 g_free (dbus_daemon);
235 f->left_conn = connect_to_bus (f, address);
236 f->right_conn = connect_to_bus (f, address);
241 add_echo_filter (Fixture *f)
243 if (!dbus_connection_add_filter (f->right_conn, echo_filter, NULL, NULL))
246 f->right_conn_echo = TRUE;
250 pc_count (DBusPendingCall *pc,
253 guint *received_p = data;
259 test_echo (Fixture *f,
260 gconstpointer context)
262 const Config *config = context;
271 if (config != NULL && config->bug_ref != NULL)
272 g_test_bug (config->bug_ref);
278 count = MAX (config->min_messages, count);
282 g_test_timer_start ();
284 for (sent = 0; sent < count; sent++)
286 DBusMessage *m = dbus_message_new_method_call (
287 dbus_bus_get_unique_name (f->right_conn), "/",
288 "com.example", "Spam");
294 if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
295 DBUS_TIMEOUT_INFINITE) ||
299 if (dbus_pending_call_get_completed (pc))
300 pc_count (pc, &received);
301 else if (!dbus_pending_call_set_notify (pc, pc_count, &received,
305 dbus_pending_call_unref (pc);
306 dbus_message_unref (m);
309 while (received < count)
310 test_main_context_iterate (f->ctx, TRUE);
312 elapsed = g_test_timer_elapsed ();
314 g_test_maximized_result (count / elapsed, "%u messages / %f seconds",
319 pending_call_store_reply (DBusPendingCall *pc,
322 DBusMessage **message_p = data;
324 *message_p = dbus_pending_call_steal_reply (pc);
325 g_assert (*message_p != NULL);
329 test_creds (Fixture *f,
330 gconstpointer context)
332 const char *unique = dbus_bus_get_unique_name (f->left_conn);
333 DBusMessage *m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
334 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetConnectionCredentials");
336 DBusMessageIter args_iter;
337 DBusMessageIter arr_iter;
338 DBusMessageIter pair_iter;
339 DBusMessageIter var_iter;
349 if (!dbus_message_append_args (m,
350 DBUS_TYPE_STRING, &unique,
354 if (!dbus_connection_send_with_reply (f->left_conn, m, &pc,
355 DBUS_TIMEOUT_USE_DEFAULT) ||
359 dbus_message_unref (m);
362 if (dbus_pending_call_get_completed (pc))
363 pending_call_store_reply (pc, &m);
364 else if (!dbus_pending_call_set_notify (pc, pending_call_store_reply,
369 test_main_context_iterate (f->ctx, TRUE);
371 g_assert_cmpstr (dbus_message_get_signature (m), ==, "a{sv}");
373 dbus_message_iter_init (m, &args_iter);
374 g_assert_cmpuint (dbus_message_iter_get_arg_type (&args_iter), ==,
376 g_assert_cmpuint (dbus_message_iter_get_element_type (&args_iter), ==,
377 DBUS_TYPE_DICT_ENTRY);
378 dbus_message_iter_recurse (&args_iter, &arr_iter);
380 while (dbus_message_iter_get_arg_type (&arr_iter) != DBUS_TYPE_INVALID)
384 dbus_message_iter_recurse (&arr_iter, &pair_iter);
385 g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==,
387 dbus_message_iter_get_basic (&pair_iter, &name);
388 dbus_message_iter_next (&pair_iter);
389 g_assert_cmpuint (dbus_message_iter_get_arg_type (&pair_iter), ==,
391 dbus_message_iter_recurse (&pair_iter, &var_iter);
393 if (g_strcmp0 (name, "UnixUserID") == 0)
398 g_assert (!(seen & SEEN_UNIX_USER));
399 g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
401 dbus_message_iter_get_basic (&var_iter, &u32);
402 g_message ("%s of this process is %u", name, u32);
403 g_assert_cmpuint (u32, ==, geteuid ());
404 seen |= SEEN_UNIX_USER;
406 g_assert_not_reached ();
409 else if (g_strcmp0 (name, "ProcessID") == 0)
413 g_assert (!(seen & SEEN_PID));
414 g_assert_cmpuint (dbus_message_iter_get_arg_type (&var_iter), ==,
416 dbus_message_iter_get_basic (&var_iter, &u32);
417 g_message ("%s of this process is %u", name, u32);
419 g_assert_cmpuint (u32, ==, getpid ());
420 #elif defined(G_OS_WIN32)
421 g_assert_cmpuint (u32, ==, GetCurrentProcessId ());
423 g_assert_not_reached ();
428 dbus_message_iter_next (&arr_iter);
432 g_assert (seen & SEEN_UNIX_USER);
433 g_assert (seen & SEEN_PID);
437 /* FIXME: when implemented:
438 g_assert (seen & SEEN_WINDOWS_SID);
444 teardown (Fixture *f,
445 gconstpointer context G_GNUC_UNUSED)
447 dbus_error_free (&f->e);
448 g_clear_error (&f->ge);
450 if (f->left_conn != NULL)
452 dbus_connection_close (f->left_conn);
453 dbus_connection_unref (f->left_conn);
457 if (f->right_conn != NULL)
459 if (f->right_conn_echo)
461 dbus_connection_remove_filter (f->right_conn, echo_filter, NULL);
462 f->right_conn_echo = FALSE;
465 dbus_connection_close (f->right_conn);
466 dbus_connection_unref (f->right_conn);
467 f->right_conn = NULL;
470 if (f->daemon_pid != 0)
473 TerminateProcess (f->daemon_pid, 1);
475 kill (f->daemon_pid, SIGTERM);
478 g_spawn_close_pid (f->daemon_pid);
482 test_main_context_unref (f->ctx);
485 static Config limited_config = {
486 "34393", 10000, "valid-config-files/incoming-limit.conf"
493 g_test_init (&argc, &argv, NULL);
494 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
496 g_test_add ("/echo/session", Fixture, NULL, setup, test_echo, teardown);
497 g_test_add ("/echo/limited", Fixture, &limited_config,
498 setup, test_echo, teardown);
499 g_test_add ("/creds", Fixture, NULL, setup, test_creds, teardown);
501 return g_test_run ();