1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connection.h"
32 #include "activation.h"
38 #include <dbus/dbus-internals.h>
39 #include <dbus/dbus-connection-internal.h>
40 #include <dbus/dbus-misc.h>
43 #ifdef HAVE_UNIX_FD_PASSING
44 #include <dbus/dbus-sysdeps-unix.h>
48 /* This is hard-coded in the files in valid-config-files-*. We have to use
49 * the debug-pipe transport because the tests in this file require that
50 * dbus_connection_open_private() does not block. */
51 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
53 static inline const char *
54 nonnull (const char *maybe_null,
57 return (maybe_null ? maybe_null : if_null);
61 send_one_message (DBusConnection *connection,
63 DBusConnection *sender,
64 DBusConnection *addressed_recipient,
66 BusTransaction *transaction,
69 DBusError stack_error = DBUS_ERROR_INIT;
70 BusDeferredMessage *deferred_message = NULL;
73 result = bus_context_check_security_policy (context, transaction, sender, addressed_recipient,
74 connection, message, NULL, &stack_error, &deferred_message);
76 if (result == BUS_RESULT_FALSE)
78 if (!bus_transaction_capture_error_reply (transaction, sender,
79 &stack_error, message))
81 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
82 "broadcast rejected, but not enough "
83 "memory to tell monitors");
86 dbus_error_free (&stack_error);
87 return TRUE; /* don't send it but don't return an error either */
90 if (dbus_message_contains_unix_fds(message) &&
91 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
93 dbus_set_error (&stack_error, DBUS_ERROR_NOT_SUPPORTED,
94 "broadcast cannot be delivered to %s (%s) because "
95 "it does not support receiving Unix fds",
96 bus_connection_get_name (connection),
97 bus_connection_get_loginfo (connection));
99 if (!bus_transaction_capture_error_reply (transaction, sender,
100 &stack_error, message))
102 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
103 "broadcast with Unix fd not delivered, but not "
104 "enough memory to tell monitors");
107 dbus_error_free (&stack_error);
108 return TRUE; /* don't send it but don't return an error either */
111 if (result == BUS_RESULT_LATER)
113 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
118 return TRUE; /* pretend to have sent it */
121 if (!bus_transaction_send (transaction,
133 bus_dispatch_matches (BusTransaction *transaction,
134 DBusConnection *sender,
135 DBusConnection *addressed_recipient,
136 DBusMessage *message,
137 BusDeferredMessage *dispatched_deferred_message,
141 BusConnections *connections;
142 DBusList *recipients;
143 BusMatchmaker *matchmaker;
146 BusDeferredMessage *deferred_message = NULL;
148 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
150 /* sender and recipient can both be NULL for the bus driver,
151 * or for signals with no particular recipient
155 * With deferred messages, dispatch may happen when there is no sender anymore.
156 * Policy rules involving the sender should be checked before, anyway.
158 /* _dbus_assert (sender == NULL || bus_connection_is_active (sender)); */
159 _dbus_assert (dbus_message_get_sender (message) != NULL);
161 context = bus_transaction_get_context (transaction);
163 /* First, send the message to the addressed_recipient, if there is one. */
164 if (addressed_recipient != NULL)
167 /* To maintain message order message needs to be appended at the recipient if there are already
168 * deferred messages and we are not doing deferred dispatch
170 if (dispatched_deferred_message == NULL && bus_connection_has_deferred_messages(addressed_recipient))
172 result = bus_context_check_security_policy(context, transaction,
173 sender, addressed_recipient, addressed_recipient, message, NULL, error,
176 if (deferred_message == NULL)
177 deferred_message = bus_deferred_message_new(message, sender,
178 addressed_recipient, addressed_recipient, result);
180 bus_deferred_message_ref(deferred_message);
182 if (deferred_message == NULL)
185 return BUS_RESULT_FALSE;
188 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, FALSE))
190 bus_deferred_message_unref(deferred_message);
192 return BUS_RESULT_FALSE;
195 bus_deferred_message_unref(deferred_message);
196 return BUS_RESULT_TRUE; /* pretend to have sent it */
199 if (dispatched_deferred_message != NULL)
201 result = bus_deferred_message_get_response(dispatched_deferred_message);
202 if (result == BUS_RESULT_TRUE)
204 /* if we know the result of policy check we still need to check if message limits
205 * are not exceeded. It is also required to add entry in expected replies list if
206 * this is a method call
208 if (!bus_deferred_message_check_message_limits(dispatched_deferred_message, error))
209 return BUS_RESULT_FALSE;
211 if (!bus_deferred_message_expect_method_reply(dispatched_deferred_message, transaction, error))
212 return BUS_RESULT_FALSE;
214 else if (result == BUS_RESULT_FALSE)
216 bus_deferred_message_create_error(dispatched_deferred_message, "Rejected message", error);
217 return BUS_RESULT_FALSE;
221 result = BUS_RESULT_LATER;
223 if (result == BUS_RESULT_LATER)
224 result = bus_context_check_security_policy(context, transaction,
225 sender, addressed_recipient, addressed_recipient, message, NULL, error,
230 case BUS_RESULT_TRUE:
232 case BUS_RESULT_FALSE:
233 return BUS_RESULT_FALSE;
234 case BUS_RESULT_LATER:
236 BusDeferredMessageStatus status;
238 if (dispatched_deferred_message != NULL)
240 /* for deferred dispatch prepend message at the recipient */
241 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, TRUE))
244 return BUS_RESULT_FALSE;
246 return BUS_RESULT_TRUE; /* pretend to have sent it */
249 status = bus_deferred_message_get_status(deferred_message);
251 if (status & BUS_DEFERRED_MESSAGE_CHECK_SEND)
253 /* send rule result not available - disable dispatching messages from the sender */
254 bus_deferred_message_disable_sender(deferred_message);
255 return BUS_RESULT_LATER;
257 else if (status & BUS_DEFERRED_MESSAGE_CHECK_RECEIVE)
259 /* receive rule result not available - queue message at the recipient */
260 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, FALSE))
263 return BUS_RESULT_FALSE;
266 return BUS_RESULT_TRUE; /* pretend to have sent it */
270 _dbus_verbose("deferred message has no status field set unexpectedly\n");
271 return BUS_RESULT_FALSE;
276 if (dbus_message_contains_unix_fds (message) &&
277 !dbus_connection_can_send_type (addressed_recipient,
280 dbus_set_error (error,
281 DBUS_ERROR_NOT_SUPPORTED,
282 "Tried to send message with Unix file descriptors"
283 "to a client that doesn't support that.");
284 return BUS_RESULT_FALSE;
287 /* Dispatch the message */
288 if (!bus_transaction_send(transaction, addressed_recipient, message,
289 dispatched_deferred_message != NULL ? TRUE : FALSE))
292 return BUS_RESULT_FALSE;
296 /* Now dispatch to others who look interested in this message */
297 connections = bus_context_get_connections (context);
298 dbus_error_init (&tmp_error);
299 matchmaker = bus_context_get_matchmaker (context);
302 if (!bus_matchmaker_get_recipients (matchmaker, connections,
303 sender, addressed_recipient, message,
307 return BUS_RESULT_FALSE;
310 link = _dbus_list_get_first_link (&recipients);
313 DBusConnection *dest;
317 if (!send_one_message (dest, context, sender, addressed_recipient,
318 message, transaction, &tmp_error))
321 link = _dbus_list_get_next_link (&recipients, link);
324 _dbus_list_clear (&recipients);
326 if (dbus_error_is_set (&tmp_error))
328 dbus_move_error (&tmp_error, error);
329 return BUS_RESULT_FALSE;
332 return BUS_RESULT_TRUE;
335 static DBusHandlerResult
336 bus_dispatch (DBusConnection *connection,
337 DBusMessage *message)
339 const char *sender, *service_name;
341 BusTransaction *transaction;
343 DBusHandlerResult result;
344 DBusConnection *addressed_recipient;
346 result = DBUS_HANDLER_RESULT_HANDLED;
349 addressed_recipient = NULL;
350 dbus_error_init (&error);
352 context = bus_connection_get_context (connection);
353 _dbus_assert (context != NULL);
355 /* If we can't even allocate an OOM error, we just go to sleep
358 while (!bus_connection_preallocate_oom_error (connection))
359 _dbus_wait_for_memory ();
361 /* Ref connection in case we disconnect it at some point in here */
362 dbus_connection_ref (connection);
364 /* Monitors aren't meant to send messages to us. */
365 if (bus_connection_is_monitor (connection))
367 sender = bus_connection_get_name (connection);
369 /* should never happen */
371 sender = "(unknown)";
373 if (dbus_message_is_signal (message,
374 DBUS_INTERFACE_LOCAL,
377 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
378 "Monitoring connection %s closed.", sender);
379 bus_connection_disconnected (connection);
384 /* Monitors are not allowed to send messages, because that
385 * probably indicates that the monitor is incorrectly replying
386 * to its eavesdropped messages, and we want the authors of
387 * such monitors to fix them.
389 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
390 "Monitoring connection %s (%s) is not allowed "
391 "to send messages; closing it. Please fix the "
392 "monitor to not do that. "
393 "(message type=\"%s\" interface=\"%s\" "
394 "member=\"%s\" error name=\"%s\" "
395 "destination=\"%s\")",
396 sender, bus_connection_get_loginfo (connection),
397 dbus_message_type_to_string (
398 dbus_message_get_type (message)),
399 nonnull (dbus_message_get_interface (message),
401 nonnull (dbus_message_get_member (message),
403 nonnull (dbus_message_get_error_name (message),
405 nonnull (dbus_message_get_destination (message),
407 dbus_connection_close (connection);
412 service_name = dbus_message_get_destination (message);
414 #ifdef DBUS_ENABLE_VERBOSE_MODE
416 const char *interface_name, *member_name, *error_name;
418 interface_name = dbus_message_get_interface (message);
419 member_name = dbus_message_get_member (message);
420 error_name = dbus_message_get_error_name (message);
422 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
423 interface_name ? interface_name : "(no interface)",
424 member_name ? member_name : "(no member)",
425 error_name ? error_name : "(no error name)",
426 service_name ? service_name : "peer");
428 #endif /* DBUS_ENABLE_VERBOSE_MODE */
430 /* If service_name is NULL, if it's a signal we send it to all
431 * connections with a match rule. If it's not a signal, there
432 * are some special cases here but mostly we just bail out.
434 if (service_name == NULL)
436 if (dbus_message_is_signal (message,
437 DBUS_INTERFACE_LOCAL,
440 bus_connection_disconnected (connection);
444 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
446 /* DBusConnection also handles some of these automatically, we leave
449 * FIXME: this means monitors won't get the opportunity to see
450 * non-signals with NULL destination, or their replies (which in
451 * practice are UnknownMethod errors)
453 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
458 /* Create our transaction */
459 transaction = bus_transaction_new (context);
460 if (transaction == NULL)
462 BUS_SET_OOM (&error);
466 /* Assign a sender to the message */
467 if (bus_connection_is_active (connection))
469 sender = bus_connection_get_name (connection);
470 _dbus_assert (sender != NULL);
472 if (!dbus_message_set_sender (message, sender))
474 BUS_SET_OOM (&error);
480 /* For monitors' benefit: we don't want the sender to be able to
481 * trick the monitor by supplying a forged sender, and we also
482 * don't want the message to have no sender at all. */
483 if (!dbus_message_set_sender (message, ":not.active.yet"))
485 BUS_SET_OOM (&error);
490 /* We need to refetch the service name here, because
491 * dbus_message_set_sender can cause the header to be
492 * reallocated, and thus the service_name pointer will become
495 service_name = dbus_message_get_destination (message);
498 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
500 BusDeferredMessage *deferred_message = NULL;
502 if (!bus_transaction_capture (transaction, connection, NULL, message))
504 BUS_SET_OOM (&error);
508 switch (bus_context_check_security_policy (context, transaction,
509 connection, NULL, NULL, message,
513 case BUS_RESULT_TRUE:
515 case BUS_RESULT_FALSE:
516 _dbus_verbose ("Security policy rejected message\n");
518 case BUS_RESULT_LATER:
519 /* Disable dispatching messages from the sender,
520 * roll back and dispatch the message once the policy result is available */
521 bus_deferred_message_disable_sender(deferred_message);
522 bus_transaction_cancel_and_free (transaction);
524 result = DBUS_HANDLER_RESULT_LATER;
528 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
529 switch (bus_driver_handle_message (connection, transaction, message, &error))
531 case BUS_RESULT_TRUE:
533 case BUS_RESULT_FALSE:
535 case BUS_RESULT_LATER:
536 bus_transaction_cancel_and_free (transaction);
538 result = DBUS_HANDLER_RESULT_LATER;
542 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
544 if (!bus_transaction_capture (transaction, connection, NULL, message))
546 BUS_SET_OOM (&error);
550 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
551 dbus_connection_close (connection);
554 else if (service_name != NULL) /* route to named service */
556 DBusString service_string;
558 BusRegistry *registry;
560 _dbus_assert (service_name != NULL);
562 registry = bus_connection_get_registry (connection);
564 _dbus_string_init_const (&service_string, service_name);
565 service = bus_registry_lookup (registry, &service_string);
567 if (service == NULL && dbus_message_get_auto_start (message))
569 BusActivation *activation;
570 BusDeferredMessage *deferred_message = NULL;
572 if (!bus_transaction_capture (transaction, connection, NULL,
575 BUS_SET_OOM (&error);
579 activation = bus_connection_get_activation (connection);
581 /* This will do as much of a security policy check as it can.
582 * We can't do the full security policy check here, since the
583 * addressed recipient service doesn't exist yet. We do it before
584 * sending the message after the service has been created.
586 switch (bus_activation_activate_service (activation, connection, transaction, TRUE,
587 message, service_name, &error,
590 case BUS_RESULT_FALSE:
591 _DBUS_ASSERT_ERROR_IS_SET (&error);
592 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
594 case BUS_RESULT_LATER:
595 bus_deferred_message_disable_sender(deferred_message);
596 bus_transaction_cancel_and_free (transaction);
598 result = DBUS_HANDLER_RESULT_LATER;
600 case BUS_RESULT_TRUE:
606 else if (service == NULL)
608 if (!bus_transaction_capture (transaction, connection,
611 BUS_SET_OOM (&error);
615 dbus_set_error (&error,
616 DBUS_ERROR_NAME_HAS_NO_OWNER,
617 "Name \"%s\" does not exist",
623 addressed_recipient = bus_service_get_primary_owners_connection (service);
624 _dbus_assert (addressed_recipient != NULL);
626 if (!bus_transaction_capture (transaction, connection,
627 addressed_recipient, message))
629 BUS_SET_OOM (&error);
634 else /* service_name == NULL */
636 if (!bus_transaction_capture (transaction, connection, NULL, message))
638 BUS_SET_OOM (&error);
643 /* Now send the message to its destination (or not, if
644 * addressed_recipient == NULL), and match it against other connections'
647 switch (bus_dispatch_matches (transaction, connection, addressed_recipient, message, NULL, &error))
649 case BUS_RESULT_TRUE:
650 case BUS_RESULT_FALSE:
652 case BUS_RESULT_LATER:
653 /* Roll back and dispatch the message once the policy result is available */
654 bus_transaction_cancel_and_free (transaction);
656 result = DBUS_HANDLER_RESULT_LATER;
661 if (dbus_error_is_set (&error))
663 /* Even if we disconnected it, pretend to send it any pending error
664 * messages so that monitors can observe them.
666 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
668 bus_connection_send_oom_error (connection, message);
670 /* cancel transaction due to OOM */
671 if (transaction != NULL)
673 bus_transaction_cancel_and_free (transaction);
679 /* Try to send the real error, if no mem to do that, send
682 _dbus_assert (transaction != NULL);
683 if (!bus_transaction_send_error_reply (transaction, connection,
686 bus_connection_send_oom_error (connection, message);
688 /* cancel transaction due to OOM */
689 if (transaction != NULL)
691 bus_transaction_cancel_and_free (transaction);
698 dbus_error_free (&error);
701 if (transaction != NULL)
703 bus_transaction_execute_and_free (transaction);
706 dbus_connection_unref (connection);
711 static DBusHandlerResult
712 bus_dispatch_message_filter (DBusConnection *connection,
713 DBusMessage *message,
716 return bus_dispatch (connection, message);
720 bus_dispatch_add_connection (DBusConnection *connection)
722 if (!dbus_connection_add_filter (connection,
723 bus_dispatch_message_filter,
731 bus_dispatch_remove_connection (DBusConnection *connection)
733 /* Here we tell the bus driver that we want to get off. */
734 bus_driver_remove_connection (connection);
736 dbus_connection_remove_filter (connection,
737 bus_dispatch_message_filter,
741 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
747 /* This is used to know whether we need to block in order to finish
748 * sending a message, or whether the initial dbus_connection_send()
749 * already flushed the queue.
751 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
753 typedef dbus_bool_t (* Check1Func) (BusContext *context);
754 typedef dbus_bool_t (* Check2Func) (BusContext *context,
755 DBusConnection *connection);
757 static dbus_bool_t check_no_leftovers (BusContext *context);
760 block_connection_until_message_from_bus (BusContext *context,
761 DBusConnection *connection,
762 const char *what_is_expected)
764 _dbus_verbose ("expecting: %s\n", what_is_expected);
766 while (dbus_connection_get_dispatch_status (connection) ==
767 DBUS_DISPATCH_COMPLETE &&
768 dbus_connection_get_is_connected (connection))
770 bus_test_run_bus_loop (context, TRUE);
771 bus_test_run_clients_loop (FALSE);
776 spin_connection_until_authenticated (BusContext *context,
777 DBusConnection *connection)
779 _dbus_verbose ("Spinning to auth connection %p\n", connection);
780 while (!dbus_connection_get_is_authenticated (connection) &&
781 dbus_connection_get_is_connected (connection))
783 bus_test_run_bus_loop (context, FALSE);
784 bus_test_run_clients_loop (FALSE);
786 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
789 /* compensate for fact that pop_message() can return #NULL due to OOM */
791 pop_message_waiting_for_memory (DBusConnection *connection)
793 while (dbus_connection_get_dispatch_status (connection) ==
794 DBUS_DISPATCH_NEED_MEMORY)
795 _dbus_wait_for_memory ();
797 return dbus_connection_pop_message (connection);
801 borrow_message_waiting_for_memory (DBusConnection *connection)
803 while (dbus_connection_get_dispatch_status (connection) ==
804 DBUS_DISPATCH_NEED_MEMORY)
805 _dbus_wait_for_memory ();
807 return dbus_connection_borrow_message (connection);
811 warn_unexpected_real (DBusConnection *connection,
812 DBusMessage *message,
813 const char *expected,
814 const char *function,
818 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s",
820 dbus_message_get_interface (message) ?
821 dbus_message_get_interface (message) : "(unset)",
822 dbus_message_get_member (message) ?
823 dbus_message_get_member (message) : "(unset)",
824 dbus_message_get_error_name (message) ?
825 dbus_message_get_error_name (message) : "(unset)",
829 _dbus_warn ("%s:%d received no message on %p, expecting %s",
830 function, line, connection, expected);
833 #define warn_unexpected(connection, message, expected) \
834 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
837 verbose_message_received (DBusConnection *connection,
838 DBusMessage *message)
840 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
841 dbus_message_get_interface (message) ?
842 dbus_message_get_interface (message) : "(unset)",
843 dbus_message_get_member (message) ?
844 dbus_message_get_member (message) : "(unset)",
845 dbus_message_get_error_name (message) ?
846 dbus_message_get_error_name (message) : "(unset)",
859 ServiceInfoKind expected_kind;
860 const char *expected_service_name;
862 DBusConnection *skip_connection;
864 } CheckServiceOwnerChangedData;
867 check_service_owner_changed_foreach (DBusConnection *connection,
870 CheckServiceOwnerChangedData *d = data;
871 DBusMessage *message;
873 const char *service_name, *old_owner, *new_owner;
875 if (d->expected_kind == SERVICE_CREATED
876 && connection == d->skip_connection)
879 dbus_error_init (&error);
882 message = pop_message_waiting_for_memory (connection);
885 block_connection_until_message_from_bus (d->context, connection, "NameOwnerChanged");
886 message = pop_message_waiting_for_memory (connection);
889 _dbus_warn ("Did not receive a message on %p, expecting %s",
890 connection, "NameOwnerChanged");
894 else if (!dbus_message_is_signal (message,
898 warn_unexpected (connection, message, "NameOwnerChanged");
904 reget_service_info_data:
909 dbus_message_get_args (message, &error,
910 DBUS_TYPE_STRING, &service_name,
911 DBUS_TYPE_STRING, &old_owner,
912 DBUS_TYPE_STRING, &new_owner,
915 if (dbus_error_is_set (&error))
917 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
919 dbus_error_free (&error);
920 _dbus_wait_for_memory ();
921 goto reget_service_info_data;
925 _dbus_warn ("Did not get the expected arguments");
930 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
931 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
932 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
934 _dbus_warn ("inconsistent NameOwnerChanged arguments");
938 if (strcmp (service_name, d->expected_service_name) != 0)
940 _dbus_warn ("expected info on service %s, got info on %s",
941 d->expected_service_name,
946 if (*service_name == ':' && new_owner[0]
947 && strcmp (service_name, new_owner) != 0)
949 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])",
950 service_name, old_owner, new_owner);
958 dbus_error_free (&error);
961 dbus_message_unref (message);
968 kill_client_connection (BusContext *context,
969 DBusConnection *connection)
973 CheckServiceOwnerChangedData socd;
975 _dbus_verbose ("killing connection %p\n", connection);
977 s = dbus_bus_get_unique_name (connection);
978 _dbus_assert (s != NULL);
980 while ((base_service = _dbus_strdup (s)) == NULL)
981 _dbus_wait_for_memory ();
983 dbus_connection_ref (connection);
985 /* kick in the disconnect handler that unrefs the connection */
986 dbus_connection_close (connection);
988 bus_test_run_everything (context);
990 _dbus_assert (bus_test_client_listed (connection));
992 /* Run disconnect handler in test.c */
993 if (bus_connection_dispatch_one_message (connection))
994 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
996 _dbus_assert (!dbus_connection_get_is_connected (connection));
997 dbus_connection_unref (connection);
999 _dbus_assert (!bus_test_client_listed (connection));
1001 socd.expected_kind = SERVICE_DELETED;
1002 socd.expected_service_name = base_service;
1003 socd.failed = FALSE;
1004 socd.skip_connection = NULL;
1005 socd.context = context;
1007 bus_test_clients_foreach (check_service_owner_changed_foreach,
1010 dbus_free (base_service);
1013 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
1015 if (!check_no_leftovers (context))
1016 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
1020 kill_client_connection_unchecked (DBusConnection *connection)
1022 /* This kills the connection without expecting it to affect
1023 * the rest of the bus.
1025 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
1027 dbus_connection_ref (connection);
1028 dbus_connection_close (connection);
1029 /* dispatching disconnect handler will unref once */
1030 if (bus_connection_dispatch_one_message (connection))
1031 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
1033 _dbus_assert (!bus_test_client_listed (connection));
1034 dbus_connection_unref (connection);
1040 } CheckNoMessagesData;
1043 check_no_messages_foreach (DBusConnection *connection,
1046 CheckNoMessagesData *d = data;
1047 DBusMessage *message;
1049 message = pop_message_waiting_for_memory (connection);
1050 if (message != NULL)
1052 warn_unexpected (connection, message, "no messages");
1058 dbus_message_unref (message);
1063 check_no_leftovers (BusContext *context)
1065 CheckNoMessagesData nmd;
1068 bus_test_clients_foreach (check_no_messages_foreach,
1073 _dbus_verbose ("leftover message found\n");
1080 /* returns TRUE if the correct thing happens,
1081 * but the correct thing may include OOM errors.
1084 check_hello_message (BusContext *context,
1085 DBusConnection *connection)
1087 DBusMessage *message;
1088 DBusMessage *name_message;
1089 dbus_uint32_t serial;
1093 const char *acquired;
1096 dbus_error_init (&error);
1100 name_message = NULL;
1102 _dbus_verbose ("check_hello_message for %p\n", connection);
1104 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1106 DBUS_INTERFACE_DBUS,
1109 if (message == NULL)
1112 dbus_connection_ref (connection); /* because we may get disconnected */
1114 if (!dbus_connection_send (connection, message, &serial))
1116 dbus_message_unref (message);
1117 dbus_connection_unref (connection);
1121 _dbus_assert (dbus_message_has_signature (message, ""));
1123 dbus_message_unref (message);
1126 if (!dbus_connection_get_is_connected (connection))
1128 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1130 dbus_connection_unref (connection);
1135 /* send our message */
1136 bus_test_run_clients_loop (SEND_PENDING (connection));
1138 if (!dbus_connection_get_is_connected (connection))
1140 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1142 dbus_connection_unref (connection);
1147 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1149 if (!dbus_connection_get_is_connected (connection))
1151 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1153 dbus_connection_unref (connection);
1158 message = pop_message_waiting_for_memory (connection);
1159 if (message == NULL)
1161 _dbus_warn ("Did not receive a reply to %s %d on %p",
1162 "Hello", serial, connection);
1166 verbose_message_received (connection, message);
1168 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1170 _dbus_warn ("Message has wrong sender %s",
1171 dbus_message_get_sender (message) ?
1172 dbus_message_get_sender (message) : "(none)");
1176 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1178 if (dbus_message_is_error (message,
1179 DBUS_ERROR_NO_MEMORY))
1181 ; /* good, this is a valid response */
1185 warn_unexpected (connection, message, "not this error");
1192 CheckServiceOwnerChangedData socd;
1194 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1196 ; /* good, expected */
1200 warn_unexpected (connection, message, "method return for Hello");
1205 retry_get_hello_name:
1206 if (!dbus_message_get_args (message, &error,
1207 DBUS_TYPE_STRING, &name,
1210 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1212 _dbus_verbose ("no memory to get service name arg from hello\n");
1213 dbus_error_free (&error);
1214 _dbus_wait_for_memory ();
1215 goto retry_get_hello_name;
1219 _dbus_assert (dbus_error_is_set (&error));
1220 _dbus_warn ("Did not get the expected single string argument to hello");
1225 _dbus_verbose ("Got hello name: %s\n", name);
1227 while (!dbus_bus_set_unique_name (connection, name))
1228 _dbus_wait_for_memory ();
1230 socd.expected_kind = SERVICE_CREATED;
1231 socd.expected_service_name = name;
1232 socd.failed = FALSE;
1233 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
1234 socd.context = context;
1236 bus_test_clients_foreach (check_service_owner_changed_foreach,
1242 name_message = message;
1243 /* Client should also have gotten ServiceAcquired */
1245 message = pop_message_waiting_for_memory (connection);
1246 if (message == NULL)
1248 block_connection_until_message_from_bus (context, connection, "signal NameAcquired");
1249 message = pop_message_waiting_for_memory (connection);
1250 if (message == NULL)
1252 _dbus_warn ("Expecting %s, got nothing",
1257 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
1260 _dbus_warn ("Expecting %s, got smthg else",
1265 retry_get_acquired_name:
1266 if (!dbus_message_get_args (message, &error,
1267 DBUS_TYPE_STRING, &acquired,
1270 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1272 _dbus_verbose ("no memory to get service name arg from acquired\n");
1273 dbus_error_free (&error);
1274 _dbus_wait_for_memory ();
1275 goto retry_get_acquired_name;
1279 _dbus_assert (dbus_error_is_set (&error));
1280 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired");
1285 _dbus_verbose ("Got acquired name: %s\n", acquired);
1287 if (strcmp (acquired, name) != 0)
1289 _dbus_warn ("Acquired name is %s but expected %s",
1296 if (!check_no_leftovers (context))
1302 _dbus_verbose ("ending - retval = %d\n", retval);
1304 dbus_error_free (&error);
1307 dbus_message_unref (message);
1310 dbus_message_unref (name_message);
1312 dbus_connection_unref (connection);
1317 /* returns TRUE if the correct thing happens,
1318 * but the correct thing may include OOM errors.
1321 check_double_hello_message (BusContext *context,
1322 DBusConnection *connection)
1324 DBusMessage *message;
1325 dbus_uint32_t serial;
1330 dbus_error_init (&error);
1333 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1335 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1337 DBUS_INTERFACE_DBUS,
1340 if (message == NULL)
1343 if (!dbus_connection_send (connection, message, &serial))
1345 dbus_message_unref (message);
1349 dbus_message_unref (message);
1352 /* send our message */
1353 bus_test_run_clients_loop (SEND_PENDING (connection));
1355 dbus_connection_ref (connection); /* because we may get disconnected */
1356 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1358 if (!dbus_connection_get_is_connected (connection))
1360 _dbus_verbose ("connection was disconnected\n");
1362 dbus_connection_unref (connection);
1367 dbus_connection_unref (connection);
1369 message = pop_message_waiting_for_memory (connection);
1370 if (message == NULL)
1372 _dbus_warn ("Did not receive a reply to %s %d on %p",
1373 "Hello", serial, connection);
1377 verbose_message_received (connection, message);
1379 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1381 _dbus_warn ("Message has wrong sender %s",
1382 dbus_message_get_sender (message) ?
1383 dbus_message_get_sender (message) : "(none)");
1387 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1389 warn_unexpected (connection, message, "method return for Hello");
1393 if (!check_no_leftovers (context))
1399 dbus_error_free (&error);
1402 dbus_message_unref (message);
1407 /* returns TRUE if the correct thing happens,
1408 * but the correct thing may include OOM errors.
1411 check_get_connection_unix_user (BusContext *context,
1412 DBusConnection *connection)
1414 DBusMessage *message;
1415 dbus_uint32_t serial;
1418 const char *base_service_name;
1422 dbus_error_init (&error);
1425 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1427 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1429 DBUS_INTERFACE_DBUS,
1430 "GetConnectionUnixUser");
1432 if (message == NULL)
1435 base_service_name = dbus_bus_get_unique_name (connection);
1437 if (!dbus_message_append_args (message,
1438 DBUS_TYPE_STRING, &base_service_name,
1441 dbus_message_unref (message);
1445 if (!dbus_connection_send (connection, message, &serial))
1447 dbus_message_unref (message);
1451 /* send our message */
1452 bus_test_run_clients_loop (SEND_PENDING (connection));
1454 dbus_message_unref (message);
1457 dbus_connection_ref (connection); /* because we may get disconnected */
1458 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1460 if (!dbus_connection_get_is_connected (connection))
1462 _dbus_verbose ("connection was disconnected\n");
1464 dbus_connection_unref (connection);
1469 dbus_connection_unref (connection);
1471 message = pop_message_waiting_for_memory (connection);
1472 if (message == NULL)
1474 _dbus_warn ("Did not receive a reply to %s %d on %p",
1475 "GetConnectionUnixUser", serial, connection);
1479 verbose_message_received (connection, message);
1481 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1483 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1485 ; /* good, this is a valid response */
1488 else if (dbus_message_is_error (message, DBUS_ERROR_FAILED))
1490 /* this is OK, Unix uids aren't meaningful on Windows */
1495 warn_unexpected (connection, message, "not this error");
1502 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1504 ; /* good, expected */
1508 warn_unexpected (connection, message,
1509 "method_return for GetConnectionUnixUser");
1516 if (!dbus_message_get_args (message, &error,
1517 DBUS_TYPE_UINT32, &uid,
1520 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1522 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1523 dbus_error_free (&error);
1524 _dbus_wait_for_memory ();
1525 goto retry_get_property;
1529 _dbus_assert (dbus_error_is_set (&error));
1530 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser");
1536 if (!check_no_leftovers (context))
1542 dbus_error_free (&error);
1545 dbus_message_unref (message);
1550 /* returns TRUE if the correct thing happens,
1551 * but the correct thing may include OOM errors.
1554 check_get_connection_unix_process_id (BusContext *context,
1555 DBusConnection *connection)
1557 DBusMessage *message;
1558 dbus_uint32_t serial;
1561 const char *base_service_name;
1567 dbus_error_init (&error);
1570 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1572 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1574 DBUS_INTERFACE_DBUS,
1575 "GetConnectionUnixProcessID");
1577 if (message == NULL)
1580 base_service_name = dbus_bus_get_unique_name (connection);
1582 if (!dbus_message_append_args (message,
1583 DBUS_TYPE_STRING, &base_service_name,
1586 dbus_message_unref (message);
1590 if (!dbus_connection_send (connection, message, &serial))
1592 dbus_message_unref (message);
1596 /* send our message */
1597 bus_test_run_clients_loop (SEND_PENDING (connection));
1599 dbus_message_unref (message);
1602 dbus_connection_ref (connection); /* because we may get disconnected */
1603 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1605 if (!dbus_connection_get_is_connected (connection))
1607 _dbus_verbose ("connection was disconnected\n");
1609 dbus_connection_unref (connection);
1614 dbus_connection_unref (connection);
1616 message = pop_message_waiting_for_memory (connection);
1617 if (message == NULL)
1619 _dbus_warn ("Did not receive a reply to %s %d on %p",
1620 "GetConnectionUnixProcessID", serial, connection);
1624 verbose_message_received (connection, message);
1626 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1628 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1630 ; /* good, this is a valid response */
1633 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1635 /* We are expecting this error, since we know in the test suite we aren't
1636 * talking to a client running on UNIX
1638 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1643 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
1644 defined(__linux__) || \
1645 defined(__OpenBSD__)
1646 /* In principle NetBSD should also be in that list, but
1647 * its implementation of PID-passing doesn't work
1648 * over a socketpair() as used in the debug-pipe transport.
1649 * We test this functionality in a more realistic situation
1650 * in test/dbus-daemon.c. */
1651 warn_unexpected (connection, message, "not this error");
1655 _dbus_verbose ("does not support GetConnectionUnixProcessID but perhaps that's OK?\n");
1662 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1665 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1667 ; /* good, expected */
1671 warn_unexpected (connection, message,
1672 "method_return for GetConnectionUnixProcessID");
1679 if (!dbus_message_get_args (message, &error,
1680 DBUS_TYPE_UINT32, &pid,
1683 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1685 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1686 dbus_error_free (&error);
1687 _dbus_wait_for_memory ();
1688 goto retry_get_property;
1692 _dbus_assert (dbus_error_is_set (&error));
1693 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID");
1699 /* test if returned pid is the same as our own pid
1701 * @todo It would probably be good to restructure the tests
1702 * in a way so our parent is the bus that we're testing
1703 * cause then we can test that the pid returned matches
1706 if (pid != (dbus_uint32_t) _dbus_getpid ())
1708 _dbus_assert (dbus_error_is_set (&error));
1709 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid");
1713 #endif /* !DBUS_WIN */
1716 if (!check_no_leftovers (context))
1722 dbus_error_free (&error);
1725 dbus_message_unref (message);
1730 /* returns TRUE if the correct thing happens,
1731 * but the correct thing may include OOM errors.
1734 check_add_match (BusContext *context,
1735 DBusConnection *connection,
1738 DBusMessage *message;
1740 dbus_uint32_t serial;
1744 dbus_error_init (&error);
1747 _dbus_verbose ("check_add_match for connection %p, rule %s\n",
1750 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1752 DBUS_INTERFACE_DBUS,
1755 if (message == NULL)
1758 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &rule,
1761 dbus_message_unref (message);
1765 if (!dbus_connection_send (connection, message, &serial))
1767 dbus_message_unref (message);
1771 dbus_message_unref (message);
1774 dbus_connection_ref (connection); /* because we may get disconnected */
1776 /* send our message */
1777 bus_test_run_clients_loop (SEND_PENDING (connection));
1779 if (!dbus_connection_get_is_connected (connection))
1781 _dbus_verbose ("connection was disconnected\n");
1783 dbus_connection_unref (connection);
1788 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1790 if (!dbus_connection_get_is_connected (connection))
1792 _dbus_verbose ("connection was disconnected\n");
1794 dbus_connection_unref (connection);
1799 dbus_connection_unref (connection);
1801 message = pop_message_waiting_for_memory (connection);
1802 if (message == NULL)
1804 _dbus_warn ("Did not receive a reply to %s %d on %p",
1805 "AddMatch", serial, connection);
1809 verbose_message_received (connection, message);
1811 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1813 _dbus_warn ("Message has wrong sender %s",
1814 dbus_message_get_sender (message) ?
1815 dbus_message_get_sender (message) : "(none)");
1819 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1821 if (dbus_message_is_error (message,
1822 DBUS_ERROR_NO_MEMORY))
1824 ; /* good, this is a valid response */
1828 warn_unexpected (connection, message, "not this error");
1835 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1837 ; /* good, expected */
1838 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1842 warn_unexpected (connection, message, "method return for AddMatch");
1848 if (!check_no_leftovers (context))
1854 dbus_error_free (&error);
1857 dbus_message_unref (message);
1862 #ifdef DBUS_ENABLE_STATS
1863 /* returns TRUE if the correct thing happens,
1864 * but the correct thing may include OOM errors.
1867 check_get_all_match_rules (BusContext *context,
1868 DBusConnection *connection)
1870 DBusMessage *message;
1872 dbus_uint32_t serial;
1876 dbus_error_init (&error);
1879 _dbus_verbose ("check_get_all_match_rules for connection %p\n",
1882 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1884 BUS_INTERFACE_STATS,
1885 "GetAllMatchRules");
1887 if (message == NULL)
1890 if (!dbus_connection_send (connection, message, &serial))
1892 dbus_message_unref (message);
1896 dbus_message_unref (message);
1899 dbus_connection_ref (connection); /* because we may get disconnected */
1901 /* send our message */
1902 bus_test_run_clients_loop (SEND_PENDING (connection));
1904 if (!dbus_connection_get_is_connected (connection))
1906 _dbus_verbose ("connection was disconnected\n");
1908 dbus_connection_unref (connection);
1913 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1915 if (!dbus_connection_get_is_connected (connection))
1917 _dbus_verbose ("connection was disconnected\n");
1919 dbus_connection_unref (connection);
1924 dbus_connection_unref (connection);
1926 message = pop_message_waiting_for_memory (connection);
1927 if (message == NULL)
1929 _dbus_warn ("Did not receive a reply to %s %d on %p",
1930 "AddMatch", serial, connection);
1934 verbose_message_received (connection, message);
1936 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1938 _dbus_warn ("Message has wrong sender %s",
1939 dbus_message_get_sender (message) ?
1940 dbus_message_get_sender (message) : "(none)");
1944 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1946 if (dbus_message_is_error (message,
1947 DBUS_ERROR_NO_MEMORY))
1949 ; /* good, this is a valid response */
1953 warn_unexpected (connection, message, "not this error");
1960 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1962 ; /* good, expected */
1963 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1967 warn_unexpected (connection, message, "method return for AddMatch");
1973 if (!check_no_leftovers (context))
1979 dbus_error_free (&error);
1982 dbus_message_unref (message);
1988 /* returns TRUE if the correct thing happens,
1989 * but the correct thing may include OOM errors.
1992 check_hello_connection (BusContext *context)
1994 DBusConnection *connection;
1997 dbus_error_init (&error);
1999 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
2000 if (connection == NULL)
2002 _DBUS_ASSERT_ERROR_IS_SET (&error);
2003 dbus_error_free (&error);
2007 if (!bus_setup_debug_client (connection))
2009 dbus_connection_close (connection);
2010 dbus_connection_unref (connection);
2014 spin_connection_until_authenticated (context, connection);
2016 if (!check_hello_message (context, connection))
2019 if (dbus_bus_get_unique_name (connection) == NULL)
2021 /* We didn't successfully register, so we can't
2022 * do the usual kill_client_connection() checks
2024 kill_client_connection_unchecked (connection);
2028 if (!check_add_match (context, connection, ""))
2031 kill_client_connection (context, connection);
2037 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
2039 /* returns TRUE if the correct thing happens,
2040 * but the correct thing may include OOM errors.
2043 check_nonexistent_service_no_auto_start (BusContext *context,
2044 DBusConnection *connection)
2046 DBusMessage *message;
2047 dbus_uint32_t serial;
2049 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
2050 dbus_uint32_t flags;
2052 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2054 DBUS_INTERFACE_DBUS,
2055 "StartServiceByName");
2057 if (message == NULL)
2060 dbus_message_set_auto_start (message, FALSE);
2063 if (!dbus_message_append_args (message,
2064 DBUS_TYPE_STRING, &nonexistent,
2065 DBUS_TYPE_UINT32, &flags,
2068 dbus_message_unref (message);
2072 if (!dbus_connection_send (connection, message, &serial))
2074 dbus_message_unref (message);
2078 dbus_message_unref (message);
2081 bus_test_run_everything (context);
2082 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
2083 bus_test_run_everything (context);
2085 if (!dbus_connection_get_is_connected (connection))
2087 _dbus_verbose ("connection was disconnected\n");
2093 message = pop_message_waiting_for_memory (connection);
2094 if (message == NULL)
2096 _dbus_warn ("Did not receive a reply to %s %d on %p",
2097 "StartServiceByName", serial, connection);
2101 verbose_message_received (connection, message);
2103 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2105 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2107 _dbus_warn ("Message has wrong sender %s",
2108 dbus_message_get_sender (message) ?
2109 dbus_message_get_sender (message) : "(none)");
2113 if (dbus_message_is_error (message,
2114 DBUS_ERROR_NO_MEMORY))
2116 ; /* good, this is a valid response */
2118 else if (dbus_message_is_error (message,
2119 DBUS_ERROR_SERVICE_UNKNOWN))
2121 ; /* good, this is expected also */
2125 warn_unexpected (connection, message, "not this error");
2131 _dbus_warn ("Did not expect to successfully activate %s",
2132 NONEXISTENT_SERVICE_NAME);
2140 dbus_message_unref (message);
2145 /* returns TRUE if the correct thing happens,
2146 * but the correct thing may include OOM errors.
2149 check_nonexistent_service_auto_start (BusContext *context,
2150 DBusConnection *connection)
2152 DBusMessage *message;
2153 dbus_uint32_t serial;
2156 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
2157 "/org/freedesktop/TestSuite",
2158 "org.freedesktop.TestSuite",
2161 if (message == NULL)
2164 if (!dbus_connection_send (connection, message, &serial))
2166 dbus_message_unref (message);
2170 dbus_message_unref (message);
2173 bus_test_run_everything (context);
2174 block_connection_until_message_from_bus (context, connection, "reply to Echo");
2175 bus_test_run_everything (context);
2177 if (!dbus_connection_get_is_connected (connection))
2179 _dbus_verbose ("connection was disconnected\n");
2185 message = pop_message_waiting_for_memory (connection);
2187 if (message == NULL)
2189 _dbus_warn ("Did not receive a reply to %s %d on %p",
2190 "Echo message (auto activation)", serial, connection);
2194 verbose_message_received (connection, message);
2196 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2198 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2200 _dbus_warn ("Message has wrong sender %s",
2201 dbus_message_get_sender (message) ?
2202 dbus_message_get_sender (message) : "(none)");
2206 if (dbus_message_is_error (message,
2207 DBUS_ERROR_NO_MEMORY))
2209 ; /* good, this is a valid response */
2211 else if (dbus_message_is_error (message,
2212 DBUS_ERROR_SERVICE_UNKNOWN))
2214 ; /* good, this is expected also */
2218 warn_unexpected (connection, message, "not this error");
2224 _dbus_warn ("Did not expect to successfully activate %s",
2225 NONEXISTENT_SERVICE_NAME);
2233 dbus_message_unref (message);
2239 check_base_service_activated (BusContext *context,
2240 DBusConnection *connection,
2241 DBusMessage *initial_message,
2242 const char **base_service_p)
2244 DBusMessage *message;
2247 const char *base_service, *base_service_from_bus, *old_owner;
2251 dbus_error_init (&error);
2252 base_service = NULL;
2254 base_service_from_bus = NULL;
2256 message = initial_message;
2257 dbus_message_ref (message);
2259 if (dbus_message_is_signal (message,
2260 DBUS_INTERFACE_DBUS,
2261 "NameOwnerChanged"))
2263 CheckServiceOwnerChangedData socd;
2265 reget_service_name_arg:
2266 base_service = NULL;
2268 base_service_from_bus = NULL;
2270 if (!dbus_message_get_args (message, &error,
2271 DBUS_TYPE_STRING, &base_service,
2272 DBUS_TYPE_STRING, &old_owner,
2273 DBUS_TYPE_STRING, &base_service_from_bus,
2276 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2278 dbus_error_free (&error);
2279 _dbus_wait_for_memory ();
2280 goto reget_service_name_arg;
2284 _dbus_warn ("Message %s doesn't have a service name: %s",
2285 "NameOwnerChanged (creation)",
2291 if (*base_service != ':')
2293 _dbus_warn ("Expected base service activation, got \"%s\" instead",
2298 if (strcmp (base_service, base_service_from_bus) != 0)
2300 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"",
2301 base_service, base_service_from_bus);
2307 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"",
2312 socd.expected_kind = SERVICE_CREATED;
2313 socd.expected_service_name = base_service;
2314 socd.failed = FALSE;
2315 socd.skip_connection = connection;
2316 socd.context = context;
2318 bus_test_clients_foreach (check_service_owner_changed_foreach,
2326 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
2332 *base_service_p = base_service;
2338 dbus_message_unref (message);
2339 dbus_error_free (&error);
2345 check_service_activated (BusContext *context,
2346 DBusConnection *connection,
2347 const char *activated_name,
2348 const char *base_service_name,
2349 DBusMessage *initial_message)
2351 DBusMessage *message;
2354 dbus_uint32_t activation_result;
2358 dbus_error_init (&error);
2360 message = initial_message;
2361 dbus_message_ref (message);
2363 if (dbus_message_is_signal (message,
2364 DBUS_INTERFACE_DBUS,
2365 "NameOwnerChanged"))
2367 CheckServiceOwnerChangedData socd;
2368 const char *service_name, *base_service_from_bus, *old_owner;
2370 reget_service_name_arg:
2371 service_name = NULL;
2373 base_service_from_bus = NULL;
2375 if (!dbus_message_get_args (message, &error,
2376 DBUS_TYPE_STRING, &service_name,
2377 DBUS_TYPE_STRING, &old_owner,
2378 DBUS_TYPE_STRING, &base_service_from_bus,
2381 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2383 dbus_error_free (&error);
2384 _dbus_wait_for_memory ();
2385 goto reget_service_name_arg;
2389 _dbus_warn ("Message %s doesn't have a service name: %s",
2390 "NameOwnerChanged (creation)",
2396 if (strcmp (service_name, activated_name) != 0)
2398 _dbus_warn ("Expected to see service %s created, saw %s instead",
2399 activated_name, service_name);
2403 if (strcmp (base_service_name, base_service_from_bus) != 0)
2405 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead",
2406 base_service_from_bus, base_service_name);
2412 _dbus_warn ("expected a %s, got a %s",
2413 "NameOwnerChanged (creation)",
2414 "NameOwnerChanged (change)");
2418 socd.expected_kind = SERVICE_CREATED;
2419 socd.skip_connection = connection;
2420 socd.failed = FALSE;
2421 socd.expected_service_name = service_name;
2422 socd.context = context;
2424 bus_test_clients_foreach (check_service_owner_changed_foreach,
2430 dbus_message_unref (message);
2431 service_name = NULL;
2433 base_service_from_bus = NULL;
2435 message = pop_message_waiting_for_memory (connection);
2436 if (message == NULL)
2438 _dbus_warn ("Expected a reply to %s, got nothing",
2439 "StartServiceByName");
2445 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2450 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2452 warn_unexpected (connection, message, "reply to StartServiceByName");
2457 activation_result = 0;
2458 if (!dbus_message_get_args (message, &error,
2459 DBUS_TYPE_UINT32, &activation_result,
2462 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2464 _dbus_warn ("Did not have activation result first argument to %s: %s",
2465 "StartServiceByName", error.message);
2469 dbus_error_free (&error);
2473 if (activation_result == DBUS_START_REPLY_SUCCESS)
2475 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2479 _dbus_warn ("Activation result was %u, no good.",
2485 dbus_message_unref (message);
2488 if (!check_no_leftovers (context))
2490 _dbus_warn ("Messages were left over after verifying existent activation results");
2498 dbus_message_unref (message);
2499 dbus_error_free (&error);
2505 check_service_auto_activated (BusContext *context,
2506 DBusConnection *connection,
2507 const char *activated_name,
2508 const char *base_service_name,
2509 DBusMessage *initial_message)
2511 DBusMessage *message;
2517 dbus_error_init (&error);
2519 message = initial_message;
2520 dbus_message_ref (message);
2522 if (dbus_message_is_signal (message,
2523 DBUS_INTERFACE_DBUS,
2524 "NameOwnerChanged"))
2526 const char *service_name;
2527 CheckServiceOwnerChangedData socd;
2529 reget_service_name_arg:
2530 if (!dbus_message_get_args (message, &error,
2531 DBUS_TYPE_STRING, &service_name,
2534 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2536 dbus_error_free (&error);
2537 _dbus_wait_for_memory ();
2538 goto reget_service_name_arg;
2542 _dbus_warn ("Message %s doesn't have a service name: %s",
2545 dbus_error_free (&error);
2550 if (strcmp (service_name, activated_name) != 0)
2552 _dbus_warn ("Expected to see service %s created, saw %s instead",
2553 activated_name, service_name);
2557 socd.expected_kind = SERVICE_CREATED;
2558 socd.expected_service_name = service_name;
2559 socd.failed = FALSE;
2560 socd.skip_connection = connection;
2561 socd.context = context;
2563 bus_test_clients_foreach (check_service_owner_changed_foreach,
2569 /* Note that this differs from regular activation in that we don't get a
2570 * reply to ActivateService here.
2573 dbus_message_unref (message);
2575 service_name = NULL;
2579 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2588 dbus_message_unref (message);
2594 check_service_deactivated (BusContext *context,
2595 DBusConnection *connection,
2596 const char *activated_name,
2597 const char *base_service)
2600 CheckServiceOwnerChangedData socd;
2604 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2605 * service and the activated_name. The base service
2606 * notification is required to come last.
2608 socd.expected_kind = SERVICE_DELETED;
2609 socd.expected_service_name = activated_name;
2610 socd.failed = FALSE;
2611 socd.skip_connection = NULL;
2612 socd.context = context;
2614 bus_test_clients_foreach (check_service_owner_changed_foreach,
2620 socd.expected_kind = SERVICE_DELETED;
2621 socd.expected_service_name = base_service;
2622 socd.failed = FALSE;
2623 socd.skip_connection = NULL;
2624 socd.context = context;
2626 bus_test_clients_foreach (check_service_owner_changed_foreach,
2639 check_send_exit_to_service (BusContext *context,
2640 DBusConnection *connection,
2641 const char *service_name,
2642 const char *base_service)
2644 dbus_bool_t got_error;
2645 DBusMessage *message;
2646 dbus_uint32_t serial;
2649 _dbus_verbose ("Sending exit message to the test service\n");
2653 /* Kill off the test service by sending it a quit message */
2654 message = dbus_message_new_method_call (service_name,
2655 "/org/freedesktop/TestSuite",
2656 "org.freedesktop.TestSuite",
2659 if (message == NULL)
2661 /* Do this again; we still need the service to exit... */
2662 if (!check_send_exit_to_service (context, connection,
2663 service_name, base_service))
2669 if (!dbus_connection_send (connection, message, &serial))
2671 dbus_message_unref (message);
2673 /* Do this again; we still need the service to exit... */
2674 if (!check_send_exit_to_service (context, connection,
2675 service_name, base_service))
2681 dbus_message_unref (message);
2685 bus_test_run_clients_loop (SEND_PENDING (connection));
2687 /* read it in and write it out to test service */
2688 bus_test_run_bus_loop (context, FALSE);
2690 /* see if we got an error during message bus dispatching */
2691 bus_test_run_clients_loop (FALSE);
2692 message = borrow_message_waiting_for_memory (connection);
2693 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2696 dbus_connection_return_message (connection, message);
2702 /* If no error, wait for the test service to exit */
2703 block_connection_until_message_from_bus (context, connection, "test service to exit");
2705 bus_test_run_everything (context);
2710 message = pop_message_waiting_for_memory (connection);
2711 _dbus_assert (message != NULL);
2713 if (dbus_message_get_reply_serial (message) != serial)
2715 warn_unexpected (connection, message,
2716 "error with the correct reply serial");
2720 if (!dbus_message_is_error (message,
2721 DBUS_ERROR_NO_MEMORY))
2723 warn_unexpected (connection, message,
2724 "a no memory error from asking test service to exit");
2728 _dbus_verbose ("Got error %s when asking test service to exit\n",
2729 dbus_message_get_error_name (message));
2731 /* Do this again; we still need the service to exit... */
2732 if (!check_send_exit_to_service (context, connection,
2733 service_name, base_service))
2738 if (!check_service_deactivated (context, connection,
2739 service_name, base_service))
2742 /* Should now have a NoReply error from the Exit() method
2743 * call; it should have come after all the deactivation
2746 message = pop_message_waiting_for_memory (connection);
2748 if (message == NULL)
2750 warn_unexpected (connection, NULL,
2751 "reply to Exit() method call");
2754 if (!dbus_message_is_error (message,
2755 DBUS_ERROR_NO_REPLY))
2757 warn_unexpected (connection, message,
2758 "NoReply error from Exit() method call");
2762 if (dbus_message_get_reply_serial (message) != serial)
2764 warn_unexpected (connection, message,
2765 "error with the correct reply serial");
2769 _dbus_verbose ("Got error %s after test service exited\n",
2770 dbus_message_get_error_name (message));
2772 if (!check_no_leftovers (context))
2774 _dbus_warn ("Messages were left over after %s",
2775 _DBUS_FUNCTION_NAME);
2784 dbus_message_unref (message);
2790 check_got_error (BusContext *context,
2791 DBusConnection *connection,
2792 const char *first_error_name,
2795 DBusMessage *message;
2798 dbus_bool_t error_found;
2799 const char *error_name;
2803 message = pop_message_waiting_for_memory (connection);
2804 if (message == NULL)
2806 _dbus_warn ("Did not get an expected error");
2810 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2812 warn_unexpected (connection, message, "an error");
2817 error_found = FALSE;
2819 va_start (ap, first_error_name);
2820 error_name = first_error_name;
2821 while (error_name != NULL)
2823 if (dbus_message_is_error (message, error_name))
2828 error_name = va_arg (ap, char*);
2834 _dbus_warn ("Expected error %s or other, got %s instead",
2836 dbus_message_get_error_name (message));
2844 dbus_message_unref (message);
2851 GOT_SERVICE_CREATED,
2852 GOT_SERVICE_DELETED,
2857 static GotServiceInfo
2858 check_got_service_info (DBusMessage *message)
2860 GotServiceInfo message_kind;
2862 if (dbus_message_is_signal (message,
2863 DBUS_INTERFACE_DBUS,
2864 "NameOwnerChanged"))
2867 const char *service_name, *old_owner, *new_owner;
2868 dbus_error_init (&error);
2870 reget_service_info_data:
2871 service_name = NULL;
2875 dbus_message_get_args (message, &error,
2876 DBUS_TYPE_STRING, &service_name,
2877 DBUS_TYPE_STRING, &old_owner,
2878 DBUS_TYPE_STRING, &new_owner,
2880 if (dbus_error_is_set (&error))
2882 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2884 dbus_error_free (&error);
2885 goto reget_service_info_data;
2889 _dbus_warn ("unexpected arguments for NameOwnerChanged message");
2890 message_kind = GOT_SOMETHING_ELSE;
2893 else if (!old_owner[0])
2894 message_kind = GOT_SERVICE_CREATED;
2895 else if (!new_owner[0])
2896 message_kind = GOT_SERVICE_DELETED;
2898 message_kind = GOT_SOMETHING_ELSE;
2900 dbus_error_free (&error);
2902 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2903 message_kind = GOT_ERROR;
2905 message_kind = GOT_SOMETHING_ELSE;
2907 return message_kind;
2910 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2912 /* returns TRUE if the correct thing happens,
2913 * but the correct thing may include OOM errors.
2916 check_existent_service_no_auto_start (BusContext *context,
2917 DBusConnection *connection)
2919 DBusMessage *message;
2920 DBusMessage *base_service_message;
2921 const char *base_service;
2922 dbus_uint32_t serial;
2924 const char *existent = EXISTENT_SERVICE_NAME;
2925 dbus_uint32_t flags;
2927 base_service_message = NULL;
2929 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2931 DBUS_INTERFACE_DBUS,
2932 "StartServiceByName");
2934 if (message == NULL)
2937 dbus_message_set_auto_start (message, FALSE);
2940 if (!dbus_message_append_args (message,
2941 DBUS_TYPE_STRING, &existent,
2942 DBUS_TYPE_UINT32, &flags,
2945 dbus_message_unref (message);
2949 if (!dbus_connection_send (connection, message, &serial))
2951 dbus_message_unref (message);
2955 dbus_message_unref (message);
2958 bus_test_run_everything (context);
2960 /* now wait for the message bus to hear back from the activated
2963 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2965 bus_test_run_everything (context);
2967 if (!dbus_connection_get_is_connected (connection))
2969 _dbus_verbose ("connection was disconnected\n");
2975 message = pop_message_waiting_for_memory (connection);
2976 if (message == NULL)
2978 _dbus_warn ("Did not receive any messages after %s %d on %p",
2979 "StartServiceByName", serial, connection);
2983 verbose_message_received (connection, message);
2984 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2986 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2988 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2990 _dbus_warn ("Message has wrong sender %s",
2991 dbus_message_get_sender (message) ?
2992 dbus_message_get_sender (message) : "(none)");
2996 if (dbus_message_is_error (message,
2997 DBUS_ERROR_NO_MEMORY))
2999 ; /* good, this is a valid response */
3001 else if (dbus_message_is_error (message,
3002 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
3003 dbus_message_is_error (message,
3004 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
3005 dbus_message_is_error (message,
3006 DBUS_ERROR_SPAWN_EXEC_FAILED))
3008 ; /* good, this is expected also */
3012 _dbus_warn ("Did not expect error %s",
3013 dbus_message_get_error_name (message));
3019 GotServiceInfo message_kind;
3021 if (!check_base_service_activated (context, connection,
3022 message, &base_service))
3025 base_service_message = message;
3028 /* We may need to block here for the test service to exit or finish up */
3029 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
3031 message = dbus_connection_borrow_message (connection);
3032 if (message == NULL)
3034 _dbus_warn ("Did not receive any messages after base service creation notification");
3038 message_kind = check_got_service_info (message);
3040 dbus_connection_return_message (connection, message);
3043 switch (message_kind)
3045 case GOT_SOMETHING_ELSE:
3047 _dbus_warn ("Unexpected message after ActivateService "
3048 "(should be an error or a service announcement");
3052 if (!check_got_error (context, connection,
3053 DBUS_ERROR_SPAWN_CHILD_EXITED,
3054 DBUS_ERROR_NO_MEMORY,
3057 /* A service deleted should be coming along now after this error.
3058 * We can also get the error *after* the service deleted.
3063 case GOT_SERVICE_DELETED:
3065 /* The service started up and got a base address, but then
3066 * failed to register under EXISTENT_SERVICE_NAME
3068 CheckServiceOwnerChangedData socd;
3070 socd.expected_kind = SERVICE_DELETED;
3071 socd.expected_service_name = base_service;
3072 socd.failed = FALSE;
3073 socd.skip_connection = NULL;
3074 socd.context = context;
3076 bus_test_clients_foreach (check_service_owner_changed_foreach,
3082 /* Now we should get an error about the service exiting
3083 * if we didn't get it before.
3085 if (message_kind != GOT_ERROR)
3087 block_connection_until_message_from_bus (context, connection, "error about service exiting");
3089 /* and process everything again */
3090 bus_test_run_everything (context);
3092 if (!check_got_error (context, connection,
3093 DBUS_ERROR_SPAWN_CHILD_EXITED,
3094 DBUS_ERROR_NO_MEMORY,
3101 case GOT_SERVICE_CREATED:
3102 message = pop_message_waiting_for_memory (connection);
3103 if (message == NULL)
3105 _dbus_warn ("Failed to pop message we just put back! "
3106 "should have been a NameOwnerChanged (creation)");
3110 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
3111 base_service, message))
3114 dbus_message_unref (message);
3117 if (!check_no_leftovers (context))
3119 _dbus_warn ("Messages were left over after successful activation");
3123 if (!check_send_exit_to_service (context, connection,
3124 EXISTENT_SERVICE_NAME, base_service))
3135 dbus_message_unref (message);
3137 if (base_service_message)
3138 dbus_message_unref (base_service_message);
3143 /* returns TRUE if the correct thing happens,
3144 * but the correct thing may include OOM errors.
3147 check_segfault_service_no_auto_start (BusContext *context,
3148 DBusConnection *connection)
3150 DBusMessage *message;
3151 dbus_uint32_t serial;
3153 const char *segv_service;
3154 dbus_uint32_t flags;
3156 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
3158 DBUS_INTERFACE_DBUS,
3159 "StartServiceByName");
3161 if (message == NULL)
3164 dbus_message_set_auto_start (message, FALSE);
3166 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
3168 if (!dbus_message_append_args (message,
3169 DBUS_TYPE_STRING, &segv_service,
3170 DBUS_TYPE_UINT32, &flags,
3173 dbus_message_unref (message);
3177 if (!dbus_connection_send (connection, message, &serial))
3179 dbus_message_unref (message);
3183 dbus_message_unref (message);
3186 bus_test_run_everything (context);
3187 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
3188 bus_test_run_everything (context);
3190 if (!dbus_connection_get_is_connected (connection))
3192 _dbus_verbose ("connection was disconnected\n");
3198 message = pop_message_waiting_for_memory (connection);
3199 if (message == NULL)
3201 _dbus_warn ("Did not receive a reply to %s %d on %p",
3202 "StartServiceByName", serial, connection);
3206 verbose_message_received (connection, message);
3208 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3210 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3212 _dbus_warn ("Message has wrong sender %s",
3213 dbus_message_get_sender (message) ?
3214 dbus_message_get_sender (message) : "(none)");
3218 if (dbus_message_is_error (message,
3219 DBUS_ERROR_NO_MEMORY))
3221 ; /* good, this is a valid response */
3223 else if (dbus_message_is_error (message,
3226 const char *servicehelper;
3227 servicehelper = bus_context_get_servicehelper (context);
3228 /* make sure this only happens with the launch helper */
3229 _dbus_assert (servicehelper != NULL);
3232 else if (dbus_message_is_error (message,
3233 DBUS_ERROR_SPAWN_CHILD_EXITED))
3235 /* unhandled exceptions are normal exit codes */
3238 else if (dbus_message_is_error (message,
3239 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3241 ; /* good, this is expected also */
3246 warn_unexpected (connection, message, "not this error");
3253 _dbus_warn ("Did not expect to successfully activate segfault service");
3261 dbus_message_unref (message);
3267 /* returns TRUE if the correct thing happens,
3268 * but the correct thing may include OOM errors.
3271 check_segfault_service_auto_start (BusContext *context,
3272 DBusConnection *connection)
3274 DBusMessage *message;
3275 dbus_uint32_t serial;
3278 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
3279 "/org/freedesktop/TestSuite",
3280 "org.freedesktop.TestSuite",
3283 if (message == NULL)
3286 if (!dbus_connection_send (connection, message, &serial))
3288 dbus_message_unref (message);
3292 dbus_message_unref (message);
3295 bus_test_run_everything (context);
3296 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
3297 bus_test_run_everything (context);
3299 if (!dbus_connection_get_is_connected (connection))
3301 _dbus_verbose ("connection was disconnected\n");
3307 message = pop_message_waiting_for_memory (connection);
3308 if (message == NULL)
3310 _dbus_warn ("Did not receive a reply to %s %d on %p",
3311 "Echo message (auto activation)", serial, connection);
3315 verbose_message_received (connection, message);
3317 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3319 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3321 _dbus_warn ("Message has wrong sender %s",
3322 dbus_message_get_sender (message) ?
3323 dbus_message_get_sender (message) : "(none)");
3327 if (dbus_message_is_error (message,
3328 DBUS_ERROR_NO_MEMORY))
3330 ; /* good, this is a valid response */
3333 else if (dbus_message_is_error (message,
3334 DBUS_ERROR_SPAWN_CHILD_EXITED))
3336 /* unhandled exceptions are normal exit codes */
3339 else if (dbus_message_is_error (message,
3340 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3342 ; /* good, this is expected also */
3347 warn_unexpected (connection, message, "not this error");
3354 _dbus_warn ("Did not expect to successfully activate segfault service");
3362 dbus_message_unref (message);
3367 #define TEST_ECHO_MESSAGE "Test echo message"
3368 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
3370 /* returns TRUE if the correct thing happens,
3371 * but the correct thing may include OOM errors.
3374 check_existent_hello_from_self (BusContext *context,
3375 DBusConnection *connection)
3377 DBusMessage *message;
3378 dbus_uint32_t serial;
3381 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3382 "/org/freedesktop/TestSuite",
3383 "org.freedesktop.TestSuite",
3384 "RunHelloFromSelf");
3386 if (message == NULL)
3389 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
3390 if (!dbus_message_append_args (message,
3391 DBUS_TYPE_STRING, &text,
3394 dbus_message_unref (message);
3398 if (!dbus_connection_send (connection, message, &serial))
3400 dbus_message_unref (message);
3404 dbus_message_unref (message);
3407 bus_test_run_everything (context);
3409 /* Note: if this test is run in OOM mode, it will block when the bus
3410 * doesn't send a reply due to OOM.
3412 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
3414 message = pop_message_waiting_for_memory (connection);
3415 if (message == NULL)
3417 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message");
3421 if (dbus_message_get_reply_serial (message) != serial)
3423 _dbus_warn ("Wrong reply serial");
3424 dbus_message_unref (message);
3428 dbus_message_unref (message);
3434 /* returns TRUE if the correct thing happens,
3435 * but the correct thing may include OOM errors.
3438 check_existent_ping (BusContext *context,
3439 DBusConnection *connection)
3441 DBusMessage *message;
3442 dbus_uint32_t serial;
3443 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3444 "/org/freedesktop/TestSuite",
3445 "org.freedesktop.DBus.Peer",
3448 if (message == NULL)
3451 if (!dbus_connection_send (connection, message, &serial))
3453 dbus_message_unref (message);
3457 dbus_message_unref (message);
3460 bus_test_run_everything (context);
3462 /* Note: if this test is run in OOM mode, it will block when the bus
3463 * doesn't send a reply due to OOM.
3465 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
3467 message = pop_message_waiting_for_memory (connection);
3468 if (message == NULL)
3470 _dbus_warn ("Failed to pop message! Should have been reply from Ping message");
3474 if (dbus_message_get_reply_serial (message) != serial)
3476 _dbus_warn ("Wrong reply serial");
3477 dbus_message_unref (message);
3481 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3483 _dbus_warn ("Unexpected message return during Ping");
3484 dbus_message_unref (message);
3488 dbus_message_unref (message);
3494 /* returns TRUE if the correct thing happens,
3495 * but the correct thing may include OOM errors.
3498 check_existent_get_machine_id (BusContext *context,
3499 DBusConnection *connection)
3501 DBusError error = DBUS_ERROR_INIT;
3502 DBusMessage *message;
3503 dbus_uint32_t serial;
3505 const char *machine_id;
3507 if (!_dbus_read_local_machine_uuid (&uuid, FALSE, &error))
3509 /* Unable to test further: either we ran out of memory, or neither
3510 * dbus nor systemd was ever correctly installed on this machine */
3511 _dbus_verbose ("Machine UUID not available: %s", error.message);
3512 dbus_error_free (&error);
3516 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3517 "/org/freedesktop/TestSuite",
3518 "org.freedesktop.DBus.Peer",
3521 if (message == NULL)
3524 if (!dbus_connection_send (connection, message, &serial))
3526 dbus_message_unref (message);
3530 dbus_message_unref (message);
3533 bus_test_run_everything (context);
3535 /* Note: if this test is run in OOM mode, it will block when the bus
3536 * doesn't send a reply due to OOM.
3538 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3540 message = pop_message_waiting_for_memory (connection);
3541 if (message == NULL)
3543 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message");
3547 if (dbus_message_get_reply_serial (message) != serial)
3549 _dbus_warn ("Wrong reply serial");
3550 dbus_message_unref (message);
3554 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3556 _dbus_warn ("Unexpected message return during GetMachineId");
3557 dbus_message_unref (message);
3562 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3564 _dbus_warn ("Did not get a machine ID in reply to GetMachineId");
3565 dbus_message_unref (message);
3569 if (machine_id == NULL || strlen (machine_id) != 32)
3571 _dbus_warn ("Machine id looks bogus: '%s'", machine_id ? machine_id : "null");
3572 dbus_message_unref (message);
3576 /* We can't check that the machine id is correct because during make check it is
3577 * just made up for each process separately
3580 dbus_message_unref (message);
3586 /* returns TRUE if the correct thing happens,
3587 * but the correct thing may include OOM errors.
3590 check_existent_service_auto_start (BusContext *context,
3591 DBusConnection *connection)
3593 DBusMessage *message;
3594 DBusMessage *base_service_message;
3595 dbus_uint32_t serial;
3597 const char *base_service;
3600 base_service_message = NULL;
3602 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3603 "/org/freedesktop/TestSuite",
3604 "org.freedesktop.TestSuite",
3607 if (message == NULL)
3610 text = TEST_ECHO_MESSAGE;
3611 if (!dbus_message_append_args (message,
3612 DBUS_TYPE_STRING, &text,
3615 dbus_message_unref (message);
3619 if (!dbus_connection_send (connection, message, &serial))
3621 dbus_message_unref (message);
3625 dbus_message_unref (message);
3628 bus_test_run_everything (context);
3630 /* now wait for the message bus to hear back from the activated
3633 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3634 bus_test_run_everything (context);
3636 if (!dbus_connection_get_is_connected (connection))
3638 _dbus_verbose ("connection was disconnected\n");
3644 message = pop_message_waiting_for_memory (connection);
3645 if (message == NULL)
3647 _dbus_warn ("Did not receive any messages after auto start %d on %p",
3648 serial, connection);
3652 verbose_message_received (connection, message);
3653 _dbus_verbose (" (after sending %s)\n", "auto start");
3655 /* we should get zero or two ServiceOwnerChanged signals */
3656 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3658 GotServiceInfo message_kind;
3660 if (!check_base_service_activated (context, connection,
3661 message, &base_service))
3664 base_service_message = message;
3667 /* We may need to block here for the test service to exit or finish up */
3668 block_connection_until_message_from_bus (context, connection, "service to exit");
3670 /* Should get a service creation notification for the activated
3671 * service name, or a service deletion on the base service name
3673 message = dbus_connection_borrow_message (connection);
3674 if (message == NULL)
3676 _dbus_warn ("No message after auto activation "
3677 "(should be a service announcement)");
3678 dbus_connection_return_message (connection, message);
3683 message_kind = check_got_service_info (message);
3685 dbus_connection_return_message (connection, message);
3688 switch (message_kind)
3690 case GOT_SERVICE_CREATED:
3691 message = pop_message_waiting_for_memory (connection);
3692 if (message == NULL)
3694 _dbus_warn ("Failed to pop message we just put back! "
3695 "should have been a NameOwnerChanged (creation)");
3699 /* Check that ServiceOwnerChanged (creation) was correctly received */
3700 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3701 base_service, message))
3704 dbus_message_unref (message);
3709 case GOT_SERVICE_DELETED:
3711 /* The service started up and got a base address, but then
3712 * failed to register under EXISTENT_SERVICE_NAME
3714 CheckServiceOwnerChangedData socd;
3716 socd.expected_kind = SERVICE_DELETED;
3717 socd.expected_service_name = base_service;
3718 socd.failed = FALSE;
3719 socd.skip_connection = NULL;
3720 socd.context = context;
3722 bus_test_clients_foreach (check_service_owner_changed_foreach,
3732 case GOT_SOMETHING_ELSE:
3734 _dbus_warn ("Unexpected message after auto activation");
3739 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3740 * come the method reply (or error) from the initial method call
3743 /* Note: if this test is run in OOM mode, it will block when the bus
3744 * doesn't send a reply due to OOM.
3746 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3748 message = pop_message_waiting_for_memory (connection);
3749 if (message == NULL)
3751 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
3755 if (dbus_message_get_reply_serial (message) != serial)
3757 _dbus_warn ("Wrong reply serial");
3761 dbus_message_unref (message);
3764 if (!check_existent_ping (context, connection))
3767 if (!check_existent_get_machine_id (context, connection))
3770 if (!check_existent_hello_from_self (context, connection))
3773 if (!check_send_exit_to_service (context, connection,
3774 EXISTENT_SERVICE_NAME,
3782 dbus_message_unref (message);
3784 if (base_service_message)
3785 dbus_message_unref (base_service_message);
3790 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3792 /* returns TRUE if the correct thing happens,
3793 * but the correct thing may include OOM errors.
3796 check_launch_service_file_missing (BusContext *context,
3797 DBusConnection *connection)
3799 DBusMessage *message;
3800 dbus_uint32_t serial;
3803 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3804 "/org/freedesktop/TestSuite",
3805 "org.freedesktop.TestSuite",
3808 if (message == NULL)
3811 if (!dbus_connection_send (connection, message, &serial))
3813 dbus_message_unref (message);
3817 dbus_message_unref (message);
3820 bus_test_run_everything (context);
3821 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3822 bus_test_run_everything (context);
3824 if (!dbus_connection_get_is_connected (connection))
3826 _dbus_verbose ("connection was disconnected\n");
3832 message = pop_message_waiting_for_memory (connection);
3833 if (message == NULL)
3835 _dbus_warn ("Did not receive a reply to %s %d on %p",
3836 "Echo message (auto activation)", serial, connection);
3840 verbose_message_received (connection, message);
3842 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3844 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3846 _dbus_warn ("Message has wrong sender %s",
3847 dbus_message_get_sender (message) ?
3848 dbus_message_get_sender (message) : "(none)");
3852 if (dbus_message_is_error (message,
3853 DBUS_ERROR_NO_MEMORY))
3855 ; /* good, this is a valid response */
3857 else if (dbus_message_is_error (message,
3858 DBUS_ERROR_SERVICE_UNKNOWN))
3860 _dbus_verbose("got service unknown\n");
3861 ; /* good, this is expected (only valid when using launch helper) */
3865 warn_unexpected (connection, message, "not this error");
3872 _dbus_warn ("Did not expect to successfully auto-start missing service");
3880 dbus_message_unref (message);
3887 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3889 /* returns TRUE if the correct thing happens,
3890 * but the correct thing may include OOM errors.
3893 check_launch_service_user_missing (BusContext *context,
3894 DBusConnection *connection)
3896 DBusMessage *message;
3897 dbus_uint32_t serial;
3900 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3901 "/org/freedesktop/TestSuite",
3902 "org.freedesktop.TestSuite",
3905 if (message == NULL)
3908 if (!dbus_connection_send (connection, message, &serial))
3910 dbus_message_unref (message);
3914 dbus_message_unref (message);
3917 bus_test_run_everything (context);
3918 block_connection_until_message_from_bus (context, connection,
3919 "reply to service which should fail to auto-start (missing User)");
3920 bus_test_run_everything (context);
3922 if (!dbus_connection_get_is_connected (connection))
3924 _dbus_warn ("connection was disconnected");
3930 message = pop_message_waiting_for_memory (connection);
3931 if (message == NULL)
3933 _dbus_warn ("Did not receive a reply to %s %d on %p",
3934 "Echo message (auto activation)", serial, connection);
3938 verbose_message_received (connection, message);
3940 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3942 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3944 _dbus_warn ("Message has wrong sender %s",
3945 dbus_message_get_sender (message) ?
3946 dbus_message_get_sender (message) : "(none)");
3950 if (dbus_message_is_error (message,
3951 DBUS_ERROR_NO_MEMORY))
3953 ; /* good, this is a valid response */
3955 else if (dbus_message_is_error (message,
3956 DBUS_ERROR_SPAWN_FILE_INVALID))
3958 _dbus_verbose("got service file invalid\n");
3959 ; /* good, this is expected (only valid when using launch helper) */
3963 warn_unexpected (connection, message, "not this error");
3970 _dbus_warn ("Did not expect to successfully auto-start missing service");
3978 dbus_message_unref (message);
3983 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3985 /* returns TRUE if the correct thing happens,
3986 * but the correct thing may include OOM errors.
3989 check_launch_service_exec_missing (BusContext *context,
3990 DBusConnection *connection)
3992 DBusMessage *message;
3993 dbus_uint32_t serial;
3996 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3997 "/org/freedesktop/TestSuite",
3998 "org.freedesktop.TestSuite",
4001 if (message == NULL)
4004 if (!dbus_connection_send (connection, message, &serial))
4006 dbus_message_unref (message);
4010 dbus_message_unref (message);
4013 bus_test_run_everything (context);
4014 block_connection_until_message_from_bus (context, connection,
4015 "reply to service which should fail to auto-start (missing Exec)");
4016 bus_test_run_everything (context);
4018 if (!dbus_connection_get_is_connected (connection))
4020 _dbus_warn ("connection was disconnected");
4026 message = pop_message_waiting_for_memory (connection);
4027 if (message == NULL)
4029 _dbus_warn ("Did not receive a reply to %s %d on %p",
4030 "Echo message (auto activation)", serial, connection);
4034 verbose_message_received (connection, message);
4036 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4038 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4040 _dbus_warn ("Message has wrong sender %s",
4041 dbus_message_get_sender (message) ?
4042 dbus_message_get_sender (message) : "(none)");
4046 if (dbus_message_is_error (message,
4047 DBUS_ERROR_NO_MEMORY))
4049 ; /* good, this is a valid response */
4051 else if (dbus_message_is_error (message,
4052 DBUS_ERROR_SERVICE_UNKNOWN))
4054 _dbus_verbose("could not activate as invalid service file was not added\n");
4055 ; /* good, this is expected as we shouldn't have been added to
4056 * the activation list with a missing Exec key */
4058 else if (dbus_message_is_error (message,
4059 DBUS_ERROR_SPAWN_FILE_INVALID))
4061 _dbus_verbose("got service file invalid\n");
4062 ; /* good, this is allowed, and is the message passed back from the
4067 warn_unexpected (connection, message, "not this error");
4074 _dbus_warn ("Did not expect to successfully auto-start missing service");
4082 dbus_message_unref (message);
4087 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
4089 /* returns TRUE if the correct thing happens,
4090 * but the correct thing may include OOM errors.
4093 check_launch_service_service_missing (BusContext *context,
4094 DBusConnection *connection)
4096 DBusMessage *message;
4097 dbus_uint32_t serial;
4100 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
4101 "/org/freedesktop/TestSuite",
4102 "org.freedesktop.TestSuite",
4105 if (message == NULL)
4108 if (!dbus_connection_send (connection, message, &serial))
4110 dbus_message_unref (message);
4114 dbus_message_unref (message);
4117 bus_test_run_everything (context);
4118 block_connection_until_message_from_bus (context, connection,
4119 "reply to service which should fail to auto-start (missing Service)");
4120 bus_test_run_everything (context);
4122 if (!dbus_connection_get_is_connected (connection))
4124 _dbus_warn ("connection was disconnected");
4130 message = pop_message_waiting_for_memory (connection);
4131 if (message == NULL)
4133 _dbus_warn ("Did not receive a reply to %s %d on %p",
4134 "Echo message (auto activation)", serial, connection);
4138 verbose_message_received (connection, message);
4140 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4142 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4144 _dbus_warn ("Message has wrong sender %s",
4145 dbus_message_get_sender (message) ?
4146 dbus_message_get_sender (message) : "(none)");
4150 if (dbus_message_is_error (message,
4151 DBUS_ERROR_NO_MEMORY))
4153 ; /* good, this is a valid response */
4155 else if (dbus_message_is_error (message,
4156 DBUS_ERROR_SERVICE_UNKNOWN))
4158 _dbus_verbose("could not activate as invalid service file was not added\n");
4159 ; /* good, this is expected as we shouldn't have been added to
4160 * the activation list with a missing Exec key */
4162 else if (dbus_message_is_error (message,
4163 DBUS_ERROR_SPAWN_FILE_INVALID))
4165 _dbus_verbose("got service file invalid\n");
4166 ; /* good, this is allowed, and is the message passed back from the
4171 warn_unexpected (connection, message, "not this error");
4178 _dbus_warn ("Did not expect to successfully auto-start missing service");
4186 dbus_message_unref (message);
4192 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
4194 /* returns TRUE if the correct thing happens,
4195 * but the correct thing may include OOM errors.
4198 check_shell_fail_service_auto_start (BusContext *context,
4199 DBusConnection *connection)
4201 DBusMessage *message;
4202 dbus_uint32_t serial;
4205 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
4206 "/org/freedesktop/TestSuite",
4207 "org.freedesktop.TestSuite",
4210 if (message == NULL)
4213 if (!dbus_connection_send (connection, message, &serial))
4215 dbus_message_unref (message);
4219 dbus_message_unref (message);
4222 bus_test_run_everything (context);
4223 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
4224 bus_test_run_everything (context);
4226 if (!dbus_connection_get_is_connected (connection))
4228 _dbus_verbose ("connection was disconnected\n");
4234 message = pop_message_waiting_for_memory (connection);
4235 if (message == NULL)
4237 _dbus_warn ("Did not receive a reply to %s %d on %p",
4238 "Echo message (auto activation)", serial, connection);
4242 verbose_message_received (connection, message);
4244 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4246 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4248 _dbus_warn ("Message has wrong sender %s",
4249 dbus_message_get_sender (message) ?
4250 dbus_message_get_sender (message) : "(none)");
4254 if (dbus_message_is_error (message,
4255 DBUS_ERROR_NO_MEMORY))
4257 ; /* good, this is a valid response */
4259 else if (dbus_message_is_error (message,
4260 DBUS_ERROR_INVALID_ARGS))
4262 _dbus_verbose("got invalid args\n");
4263 ; /* good, this is expected also */
4267 warn_unexpected (connection, message, "not this error");
4274 _dbus_warn ("Did not expect to successfully auto-start shell fail service");
4282 dbus_message_unref (message);
4287 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
4289 /* returns TRUE if the correct thing happens,
4290 * but the correct thing may include OOM errors.
4293 check_shell_service_success_auto_start (BusContext *context,
4294 DBusConnection *connection)
4296 DBusMessage *message;
4297 DBusMessage *base_service_message;
4298 dbus_uint32_t serial;
4300 const char *base_service;
4301 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4303 base_service_message = NULL;
4305 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
4306 "/org/freedesktop/TestSuite",
4307 "org.freedesktop.TestSuite",
4310 if (message == NULL)
4313 if (!dbus_connection_send (connection, message, &serial))
4315 dbus_message_unref (message);
4319 dbus_message_unref (message);
4322 bus_test_run_everything (context);
4324 /* now wait for the message bus to hear back from the activated
4327 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
4328 bus_test_run_everything (context);
4330 if (!dbus_connection_get_is_connected (connection))
4332 _dbus_verbose ("connection was disconnected\n");
4338 message = pop_message_waiting_for_memory (connection);
4339 if (message == NULL)
4341 _dbus_warn ("Did not receive any messages after auto start %d on %p",
4342 serial, connection);
4346 verbose_message_received (connection, message);
4347 _dbus_verbose (" (after sending %s)\n", "auto start");
4349 /* we should get zero or two ServiceOwnerChanged signals */
4350 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
4352 GotServiceInfo message_kind;
4354 if (!check_base_service_activated (context, connection,
4355 message, &base_service))
4358 base_service_message = message;
4361 /* We may need to block here for the test service to exit or finish up */
4362 block_connection_until_message_from_bus (context, connection, "service to exit");
4364 /* Should get a service creation notification for the activated
4365 * service name, or a service deletion on the base service name
4367 message = dbus_connection_borrow_message (connection);
4368 if (message == NULL)
4370 _dbus_warn ("No message after auto activation "
4371 "(should be a service announcement)");
4372 dbus_connection_return_message (connection, message);
4377 message_kind = check_got_service_info (message);
4379 dbus_connection_return_message (connection, message);
4382 switch (message_kind)
4384 case GOT_SERVICE_CREATED:
4385 message = pop_message_waiting_for_memory (connection);
4386 if (message == NULL)
4388 _dbus_warn ("Failed to pop message we just put back! "
4389 "should have been a NameOwnerChanged (creation)");
4393 /* Check that ServiceOwnerChanged (creation) was correctly received */
4394 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
4395 base_service, message))
4398 dbus_message_unref (message);
4403 case GOT_SERVICE_DELETED:
4405 /* The service started up and got a base address, but then
4406 * failed to register under SHELL_SUCCESS_SERVICE_NAME
4408 CheckServiceOwnerChangedData socd;
4410 socd.expected_kind = SERVICE_DELETED;
4411 socd.expected_service_name = base_service;
4412 socd.failed = FALSE;
4413 socd.skip_connection = NULL;
4414 socd.context = context;
4416 bus_test_clients_foreach (check_service_owner_changed_foreach,
4426 case GOT_SOMETHING_ELSE:
4428 _dbus_warn ("Unexpected message after auto activation");
4433 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
4434 * come the method reply (or error) from the initial method call
4437 /* Note: if this test is run in OOM mode, it will block when the bus
4438 * doesn't send a reply due to OOM.
4440 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
4442 message = pop_message_waiting_for_memory (connection);
4443 if (message == NULL)
4445 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
4449 if (dbus_message_get_reply_serial (message) != serial)
4451 _dbus_warn ("Wrong reply serial");
4455 if (!dbus_message_get_args (message, NULL,
4456 DBUS_TYPE_STRING, &argv[0],
4457 DBUS_TYPE_STRING, &argv[1],
4458 DBUS_TYPE_STRING, &argv[2],
4459 DBUS_TYPE_STRING, &argv[3],
4460 DBUS_TYPE_STRING, &argv[4],
4461 DBUS_TYPE_STRING, &argv[5],
4462 DBUS_TYPE_STRING, &argv[6],
4465 _dbus_warn ("Error getting arguments from return");
4469 /* don't worry about arg[0] as it may be different
4470 depending on the path to the tests
4472 if (strcmp("-test", argv[1]) != 0)
4474 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)",
4479 if (strcmp("that", argv[2]) != 0)
4481 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)",
4486 if (strcmp("we get", argv[3]) != 0)
4488 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)",
4493 if (strcmp("back", argv[4]) != 0)
4495 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)",
4500 if (strcmp("--what", argv[5]) != 0)
4502 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)",
4507 if (strcmp("we put in", argv[6]) != 0)
4509 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)",
4510 "we put in", argv[6]);
4514 dbus_message_unref (message);
4517 if (!check_send_exit_to_service (context, connection,
4518 SHELL_SUCCESS_SERVICE_NAME,
4526 dbus_message_unref (message);
4528 if (base_service_message)
4529 dbus_message_unref (base_service_message);
4537 BusContext *context;
4541 check_oom_check1_func (void *data)
4543 Check1Data *d = data;
4545 if (! (* d->func) (d->context))
4548 if (!check_no_leftovers (d->context))
4550 _dbus_warn ("Messages were left over, should be covered by test suite");
4558 check1_try_iterations (BusContext *context,
4559 const char *description,
4565 d.context = context;
4567 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4569 _dbus_assert_not_reached ("test failed");
4573 check_get_services (BusContext *context,
4574 DBusConnection *connection,
4579 DBusMessage *message;
4580 dbus_uint32_t serial;
4587 dbus_error_init (&error);
4590 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4592 DBUS_INTERFACE_DBUS,
4595 if (message == NULL)
4598 if (!dbus_connection_send (connection, message, &serial))
4600 dbus_message_unref (message);
4604 /* send our message */
4605 bus_test_run_clients_loop (SEND_PENDING (connection));
4607 dbus_message_unref (message);
4610 dbus_connection_ref (connection); /* because we may get disconnected */
4611 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4613 if (!dbus_connection_get_is_connected (connection))
4615 _dbus_verbose ("connection was disconnected\n");
4617 dbus_connection_unref (connection);
4622 dbus_connection_unref (connection);
4624 message = pop_message_waiting_for_memory (connection);
4625 if (message == NULL)
4627 _dbus_warn ("Did not receive a reply to %s %d on %p",
4628 method, serial, connection);
4632 verbose_message_received (connection, message);
4634 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4636 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4638 ; /* good, this is a valid response */
4642 warn_unexpected (connection, message, "not this error");
4649 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4651 ; /* good, expected */
4655 warn_unexpected (connection, message,
4656 "method_return for ListActivatableNames/ListNames");
4663 if (!dbus_message_get_args (message, &error,
4669 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4671 _dbus_verbose ("no memory to list services by %s\n", method);
4672 dbus_error_free (&error);
4673 _dbus_wait_for_memory ();
4674 goto retry_get_property;
4678 _dbus_assert (dbus_error_is_set (&error));
4679 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s", method);
4688 if (!check_no_leftovers (context))
4694 dbus_error_free (&error);
4697 dbus_message_unref (message);
4702 /* returns TRUE if the correct thing happens,
4703 * but the correct thing may include OOM errors.
4706 check_list_services (BusContext *context,
4707 DBusConnection *connection)
4709 DBusMessage *message;
4710 DBusMessage *base_service_message;
4711 const char *base_service;
4712 dbus_uint32_t serial;
4714 const char *existent = EXISTENT_SERVICE_NAME;
4715 dbus_uint32_t flags;
4719 _dbus_verbose ("check_list_services for %p\n", connection);
4721 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4726 if (!_dbus_string_array_contains ((const char **)services, existent))
4728 _dbus_warn ("Did not get the expected %s from ListActivatableNames", existent);
4729 dbus_free_string_array (services);
4733 dbus_free_string_array (services);
4735 base_service_message = NULL;
4737 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4739 DBUS_INTERFACE_DBUS,
4740 "StartServiceByName");
4742 if (message == NULL)
4745 dbus_message_set_auto_start (message, FALSE);
4748 if (!dbus_message_append_args (message,
4749 DBUS_TYPE_STRING, &existent,
4750 DBUS_TYPE_UINT32, &flags,
4753 dbus_message_unref (message);
4757 if (!dbus_connection_send (connection, message, &serial))
4759 dbus_message_unref (message);
4763 dbus_message_unref (message);
4766 bus_test_run_everything (context);
4768 /* now wait for the message bus to hear back from the activated
4771 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4773 bus_test_run_everything (context);
4775 if (!dbus_connection_get_is_connected (connection))
4777 _dbus_verbose ("connection was disconnected\n");
4783 message = pop_message_waiting_for_memory (connection);
4784 if (message == NULL)
4786 _dbus_warn ("Did not receive any messages after %s %d on %p",
4787 "StartServiceByName", serial, connection);
4791 verbose_message_received (connection, message);
4792 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4794 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4796 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4798 _dbus_warn ("Message has wrong sender %s",
4799 dbus_message_get_sender (message) ?
4800 dbus_message_get_sender (message) : "(none)");
4804 if (dbus_message_is_error (message,
4805 DBUS_ERROR_NO_MEMORY))
4807 ; /* good, this is a valid response */
4809 else if (dbus_message_is_error (message,
4810 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4811 dbus_message_is_error (message,
4812 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4813 dbus_message_is_error (message,
4814 DBUS_ERROR_SPAWN_EXEC_FAILED))
4816 ; /* good, this is expected also */
4820 _dbus_warn ("Did not expect error %s",
4821 dbus_message_get_error_name (message));
4827 GotServiceInfo message_kind;
4829 if (!check_base_service_activated (context, connection,
4830 message, &base_service))
4833 base_service_message = message;
4836 /* We may need to block here for the test service to exit or finish up */
4837 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4839 message = dbus_connection_borrow_message (connection);
4840 if (message == NULL)
4842 _dbus_warn ("Did not receive any messages after base service creation notification");
4846 message_kind = check_got_service_info (message);
4848 dbus_connection_return_message (connection, message);
4851 switch (message_kind)
4853 case GOT_SOMETHING_ELSE:
4855 case GOT_SERVICE_DELETED:
4857 _dbus_warn ("Unexpected message after ActivateService "
4858 "(should be an error or a service announcement)");
4861 case GOT_SERVICE_CREATED:
4862 message = pop_message_waiting_for_memory (connection);
4863 if (message == NULL)
4865 _dbus_warn ("Failed to pop message we just put back! "
4866 "should have been a NameOwnerChanged (creation)");
4870 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4871 base_service, message))
4874 dbus_message_unref (message);
4877 if (!check_no_leftovers (context))
4879 _dbus_warn ("Messages were left over after successful activation");
4887 if (!check_get_services (context, connection, "ListNames", &services, &len))
4892 if (!_dbus_string_array_contains ((const char **)services, existent))
4894 _dbus_warn ("Did not get the expected %s from ListNames", existent);
4898 dbus_free_string_array (services);
4900 if (!check_send_exit_to_service (context, connection,
4901 EXISTENT_SERVICE_NAME, base_service))
4908 dbus_message_unref (message);
4910 if (base_service_message)
4911 dbus_message_unref (base_service_message);
4919 BusContext *context;
4920 DBusConnection *connection;
4924 check_oom_check2_func (void *data)
4926 Check2Data *d = data;
4928 if (! (* d->func) (d->context, d->connection))
4931 if (!check_no_leftovers (d->context))
4933 _dbus_warn ("Messages were left over, should be covered by test suite");
4941 check2_try_iterations (BusContext *context,
4942 DBusConnection *connection,
4943 const char *description,
4949 d.context = context;
4950 d.connection = connection;
4952 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4955 _dbus_warn ("%s failed during oom", description);
4956 _dbus_assert_not_reached ("test failed");
4961 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4962 const char *filename)
4967 if (!_dbus_string_init (&full))
4970 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4972 _dbus_string_free (&full);
4976 _dbus_string_init_const (&file, filename);
4978 if (!_dbus_concat_dir_and_file (&full, &file))
4980 _dbus_string_free (&full);
4984 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4985 _dbus_string_get_const_data (&full));
4987 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4989 _dbus_string_free (&full);
4995 bus_dispatch_test_conf (const DBusString *test_data_dir,
4996 const char *filename,
4997 dbus_bool_t use_launcher)
4999 BusContext *context;
5000 DBusConnection *foo;
5001 DBusConnection *bar;
5002 DBusConnection *baz;
5005 /* save the config name for the activation helper */
5006 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5007 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5009 dbus_error_init (&error);
5011 context = bus_context_new_test (test_data_dir, filename);
5012 if (context == NULL)
5015 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5017 _dbus_assert_not_reached ("could not alloc connection");
5019 if (!bus_setup_debug_client (foo))
5020 _dbus_assert_not_reached ("could not set up connection");
5022 spin_connection_until_authenticated (context, foo);
5024 if (!check_hello_message (context, foo))
5025 _dbus_assert_not_reached ("hello message failed");
5027 if (!check_double_hello_message (context, foo))
5028 _dbus_assert_not_reached ("double hello message failed");
5030 if (!check_add_match (context, foo, ""))
5031 _dbus_assert_not_reached ("AddMatch message failed");
5033 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5035 _dbus_assert_not_reached ("could not alloc connection");
5037 if (!bus_setup_debug_client (bar))
5038 _dbus_assert_not_reached ("could not set up connection");
5040 spin_connection_until_authenticated (context, bar);
5042 if (!check_hello_message (context, bar))
5043 _dbus_assert_not_reached ("hello message failed");
5045 if (!check_add_match (context, bar, ""))
5046 _dbus_assert_not_reached ("AddMatch message failed");
5048 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5050 _dbus_assert_not_reached ("could not alloc connection");
5052 if (!bus_setup_debug_client (baz))
5053 _dbus_assert_not_reached ("could not set up connection");
5055 spin_connection_until_authenticated (context, baz);
5057 if (!check_hello_message (context, baz))
5058 _dbus_assert_not_reached ("hello message failed");
5060 if (!check_add_match (context, baz, ""))
5061 _dbus_assert_not_reached ("AddMatch message failed");
5063 if (!check_add_match (context, baz, "interface='com.example'"))
5064 _dbus_assert_not_reached ("AddMatch message failed");
5066 #ifdef DBUS_ENABLE_STATS
5067 if (!check_get_all_match_rules (context, baz))
5068 _dbus_assert_not_reached ("GetAllMatchRules message failed");
5071 if (!check_get_connection_unix_user (context, baz))
5072 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
5074 #ifdef DBUS_WIN_FIXME
5075 _dbus_verbose("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
5077 if (!check_get_connection_unix_process_id (context, baz))
5078 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
5081 if (!check_list_services (context, baz))
5082 _dbus_assert_not_reached ("ListActivatableNames message failed");
5084 if (!check_no_leftovers (context))
5086 _dbus_warn ("Messages were left over after setting up initial connections");
5087 _dbus_assert_not_reached ("initial connection setup failed");
5090 check1_try_iterations (context, "create_and_hello",
5091 check_hello_connection);
5093 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
5094 check_nonexistent_service_no_auto_start);
5096 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
5097 check_segfault_service_no_auto_start);
5099 check2_try_iterations (context, foo, "existent_service_no_auto_start",
5100 check_existent_service_no_auto_start);
5102 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
5103 check_nonexistent_service_auto_start);
5105 /* only do the segfault test if we are not using the launcher */
5106 check2_try_iterations (context, foo, "segfault_service_auto_start",
5107 check_segfault_service_auto_start);
5109 /* only do the shell fail test if we are not using the launcher */
5110 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
5111 check_shell_fail_service_auto_start);
5113 /* specific to launcher */
5115 if (!check_launch_service_file_missing (context, foo))
5116 _dbus_assert_not_reached ("did not get service file not found error");
5119 /* Note: need to resolve some issues with the testing code in order to run
5120 * this in oom (handle that we sometimes don't get replies back from the bus
5121 * when oom happens, without blocking the test).
5123 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
5124 check_existent_service_auto_start);
5127 if (!check_existent_service_auto_start (context, foo))
5128 _dbus_assert_not_reached ("existent service auto start failed");
5130 if (!check_shell_service_success_auto_start (context, foo))
5131 _dbus_assert_not_reached ("shell success service auto start failed");
5133 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
5135 kill_client_connection_unchecked (foo);
5136 kill_client_connection_unchecked (bar);
5137 kill_client_connection_unchecked (baz);
5139 bus_context_unref (context);
5146 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
5147 const char *filename)
5149 BusContext *context;
5150 DBusConnection *foo;
5153 /* save the config name for the activation helper */
5154 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5155 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5157 dbus_error_init (&error);
5159 context = bus_context_new_test (test_data_dir, filename);
5160 if (context == NULL)
5163 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5165 _dbus_assert_not_reached ("could not alloc connection");
5167 if (!bus_setup_debug_client (foo))
5168 _dbus_assert_not_reached ("could not set up connection");
5170 spin_connection_until_authenticated (context, foo);
5172 if (!check_hello_message (context, foo))
5173 _dbus_assert_not_reached ("hello message failed");
5175 if (!check_double_hello_message (context, foo))
5176 _dbus_assert_not_reached ("double hello message failed");
5178 if (!check_add_match (context, foo, ""))
5179 _dbus_assert_not_reached ("AddMatch message failed");
5181 /* this only tests the activation.c user check */
5182 if (!check_launch_service_user_missing (context, foo))
5183 _dbus_assert_not_reached ("user missing did not trigger error");
5185 /* this only tests the desktop.c exec check */
5186 if (!check_launch_service_exec_missing (context, foo))
5187 _dbus_assert_not_reached ("exec missing did not trigger error");
5189 /* this only tests the desktop.c service check */
5190 if (!check_launch_service_service_missing (context, foo))
5191 _dbus_assert_not_reached ("service missing did not trigger error");
5193 _dbus_verbose ("Disconnecting foo\n");
5195 kill_client_connection_unchecked (foo);
5197 bus_context_unref (context);
5204 DBusTimeout *timeout;
5205 DBusConnection *connection;
5206 dbus_bool_t timedout;
5210 static BusTestCheckData *cdata;
5213 bus_dispatch_test_check_timeout (void *data)
5215 _dbus_verbose ("timeout triggered - pretend that privilege check result is available\n");
5217 /* should only happen once during the test */
5218 _dbus_assert (!cdata->timedout);
5219 cdata->timedout = TRUE;
5220 _dbus_connection_enable_dispatch (cdata->connection);
5222 /* don't call this again */
5223 _dbus_loop_remove_timeout (bus_connection_get_loop (cdata->connection),
5225 dbus_connection_unref (cdata->connection);
5226 cdata->connection = NULL;
5231 bus_dispatch_test_check_override (DBusConnection *connection,
5232 const char *privilege)
5234 _dbus_verbose ("overriding privilege check %s #%d\n", privilege, cdata->check_counter);
5235 cdata->check_counter++;
5236 if (!cdata->timedout)
5240 /* Should be the first privilege check for the "Echo" method. */
5241 _dbus_assert (cdata->check_counter == 1);
5242 cdata->timeout = _dbus_timeout_new (1, bus_dispatch_test_check_timeout,
5244 _dbus_assert (cdata->timeout);
5245 added = _dbus_loop_add_timeout (bus_connection_get_loop (connection),
5247 _dbus_assert (added);
5248 cdata->connection = connection;
5249 dbus_connection_ref (connection);
5250 _dbus_connection_disable_dispatch (connection);
5251 return BUS_RESULT_LATER;
5255 /* Should only be checked one more time, and this time succeeds. */
5256 _dbus_assert (cdata->check_counter == 2);
5257 return BUS_RESULT_TRUE;
5262 bus_dispatch_test_check (const DBusString *test_data_dir)
5264 const char *filename = "valid-config-files/debug-check-some.conf";
5265 BusContext *context;
5266 DBusConnection *foo;
5268 dbus_bool_t result = TRUE;
5269 BusTestCheckData data;
5271 /* save the config name for the activation helper */
5272 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5273 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5275 dbus_error_init (&error);
5277 context = bus_context_new_test (test_data_dir, filename);
5278 if (context == NULL)
5281 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5283 _dbus_assert_not_reached ("could not alloc connection");
5285 if (!bus_setup_debug_client (foo))
5286 _dbus_assert_not_reached ("could not set up connection");
5288 spin_connection_until_authenticated (context, foo);
5290 if (!check_hello_message (context, foo))
5291 _dbus_assert_not_reached ("hello message failed");
5293 if (!check_double_hello_message (context, foo))
5294 _dbus_assert_not_reached ("double hello message failed");
5296 if (!check_add_match_all (context, foo))
5297 _dbus_assert_not_reached ("AddMatch message failed");
5300 * Cause bus_check_send_privilege() to return BUS_RESULT_LATER in the
5301 * first call, then BUS_RESULT_TRUE.
5304 memset (cdata, 0, sizeof(*cdata));
5305 bus_check_test_override = bus_dispatch_test_check_override;
5307 result = check_existent_service_auto_start (context, foo);
5309 _dbus_assert (cdata->check_counter == 2);
5310 _dbus_assert (cdata->timedout);
5311 _dbus_assert (cdata->timeout);
5312 _dbus_assert (!cdata->connection);
5313 _dbus_timeout_unref (cdata->timeout);
5315 kill_client_connection_unchecked (foo);
5317 bus_context_unref (context);
5323 bus_dispatch_test (const DBusString *test_data_dir)
5325 _dbus_verbose ("<check> tests\n");
5326 if (!bus_dispatch_test_check (test_data_dir))
5329 /* run normal activation tests */
5330 _dbus_verbose ("Normal activation tests\n");
5331 if (!bus_dispatch_test_conf (test_data_dir,
5332 "valid-config-files/debug-allow-all.conf", FALSE))
5336 /* run launch-helper activation tests */
5337 _dbus_verbose ("Launch helper activation tests\n");
5338 if (!bus_dispatch_test_conf (test_data_dir,
5339 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
5342 /* run select launch-helper activation tests on broken service files */
5343 if (!bus_dispatch_test_conf_fail (test_data_dir,
5344 "valid-config-files-system/debug-allow-all-fail.conf"))
5352 bus_dispatch_sha1_test (const DBusString *test_data_dir)
5354 BusContext *context;
5355 DBusConnection *foo;
5358 dbus_error_init (&error);
5360 /* Test SHA1 authentication */
5361 _dbus_verbose ("Testing SHA1 context\n");
5363 context = bus_context_new_test (test_data_dir,
5364 "valid-config-files/debug-allow-all-sha1.conf");
5365 if (context == NULL)
5368 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5370 _dbus_assert_not_reached ("could not alloc connection");
5372 if (!bus_setup_debug_client (foo))
5373 _dbus_assert_not_reached ("could not set up connection");
5375 spin_connection_until_authenticated (context, foo);
5377 if (!check_hello_message (context, foo))
5378 _dbus_assert_not_reached ("hello message failed");
5380 if (!check_add_match (context, foo, ""))
5381 _dbus_assert_not_reached ("addmatch message failed");
5383 if (!check_no_leftovers (context))
5385 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection");
5386 _dbus_assert_not_reached ("initial connection setup failed");
5389 check1_try_iterations (context, "create_and_hello_sha1",
5390 check_hello_connection);
5392 kill_client_connection_unchecked (foo);
5394 bus_context_unref (context);
5399 #ifdef HAVE_UNIX_FD_PASSING
5402 bus_unix_fds_passing_test(const DBusString *test_data_dir)
5404 BusContext *context;
5405 DBusConnection *foo, *bar;
5408 DBusSocket one[2], two[2];
5412 dbus_error_init (&error);
5414 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
5415 if (context == NULL)
5416 _dbus_assert_not_reached ("could not alloc context");
5418 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5420 _dbus_assert_not_reached ("could not alloc connection");
5422 if (!bus_setup_debug_client (foo))
5423 _dbus_assert_not_reached ("could not set up connection");
5425 spin_connection_until_authenticated (context, foo);
5427 if (!check_hello_message (context, foo))
5428 _dbus_assert_not_reached ("hello message failed");
5430 if (!check_add_match (context, foo, ""))
5431 _dbus_assert_not_reached ("AddMatch message failed");
5433 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5435 _dbus_assert_not_reached ("could not alloc connection");
5437 if (!bus_setup_debug_client (bar))
5438 _dbus_assert_not_reached ("could not set up connection");
5440 spin_connection_until_authenticated (context, bar);
5442 if (!check_hello_message (context, bar))
5443 _dbus_assert_not_reached ("hello message failed");
5445 if (!check_add_match (context, bar, ""))
5446 _dbus_assert_not_reached ("AddMatch message failed");
5448 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
5449 _dbus_assert_not_reached ("could not alloc message");
5451 if (!(_dbus_socketpair (one, one+1, TRUE, &error)))
5452 _dbus_assert_not_reached("Failed to allocate pipe #1");
5454 if (!(_dbus_socketpair (two, two+1, TRUE, &error)))
5455 _dbus_assert_not_reached("Failed to allocate pipe #2");
5457 if (!dbus_message_append_args(m,
5458 DBUS_TYPE_UNIX_FD, one,
5459 DBUS_TYPE_UNIX_FD, two,
5460 DBUS_TYPE_UNIX_FD, two,
5462 _dbus_assert_not_reached("Failed to attach fds.");
5464 if (!_dbus_close_socket (one[0], &error))
5465 _dbus_assert_not_reached("Failed to close pipe #1 ");
5466 if (!_dbus_close_socket (two[0], &error))
5467 _dbus_assert_not_reached("Failed to close pipe #2 ");
5469 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
5470 _dbus_assert_not_reached("Connection cannot do fd passing");
5472 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
5473 _dbus_assert_not_reached("Connection cannot do fd passing");
5475 if (!dbus_connection_send (foo, m, NULL))
5476 _dbus_assert_not_reached("Failed to send fds");
5478 dbus_message_unref(m);
5480 bus_test_run_clients_loop (SEND_PENDING (foo));
5482 bus_test_run_everything (context);
5484 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
5486 if (!(m = pop_message_waiting_for_memory (foo)))
5487 _dbus_assert_not_reached("Failed to receive msg");
5489 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5490 _dbus_assert_not_reached("bogus message received");
5492 dbus_message_unref(m);
5494 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
5496 if (!(m = pop_message_waiting_for_memory (bar)))
5497 _dbus_assert_not_reached("Failed to receive msg");
5499 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5500 _dbus_assert_not_reached("bogus message received");
5502 if (!dbus_message_get_args(m,
5504 DBUS_TYPE_UNIX_FD, &x,
5505 DBUS_TYPE_UNIX_FD, &y,
5506 DBUS_TYPE_UNIX_FD, &z,
5508 _dbus_assert_not_reached("Failed to parse fds.");
5510 dbus_message_unref(m);
5512 if (write(x, "X", 1) != 1)
5513 _dbus_assert_not_reached("Failed to write to pipe #1");
5514 if (write(y, "Y", 1) != 1)
5515 _dbus_assert_not_reached("Failed to write to pipe #2");
5516 if (write(z, "Z", 1) != 1)
5517 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
5519 if (!_dbus_close(x, &error))
5520 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
5521 if (!_dbus_close(y, &error))
5522 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
5523 if (!_dbus_close(z, &error))
5524 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
5526 if (read(one[1].fd, &r, 1) != 1 || r != 'X')
5527 _dbus_assert_not_reached("Failed to read value from pipe.");
5528 if (read(two[1].fd, &r, 1) != 1 || r != 'Y')
5529 _dbus_assert_not_reached("Failed to read value from pipe.");
5530 if (read(two[1].fd, &r, 1) != 1 || r != 'Z')
5531 _dbus_assert_not_reached("Failed to read value from pipe.");
5533 if (!_dbus_close_socket (one[1], &error))
5534 _dbus_assert_not_reached("Failed to close pipe #1 ");
5535 if (!_dbus_close_socket (two[1], &error))
5536 _dbus_assert_not_reached("Failed to close pipe #2 ");
5538 _dbus_verbose ("Disconnecting foo\n");
5539 kill_client_connection_unchecked (foo);
5541 _dbus_verbose ("Disconnecting bar\n");
5542 kill_client_connection_unchecked (bar);
5544 bus_context_unref (context);
5550 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */