1 /* Integration tests for the eavesdrop=true|false keyword in DBusMatchRule
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
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:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
32 #include <dbus/dbus.h>
33 #include <dbus/dbus-glib-lowlevel.h>
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"
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 "'," \
58 "member='" SENDER_SIGNAL_NAME "'"
59 #define POLITELISTENER_RULE RECEIVER_RULE
60 #define EAVESDROPPER_RULE RECEIVER_RULE ",eavesdrop=true"
62 #define STOPPER_RULE "sender='" SENDER_NAME \
63 "',interface='" SENDER_IFACE "',type='signal',member='" SENDER_STOPPER_NAME "'"
65 /* a connection received a signal to whom? */
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;
92 #define assert_no_error(e) _assert_no_error (e, __FILE__, __LINE__)
94 _assert_no_error (const DBusError *e,
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);
104 spawn_dbus_daemon (gchar *binary,
105 gchar *configuration,
108 GError *error = NULL;
115 "--print-address=1", /* stdout */
119 g_spawn_async_with_pipes (NULL, /* working directory */
122 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
123 NULL, /* child_setup */
124 NULL, /* user data */
126 NULL, /* child's stdin = /dev/null */
128 NULL, /* child's stderr = our stderr */
130 g_assert_no_error (error);
132 address = g_string_new (NULL);
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 */
143 bytes = read (address_fd, buf, sizeof (buf));
146 g_string_append_len (address, buf, bytes);
148 newline = strchr (address->str, '\n');
152 g_string_truncate (address, newline - address->str);
156 g_usleep (G_USEC_PER_SEC / 10);
159 return g_string_free (address, FALSE);
162 static DBusConnection *
163 connect_to_bus (const gchar *address)
165 DBusConnection *conn;
166 DBusError error = DBUS_ERROR_INIT;
169 conn = dbus_connection_open_private (address, &error);
170 assert_no_error (&error);
171 g_assert (conn != NULL);
173 ok = dbus_bus_register (conn, &error);
174 assert_no_error (&error);
176 g_assert (dbus_bus_get_unique_name (conn) != NULL);
178 dbus_connection_setup_with_g_main (conn, NULL);
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)
187 DBusError error = DBUS_ERROR_INIT;
190 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE,
192 dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->sender));
197 if (!dbus_connection_send (f->sender, signal, NULL))
200 dbus_message_unref (signal);
202 return DBUS_HANDLER_RESULT_HANDLED;
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)
210 DBusError error = DBUS_ERROR_INIT;
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));
219 if (!dbus_connection_send (f->sender, signal, NULL))
222 dbus_message_unref (signal);
224 return DBUS_HANDLER_RESULT_HANDLED;
227 static DBusHandlerResult
228 sender_send_broadcast (Fixture *f)
230 DBusError error = DBUS_ERROR_INIT;
233 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
234 dbus_message_set_destination (signal, NULL);
239 if (!dbus_connection_send (f->sender, signal, NULL))
242 dbus_message_unref (signal);
244 return DBUS_HANDLER_RESULT_HANDLED;
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
252 static DBusHandlerResult
253 sender_send_stopper (Fixture *f)
255 DBusError error = DBUS_ERROR_INIT;
258 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_STOPPER_NAME);
259 dbus_message_set_destination (signal, NULL);
264 if (!dbus_connection_send (f->sender, signal, NULL))
267 dbus_message_unref (signal);
269 return DBUS_HANDLER_RESULT_HANDLED;
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
276 static DBusHandlerResult
277 signal_filter (DBusConnection *connection,
278 DBusMessage *message,
281 Fixture *f = user_data;
282 SignalDst *dst = NULL;
283 DBusConnection **conn;
284 dbus_bool_t *got_stopper;
286 if (0 == strcmp (dbus_message_get_member (message), "NameAcquired"))
287 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
289 if (connection == f->receiver)
291 dst = &(f->receiver_dst);
292 conn = &(f->receiver);
293 got_stopper = &(f->receiver_got_stopper);
295 else if (connection == f->eavesdropper)
297 dst = &(f->eavesdropper_dst);
298 conn = &(f->eavesdropper);
299 got_stopper = &(f->eavesdropper_got_stopper);
301 else if (connection == f->politelistener)
303 dst = &(f->politelistener_dst);
304 conn = &(f->politelistener);
305 got_stopper = &(f->politelistener_got_stopper);
309 g_error ("connection not matching");
312 if (0 == strcmp (dbus_message_get_member (message), SENDER_SIGNAL_NAME))
314 if (dbus_message_get_destination (message) == NULL)
316 else if (0 == strcmp (dbus_message_get_destination (message), dbus_bus_get_unique_name (*conn)))
318 else /* if (dbus_message_get_destination (message) != NULL) */
321 else if (0 == strcmp (dbus_message_get_member (message), SENDER_STOPPER_NAME))
327 g_error ("got unknown member from message: %s",
328 dbus_message_get_member (message));
331 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
335 add_receiver_filter (Fixture *f)
337 DBusError e = DBUS_ERROR_INIT;
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);
344 if (!dbus_connection_add_filter (f->receiver,
345 signal_filter, f, NULL))
350 add_eavesdropper_filter (Fixture *f)
352 DBusError e = DBUS_ERROR_INIT;
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);
359 if (!dbus_connection_add_filter (f->eavesdropper,
360 signal_filter, f, NULL))
365 add_politelistener_filter (Fixture *f)
367 DBusError e = DBUS_ERROR_INIT;
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);
374 if (!dbus_connection_add_filter (f->politelistener,
375 signal_filter, f, NULL))
381 gconstpointer context G_GNUC_UNUSED)
388 dbus_error_init (&f->e);
390 dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
392 if (dbus_daemon == NULL)
393 dbus_daemon = g_strdup ("dbus-daemon");
395 if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
397 config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
398 g_getenv ("DBUS_TEST_SYSCONFDIR"));
400 else if (g_getenv ("DBUS_TEST_DATA") != NULL)
402 config = g_strdup_printf (
403 "--config-file=%s/valid-config-files/session.conf",
404 g_getenv ("DBUS_TEST_DATA"));
408 config = g_strdup ("--session");
411 address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
413 g_free (dbus_daemon);
416 f->sender = connect_to_bus (address);
417 dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
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);
430 test_eavesdrop_broadcast (Fixture *f,
431 gconstpointer context G_GNUC_UNUSED)
433 sender_send_broadcast (f);
434 sender_send_stopper (f);
436 while (!f->receiver_got_stopper ||
437 !f->politelistener_got_stopper ||
438 !f->eavesdropper_got_stopper)
439 g_main_context_iteration (NULL, TRUE);
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);
447 /* a way to say that none of the listening connection are destination of the
450 test_eavesdrop_unicast_to_sender (Fixture *f,
451 gconstpointer context G_GNUC_UNUSED)
453 sender_send_unicast_to_sender (f);
454 sender_send_stopper (f);
456 while (!f->receiver_got_stopper ||
457 !f->politelistener_got_stopper ||
458 !f->eavesdropper_got_stopper)
459 g_main_context_iteration (NULL, TRUE);
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
466 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
470 test_eavesdrop_unicast_to_receiver (Fixture *f,
471 gconstpointer context G_GNUC_UNUSED)
473 sender_send_unicast_to_receiver (f);
474 sender_send_stopper (f);
476 while (!f->receiver_got_stopper ||
477 !f->politelistener_got_stopper ||
478 !f->eavesdropper_got_stopper)
479 g_main_context_iteration (NULL, TRUE);
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
487 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
491 teardown (Fixture *f,
492 gconstpointer context G_GNUC_UNUSED)
494 dbus_error_free (&f->e);
495 g_clear_error (&f->ge);
497 if (f->sender != NULL)
499 dbus_connection_close (f->sender);
500 dbus_connection_unref (f->sender);
504 if (f->receiver != NULL)
506 dbus_connection_remove_filter (f->receiver,
509 dbus_connection_close (f->receiver);
510 dbus_connection_unref (f->receiver);
514 if (f->politelistener != NULL)
516 dbus_connection_remove_filter (f->politelistener,
519 dbus_connection_close (f->politelistener);
520 dbus_connection_unref (f->politelistener);
521 f->politelistener = NULL;
524 if (f->eavesdropper != NULL)
526 dbus_connection_remove_filter (f->eavesdropper,
529 dbus_connection_close (f->eavesdropper);
530 dbus_connection_unref (f->eavesdropper);
531 f->eavesdropper = NULL;
535 TerminateProcess (f->daemon_pid, 1);
537 kill (f->daemon_pid, SIGTERM);
540 g_spawn_close_pid (f->daemon_pid);
547 g_test_init (&argc, &argv, NULL);
548 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
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,
555 g_test_add ("/eavedrop/match_keyword/unicast_to_sender", Fixture, NULL,
556 setup, test_eavesdrop_unicast_to_sender, teardown);
558 return g_test_run ();