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 link = _dbus_list_get_next_link (&recipients, link);
319 if (_dbus_connection_is_overflowed (dest)
320 && dbus_message_is_signal (message,
321 DBUS_INTERFACE_TIZEN,
322 DBUS_TIZEN_CONNECTION_OVERFLOW_SIGNAL))
325 if (!send_one_message (dest, context, sender, addressed_recipient,
326 message, transaction, &tmp_error))
330 _dbus_list_clear (&recipients);
332 if (dbus_error_is_set (&tmp_error))
334 dbus_move_error (&tmp_error, error);
335 return BUS_RESULT_FALSE;
338 return BUS_RESULT_TRUE;
342 bus_check_connection_overflow (BusContext *context,
343 DBusList *connections)
345 BusTransaction *transaction;
346 DBusConnection *recipient;
349 transaction = bus_transaction_new (context);
350 if (transaction == NULL)
353 dbus_error_init (&error);
355 while ((recipient = _dbus_list_pop_first (&connections)))
356 if (_dbus_connection_is_overflowed (recipient))
358 if (!bus_driver_send_connection_overflow (recipient, transaction, &error))
360 bus_transaction_cancel_and_free (transaction);
366 if (transaction != NULL)
367 bus_transaction_execute_and_free (transaction);
370 static DBusHandlerResult
371 bus_dispatch (DBusConnection *connection,
372 DBusMessage *message)
374 const char *sender, *service_name;
376 BusTransaction *transaction;
378 DBusHandlerResult result;
379 DBusConnection *addressed_recipient;
381 result = DBUS_HANDLER_RESULT_HANDLED;
384 addressed_recipient = NULL;
385 dbus_error_init (&error);
387 context = bus_connection_get_context (connection);
388 _dbus_assert (context != NULL);
390 /* If we can't even allocate an OOM error, we just go to sleep
393 while (!bus_connection_preallocate_oom_error (connection))
394 _dbus_wait_for_memory ();
396 /* Ref connection in case we disconnect it at some point in here */
397 dbus_connection_ref (connection);
399 /* Monitors aren't meant to send messages to us. */
400 if (bus_connection_is_monitor (connection))
402 sender = bus_connection_get_name (connection);
404 /* should never happen */
406 sender = "(unknown)";
408 if (dbus_message_is_signal (message,
409 DBUS_INTERFACE_LOCAL,
412 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
413 "Monitoring connection %s closed.", sender);
414 bus_connection_disconnected (connection);
419 /* Monitors are not allowed to send messages, because that
420 * probably indicates that the monitor is incorrectly replying
421 * to its eavesdropped messages, and we want the authors of
422 * such monitors to fix them.
424 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
425 "Monitoring connection %s (%s) is not allowed "
426 "to send messages; closing it. Please fix the "
427 "monitor to not do that. "
428 "(message type=\"%s\" interface=\"%s\" "
429 "member=\"%s\" error name=\"%s\" "
430 "destination=\"%s\")",
431 sender, bus_connection_get_loginfo (connection),
432 dbus_message_type_to_string (
433 dbus_message_get_type (message)),
434 nonnull (dbus_message_get_interface (message),
436 nonnull (dbus_message_get_member (message),
438 nonnull (dbus_message_get_error_name (message),
440 nonnull (dbus_message_get_destination (message),
442 dbus_connection_close (connection);
447 service_name = dbus_message_get_destination (message);
449 #ifdef DBUS_ENABLE_VERBOSE_MODE
451 const char *interface_name, *member_name, *error_name;
453 interface_name = dbus_message_get_interface (message);
454 member_name = dbus_message_get_member (message);
455 error_name = dbus_message_get_error_name (message);
457 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
458 interface_name ? interface_name : "(no interface)",
459 member_name ? member_name : "(no member)",
460 error_name ? error_name : "(no error name)",
461 service_name ? service_name : "peer");
463 #endif /* DBUS_ENABLE_VERBOSE_MODE */
465 /* If service_name is NULL, if it's a signal we send it to all
466 * connections with a match rule. If it's not a signal, there
467 * are some special cases here but mostly we just bail out.
469 if (service_name == NULL)
471 if (dbus_message_is_signal (message,
472 DBUS_INTERFACE_LOCAL,
475 bus_connection_disconnected (connection);
479 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
481 /* DBusConnection also handles some of these automatically, we leave
484 * FIXME: this means monitors won't get the opportunity to see
485 * non-signals with NULL destination, or their replies (which in
486 * practice are UnknownMethod errors)
488 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
493 /* Create our transaction */
494 transaction = bus_transaction_new (context);
495 if (transaction == NULL)
497 BUS_SET_OOM (&error);
501 /* Assign a sender to the message */
502 if (bus_connection_is_active (connection))
504 sender = bus_connection_get_name (connection);
505 _dbus_assert (sender != NULL);
507 if (!dbus_message_set_sender (message, sender))
509 BUS_SET_OOM (&error);
515 /* For monitors' benefit: we don't want the sender to be able to
516 * trick the monitor by supplying a forged sender, and we also
517 * don't want the message to have no sender at all. */
518 if (!dbus_message_set_sender (message, ":not.active.yet"))
520 BUS_SET_OOM (&error);
525 /* We need to refetch the service name here, because
526 * dbus_message_set_sender can cause the header to be
527 * reallocated, and thus the service_name pointer will become
530 service_name = dbus_message_get_destination (message);
533 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
535 BusDeferredMessage *deferred_message = NULL;
537 if (!bus_transaction_capture (transaction, connection, NULL, message))
539 BUS_SET_OOM (&error);
543 switch (bus_context_check_security_policy (context, transaction,
544 connection, NULL, NULL, message,
548 case BUS_RESULT_TRUE:
550 case BUS_RESULT_FALSE:
551 _dbus_verbose ("Security policy rejected message\n");
553 case BUS_RESULT_LATER:
554 /* Disable dispatching messages from the sender,
555 * roll back and dispatch the message once the policy result is available */
556 bus_deferred_message_disable_sender(deferred_message);
557 bus_transaction_cancel_and_free (transaction);
559 result = DBUS_HANDLER_RESULT_LATER;
563 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
564 switch (bus_driver_handle_message (connection, transaction, message, &error))
566 case BUS_RESULT_TRUE:
568 case BUS_RESULT_FALSE:
570 case BUS_RESULT_LATER:
571 bus_transaction_cancel_and_free (transaction);
573 result = DBUS_HANDLER_RESULT_LATER;
577 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
579 if (!bus_transaction_capture (transaction, connection, NULL, message))
581 BUS_SET_OOM (&error);
585 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
586 dbus_connection_close (connection);
589 else if (service_name != NULL) /* route to named service */
591 DBusString service_string;
593 BusRegistry *registry;
595 _dbus_assert (service_name != NULL);
597 registry = bus_connection_get_registry (connection);
599 _dbus_string_init_const (&service_string, service_name);
600 service = bus_registry_lookup (registry, &service_string);
602 if (service == NULL && dbus_message_get_auto_start (message))
604 BusActivation *activation;
605 BusDeferredMessage *deferred_message = NULL;
607 if (!bus_transaction_capture (transaction, connection, NULL,
610 BUS_SET_OOM (&error);
614 activation = bus_connection_get_activation (connection);
616 /* This will do as much of a security policy check as it can.
617 * We can't do the full security policy check here, since the
618 * addressed recipient service doesn't exist yet. We do it before
619 * sending the message after the service has been created.
621 switch (bus_activation_activate_service (activation, connection, transaction, TRUE,
622 message, service_name, &error,
625 case BUS_RESULT_FALSE:
626 _DBUS_ASSERT_ERROR_IS_SET (&error);
627 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
629 case BUS_RESULT_LATER:
630 bus_deferred_message_disable_sender(deferred_message);
631 bus_transaction_cancel_and_free (transaction);
633 result = DBUS_HANDLER_RESULT_LATER;
635 case BUS_RESULT_TRUE:
641 else if (service == NULL)
643 if (!bus_transaction_capture (transaction, connection,
646 BUS_SET_OOM (&error);
650 dbus_set_error (&error,
651 DBUS_ERROR_NAME_HAS_NO_OWNER,
652 "Name \"%s\" does not exist",
658 addressed_recipient = bus_service_get_primary_owners_connection (service);
659 _dbus_assert (addressed_recipient != NULL);
661 if (!bus_transaction_capture (transaction, connection,
662 addressed_recipient, message))
664 BUS_SET_OOM (&error);
669 else /* service_name == NULL */
671 if (!bus_transaction_capture (transaction, connection, NULL, message))
673 BUS_SET_OOM (&error);
678 /* Now send the message to its destination (or not, if
679 * addressed_recipient == NULL), and match it against other connections'
682 switch (bus_dispatch_matches (transaction, connection, addressed_recipient, message, NULL, &error))
684 case BUS_RESULT_TRUE:
685 case BUS_RESULT_FALSE:
687 case BUS_RESULT_LATER:
688 /* Roll back and dispatch the message once the policy result is available */
689 bus_transaction_cancel_and_free (transaction);
691 result = DBUS_HANDLER_RESULT_LATER;
696 if (dbus_error_is_set (&error))
698 /* Even if we disconnected it, pretend to send it any pending error
699 * messages so that monitors can observe them.
701 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
703 bus_connection_send_oom_error (connection, message);
705 /* cancel transaction due to OOM */
706 if (transaction != NULL)
708 bus_transaction_cancel_and_free (transaction);
714 /* Try to send the real error, if no mem to do that, send
717 _dbus_assert (transaction != NULL);
718 if (!bus_transaction_send_error_reply (transaction, connection,
721 bus_connection_send_oom_error (connection, message);
723 /* cancel transaction due to OOM */
724 if (transaction != NULL)
726 bus_transaction_cancel_and_free (transaction);
733 dbus_error_free (&error);
736 if (transaction != NULL)
738 DBusList *connections_copy = bus_transaction_copy_connections (transaction);
740 bus_transaction_execute_and_free (transaction);
742 bus_check_connection_overflow (context, connections_copy);
745 dbus_connection_unref (connection);
750 static DBusHandlerResult
751 bus_dispatch_message_filter (DBusConnection *connection,
752 DBusMessage *message,
755 return bus_dispatch (connection, message);
759 bus_dispatch_add_connection (DBusConnection *connection)
761 if (!dbus_connection_add_filter (connection,
762 bus_dispatch_message_filter,
770 bus_dispatch_remove_connection (DBusConnection *connection)
772 /* Here we tell the bus driver that we want to get off. */
773 bus_driver_remove_connection (connection);
775 dbus_connection_remove_filter (connection,
776 bus_dispatch_message_filter,
780 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
786 /* This is used to know whether we need to block in order to finish
787 * sending a message, or whether the initial dbus_connection_send()
788 * already flushed the queue.
790 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
792 typedef dbus_bool_t (* Check1Func) (BusContext *context);
793 typedef dbus_bool_t (* Check2Func) (BusContext *context,
794 DBusConnection *connection);
796 static dbus_bool_t check_no_leftovers (BusContext *context);
799 block_connection_until_message_from_bus (BusContext *context,
800 DBusConnection *connection,
801 const char *what_is_expected)
803 _dbus_verbose ("expecting: %s\n", what_is_expected);
805 while (dbus_connection_get_dispatch_status (connection) ==
806 DBUS_DISPATCH_COMPLETE &&
807 dbus_connection_get_is_connected (connection))
809 bus_test_run_bus_loop (context, TRUE);
810 bus_test_run_clients_loop (FALSE);
815 spin_connection_until_authenticated (BusContext *context,
816 DBusConnection *connection)
818 _dbus_verbose ("Spinning to auth connection %p\n", connection);
819 while (!dbus_connection_get_is_authenticated (connection) &&
820 dbus_connection_get_is_connected (connection))
822 bus_test_run_bus_loop (context, FALSE);
823 bus_test_run_clients_loop (FALSE);
825 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
828 /* compensate for fact that pop_message() can return #NULL due to OOM */
830 pop_message_waiting_for_memory (DBusConnection *connection)
832 while (dbus_connection_get_dispatch_status (connection) ==
833 DBUS_DISPATCH_NEED_MEMORY)
834 _dbus_wait_for_memory ();
836 return dbus_connection_pop_message (connection);
840 borrow_message_waiting_for_memory (DBusConnection *connection)
842 while (dbus_connection_get_dispatch_status (connection) ==
843 DBUS_DISPATCH_NEED_MEMORY)
844 _dbus_wait_for_memory ();
846 return dbus_connection_borrow_message (connection);
850 warn_unexpected_real (DBusConnection *connection,
851 DBusMessage *message,
852 const char *expected,
853 const char *function,
857 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s",
859 dbus_message_get_interface (message) ?
860 dbus_message_get_interface (message) : "(unset)",
861 dbus_message_get_member (message) ?
862 dbus_message_get_member (message) : "(unset)",
863 dbus_message_get_error_name (message) ?
864 dbus_message_get_error_name (message) : "(unset)",
868 _dbus_warn ("%s:%d received no message on %p, expecting %s",
869 function, line, connection, expected);
872 #define warn_unexpected(connection, message, expected) \
873 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
876 verbose_message_received (DBusConnection *connection,
877 DBusMessage *message)
879 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
880 dbus_message_get_interface (message) ?
881 dbus_message_get_interface (message) : "(unset)",
882 dbus_message_get_member (message) ?
883 dbus_message_get_member (message) : "(unset)",
884 dbus_message_get_error_name (message) ?
885 dbus_message_get_error_name (message) : "(unset)",
898 ServiceInfoKind expected_kind;
899 const char *expected_service_name;
901 DBusConnection *skip_connection;
903 } CheckServiceOwnerChangedData;
906 check_service_owner_changed_foreach (DBusConnection *connection,
909 CheckServiceOwnerChangedData *d = data;
910 DBusMessage *message;
912 const char *service_name, *old_owner, *new_owner;
914 if (d->expected_kind == SERVICE_CREATED
915 && connection == d->skip_connection)
918 dbus_error_init (&error);
921 message = pop_message_waiting_for_memory (connection);
924 block_connection_until_message_from_bus (d->context, connection, "NameOwnerChanged");
925 message = pop_message_waiting_for_memory (connection);
928 _dbus_warn ("Did not receive a message on %p, expecting %s",
929 connection, "NameOwnerChanged");
933 else if (!dbus_message_is_signal (message,
937 warn_unexpected (connection, message, "NameOwnerChanged");
943 reget_service_info_data:
948 dbus_message_get_args (message, &error,
949 DBUS_TYPE_STRING, &service_name,
950 DBUS_TYPE_STRING, &old_owner,
951 DBUS_TYPE_STRING, &new_owner,
954 if (dbus_error_is_set (&error))
956 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
958 dbus_error_free (&error);
959 _dbus_wait_for_memory ();
960 goto reget_service_info_data;
964 _dbus_warn ("Did not get the expected arguments");
969 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
970 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
971 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
973 _dbus_warn ("inconsistent NameOwnerChanged arguments");
977 if (strcmp (service_name, d->expected_service_name) != 0)
979 _dbus_warn ("expected info on service %s, got info on %s",
980 d->expected_service_name,
985 if (*service_name == ':' && new_owner[0]
986 && strcmp (service_name, new_owner) != 0)
988 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])",
989 service_name, old_owner, new_owner);
997 dbus_error_free (&error);
1000 dbus_message_unref (message);
1007 kill_client_connection (BusContext *context,
1008 DBusConnection *connection)
1012 CheckServiceOwnerChangedData socd;
1014 _dbus_verbose ("killing connection %p\n", connection);
1016 s = dbus_bus_get_unique_name (connection);
1017 _dbus_assert (s != NULL);
1019 while ((base_service = _dbus_strdup (s)) == NULL)
1020 _dbus_wait_for_memory ();
1022 dbus_connection_ref (connection);
1024 /* kick in the disconnect handler that unrefs the connection */
1025 dbus_connection_close (connection);
1027 bus_test_run_everything (context);
1029 _dbus_assert (bus_test_client_listed (connection));
1031 /* Run disconnect handler in test.c */
1032 if (bus_connection_dispatch_one_message (connection))
1033 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
1035 _dbus_assert (!dbus_connection_get_is_connected (connection));
1036 dbus_connection_unref (connection);
1038 _dbus_assert (!bus_test_client_listed (connection));
1040 socd.expected_kind = SERVICE_DELETED;
1041 socd.expected_service_name = base_service;
1042 socd.failed = FALSE;
1043 socd.skip_connection = NULL;
1044 socd.context = context;
1046 bus_test_clients_foreach (check_service_owner_changed_foreach,
1049 dbus_free (base_service);
1052 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
1054 if (!check_no_leftovers (context))
1055 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
1059 kill_client_connection_unchecked (DBusConnection *connection)
1061 /* This kills the connection without expecting it to affect
1062 * the rest of the bus.
1064 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
1066 dbus_connection_ref (connection);
1067 dbus_connection_close (connection);
1068 /* dispatching disconnect handler will unref once */
1069 if (bus_connection_dispatch_one_message (connection))
1070 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
1072 _dbus_assert (!bus_test_client_listed (connection));
1073 dbus_connection_unref (connection);
1079 } CheckNoMessagesData;
1082 check_no_messages_foreach (DBusConnection *connection,
1085 CheckNoMessagesData *d = data;
1086 DBusMessage *message;
1088 message = pop_message_waiting_for_memory (connection);
1089 if (message != NULL)
1091 warn_unexpected (connection, message, "no messages");
1097 dbus_message_unref (message);
1102 check_no_leftovers (BusContext *context)
1104 CheckNoMessagesData nmd;
1107 bus_test_clients_foreach (check_no_messages_foreach,
1112 _dbus_verbose ("leftover message found\n");
1119 /* returns TRUE if the correct thing happens,
1120 * but the correct thing may include OOM errors.
1123 check_hello_message (BusContext *context,
1124 DBusConnection *connection)
1126 DBusMessage *message;
1127 DBusMessage *name_message;
1128 dbus_uint32_t serial;
1132 const char *acquired;
1135 dbus_error_init (&error);
1139 name_message = NULL;
1141 _dbus_verbose ("check_hello_message for %p\n", connection);
1143 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1145 DBUS_INTERFACE_DBUS,
1148 if (message == NULL)
1151 dbus_connection_ref (connection); /* because we may get disconnected */
1153 if (!dbus_connection_send (connection, message, &serial))
1155 dbus_message_unref (message);
1156 dbus_connection_unref (connection);
1160 _dbus_assert (dbus_message_has_signature (message, ""));
1162 dbus_message_unref (message);
1165 if (!dbus_connection_get_is_connected (connection))
1167 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1169 dbus_connection_unref (connection);
1174 /* send our message */
1175 bus_test_run_clients_loop (SEND_PENDING (connection));
1177 if (!dbus_connection_get_is_connected (connection))
1179 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1181 dbus_connection_unref (connection);
1186 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1188 if (!dbus_connection_get_is_connected (connection))
1190 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1192 dbus_connection_unref (connection);
1197 message = pop_message_waiting_for_memory (connection);
1198 if (message == NULL)
1200 _dbus_warn ("Did not receive a reply to %s %d on %p",
1201 "Hello", serial, connection);
1205 verbose_message_received (connection, message);
1207 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1209 _dbus_warn ("Message has wrong sender %s",
1210 dbus_message_get_sender (message) ?
1211 dbus_message_get_sender (message) : "(none)");
1215 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1217 if (dbus_message_is_error (message,
1218 DBUS_ERROR_NO_MEMORY))
1220 ; /* good, this is a valid response */
1224 warn_unexpected (connection, message, "not this error");
1231 CheckServiceOwnerChangedData socd;
1233 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1235 ; /* good, expected */
1239 warn_unexpected (connection, message, "method return for Hello");
1244 retry_get_hello_name:
1245 if (!dbus_message_get_args (message, &error,
1246 DBUS_TYPE_STRING, &name,
1249 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1251 _dbus_verbose ("no memory to get service name arg from hello\n");
1252 dbus_error_free (&error);
1253 _dbus_wait_for_memory ();
1254 goto retry_get_hello_name;
1258 _dbus_assert (dbus_error_is_set (&error));
1259 _dbus_warn ("Did not get the expected single string argument to hello");
1264 _dbus_verbose ("Got hello name: %s\n", name);
1266 while (!dbus_bus_set_unique_name (connection, name))
1267 _dbus_wait_for_memory ();
1269 socd.expected_kind = SERVICE_CREATED;
1270 socd.expected_service_name = name;
1271 socd.failed = FALSE;
1272 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
1273 socd.context = context;
1275 bus_test_clients_foreach (check_service_owner_changed_foreach,
1281 name_message = message;
1282 /* Client should also have gotten ServiceAcquired */
1284 message = pop_message_waiting_for_memory (connection);
1285 if (message == NULL)
1287 block_connection_until_message_from_bus (context, connection, "signal NameAcquired");
1288 message = pop_message_waiting_for_memory (connection);
1289 if (message == NULL)
1291 _dbus_warn ("Expecting %s, got nothing",
1296 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
1299 _dbus_warn ("Expecting %s, got smthg else",
1304 retry_get_acquired_name:
1305 if (!dbus_message_get_args (message, &error,
1306 DBUS_TYPE_STRING, &acquired,
1309 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1311 _dbus_verbose ("no memory to get service name arg from acquired\n");
1312 dbus_error_free (&error);
1313 _dbus_wait_for_memory ();
1314 goto retry_get_acquired_name;
1318 _dbus_assert (dbus_error_is_set (&error));
1319 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired");
1324 _dbus_verbose ("Got acquired name: %s\n", acquired);
1326 if (strcmp (acquired, name) != 0)
1328 _dbus_warn ("Acquired name is %s but expected %s",
1335 if (!check_no_leftovers (context))
1341 _dbus_verbose ("ending - retval = %d\n", retval);
1343 dbus_error_free (&error);
1346 dbus_message_unref (message);
1349 dbus_message_unref (name_message);
1351 dbus_connection_unref (connection);
1356 /* returns TRUE if the correct thing happens,
1357 * but the correct thing may include OOM errors.
1360 check_double_hello_message (BusContext *context,
1361 DBusConnection *connection)
1363 DBusMessage *message;
1364 dbus_uint32_t serial;
1369 dbus_error_init (&error);
1372 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1374 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1376 DBUS_INTERFACE_DBUS,
1379 if (message == NULL)
1382 if (!dbus_connection_send (connection, message, &serial))
1384 dbus_message_unref (message);
1388 dbus_message_unref (message);
1391 /* send our message */
1392 bus_test_run_clients_loop (SEND_PENDING (connection));
1394 dbus_connection_ref (connection); /* because we may get disconnected */
1395 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1397 if (!dbus_connection_get_is_connected (connection))
1399 _dbus_verbose ("connection was disconnected\n");
1401 dbus_connection_unref (connection);
1406 dbus_connection_unref (connection);
1408 message = pop_message_waiting_for_memory (connection);
1409 if (message == NULL)
1411 _dbus_warn ("Did not receive a reply to %s %d on %p",
1412 "Hello", serial, connection);
1416 verbose_message_received (connection, message);
1418 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1420 _dbus_warn ("Message has wrong sender %s",
1421 dbus_message_get_sender (message) ?
1422 dbus_message_get_sender (message) : "(none)");
1426 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1428 warn_unexpected (connection, message, "method return for Hello");
1432 if (!check_no_leftovers (context))
1438 dbus_error_free (&error);
1441 dbus_message_unref (message);
1446 /* returns TRUE if the correct thing happens,
1447 * but the correct thing may include OOM errors.
1450 check_get_connection_unix_user (BusContext *context,
1451 DBusConnection *connection)
1453 DBusMessage *message;
1454 dbus_uint32_t serial;
1457 const char *base_service_name;
1461 dbus_error_init (&error);
1464 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1466 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1468 DBUS_INTERFACE_DBUS,
1469 "GetConnectionUnixUser");
1471 if (message == NULL)
1474 base_service_name = dbus_bus_get_unique_name (connection);
1476 if (!dbus_message_append_args (message,
1477 DBUS_TYPE_STRING, &base_service_name,
1480 dbus_message_unref (message);
1484 if (!dbus_connection_send (connection, message, &serial))
1486 dbus_message_unref (message);
1490 /* send our message */
1491 bus_test_run_clients_loop (SEND_PENDING (connection));
1493 dbus_message_unref (message);
1496 dbus_connection_ref (connection); /* because we may get disconnected */
1497 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1499 if (!dbus_connection_get_is_connected (connection))
1501 _dbus_verbose ("connection was disconnected\n");
1503 dbus_connection_unref (connection);
1508 dbus_connection_unref (connection);
1510 message = pop_message_waiting_for_memory (connection);
1511 if (message == NULL)
1513 _dbus_warn ("Did not receive a reply to %s %d on %p",
1514 "GetConnectionUnixUser", serial, connection);
1518 verbose_message_received (connection, message);
1520 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1522 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1524 ; /* good, this is a valid response */
1527 else if (dbus_message_is_error (message, DBUS_ERROR_FAILED))
1529 /* this is OK, Unix uids aren't meaningful on Windows */
1534 warn_unexpected (connection, message, "not this error");
1541 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1543 ; /* good, expected */
1547 warn_unexpected (connection, message,
1548 "method_return for GetConnectionUnixUser");
1555 if (!dbus_message_get_args (message, &error,
1556 DBUS_TYPE_UINT32, &uid,
1559 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1561 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1562 dbus_error_free (&error);
1563 _dbus_wait_for_memory ();
1564 goto retry_get_property;
1568 _dbus_assert (dbus_error_is_set (&error));
1569 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser");
1575 if (!check_no_leftovers (context))
1581 dbus_error_free (&error);
1584 dbus_message_unref (message);
1589 /* returns TRUE if the correct thing happens,
1590 * but the correct thing may include OOM errors.
1593 check_get_connection_unix_process_id (BusContext *context,
1594 DBusConnection *connection)
1596 DBusMessage *message;
1597 dbus_uint32_t serial;
1600 const char *base_service_name;
1606 dbus_error_init (&error);
1609 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1611 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1613 DBUS_INTERFACE_DBUS,
1614 "GetConnectionUnixProcessID");
1616 if (message == NULL)
1619 base_service_name = dbus_bus_get_unique_name (connection);
1621 if (!dbus_message_append_args (message,
1622 DBUS_TYPE_STRING, &base_service_name,
1625 dbus_message_unref (message);
1629 if (!dbus_connection_send (connection, message, &serial))
1631 dbus_message_unref (message);
1635 /* send our message */
1636 bus_test_run_clients_loop (SEND_PENDING (connection));
1638 dbus_message_unref (message);
1641 dbus_connection_ref (connection); /* because we may get disconnected */
1642 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1644 if (!dbus_connection_get_is_connected (connection))
1646 _dbus_verbose ("connection was disconnected\n");
1648 dbus_connection_unref (connection);
1653 dbus_connection_unref (connection);
1655 message = pop_message_waiting_for_memory (connection);
1656 if (message == NULL)
1658 _dbus_warn ("Did not receive a reply to %s %d on %p",
1659 "GetConnectionUnixProcessID", serial, connection);
1663 verbose_message_received (connection, message);
1665 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1667 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1669 ; /* good, this is a valid response */
1672 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1674 /* We are expecting this error, since we know in the test suite we aren't
1675 * talking to a client running on UNIX
1677 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1682 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
1683 defined(__linux__) || \
1684 defined(__OpenBSD__)
1685 /* In principle NetBSD should also be in that list, but
1686 * its implementation of PID-passing doesn't work
1687 * over a socketpair() as used in the debug-pipe transport.
1688 * We test this functionality in a more realistic situation
1689 * in test/dbus-daemon.c. */
1690 warn_unexpected (connection, message, "not this error");
1694 _dbus_verbose ("does not support GetConnectionUnixProcessID but perhaps that's OK?\n");
1701 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1704 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1706 ; /* good, expected */
1710 warn_unexpected (connection, message,
1711 "method_return for GetConnectionUnixProcessID");
1718 if (!dbus_message_get_args (message, &error,
1719 DBUS_TYPE_UINT32, &pid,
1722 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1724 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1725 dbus_error_free (&error);
1726 _dbus_wait_for_memory ();
1727 goto retry_get_property;
1731 _dbus_assert (dbus_error_is_set (&error));
1732 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID");
1738 /* test if returned pid is the same as our own pid
1740 * @todo It would probably be good to restructure the tests
1741 * in a way so our parent is the bus that we're testing
1742 * cause then we can test that the pid returned matches
1745 if (pid != (dbus_uint32_t) _dbus_getpid ())
1747 _dbus_assert (dbus_error_is_set (&error));
1748 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid");
1752 #endif /* !DBUS_WIN */
1755 if (!check_no_leftovers (context))
1761 dbus_error_free (&error);
1764 dbus_message_unref (message);
1769 /* returns TRUE if the correct thing happens,
1770 * but the correct thing may include OOM errors.
1773 check_add_match (BusContext *context,
1774 DBusConnection *connection,
1777 DBusMessage *message;
1779 dbus_uint32_t serial;
1783 dbus_error_init (&error);
1786 _dbus_verbose ("check_add_match for connection %p, rule %s\n",
1789 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1791 DBUS_INTERFACE_DBUS,
1794 if (message == NULL)
1797 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &rule,
1800 dbus_message_unref (message);
1804 if (!dbus_connection_send (connection, message, &serial))
1806 dbus_message_unref (message);
1810 dbus_message_unref (message);
1813 dbus_connection_ref (connection); /* because we may get disconnected */
1815 /* send our message */
1816 bus_test_run_clients_loop (SEND_PENDING (connection));
1818 if (!dbus_connection_get_is_connected (connection))
1820 _dbus_verbose ("connection was disconnected\n");
1822 dbus_connection_unref (connection);
1827 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1829 if (!dbus_connection_get_is_connected (connection))
1831 _dbus_verbose ("connection was disconnected\n");
1833 dbus_connection_unref (connection);
1838 dbus_connection_unref (connection);
1840 message = pop_message_waiting_for_memory (connection);
1841 if (message == NULL)
1843 _dbus_warn ("Did not receive a reply to %s %d on %p",
1844 "AddMatch", serial, connection);
1848 verbose_message_received (connection, message);
1850 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1852 _dbus_warn ("Message has wrong sender %s",
1853 dbus_message_get_sender (message) ?
1854 dbus_message_get_sender (message) : "(none)");
1858 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1860 if (dbus_message_is_error (message,
1861 DBUS_ERROR_NO_MEMORY))
1863 ; /* good, this is a valid response */
1867 warn_unexpected (connection, message, "not this error");
1874 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1876 ; /* good, expected */
1877 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1881 warn_unexpected (connection, message, "method return for AddMatch");
1887 if (!check_no_leftovers (context))
1893 dbus_error_free (&error);
1896 dbus_message_unref (message);
1901 #ifdef DBUS_ENABLE_STATS
1902 /* returns TRUE if the correct thing happens,
1903 * but the correct thing may include OOM errors.
1906 check_get_all_match_rules (BusContext *context,
1907 DBusConnection *connection)
1909 DBusMessage *message;
1911 dbus_uint32_t serial;
1915 dbus_error_init (&error);
1918 _dbus_verbose ("check_get_all_match_rules for connection %p\n",
1921 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1923 BUS_INTERFACE_STATS,
1924 "GetAllMatchRules");
1926 if (message == NULL)
1929 if (!dbus_connection_send (connection, message, &serial))
1931 dbus_message_unref (message);
1935 dbus_message_unref (message);
1938 dbus_connection_ref (connection); /* because we may get disconnected */
1940 /* send our message */
1941 bus_test_run_clients_loop (SEND_PENDING (connection));
1943 if (!dbus_connection_get_is_connected (connection))
1945 _dbus_verbose ("connection was disconnected\n");
1947 dbus_connection_unref (connection);
1952 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1954 if (!dbus_connection_get_is_connected (connection))
1956 _dbus_verbose ("connection was disconnected\n");
1958 dbus_connection_unref (connection);
1963 dbus_connection_unref (connection);
1965 message = pop_message_waiting_for_memory (connection);
1966 if (message == NULL)
1968 _dbus_warn ("Did not receive a reply to %s %d on %p",
1969 "AddMatch", serial, connection);
1973 verbose_message_received (connection, message);
1975 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1977 _dbus_warn ("Message has wrong sender %s",
1978 dbus_message_get_sender (message) ?
1979 dbus_message_get_sender (message) : "(none)");
1983 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1985 if (dbus_message_is_error (message,
1986 DBUS_ERROR_NO_MEMORY))
1988 ; /* good, this is a valid response */
1992 warn_unexpected (connection, message, "not this error");
1999 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
2001 ; /* good, expected */
2002 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
2006 warn_unexpected (connection, message, "method return for AddMatch");
2012 if (!check_no_leftovers (context))
2018 dbus_error_free (&error);
2021 dbus_message_unref (message);
2027 /* returns TRUE if the correct thing happens,
2028 * but the correct thing may include OOM errors.
2031 check_hello_connection (BusContext *context)
2033 DBusConnection *connection;
2036 dbus_error_init (&error);
2038 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
2039 if (connection == NULL)
2041 _DBUS_ASSERT_ERROR_IS_SET (&error);
2042 dbus_error_free (&error);
2046 if (!bus_setup_debug_client (connection))
2048 dbus_connection_close (connection);
2049 dbus_connection_unref (connection);
2053 spin_connection_until_authenticated (context, connection);
2055 if (!check_hello_message (context, connection))
2058 if (dbus_bus_get_unique_name (connection) == NULL)
2060 /* We didn't successfully register, so we can't
2061 * do the usual kill_client_connection() checks
2063 kill_client_connection_unchecked (connection);
2067 if (!check_add_match (context, connection, ""))
2070 kill_client_connection (context, connection);
2076 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
2078 /* returns TRUE if the correct thing happens,
2079 * but the correct thing may include OOM errors.
2082 check_nonexistent_service_no_auto_start (BusContext *context,
2083 DBusConnection *connection)
2085 DBusMessage *message;
2086 dbus_uint32_t serial;
2088 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
2089 dbus_uint32_t flags;
2091 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2093 DBUS_INTERFACE_DBUS,
2094 "StartServiceByName");
2096 if (message == NULL)
2099 dbus_message_set_auto_start (message, FALSE);
2102 if (!dbus_message_append_args (message,
2103 DBUS_TYPE_STRING, &nonexistent,
2104 DBUS_TYPE_UINT32, &flags,
2107 dbus_message_unref (message);
2111 if (!dbus_connection_send (connection, message, &serial))
2113 dbus_message_unref (message);
2117 dbus_message_unref (message);
2120 bus_test_run_everything (context);
2121 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
2122 bus_test_run_everything (context);
2124 if (!dbus_connection_get_is_connected (connection))
2126 _dbus_verbose ("connection was disconnected\n");
2132 message = pop_message_waiting_for_memory (connection);
2133 if (message == NULL)
2135 _dbus_warn ("Did not receive a reply to %s %d on %p",
2136 "StartServiceByName", serial, connection);
2140 verbose_message_received (connection, message);
2142 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2144 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2146 _dbus_warn ("Message has wrong sender %s",
2147 dbus_message_get_sender (message) ?
2148 dbus_message_get_sender (message) : "(none)");
2152 if (dbus_message_is_error (message,
2153 DBUS_ERROR_NO_MEMORY))
2155 ; /* good, this is a valid response */
2157 else if (dbus_message_is_error (message,
2158 DBUS_ERROR_SERVICE_UNKNOWN))
2160 ; /* good, this is expected also */
2164 warn_unexpected (connection, message, "not this error");
2170 _dbus_warn ("Did not expect to successfully activate %s",
2171 NONEXISTENT_SERVICE_NAME);
2179 dbus_message_unref (message);
2184 /* returns TRUE if the correct thing happens,
2185 * but the correct thing may include OOM errors.
2188 check_nonexistent_service_auto_start (BusContext *context,
2189 DBusConnection *connection)
2191 DBusMessage *message;
2192 dbus_uint32_t serial;
2195 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
2196 "/org/freedesktop/TestSuite",
2197 "org.freedesktop.TestSuite",
2200 if (message == NULL)
2203 if (!dbus_connection_send (connection, message, &serial))
2205 dbus_message_unref (message);
2209 dbus_message_unref (message);
2212 bus_test_run_everything (context);
2213 block_connection_until_message_from_bus (context, connection, "reply to Echo");
2214 bus_test_run_everything (context);
2216 if (!dbus_connection_get_is_connected (connection))
2218 _dbus_verbose ("connection was disconnected\n");
2224 message = pop_message_waiting_for_memory (connection);
2226 if (message == NULL)
2228 _dbus_warn ("Did not receive a reply to %s %d on %p",
2229 "Echo message (auto activation)", serial, connection);
2233 verbose_message_received (connection, message);
2235 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2237 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2239 _dbus_warn ("Message has wrong sender %s",
2240 dbus_message_get_sender (message) ?
2241 dbus_message_get_sender (message) : "(none)");
2245 if (dbus_message_is_error (message,
2246 DBUS_ERROR_NO_MEMORY))
2248 ; /* good, this is a valid response */
2250 else if (dbus_message_is_error (message,
2251 DBUS_ERROR_SERVICE_UNKNOWN))
2253 ; /* good, this is expected also */
2257 warn_unexpected (connection, message, "not this error");
2263 _dbus_warn ("Did not expect to successfully activate %s",
2264 NONEXISTENT_SERVICE_NAME);
2272 dbus_message_unref (message);
2278 check_base_service_activated (BusContext *context,
2279 DBusConnection *connection,
2280 DBusMessage *initial_message,
2281 const char **base_service_p)
2283 DBusMessage *message;
2286 const char *base_service, *base_service_from_bus, *old_owner;
2290 dbus_error_init (&error);
2291 base_service = NULL;
2293 base_service_from_bus = NULL;
2295 message = initial_message;
2296 dbus_message_ref (message);
2298 if (dbus_message_is_signal (message,
2299 DBUS_INTERFACE_DBUS,
2300 "NameOwnerChanged"))
2302 CheckServiceOwnerChangedData socd;
2304 reget_service_name_arg:
2305 base_service = NULL;
2307 base_service_from_bus = NULL;
2309 if (!dbus_message_get_args (message, &error,
2310 DBUS_TYPE_STRING, &base_service,
2311 DBUS_TYPE_STRING, &old_owner,
2312 DBUS_TYPE_STRING, &base_service_from_bus,
2315 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2317 dbus_error_free (&error);
2318 _dbus_wait_for_memory ();
2319 goto reget_service_name_arg;
2323 _dbus_warn ("Message %s doesn't have a service name: %s",
2324 "NameOwnerChanged (creation)",
2330 if (*base_service != ':')
2332 _dbus_warn ("Expected base service activation, got \"%s\" instead",
2337 if (strcmp (base_service, base_service_from_bus) != 0)
2339 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"",
2340 base_service, base_service_from_bus);
2346 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"",
2351 socd.expected_kind = SERVICE_CREATED;
2352 socd.expected_service_name = base_service;
2353 socd.failed = FALSE;
2354 socd.skip_connection = connection;
2355 socd.context = context;
2357 bus_test_clients_foreach (check_service_owner_changed_foreach,
2365 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
2371 *base_service_p = base_service;
2377 dbus_message_unref (message);
2378 dbus_error_free (&error);
2384 check_service_activated (BusContext *context,
2385 DBusConnection *connection,
2386 const char *activated_name,
2387 const char *base_service_name,
2388 DBusMessage *initial_message)
2390 DBusMessage *message;
2393 dbus_uint32_t activation_result;
2397 dbus_error_init (&error);
2399 message = initial_message;
2400 dbus_message_ref (message);
2402 if (dbus_message_is_signal (message,
2403 DBUS_INTERFACE_DBUS,
2404 "NameOwnerChanged"))
2406 CheckServiceOwnerChangedData socd;
2407 const char *service_name, *base_service_from_bus, *old_owner;
2409 reget_service_name_arg:
2410 service_name = NULL;
2412 base_service_from_bus = NULL;
2414 if (!dbus_message_get_args (message, &error,
2415 DBUS_TYPE_STRING, &service_name,
2416 DBUS_TYPE_STRING, &old_owner,
2417 DBUS_TYPE_STRING, &base_service_from_bus,
2420 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2422 dbus_error_free (&error);
2423 _dbus_wait_for_memory ();
2424 goto reget_service_name_arg;
2428 _dbus_warn ("Message %s doesn't have a service name: %s",
2429 "NameOwnerChanged (creation)",
2435 if (strcmp (service_name, activated_name) != 0)
2437 _dbus_warn ("Expected to see service %s created, saw %s instead",
2438 activated_name, service_name);
2442 if (strcmp (base_service_name, base_service_from_bus) != 0)
2444 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead",
2445 base_service_from_bus, base_service_name);
2451 _dbus_warn ("expected a %s, got a %s",
2452 "NameOwnerChanged (creation)",
2453 "NameOwnerChanged (change)");
2457 socd.expected_kind = SERVICE_CREATED;
2458 socd.skip_connection = connection;
2459 socd.failed = FALSE;
2460 socd.expected_service_name = service_name;
2461 socd.context = context;
2463 bus_test_clients_foreach (check_service_owner_changed_foreach,
2469 dbus_message_unref (message);
2470 service_name = NULL;
2472 base_service_from_bus = NULL;
2474 message = pop_message_waiting_for_memory (connection);
2475 if (message == NULL)
2477 _dbus_warn ("Expected a reply to %s, got nothing",
2478 "StartServiceByName");
2484 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2489 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2491 warn_unexpected (connection, message, "reply to StartServiceByName");
2496 activation_result = 0;
2497 if (!dbus_message_get_args (message, &error,
2498 DBUS_TYPE_UINT32, &activation_result,
2501 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2503 _dbus_warn ("Did not have activation result first argument to %s: %s",
2504 "StartServiceByName", error.message);
2508 dbus_error_free (&error);
2512 if (activation_result == DBUS_START_REPLY_SUCCESS)
2514 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2518 _dbus_warn ("Activation result was %u, no good.",
2524 dbus_message_unref (message);
2527 if (!check_no_leftovers (context))
2529 _dbus_warn ("Messages were left over after verifying existent activation results");
2537 dbus_message_unref (message);
2538 dbus_error_free (&error);
2544 check_service_auto_activated (BusContext *context,
2545 DBusConnection *connection,
2546 const char *activated_name,
2547 const char *base_service_name,
2548 DBusMessage *initial_message)
2550 DBusMessage *message;
2556 dbus_error_init (&error);
2558 message = initial_message;
2559 dbus_message_ref (message);
2561 if (dbus_message_is_signal (message,
2562 DBUS_INTERFACE_DBUS,
2563 "NameOwnerChanged"))
2565 const char *service_name;
2566 CheckServiceOwnerChangedData socd;
2568 reget_service_name_arg:
2569 if (!dbus_message_get_args (message, &error,
2570 DBUS_TYPE_STRING, &service_name,
2573 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2575 dbus_error_free (&error);
2576 _dbus_wait_for_memory ();
2577 goto reget_service_name_arg;
2581 _dbus_warn ("Message %s doesn't have a service name: %s",
2584 dbus_error_free (&error);
2589 if (strcmp (service_name, activated_name) != 0)
2591 _dbus_warn ("Expected to see service %s created, saw %s instead",
2592 activated_name, service_name);
2596 socd.expected_kind = SERVICE_CREATED;
2597 socd.expected_service_name = service_name;
2598 socd.failed = FALSE;
2599 socd.skip_connection = connection;
2600 socd.context = context;
2602 bus_test_clients_foreach (check_service_owner_changed_foreach,
2608 /* Note that this differs from regular activation in that we don't get a
2609 * reply to ActivateService here.
2612 dbus_message_unref (message);
2614 service_name = NULL;
2618 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2627 dbus_message_unref (message);
2633 check_service_deactivated (BusContext *context,
2634 DBusConnection *connection,
2635 const char *activated_name,
2636 const char *base_service)
2639 CheckServiceOwnerChangedData socd;
2643 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2644 * service and the activated_name. The base service
2645 * notification is required to come last.
2647 socd.expected_kind = SERVICE_DELETED;
2648 socd.expected_service_name = activated_name;
2649 socd.failed = FALSE;
2650 socd.skip_connection = NULL;
2651 socd.context = context;
2653 bus_test_clients_foreach (check_service_owner_changed_foreach,
2659 socd.expected_kind = SERVICE_DELETED;
2660 socd.expected_service_name = base_service;
2661 socd.failed = FALSE;
2662 socd.skip_connection = NULL;
2663 socd.context = context;
2665 bus_test_clients_foreach (check_service_owner_changed_foreach,
2678 check_send_exit_to_service (BusContext *context,
2679 DBusConnection *connection,
2680 const char *service_name,
2681 const char *base_service)
2683 dbus_bool_t got_error;
2684 DBusMessage *message;
2685 dbus_uint32_t serial;
2688 _dbus_verbose ("Sending exit message to the test service\n");
2692 /* Kill off the test service by sending it a quit message */
2693 message = dbus_message_new_method_call (service_name,
2694 "/org/freedesktop/TestSuite",
2695 "org.freedesktop.TestSuite",
2698 if (message == NULL)
2700 /* Do this again; we still need the service to exit... */
2701 if (!check_send_exit_to_service (context, connection,
2702 service_name, base_service))
2708 if (!dbus_connection_send (connection, message, &serial))
2710 dbus_message_unref (message);
2712 /* Do this again; we still need the service to exit... */
2713 if (!check_send_exit_to_service (context, connection,
2714 service_name, base_service))
2720 dbus_message_unref (message);
2724 bus_test_run_clients_loop (SEND_PENDING (connection));
2726 /* read it in and write it out to test service */
2727 bus_test_run_bus_loop (context, FALSE);
2729 /* see if we got an error during message bus dispatching */
2730 bus_test_run_clients_loop (FALSE);
2731 message = borrow_message_waiting_for_memory (connection);
2732 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2735 dbus_connection_return_message (connection, message);
2741 /* If no error, wait for the test service to exit */
2742 block_connection_until_message_from_bus (context, connection, "test service to exit");
2744 bus_test_run_everything (context);
2749 message = pop_message_waiting_for_memory (connection);
2750 _dbus_assert (message != NULL);
2752 if (dbus_message_get_reply_serial (message) != serial)
2754 warn_unexpected (connection, message,
2755 "error with the correct reply serial");
2759 if (!dbus_message_is_error (message,
2760 DBUS_ERROR_NO_MEMORY))
2762 warn_unexpected (connection, message,
2763 "a no memory error from asking test service to exit");
2767 _dbus_verbose ("Got error %s when asking test service to exit\n",
2768 dbus_message_get_error_name (message));
2770 /* Do this again; we still need the service to exit... */
2771 if (!check_send_exit_to_service (context, connection,
2772 service_name, base_service))
2777 if (!check_service_deactivated (context, connection,
2778 service_name, base_service))
2781 /* Should now have a NoReply error from the Exit() method
2782 * call; it should have come after all the deactivation
2785 message = pop_message_waiting_for_memory (connection);
2787 if (message == NULL)
2789 warn_unexpected (connection, NULL,
2790 "reply to Exit() method call");
2793 if (!dbus_message_is_error (message,
2794 DBUS_ERROR_NO_REPLY))
2796 warn_unexpected (connection, message,
2797 "NoReply error from Exit() method call");
2801 if (dbus_message_get_reply_serial (message) != serial)
2803 warn_unexpected (connection, message,
2804 "error with the correct reply serial");
2808 _dbus_verbose ("Got error %s after test service exited\n",
2809 dbus_message_get_error_name (message));
2811 if (!check_no_leftovers (context))
2813 _dbus_warn ("Messages were left over after %s",
2814 _DBUS_FUNCTION_NAME);
2823 dbus_message_unref (message);
2829 check_got_error (BusContext *context,
2830 DBusConnection *connection,
2831 const char *first_error_name,
2834 DBusMessage *message;
2837 dbus_bool_t error_found;
2838 const char *error_name;
2842 message = pop_message_waiting_for_memory (connection);
2843 if (message == NULL)
2845 _dbus_warn ("Did not get an expected error");
2849 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2851 warn_unexpected (connection, message, "an error");
2856 error_found = FALSE;
2858 va_start (ap, first_error_name);
2859 error_name = first_error_name;
2860 while (error_name != NULL)
2862 if (dbus_message_is_error (message, error_name))
2867 error_name = va_arg (ap, char*);
2873 _dbus_warn ("Expected error %s or other, got %s instead",
2875 dbus_message_get_error_name (message));
2883 dbus_message_unref (message);
2890 GOT_SERVICE_CREATED,
2891 GOT_SERVICE_DELETED,
2896 static GotServiceInfo
2897 check_got_service_info (DBusMessage *message)
2899 GotServiceInfo message_kind;
2901 if (dbus_message_is_signal (message,
2902 DBUS_INTERFACE_DBUS,
2903 "NameOwnerChanged"))
2906 const char *service_name, *old_owner, *new_owner;
2907 dbus_error_init (&error);
2909 reget_service_info_data:
2910 service_name = NULL;
2914 dbus_message_get_args (message, &error,
2915 DBUS_TYPE_STRING, &service_name,
2916 DBUS_TYPE_STRING, &old_owner,
2917 DBUS_TYPE_STRING, &new_owner,
2919 if (dbus_error_is_set (&error))
2921 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2923 dbus_error_free (&error);
2924 goto reget_service_info_data;
2928 _dbus_warn ("unexpected arguments for NameOwnerChanged message");
2929 message_kind = GOT_SOMETHING_ELSE;
2932 else if (!old_owner[0])
2933 message_kind = GOT_SERVICE_CREATED;
2934 else if (!new_owner[0])
2935 message_kind = GOT_SERVICE_DELETED;
2937 message_kind = GOT_SOMETHING_ELSE;
2939 dbus_error_free (&error);
2941 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2942 message_kind = GOT_ERROR;
2944 message_kind = GOT_SOMETHING_ELSE;
2946 return message_kind;
2949 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2951 /* returns TRUE if the correct thing happens,
2952 * but the correct thing may include OOM errors.
2955 check_existent_service_no_auto_start (BusContext *context,
2956 DBusConnection *connection)
2958 DBusMessage *message;
2959 DBusMessage *base_service_message;
2960 const char *base_service;
2961 dbus_uint32_t serial;
2963 const char *existent = EXISTENT_SERVICE_NAME;
2964 dbus_uint32_t flags;
2966 base_service_message = NULL;
2968 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2970 DBUS_INTERFACE_DBUS,
2971 "StartServiceByName");
2973 if (message == NULL)
2976 dbus_message_set_auto_start (message, FALSE);
2979 if (!dbus_message_append_args (message,
2980 DBUS_TYPE_STRING, &existent,
2981 DBUS_TYPE_UINT32, &flags,
2984 dbus_message_unref (message);
2988 if (!dbus_connection_send (connection, message, &serial))
2990 dbus_message_unref (message);
2994 dbus_message_unref (message);
2997 bus_test_run_everything (context);
2999 /* now wait for the message bus to hear back from the activated
3002 block_connection_until_message_from_bus (context, connection, "activated service to connect");
3004 bus_test_run_everything (context);
3006 if (!dbus_connection_get_is_connected (connection))
3008 _dbus_verbose ("connection was disconnected\n");
3014 message = pop_message_waiting_for_memory (connection);
3015 if (message == NULL)
3017 _dbus_warn ("Did not receive any messages after %s %d on %p",
3018 "StartServiceByName", serial, connection);
3022 verbose_message_received (connection, message);
3023 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
3025 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3027 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3029 _dbus_warn ("Message has wrong sender %s",
3030 dbus_message_get_sender (message) ?
3031 dbus_message_get_sender (message) : "(none)");
3035 if (dbus_message_is_error (message,
3036 DBUS_ERROR_NO_MEMORY))
3038 ; /* good, this is a valid response */
3040 else if (dbus_message_is_error (message,
3041 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
3042 dbus_message_is_error (message,
3043 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
3044 dbus_message_is_error (message,
3045 DBUS_ERROR_SPAWN_EXEC_FAILED))
3047 ; /* good, this is expected also */
3051 _dbus_warn ("Did not expect error %s",
3052 dbus_message_get_error_name (message));
3058 GotServiceInfo message_kind;
3060 if (!check_base_service_activated (context, connection,
3061 message, &base_service))
3064 base_service_message = message;
3067 /* We may need to block here for the test service to exit or finish up */
3068 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
3070 message = dbus_connection_borrow_message (connection);
3071 if (message == NULL)
3073 _dbus_warn ("Did not receive any messages after base service creation notification");
3077 message_kind = check_got_service_info (message);
3079 dbus_connection_return_message (connection, message);
3082 switch (message_kind)
3084 case GOT_SOMETHING_ELSE:
3086 _dbus_warn ("Unexpected message after ActivateService "
3087 "(should be an error or a service announcement");
3091 if (!check_got_error (context, connection,
3092 DBUS_ERROR_SPAWN_CHILD_EXITED,
3093 DBUS_ERROR_NO_MEMORY,
3096 /* A service deleted should be coming along now after this error.
3097 * We can also get the error *after* the service deleted.
3102 case GOT_SERVICE_DELETED:
3104 /* The service started up and got a base address, but then
3105 * failed to register under EXISTENT_SERVICE_NAME
3107 CheckServiceOwnerChangedData socd;
3109 socd.expected_kind = SERVICE_DELETED;
3110 socd.expected_service_name = base_service;
3111 socd.failed = FALSE;
3112 socd.skip_connection = NULL;
3113 socd.context = context;
3115 bus_test_clients_foreach (check_service_owner_changed_foreach,
3121 /* Now we should get an error about the service exiting
3122 * if we didn't get it before.
3124 if (message_kind != GOT_ERROR)
3126 block_connection_until_message_from_bus (context, connection, "error about service exiting");
3128 /* and process everything again */
3129 bus_test_run_everything (context);
3131 if (!check_got_error (context, connection,
3132 DBUS_ERROR_SPAWN_CHILD_EXITED,
3133 DBUS_ERROR_NO_MEMORY,
3140 case GOT_SERVICE_CREATED:
3141 message = pop_message_waiting_for_memory (connection);
3142 if (message == NULL)
3144 _dbus_warn ("Failed to pop message we just put back! "
3145 "should have been a NameOwnerChanged (creation)");
3149 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
3150 base_service, message))
3153 dbus_message_unref (message);
3156 if (!check_no_leftovers (context))
3158 _dbus_warn ("Messages were left over after successful activation");
3162 if (!check_send_exit_to_service (context, connection,
3163 EXISTENT_SERVICE_NAME, base_service))
3174 dbus_message_unref (message);
3176 if (base_service_message)
3177 dbus_message_unref (base_service_message);
3182 /* returns TRUE if the correct thing happens,
3183 * but the correct thing may include OOM errors.
3186 check_segfault_service_no_auto_start (BusContext *context,
3187 DBusConnection *connection)
3189 DBusMessage *message;
3190 dbus_uint32_t serial;
3192 const char *segv_service;
3193 dbus_uint32_t flags;
3195 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
3197 DBUS_INTERFACE_DBUS,
3198 "StartServiceByName");
3200 if (message == NULL)
3203 dbus_message_set_auto_start (message, FALSE);
3205 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
3207 if (!dbus_message_append_args (message,
3208 DBUS_TYPE_STRING, &segv_service,
3209 DBUS_TYPE_UINT32, &flags,
3212 dbus_message_unref (message);
3216 if (!dbus_connection_send (connection, message, &serial))
3218 dbus_message_unref (message);
3222 dbus_message_unref (message);
3225 bus_test_run_everything (context);
3226 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
3227 bus_test_run_everything (context);
3229 if (!dbus_connection_get_is_connected (connection))
3231 _dbus_verbose ("connection was disconnected\n");
3237 message = pop_message_waiting_for_memory (connection);
3238 if (message == NULL)
3240 _dbus_warn ("Did not receive a reply to %s %d on %p",
3241 "StartServiceByName", serial, connection);
3245 verbose_message_received (connection, message);
3247 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3249 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3251 _dbus_warn ("Message has wrong sender %s",
3252 dbus_message_get_sender (message) ?
3253 dbus_message_get_sender (message) : "(none)");
3257 if (dbus_message_is_error (message,
3258 DBUS_ERROR_NO_MEMORY))
3260 ; /* good, this is a valid response */
3262 else if (dbus_message_is_error (message,
3265 const char *servicehelper;
3266 servicehelper = bus_context_get_servicehelper (context);
3267 /* make sure this only happens with the launch helper */
3268 _dbus_assert (servicehelper != NULL);
3271 else if (dbus_message_is_error (message,
3272 DBUS_ERROR_SPAWN_CHILD_EXITED))
3274 /* unhandled exceptions are normal exit codes */
3277 else if (dbus_message_is_error (message,
3278 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3280 ; /* good, this is expected also */
3285 warn_unexpected (connection, message, "not this error");
3292 _dbus_warn ("Did not expect to successfully activate segfault service");
3300 dbus_message_unref (message);
3306 /* returns TRUE if the correct thing happens,
3307 * but the correct thing may include OOM errors.
3310 check_segfault_service_auto_start (BusContext *context,
3311 DBusConnection *connection)
3313 DBusMessage *message;
3314 dbus_uint32_t serial;
3317 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
3318 "/org/freedesktop/TestSuite",
3319 "org.freedesktop.TestSuite",
3322 if (message == NULL)
3325 if (!dbus_connection_send (connection, message, &serial))
3327 dbus_message_unref (message);
3331 dbus_message_unref (message);
3334 bus_test_run_everything (context);
3335 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
3336 bus_test_run_everything (context);
3338 if (!dbus_connection_get_is_connected (connection))
3340 _dbus_verbose ("connection was disconnected\n");
3346 message = pop_message_waiting_for_memory (connection);
3347 if (message == NULL)
3349 _dbus_warn ("Did not receive a reply to %s %d on %p",
3350 "Echo message (auto activation)", serial, connection);
3354 verbose_message_received (connection, message);
3356 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3358 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3360 _dbus_warn ("Message has wrong sender %s",
3361 dbus_message_get_sender (message) ?
3362 dbus_message_get_sender (message) : "(none)");
3366 if (dbus_message_is_error (message,
3367 DBUS_ERROR_NO_MEMORY))
3369 ; /* good, this is a valid response */
3372 else if (dbus_message_is_error (message,
3373 DBUS_ERROR_SPAWN_CHILD_EXITED))
3375 /* unhandled exceptions are normal exit codes */
3378 else if (dbus_message_is_error (message,
3379 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3381 ; /* good, this is expected also */
3386 warn_unexpected (connection, message, "not this error");
3393 _dbus_warn ("Did not expect to successfully activate segfault service");
3401 dbus_message_unref (message);
3406 #define TEST_ECHO_MESSAGE "Test echo message"
3407 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
3409 /* returns TRUE if the correct thing happens,
3410 * but the correct thing may include OOM errors.
3413 check_existent_hello_from_self (BusContext *context,
3414 DBusConnection *connection)
3416 DBusMessage *message;
3417 dbus_uint32_t serial;
3420 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3421 "/org/freedesktop/TestSuite",
3422 "org.freedesktop.TestSuite",
3423 "RunHelloFromSelf");
3425 if (message == NULL)
3428 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
3429 if (!dbus_message_append_args (message,
3430 DBUS_TYPE_STRING, &text,
3433 dbus_message_unref (message);
3437 if (!dbus_connection_send (connection, message, &serial))
3439 dbus_message_unref (message);
3443 dbus_message_unref (message);
3446 bus_test_run_everything (context);
3448 /* Note: if this test is run in OOM mode, it will block when the bus
3449 * doesn't send a reply due to OOM.
3451 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
3453 message = pop_message_waiting_for_memory (connection);
3454 if (message == NULL)
3456 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message");
3460 if (dbus_message_get_reply_serial (message) != serial)
3462 _dbus_warn ("Wrong reply serial");
3463 dbus_message_unref (message);
3467 dbus_message_unref (message);
3473 /* returns TRUE if the correct thing happens,
3474 * but the correct thing may include OOM errors.
3477 check_existent_ping (BusContext *context,
3478 DBusConnection *connection)
3480 DBusMessage *message;
3481 dbus_uint32_t serial;
3482 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3483 "/org/freedesktop/TestSuite",
3484 "org.freedesktop.DBus.Peer",
3487 if (message == NULL)
3490 if (!dbus_connection_send (connection, message, &serial))
3492 dbus_message_unref (message);
3496 dbus_message_unref (message);
3499 bus_test_run_everything (context);
3501 /* Note: if this test is run in OOM mode, it will block when the bus
3502 * doesn't send a reply due to OOM.
3504 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
3506 message = pop_message_waiting_for_memory (connection);
3507 if (message == NULL)
3509 _dbus_warn ("Failed to pop message! Should have been reply from Ping message");
3513 if (dbus_message_get_reply_serial (message) != serial)
3515 _dbus_warn ("Wrong reply serial");
3516 dbus_message_unref (message);
3520 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3522 _dbus_warn ("Unexpected message return during Ping");
3523 dbus_message_unref (message);
3527 dbus_message_unref (message);
3533 /* returns TRUE if the correct thing happens,
3534 * but the correct thing may include OOM errors.
3537 check_existent_get_machine_id (BusContext *context,
3538 DBusConnection *connection)
3540 DBusError error = DBUS_ERROR_INIT;
3541 DBusMessage *message;
3542 dbus_uint32_t serial;
3544 const char *machine_id;
3546 if (!_dbus_read_local_machine_uuid (&uuid, FALSE, &error))
3548 /* Unable to test further: either we ran out of memory, or neither
3549 * dbus nor systemd was ever correctly installed on this machine */
3550 _dbus_verbose ("Machine UUID not available: %s", error.message);
3551 dbus_error_free (&error);
3555 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3556 "/org/freedesktop/TestSuite",
3557 "org.freedesktop.DBus.Peer",
3560 if (message == NULL)
3563 if (!dbus_connection_send (connection, message, &serial))
3565 dbus_message_unref (message);
3569 dbus_message_unref (message);
3572 bus_test_run_everything (context);
3574 /* Note: if this test is run in OOM mode, it will block when the bus
3575 * doesn't send a reply due to OOM.
3577 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3579 message = pop_message_waiting_for_memory (connection);
3580 if (message == NULL)
3582 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message");
3586 if (dbus_message_get_reply_serial (message) != serial)
3588 _dbus_warn ("Wrong reply serial");
3589 dbus_message_unref (message);
3593 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3595 _dbus_warn ("Unexpected message return during GetMachineId");
3596 dbus_message_unref (message);
3601 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3603 _dbus_warn ("Did not get a machine ID in reply to GetMachineId");
3604 dbus_message_unref (message);
3608 if (machine_id == NULL || strlen (machine_id) != 32)
3610 _dbus_warn ("Machine id looks bogus: '%s'", machine_id ? machine_id : "null");
3611 dbus_message_unref (message);
3615 /* We can't check that the machine id is correct because during make check it is
3616 * just made up for each process separately
3619 dbus_message_unref (message);
3625 /* returns TRUE if the correct thing happens,
3626 * but the correct thing may include OOM errors.
3629 check_existent_service_auto_start (BusContext *context,
3630 DBusConnection *connection)
3632 DBusMessage *message;
3633 DBusMessage *base_service_message;
3634 dbus_uint32_t serial;
3636 const char *base_service;
3639 base_service_message = NULL;
3641 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3642 "/org/freedesktop/TestSuite",
3643 "org.freedesktop.TestSuite",
3646 if (message == NULL)
3649 text = TEST_ECHO_MESSAGE;
3650 if (!dbus_message_append_args (message,
3651 DBUS_TYPE_STRING, &text,
3654 dbus_message_unref (message);
3658 if (!dbus_connection_send (connection, message, &serial))
3660 dbus_message_unref (message);
3664 dbus_message_unref (message);
3667 bus_test_run_everything (context);
3669 /* now wait for the message bus to hear back from the activated
3672 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3673 bus_test_run_everything (context);
3675 if (!dbus_connection_get_is_connected (connection))
3677 _dbus_verbose ("connection was disconnected\n");
3683 message = pop_message_waiting_for_memory (connection);
3684 if (message == NULL)
3686 _dbus_warn ("Did not receive any messages after auto start %d on %p",
3687 serial, connection);
3691 verbose_message_received (connection, message);
3692 _dbus_verbose (" (after sending %s)\n", "auto start");
3694 /* we should get zero or two ServiceOwnerChanged signals */
3695 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3697 GotServiceInfo message_kind;
3699 if (!check_base_service_activated (context, connection,
3700 message, &base_service))
3703 base_service_message = message;
3706 /* We may need to block here for the test service to exit or finish up */
3707 block_connection_until_message_from_bus (context, connection, "service to exit");
3709 /* Should get a service creation notification for the activated
3710 * service name, or a service deletion on the base service name
3712 message = dbus_connection_borrow_message (connection);
3713 if (message == NULL)
3715 _dbus_warn ("No message after auto activation "
3716 "(should be a service announcement)");
3717 dbus_connection_return_message (connection, message);
3722 message_kind = check_got_service_info (message);
3724 dbus_connection_return_message (connection, message);
3727 switch (message_kind)
3729 case GOT_SERVICE_CREATED:
3730 message = pop_message_waiting_for_memory (connection);
3731 if (message == NULL)
3733 _dbus_warn ("Failed to pop message we just put back! "
3734 "should have been a NameOwnerChanged (creation)");
3738 /* Check that ServiceOwnerChanged (creation) was correctly received */
3739 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3740 base_service, message))
3743 dbus_message_unref (message);
3748 case GOT_SERVICE_DELETED:
3750 /* The service started up and got a base address, but then
3751 * failed to register under EXISTENT_SERVICE_NAME
3753 CheckServiceOwnerChangedData socd;
3755 socd.expected_kind = SERVICE_DELETED;
3756 socd.expected_service_name = base_service;
3757 socd.failed = FALSE;
3758 socd.skip_connection = NULL;
3759 socd.context = context;
3761 bus_test_clients_foreach (check_service_owner_changed_foreach,
3771 case GOT_SOMETHING_ELSE:
3773 _dbus_warn ("Unexpected message after auto activation");
3778 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3779 * come the method reply (or error) from the initial method call
3782 /* Note: if this test is run in OOM mode, it will block when the bus
3783 * doesn't send a reply due to OOM.
3785 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3787 message = pop_message_waiting_for_memory (connection);
3788 if (message == NULL)
3790 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
3794 if (dbus_message_get_reply_serial (message) != serial)
3796 _dbus_warn ("Wrong reply serial");
3800 dbus_message_unref (message);
3803 if (!check_existent_ping (context, connection))
3806 if (!check_existent_get_machine_id (context, connection))
3809 if (!check_existent_hello_from_self (context, connection))
3812 if (!check_send_exit_to_service (context, connection,
3813 EXISTENT_SERVICE_NAME,
3821 dbus_message_unref (message);
3823 if (base_service_message)
3824 dbus_message_unref (base_service_message);
3829 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3831 /* returns TRUE if the correct thing happens,
3832 * but the correct thing may include OOM errors.
3835 check_launch_service_file_missing (BusContext *context,
3836 DBusConnection *connection)
3838 DBusMessage *message;
3839 dbus_uint32_t serial;
3842 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3843 "/org/freedesktop/TestSuite",
3844 "org.freedesktop.TestSuite",
3847 if (message == NULL)
3850 if (!dbus_connection_send (connection, message, &serial))
3852 dbus_message_unref (message);
3856 dbus_message_unref (message);
3859 bus_test_run_everything (context);
3860 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3861 bus_test_run_everything (context);
3863 if (!dbus_connection_get_is_connected (connection))
3865 _dbus_verbose ("connection was disconnected\n");
3871 message = pop_message_waiting_for_memory (connection);
3872 if (message == NULL)
3874 _dbus_warn ("Did not receive a reply to %s %d on %p",
3875 "Echo message (auto activation)", serial, connection);
3879 verbose_message_received (connection, message);
3881 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3883 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3885 _dbus_warn ("Message has wrong sender %s",
3886 dbus_message_get_sender (message) ?
3887 dbus_message_get_sender (message) : "(none)");
3891 if (dbus_message_is_error (message,
3892 DBUS_ERROR_NO_MEMORY))
3894 ; /* good, this is a valid response */
3896 else if (dbus_message_is_error (message,
3897 DBUS_ERROR_SERVICE_UNKNOWN))
3899 _dbus_verbose("got service unknown\n");
3900 ; /* good, this is expected (only valid when using launch helper) */
3904 warn_unexpected (connection, message, "not this error");
3911 _dbus_warn ("Did not expect to successfully auto-start missing service");
3919 dbus_message_unref (message);
3926 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3928 /* returns TRUE if the correct thing happens,
3929 * but the correct thing may include OOM errors.
3932 check_launch_service_user_missing (BusContext *context,
3933 DBusConnection *connection)
3935 DBusMessage *message;
3936 dbus_uint32_t serial;
3939 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3940 "/org/freedesktop/TestSuite",
3941 "org.freedesktop.TestSuite",
3944 if (message == NULL)
3947 if (!dbus_connection_send (connection, message, &serial))
3949 dbus_message_unref (message);
3953 dbus_message_unref (message);
3956 bus_test_run_everything (context);
3957 block_connection_until_message_from_bus (context, connection,
3958 "reply to service which should fail to auto-start (missing User)");
3959 bus_test_run_everything (context);
3961 if (!dbus_connection_get_is_connected (connection))
3963 _dbus_warn ("connection was disconnected");
3969 message = pop_message_waiting_for_memory (connection);
3970 if (message == NULL)
3972 _dbus_warn ("Did not receive a reply to %s %d on %p",
3973 "Echo message (auto activation)", serial, connection);
3977 verbose_message_received (connection, message);
3979 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3981 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3983 _dbus_warn ("Message has wrong sender %s",
3984 dbus_message_get_sender (message) ?
3985 dbus_message_get_sender (message) : "(none)");
3989 if (dbus_message_is_error (message,
3990 DBUS_ERROR_NO_MEMORY))
3992 ; /* good, this is a valid response */
3994 else if (dbus_message_is_error (message,
3995 DBUS_ERROR_SPAWN_FILE_INVALID))
3997 _dbus_verbose("got service file invalid\n");
3998 ; /* good, this is expected (only valid when using launch helper) */
4002 warn_unexpected (connection, message, "not this error");
4009 _dbus_warn ("Did not expect to successfully auto-start missing service");
4017 dbus_message_unref (message);
4022 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
4024 /* returns TRUE if the correct thing happens,
4025 * but the correct thing may include OOM errors.
4028 check_launch_service_exec_missing (BusContext *context,
4029 DBusConnection *connection)
4031 DBusMessage *message;
4032 dbus_uint32_t serial;
4035 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
4036 "/org/freedesktop/TestSuite",
4037 "org.freedesktop.TestSuite",
4040 if (message == NULL)
4043 if (!dbus_connection_send (connection, message, &serial))
4045 dbus_message_unref (message);
4049 dbus_message_unref (message);
4052 bus_test_run_everything (context);
4053 block_connection_until_message_from_bus (context, connection,
4054 "reply to service which should fail to auto-start (missing Exec)");
4055 bus_test_run_everything (context);
4057 if (!dbus_connection_get_is_connected (connection))
4059 _dbus_warn ("connection was disconnected");
4065 message = pop_message_waiting_for_memory (connection);
4066 if (message == NULL)
4068 _dbus_warn ("Did not receive a reply to %s %d on %p",
4069 "Echo message (auto activation)", serial, connection);
4073 verbose_message_received (connection, message);
4075 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4077 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4079 _dbus_warn ("Message has wrong sender %s",
4080 dbus_message_get_sender (message) ?
4081 dbus_message_get_sender (message) : "(none)");
4085 if (dbus_message_is_error (message,
4086 DBUS_ERROR_NO_MEMORY))
4088 ; /* good, this is a valid response */
4090 else if (dbus_message_is_error (message,
4091 DBUS_ERROR_SERVICE_UNKNOWN))
4093 _dbus_verbose("could not activate as invalid service file was not added\n");
4094 ; /* good, this is expected as we shouldn't have been added to
4095 * the activation list with a missing Exec key */
4097 else if (dbus_message_is_error (message,
4098 DBUS_ERROR_SPAWN_FILE_INVALID))
4100 _dbus_verbose("got service file invalid\n");
4101 ; /* good, this is allowed, and is the message passed back from the
4106 warn_unexpected (connection, message, "not this error");
4113 _dbus_warn ("Did not expect to successfully auto-start missing service");
4121 dbus_message_unref (message);
4126 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
4128 /* returns TRUE if the correct thing happens,
4129 * but the correct thing may include OOM errors.
4132 check_launch_service_service_missing (BusContext *context,
4133 DBusConnection *connection)
4135 DBusMessage *message;
4136 dbus_uint32_t serial;
4139 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
4140 "/org/freedesktop/TestSuite",
4141 "org.freedesktop.TestSuite",
4144 if (message == NULL)
4147 if (!dbus_connection_send (connection, message, &serial))
4149 dbus_message_unref (message);
4153 dbus_message_unref (message);
4156 bus_test_run_everything (context);
4157 block_connection_until_message_from_bus (context, connection,
4158 "reply to service which should fail to auto-start (missing Service)");
4159 bus_test_run_everything (context);
4161 if (!dbus_connection_get_is_connected (connection))
4163 _dbus_warn ("connection was disconnected");
4169 message = pop_message_waiting_for_memory (connection);
4170 if (message == NULL)
4172 _dbus_warn ("Did not receive a reply to %s %d on %p",
4173 "Echo message (auto activation)", serial, connection);
4177 verbose_message_received (connection, message);
4179 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4181 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4183 _dbus_warn ("Message has wrong sender %s",
4184 dbus_message_get_sender (message) ?
4185 dbus_message_get_sender (message) : "(none)");
4189 if (dbus_message_is_error (message,
4190 DBUS_ERROR_NO_MEMORY))
4192 ; /* good, this is a valid response */
4194 else if (dbus_message_is_error (message,
4195 DBUS_ERROR_SERVICE_UNKNOWN))
4197 _dbus_verbose("could not activate as invalid service file was not added\n");
4198 ; /* good, this is expected as we shouldn't have been added to
4199 * the activation list with a missing Exec key */
4201 else if (dbus_message_is_error (message,
4202 DBUS_ERROR_SPAWN_FILE_INVALID))
4204 _dbus_verbose("got service file invalid\n");
4205 ; /* good, this is allowed, and is the message passed back from the
4210 warn_unexpected (connection, message, "not this error");
4217 _dbus_warn ("Did not expect to successfully auto-start missing service");
4225 dbus_message_unref (message);
4231 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
4233 /* returns TRUE if the correct thing happens,
4234 * but the correct thing may include OOM errors.
4237 check_shell_fail_service_auto_start (BusContext *context,
4238 DBusConnection *connection)
4240 DBusMessage *message;
4241 dbus_uint32_t serial;
4244 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
4245 "/org/freedesktop/TestSuite",
4246 "org.freedesktop.TestSuite",
4249 if (message == NULL)
4252 if (!dbus_connection_send (connection, message, &serial))
4254 dbus_message_unref (message);
4258 dbus_message_unref (message);
4261 bus_test_run_everything (context);
4262 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
4263 bus_test_run_everything (context);
4265 if (!dbus_connection_get_is_connected (connection))
4267 _dbus_verbose ("connection was disconnected\n");
4273 message = pop_message_waiting_for_memory (connection);
4274 if (message == NULL)
4276 _dbus_warn ("Did not receive a reply to %s %d on %p",
4277 "Echo message (auto activation)", serial, connection);
4281 verbose_message_received (connection, message);
4283 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4285 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4287 _dbus_warn ("Message has wrong sender %s",
4288 dbus_message_get_sender (message) ?
4289 dbus_message_get_sender (message) : "(none)");
4293 if (dbus_message_is_error (message,
4294 DBUS_ERROR_NO_MEMORY))
4296 ; /* good, this is a valid response */
4298 else if (dbus_message_is_error (message,
4299 DBUS_ERROR_INVALID_ARGS))
4301 _dbus_verbose("got invalid args\n");
4302 ; /* good, this is expected also */
4306 warn_unexpected (connection, message, "not this error");
4313 _dbus_warn ("Did not expect to successfully auto-start shell fail service");
4321 dbus_message_unref (message);
4326 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
4328 /* returns TRUE if the correct thing happens,
4329 * but the correct thing may include OOM errors.
4332 check_shell_service_success_auto_start (BusContext *context,
4333 DBusConnection *connection)
4335 DBusMessage *message;
4336 DBusMessage *base_service_message;
4337 dbus_uint32_t serial;
4339 const char *base_service;
4340 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4342 base_service_message = NULL;
4344 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
4345 "/org/freedesktop/TestSuite",
4346 "org.freedesktop.TestSuite",
4349 if (message == NULL)
4352 if (!dbus_connection_send (connection, message, &serial))
4354 dbus_message_unref (message);
4358 dbus_message_unref (message);
4361 bus_test_run_everything (context);
4363 /* now wait for the message bus to hear back from the activated
4366 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
4367 bus_test_run_everything (context);
4369 if (!dbus_connection_get_is_connected (connection))
4371 _dbus_verbose ("connection was disconnected\n");
4377 message = pop_message_waiting_for_memory (connection);
4378 if (message == NULL)
4380 _dbus_warn ("Did not receive any messages after auto start %d on %p",
4381 serial, connection);
4385 verbose_message_received (connection, message);
4386 _dbus_verbose (" (after sending %s)\n", "auto start");
4388 /* we should get zero or two ServiceOwnerChanged signals */
4389 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
4391 GotServiceInfo message_kind;
4393 if (!check_base_service_activated (context, connection,
4394 message, &base_service))
4397 base_service_message = message;
4400 /* We may need to block here for the test service to exit or finish up */
4401 block_connection_until_message_from_bus (context, connection, "service to exit");
4403 /* Should get a service creation notification for the activated
4404 * service name, or a service deletion on the base service name
4406 message = dbus_connection_borrow_message (connection);
4407 if (message == NULL)
4409 _dbus_warn ("No message after auto activation "
4410 "(should be a service announcement)");
4411 dbus_connection_return_message (connection, message);
4416 message_kind = check_got_service_info (message);
4418 dbus_connection_return_message (connection, message);
4421 switch (message_kind)
4423 case GOT_SERVICE_CREATED:
4424 message = pop_message_waiting_for_memory (connection);
4425 if (message == NULL)
4427 _dbus_warn ("Failed to pop message we just put back! "
4428 "should have been a NameOwnerChanged (creation)");
4432 /* Check that ServiceOwnerChanged (creation) was correctly received */
4433 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
4434 base_service, message))
4437 dbus_message_unref (message);
4442 case GOT_SERVICE_DELETED:
4444 /* The service started up and got a base address, but then
4445 * failed to register under SHELL_SUCCESS_SERVICE_NAME
4447 CheckServiceOwnerChangedData socd;
4449 socd.expected_kind = SERVICE_DELETED;
4450 socd.expected_service_name = base_service;
4451 socd.failed = FALSE;
4452 socd.skip_connection = NULL;
4453 socd.context = context;
4455 bus_test_clients_foreach (check_service_owner_changed_foreach,
4465 case GOT_SOMETHING_ELSE:
4467 _dbus_warn ("Unexpected message after auto activation");
4472 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
4473 * come the method reply (or error) from the initial method call
4476 /* Note: if this test is run in OOM mode, it will block when the bus
4477 * doesn't send a reply due to OOM.
4479 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
4481 message = pop_message_waiting_for_memory (connection);
4482 if (message == NULL)
4484 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
4488 if (dbus_message_get_reply_serial (message) != serial)
4490 _dbus_warn ("Wrong reply serial");
4494 if (!dbus_message_get_args (message, NULL,
4495 DBUS_TYPE_STRING, &argv[0],
4496 DBUS_TYPE_STRING, &argv[1],
4497 DBUS_TYPE_STRING, &argv[2],
4498 DBUS_TYPE_STRING, &argv[3],
4499 DBUS_TYPE_STRING, &argv[4],
4500 DBUS_TYPE_STRING, &argv[5],
4501 DBUS_TYPE_STRING, &argv[6],
4504 _dbus_warn ("Error getting arguments from return");
4508 /* don't worry about arg[0] as it may be different
4509 depending on the path to the tests
4511 if (strcmp("-test", argv[1]) != 0)
4513 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)",
4518 if (strcmp("that", argv[2]) != 0)
4520 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)",
4525 if (strcmp("we get", argv[3]) != 0)
4527 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)",
4532 if (strcmp("back", argv[4]) != 0)
4534 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)",
4539 if (strcmp("--what", argv[5]) != 0)
4541 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)",
4546 if (strcmp("we put in", argv[6]) != 0)
4548 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)",
4549 "we put in", argv[6]);
4553 dbus_message_unref (message);
4556 if (!check_send_exit_to_service (context, connection,
4557 SHELL_SUCCESS_SERVICE_NAME,
4565 dbus_message_unref (message);
4567 if (base_service_message)
4568 dbus_message_unref (base_service_message);
4576 BusContext *context;
4580 check_oom_check1_func (void *data)
4582 Check1Data *d = data;
4584 if (! (* d->func) (d->context))
4587 if (!check_no_leftovers (d->context))
4589 _dbus_warn ("Messages were left over, should be covered by test suite");
4597 check1_try_iterations (BusContext *context,
4598 const char *description,
4604 d.context = context;
4606 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4608 _dbus_assert_not_reached ("test failed");
4612 check_get_services (BusContext *context,
4613 DBusConnection *connection,
4618 DBusMessage *message;
4619 dbus_uint32_t serial;
4626 dbus_error_init (&error);
4629 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4631 DBUS_INTERFACE_DBUS,
4634 if (message == NULL)
4637 if (!dbus_connection_send (connection, message, &serial))
4639 dbus_message_unref (message);
4643 /* send our message */
4644 bus_test_run_clients_loop (SEND_PENDING (connection));
4646 dbus_message_unref (message);
4649 dbus_connection_ref (connection); /* because we may get disconnected */
4650 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4652 if (!dbus_connection_get_is_connected (connection))
4654 _dbus_verbose ("connection was disconnected\n");
4656 dbus_connection_unref (connection);
4661 dbus_connection_unref (connection);
4663 message = pop_message_waiting_for_memory (connection);
4664 if (message == NULL)
4666 _dbus_warn ("Did not receive a reply to %s %d on %p",
4667 method, serial, connection);
4671 verbose_message_received (connection, message);
4673 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4675 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4677 ; /* good, this is a valid response */
4681 warn_unexpected (connection, message, "not this error");
4688 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4690 ; /* good, expected */
4694 warn_unexpected (connection, message,
4695 "method_return for ListActivatableNames/ListNames");
4702 if (!dbus_message_get_args (message, &error,
4708 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4710 _dbus_verbose ("no memory to list services by %s\n", method);
4711 dbus_error_free (&error);
4712 _dbus_wait_for_memory ();
4713 goto retry_get_property;
4717 _dbus_assert (dbus_error_is_set (&error));
4718 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s", method);
4727 if (!check_no_leftovers (context))
4733 dbus_error_free (&error);
4736 dbus_message_unref (message);
4741 /* returns TRUE if the correct thing happens,
4742 * but the correct thing may include OOM errors.
4745 check_list_services (BusContext *context,
4746 DBusConnection *connection)
4748 DBusMessage *message;
4749 DBusMessage *base_service_message;
4750 const char *base_service;
4751 dbus_uint32_t serial;
4753 const char *existent = EXISTENT_SERVICE_NAME;
4754 dbus_uint32_t flags;
4758 _dbus_verbose ("check_list_services for %p\n", connection);
4760 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4765 if (!_dbus_string_array_contains ((const char **)services, existent))
4767 _dbus_warn ("Did not get the expected %s from ListActivatableNames", existent);
4768 dbus_free_string_array (services);
4772 dbus_free_string_array (services);
4774 base_service_message = NULL;
4776 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4778 DBUS_INTERFACE_DBUS,
4779 "StartServiceByName");
4781 if (message == NULL)
4784 dbus_message_set_auto_start (message, FALSE);
4787 if (!dbus_message_append_args (message,
4788 DBUS_TYPE_STRING, &existent,
4789 DBUS_TYPE_UINT32, &flags,
4792 dbus_message_unref (message);
4796 if (!dbus_connection_send (connection, message, &serial))
4798 dbus_message_unref (message);
4802 dbus_message_unref (message);
4805 bus_test_run_everything (context);
4807 /* now wait for the message bus to hear back from the activated
4810 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4812 bus_test_run_everything (context);
4814 if (!dbus_connection_get_is_connected (connection))
4816 _dbus_verbose ("connection was disconnected\n");
4822 message = pop_message_waiting_for_memory (connection);
4823 if (message == NULL)
4825 _dbus_warn ("Did not receive any messages after %s %d on %p",
4826 "StartServiceByName", serial, connection);
4830 verbose_message_received (connection, message);
4831 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4833 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4835 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4837 _dbus_warn ("Message has wrong sender %s",
4838 dbus_message_get_sender (message) ?
4839 dbus_message_get_sender (message) : "(none)");
4843 if (dbus_message_is_error (message,
4844 DBUS_ERROR_NO_MEMORY))
4846 ; /* good, this is a valid response */
4848 else if (dbus_message_is_error (message,
4849 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4850 dbus_message_is_error (message,
4851 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4852 dbus_message_is_error (message,
4853 DBUS_ERROR_SPAWN_EXEC_FAILED))
4855 ; /* good, this is expected also */
4859 _dbus_warn ("Did not expect error %s",
4860 dbus_message_get_error_name (message));
4866 GotServiceInfo message_kind;
4868 if (!check_base_service_activated (context, connection,
4869 message, &base_service))
4872 base_service_message = message;
4875 /* We may need to block here for the test service to exit or finish up */
4876 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4878 message = dbus_connection_borrow_message (connection);
4879 if (message == NULL)
4881 _dbus_warn ("Did not receive any messages after base service creation notification");
4885 message_kind = check_got_service_info (message);
4887 dbus_connection_return_message (connection, message);
4890 switch (message_kind)
4892 case GOT_SOMETHING_ELSE:
4894 case GOT_SERVICE_DELETED:
4896 _dbus_warn ("Unexpected message after ActivateService "
4897 "(should be an error or a service announcement)");
4900 case GOT_SERVICE_CREATED:
4901 message = pop_message_waiting_for_memory (connection);
4902 if (message == NULL)
4904 _dbus_warn ("Failed to pop message we just put back! "
4905 "should have been a NameOwnerChanged (creation)");
4909 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4910 base_service, message))
4913 dbus_message_unref (message);
4916 if (!check_no_leftovers (context))
4918 _dbus_warn ("Messages were left over after successful activation");
4926 if (!check_get_services (context, connection, "ListNames", &services, &len))
4931 if (!_dbus_string_array_contains ((const char **)services, existent))
4933 _dbus_warn ("Did not get the expected %s from ListNames", existent);
4937 dbus_free_string_array (services);
4939 if (!check_send_exit_to_service (context, connection,
4940 EXISTENT_SERVICE_NAME, base_service))
4947 dbus_message_unref (message);
4949 if (base_service_message)
4950 dbus_message_unref (base_service_message);
4958 BusContext *context;
4959 DBusConnection *connection;
4963 check_oom_check2_func (void *data)
4965 Check2Data *d = data;
4967 if (! (* d->func) (d->context, d->connection))
4970 if (!check_no_leftovers (d->context))
4972 _dbus_warn ("Messages were left over, should be covered by test suite");
4980 check2_try_iterations (BusContext *context,
4981 DBusConnection *connection,
4982 const char *description,
4988 d.context = context;
4989 d.connection = connection;
4991 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4994 _dbus_warn ("%s failed during oom", description);
4995 _dbus_assert_not_reached ("test failed");
5000 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
5001 const char *filename)
5006 if (!_dbus_string_init (&full))
5009 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
5011 _dbus_string_free (&full);
5015 _dbus_string_init_const (&file, filename);
5017 if (!_dbus_concat_dir_and_file (&full, &file))
5019 _dbus_string_free (&full);
5023 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
5024 _dbus_string_get_const_data (&full));
5026 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
5028 _dbus_string_free (&full);
5034 bus_dispatch_test_conf (const DBusString *test_data_dir,
5035 const char *filename,
5036 dbus_bool_t use_launcher)
5038 BusContext *context;
5039 DBusConnection *foo;
5040 DBusConnection *bar;
5041 DBusConnection *baz;
5044 /* save the config name for the activation helper */
5045 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5046 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5048 dbus_error_init (&error);
5050 context = bus_context_new_test (test_data_dir, filename);
5051 if (context == NULL)
5054 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5056 _dbus_assert_not_reached ("could not alloc connection");
5058 if (!bus_setup_debug_client (foo))
5059 _dbus_assert_not_reached ("could not set up connection");
5061 spin_connection_until_authenticated (context, foo);
5063 if (!check_hello_message (context, foo))
5064 _dbus_assert_not_reached ("hello message failed");
5066 if (!check_double_hello_message (context, foo))
5067 _dbus_assert_not_reached ("double hello message failed");
5069 if (!check_add_match (context, foo, ""))
5070 _dbus_assert_not_reached ("AddMatch message failed");
5072 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5074 _dbus_assert_not_reached ("could not alloc connection");
5076 if (!bus_setup_debug_client (bar))
5077 _dbus_assert_not_reached ("could not set up connection");
5079 spin_connection_until_authenticated (context, bar);
5081 if (!check_hello_message (context, bar))
5082 _dbus_assert_not_reached ("hello message failed");
5084 if (!check_add_match (context, bar, ""))
5085 _dbus_assert_not_reached ("AddMatch message failed");
5087 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5089 _dbus_assert_not_reached ("could not alloc connection");
5091 if (!bus_setup_debug_client (baz))
5092 _dbus_assert_not_reached ("could not set up connection");
5094 spin_connection_until_authenticated (context, baz);
5096 if (!check_hello_message (context, baz))
5097 _dbus_assert_not_reached ("hello message failed");
5099 if (!check_add_match (context, baz, ""))
5100 _dbus_assert_not_reached ("AddMatch message failed");
5102 if (!check_add_match (context, baz, "interface='com.example'"))
5103 _dbus_assert_not_reached ("AddMatch message failed");
5105 #ifdef DBUS_ENABLE_STATS
5106 if (!check_get_all_match_rules (context, baz))
5107 _dbus_assert_not_reached ("GetAllMatchRules message failed");
5110 if (!check_get_connection_unix_user (context, baz))
5111 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
5113 #ifdef DBUS_WIN_FIXME
5114 _dbus_verbose("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
5116 if (!check_get_connection_unix_process_id (context, baz))
5117 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
5120 if (!check_list_services (context, baz))
5121 _dbus_assert_not_reached ("ListActivatableNames message failed");
5123 if (!check_no_leftovers (context))
5125 _dbus_warn ("Messages were left over after setting up initial connections");
5126 _dbus_assert_not_reached ("initial connection setup failed");
5129 check1_try_iterations (context, "create_and_hello",
5130 check_hello_connection);
5132 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
5133 check_nonexistent_service_no_auto_start);
5135 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
5136 check_segfault_service_no_auto_start);
5138 check2_try_iterations (context, foo, "existent_service_no_auto_start",
5139 check_existent_service_no_auto_start);
5141 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
5142 check_nonexistent_service_auto_start);
5144 /* only do the segfault test if we are not using the launcher */
5145 check2_try_iterations (context, foo, "segfault_service_auto_start",
5146 check_segfault_service_auto_start);
5148 /* only do the shell fail test if we are not using the launcher */
5149 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
5150 check_shell_fail_service_auto_start);
5152 /* specific to launcher */
5154 if (!check_launch_service_file_missing (context, foo))
5155 _dbus_assert_not_reached ("did not get service file not found error");
5158 /* Note: need to resolve some issues with the testing code in order to run
5159 * this in oom (handle that we sometimes don't get replies back from the bus
5160 * when oom happens, without blocking the test).
5162 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
5163 check_existent_service_auto_start);
5166 if (!check_existent_service_auto_start (context, foo))
5167 _dbus_assert_not_reached ("existent service auto start failed");
5169 if (!check_shell_service_success_auto_start (context, foo))
5170 _dbus_assert_not_reached ("shell success service auto start failed");
5172 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
5174 kill_client_connection_unchecked (foo);
5175 kill_client_connection_unchecked (bar);
5176 kill_client_connection_unchecked (baz);
5178 bus_context_unref (context);
5185 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
5186 const char *filename)
5188 BusContext *context;
5189 DBusConnection *foo;
5192 /* save the config name for the activation helper */
5193 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5194 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5196 dbus_error_init (&error);
5198 context = bus_context_new_test (test_data_dir, filename);
5199 if (context == NULL)
5202 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5204 _dbus_assert_not_reached ("could not alloc connection");
5206 if (!bus_setup_debug_client (foo))
5207 _dbus_assert_not_reached ("could not set up connection");
5209 spin_connection_until_authenticated (context, foo);
5211 if (!check_hello_message (context, foo))
5212 _dbus_assert_not_reached ("hello message failed");
5214 if (!check_double_hello_message (context, foo))
5215 _dbus_assert_not_reached ("double hello message failed");
5217 if (!check_add_match (context, foo, ""))
5218 _dbus_assert_not_reached ("AddMatch message failed");
5220 /* this only tests the activation.c user check */
5221 if (!check_launch_service_user_missing (context, foo))
5222 _dbus_assert_not_reached ("user missing did not trigger error");
5224 /* this only tests the desktop.c exec check */
5225 if (!check_launch_service_exec_missing (context, foo))
5226 _dbus_assert_not_reached ("exec missing did not trigger error");
5228 /* this only tests the desktop.c service check */
5229 if (!check_launch_service_service_missing (context, foo))
5230 _dbus_assert_not_reached ("service missing did not trigger error");
5232 _dbus_verbose ("Disconnecting foo\n");
5234 kill_client_connection_unchecked (foo);
5236 bus_context_unref (context);
5243 DBusTimeout *timeout;
5244 DBusConnection *connection;
5245 dbus_bool_t timedout;
5249 static BusTestCheckData *cdata;
5252 bus_dispatch_test_check_timeout (void *data)
5254 _dbus_verbose ("timeout triggered - pretend that privilege check result is available\n");
5256 /* should only happen once during the test */
5257 _dbus_assert (!cdata->timedout);
5258 cdata->timedout = TRUE;
5259 _dbus_connection_enable_dispatch (cdata->connection);
5261 /* don't call this again */
5262 _dbus_loop_remove_timeout (bus_connection_get_loop (cdata->connection),
5264 dbus_connection_unref (cdata->connection);
5265 cdata->connection = NULL;
5270 bus_dispatch_test_check_override (DBusConnection *connection,
5271 const char *privilege)
5273 _dbus_verbose ("overriding privilege check %s #%d\n", privilege, cdata->check_counter);
5274 cdata->check_counter++;
5275 if (!cdata->timedout)
5279 /* Should be the first privilege check for the "Echo" method. */
5280 _dbus_assert (cdata->check_counter == 1);
5281 cdata->timeout = _dbus_timeout_new (1, bus_dispatch_test_check_timeout,
5283 _dbus_assert (cdata->timeout);
5284 added = _dbus_loop_add_timeout (bus_connection_get_loop (connection),
5286 _dbus_assert (added);
5287 cdata->connection = connection;
5288 dbus_connection_ref (connection);
5289 _dbus_connection_disable_dispatch (connection);
5290 return BUS_RESULT_LATER;
5294 /* Should only be checked one more time, and this time succeeds. */
5295 _dbus_assert (cdata->check_counter == 2);
5296 return BUS_RESULT_TRUE;
5301 bus_dispatch_test_check (const DBusString *test_data_dir)
5303 const char *filename = "valid-config-files/debug-check-some.conf";
5304 BusContext *context;
5305 DBusConnection *foo;
5307 dbus_bool_t result = TRUE;
5308 BusTestCheckData data;
5310 /* save the config name for the activation helper */
5311 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5312 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5314 dbus_error_init (&error);
5316 context = bus_context_new_test (test_data_dir, filename);
5317 if (context == NULL)
5320 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5322 _dbus_assert_not_reached ("could not alloc connection");
5324 if (!bus_setup_debug_client (foo))
5325 _dbus_assert_not_reached ("could not set up connection");
5327 spin_connection_until_authenticated (context, foo);
5329 if (!check_hello_message (context, foo))
5330 _dbus_assert_not_reached ("hello message failed");
5332 if (!check_double_hello_message (context, foo))
5333 _dbus_assert_not_reached ("double hello message failed");
5335 if (!check_add_match_all (context, foo))
5336 _dbus_assert_not_reached ("AddMatch message failed");
5339 * Cause bus_check_send_privilege() to return BUS_RESULT_LATER in the
5340 * first call, then BUS_RESULT_TRUE.
5343 memset (cdata, 0, sizeof(*cdata));
5344 bus_check_test_override = bus_dispatch_test_check_override;
5346 result = check_existent_service_auto_start (context, foo);
5348 _dbus_assert (cdata->check_counter == 2);
5349 _dbus_assert (cdata->timedout);
5350 _dbus_assert (cdata->timeout);
5351 _dbus_assert (!cdata->connection);
5352 _dbus_timeout_unref (cdata->timeout);
5354 kill_client_connection_unchecked (foo);
5356 bus_context_unref (context);
5362 bus_dispatch_test (const DBusString *test_data_dir)
5364 _dbus_verbose ("<check> tests\n");
5365 if (!bus_dispatch_test_check (test_data_dir))
5368 /* run normal activation tests */
5369 _dbus_verbose ("Normal activation tests\n");
5370 if (!bus_dispatch_test_conf (test_data_dir,
5371 "valid-config-files/debug-allow-all.conf", FALSE))
5375 /* run launch-helper activation tests */
5376 _dbus_verbose ("Launch helper activation tests\n");
5377 if (!bus_dispatch_test_conf (test_data_dir,
5378 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
5381 /* run select launch-helper activation tests on broken service files */
5382 if (!bus_dispatch_test_conf_fail (test_data_dir,
5383 "valid-config-files-system/debug-allow-all-fail.conf"))
5391 bus_dispatch_sha1_test (const DBusString *test_data_dir)
5393 BusContext *context;
5394 DBusConnection *foo;
5397 dbus_error_init (&error);
5399 /* Test SHA1 authentication */
5400 _dbus_verbose ("Testing SHA1 context\n");
5402 context = bus_context_new_test (test_data_dir,
5403 "valid-config-files/debug-allow-all-sha1.conf");
5404 if (context == NULL)
5407 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5409 _dbus_assert_not_reached ("could not alloc connection");
5411 if (!bus_setup_debug_client (foo))
5412 _dbus_assert_not_reached ("could not set up connection");
5414 spin_connection_until_authenticated (context, foo);
5416 if (!check_hello_message (context, foo))
5417 _dbus_assert_not_reached ("hello message failed");
5419 if (!check_add_match (context, foo, ""))
5420 _dbus_assert_not_reached ("addmatch message failed");
5422 if (!check_no_leftovers (context))
5424 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection");
5425 _dbus_assert_not_reached ("initial connection setup failed");
5428 check1_try_iterations (context, "create_and_hello_sha1",
5429 check_hello_connection);
5431 kill_client_connection_unchecked (foo);
5433 bus_context_unref (context);
5438 #ifdef HAVE_UNIX_FD_PASSING
5441 bus_unix_fds_passing_test(const DBusString *test_data_dir)
5443 BusContext *context;
5444 DBusConnection *foo, *bar;
5447 DBusSocket one[2], two[2];
5451 dbus_error_init (&error);
5453 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
5454 if (context == NULL)
5455 _dbus_assert_not_reached ("could not alloc context");
5457 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5459 _dbus_assert_not_reached ("could not alloc connection");
5461 if (!bus_setup_debug_client (foo))
5462 _dbus_assert_not_reached ("could not set up connection");
5464 spin_connection_until_authenticated (context, foo);
5466 if (!check_hello_message (context, foo))
5467 _dbus_assert_not_reached ("hello message failed");
5469 if (!check_add_match (context, foo, ""))
5470 _dbus_assert_not_reached ("AddMatch message failed");
5472 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5474 _dbus_assert_not_reached ("could not alloc connection");
5476 if (!bus_setup_debug_client (bar))
5477 _dbus_assert_not_reached ("could not set up connection");
5479 spin_connection_until_authenticated (context, bar);
5481 if (!check_hello_message (context, bar))
5482 _dbus_assert_not_reached ("hello message failed");
5484 if (!check_add_match (context, bar, ""))
5485 _dbus_assert_not_reached ("AddMatch message failed");
5487 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
5488 _dbus_assert_not_reached ("could not alloc message");
5490 if (!(_dbus_socketpair (one, one+1, TRUE, &error)))
5491 _dbus_assert_not_reached("Failed to allocate pipe #1");
5493 if (!(_dbus_socketpair (two, two+1, TRUE, &error)))
5494 _dbus_assert_not_reached("Failed to allocate pipe #2");
5496 if (!dbus_message_append_args(m,
5497 DBUS_TYPE_UNIX_FD, one,
5498 DBUS_TYPE_UNIX_FD, two,
5499 DBUS_TYPE_UNIX_FD, two,
5501 _dbus_assert_not_reached("Failed to attach fds.");
5503 if (!_dbus_close_socket (one[0], &error))
5504 _dbus_assert_not_reached("Failed to close pipe #1 ");
5505 if (!_dbus_close_socket (two[0], &error))
5506 _dbus_assert_not_reached("Failed to close pipe #2 ");
5508 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
5509 _dbus_assert_not_reached("Connection cannot do fd passing");
5511 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
5512 _dbus_assert_not_reached("Connection cannot do fd passing");
5514 if (!dbus_connection_send (foo, m, NULL))
5515 _dbus_assert_not_reached("Failed to send fds");
5517 dbus_message_unref(m);
5519 bus_test_run_clients_loop (SEND_PENDING (foo));
5521 bus_test_run_everything (context);
5523 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
5525 if (!(m = pop_message_waiting_for_memory (foo)))
5526 _dbus_assert_not_reached("Failed to receive msg");
5528 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5529 _dbus_assert_not_reached("bogus message received");
5531 dbus_message_unref(m);
5533 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
5535 if (!(m = pop_message_waiting_for_memory (bar)))
5536 _dbus_assert_not_reached("Failed to receive msg");
5538 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5539 _dbus_assert_not_reached("bogus message received");
5541 if (!dbus_message_get_args(m,
5543 DBUS_TYPE_UNIX_FD, &x,
5544 DBUS_TYPE_UNIX_FD, &y,
5545 DBUS_TYPE_UNIX_FD, &z,
5547 _dbus_assert_not_reached("Failed to parse fds.");
5549 dbus_message_unref(m);
5551 if (write(x, "X", 1) != 1)
5552 _dbus_assert_not_reached("Failed to write to pipe #1");
5553 if (write(y, "Y", 1) != 1)
5554 _dbus_assert_not_reached("Failed to write to pipe #2");
5555 if (write(z, "Z", 1) != 1)
5556 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
5558 if (!_dbus_close(x, &error))
5559 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
5560 if (!_dbus_close(y, &error))
5561 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
5562 if (!_dbus_close(z, &error))
5563 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
5565 if (read(one[1].fd, &r, 1) != 1 || r != 'X')
5566 _dbus_assert_not_reached("Failed to read value from pipe.");
5567 if (read(two[1].fd, &r, 1) != 1 || r != 'Y')
5568 _dbus_assert_not_reached("Failed to read value from pipe.");
5569 if (read(two[1].fd, &r, 1) != 1 || r != 'Z')
5570 _dbus_assert_not_reached("Failed to read value from pipe.");
5572 if (!_dbus_close_socket (one[1], &error))
5573 _dbus_assert_not_reached("Failed to close pipe #1 ");
5574 if (!_dbus_close_socket (two[1], &error))
5575 _dbus_assert_not_reached("Failed to close pipe #2 ");
5577 _dbus_verbose ("Disconnecting foo\n");
5578 kill_client_connection_unchecked (foo);
5580 _dbus_verbose ("Disconnecting bar\n");
5581 kill_client_connection_unchecked (bar);
5583 bus_context_unref (context);
5589 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */