1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connection.h"
32 #include "activation.h"
38 #include <dbus/dbus-internals.h>
39 #include <dbus/dbus-connection-internal.h>
40 #include <dbus/dbus-misc.h>
43 #ifdef HAVE_UNIX_FD_PASSING
44 #include <dbus/dbus-sysdeps-unix.h>
48 /* This is hard-coded in the files in valid-config-files-*. We have to use
49 * the debug-pipe transport because the tests in this file require that
50 * dbus_connection_open_private() does not block. */
51 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
53 static inline const char *
54 nonnull (const char *maybe_null,
57 return (maybe_null ? maybe_null : if_null);
61 send_one_message (DBusConnection *connection,
63 DBusConnection *sender,
64 DBusConnection *addressed_recipient,
66 BusTransaction *transaction,
69 DBusError stack_error = DBUS_ERROR_INIT;
70 BusDeferredMessage *deferred_message;
73 result = bus_context_check_security_policy (context, transaction, sender, addressed_recipient,
74 connection, message, &stack_error, &deferred_message);
76 if (result == BUS_RESULT_FALSE)
78 if (!bus_transaction_capture_error_reply (transaction, &stack_error,
81 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
82 "broadcast rejected, but not enough "
83 "memory to tell monitors");
86 dbus_error_free (&stack_error);
87 return TRUE; /* don't send it but don't return an error either */
90 if (dbus_message_contains_unix_fds(message) &&
91 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
93 dbus_set_error (&stack_error, DBUS_ERROR_NOT_SUPPORTED,
94 "broadcast cannot be delivered to %s (%s) because "
95 "it does not support receiving Unix fds",
96 bus_connection_get_name (connection),
97 bus_connection_get_loginfo (connection));
99 if (!bus_transaction_capture_error_reply (transaction, &stack_error,
102 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
103 "broadcast with Unix fd not delivered, but not "
104 "enough memory to tell monitors");
107 dbus_error_free (&stack_error);
108 return TRUE; /* don't send it but don't return an error either */
111 if (result == BUS_RESULT_LATER)
113 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, FALSE, FALSE))
118 return TRUE; /* pretend to have sent it */
121 if (!bus_transaction_send (transaction,
133 bus_dispatch_matches (BusTransaction *transaction,
134 DBusConnection *sender,
135 DBusConnection *addressed_recipient,
136 DBusMessage *message,
137 BusDeferredMessage *dispatched_deferred_message,
141 BusConnections *connections;
142 DBusList *recipients;
143 BusMatchmaker *matchmaker;
146 BusDeferredMessage *deferred_message;
148 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
150 /* sender and recipient can both be NULL for the bus driver,
151 * or for signals with no particular recipient
154 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
155 _dbus_assert (dbus_message_get_sender (message) != NULL);
157 context = bus_transaction_get_context (transaction);
159 /* First, send the message to the addressed_recipient, if there is one. */
160 if (addressed_recipient != NULL)
163 /* To maintain message order message needs to be appended at the recipient if there are already
164 * deferred messages and we are not doing deferred dispatch
166 if (dispatched_deferred_message == NULL && bus_connection_has_deferred_messages(addressed_recipient))
168 deferred_message = bus_deferred_message_new(message, sender,
169 addressed_recipient, addressed_recipient, BUS_RESULT_LATER);
171 if (deferred_message == NULL)
174 return BUS_RESULT_FALSE;
177 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, FALSE))
179 bus_deferred_message_unref(deferred_message);
181 return BUS_RESULT_FALSE;
184 bus_deferred_message_unref(deferred_message);
185 return BUS_RESULT_TRUE; /* pretend to have sent it */
188 if (dispatched_deferred_message != NULL)
190 result = bus_deferred_message_get_response(dispatched_deferred_message);
191 if (result == BUS_RESULT_TRUE)
193 /* if we know the result of policy check we still need to check if message limits
194 * are not exceeded. It is also required to add entry in expected replies list if
195 * this is a method call
197 if (!bus_deferred_message_check_message_limits(dispatched_deferred_message, error))
198 return BUS_RESULT_FALSE;
200 if (!bus_deferred_message_expect_method_reply(dispatched_deferred_message, transaction, error))
201 return BUS_RESULT_FALSE;
203 else if (result == BUS_RESULT_FALSE)
205 bus_deferred_message_create_error(dispatched_deferred_message, "Rejected message", error);
206 return BUS_RESULT_FALSE;
210 result = BUS_RESULT_LATER;
212 if (result == BUS_RESULT_LATER)
213 result = bus_context_check_security_policy(context, transaction,
214 sender, addressed_recipient, addressed_recipient, message, error,
219 case BUS_RESULT_TRUE:
221 case BUS_RESULT_FALSE:
222 return BUS_RESULT_FALSE;
223 case BUS_RESULT_LATER:
225 BusDeferredMessageStatus status;
227 if (dispatched_deferred_message != NULL)
229 /* for deferred dispatch prepend message at the recipient */
230 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, TRUE))
233 return BUS_RESULT_FALSE;
235 return BUS_RESULT_TRUE; /* pretend to have sent it */
238 status = bus_deferred_message_get_status(deferred_message);
240 if (status & BUS_DEFERRED_MESSAGE_CHECK_SEND)
242 /* send rule result not available - disable dispatching messages from the sender */
243 bus_deferred_message_disable_sender(deferred_message);
244 return BUS_RESULT_LATER;
246 else if (status & BUS_DEFERRED_MESSAGE_CHECK_RECEIVE)
248 /* receive rule result not available - queue message at the recipient */
249 if (!bus_deferred_message_queue_at_recipient(deferred_message, transaction, TRUE, FALSE))
252 return BUS_RESULT_FALSE;
255 return BUS_RESULT_TRUE; /* pretend to have sent it */
259 _dbus_verbose("deferred message has no status field set unexpectedly\n");
260 return BUS_RESULT_FALSE;
265 if (dbus_message_contains_unix_fds (message) &&
266 !dbus_connection_can_send_type (addressed_recipient,
269 dbus_set_error (error,
270 DBUS_ERROR_NOT_SUPPORTED,
271 "Tried to send message with Unix file descriptors"
272 "to a client that doesn't support that.");
273 return BUS_RESULT_FALSE;
276 /* Dispatch the message */
277 if (!bus_transaction_send(transaction, addressed_recipient, message,
278 dispatched_deferred_message != NULL ? TRUE : FALSE))
281 return BUS_RESULT_FALSE;
285 /* Now dispatch to others who look interested in this message */
286 connections = bus_context_get_connections (context);
287 dbus_error_init (&tmp_error);
288 matchmaker = bus_context_get_matchmaker (context);
291 if (!bus_matchmaker_get_recipients (matchmaker, connections,
292 sender, addressed_recipient, message,
296 return BUS_RESULT_FALSE;
299 link = _dbus_list_get_first_link (&recipients);
302 DBusConnection *dest;
306 if (!send_one_message (dest, context, sender, addressed_recipient,
307 message, transaction, &tmp_error))
310 link = _dbus_list_get_next_link (&recipients, link);
313 _dbus_list_clear (&recipients);
315 if (dbus_error_is_set (&tmp_error))
317 dbus_move_error (&tmp_error, error);
318 return BUS_RESULT_FALSE;
321 return BUS_RESULT_TRUE;
324 static DBusHandlerResult
325 bus_dispatch (DBusConnection *connection,
326 DBusMessage *message)
328 const char *sender, *service_name;
330 BusTransaction *transaction;
332 DBusHandlerResult result;
333 DBusConnection *addressed_recipient;
335 result = DBUS_HANDLER_RESULT_HANDLED;
338 addressed_recipient = NULL;
339 dbus_error_init (&error);
341 context = bus_connection_get_context (connection);
342 _dbus_assert (context != NULL);
344 /* If we can't even allocate an OOM error, we just go to sleep
347 while (!bus_connection_preallocate_oom_error (connection))
348 _dbus_wait_for_memory ();
350 /* Ref connection in case we disconnect it at some point in here */
351 dbus_connection_ref (connection);
353 /* Monitors aren't meant to send messages to us. */
354 if (bus_connection_is_monitor (connection))
356 sender = bus_connection_get_name (connection);
358 /* should never happen */
360 sender = "(unknown)";
362 if (dbus_message_is_signal (message,
363 DBUS_INTERFACE_LOCAL,
366 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
367 "Monitoring connection %s closed.", sender);
368 bus_connection_disconnected (connection);
373 /* Monitors are not allowed to send messages, because that
374 * probably indicates that the monitor is incorrectly replying
375 * to its eavesdropped messages, and we want the authors of
376 * such monitors to fix them.
378 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
379 "Monitoring connection %s (%s) is not allowed "
380 "to send messages; closing it. Please fix the "
381 "monitor to not do that. "
382 "(message type=\"%s\" interface=\"%s\" "
383 "member=\"%s\" error name=\"%s\" "
384 "destination=\"%s\")",
385 sender, bus_connection_get_loginfo (connection),
386 dbus_message_type_to_string (
387 dbus_message_get_type (message)),
388 nonnull (dbus_message_get_interface (message),
390 nonnull (dbus_message_get_member (message),
392 nonnull (dbus_message_get_error_name (message),
394 nonnull (dbus_message_get_destination (message),
396 dbus_connection_close (connection);
401 service_name = dbus_message_get_destination (message);
403 #ifdef DBUS_ENABLE_VERBOSE_MODE
405 const char *interface_name, *member_name, *error_name;
407 interface_name = dbus_message_get_interface (message);
408 member_name = dbus_message_get_member (message);
409 error_name = dbus_message_get_error_name (message);
411 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
412 interface_name ? interface_name : "(no interface)",
413 member_name ? member_name : "(no member)",
414 error_name ? error_name : "(no error name)",
415 service_name ? service_name : "peer");
417 #endif /* DBUS_ENABLE_VERBOSE_MODE */
419 /* If service_name is NULL, if it's a signal we send it to all
420 * connections with a match rule. If it's not a signal, there
421 * are some special cases here but mostly we just bail out.
423 if (service_name == NULL)
425 if (dbus_message_is_signal (message,
426 DBUS_INTERFACE_LOCAL,
429 bus_connection_disconnected (connection);
433 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
435 /* DBusConnection also handles some of these automatically, we leave
438 * FIXME: this means monitors won't get the opportunity to see
439 * non-signals with NULL destination, or their replies (which in
440 * practice are UnknownMethod errors)
442 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
447 /* Create our transaction */
448 transaction = bus_transaction_new (context);
449 if (transaction == NULL)
451 BUS_SET_OOM (&error);
455 /* Assign a sender to the message */
456 if (bus_connection_is_active (connection))
458 sender = bus_connection_get_name (connection);
459 _dbus_assert (sender != NULL);
461 if (!dbus_message_set_sender (message, sender))
463 BUS_SET_OOM (&error);
469 /* For monitors' benefit: we don't want the sender to be able to
470 * trick the monitor by supplying a forged sender, and we also
471 * don't want the message to have no sender at all. */
472 if (!dbus_message_set_sender (message, ":not.active.yet"))
474 BUS_SET_OOM (&error);
479 /* We need to refetch the service name here, because
480 * dbus_message_set_sender can cause the header to be
481 * reallocated, and thus the service_name pointer will become
484 service_name = dbus_message_get_destination (message);
486 if (!bus_transaction_capture (transaction, connection, message))
488 BUS_SET_OOM (&error);
493 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
495 BusDeferredMessage *deferred_message;
497 switch (bus_context_check_security_policy (context, transaction,
498 connection, NULL, NULL, message, &error,
501 case BUS_RESULT_TRUE:
503 case BUS_RESULT_FALSE:
504 _dbus_verbose ("Security policy rejected message\n");
506 case BUS_RESULT_LATER:
507 /* Disable dispatching messages from the sender,
508 * roll back and dispatch the message once the policy result is available */
509 bus_deferred_message_disable_sender(deferred_message);
510 bus_transaction_cancel_and_free (transaction);
512 result = DBUS_HANDLER_RESULT_LATER;
516 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
517 switch (bus_driver_handle_message (connection, transaction, message, &error))
519 case BUS_RESULT_TRUE:
521 case BUS_RESULT_FALSE:
523 case BUS_RESULT_LATER:
524 bus_transaction_cancel_and_free (transaction);
526 result = DBUS_HANDLER_RESULT_LATER;
530 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
532 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
533 dbus_connection_close (connection);
536 else if (service_name != NULL) /* route to named service */
538 DBusString service_string;
540 BusRegistry *registry;
542 _dbus_assert (service_name != NULL);
544 registry = bus_connection_get_registry (connection);
546 _dbus_string_init_const (&service_string, service_name);
547 service = bus_registry_lookup (registry, &service_string);
549 if (service == NULL && dbus_message_get_auto_start (message))
551 BusActivation *activation;
552 /* We can't do the security policy check here, since the addressed
553 * recipient service doesn't exist yet. We do it before sending the
554 * message after the service has been created.
556 activation = bus_connection_get_activation (connection);
558 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
559 message, service_name, &error))
561 _DBUS_ASSERT_ERROR_IS_SET (&error);
562 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
568 else if (service == NULL)
570 dbus_set_error (&error,
571 DBUS_ERROR_NAME_HAS_NO_OWNER,
572 "Name \"%s\" does not exist",
578 addressed_recipient = bus_service_get_primary_owners_connection (service);
579 _dbus_assert (addressed_recipient != NULL);
583 /* Now send the message to its destination (or not, if
584 * addressed_recipient == NULL), and match it against other connections'
587 switch (bus_dispatch_matches (transaction, connection, addressed_recipient, message, NULL, &error))
589 case BUS_RESULT_TRUE:
590 case BUS_RESULT_FALSE:
592 case BUS_RESULT_LATER:
593 /* Roll back and dispatch the message once the policy result is available */
594 bus_transaction_cancel_and_free (transaction);
596 result = DBUS_HANDLER_RESULT_LATER;
601 if (dbus_error_is_set (&error))
603 /* Even if we disconnected it, pretend to send it any pending error
604 * messages so that monitors can observe them.
606 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
608 bus_connection_send_oom_error (connection, message);
610 /* cancel transaction due to OOM */
611 if (transaction != NULL)
613 bus_transaction_cancel_and_free (transaction);
619 /* Try to send the real error, if no mem to do that, send
622 _dbus_assert (transaction != NULL);
623 if (!bus_transaction_send_error_reply (transaction, connection,
626 bus_connection_send_oom_error (connection, message);
628 /* cancel transaction due to OOM */
629 if (transaction != NULL)
631 bus_transaction_cancel_and_free (transaction);
638 dbus_error_free (&error);
641 if (transaction != NULL)
643 bus_transaction_execute_and_free (transaction);
646 dbus_connection_unref (connection);
651 static DBusHandlerResult
652 bus_dispatch_message_filter (DBusConnection *connection,
653 DBusMessage *message,
656 return bus_dispatch (connection, message);
660 bus_dispatch_add_connection (DBusConnection *connection)
662 if (!dbus_connection_add_filter (connection,
663 bus_dispatch_message_filter,
671 bus_dispatch_remove_connection (DBusConnection *connection)
673 /* Here we tell the bus driver that we want to get off. */
674 bus_driver_remove_connection (connection);
676 dbus_connection_remove_filter (connection,
677 bus_dispatch_message_filter,
681 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
687 /* This is used to know whether we need to block in order to finish
688 * sending a message, or whether the initial dbus_connection_send()
689 * already flushed the queue.
691 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
693 typedef dbus_bool_t (* Check1Func) (BusContext *context);
694 typedef dbus_bool_t (* Check2Func) (BusContext *context,
695 DBusConnection *connection);
697 static dbus_bool_t check_no_leftovers (BusContext *context);
700 block_connection_until_message_from_bus (BusContext *context,
701 DBusConnection *connection,
702 const char *what_is_expected)
704 _dbus_verbose ("expecting: %s\n", what_is_expected);
706 while (dbus_connection_get_dispatch_status (connection) ==
707 DBUS_DISPATCH_COMPLETE &&
708 dbus_connection_get_is_connected (connection))
710 bus_test_run_bus_loop (context, TRUE);
711 bus_test_run_clients_loop (FALSE);
716 spin_connection_until_authenticated (BusContext *context,
717 DBusConnection *connection)
719 _dbus_verbose ("Spinning to auth connection %p\n", connection);
720 while (!dbus_connection_get_is_authenticated (connection) &&
721 dbus_connection_get_is_connected (connection))
723 bus_test_run_bus_loop (context, FALSE);
724 bus_test_run_clients_loop (FALSE);
726 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
729 /* compensate for fact that pop_message() can return #NULL due to OOM */
731 pop_message_waiting_for_memory (DBusConnection *connection)
733 while (dbus_connection_get_dispatch_status (connection) ==
734 DBUS_DISPATCH_NEED_MEMORY)
735 _dbus_wait_for_memory ();
737 return dbus_connection_pop_message (connection);
741 borrow_message_waiting_for_memory (DBusConnection *connection)
743 while (dbus_connection_get_dispatch_status (connection) ==
744 DBUS_DISPATCH_NEED_MEMORY)
745 _dbus_wait_for_memory ();
747 return dbus_connection_borrow_message (connection);
751 warn_unexpected_real (DBusConnection *connection,
752 DBusMessage *message,
753 const char *expected,
754 const char *function,
758 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
760 dbus_message_get_interface (message) ?
761 dbus_message_get_interface (message) : "(unset)",
762 dbus_message_get_member (message) ?
763 dbus_message_get_member (message) : "(unset)",
764 dbus_message_get_error_name (message) ?
765 dbus_message_get_error_name (message) : "(unset)",
769 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
770 function, line, connection, expected);
773 #define warn_unexpected(connection, message, expected) \
774 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
777 verbose_message_received (DBusConnection *connection,
778 DBusMessage *message)
780 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
781 dbus_message_get_interface (message) ?
782 dbus_message_get_interface (message) : "(unset)",
783 dbus_message_get_member (message) ?
784 dbus_message_get_member (message) : "(unset)",
785 dbus_message_get_error_name (message) ?
786 dbus_message_get_error_name (message) : "(unset)",
799 ServiceInfoKind expected_kind;
800 const char *expected_service_name;
802 DBusConnection *skip_connection;
804 } CheckServiceOwnerChangedData;
807 check_service_owner_changed_foreach (DBusConnection *connection,
810 CheckServiceOwnerChangedData *d = data;
811 DBusMessage *message;
813 const char *service_name, *old_owner, *new_owner;
815 if (d->expected_kind == SERVICE_CREATED
816 && connection == d->skip_connection)
819 dbus_error_init (&error);
822 message = pop_message_waiting_for_memory (connection);
825 block_connection_until_message_from_bus (d->context, connection, "NameOwnerChanged");
826 message = pop_message_waiting_for_memory (connection);
829 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
830 connection, "NameOwnerChanged");
834 else if (!dbus_message_is_signal (message,
838 warn_unexpected (connection, message, "NameOwnerChanged");
844 reget_service_info_data:
849 dbus_message_get_args (message, &error,
850 DBUS_TYPE_STRING, &service_name,
851 DBUS_TYPE_STRING, &old_owner,
852 DBUS_TYPE_STRING, &new_owner,
855 if (dbus_error_is_set (&error))
857 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
859 dbus_error_free (&error);
860 _dbus_wait_for_memory ();
861 goto reget_service_info_data;
865 _dbus_warn ("Did not get the expected arguments\n");
870 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
871 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
872 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
874 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
878 if (strcmp (service_name, d->expected_service_name) != 0)
880 _dbus_warn ("expected info on service %s, got info on %s\n",
881 d->expected_service_name,
886 if (*service_name == ':' && new_owner[0]
887 && strcmp (service_name, new_owner) != 0)
889 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
890 service_name, old_owner, new_owner);
898 dbus_error_free (&error);
901 dbus_message_unref (message);
908 kill_client_connection (BusContext *context,
909 DBusConnection *connection)
913 CheckServiceOwnerChangedData socd;
915 _dbus_verbose ("killing connection %p\n", connection);
917 s = dbus_bus_get_unique_name (connection);
918 _dbus_assert (s != NULL);
920 while ((base_service = _dbus_strdup (s)) == NULL)
921 _dbus_wait_for_memory ();
923 dbus_connection_ref (connection);
925 /* kick in the disconnect handler that unrefs the connection */
926 dbus_connection_close (connection);
928 bus_test_run_everything (context);
930 _dbus_assert (bus_test_client_listed (connection));
932 /* Run disconnect handler in test.c */
933 if (bus_connection_dispatch_one_message (connection))
934 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
936 _dbus_assert (!dbus_connection_get_is_connected (connection));
937 dbus_connection_unref (connection);
939 _dbus_assert (!bus_test_client_listed (connection));
941 socd.expected_kind = SERVICE_DELETED;
942 socd.expected_service_name = base_service;
944 socd.skip_connection = NULL;
945 socd.context = context;
947 bus_test_clients_foreach (check_service_owner_changed_foreach,
950 dbus_free (base_service);
953 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
955 if (!check_no_leftovers (context))
956 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
960 kill_client_connection_unchecked (DBusConnection *connection)
962 /* This kills the connection without expecting it to affect
963 * the rest of the bus.
965 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
967 dbus_connection_ref (connection);
968 dbus_connection_close (connection);
969 /* dispatching disconnect handler will unref once */
970 if (bus_connection_dispatch_one_message (connection))
971 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
973 _dbus_assert (!bus_test_client_listed (connection));
974 dbus_connection_unref (connection);
980 } CheckNoMessagesData;
983 check_no_messages_foreach (DBusConnection *connection,
986 CheckNoMessagesData *d = data;
987 DBusMessage *message;
989 message = pop_message_waiting_for_memory (connection);
992 warn_unexpected (connection, message, "no messages");
998 dbus_message_unref (message);
1003 check_no_leftovers (BusContext *context)
1005 CheckNoMessagesData nmd;
1008 bus_test_clients_foreach (check_no_messages_foreach,
1013 _dbus_verbose ("leftover message found\n");
1020 /* returns TRUE if the correct thing happens,
1021 * but the correct thing may include OOM errors.
1024 check_hello_message (BusContext *context,
1025 DBusConnection *connection)
1027 DBusMessage *message;
1028 DBusMessage *name_message;
1029 dbus_uint32_t serial;
1033 const char *acquired;
1036 dbus_error_init (&error);
1040 name_message = NULL;
1042 _dbus_verbose ("check_hello_message for %p\n", connection);
1044 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1046 DBUS_INTERFACE_DBUS,
1049 if (message == NULL)
1052 dbus_connection_ref (connection); /* because we may get disconnected */
1054 if (!dbus_connection_send (connection, message, &serial))
1056 dbus_message_unref (message);
1057 dbus_connection_unref (connection);
1061 _dbus_assert (dbus_message_has_signature (message, ""));
1063 dbus_message_unref (message);
1066 if (!dbus_connection_get_is_connected (connection))
1068 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1070 dbus_connection_unref (connection);
1075 /* send our message */
1076 bus_test_run_clients_loop (SEND_PENDING (connection));
1078 if (!dbus_connection_get_is_connected (connection))
1080 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1082 dbus_connection_unref (connection);
1087 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1089 if (!dbus_connection_get_is_connected (connection))
1091 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
1093 dbus_connection_unref (connection);
1098 message = pop_message_waiting_for_memory (connection);
1099 if (message == NULL)
1101 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1102 "Hello", serial, connection);
1106 verbose_message_received (connection, message);
1108 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1110 _dbus_warn ("Message has wrong sender %s\n",
1111 dbus_message_get_sender (message) ?
1112 dbus_message_get_sender (message) : "(none)");
1116 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1118 if (dbus_message_is_error (message,
1119 DBUS_ERROR_NO_MEMORY))
1121 ; /* good, this is a valid response */
1125 warn_unexpected (connection, message, "not this error");
1132 CheckServiceOwnerChangedData socd;
1134 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1136 ; /* good, expected */
1140 warn_unexpected (connection, message, "method return for Hello");
1145 retry_get_hello_name:
1146 if (!dbus_message_get_args (message, &error,
1147 DBUS_TYPE_STRING, &name,
1150 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1152 _dbus_verbose ("no memory to get service name arg from hello\n");
1153 dbus_error_free (&error);
1154 _dbus_wait_for_memory ();
1155 goto retry_get_hello_name;
1159 _dbus_assert (dbus_error_is_set (&error));
1160 _dbus_warn ("Did not get the expected single string argument to hello\n");
1165 _dbus_verbose ("Got hello name: %s\n", name);
1167 while (!dbus_bus_set_unique_name (connection, name))
1168 _dbus_wait_for_memory ();
1170 socd.expected_kind = SERVICE_CREATED;
1171 socd.expected_service_name = name;
1172 socd.failed = FALSE;
1173 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
1174 socd.context = context;
1176 bus_test_clients_foreach (check_service_owner_changed_foreach,
1182 name_message = message;
1183 /* Client should also have gotten ServiceAcquired */
1185 message = pop_message_waiting_for_memory (connection);
1186 if (message == NULL)
1188 block_connection_until_message_from_bus (context, connection, "signal NameAcquired");
1189 message = pop_message_waiting_for_memory (connection);
1190 if (message == NULL)
1192 _dbus_warn ("Expecting %s, got nothing\n",
1197 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
1200 _dbus_warn ("Expecting %s, got smthg else\n",
1205 retry_get_acquired_name:
1206 if (!dbus_message_get_args (message, &error,
1207 DBUS_TYPE_STRING, &acquired,
1210 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1212 _dbus_verbose ("no memory to get service name arg from acquired\n");
1213 dbus_error_free (&error);
1214 _dbus_wait_for_memory ();
1215 goto retry_get_acquired_name;
1219 _dbus_assert (dbus_error_is_set (&error));
1220 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
1225 _dbus_verbose ("Got acquired name: %s\n", acquired);
1227 if (strcmp (acquired, name) != 0)
1229 _dbus_warn ("Acquired name is %s but expected %s\n",
1236 if (!check_no_leftovers (context))
1242 _dbus_verbose ("ending - retval = %d\n", retval);
1244 dbus_error_free (&error);
1247 dbus_message_unref (message);
1250 dbus_message_unref (name_message);
1252 dbus_connection_unref (connection);
1257 /* returns TRUE if the correct thing happens,
1258 * but the correct thing may include OOM errors.
1261 check_double_hello_message (BusContext *context,
1262 DBusConnection *connection)
1264 DBusMessage *message;
1265 dbus_uint32_t serial;
1270 dbus_error_init (&error);
1273 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1275 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1277 DBUS_INTERFACE_DBUS,
1280 if (message == NULL)
1283 if (!dbus_connection_send (connection, message, &serial))
1285 dbus_message_unref (message);
1289 dbus_message_unref (message);
1292 /* send our message */
1293 bus_test_run_clients_loop (SEND_PENDING (connection));
1295 dbus_connection_ref (connection); /* because we may get disconnected */
1296 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1298 if (!dbus_connection_get_is_connected (connection))
1300 _dbus_verbose ("connection was disconnected\n");
1302 dbus_connection_unref (connection);
1307 dbus_connection_unref (connection);
1309 message = pop_message_waiting_for_memory (connection);
1310 if (message == NULL)
1312 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1313 "Hello", serial, connection);
1317 verbose_message_received (connection, message);
1319 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1321 _dbus_warn ("Message has wrong sender %s\n",
1322 dbus_message_get_sender (message) ?
1323 dbus_message_get_sender (message) : "(none)");
1327 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1329 warn_unexpected (connection, message, "method return for Hello");
1333 if (!check_no_leftovers (context))
1339 dbus_error_free (&error);
1342 dbus_message_unref (message);
1347 /* returns TRUE if the correct thing happens,
1348 * but the correct thing may include OOM errors.
1351 check_get_connection_unix_user (BusContext *context,
1352 DBusConnection *connection)
1354 DBusMessage *message;
1355 dbus_uint32_t serial;
1358 const char *base_service_name;
1362 dbus_error_init (&error);
1365 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1367 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1369 DBUS_INTERFACE_DBUS,
1370 "GetConnectionUnixUser");
1372 if (message == NULL)
1375 base_service_name = dbus_bus_get_unique_name (connection);
1377 if (!dbus_message_append_args (message,
1378 DBUS_TYPE_STRING, &base_service_name,
1381 dbus_message_unref (message);
1385 if (!dbus_connection_send (connection, message, &serial))
1387 dbus_message_unref (message);
1391 /* send our message */
1392 bus_test_run_clients_loop (SEND_PENDING (connection));
1394 dbus_message_unref (message);
1397 dbus_connection_ref (connection); /* because we may get disconnected */
1398 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1400 if (!dbus_connection_get_is_connected (connection))
1402 _dbus_verbose ("connection was disconnected\n");
1404 dbus_connection_unref (connection);
1409 dbus_connection_unref (connection);
1411 message = pop_message_waiting_for_memory (connection);
1412 if (message == NULL)
1414 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1415 "GetConnectionUnixUser", serial, connection);
1419 verbose_message_received (connection, message);
1421 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1423 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1425 ; /* good, this is a valid response */
1428 else if (dbus_message_is_error (message, DBUS_ERROR_FAILED))
1430 /* this is OK, Unix uids aren't meaningful on Windows */
1435 warn_unexpected (connection, message, "not this error");
1442 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1444 ; /* good, expected */
1448 warn_unexpected (connection, message,
1449 "method_return for GetConnectionUnixUser");
1456 if (!dbus_message_get_args (message, &error,
1457 DBUS_TYPE_UINT32, &uid,
1460 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1462 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1463 dbus_error_free (&error);
1464 _dbus_wait_for_memory ();
1465 goto retry_get_property;
1469 _dbus_assert (dbus_error_is_set (&error));
1470 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1476 if (!check_no_leftovers (context))
1482 dbus_error_free (&error);
1485 dbus_message_unref (message);
1490 /* returns TRUE if the correct thing happens,
1491 * but the correct thing may include OOM errors.
1494 check_get_connection_unix_process_id (BusContext *context,
1495 DBusConnection *connection)
1497 DBusMessage *message;
1498 dbus_uint32_t serial;
1501 const char *base_service_name;
1507 dbus_error_init (&error);
1510 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1512 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1514 DBUS_INTERFACE_DBUS,
1515 "GetConnectionUnixProcessID");
1517 if (message == NULL)
1520 base_service_name = dbus_bus_get_unique_name (connection);
1522 if (!dbus_message_append_args (message,
1523 DBUS_TYPE_STRING, &base_service_name,
1526 dbus_message_unref (message);
1530 if (!dbus_connection_send (connection, message, &serial))
1532 dbus_message_unref (message);
1536 /* send our message */
1537 bus_test_run_clients_loop (SEND_PENDING (connection));
1539 dbus_message_unref (message);
1542 dbus_connection_ref (connection); /* because we may get disconnected */
1543 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1545 if (!dbus_connection_get_is_connected (connection))
1547 _dbus_verbose ("connection was disconnected\n");
1549 dbus_connection_unref (connection);
1554 dbus_connection_unref (connection);
1556 message = pop_message_waiting_for_memory (connection);
1557 if (message == NULL)
1559 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1560 "GetConnectionUnixProcessID", serial, connection);
1564 verbose_message_received (connection, message);
1566 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1568 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1570 ; /* good, this is a valid response */
1573 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1575 /* We are expecting this error, since we know in the test suite we aren't
1576 * talking to a client running on UNIX
1578 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1583 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
1584 defined(__linux__) || \
1585 defined(__OpenBSD__)
1586 /* In principle NetBSD should also be in that list, but
1587 * its implementation of PID-passing doesn't work
1588 * over a socketpair() as used in the debug-pipe transport.
1589 * We test this functionality in a more realistic situation
1590 * in test/dbus-daemon.c. */
1591 warn_unexpected (connection, message, "not this error");
1595 _dbus_verbose ("does not support GetConnectionUnixProcessID but perhaps that's OK?\n");
1602 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1605 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1607 ; /* good, expected */
1611 warn_unexpected (connection, message,
1612 "method_return for GetConnectionUnixProcessID");
1619 if (!dbus_message_get_args (message, &error,
1620 DBUS_TYPE_UINT32, &pid,
1623 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1625 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1626 dbus_error_free (&error);
1627 _dbus_wait_for_memory ();
1628 goto retry_get_property;
1632 _dbus_assert (dbus_error_is_set (&error));
1633 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1639 /* test if returned pid is the same as our own pid
1641 * @todo It would probably be good to restructure the tests
1642 * in a way so our parent is the bus that we're testing
1643 * cause then we can test that the pid returned matches
1646 if (pid != (dbus_uint32_t) _dbus_getpid ())
1648 _dbus_assert (dbus_error_is_set (&error));
1649 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1653 #endif /* !DBUS_WIN */
1656 if (!check_no_leftovers (context))
1662 dbus_error_free (&error);
1665 dbus_message_unref (message);
1670 /* returns TRUE if the correct thing happens,
1671 * but the correct thing may include OOM errors.
1674 check_add_match (BusContext *context,
1675 DBusConnection *connection,
1678 DBusMessage *message;
1680 dbus_uint32_t serial;
1684 dbus_error_init (&error);
1687 _dbus_verbose ("check_add_match for connection %p, rule %s\n",
1690 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1692 DBUS_INTERFACE_DBUS,
1695 if (message == NULL)
1698 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &rule,
1701 dbus_message_unref (message);
1705 if (!dbus_connection_send (connection, message, &serial))
1707 dbus_message_unref (message);
1711 dbus_message_unref (message);
1714 dbus_connection_ref (connection); /* because we may get disconnected */
1716 /* send our message */
1717 bus_test_run_clients_loop (SEND_PENDING (connection));
1719 if (!dbus_connection_get_is_connected (connection))
1721 _dbus_verbose ("connection was disconnected\n");
1723 dbus_connection_unref (connection);
1728 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1730 if (!dbus_connection_get_is_connected (connection))
1732 _dbus_verbose ("connection was disconnected\n");
1734 dbus_connection_unref (connection);
1739 dbus_connection_unref (connection);
1741 message = pop_message_waiting_for_memory (connection);
1742 if (message == NULL)
1744 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1745 "AddMatch", serial, connection);
1749 verbose_message_received (connection, message);
1751 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1753 _dbus_warn ("Message has wrong sender %s\n",
1754 dbus_message_get_sender (message) ?
1755 dbus_message_get_sender (message) : "(none)");
1759 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1761 if (dbus_message_is_error (message,
1762 DBUS_ERROR_NO_MEMORY))
1764 ; /* good, this is a valid response */
1768 warn_unexpected (connection, message, "not this error");
1775 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1777 ; /* good, expected */
1778 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1782 warn_unexpected (connection, message, "method return for AddMatch");
1788 if (!check_no_leftovers (context))
1794 dbus_error_free (&error);
1797 dbus_message_unref (message);
1802 #ifdef DBUS_ENABLE_STATS
1803 /* returns TRUE if the correct thing happens,
1804 * but the correct thing may include OOM errors.
1807 check_get_all_match_rules (BusContext *context,
1808 DBusConnection *connection)
1810 DBusMessage *message;
1812 dbus_uint32_t serial;
1816 dbus_error_init (&error);
1819 _dbus_verbose ("check_get_all_match_rules for connection %p\n",
1822 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1824 BUS_INTERFACE_STATS,
1825 "GetAllMatchRules");
1827 if (message == NULL)
1830 if (!dbus_connection_send (connection, message, &serial))
1832 dbus_message_unref (message);
1836 dbus_message_unref (message);
1839 dbus_connection_ref (connection); /* because we may get disconnected */
1841 /* send our message */
1842 bus_test_run_clients_loop (SEND_PENDING (connection));
1844 if (!dbus_connection_get_is_connected (connection))
1846 _dbus_verbose ("connection was disconnected\n");
1848 dbus_connection_unref (connection);
1853 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1855 if (!dbus_connection_get_is_connected (connection))
1857 _dbus_verbose ("connection was disconnected\n");
1859 dbus_connection_unref (connection);
1864 dbus_connection_unref (connection);
1866 message = pop_message_waiting_for_memory (connection);
1867 if (message == NULL)
1869 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1870 "AddMatch", serial, connection);
1874 verbose_message_received (connection, message);
1876 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1878 _dbus_warn ("Message has wrong sender %s\n",
1879 dbus_message_get_sender (message) ?
1880 dbus_message_get_sender (message) : "(none)");
1884 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1886 if (dbus_message_is_error (message,
1887 DBUS_ERROR_NO_MEMORY))
1889 ; /* good, this is a valid response */
1893 warn_unexpected (connection, message, "not this error");
1900 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1902 ; /* good, expected */
1903 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1907 warn_unexpected (connection, message, "method return for AddMatch");
1913 if (!check_no_leftovers (context))
1919 dbus_error_free (&error);
1922 dbus_message_unref (message);
1928 /* returns TRUE if the correct thing happens,
1929 * but the correct thing may include OOM errors.
1932 check_hello_connection (BusContext *context)
1934 DBusConnection *connection;
1937 dbus_error_init (&error);
1939 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1940 if (connection == NULL)
1942 _DBUS_ASSERT_ERROR_IS_SET (&error);
1943 dbus_error_free (&error);
1947 if (!bus_setup_debug_client (connection))
1949 dbus_connection_close (connection);
1950 dbus_connection_unref (connection);
1954 spin_connection_until_authenticated (context, connection);
1956 if (!check_hello_message (context, connection))
1959 if (dbus_bus_get_unique_name (connection) == NULL)
1961 /* We didn't successfully register, so we can't
1962 * do the usual kill_client_connection() checks
1964 kill_client_connection_unchecked (connection);
1968 if (!check_add_match (context, connection, ""))
1971 kill_client_connection (context, connection);
1977 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1979 /* returns TRUE if the correct thing happens,
1980 * but the correct thing may include OOM errors.
1983 check_nonexistent_service_no_auto_start (BusContext *context,
1984 DBusConnection *connection)
1986 DBusMessage *message;
1987 dbus_uint32_t serial;
1989 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1990 dbus_uint32_t flags;
1992 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1994 DBUS_INTERFACE_DBUS,
1995 "StartServiceByName");
1997 if (message == NULL)
2000 dbus_message_set_auto_start (message, FALSE);
2003 if (!dbus_message_append_args (message,
2004 DBUS_TYPE_STRING, &nonexistent,
2005 DBUS_TYPE_UINT32, &flags,
2008 dbus_message_unref (message);
2012 if (!dbus_connection_send (connection, message, &serial))
2014 dbus_message_unref (message);
2018 dbus_message_unref (message);
2021 bus_test_run_everything (context);
2022 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
2023 bus_test_run_everything (context);
2025 if (!dbus_connection_get_is_connected (connection))
2027 _dbus_verbose ("connection was disconnected\n");
2033 message = pop_message_waiting_for_memory (connection);
2034 if (message == NULL)
2036 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2037 "StartServiceByName", serial, connection);
2041 verbose_message_received (connection, message);
2043 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2045 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2047 _dbus_warn ("Message has wrong sender %s\n",
2048 dbus_message_get_sender (message) ?
2049 dbus_message_get_sender (message) : "(none)");
2053 if (dbus_message_is_error (message,
2054 DBUS_ERROR_NO_MEMORY))
2056 ; /* good, this is a valid response */
2058 else if (dbus_message_is_error (message,
2059 DBUS_ERROR_SERVICE_UNKNOWN))
2061 ; /* good, this is expected also */
2065 warn_unexpected (connection, message, "not this error");
2071 _dbus_warn ("Did not expect to successfully activate %s\n",
2072 NONEXISTENT_SERVICE_NAME);
2080 dbus_message_unref (message);
2085 /* returns TRUE if the correct thing happens,
2086 * but the correct thing may include OOM errors.
2089 check_nonexistent_service_auto_start (BusContext *context,
2090 DBusConnection *connection)
2092 DBusMessage *message;
2093 dbus_uint32_t serial;
2096 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
2097 "/org/freedesktop/TestSuite",
2098 "org.freedesktop.TestSuite",
2101 if (message == NULL)
2104 if (!dbus_connection_send (connection, message, &serial))
2106 dbus_message_unref (message);
2110 dbus_message_unref (message);
2113 bus_test_run_everything (context);
2114 block_connection_until_message_from_bus (context, connection, "reply to Echo");
2115 bus_test_run_everything (context);
2117 if (!dbus_connection_get_is_connected (connection))
2119 _dbus_verbose ("connection was disconnected\n");
2125 message = pop_message_waiting_for_memory (connection);
2127 if (message == NULL)
2129 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2130 "Echo message (auto activation)", serial, connection);
2134 verbose_message_received (connection, message);
2136 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2138 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2140 _dbus_warn ("Message has wrong sender %s\n",
2141 dbus_message_get_sender (message) ?
2142 dbus_message_get_sender (message) : "(none)");
2146 if (dbus_message_is_error (message,
2147 DBUS_ERROR_NO_MEMORY))
2149 ; /* good, this is a valid response */
2151 else if (dbus_message_is_error (message,
2152 DBUS_ERROR_SERVICE_UNKNOWN))
2154 ; /* good, this is expected also */
2158 warn_unexpected (connection, message, "not this error");
2164 _dbus_warn ("Did not expect to successfully activate %s\n",
2165 NONEXISTENT_SERVICE_NAME);
2173 dbus_message_unref (message);
2179 check_base_service_activated (BusContext *context,
2180 DBusConnection *connection,
2181 DBusMessage *initial_message,
2182 const char **base_service_p)
2184 DBusMessage *message;
2187 const char *base_service, *base_service_from_bus, *old_owner;
2191 dbus_error_init (&error);
2192 base_service = NULL;
2194 base_service_from_bus = NULL;
2196 message = initial_message;
2197 dbus_message_ref (message);
2199 if (dbus_message_is_signal (message,
2200 DBUS_INTERFACE_DBUS,
2201 "NameOwnerChanged"))
2203 CheckServiceOwnerChangedData socd;
2205 reget_service_name_arg:
2206 base_service = NULL;
2208 base_service_from_bus = NULL;
2210 if (!dbus_message_get_args (message, &error,
2211 DBUS_TYPE_STRING, &base_service,
2212 DBUS_TYPE_STRING, &old_owner,
2213 DBUS_TYPE_STRING, &base_service_from_bus,
2216 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2218 dbus_error_free (&error);
2219 _dbus_wait_for_memory ();
2220 goto reget_service_name_arg;
2224 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2225 "NameOwnerChanged (creation)",
2231 if (*base_service != ':')
2233 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
2238 if (strcmp (base_service, base_service_from_bus) != 0)
2240 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
2241 base_service, base_service_from_bus);
2247 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
2252 socd.expected_kind = SERVICE_CREATED;
2253 socd.expected_service_name = base_service;
2254 socd.failed = FALSE;
2255 socd.skip_connection = connection;
2256 socd.context = context;
2258 bus_test_clients_foreach (check_service_owner_changed_foreach,
2266 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
2272 *base_service_p = base_service;
2278 dbus_message_unref (message);
2279 dbus_error_free (&error);
2285 check_service_activated (BusContext *context,
2286 DBusConnection *connection,
2287 const char *activated_name,
2288 const char *base_service_name,
2289 DBusMessage *initial_message)
2291 DBusMessage *message;
2294 dbus_uint32_t activation_result;
2298 dbus_error_init (&error);
2300 message = initial_message;
2301 dbus_message_ref (message);
2303 if (dbus_message_is_signal (message,
2304 DBUS_INTERFACE_DBUS,
2305 "NameOwnerChanged"))
2307 CheckServiceOwnerChangedData socd;
2308 const char *service_name, *base_service_from_bus, *old_owner;
2310 reget_service_name_arg:
2311 service_name = NULL;
2313 base_service_from_bus = NULL;
2315 if (!dbus_message_get_args (message, &error,
2316 DBUS_TYPE_STRING, &service_name,
2317 DBUS_TYPE_STRING, &old_owner,
2318 DBUS_TYPE_STRING, &base_service_from_bus,
2321 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2323 dbus_error_free (&error);
2324 _dbus_wait_for_memory ();
2325 goto reget_service_name_arg;
2329 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2330 "NameOwnerChanged (creation)",
2336 if (strcmp (service_name, activated_name) != 0)
2338 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2339 activated_name, service_name);
2343 if (strcmp (base_service_name, base_service_from_bus) != 0)
2345 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
2346 base_service_from_bus, base_service_name);
2352 _dbus_warn ("expected a %s, got a %s\n",
2353 "NameOwnerChanged (creation)",
2354 "NameOwnerChanged (change)");
2358 socd.expected_kind = SERVICE_CREATED;
2359 socd.skip_connection = connection;
2360 socd.failed = FALSE;
2361 socd.expected_service_name = service_name;
2362 socd.context = context;
2364 bus_test_clients_foreach (check_service_owner_changed_foreach,
2370 dbus_message_unref (message);
2371 service_name = NULL;
2373 base_service_from_bus = NULL;
2375 message = pop_message_waiting_for_memory (connection);
2376 if (message == NULL)
2378 _dbus_warn ("Expected a reply to %s, got nothing\n",
2379 "StartServiceByName");
2385 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2390 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2392 warn_unexpected (connection, message, "reply to StartServiceByName");
2397 activation_result = 0;
2398 if (!dbus_message_get_args (message, &error,
2399 DBUS_TYPE_UINT32, &activation_result,
2402 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2404 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
2405 "StartServiceByName", error.message);
2409 dbus_error_free (&error);
2413 if (activation_result == DBUS_START_REPLY_SUCCESS)
2415 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2419 _dbus_warn ("Activation result was %u, no good.\n",
2425 dbus_message_unref (message);
2428 if (!check_no_leftovers (context))
2430 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2438 dbus_message_unref (message);
2439 dbus_error_free (&error);
2445 check_service_auto_activated (BusContext *context,
2446 DBusConnection *connection,
2447 const char *activated_name,
2448 const char *base_service_name,
2449 DBusMessage *initial_message)
2451 DBusMessage *message;
2457 dbus_error_init (&error);
2459 message = initial_message;
2460 dbus_message_ref (message);
2462 if (dbus_message_is_signal (message,
2463 DBUS_INTERFACE_DBUS,
2464 "NameOwnerChanged"))
2466 const char *service_name;
2467 CheckServiceOwnerChangedData socd;
2469 reget_service_name_arg:
2470 if (!dbus_message_get_args (message, &error,
2471 DBUS_TYPE_STRING, &service_name,
2474 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2476 dbus_error_free (&error);
2477 _dbus_wait_for_memory ();
2478 goto reget_service_name_arg;
2482 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2485 dbus_error_free (&error);
2490 if (strcmp (service_name, activated_name) != 0)
2492 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2493 activated_name, service_name);
2497 socd.expected_kind = SERVICE_CREATED;
2498 socd.expected_service_name = service_name;
2499 socd.failed = FALSE;
2500 socd.skip_connection = connection;
2501 socd.context = context;
2503 bus_test_clients_foreach (check_service_owner_changed_foreach,
2509 /* Note that this differs from regular activation in that we don't get a
2510 * reply to ActivateService here.
2513 dbus_message_unref (message);
2515 service_name = NULL;
2519 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2528 dbus_message_unref (message);
2534 check_service_deactivated (BusContext *context,
2535 DBusConnection *connection,
2536 const char *activated_name,
2537 const char *base_service)
2540 CheckServiceOwnerChangedData socd;
2544 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2545 * service and the activated_name. The base service
2546 * notification is required to come last.
2548 socd.expected_kind = SERVICE_DELETED;
2549 socd.expected_service_name = activated_name;
2550 socd.failed = FALSE;
2551 socd.skip_connection = NULL;
2552 socd.context = context;
2554 bus_test_clients_foreach (check_service_owner_changed_foreach,
2560 socd.expected_kind = SERVICE_DELETED;
2561 socd.expected_service_name = base_service;
2562 socd.failed = FALSE;
2563 socd.skip_connection = NULL;
2564 socd.context = context;
2566 bus_test_clients_foreach (check_service_owner_changed_foreach,
2579 check_send_exit_to_service (BusContext *context,
2580 DBusConnection *connection,
2581 const char *service_name,
2582 const char *base_service)
2584 dbus_bool_t got_error;
2585 DBusMessage *message;
2586 dbus_uint32_t serial;
2589 _dbus_verbose ("Sending exit message to the test service\n");
2593 /* Kill off the test service by sending it a quit message */
2594 message = dbus_message_new_method_call (service_name,
2595 "/org/freedesktop/TestSuite",
2596 "org.freedesktop.TestSuite",
2599 if (message == NULL)
2601 /* Do this again; we still need the service to exit... */
2602 if (!check_send_exit_to_service (context, connection,
2603 service_name, base_service))
2609 if (!dbus_connection_send (connection, message, &serial))
2611 dbus_message_unref (message);
2613 /* Do this again; we still need the service to exit... */
2614 if (!check_send_exit_to_service (context, connection,
2615 service_name, base_service))
2621 dbus_message_unref (message);
2625 bus_test_run_clients_loop (SEND_PENDING (connection));
2627 /* read it in and write it out to test service */
2628 bus_test_run_bus_loop (context, FALSE);
2630 /* see if we got an error during message bus dispatching */
2631 bus_test_run_clients_loop (FALSE);
2632 message = borrow_message_waiting_for_memory (connection);
2633 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2636 dbus_connection_return_message (connection, message);
2642 /* If no error, wait for the test service to exit */
2643 block_connection_until_message_from_bus (context, connection, "test service to exit");
2645 bus_test_run_everything (context);
2650 message = pop_message_waiting_for_memory (connection);
2651 _dbus_assert (message != NULL);
2653 if (dbus_message_get_reply_serial (message) != serial)
2655 warn_unexpected (connection, message,
2656 "error with the correct reply serial");
2660 if (!dbus_message_is_error (message,
2661 DBUS_ERROR_NO_MEMORY))
2663 warn_unexpected (connection, message,
2664 "a no memory error from asking test service to exit");
2668 _dbus_verbose ("Got error %s when asking test service to exit\n",
2669 dbus_message_get_error_name (message));
2671 /* Do this again; we still need the service to exit... */
2672 if (!check_send_exit_to_service (context, connection,
2673 service_name, base_service))
2678 if (!check_service_deactivated (context, connection,
2679 service_name, base_service))
2682 /* Should now have a NoReply error from the Exit() method
2683 * call; it should have come after all the deactivation
2686 message = pop_message_waiting_for_memory (connection);
2688 if (message == NULL)
2690 warn_unexpected (connection, NULL,
2691 "reply to Exit() method call");
2694 if (!dbus_message_is_error (message,
2695 DBUS_ERROR_NO_REPLY))
2697 warn_unexpected (connection, message,
2698 "NoReply error from Exit() method call");
2702 if (dbus_message_get_reply_serial (message) != serial)
2704 warn_unexpected (connection, message,
2705 "error with the correct reply serial");
2709 _dbus_verbose ("Got error %s after test service exited\n",
2710 dbus_message_get_error_name (message));
2712 if (!check_no_leftovers (context))
2714 _dbus_warn ("Messages were left over after %s\n",
2715 _DBUS_FUNCTION_NAME);
2724 dbus_message_unref (message);
2730 check_got_error (BusContext *context,
2731 DBusConnection *connection,
2732 const char *first_error_name,
2735 DBusMessage *message;
2738 dbus_bool_t error_found;
2739 const char *error_name;
2743 message = pop_message_waiting_for_memory (connection);
2744 if (message == NULL)
2746 _dbus_warn ("Did not get an expected error\n");
2750 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2752 warn_unexpected (connection, message, "an error");
2757 error_found = FALSE;
2759 va_start (ap, first_error_name);
2760 error_name = first_error_name;
2761 while (error_name != NULL)
2763 if (dbus_message_is_error (message, error_name))
2768 error_name = va_arg (ap, char*);
2774 _dbus_warn ("Expected error %s or other, got %s instead\n",
2776 dbus_message_get_error_name (message));
2784 dbus_message_unref (message);
2791 GOT_SERVICE_CREATED,
2792 GOT_SERVICE_DELETED,
2797 static GotServiceInfo
2798 check_got_service_info (DBusMessage *message)
2800 GotServiceInfo message_kind;
2802 if (dbus_message_is_signal (message,
2803 DBUS_INTERFACE_DBUS,
2804 "NameOwnerChanged"))
2807 const char *service_name, *old_owner, *new_owner;
2808 dbus_error_init (&error);
2810 reget_service_info_data:
2811 service_name = NULL;
2815 dbus_message_get_args (message, &error,
2816 DBUS_TYPE_STRING, &service_name,
2817 DBUS_TYPE_STRING, &old_owner,
2818 DBUS_TYPE_STRING, &new_owner,
2820 if (dbus_error_is_set (&error))
2822 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2824 dbus_error_free (&error);
2825 goto reget_service_info_data;
2829 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2830 message_kind = GOT_SOMETHING_ELSE;
2833 else if (!old_owner[0])
2834 message_kind = GOT_SERVICE_CREATED;
2835 else if (!new_owner[0])
2836 message_kind = GOT_SERVICE_DELETED;
2838 message_kind = GOT_SOMETHING_ELSE;
2840 dbus_error_free (&error);
2842 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2843 message_kind = GOT_ERROR;
2845 message_kind = GOT_SOMETHING_ELSE;
2847 return message_kind;
2850 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2852 /* returns TRUE if the correct thing happens,
2853 * but the correct thing may include OOM errors.
2856 check_existent_service_no_auto_start (BusContext *context,
2857 DBusConnection *connection)
2859 DBusMessage *message;
2860 DBusMessage *base_service_message;
2861 const char *base_service;
2862 dbus_uint32_t serial;
2864 const char *existent = EXISTENT_SERVICE_NAME;
2865 dbus_uint32_t flags;
2867 base_service_message = NULL;
2869 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2871 DBUS_INTERFACE_DBUS,
2872 "StartServiceByName");
2874 if (message == NULL)
2877 dbus_message_set_auto_start (message, FALSE);
2880 if (!dbus_message_append_args (message,
2881 DBUS_TYPE_STRING, &existent,
2882 DBUS_TYPE_UINT32, &flags,
2885 dbus_message_unref (message);
2889 if (!dbus_connection_send (connection, message, &serial))
2891 dbus_message_unref (message);
2895 dbus_message_unref (message);
2898 bus_test_run_everything (context);
2900 /* now wait for the message bus to hear back from the activated
2903 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2905 bus_test_run_everything (context);
2907 if (!dbus_connection_get_is_connected (connection))
2909 _dbus_verbose ("connection was disconnected\n");
2915 message = pop_message_waiting_for_memory (connection);
2916 if (message == NULL)
2918 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2919 "StartServiceByName", serial, connection);
2923 verbose_message_received (connection, message);
2924 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2926 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2928 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2930 _dbus_warn ("Message has wrong sender %s\n",
2931 dbus_message_get_sender (message) ?
2932 dbus_message_get_sender (message) : "(none)");
2936 if (dbus_message_is_error (message,
2937 DBUS_ERROR_NO_MEMORY))
2939 ; /* good, this is a valid response */
2941 else if (dbus_message_is_error (message,
2942 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2943 dbus_message_is_error (message,
2944 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2945 dbus_message_is_error (message,
2946 DBUS_ERROR_SPAWN_EXEC_FAILED))
2948 ; /* good, this is expected also */
2952 _dbus_warn ("Did not expect error %s\n",
2953 dbus_message_get_error_name (message));
2959 GotServiceInfo message_kind;
2961 if (!check_base_service_activated (context, connection,
2962 message, &base_service))
2965 base_service_message = message;
2968 /* We may need to block here for the test service to exit or finish up */
2969 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2971 message = dbus_connection_borrow_message (connection);
2972 if (message == NULL)
2974 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2978 message_kind = check_got_service_info (message);
2980 dbus_connection_return_message (connection, message);
2983 switch (message_kind)
2985 case GOT_SOMETHING_ELSE:
2986 _dbus_warn ("Unexpected message after ActivateService "
2987 "(should be an error or a service announcement");
2991 if (!check_got_error (context, connection,
2992 DBUS_ERROR_SPAWN_CHILD_EXITED,
2993 DBUS_ERROR_NO_MEMORY,
2996 /* A service deleted should be coming along now after this error.
2997 * We can also get the error *after* the service deleted.
3002 case GOT_SERVICE_DELETED:
3004 /* The service started up and got a base address, but then
3005 * failed to register under EXISTENT_SERVICE_NAME
3007 CheckServiceOwnerChangedData socd;
3009 socd.expected_kind = SERVICE_DELETED;
3010 socd.expected_service_name = base_service;
3011 socd.failed = FALSE;
3012 socd.skip_connection = NULL;
3013 socd.context = context;
3015 bus_test_clients_foreach (check_service_owner_changed_foreach,
3021 /* Now we should get an error about the service exiting
3022 * if we didn't get it before.
3024 if (message_kind != GOT_ERROR)
3026 block_connection_until_message_from_bus (context, connection, "error about service exiting");
3028 /* and process everything again */
3029 bus_test_run_everything (context);
3031 if (!check_got_error (context, connection,
3032 DBUS_ERROR_SPAWN_CHILD_EXITED,
3033 DBUS_ERROR_NO_MEMORY,
3040 case GOT_SERVICE_CREATED:
3041 message = pop_message_waiting_for_memory (connection);
3042 if (message == NULL)
3044 _dbus_warn ("Failed to pop message we just put back! "
3045 "should have been a NameOwnerChanged (creation)\n");
3049 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
3050 base_service, message))
3053 dbus_message_unref (message);
3056 if (!check_no_leftovers (context))
3058 _dbus_warn ("Messages were left over after successful activation\n");
3062 if (!check_send_exit_to_service (context, connection,
3063 EXISTENT_SERVICE_NAME, base_service))
3074 dbus_message_unref (message);
3076 if (base_service_message)
3077 dbus_message_unref (base_service_message);
3082 /* returns TRUE if the correct thing happens,
3083 * but the correct thing may include OOM errors.
3086 check_segfault_service_no_auto_start (BusContext *context,
3087 DBusConnection *connection)
3089 DBusMessage *message;
3090 dbus_uint32_t serial;
3092 const char *segv_service;
3093 dbus_uint32_t flags;
3095 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
3097 DBUS_INTERFACE_DBUS,
3098 "StartServiceByName");
3100 if (message == NULL)
3103 dbus_message_set_auto_start (message, FALSE);
3105 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
3107 if (!dbus_message_append_args (message,
3108 DBUS_TYPE_STRING, &segv_service,
3109 DBUS_TYPE_UINT32, &flags,
3112 dbus_message_unref (message);
3116 if (!dbus_connection_send (connection, message, &serial))
3118 dbus_message_unref (message);
3122 dbus_message_unref (message);
3125 bus_test_run_everything (context);
3126 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
3127 bus_test_run_everything (context);
3129 if (!dbus_connection_get_is_connected (connection))
3131 _dbus_verbose ("connection was disconnected\n");
3137 message = pop_message_waiting_for_memory (connection);
3138 if (message == NULL)
3140 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3141 "StartServiceByName", serial, connection);
3145 verbose_message_received (connection, message);
3147 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3149 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3151 _dbus_warn ("Message has wrong sender %s\n",
3152 dbus_message_get_sender (message) ?
3153 dbus_message_get_sender (message) : "(none)");
3157 if (dbus_message_is_error (message,
3158 DBUS_ERROR_NO_MEMORY))
3160 ; /* good, this is a valid response */
3162 else if (dbus_message_is_error (message,
3165 const char *servicehelper;
3166 servicehelper = bus_context_get_servicehelper (context);
3167 /* make sure this only happens with the launch helper */
3168 _dbus_assert (servicehelper != NULL);
3171 else if (dbus_message_is_error (message,
3172 DBUS_ERROR_SPAWN_CHILD_EXITED))
3174 /* unhandled exceptions are normal exit codes */
3177 else if (dbus_message_is_error (message,
3178 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3180 ; /* good, this is expected also */
3185 warn_unexpected (connection, message, "not this error");
3192 _dbus_warn ("Did not expect to successfully activate segfault service\n");
3200 dbus_message_unref (message);
3206 /* returns TRUE if the correct thing happens,
3207 * but the correct thing may include OOM errors.
3210 check_segfault_service_auto_start (BusContext *context,
3211 DBusConnection *connection)
3213 DBusMessage *message;
3214 dbus_uint32_t serial;
3217 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
3218 "/org/freedesktop/TestSuite",
3219 "org.freedesktop.TestSuite",
3222 if (message == NULL)
3225 if (!dbus_connection_send (connection, message, &serial))
3227 dbus_message_unref (message);
3231 dbus_message_unref (message);
3234 bus_test_run_everything (context);
3235 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
3236 bus_test_run_everything (context);
3238 if (!dbus_connection_get_is_connected (connection))
3240 _dbus_verbose ("connection was disconnected\n");
3246 message = pop_message_waiting_for_memory (connection);
3247 if (message == NULL)
3249 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3250 "Echo message (auto activation)", serial, connection);
3254 verbose_message_received (connection, message);
3256 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3258 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3260 _dbus_warn ("Message has wrong sender %s\n",
3261 dbus_message_get_sender (message) ?
3262 dbus_message_get_sender (message) : "(none)");
3266 if (dbus_message_is_error (message,
3267 DBUS_ERROR_NO_MEMORY))
3269 ; /* good, this is a valid response */
3272 else if (dbus_message_is_error (message,
3273 DBUS_ERROR_SPAWN_CHILD_EXITED))
3275 /* unhandled exceptions are normal exit codes */
3278 else if (dbus_message_is_error (message,
3279 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
3281 ; /* good, this is expected also */
3286 warn_unexpected (connection, message, "not this error");
3293 _dbus_warn ("Did not expect to successfully activate segfault service\n");
3301 dbus_message_unref (message);
3306 #define TEST_ECHO_MESSAGE "Test echo message"
3307 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
3309 /* returns TRUE if the correct thing happens,
3310 * but the correct thing may include OOM errors.
3313 check_existent_hello_from_self (BusContext *context,
3314 DBusConnection *connection)
3316 DBusMessage *message;
3317 dbus_uint32_t serial;
3320 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3321 "/org/freedesktop/TestSuite",
3322 "org.freedesktop.TestSuite",
3323 "RunHelloFromSelf");
3325 if (message == NULL)
3328 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
3329 if (!dbus_message_append_args (message,
3330 DBUS_TYPE_STRING, &text,
3333 dbus_message_unref (message);
3337 if (!dbus_connection_send (connection, message, &serial))
3339 dbus_message_unref (message);
3343 dbus_message_unref (message);
3346 bus_test_run_everything (context);
3348 /* Note: if this test is run in OOM mode, it will block when the bus
3349 * doesn't send a reply due to OOM.
3351 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
3353 message = pop_message_waiting_for_memory (connection);
3354 if (message == NULL)
3356 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
3360 if (dbus_message_get_reply_serial (message) != serial)
3362 _dbus_warn ("Wrong reply serial\n");
3363 dbus_message_unref (message);
3367 dbus_message_unref (message);
3373 /* returns TRUE if the correct thing happens,
3374 * but the correct thing may include OOM errors.
3377 check_existent_ping (BusContext *context,
3378 DBusConnection *connection)
3380 DBusMessage *message;
3381 dbus_uint32_t serial;
3382 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3383 "/org/freedesktop/TestSuite",
3384 "org.freedesktop.DBus.Peer",
3387 if (message == NULL)
3390 if (!dbus_connection_send (connection, message, &serial))
3392 dbus_message_unref (message);
3396 dbus_message_unref (message);
3399 bus_test_run_everything (context);
3401 /* Note: if this test is run in OOM mode, it will block when the bus
3402 * doesn't send a reply due to OOM.
3404 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
3406 message = pop_message_waiting_for_memory (connection);
3407 if (message == NULL)
3409 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
3413 if (dbus_message_get_reply_serial (message) != serial)
3415 _dbus_warn ("Wrong reply serial\n");
3416 dbus_message_unref (message);
3420 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3422 _dbus_warn ("Unexpected message return during Ping\n");
3423 dbus_message_unref (message);
3427 dbus_message_unref (message);
3433 /* returns TRUE if the correct thing happens,
3434 * but the correct thing may include OOM errors.
3437 check_existent_get_machine_id (BusContext *context,
3438 DBusConnection *connection)
3440 DBusMessage *message;
3441 dbus_uint32_t serial;
3442 const char *machine_id;
3444 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3445 "/org/freedesktop/TestSuite",
3446 "org.freedesktop.DBus.Peer",
3449 if (message == NULL)
3452 if (!dbus_connection_send (connection, message, &serial))
3454 dbus_message_unref (message);
3458 dbus_message_unref (message);
3461 bus_test_run_everything (context);
3463 /* Note: if this test is run in OOM mode, it will block when the bus
3464 * doesn't send a reply due to OOM.
3466 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3468 message = pop_message_waiting_for_memory (connection);
3469 if (message == NULL)
3471 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3475 if (dbus_message_get_reply_serial (message) != serial)
3477 _dbus_warn ("Wrong reply serial\n");
3478 dbus_message_unref (message);
3482 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3484 _dbus_warn ("Unexpected message return during GetMachineId\n");
3485 dbus_message_unref (message);
3490 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3492 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3493 dbus_message_unref (message);
3497 if (machine_id == NULL || strlen (machine_id) != 32)
3499 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3500 dbus_message_unref (message);
3504 /* We can't check that the machine id is correct because during make check it is
3505 * just made up for each process separately
3508 dbus_message_unref (message);
3514 /* returns TRUE if the correct thing happens,
3515 * but the correct thing may include OOM errors.
3518 check_existent_service_auto_start (BusContext *context,
3519 DBusConnection *connection)
3521 DBusMessage *message;
3522 DBusMessage *base_service_message;
3523 dbus_uint32_t serial;
3525 const char *base_service;
3528 base_service_message = NULL;
3530 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3531 "/org/freedesktop/TestSuite",
3532 "org.freedesktop.TestSuite",
3535 if (message == NULL)
3538 text = TEST_ECHO_MESSAGE;
3539 if (!dbus_message_append_args (message,
3540 DBUS_TYPE_STRING, &text,
3543 dbus_message_unref (message);
3547 if (!dbus_connection_send (connection, message, &serial))
3549 dbus_message_unref (message);
3553 dbus_message_unref (message);
3556 bus_test_run_everything (context);
3558 /* now wait for the message bus to hear back from the activated
3561 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3562 bus_test_run_everything (context);
3564 if (!dbus_connection_get_is_connected (connection))
3566 _dbus_verbose ("connection was disconnected\n");
3572 message = pop_message_waiting_for_memory (connection);
3573 if (message == NULL)
3575 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3576 serial, connection);
3580 verbose_message_received (connection, message);
3581 _dbus_verbose (" (after sending %s)\n", "auto start");
3583 /* we should get zero or two ServiceOwnerChanged signals */
3584 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3586 GotServiceInfo message_kind;
3588 if (!check_base_service_activated (context, connection,
3589 message, &base_service))
3592 base_service_message = message;
3595 /* We may need to block here for the test service to exit or finish up */
3596 block_connection_until_message_from_bus (context, connection, "service to exit");
3598 /* Should get a service creation notification for the activated
3599 * service name, or a service deletion on the base service name
3601 message = dbus_connection_borrow_message (connection);
3602 if (message == NULL)
3604 _dbus_warn ("No message after auto activation "
3605 "(should be a service announcement)\n");
3606 dbus_connection_return_message (connection, message);
3611 message_kind = check_got_service_info (message);
3613 dbus_connection_return_message (connection, message);
3616 switch (message_kind)
3618 case GOT_SERVICE_CREATED:
3619 message = pop_message_waiting_for_memory (connection);
3620 if (message == NULL)
3622 _dbus_warn ("Failed to pop message we just put back! "
3623 "should have been a NameOwnerChanged (creation)\n");
3627 /* Check that ServiceOwnerChanged (creation) was correctly received */
3628 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3629 base_service, message))
3632 dbus_message_unref (message);
3637 case GOT_SERVICE_DELETED:
3639 /* The service started up and got a base address, but then
3640 * failed to register under EXISTENT_SERVICE_NAME
3642 CheckServiceOwnerChangedData socd;
3644 socd.expected_kind = SERVICE_DELETED;
3645 socd.expected_service_name = base_service;
3646 socd.failed = FALSE;
3647 socd.skip_connection = NULL;
3648 socd.context = context;
3650 bus_test_clients_foreach (check_service_owner_changed_foreach,
3660 case GOT_SOMETHING_ELSE:
3661 _dbus_warn ("Unexpected message after auto activation\n");
3666 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3667 * come the method reply (or error) from the initial method call
3670 /* Note: if this test is run in OOM mode, it will block when the bus
3671 * doesn't send a reply due to OOM.
3673 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3675 message = pop_message_waiting_for_memory (connection);
3676 if (message == NULL)
3678 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3682 if (dbus_message_get_reply_serial (message) != serial)
3684 _dbus_warn ("Wrong reply serial\n");
3688 dbus_message_unref (message);
3691 if (!check_existent_ping (context, connection))
3694 if (!check_existent_get_machine_id (context, connection))
3697 if (!check_existent_hello_from_self (context, connection))
3700 if (!check_send_exit_to_service (context, connection,
3701 EXISTENT_SERVICE_NAME,
3709 dbus_message_unref (message);
3711 if (base_service_message)
3712 dbus_message_unref (base_service_message);
3717 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3719 /* returns TRUE if the correct thing happens,
3720 * but the correct thing may include OOM errors.
3723 check_launch_service_file_missing (BusContext *context,
3724 DBusConnection *connection)
3726 DBusMessage *message;
3727 dbus_uint32_t serial;
3730 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3731 "/org/freedesktop/TestSuite",
3732 "org.freedesktop.TestSuite",
3735 if (message == NULL)
3738 if (!dbus_connection_send (connection, message, &serial))
3740 dbus_message_unref (message);
3744 dbus_message_unref (message);
3747 bus_test_run_everything (context);
3748 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3749 bus_test_run_everything (context);
3751 if (!dbus_connection_get_is_connected (connection))
3753 _dbus_verbose ("connection was disconnected\n");
3759 message = pop_message_waiting_for_memory (connection);
3760 if (message == NULL)
3762 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3763 "Echo message (auto activation)", serial, connection);
3767 verbose_message_received (connection, message);
3769 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3771 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3773 _dbus_warn ("Message has wrong sender %s\n",
3774 dbus_message_get_sender (message) ?
3775 dbus_message_get_sender (message) : "(none)");
3779 if (dbus_message_is_error (message,
3780 DBUS_ERROR_NO_MEMORY))
3782 ; /* good, this is a valid response */
3784 else if (dbus_message_is_error (message,
3785 DBUS_ERROR_SERVICE_UNKNOWN))
3787 _dbus_verbose("got service unknown\n");
3788 ; /* good, this is expected (only valid when using launch helper) */
3792 warn_unexpected (connection, message, "not this error");
3799 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3807 dbus_message_unref (message);
3812 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3814 /* returns TRUE if the correct thing happens,
3815 * but the correct thing may include OOM errors.
3818 check_launch_service_user_missing (BusContext *context,
3819 DBusConnection *connection)
3821 DBusMessage *message;
3822 dbus_uint32_t serial;
3825 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3826 "/org/freedesktop/TestSuite",
3827 "org.freedesktop.TestSuite",
3830 if (message == NULL)
3833 if (!dbus_connection_send (connection, message, &serial))
3835 dbus_message_unref (message);
3839 dbus_message_unref (message);
3842 bus_test_run_everything (context);
3843 block_connection_until_message_from_bus (context, connection,
3844 "reply to service which should fail to auto-start (missing User)");
3845 bus_test_run_everything (context);
3847 if (!dbus_connection_get_is_connected (connection))
3849 _dbus_warn ("connection was disconnected\n");
3855 message = pop_message_waiting_for_memory (connection);
3856 if (message == NULL)
3858 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3859 "Echo message (auto activation)", serial, connection);
3863 verbose_message_received (connection, message);
3865 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3867 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3869 _dbus_warn ("Message has wrong sender %s\n",
3870 dbus_message_get_sender (message) ?
3871 dbus_message_get_sender (message) : "(none)");
3875 if (dbus_message_is_error (message,
3876 DBUS_ERROR_NO_MEMORY))
3878 ; /* good, this is a valid response */
3880 else if (dbus_message_is_error (message,
3881 DBUS_ERROR_SPAWN_FILE_INVALID))
3883 _dbus_verbose("got service file invalid\n");
3884 ; /* good, this is expected (only valid when using launch helper) */
3888 warn_unexpected (connection, message, "not this error");
3895 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3903 dbus_message_unref (message);
3908 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3910 /* returns TRUE if the correct thing happens,
3911 * but the correct thing may include OOM errors.
3914 check_launch_service_exec_missing (BusContext *context,
3915 DBusConnection *connection)
3917 DBusMessage *message;
3918 dbus_uint32_t serial;
3921 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3922 "/org/freedesktop/TestSuite",
3923 "org.freedesktop.TestSuite",
3926 if (message == NULL)
3929 if (!dbus_connection_send (connection, message, &serial))
3931 dbus_message_unref (message);
3935 dbus_message_unref (message);
3938 bus_test_run_everything (context);
3939 block_connection_until_message_from_bus (context, connection,
3940 "reply to service which should fail to auto-start (missing Exec)");
3941 bus_test_run_everything (context);
3943 if (!dbus_connection_get_is_connected (connection))
3945 _dbus_warn ("connection was disconnected\n");
3951 message = pop_message_waiting_for_memory (connection);
3952 if (message == NULL)
3954 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3955 "Echo message (auto activation)", serial, connection);
3959 verbose_message_received (connection, message);
3961 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3963 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3965 _dbus_warn ("Message has wrong sender %s\n",
3966 dbus_message_get_sender (message) ?
3967 dbus_message_get_sender (message) : "(none)");
3971 if (dbus_message_is_error (message,
3972 DBUS_ERROR_NO_MEMORY))
3974 ; /* good, this is a valid response */
3976 else if (dbus_message_is_error (message,
3977 DBUS_ERROR_SERVICE_UNKNOWN))
3979 _dbus_verbose("could not activate as invalid service file was not added\n");
3980 ; /* good, this is expected as we shouldn't have been added to
3981 * the activation list with a missing Exec key */
3983 else if (dbus_message_is_error (message,
3984 DBUS_ERROR_SPAWN_FILE_INVALID))
3986 _dbus_verbose("got service file invalid\n");
3987 ; /* good, this is allowed, and is the message passed back from the
3992 warn_unexpected (connection, message, "not this error");
3999 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
4007 dbus_message_unref (message);
4012 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
4014 /* returns TRUE if the correct thing happens,
4015 * but the correct thing may include OOM errors.
4018 check_launch_service_service_missing (BusContext *context,
4019 DBusConnection *connection)
4021 DBusMessage *message;
4022 dbus_uint32_t serial;
4025 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
4026 "/org/freedesktop/TestSuite",
4027 "org.freedesktop.TestSuite",
4030 if (message == NULL)
4033 if (!dbus_connection_send (connection, message, &serial))
4035 dbus_message_unref (message);
4039 dbus_message_unref (message);
4042 bus_test_run_everything (context);
4043 block_connection_until_message_from_bus (context, connection,
4044 "reply to service which should fail to auto-start (missing Service)");
4045 bus_test_run_everything (context);
4047 if (!dbus_connection_get_is_connected (connection))
4049 _dbus_warn ("connection was disconnected\n");
4055 message = pop_message_waiting_for_memory (connection);
4056 if (message == NULL)
4058 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4059 "Echo message (auto activation)", serial, connection);
4063 verbose_message_received (connection, message);
4065 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4067 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4069 _dbus_warn ("Message has wrong sender %s\n",
4070 dbus_message_get_sender (message) ?
4071 dbus_message_get_sender (message) : "(none)");
4075 if (dbus_message_is_error (message,
4076 DBUS_ERROR_NO_MEMORY))
4078 ; /* good, this is a valid response */
4080 else if (dbus_message_is_error (message,
4081 DBUS_ERROR_SERVICE_UNKNOWN))
4083 _dbus_verbose("could not activate as invalid service file was not added\n");
4084 ; /* good, this is expected as we shouldn't have been added to
4085 * the activation list with a missing Exec key */
4087 else if (dbus_message_is_error (message,
4088 DBUS_ERROR_SPAWN_FILE_INVALID))
4090 _dbus_verbose("got service file invalid\n");
4091 ; /* good, this is allowed, and is the message passed back from the
4096 warn_unexpected (connection, message, "not this error");
4103 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
4111 dbus_message_unref (message);
4116 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
4118 /* returns TRUE if the correct thing happens,
4119 * but the correct thing may include OOM errors.
4122 check_shell_fail_service_auto_start (BusContext *context,
4123 DBusConnection *connection)
4125 DBusMessage *message;
4126 dbus_uint32_t serial;
4129 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
4130 "/org/freedesktop/TestSuite",
4131 "org.freedesktop.TestSuite",
4134 if (message == NULL)
4137 if (!dbus_connection_send (connection, message, &serial))
4139 dbus_message_unref (message);
4143 dbus_message_unref (message);
4146 bus_test_run_everything (context);
4147 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
4148 bus_test_run_everything (context);
4150 if (!dbus_connection_get_is_connected (connection))
4152 _dbus_verbose ("connection was disconnected\n");
4158 message = pop_message_waiting_for_memory (connection);
4159 if (message == NULL)
4161 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4162 "Echo message (auto activation)", serial, connection);
4166 verbose_message_received (connection, message);
4168 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4170 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4172 _dbus_warn ("Message has wrong sender %s\n",
4173 dbus_message_get_sender (message) ?
4174 dbus_message_get_sender (message) : "(none)");
4178 if (dbus_message_is_error (message,
4179 DBUS_ERROR_NO_MEMORY))
4181 ; /* good, this is a valid response */
4183 else if (dbus_message_is_error (message,
4184 DBUS_ERROR_INVALID_ARGS))
4186 _dbus_verbose("got invalid args\n");
4187 ; /* good, this is expected also */
4191 warn_unexpected (connection, message, "not this error");
4198 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
4206 dbus_message_unref (message);
4211 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
4213 /* returns TRUE if the correct thing happens,
4214 * but the correct thing may include OOM errors.
4217 check_shell_service_success_auto_start (BusContext *context,
4218 DBusConnection *connection)
4220 DBusMessage *message;
4221 DBusMessage *base_service_message;
4222 dbus_uint32_t serial;
4224 const char *base_service;
4225 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4227 base_service_message = NULL;
4229 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
4230 "/org/freedesktop/TestSuite",
4231 "org.freedesktop.TestSuite",
4234 if (message == NULL)
4237 if (!dbus_connection_send (connection, message, &serial))
4239 dbus_message_unref (message);
4243 dbus_message_unref (message);
4246 bus_test_run_everything (context);
4248 /* now wait for the message bus to hear back from the activated
4251 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
4252 bus_test_run_everything (context);
4254 if (!dbus_connection_get_is_connected (connection))
4256 _dbus_verbose ("connection was disconnected\n");
4262 message = pop_message_waiting_for_memory (connection);
4263 if (message == NULL)
4265 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
4266 serial, connection);
4270 verbose_message_received (connection, message);
4271 _dbus_verbose (" (after sending %s)\n", "auto start");
4273 /* we should get zero or two ServiceOwnerChanged signals */
4274 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
4276 GotServiceInfo message_kind;
4278 if (!check_base_service_activated (context, connection,
4279 message, &base_service))
4282 base_service_message = message;
4285 /* We may need to block here for the test service to exit or finish up */
4286 block_connection_until_message_from_bus (context, connection, "service to exit");
4288 /* Should get a service creation notification for the activated
4289 * service name, or a service deletion on the base service name
4291 message = dbus_connection_borrow_message (connection);
4292 if (message == NULL)
4294 _dbus_warn ("No message after auto activation "
4295 "(should be a service announcement)\n");
4296 dbus_connection_return_message (connection, message);
4301 message_kind = check_got_service_info (message);
4303 dbus_connection_return_message (connection, message);
4306 switch (message_kind)
4308 case GOT_SERVICE_CREATED:
4309 message = pop_message_waiting_for_memory (connection);
4310 if (message == NULL)
4312 _dbus_warn ("Failed to pop message we just put back! "
4313 "should have been a NameOwnerChanged (creation)\n");
4317 /* Check that ServiceOwnerChanged (creation) was correctly received */
4318 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
4319 base_service, message))
4322 dbus_message_unref (message);
4327 case GOT_SERVICE_DELETED:
4329 /* The service started up and got a base address, but then
4330 * failed to register under SHELL_SUCCESS_SERVICE_NAME
4332 CheckServiceOwnerChangedData socd;
4334 socd.expected_kind = SERVICE_DELETED;
4335 socd.expected_service_name = base_service;
4336 socd.failed = FALSE;
4337 socd.skip_connection = NULL;
4338 socd.context = context;
4340 bus_test_clients_foreach (check_service_owner_changed_foreach,
4350 case GOT_SOMETHING_ELSE:
4351 _dbus_warn ("Unexpected message after auto activation\n");
4356 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
4357 * come the method reply (or error) from the initial method call
4360 /* Note: if this test is run in OOM mode, it will block when the bus
4361 * doesn't send a reply due to OOM.
4363 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
4365 message = pop_message_waiting_for_memory (connection);
4366 if (message == NULL)
4368 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
4372 if (dbus_message_get_reply_serial (message) != serial)
4374 _dbus_warn ("Wrong reply serial\n");
4378 if (!dbus_message_get_args (message, NULL,
4379 DBUS_TYPE_STRING, &argv[0],
4380 DBUS_TYPE_STRING, &argv[1],
4381 DBUS_TYPE_STRING, &argv[2],
4382 DBUS_TYPE_STRING, &argv[3],
4383 DBUS_TYPE_STRING, &argv[4],
4384 DBUS_TYPE_STRING, &argv[5],
4385 DBUS_TYPE_STRING, &argv[6],
4388 _dbus_warn ("Error getting arguments from return\n");
4392 /* don't worry about arg[0] as it may be different
4393 depending on the path to the tests
4395 if (strcmp("-test", argv[1]) != 0)
4397 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
4402 if (strcmp("that", argv[2]) != 0)
4404 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
4409 if (strcmp("we get", argv[3]) != 0)
4411 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
4416 if (strcmp("back", argv[4]) != 0)
4418 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
4423 if (strcmp("--what", argv[5]) != 0)
4425 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
4430 if (strcmp("we put in", argv[6]) != 0)
4432 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
4433 "we put in", argv[6]);
4437 dbus_message_unref (message);
4440 if (!check_send_exit_to_service (context, connection,
4441 SHELL_SUCCESS_SERVICE_NAME,
4449 dbus_message_unref (message);
4451 if (base_service_message)
4452 dbus_message_unref (base_service_message);
4460 BusContext *context;
4464 check_oom_check1_func (void *data)
4466 Check1Data *d = data;
4468 if (! (* d->func) (d->context))
4471 if (!check_no_leftovers (d->context))
4473 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4481 check1_try_iterations (BusContext *context,
4482 const char *description,
4488 d.context = context;
4490 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4492 _dbus_assert_not_reached ("test failed");
4496 check_get_services (BusContext *context,
4497 DBusConnection *connection,
4502 DBusMessage *message;
4503 dbus_uint32_t serial;
4510 dbus_error_init (&error);
4513 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4515 DBUS_INTERFACE_DBUS,
4518 if (message == NULL)
4521 if (!dbus_connection_send (connection, message, &serial))
4523 dbus_message_unref (message);
4527 /* send our message */
4528 bus_test_run_clients_loop (SEND_PENDING (connection));
4530 dbus_message_unref (message);
4533 dbus_connection_ref (connection); /* because we may get disconnected */
4534 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4536 if (!dbus_connection_get_is_connected (connection))
4538 _dbus_verbose ("connection was disconnected\n");
4540 dbus_connection_unref (connection);
4545 dbus_connection_unref (connection);
4547 message = pop_message_waiting_for_memory (connection);
4548 if (message == NULL)
4550 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4551 method, serial, connection);
4555 verbose_message_received (connection, message);
4557 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4559 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4561 ; /* good, this is a valid response */
4565 warn_unexpected (connection, message, "not this error");
4572 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4574 ; /* good, expected */
4578 warn_unexpected (connection, message,
4579 "method_return for ListActivatableNames/ListNames");
4586 if (!dbus_message_get_args (message, &error,
4592 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4594 _dbus_verbose ("no memory to list services by %s\n", method);
4595 dbus_error_free (&error);
4596 _dbus_wait_for_memory ();
4597 goto retry_get_property;
4601 _dbus_assert (dbus_error_is_set (&error));
4602 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4611 if (!check_no_leftovers (context))
4617 dbus_error_free (&error);
4620 dbus_message_unref (message);
4625 /* returns TRUE if the correct thing happens,
4626 * but the correct thing may include OOM errors.
4629 check_list_services (BusContext *context,
4630 DBusConnection *connection)
4632 DBusMessage *message;
4633 DBusMessage *base_service_message;
4634 const char *base_service;
4635 dbus_uint32_t serial;
4637 const char *existent = EXISTENT_SERVICE_NAME;
4638 dbus_uint32_t flags;
4642 _dbus_verbose ("check_list_services for %p\n", connection);
4644 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4649 if (!_dbus_string_array_contains ((const char **)services, existent))
4651 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4652 dbus_free_string_array (services);
4656 dbus_free_string_array (services);
4658 base_service_message = NULL;
4660 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4662 DBUS_INTERFACE_DBUS,
4663 "StartServiceByName");
4665 if (message == NULL)
4668 dbus_message_set_auto_start (message, FALSE);
4671 if (!dbus_message_append_args (message,
4672 DBUS_TYPE_STRING, &existent,
4673 DBUS_TYPE_UINT32, &flags,
4676 dbus_message_unref (message);
4680 if (!dbus_connection_send (connection, message, &serial))
4682 dbus_message_unref (message);
4686 dbus_message_unref (message);
4689 bus_test_run_everything (context);
4691 /* now wait for the message bus to hear back from the activated
4694 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4696 bus_test_run_everything (context);
4698 if (!dbus_connection_get_is_connected (connection))
4700 _dbus_verbose ("connection was disconnected\n");
4706 message = pop_message_waiting_for_memory (connection);
4707 if (message == NULL)
4709 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4710 "StartServiceByName", serial, connection);
4714 verbose_message_received (connection, message);
4715 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4717 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4719 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4721 _dbus_warn ("Message has wrong sender %s\n",
4722 dbus_message_get_sender (message) ?
4723 dbus_message_get_sender (message) : "(none)");
4727 if (dbus_message_is_error (message,
4728 DBUS_ERROR_NO_MEMORY))
4730 ; /* good, this is a valid response */
4732 else if (dbus_message_is_error (message,
4733 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4734 dbus_message_is_error (message,
4735 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4736 dbus_message_is_error (message,
4737 DBUS_ERROR_SPAWN_EXEC_FAILED))
4739 ; /* good, this is expected also */
4743 _dbus_warn ("Did not expect error %s\n",
4744 dbus_message_get_error_name (message));
4750 GotServiceInfo message_kind;
4752 if (!check_base_service_activated (context, connection,
4753 message, &base_service))
4756 base_service_message = message;
4759 /* We may need to block here for the test service to exit or finish up */
4760 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4762 message = dbus_connection_borrow_message (connection);
4763 if (message == NULL)
4765 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4769 message_kind = check_got_service_info (message);
4771 dbus_connection_return_message (connection, message);
4774 switch (message_kind)
4776 case GOT_SOMETHING_ELSE:
4778 case GOT_SERVICE_DELETED:
4779 _dbus_warn ("Unexpected message after ActivateService "
4780 "(should be an error or a service announcement)\n");
4783 case GOT_SERVICE_CREATED:
4784 message = pop_message_waiting_for_memory (connection);
4785 if (message == NULL)
4787 _dbus_warn ("Failed to pop message we just put back! "
4788 "should have been a NameOwnerChanged (creation)\n");
4792 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4793 base_service, message))
4796 dbus_message_unref (message);
4799 if (!check_no_leftovers (context))
4801 _dbus_warn ("Messages were left over after successful activation\n");
4809 if (!check_get_services (context, connection, "ListNames", &services, &len))
4814 if (!_dbus_string_array_contains ((const char **)services, existent))
4816 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4820 dbus_free_string_array (services);
4822 if (!check_send_exit_to_service (context, connection,
4823 EXISTENT_SERVICE_NAME, base_service))
4830 dbus_message_unref (message);
4832 if (base_service_message)
4833 dbus_message_unref (base_service_message);
4841 BusContext *context;
4842 DBusConnection *connection;
4846 check_oom_check2_func (void *data)
4848 Check2Data *d = data;
4850 if (! (* d->func) (d->context, d->connection))
4853 if (!check_no_leftovers (d->context))
4855 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4863 check2_try_iterations (BusContext *context,
4864 DBusConnection *connection,
4865 const char *description,
4871 d.context = context;
4872 d.connection = connection;
4874 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4877 _dbus_warn ("%s failed during oom\n", description);
4878 _dbus_assert_not_reached ("test failed");
4883 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4884 const char *filename)
4889 if (!_dbus_string_init (&full))
4892 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4894 _dbus_string_free (&full);
4898 _dbus_string_init_const (&file, filename);
4900 if (!_dbus_concat_dir_and_file (&full, &file))
4902 _dbus_string_free (&full);
4906 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4907 _dbus_string_get_const_data (&full));
4909 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4911 _dbus_string_free (&full);
4917 bus_dispatch_test_conf (const DBusString *test_data_dir,
4918 const char *filename,
4919 dbus_bool_t use_launcher)
4921 BusContext *context;
4922 DBusConnection *foo;
4923 DBusConnection *bar;
4924 DBusConnection *baz;
4927 /* save the config name for the activation helper */
4928 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4929 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4931 dbus_error_init (&error);
4933 context = bus_context_new_test (test_data_dir, filename);
4934 if (context == NULL)
4937 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4939 _dbus_assert_not_reached ("could not alloc connection");
4941 if (!bus_setup_debug_client (foo))
4942 _dbus_assert_not_reached ("could not set up connection");
4944 spin_connection_until_authenticated (context, foo);
4946 if (!check_hello_message (context, foo))
4947 _dbus_assert_not_reached ("hello message failed");
4949 if (!check_double_hello_message (context, foo))
4950 _dbus_assert_not_reached ("double hello message failed");
4952 if (!check_add_match (context, foo, ""))
4953 _dbus_assert_not_reached ("AddMatch message failed");
4955 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4957 _dbus_assert_not_reached ("could not alloc connection");
4959 if (!bus_setup_debug_client (bar))
4960 _dbus_assert_not_reached ("could not set up connection");
4962 spin_connection_until_authenticated (context, bar);
4964 if (!check_hello_message (context, bar))
4965 _dbus_assert_not_reached ("hello message failed");
4967 if (!check_add_match (context, bar, ""))
4968 _dbus_assert_not_reached ("AddMatch message failed");
4970 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4972 _dbus_assert_not_reached ("could not alloc connection");
4974 if (!bus_setup_debug_client (baz))
4975 _dbus_assert_not_reached ("could not set up connection");
4977 spin_connection_until_authenticated (context, baz);
4979 if (!check_hello_message (context, baz))
4980 _dbus_assert_not_reached ("hello message failed");
4982 if (!check_add_match (context, baz, ""))
4983 _dbus_assert_not_reached ("AddMatch message failed");
4985 if (!check_add_match (context, baz, "interface='com.example'"))
4986 _dbus_assert_not_reached ("AddMatch message failed");
4988 #ifdef DBUS_ENABLE_STATS
4989 if (!check_get_all_match_rules (context, baz))
4990 _dbus_assert_not_reached ("GetAllMatchRules message failed");
4993 if (!check_get_connection_unix_user (context, baz))
4994 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4996 #ifdef DBUS_WIN_FIXME
4997 _dbus_verbose("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4999 if (!check_get_connection_unix_process_id (context, baz))
5000 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
5003 if (!check_list_services (context, baz))
5004 _dbus_assert_not_reached ("ListActivatableNames message failed");
5006 if (!check_no_leftovers (context))
5008 _dbus_warn ("Messages were left over after setting up initial connections\n");
5009 _dbus_assert_not_reached ("initial connection setup failed");
5012 check1_try_iterations (context, "create_and_hello",
5013 check_hello_connection);
5015 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
5016 check_nonexistent_service_no_auto_start);
5018 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
5019 check_segfault_service_no_auto_start);
5021 check2_try_iterations (context, foo, "existent_service_no_auto_start",
5022 check_existent_service_no_auto_start);
5024 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
5025 check_nonexistent_service_auto_start);
5027 /* only do the segfault test if we are not using the launcher */
5028 check2_try_iterations (context, foo, "segfault_service_auto_start",
5029 check_segfault_service_auto_start);
5031 /* only do the shell fail test if we are not using the launcher */
5032 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
5033 check_shell_fail_service_auto_start);
5035 /* specific to launcher */
5037 if (!check_launch_service_file_missing (context, foo))
5038 _dbus_assert_not_reached ("did not get service file not found error");
5041 /* Note: need to resolve some issues with the testing code in order to run
5042 * this in oom (handle that we sometimes don't get replies back from the bus
5043 * when oom happens, without blocking the test).
5045 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
5046 check_existent_service_auto_start);
5049 if (!check_existent_service_auto_start (context, foo))
5050 _dbus_assert_not_reached ("existent service auto start failed");
5052 if (!check_shell_service_success_auto_start (context, foo))
5053 _dbus_assert_not_reached ("shell success service auto start failed");
5055 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
5057 kill_client_connection_unchecked (foo);
5058 kill_client_connection_unchecked (bar);
5059 kill_client_connection_unchecked (baz);
5061 bus_context_unref (context);
5067 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
5068 const char *filename)
5070 BusContext *context;
5071 DBusConnection *foo;
5074 /* save the config name for the activation helper */
5075 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5076 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5078 dbus_error_init (&error);
5080 context = bus_context_new_test (test_data_dir, filename);
5081 if (context == NULL)
5084 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5086 _dbus_assert_not_reached ("could not alloc connection");
5088 if (!bus_setup_debug_client (foo))
5089 _dbus_assert_not_reached ("could not set up connection");
5091 spin_connection_until_authenticated (context, foo);
5093 if (!check_hello_message (context, foo))
5094 _dbus_assert_not_reached ("hello message failed");
5096 if (!check_double_hello_message (context, foo))
5097 _dbus_assert_not_reached ("double hello message failed");
5099 if (!check_add_match (context, foo, ""))
5100 _dbus_assert_not_reached ("AddMatch message failed");
5102 /* this only tests the activation.c user check */
5103 if (!check_launch_service_user_missing (context, foo))
5104 _dbus_assert_not_reached ("user missing did not trigger error");
5106 /* this only tests the desktop.c exec check */
5107 if (!check_launch_service_exec_missing (context, foo))
5108 _dbus_assert_not_reached ("exec missing did not trigger error");
5110 /* this only tests the desktop.c service check */
5111 if (!check_launch_service_service_missing (context, foo))
5112 _dbus_assert_not_reached ("service missing did not trigger error");
5114 _dbus_verbose ("Disconnecting foo\n");
5116 kill_client_connection_unchecked (foo);
5118 bus_context_unref (context);
5124 DBusTimeout *timeout;
5125 DBusConnection *connection;
5126 dbus_bool_t timedout;
5130 static BusTestCheckData *cdata;
5133 bus_dispatch_test_check_timeout (void *data)
5135 _dbus_verbose ("timeout triggered - pretend that privilege check result is available\n");
5137 /* should only happen once during the test */
5138 _dbus_assert (!cdata->timedout);
5139 cdata->timedout = TRUE;
5140 _dbus_connection_enable_dispatch (cdata->connection);
5142 /* don't call this again */
5143 _dbus_loop_remove_timeout (bus_connection_get_loop (cdata->connection),
5145 dbus_connection_unref (cdata->connection);
5146 cdata->connection = NULL;
5151 bus_dispatch_test_check_override (DBusConnection *connection,
5152 const char *privilege)
5154 _dbus_verbose ("overriding privilege check %s #%d\n", privilege, cdata->check_counter);
5155 cdata->check_counter++;
5156 if (!cdata->timedout)
5160 /* Should be the first privilege check for the "Echo" method. */
5161 _dbus_assert (cdata->check_counter == 1);
5162 cdata->timeout = _dbus_timeout_new (1, bus_dispatch_test_check_timeout,
5164 _dbus_assert (cdata->timeout);
5165 added = _dbus_loop_add_timeout (bus_connection_get_loop (connection),
5167 _dbus_assert (added);
5168 cdata->connection = connection;
5169 dbus_connection_ref (connection);
5170 _dbus_connection_disable_dispatch (connection);
5171 return BUS_RESULT_LATER;
5175 /* Should only be checked one more time, and this time succeeds. */
5176 _dbus_assert (cdata->check_counter == 2);
5177 return BUS_RESULT_TRUE;
5182 bus_dispatch_test_check (const DBusString *test_data_dir)
5184 const char *filename = "valid-config-files/debug-check-some.conf";
5185 BusContext *context;
5186 DBusConnection *foo;
5188 dbus_bool_t result = TRUE;
5189 BusTestCheckData data;
5191 /* save the config name for the activation helper */
5192 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
5193 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
5195 dbus_error_init (&error);
5197 context = bus_context_new_test (test_data_dir, filename);
5198 if (context == NULL)
5201 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5203 _dbus_assert_not_reached ("could not alloc connection");
5205 if (!bus_setup_debug_client (foo))
5206 _dbus_assert_not_reached ("could not set up connection");
5208 spin_connection_until_authenticated (context, foo);
5210 if (!check_hello_message (context, foo))
5211 _dbus_assert_not_reached ("hello message failed");
5213 if (!check_double_hello_message (context, foo))
5214 _dbus_assert_not_reached ("double hello message failed");
5216 if (!check_add_match_all (context, foo))
5217 _dbus_assert_not_reached ("AddMatch message failed");
5220 * Cause bus_check_send_privilege() to return BUS_RESULT_LATER in the
5221 * first call, then BUS_RESULT_TRUE.
5224 memset (cdata, 0, sizeof(*cdata));
5225 bus_check_test_override = bus_dispatch_test_check_override;
5227 result = check_existent_service_auto_start (context, foo);
5229 _dbus_assert (cdata->check_counter == 2);
5230 _dbus_assert (cdata->timedout);
5231 _dbus_assert (cdata->timeout);
5232 _dbus_assert (!cdata->connection);
5233 _dbus_timeout_unref (cdata->timeout);
5235 kill_client_connection_unchecked (foo);
5237 bus_context_unref (context);
5243 bus_dispatch_test (const DBusString *test_data_dir)
5245 _dbus_verbose ("<check> tests\n");
5246 if (!bus_dispatch_test_check (test_data_dir))
5249 /* run normal activation tests */
5250 _dbus_verbose ("Normal activation tests\n");
5251 if (!bus_dispatch_test_conf (test_data_dir,
5252 "valid-config-files/debug-allow-all.conf", FALSE))
5256 /* run launch-helper activation tests */
5257 _dbus_verbose ("Launch helper activation tests\n");
5258 if (!bus_dispatch_test_conf (test_data_dir,
5259 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
5262 /* run select launch-helper activation tests on broken service files */
5263 if (!bus_dispatch_test_conf_fail (test_data_dir,
5264 "valid-config-files-system/debug-allow-all-fail.conf"))
5272 bus_dispatch_sha1_test (const DBusString *test_data_dir)
5274 BusContext *context;
5275 DBusConnection *foo;
5278 dbus_error_init (&error);
5280 /* Test SHA1 authentication */
5281 _dbus_verbose ("Testing SHA1 context\n");
5283 context = bus_context_new_test (test_data_dir,
5284 "valid-config-files/debug-allow-all-sha1.conf");
5285 if (context == NULL)
5288 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5290 _dbus_assert_not_reached ("could not alloc connection");
5292 if (!bus_setup_debug_client (foo))
5293 _dbus_assert_not_reached ("could not set up connection");
5295 spin_connection_until_authenticated (context, foo);
5297 if (!check_hello_message (context, foo))
5298 _dbus_assert_not_reached ("hello message failed");
5300 if (!check_add_match (context, foo, ""))
5301 _dbus_assert_not_reached ("addmatch message failed");
5303 if (!check_no_leftovers (context))
5305 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
5306 _dbus_assert_not_reached ("initial connection setup failed");
5309 check1_try_iterations (context, "create_and_hello_sha1",
5310 check_hello_connection);
5312 kill_client_connection_unchecked (foo);
5314 bus_context_unref (context);
5319 #ifdef HAVE_UNIX_FD_PASSING
5322 bus_unix_fds_passing_test(const DBusString *test_data_dir)
5324 BusContext *context;
5325 DBusConnection *foo, *bar;
5328 DBusSocket one[2], two[2];
5332 dbus_error_init (&error);
5334 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
5335 if (context == NULL)
5336 _dbus_assert_not_reached ("could not alloc context");
5338 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5340 _dbus_assert_not_reached ("could not alloc connection");
5342 if (!bus_setup_debug_client (foo))
5343 _dbus_assert_not_reached ("could not set up connection");
5345 spin_connection_until_authenticated (context, foo);
5347 if (!check_hello_message (context, foo))
5348 _dbus_assert_not_reached ("hello message failed");
5350 if (!check_add_match (context, foo, ""))
5351 _dbus_assert_not_reached ("AddMatch message failed");
5353 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
5355 _dbus_assert_not_reached ("could not alloc connection");
5357 if (!bus_setup_debug_client (bar))
5358 _dbus_assert_not_reached ("could not set up connection");
5360 spin_connection_until_authenticated (context, bar);
5362 if (!check_hello_message (context, bar))
5363 _dbus_assert_not_reached ("hello message failed");
5365 if (!check_add_match (context, bar, ""))
5366 _dbus_assert_not_reached ("AddMatch message failed");
5368 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
5369 _dbus_assert_not_reached ("could not alloc message");
5371 if (!(_dbus_socketpair (one, one+1, TRUE, &error)))
5372 _dbus_assert_not_reached("Failed to allocate pipe #1");
5374 if (!(_dbus_socketpair (two, two+1, TRUE, &error)))
5375 _dbus_assert_not_reached("Failed to allocate pipe #2");
5377 if (!dbus_message_append_args(m,
5378 DBUS_TYPE_UNIX_FD, one,
5379 DBUS_TYPE_UNIX_FD, two,
5380 DBUS_TYPE_UNIX_FD, two,
5382 _dbus_assert_not_reached("Failed to attach fds.");
5384 if (!_dbus_close_socket (one[0], &error))
5385 _dbus_assert_not_reached("Failed to close pipe #1 ");
5386 if (!_dbus_close_socket (two[0], &error))
5387 _dbus_assert_not_reached("Failed to close pipe #2 ");
5389 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
5390 _dbus_assert_not_reached("Connection cannot do fd passing");
5392 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
5393 _dbus_assert_not_reached("Connection cannot do fd passing");
5395 if (!dbus_connection_send (foo, m, NULL))
5396 _dbus_assert_not_reached("Failed to send fds");
5398 dbus_message_unref(m);
5400 bus_test_run_clients_loop (SEND_PENDING (foo));
5402 bus_test_run_everything (context);
5404 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
5406 if (!(m = pop_message_waiting_for_memory (foo)))
5407 _dbus_assert_not_reached("Failed to receive msg");
5409 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5410 _dbus_assert_not_reached("bogus message received");
5412 dbus_message_unref(m);
5414 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
5416 if (!(m = pop_message_waiting_for_memory (bar)))
5417 _dbus_assert_not_reached("Failed to receive msg");
5419 if (!dbus_message_is_signal(m, "a.b.c", "d"))
5420 _dbus_assert_not_reached("bogus message received");
5422 if (!dbus_message_get_args(m,
5424 DBUS_TYPE_UNIX_FD, &x,
5425 DBUS_TYPE_UNIX_FD, &y,
5426 DBUS_TYPE_UNIX_FD, &z,
5428 _dbus_assert_not_reached("Failed to parse fds.");
5430 dbus_message_unref(m);
5432 if (write(x, "X", 1) != 1)
5433 _dbus_assert_not_reached("Failed to write to pipe #1");
5434 if (write(y, "Y", 1) != 1)
5435 _dbus_assert_not_reached("Failed to write to pipe #2");
5436 if (write(z, "Z", 1) != 1)
5437 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
5439 if (!_dbus_close(x, &error))
5440 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
5441 if (!_dbus_close(y, &error))
5442 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
5443 if (!_dbus_close(z, &error))
5444 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
5446 if (read(one[1].fd, &r, 1) != 1 || r != 'X')
5447 _dbus_assert_not_reached("Failed to read value from pipe.");
5448 if (read(two[1].fd, &r, 1) != 1 || r != 'Y')
5449 _dbus_assert_not_reached("Failed to read value from pipe.");
5450 if (read(two[1].fd, &r, 1) != 1 || r != 'Z')
5451 _dbus_assert_not_reached("Failed to read value from pipe.");
5453 if (!_dbus_close_socket (one[1], &error))
5454 _dbus_assert_not_reached("Failed to close pipe #1 ");
5455 if (!_dbus_close_socket (two[1], &error))
5456 _dbus_assert_not_reached("Failed to close pipe #2 ");
5458 _dbus_verbose ("Disconnecting foo\n");
5459 kill_client_connection_unchecked (foo);
5461 _dbus_verbose ("Disconnecting bar\n");
5462 kill_client_connection_unchecked (bar);
5464 bus_context_unref (context);
5470 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */