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>
33 #include <dbus/dbus-glib-lowlevel.h>
39 DBusConnection *server_conn;
40 /* queue of DBusMessage */
41 GQueue client_messages;
43 DBusConnection *client_conn;
47 assert_no_error (const DBusError *e)
49 if (G_UNLIKELY (dbus_error_is_set (e)))
50 g_error ("expected success but got error: %s: %s", e->name, e->message);
53 static DBusHandlerResult
54 client_message_cb (DBusConnection *client_conn,
60 g_assert (client_conn == f->client_conn);
61 g_queue_push_tail (&f->client_messages, dbus_message_ref (message));
63 return DBUS_HANDLER_RESULT_HANDLED;
67 new_conn_cb (DBusServer *server,
68 DBusConnection *server_conn,
73 g_assert (f->server_conn == NULL);
74 f->server_conn = dbus_connection_ref (server_conn);
75 dbus_connection_setup_with_g_main (server_conn, NULL);
82 dbus_error_init (&f->e);
83 g_queue_init (&f->client_messages);
85 f->server = dbus_server_listen (addr, &f->e);
86 assert_no_error (&f->e);
87 g_assert (f->server != NULL);
89 dbus_server_set_new_connection_function (f->server,
90 new_conn_cb, f, NULL);
91 dbus_server_setup_with_g_main (f->server, NULL);
95 test_connect (Fixture *f,
96 gconstpointer addr G_GNUC_UNUSED)
100 g_assert (f->server_conn == NULL);
102 f->client_conn = dbus_connection_open_private (
103 dbus_server_get_address (f->server), &f->e);
104 assert_no_error (&f->e);
105 g_assert (f->client_conn != NULL);
106 dbus_connection_setup_with_g_main (f->client_conn, NULL);
108 while (f->server_conn == NULL)
111 g_main_context_iteration (NULL, TRUE);
114 have_mem = dbus_connection_add_filter (f->client_conn,
115 client_message_cb, f, NULL);
120 test_message (Fixture *f,
123 dbus_bool_t have_mem;
124 dbus_uint32_t serial;
125 DBusMessage *outgoing, *incoming;
127 test_connect (f, addr);
129 outgoing = dbus_message_new_signal ("/com/example/Hello",
130 "com.example.Hello", "Greeting");
131 g_assert (outgoing != NULL);
133 have_mem = dbus_connection_send (f->server_conn, outgoing, &serial);
135 g_assert (serial != 0);
137 while (g_queue_is_empty (&f->client_messages))
140 g_main_context_iteration (NULL, TRUE);
143 g_assert_cmpuint (g_queue_get_length (&f->client_messages), ==, 1);
145 incoming = g_queue_pop_head (&f->client_messages);
147 g_assert (!dbus_message_contains_unix_fds (incoming));
148 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
149 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
150 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
151 "com.example.Hello");
152 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
153 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
154 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
155 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
156 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
158 dbus_message_unref (incoming);
160 dbus_message_unref (outgoing);
163 /* Enough bytes for it to be obvious that this connection is broken */
164 #define CORRUPT_LEN 1024
166 /* All-zero is not a valid D-Bus message header - for a start, this is
167 * protocol version 1, not 0 */
168 static const gchar not_a_dbus_message[CORRUPT_LEN] = { 0 };
171 test_corrupt (Fixture *f,
175 GError *gerror = NULL;
177 gssize len, total_sent;
178 DBusMessage *incoming;
180 test_message (f, addr);
182 dbus_connection_flush (f->server_conn);
184 /* OK, now the connection is working, let's break it! Don't try this
185 * at home; splicing arbitrary bytes into the middle of the stream is
186 * specifically documented as not a valid thing to do. Who'd have thought? */
187 if (!dbus_connection_get_socket (f->server_conn, &fd))
188 g_error ("failed to steal fd from server connection");
190 socket = g_socket_new_from_fd (fd, &gerror);
191 g_assert_no_error (gerror);
192 g_assert (socket != NULL);
196 while (total_sent < CORRUPT_LEN)
198 len = g_socket_send_with_blocking (socket,
199 not_a_dbus_message + total_sent, CORRUPT_LEN - total_sent,
200 TRUE, NULL, &gerror);
201 g_assert_no_error (gerror);
206 /* Now spin on the client connection: the server just sent it complete
207 * rubbish, so it should disconnect */
208 while (g_queue_is_empty (&f->client_messages))
211 g_main_context_iteration (NULL, TRUE);
214 incoming = g_queue_pop_head (&f->client_messages);
216 g_assert (!dbus_message_contains_unix_fds (incoming));
217 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
218 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
219 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
220 "org.freedesktop.DBus.Local");
221 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
222 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
223 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
224 g_assert_cmpstr (dbus_message_get_path (incoming), ==,
225 "/org/freedesktop/DBus/Local");
227 dbus_message_unref (incoming);
231 test_byte_order (Fixture *f,
235 GError *gerror = NULL;
238 const gchar *arg = not_a_dbus_message;
239 const gchar * const *args = &arg;
240 int blob_len, len, total_sent;
241 DBusMessage *message;
244 test_message (f, addr);
246 message = dbus_message_new_signal ("/", "a.b", "c");
247 g_assert (message != NULL);
248 /* Append 0xFF bytes, so that the length of the body when byte-swapped
249 * is 0xFF000000, which is invalid */
250 mem = dbus_message_append_args (message,
251 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &args, 0xFF,
254 mem = dbus_message_marshal (message, &blob, &blob_len);
256 g_assert_cmpuint (blob_len, >, 0xFF);
257 g_assert (blob != NULL);
259 dbus_message_unref (message);
261 /* Break the message by changing its claimed byte order, without actually
262 * byteswapping anything. We happen to know that byte order is the first
269 /* OK, now the connection is working, let's break it */
271 dbus_connection_flush (f->server_conn);
273 if (!dbus_connection_get_socket (f->server_conn, &fd))
274 g_error ("failed to steal fd from server connection");
276 socket = g_socket_new_from_fd (fd, &gerror);
277 g_assert_no_error (gerror);
278 g_assert (socket != NULL);
282 while (total_sent < blob_len)
284 len = g_socket_send_with_blocking (socket, blob + total_sent,
285 blob_len - total_sent, TRUE, NULL, &gerror);
286 g_assert_no_error (gerror);
293 /* Now spin on the client connection: the server just sent it a faulty
294 * message, so it should disconnect */
295 while (g_queue_is_empty (&f->client_messages))
298 g_main_context_iteration (NULL, TRUE);
301 message = g_queue_pop_head (&f->client_messages);
303 g_assert (!dbus_message_contains_unix_fds (message));
304 g_assert_cmpstr (dbus_message_get_destination (message), ==, NULL);
305 g_assert_cmpstr (dbus_message_get_error_name (message), ==, NULL);
306 g_assert_cmpstr (dbus_message_get_interface (message), ==,
307 "org.freedesktop.DBus.Local");
308 g_assert_cmpstr (dbus_message_get_member (message), ==, "Disconnected");
309 g_assert_cmpstr (dbus_message_get_sender (message), ==, NULL);
310 g_assert_cmpstr (dbus_message_get_signature (message), ==, "");
311 g_assert_cmpstr (dbus_message_get_path (message), ==,
312 "/org/freedesktop/DBus/Local");
314 dbus_message_unref (message);
318 teardown (Fixture *f,
319 gconstpointer addr G_GNUC_UNUSED)
321 if (f->client_conn != NULL)
323 dbus_connection_close (f->client_conn);
324 dbus_connection_unref (f->client_conn);
325 f->client_conn = NULL;
328 if (f->server_conn != NULL)
330 dbus_connection_close (f->server_conn);
331 dbus_connection_unref (f->server_conn);
332 f->server_conn = NULL;
335 if (f->server != NULL)
337 dbus_server_disconnect (f->server);
338 dbus_server_unref (f->server);
347 g_test_init (&argc, &argv, NULL);
350 g_test_add ("/corrupt/tcp", Fixture, "tcp:host=127.0.0.1", setup,
351 test_corrupt, teardown);
354 g_test_add ("/corrupt/unix", Fixture, "unix:tmpdir=/tmp", setup,
355 test_corrupt, teardown);
358 g_test_add ("/corrupt/byte-order/tcp", Fixture, "tcp:host=127.0.0.1", setup,
359 test_byte_order, teardown);
361 return g_test_run ();