1 /* Regression test for passing unmodified messages between connections
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
31 #include <dbus/dbus.h>
33 #include "test-utils-glib.h"
35 /* This is basically a miniature dbus-daemon. We relay messages from the client
36 * on the left to the client on the right.
38 * left socket left dispatch right socket right
39 * client ===========> server --------------> server ===========> client
42 * In the real dbus-daemon, the client connections would be out-of-process,
43 * but here we're cheating and doing everything in-process.
53 DBusConnection *left_client_conn;
54 DBusConnection *left_server_conn;
56 DBusConnection *right_server_conn;
57 DBusConnection *right_client_conn;
58 /* queue of DBusMessage received by right_client_conn */
63 assert_no_error (const DBusError *e)
65 if (G_UNLIKELY (dbus_error_is_set (e)))
66 g_error ("expected success but got error: %s: %s", e->name, e->message);
69 static DBusHandlerResult
70 server_message_cb (DBusConnection *server_conn,
76 g_assert (server_conn == f->left_server_conn);
77 g_assert (f->right_server_conn != NULL);
79 dbus_connection_send (f->right_server_conn, message, NULL);
81 return DBUS_HANDLER_RESULT_HANDLED;
84 static DBusHandlerResult
85 right_client_message_cb (DBusConnection *client_conn,
91 g_assert (client_conn == f->right_client_conn);
92 g_queue_push_tail (&f->messages, dbus_message_ref (message));
94 return DBUS_HANDLER_RESULT_HANDLED;
98 new_conn_cb (DBusServer *server,
99 DBusConnection *server_conn,
103 dbus_bool_t have_mem;
105 if (f->left_server_conn == NULL)
107 f->left_server_conn = dbus_connection_ref (server_conn);
109 have_mem = dbus_connection_add_filter (server_conn,
110 server_message_cb, f, NULL);
115 g_assert (f->right_server_conn == NULL);
116 f->right_server_conn = dbus_connection_ref (server_conn);
119 test_connection_setup (f->ctx, server_conn);
124 gconstpointer address)
126 test_timeout_reset (1);
128 f->ctx = test_main_context_get ();
129 dbus_error_init (&f->e);
130 g_queue_init (&f->messages);
132 if ((g_str_has_prefix (address, "tcp:") ||
133 g_str_has_prefix (address, "nonce-tcp:")) &&
134 !test_check_tcp_works ())
140 f->server = dbus_server_listen (address, &f->e);
141 assert_no_error (&f->e);
142 g_assert (f->server != NULL);
144 dbus_server_set_new_connection_function (f->server,
145 new_conn_cb, f, NULL);
146 test_server_setup (f->ctx, f->server);
150 test_connect (Fixture *f,
151 gconstpointer data G_GNUC_UNUSED)
153 dbus_bool_t have_mem;
159 g_assert (f->left_server_conn == NULL);
160 g_assert (f->right_server_conn == NULL);
162 address = dbus_server_get_address (f->server);
163 g_assert (address != NULL);
165 f->left_client_conn = dbus_connection_open_private (address, &f->e);
166 assert_no_error (&f->e);
167 g_assert (f->left_client_conn != NULL);
168 test_connection_setup (f->ctx, f->left_client_conn);
170 while (f->left_server_conn == NULL)
173 test_main_context_iterate (f->ctx, TRUE);
176 f->right_client_conn = dbus_connection_open_private (address, &f->e);
177 assert_no_error (&f->e);
178 g_assert (f->right_client_conn != NULL);
179 test_connection_setup (f->ctx, f->right_client_conn);
183 while (f->right_server_conn == NULL)
186 test_main_context_iterate (f->ctx, TRUE);
189 have_mem = dbus_connection_add_filter (f->right_client_conn,
190 right_client_message_cb, f, NULL);
195 send_one (Fixture *f,
198 dbus_bool_t have_mem;
199 dbus_uint32_t serial;
200 DBusMessage *outgoing;
202 outgoing = dbus_message_new_signal ("/com/example/Hello",
203 "com.example.Hello", member);
204 g_assert (outgoing != NULL);
206 have_mem = dbus_connection_send (f->left_client_conn, outgoing, &serial);
208 g_assert (serial != 0);
210 dbus_message_unref (outgoing);
215 test_relay (Fixture *f,
218 DBusMessage *incoming;
223 test_connect (f, data);
225 send_one (f, "First");
226 send_one (f, "Second");
228 while (g_queue_get_length (&f->messages) < 2)
231 test_main_context_iterate (f->ctx, TRUE);
234 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2);
236 incoming = g_queue_pop_head (&f->messages);
237 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "First");
238 dbus_message_unref (incoming);
240 incoming = g_queue_pop_head (&f->messages);
241 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Second");
242 dbus_message_unref (incoming);
245 /* An arbitrary number of messages */
249 test_limit (Fixture *f,
252 DBusMessage *incoming;
258 test_connect (f, data);
260 /* This was an attempt to reproduce fd.o #34393. It didn't work. */
261 g_test_bug ("34393");
262 dbus_connection_set_max_received_size (f->left_server_conn, 1);
263 test_main_context_iterate (f->ctx, TRUE);
265 for (i = 0; i < MANY; i++)
267 gchar *buf = g_strdup_printf ("Message%u", i);
277 while (g_queue_is_empty (&f->messages))
279 test_main_context_iterate (f->ctx, TRUE);
282 while ((incoming = g_queue_pop_head (&f->messages)) != NULL)
285 dbus_message_unref (incoming);
291 teardown (Fixture *f,
292 gconstpointer data G_GNUC_UNUSED)
294 if (f->left_client_conn != NULL)
296 test_connection_shutdown(NULL, f->left_client_conn);
297 dbus_connection_close (f->left_client_conn);
298 dbus_connection_unref (f->left_client_conn);
299 f->left_client_conn = NULL;
302 if (f->right_client_conn != NULL)
304 test_connection_shutdown(NULL, f->right_client_conn);
305 dbus_connection_close (f->right_client_conn);
306 dbus_connection_unref (f->right_client_conn);
307 f->right_client_conn = NULL;
310 if (f->left_server_conn != NULL)
312 test_connection_shutdown(NULL, f->left_server_conn);
313 dbus_connection_close (f->left_server_conn);
314 dbus_connection_unref (f->left_server_conn);
315 f->left_server_conn = NULL;
318 if (f->right_server_conn != NULL)
320 test_connection_shutdown(NULL, f->right_server_conn);
321 dbus_connection_close (f->right_server_conn);
322 dbus_connection_unref (f->right_server_conn);
323 f->right_server_conn = NULL;
326 if (f->server != NULL)
328 dbus_server_disconnect (f->server);
329 dbus_server_unref (f->server);
333 test_main_context_unref (f->ctx);
340 test_init (&argc, &argv);
342 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
343 test_connect, teardown);
344 g_test_add ("/relay/tcp", Fixture, "tcp:host=127.0.0.1", setup,
345 test_relay, teardown);
346 g_test_add ("/limit/tcp", Fixture, "tcp:host=127.0.0.1", setup,
347 test_limit, teardown);
350 g_test_add ("/connect/unix", Fixture, "unix:tmpdir=/tmp", setup,
351 test_connect, teardown);
352 g_test_add ("/relay/unix", Fixture, "unix:tmpdir=/tmp", setup,
353 test_relay, teardown);
354 g_test_add ("/limit/unix", Fixture, "unix:tmpdir=/tmp", setup,
355 test_limit, teardown);
358 return g_test_run ();