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;
54 const char *monitor_name;
55 const char *sender_name;
56 const char *recipient_name;
58 DBusConnection *systemd;
59 const char *systemd_name;
60 DBusMessage *systemd_message;
61 DBusConnection *activated;
62 const char *activated_name;
63 DBusMessage *activated_message;
66 static const char * const no_match_rules[] = {
70 static const char * const wildcard_match_rules[] = {
76 static const char * const eavesdrop_match_rules[] = {
82 static const char * const no_eavesdrop_match_rules[] = {
88 static const char * const selective_match_rules[] = {
89 "interface='com.example.Interesting'",
90 "interface='com.example.Fun'",
95 static const char * const well_known_destination_match_rules[] = {
96 "destination='com.example.Recipient'",
100 static Config forbidding_config = {
101 "valid-config-files/forbidding.conf",
106 static Config wildcard_config = {
108 wildcard_match_rules,
112 static Config selective_config = {
114 selective_match_rules,
118 static Config well_known_destination_config = {
120 well_known_destination_match_rules,
124 static Config no_rules_config = {
130 static Config eavesdrop_config = {
132 eavesdrop_match_rules,
136 static Config no_eavesdrop_config = {
138 no_eavesdrop_match_rules,
143 static Config fake_systemd_config = {
144 "valid-config-files/systemd-activation.conf",
150 static Config side_effects_config = {
156 static inline const char *
157 not_null2 (const char *x,
158 const char *fallback)
166 static inline const char *
167 not_null (const char *x)
169 return not_null2 (x, "(null)");
172 #define log_message(m) _log_message (m, __FILE__, __LINE__)
176 _log_message (DBusMessage *m,
180 g_test_message ("%s:%d: message type %d (%s)", file, line,
181 dbus_message_get_type (m),
182 dbus_message_type_to_string (dbus_message_get_type (m)));
183 g_test_message ("\tfrom: %s",
184 not_null2 (dbus_message_get_sender (m), "(dbus-daemon)"));
185 g_test_message ("\tto: %s",
186 not_null2 (dbus_message_get_destination (m), "(broadcast)"));
187 g_test_message ("\tpath: %s",
188 not_null (dbus_message_get_path (m)));
189 g_test_message ("\tinterface: %s",
190 not_null (dbus_message_get_interface (m)));
191 g_test_message ("\tmember: %s",
192 not_null (dbus_message_get_member (m)));
193 g_test_message ("\tsignature: %s",
194 not_null (dbus_message_get_signature (m)));
195 g_test_message ("\terror name: %s",
196 not_null (dbus_message_get_error_name (m)));
198 if (strcmp ("s", dbus_message_get_signature (m)) == 0)
200 DBusError e = DBUS_ERROR_INIT;
203 dbus_message_get_args (m, &e,
204 DBUS_TYPE_STRING, &s,
206 test_assert_no_error (&e);
207 g_test_message ("\tstring payload: %s", s);
211 /* these are macros so they get the right line number */
213 #define assert_hello(m) \
215 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
216 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
217 g_assert_cmpstr (dbus_message_get_destination (m), ==, DBUS_SERVICE_DBUS); \
218 g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
219 g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
220 g_assert_cmpstr (dbus_message_get_member (m), ==, "Hello"); \
221 g_assert_cmpstr (dbus_message_get_signature (m), ==, ""); \
222 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
223 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
226 #define assert_hello_reply(m) \
228 DBusError _e = DBUS_ERROR_INIT; \
231 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
232 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
233 g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
234 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
235 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
236 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
237 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
238 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
239 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
241 dbus_message_get_args (m, &_e, \
242 DBUS_TYPE_STRING, &_s, \
243 DBUS_TYPE_INVALID); \
244 test_assert_no_error (&_e); \
245 g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
248 #define assert_name_acquired(m) \
250 DBusError _e = DBUS_ERROR_INIT; \
253 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
254 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
255 g_assert_cmpstr (dbus_message_get_sender (m), ==, DBUS_SERVICE_DBUS); \
256 g_assert_cmpstr (dbus_message_get_path (m), ==, DBUS_PATH_DBUS); \
257 g_assert_cmpstr (dbus_message_get_interface (m), ==, DBUS_INTERFACE_DBUS); \
258 g_assert_cmpstr (dbus_message_get_member (m), ==, "NameAcquired"); \
259 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
260 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
261 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
263 dbus_message_get_args (m, &_e, \
264 DBUS_TYPE_STRING, &_s, \
265 DBUS_TYPE_INVALID); \
266 test_assert_no_error (&_e); \
267 g_assert_cmpstr (dbus_message_get_destination (m), ==, _s); \
270 #define assert_method_call(m, sender, \
271 destination, path, iface, method, signature) \
273 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
274 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_CALL)); \
275 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
276 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
277 g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
278 g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
279 g_assert_cmpstr (dbus_message_get_member (m), ==, method); \
280 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
281 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
282 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
285 #define assert_signal(m, \
286 sender, path, iface, member, signature, \
289 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
290 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_SIGNAL)); \
291 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
292 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
293 g_assert_cmpstr (dbus_message_get_path (m), ==, path); \
294 g_assert_cmpstr (dbus_message_get_interface (m), ==, iface); \
295 g_assert_cmpstr (dbus_message_get_member (m), ==, member); \
296 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
297 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
298 g_assert_cmpint (dbus_message_get_reply_serial (m), ==, 0); \
301 #define assert_method_reply(m, sender, destination, signature) \
303 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
304 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_METHOD_RETURN)); \
305 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
306 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
307 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
308 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
309 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
310 g_assert_cmpstr (dbus_message_get_signature (m), ==, signature); \
311 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
312 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
315 #define assert_error_reply(m, sender, destination, error_name) \
317 g_assert_cmpstr (dbus_message_type_to_string (dbus_message_get_type (m)), \
318 ==, dbus_message_type_to_string (DBUS_MESSAGE_TYPE_ERROR)); \
319 g_assert_cmpstr (dbus_message_get_sender (m), ==, sender); \
320 g_assert_cmpstr (dbus_message_get_destination (m), ==, destination); \
321 g_assert_cmpstr (dbus_message_get_error_name (m), ==, error_name); \
322 g_assert_cmpstr (dbus_message_get_path (m), ==, NULL); \
323 g_assert_cmpstr (dbus_message_get_interface (m), ==, NULL); \
324 g_assert_cmpstr (dbus_message_get_member (m), ==, NULL); \
325 g_assert_cmpstr (dbus_message_get_signature (m), ==, "s"); \
326 g_assert_cmpint (dbus_message_get_serial (m), !=, 0); \
327 g_assert_cmpint (dbus_message_get_reply_serial (m), !=, 0); \
330 /* This is called after processing pending replies to our own method
331 * calls, but before anything else.
333 static DBusHandlerResult
334 monitor_filter (DBusConnection *connection,
335 DBusMessage *message,
338 Fixture *f = user_data;
340 g_assert_cmpstr (dbus_message_get_interface (message), !=,
341 "com.example.Tedious");
343 /* we are not interested in the monitor getting NameAcquired or NameLost
345 if (f->config == NULL || !f->config->care_about_our_names)
347 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
349 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
352 DBusError e = DBUS_ERROR_INIT;
355 dbus_message_get_args (message, &e,
356 DBUS_TYPE_STRING, &s,
358 test_assert_no_error (&e);
360 if (strcmp (s, f->monitor_name) == 0)
363 return DBUS_HANDLER_RESULT_HANDLED;
368 g_queue_push_tail (&f->monitored, dbus_message_ref (message));
370 return DBUS_HANDLER_RESULT_HANDLED;
373 static DBusHandlerResult
374 recipient_filter (DBusConnection *connection,
375 DBusMessage *message,
378 g_assert_cmpstr (dbus_message_get_interface (message), !=,
379 "com.example.CannotSend");
380 g_assert_cmpstr (dbus_message_get_interface (message), !=,
381 "com.example.CannotReceive");
383 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
386 static DBusHandlerResult
387 systemd_filter (DBusConnection *connection,
388 DBusMessage *message,
391 Fixture *f = user_data;
393 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
395 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
398 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
401 g_assert (f->systemd_message == NULL);
402 f->systemd_message = dbus_message_ref (message);
404 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
407 static DBusHandlerResult
408 activated_filter (DBusConnection *connection,
409 DBusMessage *message,
412 Fixture *f = user_data;
414 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
416 dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
419 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
422 g_assert (f->activated_message == NULL);
423 f->activated_message = dbus_message_ref (message);
425 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
429 take_well_known_name (Fixture *f,
430 DBusConnection *connection,
435 ret = dbus_bus_request_name (connection, name,
436 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
437 test_assert_no_error (&f->e);
438 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
443 gconstpointer context)
447 f->ctx = test_main_context_get ();
450 dbus_error_init (&f->e);
452 f->address = test_get_dbus_daemon (f->config ? f->config->config_file : NULL,
453 TEST_USER_ME, NULL, &f->daemon_pid);
455 if (f->address == NULL)
458 f->monitor = test_connect_to_bus (f->ctx, f->address);
459 f->monitor_name = dbus_bus_get_unique_name (f->monitor);
460 f->sender = test_connect_to_bus (f->ctx, f->address);
461 f->sender_name = dbus_bus_get_unique_name (f->sender);
462 f->recipient = test_connect_to_bus (f->ctx, f->address);
463 f->recipient_name = dbus_bus_get_unique_name (f->recipient);
465 if (!dbus_connection_add_filter (f->monitor, monitor_filter, f, NULL))
468 if (!dbus_connection_add_filter (f->recipient, recipient_filter, f, NULL))
473 become_monitor (Fixture *f,
474 const Config *config)
479 DBusMessageIter appender, array_appender;
480 const char * const *match_rules;
482 dbus_uint32_t zero = 0;
484 dbus_connection_set_route_peer_messages (f->monitor, TRUE);
489 if (config != NULL && config->match_rules != NULL)
490 match_rules = config->match_rules;
492 match_rules = wildcard_match_rules;
494 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
495 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
500 dbus_message_iter_init_append (m, &appender);
502 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
506 for (i = 0; match_rules[i] != NULL; i++)
508 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
513 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
514 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
517 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
518 DBUS_TIMEOUT_USE_DEFAULT) ||
522 dbus_message_unref (m);
525 if (dbus_pending_call_get_completed (pc))
526 test_pending_call_store_reply (pc, &m);
527 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
532 test_main_context_iterate (f->ctx, TRUE);
534 ok = dbus_message_get_args (m, &f->e,
536 test_assert_no_error (&f->e);
539 dbus_pending_call_unref (pc);
540 dbus_message_unref (m);
545 * Test what happens if the method call arguments are invalid.
548 test_invalid (Fixture *f,
549 gconstpointer context)
554 DBusMessageIter appender, array_appender;
555 dbus_uint32_t zero = 0;
556 dbus_uint32_t invalid_flags = G_MAXUINT32;
559 if (f->address == NULL)
562 dbus_connection_set_route_peer_messages (f->monitor, TRUE);
564 /* Try to become a monitor but specify nonzero flags - not allowed */
566 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
567 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
572 dbus_message_iter_init_append (m, &appender);
574 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
578 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
579 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32,
583 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
584 DBUS_TIMEOUT_USE_DEFAULT) ||
588 dbus_message_unref (m);
591 if (dbus_pending_call_get_completed (pc))
592 test_pending_call_store_reply (pc, &m);
593 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
598 test_main_context_iterate (f->ctx, TRUE);
600 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
601 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
602 DBUS_ERROR_INVALID_ARGS);
604 /* Try to become a monitor but use the wrong object path - not allowed
605 * (security hardening against inappropriate XML policy rules) */
607 dbus_pending_call_unref (pc);
608 dbus_message_unref (m);
610 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
611 "/", DBUS_INTERFACE_MONITORING, "BecomeMonitor");
616 dbus_message_iter_init_append (m, &appender);
618 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
622 if (!dbus_message_iter_close_container (&appender, &array_appender) ||
623 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
626 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
627 DBUS_TIMEOUT_USE_DEFAULT) ||
631 dbus_message_unref (m);
634 if (dbus_pending_call_get_completed (pc))
635 test_pending_call_store_reply (pc, &m);
636 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
641 test_main_context_iterate (f->ctx, TRUE);
643 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
644 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
645 DBUS_ERROR_UNKNOWN_INTERFACE);
647 /* Try to become a monitor but specify a bad match rule -
648 * also not allowed */
650 dbus_pending_call_unref (pc);
651 dbus_message_unref (m);
653 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
654 DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
659 dbus_message_iter_init_append (m, &appender);
661 if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
665 /* Syntactically incorrect match rule taken from #92298 - was probably
666 * intended to be path='/modules/...'
668 s = "interface='org.kde.walletd',member='/modules/kwalletd/org.kde.KWallet/walletOpened'";
670 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
672 !dbus_message_iter_close_container (&appender, &array_appender) ||
673 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero) ||
674 !dbus_connection_send_with_reply (f->monitor, m, &pc,
675 DBUS_TIMEOUT_USE_DEFAULT) ||
679 dbus_message_unref (m);
682 if (dbus_pending_call_get_completed (pc))
683 test_pending_call_store_reply (pc, &m);
684 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
689 test_main_context_iterate (f->ctx, TRUE);
691 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
692 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
693 DBUS_ERROR_MATCH_RULE_INVALID);
695 dbus_pending_call_unref (pc);
696 dbus_message_unref (m);
698 /* We did not become a monitor, so we can still call methods. */
701 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
702 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetId");
707 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
708 DBUS_TIMEOUT_USE_DEFAULT) ||
712 dbus_message_unref (m);
715 if (dbus_pending_call_get_completed (pc))
716 test_pending_call_store_reply (pc, &m);
717 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
722 test_main_context_iterate (f->ctx, TRUE);
724 if (dbus_set_error_from_message (&f->e, m))
725 g_error ("%s: %s", f->e.name, f->e.message);
727 ok = dbus_message_get_args (m, &f->e,
728 DBUS_TYPE_STRING, &s,
730 test_assert_no_error (&f->e);
732 g_assert_cmpstr (s, !=, NULL);
733 g_assert_cmpstr (s, !=, "");
735 dbus_pending_call_unref (pc);
736 dbus_message_unref (m);
740 * Test the side-effects of becoming a monitor.
743 test_become_monitor (Fixture *f,
744 gconstpointer context)
748 dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
749 dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
751 if (f->address == NULL)
754 ret = dbus_bus_request_name (f->monitor, "com.example.A",
755 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
756 test_assert_no_error (&f->e);
757 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
759 ret = dbus_bus_request_name (f->monitor, "com.example.B",
760 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
761 test_assert_no_error (&f->e);
762 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
764 ret = dbus_bus_request_name (f->monitor, "com.example.C",
765 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
766 test_assert_no_error (&f->e);
767 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
769 while (!got_unique || !got_a || !got_b || !got_c)
771 if (g_queue_is_empty (&f->monitored))
772 test_main_context_iterate (f->ctx, TRUE);
774 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
776 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
780 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
781 DBUS_TYPE_STRING, &name,
784 g_assert_cmpstr (dbus_message_get_path (m), ==,
787 test_assert_no_error (&f->e);
790 if (g_str_equal (name, f->monitor_name))
792 g_assert (!got_unique);
795 else if (g_str_equal (name, "com.example.A"))
800 else if (g_str_equal (name, "com.example.B"))
807 g_assert_cmpstr (name, ==, "com.example.C");
814 g_error ("unexpected message %s.%s",
815 dbus_message_get_interface (m),
816 dbus_message_get_member (m));
819 dbus_message_unref (m);
823 become_monitor (f, NULL);
825 while (!lost_unique || !lost_a || !lost_b || !lost_c)
827 if (g_queue_is_empty (&f->monitored))
828 test_main_context_iterate (f->ctx, TRUE);
830 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
832 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
836 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
837 DBUS_TYPE_STRING, &name,
840 test_assert_no_error (&f->e);
843 if (g_str_equal (name, f->monitor_name))
845 g_assert (!lost_unique);
848 else if (g_str_equal (name, "com.example.A"))
853 else if (g_str_equal (name, "com.example.B"))
860 g_assert_cmpstr (name, ==, "com.example.C");
867 g_error ("unexpected message %s.%s",
868 dbus_message_get_interface (m),
869 dbus_message_get_member (m));
872 dbus_message_unref (m);
876 /* Calling methods is forbidden; we get disconnected. */
877 dbus_bus_add_match (f->monitor, "", &f->e);
878 g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
879 g_assert (!dbus_connection_get_is_connected (f->monitor));
883 if (g_queue_is_empty (&f->monitored))
884 test_main_context_iterate (f->ctx, TRUE);
886 /* When we iterate all the connection's messages, we see ourselves
887 * losing all our names, then we're disconnected. */
888 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
890 if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
892 dbus_message_unref (m);
897 g_error ("unexpected message %s.%s",
898 dbus_message_get_interface (m),
899 dbus_message_get_member (m));
902 dbus_message_unref (m);
914 test_broadcast (Fixture *f,
915 gconstpointer context)
919 if (f->address == NULL)
922 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
923 test_assert_no_error (&f->e);
925 become_monitor (f, NULL);
927 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
928 dbus_connection_send (f->sender, m, NULL);
929 dbus_message_unref (m);
931 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
932 dbus_connection_send (f->sender, m, NULL);
933 dbus_message_unref (m);
935 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
936 dbus_connection_send (f->sender, m, NULL);
937 dbus_message_unref (m);
939 while (g_queue_get_length (&f->monitored) < 3)
940 test_main_context_iterate (f->ctx, TRUE);
942 m = g_queue_pop_head (&f->monitored);
943 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
944 "BroadcastSignal1", "", NULL);
945 dbus_message_unref (m);
947 m = g_queue_pop_head (&f->monitored);
948 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
949 "BroadcastSignal2", "", NULL);
950 dbus_message_unref (m);
952 m = g_queue_pop_head (&f->monitored);
953 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
954 "BroadcastSignal3", "", NULL);
955 dbus_message_unref (m);
957 m = g_queue_pop_head (&f->monitored);
958 g_assert (m == NULL);
962 test_forbidden_broadcast (Fixture *f,
963 gconstpointer context)
967 if (f->address == NULL)
970 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
971 test_assert_no_error (&f->e);
973 become_monitor (f, NULL);
975 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
977 dbus_connection_send (f->sender, m, NULL);
978 dbus_message_unref (m);
980 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
982 dbus_connection_send (f->sender, m, NULL);
983 dbus_message_unref (m);
985 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
987 dbus_connection_send (f->sender, m, NULL);
988 dbus_message_unref (m);
990 while (g_queue_get_length (&f->monitored) < 6)
991 test_main_context_iterate (f->ctx, TRUE);
993 m = g_queue_pop_head (&f->monitored);
994 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
995 "BroadcastSignal1", "", NULL);
996 dbus_message_unref (m);
998 m = g_queue_pop_head (&f->monitored);
999 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1000 DBUS_ERROR_ACCESS_DENIED);
1001 dbus_message_unref (m);
1003 m = g_queue_pop_head (&f->monitored);
1004 assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
1005 "BroadcastSignal2", "", NULL);
1006 dbus_message_unref (m);
1008 m = g_queue_pop_head (&f->monitored);
1009 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1010 DBUS_ERROR_ACCESS_DENIED);
1011 dbus_message_unref (m);
1013 m = g_queue_pop_head (&f->monitored);
1014 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
1015 "BroadcastSignal3", "", NULL);
1016 dbus_message_unref (m);
1018 m = g_queue_pop_head (&f->monitored);
1019 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1020 DBUS_ERROR_ACCESS_DENIED);
1021 dbus_message_unref (m);
1023 m = g_queue_pop_head (&f->monitored);
1024 g_assert (m == NULL);
1028 test_unicast_signal (Fixture *f,
1029 gconstpointer context)
1033 if (f->address == NULL)
1036 become_monitor (f, NULL);
1038 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1039 if (!dbus_message_set_destination (m, f->recipient_name))
1041 dbus_connection_send (f->sender, m, NULL);
1042 dbus_message_unref (m);
1044 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1045 if (!dbus_message_set_destination (m, f->recipient_name))
1047 dbus_connection_send (f->sender, m, NULL);
1048 dbus_message_unref (m);
1050 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1051 if (!dbus_message_set_destination (m, f->recipient_name))
1053 dbus_connection_send (f->sender, m, NULL);
1054 dbus_message_unref (m);
1056 while (g_queue_get_length (&f->monitored) < 3)
1057 test_main_context_iterate (f->ctx, TRUE);
1059 m = g_queue_pop_head (&f->monitored);
1060 assert_signal (m, f->sender_name, "/foo",
1061 "com.example.bar", "UnicastSignal1", "", f->recipient_name);
1062 dbus_message_unref (m);
1064 m = g_queue_pop_head (&f->monitored);
1065 assert_signal (m, f->sender_name, "/foo",
1066 "com.example.bar", "UnicastSignal2", "", f->recipient_name);
1067 dbus_message_unref (m);
1069 m = g_queue_pop_head (&f->monitored);
1070 assert_signal (m, f->sender_name, "/foo",
1071 "com.example.bar", "UnicastSignal3", "", f->recipient_name);
1072 dbus_message_unref (m);
1074 m = g_queue_pop_head (&f->monitored);
1075 g_assert (m == NULL);
1079 test_forbidden (Fixture *f,
1080 gconstpointer context)
1084 if (f->address == NULL)
1087 become_monitor (f, NULL);
1089 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1091 if (!dbus_message_set_destination (m, f->recipient_name))
1093 dbus_connection_send (f->sender, m, NULL);
1094 dbus_message_unref (m);
1096 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
1098 if (!dbus_message_set_destination (m, f->recipient_name))
1100 dbus_connection_send (f->sender, m, NULL);
1101 dbus_message_unref (m);
1103 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1105 if (!dbus_message_set_destination (m, f->recipient_name))
1107 dbus_connection_send (f->sender, m, NULL);
1108 dbus_message_unref (m);
1110 while (g_queue_get_length (&f->monitored) < 6)
1111 test_main_context_iterate (f->ctx, TRUE);
1113 m = g_queue_pop_head (&f->monitored);
1114 assert_signal (m, f->sender_name, "/foo",
1115 "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
1116 dbus_message_unref (m);
1118 m = g_queue_pop_head (&f->monitored);
1119 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1120 DBUS_ERROR_ACCESS_DENIED);
1121 dbus_message_unref (m);
1123 m = g_queue_pop_head (&f->monitored);
1124 assert_signal (m, f->sender_name, "/foo",
1125 "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
1126 dbus_message_unref (m);
1128 m = g_queue_pop_head (&f->monitored);
1129 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1130 DBUS_ERROR_ACCESS_DENIED);
1131 dbus_message_unref (m);
1133 m = g_queue_pop_head (&f->monitored);
1134 assert_signal (m, f->sender_name, "/foo",
1135 "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
1136 dbus_message_unref (m);
1138 m = g_queue_pop_head (&f->monitored);
1139 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1140 DBUS_ERROR_ACCESS_DENIED);
1141 dbus_message_unref (m);
1143 m = g_queue_pop_head (&f->monitored);
1144 g_assert (m == NULL);
1148 test_method_call (Fixture *f,
1149 gconstpointer context)
1153 if (f->address == NULL)
1156 become_monitor (f, NULL);
1158 /* regression test for
1159 * https://bugs.freedesktop.org/show_bug.cgi?id=90952 */
1160 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1161 DBUS_INTERFACE_PEER, "Ping");
1162 dbus_connection_send (f->sender, m, NULL);
1163 dbus_message_unref (m);
1165 while (g_queue_get_length (&f->monitored) < 2)
1166 test_main_context_iterate (f->ctx, TRUE);
1168 m = g_queue_pop_head (&f->monitored);
1169 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1170 DBUS_INTERFACE_PEER, "Ping", "");
1171 dbus_message_unref (m);
1173 m = g_queue_pop_head (&f->monitored);
1174 assert_method_reply (m, f->recipient_name, f->sender_name, "");
1175 dbus_message_unref (m);
1177 m = g_queue_pop_head (&f->monitored);
1178 g_assert (m == NULL);
1180 m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
1182 dbus_connection_send (f->sender, m, NULL);
1183 dbus_message_unref (m);
1185 while (g_queue_get_length (&f->monitored) < 2)
1186 test_main_context_iterate (f->ctx, TRUE);
1188 m = g_queue_pop_head (&f->monitored);
1189 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1190 "com.example.bar", "Call1", "");
1191 dbus_message_unref (m);
1193 m = g_queue_pop_head (&f->monitored);
1194 assert_error_reply (m, f->recipient_name, f->sender_name,
1195 DBUS_ERROR_UNKNOWN_METHOD);
1196 dbus_message_unref (m);
1198 m = g_queue_pop_head (&f->monitored);
1199 g_assert (m == NULL);
1203 test_forbidden_method_call (Fixture *f,
1204 gconstpointer context)
1208 if (f->address == NULL)
1211 become_monitor (f, NULL);
1213 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1214 "com.example.CannotSend", "Call1");
1215 dbus_connection_send (f->sender, m, NULL);
1216 dbus_message_unref (m);
1218 while (g_queue_get_length (&f->monitored) < 2)
1219 test_main_context_iterate (f->ctx, TRUE);
1221 m = g_queue_pop_head (&f->monitored);
1222 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1223 "com.example.CannotSend", "Call1", "");
1224 dbus_message_unref (m);
1226 m = g_queue_pop_head (&f->monitored);
1227 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1228 DBUS_ERROR_ACCESS_DENIED);
1229 dbus_message_unref (m);
1231 m = g_queue_pop_head (&f->monitored);
1232 g_assert (m == NULL);
1234 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1235 "com.example.CannotReceive", "Call2");
1236 dbus_connection_send (f->sender, m, NULL);
1237 dbus_message_unref (m);
1239 while (g_queue_get_length (&f->monitored) < 2)
1240 test_main_context_iterate (f->ctx, TRUE);
1242 m = g_queue_pop_head (&f->monitored);
1243 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1244 "com.example.CannotReceive", "Call2", "");
1245 dbus_message_unref (m);
1247 m = g_queue_pop_head (&f->monitored);
1248 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1249 DBUS_ERROR_ACCESS_DENIED);
1250 dbus_message_unref (m);
1252 m = g_queue_pop_head (&f->monitored);
1253 g_assert (m == NULL);
1257 test_dbus_daemon (Fixture *f,
1258 gconstpointer context)
1263 if (f->address == NULL)
1266 become_monitor (f, NULL);
1268 res = dbus_bus_request_name (f->sender, "com.example.Sender",
1269 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1270 test_assert_no_error (&f->e);
1271 g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1273 res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1274 test_assert_no_error (&f->e);
1275 g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1277 while (g_queue_get_length (&f->monitored) < 8)
1278 test_main_context_iterate (f->ctx, TRUE);
1280 m = g_queue_pop_head (&f->monitored);
1281 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1282 DBUS_INTERFACE_DBUS, "RequestName", "su");
1283 dbus_message_unref (m);
1285 m = g_queue_pop_head (&f->monitored);
1286 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1287 "NameOwnerChanged", "sss", NULL);
1288 dbus_message_unref (m);
1290 /* FIXME: should we get this? */
1291 m = g_queue_pop_head (&f->monitored);
1292 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1293 "NameAcquired", "s", f->sender_name);
1294 dbus_message_unref (m);
1296 m = g_queue_pop_head (&f->monitored);
1297 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1298 dbus_message_unref (m);
1300 m = g_queue_pop_head (&f->monitored);
1301 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1302 DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1303 dbus_message_unref (m);
1305 /* FIXME: should we get this? */
1306 m = g_queue_pop_head (&f->monitored);
1307 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1308 "NameLost", "s", f->sender_name);
1309 dbus_message_unref (m);
1311 m = g_queue_pop_head (&f->monitored);
1312 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1313 "NameOwnerChanged", "sss", NULL);
1314 dbus_message_unref (m);
1316 m = g_queue_pop_head (&f->monitored);
1317 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1318 dbus_message_unref (m);
1320 m = g_queue_pop_head (&f->monitored);
1321 g_assert (m == NULL);
1325 test_selective (Fixture *f,
1326 gconstpointer context)
1330 if (f->address == NULL)
1333 /* Match rules added before becoming a monitor should be cleared:
1334 * if they weren't, this test would get Interesting twice, then Tedious,
1335 * and only see Fun after that. */
1336 dbus_bus_add_match (f->monitor,
1337 "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1338 test_assert_no_error (&f->e);
1339 dbus_bus_add_match (f->monitor,
1340 "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1341 test_assert_no_error (&f->e);
1343 become_monitor (f, NULL);
1345 m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1347 if (!dbus_message_set_destination (m, f->recipient_name))
1349 dbus_connection_send (f->sender, m, NULL);
1350 dbus_message_unref (m);
1352 m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1354 if (!dbus_message_set_destination (m, f->recipient_name))
1356 dbus_connection_send (f->sender, m, NULL);
1357 dbus_message_unref (m);
1359 m = dbus_message_new_signal ("/foo", "com.example.Fun",
1361 if (!dbus_message_set_destination (m, f->recipient_name))
1363 dbus_connection_send (f->sender, m, NULL);
1364 dbus_message_unref (m);
1366 while (g_queue_get_length (&f->monitored) < 2)
1367 test_main_context_iterate (f->ctx, TRUE);
1369 /* We get the interesting signal and the fun signal, but not the tedious
1372 m = g_queue_pop_head (&f->monitored);
1373 assert_signal (m, f->sender_name, "/foo",
1374 "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1375 dbus_message_unref (m);
1377 m = g_queue_pop_head (&f->monitored);
1378 assert_signal (m, f->sender_name, "/foo",
1379 "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1380 dbus_message_unref (m);
1382 m = g_queue_pop_head (&f->monitored);
1383 g_assert (m == NULL);
1387 test_well_known_destination (Fixture *f,
1388 gconstpointer context)
1392 if (f->address == NULL)
1395 take_well_known_name (f, f->recipient, "com.example.Recipient");
1396 /* we don't expect_take_well_known_name here because the
1397 * monitor isn't up yet */
1399 become_monitor (f, NULL);
1401 /* The sender sends a message to itself. It will not be observed. */
1402 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1403 if (!dbus_message_set_destination (m, f->sender_name))
1405 dbus_connection_send (f->sender, m, NULL);
1406 dbus_message_unref (m);
1408 /* The sender sends a message to the recipient by well-known name.
1409 * It will be observed. */
1410 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1411 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1413 dbus_connection_send (f->sender, m, NULL);
1414 dbus_message_unref (m);
1416 /* The sender sends a message to the recipient by unique name.
1417 * It will still be observed. */
1418 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1419 if (!dbus_message_set_destination (m, f->recipient_name))
1421 dbus_connection_send (f->sender, m, NULL);
1422 dbus_message_unref (m);
1424 while (g_queue_get_length (&f->monitored) < 2)
1425 test_main_context_iterate (f->ctx, TRUE);
1427 m = g_queue_pop_head (&f->monitored);
1428 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1429 "Observed1", "", "com.example.Recipient");
1430 dbus_message_unref (m);
1432 m = g_queue_pop_head (&f->monitored);
1433 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1434 "Observed2", "", f->recipient_name);
1435 dbus_message_unref (m);
1437 m = g_queue_pop_head (&f->monitored);
1438 g_assert (m == NULL);
1442 test_unique_destination (Fixture *f,
1443 gconstpointer context)
1448 NULL, /* match rules */
1451 const gchar *match_rules[2] = { NULL, NULL };
1454 if (f->address == NULL)
1457 take_well_known_name (f, f->recipient, "com.example.Recipient");
1458 /* we don't expect_take_well_known_name here because the
1459 * monitor isn't up yet */
1461 rule = g_strdup_printf ("destination='%s'", f->recipient_name);
1463 g_test_queue_free (rule);
1464 match_rules[0] = rule;
1465 config.match_rules = match_rules;
1467 become_monitor (f, &config);
1469 /* The sender sends a message to itself. It will not be observed. */
1470 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1471 if (!dbus_message_set_destination (m, f->sender_name))
1473 dbus_connection_send (f->sender, m, NULL);
1474 dbus_message_unref (m);
1476 /* The sender sends a message to the recipient by well-known name.
1477 * It will be observed. */
1478 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1479 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1481 dbus_connection_send (f->sender, m, NULL);
1482 dbus_message_unref (m);
1484 /* The sender sends a message to the recipient by unique name.
1485 * It will still be observed. */
1486 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1487 if (!dbus_message_set_destination (m, f->recipient_name))
1489 dbus_connection_send (f->sender, m, NULL);
1490 dbus_message_unref (m);
1492 while (g_queue_get_length (&f->monitored) < 2)
1493 test_main_context_iterate (f->ctx, TRUE);
1495 m = g_queue_pop_head (&f->monitored);
1496 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1497 "Observed1", "", "com.example.Recipient");
1498 dbus_message_unref (m);
1500 m = g_queue_pop_head (&f->monitored);
1501 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1502 "Observed2", "", f->recipient_name);
1503 dbus_message_unref (m);
1505 m = g_queue_pop_head (&f->monitored);
1506 g_assert (m == NULL);
1510 /* currently only used for the systemd activation test */
1512 expect_new_connection (Fixture *f)
1516 while (g_queue_get_length (&f->monitored) < 4)
1517 test_main_context_iterate (f->ctx, TRUE);
1519 m = g_queue_pop_head (&f->monitored);
1521 dbus_message_unref (m);
1523 m = g_queue_pop_head (&f->monitored);
1524 assert_hello_reply (m);
1525 dbus_message_unref (m);
1527 m = g_queue_pop_head (&f->monitored);
1528 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1529 "NameOwnerChanged", "sss", NULL);
1530 dbus_message_unref (m);
1532 m = g_queue_pop_head (&f->monitored);
1533 assert_name_acquired (m);
1534 dbus_message_unref (m);
1537 /* currently only used for the systemd activation test */
1539 expect_take_well_known_name (Fixture *f,
1540 DBusConnection *connection,
1544 const char *connection_name = dbus_bus_get_unique_name (connection);
1546 while (g_queue_get_length (&f->monitored) < 4)
1547 test_main_context_iterate (f->ctx, TRUE);
1549 m = g_queue_pop_head (&f->monitored);
1550 assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1551 DBUS_INTERFACE_DBUS, "RequestName", "su");
1552 dbus_message_unref (m);
1554 m = g_queue_pop_head (&f->monitored);
1555 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1556 "NameOwnerChanged", "sss", NULL);
1557 dbus_message_unref (m);
1559 m = g_queue_pop_head (&f->monitored);
1560 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1561 "NameAcquired", "s", connection_name);
1562 dbus_message_unref (m);
1564 m = g_queue_pop_head (&f->monitored);
1565 assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1566 dbus_message_unref (m);
1570 test_activation (Fixture *f,
1571 gconstpointer context)
1575 if (f->address == NULL)
1578 become_monitor (f, NULL);
1580 /* The sender sends a message to an activatable service. */
1581 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1582 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1584 dbus_connection_send (f->sender, m, NULL);
1585 dbus_message_unref (m);
1587 /* We observe the activation request, and the message that caused it,
1588 * before systemd has even joined the bus. */
1589 while (g_queue_get_length (&f->monitored) < 2)
1590 test_main_context_iterate (f->ctx, TRUE);
1592 m = g_queue_pop_head (&f->monitored);
1593 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1594 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1595 "org.freedesktop.systemd1");
1596 dbus_message_unref (m);
1597 m = g_queue_pop_head (&f->monitored);
1598 assert_signal (m, f->sender_name, "/foo",
1599 "com.example.bar", "UnicastSignal1", "",
1600 "com.example.SystemdActivatable1");
1601 dbus_message_unref (m);
1603 /* The fake systemd connects to the bus. */
1604 f->systemd = test_connect_to_bus (f->ctx, f->address);
1605 if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1607 f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1609 expect_new_connection (f);
1610 take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1611 expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1613 /* It gets its activation request. */
1614 while (f->systemd_message == NULL)
1615 test_main_context_iterate (f->ctx, TRUE);
1617 m = f->systemd_message;
1618 f->systemd_message = NULL;
1619 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1620 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1621 "org.freedesktop.systemd1");
1622 dbus_message_unref (m);
1624 /* systemd starts the activatable service. */
1625 f->activated = test_connect_to_bus (f->ctx, f->address);
1626 if (!dbus_connection_add_filter (f->activated, activated_filter,
1629 f->activated_name = dbus_bus_get_unique_name (f->activated);
1631 expect_new_connection (f);
1632 take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1633 expect_take_well_known_name (f, f->activated,
1634 "com.example.SystemdActivatable1");
1636 /* The message is delivered to the activatable service. */
1637 while (f->activated_message == NULL)
1638 test_main_context_iterate (f->ctx, TRUE);
1640 m = f->activated_message;
1641 f->activated_message = NULL;
1642 assert_signal (m, f->sender_name, "/foo",
1643 "com.example.bar", "UnicastSignal1", "",
1644 "com.example.SystemdActivatable1");
1645 dbus_message_unref (m);
1647 /* The sender sends a message to a different activatable service. */
1648 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1649 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1651 dbus_connection_send (f->sender, m, NULL);
1652 dbus_message_unref (m);
1654 /* This time systemd is already ready for it. */
1655 while (g_queue_get_length (&f->monitored) < 2 ||
1656 f->systemd_message == NULL)
1657 test_main_context_iterate (f->ctx, TRUE);
1659 m = f->systemd_message;
1660 f->systemd_message = NULL;
1661 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1662 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1663 "org.freedesktop.systemd1");
1664 dbus_message_unref (m);
1666 /* The monitor sees the activation request and the signal that
1668 m = g_queue_pop_head (&f->monitored);
1669 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1670 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1671 "org.freedesktop.systemd1");
1672 dbus_message_unref (m);
1673 m = g_queue_pop_head (&f->monitored);
1674 assert_signal (m, f->sender_name, "/foo",
1675 "com.example.bar", "UnicastSignal2", "",
1676 "com.example.SystemdActivatable2");
1677 dbus_message_unref (m);
1679 /* The activatable service takes its name. Here I'm faking it by using
1680 * an existing connection. */
1681 take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1683 /* The message is delivered to the activatable service.
1684 * Implementation detail: the monitor sees this happen before it even
1685 * sees that the name request happened, which is pretty odd. */
1686 while (f->activated_message == NULL)
1687 test_main_context_iterate (f->ctx, TRUE);
1689 m = f->activated_message;
1690 f->activated_message = NULL;
1691 assert_signal (m, f->sender_name, "/foo",
1692 "com.example.bar", "UnicastSignal2", "",
1693 "com.example.SystemdActivatable2");
1694 dbus_message_unref (m);
1696 expect_take_well_known_name (f, f->activated,
1697 "com.example.SystemdActivatable2");
1699 /* A third activation. */
1700 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1701 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1703 dbus_connection_send (f->sender, m, NULL);
1704 dbus_message_unref (m);
1706 /* Once again, we see the activation request and the reason. */
1707 while (g_queue_get_length (&f->monitored) < 2)
1708 test_main_context_iterate (f->ctx, TRUE);
1710 m = g_queue_pop_head (&f->monitored);
1711 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1712 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1713 "org.freedesktop.systemd1");
1714 dbus_message_unref (m);
1715 m = g_queue_pop_head (&f->monitored);
1716 assert_signal (m, f->sender_name, "/foo",
1717 "com.example.bar", "UnicastSignal3", "",
1718 "com.example.SystemdActivatable3");
1719 dbus_message_unref (m);
1721 /* systemd gets the request too. */
1722 while (f->systemd_message == NULL)
1723 test_main_context_iterate (f->ctx, TRUE);
1725 m = f->systemd_message;
1726 f->systemd_message = NULL;
1727 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1728 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1729 "org.freedesktop.systemd1");
1730 dbus_message_unref (m);
1732 /* This time activation fails */
1733 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1734 "org.freedesktop.systemd1.Activator", "ActivationFailure");
1738 const char *unit = "dbus-com.example.SystemdActivatable3.service";
1739 const char *error_name = "com.example.Nope";
1740 const char *error_message = "Computer says no";
1742 if (!dbus_message_append_args (m,
1743 DBUS_TYPE_STRING, &unit,
1744 DBUS_TYPE_STRING, &error_name,
1745 DBUS_TYPE_STRING, &error_message,
1751 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1753 dbus_connection_send (f->systemd, m, NULL);
1754 dbus_message_unref (m);
1756 /* The monitor sees activation fail */
1758 /* Once again, we see the activation request and the reason. */
1759 while (g_queue_get_length (&f->monitored) < 1)
1760 test_main_context_iterate (f->ctx, TRUE);
1762 m = g_queue_pop_head (&f->monitored);
1763 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1764 "com.example.Nope");
1765 dbus_message_unref (m);
1767 #endif /* DBUS_UNIX */
1770 teardown (Fixture *f,
1771 gconstpointer context G_GNUC_UNUSED)
1773 dbus_error_free (&f->e);
1774 g_clear_error (&f->ge);
1776 if (f->monitor != NULL)
1778 dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1779 dbus_connection_close (f->monitor);
1780 dbus_connection_unref (f->monitor);
1784 if (f->sender != NULL)
1786 dbus_connection_close (f->sender);
1787 dbus_connection_unref (f->sender);
1791 if (f->recipient != NULL)
1793 dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1794 dbus_connection_close (f->recipient);
1795 dbus_connection_unref (f->recipient);
1796 f->recipient = NULL;
1799 if (f->systemd != NULL)
1801 dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1802 dbus_connection_close (f->systemd);
1803 dbus_connection_unref (f->systemd);
1807 if (f->activated != NULL)
1809 dbus_connection_remove_filter (f->activated, activated_filter, f);
1810 dbus_connection_close (f->activated);
1811 dbus_connection_unref (f->activated);
1812 f->activated = NULL;
1815 if (f->daemon_pid != 0)
1817 test_kill_pid (f->daemon_pid);
1818 g_spawn_close_pid (f->daemon_pid);
1822 test_main_context_unref (f->ctx);
1824 g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1825 g_queue_clear (&f->monitored);
1827 g_free (f->address);
1834 test_init (&argc, &argv);
1836 g_test_add ("/monitor/invalid", Fixture, NULL,
1837 setup, test_invalid, teardown);
1838 g_test_add ("/monitor/become", Fixture, &side_effects_config,
1839 setup, test_become_monitor, teardown);
1840 g_test_add ("/monitor/broadcast", Fixture, NULL,
1841 setup, test_broadcast, teardown);
1842 g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1843 setup, test_forbidden_broadcast, teardown);
1844 g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1845 setup, test_unicast_signal, teardown);
1846 g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1847 setup, test_forbidden, teardown);
1848 g_test_add ("/monitor/method-call", Fixture, NULL,
1849 setup, test_method_call, teardown);
1850 g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1851 setup, test_forbidden_method_call, teardown);
1852 g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1853 setup, test_dbus_daemon, teardown);
1854 g_test_add ("/monitor/selective", Fixture, &selective_config,
1855 setup, test_selective, teardown);
1856 g_test_add ("/monitor/well-known-destination",
1857 Fixture, &well_known_destination_config,
1858 setup, test_well_known_destination, teardown);
1859 g_test_add ("/monitor/unique-destination",
1861 setup, test_unique_destination, teardown);
1862 g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1863 setup, test_unicast_signal, teardown);
1864 g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1865 setup, test_unicast_signal, teardown);
1866 g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1867 setup, test_unicast_signal, teardown);
1868 g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1869 setup, test_unicast_signal, teardown);
1872 /* this relies on the systemd activation code path */
1873 g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1874 setup, test_activation, teardown);
1877 return g_test_run ();