Merge branch 'dbus-1.4'
[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 <glib.h>
31
32 #include <dbus/dbus.h>
33 #include <dbus/dbus-glib-lowlevel.h>
34
35 #include <string.h>
36
37 #ifdef DBUS_WIN
38 # include <io.h>
39 # include <windows.h>
40 #else
41 # include <signal.h>
42 # include <unistd.h>
43 #endif
44
45 #define SENDER_NAME "test.eavesdrop.sender"
46 #define SENDER_PATH "/test/eavesdrop/sender"
47 #define SENDER_IFACE SENDER_NAME
48 #define SENDER_SIGNAL_NAME "Signal"
49 #define SENDER_STOPPER_NAME "Stopper"
50
51 /* This rule is equivalent to the one added to a proxy connecting to
52  * SENDER_NAME+SENDER_IFACE, plus restricting on signal name.
53  * Being more restrictive, if the connection receives what we need, for sure
54  * the original proxy rule will match it */
55 #define RECEIVER_RULE "sender='" SENDER_NAME "'," \
56   "interface='" SENDER_IFACE "'," \
57   "type='signal'," \
58   "member='" SENDER_SIGNAL_NAME "'"
59 #define POLITELISTENER_RULE RECEIVER_RULE
60 #define EAVESDROPPER_RULE RECEIVER_RULE ",eavesdrop=true"
61
62 #define STOPPER_RULE "sender='" SENDER_NAME \
63   "',interface='" SENDER_IFACE "',type='signal',member='" SENDER_STOPPER_NAME "'"
64
65 /* a connection received a signal to whom? */
66 typedef enum {
67   NONE_YET = 0,
68   TO_ME,
69   TO_OTHER,
70   BROADCAST,
71 } SignalDst;
72
73 typedef struct {
74     DBusError e;
75     GError *ge;
76
77     GPid daemon_pid;
78
79     /* eavedrop keyword tests */
80     DBusConnection *sender;
81     DBusConnection *receiver;
82     SignalDst receiver_dst;
83     dbus_bool_t receiver_got_stopper;
84     DBusConnection *eavesdropper;
85     SignalDst eavesdropper_dst;
86     dbus_bool_t eavesdropper_got_stopper;
87     DBusConnection *politelistener;
88     SignalDst politelistener_dst;
89     dbus_bool_t politelistener_got_stopper;
90 } Fixture;
91
92 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
93 static void
94 _assert_no_error (const DBusError *e,
95     const char *file,
96     int line)
97 {
98   if (G_UNLIKELY (dbus_error_is_set (e)))
99     g_error ("%s:%d: expected success but got error: %s: %s",
100         file, line, e->name, e->message);
101 }
102
103 static gchar *
104 spawn_dbus_daemon (gchar *binary,
105     gchar *configuration,
106     GPid *daemon_pid)
107 {
108   GError *error = NULL;
109   GString *address;
110   gint address_fd;
111   gchar *argv[] = {
112       binary,
113       configuration,
114       "--nofork",
115       "--print-address=1", /* stdout */
116       NULL
117   };
118
119   g_spawn_async_with_pipes (NULL, /* working directory */
120       argv,
121       NULL, /* envp */
122       G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
123       NULL, /* child_setup */
124       NULL, /* user data */
125       daemon_pid,
126       NULL, /* child's stdin = /dev/null */
127       &address_fd,
128       NULL, /* child's stderr = our stderr */
129       &error);
130   g_assert_no_error (error);
131
132   address = g_string_new (NULL);
133
134   /* polling until the dbus-daemon writes out its address is a bit stupid,
135    * but at least it's simple, unlike dbus-launch... in principle we could
136    * use select() here, but life's too short */
137   while (1)
138     {
139       gssize bytes;
140       gchar buf[4096];
141       gchar *newline;
142
143       bytes = read (address_fd, buf, sizeof (buf));
144
145       if (bytes > 0)
146         g_string_append_len (address, buf, bytes);
147
148       newline = strchr (address->str, '\n');
149
150       if (newline != NULL)
151         {
152           g_string_truncate (address, newline - address->str);
153           break;
154         }
155
156       g_usleep (G_USEC_PER_SEC / 10);
157     }
158
159   return g_string_free (address, FALSE);
160 }
161
162 static DBusConnection *
163 connect_to_bus (const gchar *address)
164 {
165   DBusConnection *conn;
166   DBusError error = DBUS_ERROR_INIT;
167   dbus_bool_t ok;
168
169   conn = dbus_connection_open_private (address, &error);
170   assert_no_error (&error);
171   g_assert (conn != NULL);
172
173   ok = dbus_bus_register (conn, &error);
174   assert_no_error (&error);
175   g_assert (ok);
176   g_assert (dbus_bus_get_unique_name (conn) != NULL);
177
178   dbus_connection_setup_with_g_main (conn, NULL);
179   return conn;
180 }
181
182 /* send a unicast signal to <self> to ensure that no other connection
183  * listening is the actual recipient for the signal */
184 static DBusHandlerResult
185 sender_send_unicast_to_sender (Fixture *f)
186 {
187   DBusError error = DBUS_ERROR_INIT;
188   DBusMessage *signal;
189
190   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE,
191       SENDER_SIGNAL_NAME);
192   dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->sender));
193
194   if (signal == NULL)
195     g_error ("OOM");
196
197   if (!dbus_connection_send (f->sender, signal, NULL))
198     g_error ("OOM");
199
200   dbus_message_unref (signal);
201
202   return DBUS_HANDLER_RESULT_HANDLED;
203 }
204
205 /* send a unicast signal to <receiver>, making <politelistener> and
206  * <eavesdropper> not a actual recipient for it */
207 static DBusHandlerResult
208 sender_send_unicast_to_receiver (Fixture *f)
209 {
210   DBusError error = DBUS_ERROR_INIT;
211   DBusMessage *signal;
212
213   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
214   dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->receiver));
215
216   if (signal == NULL)
217     g_error ("OOM");
218
219   if (!dbus_connection_send (f->sender, signal, NULL))
220     g_error ("OOM");
221
222   dbus_message_unref (signal);
223
224   return DBUS_HANDLER_RESULT_HANDLED;
225 }
226
227 static DBusHandlerResult
228 sender_send_broadcast (Fixture *f)
229 {
230   DBusError error = DBUS_ERROR_INIT;
231   DBusMessage *signal;
232
233   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
234   dbus_message_set_destination (signal, NULL);
235
236   if (signal == NULL)
237     g_error ("OOM");
238
239   if (!dbus_connection_send (f->sender, signal, NULL))
240     g_error ("OOM");
241
242   dbus_message_unref (signal);
243
244   return DBUS_HANDLER_RESULT_HANDLED;
245 }
246
247 /* Send special broadcast signal to indicate that the connections can "stop"
248  * listening and check their results.
249  * DBus does not re-order messages, so when the three connections have received
250  * this signal, we are sure that any message sent before it has also been
251  * dispatched. */
252 static DBusHandlerResult
253 sender_send_stopper (Fixture *f)
254 {
255   DBusError error = DBUS_ERROR_INIT;
256   DBusMessage *signal;
257
258   signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_STOPPER_NAME);
259   dbus_message_set_destination (signal, NULL);
260
261   if (signal == NULL)
262     g_error ("OOM");
263
264   if (!dbus_connection_send (f->sender, signal, NULL))
265     g_error ("OOM");
266
267   dbus_message_unref (signal);
268
269   return DBUS_HANDLER_RESULT_HANDLED;
270 }
271
272 /* Ignore NameAcquired, then depending on the signal received:
273  * - updates f-><conn>_dst based on the destination of the message
274  * - asserts that <conn> received the stop signal
275  */
276 static DBusHandlerResult
277 signal_filter (DBusConnection *connection,
278     DBusMessage *message,
279     void *user_data)
280 {
281   Fixture *f = user_data;
282   SignalDst *dst = NULL;
283   DBusConnection **conn;
284   dbus_bool_t *got_stopper;
285
286   if (0 == strcmp (dbus_message_get_member (message), "NameAcquired"))
287     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288
289   if (connection == f->receiver)
290     {
291       dst = &(f->receiver_dst);
292       conn = &(f->receiver);
293       got_stopper = &(f->receiver_got_stopper);
294     }
295   else if (connection == f->eavesdropper)
296     {
297       dst = &(f->eavesdropper_dst);
298       conn = &(f->eavesdropper);
299       got_stopper = &(f->eavesdropper_got_stopper);
300     }
301   else if (connection == f->politelistener)
302     {
303       dst = &(f->politelistener_dst);
304       conn = &(f->politelistener);
305       got_stopper = &(f->politelistener_got_stopper);
306     }
307   else
308     {
309       g_error ("connection not matching");
310     }
311
312   if (0 == strcmp (dbus_message_get_member (message), SENDER_SIGNAL_NAME))
313     {
314       if (dbus_message_get_destination (message) == NULL)
315         *dst = BROADCAST;
316       else if (0 == strcmp (dbus_message_get_destination (message), dbus_bus_get_unique_name (*conn)))
317         *dst = TO_ME;
318       else /* if (dbus_message_get_destination (message) != NULL) */
319         *dst = TO_OTHER;
320     }
321   else if (0 == strcmp (dbus_message_get_member (message), SENDER_STOPPER_NAME))
322     {
323       *got_stopper = TRUE;
324     }
325   else
326     {
327       g_error ("got unknown member from message: %s",
328           dbus_message_get_member (message));
329     }
330
331   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
332 }
333
334 static void
335 add_receiver_filter (Fixture *f)
336 {
337   DBusError e = DBUS_ERROR_INIT;
338
339   dbus_bus_add_match (f->receiver, RECEIVER_RULE, &e);
340   assert_no_error (&e);
341   dbus_bus_add_match (f->receiver, STOPPER_RULE, &e);
342   assert_no_error (&e);
343
344   if (!dbus_connection_add_filter (f->receiver,
345         signal_filter, f, NULL))
346     g_error ("OOM");
347 }
348
349 static void
350 add_eavesdropper_filter (Fixture *f)
351 {
352   DBusError e = DBUS_ERROR_INIT;
353
354   dbus_bus_add_match (f->eavesdropper, EAVESDROPPER_RULE, &e);
355   assert_no_error (&e);
356   dbus_bus_add_match (f->eavesdropper, STOPPER_RULE, &e);
357   assert_no_error (&e);
358
359   if (!dbus_connection_add_filter (f->eavesdropper,
360         signal_filter, f, NULL))
361     g_error ("OOM");
362 }
363
364 static void
365 add_politelistener_filter (Fixture *f)
366 {
367   DBusError e = DBUS_ERROR_INIT;
368
369   dbus_bus_add_match (f->politelistener, POLITELISTENER_RULE, &e);
370   assert_no_error (&e);
371   dbus_bus_add_match (f->politelistener, STOPPER_RULE, &e);
372   assert_no_error (&e);
373
374   if (!dbus_connection_add_filter (f->politelistener,
375         signal_filter, f, NULL))
376     g_error ("OOM");
377 }
378
379 static void
380 setup (Fixture *f,
381     gconstpointer context G_GNUC_UNUSED)
382 {
383   gchar *dbus_daemon;
384   gchar *config;
385   gchar *address;
386
387   f->ge = NULL;
388   dbus_error_init (&f->e);
389
390   dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
391
392   if (dbus_daemon == NULL)
393     dbus_daemon = g_strdup ("dbus-daemon");
394
395   if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
396     {
397       config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
398           g_getenv ("DBUS_TEST_SYSCONFDIR"));
399     }
400   else if (g_getenv ("DBUS_TEST_DATA") != NULL)
401     {
402       config = g_strdup_printf (
403           "--config-file=%s/valid-config-files/session.conf",
404           g_getenv ("DBUS_TEST_DATA"));
405     }
406   else
407     {
408       config = g_strdup ("--session");
409     }
410
411   address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
412
413   g_free (dbus_daemon);
414   g_free (config);
415
416   f->sender = connect_to_bus (address);
417   dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
418       &(f->e));
419   f->receiver = connect_to_bus (address);
420   f->eavesdropper = connect_to_bus (address);
421   f->politelistener = connect_to_bus (address);
422   add_receiver_filter (f);
423   add_politelistener_filter (f);
424   add_eavesdropper_filter (f);
425
426   g_free (address);
427 }
428
429 static void
430 test_eavesdrop_broadcast (Fixture *f,
431     gconstpointer context G_GNUC_UNUSED)
432 {
433   sender_send_broadcast (f);
434   sender_send_stopper (f);
435
436   while (!f->receiver_got_stopper ||
437       !f->politelistener_got_stopper ||
438       !f->eavesdropper_got_stopper)
439     g_main_context_iteration (NULL, TRUE);
440
441   /* all the three connection can receive a broadcast */
442   g_assert_cmpint (f->receiver_dst, ==, BROADCAST);
443   g_assert_cmpint (f->politelistener_dst, ==, BROADCAST);
444   g_assert_cmpint (f->eavesdropper_dst, ==, BROADCAST);
445 }
446
447 /* a way to say that none of the listening connection are destination of the
448  * signal */
449 static void
450 test_eavesdrop_unicast_to_sender (Fixture *f,
451     gconstpointer context G_GNUC_UNUSED)
452 {
453   sender_send_unicast_to_sender (f);
454   sender_send_stopper (f);
455
456   while (!f->receiver_got_stopper ||
457       !f->politelistener_got_stopper ||
458       !f->eavesdropper_got_stopper)
459     g_main_context_iteration (NULL, TRUE);
460
461   /* not directed to it and not broadcasted, they cannot receive it */
462   g_assert_cmpint (f->receiver_dst, ==, NONE_YET);
463   g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
464   /* eavesdrop=true, it will receive the signal even though it's not directed
465    * to it */
466   g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
467 }
468
469 static void
470 test_eavesdrop_unicast_to_receiver (Fixture *f,
471     gconstpointer context G_GNUC_UNUSED)
472 {
473   sender_send_unicast_to_receiver (f);
474   sender_send_stopper (f);
475
476   while (!f->receiver_got_stopper ||
477       !f->politelistener_got_stopper ||
478       !f->eavesdropper_got_stopper)
479     g_main_context_iteration (NULL, TRUE);
480
481   /* direct to him */
482   g_assert_cmpint (f->receiver_dst, ==, TO_ME);
483   /* not directed to it and not broadcasted, it cannot receive it */
484   g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
485   /* eavesdrop=true, it will receive the signal even though it's not directed
486    * to it */
487   g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
488 }
489
490 static void
491 teardown (Fixture *f,
492     gconstpointer context G_GNUC_UNUSED)
493 {
494   dbus_error_free (&f->e);
495   g_clear_error (&f->ge);
496
497   if (f->sender != NULL)
498     {
499       dbus_connection_close (f->sender);
500       dbus_connection_unref (f->sender);
501       f->sender = NULL;
502     }
503
504   if (f->receiver != NULL)
505     {
506       dbus_connection_remove_filter (f->receiver,
507           signal_filter, f);
508
509       dbus_connection_close (f->receiver);
510       dbus_connection_unref (f->receiver);
511       f->receiver = NULL;
512     }
513
514   if (f->politelistener != NULL)
515     {
516       dbus_connection_remove_filter (f->politelistener,
517           signal_filter, f);
518
519       dbus_connection_close (f->politelistener);
520       dbus_connection_unref (f->politelistener);
521       f->politelistener = NULL;
522     }
523
524   if (f->eavesdropper != NULL)
525     {
526       dbus_connection_remove_filter (f->eavesdropper,
527           signal_filter, f);
528
529       dbus_connection_close (f->eavesdropper);
530       dbus_connection_unref (f->eavesdropper);
531       f->eavesdropper = NULL;
532     }
533
534 #ifdef DBUS_WIN
535   TerminateProcess (f->daemon_pid, 1);
536 #else
537   kill (f->daemon_pid, SIGTERM);
538 #endif
539
540   g_spawn_close_pid (f->daemon_pid);
541 }
542
543 int
544 main (int argc,
545     char **argv)
546 {
547   g_test_init (&argc, &argv, NULL);
548   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
549
550   g_test_add ("/eavedrop/match_keyword/broadcast", Fixture, NULL,
551       setup, test_eavesdrop_broadcast, teardown);
552   g_test_add ("/eavedrop/match_keyword/unicast_to_receiver", Fixture, NULL,
553       setup, test_eavesdrop_unicast_to_receiver,
554       teardown);
555   g_test_add ("/eavedrop/match_keyword/unicast_to_sender", Fixture, NULL,
556       setup, test_eavesdrop_unicast_to_sender, teardown);
557
558   return g_test_run ();
559 }