1 /* Simple sanity-check for loopback through TCP and Unix sockets.
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2010-2012 Nokia Corporation
5 * Copyright © 2015 Collabora Ltd.
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction,
10 * including without limitation the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <glib/gstdio.h>
33 #include <dbus/dbus.h>
38 #include "test-utils-glib.h"
46 DBusConnection *server_conn;
47 /* queue of DBusMessage */
48 GQueue server_messages;
50 DBusConnection *client_conn;
52 gchar *tmp_runtime_dir;
53 gchar *saved_runtime_dir;
57 assert_no_error (const DBusError *e)
59 if (G_UNLIKELY (dbus_error_is_set (e)))
60 g_error ("expected success but got error: %s: %s", e->name, e->message);
63 static DBusHandlerResult
64 server_message_cb (DBusConnection *server_conn,
70 g_assert (server_conn == f->server_conn);
71 g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
73 return DBUS_HANDLER_RESULT_HANDLED;
77 new_conn_cb (DBusServer *server,
78 DBusConnection *server_conn,
84 g_assert (f->server_conn == NULL);
85 f->server_conn = dbus_connection_ref (server_conn);
86 test_connection_setup (f->ctx, server_conn);
88 have_mem = dbus_connection_add_filter (server_conn,
89 server_message_cb, f, NULL);
97 f->ctx = test_main_context_get ();
98 dbus_error_init (&f->e);
99 g_queue_init (&f->server_messages);
101 if ((g_str_has_prefix (addr, "tcp:") ||
102 g_str_has_prefix (addr, "nonce-tcp:")) &&
103 !test_check_tcp_works ())
109 f->server = dbus_server_listen (addr, &f->e);
110 assert_no_error (&f->e);
111 g_assert (f->server != NULL);
113 dbus_server_set_new_connection_function (f->server,
114 new_conn_cb, f, NULL);
115 test_server_setup (f->ctx, f->server);
120 setup_runtime (Fixture *f,
124 GError *error = NULL;
126 /* this is chosen to be something needing escaping */
127 f->tmp_runtime_dir = g_dir_make_tmp ("dbus=daemon=test.XXXXXX", &error);
128 g_assert_no_error (error);
130 /* we're relying on being single-threaded for this to be safe */
131 f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
132 g_setenv ("XDG_RUNTIME_DIR", f->tmp_runtime_dir, TRUE);
139 listening_at = dbus_server_get_address (f->server);
140 g_test_message ("listening at %s", listening_at);
141 g_assert (g_str_has_prefix (listening_at, "unix:path="));
142 g_assert (strstr (listening_at, "dbus%3ddaemon%3dtest.") != NULL);
143 g_assert (strstr (listening_at, "/bus,") != NULL ||
144 g_str_has_suffix (listening_at, "/bus"));
146 dbus_free (listening_at);
150 setup_no_runtime (Fixture *f,
155 /* we're relying on being single-threaded for this to be safe */
156 f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
157 g_unsetenv ("XDG_RUNTIME_DIR");
164 listening_at = dbus_server_get_address (f->server);
165 g_test_message ("listening at %s", listening_at);
166 /* we have fallen back to something in /tmp, either abstract or not */
167 g_assert (g_str_has_prefix (listening_at, "unix:"));
168 g_assert (strstr (listening_at, "=/tmp/") != NULL);
170 dbus_free (listening_at);
175 test_connect (Fixture *f,
178 const char *listening_address = addr;
180 DBusAddressEntry **entries;
187 g_assert (f->server_conn == NULL);
189 address = dbus_server_get_address (f->server);
190 g_test_message ("listening at %s", address);
192 ok = dbus_parse_address (address, &entries, &n_entries, &f->e);
193 assert_no_error (&f->e);
195 g_assert_cmpint (n_entries, ==, 1);
197 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "guid"), !=,
200 if (g_strcmp0 (listening_address, "tcp:host=127.0.0.1") == 0)
202 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "tcp");
203 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "host"), ==,
205 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "port"), !=,
207 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "noncefile"),
210 else if (g_strcmp0 (listening_address, "nonce-tcp:host=127.0.0.1") == 0)
212 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==,
214 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "host"), ==,
216 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "port"), !=,
218 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "noncefile"),
222 else if (g_strcmp0 (listening_address, "unix:tmpdir=/tmp") == 0)
224 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
226 if (dbus_address_entry_get_value (entries[0], "abstract") != NULL)
228 const char *abstract = dbus_address_entry_get_value (entries[0],
231 g_assert_true (g_str_has_prefix (abstract, "/tmp/dbus-"));
232 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "path"),
237 const char *path = dbus_address_entry_get_value (entries[0],
240 g_assert_nonnull (path);
241 g_assert_true (g_str_has_prefix (path, "/tmp/dbus-"));
244 else if (g_strcmp0 (listening_address, "unix:dir=/tmp") == 0)
246 const char *path = dbus_address_entry_get_value (entries[0],
249 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
250 g_assert_nonnull (path);
251 g_assert_true (g_str_has_prefix (path, "/tmp/dbus-"));
253 else if (g_strcmp0 (listening_address,
254 "unix:runtime=yes;unix:tmpdir=/tmp") == 0)
256 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
257 /* No particular statement about the path here: for that see
258 * setup_runtime() and setup_no_runtime() */
263 g_assert_not_reached ();
266 dbus_address_entries_free (entries);
268 f->client_conn = dbus_connection_open_private (address, &f->e);
269 assert_no_error (&f->e);
270 g_assert (f->client_conn != NULL);
271 test_connection_setup (f->ctx, f->client_conn);
273 while (f->server_conn == NULL)
276 test_main_context_iterate (f->ctx, TRUE);
283 test_bad_guid (Fixture *f,
284 gconstpointer addr G_GNUC_UNUSED)
286 DBusMessage *incoming;
293 g_test_bug ("39720");
295 g_assert (f->server_conn == NULL);
297 address = dbus_server_get_address (f->server);
298 g_assert (strstr (address, "guid=") != NULL);
299 guid = strstr (address, "guid=");
300 g_assert_cmpuint (strlen (guid), >=, 5 + 32);
302 /* Change the first char of the guid to something different */
308 f->client_conn = dbus_connection_open_private (address, &f->e);
309 assert_no_error (&f->e);
310 g_assert (f->client_conn != NULL);
311 test_connection_setup (f->ctx, f->client_conn);
313 while (f->server_conn == NULL)
316 test_main_context_iterate (f->ctx, TRUE);
319 /* We get disconnected */
321 while (g_queue_is_empty (&f->server_messages))
324 test_main_context_iterate (f->ctx, TRUE);
327 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
329 incoming = g_queue_pop_head (&f->server_messages);
331 g_assert (!dbus_message_contains_unix_fds (incoming));
332 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
333 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
334 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
335 DBUS_INTERFACE_LOCAL);
336 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
337 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
338 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
339 g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
341 dbus_clear_message (&incoming);
346 test_message (Fixture *f,
349 dbus_bool_t have_mem;
350 dbus_uint32_t serial;
351 DBusMessage *outgoing, *incoming;
356 test_connect (f, addr);
358 outgoing = dbus_message_new_signal ("/com/example/Hello",
359 "com.example.Hello", "Greeting");
360 g_assert (outgoing != NULL);
362 have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
364 g_assert (serial != 0);
366 while (g_queue_is_empty (&f->server_messages))
369 test_main_context_iterate (f->ctx, TRUE);
372 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
374 incoming = g_queue_pop_head (&f->server_messages);
376 g_assert (!dbus_message_contains_unix_fds (incoming));
377 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
378 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
379 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
380 "com.example.Hello");
381 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
382 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
383 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
384 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
385 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
387 dbus_clear_message (&incoming);
388 dbus_clear_message (&outgoing);
392 teardown (Fixture *f,
393 gconstpointer addr G_GNUC_UNUSED)
395 if (f->client_conn != NULL)
396 dbus_connection_close (f->client_conn);
398 if (f->server_conn != NULL)
399 dbus_connection_close (f->server_conn);
401 dbus_clear_connection (&f->client_conn);
402 dbus_clear_connection (&f->server_conn);
404 if (f->server != NULL)
405 dbus_server_disconnect (f->server);
407 dbus_clear_server (&f->server);
408 test_main_context_unref (f->ctx);
413 teardown_no_runtime (Fixture *f,
418 /* we're relying on being single-threaded for this to be safe */
419 if (f->saved_runtime_dir != NULL)
420 g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE);
422 g_unsetenv ("XDG_RUNTIME_DIR");
423 g_free (f->saved_runtime_dir);
427 teardown_runtime (Fixture *f,
434 /* the socket may exist */
435 path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir);
436 test_remove_if_exists (path);
438 /* there shouldn't be anything else in there */
439 test_rmdir_must_exist (f->tmp_runtime_dir);
441 /* we're relying on being single-threaded for this to be safe */
442 if (f->saved_runtime_dir != NULL)
443 g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE);
445 g_unsetenv ("XDG_RUNTIME_DIR");
446 g_free (f->saved_runtime_dir);
447 g_free (f->tmp_runtime_dir);
455 test_init (&argc, &argv);
457 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
458 test_connect, teardown);
459 g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
460 test_message, teardown);
462 g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
463 test_connect, teardown);
464 g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
465 test_message, teardown);
467 g_test_add ("/message/bad-guid/tcp", Fixture, "tcp:host=127.0.0.1", setup,
468 test_bad_guid, teardown);
471 g_test_add ("/connect/unix/tmpdir", Fixture, "unix:tmpdir=/tmp", setup,
472 test_connect, teardown);
473 g_test_add ("/message/unix/tmpdir", Fixture, "unix:tmpdir=/tmp", setup,
474 test_message, teardown);
475 g_test_add ("/connect/unix/dir", Fixture, "unix:dir=/tmp", setup,
476 test_connect, teardown);
477 g_test_add ("/message/unix/dir", Fixture, "unix:dir=/tmp", setup,
478 test_message, teardown);
480 g_test_add ("/connect/unix/runtime", Fixture,
481 "unix:runtime=yes;unix:tmpdir=/tmp", setup_runtime, test_connect,
483 g_test_add ("/connect/unix/no-runtime", Fixture,
484 "unix:runtime=yes;unix:tmpdir=/tmp", setup_no_runtime, test_connect,
485 teardown_no_runtime);
487 g_test_add ("/message/bad-guid/unix", Fixture, "unix:tmpdir=/tmp", setup,
488 test_bad_guid, teardown);
491 return g_test_run ();