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, &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 specify a bad match rule -
605 * also not allowed */
607 dbus_pending_call_unref (pc);
608 dbus_message_unref (m);
610 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
611 DBUS_PATH_DBUS, 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 /* Syntactically incorrect match rule taken from #92298 - was probably
623 * intended to be path='/modules/...'
625 s = "interface='org.kde.walletd',member='/modules/kwalletd/org.kde.KWallet/walletOpened'";
627 if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
629 !dbus_message_iter_close_container (&appender, &array_appender) ||
630 !dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero) ||
631 !dbus_connection_send_with_reply (f->monitor, m, &pc,
632 DBUS_TIMEOUT_USE_DEFAULT) ||
636 dbus_message_unref (m);
639 if (dbus_pending_call_get_completed (pc))
640 test_pending_call_store_reply (pc, &m);
641 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
646 test_main_context_iterate (f->ctx, TRUE);
648 g_assert_cmpint (dbus_message_get_type (m), ==, DBUS_MESSAGE_TYPE_ERROR);
649 g_assert_cmpstr (dbus_message_get_error_name (m), ==,
650 DBUS_ERROR_MATCH_RULE_INVALID);
652 dbus_pending_call_unref (pc);
653 dbus_message_unref (m);
655 /* We did not become a monitor, so we can still call methods. */
658 m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
659 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "GetId");
664 if (!dbus_connection_send_with_reply (f->monitor, m, &pc,
665 DBUS_TIMEOUT_USE_DEFAULT) ||
669 dbus_message_unref (m);
672 if (dbus_pending_call_get_completed (pc))
673 test_pending_call_store_reply (pc, &m);
674 else if (!dbus_pending_call_set_notify (pc, test_pending_call_store_reply,
679 test_main_context_iterate (f->ctx, TRUE);
681 if (dbus_set_error_from_message (&f->e, m))
682 g_error ("%s: %s", f->e.name, f->e.message);
684 ok = dbus_message_get_args (m, &f->e,
685 DBUS_TYPE_STRING, &s,
687 test_assert_no_error (&f->e);
689 g_assert_cmpstr (s, !=, NULL);
690 g_assert_cmpstr (s, !=, "");
692 dbus_pending_call_unref (pc);
693 dbus_message_unref (m);
697 * Test the side-effects of becoming a monitor.
700 test_become_monitor (Fixture *f,
701 gconstpointer context)
705 dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
706 dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
708 if (f->address == NULL)
711 ret = dbus_bus_request_name (f->monitor, "com.example.A",
712 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
713 test_assert_no_error (&f->e);
714 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
716 ret = dbus_bus_request_name (f->monitor, "com.example.B",
717 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
718 test_assert_no_error (&f->e);
719 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
721 ret = dbus_bus_request_name (f->monitor, "com.example.C",
722 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
723 test_assert_no_error (&f->e);
724 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
726 while (!got_unique || !got_a || !got_b || !got_c)
728 if (g_queue_is_empty (&f->monitored))
729 test_main_context_iterate (f->ctx, TRUE);
731 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
733 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
737 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
738 DBUS_TYPE_STRING, &name,
741 g_assert_cmpstr (dbus_message_get_path (m), ==,
744 test_assert_no_error (&f->e);
747 if (g_str_equal (name, f->monitor_name))
749 g_assert (!got_unique);
752 else if (g_str_equal (name, "com.example.A"))
757 else if (g_str_equal (name, "com.example.B"))
764 g_assert_cmpstr (name, ==, "com.example.C");
771 g_error ("unexpected message %s.%s",
772 dbus_message_get_interface (m),
773 dbus_message_get_member (m));
776 dbus_message_unref (m);
780 become_monitor (f, NULL);
782 while (!lost_unique || !lost_a || !lost_b || !lost_c)
784 if (g_queue_is_empty (&f->monitored))
785 test_main_context_iterate (f->ctx, TRUE);
787 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
789 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
793 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
794 DBUS_TYPE_STRING, &name,
797 test_assert_no_error (&f->e);
800 if (g_str_equal (name, f->monitor_name))
802 g_assert (!lost_unique);
805 else if (g_str_equal (name, "com.example.A"))
810 else if (g_str_equal (name, "com.example.B"))
817 g_assert_cmpstr (name, ==, "com.example.C");
824 g_error ("unexpected message %s.%s",
825 dbus_message_get_interface (m),
826 dbus_message_get_member (m));
829 dbus_message_unref (m);
833 /* Calling methods is forbidden; we get disconnected. */
834 dbus_bus_add_match (f->monitor, "", &f->e);
835 g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
836 g_assert (!dbus_connection_get_is_connected (f->monitor));
840 if (g_queue_is_empty (&f->monitored))
841 test_main_context_iterate (f->ctx, TRUE);
843 /* When we iterate all the connection's messages, we see ourselves
844 * losing all our names, then we're disconnected. */
845 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
847 if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
849 dbus_message_unref (m);
854 g_error ("unexpected message %s.%s",
855 dbus_message_get_interface (m),
856 dbus_message_get_member (m));
859 dbus_message_unref (m);
871 test_broadcast (Fixture *f,
872 gconstpointer context)
876 if (f->address == NULL)
879 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
880 test_assert_no_error (&f->e);
882 become_monitor (f, NULL);
884 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
885 dbus_connection_send (f->sender, m, NULL);
886 dbus_message_unref (m);
888 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
889 dbus_connection_send (f->sender, m, NULL);
890 dbus_message_unref (m);
892 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
893 dbus_connection_send (f->sender, m, NULL);
894 dbus_message_unref (m);
896 while (g_queue_get_length (&f->monitored) < 3)
897 test_main_context_iterate (f->ctx, TRUE);
899 m = g_queue_pop_head (&f->monitored);
900 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
901 "BroadcastSignal1", "", NULL);
902 dbus_message_unref (m);
904 m = g_queue_pop_head (&f->monitored);
905 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
906 "BroadcastSignal2", "", NULL);
907 dbus_message_unref (m);
909 m = g_queue_pop_head (&f->monitored);
910 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
911 "BroadcastSignal3", "", NULL);
912 dbus_message_unref (m);
914 m = g_queue_pop_head (&f->monitored);
915 g_assert (m == NULL);
919 test_forbidden_broadcast (Fixture *f,
920 gconstpointer context)
924 if (f->address == NULL)
927 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
928 test_assert_no_error (&f->e);
930 become_monitor (f, NULL);
932 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
934 dbus_connection_send (f->sender, m, NULL);
935 dbus_message_unref (m);
937 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
939 dbus_connection_send (f->sender, m, NULL);
940 dbus_message_unref (m);
942 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
944 dbus_connection_send (f->sender, m, NULL);
945 dbus_message_unref (m);
947 while (g_queue_get_length (&f->monitored) < 6)
948 test_main_context_iterate (f->ctx, TRUE);
950 m = g_queue_pop_head (&f->monitored);
951 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
952 "BroadcastSignal1", "", NULL);
953 dbus_message_unref (m);
955 m = g_queue_pop_head (&f->monitored);
956 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
957 DBUS_ERROR_ACCESS_DENIED);
958 dbus_message_unref (m);
960 m = g_queue_pop_head (&f->monitored);
961 assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
962 "BroadcastSignal2", "", NULL);
963 dbus_message_unref (m);
965 m = g_queue_pop_head (&f->monitored);
966 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
967 DBUS_ERROR_ACCESS_DENIED);
968 dbus_message_unref (m);
970 m = g_queue_pop_head (&f->monitored);
971 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
972 "BroadcastSignal3", "", NULL);
973 dbus_message_unref (m);
975 m = g_queue_pop_head (&f->monitored);
976 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
977 DBUS_ERROR_ACCESS_DENIED);
978 dbus_message_unref (m);
980 m = g_queue_pop_head (&f->monitored);
981 g_assert (m == NULL);
985 test_unicast_signal (Fixture *f,
986 gconstpointer context)
990 if (f->address == NULL)
993 become_monitor (f, NULL);
995 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
996 if (!dbus_message_set_destination (m, f->recipient_name))
998 dbus_connection_send (f->sender, m, NULL);
999 dbus_message_unref (m);
1001 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1002 if (!dbus_message_set_destination (m, f->recipient_name))
1004 dbus_connection_send (f->sender, m, NULL);
1005 dbus_message_unref (m);
1007 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1008 if (!dbus_message_set_destination (m, f->recipient_name))
1010 dbus_connection_send (f->sender, m, NULL);
1011 dbus_message_unref (m);
1013 while (g_queue_get_length (&f->monitored) < 3)
1014 test_main_context_iterate (f->ctx, TRUE);
1016 m = g_queue_pop_head (&f->monitored);
1017 assert_signal (m, f->sender_name, "/foo",
1018 "com.example.bar", "UnicastSignal1", "", f->recipient_name);
1019 dbus_message_unref (m);
1021 m = g_queue_pop_head (&f->monitored);
1022 assert_signal (m, f->sender_name, "/foo",
1023 "com.example.bar", "UnicastSignal2", "", f->recipient_name);
1024 dbus_message_unref (m);
1026 m = g_queue_pop_head (&f->monitored);
1027 assert_signal (m, f->sender_name, "/foo",
1028 "com.example.bar", "UnicastSignal3", "", f->recipient_name);
1029 dbus_message_unref (m);
1031 m = g_queue_pop_head (&f->monitored);
1032 g_assert (m == NULL);
1036 test_forbidden (Fixture *f,
1037 gconstpointer context)
1041 if (f->address == NULL)
1044 become_monitor (f, NULL);
1046 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1048 if (!dbus_message_set_destination (m, f->recipient_name))
1050 dbus_connection_send (f->sender, m, NULL);
1051 dbus_message_unref (m);
1053 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
1055 if (!dbus_message_set_destination (m, f->recipient_name))
1057 dbus_connection_send (f->sender, m, NULL);
1058 dbus_message_unref (m);
1060 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1062 if (!dbus_message_set_destination (m, f->recipient_name))
1064 dbus_connection_send (f->sender, m, NULL);
1065 dbus_message_unref (m);
1067 while (g_queue_get_length (&f->monitored) < 6)
1068 test_main_context_iterate (f->ctx, TRUE);
1070 m = g_queue_pop_head (&f->monitored);
1071 assert_signal (m, f->sender_name, "/foo",
1072 "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
1073 dbus_message_unref (m);
1075 m = g_queue_pop_head (&f->monitored);
1076 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1077 DBUS_ERROR_ACCESS_DENIED);
1078 dbus_message_unref (m);
1080 m = g_queue_pop_head (&f->monitored);
1081 assert_signal (m, f->sender_name, "/foo",
1082 "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
1083 dbus_message_unref (m);
1085 m = g_queue_pop_head (&f->monitored);
1086 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1087 DBUS_ERROR_ACCESS_DENIED);
1088 dbus_message_unref (m);
1090 m = g_queue_pop_head (&f->monitored);
1091 assert_signal (m, f->sender_name, "/foo",
1092 "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
1093 dbus_message_unref (m);
1095 m = g_queue_pop_head (&f->monitored);
1096 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1097 DBUS_ERROR_ACCESS_DENIED);
1098 dbus_message_unref (m);
1100 m = g_queue_pop_head (&f->monitored);
1101 g_assert (m == NULL);
1105 test_method_call (Fixture *f,
1106 gconstpointer context)
1110 if (f->address == NULL)
1113 become_monitor (f, NULL);
1115 /* regression test for
1116 * https://bugs.freedesktop.org/show_bug.cgi?id=90952 */
1117 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1118 DBUS_INTERFACE_PEER, "Ping");
1119 dbus_connection_send (f->sender, m, NULL);
1120 dbus_message_unref (m);
1122 while (g_queue_get_length (&f->monitored) < 2)
1123 test_main_context_iterate (f->ctx, TRUE);
1125 m = g_queue_pop_head (&f->monitored);
1126 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1127 DBUS_INTERFACE_PEER, "Ping", "");
1128 dbus_message_unref (m);
1130 m = g_queue_pop_head (&f->monitored);
1131 assert_method_reply (m, f->recipient_name, f->sender_name, "");
1132 dbus_message_unref (m);
1134 m = g_queue_pop_head (&f->monitored);
1135 g_assert (m == NULL);
1137 m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
1139 dbus_connection_send (f->sender, m, NULL);
1140 dbus_message_unref (m);
1142 while (g_queue_get_length (&f->monitored) < 2)
1143 test_main_context_iterate (f->ctx, TRUE);
1145 m = g_queue_pop_head (&f->monitored);
1146 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1147 "com.example.bar", "Call1", "");
1148 dbus_message_unref (m);
1150 m = g_queue_pop_head (&f->monitored);
1151 assert_error_reply (m, f->recipient_name, f->sender_name,
1152 DBUS_ERROR_UNKNOWN_METHOD);
1153 dbus_message_unref (m);
1155 m = g_queue_pop_head (&f->monitored);
1156 g_assert (m == NULL);
1160 test_forbidden_method_call (Fixture *f,
1161 gconstpointer context)
1165 if (f->address == NULL)
1168 become_monitor (f, NULL);
1170 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1171 "com.example.CannotSend", "Call1");
1172 dbus_connection_send (f->sender, m, NULL);
1173 dbus_message_unref (m);
1175 while (g_queue_get_length (&f->monitored) < 2)
1176 test_main_context_iterate (f->ctx, TRUE);
1178 m = g_queue_pop_head (&f->monitored);
1179 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1180 "com.example.CannotSend", "Call1", "");
1181 dbus_message_unref (m);
1183 m = g_queue_pop_head (&f->monitored);
1184 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1185 DBUS_ERROR_ACCESS_DENIED);
1186 dbus_message_unref (m);
1188 m = g_queue_pop_head (&f->monitored);
1189 g_assert (m == NULL);
1191 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1192 "com.example.CannotReceive", "Call2");
1193 dbus_connection_send (f->sender, m, NULL);
1194 dbus_message_unref (m);
1196 while (g_queue_get_length (&f->monitored) < 2)
1197 test_main_context_iterate (f->ctx, TRUE);
1199 m = g_queue_pop_head (&f->monitored);
1200 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1201 "com.example.CannotReceive", "Call2", "");
1202 dbus_message_unref (m);
1204 m = g_queue_pop_head (&f->monitored);
1205 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1206 DBUS_ERROR_ACCESS_DENIED);
1207 dbus_message_unref (m);
1209 m = g_queue_pop_head (&f->monitored);
1210 g_assert (m == NULL);
1214 test_dbus_daemon (Fixture *f,
1215 gconstpointer context)
1220 if (f->address == NULL)
1223 become_monitor (f, NULL);
1225 res = dbus_bus_request_name (f->sender, "com.example.Sender",
1226 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1227 test_assert_no_error (&f->e);
1228 g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1230 res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1231 test_assert_no_error (&f->e);
1232 g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1234 while (g_queue_get_length (&f->monitored) < 8)
1235 test_main_context_iterate (f->ctx, TRUE);
1237 m = g_queue_pop_head (&f->monitored);
1238 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1239 DBUS_INTERFACE_DBUS, "RequestName", "su");
1240 dbus_message_unref (m);
1242 m = g_queue_pop_head (&f->monitored);
1243 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1244 "NameOwnerChanged", "sss", NULL);
1245 dbus_message_unref (m);
1247 /* FIXME: should we get this? */
1248 m = g_queue_pop_head (&f->monitored);
1249 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1250 "NameAcquired", "s", f->sender_name);
1251 dbus_message_unref (m);
1253 m = g_queue_pop_head (&f->monitored);
1254 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1255 dbus_message_unref (m);
1257 m = g_queue_pop_head (&f->monitored);
1258 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1259 DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1260 dbus_message_unref (m);
1262 /* FIXME: should we get this? */
1263 m = g_queue_pop_head (&f->monitored);
1264 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1265 "NameLost", "s", f->sender_name);
1266 dbus_message_unref (m);
1268 m = g_queue_pop_head (&f->monitored);
1269 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1270 "NameOwnerChanged", "sss", NULL);
1271 dbus_message_unref (m);
1273 m = g_queue_pop_head (&f->monitored);
1274 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1275 dbus_message_unref (m);
1277 m = g_queue_pop_head (&f->monitored);
1278 g_assert (m == NULL);
1282 test_selective (Fixture *f,
1283 gconstpointer context)
1287 if (f->address == NULL)
1290 /* Match rules added before becoming a monitor should be cleared:
1291 * if they weren't, this test would get Interesting twice, then Tedious,
1292 * and only see Fun after that. */
1293 dbus_bus_add_match (f->monitor,
1294 "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1295 test_assert_no_error (&f->e);
1296 dbus_bus_add_match (f->monitor,
1297 "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1298 test_assert_no_error (&f->e);
1300 become_monitor (f, NULL);
1302 m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1304 if (!dbus_message_set_destination (m, f->recipient_name))
1306 dbus_connection_send (f->sender, m, NULL);
1307 dbus_message_unref (m);
1309 m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1311 if (!dbus_message_set_destination (m, f->recipient_name))
1313 dbus_connection_send (f->sender, m, NULL);
1314 dbus_message_unref (m);
1316 m = dbus_message_new_signal ("/foo", "com.example.Fun",
1318 if (!dbus_message_set_destination (m, f->recipient_name))
1320 dbus_connection_send (f->sender, m, NULL);
1321 dbus_message_unref (m);
1323 while (g_queue_get_length (&f->monitored) < 2)
1324 test_main_context_iterate (f->ctx, TRUE);
1326 /* We get the interesting signal and the fun signal, but not the tedious
1329 m = g_queue_pop_head (&f->monitored);
1330 assert_signal (m, f->sender_name, "/foo",
1331 "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1332 dbus_message_unref (m);
1334 m = g_queue_pop_head (&f->monitored);
1335 assert_signal (m, f->sender_name, "/foo",
1336 "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1337 dbus_message_unref (m);
1339 m = g_queue_pop_head (&f->monitored);
1340 g_assert (m == NULL);
1344 test_well_known_destination (Fixture *f,
1345 gconstpointer context)
1349 if (f->address == NULL)
1352 take_well_known_name (f, f->recipient, "com.example.Recipient");
1353 /* we don't expect_take_well_known_name here because the
1354 * monitor isn't up yet */
1356 become_monitor (f, NULL);
1358 /* The sender sends a message to itself. It will not be observed. */
1359 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1360 if (!dbus_message_set_destination (m, f->sender_name))
1362 dbus_connection_send (f->sender, m, NULL);
1363 dbus_message_unref (m);
1365 /* The sender sends a message to the recipient by well-known name.
1366 * It will be observed. */
1367 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1368 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1370 dbus_connection_send (f->sender, m, NULL);
1371 dbus_message_unref (m);
1373 /* The sender sends a message to the recipient by unique name.
1374 * It will still be observed. */
1375 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1376 if (!dbus_message_set_destination (m, f->recipient_name))
1378 dbus_connection_send (f->sender, m, NULL);
1379 dbus_message_unref (m);
1381 while (g_queue_get_length (&f->monitored) < 2)
1382 test_main_context_iterate (f->ctx, TRUE);
1384 m = g_queue_pop_head (&f->monitored);
1385 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1386 "Observed1", "", "com.example.Recipient");
1387 dbus_message_unref (m);
1389 m = g_queue_pop_head (&f->monitored);
1390 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1391 "Observed2", "", f->recipient_name);
1392 dbus_message_unref (m);
1394 m = g_queue_pop_head (&f->monitored);
1395 g_assert (m == NULL);
1399 test_unique_destination (Fixture *f,
1400 gconstpointer context)
1405 NULL, /* match rules */
1408 const gchar *match_rules[2] = { NULL, NULL };
1411 if (f->address == NULL)
1414 take_well_known_name (f, f->recipient, "com.example.Recipient");
1415 /* we don't expect_take_well_known_name here because the
1416 * monitor isn't up yet */
1418 rule = g_strdup_printf ("destination='%s'", f->recipient_name);
1420 g_test_queue_free (rule);
1421 match_rules[0] = rule;
1422 config.match_rules = match_rules;
1424 become_monitor (f, &config);
1426 /* The sender sends a message to itself. It will not be observed. */
1427 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1428 if (!dbus_message_set_destination (m, f->sender_name))
1430 dbus_connection_send (f->sender, m, NULL);
1431 dbus_message_unref (m);
1433 /* The sender sends a message to the recipient by well-known name.
1434 * It will be observed. */
1435 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1436 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1438 dbus_connection_send (f->sender, m, NULL);
1439 dbus_message_unref (m);
1441 /* The sender sends a message to the recipient by unique name.
1442 * It will still be observed. */
1443 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1444 if (!dbus_message_set_destination (m, f->recipient_name))
1446 dbus_connection_send (f->sender, m, NULL);
1447 dbus_message_unref (m);
1449 while (g_queue_get_length (&f->monitored) < 2)
1450 test_main_context_iterate (f->ctx, TRUE);
1452 m = g_queue_pop_head (&f->monitored);
1453 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1454 "Observed1", "", "com.example.Recipient");
1455 dbus_message_unref (m);
1457 m = g_queue_pop_head (&f->monitored);
1458 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1459 "Observed2", "", f->recipient_name);
1460 dbus_message_unref (m);
1462 m = g_queue_pop_head (&f->monitored);
1463 g_assert (m == NULL);
1467 /* currently only used for the systemd activation test */
1469 expect_new_connection (Fixture *f)
1473 while (g_queue_get_length (&f->monitored) < 4)
1474 test_main_context_iterate (f->ctx, TRUE);
1476 m = g_queue_pop_head (&f->monitored);
1478 dbus_message_unref (m);
1480 m = g_queue_pop_head (&f->monitored);
1481 assert_hello_reply (m);
1482 dbus_message_unref (m);
1484 m = g_queue_pop_head (&f->monitored);
1485 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1486 "NameOwnerChanged", "sss", NULL);
1487 dbus_message_unref (m);
1489 m = g_queue_pop_head (&f->monitored);
1490 assert_name_acquired (m);
1491 dbus_message_unref (m);
1494 /* currently only used for the systemd activation test */
1496 expect_take_well_known_name (Fixture *f,
1497 DBusConnection *connection,
1501 const char *connection_name = dbus_bus_get_unique_name (connection);
1503 while (g_queue_get_length (&f->monitored) < 4)
1504 test_main_context_iterate (f->ctx, TRUE);
1506 m = g_queue_pop_head (&f->monitored);
1507 assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1508 DBUS_INTERFACE_DBUS, "RequestName", "su");
1509 dbus_message_unref (m);
1511 m = g_queue_pop_head (&f->monitored);
1512 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1513 "NameOwnerChanged", "sss", NULL);
1514 dbus_message_unref (m);
1516 m = g_queue_pop_head (&f->monitored);
1517 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1518 "NameAcquired", "s", connection_name);
1519 dbus_message_unref (m);
1521 m = g_queue_pop_head (&f->monitored);
1522 assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1523 dbus_message_unref (m);
1527 test_activation (Fixture *f,
1528 gconstpointer context)
1532 if (f->address == NULL)
1535 become_monitor (f, NULL);
1537 /* The sender sends a message to an activatable service. */
1538 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1539 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1541 dbus_connection_send (f->sender, m, NULL);
1542 dbus_message_unref (m);
1544 /* We observe the activation request, and the message that caused it,
1545 * before systemd has even joined the bus. */
1546 while (g_queue_get_length (&f->monitored) < 2)
1547 test_main_context_iterate (f->ctx, TRUE);
1549 m = g_queue_pop_head (&f->monitored);
1550 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1551 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1552 "org.freedesktop.systemd1");
1553 dbus_message_unref (m);
1554 m = g_queue_pop_head (&f->monitored);
1555 assert_signal (m, f->sender_name, "/foo",
1556 "com.example.bar", "UnicastSignal1", "",
1557 "com.example.SystemdActivatable1");
1558 dbus_message_unref (m);
1560 /* The fake systemd connects to the bus. */
1561 f->systemd = test_connect_to_bus (f->ctx, f->address);
1562 if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1564 f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1566 expect_new_connection (f);
1567 take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1568 expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1570 /* It gets its activation request. */
1571 while (f->systemd_message == NULL)
1572 test_main_context_iterate (f->ctx, TRUE);
1574 m = f->systemd_message;
1575 f->systemd_message = NULL;
1576 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1577 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1578 "org.freedesktop.systemd1");
1579 dbus_message_unref (m);
1581 /* systemd starts the activatable service. */
1582 f->activated = test_connect_to_bus (f->ctx, f->address);
1583 if (!dbus_connection_add_filter (f->activated, activated_filter,
1586 f->activated_name = dbus_bus_get_unique_name (f->activated);
1588 expect_new_connection (f);
1589 take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1590 expect_take_well_known_name (f, f->activated,
1591 "com.example.SystemdActivatable1");
1593 /* The message is delivered to the activatable service. */
1594 while (f->activated_message == NULL)
1595 test_main_context_iterate (f->ctx, TRUE);
1597 m = f->activated_message;
1598 f->activated_message = NULL;
1599 assert_signal (m, f->sender_name, "/foo",
1600 "com.example.bar", "UnicastSignal1", "",
1601 "com.example.SystemdActivatable1");
1602 dbus_message_unref (m);
1604 /* The sender sends a message to a different activatable service. */
1605 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1606 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1608 dbus_connection_send (f->sender, m, NULL);
1609 dbus_message_unref (m);
1611 /* This time systemd is already ready for it. */
1612 while (g_queue_get_length (&f->monitored) < 2 ||
1613 f->systemd_message == NULL)
1614 test_main_context_iterate (f->ctx, TRUE);
1616 m = f->systemd_message;
1617 f->systemd_message = NULL;
1618 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1619 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1620 "org.freedesktop.systemd1");
1621 dbus_message_unref (m);
1623 /* The monitor sees the activation request and the signal that
1625 m = g_queue_pop_head (&f->monitored);
1626 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1627 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1628 "org.freedesktop.systemd1");
1629 dbus_message_unref (m);
1630 m = g_queue_pop_head (&f->monitored);
1631 assert_signal (m, f->sender_name, "/foo",
1632 "com.example.bar", "UnicastSignal2", "",
1633 "com.example.SystemdActivatable2");
1634 dbus_message_unref (m);
1636 /* The activatable service takes its name. Here I'm faking it by using
1637 * an existing connection. */
1638 take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1640 /* The message is delivered to the activatable service.
1641 * Implementation detail: the monitor sees this happen before it even
1642 * sees that the name request happened, which is pretty odd. */
1643 while (f->activated_message == NULL)
1644 test_main_context_iterate (f->ctx, TRUE);
1646 m = f->activated_message;
1647 f->activated_message = NULL;
1648 assert_signal (m, f->sender_name, "/foo",
1649 "com.example.bar", "UnicastSignal2", "",
1650 "com.example.SystemdActivatable2");
1651 dbus_message_unref (m);
1653 expect_take_well_known_name (f, f->activated,
1654 "com.example.SystemdActivatable2");
1656 /* A third activation. */
1657 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1658 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1660 dbus_connection_send (f->sender, m, NULL);
1661 dbus_message_unref (m);
1663 /* Once again, we see the activation request and the reason. */
1664 while (g_queue_get_length (&f->monitored) < 2)
1665 test_main_context_iterate (f->ctx, TRUE);
1667 m = g_queue_pop_head (&f->monitored);
1668 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1669 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1670 "org.freedesktop.systemd1");
1671 dbus_message_unref (m);
1672 m = g_queue_pop_head (&f->monitored);
1673 assert_signal (m, f->sender_name, "/foo",
1674 "com.example.bar", "UnicastSignal3", "",
1675 "com.example.SystemdActivatable3");
1676 dbus_message_unref (m);
1678 /* systemd gets the request too. */
1679 while (f->systemd_message == NULL)
1680 test_main_context_iterate (f->ctx, TRUE);
1682 m = f->systemd_message;
1683 f->systemd_message = NULL;
1684 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1685 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1686 "org.freedesktop.systemd1");
1687 dbus_message_unref (m);
1689 /* This time activation fails */
1690 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1691 "org.freedesktop.systemd1.Activator", "ActivationFailure");
1695 const char *unit = "dbus-com.example.SystemdActivatable3.service";
1696 const char *error_name = "com.example.Nope";
1697 const char *error_message = "Computer says no";
1699 if (!dbus_message_append_args (m,
1700 DBUS_TYPE_STRING, &unit,
1701 DBUS_TYPE_STRING, &error_name,
1702 DBUS_TYPE_STRING, &error_message,
1708 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1710 dbus_connection_send (f->systemd, m, NULL);
1711 dbus_message_unref (m);
1713 /* The monitor sees activation fail */
1715 /* Once again, we see the activation request and the reason. */
1716 while (g_queue_get_length (&f->monitored) < 1)
1717 test_main_context_iterate (f->ctx, TRUE);
1719 m = g_queue_pop_head (&f->monitored);
1720 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1721 "com.example.Nope");
1722 dbus_message_unref (m);
1724 #endif /* DBUS_UNIX */
1727 teardown (Fixture *f,
1728 gconstpointer context G_GNUC_UNUSED)
1730 dbus_error_free (&f->e);
1731 g_clear_error (&f->ge);
1733 if (f->monitor != NULL)
1735 dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1736 dbus_connection_close (f->monitor);
1737 dbus_connection_unref (f->monitor);
1741 if (f->sender != NULL)
1743 dbus_connection_close (f->sender);
1744 dbus_connection_unref (f->sender);
1748 if (f->recipient != NULL)
1750 dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1751 dbus_connection_close (f->recipient);
1752 dbus_connection_unref (f->recipient);
1753 f->recipient = NULL;
1756 if (f->systemd != NULL)
1758 dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1759 dbus_connection_close (f->systemd);
1760 dbus_connection_unref (f->systemd);
1764 if (f->activated != NULL)
1766 dbus_connection_remove_filter (f->activated, activated_filter, f);
1767 dbus_connection_close (f->activated);
1768 dbus_connection_unref (f->activated);
1769 f->activated = NULL;
1772 test_kill_pid (f->daemon_pid);
1773 g_spawn_close_pid (f->daemon_pid);
1775 test_main_context_unref (f->ctx);
1777 g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1778 g_queue_clear (&f->monitored);
1780 g_free (f->address);
1787 test_init (&argc, &argv);
1789 g_test_add ("/monitor/invalid", Fixture, NULL,
1790 setup, test_invalid, teardown);
1791 g_test_add ("/monitor/become", Fixture, &side_effects_config,
1792 setup, test_become_monitor, teardown);
1793 g_test_add ("/monitor/broadcast", Fixture, NULL,
1794 setup, test_broadcast, teardown);
1795 g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1796 setup, test_forbidden_broadcast, teardown);
1797 g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1798 setup, test_unicast_signal, teardown);
1799 g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1800 setup, test_forbidden, teardown);
1801 g_test_add ("/monitor/method-call", Fixture, NULL,
1802 setup, test_method_call, teardown);
1803 g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1804 setup, test_forbidden_method_call, teardown);
1805 g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1806 setup, test_dbus_daemon, teardown);
1807 g_test_add ("/monitor/selective", Fixture, &selective_config,
1808 setup, test_selective, teardown);
1809 g_test_add ("/monitor/well-known-destination",
1810 Fixture, &well_known_destination_config,
1811 setup, test_well_known_destination, teardown);
1812 g_test_add ("/monitor/unique-destination",
1814 setup, test_unique_destination, teardown);
1815 g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1816 setup, test_unicast_signal, teardown);
1817 g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1818 setup, test_unicast_signal, teardown);
1819 g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1820 setup, test_unicast_signal, teardown);
1821 g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1822 setup, test_unicast_signal, teardown);
1825 /* this relies on the systemd activation code path */
1826 g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1827 setup, test_activation, teardown);
1830 return g_test_run ();