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
28 #include "connection.h"
31 #include "activation.h"
36 #include <dbus/dbus-internals.h>
37 #include <dbus/dbus-misc.h>
40 #ifdef HAVE_UNIX_FD_PASSING
41 #include <dbus/dbus-sysdeps-unix.h>
45 /* This is hard-coded in the files in valid-config-files-*. We have to use
46 * the debug-pipe transport because the tests in this file require that
47 * dbus_connection_open_private() does not block. */
48 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
50 static inline const char *
51 nonnull (const char *maybe_null,
54 return (maybe_null ? maybe_null : if_null);
58 send_one_message (DBusConnection *connection,
60 DBusConnection *sender,
61 DBusConnection *addressed_recipient,
63 BusTransaction *transaction,
66 DBusError stack_error = DBUS_ERROR_INIT;
68 if (!bus_context_check_security_policy (context, transaction,
76 if (!bus_transaction_capture_error_reply (transaction, sender,
77 &stack_error, message))
79 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
80 "broadcast rejected, but not enough "
81 "memory to tell monitors");
84 dbus_error_free (&stack_error);
85 return TRUE; /* don't send it but don't return an error either */
88 if (dbus_message_contains_unix_fds(message) &&
89 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
91 dbus_set_error (&stack_error, DBUS_ERROR_NOT_SUPPORTED,
92 "broadcast cannot be delivered to %s (%s) because "
93 "it does not support receiving Unix fds",
94 bus_connection_get_name (connection),
95 bus_connection_get_loginfo (connection));
97 if (!bus_transaction_capture_error_reply (transaction, sender,
98 &stack_error, message))
100 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
101 "broadcast with Unix fd not delivered, but not "
102 "enough memory to tell monitors");
105 dbus_error_free (&stack_error);
106 return TRUE; /* don't send it but don't return an error either */
109 if (!bus_transaction_send (transaction,
121 bus_dispatch_matches (BusTransaction *transaction,
122 DBusConnection *sender,
123 DBusConnection *addressed_recipient,
124 DBusMessage *message,
128 BusConnections *connections;
129 DBusList *recipients;
130 BusMatchmaker *matchmaker;
134 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
136 /* sender and recipient can both be NULL for the bus driver,
137 * or for signals with no particular recipient
140 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
141 _dbus_assert (dbus_message_get_sender (message) != NULL);
143 context = bus_transaction_get_context (transaction);
145 /* First, send the message to the addressed_recipient, if there is one. */
146 if (addressed_recipient != NULL)
148 if (!bus_context_check_security_policy (context, transaction,
149 sender, addressed_recipient,
151 message, NULL, error))
154 if (dbus_message_contains_unix_fds (message) &&
155 !dbus_connection_can_send_type (addressed_recipient,
158 dbus_set_error (error,
159 DBUS_ERROR_NOT_SUPPORTED,
160 "Tried to send message with Unix file descriptors"
161 "to a client that doesn't support that.");
165 /* Dispatch the message */
166 if (!bus_transaction_send (transaction, addressed_recipient, message))
173 /* Now dispatch to others who look interested in this message */
174 connections = bus_context_get_connections (context);
175 dbus_error_init (&tmp_error);
176 matchmaker = bus_context_get_matchmaker (context);
179 if (!bus_matchmaker_get_recipients (matchmaker, connections,
180 sender, addressed_recipient, message,
187 link = _dbus_list_get_first_link (&recipients);
190 DBusConnection *dest;
194 if (!send_one_message (dest, context, sender, addressed_recipient,
195 message, transaction, &tmp_error))
198 link = _dbus_list_get_next_link (&recipients, link);
201 _dbus_list_clear (&recipients);
203 if (dbus_error_is_set (&tmp_error))
205 dbus_move_error (&tmp_error, error);
212 static DBusHandlerResult
213 bus_dispatch (DBusConnection *connection,
214 DBusMessage *message)
216 const char *sender, *service_name;
218 BusTransaction *transaction;
220 DBusHandlerResult result;
221 DBusConnection *addressed_recipient;
223 result = DBUS_HANDLER_RESULT_HANDLED;
226 addressed_recipient = NULL;
227 dbus_error_init (&error);
229 context = bus_connection_get_context (connection);
230 _dbus_assert (context != NULL);
232 /* If we can't even allocate an OOM error, we just go to sleep
235 while (!bus_connection_preallocate_oom_error (connection))
236 _dbus_wait_for_memory ();
238 /* Ref connection in case we disconnect it at some point in here */
239 dbus_connection_ref (connection);
241 /* Monitors aren't meant to send messages to us. */
242 if (bus_connection_is_monitor (connection))
244 sender = bus_connection_get_name (connection);
246 /* should never happen */
248 sender = "(unknown)";
250 if (dbus_message_is_signal (message,
251 DBUS_INTERFACE_LOCAL,
254 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
255 "Monitoring connection %s closed.", sender);
256 bus_connection_disconnected (connection);
261 /* Monitors are not allowed to send messages, because that
262 * probably indicates that the monitor is incorrectly replying
263 * to its eavesdropped messages, and we want the authors of
264 * such monitors to fix them.
266 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
267 "Monitoring connection %s (%s) is not allowed "
268 "to send messages; closing it. Please fix the "
269 "monitor to not do that. "
270 "(message type=\"%s\" interface=\"%s\" "
271 "member=\"%s\" error name=\"%s\" "
272 "destination=\"%s\")",
273 sender, bus_connection_get_loginfo (connection),
274 dbus_message_type_to_string (
275 dbus_message_get_type (message)),
276 nonnull (dbus_message_get_interface (message),
278 nonnull (dbus_message_get_member (message),
280 nonnull (dbus_message_get_error_name (message),
282 nonnull (dbus_message_get_destination (message),
284 dbus_connection_close (connection);
289 service_name = dbus_message_get_destination (message);
291 #ifdef DBUS_ENABLE_VERBOSE_MODE
293 const char *interface_name, *member_name, *error_name;
295 interface_name = dbus_message_get_interface (message);
296 member_name = dbus_message_get_member (message);
297 error_name = dbus_message_get_error_name (message);
299 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
300 interface_name ? interface_name : "(no interface)",
301 member_name ? member_name : "(no member)",
302 error_name ? error_name : "(no error name)",
303 service_name ? service_name : "peer");
305 #endif /* DBUS_ENABLE_VERBOSE_MODE */
307 /* If service_name is NULL, if it's a signal we send it to all
308 * connections with a match rule. If it's not a signal, there
309 * are some special cases here but mostly we just bail out.
311 if (service_name == NULL)
313 if (dbus_message_is_signal (message,
314 DBUS_INTERFACE_LOCAL,
317 bus_connection_disconnected (connection);
321 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
323 /* DBusConnection also handles some of these automatically, we leave
326 * FIXME: this means monitors won't get the opportunity to see
327 * non-signals with NULL destination, or their replies (which in
328 * practice are UnknownMethod errors)
330 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
335 /* Create our transaction */
336 transaction = bus_transaction_new (context);
337 if (transaction == NULL)
339 BUS_SET_OOM (&error);
343 /* Assign a sender to the message */
344 if (bus_connection_is_active (connection))
346 sender = bus_connection_get_name (connection);
347 _dbus_assert (sender != NULL);
349 if (!dbus_message_set_sender (message, sender))
351 BUS_SET_OOM (&error);
357 /* For monitors' benefit: we don't want the sender to be able to
358 * trick the monitor by supplying a forged sender, and we also
359 * don't want the message to have no sender at all. */
360 if (!dbus_message_set_sender (message, ":not.active.yet"))
362 BUS_SET_OOM (&error);
367 /* We need to refetch the service name here, because
368 * dbus_message_set_sender can cause the header to be
369 * reallocated, and thus the service_name pointer will become
372 service_name = dbus_message_get_destination (message);
375 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
377 if (!bus_transaction_capture (transaction, connection, NULL, message))
379 BUS_SET_OOM (&error);
383 if (!bus_context_check_security_policy (context, transaction,
384 connection, NULL, NULL, message,
387 _dbus_verbose ("Security policy rejected message\n");
391 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
392 if (!bus_driver_handle_message (connection, transaction, message, &error))
395 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
397 if (!bus_transaction_capture (transaction, connection, NULL, message))
399 BUS_SET_OOM (&error);
403 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
404 dbus_connection_close (connection);
407 else if (service_name != NULL) /* route to named service */
409 DBusString service_string;
411 BusRegistry *registry;
413 _dbus_assert (service_name != NULL);
415 registry = bus_connection_get_registry (connection);
417 _dbus_string_init_const (&service_string, service_name);
418 service = bus_registry_lookup (registry, &service_string);
420 if (service == NULL && dbus_message_get_auto_start (message))
422 BusActivation *activation;
424 if (!bus_transaction_capture (transaction, connection, NULL,
427 BUS_SET_OOM (&error);
431 activation = bus_connection_get_activation (connection);
433 /* This will do as much of a security policy check as it can.
434 * We can't do the full security policy check here, since the
435 * addressed recipient service doesn't exist yet. We do it before
436 * sending the message after the service has been created.
438 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
439 message, service_name, &error))
441 _DBUS_ASSERT_ERROR_IS_SET (&error);
442 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
448 else if (service == NULL)
450 if (!bus_transaction_capture (transaction, connection,
453 BUS_SET_OOM (&error);
457 dbus_set_error (&error,
458 DBUS_ERROR_NAME_HAS_NO_OWNER,
459 "Name \"%s\" does not exist",
465 addressed_recipient = bus_service_get_primary_owners_connection (service);
466 _dbus_assert (addressed_recipient != NULL);
468 if (!bus_transaction_capture (transaction, connection,
469 addressed_recipient, message))
471 BUS_SET_OOM (&error);
476 else /* service_name == NULL */
478 if (!bus_transaction_capture (transaction, connection, NULL, message))
480 BUS_SET_OOM (&error);
485 /* Now send the message to its destination (or not, if
486 * addressed_recipient == NULL), and match it against other connections'
489 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
493 if (dbus_error_is_set (&error))
495 /* Even if we disconnected it, pretend to send it any pending error
496 * messages so that monitors can observe them.
498 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
500 bus_connection_send_oom_error (connection, message);
502 /* cancel transaction due to OOM */
503 if (transaction != NULL)
505 bus_transaction_cancel_and_free (transaction);
511 /* Try to send the real error, if no mem to do that, send
514 _dbus_assert (transaction != NULL);
515 if (!bus_transaction_send_error_reply (transaction, connection,
518 bus_connection_send_oom_error (connection, message);
520 /* cancel transaction due to OOM */
521 if (transaction != NULL)
523 bus_transaction_cancel_and_free (transaction);
530 dbus_error_free (&error);
533 if (transaction != NULL)
535 bus_transaction_execute_and_free (transaction);
538 dbus_connection_unref (connection);
543 static DBusHandlerResult
544 bus_dispatch_message_filter (DBusConnection *connection,
545 DBusMessage *message,
548 return bus_dispatch (connection, message);
552 bus_dispatch_add_connection (DBusConnection *connection)
554 if (!dbus_connection_add_filter (connection,
555 bus_dispatch_message_filter,
563 bus_dispatch_remove_connection (DBusConnection *connection)
565 /* Here we tell the bus driver that we want to get off. */
566 bus_driver_remove_connection (connection);
568 dbus_connection_remove_filter (connection,
569 bus_dispatch_message_filter,
573 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
579 /* This is used to know whether we need to block in order to finish
580 * sending a message, or whether the initial dbus_connection_send()
581 * already flushed the queue.
583 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
585 typedef dbus_bool_t (* Check1Func) (BusContext *context);
586 typedef dbus_bool_t (* Check2Func) (BusContext *context,
587 DBusConnection *connection);
589 static dbus_bool_t check_no_leftovers (BusContext *context);
592 block_connection_until_message_from_bus (BusContext *context,
593 DBusConnection *connection,
594 const char *what_is_expected)
596 _dbus_verbose ("expecting: %s\n", what_is_expected);
598 while (dbus_connection_get_dispatch_status (connection) ==
599 DBUS_DISPATCH_COMPLETE &&
600 dbus_connection_get_is_connected (connection))
602 bus_test_run_bus_loop (context, TRUE);
603 bus_test_run_clients_loop (FALSE);
608 spin_connection_until_authenticated (BusContext *context,
609 DBusConnection *connection)
611 _dbus_verbose ("Spinning to auth connection %p\n", connection);
612 while (!dbus_connection_get_is_authenticated (connection) &&
613 dbus_connection_get_is_connected (connection))
615 bus_test_run_bus_loop (context, FALSE);
616 bus_test_run_clients_loop (FALSE);
618 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
621 /* compensate for fact that pop_message() can return #NULL due to OOM */
623 pop_message_waiting_for_memory (DBusConnection *connection)
625 while (dbus_connection_get_dispatch_status (connection) ==
626 DBUS_DISPATCH_NEED_MEMORY)
627 _dbus_wait_for_memory ();
629 return dbus_connection_pop_message (connection);
633 borrow_message_waiting_for_memory (DBusConnection *connection)
635 while (dbus_connection_get_dispatch_status (connection) ==
636 DBUS_DISPATCH_NEED_MEMORY)
637 _dbus_wait_for_memory ();
639 return dbus_connection_borrow_message (connection);
643 warn_unexpected_real (DBusConnection *connection,
644 DBusMessage *message,
645 const char *expected,
646 const char *function,
650 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s",
652 dbus_message_get_interface (message) ?
653 dbus_message_get_interface (message) : "(unset)",
654 dbus_message_get_member (message) ?
655 dbus_message_get_member (message) : "(unset)",
656 dbus_message_get_error_name (message) ?
657 dbus_message_get_error_name (message) : "(unset)",
661 _dbus_warn ("%s:%d received no message on %p, expecting %s",
662 function, line, connection, expected);
665 #define warn_unexpected(connection, message, expected) \
666 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
669 verbose_message_received (DBusConnection *connection,
670 DBusMessage *message)
672 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
673 dbus_message_get_interface (message) ?
674 dbus_message_get_interface (message) : "(unset)",
675 dbus_message_get_member (message) ?
676 dbus_message_get_member (message) : "(unset)",
677 dbus_message_get_error_name (message) ?
678 dbus_message_get_error_name (message) : "(unset)",
691 ServiceInfoKind expected_kind;
692 const char *expected_service_name;
694 DBusConnection *skip_connection;
696 } CheckServiceOwnerChangedData;
699 check_service_owner_changed_foreach (DBusConnection *connection,
702 CheckServiceOwnerChangedData *d = data;
703 DBusMessage *message;
705 const char *service_name, *old_owner, *new_owner;
707 if (d->expected_kind == SERVICE_CREATED
708 && connection == d->skip_connection)
711 dbus_error_init (&error);
714 message = pop_message_waiting_for_memory (connection);
717 block_connection_until_message_from_bus (d->context, connection, "NameOwnerChanged");
718 message = pop_message_waiting_for_memory (connection);
721 _dbus_warn ("Did not receive a message on %p, expecting %s",
722 connection, "NameOwnerChanged");
726 else if (!dbus_message_is_signal (message,
730 warn_unexpected (connection, message, "NameOwnerChanged");
736 reget_service_info_data:
741 dbus_message_get_args (message, &error,
742 DBUS_TYPE_STRING, &service_name,
743 DBUS_TYPE_STRING, &old_owner,
744 DBUS_TYPE_STRING, &new_owner,
747 if (dbus_error_is_set (&error))
749 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
751 dbus_error_free (&error);
752 _dbus_wait_for_memory ();
753 goto reget_service_info_data;
757 _dbus_warn ("Did not get the expected arguments");
762 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
763 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
764 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
766 _dbus_warn ("inconsistent NameOwnerChanged arguments");
770 if (strcmp (service_name, d->expected_service_name) != 0)
772 _dbus_warn ("expected info on service %s, got info on %s",
773 d->expected_service_name,
778 if (*service_name == ':' && new_owner[0]
779 && strcmp (service_name, new_owner) != 0)
781 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])",
782 service_name, old_owner, new_owner);
790 dbus_error_free (&error);
793 dbus_message_unref (message);
800 kill_client_connection (BusContext *context,
801 DBusConnection *connection)
805 CheckServiceOwnerChangedData socd;
807 _dbus_verbose ("killing connection %p\n", connection);
809 s = dbus_bus_get_unique_name (connection);
810 _dbus_assert (s != NULL);
812 while ((base_service = _dbus_strdup (s)) == NULL)
813 _dbus_wait_for_memory ();
815 dbus_connection_ref (connection);
817 /* kick in the disconnect handler that unrefs the connection */
818 dbus_connection_close (connection);
820 bus_test_run_everything (context);
822 _dbus_assert (bus_test_client_listed (connection));
824 /* Run disconnect handler in test.c */
825 if (bus_connection_dispatch_one_message (connection))
826 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
828 _dbus_assert (!dbus_connection_get_is_connected (connection));
829 dbus_connection_unref (connection);
831 _dbus_assert (!bus_test_client_listed (connection));
833 socd.expected_kind = SERVICE_DELETED;
834 socd.expected_service_name = base_service;
836 socd.skip_connection = NULL;
837 socd.context = context;
839 bus_test_clients_foreach (check_service_owner_changed_foreach,
842 dbus_free (base_service);
845 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
847 if (!check_no_leftovers (context))
848 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
852 kill_client_connection_unchecked (DBusConnection *connection)
854 /* This kills the connection without expecting it to affect
855 * the rest of the bus.
857 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
859 dbus_connection_ref (connection);
860 dbus_connection_close (connection);
861 /* dispatching disconnect handler will unref once */
862 if (bus_connection_dispatch_one_message (connection))
863 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
865 _dbus_assert (!bus_test_client_listed (connection));
866 dbus_connection_unref (connection);
872 } CheckNoMessagesData;
875 check_no_messages_foreach (DBusConnection *connection,
878 CheckNoMessagesData *d = data;
879 DBusMessage *message;
881 message = pop_message_waiting_for_memory (connection);
884 warn_unexpected (connection, message, "no messages");
890 dbus_message_unref (message);
895 check_no_leftovers (BusContext *context)
897 CheckNoMessagesData nmd;
900 bus_test_clients_foreach (check_no_messages_foreach,
905 _dbus_verbose ("leftover message found\n");
912 /* returns TRUE if the correct thing happens,
913 * but the correct thing may include OOM errors.
916 check_hello_message (BusContext *context,
917 DBusConnection *connection)
919 DBusMessage *message;
920 DBusMessage *name_message;
921 dbus_uint32_t serial;
925 const char *acquired;
928 dbus_error_init (&error);
934 _dbus_verbose ("check_hello_message for %p\n", connection);
936 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
944 dbus_connection_ref (connection); /* because we may get disconnected */
946 if (!dbus_connection_send (connection, message, &serial))
948 dbus_message_unref (message);
949 dbus_connection_unref (connection);
953 _dbus_assert (dbus_message_has_signature (message, ""));
955 dbus_message_unref (message);
958 if (!dbus_connection_get_is_connected (connection))
960 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
962 dbus_connection_unref (connection);
967 /* send our message */
968 bus_test_run_clients_loop (SEND_PENDING (connection));
970 if (!dbus_connection_get_is_connected (connection))
972 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
974 dbus_connection_unref (connection);
979 block_connection_until_message_from_bus (context, connection, "reply to Hello");
981 if (!dbus_connection_get_is_connected (connection))
983 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
985 dbus_connection_unref (connection);
990 message = pop_message_waiting_for_memory (connection);
993 _dbus_warn ("Did not receive a reply to %s %d on %p",
994 "Hello", serial, connection);
998 verbose_message_received (connection, message);
1000 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1002 _dbus_warn ("Message has wrong sender %s",
1003 dbus_message_get_sender (message) ?
1004 dbus_message_get_sender (message) : "(none)");
1008 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1010 if (dbus_message_is_error (message,
1011 DBUS_ERROR_NO_MEMORY))
1013 ; /* good, this is a valid response */
1017 warn_unexpected (connection, message, "not this error");
1024 CheckServiceOwnerChangedData socd;
1026 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1028 ; /* good, expected */
1032 warn_unexpected (connection, message, "method return for Hello");
1037 retry_get_hello_name:
1038 if (!dbus_message_get_args (message, &error,
1039 DBUS_TYPE_STRING, &name,
1042 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1044 _dbus_verbose ("no memory to get service name arg from hello\n");
1045 dbus_error_free (&error);
1046 _dbus_wait_for_memory ();
1047 goto retry_get_hello_name;
1051 _dbus_assert (dbus_error_is_set (&error));
1052 _dbus_warn ("Did not get the expected single string argument to hello");
1057 _dbus_verbose ("Got hello name: %s\n", name);
1059 while (!dbus_bus_set_unique_name (connection, name))
1060 _dbus_wait_for_memory ();
1062 socd.expected_kind = SERVICE_CREATED;
1063 socd.expected_service_name = name;
1064 socd.failed = FALSE;
1065 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
1066 socd.context = context;
1068 bus_test_clients_foreach (check_service_owner_changed_foreach,
1074 name_message = message;
1075 /* Client should also have gotten ServiceAcquired */
1077 message = pop_message_waiting_for_memory (connection);
1078 if (message == NULL)
1080 block_connection_until_message_from_bus (context, connection, "signal NameAcquired");
1081 message = pop_message_waiting_for_memory (connection);
1082 if (message == NULL)
1084 _dbus_warn ("Expecting %s, got nothing",
1089 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
1092 _dbus_warn ("Expecting %s, got smthg else",
1097 retry_get_acquired_name:
1098 if (!dbus_message_get_args (message, &error,
1099 DBUS_TYPE_STRING, &acquired,
1102 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1104 _dbus_verbose ("no memory to get service name arg from acquired\n");
1105 dbus_error_free (&error);
1106 _dbus_wait_for_memory ();
1107 goto retry_get_acquired_name;
1111 _dbus_assert (dbus_error_is_set (&error));
1112 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired");
1117 _dbus_verbose ("Got acquired name: %s\n", acquired);
1119 if (strcmp (acquired, name) != 0)
1121 _dbus_warn ("Acquired name is %s but expected %s",
1128 if (!check_no_leftovers (context))
1134 _dbus_verbose ("ending - retval = %d\n", retval);
1136 dbus_error_free (&error);
1139 dbus_message_unref (message);
1142 dbus_message_unref (name_message);
1144 dbus_connection_unref (connection);
1149 /* returns TRUE if the correct thing happens,
1150 * but the correct thing may include OOM errors.
1153 check_double_hello_message (BusContext *context,
1154 DBusConnection *connection)
1156 DBusMessage *message;
1157 dbus_uint32_t serial;
1162 dbus_error_init (&error);
1165 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1167 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1169 DBUS_INTERFACE_DBUS,
1172 if (message == NULL)
1175 if (!dbus_connection_send (connection, message, &serial))
1177 dbus_message_unref (message);
1181 dbus_message_unref (message);
1184 /* send our message */
1185 bus_test_run_clients_loop (SEND_PENDING (connection));
1187 dbus_connection_ref (connection); /* because we may get disconnected */
1188 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1190 if (!dbus_connection_get_is_connected (connection))
1192 _dbus_verbose ("connection was disconnected\n");
1194 dbus_connection_unref (connection);
1199 dbus_connection_unref (connection);
1201 message = pop_message_waiting_for_memory (connection);
1202 if (message == NULL)
1204 _dbus_warn ("Did not receive a reply to %s %d on %p",
1205 "Hello", serial, connection);
1209 verbose_message_received (connection, message);
1211 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1213 _dbus_warn ("Message has wrong sender %s",
1214 dbus_message_get_sender (message) ?
1215 dbus_message_get_sender (message) : "(none)");
1219 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1221 warn_unexpected (connection, message, "method return for Hello");
1225 if (!check_no_leftovers (context))
1231 dbus_error_free (&error);
1234 dbus_message_unref (message);
1239 /* returns TRUE if the correct thing happens,
1240 * but the correct thing may include OOM errors.
1243 check_get_connection_unix_user (BusContext *context,
1244 DBusConnection *connection)
1246 DBusMessage *message;
1247 dbus_uint32_t serial;
1250 const char *base_service_name;
1254 dbus_error_init (&error);
1257 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1259 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1261 DBUS_INTERFACE_DBUS,
1262 "GetConnectionUnixUser");
1264 if (message == NULL)
1267 base_service_name = dbus_bus_get_unique_name (connection);
1269 if (!dbus_message_append_args (message,
1270 DBUS_TYPE_STRING, &base_service_name,
1273 dbus_message_unref (message);
1277 if (!dbus_connection_send (connection, message, &serial))
1279 dbus_message_unref (message);
1283 /* send our message */
1284 bus_test_run_clients_loop (SEND_PENDING (connection));
1286 dbus_message_unref (message);
1289 dbus_connection_ref (connection); /* because we may get disconnected */
1290 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1292 if (!dbus_connection_get_is_connected (connection))
1294 _dbus_verbose ("connection was disconnected\n");
1296 dbus_connection_unref (connection);
1301 dbus_connection_unref (connection);
1303 message = pop_message_waiting_for_memory (connection);
1304 if (message == NULL)
1306 _dbus_warn ("Did not receive a reply to %s %d on %p",
1307 "GetConnectionUnixUser", serial, connection);
1311 verbose_message_received (connection, message);
1313 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1315 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1317 ; /* good, this is a valid response */
1320 else if (dbus_message_is_error (message, DBUS_ERROR_FAILED))
1322 /* this is OK, Unix uids aren't meaningful on Windows */
1327 warn_unexpected (connection, message, "not this error");
1334 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1336 ; /* good, expected */
1340 warn_unexpected (connection, message,
1341 "method_return for GetConnectionUnixUser");
1348 if (!dbus_message_get_args (message, &error,
1349 DBUS_TYPE_UINT32, &uid,
1352 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1354 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1355 dbus_error_free (&error);
1356 _dbus_wait_for_memory ();
1357 goto retry_get_property;
1361 _dbus_assert (dbus_error_is_set (&error));
1362 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser");
1368 if (!check_no_leftovers (context))
1374 dbus_error_free (&error);
1377 dbus_message_unref (message);
1382 /* returns TRUE if the correct thing happens,
1383 * but the correct thing may include OOM errors.
1386 check_get_connection_unix_process_id (BusContext *context,
1387 DBusConnection *connection)
1389 DBusMessage *message;
1390 dbus_uint32_t serial;
1393 const char *base_service_name;
1399 dbus_error_init (&error);
1402 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1404 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1406 DBUS_INTERFACE_DBUS,
1407 "GetConnectionUnixProcessID");
1409 if (message == NULL)
1412 base_service_name = dbus_bus_get_unique_name (connection);
1414 if (!dbus_message_append_args (message,
1415 DBUS_TYPE_STRING, &base_service_name,
1418 dbus_message_unref (message);
1422 if (!dbus_connection_send (connection, message, &serial))
1424 dbus_message_unref (message);
1428 /* send our message */
1429 bus_test_run_clients_loop (SEND_PENDING (connection));
1431 dbus_message_unref (message);
1434 dbus_connection_ref (connection); /* because we may get disconnected */
1435 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1437 if (!dbus_connection_get_is_connected (connection))
1439 _dbus_verbose ("connection was disconnected\n");
1441 dbus_connection_unref (connection);
1446 dbus_connection_unref (connection);
1448 message = pop_message_waiting_for_memory (connection);
1449 if (message == NULL)
1451 _dbus_warn ("Did not receive a reply to %s %d on %p",
1452 "GetConnectionUnixProcessID", serial, connection);
1456 verbose_message_received (connection, message);
1458 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1460 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1462 ; /* good, this is a valid response */
1465 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1467 /* We are expecting this error, since we know in the test suite we aren't
1468 * talking to a client running on UNIX
1470 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1475 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
1476 defined(__linux__) || \
1477 defined(__OpenBSD__)
1478 /* In principle NetBSD should also be in that list, but
1479 * its implementation of PID-passing doesn't work
1480 * over a socketpair() as used in the debug-pipe transport.
1481 * We test this functionality in a more realistic situation
1482 * in test/dbus-daemon.c. */
1483 warn_unexpected (connection, message, "not this error");
1487 _dbus_verbose ("does not support GetConnectionUnixProcessID but perhaps that's OK?\n");
1494 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1497 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1499 ; /* good, expected */
1503 warn_unexpected (connection, message,
1504 "method_return for GetConnectionUnixProcessID");
1511 if (!dbus_message_get_args (message, &error,
1512 DBUS_TYPE_UINT32, &pid,
1515 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1517 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1518 dbus_error_free (&error);
1519 _dbus_wait_for_memory ();
1520 goto retry_get_property;
1524 _dbus_assert (dbus_error_is_set (&error));
1525 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID");
1531 /* test if returned pid is the same as our own pid
1533 * @todo It would probably be good to restructure the tests
1534 * in a way so our parent is the bus that we're testing
1535 * cause then we can test that the pid returned matches
1538 if (pid != (dbus_uint32_t) _dbus_getpid ())
1540 _dbus_assert (dbus_error_is_set (&error));
1541 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid");
1545 #endif /* !DBUS_WIN */
1548 if (!check_no_leftovers (context))
1554 dbus_error_free (&error);
1557 dbus_message_unref (message);
1562 /* returns TRUE if the correct thing happens,
1563 * but the correct thing may include OOM errors.
1566 check_add_match (BusContext *context,
1567 DBusConnection *connection,
1570 DBusMessage *message;
1572 dbus_uint32_t serial;
1576 dbus_error_init (&error);
1579 _dbus_verbose ("check_add_match for connection %p, rule %s\n",
1582 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1584 DBUS_INTERFACE_DBUS,
1587 if (message == NULL)
1590 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &rule,
1593 dbus_message_unref (message);
1597 if (!dbus_connection_send (connection, message, &serial))
1599 dbus_message_unref (message);
1603 dbus_message_unref (message);
1606 dbus_connection_ref (connection); /* because we may get disconnected */
1608 /* send our message */
1609 bus_test_run_clients_loop (SEND_PENDING (connection));
1611 if (!dbus_connection_get_is_connected (connection))
1613 _dbus_verbose ("connection was disconnected\n");
1615 dbus_connection_unref (connection);
1620 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1622 if (!dbus_connection_get_is_connected (connection))
1624 _dbus_verbose ("connection was disconnected\n");
1626 dbus_connection_unref (connection);
1631 dbus_connection_unref (connection);
1633 message = pop_message_waiting_for_memory (connection);
1634 if (message == NULL)
1636 _dbus_warn ("Did not receive a reply to %s %d on %p",
1637 "AddMatch", serial, connection);
1641 verbose_message_received (connection, message);
1643 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1645 _dbus_warn ("Message has wrong sender %s",
1646 dbus_message_get_sender (message) ?
1647 dbus_message_get_sender (message) : "(none)");
1651 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1653 if (dbus_message_is_error (message,
1654 DBUS_ERROR_NO_MEMORY))
1656 ; /* good, this is a valid response */
1660 warn_unexpected (connection, message, "not this error");
1667 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1669 ; /* good, expected */
1670 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1674 warn_unexpected (connection, message, "method return for AddMatch");
1680 if (!check_no_leftovers (context))
1686 dbus_error_free (&error);
1689 dbus_message_unref (message);
1694 #ifdef DBUS_ENABLE_STATS
1695 /* returns TRUE if the correct thing happens,
1696 * but the correct thing may include OOM errors.
1699 check_get_all_match_rules (BusContext *context,
1700 DBusConnection *connection)
1702 DBusMessage *message;
1704 dbus_uint32_t serial;
1708 dbus_error_init (&error);
1711 _dbus_verbose ("check_get_all_match_rules for connection %p\n",
1714 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1716 BUS_INTERFACE_STATS,
1717 "GetAllMatchRules");
1719 if (message == NULL)
1722 if (!dbus_connection_send (connection, message, &serial))
1724 dbus_message_unref (message);
1728 dbus_message_unref (message);
1731 dbus_connection_ref (connection); /* because we may get disconnected */
1733 /* send our message */
1734 bus_test_run_clients_loop (SEND_PENDING (connection));
1736 if (!dbus_connection_get_is_connected (connection))
1738 _dbus_verbose ("connection was disconnected\n");
1740 dbus_connection_unref (connection);
1745 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1747 if (!dbus_connection_get_is_connected (connection))
1749 _dbus_verbose ("connection was disconnected\n");
1751 dbus_connection_unref (connection);
1756 dbus_connection_unref (connection);
1758 message = pop_message_waiting_for_memory (connection);
1759 if (message == NULL)
1761 _dbus_warn ("Did not receive a reply to %s %d on %p",
1762 "AddMatch", serial, connection);
1766 verbose_message_received (connection, message);
1768 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1770 _dbus_warn ("Message has wrong sender %s",
1771 dbus_message_get_sender (message) ?
1772 dbus_message_get_sender (message) : "(none)");
1776 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1778 if (dbus_message_is_error (message,
1779 DBUS_ERROR_NO_MEMORY))
1781 ; /* good, this is a valid response */
1785 warn_unexpected (connection, message, "not this error");
1792 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1794 ; /* good, expected */
1795 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1799 warn_unexpected (connection, message, "method return for AddMatch");
1805 if (!check_no_leftovers (context))
1811 dbus_error_free (&error);
1814 dbus_message_unref (message);
1820 /* returns TRUE if the correct thing happens,
1821 * but the correct thing may include OOM errors.
1824 check_hello_connection (BusContext *context)
1826 DBusConnection *connection;
1829 dbus_error_init (&error);
1831 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1832 if (connection == NULL)
1834 _DBUS_ASSERT_ERROR_IS_SET (&error);
1835 dbus_error_free (&error);
1839 if (!bus_setup_debug_client (connection))
1841 dbus_connection_close (connection);
1842 dbus_connection_unref (connection);
1846 spin_connection_until_authenticated (context, connection);
1848 if (!check_hello_message (context, connection))
1851 if (dbus_bus_get_unique_name (connection) == NULL)
1853 /* We didn't successfully register, so we can't
1854 * do the usual kill_client_connection() checks
1856 kill_client_connection_unchecked (connection);
1860 if (!check_add_match (context, connection, ""))
1863 kill_client_connection (context, connection);
1869 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1871 /* returns TRUE if the correct thing happens,
1872 * but the correct thing may include OOM errors.
1875 check_nonexistent_service_no_auto_start (BusContext *context,
1876 DBusConnection *connection)
1878 DBusMessage *message;
1879 dbus_uint32_t serial;
1881 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1882 dbus_uint32_t flags;
1884 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1886 DBUS_INTERFACE_DBUS,
1887 "StartServiceByName");
1889 if (message == NULL)
1892 dbus_message_set_auto_start (message, FALSE);
1895 if (!dbus_message_append_args (message,
1896 DBUS_TYPE_STRING, &nonexistent,
1897 DBUS_TYPE_UINT32, &flags,
1900 dbus_message_unref (message);
1904 if (!dbus_connection_send (connection, message, &serial))
1906 dbus_message_unref (message);
1910 dbus_message_unref (message);
1913 bus_test_run_everything (context);
1914 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1915 bus_test_run_everything (context);
1917 if (!dbus_connection_get_is_connected (connection))
1919 _dbus_verbose ("connection was disconnected\n");
1925 message = pop_message_waiting_for_memory (connection);
1926 if (message == NULL)
1928 _dbus_warn ("Did not receive a reply to %s %d on %p",
1929 "StartServiceByName", serial, connection);
1933 verbose_message_received (connection, message);
1935 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1937 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1939 _dbus_warn ("Message has wrong sender %s",
1940 dbus_message_get_sender (message) ?
1941 dbus_message_get_sender (message) : "(none)");
1945 if (dbus_message_is_error (message,
1946 DBUS_ERROR_NO_MEMORY))
1948 ; /* good, this is a valid response */
1950 else if (dbus_message_is_error (message,
1951 DBUS_ERROR_SERVICE_UNKNOWN))
1953 ; /* good, this is expected also */
1957 warn_unexpected (connection, message, "not this error");
1963 _dbus_warn ("Did not expect to successfully activate %s",
1964 NONEXISTENT_SERVICE_NAME);
1972 dbus_message_unref (message);
1977 /* returns TRUE if the correct thing happens,
1978 * but the correct thing may include OOM errors.
1981 check_nonexistent_service_auto_start (BusContext *context,
1982 DBusConnection *connection)
1984 DBusMessage *message;
1985 dbus_uint32_t serial;
1988 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1989 "/org/freedesktop/TestSuite",
1990 "org.freedesktop.TestSuite",
1993 if (message == NULL)
1996 if (!dbus_connection_send (connection, message, &serial))
1998 dbus_message_unref (message);
2002 dbus_message_unref (message);
2005 bus_test_run_everything (context);
2006 block_connection_until_message_from_bus (context, connection, "reply to Echo");
2007 bus_test_run_everything (context);
2009 if (!dbus_connection_get_is_connected (connection))
2011 _dbus_verbose ("connection was disconnected\n");
2017 message = pop_message_waiting_for_memory (connection);
2019 if (message == NULL)
2021 _dbus_warn ("Did not receive a reply to %s %d on %p",
2022 "Echo message (auto activation)", serial, connection);
2026 verbose_message_received (connection, message);
2028 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2030 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2032 _dbus_warn ("Message has wrong sender %s",
2033 dbus_message_get_sender (message) ?
2034 dbus_message_get_sender (message) : "(none)");
2038 if (dbus_message_is_error (message,
2039 DBUS_ERROR_NO_MEMORY))
2041 ; /* good, this is a valid response */
2043 else if (dbus_message_is_error (message,
2044 DBUS_ERROR_SERVICE_UNKNOWN))
2046 ; /* good, this is expected also */
2050 warn_unexpected (connection, message, "not this error");
2056 _dbus_warn ("Did not expect to successfully activate %s",
2057 NONEXISTENT_SERVICE_NAME);
2065 dbus_message_unref (message);
2071 check_base_service_activated (BusContext *context,
2072 DBusConnection *connection,
2073 DBusMessage *initial_message,
2074 const char **base_service_p)
2076 DBusMessage *message;
2079 const char *base_service, *base_service_from_bus, *old_owner;
2083 dbus_error_init (&error);
2084 base_service = NULL;
2086 base_service_from_bus = NULL;
2088 message = initial_message;
2089 dbus_message_ref (message);
2091 if (dbus_message_is_signal (message,
2092 DBUS_INTERFACE_DBUS,
2093 "NameOwnerChanged"))
2095 CheckServiceOwnerChangedData socd;
2097 reget_service_name_arg:
2098 base_service = NULL;
2100 base_service_from_bus = NULL;
2102 if (!dbus_message_get_args (message, &error,
2103 DBUS_TYPE_STRING, &base_service,
2104 DBUS_TYPE_STRING, &old_owner,
2105 DBUS_TYPE_STRING, &base_service_from_bus,
2108 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2110 dbus_error_free (&error);
2111 _dbus_wait_for_memory ();
2112 goto reget_service_name_arg;
2116 _dbus_warn ("Message %s doesn't have a service name: %s",
2117 "NameOwnerChanged (creation)",
2123 if (*base_service != ':')
2125 _dbus_warn ("Expected base service activation, got \"%s\" instead",
2130 if (strcmp (base_service, base_service_from_bus) != 0)
2132 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"",
2133 base_service, base_service_from_bus);
2139 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"",
2144 socd.expected_kind = SERVICE_CREATED;
2145 socd.expected_service_name = base_service;
2146 socd.failed = FALSE;
2147 socd.skip_connection = connection;
2148 socd.context = context;
2150 bus_test_clients_foreach (check_service_owner_changed_foreach,
2158 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
2164 *base_service_p = base_service;
2170 dbus_message_unref (message);
2171 dbus_error_free (&error);
2177 check_service_activated (BusContext *context,
2178 DBusConnection *connection,
2179 const char *activated_name,
2180 const char *base_service_name,
2181 DBusMessage *initial_message)
2183 DBusMessage *message;
2186 dbus_uint32_t activation_result;
2190 dbus_error_init (&error);
2192 message = initial_message;
2193 dbus_message_ref (message);
2195 if (dbus_message_is_signal (message,
2196 DBUS_INTERFACE_DBUS,
2197 "NameOwnerChanged"))
2199 CheckServiceOwnerChangedData socd;
2200 const char *service_name, *base_service_from_bus, *old_owner;
2202 reget_service_name_arg:
2203 service_name = NULL;
2205 base_service_from_bus = NULL;
2207 if (!dbus_message_get_args (message, &error,
2208 DBUS_TYPE_STRING, &service_name,
2209 DBUS_TYPE_STRING, &old_owner,
2210 DBUS_TYPE_STRING, &base_service_from_bus,
2213 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2215 dbus_error_free (&error);
2216 _dbus_wait_for_memory ();
2217 goto reget_service_name_arg;
2221 _dbus_warn ("Message %s doesn't have a service name: %s",
2222 "NameOwnerChanged (creation)",
2228 if (strcmp (service_name, activated_name) != 0)
2230 _dbus_warn ("Expected to see service %s created, saw %s instead",
2231 activated_name, service_name);
2235 if (strcmp (base_service_name, base_service_from_bus) != 0)
2237 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead",
2238 base_service_from_bus, base_service_name);
2244 _dbus_warn ("expected a %s, got a %s",
2245 "NameOwnerChanged (creation)",
2246 "NameOwnerChanged (change)");
2250 socd.expected_kind = SERVICE_CREATED;
2251 socd.skip_connection = connection;
2252 socd.failed = FALSE;
2253 socd.expected_service_name = service_name;
2254 socd.context = context;
2256 bus_test_clients_foreach (check_service_owner_changed_foreach,
2262 dbus_message_unref (message);
2263 service_name = NULL;
2265 base_service_from_bus = NULL;
2267 message = pop_message_waiting_for_memory (connection);
2268 if (message == NULL)
2270 _dbus_warn ("Expected a reply to %s, got nothing",
2271 "StartServiceByName");
2277 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2282 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2284 warn_unexpected (connection, message, "reply to StartServiceByName");
2289 activation_result = 0;
2290 if (!dbus_message_get_args (message, &error,
2291 DBUS_TYPE_UINT32, &activation_result,
2294 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2296 _dbus_warn ("Did not have activation result first argument to %s: %s",
2297 "StartServiceByName", error.message);
2301 dbus_error_free (&error);
2305 if (activation_result == DBUS_START_REPLY_SUCCESS)
2307 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2311 _dbus_warn ("Activation result was %u, no good.",
2317 dbus_message_unref (message);
2320 if (!check_no_leftovers (context))
2322 _dbus_warn ("Messages were left over after verifying existent activation results");
2330 dbus_message_unref (message);
2331 dbus_error_free (&error);
2337 check_service_auto_activated (BusContext *context,
2338 DBusConnection *connection,
2339 const char *activated_name,
2340 const char *base_service_name,
2341 DBusMessage *initial_message)
2343 DBusMessage *message;
2349 dbus_error_init (&error);
2351 message = initial_message;
2352 dbus_message_ref (message);
2354 if (dbus_message_is_signal (message,
2355 DBUS_INTERFACE_DBUS,
2356 "NameOwnerChanged"))
2358 const char *service_name;
2359 CheckServiceOwnerChangedData socd;
2361 reget_service_name_arg:
2362 if (!dbus_message_get_args (message, &error,
2363 DBUS_TYPE_STRING, &service_name,
2366 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2368 dbus_error_free (&error);
2369 _dbus_wait_for_memory ();
2370 goto reget_service_name_arg;
2374 _dbus_warn ("Message %s doesn't have a service name: %s",
2377 dbus_error_free (&error);
2382 if (strcmp (service_name, activated_name) != 0)
2384 _dbus_warn ("Expected to see service %s created, saw %s instead",
2385 activated_name, service_name);
2389 socd.expected_kind = SERVICE_CREATED;
2390 socd.expected_service_name = service_name;
2391 socd.failed = FALSE;
2392 socd.skip_connection = connection;
2393 socd.context = context;
2395 bus_test_clients_foreach (check_service_owner_changed_foreach,
2401 /* Note that this differs from regular activation in that we don't get a
2402 * reply to ActivateService here.
2405 dbus_message_unref (message);
2407 service_name = NULL;
2411 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2420 dbus_message_unref (message);
2426 check_service_deactivated (BusContext *context,
2427 DBusConnection *connection,
2428 const char *activated_name,
2429 const char *base_service)
2432 CheckServiceOwnerChangedData socd;
2436 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2437 * service and the activated_name. The base service
2438 * notification is required to come last.
2440 socd.expected_kind = SERVICE_DELETED;
2441 socd.expected_service_name = activated_name;
2442 socd.failed = FALSE;
2443 socd.skip_connection = NULL;
2444 socd.context = context;
2446 bus_test_clients_foreach (check_service_owner_changed_foreach,
2452 socd.expected_kind = SERVICE_DELETED;
2453 socd.expected_service_name = base_service;
2454 socd.failed = FALSE;
2455 socd.skip_connection = NULL;
2456 socd.context = context;
2458 bus_test_clients_foreach (check_service_owner_changed_foreach,
2471 check_send_exit_to_service (BusContext *context,
2472 DBusConnection *connection,
2473 const char *service_name,
2474 const char *base_service)
2476 dbus_bool_t got_error;
2477 DBusMessage *message;
2478 dbus_uint32_t serial;
2481 _dbus_verbose ("Sending exit message to the test service\n");
2485 /* Kill off the test service by sending it a quit message */
2486 message = dbus_message_new_method_call (service_name,
2487 "/org/freedesktop/TestSuite",
2488 "org.freedesktop.TestSuite",
2491 if (message == NULL)
2493 /* Do this again; we still need the service to exit... */
2494 if (!check_send_exit_to_service (context, connection,
2495 service_name, base_service))
2501 if (!dbus_connection_send (connection, message, &serial))
2503 dbus_message_unref (message);
2505 /* Do this again; we still need the service to exit... */
2506 if (!check_send_exit_to_service (context, connection,
2507 service_name, base_service))
2513 dbus_message_unref (message);
2517 bus_test_run_clients_loop (SEND_PENDING (connection));
2519 /* read it in and write it out to test service */
2520 bus_test_run_bus_loop (context, FALSE);
2522 /* see if we got an error during message bus dispatching */
2523 bus_test_run_clients_loop (FALSE);
2524 message = borrow_message_waiting_for_memory (connection);
2525 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2528 dbus_connection_return_message (connection, message);
2534 /* If no error, wait for the test service to exit */
2535 block_connection_until_message_from_bus (context, connection, "test service to exit");
2537 bus_test_run_everything (context);
2542 message = pop_message_waiting_for_memory (connection);
2543 _dbus_assert (message != NULL);
2545 if (dbus_message_get_reply_serial (message) != serial)
2547 warn_unexpected (connection, message,
2548 "error with the correct reply serial");
2552 if (!dbus_message_is_error (message,
2553 DBUS_ERROR_NO_MEMORY))
2555 warn_unexpected (connection, message,
2556 "a no memory error from asking test service to exit");
2560 _dbus_verbose ("Got error %s when asking test service to exit\n",
2561 dbus_message_get_error_name (message));
2563 /* Do this again; we still need the service to exit... */
2564 if (!check_send_exit_to_service (context, connection,
2565 service_name, base_service))
2570 if (!check_service_deactivated (context, connection,
2571 service_name, base_service))
2574 /* Should now have a NoReply error from the Exit() method
2575 * call; it should have come after all the deactivation
2578 message = pop_message_waiting_for_memory (connection);
2580 if (message == NULL)
2582 warn_unexpected (connection, NULL,
2583 "reply to Exit() method call");
2586 if (!dbus_message_is_error (message,
2587 DBUS_ERROR_NO_REPLY))
2589 warn_unexpected (connection, message,
2590 "NoReply error from Exit() method call");
2594 if (dbus_message_get_reply_serial (message) != serial)
2596 warn_unexpected (connection, message,
2597 "error with the correct reply serial");
2601 _dbus_verbose ("Got error %s after test service exited\n",
2602 dbus_message_get_error_name (message));
2604 if (!check_no_leftovers (context))
2606 _dbus_warn ("Messages were left over after %s",
2607 _DBUS_FUNCTION_NAME);
2616 dbus_message_unref (message);
2622 check_got_error (BusContext *context,
2623 DBusConnection *connection,
2624 const char *first_error_name,
2627 DBusMessage *message;
2630 dbus_bool_t error_found;
2631 const char *error_name;
2635 message = pop_message_waiting_for_memory (connection);
2636 if (message == NULL)
2638 _dbus_warn ("Did not get an expected error");
2642 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2644 warn_unexpected (connection, message, "an error");
2649 error_found = FALSE;
2651 va_start (ap, first_error_name);
2652 error_name = first_error_name;
2653 while (error_name != NULL)
2655 if (dbus_message_is_error (message, error_name))
2660 error_name = va_arg (ap, char*);
2666 _dbus_warn ("Expected error %s or other, got %s instead",
2668 dbus_message_get_error_name (message));
2676 dbus_message_unref (message);
2683 GOT_SERVICE_CREATED,
2684 GOT_SERVICE_DELETED,
2689 static GotServiceInfo
2690 check_got_service_info (DBusMessage *message)
2692 GotServiceInfo message_kind;
2694 if (dbus_message_is_signal (message,
2695 DBUS_INTERFACE_DBUS,
2696 "NameOwnerChanged"))
2699 const char *service_name, *old_owner, *new_owner;
2700 dbus_error_init (&error);
2702 reget_service_info_data:
2703 service_name = NULL;
2707 dbus_message_get_args (message, &error,
2708 DBUS_TYPE_STRING, &service_name,
2709 DBUS_TYPE_STRING, &old_owner,
2710 DBUS_TYPE_STRING, &new_owner,
2712 if (dbus_error_is_set (&error))
2714 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2716 dbus_error_free (&error);
2717 goto reget_service_info_data;
2721 _dbus_warn ("unexpected arguments for NameOwnerChanged message");
2722 message_kind = GOT_SOMETHING_ELSE;
2725 else if (!old_owner[0])
2726 message_kind = GOT_SERVICE_CREATED;
2727 else if (!new_owner[0])
2728 message_kind = GOT_SERVICE_DELETED;
2730 message_kind = GOT_SOMETHING_ELSE;
2732 dbus_error_free (&error);
2734 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2735 message_kind = GOT_ERROR;
2737 message_kind = GOT_SOMETHING_ELSE;
2739 return message_kind;
2742 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2744 /* returns TRUE if the correct thing happens,
2745 * but the correct thing may include OOM errors.
2748 check_existent_service_no_auto_start (BusContext *context,
2749 DBusConnection *connection)
2751 DBusMessage *message;
2752 DBusMessage *base_service_message;
2753 const char *base_service;
2754 dbus_uint32_t serial;
2756 const char *existent = EXISTENT_SERVICE_NAME;
2757 dbus_uint32_t flags;
2759 base_service_message = NULL;
2761 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2763 DBUS_INTERFACE_DBUS,
2764 "StartServiceByName");
2766 if (message == NULL)
2769 dbus_message_set_auto_start (message, FALSE);
2772 if (!dbus_message_append_args (message,
2773 DBUS_TYPE_STRING, &existent,
2774 DBUS_TYPE_UINT32, &flags,
2777 dbus_message_unref (message);
2781 if (!dbus_connection_send (connection, message, &serial))
2783 dbus_message_unref (message);
2787 dbus_message_unref (message);
2790 bus_test_run_everything (context);
2792 /* now wait for the message bus to hear back from the activated
2795 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2797 bus_test_run_everything (context);
2799 if (!dbus_connection_get_is_connected (connection))
2801 _dbus_verbose ("connection was disconnected\n");
2807 message = pop_message_waiting_for_memory (connection);
2808 if (message == NULL)
2810 _dbus_warn ("Did not receive any messages after %s %d on %p",
2811 "StartServiceByName", serial, connection);
2815 verbose_message_received (connection, message);
2816 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2818 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2820 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2822 _dbus_warn ("Message has wrong sender %s",
2823 dbus_message_get_sender (message) ?
2824 dbus_message_get_sender (message) : "(none)");
2828 if (dbus_message_is_error (message,
2829 DBUS_ERROR_NO_MEMORY))
2831 ; /* good, this is a valid response */
2833 else if (dbus_message_is_error (message,
2834 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2835 dbus_message_is_error (message,
2836 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2837 dbus_message_is_error (message,
2838 DBUS_ERROR_SPAWN_EXEC_FAILED))
2840 ; /* good, this is expected also */
2844 _dbus_warn ("Did not expect error %s",
2845 dbus_message_get_error_name (message));
2851 GotServiceInfo message_kind;
2853 if (!check_base_service_activated (context, connection,
2854 message, &base_service))
2857 base_service_message = message;
2860 /* We may need to block here for the test service to exit or finish up */
2861 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2863 message = dbus_connection_borrow_message (connection);
2864 if (message == NULL)
2866 _dbus_warn ("Did not receive any messages after base service creation notification");
2870 message_kind = check_got_service_info (message);
2872 dbus_connection_return_message (connection, message);
2875 switch (message_kind)
2877 case GOT_SOMETHING_ELSE:
2878 _dbus_warn ("Unexpected message after ActivateService "
2879 "(should be an error or a service announcement");
2883 if (!check_got_error (context, connection,
2884 DBUS_ERROR_SPAWN_CHILD_EXITED,
2885 DBUS_ERROR_NO_MEMORY,
2888 /* A service deleted should be coming along now after this error.
2889 * We can also get the error *after* the service deleted.
2894 case GOT_SERVICE_DELETED:
2896 /* The service started up and got a base address, but then
2897 * failed to register under EXISTENT_SERVICE_NAME
2899 CheckServiceOwnerChangedData socd;
2901 socd.expected_kind = SERVICE_DELETED;
2902 socd.expected_service_name = base_service;
2903 socd.failed = FALSE;
2904 socd.skip_connection = NULL;
2905 socd.context = context;
2907 bus_test_clients_foreach (check_service_owner_changed_foreach,
2913 /* Now we should get an error about the service exiting
2914 * if we didn't get it before.
2916 if (message_kind != GOT_ERROR)
2918 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2920 /* and process everything again */
2921 bus_test_run_everything (context);
2923 if (!check_got_error (context, connection,
2924 DBUS_ERROR_SPAWN_CHILD_EXITED,
2925 DBUS_ERROR_NO_MEMORY,
2932 case GOT_SERVICE_CREATED:
2933 message = pop_message_waiting_for_memory (connection);
2934 if (message == NULL)
2936 _dbus_warn ("Failed to pop message we just put back! "
2937 "should have been a NameOwnerChanged (creation)");
2941 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2942 base_service, message))
2945 dbus_message_unref (message);
2948 if (!check_no_leftovers (context))
2950 _dbus_warn ("Messages were left over after successful activation");
2954 if (!check_send_exit_to_service (context, connection,
2955 EXISTENT_SERVICE_NAME, base_service))
2966 dbus_message_unref (message);
2968 if (base_service_message)
2969 dbus_message_unref (base_service_message);
2974 /* returns TRUE if the correct thing happens,
2975 * but the correct thing may include OOM errors.
2978 check_segfault_service_no_auto_start (BusContext *context,
2979 DBusConnection *connection)
2981 DBusMessage *message;
2982 dbus_uint32_t serial;
2984 const char *segv_service;
2985 dbus_uint32_t flags;
2987 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2989 DBUS_INTERFACE_DBUS,
2990 "StartServiceByName");
2992 if (message == NULL)
2995 dbus_message_set_auto_start (message, FALSE);
2997 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2999 if (!dbus_message_append_args (message,
3000 DBUS_TYPE_STRING, &segv_service,
3001 DBUS_TYPE_UINT32, &flags,
3004 dbus_message_unref (message);
3008 if (!dbus_connection_send (connection, message, &serial))
3010 dbus_message_unref (message);
3014 dbus_message_unref (message);
3017 bus_test_run_everything (context);
3018 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
3019 bus_test_run_everything (context);
3021 if (!dbus_connection_get_is_connected (connection))
3023 _dbus_verbose ("connection was disconnected\n");
3029 message = pop_message_waiting_for_memory (connection);
3030 if (message == NULL)
3032 _dbus_warn ("Did not receive a reply to %s %d on %p",
3033 "StartServiceByName", serial, connection);
3037 verbose_message_received (connection, message);
3039 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3041 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3043 _dbus_warn ("Message has wrong sender %s",
3044 dbus_message_get_sender (message) ?
3045 dbus_message_get_sender (message) : "(none)");
3049 if (dbus_message_is_error (message,
3050 DBUS_ERROR_NO_MEMORY))
3052 ; /* good, this is a valid response */
3054 else if (dbus_message_is_error (message,
3057 const char *servicehelper;
3058 servicehelper = bus_context_get_servicehelper (context);
3059 /* make sure this only happens with the launch helper */
3060 _dbus_assert (servicehelper != NULL);
3063 else if (dbus_message_is_error (message,
3064 DBUS_ERROR_SPAWN_CHILD_EXITED))
3066 /* unhandled exceptions are normal exit codes */
3069 else if (dbus_message_is_error (message,
3070 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3072 ; /* good, this is expected also */
3077 warn_unexpected (connection, message, "not this error");
3084 _dbus_warn ("Did not expect to successfully activate segfault service");
3092 dbus_message_unref (message);
3098 /* returns TRUE if the correct thing happens,
3099 * but the correct thing may include OOM errors.
3102 check_segfault_service_auto_start (BusContext *context,
3103 DBusConnection *connection)
3105 DBusMessage *message;
3106 dbus_uint32_t serial;
3109 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
3110 "/org/freedesktop/TestSuite",
3111 "org.freedesktop.TestSuite",
3114 if (message == NULL)
3117 if (!dbus_connection_send (connection, message, &serial))
3119 dbus_message_unref (message);
3123 dbus_message_unref (message);
3126 bus_test_run_everything (context);
3127 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
3128 bus_test_run_everything (context);
3130 if (!dbus_connection_get_is_connected (connection))
3132 _dbus_verbose ("connection was disconnected\n");
3138 message = pop_message_waiting_for_memory (connection);
3139 if (message == NULL)
3141 _dbus_warn ("Did not receive a reply to %s %d on %p",
3142 "Echo message (auto activation)", serial, connection);
3146 verbose_message_received (connection, message);
3148 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3150 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3152 _dbus_warn ("Message has wrong sender %s",
3153 dbus_message_get_sender (message) ?
3154 dbus_message_get_sender (message) : "(none)");
3158 if (dbus_message_is_error (message,
3159 DBUS_ERROR_NO_MEMORY))
3161 ; /* good, this is a valid response */
3164 else if (dbus_message_is_error (message,
3165 DBUS_ERROR_SPAWN_CHILD_EXITED))
3167 /* unhandled exceptions are normal exit codes */
3170 else if (dbus_message_is_error (message,
3171 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3173 ; /* good, this is expected also */
3178 warn_unexpected (connection, message, "not this error");
3185 _dbus_warn ("Did not expect to successfully activate segfault service");
3193 dbus_message_unref (message);
3198 #define TEST_ECHO_MESSAGE "Test echo message"
3199 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
3201 /* returns TRUE if the correct thing happens,
3202 * but the correct thing may include OOM errors.
3205 check_existent_hello_from_self (BusContext *context,
3206 DBusConnection *connection)
3208 DBusMessage *message;
3209 dbus_uint32_t serial;
3212 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3213 "/org/freedesktop/TestSuite",
3214 "org.freedesktop.TestSuite",
3215 "RunHelloFromSelf");
3217 if (message == NULL)
3220 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
3221 if (!dbus_message_append_args (message,
3222 DBUS_TYPE_STRING, &text,
3225 dbus_message_unref (message);
3229 if (!dbus_connection_send (connection, message, &serial))
3231 dbus_message_unref (message);
3235 dbus_message_unref (message);
3238 bus_test_run_everything (context);
3240 /* Note: if this test is run in OOM mode, it will block when the bus
3241 * doesn't send a reply due to OOM.
3243 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
3245 message = pop_message_waiting_for_memory (connection);
3246 if (message == NULL)
3248 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message");
3252 if (dbus_message_get_reply_serial (message) != serial)
3254 _dbus_warn ("Wrong reply serial");
3255 dbus_message_unref (message);
3259 dbus_message_unref (message);
3265 /* returns TRUE if the correct thing happens,
3266 * but the correct thing may include OOM errors.
3269 check_existent_ping (BusContext *context,
3270 DBusConnection *connection)
3272 DBusMessage *message;
3273 dbus_uint32_t serial;
3274 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3275 "/org/freedesktop/TestSuite",
3276 "org.freedesktop.DBus.Peer",
3279 if (message == NULL)
3282 if (!dbus_connection_send (connection, message, &serial))
3284 dbus_message_unref (message);
3288 dbus_message_unref (message);
3291 bus_test_run_everything (context);
3293 /* Note: if this test is run in OOM mode, it will block when the bus
3294 * doesn't send a reply due to OOM.
3296 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
3298 message = pop_message_waiting_for_memory (connection);
3299 if (message == NULL)
3301 _dbus_warn ("Failed to pop message! Should have been reply from Ping message");
3305 if (dbus_message_get_reply_serial (message) != serial)
3307 _dbus_warn ("Wrong reply serial");
3308 dbus_message_unref (message);
3312 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3314 _dbus_warn ("Unexpected message return during Ping");
3315 dbus_message_unref (message);
3319 dbus_message_unref (message);
3325 /* returns TRUE if the correct thing happens,
3326 * but the correct thing may include OOM errors.
3329 check_existent_get_machine_id (BusContext *context,
3330 DBusConnection *connection)
3332 DBusMessage *message;
3333 dbus_uint32_t serial;
3334 const char *machine_id;
3336 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3337 "/org/freedesktop/TestSuite",
3338 "org.freedesktop.DBus.Peer",
3341 if (message == NULL)
3344 if (!dbus_connection_send (connection, message, &serial))
3346 dbus_message_unref (message);
3350 dbus_message_unref (message);
3353 bus_test_run_everything (context);
3355 /* Note: if this test is run in OOM mode, it will block when the bus
3356 * doesn't send a reply due to OOM.
3358 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3360 message = pop_message_waiting_for_memory (connection);
3361 if (message == NULL)
3363 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message");
3367 if (dbus_message_get_reply_serial (message) != serial)
3369 _dbus_warn ("Wrong reply serial");
3370 dbus_message_unref (message);
3374 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3376 _dbus_warn ("Unexpected message return during GetMachineId");
3377 dbus_message_unref (message);
3382 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3384 _dbus_warn ("Did not get a machine ID in reply to GetMachineId");
3385 dbus_message_unref (message);
3389 if (machine_id == NULL || strlen (machine_id) != 32)
3391 _dbus_warn ("Machine id looks bogus: '%s'", machine_id ? machine_id : "null");
3392 dbus_message_unref (message);
3396 /* We can't check that the machine id is correct because during make check it is
3397 * just made up for each process separately
3400 dbus_message_unref (message);
3406 /* returns TRUE if the correct thing happens,
3407 * but the correct thing may include OOM errors.
3410 check_existent_service_auto_start (BusContext *context,
3411 DBusConnection *connection)
3413 DBusMessage *message;
3414 DBusMessage *base_service_message;
3415 dbus_uint32_t serial;
3417 const char *base_service;
3420 base_service_message = NULL;
3422 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3423 "/org/freedesktop/TestSuite",
3424 "org.freedesktop.TestSuite",
3427 if (message == NULL)
3430 text = TEST_ECHO_MESSAGE;
3431 if (!dbus_message_append_args (message,
3432 DBUS_TYPE_STRING, &text,
3435 dbus_message_unref (message);
3439 if (!dbus_connection_send (connection, message, &serial))
3441 dbus_message_unref (message);
3445 dbus_message_unref (message);
3448 bus_test_run_everything (context);
3450 /* now wait for the message bus to hear back from the activated
3453 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3454 bus_test_run_everything (context);
3456 if (!dbus_connection_get_is_connected (connection))
3458 _dbus_verbose ("connection was disconnected\n");
3464 message = pop_message_waiting_for_memory (connection);
3465 if (message == NULL)
3467 _dbus_warn ("Did not receive any messages after auto start %d on %p",
3468 serial, connection);
3472 verbose_message_received (connection, message);
3473 _dbus_verbose (" (after sending %s)\n", "auto start");
3475 /* we should get zero or two ServiceOwnerChanged signals */
3476 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3478 GotServiceInfo message_kind;
3480 if (!check_base_service_activated (context, connection,
3481 message, &base_service))
3484 base_service_message = message;
3487 /* We may need to block here for the test service to exit or finish up */
3488 block_connection_until_message_from_bus (context, connection, "service to exit");
3490 /* Should get a service creation notification for the activated
3491 * service name, or a service deletion on the base service name
3493 message = dbus_connection_borrow_message (connection);
3494 if (message == NULL)
3496 _dbus_warn ("No message after auto activation "
3497 "(should be a service announcement)");
3498 dbus_connection_return_message (connection, message);
3503 message_kind = check_got_service_info (message);
3505 dbus_connection_return_message (connection, message);
3508 switch (message_kind)
3510 case GOT_SERVICE_CREATED:
3511 message = pop_message_waiting_for_memory (connection);
3512 if (message == NULL)
3514 _dbus_warn ("Failed to pop message we just put back! "
3515 "should have been a NameOwnerChanged (creation)");
3519 /* Check that ServiceOwnerChanged (creation) was correctly received */
3520 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3521 base_service, message))
3524 dbus_message_unref (message);
3529 case GOT_SERVICE_DELETED:
3531 /* The service started up and got a base address, but then
3532 * failed to register under EXISTENT_SERVICE_NAME
3534 CheckServiceOwnerChangedData socd;
3536 socd.expected_kind = SERVICE_DELETED;
3537 socd.expected_service_name = base_service;
3538 socd.failed = FALSE;
3539 socd.skip_connection = NULL;
3540 socd.context = context;
3542 bus_test_clients_foreach (check_service_owner_changed_foreach,
3552 case GOT_SOMETHING_ELSE:
3553 _dbus_warn ("Unexpected message after auto activation");
3558 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3559 * come the method reply (or error) from the initial method call
3562 /* Note: if this test is run in OOM mode, it will block when the bus
3563 * doesn't send a reply due to OOM.
3565 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3567 message = pop_message_waiting_for_memory (connection);
3568 if (message == NULL)
3570 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
3574 if (dbus_message_get_reply_serial (message) != serial)
3576 _dbus_warn ("Wrong reply serial");
3580 dbus_message_unref (message);
3583 if (!check_existent_ping (context, connection))
3586 if (!check_existent_get_machine_id (context, connection))
3589 if (!check_existent_hello_from_self (context, connection))
3592 if (!check_send_exit_to_service (context, connection,
3593 EXISTENT_SERVICE_NAME,
3601 dbus_message_unref (message);
3603 if (base_service_message)
3604 dbus_message_unref (base_service_message);
3609 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3611 /* returns TRUE if the correct thing happens,
3612 * but the correct thing may include OOM errors.
3615 check_launch_service_file_missing (BusContext *context,
3616 DBusConnection *connection)
3618 DBusMessage *message;
3619 dbus_uint32_t serial;
3622 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3623 "/org/freedesktop/TestSuite",
3624 "org.freedesktop.TestSuite",
3627 if (message == NULL)
3630 if (!dbus_connection_send (connection, message, &serial))
3632 dbus_message_unref (message);
3636 dbus_message_unref (message);
3639 bus_test_run_everything (context);
3640 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3641 bus_test_run_everything (context);
3643 if (!dbus_connection_get_is_connected (connection))
3645 _dbus_verbose ("connection was disconnected\n");
3651 message = pop_message_waiting_for_memory (connection);
3652 if (message == NULL)
3654 _dbus_warn ("Did not receive a reply to %s %d on %p",
3655 "Echo message (auto activation)", serial, connection);
3659 verbose_message_received (connection, message);
3661 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3663 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3665 _dbus_warn ("Message has wrong sender %s",
3666 dbus_message_get_sender (message) ?
3667 dbus_message_get_sender (message) : "(none)");
3671 if (dbus_message_is_error (message,
3672 DBUS_ERROR_NO_MEMORY))
3674 ; /* good, this is a valid response */
3676 else if (dbus_message_is_error (message,
3677 DBUS_ERROR_SERVICE_UNKNOWN))
3679 _dbus_verbose("got service unknown\n");
3680 ; /* good, this is expected (only valid when using launch helper) */
3684 warn_unexpected (connection, message, "not this error");
3691 _dbus_warn ("Did not expect to successfully auto-start missing service");
3699 dbus_message_unref (message);
3706 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3708 /* returns TRUE if the correct thing happens,
3709 * but the correct thing may include OOM errors.
3712 check_launch_service_user_missing (BusContext *context,
3713 DBusConnection *connection)
3715 DBusMessage *message;
3716 dbus_uint32_t serial;
3719 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3720 "/org/freedesktop/TestSuite",
3721 "org.freedesktop.TestSuite",
3724 if (message == NULL)
3727 if (!dbus_connection_send (connection, message, &serial))
3729 dbus_message_unref (message);
3733 dbus_message_unref (message);
3736 bus_test_run_everything (context);
3737 block_connection_until_message_from_bus (context, connection,
3738 "reply to service which should fail to auto-start (missing User)");
3739 bus_test_run_everything (context);
3741 if (!dbus_connection_get_is_connected (connection))
3743 _dbus_warn ("connection was disconnected");
3749 message = pop_message_waiting_for_memory (connection);
3750 if (message == NULL)
3752 _dbus_warn ("Did not receive a reply to %s %d on %p",
3753 "Echo message (auto activation)", serial, connection);
3757 verbose_message_received (connection, message);
3759 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3761 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3763 _dbus_warn ("Message has wrong sender %s",
3764 dbus_message_get_sender (message) ?
3765 dbus_message_get_sender (message) : "(none)");
3769 if (dbus_message_is_error (message,
3770 DBUS_ERROR_NO_MEMORY))
3772 ; /* good, this is a valid response */
3774 else if (dbus_message_is_error (message,
3775 DBUS_ERROR_SPAWN_FILE_INVALID))
3777 _dbus_verbose("got service file invalid\n");
3778 ; /* good, this is expected (only valid when using launch helper) */
3782 warn_unexpected (connection, message, "not this error");
3789 _dbus_warn ("Did not expect to successfully auto-start missing service");
3797 dbus_message_unref (message);
3802 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3804 /* returns TRUE if the correct thing happens,
3805 * but the correct thing may include OOM errors.
3808 check_launch_service_exec_missing (BusContext *context,
3809 DBusConnection *connection)
3811 DBusMessage *message;
3812 dbus_uint32_t serial;
3815 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3816 "/org/freedesktop/TestSuite",
3817 "org.freedesktop.TestSuite",
3820 if (message == NULL)
3823 if (!dbus_connection_send (connection, message, &serial))
3825 dbus_message_unref (message);
3829 dbus_message_unref (message);
3832 bus_test_run_everything (context);
3833 block_connection_until_message_from_bus (context, connection,
3834 "reply to service which should fail to auto-start (missing Exec)");
3835 bus_test_run_everything (context);
3837 if (!dbus_connection_get_is_connected (connection))
3839 _dbus_warn ("connection was disconnected");
3845 message = pop_message_waiting_for_memory (connection);
3846 if (message == NULL)
3848 _dbus_warn ("Did not receive a reply to %s %d on %p",
3849 "Echo message (auto activation)", serial, connection);
3853 verbose_message_received (connection, message);
3855 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3857 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3859 _dbus_warn ("Message has wrong sender %s",
3860 dbus_message_get_sender (message) ?
3861 dbus_message_get_sender (message) : "(none)");
3865 if (dbus_message_is_error (message,
3866 DBUS_ERROR_NO_MEMORY))
3868 ; /* good, this is a valid response */
3870 else if (dbus_message_is_error (message,
3871 DBUS_ERROR_SERVICE_UNKNOWN))
3873 _dbus_verbose("could not activate as invalid service file was not added\n");
3874 ; /* good, this is expected as we shouldn't have been added to
3875 * the activation list with a missing Exec key */
3877 else if (dbus_message_is_error (message,
3878 DBUS_ERROR_SPAWN_FILE_INVALID))
3880 _dbus_verbose("got service file invalid\n");
3881 ; /* good, this is allowed, and is the message passed back from the
3886 warn_unexpected (connection, message, "not this error");
3893 _dbus_warn ("Did not expect to successfully auto-start missing service");
3901 dbus_message_unref (message);
3906 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3908 /* returns TRUE if the correct thing happens,
3909 * but the correct thing may include OOM errors.
3912 check_launch_service_service_missing (BusContext *context,
3913 DBusConnection *connection)
3915 DBusMessage *message;
3916 dbus_uint32_t serial;
3919 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3920 "/org/freedesktop/TestSuite",
3921 "org.freedesktop.TestSuite",
3924 if (message == NULL)
3927 if (!dbus_connection_send (connection, message, &serial))
3929 dbus_message_unref (message);
3933 dbus_message_unref (message);
3936 bus_test_run_everything (context);
3937 block_connection_until_message_from_bus (context, connection,
3938 "reply to service which should fail to auto-start (missing Service)");
3939 bus_test_run_everything (context);
3941 if (!dbus_connection_get_is_connected (connection))
3943 _dbus_warn ("connection was disconnected");
3949 message = pop_message_waiting_for_memory (connection);
3950 if (message == NULL)
3952 _dbus_warn ("Did not receive a reply to %s %d on %p",
3953 "Echo message (auto activation)", serial, connection);
3957 verbose_message_received (connection, message);
3959 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3961 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3963 _dbus_warn ("Message has wrong sender %s",
3964 dbus_message_get_sender (message) ?
3965 dbus_message_get_sender (message) : "(none)");
3969 if (dbus_message_is_error (message,
3970 DBUS_ERROR_NO_MEMORY))
3972 ; /* good, this is a valid response */
3974 else if (dbus_message_is_error (message,
3975 DBUS_ERROR_SERVICE_UNKNOWN))
3977 _dbus_verbose("could not activate as invalid service file was not added\n");
3978 ; /* good, this is expected as we shouldn't have been added to
3979 * the activation list with a missing Exec key */
3981 else if (dbus_message_is_error (message,
3982 DBUS_ERROR_SPAWN_FILE_INVALID))
3984 _dbus_verbose("got service file invalid\n");
3985 ; /* good, this is allowed, and is the message passed back from the
3990 warn_unexpected (connection, message, "not this error");
3997 _dbus_warn ("Did not expect to successfully auto-start missing service");
4005 dbus_message_unref (message);
4011 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
4013 /* returns TRUE if the correct thing happens,
4014 * but the correct thing may include OOM errors.
4017 check_shell_fail_service_auto_start (BusContext *context,
4018 DBusConnection *connection)
4020 DBusMessage *message;
4021 dbus_uint32_t serial;
4024 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
4025 "/org/freedesktop/TestSuite",
4026 "org.freedesktop.TestSuite",
4029 if (message == NULL)
4032 if (!dbus_connection_send (connection, message, &serial))
4034 dbus_message_unref (message);
4038 dbus_message_unref (message);
4041 bus_test_run_everything (context);
4042 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
4043 bus_test_run_everything (context);
4045 if (!dbus_connection_get_is_connected (connection))
4047 _dbus_verbose ("connection was disconnected\n");
4053 message = pop_message_waiting_for_memory (connection);
4054 if (message == NULL)
4056 _dbus_warn ("Did not receive a reply to %s %d on %p",
4057 "Echo message (auto activation)", serial, connection);
4061 verbose_message_received (connection, message);
4063 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4065 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4067 _dbus_warn ("Message has wrong sender %s",
4068 dbus_message_get_sender (message) ?
4069 dbus_message_get_sender (message) : "(none)");
4073 if (dbus_message_is_error (message,
4074 DBUS_ERROR_NO_MEMORY))
4076 ; /* good, this is a valid response */
4078 else if (dbus_message_is_error (message,
4079 DBUS_ERROR_INVALID_ARGS))
4081 _dbus_verbose("got invalid args\n");
4082 ; /* good, this is expected also */
4086 warn_unexpected (connection, message, "not this error");
4093 _dbus_warn ("Did not expect to successfully auto-start shell fail service");
4101 dbus_message_unref (message);
4106 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
4108 /* returns TRUE if the correct thing happens,
4109 * but the correct thing may include OOM errors.
4112 check_shell_service_success_auto_start (BusContext *context,
4113 DBusConnection *connection)
4115 DBusMessage *message;
4116 DBusMessage *base_service_message;
4117 dbus_uint32_t serial;
4119 const char *base_service;
4120 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4122 base_service_message = NULL;
4124 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
4125 "/org/freedesktop/TestSuite",
4126 "org.freedesktop.TestSuite",
4129 if (message == NULL)
4132 if (!dbus_connection_send (connection, message, &serial))
4134 dbus_message_unref (message);
4138 dbus_message_unref (message);
4141 bus_test_run_everything (context);
4143 /* now wait for the message bus to hear back from the activated
4146 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
4147 bus_test_run_everything (context);
4149 if (!dbus_connection_get_is_connected (connection))
4151 _dbus_verbose ("connection was disconnected\n");
4157 message = pop_message_waiting_for_memory (connection);
4158 if (message == NULL)
4160 _dbus_warn ("Did not receive any messages after auto start %d on %p",
4161 serial, connection);
4165 verbose_message_received (connection, message);
4166 _dbus_verbose (" (after sending %s)\n", "auto start");
4168 /* we should get zero or two ServiceOwnerChanged signals */
4169 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
4171 GotServiceInfo message_kind;
4173 if (!check_base_service_activated (context, connection,
4174 message, &base_service))
4177 base_service_message = message;
4180 /* We may need to block here for the test service to exit or finish up */
4181 block_connection_until_message_from_bus (context, connection, "service to exit");
4183 /* Should get a service creation notification for the activated
4184 * service name, or a service deletion on the base service name
4186 message = dbus_connection_borrow_message (connection);
4187 if (message == NULL)
4189 _dbus_warn ("No message after auto activation "
4190 "(should be a service announcement)");
4191 dbus_connection_return_message (connection, message);
4196 message_kind = check_got_service_info (message);
4198 dbus_connection_return_message (connection, message);
4201 switch (message_kind)
4203 case GOT_SERVICE_CREATED:
4204 message = pop_message_waiting_for_memory (connection);
4205 if (message == NULL)
4207 _dbus_warn ("Failed to pop message we just put back! "
4208 "should have been a NameOwnerChanged (creation)");
4212 /* Check that ServiceOwnerChanged (creation) was correctly received */
4213 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
4214 base_service, message))
4217 dbus_message_unref (message);
4222 case GOT_SERVICE_DELETED:
4224 /* The service started up and got a base address, but then
4225 * failed to register under SHELL_SUCCESS_SERVICE_NAME
4227 CheckServiceOwnerChangedData socd;
4229 socd.expected_kind = SERVICE_DELETED;
4230 socd.expected_service_name = base_service;
4231 socd.failed = FALSE;
4232 socd.skip_connection = NULL;
4233 socd.context = context;
4235 bus_test_clients_foreach (check_service_owner_changed_foreach,
4245 case GOT_SOMETHING_ELSE:
4246 _dbus_warn ("Unexpected message after auto activation");
4251 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
4252 * come the method reply (or error) from the initial method call
4255 /* Note: if this test is run in OOM mode, it will block when the bus
4256 * doesn't send a reply due to OOM.
4258 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
4260 message = pop_message_waiting_for_memory (connection);
4261 if (message == NULL)
4263 _dbus_warn ("Failed to pop message! Should have been reply from echo message");
4267 if (dbus_message_get_reply_serial (message) != serial)
4269 _dbus_warn ("Wrong reply serial");
4273 if (!dbus_message_get_args (message, NULL,
4274 DBUS_TYPE_STRING, &argv[0],
4275 DBUS_TYPE_STRING, &argv[1],
4276 DBUS_TYPE_STRING, &argv[2],
4277 DBUS_TYPE_STRING, &argv[3],
4278 DBUS_TYPE_STRING, &argv[4],
4279 DBUS_TYPE_STRING, &argv[5],
4280 DBUS_TYPE_STRING, &argv[6],
4283 _dbus_warn ("Error getting arguments from return");
4287 /* don't worry about arg[0] as it may be different
4288 depending on the path to the tests
4290 if (strcmp("-test", argv[1]) != 0)
4292 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)",
4297 if (strcmp("that", argv[2]) != 0)
4299 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)",
4304 if (strcmp("we get", argv[3]) != 0)
4306 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)",
4311 if (strcmp("back", argv[4]) != 0)
4313 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)",
4318 if (strcmp("--what", argv[5]) != 0)
4320 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)",
4325 if (strcmp("we put in", argv[6]) != 0)
4327 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)",
4328 "we put in", argv[6]);
4332 dbus_message_unref (message);
4335 if (!check_send_exit_to_service (context, connection,
4336 SHELL_SUCCESS_SERVICE_NAME,
4344 dbus_message_unref (message);
4346 if (base_service_message)
4347 dbus_message_unref (base_service_message);
4355 BusContext *context;
4359 check_oom_check1_func (void *data)
4361 Check1Data *d = data;
4363 if (! (* d->func) (d->context))
4366 if (!check_no_leftovers (d->context))
4368 _dbus_warn ("Messages were left over, should be covered by test suite");
4376 check1_try_iterations (BusContext *context,
4377 const char *description,
4383 d.context = context;
4385 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4387 _dbus_assert_not_reached ("test failed");
4391 check_get_services (BusContext *context,
4392 DBusConnection *connection,
4397 DBusMessage *message;
4398 dbus_uint32_t serial;
4405 dbus_error_init (&error);
4408 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4410 DBUS_INTERFACE_DBUS,
4413 if (message == NULL)
4416 if (!dbus_connection_send (connection, message, &serial))
4418 dbus_message_unref (message);
4422 /* send our message */
4423 bus_test_run_clients_loop (SEND_PENDING (connection));
4425 dbus_message_unref (message);
4428 dbus_connection_ref (connection); /* because we may get disconnected */
4429 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4431 if (!dbus_connection_get_is_connected (connection))
4433 _dbus_verbose ("connection was disconnected\n");
4435 dbus_connection_unref (connection);
4440 dbus_connection_unref (connection);
4442 message = pop_message_waiting_for_memory (connection);
4443 if (message == NULL)
4445 _dbus_warn ("Did not receive a reply to %s %d on %p",
4446 method, serial, connection);
4450 verbose_message_received (connection, message);
4452 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4454 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4456 ; /* good, this is a valid response */
4460 warn_unexpected (connection, message, "not this error");
4467 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4469 ; /* good, expected */
4473 warn_unexpected (connection, message,
4474 "method_return for ListActivatableNames/ListNames");
4481 if (!dbus_message_get_args (message, &error,
4487 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4489 _dbus_verbose ("no memory to list services by %s\n", method);
4490 dbus_error_free (&error);
4491 _dbus_wait_for_memory ();
4492 goto retry_get_property;
4496 _dbus_assert (dbus_error_is_set (&error));
4497 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s", method);
4506 if (!check_no_leftovers (context))
4512 dbus_error_free (&error);
4515 dbus_message_unref (message);
4520 /* returns TRUE if the correct thing happens,
4521 * but the correct thing may include OOM errors.
4524 check_list_services (BusContext *context,
4525 DBusConnection *connection)
4527 DBusMessage *message;
4528 DBusMessage *base_service_message;
4529 const char *base_service;
4530 dbus_uint32_t serial;
4532 const char *existent = EXISTENT_SERVICE_NAME;
4533 dbus_uint32_t flags;
4537 _dbus_verbose ("check_list_services for %p\n", connection);
4539 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4544 if (!_dbus_string_array_contains ((const char **)services, existent))
4546 _dbus_warn ("Did not get the expected %s from ListActivatableNames", existent);
4547 dbus_free_string_array (services);
4551 dbus_free_string_array (services);
4553 base_service_message = NULL;
4555 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4557 DBUS_INTERFACE_DBUS,
4558 "StartServiceByName");
4560 if (message == NULL)
4563 dbus_message_set_auto_start (message, FALSE);
4566 if (!dbus_message_append_args (message,
4567 DBUS_TYPE_STRING, &existent,
4568 DBUS_TYPE_UINT32, &flags,
4571 dbus_message_unref (message);
4575 if (!dbus_connection_send (connection, message, &serial))
4577 dbus_message_unref (message);
4581 dbus_message_unref (message);
4584 bus_test_run_everything (context);
4586 /* now wait for the message bus to hear back from the activated
4589 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4591 bus_test_run_everything (context);
4593 if (!dbus_connection_get_is_connected (connection))
4595 _dbus_verbose ("connection was disconnected\n");
4601 message = pop_message_waiting_for_memory (connection);
4602 if (message == NULL)
4604 _dbus_warn ("Did not receive any messages after %s %d on %p",
4605 "StartServiceByName", serial, connection);
4609 verbose_message_received (connection, message);
4610 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4612 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4614 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4616 _dbus_warn ("Message has wrong sender %s",
4617 dbus_message_get_sender (message) ?
4618 dbus_message_get_sender (message) : "(none)");
4622 if (dbus_message_is_error (message,
4623 DBUS_ERROR_NO_MEMORY))
4625 ; /* good, this is a valid response */
4627 else if (dbus_message_is_error (message,
4628 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4629 dbus_message_is_error (message,
4630 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4631 dbus_message_is_error (message,
4632 DBUS_ERROR_SPAWN_EXEC_FAILED))
4634 ; /* good, this is expected also */
4638 _dbus_warn ("Did not expect error %s",
4639 dbus_message_get_error_name (message));
4645 GotServiceInfo message_kind;
4647 if (!check_base_service_activated (context, connection,
4648 message, &base_service))
4651 base_service_message = message;
4654 /* We may need to block here for the test service to exit or finish up */
4655 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4657 message = dbus_connection_borrow_message (connection);
4658 if (message == NULL)
4660 _dbus_warn ("Did not receive any messages after base service creation notification");
4664 message_kind = check_got_service_info (message);
4666 dbus_connection_return_message (connection, message);
4669 switch (message_kind)
4671 case GOT_SOMETHING_ELSE:
4673 case GOT_SERVICE_DELETED:
4674 _dbus_warn ("Unexpected message after ActivateService "
4675 "(should be an error or a service announcement)");
4678 case GOT_SERVICE_CREATED:
4679 message = pop_message_waiting_for_memory (connection);
4680 if (message == NULL)
4682 _dbus_warn ("Failed to pop message we just put back! "
4683 "should have been a NameOwnerChanged (creation)");
4687 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4688 base_service, message))
4691 dbus_message_unref (message);
4694 if (!check_no_leftovers (context))
4696 _dbus_warn ("Messages were left over after successful activation");
4704 if (!check_get_services (context, connection, "ListNames", &services, &len))
4709 if (!_dbus_string_array_contains ((const char **)services, existent))
4711 _dbus_warn ("Did not get the expected %s from ListNames", existent);
4715 dbus_free_string_array (services);
4717 if (!check_send_exit_to_service (context, connection,
4718 EXISTENT_SERVICE_NAME, base_service))
4725 dbus_message_unref (message);
4727 if (base_service_message)
4728 dbus_message_unref (base_service_message);
4736 BusContext *context;
4737 DBusConnection *connection;
4741 check_oom_check2_func (void *data)
4743 Check2Data *d = data;
4745 if (! (* d->func) (d->context, d->connection))
4748 if (!check_no_leftovers (d->context))
4750 _dbus_warn ("Messages were left over, should be covered by test suite");
4758 check2_try_iterations (BusContext *context,
4759 DBusConnection *connection,
4760 const char *description,
4766 d.context = context;
4767 d.connection = connection;
4769 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4772 _dbus_warn ("%s failed during oom", description);
4773 _dbus_assert_not_reached ("test failed");
4778 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4779 const char *filename)
4784 if (!_dbus_string_init (&full))
4787 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4789 _dbus_string_free (&full);
4793 _dbus_string_init_const (&file, filename);
4795 if (!_dbus_concat_dir_and_file (&full, &file))
4797 _dbus_string_free (&full);
4801 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4802 _dbus_string_get_const_data (&full));
4804 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4806 _dbus_string_free (&full);
4812 bus_dispatch_test_conf (const DBusString *test_data_dir,
4813 const char *filename,
4814 dbus_bool_t use_launcher)
4816 BusContext *context;
4817 DBusConnection *foo;
4818 DBusConnection *bar;
4819 DBusConnection *baz;
4822 /* save the config name for the activation helper */
4823 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4824 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4826 dbus_error_init (&error);
4828 context = bus_context_new_test (test_data_dir, filename);
4829 if (context == NULL)
4832 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4834 _dbus_assert_not_reached ("could not alloc connection");
4836 if (!bus_setup_debug_client (foo))
4837 _dbus_assert_not_reached ("could not set up connection");
4839 spin_connection_until_authenticated (context, foo);
4841 if (!check_hello_message (context, foo))
4842 _dbus_assert_not_reached ("hello message failed");
4844 if (!check_double_hello_message (context, foo))
4845 _dbus_assert_not_reached ("double hello message failed");
4847 if (!check_add_match (context, foo, ""))
4848 _dbus_assert_not_reached ("AddMatch message failed");
4850 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4852 _dbus_assert_not_reached ("could not alloc connection");
4854 if (!bus_setup_debug_client (bar))
4855 _dbus_assert_not_reached ("could not set up connection");
4857 spin_connection_until_authenticated (context, bar);
4859 if (!check_hello_message (context, bar))
4860 _dbus_assert_not_reached ("hello message failed");
4862 if (!check_add_match (context, bar, ""))
4863 _dbus_assert_not_reached ("AddMatch message failed");
4865 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4867 _dbus_assert_not_reached ("could not alloc connection");
4869 if (!bus_setup_debug_client (baz))
4870 _dbus_assert_not_reached ("could not set up connection");
4872 spin_connection_until_authenticated (context, baz);
4874 if (!check_hello_message (context, baz))
4875 _dbus_assert_not_reached ("hello message failed");
4877 if (!check_add_match (context, baz, ""))
4878 _dbus_assert_not_reached ("AddMatch message failed");
4880 if (!check_add_match (context, baz, "interface='com.example'"))
4881 _dbus_assert_not_reached ("AddMatch message failed");
4883 #ifdef DBUS_ENABLE_STATS
4884 if (!check_get_all_match_rules (context, baz))
4885 _dbus_assert_not_reached ("GetAllMatchRules message failed");
4888 if (!check_get_connection_unix_user (context, baz))
4889 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4891 #ifdef DBUS_WIN_FIXME
4892 _dbus_verbose("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4894 if (!check_get_connection_unix_process_id (context, baz))
4895 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4898 if (!check_list_services (context, baz))
4899 _dbus_assert_not_reached ("ListActivatableNames message failed");
4901 if (!check_no_leftovers (context))
4903 _dbus_warn ("Messages were left over after setting up initial connections");
4904 _dbus_assert_not_reached ("initial connection setup failed");
4907 check1_try_iterations (context, "create_and_hello",
4908 check_hello_connection);
4910 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4911 check_nonexistent_service_no_auto_start);
4913 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4914 check_segfault_service_no_auto_start);
4916 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4917 check_existent_service_no_auto_start);
4919 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4920 check_nonexistent_service_auto_start);
4922 /* only do the segfault test if we are not using the launcher */
4923 check2_try_iterations (context, foo, "segfault_service_auto_start",
4924 check_segfault_service_auto_start);
4926 /* only do the shell fail test if we are not using the launcher */
4927 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4928 check_shell_fail_service_auto_start);
4930 /* specific to launcher */
4932 if (!check_launch_service_file_missing (context, foo))
4933 _dbus_assert_not_reached ("did not get service file not found error");
4936 /* Note: need to resolve some issues with the testing code in order to run
4937 * this in oom (handle that we sometimes don't get replies back from the bus
4938 * when oom happens, without blocking the test).
4940 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4941 check_existent_service_auto_start);
4944 if (!check_existent_service_auto_start (context, foo))
4945 _dbus_assert_not_reached ("existent service auto start failed");
4947 if (!check_shell_service_success_auto_start (context, foo))
4948 _dbus_assert_not_reached ("shell success service auto start failed");
4950 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4952 kill_client_connection_unchecked (foo);
4953 kill_client_connection_unchecked (bar);
4954 kill_client_connection_unchecked (baz);
4956 bus_context_unref (context);
4963 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4964 const char *filename)
4966 BusContext *context;
4967 DBusConnection *foo;
4970 /* save the config name for the activation helper */
4971 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4972 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4974 dbus_error_init (&error);
4976 context = bus_context_new_test (test_data_dir, filename);
4977 if (context == NULL)
4980 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4982 _dbus_assert_not_reached ("could not alloc connection");
4984 if (!bus_setup_debug_client (foo))
4985 _dbus_assert_not_reached ("could not set up connection");
4987 spin_connection_until_authenticated (context, foo);
4989 if (!check_hello_message (context, foo))
4990 _dbus_assert_not_reached ("hello message failed");
4992 if (!check_double_hello_message (context, foo))
4993 _dbus_assert_not_reached ("double hello message failed");
4995 if (!check_add_match (context, foo, ""))
4996 _dbus_assert_not_reached ("AddMatch message failed");
4998 /* this only tests the activation.c user check */
4999 if (!check_launch_service_user_missing (context, foo))
5000 _dbus_assert_not_reached ("user missing did not trigger error");
5002 /* this only tests the desktop.c exec check */
5003 if (!check_launch_service_exec_missing (context, foo))
5004 _dbus_assert_not_reached ("exec missing did not trigger error");
5006 /* this only tests the desktop.c service check */
5007 if (!check_launch_service_service_missing (context, foo))
5008 _dbus_assert_not_reached ("service missing did not trigger error");
5010 _dbus_verbose ("Disconnecting foo\n");
5012 kill_client_connection_unchecked (foo);
5014 bus_context_unref (context);
5021 bus_dispatch_test (const DBusString *test_data_dir)
5023 /* run normal activation tests */
5024 _dbus_verbose ("Normal activation tests\n");
5025 if (!bus_dispatch_test_conf (test_data_dir,
5026 "valid-config-files/debug-allow-all.conf", FALSE))
5030 /* run launch-helper activation tests */
5031 _dbus_verbose ("Launch helper activation tests\n");
5032 if (!bus_dispatch_test_conf (test_data_dir,
5033 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
5036 /* run select launch-helper activation tests on broken service files */
5037 if (!bus_dispatch_test_conf_fail (test_data_dir,
5038 "valid-config-files-system/debug-allow-all-fail.conf"))
5046 bus_dispatch_sha1_test (const DBusString *test_data_dir)
5048 BusContext *context;
5049 DBusConnection *foo;
5052 dbus_error_init (&error);
5054 /* Test SHA1 authentication */
5055 _dbus_verbose ("Testing SHA1 context\n");
5057 context = bus_context_new_test (test_data_dir,
5058 "valid-config-files/debug-allow-all-sha1.conf");
5059 if (context == NULL)
5062 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5064 _dbus_assert_not_reached ("could not alloc connection");
5066 if (!bus_setup_debug_client (foo))
5067 _dbus_assert_not_reached ("could not set up connection");
5069 spin_connection_until_authenticated (context, foo);
5071 if (!check_hello_message (context, foo))
5072 _dbus_assert_not_reached ("hello message failed");
5074 if (!check_add_match (context, foo, ""))
5075 _dbus_assert_not_reached ("addmatch message failed");
5077 if (!check_no_leftovers (context))
5079 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection");
5080 _dbus_assert_not_reached ("initial connection setup failed");
5083 check1_try_iterations (context, "create_and_hello_sha1",
5084 check_hello_connection);
5086 kill_client_connection_unchecked (foo);
5088 bus_context_unref (context);
5093 #ifdef HAVE_UNIX_FD_PASSING
5096 bus_unix_fds_passing_test(const DBusString *test_data_dir)
5098 BusContext *context;
5099 DBusConnection *foo, *bar;
5102 DBusSocket one[2], two[2];
5106 dbus_error_init (&error);
5108 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
5109 if (context == NULL)
5110 _dbus_assert_not_reached ("could not alloc context");
5112 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5114 _dbus_assert_not_reached ("could not alloc connection");
5116 if (!bus_setup_debug_client (foo))
5117 _dbus_assert_not_reached ("could not set up connection");
5119 spin_connection_until_authenticated (context, foo);
5121 if (!check_hello_message (context, foo))
5122 _dbus_assert_not_reached ("hello message failed");
5124 if (!check_add_match (context, foo, ""))
5125 _dbus_assert_not_reached ("AddMatch message failed");
5127 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5129 _dbus_assert_not_reached ("could not alloc connection");
5131 if (!bus_setup_debug_client (bar))
5132 _dbus_assert_not_reached ("could not set up connection");
5134 spin_connection_until_authenticated (context, bar);
5136 if (!check_hello_message (context, bar))
5137 _dbus_assert_not_reached ("hello message failed");
5139 if (!check_add_match (context, bar, ""))
5140 _dbus_assert_not_reached ("AddMatch message failed");
5142 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
5143 _dbus_assert_not_reached ("could not alloc message");
5145 if (!(_dbus_socketpair (one, one+1, TRUE, &error)))
5146 _dbus_assert_not_reached("Failed to allocate pipe #1");
5148 if (!(_dbus_socketpair (two, two+1, TRUE, &error)))
5149 _dbus_assert_not_reached("Failed to allocate pipe #2");
5151 if (!dbus_message_append_args(m,
5152 DBUS_TYPE_UNIX_FD, one,
5153 DBUS_TYPE_UNIX_FD, two,
5154 DBUS_TYPE_UNIX_FD, two,
5156 _dbus_assert_not_reached("Failed to attach fds.");
5158 if (!_dbus_close_socket (one[0], &error))
5159 _dbus_assert_not_reached("Failed to close pipe #1 ");
5160 if (!_dbus_close_socket (two[0], &error))
5161 _dbus_assert_not_reached("Failed to close pipe #2 ");
5163 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
5164 _dbus_assert_not_reached("Connection cannot do fd passing");
5166 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
5167 _dbus_assert_not_reached("Connection cannot do fd passing");
5169 if (!dbus_connection_send (foo, m, NULL))
5170 _dbus_assert_not_reached("Failed to send fds");
5172 dbus_message_unref(m);
5174 bus_test_run_clients_loop (SEND_PENDING (foo));
5176 bus_test_run_everything (context);
5178 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
5180 if (!(m = pop_message_waiting_for_memory (foo)))
5181 _dbus_assert_not_reached("Failed to receive msg");
5183 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5184 _dbus_assert_not_reached("bogus message received");
5186 dbus_message_unref(m);
5188 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
5190 if (!(m = pop_message_waiting_for_memory (bar)))
5191 _dbus_assert_not_reached("Failed to receive msg");
5193 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5194 _dbus_assert_not_reached("bogus message received");
5196 if (!dbus_message_get_args(m,
5198 DBUS_TYPE_UNIX_FD, &x,
5199 DBUS_TYPE_UNIX_FD, &y,
5200 DBUS_TYPE_UNIX_FD, &z,
5202 _dbus_assert_not_reached("Failed to parse fds.");
5204 dbus_message_unref(m);
5206 if (write(x, "X", 1) != 1)
5207 _dbus_assert_not_reached("Failed to write to pipe #1");
5208 if (write(y, "Y", 1) != 1)
5209 _dbus_assert_not_reached("Failed to write to pipe #2");
5210 if (write(z, "Z", 1) != 1)
5211 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
5213 if (!_dbus_close(x, &error))
5214 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
5215 if (!_dbus_close(y, &error))
5216 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
5217 if (!_dbus_close(z, &error))
5218 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
5220 if (read(one[1].fd, &r, 1) != 1 || r != 'X')
5221 _dbus_assert_not_reached("Failed to read value from pipe.");
5222 if (read(two[1].fd, &r, 1) != 1 || r != 'Y')
5223 _dbus_assert_not_reached("Failed to read value from pipe.");
5224 if (read(two[1].fd, &r, 1) != 1 || r != 'Z')
5225 _dbus_assert_not_reached("Failed to read value from pipe.");
5227 if (!_dbus_close_socket (one[1], &error))
5228 _dbus_assert_not_reached("Failed to close pipe #1 ");
5229 if (!_dbus_close_socket (two[1], &error))
5230 _dbus_assert_not_reached("Failed to close pipe #2 ");
5232 _dbus_verbose ("Disconnecting foo\n");
5233 kill_client_connection_unchecked (foo);
5235 _dbus_verbose ("Disconnecting bar\n");
5236 kill_client_connection_unchecked (bar);
5238 bus_context_unref (context);
5244 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */