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)
189 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE,
191 dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->sender));
196 if (!dbus_connection_send (f->sender, signal, NULL))
199 dbus_message_unref (signal);
201 return DBUS_HANDLER_RESULT_HANDLED;
204 /* send a unicast signal to <receiver>, making <politelistener> and
205 * <eavesdropper> not a actual recipient for it */
206 static DBusHandlerResult
207 sender_send_unicast_to_receiver (Fixture *f)
211 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
212 dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->receiver));
217 if (!dbus_connection_send (f->sender, signal, NULL))
220 dbus_message_unref (signal);
222 return DBUS_HANDLER_RESULT_HANDLED;
225 static DBusHandlerResult
226 sender_send_broadcast (Fixture *f)
230 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
231 dbus_message_set_destination (signal, NULL);
236 if (!dbus_connection_send (f->sender, signal, NULL))
239 dbus_message_unref (signal);
241 return DBUS_HANDLER_RESULT_HANDLED;
244 /* Send special broadcast signal to indicate that the connections can "stop"
245 * listening and check their results.
246 * DBus does not re-order messages, so when the three connections have received
247 * this signal, we are sure that any message sent before it has also been
249 static DBusHandlerResult
250 sender_send_stopper (Fixture *f)
254 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_STOPPER_NAME);
255 dbus_message_set_destination (signal, NULL);
260 if (!dbus_connection_send (f->sender, signal, NULL))
263 dbus_message_unref (signal);
265 return DBUS_HANDLER_RESULT_HANDLED;
268 /* Ignore NameAcquired, then depending on the signal received:
269 * - updates f-><conn>_dst based on the destination of the message
270 * - asserts that <conn> received the stop signal
272 static DBusHandlerResult
273 signal_filter (DBusConnection *connection,
274 DBusMessage *message,
277 Fixture *f = user_data;
278 SignalDst *dst = NULL;
279 DBusConnection **conn;
280 dbus_bool_t *got_stopper;
282 if (0 == strcmp (dbus_message_get_member (message), "NameAcquired"))
283 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
285 if (connection == f->receiver)
287 dst = &(f->receiver_dst);
288 conn = &(f->receiver);
289 got_stopper = &(f->receiver_got_stopper);
291 else if (connection == f->eavesdropper)
293 dst = &(f->eavesdropper_dst);
294 conn = &(f->eavesdropper);
295 got_stopper = &(f->eavesdropper_got_stopper);
297 else if (connection == f->politelistener)
299 dst = &(f->politelistener_dst);
300 conn = &(f->politelistener);
301 got_stopper = &(f->politelistener_got_stopper);
305 g_error ("connection not matching");
308 if (0 == strcmp (dbus_message_get_member (message), SENDER_SIGNAL_NAME))
310 if (dbus_message_get_destination (message) == NULL)
312 else if (0 == strcmp (dbus_message_get_destination (message), dbus_bus_get_unique_name (*conn)))
314 else /* if (dbus_message_get_destination (message) != NULL) */
317 else if (0 == strcmp (dbus_message_get_member (message), SENDER_STOPPER_NAME))
323 g_error ("got unknown member from message: %s",
324 dbus_message_get_member (message));
327 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
331 add_receiver_filter (Fixture *f)
333 DBusError e = DBUS_ERROR_INIT;
335 dbus_bus_add_match (f->receiver, RECEIVER_RULE, &e);
336 assert_no_error (&e);
337 dbus_bus_add_match (f->receiver, STOPPER_RULE, &e);
338 assert_no_error (&e);
340 if (!dbus_connection_add_filter (f->receiver,
341 signal_filter, f, NULL))
346 add_eavesdropper_filter (Fixture *f)
348 DBusError e = DBUS_ERROR_INIT;
350 dbus_bus_add_match (f->eavesdropper, EAVESDROPPER_RULE, &e);
351 assert_no_error (&e);
352 dbus_bus_add_match (f->eavesdropper, STOPPER_RULE, &e);
353 assert_no_error (&e);
355 if (!dbus_connection_add_filter (f->eavesdropper,
356 signal_filter, f, NULL))
361 add_politelistener_filter (Fixture *f)
363 DBusError e = DBUS_ERROR_INIT;
365 dbus_bus_add_match (f->politelistener, POLITELISTENER_RULE, &e);
366 assert_no_error (&e);
367 dbus_bus_add_match (f->politelistener, STOPPER_RULE, &e);
368 assert_no_error (&e);
370 if (!dbus_connection_add_filter (f->politelistener,
371 signal_filter, f, NULL))
377 gconstpointer context G_GNUC_UNUSED)
384 dbus_error_init (&f->e);
386 dbus_daemon = g_strdup (g_getenv ("DBUS_TEST_DAEMON"));
388 if (dbus_daemon == NULL)
389 dbus_daemon = g_strdup ("dbus-daemon");
391 if (g_getenv ("DBUS_TEST_SYSCONFDIR") != NULL)
393 config = g_strdup_printf ("--config-file=%s/dbus-1/session.conf",
394 g_getenv ("DBUS_TEST_SYSCONFDIR"));
396 else if (g_getenv ("DBUS_TEST_DATA") != NULL)
398 config = g_strdup_printf (
399 "--config-file=%s/valid-config-files/session.conf",
400 g_getenv ("DBUS_TEST_DATA"));
404 config = g_strdup ("--session");
407 address = spawn_dbus_daemon (dbus_daemon, config, &f->daemon_pid);
409 g_free (dbus_daemon);
412 f->sender = connect_to_bus (address);
413 dbus_bus_request_name (f->sender, SENDER_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE,
415 f->receiver = connect_to_bus (address);
416 f->eavesdropper = connect_to_bus (address);
417 f->politelistener = connect_to_bus (address);
418 add_receiver_filter (f);
419 add_politelistener_filter (f);
420 add_eavesdropper_filter (f);
426 test_eavesdrop_broadcast (Fixture *f,
427 gconstpointer context G_GNUC_UNUSED)
429 sender_send_broadcast (f);
430 sender_send_stopper (f);
432 while (!f->receiver_got_stopper ||
433 !f->politelistener_got_stopper ||
434 !f->eavesdropper_got_stopper)
435 g_main_context_iteration (NULL, TRUE);
437 /* all the three connection can receive a broadcast */
438 g_assert_cmpint (f->receiver_dst, ==, BROADCAST);
439 g_assert_cmpint (f->politelistener_dst, ==, BROADCAST);
440 g_assert_cmpint (f->eavesdropper_dst, ==, BROADCAST);
443 /* a way to say that none of the listening connection are destination of the
446 test_eavesdrop_unicast_to_sender (Fixture *f,
447 gconstpointer context G_GNUC_UNUSED)
449 sender_send_unicast_to_sender (f);
450 sender_send_stopper (f);
452 while (!f->receiver_got_stopper ||
453 !f->politelistener_got_stopper ||
454 !f->eavesdropper_got_stopper)
455 g_main_context_iteration (NULL, TRUE);
457 /* not directed to it and not broadcasted, they cannot receive it */
458 g_assert_cmpint (f->receiver_dst, ==, NONE_YET);
459 g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
460 /* eavesdrop=true, it will receive the signal even though it's not directed
462 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
466 test_eavesdrop_unicast_to_receiver (Fixture *f,
467 gconstpointer context G_GNUC_UNUSED)
469 sender_send_unicast_to_receiver (f);
470 sender_send_stopper (f);
472 while (!f->receiver_got_stopper ||
473 !f->politelistener_got_stopper ||
474 !f->eavesdropper_got_stopper)
475 g_main_context_iteration (NULL, TRUE);
478 g_assert_cmpint (f->receiver_dst, ==, TO_ME);
479 /* not directed to it and not broadcasted, it cannot receive it */
480 g_assert_cmpint (f->politelistener_dst, ==, NONE_YET);
481 /* eavesdrop=true, it will receive the signal even though it's not directed
483 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
487 teardown (Fixture *f,
488 gconstpointer context G_GNUC_UNUSED)
490 dbus_error_free (&f->e);
491 g_clear_error (&f->ge);
493 if (f->sender != NULL)
495 dbus_connection_close (f->sender);
496 dbus_connection_unref (f->sender);
500 if (f->receiver != NULL)
502 dbus_connection_remove_filter (f->receiver,
505 dbus_connection_close (f->receiver);
506 dbus_connection_unref (f->receiver);
510 if (f->politelistener != NULL)
512 dbus_connection_remove_filter (f->politelistener,
515 dbus_connection_close (f->politelistener);
516 dbus_connection_unref (f->politelistener);
517 f->politelistener = NULL;
520 if (f->eavesdropper != NULL)
522 dbus_connection_remove_filter (f->eavesdropper,
525 dbus_connection_close (f->eavesdropper);
526 dbus_connection_unref (f->eavesdropper);
527 f->eavesdropper = NULL;
531 TerminateProcess (f->daemon_pid, 1);
533 kill (f->daemon_pid, SIGTERM);
536 g_spawn_close_pid (f->daemon_pid);
543 g_test_init (&argc, &argv, NULL);
544 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
546 g_test_add ("/eavedrop/match_keyword/broadcast", Fixture, NULL,
547 setup, test_eavesdrop_broadcast, teardown);
548 g_test_add ("/eavedrop/match_keyword/unicast_to_receiver", Fixture, NULL,
549 setup, test_eavesdrop_unicast_to_receiver,
551 g_test_add ("/eavedrop/match_keyword/unicast_to_sender", Fixture, NULL,
552 setup, test_eavesdrop_unicast_to_sender, teardown);
554 return g_test_run ();