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>
32 #include <dbus/dbus-glib-lowlevel.h>
34 /* This is basically a miniature dbus-daemon. We relay messages from the client
35 * on the left to the client on the right.
37 * left socket left dispatch right socket right
38 * client ===========> server --------------> server ===========> client
41 * In the real dbus-daemon, the client connections would be out-of-process,
42 * but here we're cheating and doing everything in-process.
50 DBusConnection *left_client_conn;
51 DBusConnection *left_server_conn;
53 DBusConnection *right_server_conn;
54 DBusConnection *right_client_conn;
55 /* queue of DBusMessage received by right_client_conn */
60 assert_no_error (const DBusError *e)
62 if (G_UNLIKELY (dbus_error_is_set (e)))
63 g_error ("expected success but got error: %s: %s", e->name, e->message);
66 static DBusHandlerResult
67 server_message_cb (DBusConnection *server_conn,
73 g_assert (server_conn == f->left_server_conn);
74 g_assert (f->right_server_conn != NULL);
76 dbus_connection_send (f->right_server_conn, message, NULL);
78 return DBUS_HANDLER_RESULT_HANDLED;
81 static DBusHandlerResult
82 right_client_message_cb (DBusConnection *client_conn,
88 g_assert (client_conn == f->right_client_conn);
89 g_queue_push_tail (&f->messages, dbus_message_ref (message));
91 return DBUS_HANDLER_RESULT_HANDLED;
95 new_conn_cb (DBusServer *server,
96 DBusConnection *server_conn,
100 dbus_bool_t have_mem;
102 if (f->left_server_conn == NULL)
104 f->left_server_conn = dbus_connection_ref (server_conn);
106 have_mem = dbus_connection_add_filter (server_conn,
107 server_message_cb, f, NULL);
112 g_assert (f->right_server_conn == NULL);
113 f->right_server_conn = dbus_connection_ref (server_conn);
116 dbus_connection_setup_with_g_main (server_conn, NULL);
121 gconstpointer data G_GNUC_UNUSED)
123 dbus_error_init (&f->e);
124 g_queue_init (&f->messages);
126 f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
127 assert_no_error (&f->e);
128 g_assert (f->server != NULL);
130 dbus_server_set_new_connection_function (f->server,
131 new_conn_cb, f, NULL);
132 dbus_server_setup_with_g_main (f->server, NULL);
136 test_connect (Fixture *f,
137 gconstpointer data G_GNUC_UNUSED)
139 dbus_bool_t have_mem;
142 g_assert (f->left_server_conn == NULL);
143 g_assert (f->right_server_conn == NULL);
145 address = dbus_server_get_address (f->server);
146 g_assert (address != NULL);
148 f->left_client_conn = dbus_connection_open_private (address, &f->e);
149 assert_no_error (&f->e);
150 g_assert (f->left_client_conn != NULL);
151 dbus_connection_setup_with_g_main (f->left_client_conn, NULL);
153 while (f->left_server_conn == NULL)
156 g_main_context_iteration (NULL, TRUE);
159 f->right_client_conn = dbus_connection_open_private (address, &f->e);
160 assert_no_error (&f->e);
161 g_assert (f->right_client_conn != NULL);
162 dbus_connection_setup_with_g_main (f->right_client_conn, NULL);
166 while (f->right_server_conn == NULL)
169 g_main_context_iteration (NULL, TRUE);
172 have_mem = dbus_connection_add_filter (f->right_client_conn,
173 right_client_message_cb, f, NULL);
178 send_one (Fixture *f,
181 dbus_bool_t have_mem;
182 dbus_uint32_t serial;
183 DBusMessage *outgoing;
185 outgoing = dbus_message_new_signal ("/com/example/Hello",
186 "com.example.Hello", member);
187 g_assert (outgoing != NULL);
189 have_mem = dbus_connection_send (f->left_client_conn, outgoing, &serial);
191 g_assert (serial != 0);
193 dbus_message_unref (outgoing);
198 test_relay (Fixture *f,
201 DBusMessage *incoming;
203 test_connect (f, data);
205 send_one (f, "First");
206 send_one (f, "Second");
208 while (g_queue_get_length (&f->messages) < 2)
211 g_main_context_iteration (NULL, TRUE);
214 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2);
216 incoming = g_queue_pop_head (&f->messages);
217 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "First");
218 dbus_message_unref (incoming);
220 incoming = g_queue_pop_head (&f->messages);
221 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Second");
222 dbus_message_unref (incoming);
225 /* An arbitrary number of messages */
229 test_limit (Fixture *f,
232 DBusMessage *incoming;
235 test_connect (f, data);
237 /* This was an attempt to reproduce fd.o #34393. It didn't work. */
238 g_test_bug ("34393");
239 dbus_connection_set_max_received_size (f->left_server_conn, 1);
240 g_main_context_iteration (NULL, TRUE);
242 for (i = 0; i < MANY; i++)
244 gchar *buf = g_strdup_printf ("Message%u", i);
254 while (g_queue_is_empty (&f->messages))
256 g_main_context_iteration (NULL, TRUE);
259 while ((incoming = g_queue_pop_head (&f->messages)) != NULL)
262 dbus_message_unref (incoming);
268 teardown (Fixture *f,
269 gconstpointer data G_GNUC_UNUSED)
271 if (f->left_client_conn != NULL)
273 dbus_connection_close (f->left_client_conn);
274 dbus_connection_unref (f->left_client_conn);
275 f->left_client_conn = NULL;
278 if (f->right_client_conn != NULL)
280 dbus_connection_close (f->right_client_conn);
281 dbus_connection_unref (f->right_client_conn);
282 f->right_client_conn = NULL;
285 if (f->left_server_conn != NULL)
287 dbus_connection_close (f->left_server_conn);
288 dbus_connection_unref (f->left_server_conn);
289 f->left_server_conn = NULL;
292 if (f->right_server_conn != NULL)
294 dbus_connection_close (f->right_server_conn);
295 dbus_connection_unref (f->right_server_conn);
296 f->right_server_conn = NULL;
299 if (f->server != NULL)
301 dbus_server_disconnect (f->server);
302 dbus_server_unref (f->server);
311 g_test_init (&argc, &argv, NULL);
312 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
314 g_test_add ("/connect", Fixture, NULL, setup,
315 test_connect, teardown);
316 g_test_add ("/relay", Fixture, NULL, setup,
317 test_relay, teardown);
318 g_test_add ("/limit", Fixture, NULL, setup,
319 test_limit, teardown);
321 return g_test_run ();