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