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
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>
35 #include "test-utils.h"
42 DBusConnection *server_conn;
43 /* queue of DBusMessage */
44 GQueue server_messages;
46 DBusConnection *client_conn;
50 assert_no_error (const DBusError *e)
52 if (G_UNLIKELY (dbus_error_is_set (e)))
53 g_error ("expected success but got error: %s: %s", e->name, e->message);
56 static DBusHandlerResult
57 server_message_cb (DBusConnection *server_conn,
63 g_assert (server_conn == f->server_conn);
64 g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
66 return DBUS_HANDLER_RESULT_HANDLED;
70 new_conn_cb (DBusServer *server,
71 DBusConnection *server_conn,
77 g_assert (f->server_conn == NULL);
78 f->server_conn = dbus_connection_ref (server_conn);
79 test_connection_setup (f->ctx, server_conn);
81 have_mem = dbus_connection_add_filter (server_conn,
82 server_message_cb, f, NULL);
90 f->ctx = test_main_context_get ();
91 dbus_error_init (&f->e);
92 g_queue_init (&f->server_messages);
94 f->server = dbus_server_listen (addr, &f->e);
95 assert_no_error (&f->e);
96 g_assert (f->server != NULL);
98 dbus_server_set_new_connection_function (f->server,
99 new_conn_cb, f, NULL);
100 test_server_setup (f->ctx, f->server);
104 test_connect (Fixture *f,
105 gconstpointer addr G_GNUC_UNUSED)
107 g_assert (f->server_conn == NULL);
109 f->client_conn = dbus_connection_open_private (
110 dbus_server_get_address (f->server), &f->e);
111 assert_no_error (&f->e);
112 g_assert (f->client_conn != NULL);
113 test_connection_setup (f->ctx, f->client_conn);
115 while (f->server_conn == NULL)
118 test_main_context_iterate (f->ctx, TRUE);
123 test_bad_guid (Fixture *f,
124 gconstpointer addr G_GNUC_UNUSED)
126 DBusMessage *incoming;
127 gchar *address = g_strdup (dbus_server_get_address (f->server));
130 g_test_bug ("39720");
132 g_assert (f->server_conn == NULL);
134 g_assert (strstr (address, "guid=") != NULL);
135 guid = strstr (address, "guid=");
136 g_assert_cmpuint (strlen (guid), >=, 5 + 32);
138 /* Change the first char of the guid to something different */
144 f->client_conn = dbus_connection_open_private (address, &f->e);
145 assert_no_error (&f->e);
146 g_assert (f->client_conn != NULL);
147 test_connection_setup (f->ctx, f->client_conn);
149 while (f->server_conn == NULL)
152 test_main_context_iterate (f->ctx, TRUE);
155 /* We get disconnected */
157 while (g_queue_is_empty (&f->server_messages))
160 test_main_context_iterate (f->ctx, TRUE);
163 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
165 incoming = g_queue_pop_head (&f->server_messages);
167 g_assert (!dbus_message_contains_unix_fds (incoming));
168 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
169 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
170 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
171 DBUS_INTERFACE_LOCAL);
172 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
173 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
174 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
175 g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
177 dbus_message_unref (incoming);
183 test_message (Fixture *f,
186 dbus_bool_t have_mem;
187 dbus_uint32_t serial;
188 DBusMessage *outgoing, *incoming;
190 test_connect (f, addr);
192 outgoing = dbus_message_new_signal ("/com/example/Hello",
193 "com.example.Hello", "Greeting");
194 g_assert (outgoing != NULL);
196 have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
198 g_assert (serial != 0);
200 while (g_queue_is_empty (&f->server_messages))
203 test_main_context_iterate (f->ctx, TRUE);
206 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
208 incoming = g_queue_pop_head (&f->server_messages);
210 g_assert (!dbus_message_contains_unix_fds (incoming));
211 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
212 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
213 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
214 "com.example.Hello");
215 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
216 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
217 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
218 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
219 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
221 dbus_message_unref (incoming);
223 dbus_message_unref (outgoing);
227 teardown (Fixture *f,
228 gconstpointer addr G_GNUC_UNUSED)
230 if (f->client_conn != NULL)
232 dbus_connection_close (f->client_conn);
233 dbus_connection_unref (f->client_conn);
234 f->client_conn = NULL;
237 if (f->server_conn != NULL)
239 dbus_connection_close (f->server_conn);
240 dbus_connection_unref (f->server_conn);
241 f->server_conn = NULL;
244 if (f->server != NULL)
246 dbus_server_disconnect (f->server);
247 dbus_server_unref (f->server);
251 test_main_context_unref (f->ctx);
258 g_test_init (&argc, &argv, NULL);
259 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
261 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
262 test_connect, teardown);
263 g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
264 test_message, teardown);
266 g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
267 test_connect, teardown);
268 g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
269 test_message, teardown);
272 g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
273 test_connect, teardown);
274 g_test_add ("/message/unix", Fixture, "unix:tmpdir=/tmp", setup,
275 test_message, teardown);
278 g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup,
279 test_bad_guid, teardown);
281 return g_test_run ();