GVariant: fix dbus_message_copy()
[platform/upstream/dbus.git] / test / dbus-daemon-eavesdrop.c
1 /* Integration tests for the eavesdrop=true|false keyword in DBusMatchRule
2  *
3  * Author: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
4  * Based on: tests/dbus-daemon.c by Simon McVittie
5  * Copyright © 2010-2011 Nokia Corporation
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction,
10  * including without limitation the rights to use, copy, modify, merge,
11  * publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so,
13  * subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 #include <config.h>
29
30 #include <string.h>
31
32 #include "test-utils-glib.h"
33
34 #define SENDER_NAME "test.eavesdrop.sender"
35 #define SENDER_PATH "/test/eavesdrop/sender"
36 #define SENDER_IFACE SENDER_NAME
37 #define SENDER_SIGNAL_NAME "Signal"
38 #define SENDER_STOPPER_NAME "Stopper"
39
40 /* This rule is equivalent to the one added to a proxy connecting to
41  * SENDER_NAME+SENDER_IFACE, plus restricting on signal name.
42  * Being more restrictive, if the connection receives what we need, for sure
43  * the original proxy rule will match it */
44 #define RECEIVER_RULE "sender='" SENDER_NAME "'," \
45   "interface='" SENDER_IFACE "'," \
46   "type='signal'," \
47   "member='" SENDER_SIGNAL_NAME "'"
48 #define POLITELISTENER_RULE RECEIVER_RULE
49 #define EAVESDROPPER_RULE RECEIVER_RULE ",eavesdrop=true"
50
51 #define STOPPER_RULE "sender='" SENDER_NAME \
52   "',interface='" SENDER_IFACE "',type='signal',member='" SENDER_STOPPER_NAME "'"
53
54 /* a connection received a signal to whom? */
55 typedef enum {
56   NONE_YET = 0,
57   TO_ME,
58   TO_OTHER,
59   BROADCAST,
60 } SignalDst;
61
62 typedef struct {
63     TestMainContext *ctx;
64     DBusError e;
65     GError *ge;
66
67     GPid daemon_pid;
68
69     /* eavedrop keyword tests */
70     DBusConnection *sender;
71     DBusConnection *receiver;
72     SignalDst receiver_dst;
73     dbus_bool_t receiver_got_stopper;
74     DBusConnection *eavesdropper;
75     SignalDst eavesdropper_dst;
76     dbus_bool_t eavesdropper_got_stopper;
77     DBusConnection *politelistener;
78     SignalDst politelistener_dst;
79     dbus_bool_t politelistener_got_stopper;
80 } Fixture;
81
82 /* send a unicast signal to <self> to ensure that no other connection
83  * listening is the actual recipient for the signal */
84 static DBusHandlerResult
85 sender_send_unicast_to_sender (Fixture *f)
86 {
87   DBusMessage *signal;
88
89   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE,
90       SENDER_SIGNAL_NAME);
91   if (signal == NULL)
92     g_error ("OOM");
93
94   if (!dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->sender)))
95     g_error ("OOM");
96
97   if (!dbus_connection_send (f->sender, signal, NULL))
98     g_error ("OOM");
99
100   dbus_message_unref (signal);
101
102   return DBUS_HANDLER_RESULT_HANDLED;
103 }
104
105 /* send a unicast signal to <receiver>, making <politelistener> and
106  * <eavesdropper> not a actual recipient for it */
107 static DBusHandlerResult
108 sender_send_unicast_to_receiver (Fixture *f)
109 {
110   DBusMessage *signal;
111
112   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
113   if (signal == NULL)
114     g_error ("OOM");
115
116   if (!dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->receiver)))
117     g_error ("OOM");
118
119   if (!dbus_connection_send (f->sender, signal, NULL))
120     g_error ("OOM");
121
122   dbus_message_unref (signal);
123
124   return DBUS_HANDLER_RESULT_HANDLED;
125 }
126
127 static DBusHandlerResult
128 sender_send_broadcast (Fixture *f)
129 {
130   DBusMessage *signal;
131
132   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
133   if (signal == NULL)
134     g_error ("OOM");
135
136   if (!dbus_message_set_destination (signal, NULL))
137     g_error ("OOM");
138
139   if (!dbus_connection_send (f->sender, signal, NULL))
140     g_error ("OOM");
141
142   dbus_message_unref (signal);
143
144   return DBUS_HANDLER_RESULT_HANDLED;
145 }
146
147 /* Send special broadcast signal to indicate that the connections can "stop"
148  * listening and check their results.
149  * DBus does not re-order messages, so when the three connections have received
150  * this signal, we are sure that any message sent before it has also been
151  * dispatched. */
152 static DBusHandlerResult
153 sender_send_stopper (Fixture *f)
154 {
155   DBusMessage *signal;
156
157   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_STOPPER_NAME);
158   if (signal == NULL)
159     g_error ("OOM");
160
161   if (!dbus_message_set_destination (signal, NULL))
162     g_error ("OOM");
163
164   if (!dbus_connection_send (f->sender, signal, NULL))
165     g_error ("OOM");
166
167   dbus_message_unref (signal);
168
169   return DBUS_HANDLER_RESULT_HANDLED;
170 }
171
172 /* Ignore NameAcquired, then depending on the signal received:
173  * - updates f-><conn>_dst based on the destination of the message
174  * - asserts that <conn> received the stop signal
175  */
176 static DBusHandlerResult
177 signal_filter (DBusConnection *connection,
178     DBusMessage *message,
179     void *user_data)
180 {
181   Fixture *f = user_data;
182   SignalDst *dst = NULL;
183   DBusConnection **conn;
184   dbus_bool_t *got_stopper;
185
186   if (0 == strcmp (dbus_message_get_member (message), "NameAcquired"))
187     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
188
189   if (connection == f->receiver)
190     {
191       dst = &(f->receiver_dst);
192       conn = &(f->receiver);
193       got_stopper = &(f->receiver_got_stopper);
194     }
195   else if (connection == f->eavesdropper)
196     {
197       dst = &(f->eavesdropper_dst);
198       conn = &(f->eavesdropper);
199       got_stopper = &(f->eavesdropper_got_stopper);
200     }
201   else if (connection == f->politelistener)
202     {
203       dst = &(f->politelistener_dst);
204       conn = &(f->politelistener);
205       got_stopper = &(f->politelistener_got_stopper);
206     }
207   else
208     {
209       g_error ("connection not matching");
210     }
211
212   if (0 == strcmp (dbus_message_get_member (message), SENDER_SIGNAL_NAME))
213     {
214       if (dbus_message_get_destination (message) == NULL)
215         *dst = BROADCAST;
216       else if (0 == strcmp (dbus_message_get_destination (message), dbus_bus_get_unique_name (*conn)))
217         *dst = TO_ME;
218       else /* if (dbus_message_get_destination (message) != NULL) */
219         *dst = TO_OTHER;
220     }
221   else if (0 == strcmp (dbus_message_get_member (message), SENDER_STOPPER_NAME))
222     {
223       *got_stopper = TRUE;
224     }
225   else
226     {
227       g_error ("got unknown member from message: %s",
228           dbus_message_get_member (message));
229     }
230
231   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
232 }
233
234 static void
235 add_receiver_filter (Fixture *f)
236 {
237   DBusError e = DBUS_ERROR_INIT;
238
239   dbus_bus_add_match (f->receiver, RECEIVER_RULE, &e);
240   test_assert_no_error (&e);
241   dbus_bus_add_match (f->receiver, STOPPER_RULE, &e);
242   test_assert_no_error (&e);
243
244   if (!dbus_connection_add_filter (f->receiver,
245         signal_filter, f, NULL))
246     g_error ("OOM");
247 }
248
249 static void
250 add_eavesdropper_filter (Fixture *f)
251 {
252   DBusError e = DBUS_ERROR_INIT;
253
254   dbus_bus_add_match (f->eavesdropper, EAVESDROPPER_RULE, &e);
255   test_assert_no_error (&e);
256   dbus_bus_add_match (f->eavesdropper, STOPPER_RULE, &e);
257   test_assert_no_error (&e);
258
259   if (!dbus_connection_add_filter (f->eavesdropper,
260         signal_filter, f, NULL))
261     g_error ("OOM");
262 }
263
264 static void
265 add_politelistener_filter (Fixture *f)
266 {
267   DBusError e = DBUS_ERROR_INIT;
268
269   dbus_bus_add_match (f->politelistener, POLITELISTENER_RULE, &e);
270   test_assert_no_error (&e);
271   dbus_bus_add_match (f->politelistener, STOPPER_RULE, &e);
272   test_assert_no_error (&e);
273
274   if (!dbus_connection_add_filter (f->politelistener,
275         signal_filter, f, NULL))
276     g_error ("OOM");
277 }
278
279 static void
280 setup (Fixture *f,
281     gconstpointer context G_GNUC_UNUSED)
282 {
283   gchar *address;
284
285   f->ctx = test_main_context_get ();
286
287   f->ge = NULL;
288   dbus_error_init (&f->e);
289
290   address = test_get_dbus_daemon (NULL, TEST_USER_ME, NULL, &f->daemon_pid);
291
292   f->sender = test_connect_to_bus (f->ctx, address);
293   dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
294       &(f->e));
295   f->receiver = test_connect_to_bus (f->ctx, address);
296   f->eavesdropper = test_connect_to_bus (f->ctx, address);
297   f->politelistener = test_connect_to_bus (f->ctx, address);
298   add_receiver_filter (f);
299   add_politelistener_filter (f);
300   add_eavesdropper_filter (f);
301
302   g_free (address);
303 }
304
305 static void
306 test_eavesdrop_broadcast (Fixture *f,
307     gconstpointer context G_GNUC_UNUSED)
308 {
309   sender_send_broadcast (f);
310   sender_send_stopper (f);
311
312   while (!f->receiver_got_stopper ||
313       !f->politelistener_got_stopper ||
314       !f->eavesdropper_got_stopper)
315     test_main_context_iterate (f->ctx, TRUE);
316
317   /* all the three connection can receive a broadcast */
318   g_assert_cmpint (f->receiver_dst, ==, BROADCAST);
319   g_assert_cmpint (f->politelistener_dst, ==, BROADCAST);
320   g_assert_cmpint (f->eavesdropper_dst, ==, BROADCAST);
321 }
322
323 /* a way to say that none of the listening connection are destination of the
324  * signal */
325 static void
326 test_eavesdrop_unicast_to_sender (Fixture *f,
327     gconstpointer context G_GNUC_UNUSED)
328 {
329   sender_send_unicast_to_sender (f);
330   sender_send_stopper (f);
331
332   while (!f->receiver_got_stopper ||
333       !f->politelistener_got_stopper ||
334       !f->eavesdropper_got_stopper)
335     test_main_context_iterate (f->ctx, TRUE);
336
337   /* not directed to it and not broadcasted, they cannot receive it */
338   g_assert_cmpint (f->receiver_dst, ==, NONE_YET);
339   g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
340   /* eavesdrop=true, it will receive the signal even though it's not directed
341    * to it */
342   g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
343 }
344
345 static void
346 test_eavesdrop_unicast_to_receiver (Fixture *f,
347     gconstpointer context G_GNUC_UNUSED)
348 {
349   sender_send_unicast_to_receiver (f);
350   sender_send_stopper (f);
351
352   while (!f->receiver_got_stopper ||
353       !f->politelistener_got_stopper ||
354       !f->eavesdropper_got_stopper)
355     test_main_context_iterate (f->ctx, TRUE);
356
357   /* direct to him */
358   g_assert_cmpint (f->receiver_dst, ==, TO_ME);
359   /* not directed to it and not broadcasted, it cannot receive it */
360   g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
361   /* eavesdrop=true, it will receive the signal even though it's not directed
362    * to it */
363   g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
364 }
365
366 static void
367 teardown (Fixture *f,
368     gconstpointer context G_GNUC_UNUSED)
369 {
370   dbus_error_free (&f->e);
371   g_clear_error (&f->ge);
372
373   if (f->sender != NULL)
374     {
375       dbus_connection_close (f->sender);
376       dbus_connection_unref (f->sender);
377       f->sender = NULL;
378     }
379
380   if (f->receiver != NULL)
381     {
382       dbus_connection_remove_filter (f->receiver,
383           signal_filter, f);
384
385       dbus_connection_close (f->receiver);
386       dbus_connection_unref (f->receiver);
387       f->receiver = NULL;
388     }
389
390   if (f->politelistener != NULL)
391     {
392       dbus_connection_remove_filter (f->politelistener,
393           signal_filter, f);
394
395       dbus_connection_close (f->politelistener);
396       dbus_connection_unref (f->politelistener);
397       f->politelistener = NULL;
398     }
399
400   if (f->eavesdropper != NULL)
401     {
402       dbus_connection_remove_filter (f->eavesdropper,
403           signal_filter, f);
404
405       dbus_connection_close (f->eavesdropper);
406       dbus_connection_unref (f->eavesdropper);
407       f->eavesdropper = NULL;
408     }
409
410   if (f->daemon_pid != 0)
411     {
412       test_kill_pid (f->daemon_pid);
413       g_spawn_close_pid (f->daemon_pid);
414       f->daemon_pid = 0;
415     }
416
417   test_main_context_unref (f->ctx);
418 }
419
420 int
421 main (int argc,
422     char **argv)
423 {
424   test_init (&argc, &argv);
425
426   g_test_add ("/eavedrop/match_keyword/broadcast", Fixture, NULL,
427       setup, test_eavesdrop_broadcast, teardown);
428   g_test_add ("/eavedrop/match_keyword/unicast_to_receiver", Fixture, NULL,
429       setup, test_eavesdrop_unicast_to_receiver,
430       teardown);
431   g_test_add ("/eavedrop/match_keyword/unicast_to_sender", Fixture, NULL,
432       setup, test_eavesdrop_unicast_to_sender, teardown);
433
434   return g_test_run ();
435 }