1 /* Integration tests for monitor-mode D-Bus connections
3 * Copyright © 2010-2011 Nokia Corporation
4 * Copyright © 2015 Collabora Ltd.
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include "test-utils-glib.h"
34 const char *config_file;
35 const char * const *match_rules;
36 gboolean care_about_our_names;
48 DBusConnection *monitor;
49 DBusConnection *sender;
50 DBusConnection *recipient;
51 gboolean recipient_enqueue_filter_added;
56 const char *monitor_name;
57 const char *sender_name;
58 const char *recipient_name;
60 DBusConnection *systemd;
61 const char *systemd_name;
62 DBusMessage *systemd_message;
63 DBusConnection *activated;
64 const char *activated_name;
65 DBusMessage *activated_message;
68 static const char * const no_match_rules[] = {
72 static const char * const wildcard_match_rules[] = {
78 static const char * const eavesdrop_match_rules[] = {
84 static const char * const no_eavesdrop_match_rules[] = {
90 static const char * const selective_match_rules[] = {
91 "interface='com.example.Interesting'",
92 "interface='com.example.Fun'",
97 static const char * const well_known_destination_match_rules[] = {
98 "destination='com.example.Recipient'",
102 static Config forbidding_config = {
103 "valid-config-files/forbidding.conf",
108 static Config wildcard_config = {
110 wildcard_match_rules,
114 static Config selective_config = {
116 selective_match_rules,
120 static Config well_known_destination_config = {
122 well_known_destination_match_rules,
126 static Config no_rules_config = {
132 static Config eavesdrop_config = {
134 eavesdrop_match_rules,
138 static Config no_eavesdrop_config = {
140 no_eavesdrop_match_rules,
145 static Config fake_systemd_config = {
146 "valid-config-files/systemd-activation.conf",
152 static Config side_effects_config = {
158 static inline const char *
159 not_null2 (const char *x,
160 const char *fallback)
168 static inline const char *
169 not_null (const char *x)
171 return not_null2 (x, "(null)");
174 #define log_message(m) _log_message (m, __FILE__, __LINE__)
178 _log_message (DBusMessage *m,
182 g_test_message ("%s:%d: message type %d (%s)", file, line,
183 dbus_message_get_type (m),
184 dbus_message_type_to_string (dbus_message_get_type (m)));
185 g_test_message ("\tfrom: %s",
186 not_null2 (dbus_message_get_sender (m), "(dbus-daemon)"));
187 g_test_message ("\tto: %s",
188 not_null2 (dbus_message_get_destination (m), "(broadcast)"));
189 g_test_message ("\tpath: %s",
190 not_null (dbus_message_get_path (m)));
191 g_test_message ("\tinterface: %s",
192 not_null (dbus_message_get_interface (m)));
193 g_test_message ("\tmember: %s",
194 not_null (dbus_message_get_member (m)));
195 g_test_message ("\tsignature: %s",
196 not_null (dbus_message_get_signature (m)));
197 g_test_message ("\terror name: %s",
198 not_null (dbus_message_get_error_name (m)));
200 if (strcmp ("s", dbus_message_get_signature (m)) == 0)
202 DBusError e = DBUS_ERROR_INIT;
205 dbus_message_get_args (m, &e,
206 DBUS_TYPE_STRING, &s,
208 test_assert_no_error (&e);
209 g_test_message ("\tstring payload: %s", s);
213 /* these are macros so they get the right line number */
215 #define assert_hello(m) \
217 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
218 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
219 g_assert_cmpstr (dbus_message_get_destination (m), ==, DBUS_SERVICE_DBUS); \
220 g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
221 g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
222 g_assert_cmpstr (dbus_message_get_member (m), ==, "Hello"); \
223 g_assert_cmpstr (dbus_message_get_signature (m), ==, ""); \
224 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
225 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
228 #define assert_hello_reply(m) \
230 DBusError _e = DBUS_ERROR_INIT; \
233 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
234 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
235 g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
236 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
237 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
238 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
239 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
240 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
241 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
243 dbus_message_get_args (m, &_e, \
244 DBUS_TYPE_STRING, &_s, \
245 DBUS_TYPE_INVALID); \
246 test_assert_no_error (&_e); \
247 g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
250 #define assert_name_acquired(m) \
252 DBusError _e = DBUS_ERROR_INIT; \
255 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
256 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
257 g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
258 g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
259 g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
260 g_assert_cmpstr (dbus_message_get_member (m), ==, "NameAcquired"); \
261 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
262 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
263 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
265 dbus_message_get_args (m, &_e, \
266 DBUS_TYPE_STRING, &_s, \
267 DBUS_TYPE_INVALID); \
268 test_assert_no_error (&_e); \
269 g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
272 #define assert_method_call(m, sender, \
273 destination, path, iface, method, signature) \
275 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
276 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
277 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
278 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
279 g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
280 g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
281 g_assert_cmpstr (dbus_message_get_member (m), ==, method); \
282 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
283 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
284 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
287 #define assert_signal(m, \
288 sender, path, iface, member, signature, \
291 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
292 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
293 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
294 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
295 g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
296 g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
297 g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
298 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
299 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
300 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
303 #define assert_method_reply(m, sender, destination, signature) \
305 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
306 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
307 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
308 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
309 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
310 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
311 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
312 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
313 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
314 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
317 #define assert_error_reply(m, sender, destination, error_name) \
319 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
320 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR)); \
321 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
322 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
323 g_assert_cmpstr (dbus_message_get_error_name (m), ==, error_name); \
324 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
325 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
326 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
327 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
328 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
329 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
332 /* This is called after processing pending replies to our own method
333 * calls, but before anything else.
335 static DBusHandlerResult
336 monitor_filter (DBusConnection *connection,
337 DBusMessage *message,
340 Fixture *f = user_data;
342 g_assert_cmpstr (dbus_message_get_interface (message), !=,
343 "com.example.Tedious");
345 /* we are not interested in the monitor getting NameAcquired or NameLost
347 if (f->config == NULL || !f->config->care_about_our_names)
349 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
351 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
354 DBusError e = DBUS_ERROR_INIT;
357 dbus_message_get_args (message, &e,
358 DBUS_TYPE_STRING, &s,
360 test_assert_no_error (&e);
362 if (strcmp (s, f->monitor_name) == 0)
365 return DBUS_HANDLER_RESULT_HANDLED;
370 g_queue_push_tail (&f->monitored, dbus_message_ref (message));
372 return DBUS_HANDLER_RESULT_HANDLED;
375 static DBusHandlerResult
376 recipient_check_filter (DBusConnection *connection,
377 DBusMessage *message,
380 g_assert_cmpstr (dbus_message_get_interface (message), !=,
381 "com.example.CannotSend");
382 g_assert_cmpstr (dbus_message_get_interface (message), !=,
383 "com.example.CannotReceive");
385 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
388 static DBusHandlerResult
389 recipient_enqueue_filter (DBusConnection *connection,
390 DBusMessage *message,
393 Fixture *f = user_data;
395 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
397 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
399 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
402 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
405 g_queue_push_tail (&f->received, dbus_message_ref (message));
406 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
409 static DBusHandlerResult
410 systemd_filter (DBusConnection *connection,
411 DBusMessage *message,
414 Fixture *f = user_data;
416 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
418 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
421 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
424 g_assert (f->systemd_message == NULL);
425 f->systemd_message = dbus_message_ref (message);
427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
430 static DBusHandlerResult
431 activated_filter (DBusConnection *connection,
432 DBusMessage *message,
435 Fixture *f = user_data;
437 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
439 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
442 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
445 g_assert (f->activated_message == NULL);
446 f->activated_message = dbus_message_ref (message);
448 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
452 take_well_known_name (Fixture *f,
453 DBusConnection *connection,
458 ret = dbus_bus_request_name (connection, name,
459 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
460 test_assert_no_error (&f->e);
461 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
466 gconstpointer context)
470 f->ctx = test_main_context_get ();
473 dbus_error_init (&f->e);
475 f->address = test_get_dbus_daemon (f->config ? f->config->config_file : NULL,
476 TEST_USER_ME, NULL, &f->daemon_pid);
478 if (f->address == NULL)
481 f->monitor = test_connect_to_bus (f->ctx, f->address);
482 f->monitor_name = dbus_bus_get_unique_name (f->monitor);
483 f->sender = test_connect_to_bus (f->ctx, f->address);
484 f->sender_name = dbus_bus_get_unique_name (f->sender);
485 f->recipient = test_connect_to_bus (f->ctx, f->address);
486 f->recipient_name = dbus_bus_get_unique_name (f->recipient);
488 if (!dbus_connection_add_filter (f->monitor, monitor_filter, f, NULL))
491 if (!dbus_connection_add_filter (f->recipient, recipient_check_filter,
497 become_monitor (Fixture *f,
498 const Config *config)
503 DBusMessageIter appender, array_appender;
504 const char * const *match_rules;
506 dbus_uint32_t zero = 0;
508 dbus_connection_set_route_peer_messages (f->monitor, TRUE);
513 if (config != NULL && config->match_rules != NULL)
514 match_rules = config->match_rules;
516 match_rules = wildcard_match_rules;
518 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
519 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
524 dbus_message_iter_init_append (m, &appender);
526 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
530 for (i = 0; match_rules[i] != NULL; i++)
532 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
537 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
538 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
541 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
542 DBUS_TIMEOUT_USE_DEFAULT) ||
546 dbus_message_unref (m);
549 if (dbus_pending_call_get_completed (pc))
550 test_pending_call_store_reply (pc, &m);
551 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
556 test_main_context_iterate (f->ctx, TRUE);
558 ok = dbus_message_get_args (m, &f->e,
560 test_assert_no_error (&f->e);
563 dbus_pending_call_unref (pc);
564 dbus_message_unref (m);
569 * Test what happens if the method call arguments are invalid.
572 test_invalid (Fixture *f,
573 gconstpointer context)
578 DBusMessageIter appender, array_appender;
579 dbus_uint32_t zero = 0;
580 dbus_uint32_t invalid_flags = G_MAXUINT32;
583 if (f->address == NULL)
586 dbus_connection_set_route_peer_messages (f->monitor, TRUE);
588 /* Try to become a monitor but specify nonzero flags - not allowed */
590 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
591 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
596 dbus_message_iter_init_append (m, &appender);
598 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
602 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
603 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32,
607 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
608 DBUS_TIMEOUT_USE_DEFAULT) ||
612 dbus_message_unref (m);
615 if (dbus_pending_call_get_completed (pc))
616 test_pending_call_store_reply (pc, &m);
617 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
622 test_main_context_iterate (f->ctx, TRUE);
624 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
625 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
626 DBUS_ERROR_INVALID_ARGS);
628 /* Try to become a monitor but use the wrong object path - not allowed
629 * (security hardening against inappropriate XML policy rules) */
631 dbus_pending_call_unref (pc);
632 dbus_message_unref (m);
634 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
635 "/", DBUS_INTERFACE_MONITORING, "BecomeMonitor");
640 dbus_message_iter_init_append (m, &appender);
642 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
646 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
647 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
650 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
651 DBUS_TIMEOUT_USE_DEFAULT) ||
655 dbus_message_unref (m);
658 if (dbus_pending_call_get_completed (pc))
659 test_pending_call_store_reply (pc, &m);
660 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
665 test_main_context_iterate (f->ctx, TRUE);
667 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
668 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
669 DBUS_ERROR_UNKNOWN_INTERFACE);
671 /* Try to become a monitor but specify a bad match rule -
672 * also not allowed */
674 dbus_pending_call_unref (pc);
675 dbus_message_unref (m);
677 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
678 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
683 dbus_message_iter_init_append (m, &appender);
685 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
689 /* Syntactically incorrect match rule taken from #92298 - was probably
690 * intended to be path='/modules/...'
692 s = "interface='org.kde.walletd',member='/modules/kwalletd/org.kde.KWallet/walletOpened'";
694 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
696 !dbus_message_iter_close_container (&appender, &array_appender) ||
697 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero) ||
698 !dbus_connection_send_with_reply (f->monitor, m, &pc,
699 DBUS_TIMEOUT_USE_DEFAULT) ||
703 dbus_message_unref (m);
706 if (dbus_pending_call_get_completed (pc))
707 test_pending_call_store_reply (pc, &m);
708 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
713 test_main_context_iterate (f->ctx, TRUE);
715 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
716 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
717 DBUS_ERROR_MATCH_RULE_INVALID);
719 dbus_pending_call_unref (pc);
720 dbus_message_unref (m);
722 /* We did not become a monitor, so we can still call methods. */
725 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
726 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetId");
731 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
732 DBUS_TIMEOUT_USE_DEFAULT) ||
736 dbus_message_unref (m);
739 if (dbus_pending_call_get_completed (pc))
740 test_pending_call_store_reply (pc, &m);
741 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
746 test_main_context_iterate (f->ctx, TRUE);
748 if (dbus_set_error_from_message (&f->e, m))
749 g_error ("%s: %s", f->e.name, f->e.message);
751 ok = dbus_message_get_args (m, &f->e,
752 DBUS_TYPE_STRING, &s,
754 test_assert_no_error (&f->e);
756 g_assert_cmpstr (s, !=, NULL);
757 g_assert_cmpstr (s, !=, "");
759 dbus_pending_call_unref (pc);
760 dbus_message_unref (m);
764 * Test the side-effects of becoming a monitor.
767 test_become_monitor (Fixture *f,
768 gconstpointer context)
772 dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
773 dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
775 if (f->address == NULL)
778 ret = dbus_bus_request_name (f->monitor, "com.example.A",
779 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
780 test_assert_no_error (&f->e);
781 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
783 ret = dbus_bus_request_name (f->monitor, "com.example.B",
784 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
785 test_assert_no_error (&f->e);
786 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
788 ret = dbus_bus_request_name (f->monitor, "com.example.C",
789 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
790 test_assert_no_error (&f->e);
791 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
793 while (!got_unique || !got_a || !got_b || !got_c)
795 if (g_queue_is_empty (&f->monitored))
796 test_main_context_iterate (f->ctx, TRUE);
798 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
800 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
804 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
805 DBUS_TYPE_STRING, &name,
808 g_assert_cmpstr (dbus_message_get_path (m), ==,
811 test_assert_no_error (&f->e);
814 if (g_str_equal (name, f->monitor_name))
816 g_assert (!got_unique);
819 else if (g_str_equal (name, "com.example.A"))
824 else if (g_str_equal (name, "com.example.B"))
831 g_assert_cmpstr (name, ==, "com.example.C");
838 g_error ("unexpected message %s.%s",
839 dbus_message_get_interface (m),
840 dbus_message_get_member (m));
843 dbus_message_unref (m);
847 become_monitor (f, NULL);
849 while (!lost_unique || !lost_a || !lost_b || !lost_c)
851 if (g_queue_is_empty (&f->monitored))
852 test_main_context_iterate (f->ctx, TRUE);
854 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
856 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
860 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
861 DBUS_TYPE_STRING, &name,
864 test_assert_no_error (&f->e);
867 if (g_str_equal (name, f->monitor_name))
869 g_assert (!lost_unique);
872 else if (g_str_equal (name, "com.example.A"))
877 else if (g_str_equal (name, "com.example.B"))
884 g_assert_cmpstr (name, ==, "com.example.C");
891 g_error ("unexpected message %s.%s",
892 dbus_message_get_interface (m),
893 dbus_message_get_member (m));
896 dbus_message_unref (m);
900 /* Calling methods is forbidden; we get disconnected. */
901 dbus_bus_add_match (f->monitor, "", &f->e);
902 g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
903 g_assert (!dbus_connection_get_is_connected (f->monitor));
907 if (g_queue_is_empty (&f->monitored))
908 test_main_context_iterate (f->ctx, TRUE);
910 /* When we iterate all the connection's messages, we see ourselves
911 * losing all our names, then we're disconnected. */
912 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
914 if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
916 dbus_message_unref (m);
921 g_error ("unexpected message %s.%s",
922 dbus_message_get_interface (m),
923 dbus_message_get_member (m));
926 dbus_message_unref (m);
938 test_broadcast (Fixture *f,
939 gconstpointer context)
943 if (f->address == NULL)
946 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
947 test_assert_no_error (&f->e);
949 become_monitor (f, NULL);
951 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
952 dbus_connection_send (f->sender, m, NULL);
953 dbus_message_unref (m);
955 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
956 dbus_connection_send (f->sender, m, NULL);
957 dbus_message_unref (m);
959 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
960 dbus_connection_send (f->sender, m, NULL);
961 dbus_message_unref (m);
963 while (g_queue_get_length (&f->monitored) < 3)
964 test_main_context_iterate (f->ctx, TRUE);
966 m = g_queue_pop_head (&f->monitored);
967 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
968 "BroadcastSignal1", "", NULL);
969 dbus_message_unref (m);
971 m = g_queue_pop_head (&f->monitored);
972 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
973 "BroadcastSignal2", "", NULL);
974 dbus_message_unref (m);
976 m = g_queue_pop_head (&f->monitored);
977 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
978 "BroadcastSignal3", "", NULL);
979 dbus_message_unref (m);
981 m = g_queue_pop_head (&f->monitored);
982 g_assert (m == NULL);
986 test_forbidden_broadcast (Fixture *f,
987 gconstpointer context)
991 if (f->address == NULL)
994 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
995 test_assert_no_error (&f->e);
997 if (!dbus_connection_add_filter (f->recipient, recipient_enqueue_filter,
1000 f->recipient_enqueue_filter_added = TRUE;
1002 become_monitor (f, NULL);
1004 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1005 "BroadcastSignal1");
1006 dbus_connection_send (f->sender, m, NULL);
1007 dbus_message_unref (m);
1009 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
1010 "BroadcastSignal2");
1011 dbus_connection_send (f->sender, m, NULL);
1012 dbus_message_unref (m);
1014 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1015 "BroadcastSignal3");
1016 dbus_connection_send (f->sender, m, NULL);
1017 dbus_message_unref (m);
1019 m = dbus_message_new_signal ("/foo", "com.example.CannotBroadcast",
1021 dbus_connection_send (f->sender, m, NULL);
1022 dbus_message_unref (m);
1024 m = dbus_message_new_signal ("/foo", "com.example.CannotBroadcast2",
1025 "CannotBroadcast2");
1026 dbus_connection_send (f->sender, m, NULL);
1027 dbus_message_unref (m);
1029 /* these two will go through: we use them as an indirect way to assert that
1030 * the recipient has not received anything earlier */
1031 m = dbus_message_new_signal ("/foo", "com.example.CannotUnicast",
1033 dbus_connection_send (f->sender, m, NULL);
1034 dbus_message_unref (m);
1035 m = dbus_message_new_signal ("/foo", "com.example.CannotUnicast2",
1037 dbus_connection_send (f->sender, m, NULL);
1038 dbus_message_unref (m);
1040 while (g_queue_get_length (&f->monitored) < 12)
1041 test_main_context_iterate (f->ctx, TRUE);
1043 m = g_queue_pop_head (&f->monitored);
1044 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
1045 "BroadcastSignal1", "", NULL);
1046 dbus_message_unref (m);
1048 m = g_queue_pop_head (&f->monitored);
1049 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1050 DBUS_ERROR_ACCESS_DENIED);
1051 dbus_message_unref (m);
1053 m = g_queue_pop_head (&f->monitored);
1054 assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
1055 "BroadcastSignal2", "", NULL);
1056 dbus_message_unref (m);
1058 m = g_queue_pop_head (&f->monitored);
1059 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1060 DBUS_ERROR_ACCESS_DENIED);
1061 dbus_message_unref (m);
1063 m = g_queue_pop_head (&f->monitored);
1064 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
1065 "BroadcastSignal3", "", NULL);
1066 dbus_message_unref (m);
1068 m = g_queue_pop_head (&f->monitored);
1069 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1070 DBUS_ERROR_ACCESS_DENIED);
1071 dbus_message_unref (m);
1073 m = g_queue_pop_head (&f->monitored);
1074 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast",
1075 "CannotBroadcast", "", NULL);
1076 dbus_message_unref (m);
1078 m = g_queue_pop_head (&f->monitored);
1079 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1080 DBUS_ERROR_ACCESS_DENIED);
1081 dbus_message_unref (m);
1083 m = g_queue_pop_head (&f->monitored);
1084 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast2",
1085 "CannotBroadcast2", "", NULL);
1086 dbus_message_unref (m);
1088 m = g_queue_pop_head (&f->monitored);
1089 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1090 DBUS_ERROR_ACCESS_DENIED);
1091 dbus_message_unref (m);
1093 m = g_queue_pop_head (&f->monitored);
1094 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast",
1095 "CannotUnicast", "", NULL);
1096 dbus_message_unref (m);
1098 m = g_queue_pop_head (&f->monitored);
1099 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast2",
1100 "CannotUnicast2", "", NULL);
1101 dbus_message_unref (m);
1103 m = g_queue_pop_head (&f->monitored);
1104 g_assert (m == NULL);
1106 /* the intended recipient only received the ones that were on the interface
1107 * where broadcasts are allowed */
1109 while (g_queue_get_length (&f->received) < 2)
1110 test_main_context_iterate (f->ctx, TRUE);
1112 m = g_queue_pop_head (&f->received);
1113 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast",
1114 "CannotUnicast", "", NULL);
1115 dbus_message_unref (m);
1117 m = g_queue_pop_head (&f->received);
1118 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast2",
1119 "CannotUnicast2", "", NULL);
1120 dbus_message_unref (m);
1122 m = g_queue_pop_head (&f->received);
1123 g_assert (m == NULL);
1127 test_unicast_signal (Fixture *f,
1128 gconstpointer context)
1132 if (f->address == NULL)
1135 become_monitor (f, NULL);
1137 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1138 if (!dbus_message_set_destination (m, f->recipient_name))
1140 dbus_connection_send (f->sender, m, NULL);
1141 dbus_message_unref (m);
1143 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1144 if (!dbus_message_set_destination (m, f->recipient_name))
1146 dbus_connection_send (f->sender, m, NULL);
1147 dbus_message_unref (m);
1149 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1150 if (!dbus_message_set_destination (m, f->recipient_name))
1152 dbus_connection_send (f->sender, m, NULL);
1153 dbus_message_unref (m);
1155 while (g_queue_get_length (&f->monitored) < 3)
1156 test_main_context_iterate (f->ctx, TRUE);
1158 m = g_queue_pop_head (&f->monitored);
1159 assert_signal (m, f->sender_name, "/foo",
1160 "com.example.bar", "UnicastSignal1", "", f->recipient_name);
1161 dbus_message_unref (m);
1163 m = g_queue_pop_head (&f->monitored);
1164 assert_signal (m, f->sender_name, "/foo",
1165 "com.example.bar", "UnicastSignal2", "", f->recipient_name);
1166 dbus_message_unref (m);
1168 m = g_queue_pop_head (&f->monitored);
1169 assert_signal (m, f->sender_name, "/foo",
1170 "com.example.bar", "UnicastSignal3", "", f->recipient_name);
1171 dbus_message_unref (m);
1173 m = g_queue_pop_head (&f->monitored);
1174 g_assert (m == NULL);
1178 test_forbidden (Fixture *f,
1179 gconstpointer context)
1183 if (f->address == NULL)
1186 if (!dbus_connection_add_filter (f->recipient, recipient_enqueue_filter,
1189 f->recipient_enqueue_filter_added = TRUE;
1191 become_monitor (f, NULL);
1193 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1195 if (!dbus_message_set_destination (m, f->recipient_name))
1197 dbus_connection_send (f->sender, m, NULL);
1198 dbus_message_unref (m);
1200 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
1202 if (!dbus_message_set_destination (m, f->recipient_name))
1204 dbus_connection_send (f->sender, m, NULL);
1205 dbus_message_unref (m);
1207 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1209 if (!dbus_message_set_destination (m, f->recipient_name))
1211 dbus_connection_send (f->sender, m, NULL);
1212 dbus_message_unref (m);
1214 m = dbus_message_new_signal ("/foo", "com.example.CannotUnicast",
1216 if (!dbus_message_set_destination (m, f->recipient_name))
1218 dbus_connection_send (f->sender, m, NULL);
1219 dbus_message_unref (m);
1221 m = dbus_message_new_signal ("/foo", "com.example.CannotUnicast2",
1223 if (!dbus_message_set_destination (m, f->recipient_name))
1225 dbus_connection_send (f->sender, m, NULL);
1226 dbus_message_unref (m);
1228 /* these two will go through: we use them as an indirect way to assert that
1229 * the recipient has not received anything earlier */
1230 m = dbus_message_new_signal ("/foo", "com.example.CannotBroadcast",
1232 if (!dbus_message_set_destination (m, f->recipient_name))
1234 dbus_connection_send (f->sender, m, NULL);
1235 dbus_message_unref (m);
1236 m = dbus_message_new_signal ("/foo", "com.example.CannotBroadcast2",
1237 "CannotBroadcast2");
1238 if (!dbus_message_set_destination (m, f->recipient_name))
1240 dbus_connection_send (f->sender, m, NULL);
1241 dbus_message_unref (m);
1243 while (g_queue_get_length (&f->monitored) < 12)
1244 test_main_context_iterate (f->ctx, TRUE);
1246 m = g_queue_pop_head (&f->monitored);
1247 assert_signal (m, f->sender_name, "/foo",
1248 "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
1249 dbus_message_unref (m);
1251 m = g_queue_pop_head (&f->monitored);
1252 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1253 DBUS_ERROR_ACCESS_DENIED);
1254 dbus_message_unref (m);
1256 m = g_queue_pop_head (&f->monitored);
1257 assert_signal (m, f->sender_name, "/foo",
1258 "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
1259 dbus_message_unref (m);
1261 m = g_queue_pop_head (&f->monitored);
1262 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1263 DBUS_ERROR_ACCESS_DENIED);
1264 dbus_message_unref (m);
1266 m = g_queue_pop_head (&f->monitored);
1267 assert_signal (m, f->sender_name, "/foo",
1268 "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
1269 dbus_message_unref (m);
1271 m = g_queue_pop_head (&f->monitored);
1272 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1273 DBUS_ERROR_ACCESS_DENIED);
1274 dbus_message_unref (m);
1276 m = g_queue_pop_head (&f->monitored);
1277 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast",
1278 "CannotUnicast", "", f->recipient_name);
1279 dbus_message_unref (m);
1281 m = g_queue_pop_head (&f->monitored);
1282 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1283 DBUS_ERROR_ACCESS_DENIED);
1284 dbus_message_unref (m);
1286 m = g_queue_pop_head (&f->monitored);
1287 assert_signal (m, f->sender_name, "/foo", "com.example.CannotUnicast2",
1288 "CannotUnicast2", "", f->recipient_name);
1289 dbus_message_unref (m);
1291 m = g_queue_pop_head (&f->monitored);
1292 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1293 DBUS_ERROR_ACCESS_DENIED);
1294 dbus_message_unref (m);
1296 m = g_queue_pop_head (&f->monitored);
1297 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast",
1298 "CannotBroadcast", "", f->recipient_name);
1299 dbus_message_unref (m);
1301 m = g_queue_pop_head (&f->monitored);
1302 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast2",
1303 "CannotBroadcast2", "", f->recipient_name);
1304 dbus_message_unref (m);
1306 m = g_queue_pop_head (&f->monitored);
1307 g_assert (m == NULL);
1309 /* the intended recipient only received the ones that were on the interface
1310 * where unicasts are allowed */
1312 while (g_queue_get_length (&f->received) < 2)
1313 test_main_context_iterate (f->ctx, TRUE);
1315 m = g_queue_pop_head (&f->received);
1316 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast",
1317 "CannotBroadcast", "", f->recipient_name);
1318 dbus_message_unref (m);
1320 m = g_queue_pop_head (&f->received);
1321 assert_signal (m, f->sender_name, "/foo", "com.example.CannotBroadcast2",
1322 "CannotBroadcast2", "", f->recipient_name);
1323 dbus_message_unref (m);
1325 m = g_queue_pop_head (&f->received);
1326 g_assert (m == NULL);
1330 test_method_call (Fixture *f,
1331 gconstpointer context)
1335 if (f->address == NULL)
1338 become_monitor (f, NULL);
1340 /* regression test for
1341 * https://bugs.freedesktop.org/show_bug.cgi?id=90952 */
1342 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1343 DBUS_INTERFACE_PEER, "Ping");
1344 dbus_connection_send (f->sender, m, NULL);
1345 dbus_message_unref (m);
1347 while (g_queue_get_length (&f->monitored) < 2)
1348 test_main_context_iterate (f->ctx, TRUE);
1350 m = g_queue_pop_head (&f->monitored);
1351 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1352 DBUS_INTERFACE_PEER, "Ping", "");
1353 dbus_message_unref (m);
1355 m = g_queue_pop_head (&f->monitored);
1356 assert_method_reply (m, f->recipient_name, f->sender_name, "");
1357 dbus_message_unref (m);
1359 m = g_queue_pop_head (&f->monitored);
1360 g_assert (m == NULL);
1362 m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
1364 dbus_connection_send (f->sender, m, NULL);
1365 dbus_message_unref (m);
1367 while (g_queue_get_length (&f->monitored) < 2)
1368 test_main_context_iterate (f->ctx, TRUE);
1370 m = g_queue_pop_head (&f->monitored);
1371 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1372 "com.example.bar", "Call1", "");
1373 dbus_message_unref (m);
1375 m = g_queue_pop_head (&f->monitored);
1376 assert_error_reply (m, f->recipient_name, f->sender_name,
1377 DBUS_ERROR_UNKNOWN_METHOD);
1378 dbus_message_unref (m);
1380 m = g_queue_pop_head (&f->monitored);
1381 g_assert (m == NULL);
1385 test_forbidden_method_call (Fixture *f,
1386 gconstpointer context)
1390 if (f->address == NULL)
1393 become_monitor (f, NULL);
1395 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1396 "com.example.CannotSend", "Call1");
1397 dbus_connection_send (f->sender, m, NULL);
1398 dbus_message_unref (m);
1400 while (g_queue_get_length (&f->monitored) < 2)
1401 test_main_context_iterate (f->ctx, TRUE);
1403 m = g_queue_pop_head (&f->monitored);
1404 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1405 "com.example.CannotSend", "Call1", "");
1406 dbus_message_unref (m);
1408 m = g_queue_pop_head (&f->monitored);
1409 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1410 DBUS_ERROR_ACCESS_DENIED);
1411 dbus_message_unref (m);
1413 m = g_queue_pop_head (&f->monitored);
1414 g_assert (m == NULL);
1416 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1417 "com.example.CannotReceive", "Call2");
1418 dbus_connection_send (f->sender, m, NULL);
1419 dbus_message_unref (m);
1421 while (g_queue_get_length (&f->monitored) < 2)
1422 test_main_context_iterate (f->ctx, TRUE);
1424 m = g_queue_pop_head (&f->monitored);
1425 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1426 "com.example.CannotReceive", "Call2", "");
1427 dbus_message_unref (m);
1429 m = g_queue_pop_head (&f->monitored);
1430 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1431 DBUS_ERROR_ACCESS_DENIED);
1432 dbus_message_unref (m);
1434 m = g_queue_pop_head (&f->monitored);
1435 g_assert (m == NULL);
1439 test_dbus_daemon (Fixture *f,
1440 gconstpointer context)
1445 if (f->address == NULL)
1448 become_monitor (f, NULL);
1450 res = dbus_bus_request_name (f->sender, "com.example.Sender",
1451 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1452 test_assert_no_error (&f->e);
1453 g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1455 res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1456 test_assert_no_error (&f->e);
1457 g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1459 while (g_queue_get_length (&f->monitored) < 8)
1460 test_main_context_iterate (f->ctx, TRUE);
1462 m = g_queue_pop_head (&f->monitored);
1463 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1464 DBUS_INTERFACE_DBUS, "RequestName", "su");
1465 dbus_message_unref (m);
1467 m = g_queue_pop_head (&f->monitored);
1468 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1469 "NameOwnerChanged", "sss", NULL);
1470 dbus_message_unref (m);
1472 /* FIXME: should we get this? */
1473 m = g_queue_pop_head (&f->monitored);
1474 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1475 "NameAcquired", "s", f->sender_name);
1476 dbus_message_unref (m);
1478 m = g_queue_pop_head (&f->monitored);
1479 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1480 dbus_message_unref (m);
1482 m = g_queue_pop_head (&f->monitored);
1483 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1484 DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1485 dbus_message_unref (m);
1487 /* FIXME: should we get this? */
1488 m = g_queue_pop_head (&f->monitored);
1489 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1490 "NameLost", "s", f->sender_name);
1491 dbus_message_unref (m);
1493 m = g_queue_pop_head (&f->monitored);
1494 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1495 "NameOwnerChanged", "sss", NULL);
1496 dbus_message_unref (m);
1498 m = g_queue_pop_head (&f->monitored);
1499 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1500 dbus_message_unref (m);
1502 m = g_queue_pop_head (&f->monitored);
1503 g_assert (m == NULL);
1507 test_selective (Fixture *f,
1508 gconstpointer context)
1512 if (f->address == NULL)
1515 /* Match rules added before becoming a monitor should be cleared:
1516 * if they weren't, this test would get Interesting twice, then Tedious,
1517 * and only see Fun after that. */
1518 dbus_bus_add_match (f->monitor,
1519 "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1520 test_assert_no_error (&f->e);
1521 dbus_bus_add_match (f->monitor,
1522 "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1523 test_assert_no_error (&f->e);
1525 become_monitor (f, NULL);
1527 m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1529 if (!dbus_message_set_destination (m, f->recipient_name))
1531 dbus_connection_send (f->sender, m, NULL);
1532 dbus_message_unref (m);
1534 m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1536 if (!dbus_message_set_destination (m, f->recipient_name))
1538 dbus_connection_send (f->sender, m, NULL);
1539 dbus_message_unref (m);
1541 m = dbus_message_new_signal ("/foo", "com.example.Fun",
1543 if (!dbus_message_set_destination (m, f->recipient_name))
1545 dbus_connection_send (f->sender, m, NULL);
1546 dbus_message_unref (m);
1548 while (g_queue_get_length (&f->monitored) < 2)
1549 test_main_context_iterate (f->ctx, TRUE);
1551 /* We get the interesting signal and the fun signal, but not the tedious
1554 m = g_queue_pop_head (&f->monitored);
1555 assert_signal (m, f->sender_name, "/foo",
1556 "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1557 dbus_message_unref (m);
1559 m = g_queue_pop_head (&f->monitored);
1560 assert_signal (m, f->sender_name, "/foo",
1561 "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1562 dbus_message_unref (m);
1564 m = g_queue_pop_head (&f->monitored);
1565 g_assert (m == NULL);
1569 test_well_known_destination (Fixture *f,
1570 gconstpointer context)
1574 if (f->address == NULL)
1577 take_well_known_name (f, f->recipient, "com.example.Recipient");
1578 /* we don't expect_take_well_known_name here because the
1579 * monitor isn't up yet */
1581 become_monitor (f, NULL);
1583 /* The sender sends a message to itself. It will not be observed. */
1584 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1585 if (!dbus_message_set_destination (m, f->sender_name))
1587 dbus_connection_send (f->sender, m, NULL);
1588 dbus_message_unref (m);
1590 /* The sender sends a message to the recipient by well-known name.
1591 * It will be observed. */
1592 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1593 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1595 dbus_connection_send (f->sender, m, NULL);
1596 dbus_message_unref (m);
1598 /* The sender sends a message to the recipient by unique name.
1599 * It will still be observed. */
1600 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1601 if (!dbus_message_set_destination (m, f->recipient_name))
1603 dbus_connection_send (f->sender, m, NULL);
1604 dbus_message_unref (m);
1606 while (g_queue_get_length (&f->monitored) < 2)
1607 test_main_context_iterate (f->ctx, TRUE);
1609 m = g_queue_pop_head (&f->monitored);
1610 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1611 "Observed1", "", "com.example.Recipient");
1612 dbus_message_unref (m);
1614 m = g_queue_pop_head (&f->monitored);
1615 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1616 "Observed2", "", f->recipient_name);
1617 dbus_message_unref (m);
1619 m = g_queue_pop_head (&f->monitored);
1620 g_assert (m == NULL);
1624 test_unique_destination (Fixture *f,
1625 gconstpointer context)
1630 NULL, /* match rules */
1633 const gchar *match_rules[2] = { NULL, NULL };
1636 if (f->address == NULL)
1639 take_well_known_name (f, f->recipient, "com.example.Recipient");
1640 /* we don't expect_take_well_known_name here because the
1641 * monitor isn't up yet */
1643 rule = g_strdup_printf ("destination='%s'", f->recipient_name);
1645 g_test_queue_free (rule);
1646 match_rules[0] = rule;
1647 config.match_rules = match_rules;
1649 become_monitor (f, &config);
1651 /* The sender sends a message to itself. It will not be observed. */
1652 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1653 if (!dbus_message_set_destination (m, f->sender_name))
1655 dbus_connection_send (f->sender, m, NULL);
1656 dbus_message_unref (m);
1658 /* The sender sends a message to the recipient by well-known name.
1659 * It will be observed. */
1660 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1661 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1663 dbus_connection_send (f->sender, m, NULL);
1664 dbus_message_unref (m);
1666 /* The sender sends a message to the recipient by unique name.
1667 * It will still be observed. */
1668 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1669 if (!dbus_message_set_destination (m, f->recipient_name))
1671 dbus_connection_send (f->sender, m, NULL);
1672 dbus_message_unref (m);
1674 while (g_queue_get_length (&f->monitored) < 2)
1675 test_main_context_iterate (f->ctx, TRUE);
1677 m = g_queue_pop_head (&f->monitored);
1678 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1679 "Observed1", "", "com.example.Recipient");
1680 dbus_message_unref (m);
1682 m = g_queue_pop_head (&f->monitored);
1683 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1684 "Observed2", "", f->recipient_name);
1685 dbus_message_unref (m);
1687 m = g_queue_pop_head (&f->monitored);
1688 g_assert (m == NULL);
1692 /* currently only used for the systemd activation test */
1694 expect_new_connection (Fixture *f)
1698 while (g_queue_get_length (&f->monitored) < 4)
1699 test_main_context_iterate (f->ctx, TRUE);
1701 m = g_queue_pop_head (&f->monitored);
1703 dbus_message_unref (m);
1705 m = g_queue_pop_head (&f->monitored);
1706 assert_hello_reply (m);
1707 dbus_message_unref (m);
1709 m = g_queue_pop_head (&f->monitored);
1710 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1711 "NameOwnerChanged", "sss", NULL);
1712 dbus_message_unref (m);
1714 m = g_queue_pop_head (&f->monitored);
1715 assert_name_acquired (m);
1716 dbus_message_unref (m);
1719 /* currently only used for the systemd activation test */
1721 expect_take_well_known_name (Fixture *f,
1722 DBusConnection *connection,
1726 const char *connection_name = dbus_bus_get_unique_name (connection);
1728 while (g_queue_get_length (&f->monitored) < 4)
1729 test_main_context_iterate (f->ctx, TRUE);
1731 m = g_queue_pop_head (&f->monitored);
1732 assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1733 DBUS_INTERFACE_DBUS, "RequestName", "su");
1734 dbus_message_unref (m);
1736 m = g_queue_pop_head (&f->monitored);
1737 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1738 "NameOwnerChanged", "sss", NULL);
1739 dbus_message_unref (m);
1741 m = g_queue_pop_head (&f->monitored);
1742 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1743 "NameAcquired", "s", connection_name);
1744 dbus_message_unref (m);
1746 m = g_queue_pop_head (&f->monitored);
1747 assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1748 dbus_message_unref (m);
1752 test_activation (Fixture *f,
1753 gconstpointer context)
1757 if (f->address == NULL)
1760 become_monitor (f, NULL);
1762 /* The sender sends a message to an activatable service. */
1763 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1764 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1766 dbus_connection_send (f->sender, m, NULL);
1767 dbus_message_unref (m);
1769 /* We observe the activation request, and the message that caused it,
1770 * before systemd has even joined the bus. */
1771 while (g_queue_get_length (&f->monitored) < 2)
1772 test_main_context_iterate (f->ctx, TRUE);
1774 m = g_queue_pop_head (&f->monitored);
1775 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1776 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1777 "org.freedesktop.systemd1");
1778 dbus_message_unref (m);
1779 m = g_queue_pop_head (&f->monitored);
1780 assert_signal (m, f->sender_name, "/foo",
1781 "com.example.bar", "UnicastSignal1", "",
1782 "com.example.SystemdActivatable1");
1783 dbus_message_unref (m);
1785 /* The fake systemd connects to the bus. */
1786 f->systemd = test_connect_to_bus (f->ctx, f->address);
1787 if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1789 f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1791 expect_new_connection (f);
1792 take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1793 expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1795 /* It gets its activation request. */
1796 while (f->systemd_message == NULL)
1797 test_main_context_iterate (f->ctx, TRUE);
1799 m = f->systemd_message;
1800 f->systemd_message = NULL;
1801 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1802 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1803 "org.freedesktop.systemd1");
1804 dbus_message_unref (m);
1806 /* systemd starts the activatable service. */
1807 f->activated = test_connect_to_bus (f->ctx, f->address);
1808 if (!dbus_connection_add_filter (f->activated, activated_filter,
1811 f->activated_name = dbus_bus_get_unique_name (f->activated);
1813 expect_new_connection (f);
1814 take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1815 expect_take_well_known_name (f, f->activated,
1816 "com.example.SystemdActivatable1");
1818 /* The message is delivered to the activatable service. */
1819 while (f->activated_message == NULL)
1820 test_main_context_iterate (f->ctx, TRUE);
1822 m = f->activated_message;
1823 f->activated_message = NULL;
1824 assert_signal (m, f->sender_name, "/foo",
1825 "com.example.bar", "UnicastSignal1", "",
1826 "com.example.SystemdActivatable1");
1827 dbus_message_unref (m);
1829 /* The sender sends a message to a different activatable service. */
1830 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1831 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1833 dbus_connection_send (f->sender, m, NULL);
1834 dbus_message_unref (m);
1836 /* This time systemd is already ready for it. */
1837 while (g_queue_get_length (&f->monitored) < 2 ||
1838 f->systemd_message == NULL)
1839 test_main_context_iterate (f->ctx, TRUE);
1841 m = f->systemd_message;
1842 f->systemd_message = NULL;
1843 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1844 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1845 "org.freedesktop.systemd1");
1846 dbus_message_unref (m);
1848 /* The monitor sees the activation request and the signal that
1850 m = g_queue_pop_head (&f->monitored);
1851 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1852 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1853 "org.freedesktop.systemd1");
1854 dbus_message_unref (m);
1855 m = g_queue_pop_head (&f->monitored);
1856 assert_signal (m, f->sender_name, "/foo",
1857 "com.example.bar", "UnicastSignal2", "",
1858 "com.example.SystemdActivatable2");
1859 dbus_message_unref (m);
1861 /* The activatable service takes its name. Here I'm faking it by using
1862 * an existing connection. */
1863 take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1865 /* The message is delivered to the activatable service.
1866 * Implementation detail: the monitor sees this happen before it even
1867 * sees that the name request happened, which is pretty odd. */
1868 while (f->activated_message == NULL)
1869 test_main_context_iterate (f->ctx, TRUE);
1871 m = f->activated_message;
1872 f->activated_message = NULL;
1873 assert_signal (m, f->sender_name, "/foo",
1874 "com.example.bar", "UnicastSignal2", "",
1875 "com.example.SystemdActivatable2");
1876 dbus_message_unref (m);
1878 expect_take_well_known_name (f, f->activated,
1879 "com.example.SystemdActivatable2");
1881 /* A third activation. */
1882 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1883 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1885 dbus_connection_send (f->sender, m, NULL);
1886 dbus_message_unref (m);
1888 /* Once again, we see the activation request and the reason. */
1889 while (g_queue_get_length (&f->monitored) < 2)
1890 test_main_context_iterate (f->ctx, TRUE);
1892 m = g_queue_pop_head (&f->monitored);
1893 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1894 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1895 "org.freedesktop.systemd1");
1896 dbus_message_unref (m);
1897 m = g_queue_pop_head (&f->monitored);
1898 assert_signal (m, f->sender_name, "/foo",
1899 "com.example.bar", "UnicastSignal3", "",
1900 "com.example.SystemdActivatable3");
1901 dbus_message_unref (m);
1903 /* systemd gets the request too. */
1904 while (f->systemd_message == NULL)
1905 test_main_context_iterate (f->ctx, TRUE);
1907 m = f->systemd_message;
1908 f->systemd_message = NULL;
1909 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1910 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1911 "org.freedesktop.systemd1");
1912 dbus_message_unref (m);
1914 /* This time activation fails */
1915 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1916 "org.freedesktop.systemd1.Activator", "ActivationFailure");
1920 const char *unit = "dbus-com.example.SystemdActivatable3.service";
1921 const char *error_name = "com.example.Nope";
1922 const char *error_message = "Computer says no";
1924 if (!dbus_message_append_args (m,
1925 DBUS_TYPE_STRING, &unit,
1926 DBUS_TYPE_STRING, &error_name,
1927 DBUS_TYPE_STRING, &error_message,
1933 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1935 dbus_connection_send (f->systemd, m, NULL);
1936 dbus_message_unref (m);
1938 /* The monitor sees activation fail */
1940 /* Once again, we see the activation request and the reason. */
1941 while (g_queue_get_length (&f->monitored) < 1)
1942 test_main_context_iterate (f->ctx, TRUE);
1944 m = g_queue_pop_head (&f->monitored);
1945 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1946 "com.example.Nope");
1947 dbus_message_unref (m);
1949 #endif /* DBUS_UNIX */
1952 teardown (Fixture *f,
1953 gconstpointer context G_GNUC_UNUSED)
1955 dbus_error_free (&f->e);
1956 g_clear_error (&f->ge);
1958 if (f->monitor != NULL)
1960 dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1961 dbus_connection_close (f->monitor);
1962 dbus_connection_unref (f->monitor);
1966 if (f->sender != NULL)
1968 dbus_connection_close (f->sender);
1969 dbus_connection_unref (f->sender);
1973 if (f->recipient != NULL)
1975 dbus_connection_remove_filter (f->recipient, recipient_check_filter, f);
1976 if (f->recipient_enqueue_filter_added)
1977 dbus_connection_remove_filter (f->recipient, recipient_enqueue_filter,
1979 dbus_connection_close (f->recipient);
1980 dbus_connection_unref (f->recipient);
1981 f->recipient = NULL;
1984 if (f->systemd != NULL)
1986 dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1987 dbus_connection_close (f->systemd);
1988 dbus_connection_unref (f->systemd);
1992 if (f->activated != NULL)
1994 dbus_connection_remove_filter (f->activated, activated_filter, f);
1995 dbus_connection_close (f->activated);
1996 dbus_connection_unref (f->activated);
1997 f->activated = NULL;
2000 if (f->daemon_pid != 0)
2002 test_kill_pid (f->daemon_pid);
2003 g_spawn_close_pid (f->daemon_pid);
2007 test_main_context_unref (f->ctx);
2009 g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
2010 g_queue_clear (&f->monitored);
2012 g_queue_foreach (&f->received, (GFunc) dbus_message_unref, NULL);
2013 g_queue_clear (&f->received);
2015 g_free (f->address);
2022 test_init (&argc, &argv);
2024 g_test_add ("/monitor/invalid", Fixture, NULL,
2025 setup, test_invalid, teardown);
2026 g_test_add ("/monitor/become", Fixture, &side_effects_config,
2027 setup, test_become_monitor, teardown);
2028 g_test_add ("/monitor/broadcast", Fixture, NULL,
2029 setup, test_broadcast, teardown);
2030 g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
2031 setup, test_forbidden_broadcast, teardown);
2032 g_test_add ("/monitor/unicast-signal", Fixture, NULL,
2033 setup, test_unicast_signal, teardown);
2034 g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
2035 setup, test_forbidden, teardown);
2036 g_test_add ("/monitor/method-call", Fixture, NULL,
2037 setup, test_method_call, teardown);
2038 g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
2039 setup, test_forbidden_method_call, teardown);
2040 g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
2041 setup, test_dbus_daemon, teardown);
2042 g_test_add ("/monitor/selective", Fixture, &selective_config,
2043 setup, test_selective, teardown);
2044 g_test_add ("/monitor/well-known-destination",
2045 Fixture, &well_known_destination_config,
2046 setup, test_well_known_destination, teardown);
2047 g_test_add ("/monitor/unique-destination",
2049 setup, test_unique_destination, teardown);
2050 g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
2051 setup, test_unicast_signal, teardown);
2052 g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
2053 setup, test_unicast_signal, teardown);
2054 g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
2055 setup, test_unicast_signal, teardown);
2056 g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
2057 setup, test_unicast_signal, teardown);
2060 /* this relies on the systemd activation code path */
2061 g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
2062 setup, test_activation, teardown);
2065 return g_test_run ();