1 /* Regression test for being disconnected by a corrupt message (fd.o #15578)
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
32 #include <dbus/dbus.h>
34 #include "test-utils-glib.h"
42 DBusConnection *server_conn;
43 /* queue of DBusMessage */
44 GQueue client_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 client_message_cb (DBusConnection *client_conn,
63 g_assert (client_conn == f->client_conn);
64 g_queue_push_tail (&f->client_messages, dbus_message_ref (message));
66 return DBUS_HANDLER_RESULT_HANDLED;
70 new_conn_cb (DBusServer *server,
71 DBusConnection *server_conn,
76 g_assert (f->server_conn == NULL);
77 f->server_conn = dbus_connection_ref (server_conn);
78 test_connection_setup (f->ctx, server_conn);
85 f->ctx = test_main_context_get ();
86 dbus_error_init (&f->e);
87 g_queue_init (&f->client_messages);
89 if ((g_str_has_prefix (addr, "tcp:") ||
90 g_str_has_prefix (addr, "nonce-tcp:")) &&
91 !test_check_tcp_works ())
97 f->server = dbus_server_listen (addr, &f->e);
98 assert_no_error (&f->e);
99 g_assert (f->server != NULL);
101 dbus_server_set_new_connection_function (f->server,
102 new_conn_cb, f, NULL);
103 test_server_setup (f->ctx, f->server);
107 test_connect (Fixture *f,
108 gconstpointer addr G_GNUC_UNUSED)
110 dbus_bool_t have_mem;
111 char *address = NULL;
116 g_assert (f->server_conn == NULL);
118 address = dbus_server_get_address (f->server);
119 f->client_conn = dbus_connection_open_private (address, &f->e);
120 assert_no_error (&f->e);
121 g_assert (f->client_conn != NULL);
122 test_connection_setup (f->ctx, f->client_conn);
125 while (f->server_conn == NULL)
128 test_main_context_iterate (f->ctx, TRUE);
131 have_mem = dbus_connection_add_filter (f->client_conn,
132 client_message_cb, f, NULL);
137 test_message (Fixture *f,
140 dbus_bool_t have_mem;
141 dbus_uint32_t serial;
142 DBusMessage *outgoing, *incoming;
147 test_connect (f, addr);
149 outgoing = dbus_message_new_signal ("/com/example/Hello",
150 "com.example.Hello", "Greeting");
151 g_assert (outgoing != NULL);
153 have_mem = dbus_connection_send (f->server_conn, outgoing, &serial);
155 g_assert (serial != 0);
157 while (g_queue_is_empty (&f->client_messages))
160 test_main_context_iterate (f->ctx, TRUE);
163 g_assert_cmpuint (g_queue_get_length (&f->client_messages), ==, 1);
165 incoming = g_queue_pop_head (&f->client_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 "com.example.Hello");
172 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
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), ==, "/com/example/Hello");
176 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
178 dbus_message_unref (incoming);
180 dbus_message_unref (outgoing);
184 send_n_bytes (GSocket *socket,
188 gssize len, total_sent;
189 GError *gerror = NULL;
193 while (total_sent < blob_len)
195 len = g_socket_send (socket,
197 blob_len - total_sent,
200 /* this is NULL-safe: a NULL error does not match */
201 if (g_error_matches (gerror, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
203 /* we could wait for G_IO_OUT, but life's too short; just sleep */
204 g_clear_error (&gerror);
205 g_usleep (G_USEC_PER_SEC / 10);
209 g_assert_no_error (gerror);
215 /* Enough bytes for it to be obvious that this connection is broken */
216 #define CORRUPT_LEN 1024
218 /* All-zero is not a valid D-Bus message header - for a start, this is
219 * protocol version 1, not 0 */
220 static const gchar not_a_dbus_message[CORRUPT_LEN] = { 0 };
223 test_corrupt (Fixture *f,
227 GError *gerror = NULL;
229 DBusMessage *incoming;
234 test_message (f, addr);
236 dbus_connection_flush (f->server_conn);
238 /* OK, now the connection is working, let's break it! Don't try this
239 * at home; splicing arbitrary bytes into the middle of the stream is
240 * specifically documented as not a valid thing to do. Who'd have thought? */
241 if (!dbus_connection_get_socket (f->server_conn, &fd))
242 g_error ("failed to steal fd from server connection");
244 socket = g_socket_new_from_fd (fd, &gerror);
245 g_assert_no_error (gerror);
246 g_assert (socket != NULL);
248 send_n_bytes (socket, not_a_dbus_message, CORRUPT_LEN);
250 /* Now spin on the client connection: the server just sent it complete
251 * rubbish, so it should disconnect */
252 while (g_queue_is_empty (&f->client_messages))
255 test_main_context_iterate (f->ctx, TRUE);
258 incoming = g_queue_pop_head (&f->client_messages);
260 g_assert (!dbus_message_contains_unix_fds (incoming));
261 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
262 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
263 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
264 "org.freedesktop.DBus.Local");
265 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
266 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
267 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
268 g_assert_cmpstr (dbus_message_get_path (incoming), ==,
269 "/org/freedesktop/DBus/Local");
271 dbus_message_unref (incoming);
273 /* Free the DBusConnection before the GSocket, because GSocket is
274 * going to close our fd. GSocket tolerates closing an already-closed
275 * fd, whereas DBusLoop + DBusSocketSetEpoll doesn't. On Unix
276 * we could use dup() but that isn't portable to Windows :-(
278 dbus_connection_close (f->server_conn);
279 dbus_connection_unref (f->server_conn);
280 f->server_conn = NULL;
282 g_object_unref (socket);
286 test_byte_order (Fixture *f,
290 GError *gerror = NULL;
293 const gchar *arg = not_a_dbus_message;
295 DBusMessage *message;
301 test_message (f, addr);
303 message = dbus_message_new_signal ("/", "a.b", "c");
304 g_assert (message != NULL);
305 /* Append 0xFF bytes, so that the length of the body when byte-swapped
306 * is 0xFF000000, which is invalid */
307 mem = dbus_message_append_args (message,
308 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &arg, 0xFF,
311 mem = dbus_message_marshal (message, &blob, &blob_len);
313 g_assert_cmpuint (blob_len, >, 0xFF);
314 g_assert (blob != NULL);
316 dbus_message_unref (message);
318 /* Break the message by changing its claimed byte order, without actually
319 * byteswapping anything. We happen to know that byte order is the first
326 /* OK, now the connection is working, let's break it */
328 dbus_connection_flush (f->server_conn);
330 if (!dbus_connection_get_socket (f->server_conn, &fd))
331 g_error ("failed to steal fd from server connection");
333 socket = g_socket_new_from_fd (fd, &gerror);
334 g_assert_no_error (gerror);
335 g_assert (socket != NULL);
337 send_n_bytes (socket, blob, blob_len);
341 /* Now spin on the client connection: the server just sent it a faulty
342 * message, so it should disconnect */
343 while (g_queue_is_empty (&f->client_messages))
346 test_main_context_iterate (f->ctx, TRUE);
349 message = g_queue_pop_head (&f->client_messages);
351 g_assert (!dbus_message_contains_unix_fds (message));
352 g_assert_cmpstr (dbus_message_get_destination (message), ==, NULL);
353 g_assert_cmpstr (dbus_message_get_error_name (message), ==, NULL);
354 g_assert_cmpstr (dbus_message_get_interface (message), ==,
355 "org.freedesktop.DBus.Local");
356 g_assert_cmpstr (dbus_message_get_member (message), ==, "Disconnected");
357 g_assert_cmpstr (dbus_message_get_sender (message), ==, NULL);
358 g_assert_cmpstr (dbus_message_get_signature (message), ==, "");
359 g_assert_cmpstr (dbus_message_get_path (message), ==,
360 "/org/freedesktop/DBus/Local");
362 dbus_message_unref (message);
364 /* Free the DBusConnection before the GSocket, as above. */
365 dbus_connection_close (f->server_conn);
366 dbus_connection_unref (f->server_conn);
367 f->server_conn = NULL;
369 g_object_unref (socket);
373 teardown (Fixture *f,
374 gconstpointer addr G_GNUC_UNUSED)
376 if (f->client_conn != NULL)
378 test_connection_shutdown (f->ctx, f->client_conn);
379 dbus_connection_close (f->client_conn);
380 dbus_connection_unref (f->client_conn);
381 f->client_conn = NULL;
384 if (f->server_conn != NULL)
386 test_connection_shutdown (f->ctx, f->server_conn);
387 dbus_connection_close (f->server_conn);
388 dbus_connection_unref (f->server_conn);
389 f->server_conn = NULL;
392 if (f->server != NULL)
394 dbus_server_disconnect (f->server);
395 dbus_server_unref (f->server);
399 test_main_context_unref (f->ctx);
406 test_init (&argc, &argv);
408 g_test_add ("/corrupt/tcp", Fixture, "tcp:host=127.0.0.1", setup,
409 test_corrupt, teardown);
412 g_test_add ("/corrupt/unix", Fixture, "unix:tmpdir=/tmp", setup,
413 test_corrupt, teardown);
416 g_test_add ("/corrupt/byte-order/tcp", Fixture, "tcp:host=127.0.0.1", setup,
417 test_byte_order, teardown);
420 g_test_add ("/corrupt/byte-order/unix", Fixture, "unix:tmpdir=/tmp", setup,
421 test_byte_order, teardown);
424 return g_test_run ();