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 "test-utils-glib.h"
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"
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 "'," \
47 "member='" SENDER_SIGNAL_NAME "'"
48 #define POLITELISTENER_RULE RECEIVER_RULE
49 #define EAVESDROPPER_RULE RECEIVER_RULE ",eavesdrop=true"
51 #define STOPPER_RULE "sender='" SENDER_NAME \
52 "',interface='" SENDER_IFACE "',type='signal',member='" SENDER_STOPPER_NAME "'"
54 /* a connection received a signal to whom? */
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;
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)
89 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE,
94 if (!dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->sender)))
97 if (!dbus_connection_send (f->sender, signal, NULL))
100 dbus_message_unref (signal);
102 return DBUS_HANDLER_RESULT_HANDLED;
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)
112 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
116 if (!dbus_message_set_destination (signal, dbus_bus_get_unique_name (f->receiver)))
119 if (!dbus_connection_send (f->sender, signal, NULL))
122 dbus_message_unref (signal);
124 return DBUS_HANDLER_RESULT_HANDLED;
127 static DBusHandlerResult
128 sender_send_broadcast (Fixture *f)
132 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_SIGNAL_NAME);
136 if (!dbus_message_set_destination (signal, NULL))
139 if (!dbus_connection_send (f->sender, signal, NULL))
142 dbus_message_unref (signal);
144 return DBUS_HANDLER_RESULT_HANDLED;
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
152 static DBusHandlerResult
153 sender_send_stopper (Fixture *f)
157 signal = dbus_message_new_signal (SENDER_PATH, SENDER_IFACE, SENDER_STOPPER_NAME);
161 if (!dbus_message_set_destination (signal, NULL))
164 if (!dbus_connection_send (f->sender, signal, NULL))
167 dbus_message_unref (signal);
169 return DBUS_HANDLER_RESULT_HANDLED;
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
176 static DBusHandlerResult
177 signal_filter (DBusConnection *connection,
178 DBusMessage *message,
181 Fixture *f = user_data;
182 SignalDst *dst = NULL;
183 DBusConnection **conn;
184 dbus_bool_t *got_stopper;
186 if (0 == strcmp (dbus_message_get_member (message), "NameAcquired"))
187 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
189 if (connection == f->receiver)
191 dst = &(f->receiver_dst);
192 conn = &(f->receiver);
193 got_stopper = &(f->receiver_got_stopper);
195 else if (connection == f->eavesdropper)
197 dst = &(f->eavesdropper_dst);
198 conn = &(f->eavesdropper);
199 got_stopper = &(f->eavesdropper_got_stopper);
201 else if (connection == f->politelistener)
203 dst = &(f->politelistener_dst);
204 conn = &(f->politelistener);
205 got_stopper = &(f->politelistener_got_stopper);
209 g_error ("connection not matching");
212 if (0 == strcmp (dbus_message_get_member (message), SENDER_SIGNAL_NAME))
214 if (dbus_message_get_destination (message) == NULL)
216 else if (0 == strcmp (dbus_message_get_destination (message), dbus_bus_get_unique_name (*conn)))
218 else /* if (dbus_message_get_destination (message) != NULL) */
221 else if (0 == strcmp (dbus_message_get_member (message), SENDER_STOPPER_NAME))
227 g_error ("got unknown member from message: %s",
228 dbus_message_get_member (message));
231 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
235 add_receiver_filter (Fixture *f)
237 DBusError e = DBUS_ERROR_INIT;
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);
244 if (!dbus_connection_add_filter (f->receiver,
245 signal_filter, f, NULL))
250 add_eavesdropper_filter (Fixture *f)
252 DBusError e = DBUS_ERROR_INIT;
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);
259 if (!dbus_connection_add_filter (f->eavesdropper,
260 signal_filter, f, NULL))
265 add_politelistener_filter (Fixture *f)
267 DBusError e = DBUS_ERROR_INIT;
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);
274 if (!dbus_connection_add_filter (f->politelistener,
275 signal_filter, f, NULL))
281 gconstpointer context G_GNUC_UNUSED)
285 f->ctx = test_main_context_get ();
288 dbus_error_init (&f->e);
290 address = test_get_dbus_daemon (NULL, TEST_USER_ME, NULL, &f->daemon_pid);
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,
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);
306 test_eavesdrop_broadcast (Fixture *f,
307 gconstpointer context G_GNUC_UNUSED)
309 sender_send_broadcast (f);
310 sender_send_stopper (f);
312 while (!f->receiver_got_stopper ||
313 !f->politelistener_got_stopper ||
314 !f->eavesdropper_got_stopper)
315 test_main_context_iterate (f->ctx, TRUE);
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);
323 /* a way to say that none of the listening connection are destination of the
326 test_eavesdrop_unicast_to_sender (Fixture *f,
327 gconstpointer context G_GNUC_UNUSED)
329 sender_send_unicast_to_sender (f);
330 sender_send_stopper (f);
332 while (!f->receiver_got_stopper ||
333 !f->politelistener_got_stopper ||
334 !f->eavesdropper_got_stopper)
335 test_main_context_iterate (f->ctx, TRUE);
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
342 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
346 test_eavesdrop_unicast_to_receiver (Fixture *f,
347 gconstpointer context G_GNUC_UNUSED)
349 sender_send_unicast_to_receiver (f);
350 sender_send_stopper (f);
352 while (!f->receiver_got_stopper ||
353 !f->politelistener_got_stopper ||
354 !f->eavesdropper_got_stopper)
355 test_main_context_iterate (f->ctx, TRUE);
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
363 g_assert_cmpint (f->eavesdropper_dst, ==, TO_OTHER);
367 teardown (Fixture *f,
368 gconstpointer context G_GNUC_UNUSED)
370 dbus_error_free (&f->e);
371 g_clear_error (&f->ge);
373 if (f->sender != NULL)
375 dbus_connection_close (f->sender);
376 dbus_connection_unref (f->sender);
380 if (f->receiver != NULL)
382 dbus_connection_remove_filter (f->receiver,
385 dbus_connection_close (f->receiver);
386 dbus_connection_unref (f->receiver);
390 if (f->politelistener != NULL)
392 dbus_connection_remove_filter (f->politelistener,
395 dbus_connection_close (f->politelistener);
396 dbus_connection_unref (f->politelistener);
397 f->politelistener = NULL;
400 if (f->eavesdropper != NULL)
402 dbus_connection_remove_filter (f->eavesdropper,
405 dbus_connection_close (f->eavesdropper);
406 dbus_connection_unref (f->eavesdropper);
407 f->eavesdropper = NULL;
410 if (f->daemon_pid != 0)
412 test_kill_pid (f->daemon_pid);
413 g_spawn_close_pid (f->daemon_pid);
417 test_main_context_unref (f->ctx);
424 test_init (&argc, &argv);
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,
431 g_test_add ("/eavedrop/match_keyword/unicast_to_sender", Fixture, NULL,
432 setup, test_eavesdrop_unicast_to_sender, teardown);
434 return g_test_run ();