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
27 #include "connection.h"
30 #include "activation.h"
35 #include <dbus/dbus-internals.h>
39 send_one_message (DBusConnection *connection,
41 DBusConnection *sender,
42 DBusConnection *addressed_recipient,
44 BusTransaction *transaction,
47 if (!bus_context_check_security_policy (context, transaction,
53 return TRUE; /* silently don't send it */
55 if (!bus_transaction_send (transaction,
67 bus_dispatch_matches (BusTransaction *transaction,
68 DBusConnection *sender,
69 DBusConnection *addressed_recipient,
74 BusConnections *connections;
76 BusMatchmaker *matchmaker;
80 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
82 /* sender and recipient can both be NULL for the bus driver,
83 * or for signals with no particular recipient
86 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
87 _dbus_assert (dbus_message_get_sender (message) != NULL);
89 context = bus_transaction_get_context (transaction);
91 /* First, send the message to the addressed_recipient, if there is one. */
92 if (addressed_recipient != NULL)
94 if (!bus_context_check_security_policy (context, transaction,
95 sender, addressed_recipient,
100 /* Dispatch the message */
101 if (!bus_transaction_send (transaction, addressed_recipient, message))
108 /* Now dispatch to others who look interested in this message */
109 connections = bus_transaction_get_connections (transaction);
110 dbus_error_init (&tmp_error);
111 matchmaker = bus_context_get_matchmaker (context);
114 if (!bus_matchmaker_get_recipients (matchmaker, connections,
115 sender, addressed_recipient, message,
122 link = _dbus_list_get_first_link (&recipients);
125 DBusConnection *dest;
129 if (!send_one_message (dest, context, sender, addressed_recipient,
130 message, transaction, &tmp_error))
133 link = _dbus_list_get_next_link (&recipients, link);
136 _dbus_list_clear (&recipients);
138 if (dbus_error_is_set (&tmp_error))
140 dbus_move_error (&tmp_error, error);
147 static DBusHandlerResult
148 bus_dispatch (DBusConnection *connection,
149 DBusMessage *message)
151 const char *sender, *service_name;
153 BusTransaction *transaction;
155 DBusHandlerResult result;
156 DBusConnection *addressed_recipient;
158 result = DBUS_HANDLER_RESULT_HANDLED;
161 addressed_recipient = NULL;
162 dbus_error_init (&error);
164 context = bus_connection_get_context (connection);
165 _dbus_assert (context != NULL);
167 /* If we can't even allocate an OOM error, we just go to sleep
170 while (!bus_connection_preallocate_oom_error (connection))
171 _dbus_wait_for_memory ();
173 /* Ref connection in case we disconnect it at some point in here */
174 dbus_connection_ref (connection);
176 service_name = dbus_message_get_destination (message);
178 #ifdef DBUS_ENABLE_VERBOSE_MODE
180 const char *interface_name, *member_name, *error_name;
182 interface_name = dbus_message_get_interface (message);
183 member_name = dbus_message_get_member (message);
184 error_name = dbus_message_get_error_name (message);
186 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
187 interface_name ? interface_name : "(no interface)",
188 member_name ? member_name : "(no member)",
189 error_name ? error_name : "(no error name)",
190 service_name ? service_name : "peer");
192 #endif /* DBUS_ENABLE_VERBOSE_MODE */
194 /* If service_name is NULL, if it's a signal we send it to all
195 * connections with a match rule. If it's not a signal, there
196 * are some special cases here but mostly we just bail out.
198 if (service_name == NULL)
200 if (dbus_message_is_signal (message,
201 DBUS_INTERFACE_LOCAL,
204 bus_connection_disconnected (connection);
208 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
210 /* DBusConnection also handles some of these automatically, we leave
213 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
218 /* Create our transaction */
219 transaction = bus_transaction_new (context);
220 if (transaction == NULL)
222 BUS_SET_OOM (&error);
226 /* Assign a sender to the message */
227 if (bus_connection_is_active (connection))
229 sender = bus_connection_get_name (connection);
230 _dbus_assert (sender != NULL);
232 if (!dbus_message_set_sender (message, sender))
234 BUS_SET_OOM (&error);
238 /* We need to refetch the service name here, because
239 * dbus_message_set_sender can cause the header to be
240 * reallocated, and thus the service_name pointer will become
243 service_name = dbus_message_get_destination (message);
247 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
249 if (!bus_context_check_security_policy (context, transaction,
250 connection, NULL, NULL, message, &error))
252 _dbus_verbose ("Security policy rejected message\n");
256 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
257 if (!bus_driver_handle_message (connection, transaction, message, &error))
260 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
262 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
263 dbus_connection_close (connection);
266 else if (service_name != NULL) /* route to named service */
268 DBusString service_string;
270 BusRegistry *registry;
272 _dbus_assert (service_name != NULL);
274 registry = bus_connection_get_registry (connection);
276 _dbus_string_init_const (&service_string, service_name);
277 service = bus_registry_lookup (registry, &service_string);
279 if (service == NULL && dbus_message_get_auto_start (message))
281 BusActivation *activation;
282 /* We can't do the security policy check here, since the addressed
283 * recipient service doesn't exist yet. We do it before sending the
284 * message after the service has been created.
286 activation = bus_connection_get_activation (connection);
288 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
289 message, service_name, &error))
291 _DBUS_ASSERT_ERROR_IS_SET (&error);
292 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
298 else if (service == NULL)
300 dbus_set_error (&error,
301 DBUS_ERROR_NAME_HAS_NO_OWNER,
302 "Name \"%s\" does not exist",
308 addressed_recipient = bus_service_get_primary_owners_connection (service);
309 _dbus_assert (addressed_recipient != NULL);
313 /* Now send the message to its destination (or not, if
314 * addressed_recipient == NULL), and match it against other connections'
317 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
321 if (dbus_error_is_set (&error))
323 if (!dbus_connection_get_is_connected (connection))
325 /* If we disconnected it, we won't bother to send it any error
328 _dbus_verbose ("Not sending error to connection we disconnected\n");
330 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
332 bus_connection_send_oom_error (connection, message);
334 /* cancel transaction due to OOM */
335 if (transaction != NULL)
337 bus_transaction_cancel_and_free (transaction);
343 /* Try to send the real error, if no mem to do that, send
346 _dbus_assert (transaction != NULL);
347 if (!bus_transaction_send_error_reply (transaction, connection,
350 bus_connection_send_oom_error (connection, message);
352 /* cancel transaction due to OOM */
353 if (transaction != NULL)
355 bus_transaction_cancel_and_free (transaction);
362 dbus_error_free (&error);
365 if (transaction != NULL)
367 bus_transaction_execute_and_free (transaction);
370 dbus_connection_unref (connection);
375 static DBusHandlerResult
376 bus_dispatch_message_filter (DBusConnection *connection,
377 DBusMessage *message,
380 return bus_dispatch (connection, message);
384 bus_dispatch_add_connection (DBusConnection *connection)
386 if (!dbus_connection_add_filter (connection,
387 bus_dispatch_message_filter,
395 bus_dispatch_remove_connection (DBusConnection *connection)
397 /* Here we tell the bus driver that we want to get off. */
398 bus_driver_remove_connection (connection);
400 dbus_connection_remove_filter (connection,
401 bus_dispatch_message_filter,
405 #ifdef DBUS_BUILD_TESTS
409 /* This is used to know whether we need to block in order to finish
410 * sending a message, or whether the initial dbus_connection_send()
411 * already flushed the queue.
413 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
415 typedef dbus_bool_t (* Check1Func) (BusContext *context);
416 typedef dbus_bool_t (* Check2Func) (BusContext *context,
417 DBusConnection *connection);
419 static dbus_bool_t check_no_leftovers (BusContext *context);
422 block_connection_until_message_from_bus (BusContext *context,
423 DBusConnection *connection,
424 const char *what_is_expected)
426 _dbus_verbose ("expecting: %s\n", what_is_expected);
428 while (dbus_connection_get_dispatch_status (connection) ==
429 DBUS_DISPATCH_COMPLETE &&
430 dbus_connection_get_is_connected (connection))
432 bus_test_run_bus_loop (context, TRUE);
433 bus_test_run_clients_loop (FALSE);
438 spin_connection_until_authenticated (BusContext *context,
439 DBusConnection *connection)
441 _dbus_verbose ("Spinning to auth connection %p\n", connection);
442 while (!dbus_connection_get_is_authenticated (connection) &&
443 dbus_connection_get_is_connected (connection))
445 bus_test_run_bus_loop (context, FALSE);
446 bus_test_run_clients_loop (FALSE);
448 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
451 /* compensate for fact that pop_message() can return #NULL due to OOM */
453 pop_message_waiting_for_memory (DBusConnection *connection)
455 while (dbus_connection_get_dispatch_status (connection) ==
456 DBUS_DISPATCH_NEED_MEMORY)
457 _dbus_wait_for_memory ();
459 return dbus_connection_pop_message (connection);
463 borrow_message_waiting_for_memory (DBusConnection *connection)
465 while (dbus_connection_get_dispatch_status (connection) ==
466 DBUS_DISPATCH_NEED_MEMORY)
467 _dbus_wait_for_memory ();
469 return dbus_connection_borrow_message (connection);
473 warn_unexpected_real (DBusConnection *connection,
474 DBusMessage *message,
475 const char *expected,
476 const char *function,
480 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
482 dbus_message_get_interface (message) ?
483 dbus_message_get_interface (message) : "(unset)",
484 dbus_message_get_member (message) ?
485 dbus_message_get_member (message) : "(unset)",
486 dbus_message_get_error_name (message) ?
487 dbus_message_get_error_name (message) : "(unset)",
491 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
492 function, line, connection, expected);
495 #define warn_unexpected(connection, message, expected) \
496 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
499 verbose_message_received (DBusConnection *connection,
500 DBusMessage *message)
502 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
503 dbus_message_get_interface (message) ?
504 dbus_message_get_interface (message) : "(unset)",
505 dbus_message_get_member (message) ?
506 dbus_message_get_member (message) : "(unset)",
507 dbus_message_get_error_name (message) ?
508 dbus_message_get_error_name (message) : "(unset)",
521 ServiceInfoKind expected_kind;
522 const char *expected_service_name;
524 DBusConnection *skip_connection;
525 } CheckServiceOwnerChangedData;
528 check_service_owner_changed_foreach (DBusConnection *connection,
531 CheckServiceOwnerChangedData *d = data;
532 DBusMessage *message;
534 const char *service_name, *old_owner, *new_owner;
536 if (d->expected_kind == SERVICE_CREATED
537 && connection == d->skip_connection)
540 dbus_error_init (&error);
543 message = pop_message_waiting_for_memory (connection);
546 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
547 connection, "NameOwnerChanged");
550 else if (!dbus_message_is_signal (message,
554 warn_unexpected (connection, message, "NameOwnerChanged");
560 reget_service_info_data:
565 dbus_message_get_args (message, &error,
566 DBUS_TYPE_STRING, &service_name,
567 DBUS_TYPE_STRING, &old_owner,
568 DBUS_TYPE_STRING, &new_owner,
571 if (dbus_error_is_set (&error))
573 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
575 dbus_error_free (&error);
576 _dbus_wait_for_memory ();
577 goto reget_service_info_data;
581 _dbus_warn ("Did not get the expected arguments\n");
586 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
587 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
588 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
590 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
594 if (strcmp (service_name, d->expected_service_name) != 0)
596 _dbus_warn ("expected info on service %s, got info on %s\n",
597 d->expected_service_name,
602 if (*service_name == ':' && new_owner[0]
603 && strcmp (service_name, new_owner) != 0)
605 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
606 service_name, old_owner, new_owner);
614 dbus_error_free (&error);
617 dbus_message_unref (message);
624 kill_client_connection (BusContext *context,
625 DBusConnection *connection)
629 CheckServiceOwnerChangedData socd;
631 _dbus_verbose ("killing connection %p\n", connection);
633 s = dbus_bus_get_unique_name (connection);
634 _dbus_assert (s != NULL);
636 while ((base_service = _dbus_strdup (s)) == NULL)
637 _dbus_wait_for_memory ();
639 dbus_connection_ref (connection);
641 /* kick in the disconnect handler that unrefs the connection */
642 dbus_connection_close (connection);
644 bus_test_run_everything (context);
646 _dbus_assert (bus_test_client_listed (connection));
648 /* Run disconnect handler in test.c */
649 if (bus_connection_dispatch_one_message (connection))
650 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
652 _dbus_assert (!dbus_connection_get_is_connected (connection));
653 dbus_connection_unref (connection);
655 _dbus_assert (!bus_test_client_listed (connection));
657 socd.expected_kind = SERVICE_DELETED;
658 socd.expected_service_name = base_service;
660 socd.skip_connection = NULL;
662 bus_test_clients_foreach (check_service_owner_changed_foreach,
665 dbus_free (base_service);
668 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
670 if (!check_no_leftovers (context))
671 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
675 kill_client_connection_unchecked (DBusConnection *connection)
677 /* This kills the connection without expecting it to affect
678 * the rest of the bus.
680 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
682 dbus_connection_ref (connection);
683 dbus_connection_close (connection);
684 /* dispatching disconnect handler will unref once */
685 if (bus_connection_dispatch_one_message (connection))
686 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
688 _dbus_assert (!bus_test_client_listed (connection));
689 dbus_connection_unref (connection);
695 } CheckNoMessagesData;
698 check_no_messages_foreach (DBusConnection *connection,
701 CheckNoMessagesData *d = data;
702 DBusMessage *message;
704 message = pop_message_waiting_for_memory (connection);
707 warn_unexpected (connection, message, "no messages");
713 dbus_message_unref (message);
718 check_no_leftovers (BusContext *context)
720 CheckNoMessagesData nmd;
723 bus_test_clients_foreach (check_no_messages_foreach,
728 _dbus_verbose ("%s: leftover message found\n",
729 _DBUS_FUNCTION_NAME);
736 /* returns TRUE if the correct thing happens,
737 * but the correct thing may include OOM errors.
740 check_hello_message (BusContext *context,
741 DBusConnection *connection)
743 DBusMessage *message;
744 DBusMessage *name_message;
745 dbus_uint32_t serial;
749 const char *acquired;
752 dbus_error_init (&error);
758 _dbus_verbose ("check_hello_message for %p\n", connection);
760 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
768 dbus_connection_ref (connection); /* because we may get disconnected */
770 if (!dbus_connection_send (connection, message, &serial))
772 dbus_message_unref (message);
773 dbus_connection_unref (connection);
777 _dbus_assert (dbus_message_has_signature (message, ""));
779 dbus_message_unref (message);
782 if (!dbus_connection_get_is_connected (connection))
784 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
786 dbus_connection_unref (connection);
791 /* send our message */
792 bus_test_run_clients_loop (SEND_PENDING (connection));
794 if (!dbus_connection_get_is_connected (connection))
796 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
798 dbus_connection_unref (connection);
803 block_connection_until_message_from_bus (context, connection, "reply to Hello");
805 if (!dbus_connection_get_is_connected (connection))
807 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
809 dbus_connection_unref (connection);
814 dbus_connection_unref (connection);
816 message = pop_message_waiting_for_memory (connection);
819 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
820 "Hello", serial, connection);
824 verbose_message_received (connection, message);
826 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
828 _dbus_warn ("Message has wrong sender %s\n",
829 dbus_message_get_sender (message) ?
830 dbus_message_get_sender (message) : "(none)");
834 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
836 if (dbus_message_is_error (message,
837 DBUS_ERROR_NO_MEMORY))
839 ; /* good, this is a valid response */
843 warn_unexpected (connection, message, "not this error");
850 CheckServiceOwnerChangedData socd;
852 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
854 ; /* good, expected */
858 warn_unexpected (connection, message, "method return for Hello");
863 retry_get_hello_name:
864 if (!dbus_message_get_args (message, &error,
865 DBUS_TYPE_STRING, &name,
868 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
870 _dbus_verbose ("no memory to get service name arg from hello\n");
871 dbus_error_free (&error);
872 _dbus_wait_for_memory ();
873 goto retry_get_hello_name;
877 _dbus_assert (dbus_error_is_set (&error));
878 _dbus_warn ("Did not get the expected single string argument to hello\n");
883 _dbus_verbose ("Got hello name: %s\n", name);
885 while (!dbus_bus_set_unique_name (connection, name))
886 _dbus_wait_for_memory ();
888 socd.expected_kind = SERVICE_CREATED;
889 socd.expected_service_name = name;
891 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
892 bus_test_clients_foreach (check_service_owner_changed_foreach,
898 name_message = message;
899 /* Client should also have gotten ServiceAcquired */
901 message = pop_message_waiting_for_memory (connection);
904 _dbus_warn ("Expecting %s, got nothing\n",
908 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
911 _dbus_warn ("Expecting %s, got smthg else\n",
916 retry_get_acquired_name:
917 if (!dbus_message_get_args (message, &error,
918 DBUS_TYPE_STRING, &acquired,
921 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
923 _dbus_verbose ("no memory to get service name arg from acquired\n");
924 dbus_error_free (&error);
925 _dbus_wait_for_memory ();
926 goto retry_get_acquired_name;
930 _dbus_assert (dbus_error_is_set (&error));
931 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
936 _dbus_verbose ("Got acquired name: %s\n", acquired);
938 if (strcmp (acquired, name) != 0)
940 _dbus_warn ("Acquired name is %s but expected %s\n",
947 if (!check_no_leftovers (context))
953 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
955 dbus_error_free (&error);
958 dbus_message_unref (message);
961 dbus_message_unref (name_message);
966 /* returns TRUE if the correct thing happens,
967 * but the correct thing may include OOM errors.
970 check_double_hello_message (BusContext *context,
971 DBusConnection *connection)
973 DBusMessage *message;
974 dbus_uint32_t serial;
979 dbus_error_init (&error);
982 _dbus_verbose ("check_double_hello_message for %p\n", connection);
984 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
992 if (!dbus_connection_send (connection, message, &serial))
994 dbus_message_unref (message);
998 dbus_message_unref (message);
1001 /* send our message */
1002 bus_test_run_clients_loop (SEND_PENDING (connection));
1004 dbus_connection_ref (connection); /* because we may get disconnected */
1005 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1007 if (!dbus_connection_get_is_connected (connection))
1009 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1011 dbus_connection_unref (connection);
1016 dbus_connection_unref (connection);
1018 message = pop_message_waiting_for_memory (connection);
1019 if (message == NULL)
1021 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1022 "Hello", serial, connection);
1026 verbose_message_received (connection, message);
1028 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1030 _dbus_warn ("Message has wrong sender %s\n",
1031 dbus_message_get_sender (message) ?
1032 dbus_message_get_sender (message) : "(none)");
1036 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1038 warn_unexpected (connection, message, "method return for Hello");
1042 if (!check_no_leftovers (context))
1048 dbus_error_free (&error);
1051 dbus_message_unref (message);
1056 /* returns TRUE if the correct thing happens,
1057 * but the correct thing may include OOM errors.
1060 check_get_connection_unix_user (BusContext *context,
1061 DBusConnection *connection)
1063 DBusMessage *message;
1064 dbus_uint32_t serial;
1067 const char *base_service_name;
1071 dbus_error_init (&error);
1074 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1076 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1078 DBUS_INTERFACE_DBUS,
1079 "GetConnectionUnixUser");
1081 if (message == NULL)
1084 base_service_name = dbus_bus_get_unique_name (connection);
1086 if (!dbus_message_append_args (message,
1087 DBUS_TYPE_STRING, &base_service_name,
1090 dbus_message_unref (message);
1094 if (!dbus_connection_send (connection, message, &serial))
1096 dbus_message_unref (message);
1100 /* send our message */
1101 bus_test_run_clients_loop (SEND_PENDING (connection));
1103 dbus_message_unref (message);
1106 dbus_connection_ref (connection); /* because we may get disconnected */
1107 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1109 if (!dbus_connection_get_is_connected (connection))
1111 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1113 dbus_connection_unref (connection);
1118 dbus_connection_unref (connection);
1120 message = pop_message_waiting_for_memory (connection);
1121 if (message == NULL)
1123 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1124 "GetConnectionUnixUser", serial, connection);
1128 verbose_message_received (connection, message);
1130 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1132 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1134 ; /* good, this is a valid response */
1138 warn_unexpected (connection, message, "not this error");
1145 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1147 ; /* good, expected */
1151 warn_unexpected (connection, message,
1152 "method_return for GetConnectionUnixUser");
1159 if (!dbus_message_get_args (message, &error,
1160 DBUS_TYPE_UINT32, &uid,
1163 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1165 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1166 dbus_error_free (&error);
1167 _dbus_wait_for_memory ();
1168 goto retry_get_property;
1172 _dbus_assert (dbus_error_is_set (&error));
1173 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1179 if (!check_no_leftovers (context))
1185 dbus_error_free (&error);
1188 dbus_message_unref (message);
1193 /* returns TRUE if the correct thing happens,
1194 * but the correct thing may include OOM errors.
1197 check_get_connection_unix_process_id (BusContext *context,
1198 DBusConnection *connection)
1200 DBusMessage *message;
1201 dbus_uint32_t serial;
1204 const char *base_service_name;
1208 dbus_error_init (&error);
1211 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1213 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1215 DBUS_INTERFACE_DBUS,
1216 "GetConnectionUnixProcessID");
1218 if (message == NULL)
1221 base_service_name = dbus_bus_get_unique_name (connection);
1223 if (!dbus_message_append_args (message,
1224 DBUS_TYPE_STRING, &base_service_name,
1227 dbus_message_unref (message);
1231 if (!dbus_connection_send (connection, message, &serial))
1233 dbus_message_unref (message);
1237 /* send our message */
1238 bus_test_run_clients_loop (SEND_PENDING (connection));
1240 dbus_message_unref (message);
1243 dbus_connection_ref (connection); /* because we may get disconnected */
1244 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1246 if (!dbus_connection_get_is_connected (connection))
1248 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1250 dbus_connection_unref (connection);
1255 dbus_connection_unref (connection);
1257 message = pop_message_waiting_for_memory (connection);
1258 if (message == NULL)
1260 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1261 "GetConnectionUnixProcessID", serial, connection);
1265 verbose_message_received (connection, message);
1267 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1269 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1271 ; /* good, this is a valid response */
1274 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1276 /* We are expecting this error, since we know in the test suite we aren't
1277 * talking to a client running on UNIX
1279 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1284 warn_unexpected (connection, message, "not this error");
1292 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1295 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1297 ; /* good, expected */
1301 warn_unexpected (connection, message,
1302 "method_return for GetConnectionUnixProcessID");
1309 if (!dbus_message_get_args (message, &error,
1310 DBUS_TYPE_UINT32, &pid,
1313 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1315 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1316 dbus_error_free (&error);
1317 _dbus_wait_for_memory ();
1318 goto retry_get_property;
1322 _dbus_assert (dbus_error_is_set (&error));
1323 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1329 /* test if returned pid is the same as our own pid
1331 * @todo It would probably be good to restructure the tests
1332 * in a way so our parent is the bus that we're testing
1333 * cause then we can test that the pid returned matches
1336 if (pid != (dbus_uint32_t) _dbus_getpid ())
1338 _dbus_assert (dbus_error_is_set (&error));
1339 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1343 #endif /* !DBUS_WIN */
1346 if (!check_no_leftovers (context))
1352 dbus_error_free (&error);
1355 dbus_message_unref (message);
1360 /* returns TRUE if the correct thing happens,
1361 * but the correct thing may include OOM errors.
1364 check_add_match_all (BusContext *context,
1365 DBusConnection *connection)
1367 DBusMessage *message;
1369 dbus_uint32_t serial;
1371 const char *empty = "";
1374 dbus_error_init (&error);
1377 _dbus_verbose ("check_add_match_all for %p\n", connection);
1379 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1381 DBUS_INTERFACE_DBUS,
1384 if (message == NULL)
1387 /* empty string match rule matches everything */
1388 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1391 dbus_message_unref (message);
1395 if (!dbus_connection_send (connection, message, &serial))
1397 dbus_message_unref (message);
1401 dbus_message_unref (message);
1404 dbus_connection_ref (connection); /* because we may get disconnected */
1406 /* send our message */
1407 bus_test_run_clients_loop (SEND_PENDING (connection));
1409 if (!dbus_connection_get_is_connected (connection))
1411 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1413 dbus_connection_unref (connection);
1418 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1420 if (!dbus_connection_get_is_connected (connection))
1422 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1424 dbus_connection_unref (connection);
1429 dbus_connection_unref (connection);
1431 message = pop_message_waiting_for_memory (connection);
1432 if (message == NULL)
1434 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1435 "AddMatch", serial, connection);
1439 verbose_message_received (connection, message);
1441 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1443 _dbus_warn ("Message has wrong sender %s\n",
1444 dbus_message_get_sender (message) ?
1445 dbus_message_get_sender (message) : "(none)");
1449 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1451 if (dbus_message_is_error (message,
1452 DBUS_ERROR_NO_MEMORY))
1454 ; /* good, this is a valid response */
1458 warn_unexpected (connection, message, "not this error");
1465 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1467 ; /* good, expected */
1468 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1472 warn_unexpected (connection, message, "method return for AddMatch");
1478 if (!check_no_leftovers (context))
1484 dbus_error_free (&error);
1487 dbus_message_unref (message);
1492 /* returns TRUE if the correct thing happens,
1493 * but the correct thing may include OOM errors.
1496 check_hello_connection (BusContext *context)
1498 DBusConnection *connection;
1501 dbus_error_init (&error);
1503 connection = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
1504 if (connection == NULL)
1506 _DBUS_ASSERT_ERROR_IS_SET (&error);
1507 dbus_error_free (&error);
1511 if (!bus_setup_debug_client (connection))
1513 dbus_connection_close (connection);
1514 dbus_connection_unref (connection);
1518 spin_connection_until_authenticated (context, connection);
1520 if (!check_hello_message (context, connection))
1523 if (dbus_bus_get_unique_name (connection) == NULL)
1525 /* We didn't successfully register, so we can't
1526 * do the usual kill_client_connection() checks
1528 kill_client_connection_unchecked (connection);
1532 if (!check_add_match_all (context, connection))
1535 kill_client_connection (context, connection);
1541 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1543 /* returns TRUE if the correct thing happens,
1544 * but the correct thing may include OOM errors.
1547 check_nonexistent_service_no_auto_start (BusContext *context,
1548 DBusConnection *connection)
1550 DBusMessage *message;
1551 dbus_uint32_t serial;
1553 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1554 dbus_uint32_t flags;
1556 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1558 DBUS_INTERFACE_DBUS,
1559 "StartServiceByName");
1561 if (message == NULL)
1564 dbus_message_set_auto_start (message, FALSE);
1567 if (!dbus_message_append_args (message,
1568 DBUS_TYPE_STRING, &nonexistent,
1569 DBUS_TYPE_UINT32, &flags,
1572 dbus_message_unref (message);
1576 if (!dbus_connection_send (connection, message, &serial))
1578 dbus_message_unref (message);
1582 dbus_message_unref (message);
1585 bus_test_run_everything (context);
1586 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1587 bus_test_run_everything (context);
1589 if (!dbus_connection_get_is_connected (connection))
1591 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1597 message = pop_message_waiting_for_memory (connection);
1598 if (message == NULL)
1600 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1601 "StartServiceByName", serial, connection);
1605 verbose_message_received (connection, message);
1607 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1609 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1611 _dbus_warn ("Message has wrong sender %s\n",
1612 dbus_message_get_sender (message) ?
1613 dbus_message_get_sender (message) : "(none)");
1617 if (dbus_message_is_error (message,
1618 DBUS_ERROR_NO_MEMORY))
1620 ; /* good, this is a valid response */
1622 else if (dbus_message_is_error (message,
1623 DBUS_ERROR_SERVICE_UNKNOWN))
1625 ; /* good, this is expected also */
1629 warn_unexpected (connection, message, "not this error");
1635 _dbus_warn ("Did not expect to successfully activate %s\n",
1636 NONEXISTENT_SERVICE_NAME);
1644 dbus_message_unref (message);
1649 /* returns TRUE if the correct thing happens,
1650 * but the correct thing may include OOM errors.
1653 check_nonexistent_service_auto_start (BusContext *context,
1654 DBusConnection *connection)
1656 DBusMessage *message;
1657 dbus_uint32_t serial;
1660 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1661 "/org/freedesktop/TestSuite",
1662 "org.freedesktop.TestSuite",
1665 if (message == NULL)
1668 if (!dbus_connection_send (connection, message, &serial))
1670 dbus_message_unref (message);
1674 dbus_message_unref (message);
1677 bus_test_run_everything (context);
1678 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1679 bus_test_run_everything (context);
1681 if (!dbus_connection_get_is_connected (connection))
1683 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1689 message = pop_message_waiting_for_memory (connection);
1691 if (message == NULL)
1693 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1694 "Echo message (auto activation)", serial, connection);
1698 verbose_message_received (connection, message);
1700 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1702 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1704 _dbus_warn ("Message has wrong sender %s\n",
1705 dbus_message_get_sender (message) ?
1706 dbus_message_get_sender (message) : "(none)");
1710 if (dbus_message_is_error (message,
1711 DBUS_ERROR_NO_MEMORY))
1713 ; /* good, this is a valid response */
1715 else if (dbus_message_is_error (message,
1716 DBUS_ERROR_SERVICE_UNKNOWN))
1718 ; /* good, this is expected also */
1722 warn_unexpected (connection, message, "not this error");
1728 _dbus_warn ("Did not expect to successfully activate %s\n",
1729 NONEXISTENT_SERVICE_NAME);
1737 dbus_message_unref (message);
1743 check_base_service_activated (BusContext *context,
1744 DBusConnection *connection,
1745 DBusMessage *initial_message,
1746 const char **base_service_p)
1748 DBusMessage *message;
1751 const char *base_service, *base_service_from_bus, *old_owner;
1755 dbus_error_init (&error);
1756 base_service = NULL;
1758 base_service_from_bus = NULL;
1760 message = initial_message;
1761 dbus_message_ref (message);
1763 if (dbus_message_is_signal (message,
1764 DBUS_INTERFACE_DBUS,
1765 "NameOwnerChanged"))
1767 CheckServiceOwnerChangedData socd;
1769 reget_service_name_arg:
1770 base_service = NULL;
1772 base_service_from_bus = NULL;
1774 if (!dbus_message_get_args (message, &error,
1775 DBUS_TYPE_STRING, &base_service,
1776 DBUS_TYPE_STRING, &old_owner,
1777 DBUS_TYPE_STRING, &base_service_from_bus,
1780 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1782 dbus_error_free (&error);
1783 _dbus_wait_for_memory ();
1784 goto reget_service_name_arg;
1788 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1789 "NameOwnerChanged (creation)",
1795 if (*base_service != ':')
1797 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1802 if (strcmp (base_service, base_service_from_bus) != 0)
1804 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1805 base_service, base_service_from_bus);
1811 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1816 socd.expected_kind = SERVICE_CREATED;
1817 socd.expected_service_name = base_service;
1818 socd.failed = FALSE;
1819 socd.skip_connection = connection;
1820 bus_test_clients_foreach (check_service_owner_changed_foreach,
1828 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1834 *base_service_p = base_service;
1840 dbus_message_unref (message);
1841 dbus_error_free (&error);
1847 check_service_activated (BusContext *context,
1848 DBusConnection *connection,
1849 const char *activated_name,
1850 const char *base_service_name,
1851 DBusMessage *initial_message)
1853 DBusMessage *message;
1856 dbus_uint32_t activation_result;
1860 dbus_error_init (&error);
1862 message = initial_message;
1863 dbus_message_ref (message);
1865 if (dbus_message_is_signal (message,
1866 DBUS_INTERFACE_DBUS,
1867 "NameOwnerChanged"))
1869 CheckServiceOwnerChangedData socd;
1870 const char *service_name, *base_service_from_bus, *old_owner;
1872 reget_service_name_arg:
1873 service_name = NULL;
1875 base_service_from_bus = NULL;
1877 if (!dbus_message_get_args (message, &error,
1878 DBUS_TYPE_STRING, &service_name,
1879 DBUS_TYPE_STRING, &old_owner,
1880 DBUS_TYPE_STRING, &base_service_from_bus,
1883 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1885 dbus_error_free (&error);
1886 _dbus_wait_for_memory ();
1887 goto reget_service_name_arg;
1891 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1892 "NameOwnerChanged (creation)",
1898 if (strcmp (service_name, activated_name) != 0)
1900 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1901 activated_name, service_name);
1905 if (strcmp (base_service_name, base_service_from_bus) != 0)
1907 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1908 base_service_from_bus, base_service_name);
1914 _dbus_warn ("expected a %s, got a %s\n",
1915 "NameOwnerChanged (creation)",
1916 "NameOwnerChanged (change)");
1920 socd.expected_kind = SERVICE_CREATED;
1921 socd.skip_connection = connection;
1922 socd.failed = FALSE;
1923 socd.expected_service_name = service_name;
1924 bus_test_clients_foreach (check_service_owner_changed_foreach,
1930 dbus_message_unref (message);
1931 service_name = NULL;
1933 base_service_from_bus = NULL;
1935 message = pop_message_waiting_for_memory (connection);
1936 if (message == NULL)
1938 _dbus_warn ("Expected a reply to %s, got nothing\n",
1939 "StartServiceByName");
1945 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1950 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1952 warn_unexpected (connection, message, "reply to StartServiceByName");
1957 activation_result = 0;
1958 if (!dbus_message_get_args (message, &error,
1959 DBUS_TYPE_UINT32, &activation_result,
1962 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1964 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1965 "StartServiceByName", error.message);
1969 dbus_error_free (&error);
1973 if (activation_result == DBUS_START_REPLY_SUCCESS)
1975 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1979 _dbus_warn ("Activation result was %u, no good.\n",
1985 dbus_message_unref (message);
1988 if (!check_no_leftovers (context))
1990 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1998 dbus_message_unref (message);
1999 dbus_error_free (&error);
2005 check_service_auto_activated (BusContext *context,
2006 DBusConnection *connection,
2007 const char *activated_name,
2008 const char *base_service_name,
2009 DBusMessage *initial_message)
2011 DBusMessage *message;
2017 dbus_error_init (&error);
2019 message = initial_message;
2020 dbus_message_ref (message);
2022 if (dbus_message_is_signal (message,
2023 DBUS_INTERFACE_DBUS,
2024 "NameOwnerChanged"))
2026 const char *service_name;
2027 CheckServiceOwnerChangedData socd;
2029 reget_service_name_arg:
2030 if (!dbus_message_get_args (message, &error,
2031 DBUS_TYPE_STRING, &service_name,
2034 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2036 dbus_error_free (&error);
2037 _dbus_wait_for_memory ();
2038 goto reget_service_name_arg;
2042 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2045 dbus_error_free (&error);
2050 if (strcmp (service_name, activated_name) != 0)
2052 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2053 activated_name, service_name);
2057 socd.expected_kind = SERVICE_CREATED;
2058 socd.expected_service_name = service_name;
2059 socd.failed = FALSE;
2060 socd.skip_connection = connection;
2061 bus_test_clients_foreach (check_service_owner_changed_foreach,
2067 /* Note that this differs from regular activation in that we don't get a
2068 * reply to ActivateService here.
2071 dbus_message_unref (message);
2073 service_name = NULL;
2077 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2086 dbus_message_unref (message);
2092 check_service_deactivated (BusContext *context,
2093 DBusConnection *connection,
2094 const char *activated_name,
2095 const char *base_service)
2098 CheckServiceOwnerChangedData socd;
2102 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2103 * service and the activated_name. The base service
2104 * notification is required to come last.
2106 socd.expected_kind = SERVICE_DELETED;
2107 socd.expected_service_name = activated_name;
2108 socd.failed = FALSE;
2109 socd.skip_connection = NULL;
2110 bus_test_clients_foreach (check_service_owner_changed_foreach,
2116 socd.expected_kind = SERVICE_DELETED;
2117 socd.expected_service_name = base_service;
2118 socd.failed = FALSE;
2119 socd.skip_connection = NULL;
2120 bus_test_clients_foreach (check_service_owner_changed_foreach,
2133 check_send_exit_to_service (BusContext *context,
2134 DBusConnection *connection,
2135 const char *service_name,
2136 const char *base_service)
2138 dbus_bool_t got_error;
2139 DBusMessage *message;
2140 dbus_uint32_t serial;
2143 _dbus_verbose ("Sending exit message to the test service\n");
2147 /* Kill off the test service by sending it a quit message */
2148 message = dbus_message_new_method_call (service_name,
2149 "/org/freedesktop/TestSuite",
2150 "org.freedesktop.TestSuite",
2153 if (message == NULL)
2155 /* Do this again; we still need the service to exit... */
2156 if (!check_send_exit_to_service (context, connection,
2157 service_name, base_service))
2163 if (!dbus_connection_send (connection, message, &serial))
2165 dbus_message_unref (message);
2167 /* Do this again; we still need the service to exit... */
2168 if (!check_send_exit_to_service (context, connection,
2169 service_name, base_service))
2175 dbus_message_unref (message);
2179 bus_test_run_clients_loop (SEND_PENDING (connection));
2181 /* read it in and write it out to test service */
2182 bus_test_run_bus_loop (context, FALSE);
2184 /* see if we got an error during message bus dispatching */
2185 bus_test_run_clients_loop (FALSE);
2186 message = borrow_message_waiting_for_memory (connection);
2187 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2190 dbus_connection_return_message (connection, message);
2196 /* If no error, wait for the test service to exit */
2197 block_connection_until_message_from_bus (context, connection, "test service to exit");
2199 bus_test_run_everything (context);
2204 message = pop_message_waiting_for_memory (connection);
2205 _dbus_assert (message != NULL);
2207 if (dbus_message_get_reply_serial (message) != serial)
2209 warn_unexpected (connection, message,
2210 "error with the correct reply serial");
2214 if (!dbus_message_is_error (message,
2215 DBUS_ERROR_NO_MEMORY))
2217 warn_unexpected (connection, message,
2218 "a no memory error from asking test service to exit");
2222 _dbus_verbose ("Got error %s when asking test service to exit\n",
2223 dbus_message_get_error_name (message));
2225 /* Do this again; we still need the service to exit... */
2226 if (!check_send_exit_to_service (context, connection,
2227 service_name, base_service))
2232 if (!check_service_deactivated (context, connection,
2233 service_name, base_service))
2236 /* Should now have a NoReply error from the Exit() method
2237 * call; it should have come after all the deactivation
2240 message = pop_message_waiting_for_memory (connection);
2242 if (message == NULL)
2244 warn_unexpected (connection, NULL,
2245 "reply to Exit() method call");
2248 if (!dbus_message_is_error (message,
2249 DBUS_ERROR_NO_REPLY))
2251 warn_unexpected (connection, message,
2252 "NoReply error from Exit() method call");
2256 if (dbus_message_get_reply_serial (message) != serial)
2258 warn_unexpected (connection, message,
2259 "error with the correct reply serial");
2263 _dbus_verbose ("Got error %s after test service exited\n",
2264 dbus_message_get_error_name (message));
2266 if (!check_no_leftovers (context))
2268 _dbus_warn ("Messages were left over after %s\n",
2269 _DBUS_FUNCTION_NAME);
2278 dbus_message_unref (message);
2284 check_got_error (BusContext *context,
2285 DBusConnection *connection,
2286 const char *first_error_name,
2289 DBusMessage *message;
2292 dbus_bool_t error_found;
2293 const char *error_name;
2297 message = pop_message_waiting_for_memory (connection);
2298 if (message == NULL)
2300 _dbus_warn ("Did not get an expected error\n");
2304 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2306 warn_unexpected (connection, message, "an error");
2311 error_found = FALSE;
2313 va_start (ap, first_error_name);
2314 error_name = first_error_name;
2315 while (error_name != NULL)
2317 if (dbus_message_is_error (message, error_name))
2322 error_name = va_arg (ap, char*);
2328 _dbus_warn ("Expected error %s or other, got %s instead\n",
2330 dbus_message_get_error_name (message));
2338 dbus_message_unref (message);
2345 GOT_SERVICE_CREATED,
2346 GOT_SERVICE_DELETED,
2351 static GotServiceInfo
2352 check_got_service_info (DBusMessage *message)
2354 GotServiceInfo message_kind;
2356 if (dbus_message_is_signal (message,
2357 DBUS_INTERFACE_DBUS,
2358 "NameOwnerChanged"))
2361 const char *service_name, *old_owner, *new_owner;
2362 dbus_error_init (&error);
2364 reget_service_info_data:
2365 service_name = NULL;
2369 dbus_message_get_args (message, &error,
2370 DBUS_TYPE_STRING, &service_name,
2371 DBUS_TYPE_STRING, &old_owner,
2372 DBUS_TYPE_STRING, &new_owner,
2374 if (dbus_error_is_set (&error))
2376 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2378 dbus_error_free (&error);
2379 goto reget_service_info_data;
2383 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2384 message_kind = GOT_SOMETHING_ELSE;
2387 else if (!old_owner[0])
2388 message_kind = GOT_SERVICE_CREATED;
2389 else if (!new_owner[0])
2390 message_kind = GOT_SERVICE_DELETED;
2392 message_kind = GOT_SOMETHING_ELSE;
2394 dbus_error_free (&error);
2396 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2397 message_kind = GOT_ERROR;
2399 message_kind = GOT_SOMETHING_ELSE;
2401 return message_kind;
2404 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2406 /* returns TRUE if the correct thing happens,
2407 * but the correct thing may include OOM errors.
2410 check_existent_service_no_auto_start (BusContext *context,
2411 DBusConnection *connection)
2413 DBusMessage *message;
2414 DBusMessage *base_service_message;
2415 const char *base_service;
2416 dbus_uint32_t serial;
2418 const char *existent = EXISTENT_SERVICE_NAME;
2419 dbus_uint32_t flags;
2421 base_service_message = NULL;
2423 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2425 DBUS_INTERFACE_DBUS,
2426 "StartServiceByName");
2428 if (message == NULL)
2431 dbus_message_set_auto_start (message, FALSE);
2434 if (!dbus_message_append_args (message,
2435 DBUS_TYPE_STRING, &existent,
2436 DBUS_TYPE_UINT32, &flags,
2439 dbus_message_unref (message);
2443 if (!dbus_connection_send (connection, message, &serial))
2445 dbus_message_unref (message);
2449 dbus_message_unref (message);
2452 bus_test_run_everything (context);
2454 /* now wait for the message bus to hear back from the activated
2457 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2459 bus_test_run_everything (context);
2461 if (!dbus_connection_get_is_connected (connection))
2463 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2469 message = pop_message_waiting_for_memory (connection);
2470 if (message == NULL)
2472 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2473 "StartServiceByName", serial, connection);
2477 verbose_message_received (connection, message);
2478 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2480 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2482 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2484 _dbus_warn ("Message has wrong sender %s\n",
2485 dbus_message_get_sender (message) ?
2486 dbus_message_get_sender (message) : "(none)");
2490 if (dbus_message_is_error (message,
2491 DBUS_ERROR_NO_MEMORY))
2493 ; /* good, this is a valid response */
2495 else if (dbus_message_is_error (message,
2496 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2497 dbus_message_is_error (message,
2498 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2499 dbus_message_is_error (message,
2500 DBUS_ERROR_SPAWN_EXEC_FAILED))
2502 ; /* good, this is expected also */
2506 _dbus_warn ("Did not expect error %s\n",
2507 dbus_message_get_error_name (message));
2513 GotServiceInfo message_kind;
2515 if (!check_base_service_activated (context, connection,
2516 message, &base_service))
2519 base_service_message = message;
2522 /* We may need to block here for the test service to exit or finish up */
2523 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2525 message = dbus_connection_borrow_message (connection);
2526 if (message == NULL)
2528 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2532 message_kind = check_got_service_info (message);
2534 dbus_connection_return_message (connection, message);
2537 switch (message_kind)
2539 case GOT_SOMETHING_ELSE:
2540 _dbus_warn ("Unexpected message after ActivateService "
2541 "(should be an error or a service announcement");
2545 if (!check_got_error (context, connection,
2546 DBUS_ERROR_SPAWN_CHILD_EXITED,
2547 DBUS_ERROR_NO_MEMORY,
2550 /* A service deleted should be coming along now after this error.
2551 * We can also get the error *after* the service deleted.
2556 case GOT_SERVICE_DELETED:
2558 /* The service started up and got a base address, but then
2559 * failed to register under EXISTENT_SERVICE_NAME
2561 CheckServiceOwnerChangedData socd;
2563 socd.expected_kind = SERVICE_DELETED;
2564 socd.expected_service_name = base_service;
2565 socd.failed = FALSE;
2566 socd.skip_connection = NULL;
2568 bus_test_clients_foreach (check_service_owner_changed_foreach,
2574 /* Now we should get an error about the service exiting
2575 * if we didn't get it before.
2577 if (message_kind != GOT_ERROR)
2579 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2581 /* and process everything again */
2582 bus_test_run_everything (context);
2584 if (!check_got_error (context, connection,
2585 DBUS_ERROR_SPAWN_CHILD_EXITED,
2586 DBUS_ERROR_NO_MEMORY,
2593 case GOT_SERVICE_CREATED:
2594 message = pop_message_waiting_for_memory (connection);
2595 if (message == NULL)
2597 _dbus_warn ("Failed to pop message we just put back! "
2598 "should have been a NameOwnerChanged (creation)\n");
2602 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2603 base_service, message))
2606 dbus_message_unref (message);
2609 if (!check_no_leftovers (context))
2611 _dbus_warn ("Messages were left over after successful activation\n");
2615 if (!check_send_exit_to_service (context, connection,
2616 EXISTENT_SERVICE_NAME, base_service))
2627 dbus_message_unref (message);
2629 if (base_service_message)
2630 dbus_message_unref (base_service_message);
2635 #ifndef DBUS_WIN_FIXME
2636 /* returns TRUE if the correct thing happens,
2637 * but the correct thing may include OOM errors.
2640 check_segfault_service_no_auto_start (BusContext *context,
2641 DBusConnection *connection)
2643 DBusMessage *message;
2644 dbus_uint32_t serial;
2646 const char *segv_service;
2647 dbus_uint32_t flags;
2649 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2651 DBUS_INTERFACE_DBUS,
2652 "StartServiceByName");
2654 if (message == NULL)
2657 dbus_message_set_auto_start (message, FALSE);
2659 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2661 if (!dbus_message_append_args (message,
2662 DBUS_TYPE_STRING, &segv_service,
2663 DBUS_TYPE_UINT32, &flags,
2666 dbus_message_unref (message);
2670 if (!dbus_connection_send (connection, message, &serial))
2672 dbus_message_unref (message);
2676 dbus_message_unref (message);
2679 bus_test_run_everything (context);
2680 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2681 bus_test_run_everything (context);
2683 if (!dbus_connection_get_is_connected (connection))
2685 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2691 message = pop_message_waiting_for_memory (connection);
2692 if (message == NULL)
2694 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2695 "StartServiceByName", serial, connection);
2699 verbose_message_received (connection, message);
2701 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2703 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2705 _dbus_warn ("Message has wrong sender %s\n",
2706 dbus_message_get_sender (message) ?
2707 dbus_message_get_sender (message) : "(none)");
2711 if (dbus_message_is_error (message,
2712 DBUS_ERROR_NO_MEMORY))
2714 ; /* good, this is a valid response */
2716 else if (dbus_message_is_error (message,
2719 const char *servicehelper;
2720 servicehelper = bus_context_get_servicehelper (context);
2721 /* make sure this only happens with the launch helper */
2722 _dbus_assert (servicehelper != NULL);
2724 else if (dbus_message_is_error (message,
2725 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2727 ; /* good, this is expected also */
2731 warn_unexpected (connection, message, "not this error");
2738 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2746 dbus_message_unref (message);
2752 /* returns TRUE if the correct thing happens,
2753 * but the correct thing may include OOM errors.
2756 check_segfault_service_auto_start (BusContext *context,
2757 DBusConnection *connection)
2759 DBusMessage *message;
2760 dbus_uint32_t serial;
2763 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2764 "/org/freedesktop/TestSuite",
2765 "org.freedesktop.TestSuite",
2768 if (message == NULL)
2771 if (!dbus_connection_send (connection, message, &serial))
2773 dbus_message_unref (message);
2777 dbus_message_unref (message);
2780 bus_test_run_everything (context);
2781 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2782 bus_test_run_everything (context);
2784 if (!dbus_connection_get_is_connected (connection))
2786 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2792 message = pop_message_waiting_for_memory (connection);
2793 if (message == NULL)
2795 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2796 "Echo message (auto activation)", serial, connection);
2800 verbose_message_received (connection, message);
2802 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2804 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2806 _dbus_warn ("Message has wrong sender %s\n",
2807 dbus_message_get_sender (message) ?
2808 dbus_message_get_sender (message) : "(none)");
2812 if (dbus_message_is_error (message,
2813 DBUS_ERROR_NO_MEMORY))
2815 ; /* good, this is a valid response */
2817 else if (dbus_message_is_error (message,
2818 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2820 ; /* good, this is expected also */
2824 warn_unexpected (connection, message, "not this error");
2831 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2839 dbus_message_unref (message);
2845 #define TEST_ECHO_MESSAGE "Test echo message"
2846 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2848 /* returns TRUE if the correct thing happens,
2849 * but the correct thing may include OOM errors.
2852 check_existent_hello_from_self (BusContext *context,
2853 DBusConnection *connection)
2855 DBusMessage *message;
2856 dbus_uint32_t serial;
2859 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2860 "/org/freedesktop/TestSuite",
2861 "org.freedesktop.TestSuite",
2862 "RunHelloFromSelf");
2864 if (message == NULL)
2867 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2868 if (!dbus_message_append_args (message,
2869 DBUS_TYPE_STRING, &text,
2872 dbus_message_unref (message);
2876 if (!dbus_connection_send (connection, message, &serial))
2878 dbus_message_unref (message);
2882 dbus_message_unref (message);
2885 bus_test_run_everything (context);
2887 /* Note: if this test is run in OOM mode, it will block when the bus
2888 * doesn't send a reply due to OOM.
2890 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2892 message = pop_message_waiting_for_memory (connection);
2893 if (message == NULL)
2895 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2899 if (dbus_message_get_reply_serial (message) != serial)
2901 _dbus_warn ("Wrong reply serial\n");
2902 dbus_message_unref (message);
2906 dbus_message_unref (message);
2912 /* returns TRUE if the correct thing happens,
2913 * but the correct thing may include OOM errors.
2916 check_existent_ping (BusContext *context,
2917 DBusConnection *connection)
2919 DBusMessage *message;
2920 dbus_uint32_t serial;
2921 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2922 "/org/freedesktop/TestSuite",
2923 "org.freedesktop.DBus.Peer",
2926 if (message == NULL)
2929 if (!dbus_connection_send (connection, message, &serial))
2931 dbus_message_unref (message);
2935 dbus_message_unref (message);
2938 bus_test_run_everything (context);
2940 /* Note: if this test is run in OOM mode, it will block when the bus
2941 * doesn't send a reply due to OOM.
2943 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2945 message = pop_message_waiting_for_memory (connection);
2946 if (message == NULL)
2948 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2952 if (dbus_message_get_reply_serial (message) != serial)
2954 _dbus_warn ("Wrong reply serial\n");
2955 dbus_message_unref (message);
2959 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2961 _dbus_warn ("Unexpected message return during Ping\n");
2962 dbus_message_unref (message);
2966 dbus_message_unref (message);
2972 /* returns TRUE if the correct thing happens,
2973 * but the correct thing may include OOM errors.
2976 check_existent_get_machine_id (BusContext *context,
2977 DBusConnection *connection)
2979 DBusMessage *message;
2980 dbus_uint32_t serial;
2981 const char *machine_id;
2983 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2984 "/org/freedesktop/TestSuite",
2985 "org.freedesktop.DBus.Peer",
2988 if (message == NULL)
2991 if (!dbus_connection_send (connection, message, &serial))
2993 dbus_message_unref (message);
2997 dbus_message_unref (message);
3000 bus_test_run_everything (context);
3002 /* Note: if this test is run in OOM mode, it will block when the bus
3003 * doesn't send a reply due to OOM.
3005 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3007 message = pop_message_waiting_for_memory (connection);
3008 if (message == NULL)
3010 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3014 if (dbus_message_get_reply_serial (message) != serial)
3016 _dbus_warn ("Wrong reply serial\n");
3017 dbus_message_unref (message);
3021 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3023 _dbus_warn ("Unexpected message return during GetMachineId\n");
3024 dbus_message_unref (message);
3029 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3031 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3032 dbus_message_unref (message);
3036 if (machine_id == NULL || strlen (machine_id) != 32)
3038 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3039 dbus_message_unref (message);
3043 /* We can't check that the machine id is correct because during make check it is
3044 * just made up for each process separately
3047 dbus_message_unref (message);
3053 /* returns TRUE if the correct thing happens,
3054 * but the correct thing may include OOM errors.
3057 check_existent_service_auto_start (BusContext *context,
3058 DBusConnection *connection)
3060 DBusMessage *message;
3061 DBusMessage *base_service_message;
3062 dbus_uint32_t serial;
3064 const char *base_service;
3067 base_service_message = NULL;
3069 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3070 "/org/freedesktop/TestSuite",
3071 "org.freedesktop.TestSuite",
3074 if (message == NULL)
3077 text = TEST_ECHO_MESSAGE;
3078 if (!dbus_message_append_args (message,
3079 DBUS_TYPE_STRING, &text,
3082 dbus_message_unref (message);
3086 if (!dbus_connection_send (connection, message, &serial))
3088 dbus_message_unref (message);
3092 dbus_message_unref (message);
3095 bus_test_run_everything (context);
3097 /* now wait for the message bus to hear back from the activated
3100 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3101 bus_test_run_everything (context);
3103 if (!dbus_connection_get_is_connected (connection))
3105 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3111 message = pop_message_waiting_for_memory (connection);
3112 if (message == NULL)
3114 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3115 serial, connection);
3119 verbose_message_received (connection, message);
3120 _dbus_verbose (" (after sending %s)\n", "auto start");
3122 /* we should get zero or two ServiceOwnerChanged signals */
3123 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3125 GotServiceInfo message_kind;
3127 if (!check_base_service_activated (context, connection,
3128 message, &base_service))
3131 base_service_message = message;
3134 /* We may need to block here for the test service to exit or finish up */
3135 block_connection_until_message_from_bus (context, connection, "service to exit");
3137 /* Should get a service creation notification for the activated
3138 * service name, or a service deletion on the base service name
3140 message = dbus_connection_borrow_message (connection);
3141 if (message == NULL)
3143 _dbus_warn ("No message after auto activation "
3144 "(should be a service announcement)\n");
3145 dbus_connection_return_message (connection, message);
3150 message_kind = check_got_service_info (message);
3152 dbus_connection_return_message (connection, message);
3155 switch (message_kind)
3157 case GOT_SERVICE_CREATED:
3158 message = pop_message_waiting_for_memory (connection);
3159 if (message == NULL)
3161 _dbus_warn ("Failed to pop message we just put back! "
3162 "should have been a NameOwnerChanged (creation)\n");
3166 /* Check that ServiceOwnerChanged (creation) was correctly received */
3167 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3168 base_service, message))
3171 dbus_message_unref (message);
3176 case GOT_SERVICE_DELETED:
3178 /* The service started up and got a base address, but then
3179 * failed to register under EXISTENT_SERVICE_NAME
3181 CheckServiceOwnerChangedData socd;
3183 socd.expected_kind = SERVICE_DELETED;
3184 socd.expected_service_name = base_service;
3185 socd.failed = FALSE;
3186 socd.skip_connection = NULL;
3187 bus_test_clients_foreach (check_service_owner_changed_foreach,
3197 case GOT_SOMETHING_ELSE:
3198 _dbus_warn ("Unexpected message after auto activation\n");
3203 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3204 * come the method reply (or error) from the initial method call
3207 /* Note: if this test is run in OOM mode, it will block when the bus
3208 * doesn't send a reply due to OOM.
3210 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3212 message = pop_message_waiting_for_memory (connection);
3213 if (message == NULL)
3215 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3219 if (dbus_message_get_reply_serial (message) != serial)
3221 _dbus_warn ("Wrong reply serial\n");
3225 dbus_message_unref (message);
3228 if (!check_existent_ping (context, connection))
3231 if (!check_existent_get_machine_id (context, connection))
3234 if (!check_existent_hello_from_self (context, connection))
3237 if (!check_send_exit_to_service (context, connection,
3238 EXISTENT_SERVICE_NAME,
3246 dbus_message_unref (message);
3248 if (base_service_message)
3249 dbus_message_unref (base_service_message);
3254 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3256 /* returns TRUE if the correct thing happens,
3257 * but the correct thing may include OOM errors.
3260 check_launch_service_file_missing (BusContext *context,
3261 DBusConnection *connection)
3263 DBusMessage *message;
3264 dbus_uint32_t serial;
3267 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3268 "/org/freedesktop/TestSuite",
3269 "org.freedesktop.TestSuite",
3272 if (message == NULL)
3275 if (!dbus_connection_send (connection, message, &serial))
3277 dbus_message_unref (message);
3281 dbus_message_unref (message);
3284 bus_test_run_everything (context);
3285 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3286 bus_test_run_everything (context);
3288 if (!dbus_connection_get_is_connected (connection))
3290 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3296 message = pop_message_waiting_for_memory (connection);
3297 if (message == NULL)
3299 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3300 "Echo message (auto activation)", serial, connection);
3304 verbose_message_received (connection, message);
3306 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3308 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3310 _dbus_warn ("Message has wrong sender %s\n",
3311 dbus_message_get_sender (message) ?
3312 dbus_message_get_sender (message) : "(none)");
3316 if (dbus_message_is_error (message,
3317 DBUS_ERROR_NO_MEMORY))
3319 ; /* good, this is a valid response */
3321 else if (dbus_message_is_error (message,
3322 DBUS_ERROR_SERVICE_UNKNOWN))
3324 _dbus_verbose("got service unknown\n");
3325 ; /* good, this is expected (only valid when using launch helper) */
3329 warn_unexpected (connection, message, "not this error");
3336 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3344 dbus_message_unref (message);
3349 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3351 /* returns TRUE if the correct thing happens,
3352 * but the correct thing may include OOM errors.
3355 check_launch_service_user_missing (BusContext *context,
3356 DBusConnection *connection)
3358 DBusMessage *message;
3359 dbus_uint32_t serial;
3362 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3363 "/org/freedesktop/TestSuite",
3364 "org.freedesktop.TestSuite",
3367 if (message == NULL)
3370 if (!dbus_connection_send (connection, message, &serial))
3372 dbus_message_unref (message);
3376 dbus_message_unref (message);
3379 bus_test_run_everything (context);
3380 block_connection_until_message_from_bus (context, connection,
3381 "reply to service which should fail to auto-start (missing User)");
3382 bus_test_run_everything (context);
3384 if (!dbus_connection_get_is_connected (connection))
3386 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3392 message = pop_message_waiting_for_memory (connection);
3393 if (message == NULL)
3395 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3396 "Echo message (auto activation)", serial, connection);
3400 verbose_message_received (connection, message);
3402 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3404 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3406 _dbus_warn ("Message has wrong sender %s\n",
3407 dbus_message_get_sender (message) ?
3408 dbus_message_get_sender (message) : "(none)");
3412 if (dbus_message_is_error (message,
3413 DBUS_ERROR_NO_MEMORY))
3415 ; /* good, this is a valid response */
3417 else if (dbus_message_is_error (message,
3418 DBUS_ERROR_SPAWN_FILE_INVALID))
3420 _dbus_verbose("got service file invalid\n");
3421 ; /* good, this is expected (only valid when using launch helper) */
3425 warn_unexpected (connection, message, "not this error");
3432 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3440 dbus_message_unref (message);
3445 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3447 /* returns TRUE if the correct thing happens,
3448 * but the correct thing may include OOM errors.
3451 check_launch_service_exec_missing (BusContext *context,
3452 DBusConnection *connection)
3454 DBusMessage *message;
3455 dbus_uint32_t serial;
3458 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3459 "/org/freedesktop/TestSuite",
3460 "org.freedesktop.TestSuite",
3463 if (message == NULL)
3466 if (!dbus_connection_send (connection, message, &serial))
3468 dbus_message_unref (message);
3472 dbus_message_unref (message);
3475 bus_test_run_everything (context);
3476 block_connection_until_message_from_bus (context, connection,
3477 "reply to service which should fail to auto-start (missing Exec)");
3478 bus_test_run_everything (context);
3480 if (!dbus_connection_get_is_connected (connection))
3482 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3488 message = pop_message_waiting_for_memory (connection);
3489 if (message == NULL)
3491 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3492 "Echo message (auto activation)", serial, connection);
3496 verbose_message_received (connection, message);
3498 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3500 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3502 _dbus_warn ("Message has wrong sender %s\n",
3503 dbus_message_get_sender (message) ?
3504 dbus_message_get_sender (message) : "(none)");
3508 if (dbus_message_is_error (message,
3509 DBUS_ERROR_NO_MEMORY))
3511 ; /* good, this is a valid response */
3513 else if (dbus_message_is_error (message,
3514 DBUS_ERROR_SERVICE_UNKNOWN))
3516 _dbus_verbose("could not activate as invalid service file was not added\n");
3517 ; /* good, this is expected as we shouldn't have been added to
3518 * the activation list with a missing Exec key */
3520 else if (dbus_message_is_error (message,
3521 DBUS_ERROR_SPAWN_FILE_INVALID))
3523 _dbus_verbose("got service file invalid\n");
3524 ; /* good, this is allowed, and is the message passed back from the
3529 warn_unexpected (connection, message, "not this error");
3536 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3544 dbus_message_unref (message);
3549 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3551 /* returns TRUE if the correct thing happens,
3552 * but the correct thing may include OOM errors.
3555 check_launch_service_service_missing (BusContext *context,
3556 DBusConnection *connection)
3558 DBusMessage *message;
3559 dbus_uint32_t serial;
3562 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3563 "/org/freedesktop/TestSuite",
3564 "org.freedesktop.TestSuite",
3567 if (message == NULL)
3570 if (!dbus_connection_send (connection, message, &serial))
3572 dbus_message_unref (message);
3576 dbus_message_unref (message);
3579 bus_test_run_everything (context);
3580 block_connection_until_message_from_bus (context, connection,
3581 "reply to service which should fail to auto-start (missing Service)");
3582 bus_test_run_everything (context);
3584 if (!dbus_connection_get_is_connected (connection))
3586 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3592 message = pop_message_waiting_for_memory (connection);
3593 if (message == NULL)
3595 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3596 "Echo message (auto activation)", serial, connection);
3600 verbose_message_received (connection, message);
3602 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3604 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3606 _dbus_warn ("Message has wrong sender %s\n",
3607 dbus_message_get_sender (message) ?
3608 dbus_message_get_sender (message) : "(none)");
3612 if (dbus_message_is_error (message,
3613 DBUS_ERROR_NO_MEMORY))
3615 ; /* good, this is a valid response */
3617 else if (dbus_message_is_error (message,
3618 DBUS_ERROR_SERVICE_UNKNOWN))
3620 _dbus_verbose("could not activate as invalid service file was not added\n");
3621 ; /* good, this is expected as we shouldn't have been added to
3622 * the activation list with a missing Exec key */
3624 else if (dbus_message_is_error (message,
3625 DBUS_ERROR_SPAWN_FILE_INVALID))
3627 _dbus_verbose("got service file invalid\n");
3628 ; /* good, this is allowed, and is the message passed back from the
3633 warn_unexpected (connection, message, "not this error");
3640 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3648 dbus_message_unref (message);
3653 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3655 /* returns TRUE if the correct thing happens,
3656 * but the correct thing may include OOM errors.
3659 check_shell_fail_service_auto_start (BusContext *context,
3660 DBusConnection *connection)
3662 DBusMessage *message;
3663 dbus_uint32_t serial;
3666 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3667 "/org/freedesktop/TestSuite",
3668 "org.freedesktop.TestSuite",
3671 if (message == NULL)
3674 if (!dbus_connection_send (connection, message, &serial))
3676 dbus_message_unref (message);
3680 dbus_message_unref (message);
3683 bus_test_run_everything (context);
3684 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3685 bus_test_run_everything (context);
3687 if (!dbus_connection_get_is_connected (connection))
3689 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3695 message = pop_message_waiting_for_memory (connection);
3696 if (message == NULL)
3698 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3699 "Echo message (auto activation)", serial, connection);
3703 verbose_message_received (connection, message);
3705 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3707 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3709 _dbus_warn ("Message has wrong sender %s\n",
3710 dbus_message_get_sender (message) ?
3711 dbus_message_get_sender (message) : "(none)");
3715 if (dbus_message_is_error (message,
3716 DBUS_ERROR_NO_MEMORY))
3718 ; /* good, this is a valid response */
3720 else if (dbus_message_is_error (message,
3721 DBUS_ERROR_INVALID_ARGS))
3723 _dbus_verbose("got invalid args\n");
3724 ; /* good, this is expected also */
3728 warn_unexpected (connection, message, "not this error");
3735 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3743 dbus_message_unref (message);
3748 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3750 /* returns TRUE if the correct thing happens,
3751 * but the correct thing may include OOM errors.
3754 check_shell_service_success_auto_start (BusContext *context,
3755 DBusConnection *connection)
3757 DBusMessage *message;
3758 DBusMessage *base_service_message;
3759 dbus_uint32_t serial;
3761 const char *base_service;
3762 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3764 base_service_message = NULL;
3766 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3767 "/org/freedesktop/TestSuite",
3768 "org.freedesktop.TestSuite",
3771 if (message == NULL)
3774 if (!dbus_connection_send (connection, message, &serial))
3776 dbus_message_unref (message);
3780 dbus_message_unref (message);
3783 bus_test_run_everything (context);
3785 /* now wait for the message bus to hear back from the activated
3788 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3789 bus_test_run_everything (context);
3791 if (!dbus_connection_get_is_connected (connection))
3793 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3799 message = pop_message_waiting_for_memory (connection);
3800 if (message == NULL)
3802 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3803 serial, connection);
3807 verbose_message_received (connection, message);
3808 _dbus_verbose (" (after sending %s)\n", "auto start");
3810 /* we should get zero or two ServiceOwnerChanged signals */
3811 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3813 GotServiceInfo message_kind;
3815 if (!check_base_service_activated (context, connection,
3816 message, &base_service))
3819 base_service_message = message;
3822 /* We may need to block here for the test service to exit or finish up */
3823 block_connection_until_message_from_bus (context, connection, "service to exit");
3825 /* Should get a service creation notification for the activated
3826 * service name, or a service deletion on the base service name
3828 message = dbus_connection_borrow_message (connection);
3829 if (message == NULL)
3831 _dbus_warn ("No message after auto activation "
3832 "(should be a service announcement)\n");
3833 dbus_connection_return_message (connection, message);
3838 message_kind = check_got_service_info (message);
3840 dbus_connection_return_message (connection, message);
3843 switch (message_kind)
3845 case GOT_SERVICE_CREATED:
3846 message = pop_message_waiting_for_memory (connection);
3847 if (message == NULL)
3849 _dbus_warn ("Failed to pop message we just put back! "
3850 "should have been a NameOwnerChanged (creation)\n");
3854 /* Check that ServiceOwnerChanged (creation) was correctly received */
3855 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3856 base_service, message))
3859 dbus_message_unref (message);
3864 case GOT_SERVICE_DELETED:
3866 /* The service started up and got a base address, but then
3867 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3869 CheckServiceOwnerChangedData socd;
3871 socd.expected_kind = SERVICE_DELETED;
3872 socd.expected_service_name = base_service;
3873 socd.failed = FALSE;
3874 socd.skip_connection = NULL;
3875 bus_test_clients_foreach (check_service_owner_changed_foreach,
3885 case GOT_SOMETHING_ELSE:
3886 _dbus_warn ("Unexpected message after auto activation\n");
3891 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3892 * come the method reply (or error) from the initial method call
3895 /* Note: if this test is run in OOM mode, it will block when the bus
3896 * doesn't send a reply due to OOM.
3898 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3900 message = pop_message_waiting_for_memory (connection);
3901 if (message == NULL)
3903 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3907 if (dbus_message_get_reply_serial (message) != serial)
3909 _dbus_warn ("Wrong reply serial\n");
3913 if (!dbus_message_get_args (message, NULL,
3914 DBUS_TYPE_STRING, &argv[0],
3915 DBUS_TYPE_STRING, &argv[1],
3916 DBUS_TYPE_STRING, &argv[2],
3917 DBUS_TYPE_STRING, &argv[3],
3918 DBUS_TYPE_STRING, &argv[4],
3919 DBUS_TYPE_STRING, &argv[5],
3920 DBUS_TYPE_STRING, &argv[6],
3923 _dbus_warn ("Error getting arguments from return\n");
3927 /* don't worry about arg[0] as it may be different
3928 depending on the path to the tests
3930 if (strcmp("-test", argv[1]) != 0)
3932 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3937 if (strcmp("that", argv[2]) != 0)
3939 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3944 if (strcmp("we get", argv[3]) != 0)
3946 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3951 if (strcmp("back", argv[4]) != 0)
3953 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3958 if (strcmp("--what", argv[5]) != 0)
3960 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3965 if (strcmp("we put in", argv[6]) != 0)
3967 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3968 "we put in", argv[6]);
3972 dbus_message_unref (message);
3975 if (!check_send_exit_to_service (context, connection,
3976 SHELL_SUCCESS_SERVICE_NAME,
3984 dbus_message_unref (message);
3986 if (base_service_message)
3987 dbus_message_unref (base_service_message);
3995 BusContext *context;
3999 check_oom_check1_func (void *data)
4001 Check1Data *d = data;
4003 if (! (* d->func) (d->context))
4006 if (!check_no_leftovers (d->context))
4008 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4016 check1_try_iterations (BusContext *context,
4017 const char *description,
4023 d.context = context;
4025 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4027 _dbus_assert_not_reached ("test failed");
4031 check_get_services (BusContext *context,
4032 DBusConnection *connection,
4037 DBusMessage *message;
4038 dbus_uint32_t serial;
4045 dbus_error_init (&error);
4048 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4050 DBUS_INTERFACE_DBUS,
4053 if (message == NULL)
4056 if (!dbus_connection_send (connection, message, &serial))
4058 dbus_message_unref (message);
4062 /* send our message */
4063 bus_test_run_clients_loop (SEND_PENDING (connection));
4065 dbus_message_unref (message);
4068 dbus_connection_ref (connection); /* because we may get disconnected */
4069 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4071 if (!dbus_connection_get_is_connected (connection))
4073 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4075 dbus_connection_unref (connection);
4080 dbus_connection_unref (connection);
4082 message = pop_message_waiting_for_memory (connection);
4083 if (message == NULL)
4085 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4086 method, serial, connection);
4090 verbose_message_received (connection, message);
4092 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4094 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4096 ; /* good, this is a valid response */
4100 warn_unexpected (connection, message, "not this error");
4107 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4109 ; /* good, expected */
4113 warn_unexpected (connection, message,
4114 "method_return for ListActivatableNames/ListNames");
4121 if (!dbus_message_get_args (message, &error,
4127 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4129 _dbus_verbose ("no memory to list services by %s\n", method);
4130 dbus_error_free (&error);
4131 _dbus_wait_for_memory ();
4132 goto retry_get_property;
4136 _dbus_assert (dbus_error_is_set (&error));
4137 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4146 if (!check_no_leftovers (context))
4152 dbus_error_free (&error);
4155 dbus_message_unref (message);
4160 /* returns TRUE if the correct thing happens,
4161 * but the correct thing may include OOM errors.
4164 check_list_services (BusContext *context,
4165 DBusConnection *connection)
4167 DBusMessage *message;
4168 DBusMessage *base_service_message;
4169 const char *base_service;
4170 dbus_uint32_t serial;
4172 const char *existent = EXISTENT_SERVICE_NAME;
4173 dbus_uint32_t flags;
4177 _dbus_verbose ("check_list_services for %p\n", connection);
4179 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4184 if (!_dbus_string_array_contains ((const char **)services, existent))
4186 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4190 dbus_free_string_array (services);
4192 base_service_message = NULL;
4194 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4196 DBUS_INTERFACE_DBUS,
4197 "StartServiceByName");
4199 if (message == NULL)
4202 dbus_message_set_auto_start (message, FALSE);
4205 if (!dbus_message_append_args (message,
4206 DBUS_TYPE_STRING, &existent,
4207 DBUS_TYPE_UINT32, &flags,
4210 dbus_message_unref (message);
4214 if (!dbus_connection_send (connection, message, &serial))
4216 dbus_message_unref (message);
4220 dbus_message_unref (message);
4223 bus_test_run_everything (context);
4225 /* now wait for the message bus to hear back from the activated
4228 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4230 bus_test_run_everything (context);
4232 if (!dbus_connection_get_is_connected (connection))
4234 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4240 message = pop_message_waiting_for_memory (connection);
4241 if (message == NULL)
4243 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4244 "StartServiceByName", serial, connection);
4248 verbose_message_received (connection, message);
4249 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4251 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4253 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4255 _dbus_warn ("Message has wrong sender %s\n",
4256 dbus_message_get_sender (message) ?
4257 dbus_message_get_sender (message) : "(none)");
4261 if (dbus_message_is_error (message,
4262 DBUS_ERROR_NO_MEMORY))
4264 ; /* good, this is a valid response */
4266 else if (dbus_message_is_error (message,
4267 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4268 dbus_message_is_error (message,
4269 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4270 dbus_message_is_error (message,
4271 DBUS_ERROR_SPAWN_EXEC_FAILED))
4273 ; /* good, this is expected also */
4277 _dbus_warn ("Did not expect error %s\n",
4278 dbus_message_get_error_name (message));
4284 GotServiceInfo message_kind;
4286 if (!check_base_service_activated (context, connection,
4287 message, &base_service))
4290 base_service_message = message;
4293 /* We may need to block here for the test service to exit or finish up */
4294 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4296 message = dbus_connection_borrow_message (connection);
4297 if (message == NULL)
4299 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4303 message_kind = check_got_service_info (message);
4305 dbus_connection_return_message (connection, message);
4308 switch (message_kind)
4310 case GOT_SOMETHING_ELSE:
4312 case GOT_SERVICE_DELETED:
4313 _dbus_warn ("Unexpected message after ActivateService "
4314 "(should be an error or a service announcement)\n");
4317 case GOT_SERVICE_CREATED:
4318 message = pop_message_waiting_for_memory (connection);
4319 if (message == NULL)
4321 _dbus_warn ("Failed to pop message we just put back! "
4322 "should have been a NameOwnerChanged (creation)\n");
4326 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4327 base_service, message))
4330 dbus_message_unref (message);
4333 if (!check_no_leftovers (context))
4335 _dbus_warn ("Messages were left over after successful activation\n");
4343 if (!check_get_services (context, connection, "ListNames", &services, &len))
4348 if (!_dbus_string_array_contains ((const char **)services, existent))
4350 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4354 dbus_free_string_array (services);
4356 if (!check_send_exit_to_service (context, connection,
4357 EXISTENT_SERVICE_NAME, base_service))
4364 dbus_message_unref (message);
4366 if (base_service_message)
4367 dbus_message_unref (base_service_message);
4375 BusContext *context;
4376 DBusConnection *connection;
4380 check_oom_check2_func (void *data)
4382 Check2Data *d = data;
4384 if (! (* d->func) (d->context, d->connection))
4387 if (!check_no_leftovers (d->context))
4389 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4397 check2_try_iterations (BusContext *context,
4398 DBusConnection *connection,
4399 const char *description,
4405 d.context = context;
4406 d.connection = connection;
4408 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4411 _dbus_warn ("%s failed during oom\n", description);
4412 _dbus_assert_not_reached ("test failed");
4417 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4418 const char *filename)
4423 if (!_dbus_string_init (&full))
4426 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4428 _dbus_string_free (&full);
4432 _dbus_string_init_const (&file, filename);
4434 if (!_dbus_concat_dir_and_file (&full, &file))
4436 _dbus_string_free (&full);
4440 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4441 _dbus_string_get_const_data (&full));
4443 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4445 _dbus_string_free (&full);
4451 bus_dispatch_test_conf (const DBusString *test_data_dir,
4452 const char *filename,
4453 dbus_bool_t use_launcher)
4455 BusContext *context;
4456 DBusConnection *foo;
4457 DBusConnection *bar;
4458 DBusConnection *baz;
4461 /* save the config name for the activation helper */
4462 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4463 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4465 dbus_error_init (&error);
4467 context = bus_context_new_test (test_data_dir, filename);
4468 if (context == NULL)
4471 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4473 _dbus_assert_not_reached ("could not alloc connection");
4475 if (!bus_setup_debug_client (foo))
4476 _dbus_assert_not_reached ("could not set up connection");
4478 spin_connection_until_authenticated (context, foo);
4480 if (!check_hello_message (context, foo))
4481 _dbus_assert_not_reached ("hello message failed");
4483 if (!check_double_hello_message (context, foo))
4484 _dbus_assert_not_reached ("double hello message failed");
4486 if (!check_add_match_all (context, foo))
4487 _dbus_assert_not_reached ("AddMatch message failed");
4489 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4491 _dbus_assert_not_reached ("could not alloc connection");
4493 if (!bus_setup_debug_client (bar))
4494 _dbus_assert_not_reached ("could not set up connection");
4496 spin_connection_until_authenticated (context, bar);
4498 if (!check_hello_message (context, bar))
4499 _dbus_assert_not_reached ("hello message failed");
4501 if (!check_add_match_all (context, bar))
4502 _dbus_assert_not_reached ("AddMatch message failed");
4504 baz = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4506 _dbus_assert_not_reached ("could not alloc connection");
4508 if (!bus_setup_debug_client (baz))
4509 _dbus_assert_not_reached ("could not set up connection");
4511 spin_connection_until_authenticated (context, baz);
4513 if (!check_hello_message (context, baz))
4514 _dbus_assert_not_reached ("hello message failed");
4516 if (!check_add_match_all (context, baz))
4517 _dbus_assert_not_reached ("AddMatch message failed");
4519 if (!check_get_connection_unix_user (context, baz))
4520 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4522 if (!check_get_connection_unix_process_id (context, baz))
4523 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4525 if (!check_list_services (context, baz))
4526 _dbus_assert_not_reached ("ListActivatableNames message failed");
4528 if (!check_no_leftovers (context))
4530 _dbus_warn ("Messages were left over after setting up initial connections\n");
4531 _dbus_assert_not_reached ("initial connection setup failed");
4534 check1_try_iterations (context, "create_and_hello",
4535 check_hello_connection);
4537 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4538 check_nonexistent_service_no_auto_start);
4540 #ifdef DBUS_WIN_FIXME
4541 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4543 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4544 check_segfault_service_no_auto_start);
4547 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4548 check_existent_service_no_auto_start);
4550 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4551 check_nonexistent_service_auto_start);
4554 #ifdef DBUS_WIN_FIXME
4555 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4557 /* only do the segfault test if we are not using the launcher */
4558 check2_try_iterations (context, foo, "segfault_service_auto_start",
4559 check_segfault_service_auto_start);
4562 /* only do the shell fail test if we are not using the launcher */
4563 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4564 check_shell_fail_service_auto_start);
4566 /* specific to launcher */
4568 if (!check_launch_service_file_missing (context, foo))
4569 _dbus_assert_not_reached ("did not get service file not found error");
4572 /* Note: need to resolve some issues with the testing code in order to run
4573 * this in oom (handle that we sometimes don't get replies back from the bus
4574 * when oom happens, without blocking the test).
4576 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4577 check_existent_service_auto_start);
4580 if (!check_existent_service_auto_start (context, foo))
4581 _dbus_assert_not_reached ("existent service auto start failed");
4583 if (!check_shell_service_success_auto_start (context, foo))
4584 _dbus_assert_not_reached ("shell success service auto start failed");
4586 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4588 kill_client_connection_unchecked (foo);
4589 kill_client_connection_unchecked (bar);
4590 kill_client_connection_unchecked (baz);
4592 bus_context_unref (context);
4598 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4599 const char *filename)
4601 BusContext *context;
4602 DBusConnection *foo;
4605 /* save the config name for the activation helper */
4606 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4607 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4609 dbus_error_init (&error);
4611 context = bus_context_new_test (test_data_dir, filename);
4612 if (context == NULL)
4615 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4617 _dbus_assert_not_reached ("could not alloc connection");
4619 if (!bus_setup_debug_client (foo))
4620 _dbus_assert_not_reached ("could not set up connection");
4622 spin_connection_until_authenticated (context, foo);
4624 if (!check_hello_message (context, foo))
4625 _dbus_assert_not_reached ("hello message failed");
4627 if (!check_double_hello_message (context, foo))
4628 _dbus_assert_not_reached ("double hello message failed");
4630 if (!check_add_match_all (context, foo))
4631 _dbus_assert_not_reached ("AddMatch message failed");
4633 /* this only tests the activation.c user check */
4634 if (!check_launch_service_user_missing (context, foo))
4635 _dbus_assert_not_reached ("user missing did not trigger error");
4637 /* this only tests the desktop.c exec check */
4638 if (!check_launch_service_exec_missing (context, foo))
4639 _dbus_assert_not_reached ("exec missing did not trigger error");
4641 /* this only tests the desktop.c service check */
4642 if (!check_launch_service_service_missing (context, foo))
4643 _dbus_assert_not_reached ("service missing did not trigger error");
4645 _dbus_verbose ("Disconnecting foo\n");
4647 kill_client_connection_unchecked (foo);
4649 bus_context_unref (context);
4655 bus_dispatch_test (const DBusString *test_data_dir)
4657 /* run normal activation tests */
4658 _dbus_verbose ("Normal activation tests\n");
4659 if (!bus_dispatch_test_conf (test_data_dir,
4660 "valid-config-files/debug-allow-all.conf", FALSE))
4663 /* run launch-helper activation tests */
4664 _dbus_verbose ("Launch helper activation tests\n");
4665 if (!bus_dispatch_test_conf (test_data_dir,
4666 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4669 /* run select launch-helper activation tests on broken service files */
4670 if (!bus_dispatch_test_conf_fail (test_data_dir,
4671 "valid-config-files-system/debug-allow-all-fail.conf"))
4678 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4680 BusContext *context;
4681 DBusConnection *foo;
4684 dbus_error_init (&error);
4686 /* Test SHA1 authentication */
4687 _dbus_verbose ("Testing SHA1 context\n");
4689 context = bus_context_new_test (test_data_dir,
4690 "valid-config-files/debug-allow-all-sha1.conf");
4691 if (context == NULL)
4694 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4696 _dbus_assert_not_reached ("could not alloc connection");
4698 if (!bus_setup_debug_client (foo))
4699 _dbus_assert_not_reached ("could not set up connection");
4701 spin_connection_until_authenticated (context, foo);
4703 if (!check_hello_message (context, foo))
4704 _dbus_assert_not_reached ("hello message failed");
4706 if (!check_add_match_all (context, foo))
4707 _dbus_assert_not_reached ("addmatch message failed");
4709 if (!check_no_leftovers (context))
4711 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4712 _dbus_assert_not_reached ("initial connection setup failed");
4715 check1_try_iterations (context, "create_and_hello_sha1",
4716 check_hello_connection);
4718 kill_client_connection_unchecked (foo);
4720 bus_context_unref (context);
4725 #endif /* DBUS_BUILD_TESTS */