Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / test / relay.c
1 /* Regression test for passing unmodified messages between connections
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2010-2011 Nokia Corporation
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <glib.h>
30
31 #include <dbus/dbus.h>
32
33 #include "test-utils.h"
34
35 /* This is basically a miniature dbus-daemon. We relay messages from the client
36  * on the left to the client on the right.
37  *
38  * left      socket     left      dispatch     right    socket     right
39  * client ===========>  server --------------> server ===========> client
40  * conn                 conn                   conn                conn
41  *
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.
44  */
45
46 typedef struct {
47     TestMainContext *ctx;
48     DBusError e;
49
50     DBusServer *server;
51
52     DBusConnection *left_client_conn;
53     DBusConnection *left_server_conn;
54
55     DBusConnection *right_server_conn;
56     DBusConnection *right_client_conn;
57     /* queue of DBusMessage received by right_client_conn */
58     GQueue messages;
59 } Fixture;
60
61 static void
62 assert_no_error (const DBusError *e)
63 {
64   if (G_UNLIKELY (dbus_error_is_set (e)))
65     g_error ("expected success but got error: %s: %s", e->name, e->message);
66 }
67
68 static DBusHandlerResult
69 server_message_cb (DBusConnection *server_conn,
70     DBusMessage *message,
71     void *data)
72 {
73   Fixture *f = data;
74
75   g_assert (server_conn == f->left_server_conn);
76   g_assert (f->right_server_conn != NULL);
77
78   dbus_connection_send (f->right_server_conn, message, NULL);
79
80   return DBUS_HANDLER_RESULT_HANDLED;
81 }
82
83 static DBusHandlerResult
84 right_client_message_cb (DBusConnection *client_conn,
85     DBusMessage *message,
86     void *data)
87 {
88   Fixture *f = data;
89
90   g_assert (client_conn == f->right_client_conn);
91   g_queue_push_tail (&f->messages, dbus_message_ref (message));
92
93   return DBUS_HANDLER_RESULT_HANDLED;
94 }
95
96 static void
97 new_conn_cb (DBusServer *server,
98     DBusConnection *server_conn,
99     void *data)
100 {
101   Fixture *f = data;
102   dbus_bool_t have_mem;
103
104   if (f->left_server_conn == NULL)
105     {
106       f->left_server_conn = dbus_connection_ref (server_conn);
107
108       have_mem = dbus_connection_add_filter (server_conn,
109           server_message_cb, f, NULL);
110       g_assert (have_mem);
111     }
112   else
113     {
114       g_assert (f->right_server_conn == NULL);
115       f->right_server_conn = dbus_connection_ref (server_conn);
116     }
117
118   test_connection_setup (f->ctx, server_conn);
119 }
120
121 static void
122 setup (Fixture *f,
123     gconstpointer data G_GNUC_UNUSED)
124 {
125   f->ctx = test_main_context_get ();
126   dbus_error_init (&f->e);
127   g_queue_init (&f->messages);
128
129   f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
130   assert_no_error (&f->e);
131   g_assert (f->server != NULL);
132
133   dbus_server_set_new_connection_function (f->server,
134       new_conn_cb, f, NULL);
135   test_server_setup (f->ctx, f->server);
136 }
137
138 static void
139 test_connect (Fixture *f,
140     gconstpointer data G_GNUC_UNUSED)
141 {
142   dbus_bool_t have_mem;
143   char *address;
144
145   g_assert (f->left_server_conn == NULL);
146   g_assert (f->right_server_conn == NULL);
147
148   address = dbus_server_get_address (f->server);
149   g_assert (address != NULL);
150
151   f->left_client_conn = dbus_connection_open_private (address, &f->e);
152   assert_no_error (&f->e);
153   g_assert (f->left_client_conn != NULL);
154   test_connection_setup (f->ctx, f->left_client_conn);
155
156   while (f->left_server_conn == NULL)
157     {
158       g_print (".");
159       test_main_context_iterate (f->ctx, TRUE);
160     }
161
162   f->right_client_conn = dbus_connection_open_private (address, &f->e);
163   assert_no_error (&f->e);
164   g_assert (f->right_client_conn != NULL);
165   test_connection_setup (f->ctx, f->right_client_conn);
166
167   dbus_free (address);
168
169   while (f->right_server_conn == NULL)
170     {
171       g_print (".");
172       test_main_context_iterate (f->ctx, TRUE);
173     }
174
175   have_mem = dbus_connection_add_filter (f->right_client_conn,
176       right_client_message_cb, f, NULL);
177   g_assert (have_mem);
178 }
179
180 static dbus_uint32_t
181 send_one (Fixture *f,
182     const char *member)
183 {
184   dbus_bool_t have_mem;
185   dbus_uint32_t serial;
186   DBusMessage *outgoing;
187
188   outgoing = dbus_message_new_signal ("/com/example/Hello",
189       "com.example.Hello", member);
190   g_assert (outgoing != NULL);
191
192   have_mem = dbus_connection_send (f->left_client_conn, outgoing, &serial);
193   g_assert (have_mem);
194   g_assert (serial != 0);
195
196   dbus_message_unref (outgoing);
197   return serial;
198 }
199
200 static void
201 test_relay (Fixture *f,
202     gconstpointer data)
203 {
204   DBusMessage *incoming;
205
206   test_connect (f, data);
207
208   send_one (f, "First");
209   send_one (f, "Second");
210
211   while (g_queue_get_length (&f->messages) < 2)
212     {
213       g_print (".");
214       test_main_context_iterate (f->ctx, TRUE);
215     }
216
217   g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2);
218
219   incoming = g_queue_pop_head (&f->messages);
220   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "First");
221   dbus_message_unref (incoming);
222
223   incoming = g_queue_pop_head (&f->messages);
224   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Second");
225   dbus_message_unref (incoming);
226 }
227
228 /* An arbitrary number of messages */
229 #define MANY 8192
230
231 static void
232 test_limit (Fixture *f,
233     gconstpointer data)
234 {
235   DBusMessage *incoming;
236   guint i;
237
238   test_connect (f, data);
239
240   /* This was an attempt to reproduce fd.o #34393. It didn't work. */
241   g_test_bug ("34393");
242   dbus_connection_set_max_received_size (f->left_server_conn, 1);
243   test_main_context_iterate (f->ctx, TRUE);
244
245   for (i = 0; i < MANY; i++)
246     {
247       gchar *buf = g_strdup_printf ("Message%u", i);
248
249       send_one (f, buf);
250       g_free (buf);
251     }
252
253   i = 0;
254
255   while (i < MANY)
256     {
257       while (g_queue_is_empty (&f->messages))
258         {
259           test_main_context_iterate (f->ctx, TRUE);
260         }
261
262       while ((incoming = g_queue_pop_head (&f->messages)) != NULL)
263         {
264           i++;
265           dbus_message_unref (incoming);
266         }
267     }
268 }
269
270 static void
271 teardown (Fixture *f,
272     gconstpointer data G_GNUC_UNUSED)
273 {
274   if (f->left_client_conn != NULL)
275     {
276       dbus_connection_close (f->left_client_conn);
277       dbus_connection_unref (f->left_client_conn);
278       f->left_client_conn = NULL;
279     }
280
281   if (f->right_client_conn != NULL)
282     {
283       dbus_connection_close (f->right_client_conn);
284       dbus_connection_unref (f->right_client_conn);
285       f->right_client_conn = NULL;
286     }
287
288   if (f->left_server_conn != NULL)
289     {
290       dbus_connection_close (f->left_server_conn);
291       dbus_connection_unref (f->left_server_conn);
292       f->left_server_conn = NULL;
293     }
294
295   if (f->right_server_conn != NULL)
296     {
297       dbus_connection_close (f->right_server_conn);
298       dbus_connection_unref (f->right_server_conn);
299       f->right_server_conn = NULL;
300     }
301
302   if (f->server != NULL)
303     {
304       dbus_server_disconnect (f->server);
305       dbus_server_unref (f->server);
306       f->server = NULL;
307     }
308
309   test_main_context_unref (f->ctx);
310 }
311
312 int
313 main (int argc,
314     char **argv)
315 {
316   g_test_init (&argc, &argv, NULL);
317   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
318
319   g_test_add ("/connect", Fixture, NULL, setup,
320       test_connect, teardown);
321   g_test_add ("/relay", Fixture, NULL, setup,
322       test_relay, teardown);
323   g_test_add ("/limit", Fixture, NULL, setup,
324       test_limit, teardown);
325
326   return g_test_run ();
327 }