tests: In slower tests, make the timeout per-test-case
[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-glib.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   test_timeout_reset ();
126
127   f->ctx = test_main_context_get ();
128   dbus_error_init (&f->e);
129   g_queue_init (&f->messages);
130
131   f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
132   assert_no_error (&f->e);
133   g_assert (f->server != NULL);
134
135   dbus_server_set_new_connection_function (f->server,
136       new_conn_cb, f, NULL);
137   test_server_setup (f->ctx, f->server);
138 }
139
140 static void
141 test_connect (Fixture *f,
142     gconstpointer data G_GNUC_UNUSED)
143 {
144   dbus_bool_t have_mem;
145   char *address;
146
147   g_assert (f->left_server_conn == NULL);
148   g_assert (f->right_server_conn == NULL);
149
150   address = dbus_server_get_address (f->server);
151   g_assert (address != NULL);
152
153   f->left_client_conn = dbus_connection_open_private (address, &f->e);
154   assert_no_error (&f->e);
155   g_assert (f->left_client_conn != NULL);
156   test_connection_setup (f->ctx, f->left_client_conn);
157
158   while (f->left_server_conn == NULL)
159     {
160       test_progress ('.');
161       test_main_context_iterate (f->ctx, TRUE);
162     }
163
164   f->right_client_conn = dbus_connection_open_private (address, &f->e);
165   assert_no_error (&f->e);
166   g_assert (f->right_client_conn != NULL);
167   test_connection_setup (f->ctx, f->right_client_conn);
168
169   dbus_free (address);
170
171   while (f->right_server_conn == NULL)
172     {
173       test_progress ('.');
174       test_main_context_iterate (f->ctx, TRUE);
175     }
176
177   have_mem = dbus_connection_add_filter (f->right_client_conn,
178       right_client_message_cb, f, NULL);
179   g_assert (have_mem);
180 }
181
182 static dbus_uint32_t
183 send_one (Fixture *f,
184     const char *member)
185 {
186   dbus_bool_t have_mem;
187   dbus_uint32_t serial;
188   DBusMessage *outgoing;
189
190   outgoing = dbus_message_new_signal ("/com/example/Hello",
191       "com.example.Hello", member);
192   g_assert (outgoing != NULL);
193
194   have_mem = dbus_connection_send (f->left_client_conn, outgoing, &serial);
195   g_assert (have_mem);
196   g_assert (serial != 0);
197
198   dbus_message_unref (outgoing);
199   return serial;
200 }
201
202 static void
203 test_relay (Fixture *f,
204     gconstpointer data)
205 {
206   DBusMessage *incoming;
207
208   test_connect (f, data);
209
210   send_one (f, "First");
211   send_one (f, "Second");
212
213   while (g_queue_get_length (&f->messages) < 2)
214     {
215       test_progress ('.');
216       test_main_context_iterate (f->ctx, TRUE);
217     }
218
219   g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2);
220
221   incoming = g_queue_pop_head (&f->messages);
222   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "First");
223   dbus_message_unref (incoming);
224
225   incoming = g_queue_pop_head (&f->messages);
226   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Second");
227   dbus_message_unref (incoming);
228 }
229
230 /* An arbitrary number of messages */
231 #define MANY 8192
232
233 static void
234 test_limit (Fixture *f,
235     gconstpointer data)
236 {
237   DBusMessage *incoming;
238   guint i;
239
240   test_connect (f, data);
241
242   /* This was an attempt to reproduce fd.o #34393. It didn't work. */
243   g_test_bug ("34393");
244   dbus_connection_set_max_received_size (f->left_server_conn, 1);
245   test_main_context_iterate (f->ctx, TRUE);
246
247   for (i = 0; i < MANY; i++)
248     {
249       gchar *buf = g_strdup_printf ("Message%u", i);
250
251       send_one (f, buf);
252       g_free (buf);
253     }
254
255   i = 0;
256
257   while (i < MANY)
258     {
259       while (g_queue_is_empty (&f->messages))
260         {
261           test_main_context_iterate (f->ctx, TRUE);
262         }
263
264       while ((incoming = g_queue_pop_head (&f->messages)) != NULL)
265         {
266           i++;
267           dbus_message_unref (incoming);
268         }
269     }
270 }
271
272 static void
273 teardown (Fixture *f,
274     gconstpointer data G_GNUC_UNUSED)
275 {
276   if (f->left_client_conn != NULL)
277     {
278       test_connection_shutdown(NULL, f->left_client_conn);
279       dbus_connection_close (f->left_client_conn);
280       dbus_connection_unref (f->left_client_conn);
281       f->left_client_conn = NULL;
282     }
283
284   if (f->right_client_conn != NULL)
285     {
286       test_connection_shutdown(NULL, f->right_client_conn);
287       dbus_connection_close (f->right_client_conn);
288       dbus_connection_unref (f->right_client_conn);
289       f->right_client_conn = NULL;
290     }
291
292   if (f->left_server_conn != NULL)
293     {
294       test_connection_shutdown(NULL, f->left_server_conn);
295       dbus_connection_close (f->left_server_conn);
296       dbus_connection_unref (f->left_server_conn);
297       f->left_server_conn = NULL;
298     }
299
300   if (f->right_server_conn != NULL)
301     {
302       test_connection_shutdown(NULL, f->right_server_conn);
303       dbus_connection_close (f->right_server_conn);
304       dbus_connection_unref (f->right_server_conn);
305       f->right_server_conn = NULL;
306     }
307
308   if (f->server != NULL)
309     {
310       dbus_server_disconnect (f->server);
311       dbus_server_unref (f->server);
312       f->server = NULL;
313     }
314
315   test_main_context_unref (f->ctx);
316 }
317
318 int
319 main (int argc,
320     char **argv)
321 {
322   test_init (&argc, &argv);
323
324   g_test_add ("/connect", Fixture, NULL, setup,
325       test_connect, teardown);
326   g_test_add ("/relay", Fixture, NULL, setup,
327       test_relay, teardown);
328   g_test_add ("/limit", Fixture, NULL, setup,
329       test_limit, teardown);
330
331   return g_test_run ();
332 }