1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 connections = bus_transaction_get_connections (transaction);
91 dbus_error_init (&tmp_error);
92 context = bus_transaction_get_context (transaction);
93 matchmaker = bus_context_get_matchmaker (context);
96 if (!bus_matchmaker_get_recipients (matchmaker, connections,
97 sender, addressed_recipient, message,
104 link = _dbus_list_get_first_link (&recipients);
107 DBusConnection *dest;
111 if (!send_one_message (dest, context, sender, addressed_recipient,
112 message, transaction, &tmp_error))
115 link = _dbus_list_get_next_link (&recipients, link);
118 _dbus_list_clear (&recipients);
120 if (dbus_error_is_set (&tmp_error))
122 dbus_move_error (&tmp_error, error);
129 static DBusHandlerResult
130 bus_dispatch (DBusConnection *connection,
131 DBusMessage *message)
133 const char *sender, *service_name;
135 BusTransaction *transaction;
137 DBusHandlerResult result;
138 DBusConnection *addressed_recipient;
140 result = DBUS_HANDLER_RESULT_HANDLED;
143 addressed_recipient = NULL;
144 dbus_error_init (&error);
146 context = bus_connection_get_context (connection);
147 _dbus_assert (context != NULL);
149 /* If we can't even allocate an OOM error, we just go to sleep
152 while (!bus_connection_preallocate_oom_error (connection))
153 _dbus_wait_for_memory ();
155 /* Ref connection in case we disconnect it at some point in here */
156 dbus_connection_ref (connection);
158 service_name = dbus_message_get_destination (message);
160 #ifdef DBUS_ENABLE_VERBOSE_MODE
162 const char *interface_name, *member_name, *error_name;
164 interface_name = dbus_message_get_interface (message);
165 member_name = dbus_message_get_member (message);
166 error_name = dbus_message_get_error_name (message);
168 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
169 interface_name ? interface_name : "(no interface)",
170 member_name ? member_name : "(no member)",
171 error_name ? error_name : "(no error name)",
172 service_name ? service_name : "peer");
174 #endif /* DBUS_ENABLE_VERBOSE_MODE */
176 /* If service_name is NULL, if it's a signal we send it to all
177 * connections with a match rule. If it's not a signal, there
178 * are some special cases here but mostly we just bail out.
180 if (service_name == NULL)
182 if (dbus_message_is_signal (message,
183 DBUS_INTERFACE_LOCAL,
186 bus_connection_disconnected (connection);
190 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
192 /* DBusConnection also handles some of these automatically, we leave
195 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
200 /* Create our transaction */
201 transaction = bus_transaction_new (context);
202 if (transaction == NULL)
204 BUS_SET_OOM (&error);
208 /* Assign a sender to the message */
209 if (bus_connection_is_active (connection))
211 sender = bus_connection_get_name (connection);
212 _dbus_assert (sender != NULL);
214 if (!dbus_message_set_sender (message, sender))
216 BUS_SET_OOM (&error);
220 /* We need to refetch the service name here, because
221 * dbus_message_set_sender can cause the header to be
222 * reallocated, and thus the service_name pointer will become
225 service_name = dbus_message_get_destination (message);
229 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
231 if (!bus_context_check_security_policy (context, transaction,
232 connection, NULL, NULL, message, &error))
234 _dbus_verbose ("Security policy rejected message\n");
238 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
239 if (!bus_driver_handle_message (connection, transaction, message, &error))
242 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
244 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
245 dbus_connection_close (connection);
248 else if (service_name != NULL) /* route to named service */
250 DBusString service_string;
252 BusRegistry *registry;
254 _dbus_assert (service_name != NULL);
256 registry = bus_connection_get_registry (connection);
258 _dbus_string_init_const (&service_string, service_name);
259 service = bus_registry_lookup (registry, &service_string);
261 if (service == NULL && dbus_message_get_auto_start (message))
263 BusActivation *activation;
264 /* We can't do the security policy check here, since the addressed
265 * recipient service doesn't exist yet. We do it before sending the
266 * message after the service has been created.
268 activation = bus_connection_get_activation (connection);
270 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
271 message, service_name, &error))
273 _DBUS_ASSERT_ERROR_IS_SET (&error);
274 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
280 else if (service == NULL)
282 dbus_set_error (&error,
283 DBUS_ERROR_NAME_HAS_NO_OWNER,
284 "Name \"%s\" does not exist",
290 addressed_recipient = bus_service_get_primary_owners_connection (service);
291 _dbus_assert (addressed_recipient != NULL);
293 if (!bus_context_check_security_policy (context, transaction,
294 connection, addressed_recipient,
299 /* Dispatch the message */
300 if (!bus_transaction_send (transaction, addressed_recipient, message))
302 BUS_SET_OOM (&error);
308 /* Now match the messages against any match rules, which will send
309 * out signals and such. addressed_recipient may == NULL.
311 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
315 if (dbus_error_is_set (&error))
317 if (!dbus_connection_get_is_connected (connection))
319 /* If we disconnected it, we won't bother to send it any error
322 _dbus_verbose ("Not sending error to connection we disconnected\n");
324 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
326 bus_connection_send_oom_error (connection, message);
328 /* cancel transaction due to OOM */
329 if (transaction != NULL)
331 bus_transaction_cancel_and_free (transaction);
337 /* Try to send the real error, if no mem to do that, send
340 _dbus_assert (transaction != NULL);
341 if (!bus_transaction_send_error_reply (transaction, connection,
344 bus_connection_send_oom_error (connection, message);
346 /* cancel transaction due to OOM */
347 if (transaction != NULL)
349 bus_transaction_cancel_and_free (transaction);
356 dbus_error_free (&error);
359 if (transaction != NULL)
361 bus_transaction_execute_and_free (transaction);
364 dbus_connection_unref (connection);
369 static DBusHandlerResult
370 bus_dispatch_message_filter (DBusConnection *connection,
371 DBusMessage *message,
374 return bus_dispatch (connection, message);
378 bus_dispatch_add_connection (DBusConnection *connection)
380 if (!dbus_connection_add_filter (connection,
381 bus_dispatch_message_filter,
389 bus_dispatch_remove_connection (DBusConnection *connection)
391 /* Here we tell the bus driver that we want to get off. */
392 bus_driver_remove_connection (connection);
394 dbus_connection_remove_filter (connection,
395 bus_dispatch_message_filter,
399 #ifdef DBUS_BUILD_TESTS
403 /* This is used to know whether we need to block in order to finish
404 * sending a message, or whether the initial dbus_connection_send()
405 * already flushed the queue.
407 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
409 typedef dbus_bool_t (* Check1Func) (BusContext *context);
410 typedef dbus_bool_t (* Check2Func) (BusContext *context,
411 DBusConnection *connection);
413 static dbus_bool_t check_no_leftovers (BusContext *context);
416 block_connection_until_message_from_bus (BusContext *context,
417 DBusConnection *connection,
418 const char *what_is_expected)
420 _dbus_verbose ("expecting: %s\n", what_is_expected);
422 while (dbus_connection_get_dispatch_status (connection) ==
423 DBUS_DISPATCH_COMPLETE &&
424 dbus_connection_get_is_connected (connection))
426 bus_test_run_bus_loop (context, TRUE);
427 bus_test_run_clients_loop (FALSE);
432 spin_connection_until_authenticated (BusContext *context,
433 DBusConnection *connection)
435 _dbus_verbose ("Spinning to auth connection %p\n", connection);
436 while (!dbus_connection_get_is_authenticated (connection) &&
437 dbus_connection_get_is_connected (connection))
439 bus_test_run_bus_loop (context, FALSE);
440 bus_test_run_clients_loop (FALSE);
442 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
445 /* compensate for fact that pop_message() can return #NULL due to OOM */
447 pop_message_waiting_for_memory (DBusConnection *connection)
449 while (dbus_connection_get_dispatch_status (connection) ==
450 DBUS_DISPATCH_NEED_MEMORY)
451 _dbus_wait_for_memory ();
453 return dbus_connection_pop_message (connection);
457 borrow_message_waiting_for_memory (DBusConnection *connection)
459 while (dbus_connection_get_dispatch_status (connection) ==
460 DBUS_DISPATCH_NEED_MEMORY)
461 _dbus_wait_for_memory ();
463 return dbus_connection_borrow_message (connection);
467 warn_unexpected_real (DBusConnection *connection,
468 DBusMessage *message,
469 const char *expected,
470 const char *function,
474 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
476 dbus_message_get_interface (message) ?
477 dbus_message_get_interface (message) : "(unset)",
478 dbus_message_get_member (message) ?
479 dbus_message_get_member (message) : "(unset)",
480 dbus_message_get_error_name (message) ?
481 dbus_message_get_error_name (message) : "(unset)",
485 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
486 function, line, connection, expected);
489 #define warn_unexpected(connection, message, expected) \
490 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
493 verbose_message_received (DBusConnection *connection,
494 DBusMessage *message)
496 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
497 dbus_message_get_interface (message) ?
498 dbus_message_get_interface (message) : "(unset)",
499 dbus_message_get_member (message) ?
500 dbus_message_get_member (message) : "(unset)",
501 dbus_message_get_error_name (message) ?
502 dbus_message_get_error_name (message) : "(unset)",
515 ServiceInfoKind expected_kind;
516 const char *expected_service_name;
518 DBusConnection *skip_connection;
519 } CheckServiceOwnerChangedData;
522 check_service_owner_changed_foreach (DBusConnection *connection,
525 CheckServiceOwnerChangedData *d = data;
526 DBusMessage *message;
528 const char *service_name, *old_owner, *new_owner;
530 if (d->expected_kind == SERVICE_CREATED
531 && connection == d->skip_connection)
534 dbus_error_init (&error);
537 message = pop_message_waiting_for_memory (connection);
540 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
541 connection, "NameOwnerChanged");
544 else if (!dbus_message_is_signal (message,
548 warn_unexpected (connection, message, "NameOwnerChanged");
554 reget_service_info_data:
559 dbus_message_get_args (message, &error,
560 DBUS_TYPE_STRING, &service_name,
561 DBUS_TYPE_STRING, &old_owner,
562 DBUS_TYPE_STRING, &new_owner,
565 if (dbus_error_is_set (&error))
567 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
569 dbus_error_free (&error);
570 _dbus_wait_for_memory ();
571 goto reget_service_info_data;
575 _dbus_warn ("Did not get the expected arguments\n");
580 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
581 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
582 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
584 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
588 if (strcmp (service_name, d->expected_service_name) != 0)
590 _dbus_warn ("expected info on service %s, got info on %s\n",
591 d->expected_service_name,
596 if (*service_name == ':' && new_owner[0]
597 && strcmp (service_name, new_owner) != 0)
599 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
600 service_name, old_owner, new_owner);
608 dbus_error_free (&error);
611 dbus_message_unref (message);
618 kill_client_connection (BusContext *context,
619 DBusConnection *connection)
623 CheckServiceOwnerChangedData socd;
625 _dbus_verbose ("killing connection %p\n", connection);
627 s = dbus_bus_get_unique_name (connection);
628 _dbus_assert (s != NULL);
630 while ((base_service = _dbus_strdup (s)) == NULL)
631 _dbus_wait_for_memory ();
633 dbus_connection_ref (connection);
635 /* kick in the disconnect handler that unrefs the connection */
636 dbus_connection_close (connection);
638 bus_test_run_everything (context);
640 _dbus_assert (bus_test_client_listed (connection));
642 /* Run disconnect handler in test.c */
643 if (bus_connection_dispatch_one_message (connection))
644 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
646 _dbus_assert (!dbus_connection_get_is_connected (connection));
647 dbus_connection_unref (connection);
649 _dbus_assert (!bus_test_client_listed (connection));
651 socd.expected_kind = SERVICE_DELETED;
652 socd.expected_service_name = base_service;
654 socd.skip_connection = NULL;
656 bus_test_clients_foreach (check_service_owner_changed_foreach,
659 dbus_free (base_service);
662 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
664 if (!check_no_leftovers (context))
665 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
669 kill_client_connection_unchecked (DBusConnection *connection)
671 /* This kills the connection without expecting it to affect
672 * the rest of the bus.
674 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
676 dbus_connection_ref (connection);
677 dbus_connection_close (connection);
678 /* dispatching disconnect handler will unref once */
679 if (bus_connection_dispatch_one_message (connection))
680 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
682 _dbus_assert (!bus_test_client_listed (connection));
683 dbus_connection_unref (connection);
689 } CheckNoMessagesData;
692 check_no_messages_foreach (DBusConnection *connection,
695 CheckNoMessagesData *d = data;
696 DBusMessage *message;
698 message = pop_message_waiting_for_memory (connection);
701 warn_unexpected (connection, message, "no messages");
707 dbus_message_unref (message);
712 check_no_leftovers (BusContext *context)
714 CheckNoMessagesData nmd;
717 bus_test_clients_foreach (check_no_messages_foreach,
722 _dbus_verbose ("%s: leftover message found\n",
723 _DBUS_FUNCTION_NAME);
730 /* returns TRUE if the correct thing happens,
731 * but the correct thing may include OOM errors.
734 check_hello_message (BusContext *context,
735 DBusConnection *connection)
737 DBusMessage *message;
738 DBusMessage *name_message;
739 dbus_uint32_t serial;
743 const char *acquired;
746 dbus_error_init (&error);
752 _dbus_verbose ("check_hello_message for %p\n", connection);
754 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
762 dbus_connection_ref (connection); /* because we may get disconnected */
764 if (!dbus_connection_send (connection, message, &serial))
766 dbus_message_unref (message);
767 dbus_connection_unref (connection);
771 _dbus_assert (dbus_message_has_signature (message, ""));
773 dbus_message_unref (message);
776 if (!dbus_connection_get_is_connected (connection))
778 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
780 dbus_connection_unref (connection);
785 /* send our message */
786 bus_test_run_clients_loop (SEND_PENDING (connection));
788 if (!dbus_connection_get_is_connected (connection))
790 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
792 dbus_connection_unref (connection);
797 block_connection_until_message_from_bus (context, connection, "reply to Hello");
799 if (!dbus_connection_get_is_connected (connection))
801 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
803 dbus_connection_unref (connection);
808 dbus_connection_unref (connection);
810 message = pop_message_waiting_for_memory (connection);
813 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
814 "Hello", serial, connection);
818 verbose_message_received (connection, message);
820 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
822 _dbus_warn ("Message has wrong sender %s\n",
823 dbus_message_get_sender (message) ?
824 dbus_message_get_sender (message) : "(none)");
828 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
830 if (dbus_message_is_error (message,
831 DBUS_ERROR_NO_MEMORY))
833 ; /* good, this is a valid response */
837 warn_unexpected (connection, message, "not this error");
844 CheckServiceOwnerChangedData socd;
846 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
848 ; /* good, expected */
852 warn_unexpected (connection, message, "method return for Hello");
857 retry_get_hello_name:
858 if (!dbus_message_get_args (message, &error,
859 DBUS_TYPE_STRING, &name,
862 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
864 _dbus_verbose ("no memory to get service name arg from hello\n");
865 dbus_error_free (&error);
866 _dbus_wait_for_memory ();
867 goto retry_get_hello_name;
871 _dbus_assert (dbus_error_is_set (&error));
872 _dbus_warn ("Did not get the expected single string argument to hello\n");
877 _dbus_verbose ("Got hello name: %s\n", name);
879 while (!dbus_bus_set_unique_name (connection, name))
880 _dbus_wait_for_memory ();
882 socd.expected_kind = SERVICE_CREATED;
883 socd.expected_service_name = name;
885 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
886 bus_test_clients_foreach (check_service_owner_changed_foreach,
892 name_message = message;
893 /* Client should also have gotten ServiceAcquired */
895 message = pop_message_waiting_for_memory (connection);
898 _dbus_warn ("Expecting %s, got nothing\n",
902 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
905 _dbus_warn ("Expecting %s, got smthg else\n",
910 retry_get_acquired_name:
911 if (!dbus_message_get_args (message, &error,
912 DBUS_TYPE_STRING, &acquired,
915 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
917 _dbus_verbose ("no memory to get service name arg from acquired\n");
918 dbus_error_free (&error);
919 _dbus_wait_for_memory ();
920 goto retry_get_acquired_name;
924 _dbus_assert (dbus_error_is_set (&error));
925 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
930 _dbus_verbose ("Got acquired name: %s\n", acquired);
932 if (strcmp (acquired, name) != 0)
934 _dbus_warn ("Acquired name is %s but expected %s\n",
941 if (!check_no_leftovers (context))
947 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
949 dbus_error_free (&error);
952 dbus_message_unref (message);
955 dbus_message_unref (name_message);
960 /* returns TRUE if the correct thing happens,
961 * but the correct thing may include OOM errors.
964 check_double_hello_message (BusContext *context,
965 DBusConnection *connection)
967 DBusMessage *message;
968 dbus_uint32_t serial;
973 dbus_error_init (&error);
976 _dbus_verbose ("check_double_hello_message for %p\n", connection);
978 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
986 if (!dbus_connection_send (connection, message, &serial))
988 dbus_message_unref (message);
992 dbus_message_unref (message);
995 /* send our message */
996 bus_test_run_clients_loop (SEND_PENDING (connection));
998 dbus_connection_ref (connection); /* because we may get disconnected */
999 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1001 if (!dbus_connection_get_is_connected (connection))
1003 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1005 dbus_connection_unref (connection);
1010 dbus_connection_unref (connection);
1012 message = pop_message_waiting_for_memory (connection);
1013 if (message == NULL)
1015 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1016 "Hello", serial, connection);
1020 verbose_message_received (connection, message);
1022 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1024 _dbus_warn ("Message has wrong sender %s\n",
1025 dbus_message_get_sender (message) ?
1026 dbus_message_get_sender (message) : "(none)");
1030 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1032 warn_unexpected (connection, message, "method return for Hello");
1036 if (!check_no_leftovers (context))
1042 dbus_error_free (&error);
1045 dbus_message_unref (message);
1050 /* returns TRUE if the correct thing happens,
1051 * but the correct thing may include OOM errors.
1054 check_get_connection_unix_user (BusContext *context,
1055 DBusConnection *connection)
1057 DBusMessage *message;
1058 dbus_uint32_t serial;
1061 const char *base_service_name;
1065 dbus_error_init (&error);
1068 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1070 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1072 DBUS_INTERFACE_DBUS,
1073 "GetConnectionUnixUser");
1075 if (message == NULL)
1078 base_service_name = dbus_bus_get_unique_name (connection);
1080 if (!dbus_message_append_args (message,
1081 DBUS_TYPE_STRING, &base_service_name,
1084 dbus_message_unref (message);
1088 if (!dbus_connection_send (connection, message, &serial))
1090 dbus_message_unref (message);
1094 /* send our message */
1095 bus_test_run_clients_loop (SEND_PENDING (connection));
1097 dbus_message_unref (message);
1100 dbus_connection_ref (connection); /* because we may get disconnected */
1101 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1103 if (!dbus_connection_get_is_connected (connection))
1105 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1107 dbus_connection_unref (connection);
1112 dbus_connection_unref (connection);
1114 message = pop_message_waiting_for_memory (connection);
1115 if (message == NULL)
1117 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1118 "GetConnectionUnixUser", serial, connection);
1122 verbose_message_received (connection, message);
1124 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1126 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1128 ; /* good, this is a valid response */
1132 warn_unexpected (connection, message, "not this error");
1139 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1141 ; /* good, expected */
1145 warn_unexpected (connection, message,
1146 "method_return for GetConnectionUnixUser");
1153 if (!dbus_message_get_args (message, &error,
1154 DBUS_TYPE_UINT32, &uid,
1157 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1159 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1160 dbus_error_free (&error);
1161 _dbus_wait_for_memory ();
1162 goto retry_get_property;
1166 _dbus_assert (dbus_error_is_set (&error));
1167 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1173 if (!check_no_leftovers (context))
1179 dbus_error_free (&error);
1182 dbus_message_unref (message);
1187 /* returns TRUE if the correct thing happens,
1188 * but the correct thing may include OOM errors.
1191 check_get_connection_unix_process_id (BusContext *context,
1192 DBusConnection *connection)
1194 DBusMessage *message;
1195 dbus_uint32_t serial;
1198 const char *base_service_name;
1202 dbus_error_init (&error);
1205 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1207 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1209 DBUS_INTERFACE_DBUS,
1210 "GetConnectionUnixProcessID");
1212 if (message == NULL)
1215 base_service_name = dbus_bus_get_unique_name (connection);
1217 if (!dbus_message_append_args (message,
1218 DBUS_TYPE_STRING, &base_service_name,
1221 dbus_message_unref (message);
1225 if (!dbus_connection_send (connection, message, &serial))
1227 dbus_message_unref (message);
1231 /* send our message */
1232 bus_test_run_clients_loop (SEND_PENDING (connection));
1234 dbus_message_unref (message);
1237 dbus_connection_ref (connection); /* because we may get disconnected */
1238 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1240 if (!dbus_connection_get_is_connected (connection))
1242 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1244 dbus_connection_unref (connection);
1249 dbus_connection_unref (connection);
1251 message = pop_message_waiting_for_memory (connection);
1252 if (message == NULL)
1254 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1255 "GetConnectionUnixProcessID", serial, connection);
1259 verbose_message_received (connection, message);
1261 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1263 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1265 ; /* good, this is a valid response */
1268 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1270 /* We are expecting this error, since we know in the test suite we aren't
1271 * talking to a client running on UNIX
1273 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1278 warn_unexpected (connection, message, "not this error");
1286 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1289 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1291 ; /* good, expected */
1295 warn_unexpected (connection, message,
1296 "method_return for GetConnectionUnixProcessID");
1303 if (!dbus_message_get_args (message, &error,
1304 DBUS_TYPE_UINT32, &pid,
1307 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1309 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1310 dbus_error_free (&error);
1311 _dbus_wait_for_memory ();
1312 goto retry_get_property;
1316 _dbus_assert (dbus_error_is_set (&error));
1317 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1323 /* test if returned pid is the same as our own pid
1325 * @todo It would probably be good to restructure the tests
1326 * in a way so our parent is the bus that we're testing
1327 * cause then we can test that the pid returned matches
1330 if (pid != (dbus_uint32_t) _dbus_getpid ())
1332 _dbus_assert (dbus_error_is_set (&error));
1333 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1337 #endif /* !DBUS_WIN */
1340 if (!check_no_leftovers (context))
1346 dbus_error_free (&error);
1349 dbus_message_unref (message);
1354 /* returns TRUE if the correct thing happens,
1355 * but the correct thing may include OOM errors.
1358 check_add_match_all (BusContext *context,
1359 DBusConnection *connection)
1361 DBusMessage *message;
1363 dbus_uint32_t serial;
1365 const char *empty = "";
1368 dbus_error_init (&error);
1371 _dbus_verbose ("check_add_match_all for %p\n", connection);
1373 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1375 DBUS_INTERFACE_DBUS,
1378 if (message == NULL)
1381 /* empty string match rule matches everything */
1382 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1385 dbus_message_unref (message);
1389 if (!dbus_connection_send (connection, message, &serial))
1391 dbus_message_unref (message);
1395 dbus_message_unref (message);
1398 dbus_connection_ref (connection); /* because we may get disconnected */
1400 /* send our message */
1401 bus_test_run_clients_loop (SEND_PENDING (connection));
1403 if (!dbus_connection_get_is_connected (connection))
1405 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1407 dbus_connection_unref (connection);
1412 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1414 if (!dbus_connection_get_is_connected (connection))
1416 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1418 dbus_connection_unref (connection);
1423 dbus_connection_unref (connection);
1425 message = pop_message_waiting_for_memory (connection);
1426 if (message == NULL)
1428 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1429 "AddMatch", serial, connection);
1433 verbose_message_received (connection, message);
1435 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1437 _dbus_warn ("Message has wrong sender %s\n",
1438 dbus_message_get_sender (message) ?
1439 dbus_message_get_sender (message) : "(none)");
1443 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1445 if (dbus_message_is_error (message,
1446 DBUS_ERROR_NO_MEMORY))
1448 ; /* good, this is a valid response */
1452 warn_unexpected (connection, message, "not this error");
1459 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1461 ; /* good, expected */
1462 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1466 warn_unexpected (connection, message, "method return for AddMatch");
1472 if (!check_no_leftovers (context))
1478 dbus_error_free (&error);
1481 dbus_message_unref (message);
1486 /* returns TRUE if the correct thing happens,
1487 * but the correct thing may include OOM errors.
1490 check_hello_connection (BusContext *context)
1492 DBusConnection *connection;
1495 dbus_error_init (&error);
1497 connection = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
1498 if (connection == NULL)
1500 _DBUS_ASSERT_ERROR_IS_SET (&error);
1501 dbus_error_free (&error);
1505 if (!bus_setup_debug_client (connection))
1507 dbus_connection_close (connection);
1508 dbus_connection_unref (connection);
1512 spin_connection_until_authenticated (context, connection);
1514 if (!check_hello_message (context, connection))
1517 if (dbus_bus_get_unique_name (connection) == NULL)
1519 /* We didn't successfully register, so we can't
1520 * do the usual kill_client_connection() checks
1522 kill_client_connection_unchecked (connection);
1526 if (!check_add_match_all (context, connection))
1529 kill_client_connection (context, connection);
1535 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1537 /* returns TRUE if the correct thing happens,
1538 * but the correct thing may include OOM errors.
1541 check_nonexistent_service_no_auto_start (BusContext *context,
1542 DBusConnection *connection)
1544 DBusMessage *message;
1545 dbus_uint32_t serial;
1547 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1548 dbus_uint32_t flags;
1550 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1552 DBUS_INTERFACE_DBUS,
1553 "StartServiceByName");
1555 if (message == NULL)
1558 dbus_message_set_auto_start (message, FALSE);
1561 if (!dbus_message_append_args (message,
1562 DBUS_TYPE_STRING, &nonexistent,
1563 DBUS_TYPE_UINT32, &flags,
1566 dbus_message_unref (message);
1570 if (!dbus_connection_send (connection, message, &serial))
1572 dbus_message_unref (message);
1576 dbus_message_unref (message);
1579 bus_test_run_everything (context);
1580 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1581 bus_test_run_everything (context);
1583 if (!dbus_connection_get_is_connected (connection))
1585 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1591 message = pop_message_waiting_for_memory (connection);
1592 if (message == NULL)
1594 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1595 "StartServiceByName", serial, connection);
1599 verbose_message_received (connection, message);
1601 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1603 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1605 _dbus_warn ("Message has wrong sender %s\n",
1606 dbus_message_get_sender (message) ?
1607 dbus_message_get_sender (message) : "(none)");
1611 if (dbus_message_is_error (message,
1612 DBUS_ERROR_NO_MEMORY))
1614 ; /* good, this is a valid response */
1616 else if (dbus_message_is_error (message,
1617 DBUS_ERROR_SERVICE_UNKNOWN))
1619 ; /* good, this is expected also */
1623 warn_unexpected (connection, message, "not this error");
1629 _dbus_warn ("Did not expect to successfully activate %s\n",
1630 NONEXISTENT_SERVICE_NAME);
1638 dbus_message_unref (message);
1643 /* returns TRUE if the correct thing happens,
1644 * but the correct thing may include OOM errors.
1647 check_nonexistent_service_auto_start (BusContext *context,
1648 DBusConnection *connection)
1650 DBusMessage *message;
1651 dbus_uint32_t serial;
1654 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1655 "/org/freedesktop/TestSuite",
1656 "org.freedesktop.TestSuite",
1659 if (message == NULL)
1662 if (!dbus_connection_send (connection, message, &serial))
1664 dbus_message_unref (message);
1668 dbus_message_unref (message);
1671 bus_test_run_everything (context);
1672 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1673 bus_test_run_everything (context);
1675 if (!dbus_connection_get_is_connected (connection))
1677 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1683 message = pop_message_waiting_for_memory (connection);
1685 if (message == NULL)
1687 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1688 "Echo message (auto activation)", serial, connection);
1692 verbose_message_received (connection, message);
1694 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1696 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1698 _dbus_warn ("Message has wrong sender %s\n",
1699 dbus_message_get_sender (message) ?
1700 dbus_message_get_sender (message) : "(none)");
1704 if (dbus_message_is_error (message,
1705 DBUS_ERROR_NO_MEMORY))
1707 ; /* good, this is a valid response */
1709 else if (dbus_message_is_error (message,
1710 DBUS_ERROR_SERVICE_UNKNOWN))
1712 ; /* good, this is expected also */
1716 warn_unexpected (connection, message, "not this error");
1722 _dbus_warn ("Did not expect to successfully activate %s\n",
1723 NONEXISTENT_SERVICE_NAME);
1731 dbus_message_unref (message);
1737 check_base_service_activated (BusContext *context,
1738 DBusConnection *connection,
1739 DBusMessage *initial_message,
1740 const char **base_service_p)
1742 DBusMessage *message;
1745 const char *base_service, *base_service_from_bus, *old_owner;
1749 dbus_error_init (&error);
1750 base_service = NULL;
1752 base_service_from_bus = NULL;
1754 message = initial_message;
1755 dbus_message_ref (message);
1757 if (dbus_message_is_signal (message,
1758 DBUS_INTERFACE_DBUS,
1759 "NameOwnerChanged"))
1761 CheckServiceOwnerChangedData socd;
1763 reget_service_name_arg:
1764 base_service = NULL;
1766 base_service_from_bus = NULL;
1768 if (!dbus_message_get_args (message, &error,
1769 DBUS_TYPE_STRING, &base_service,
1770 DBUS_TYPE_STRING, &old_owner,
1771 DBUS_TYPE_STRING, &base_service_from_bus,
1774 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1776 dbus_error_free (&error);
1777 _dbus_wait_for_memory ();
1778 goto reget_service_name_arg;
1782 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1783 "NameOwnerChanged (creation)",
1789 if (*base_service != ':')
1791 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1796 if (strcmp (base_service, base_service_from_bus) != 0)
1798 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1799 base_service, base_service_from_bus);
1805 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1810 socd.expected_kind = SERVICE_CREATED;
1811 socd.expected_service_name = base_service;
1812 socd.failed = FALSE;
1813 socd.skip_connection = connection;
1814 bus_test_clients_foreach (check_service_owner_changed_foreach,
1822 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1828 *base_service_p = base_service;
1834 dbus_message_unref (message);
1835 dbus_error_free (&error);
1841 check_service_activated (BusContext *context,
1842 DBusConnection *connection,
1843 const char *activated_name,
1844 const char *base_service_name,
1845 DBusMessage *initial_message)
1847 DBusMessage *message;
1850 dbus_uint32_t activation_result;
1854 dbus_error_init (&error);
1856 message = initial_message;
1857 dbus_message_ref (message);
1859 if (dbus_message_is_signal (message,
1860 DBUS_INTERFACE_DBUS,
1861 "NameOwnerChanged"))
1863 CheckServiceOwnerChangedData socd;
1864 const char *service_name, *base_service_from_bus, *old_owner;
1866 reget_service_name_arg:
1867 service_name = NULL;
1869 base_service_from_bus = NULL;
1871 if (!dbus_message_get_args (message, &error,
1872 DBUS_TYPE_STRING, &service_name,
1873 DBUS_TYPE_STRING, &old_owner,
1874 DBUS_TYPE_STRING, &base_service_from_bus,
1877 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1879 dbus_error_free (&error);
1880 _dbus_wait_for_memory ();
1881 goto reget_service_name_arg;
1885 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1886 "NameOwnerChanged (creation)",
1892 if (strcmp (service_name, activated_name) != 0)
1894 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1895 activated_name, service_name);
1899 if (strcmp (base_service_name, base_service_from_bus) != 0)
1901 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1902 base_service_from_bus, base_service_name);
1908 _dbus_warn ("expected a %s, got a %s\n",
1909 "NameOwnerChanged (creation)",
1910 "NameOwnerChanged (change)");
1914 socd.expected_kind = SERVICE_CREATED;
1915 socd.skip_connection = connection;
1916 socd.failed = FALSE;
1917 socd.expected_service_name = service_name;
1918 bus_test_clients_foreach (check_service_owner_changed_foreach,
1924 dbus_message_unref (message);
1925 service_name = NULL;
1927 base_service_from_bus = NULL;
1929 message = pop_message_waiting_for_memory (connection);
1930 if (message == NULL)
1932 _dbus_warn ("Expected a reply to %s, got nothing\n",
1933 "StartServiceByName");
1939 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1944 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1946 warn_unexpected (connection, message, "reply to StartServiceByName");
1951 activation_result = 0;
1952 if (!dbus_message_get_args (message, &error,
1953 DBUS_TYPE_UINT32, &activation_result,
1956 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1958 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1959 "StartServiceByName", error.message);
1963 dbus_error_free (&error);
1967 if (activation_result == DBUS_START_REPLY_SUCCESS)
1969 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1973 _dbus_warn ("Activation result was %u, no good.\n",
1979 dbus_message_unref (message);
1982 if (!check_no_leftovers (context))
1984 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1992 dbus_message_unref (message);
1993 dbus_error_free (&error);
1999 check_service_auto_activated (BusContext *context,
2000 DBusConnection *connection,
2001 const char *activated_name,
2002 const char *base_service_name,
2003 DBusMessage *initial_message)
2005 DBusMessage *message;
2011 dbus_error_init (&error);
2013 message = initial_message;
2014 dbus_message_ref (message);
2016 if (dbus_message_is_signal (message,
2017 DBUS_INTERFACE_DBUS,
2018 "NameOwnerChanged"))
2020 const char *service_name;
2021 CheckServiceOwnerChangedData socd;
2023 reget_service_name_arg:
2024 if (!dbus_message_get_args (message, &error,
2025 DBUS_TYPE_STRING, &service_name,
2028 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2030 dbus_error_free (&error);
2031 _dbus_wait_for_memory ();
2032 goto reget_service_name_arg;
2036 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2039 dbus_error_free (&error);
2044 if (strcmp (service_name, activated_name) != 0)
2046 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2047 activated_name, service_name);
2051 socd.expected_kind = SERVICE_CREATED;
2052 socd.expected_service_name = service_name;
2053 socd.failed = FALSE;
2054 socd.skip_connection = connection;
2055 bus_test_clients_foreach (check_service_owner_changed_foreach,
2061 /* Note that this differs from regular activation in that we don't get a
2062 * reply to ActivateService here.
2065 dbus_message_unref (message);
2067 service_name = NULL;
2071 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2080 dbus_message_unref (message);
2086 check_service_deactivated (BusContext *context,
2087 DBusConnection *connection,
2088 const char *activated_name,
2089 const char *base_service)
2092 CheckServiceOwnerChangedData socd;
2096 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2097 * service and the activated_name. The base service
2098 * notification is required to come last.
2100 socd.expected_kind = SERVICE_DELETED;
2101 socd.expected_service_name = activated_name;
2102 socd.failed = FALSE;
2103 socd.skip_connection = NULL;
2104 bus_test_clients_foreach (check_service_owner_changed_foreach,
2110 socd.expected_kind = SERVICE_DELETED;
2111 socd.expected_service_name = base_service;
2112 socd.failed = FALSE;
2113 socd.skip_connection = NULL;
2114 bus_test_clients_foreach (check_service_owner_changed_foreach,
2127 check_send_exit_to_service (BusContext *context,
2128 DBusConnection *connection,
2129 const char *service_name,
2130 const char *base_service)
2132 dbus_bool_t got_error;
2133 DBusMessage *message;
2134 dbus_uint32_t serial;
2137 _dbus_verbose ("Sending exit message to the test service\n");
2141 /* Kill off the test service by sending it a quit message */
2142 message = dbus_message_new_method_call (service_name,
2143 "/org/freedesktop/TestSuite",
2144 "org.freedesktop.TestSuite",
2147 if (message == NULL)
2149 /* Do this again; we still need the service to exit... */
2150 if (!check_send_exit_to_service (context, connection,
2151 service_name, base_service))
2157 if (!dbus_connection_send (connection, message, &serial))
2159 dbus_message_unref (message);
2161 /* Do this again; we still need the service to exit... */
2162 if (!check_send_exit_to_service (context, connection,
2163 service_name, base_service))
2169 dbus_message_unref (message);
2173 bus_test_run_clients_loop (SEND_PENDING (connection));
2175 /* read it in and write it out to test service */
2176 bus_test_run_bus_loop (context, FALSE);
2178 /* see if we got an error during message bus dispatching */
2179 bus_test_run_clients_loop (FALSE);
2180 message = borrow_message_waiting_for_memory (connection);
2181 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2184 dbus_connection_return_message (connection, message);
2190 /* If no error, wait for the test service to exit */
2191 block_connection_until_message_from_bus (context, connection, "test service to exit");
2193 bus_test_run_everything (context);
2198 message = pop_message_waiting_for_memory (connection);
2199 _dbus_assert (message != NULL);
2201 if (dbus_message_get_reply_serial (message) != serial)
2203 warn_unexpected (connection, message,
2204 "error with the correct reply serial");
2208 if (!dbus_message_is_error (message,
2209 DBUS_ERROR_NO_MEMORY))
2211 warn_unexpected (connection, message,
2212 "a no memory error from asking test service to exit");
2216 _dbus_verbose ("Got error %s when asking test service to exit\n",
2217 dbus_message_get_error_name (message));
2219 /* Do this again; we still need the service to exit... */
2220 if (!check_send_exit_to_service (context, connection,
2221 service_name, base_service))
2226 if (!check_service_deactivated (context, connection,
2227 service_name, base_service))
2230 /* Should now have a NoReply error from the Exit() method
2231 * call; it should have come after all the deactivation
2234 message = pop_message_waiting_for_memory (connection);
2236 if (message == NULL)
2238 warn_unexpected (connection, NULL,
2239 "reply to Exit() method call");
2242 if (!dbus_message_is_error (message,
2243 DBUS_ERROR_NO_REPLY))
2245 warn_unexpected (connection, message,
2246 "NoReply error from Exit() method call");
2250 if (dbus_message_get_reply_serial (message) != serial)
2252 warn_unexpected (connection, message,
2253 "error with the correct reply serial");
2257 _dbus_verbose ("Got error %s after test service exited\n",
2258 dbus_message_get_error_name (message));
2260 if (!check_no_leftovers (context))
2262 _dbus_warn ("Messages were left over after %s\n",
2263 _DBUS_FUNCTION_NAME);
2272 dbus_message_unref (message);
2278 check_got_error (BusContext *context,
2279 DBusConnection *connection,
2280 const char *first_error_name,
2283 DBusMessage *message;
2286 dbus_bool_t error_found;
2287 const char *error_name;
2291 message = pop_message_waiting_for_memory (connection);
2292 if (message == NULL)
2294 _dbus_warn ("Did not get an expected error\n");
2298 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2300 warn_unexpected (connection, message, "an error");
2305 error_found = FALSE;
2307 va_start (ap, first_error_name);
2308 error_name = first_error_name;
2309 while (error_name != NULL)
2311 if (dbus_message_is_error (message, error_name))
2316 error_name = va_arg (ap, char*);
2322 _dbus_warn ("Expected error %s or other, got %s instead\n",
2324 dbus_message_get_error_name (message));
2332 dbus_message_unref (message);
2339 GOT_SERVICE_CREATED,
2340 GOT_SERVICE_DELETED,
2345 static GotServiceInfo
2346 check_got_service_info (DBusMessage *message)
2348 GotServiceInfo message_kind;
2350 if (dbus_message_is_signal (message,
2351 DBUS_INTERFACE_DBUS,
2352 "NameOwnerChanged"))
2355 const char *service_name, *old_owner, *new_owner;
2356 dbus_error_init (&error);
2358 reget_service_info_data:
2359 service_name = NULL;
2363 dbus_message_get_args (message, &error,
2364 DBUS_TYPE_STRING, &service_name,
2365 DBUS_TYPE_STRING, &old_owner,
2366 DBUS_TYPE_STRING, &new_owner,
2368 if (dbus_error_is_set (&error))
2370 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2372 dbus_error_free (&error);
2373 goto reget_service_info_data;
2377 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2378 message_kind = GOT_SOMETHING_ELSE;
2381 else if (!old_owner[0])
2382 message_kind = GOT_SERVICE_CREATED;
2383 else if (!new_owner[0])
2384 message_kind = GOT_SERVICE_DELETED;
2386 message_kind = GOT_SOMETHING_ELSE;
2388 dbus_error_free (&error);
2390 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2391 message_kind = GOT_ERROR;
2393 message_kind = GOT_SOMETHING_ELSE;
2395 return message_kind;
2398 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2400 /* returns TRUE if the correct thing happens,
2401 * but the correct thing may include OOM errors.
2404 check_existent_service_no_auto_start (BusContext *context,
2405 DBusConnection *connection)
2407 DBusMessage *message;
2408 DBusMessage *base_service_message;
2409 const char *base_service;
2410 dbus_uint32_t serial;
2412 const char *existent = EXISTENT_SERVICE_NAME;
2413 dbus_uint32_t flags;
2415 base_service_message = NULL;
2417 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2419 DBUS_INTERFACE_DBUS,
2420 "StartServiceByName");
2422 if (message == NULL)
2425 dbus_message_set_auto_start (message, FALSE);
2428 if (!dbus_message_append_args (message,
2429 DBUS_TYPE_STRING, &existent,
2430 DBUS_TYPE_UINT32, &flags,
2433 dbus_message_unref (message);
2437 if (!dbus_connection_send (connection, message, &serial))
2439 dbus_message_unref (message);
2443 dbus_message_unref (message);
2446 bus_test_run_everything (context);
2448 /* now wait for the message bus to hear back from the activated
2451 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2453 bus_test_run_everything (context);
2455 if (!dbus_connection_get_is_connected (connection))
2457 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2463 message = pop_message_waiting_for_memory (connection);
2464 if (message == NULL)
2466 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2467 "StartServiceByName", serial, connection);
2471 verbose_message_received (connection, message);
2472 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2474 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2476 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2478 _dbus_warn ("Message has wrong sender %s\n",
2479 dbus_message_get_sender (message) ?
2480 dbus_message_get_sender (message) : "(none)");
2484 if (dbus_message_is_error (message,
2485 DBUS_ERROR_NO_MEMORY))
2487 ; /* good, this is a valid response */
2489 else if (dbus_message_is_error (message,
2490 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2491 dbus_message_is_error (message,
2492 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2493 dbus_message_is_error (message,
2494 DBUS_ERROR_SPAWN_EXEC_FAILED))
2496 ; /* good, this is expected also */
2500 _dbus_warn ("Did not expect error %s\n",
2501 dbus_message_get_error_name (message));
2507 GotServiceInfo message_kind;
2509 if (!check_base_service_activated (context, connection,
2510 message, &base_service))
2513 base_service_message = message;
2516 /* We may need to block here for the test service to exit or finish up */
2517 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2519 message = dbus_connection_borrow_message (connection);
2520 if (message == NULL)
2522 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2526 message_kind = check_got_service_info (message);
2528 dbus_connection_return_message (connection, message);
2531 switch (message_kind)
2533 case GOT_SOMETHING_ELSE:
2534 _dbus_warn ("Unexpected message after ActivateService "
2535 "(should be an error or a service announcement");
2539 if (!check_got_error (context, connection,
2540 DBUS_ERROR_SPAWN_CHILD_EXITED,
2541 DBUS_ERROR_NO_MEMORY,
2544 /* A service deleted should be coming along now after this error.
2545 * We can also get the error *after* the service deleted.
2550 case GOT_SERVICE_DELETED:
2552 /* The service started up and got a base address, but then
2553 * failed to register under EXISTENT_SERVICE_NAME
2555 CheckServiceOwnerChangedData socd;
2557 socd.expected_kind = SERVICE_DELETED;
2558 socd.expected_service_name = base_service;
2559 socd.failed = FALSE;
2560 socd.skip_connection = NULL;
2562 bus_test_clients_foreach (check_service_owner_changed_foreach,
2568 /* Now we should get an error about the service exiting
2569 * if we didn't get it before.
2571 if (message_kind != GOT_ERROR)
2573 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2575 /* and process everything again */
2576 bus_test_run_everything (context);
2578 if (!check_got_error (context, connection,
2579 DBUS_ERROR_SPAWN_CHILD_EXITED,
2580 DBUS_ERROR_NO_MEMORY,
2587 case GOT_SERVICE_CREATED:
2588 message = pop_message_waiting_for_memory (connection);
2589 if (message == NULL)
2591 _dbus_warn ("Failed to pop message we just put back! "
2592 "should have been a NameOwnerChanged (creation)\n");
2596 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2597 base_service, message))
2600 dbus_message_unref (message);
2603 if (!check_no_leftovers (context))
2605 _dbus_warn ("Messages were left over after successful activation\n");
2609 if (!check_send_exit_to_service (context, connection,
2610 EXISTENT_SERVICE_NAME, base_service))
2621 dbus_message_unref (message);
2623 if (base_service_message)
2624 dbus_message_unref (base_service_message);
2629 #ifndef DBUS_WIN_FIXME
2630 /* returns TRUE if the correct thing happens,
2631 * but the correct thing may include OOM errors.
2634 check_segfault_service_no_auto_start (BusContext *context,
2635 DBusConnection *connection)
2637 DBusMessage *message;
2638 dbus_uint32_t serial;
2640 const char *segv_service;
2641 dbus_uint32_t flags;
2643 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2645 DBUS_INTERFACE_DBUS,
2646 "StartServiceByName");
2648 if (message == NULL)
2651 dbus_message_set_auto_start (message, FALSE);
2653 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2655 if (!dbus_message_append_args (message,
2656 DBUS_TYPE_STRING, &segv_service,
2657 DBUS_TYPE_UINT32, &flags,
2660 dbus_message_unref (message);
2664 if (!dbus_connection_send (connection, message, &serial))
2666 dbus_message_unref (message);
2670 dbus_message_unref (message);
2673 bus_test_run_everything (context);
2674 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2675 bus_test_run_everything (context);
2677 if (!dbus_connection_get_is_connected (connection))
2679 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2685 message = pop_message_waiting_for_memory (connection);
2686 if (message == NULL)
2688 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2689 "StartServiceByName", serial, connection);
2693 verbose_message_received (connection, message);
2695 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2697 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2699 _dbus_warn ("Message has wrong sender %s\n",
2700 dbus_message_get_sender (message) ?
2701 dbus_message_get_sender (message) : "(none)");
2705 if (dbus_message_is_error (message,
2706 DBUS_ERROR_NO_MEMORY))
2708 ; /* good, this is a valid response */
2710 else if (dbus_message_is_error (message,
2711 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2713 ; /* good, this is expected also */
2717 warn_unexpected (connection, message, "not this error");
2724 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2732 dbus_message_unref (message);
2738 /* returns TRUE if the correct thing happens,
2739 * but the correct thing may include OOM errors.
2742 check_segfault_service_auto_start (BusContext *context,
2743 DBusConnection *connection)
2745 DBusMessage *message;
2746 dbus_uint32_t serial;
2749 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2750 "/org/freedesktop/TestSuite",
2751 "org.freedesktop.TestSuite",
2754 if (message == NULL)
2757 if (!dbus_connection_send (connection, message, &serial))
2759 dbus_message_unref (message);
2763 dbus_message_unref (message);
2766 bus_test_run_everything (context);
2767 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2768 bus_test_run_everything (context);
2770 if (!dbus_connection_get_is_connected (connection))
2772 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2778 message = pop_message_waiting_for_memory (connection);
2779 if (message == NULL)
2781 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2782 "Echo message (auto activation)", serial, connection);
2786 verbose_message_received (connection, message);
2788 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2790 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2792 _dbus_warn ("Message has wrong sender %s\n",
2793 dbus_message_get_sender (message) ?
2794 dbus_message_get_sender (message) : "(none)");
2798 if (dbus_message_is_error (message,
2799 DBUS_ERROR_NO_MEMORY))
2801 ; /* good, this is a valid response */
2803 else if (dbus_message_is_error (message,
2804 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2806 ; /* good, this is expected also */
2810 warn_unexpected (connection, message, "not this error");
2817 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2825 dbus_message_unref (message);
2831 #define TEST_ECHO_MESSAGE "Test echo message"
2832 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2834 /* returns TRUE if the correct thing happens,
2835 * but the correct thing may include OOM errors.
2838 check_existent_hello_from_self (BusContext *context,
2839 DBusConnection *connection)
2841 DBusMessage *message;
2842 dbus_uint32_t serial;
2845 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2846 "/org/freedesktop/TestSuite",
2847 "org.freedesktop.TestSuite",
2848 "RunHelloFromSelf");
2850 if (message == NULL)
2853 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2854 if (!dbus_message_append_args (message,
2855 DBUS_TYPE_STRING, &text,
2858 dbus_message_unref (message);
2862 if (!dbus_connection_send (connection, message, &serial))
2864 dbus_message_unref (message);
2868 dbus_message_unref (message);
2871 bus_test_run_everything (context);
2873 /* Note: if this test is run in OOM mode, it will block when the bus
2874 * doesn't send a reply due to OOM.
2876 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2878 message = pop_message_waiting_for_memory (connection);
2879 if (message == NULL)
2881 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2885 if (dbus_message_get_reply_serial (message) != serial)
2887 _dbus_warn ("Wrong reply serial\n");
2888 dbus_message_unref (message);
2892 dbus_message_unref (message);
2898 /* returns TRUE if the correct thing happens,
2899 * but the correct thing may include OOM errors.
2902 check_existent_ping (BusContext *context,
2903 DBusConnection *connection)
2905 DBusMessage *message;
2906 dbus_uint32_t serial;
2907 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2908 "/org/freedesktop/TestSuite",
2909 "org.freedesktop.DBus.Peer",
2912 if (message == NULL)
2915 if (!dbus_connection_send (connection, message, &serial))
2917 dbus_message_unref (message);
2921 dbus_message_unref (message);
2924 bus_test_run_everything (context);
2926 /* Note: if this test is run in OOM mode, it will block when the bus
2927 * doesn't send a reply due to OOM.
2929 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2931 message = pop_message_waiting_for_memory (connection);
2932 if (message == NULL)
2934 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2938 if (dbus_message_get_reply_serial (message) != serial)
2940 _dbus_warn ("Wrong reply serial\n");
2941 dbus_message_unref (message);
2945 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2947 _dbus_warn ("Unexpected message return during Ping\n");
2948 dbus_message_unref (message);
2952 dbus_message_unref (message);
2958 /* returns TRUE if the correct thing happens,
2959 * but the correct thing may include OOM errors.
2962 check_existent_get_machine_id (BusContext *context,
2963 DBusConnection *connection)
2965 DBusMessage *message;
2966 dbus_uint32_t serial;
2967 const char *machine_id;
2969 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2970 "/org/freedesktop/TestSuite",
2971 "org.freedesktop.DBus.Peer",
2974 if (message == NULL)
2977 if (!dbus_connection_send (connection, message, &serial))
2979 dbus_message_unref (message);
2983 dbus_message_unref (message);
2986 bus_test_run_everything (context);
2988 /* Note: if this test is run in OOM mode, it will block when the bus
2989 * doesn't send a reply due to OOM.
2991 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
2993 message = pop_message_waiting_for_memory (connection);
2994 if (message == NULL)
2996 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3000 if (dbus_message_get_reply_serial (message) != serial)
3002 _dbus_warn ("Wrong reply serial\n");
3003 dbus_message_unref (message);
3007 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3009 _dbus_warn ("Unexpected message return during GetMachineId\n");
3010 dbus_message_unref (message);
3015 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3017 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3018 dbus_message_unref (message);
3022 if (machine_id == NULL || strlen (machine_id) != 32)
3024 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3025 dbus_message_unref (message);
3029 /* We can't check that the machine id is correct because during make check it is
3030 * just made up for each process separately
3033 dbus_message_unref (message);
3039 /* returns TRUE if the correct thing happens,
3040 * but the correct thing may include OOM errors.
3043 check_existent_service_auto_start (BusContext *context,
3044 DBusConnection *connection)
3046 DBusMessage *message;
3047 DBusMessage *base_service_message;
3048 dbus_uint32_t serial;
3050 const char *base_service;
3053 base_service_message = NULL;
3055 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3056 "/org/freedesktop/TestSuite",
3057 "org.freedesktop.TestSuite",
3060 if (message == NULL)
3063 text = TEST_ECHO_MESSAGE;
3064 if (!dbus_message_append_args (message,
3065 DBUS_TYPE_STRING, &text,
3068 dbus_message_unref (message);
3072 if (!dbus_connection_send (connection, message, &serial))
3074 dbus_message_unref (message);
3078 dbus_message_unref (message);
3081 bus_test_run_everything (context);
3083 /* now wait for the message bus to hear back from the activated
3086 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3087 bus_test_run_everything (context);
3089 if (!dbus_connection_get_is_connected (connection))
3091 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3097 message = pop_message_waiting_for_memory (connection);
3098 if (message == NULL)
3100 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3101 serial, connection);
3105 verbose_message_received (connection, message);
3106 _dbus_verbose (" (after sending %s)\n", "auto start");
3108 /* we should get zero or two ServiceOwnerChanged signals */
3109 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3111 GotServiceInfo message_kind;
3113 if (!check_base_service_activated (context, connection,
3114 message, &base_service))
3117 base_service_message = message;
3120 /* We may need to block here for the test service to exit or finish up */
3121 block_connection_until_message_from_bus (context, connection, "service to exit");
3123 /* Should get a service creation notification for the activated
3124 * service name, or a service deletion on the base service name
3126 message = dbus_connection_borrow_message (connection);
3127 if (message == NULL)
3129 _dbus_warn ("No message after auto activation "
3130 "(should be a service announcement)\n");
3131 dbus_connection_return_message (connection, message);
3136 message_kind = check_got_service_info (message);
3138 dbus_connection_return_message (connection, message);
3141 switch (message_kind)
3143 case GOT_SERVICE_CREATED:
3144 message = pop_message_waiting_for_memory (connection);
3145 if (message == NULL)
3147 _dbus_warn ("Failed to pop message we just put back! "
3148 "should have been a NameOwnerChanged (creation)\n");
3152 /* Check that ServiceOwnerChanged (creation) was correctly received */
3153 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3154 base_service, message))
3157 dbus_message_unref (message);
3162 case GOT_SERVICE_DELETED:
3164 /* The service started up and got a base address, but then
3165 * failed to register under EXISTENT_SERVICE_NAME
3167 CheckServiceOwnerChangedData socd;
3169 socd.expected_kind = SERVICE_DELETED;
3170 socd.expected_service_name = base_service;
3171 socd.failed = FALSE;
3172 socd.skip_connection = NULL;
3173 bus_test_clients_foreach (check_service_owner_changed_foreach,
3183 case GOT_SOMETHING_ELSE:
3184 _dbus_warn ("Unexpected message after auto activation\n");
3189 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3190 * come the method reply (or error) from the initial method call
3193 /* Note: if this test is run in OOM mode, it will block when the bus
3194 * doesn't send a reply due to OOM.
3196 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3198 message = pop_message_waiting_for_memory (connection);
3199 if (message == NULL)
3201 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3205 if (dbus_message_get_reply_serial (message) != serial)
3207 _dbus_warn ("Wrong reply serial\n");
3211 dbus_message_unref (message);
3214 if (!check_existent_ping (context, connection))
3217 if (!check_existent_get_machine_id (context, connection))
3220 if (!check_existent_hello_from_self (context, connection))
3223 if (!check_send_exit_to_service (context, connection,
3224 EXISTENT_SERVICE_NAME,
3232 dbus_message_unref (message);
3234 if (base_service_message)
3235 dbus_message_unref (base_service_message);
3240 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3242 /* returns TRUE if the correct thing happens,
3243 * but the correct thing may include OOM errors.
3246 check_shell_fail_service_auto_start (BusContext *context,
3247 DBusConnection *connection)
3249 DBusMessage *message;
3250 dbus_uint32_t serial;
3253 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3254 "/org/freedesktop/TestSuite",
3255 "org.freedesktop.TestSuite",
3258 if (message == NULL)
3261 if (!dbus_connection_send (connection, message, &serial))
3263 dbus_message_unref (message);
3267 dbus_message_unref (message);
3270 bus_test_run_everything (context);
3271 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3272 bus_test_run_everything (context);
3274 if (!dbus_connection_get_is_connected (connection))
3276 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3282 message = pop_message_waiting_for_memory (connection);
3283 if (message == NULL)
3285 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3286 "Echo message (auto activation)", serial, connection);
3290 verbose_message_received (connection, message);
3292 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3294 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3296 _dbus_warn ("Message has wrong sender %s\n",
3297 dbus_message_get_sender (message) ?
3298 dbus_message_get_sender (message) : "(none)");
3302 if (dbus_message_is_error (message,
3303 DBUS_ERROR_NO_MEMORY))
3305 ; /* good, this is a valid response */
3307 else if (dbus_message_is_error (message,
3308 DBUS_ERROR_INVALID_ARGS))
3310 _dbus_verbose("got invalid args\n");
3311 ; /* good, this is expected also */
3315 warn_unexpected (connection, message, "not this error");
3322 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3330 dbus_message_unref (message);
3335 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3337 /* returns TRUE if the correct thing happens,
3338 * but the correct thing may include OOM errors.
3341 check_shell_service_success_auto_start (BusContext *context,
3342 DBusConnection *connection)
3344 DBusMessage *message;
3345 DBusMessage *base_service_message;
3346 dbus_uint32_t serial;
3348 const char *base_service;
3349 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3351 base_service_message = NULL;
3353 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3354 "/org/freedesktop/TestSuite",
3355 "org.freedesktop.TestSuite",
3358 if (message == NULL)
3361 if (!dbus_connection_send (connection, message, &serial))
3363 dbus_message_unref (message);
3367 dbus_message_unref (message);
3370 bus_test_run_everything (context);
3372 /* now wait for the message bus to hear back from the activated
3375 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3376 bus_test_run_everything (context);
3378 if (!dbus_connection_get_is_connected (connection))
3380 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3386 message = pop_message_waiting_for_memory (connection);
3387 if (message == NULL)
3389 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3390 serial, connection);
3394 verbose_message_received (connection, message);
3395 _dbus_verbose (" (after sending %s)\n", "auto start");
3397 /* we should get zero or two ServiceOwnerChanged signals */
3398 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3400 GotServiceInfo message_kind;
3402 if (!check_base_service_activated (context, connection,
3403 message, &base_service))
3406 base_service_message = message;
3409 /* We may need to block here for the test service to exit or finish up */
3410 block_connection_until_message_from_bus (context, connection, "service to exit");
3412 /* Should get a service creation notification for the activated
3413 * service name, or a service deletion on the base service name
3415 message = dbus_connection_borrow_message (connection);
3416 if (message == NULL)
3418 _dbus_warn ("No message after auto activation "
3419 "(should be a service announcement)\n");
3420 dbus_connection_return_message (connection, message);
3425 message_kind = check_got_service_info (message);
3427 dbus_connection_return_message (connection, message);
3430 switch (message_kind)
3432 case GOT_SERVICE_CREATED:
3433 message = pop_message_waiting_for_memory (connection);
3434 if (message == NULL)
3436 _dbus_warn ("Failed to pop message we just put back! "
3437 "should have been a NameOwnerChanged (creation)\n");
3441 /* Check that ServiceOwnerChanged (creation) was correctly received */
3442 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3443 base_service, message))
3446 dbus_message_unref (message);
3451 case GOT_SERVICE_DELETED:
3453 /* The service started up and got a base address, but then
3454 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3456 CheckServiceOwnerChangedData socd;
3458 socd.expected_kind = SERVICE_DELETED;
3459 socd.expected_service_name = base_service;
3460 socd.failed = FALSE;
3461 socd.skip_connection = NULL;
3462 bus_test_clients_foreach (check_service_owner_changed_foreach,
3472 case GOT_SOMETHING_ELSE:
3473 _dbus_warn ("Unexpected message after auto activation\n");
3478 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3479 * come the method reply (or error) from the initial method call
3482 /* Note: if this test is run in OOM mode, it will block when the bus
3483 * doesn't send a reply due to OOM.
3485 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3487 message = pop_message_waiting_for_memory (connection);
3488 if (message == NULL)
3490 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3494 if (dbus_message_get_reply_serial (message) != serial)
3496 _dbus_warn ("Wrong reply serial\n");
3500 if (!dbus_message_get_args (message, NULL,
3501 DBUS_TYPE_STRING, &argv[0],
3502 DBUS_TYPE_STRING, &argv[1],
3503 DBUS_TYPE_STRING, &argv[2],
3504 DBUS_TYPE_STRING, &argv[3],
3505 DBUS_TYPE_STRING, &argv[4],
3506 DBUS_TYPE_STRING, &argv[5],
3507 DBUS_TYPE_STRING, &argv[6],
3510 _dbus_warn ("Error getting arguments from return\n");
3514 /* don't worry about arg[0] as it may be different
3515 depending on the path to the tests
3517 if (strcmp("-test", argv[1]) != 0)
3519 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3524 if (strcmp("that", argv[2]) != 0)
3526 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3531 if (strcmp("we get", argv[3]) != 0)
3533 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3538 if (strcmp("back", argv[4]) != 0)
3540 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3545 if (strcmp("--what", argv[5]) != 0)
3547 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3552 if (strcmp("we put in", argv[6]) != 0)
3554 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3555 "we put in", argv[6]);
3559 dbus_message_unref (message);
3562 if (!check_send_exit_to_service (context, connection,
3563 SHELL_SUCCESS_SERVICE_NAME,
3571 dbus_message_unref (message);
3573 if (base_service_message)
3574 dbus_message_unref (base_service_message);
3582 BusContext *context;
3586 check_oom_check1_func (void *data)
3588 Check1Data *d = data;
3590 if (! (* d->func) (d->context))
3593 if (!check_no_leftovers (d->context))
3595 _dbus_warn ("Messages were left over, should be covered by test suite\n");
3603 check1_try_iterations (BusContext *context,
3604 const char *description,
3610 d.context = context;
3612 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
3614 _dbus_assert_not_reached ("test failed");
3618 check_get_services (BusContext *context,
3619 DBusConnection *connection,
3624 DBusMessage *message;
3625 dbus_uint32_t serial;
3632 dbus_error_init (&error);
3635 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
3637 DBUS_INTERFACE_DBUS,
3640 if (message == NULL)
3643 if (!dbus_connection_send (connection, message, &serial))
3645 dbus_message_unref (message);
3649 /* send our message */
3650 bus_test_run_clients_loop (SEND_PENDING (connection));
3652 dbus_message_unref (message);
3655 dbus_connection_ref (connection); /* because we may get disconnected */
3656 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
3658 if (!dbus_connection_get_is_connected (connection))
3660 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3662 dbus_connection_unref (connection);
3667 dbus_connection_unref (connection);
3669 message = pop_message_waiting_for_memory (connection);
3670 if (message == NULL)
3672 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3673 method, serial, connection);
3677 verbose_message_received (connection, message);
3679 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3681 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
3683 ; /* good, this is a valid response */
3687 warn_unexpected (connection, message, "not this error");
3694 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
3696 ; /* good, expected */
3700 warn_unexpected (connection, message,
3701 "method_return for ListActivatableNames/ListNames");
3708 if (!dbus_message_get_args (message, &error,
3714 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
3716 _dbus_verbose ("no memory to list services by %s\n", method);
3717 dbus_error_free (&error);
3718 _dbus_wait_for_memory ();
3719 goto retry_get_property;
3723 _dbus_assert (dbus_error_is_set (&error));
3724 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
3733 if (!check_no_leftovers (context))
3739 dbus_error_free (&error);
3742 dbus_message_unref (message);
3747 /* returns TRUE if the correct thing happens,
3748 * but the correct thing may include OOM errors.
3751 check_list_services (BusContext *context,
3752 DBusConnection *connection)
3754 DBusMessage *message;
3755 DBusMessage *base_service_message;
3756 const char *base_service;
3757 dbus_uint32_t serial;
3759 const char *existent = EXISTENT_SERVICE_NAME;
3760 dbus_uint32_t flags;
3764 _dbus_verbose ("check_list_services for %p\n", connection);
3766 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
3771 if (!_dbus_string_array_contains ((const char **)services, existent))
3773 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
3777 dbus_free_string_array (services);
3779 base_service_message = NULL;
3781 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
3783 DBUS_INTERFACE_DBUS,
3784 "StartServiceByName");
3786 if (message == NULL)
3789 dbus_message_set_auto_start (message, FALSE);
3792 if (!dbus_message_append_args (message,
3793 DBUS_TYPE_STRING, &existent,
3794 DBUS_TYPE_UINT32, &flags,
3797 dbus_message_unref (message);
3801 if (!dbus_connection_send (connection, message, &serial))
3803 dbus_message_unref (message);
3807 dbus_message_unref (message);
3810 bus_test_run_everything (context);
3812 /* now wait for the message bus to hear back from the activated
3815 block_connection_until_message_from_bus (context, connection, "activated service to connect");
3817 bus_test_run_everything (context);
3819 if (!dbus_connection_get_is_connected (connection))
3821 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3827 message = pop_message_waiting_for_memory (connection);
3828 if (message == NULL)
3830 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
3831 "StartServiceByName", serial, connection);
3835 verbose_message_received (connection, message);
3836 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
3838 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3840 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3842 _dbus_warn ("Message has wrong sender %s\n",
3843 dbus_message_get_sender (message) ?
3844 dbus_message_get_sender (message) : "(none)");
3848 if (dbus_message_is_error (message,
3849 DBUS_ERROR_NO_MEMORY))
3851 ; /* good, this is a valid response */
3853 else if (dbus_message_is_error (message,
3854 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
3855 dbus_message_is_error (message,
3856 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
3857 dbus_message_is_error (message,
3858 DBUS_ERROR_SPAWN_EXEC_FAILED))
3860 ; /* good, this is expected also */
3864 _dbus_warn ("Did not expect error %s\n",
3865 dbus_message_get_error_name (message));
3871 GotServiceInfo message_kind;
3873 if (!check_base_service_activated (context, connection,
3874 message, &base_service))
3877 base_service_message = message;
3880 /* We may need to block here for the test service to exit or finish up */
3881 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
3883 message = dbus_connection_borrow_message (connection);
3884 if (message == NULL)
3886 _dbus_warn ("Did not receive any messages after base service creation notification\n");
3890 message_kind = check_got_service_info (message);
3892 dbus_connection_return_message (connection, message);
3895 switch (message_kind)
3897 case GOT_SOMETHING_ELSE:
3899 case GOT_SERVICE_DELETED:
3900 _dbus_warn ("Unexpected message after ActivateService "
3901 "(should be an error or a service announcement)\n");
3904 case GOT_SERVICE_CREATED:
3905 message = pop_message_waiting_for_memory (connection);
3906 if (message == NULL)
3908 _dbus_warn ("Failed to pop message we just put back! "
3909 "should have been a NameOwnerChanged (creation)\n");
3913 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
3914 base_service, message))
3917 dbus_message_unref (message);
3920 if (!check_no_leftovers (context))
3922 _dbus_warn ("Messages were left over after successful activation\n");
3930 if (!check_get_services (context, connection, "ListNames", &services, &len))
3935 if (!_dbus_string_array_contains ((const char **)services, existent))
3937 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
3941 dbus_free_string_array (services);
3943 if (!check_send_exit_to_service (context, connection,
3944 EXISTENT_SERVICE_NAME, base_service))
3951 dbus_message_unref (message);
3953 if (base_service_message)
3954 dbus_message_unref (base_service_message);
3962 BusContext *context;
3963 DBusConnection *connection;
3967 check_oom_check2_func (void *data)
3969 Check2Data *d = data;
3971 if (! (* d->func) (d->context, d->connection))
3974 if (!check_no_leftovers (d->context))
3976 _dbus_warn ("Messages were left over, should be covered by test suite\n");
3984 check2_try_iterations (BusContext *context,
3985 DBusConnection *connection,
3986 const char *description,
3992 d.context = context;
3993 d.connection = connection;
3995 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
3998 _dbus_warn ("%s failed during oom\n", description);
3999 _dbus_assert_not_reached ("test failed");
4004 bus_dispatch_test (const DBusString *test_data_dir)
4006 BusContext *context;
4007 DBusConnection *foo;
4008 DBusConnection *bar;
4009 DBusConnection *baz;
4012 dbus_error_init (&error);
4014 context = bus_context_new_test (test_data_dir,
4015 "valid-config-files/debug-allow-all.conf");
4016 if (context == NULL)
4019 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4021 _dbus_assert_not_reached ("could not alloc connection");
4023 if (!bus_setup_debug_client (foo))
4024 _dbus_assert_not_reached ("could not set up connection");
4026 spin_connection_until_authenticated (context, foo);
4028 if (!check_hello_message (context, foo))
4029 _dbus_assert_not_reached ("hello message failed");
4031 if (!check_double_hello_message (context, foo))
4032 _dbus_assert_not_reached ("double hello message failed");
4034 if (!check_add_match_all (context, foo))
4035 _dbus_assert_not_reached ("AddMatch message failed");
4037 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4039 _dbus_assert_not_reached ("could not alloc connection");
4041 if (!bus_setup_debug_client (bar))
4042 _dbus_assert_not_reached ("could not set up connection");
4044 spin_connection_until_authenticated (context, bar);
4046 if (!check_hello_message (context, bar))
4047 _dbus_assert_not_reached ("hello message failed");
4049 if (!check_add_match_all (context, bar))
4050 _dbus_assert_not_reached ("AddMatch message failed");
4052 baz = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4054 _dbus_assert_not_reached ("could not alloc connection");
4056 if (!bus_setup_debug_client (baz))
4057 _dbus_assert_not_reached ("could not set up connection");
4059 spin_connection_until_authenticated (context, baz);
4061 if (!check_hello_message (context, baz))
4062 _dbus_assert_not_reached ("hello message failed");
4064 if (!check_add_match_all (context, baz))
4065 _dbus_assert_not_reached ("AddMatch message failed");
4067 if (!check_get_connection_unix_user (context, baz))
4068 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4070 if (!check_get_connection_unix_process_id (context, baz))
4071 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4073 if (!check_list_services (context, baz))
4074 _dbus_assert_not_reached ("ListActivatableNames message failed");
4076 if (!check_no_leftovers (context))
4078 _dbus_warn ("Messages were left over after setting up initial connections\n");
4079 _dbus_assert_not_reached ("initial connection setup failed");
4082 check1_try_iterations (context, "create_and_hello",
4083 check_hello_connection);
4085 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4086 check_nonexistent_service_no_auto_start);
4088 #ifdef DBUS_WIN_FIXME
4089 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4091 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4092 check_segfault_service_no_auto_start);
4095 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4096 check_existent_service_no_auto_start);
4098 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4099 check_nonexistent_service_auto_start);
4102 #ifdef DBUS_WIN_FIXME
4103 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4105 check2_try_iterations (context, foo, "segfault_service_auto_start",
4106 check_segfault_service_auto_start);
4109 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4110 check_shell_fail_service_auto_start);
4113 /* Note: need to resolve some issues with the testing code in order to run
4114 * this in oom (handle that we sometimes don't get replies back from the bus
4115 * when oom happens, without blocking the test).
4117 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4118 check_existent_service_auto_start);
4121 if (!check_existent_service_auto_start (context, foo))
4122 _dbus_assert_not_reached ("existent service auto start failed");
4124 if (!check_shell_service_success_auto_start (context, foo))
4125 _dbus_assert_not_reached ("shell success service auto start failed");
4127 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4129 kill_client_connection_unchecked (foo);
4130 kill_client_connection_unchecked (bar);
4131 kill_client_connection_unchecked (baz);
4133 bus_context_unref (context);
4139 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4141 BusContext *context;
4142 DBusConnection *foo;
4145 dbus_error_init (&error);
4147 /* Test SHA1 authentication */
4148 _dbus_verbose ("Testing SHA1 context\n");
4150 context = bus_context_new_test (test_data_dir,
4151 "valid-config-files/debug-allow-all-sha1.conf");
4152 if (context == NULL)
4155 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4157 _dbus_assert_not_reached ("could not alloc connection");
4159 if (!bus_setup_debug_client (foo))
4160 _dbus_assert_not_reached ("could not set up connection");
4162 spin_connection_until_authenticated (context, foo);
4164 if (!check_hello_message (context, foo))
4165 _dbus_assert_not_reached ("hello message failed");
4167 if (!check_add_match_all (context, foo))
4168 _dbus_assert_not_reached ("addmatch message failed");
4170 if (!check_no_leftovers (context))
4172 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4173 _dbus_assert_not_reached ("initial connection setup failed");
4176 check1_try_iterations (context, "create_and_hello_sha1",
4177 check_hello_connection);
4179 kill_client_connection_unchecked (foo);
4181 bus_context_unref (context);
4186 #endif /* DBUS_BUILD_TESTS */