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 ok = dbus_message_get_args (m, &f->e,
682 DBUS_TYPE_STRING, &s,
684 test_assert_no_error (&f->e);
686 g_assert_cmpstr (s, !=, NULL);
687 g_assert_cmpstr (s, !=, "");
689 dbus_pending_call_unref (pc);
690 dbus_message_unref (m);
694 * Test the side-effects of becoming a monitor.
697 test_become_monitor (Fixture *f,
698 gconstpointer context)
702 dbus_bool_t got_unique = FALSE, got_a = FALSE, got_b = FALSE, got_c = FALSE;
703 dbus_bool_t lost_unique = FALSE, lost_a = FALSE, lost_b = FALSE, lost_c = FALSE;
705 if (f->address == NULL)
708 ret = dbus_bus_request_name (f->monitor, "com.example.A",
709 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
710 test_assert_no_error (&f->e);
711 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
713 ret = dbus_bus_request_name (f->monitor, "com.example.B",
714 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
715 test_assert_no_error (&f->e);
716 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
718 ret = dbus_bus_request_name (f->monitor, "com.example.C",
719 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
720 test_assert_no_error (&f->e);
721 g_assert_cmpint (ret, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
723 while (!got_unique || !got_a || !got_b || !got_c)
725 if (g_queue_is_empty (&f->monitored))
726 test_main_context_iterate (f->ctx, TRUE);
728 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
730 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
734 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
735 DBUS_TYPE_STRING, &name,
738 g_assert_cmpstr (dbus_message_get_path (m), ==,
741 test_assert_no_error (&f->e);
744 if (g_str_equal (name, f->monitor_name))
746 g_assert (!got_unique);
749 else if (g_str_equal (name, "com.example.A"))
754 else if (g_str_equal (name, "com.example.B"))
761 g_assert_cmpstr (name, ==, "com.example.C");
768 g_error ("unexpected message %s.%s",
769 dbus_message_get_interface (m),
770 dbus_message_get_member (m));
773 dbus_message_unref (m);
777 become_monitor (f, NULL);
779 while (!lost_unique || !lost_a || !lost_b || !lost_c)
781 if (g_queue_is_empty (&f->monitored))
782 test_main_context_iterate (f->ctx, TRUE);
784 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
786 if (dbus_message_is_signal (m, DBUS_INTERFACE_DBUS,
790 dbus_bool_t ok = dbus_message_get_args (m, &f->e,
791 DBUS_TYPE_STRING, &name,
794 test_assert_no_error (&f->e);
797 if (g_str_equal (name, f->monitor_name))
799 g_assert (!lost_unique);
802 else if (g_str_equal (name, "com.example.A"))
807 else if (g_str_equal (name, "com.example.B"))
814 g_assert_cmpstr (name, ==, "com.example.C");
821 g_error ("unexpected message %s.%s",
822 dbus_message_get_interface (m),
823 dbus_message_get_member (m));
826 dbus_message_unref (m);
830 /* Calling methods is forbidden; we get disconnected. */
831 dbus_bus_add_match (f->monitor, "", &f->e);
832 g_assert_cmpstr (f->e.name, ==, DBUS_ERROR_NO_REPLY);
833 g_assert (!dbus_connection_get_is_connected (f->monitor));
837 if (g_queue_is_empty (&f->monitored))
838 test_main_context_iterate (f->ctx, TRUE);
840 /* When we iterate all the connection's messages, we see ourselves
841 * losing all our names, then we're disconnected. */
842 while ((m = g_queue_pop_head (&f->monitored)) != NULL)
844 if (dbus_message_is_signal (m, DBUS_INTERFACE_LOCAL, "Disconnected"))
846 dbus_message_unref (m);
851 g_error ("unexpected message %s.%s",
852 dbus_message_get_interface (m),
853 dbus_message_get_member (m));
856 dbus_message_unref (m);
868 test_broadcast (Fixture *f,
869 gconstpointer context)
873 if (f->address == NULL)
876 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
877 test_assert_no_error (&f->e);
879 become_monitor (f, NULL);
881 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal1");
882 dbus_connection_send (f->sender, m, NULL);
883 dbus_message_unref (m);
885 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal2");
886 dbus_connection_send (f->sender, m, NULL);
887 dbus_message_unref (m);
889 m = dbus_message_new_signal ("/foo", "com.example.bar", "BroadcastSignal3");
890 dbus_connection_send (f->sender, m, NULL);
891 dbus_message_unref (m);
893 while (g_queue_get_length (&f->monitored) < 3)
894 test_main_context_iterate (f->ctx, TRUE);
896 m = g_queue_pop_head (&f->monitored);
897 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
898 "BroadcastSignal1", "", NULL);
899 dbus_message_unref (m);
901 m = g_queue_pop_head (&f->monitored);
902 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
903 "BroadcastSignal2", "", NULL);
904 dbus_message_unref (m);
906 m = g_queue_pop_head (&f->monitored);
907 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
908 "BroadcastSignal3", "", NULL);
909 dbus_message_unref (m);
911 m = g_queue_pop_head (&f->monitored);
912 g_assert (m == NULL);
916 test_forbidden_broadcast (Fixture *f,
917 gconstpointer context)
921 if (f->address == NULL)
924 dbus_bus_add_match (f->recipient, "type='signal'", &f->e);
925 test_assert_no_error (&f->e);
927 become_monitor (f, NULL);
929 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
931 dbus_connection_send (f->sender, m, NULL);
932 dbus_message_unref (m);
934 m = dbus_message_new_signal ("/foo", "com.example.CannotReceive",
936 dbus_connection_send (f->sender, m, NULL);
937 dbus_message_unref (m);
939 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
941 dbus_connection_send (f->sender, m, NULL);
942 dbus_message_unref (m);
944 while (g_queue_get_length (&f->monitored) < 6)
945 test_main_context_iterate (f->ctx, TRUE);
947 m = g_queue_pop_head (&f->monitored);
948 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
949 "BroadcastSignal1", "", NULL);
950 dbus_message_unref (m);
952 m = g_queue_pop_head (&f->monitored);
953 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
954 DBUS_ERROR_ACCESS_DENIED);
955 dbus_message_unref (m);
957 m = g_queue_pop_head (&f->monitored);
958 assert_signal (m, f->sender_name, "/foo", "com.example.CannotReceive",
959 "BroadcastSignal2", "", NULL);
960 dbus_message_unref (m);
962 m = g_queue_pop_head (&f->monitored);
963 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
964 DBUS_ERROR_ACCESS_DENIED);
965 dbus_message_unref (m);
967 m = g_queue_pop_head (&f->monitored);
968 assert_signal (m, f->sender_name, "/foo", "com.example.CannotSend",
969 "BroadcastSignal3", "", NULL);
970 dbus_message_unref (m);
972 m = g_queue_pop_head (&f->monitored);
973 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
974 DBUS_ERROR_ACCESS_DENIED);
975 dbus_message_unref (m);
977 m = g_queue_pop_head (&f->monitored);
978 g_assert (m == NULL);
982 test_unicast_signal (Fixture *f,
983 gconstpointer context)
987 if (f->address == NULL)
990 become_monitor (f, NULL);
992 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
993 if (!dbus_message_set_destination (m, f->recipient_name))
995 dbus_connection_send (f->sender, m, NULL);
996 dbus_message_unref (m);
998 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
999 if (!dbus_message_set_destination (m, f->recipient_name))
1001 dbus_connection_send (f->sender, m, NULL);
1002 dbus_message_unref (m);
1004 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1005 if (!dbus_message_set_destination (m, f->recipient_name))
1007 dbus_connection_send (f->sender, m, NULL);
1008 dbus_message_unref (m);
1010 while (g_queue_get_length (&f->monitored) < 3)
1011 test_main_context_iterate (f->ctx, TRUE);
1013 m = g_queue_pop_head (&f->monitored);
1014 assert_signal (m, f->sender_name, "/foo",
1015 "com.example.bar", "UnicastSignal1", "", f->recipient_name);
1016 dbus_message_unref (m);
1018 m = g_queue_pop_head (&f->monitored);
1019 assert_signal (m, f->sender_name, "/foo",
1020 "com.example.bar", "UnicastSignal2", "", f->recipient_name);
1021 dbus_message_unref (m);
1023 m = g_queue_pop_head (&f->monitored);
1024 assert_signal (m, f->sender_name, "/foo",
1025 "com.example.bar", "UnicastSignal3", "", f->recipient_name);
1026 dbus_message_unref (m);
1028 m = g_queue_pop_head (&f->monitored);
1029 g_assert (m == NULL);
1033 test_forbidden (Fixture *f,
1034 gconstpointer context)
1038 if (f->address == NULL)
1041 become_monitor (f, NULL);
1043 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
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.CannotReceive",
1052 if (!dbus_message_set_destination (m, f->recipient_name))
1054 dbus_connection_send (f->sender, m, NULL);
1055 dbus_message_unref (m);
1057 m = dbus_message_new_signal ("/foo", "com.example.CannotSend",
1059 if (!dbus_message_set_destination (m, f->recipient_name))
1061 dbus_connection_send (f->sender, m, NULL);
1062 dbus_message_unref (m);
1064 while (g_queue_get_length (&f->monitored) < 6)
1065 test_main_context_iterate (f->ctx, TRUE);
1067 m = g_queue_pop_head (&f->monitored);
1068 assert_signal (m, f->sender_name, "/foo",
1069 "com.example.CannotSend", "UnicastSignal1", "", f->recipient_name);
1070 dbus_message_unref (m);
1072 m = g_queue_pop_head (&f->monitored);
1073 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1074 DBUS_ERROR_ACCESS_DENIED);
1075 dbus_message_unref (m);
1077 m = g_queue_pop_head (&f->monitored);
1078 assert_signal (m, f->sender_name, "/foo",
1079 "com.example.CannotReceive", "UnicastSignal2", "", f->recipient_name);
1080 dbus_message_unref (m);
1082 m = g_queue_pop_head (&f->monitored);
1083 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1084 DBUS_ERROR_ACCESS_DENIED);
1085 dbus_message_unref (m);
1087 m = g_queue_pop_head (&f->monitored);
1088 assert_signal (m, f->sender_name, "/foo",
1089 "com.example.CannotSend", "UnicastSignal3", "", f->recipient_name);
1090 dbus_message_unref (m);
1092 m = g_queue_pop_head (&f->monitored);
1093 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1094 DBUS_ERROR_ACCESS_DENIED);
1095 dbus_message_unref (m);
1097 m = g_queue_pop_head (&f->monitored);
1098 g_assert (m == NULL);
1102 test_method_call (Fixture *f,
1103 gconstpointer context)
1107 if (f->address == NULL)
1110 become_monitor (f, NULL);
1112 /* regression test for
1113 * https://bugs.freedesktop.org/show_bug.cgi?id=90952 */
1114 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1115 DBUS_INTERFACE_PEER, "Ping");
1116 dbus_connection_send (f->sender, m, NULL);
1117 dbus_message_unref (m);
1119 while (g_queue_get_length (&f->monitored) < 2)
1120 test_main_context_iterate (f->ctx, TRUE);
1122 m = g_queue_pop_head (&f->monitored);
1123 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1124 DBUS_INTERFACE_PEER, "Ping", "");
1125 dbus_message_unref (m);
1127 m = g_queue_pop_head (&f->monitored);
1128 assert_method_reply (m, f->recipient_name, f->sender_name, "");
1129 dbus_message_unref (m);
1131 m = g_queue_pop_head (&f->monitored);
1132 g_assert (m == NULL);
1134 m = dbus_message_new_method_call (f->recipient_name, "/foo", "com.example.bar",
1136 dbus_connection_send (f->sender, m, NULL);
1137 dbus_message_unref (m);
1139 while (g_queue_get_length (&f->monitored) < 2)
1140 test_main_context_iterate (f->ctx, TRUE);
1142 m = g_queue_pop_head (&f->monitored);
1143 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1144 "com.example.bar", "Call1", "");
1145 dbus_message_unref (m);
1147 m = g_queue_pop_head (&f->monitored);
1148 assert_error_reply (m, f->recipient_name, f->sender_name,
1149 DBUS_ERROR_UNKNOWN_METHOD);
1150 dbus_message_unref (m);
1152 m = g_queue_pop_head (&f->monitored);
1153 g_assert (m == NULL);
1157 test_forbidden_method_call (Fixture *f,
1158 gconstpointer context)
1162 if (f->address == NULL)
1165 become_monitor (f, NULL);
1167 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1168 "com.example.CannotSend", "Call1");
1169 dbus_connection_send (f->sender, m, NULL);
1170 dbus_message_unref (m);
1172 while (g_queue_get_length (&f->monitored) < 2)
1173 test_main_context_iterate (f->ctx, TRUE);
1175 m = g_queue_pop_head (&f->monitored);
1176 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1177 "com.example.CannotSend", "Call1", "");
1178 dbus_message_unref (m);
1180 m = g_queue_pop_head (&f->monitored);
1181 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1182 DBUS_ERROR_ACCESS_DENIED);
1183 dbus_message_unref (m);
1185 m = g_queue_pop_head (&f->monitored);
1186 g_assert (m == NULL);
1188 m = dbus_message_new_method_call (f->recipient_name, "/foo",
1189 "com.example.CannotReceive", "Call2");
1190 dbus_connection_send (f->sender, m, NULL);
1191 dbus_message_unref (m);
1193 while (g_queue_get_length (&f->monitored) < 2)
1194 test_main_context_iterate (f->ctx, TRUE);
1196 m = g_queue_pop_head (&f->monitored);
1197 assert_method_call (m, f->sender_name, f->recipient_name, "/foo",
1198 "com.example.CannotReceive", "Call2", "");
1199 dbus_message_unref (m);
1201 m = g_queue_pop_head (&f->monitored);
1202 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1203 DBUS_ERROR_ACCESS_DENIED);
1204 dbus_message_unref (m);
1206 m = g_queue_pop_head (&f->monitored);
1207 g_assert (m == NULL);
1211 test_dbus_daemon (Fixture *f,
1212 gconstpointer context)
1217 if (f->address == NULL)
1220 become_monitor (f, NULL);
1222 res = dbus_bus_request_name (f->sender, "com.example.Sender",
1223 DBUS_NAME_FLAG_DO_NOT_QUEUE, &f->e);
1224 test_assert_no_error (&f->e);
1225 g_assert_cmpint (res, ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
1227 res = dbus_bus_release_name (f->sender, "com.example.Sender", &f->e);
1228 test_assert_no_error (&f->e);
1229 g_assert_cmpint (res, ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
1231 while (g_queue_get_length (&f->monitored) < 8)
1232 test_main_context_iterate (f->ctx, TRUE);
1234 m = g_queue_pop_head (&f->monitored);
1235 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1236 DBUS_INTERFACE_DBUS, "RequestName", "su");
1237 dbus_message_unref (m);
1239 m = g_queue_pop_head (&f->monitored);
1240 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1241 "NameOwnerChanged", "sss", NULL);
1242 dbus_message_unref (m);
1244 /* FIXME: should we get this? */
1245 m = g_queue_pop_head (&f->monitored);
1246 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1247 "NameAcquired", "s", f->sender_name);
1248 dbus_message_unref (m);
1250 m = g_queue_pop_head (&f->monitored);
1251 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1252 dbus_message_unref (m);
1254 m = g_queue_pop_head (&f->monitored);
1255 assert_method_call (m, f->sender_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1256 DBUS_INTERFACE_DBUS, "ReleaseName", "s");
1257 dbus_message_unref (m);
1259 /* FIXME: should we get this? */
1260 m = g_queue_pop_head (&f->monitored);
1261 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1262 "NameLost", "s", f->sender_name);
1263 dbus_message_unref (m);
1265 m = g_queue_pop_head (&f->monitored);
1266 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1267 "NameOwnerChanged", "sss", NULL);
1268 dbus_message_unref (m);
1270 m = g_queue_pop_head (&f->monitored);
1271 assert_method_reply (m, DBUS_SERVICE_DBUS, f->sender_name, "u");
1272 dbus_message_unref (m);
1274 m = g_queue_pop_head (&f->monitored);
1275 g_assert (m == NULL);
1279 test_selective (Fixture *f,
1280 gconstpointer context)
1284 if (f->address == NULL)
1287 /* Match rules added before becoming a monitor should be cleared:
1288 * if they weren't, this test would get Interesting twice, then Tedious,
1289 * and only see Fun after that. */
1290 dbus_bus_add_match (f->monitor,
1291 "eavesdrop='true',interface='com.example.Interesting'", &f->e);
1292 test_assert_no_error (&f->e);
1293 dbus_bus_add_match (f->monitor,
1294 "eavesdrop='true',interface='com.example.Tedious'", &f->e);
1295 test_assert_no_error (&f->e);
1297 become_monitor (f, NULL);
1299 m = dbus_message_new_signal ("/foo", "com.example.Interesting",
1301 if (!dbus_message_set_destination (m, f->recipient_name))
1303 dbus_connection_send (f->sender, m, NULL);
1304 dbus_message_unref (m);
1306 m = dbus_message_new_signal ("/foo", "com.example.Tedious",
1308 if (!dbus_message_set_destination (m, f->recipient_name))
1310 dbus_connection_send (f->sender, m, NULL);
1311 dbus_message_unref (m);
1313 m = dbus_message_new_signal ("/foo", "com.example.Fun",
1315 if (!dbus_message_set_destination (m, f->recipient_name))
1317 dbus_connection_send (f->sender, m, NULL);
1318 dbus_message_unref (m);
1320 while (g_queue_get_length (&f->monitored) < 2)
1321 test_main_context_iterate (f->ctx, TRUE);
1323 /* We get the interesting signal and the fun signal, but not the tedious
1326 m = g_queue_pop_head (&f->monitored);
1327 assert_signal (m, f->sender_name, "/foo",
1328 "com.example.Interesting", "UnicastSignal1", "", f->recipient_name);
1329 dbus_message_unref (m);
1331 m = g_queue_pop_head (&f->monitored);
1332 assert_signal (m, f->sender_name, "/foo",
1333 "com.example.Fun", "UnicastSignal3", "", f->recipient_name);
1334 dbus_message_unref (m);
1336 m = g_queue_pop_head (&f->monitored);
1337 g_assert (m == NULL);
1341 test_well_known_destination (Fixture *f,
1342 gconstpointer context)
1346 if (f->address == NULL)
1349 take_well_known_name (f, f->recipient, "com.example.Recipient");
1350 /* we don't expect_take_well_known_name here because the
1351 * monitor isn't up yet */
1353 become_monitor (f, NULL);
1355 /* The sender sends a message to itself. It will not be observed. */
1356 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1357 if (!dbus_message_set_destination (m, f->sender_name))
1359 dbus_connection_send (f->sender, m, NULL);
1360 dbus_message_unref (m);
1362 /* The sender sends a message to the recipient by well-known name.
1363 * It will be observed. */
1364 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1365 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1367 dbus_connection_send (f->sender, m, NULL);
1368 dbus_message_unref (m);
1370 /* The sender sends a message to the recipient by unique name.
1371 * It will still be observed. */
1372 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1373 if (!dbus_message_set_destination (m, f->recipient_name))
1375 dbus_connection_send (f->sender, m, NULL);
1376 dbus_message_unref (m);
1378 while (g_queue_get_length (&f->monitored) < 2)
1379 test_main_context_iterate (f->ctx, TRUE);
1381 m = g_queue_pop_head (&f->monitored);
1382 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1383 "Observed1", "", "com.example.Recipient");
1384 dbus_message_unref (m);
1386 m = g_queue_pop_head (&f->monitored);
1387 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1388 "Observed2", "", f->recipient_name);
1389 dbus_message_unref (m);
1391 m = g_queue_pop_head (&f->monitored);
1392 g_assert (m == NULL);
1396 test_unique_destination (Fixture *f,
1397 gconstpointer context)
1402 NULL, /* match rules */
1405 const gchar *match_rules[2] = { NULL, NULL };
1408 if (f->address == NULL)
1411 take_well_known_name (f, f->recipient, "com.example.Recipient");
1412 /* we don't expect_take_well_known_name here because the
1413 * monitor isn't up yet */
1415 rule = g_strdup_printf ("destination='%s'", f->recipient_name);
1417 g_test_queue_free (rule);
1418 match_rules[0] = rule;
1419 config.match_rules = match_rules;
1421 become_monitor (f, &config);
1423 /* The sender sends a message to itself. It will not be observed. */
1424 m = dbus_message_new_signal ("/foo", "com.example.bar", "Unobserved");
1425 if (!dbus_message_set_destination (m, f->sender_name))
1427 dbus_connection_send (f->sender, m, NULL);
1428 dbus_message_unref (m);
1430 /* The sender sends a message to the recipient by well-known name.
1431 * It will be observed. */
1432 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed1");
1433 if (!dbus_message_set_destination (m, "com.example.Recipient"))
1435 dbus_connection_send (f->sender, m, NULL);
1436 dbus_message_unref (m);
1438 /* The sender sends a message to the recipient by unique name.
1439 * It will still be observed. */
1440 m = dbus_message_new_signal ("/foo", "com.example.bar", "Observed2");
1441 if (!dbus_message_set_destination (m, f->recipient_name))
1443 dbus_connection_send (f->sender, m, NULL);
1444 dbus_message_unref (m);
1446 while (g_queue_get_length (&f->monitored) < 2)
1447 test_main_context_iterate (f->ctx, TRUE);
1449 m = g_queue_pop_head (&f->monitored);
1450 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1451 "Observed1", "", "com.example.Recipient");
1452 dbus_message_unref (m);
1454 m = g_queue_pop_head (&f->monitored);
1455 assert_signal (m, f->sender_name, "/foo", "com.example.bar",
1456 "Observed2", "", f->recipient_name);
1457 dbus_message_unref (m);
1459 m = g_queue_pop_head (&f->monitored);
1460 g_assert (m == NULL);
1464 /* currently only used for the systemd activation test */
1466 expect_new_connection (Fixture *f)
1470 while (g_queue_get_length (&f->monitored) < 4)
1471 test_main_context_iterate (f->ctx, TRUE);
1473 m = g_queue_pop_head (&f->monitored);
1475 dbus_message_unref (m);
1477 m = g_queue_pop_head (&f->monitored);
1478 assert_hello_reply (m);
1479 dbus_message_unref (m);
1481 m = g_queue_pop_head (&f->monitored);
1482 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1483 "NameOwnerChanged", "sss", NULL);
1484 dbus_message_unref (m);
1486 m = g_queue_pop_head (&f->monitored);
1487 assert_name_acquired (m);
1488 dbus_message_unref (m);
1491 /* currently only used for the systemd activation test */
1493 expect_take_well_known_name (Fixture *f,
1494 DBusConnection *connection,
1498 const char *connection_name = dbus_bus_get_unique_name (connection);
1500 while (g_queue_get_length (&f->monitored) < 4)
1501 test_main_context_iterate (f->ctx, TRUE);
1503 m = g_queue_pop_head (&f->monitored);
1504 assert_method_call (m, connection_name, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1505 DBUS_INTERFACE_DBUS, "RequestName", "su");
1506 dbus_message_unref (m);
1508 m = g_queue_pop_head (&f->monitored);
1509 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1510 "NameOwnerChanged", "sss", NULL);
1511 dbus_message_unref (m);
1513 m = g_queue_pop_head (&f->monitored);
1514 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS,
1515 "NameAcquired", "s", connection_name);
1516 dbus_message_unref (m);
1518 m = g_queue_pop_head (&f->monitored);
1519 assert_method_reply (m, DBUS_SERVICE_DBUS, connection_name, "u");
1520 dbus_message_unref (m);
1524 test_activation (Fixture *f,
1525 gconstpointer context)
1529 if (f->address == NULL)
1532 become_monitor (f, NULL);
1534 /* The sender sends a message to an activatable service. */
1535 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal1");
1536 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable1"))
1538 dbus_connection_send (f->sender, m, NULL);
1539 dbus_message_unref (m);
1541 /* We observe the activation request, and the message that caused it,
1542 * before systemd has even joined the bus. */
1543 while (g_queue_get_length (&f->monitored) < 2)
1544 test_main_context_iterate (f->ctx, TRUE);
1546 m = g_queue_pop_head (&f->monitored);
1547 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1548 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1549 "org.freedesktop.systemd1");
1550 dbus_message_unref (m);
1551 m = g_queue_pop_head (&f->monitored);
1552 assert_signal (m, f->sender_name, "/foo",
1553 "com.example.bar", "UnicastSignal1", "",
1554 "com.example.SystemdActivatable1");
1555 dbus_message_unref (m);
1557 /* The fake systemd connects to the bus. */
1558 f->systemd = test_connect_to_bus (f->ctx, f->address);
1559 if (!dbus_connection_add_filter (f->systemd, systemd_filter, f, NULL))
1561 f->systemd_name = dbus_bus_get_unique_name (f->systemd);
1563 expect_new_connection (f);
1564 take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1565 expect_take_well_known_name (f, f->systemd, "org.freedesktop.systemd1");
1567 /* It gets its activation request. */
1568 while (f->systemd_message == NULL)
1569 test_main_context_iterate (f->ctx, TRUE);
1571 m = f->systemd_message;
1572 f->systemd_message = NULL;
1573 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1574 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1575 "org.freedesktop.systemd1");
1576 dbus_message_unref (m);
1578 /* systemd starts the activatable service. */
1579 f->activated = test_connect_to_bus (f->ctx, f->address);
1580 if (!dbus_connection_add_filter (f->activated, activated_filter,
1583 f->activated_name = dbus_bus_get_unique_name (f->activated);
1585 expect_new_connection (f);
1586 take_well_known_name (f, f->activated, "com.example.SystemdActivatable1");
1587 expect_take_well_known_name (f, f->activated,
1588 "com.example.SystemdActivatable1");
1590 /* The message is delivered to the activatable service. */
1591 while (f->activated_message == NULL)
1592 test_main_context_iterate (f->ctx, TRUE);
1594 m = f->activated_message;
1595 f->activated_message = NULL;
1596 assert_signal (m, f->sender_name, "/foo",
1597 "com.example.bar", "UnicastSignal1", "",
1598 "com.example.SystemdActivatable1");
1599 dbus_message_unref (m);
1601 /* The sender sends a message to a different activatable service. */
1602 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal2");
1603 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable2"))
1605 dbus_connection_send (f->sender, m, NULL);
1606 dbus_message_unref (m);
1608 /* This time systemd is already ready for it. */
1609 while (g_queue_get_length (&f->monitored) < 2 ||
1610 f->systemd_message == NULL)
1611 test_main_context_iterate (f->ctx, TRUE);
1613 m = f->systemd_message;
1614 f->systemd_message = NULL;
1615 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1616 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1617 "org.freedesktop.systemd1");
1618 dbus_message_unref (m);
1620 /* The monitor sees the activation request and the signal that
1622 m = g_queue_pop_head (&f->monitored);
1623 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1624 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1625 "org.freedesktop.systemd1");
1626 dbus_message_unref (m);
1627 m = g_queue_pop_head (&f->monitored);
1628 assert_signal (m, f->sender_name, "/foo",
1629 "com.example.bar", "UnicastSignal2", "",
1630 "com.example.SystemdActivatable2");
1631 dbus_message_unref (m);
1633 /* The activatable service takes its name. Here I'm faking it by using
1634 * an existing connection. */
1635 take_well_known_name (f, f->activated, "com.example.SystemdActivatable2");
1637 /* The message is delivered to the activatable service.
1638 * Implementation detail: the monitor sees this happen before it even
1639 * sees that the name request happened, which is pretty odd. */
1640 while (f->activated_message == NULL)
1641 test_main_context_iterate (f->ctx, TRUE);
1643 m = f->activated_message;
1644 f->activated_message = NULL;
1645 assert_signal (m, f->sender_name, "/foo",
1646 "com.example.bar", "UnicastSignal2", "",
1647 "com.example.SystemdActivatable2");
1648 dbus_message_unref (m);
1650 expect_take_well_known_name (f, f->activated,
1651 "com.example.SystemdActivatable2");
1653 /* A third activation. */
1654 m = dbus_message_new_signal ("/foo", "com.example.bar", "UnicastSignal3");
1655 if (!dbus_message_set_destination (m, "com.example.SystemdActivatable3"))
1657 dbus_connection_send (f->sender, m, NULL);
1658 dbus_message_unref (m);
1660 /* Once again, we see the activation request and the reason. */
1661 while (g_queue_get_length (&f->monitored) < 2)
1662 test_main_context_iterate (f->ctx, TRUE);
1664 m = g_queue_pop_head (&f->monitored);
1665 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1666 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1667 "org.freedesktop.systemd1");
1668 dbus_message_unref (m);
1669 m = g_queue_pop_head (&f->monitored);
1670 assert_signal (m, f->sender_name, "/foo",
1671 "com.example.bar", "UnicastSignal3", "",
1672 "com.example.SystemdActivatable3");
1673 dbus_message_unref (m);
1675 /* systemd gets the request too. */
1676 while (f->systemd_message == NULL)
1677 test_main_context_iterate (f->ctx, TRUE);
1679 m = f->systemd_message;
1680 f->systemd_message = NULL;
1681 assert_signal (m, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1682 "org.freedesktop.systemd1.Activator", "ActivationRequest", "s",
1683 "org.freedesktop.systemd1");
1684 dbus_message_unref (m);
1686 /* This time activation fails */
1687 m = dbus_message_new_signal ("/org/freedesktop/systemd1",
1688 "org.freedesktop.systemd1.Activator", "ActivationFailure");
1692 const char *unit = "dbus-com.example.SystemdActivatable3.service";
1693 const char *error_name = "com.example.Nope";
1694 const char *error_message = "Computer says no";
1696 if (!dbus_message_append_args (m,
1697 DBUS_TYPE_STRING, &unit,
1698 DBUS_TYPE_STRING, &error_name,
1699 DBUS_TYPE_STRING, &error_message,
1705 if (!dbus_message_set_destination (m, "org.freedesktop.DBus"))
1707 dbus_connection_send (f->systemd, m, NULL);
1708 dbus_message_unref (m);
1710 /* The monitor sees activation fail */
1712 /* Once again, we see the activation request and the reason. */
1713 while (g_queue_get_length (&f->monitored) < 1)
1714 test_main_context_iterate (f->ctx, TRUE);
1716 m = g_queue_pop_head (&f->monitored);
1717 assert_error_reply (m, DBUS_SERVICE_DBUS, f->sender_name,
1718 "com.example.Nope");
1719 dbus_message_unref (m);
1721 #endif /* DBUS_UNIX */
1724 teardown (Fixture *f,
1725 gconstpointer context G_GNUC_UNUSED)
1727 dbus_error_free (&f->e);
1728 g_clear_error (&f->ge);
1730 if (f->monitor != NULL)
1732 dbus_connection_remove_filter (f->monitor, monitor_filter, f);
1733 dbus_connection_close (f->monitor);
1734 dbus_connection_unref (f->monitor);
1738 if (f->sender != NULL)
1740 dbus_connection_close (f->sender);
1741 dbus_connection_unref (f->sender);
1745 if (f->recipient != NULL)
1747 dbus_connection_remove_filter (f->recipient, recipient_filter, f);
1748 dbus_connection_close (f->recipient);
1749 dbus_connection_unref (f->recipient);
1750 f->recipient = NULL;
1753 if (f->systemd != NULL)
1755 dbus_connection_remove_filter (f->systemd, systemd_filter, f);
1756 dbus_connection_close (f->systemd);
1757 dbus_connection_unref (f->systemd);
1761 if (f->activated != NULL)
1763 dbus_connection_remove_filter (f->activated, activated_filter, f);
1764 dbus_connection_close (f->activated);
1765 dbus_connection_unref (f->activated);
1766 f->activated = NULL;
1769 test_kill_pid (f->daemon_pid);
1770 g_spawn_close_pid (f->daemon_pid);
1772 test_main_context_unref (f->ctx);
1774 g_queue_foreach (&f->monitored, (GFunc) dbus_message_unref, NULL);
1775 g_queue_clear (&f->monitored);
1777 g_free (f->address);
1784 test_init (&argc, &argv);
1786 g_test_add ("/monitor/invalid", Fixture, NULL,
1787 setup, test_invalid, teardown);
1788 g_test_add ("/monitor/become", Fixture, &side_effects_config,
1789 setup, test_become_monitor, teardown);
1790 g_test_add ("/monitor/broadcast", Fixture, NULL,
1791 setup, test_broadcast, teardown);
1792 g_test_add ("/monitor/forbidden-broadcast", Fixture, &forbidding_config,
1793 setup, test_forbidden_broadcast, teardown);
1794 g_test_add ("/monitor/unicast-signal", Fixture, NULL,
1795 setup, test_unicast_signal, teardown);
1796 g_test_add ("/monitor/forbidden", Fixture, &forbidding_config,
1797 setup, test_forbidden, teardown);
1798 g_test_add ("/monitor/method-call", Fixture, NULL,
1799 setup, test_method_call, teardown);
1800 g_test_add ("/monitor/forbidden-method", Fixture, &forbidding_config,
1801 setup, test_forbidden_method_call, teardown);
1802 g_test_add ("/monitor/dbus-daemon", Fixture, NULL,
1803 setup, test_dbus_daemon, teardown);
1804 g_test_add ("/monitor/selective", Fixture, &selective_config,
1805 setup, test_selective, teardown);
1806 g_test_add ("/monitor/well-known-destination",
1807 Fixture, &well_known_destination_config,
1808 setup, test_well_known_destination, teardown);
1809 g_test_add ("/monitor/unique-destination",
1811 setup, test_unique_destination, teardown);
1812 g_test_add ("/monitor/wildcard", Fixture, &wildcard_config,
1813 setup, test_unicast_signal, teardown);
1814 g_test_add ("/monitor/no-rule", Fixture, &no_rules_config,
1815 setup, test_unicast_signal, teardown);
1816 g_test_add ("/monitor/eavesdrop", Fixture, &eavesdrop_config,
1817 setup, test_unicast_signal, teardown);
1818 g_test_add ("/monitor/no-eavesdrop", Fixture, &no_eavesdrop_config,
1819 setup, test_unicast_signal, teardown);
1822 /* this relies on the systemd activation code path */
1823 g_test_add ("/monitor/activation", Fixture, &fake_systemd_config,
1824 setup, test_activation, teardown);
1827 return g_test_run ();