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>
32 #include <dbus/dbus-glib-lowlevel.h>
40 DBusConnection *server_conn;
41 /* queue of DBusMessage */
42 GQueue server_messages;
44 DBusConnection *client_conn;
48 assert_no_error (const DBusError *e)
50 if (G_UNLIKELY (dbus_error_is_set (e)))
51 g_error ("expected success but got error: %s: %s", e->name, e->message);
54 static DBusHandlerResult
55 server_message_cb (DBusConnection *server_conn,
61 g_assert (server_conn == f->server_conn);
62 g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
64 return DBUS_HANDLER_RESULT_HANDLED;
68 new_conn_cb (DBusServer *server,
69 DBusConnection *server_conn,
75 g_assert (f->server_conn == NULL);
76 f->server_conn = dbus_connection_ref (server_conn);
77 dbus_connection_setup_with_g_main (server_conn, NULL);
79 have_mem = dbus_connection_add_filter (server_conn,
80 server_message_cb, f, NULL);
88 dbus_error_init (&f->e);
89 g_queue_init (&f->server_messages);
91 f->server = dbus_server_listen (addr, &f->e);
92 assert_no_error (&f->e);
93 g_assert (f->server != NULL);
95 dbus_server_set_new_connection_function (f->server,
96 new_conn_cb, f, NULL);
97 dbus_server_setup_with_g_main (f->server, NULL);
101 test_connect (Fixture *f,
102 gconstpointer addr G_GNUC_UNUSED)
104 g_assert (f->server_conn == NULL);
106 f->client_conn = dbus_connection_open_private (
107 dbus_server_get_address (f->server), &f->e);
108 assert_no_error (&f->e);
109 g_assert (f->client_conn != NULL);
110 dbus_connection_setup_with_g_main (f->client_conn, NULL);
112 while (f->server_conn == NULL)
115 g_main_context_iteration (NULL, TRUE);
120 test_bad_guid (Fixture *f,
121 gconstpointer addr G_GNUC_UNUSED)
123 DBusMessage *incoming;
124 gchar *address = g_strdup (dbus_server_get_address (f->server));
127 g_test_bug ("39720");
129 g_assert (f->server_conn == NULL);
131 g_assert (strstr (address, "guid=") != NULL);
132 guid = strstr (address, "guid=");
133 g_assert_cmpuint (strlen (guid), >=, 5 + 32);
135 /* Change the first char of the guid to something different */
141 f->client_conn = dbus_connection_open_private (address, &f->e);
142 assert_no_error (&f->e);
143 g_assert (f->client_conn != NULL);
144 dbus_connection_setup_with_g_main (f->client_conn, NULL);
146 while (f->server_conn == NULL)
149 g_main_context_iteration (NULL, TRUE);
152 /* We get disconnected */
154 while (g_queue_is_empty (&f->server_messages))
157 g_main_context_iteration (NULL, TRUE);
160 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
162 incoming = g_queue_pop_head (&f->server_messages);
164 g_assert (!dbus_message_contains_unix_fds (incoming));
165 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
166 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
167 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
168 DBUS_INTERFACE_LOCAL);
169 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
170 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
171 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
172 g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
174 dbus_message_unref (incoming);
180 test_message (Fixture *f,
183 dbus_bool_t have_mem;
184 dbus_uint32_t serial;
185 DBusMessage *outgoing, *incoming;
187 test_connect (f, addr);
189 outgoing = dbus_message_new_signal ("/com/example/Hello",
190 "com.example.Hello", "Greeting");
191 g_assert (outgoing != NULL);
193 have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
195 g_assert (serial != 0);
197 while (g_queue_is_empty (&f->server_messages))
200 g_main_context_iteration (NULL, TRUE);
203 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
205 incoming = g_queue_pop_head (&f->server_messages);
207 g_assert (!dbus_message_contains_unix_fds (incoming));
208 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
209 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
210 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
211 "com.example.Hello");
212 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
213 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
214 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
215 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
216 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
218 dbus_message_unref (incoming);
220 dbus_message_unref (outgoing);
224 teardown (Fixture *f,
225 gconstpointer addr G_GNUC_UNUSED)
227 if (f->client_conn != NULL)
229 dbus_connection_close (f->client_conn);
230 dbus_connection_unref (f->client_conn);
231 f->client_conn = NULL;
234 if (f->server_conn != NULL)
236 dbus_connection_close (f->server_conn);
237 dbus_connection_unref (f->server_conn);
238 f->server_conn = NULL;
241 if (f->server != NULL)
243 dbus_server_disconnect (f->server);
244 dbus_server_unref (f->server);
253 g_test_init (&argc, &argv, NULL);
254 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
256 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
257 test_connect, teardown);
258 g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
259 test_message, teardown);
261 g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
262 test_connect, teardown);
263 g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
264 test_message, teardown);
267 g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
268 test_connect, teardown);
269 g_test_add ("/message/unix", Fixture, "unix:tmpdir=/tmp", setup,
270 test_message, teardown);
273 g_test_add ("/message/bad-guid", Fixture, "tcp:host=127.0.0.1", setup,
274 test_bad_guid, teardown);
276 return g_test_run ();