bus: Assign a serial number for messages from the driver
[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     gboolean skip;
50
51     DBusServer *server;
52
53     DBusConnection *left_client_conn;
54     DBusConnection *left_server_conn;
55
56     DBusConnection *right_server_conn;
57     DBusConnection *right_client_conn;
58     /* queue of DBusMessage received by right_client_conn */
59     GQueue messages;
60 } Fixture;
61
62 static void
63 assert_no_error (const DBusError *e)
64 {
65   if (G_UNLIKELY (dbus_error_is_set (e)))
66     g_error ("expected success but got error: %s: %s", e->name, e->message);
67 }
68
69 static DBusHandlerResult
70 server_message_cb (DBusConnection *server_conn,
71     DBusMessage *message,
72     void *data)
73 {
74   Fixture *f = data;
75
76   g_assert (server_conn == f->left_server_conn);
77   g_assert (f->right_server_conn != NULL);
78
79   dbus_connection_send (f->right_server_conn, message, NULL);
80
81   return DBUS_HANDLER_RESULT_HANDLED;
82 }
83
84 static DBusHandlerResult
85 right_client_message_cb (DBusConnection *client_conn,
86     DBusMessage *message,
87     void *data)
88 {
89   Fixture *f = data;
90
91   g_assert (client_conn == f->right_client_conn);
92   g_queue_push_tail (&f->messages, dbus_message_ref (message));
93
94   return DBUS_HANDLER_RESULT_HANDLED;
95 }
96
97 static void
98 new_conn_cb (DBusServer *server,
99     DBusConnection *server_conn,
100     void *data)
101 {
102   Fixture *f = data;
103   dbus_bool_t have_mem;
104
105   if (f->left_server_conn == NULL)
106     {
107       f->left_server_conn = dbus_connection_ref (server_conn);
108
109       have_mem = dbus_connection_add_filter (server_conn,
110           server_message_cb, f, NULL);
111       g_assert (have_mem);
112     }
113   else
114     {
115       g_assert (f->right_server_conn == NULL);
116       f->right_server_conn = dbus_connection_ref (server_conn);
117     }
118
119   test_connection_setup (f->ctx, server_conn);
120 }
121
122 static void
123 setup (Fixture *f,
124        gconstpointer address)
125 {
126   test_timeout_reset (1);
127
128   f->ctx = test_main_context_get ();
129   dbus_error_init (&f->e);
130   g_queue_init (&f->messages);
131
132   if ((g_str_has_prefix (address, "tcp:") ||
133        g_str_has_prefix (address, "nonce-tcp:")) &&
134       !test_check_tcp_works ())
135     {
136       f->skip = TRUE;
137       return;
138     }
139
140   f->server = dbus_server_listen (address, &f->e);
141   assert_no_error (&f->e);
142   g_assert (f->server != NULL);
143
144   dbus_server_set_new_connection_function (f->server,
145       new_conn_cb, f, NULL);
146   test_server_setup (f->ctx, f->server);
147 }
148
149 static void
150 test_connect (Fixture *f,
151     gconstpointer data G_GNUC_UNUSED)
152 {
153   dbus_bool_t have_mem;
154   char *address;
155
156   if (f->skip)
157     return;
158
159   g_assert (f->left_server_conn == NULL);
160   g_assert (f->right_server_conn == NULL);
161
162   address = dbus_server_get_address (f->server);
163   g_assert (address != NULL);
164
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);
169
170   while (f->left_server_conn == NULL)
171     {
172       test_progress ('.');
173       test_main_context_iterate (f->ctx, TRUE);
174     }
175
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);
180
181   dbus_free (address);
182
183   while (f->right_server_conn == NULL)
184     {
185       test_progress ('.');
186       test_main_context_iterate (f->ctx, TRUE);
187     }
188
189   have_mem = dbus_connection_add_filter (f->right_client_conn,
190       right_client_message_cb, f, NULL);
191   g_assert (have_mem);
192 }
193
194 static dbus_uint32_t
195 send_one (Fixture *f,
196     const char *member)
197 {
198   dbus_bool_t have_mem;
199   dbus_uint32_t serial;
200   DBusMessage *outgoing;
201
202   outgoing = dbus_message_new_signal ("/com/example/Hello",
203       "com.example.Hello", member);
204   g_assert (outgoing != NULL);
205
206   have_mem = dbus_connection_send (f->left_client_conn, outgoing, &serial);
207   g_assert (have_mem);
208   g_assert (serial != 0);
209
210   dbus_message_unref (outgoing);
211   return serial;
212 }
213
214 static void
215 test_relay (Fixture *f,
216     gconstpointer data)
217 {
218   DBusMessage *incoming;
219
220   if (f->skip)
221     return;
222
223   test_connect (f, data);
224
225   send_one (f, "First");
226   send_one (f, "Second");
227
228   while (g_queue_get_length (&f->messages) < 2)
229     {
230       test_progress ('.');
231       test_main_context_iterate (f->ctx, TRUE);
232     }
233
234   g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 2);
235
236   incoming = g_queue_pop_head (&f->messages);
237   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "First");
238   dbus_message_unref (incoming);
239
240   incoming = g_queue_pop_head (&f->messages);
241   g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Second");
242   dbus_message_unref (incoming);
243 }
244
245 /* An arbitrary number of messages */
246 #define MANY 8192
247
248 static void
249 test_limit (Fixture *f,
250     gconstpointer data)
251 {
252   DBusMessage *incoming;
253   guint i;
254
255   if (f->skip)
256     return;
257
258   test_connect (f, data);
259
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);
264
265   for (i = 0; i < MANY; i++)
266     {
267       gchar *buf = g_strdup_printf ("Message%u", i);
268
269       send_one (f, buf);
270       g_free (buf);
271     }
272
273   i = 0;
274
275   while (i < MANY)
276     {
277       while (g_queue_is_empty (&f->messages))
278         {
279           test_main_context_iterate (f->ctx, TRUE);
280         }
281
282       while ((incoming = g_queue_pop_head (&f->messages)) != NULL)
283         {
284           i++;
285           dbus_message_unref (incoming);
286         }
287     }
288 }
289
290 static void
291 teardown (Fixture *f,
292     gconstpointer data G_GNUC_UNUSED)
293 {
294   if (f->left_client_conn != NULL)
295     {
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;
300     }
301
302   if (f->right_client_conn != NULL)
303     {
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;
308     }
309
310   if (f->left_server_conn != NULL)
311     {
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;
316     }
317
318   if (f->right_server_conn != NULL)
319     {
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;
324     }
325
326   if (f->server != NULL)
327     {
328       dbus_server_disconnect (f->server);
329       dbus_server_unref (f->server);
330       f->server = NULL;
331     }
332
333   test_main_context_unref (f->ctx);
334 }
335
336 int
337 main (int argc,
338     char **argv)
339 {
340   test_init (&argc, &argv);
341
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);
348
349 #ifdef DBUS_UNIX
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);
356 #endif
357
358   return g_test_run ();
359 }