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_owner (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");
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 */
1269 warn_unexpected (connection, message, "not this error");
1276 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1278 ; /* good, expected */
1282 warn_unexpected (connection, message,
1283 "method_return for GetConnectionUnixProcessID");
1290 if (!dbus_message_get_args (message, &error,
1291 DBUS_TYPE_UINT32, &pid,
1294 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1296 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1297 dbus_error_free (&error);
1298 _dbus_wait_for_memory ();
1299 goto retry_get_property;
1303 _dbus_assert (dbus_error_is_set (&error));
1304 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1309 /* test if returned pid is the same as our own pid
1311 * @todo It would probably be good to restructure the tests
1312 * in a way so our parent is the bus that we're testing
1313 * cause then we can test that the pid returned matches
1316 if (pid != (dbus_uint32_t) _dbus_getpid ())
1318 _dbus_assert (dbus_error_is_set (&error));
1319 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1325 if (!check_no_leftovers (context))
1331 dbus_error_free (&error);
1334 dbus_message_unref (message);
1339 /* returns TRUE if the correct thing happens,
1340 * but the correct thing may include OOM errors.
1343 check_add_match_all (BusContext *context,
1344 DBusConnection *connection)
1346 DBusMessage *message;
1348 dbus_uint32_t serial;
1350 const char *empty = "";
1353 dbus_error_init (&error);
1356 _dbus_verbose ("check_add_match_all for %p\n", connection);
1358 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1360 DBUS_INTERFACE_DBUS,
1363 if (message == NULL)
1366 /* empty string match rule matches everything */
1367 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1370 dbus_message_unref (message);
1374 if (!dbus_connection_send (connection, message, &serial))
1376 dbus_message_unref (message);
1380 dbus_message_unref (message);
1383 dbus_connection_ref (connection); /* because we may get disconnected */
1385 /* send our message */
1386 bus_test_run_clients_loop (SEND_PENDING (connection));
1388 if (!dbus_connection_get_is_connected (connection))
1390 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1392 dbus_connection_unref (connection);
1397 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1399 if (!dbus_connection_get_is_connected (connection))
1401 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1403 dbus_connection_unref (connection);
1408 dbus_connection_unref (connection);
1410 message = pop_message_waiting_for_memory (connection);
1411 if (message == NULL)
1413 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1414 "AddMatch", serial, connection);
1418 verbose_message_received (connection, message);
1420 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1422 _dbus_warn ("Message has wrong sender %s\n",
1423 dbus_message_get_sender (message) ?
1424 dbus_message_get_sender (message) : "(none)");
1428 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1430 if (dbus_message_is_error (message,
1431 DBUS_ERROR_NO_MEMORY))
1433 ; /* good, this is a valid response */
1437 warn_unexpected (connection, message, "not this error");
1444 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1446 ; /* good, expected */
1447 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1451 warn_unexpected (connection, message, "method return for AddMatch");
1457 if (!check_no_leftovers (context))
1463 dbus_error_free (&error);
1466 dbus_message_unref (message);
1471 /* returns TRUE if the correct thing happens,
1472 * but the correct thing may include OOM errors.
1475 check_hello_connection (BusContext *context)
1477 DBusConnection *connection;
1480 dbus_error_init (&error);
1482 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
1483 if (connection == NULL)
1485 _DBUS_ASSERT_ERROR_IS_SET (&error);
1486 dbus_error_free (&error);
1490 if (!bus_setup_debug_client (connection))
1492 dbus_connection_close (connection);
1493 dbus_connection_unref (connection);
1497 spin_connection_until_authenticated (context, connection);
1499 if (!check_hello_message (context, connection))
1502 if (dbus_bus_get_unique_name (connection) == NULL)
1504 /* We didn't successfully register, so we can't
1505 * do the usual kill_client_connection() checks
1507 kill_client_connection_unchecked (connection);
1511 if (!check_add_match_all (context, connection))
1514 kill_client_connection (context, connection);
1520 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1522 /* returns TRUE if the correct thing happens,
1523 * but the correct thing may include OOM errors.
1526 check_nonexistent_service_no_auto_start (BusContext *context,
1527 DBusConnection *connection)
1529 DBusMessage *message;
1530 dbus_uint32_t serial;
1532 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1533 dbus_uint32_t flags;
1535 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1537 DBUS_INTERFACE_DBUS,
1538 "StartServiceByName");
1540 if (message == NULL)
1543 dbus_message_set_auto_start (message, FALSE);
1546 if (!dbus_message_append_args (message,
1547 DBUS_TYPE_STRING, &nonexistent,
1548 DBUS_TYPE_UINT32, &flags,
1551 dbus_message_unref (message);
1555 if (!dbus_connection_send (connection, message, &serial))
1557 dbus_message_unref (message);
1561 dbus_message_unref (message);
1564 bus_test_run_everything (context);
1565 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1566 bus_test_run_everything (context);
1568 if (!dbus_connection_get_is_connected (connection))
1570 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1576 message = pop_message_waiting_for_memory (connection);
1577 if (message == NULL)
1579 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1580 "StartServiceByName", serial, connection);
1584 verbose_message_received (connection, message);
1586 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1588 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1590 _dbus_warn ("Message has wrong sender %s\n",
1591 dbus_message_get_sender (message) ?
1592 dbus_message_get_sender (message) : "(none)");
1596 if (dbus_message_is_error (message,
1597 DBUS_ERROR_NO_MEMORY))
1599 ; /* good, this is a valid response */
1601 else if (dbus_message_is_error (message,
1602 DBUS_ERROR_SERVICE_UNKNOWN))
1604 ; /* good, this is expected also */
1608 warn_unexpected (connection, message, "not this error");
1614 _dbus_warn ("Did not expect to successfully activate %s\n",
1615 NONEXISTENT_SERVICE_NAME);
1623 dbus_message_unref (message);
1628 /* returns TRUE if the correct thing happens,
1629 * but the correct thing may include OOM errors.
1632 check_nonexistent_service_auto_start (BusContext *context,
1633 DBusConnection *connection)
1635 DBusMessage *message;
1636 dbus_uint32_t serial;
1639 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1640 "/org/freedesktop/TestSuite",
1641 "org.freedesktop.TestSuite",
1644 if (message == NULL)
1647 if (!dbus_connection_send (connection, message, &serial))
1649 dbus_message_unref (message);
1653 dbus_message_unref (message);
1656 bus_test_run_everything (context);
1657 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1658 bus_test_run_everything (context);
1660 if (!dbus_connection_get_is_connected (connection))
1662 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1668 message = pop_message_waiting_for_memory (connection);
1670 if (message == NULL)
1672 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1673 "Echo message (auto activation)", serial, connection);
1677 verbose_message_received (connection, message);
1679 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1681 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1683 _dbus_warn ("Message has wrong sender %s\n",
1684 dbus_message_get_sender (message) ?
1685 dbus_message_get_sender (message) : "(none)");
1689 if (dbus_message_is_error (message,
1690 DBUS_ERROR_NO_MEMORY))
1692 ; /* good, this is a valid response */
1694 else if (dbus_message_is_error (message,
1695 DBUS_ERROR_SERVICE_UNKNOWN))
1697 ; /* good, this is expected also */
1701 warn_unexpected (connection, message, "not this error");
1707 _dbus_warn ("Did not expect to successfully activate %s\n",
1708 NONEXISTENT_SERVICE_NAME);
1716 dbus_message_unref (message);
1722 check_base_service_activated (BusContext *context,
1723 DBusConnection *connection,
1724 DBusMessage *initial_message,
1725 const char **base_service_p)
1727 DBusMessage *message;
1730 const char *base_service, *base_service_from_bus, *old_owner;
1734 dbus_error_init (&error);
1735 base_service = NULL;
1737 base_service_from_bus = NULL;
1739 message = initial_message;
1740 dbus_message_ref (message);
1742 if (dbus_message_is_signal (message,
1743 DBUS_INTERFACE_DBUS,
1744 "NameOwnerChanged"))
1746 CheckServiceOwnerChangedData socd;
1748 reget_service_name_arg:
1749 base_service = NULL;
1751 base_service_from_bus = NULL;
1753 if (!dbus_message_get_args (message, &error,
1754 DBUS_TYPE_STRING, &base_service,
1755 DBUS_TYPE_STRING, &old_owner,
1756 DBUS_TYPE_STRING, &base_service_from_bus,
1759 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1761 dbus_error_free (&error);
1762 _dbus_wait_for_memory ();
1763 goto reget_service_name_arg;
1767 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1768 "NameOwnerChanged (creation)",
1774 if (*base_service != ':')
1776 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1781 if (strcmp (base_service, base_service_from_bus) != 0)
1783 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1784 base_service, base_service_from_bus);
1790 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1795 socd.expected_kind = SERVICE_CREATED;
1796 socd.expected_service_name = base_service;
1797 socd.failed = FALSE;
1798 socd.skip_connection = connection;
1799 bus_test_clients_foreach (check_service_owner_changed_foreach,
1807 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1813 *base_service_p = base_service;
1819 dbus_message_unref (message);
1820 dbus_error_free (&error);
1826 check_service_activated (BusContext *context,
1827 DBusConnection *connection,
1828 const char *activated_name,
1829 const char *base_service_name,
1830 DBusMessage *initial_message)
1832 DBusMessage *message;
1835 dbus_uint32_t activation_result;
1839 dbus_error_init (&error);
1841 message = initial_message;
1842 dbus_message_ref (message);
1844 if (dbus_message_is_signal (message,
1845 DBUS_INTERFACE_DBUS,
1846 "NameOwnerChanged"))
1848 CheckServiceOwnerChangedData socd;
1849 const char *service_name, *base_service_from_bus, *old_owner;
1851 reget_service_name_arg:
1852 service_name = NULL;
1854 base_service_from_bus = NULL;
1856 if (!dbus_message_get_args (message, &error,
1857 DBUS_TYPE_STRING, &service_name,
1858 DBUS_TYPE_STRING, &old_owner,
1859 DBUS_TYPE_STRING, &base_service_from_bus,
1862 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1864 dbus_error_free (&error);
1865 _dbus_wait_for_memory ();
1866 goto reget_service_name_arg;
1870 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1871 "NameOwnerChanged (creation)",
1877 if (strcmp (service_name, activated_name) != 0)
1879 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1880 activated_name, service_name);
1884 if (strcmp (base_service_name, base_service_from_bus) != 0)
1886 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1887 base_service_from_bus, base_service_name);
1893 _dbus_warn ("expected a %s, got a %s\n",
1894 "NameOwnerChanged (creation)",
1895 "NameOwnerChanged (change)");
1899 socd.expected_kind = SERVICE_CREATED;
1900 socd.skip_connection = connection;
1901 socd.failed = FALSE;
1902 socd.expected_service_name = service_name;
1903 bus_test_clients_foreach (check_service_owner_changed_foreach,
1909 dbus_message_unref (message);
1910 service_name = NULL;
1912 base_service_from_bus = NULL;
1914 message = pop_message_waiting_for_memory (connection);
1915 if (message == NULL)
1917 _dbus_warn ("Expected a reply to %s, got nothing\n",
1918 "StartServiceByName");
1924 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1929 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1931 warn_unexpected (connection, message, "reply to StartServiceByName");
1936 activation_result = 0;
1937 if (!dbus_message_get_args (message, &error,
1938 DBUS_TYPE_UINT32, &activation_result,
1941 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1943 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1944 "StartServiceByName", error.message);
1948 dbus_error_free (&error);
1952 if (activation_result == DBUS_START_REPLY_SUCCESS)
1954 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1958 _dbus_warn ("Activation result was %u, no good.\n",
1964 dbus_message_unref (message);
1967 if (!check_no_leftovers (context))
1969 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1977 dbus_message_unref (message);
1978 dbus_error_free (&error);
1984 check_service_auto_activated (BusContext *context,
1985 DBusConnection *connection,
1986 const char *activated_name,
1987 const char *base_service_name,
1988 DBusMessage *initial_message)
1990 DBusMessage *message;
1996 dbus_error_init (&error);
1998 message = initial_message;
1999 dbus_message_ref (message);
2001 if (dbus_message_is_signal (message,
2002 DBUS_INTERFACE_DBUS,
2003 "NameOwnerChanged"))
2005 const char *service_name;
2006 CheckServiceOwnerChangedData socd;
2008 reget_service_name_arg:
2009 if (!dbus_message_get_args (message, &error,
2010 DBUS_TYPE_STRING, &service_name,
2013 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2015 dbus_error_free (&error);
2016 _dbus_wait_for_memory ();
2017 goto reget_service_name_arg;
2021 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2024 dbus_error_free (&error);
2029 if (strcmp (service_name, activated_name) != 0)
2031 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2032 activated_name, service_name);
2036 socd.expected_kind = SERVICE_CREATED;
2037 socd.expected_service_name = service_name;
2038 socd.failed = FALSE;
2039 socd.skip_connection = connection;
2040 bus_test_clients_foreach (check_service_owner_changed_foreach,
2046 /* Note that this differs from regular activation in that we don't get a
2047 * reply to ActivateService here.
2050 dbus_message_unref (message);
2052 service_name = NULL;
2056 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2065 dbus_message_unref (message);
2071 check_service_deactivated (BusContext *context,
2072 DBusConnection *connection,
2073 const char *activated_name,
2074 const char *base_service)
2077 CheckServiceOwnerChangedData socd;
2081 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2082 * service and the activated_name. The base service
2083 * notification is required to come last.
2085 socd.expected_kind = SERVICE_DELETED;
2086 socd.expected_service_name = activated_name;
2087 socd.failed = FALSE;
2088 socd.skip_connection = NULL;
2089 bus_test_clients_foreach (check_service_owner_changed_foreach,
2095 socd.expected_kind = SERVICE_DELETED;
2096 socd.expected_service_name = base_service;
2097 socd.failed = FALSE;
2098 socd.skip_connection = NULL;
2099 bus_test_clients_foreach (check_service_owner_changed_foreach,
2112 check_send_exit_to_service (BusContext *context,
2113 DBusConnection *connection,
2114 const char *service_name,
2115 const char *base_service)
2117 dbus_bool_t got_error;
2118 DBusMessage *message;
2119 dbus_uint32_t serial;
2122 _dbus_verbose ("Sending exit message to the test service\n");
2126 /* Kill off the test service by sending it a quit message */
2127 message = dbus_message_new_method_call (service_name,
2128 "/org/freedesktop/TestSuite",
2129 "org.freedesktop.TestSuite",
2132 if (message == NULL)
2134 /* Do this again; we still need the service to exit... */
2135 if (!check_send_exit_to_service (context, connection,
2136 service_name, base_service))
2142 if (!dbus_connection_send (connection, message, &serial))
2144 dbus_message_unref (message);
2146 /* Do this again; we still need the service to exit... */
2147 if (!check_send_exit_to_service (context, connection,
2148 service_name, base_service))
2154 dbus_message_unref (message);
2158 bus_test_run_clients_loop (SEND_PENDING (connection));
2160 /* read it in and write it out to test service */
2161 bus_test_run_bus_loop (context, FALSE);
2163 /* see if we got an error during message bus dispatching */
2164 bus_test_run_clients_loop (FALSE);
2165 message = borrow_message_waiting_for_memory (connection);
2166 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2169 dbus_connection_return_message (connection, message);
2175 /* If no error, wait for the test service to exit */
2176 block_connection_until_message_from_bus (context, connection, "test service to exit");
2178 bus_test_run_everything (context);
2183 message = pop_message_waiting_for_memory (connection);
2184 _dbus_assert (message != NULL);
2186 if (dbus_message_get_reply_serial (message) != serial)
2188 warn_unexpected (connection, message,
2189 "error with the correct reply serial");
2193 if (!dbus_message_is_error (message,
2194 DBUS_ERROR_NO_MEMORY))
2196 warn_unexpected (connection, message,
2197 "a no memory error from asking test service to exit");
2201 _dbus_verbose ("Got error %s when asking test service to exit\n",
2202 dbus_message_get_error_name (message));
2204 /* Do this again; we still need the service to exit... */
2205 if (!check_send_exit_to_service (context, connection,
2206 service_name, base_service))
2211 if (!check_service_deactivated (context, connection,
2212 service_name, base_service))
2215 /* Should now have a NoReply error from the Exit() method
2216 * call; it should have come after all the deactivation
2219 message = pop_message_waiting_for_memory (connection);
2221 if (message == NULL)
2223 warn_unexpected (connection, NULL,
2224 "reply to Exit() method call");
2227 if (!dbus_message_is_error (message,
2228 DBUS_ERROR_NO_REPLY))
2230 warn_unexpected (connection, message,
2231 "NoReply error from Exit() method call");
2235 if (dbus_message_get_reply_serial (message) != serial)
2237 warn_unexpected (connection, message,
2238 "error with the correct reply serial");
2242 _dbus_verbose ("Got error %s after test service exited\n",
2243 dbus_message_get_error_name (message));
2245 if (!check_no_leftovers (context))
2247 _dbus_warn ("Messages were left over after %s\n",
2248 _DBUS_FUNCTION_NAME);
2257 dbus_message_unref (message);
2263 check_got_error (BusContext *context,
2264 DBusConnection *connection,
2265 const char *first_error_name,
2268 DBusMessage *message;
2271 dbus_bool_t error_found;
2272 const char *error_name;
2276 message = pop_message_waiting_for_memory (connection);
2277 if (message == NULL)
2279 _dbus_warn ("Did not get an expected error\n");
2283 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2285 warn_unexpected (connection, message, "an error");
2290 error_found = FALSE;
2292 va_start (ap, first_error_name);
2293 error_name = first_error_name;
2294 while (error_name != NULL)
2296 if (dbus_message_is_error (message, error_name))
2301 error_name = va_arg (ap, char*);
2307 _dbus_warn ("Expected error %s or other, got %s instead\n",
2309 dbus_message_get_error_name (message));
2317 dbus_message_unref (message);
2324 GOT_SERVICE_CREATED,
2325 GOT_SERVICE_DELETED,
2330 static GotServiceInfo
2331 check_got_service_info (DBusMessage *message)
2333 GotServiceInfo message_kind;
2335 if (dbus_message_is_signal (message,
2336 DBUS_INTERFACE_DBUS,
2337 "NameOwnerChanged"))
2340 const char *service_name, *old_owner, *new_owner;
2341 dbus_error_init (&error);
2343 reget_service_info_data:
2344 service_name = NULL;
2348 dbus_message_get_args (message, &error,
2349 DBUS_TYPE_STRING, &service_name,
2350 DBUS_TYPE_STRING, &old_owner,
2351 DBUS_TYPE_STRING, &new_owner,
2353 if (dbus_error_is_set (&error))
2355 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2357 dbus_error_free (&error);
2358 goto reget_service_info_data;
2362 _dbus_warn ("unexpected arguments for NameOwnerChanged message");
2363 message_kind = GOT_SOMETHING_ELSE;
2366 else if (!old_owner[0])
2367 message_kind = GOT_SERVICE_CREATED;
2368 else if (!new_owner[0])
2369 message_kind = GOT_SERVICE_DELETED;
2371 message_kind = GOT_SOMETHING_ELSE;
2373 dbus_error_free (&error);
2375 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2376 message_kind = GOT_ERROR;
2378 message_kind = GOT_SOMETHING_ELSE;
2380 return message_kind;
2383 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2385 /* returns TRUE if the correct thing happens,
2386 * but the correct thing may include OOM errors.
2389 check_existent_service_no_auto_start (BusContext *context,
2390 DBusConnection *connection)
2392 DBusMessage *message;
2393 DBusMessage *base_service_message;
2394 const char *base_service;
2395 dbus_uint32_t serial;
2397 const char *existent = EXISTENT_SERVICE_NAME;
2398 dbus_uint32_t flags;
2400 base_service_message = NULL;
2402 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2404 DBUS_INTERFACE_DBUS,
2405 "StartServiceByName");
2407 if (message == NULL)
2410 dbus_message_set_auto_start (message, FALSE);
2413 if (!dbus_message_append_args (message,
2414 DBUS_TYPE_STRING, &existent,
2415 DBUS_TYPE_UINT32, &flags,
2418 dbus_message_unref (message);
2422 if (!dbus_connection_send (connection, message, &serial))
2424 dbus_message_unref (message);
2428 dbus_message_unref (message);
2431 bus_test_run_everything (context);
2433 /* now wait for the message bus to hear back from the activated
2436 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2438 bus_test_run_everything (context);
2440 if (!dbus_connection_get_is_connected (connection))
2442 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2448 message = pop_message_waiting_for_memory (connection);
2449 if (message == NULL)
2451 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2452 "StartServiceByName", serial, connection);
2456 verbose_message_received (connection, message);
2457 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2459 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2461 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2463 _dbus_warn ("Message has wrong sender %s\n",
2464 dbus_message_get_sender (message) ?
2465 dbus_message_get_sender (message) : "(none)");
2469 if (dbus_message_is_error (message,
2470 DBUS_ERROR_NO_MEMORY))
2472 ; /* good, this is a valid response */
2474 else if (dbus_message_is_error (message,
2475 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2476 dbus_message_is_error (message,
2477 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2478 dbus_message_is_error (message,
2479 DBUS_ERROR_SPAWN_EXEC_FAILED))
2481 ; /* good, this is expected also */
2485 _dbus_warn ("Did not expect error %s\n",
2486 dbus_message_get_error_name (message));
2492 GotServiceInfo message_kind;
2494 if (!check_base_service_activated (context, connection,
2495 message, &base_service))
2498 base_service_message = message;
2501 /* We may need to block here for the test service to exit or finish up */
2502 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2504 message = dbus_connection_borrow_message (connection);
2505 if (message == NULL)
2507 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2511 message_kind = check_got_service_info (message);
2513 dbus_connection_return_message (connection, message);
2516 switch (message_kind)
2518 case GOT_SOMETHING_ELSE:
2519 _dbus_warn ("Unexpected message after ActivateService "
2520 "(should be an error or a service announcement");
2524 if (!check_got_error (context, connection,
2525 DBUS_ERROR_SPAWN_CHILD_EXITED,
2526 DBUS_ERROR_NO_MEMORY,
2529 /* A service deleted should be coming along now after this error.
2530 * We can also get the error *after* the service deleted.
2535 case GOT_SERVICE_DELETED:
2537 /* The service started up and got a base address, but then
2538 * failed to register under EXISTENT_SERVICE_NAME
2540 CheckServiceOwnerChangedData socd;
2542 socd.expected_kind = SERVICE_DELETED;
2543 socd.expected_service_name = base_service;
2544 socd.failed = FALSE;
2545 socd.skip_connection = NULL;
2547 bus_test_clients_foreach (check_service_owner_changed_foreach,
2553 /* Now we should get an error about the service exiting
2554 * if we didn't get it before.
2556 if (message_kind != GOT_ERROR)
2558 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2560 /* and process everything again */
2561 bus_test_run_everything (context);
2563 if (!check_got_error (context, connection,
2564 DBUS_ERROR_SPAWN_CHILD_EXITED,
2571 case GOT_SERVICE_CREATED:
2572 message = pop_message_waiting_for_memory (connection);
2573 if (message == NULL)
2575 _dbus_warn ("Failed to pop message we just put back! "
2576 "should have been a NameOwnerChanged (creation)\n");
2580 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2581 base_service, message))
2584 dbus_message_unref (message);
2587 if (!check_no_leftovers (context))
2589 _dbus_warn ("Messages were left over after successful activation\n");
2593 if (!check_send_exit_to_service (context, connection,
2594 EXISTENT_SERVICE_NAME, base_service))
2605 dbus_message_unref (message);
2607 if (base_service_message)
2608 dbus_message_unref (base_service_message);
2613 /* returns TRUE if the correct thing happens,
2614 * but the correct thing may include OOM errors.
2617 check_segfault_service_no_auto_start (BusContext *context,
2618 DBusConnection *connection)
2620 DBusMessage *message;
2621 dbus_uint32_t serial;
2623 const char *segv_service;
2624 dbus_uint32_t flags;
2626 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2628 DBUS_INTERFACE_DBUS,
2629 "StartServiceByName");
2631 if (message == NULL)
2634 dbus_message_set_auto_start (message, FALSE);
2636 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2638 if (!dbus_message_append_args (message,
2639 DBUS_TYPE_STRING, &segv_service,
2640 DBUS_TYPE_UINT32, &flags,
2643 dbus_message_unref (message);
2647 if (!dbus_connection_send (connection, message, &serial))
2649 dbus_message_unref (message);
2653 dbus_message_unref (message);
2656 bus_test_run_everything (context);
2657 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2658 bus_test_run_everything (context);
2660 if (!dbus_connection_get_is_connected (connection))
2662 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2668 message = pop_message_waiting_for_memory (connection);
2669 if (message == NULL)
2671 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2672 "StartServiceByName", serial, connection);
2676 verbose_message_received (connection, message);
2678 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2680 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2682 _dbus_warn ("Message has wrong sender %s\n",
2683 dbus_message_get_sender (message) ?
2684 dbus_message_get_sender (message) : "(none)");
2688 if (dbus_message_is_error (message,
2689 DBUS_ERROR_NO_MEMORY))
2691 ; /* good, this is a valid response */
2693 else if (dbus_message_is_error (message,
2694 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2696 ; /* good, this is expected also */
2700 warn_unexpected (connection, message, "not this error");
2707 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2715 dbus_message_unref (message);
2721 /* returns TRUE if the correct thing happens,
2722 * but the correct thing may include OOM errors.
2725 check_segfault_service_auto_start (BusContext *context,
2726 DBusConnection *connection)
2728 DBusMessage *message;
2729 dbus_uint32_t serial;
2732 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2733 "/org/freedesktop/TestSuite",
2734 "org.freedesktop.TestSuite",
2737 if (message == NULL)
2740 if (!dbus_connection_send (connection, message, &serial))
2742 dbus_message_unref (message);
2746 dbus_message_unref (message);
2749 bus_test_run_everything (context);
2750 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2751 bus_test_run_everything (context);
2753 if (!dbus_connection_get_is_connected (connection))
2755 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2761 message = pop_message_waiting_for_memory (connection);
2762 if (message == NULL)
2764 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2765 "Echo message (auto activation)", serial, connection);
2769 verbose_message_received (connection, message);
2771 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2773 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2775 _dbus_warn ("Message has wrong sender %s\n",
2776 dbus_message_get_sender (message) ?
2777 dbus_message_get_sender (message) : "(none)");
2781 if (dbus_message_is_error (message,
2782 DBUS_ERROR_NO_MEMORY))
2784 ; /* good, this is a valid response */
2786 else if (dbus_message_is_error (message,
2787 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2789 ; /* good, this is expected also */
2793 warn_unexpected (connection, message, "not this error");
2800 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2808 dbus_message_unref (message);
2813 #define TEST_ECHO_MESSAGE "Test echo message"
2814 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2816 /* returns TRUE if the correct thing happens,
2817 * but the correct thing may include OOM errors.
2820 check_existent_hello_from_self (BusContext *context,
2821 DBusConnection *connection)
2823 DBusMessage *message;
2824 dbus_uint32_t serial;
2827 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2828 "/org/freedesktop/TestSuite",
2829 "org.freedesktop.TestSuite",
2830 "RunHelloFromSelf");
2832 if (message == NULL)
2835 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2836 if (!dbus_message_append_args (message,
2837 DBUS_TYPE_STRING, &text,
2840 dbus_message_unref (message);
2844 if (!dbus_connection_send (connection, message, &serial))
2846 dbus_message_unref (message);
2850 dbus_message_unref (message);
2853 bus_test_run_everything (context);
2855 /* Note: if this test is run in OOM mode, it will block when the bus
2856 * doesn't send a reply due to OOM.
2858 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2860 message = pop_message_waiting_for_memory (connection);
2861 if (message == NULL)
2863 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2867 if (dbus_message_get_reply_serial (message) != serial)
2869 _dbus_warn ("Wrong reply serial\n");
2870 dbus_message_unref (message);
2874 dbus_message_unref (message);
2880 /* returns TRUE if the correct thing happens,
2881 * but the correct thing may include OOM errors.
2884 check_existent_ping (BusContext *context,
2885 DBusConnection *connection)
2887 DBusMessage *message;
2888 dbus_uint32_t serial;
2889 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2890 "/org/freedesktop/TestSuite",
2891 "org.freedesktop.DBus.Peer",
2894 if (message == NULL)
2897 if (!dbus_connection_send (connection, message, &serial))
2899 dbus_message_unref (message);
2903 dbus_message_unref (message);
2906 bus_test_run_everything (context);
2908 /* Note: if this test is run in OOM mode, it will block when the bus
2909 * doesn't send a reply due to OOM.
2911 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2913 message = pop_message_waiting_for_memory (connection);
2914 if (message == NULL)
2916 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2920 if (dbus_message_get_reply_serial (message) != serial)
2922 _dbus_warn ("Wrong reply serial\n");
2923 dbus_message_unref (message);
2927 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2929 _dbus_warn ("Unexpected message return during Ping\n");
2930 dbus_message_unref (message);
2934 dbus_message_unref (message);
2940 /* returns TRUE if the correct thing happens,
2941 * but the correct thing may include OOM errors.
2944 check_existent_service_auto_start (BusContext *context,
2945 DBusConnection *connection)
2947 DBusMessage *message;
2948 DBusMessage *base_service_message;
2949 dbus_uint32_t serial;
2951 const char *base_service;
2954 base_service_message = NULL;
2956 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2957 "/org/freedesktop/TestSuite",
2958 "org.freedesktop.TestSuite",
2961 if (message == NULL)
2964 text = TEST_ECHO_MESSAGE;
2965 if (!dbus_message_append_args (message,
2966 DBUS_TYPE_STRING, &text,
2969 dbus_message_unref (message);
2973 if (!dbus_connection_send (connection, message, &serial))
2975 dbus_message_unref (message);
2979 dbus_message_unref (message);
2982 bus_test_run_everything (context);
2984 /* now wait for the message bus to hear back from the activated
2987 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
2988 bus_test_run_everything (context);
2990 if (!dbus_connection_get_is_connected (connection))
2992 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2998 message = pop_message_waiting_for_memory (connection);
2999 if (message == NULL)
3001 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3002 serial, connection);
3006 verbose_message_received (connection, message);
3007 _dbus_verbose (" (after sending %s)\n", "auto start");
3009 /* we should get zero or two ServiceOwnerChanged signals */
3010 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3012 GotServiceInfo message_kind;
3014 if (!check_base_service_activated (context, connection,
3015 message, &base_service))
3018 base_service_message = message;
3021 /* We may need to block here for the test service to exit or finish up */
3022 block_connection_until_message_from_bus (context, connection, "service to exit");
3024 /* Should get a service creation notification for the activated
3025 * service name, or a service deletion on the base service name
3027 message = dbus_connection_borrow_message (connection);
3028 if (message == NULL)
3030 _dbus_warn ("No message after auto activation "
3031 "(should be a service announcement)");
3032 dbus_connection_return_message (connection, message);
3037 message_kind = check_got_service_info (message);
3039 dbus_connection_return_message (connection, message);
3042 switch (message_kind)
3044 case GOT_SERVICE_CREATED:
3045 message = pop_message_waiting_for_memory (connection);
3046 if (message == NULL)
3048 _dbus_warn ("Failed to pop message we just put back! "
3049 "should have been a NameOwnerChanged (creation)\n");
3053 /* Check that ServiceOwnerChanged (creation) was correctly received */
3054 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3055 base_service, message))
3058 dbus_message_unref (message);
3063 case GOT_SERVICE_DELETED:
3065 /* The service started up and got a base address, but then
3066 * failed to register under EXISTENT_SERVICE_NAME
3068 CheckServiceOwnerChangedData socd;
3070 socd.expected_kind = SERVICE_DELETED;
3071 socd.expected_service_name = base_service;
3072 socd.failed = FALSE;
3073 socd.skip_connection = NULL;
3074 bus_test_clients_foreach (check_service_owner_changed_foreach,
3084 case GOT_SOMETHING_ELSE:
3085 _dbus_warn ("Unexpected message after auto activation\n");
3090 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3091 * come the method reply (or error) from the initial method call
3094 /* Note: if this test is run in OOM mode, it will block when the bus
3095 * doesn't send a reply due to OOM.
3097 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3099 message = pop_message_waiting_for_memory (connection);
3100 if (message == NULL)
3102 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3106 if (dbus_message_get_reply_serial (message) != serial)
3108 _dbus_warn ("Wrong reply serial\n");
3112 dbus_message_unref (message);
3115 if (!check_existent_ping (context, connection))
3118 if (!check_existent_hello_from_self (context, connection))
3121 if (!check_send_exit_to_service (context, connection,
3122 EXISTENT_SERVICE_NAME,
3130 dbus_message_unref (message);
3132 if (base_service_message)
3133 dbus_message_unref (base_service_message);
3138 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3140 /* returns TRUE if the correct thing happens,
3141 * but the correct thing may include OOM errors.
3144 check_shell_fail_service_auto_start (BusContext *context,
3145 DBusConnection *connection)
3147 DBusMessage *message;
3148 dbus_uint32_t serial;
3151 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3152 "/org/freedesktop/TestSuite",
3153 "org.freedesktop.TestSuite",
3156 if (message == NULL)
3159 if (!dbus_connection_send (connection, message, &serial))
3161 dbus_message_unref (message);
3165 dbus_message_unref (message);
3168 bus_test_run_everything (context);
3169 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3170 bus_test_run_everything (context);
3172 if (!dbus_connection_get_is_connected (connection))
3174 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3180 message = pop_message_waiting_for_memory (connection);
3181 if (message == NULL)
3183 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3184 "Echo message (auto activation)", serial, connection);
3188 verbose_message_received (connection, message);
3190 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3192 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3194 _dbus_warn ("Message has wrong sender %s\n",
3195 dbus_message_get_sender (message) ?
3196 dbus_message_get_sender (message) : "(none)");
3200 if (dbus_message_is_error (message,
3201 DBUS_ERROR_NO_MEMORY))
3203 ; /* good, this is a valid response */
3205 else if (dbus_message_is_error (message,
3206 DBUS_ERROR_INVALID_ARGS))
3208 _dbus_verbose("got invalid args\n");
3209 ; /* good, this is expected also */
3213 warn_unexpected (connection, message, "not this error");
3220 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3228 dbus_message_unref (message);
3233 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3235 /* returns TRUE if the correct thing happens,
3236 * but the correct thing may include OOM errors.
3239 check_shell_service_success_auto_start (BusContext *context,
3240 DBusConnection *connection)
3242 DBusMessage *message;
3243 DBusMessage *base_service_message;
3244 dbus_uint32_t serial;
3246 const char *base_service;
3247 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3249 base_service_message = NULL;
3251 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3252 "/org/freedesktop/TestSuite",
3253 "org.freedesktop.TestSuite",
3256 if (message == NULL)
3259 if (!dbus_connection_send (connection, message, &serial))
3261 dbus_message_unref (message);
3265 dbus_message_unref (message);
3268 bus_test_run_everything (context);
3270 /* now wait for the message bus to hear back from the activated
3273 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3274 bus_test_run_everything (context);
3276 if (!dbus_connection_get_is_connected (connection))
3278 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3284 message = pop_message_waiting_for_memory (connection);
3285 if (message == NULL)
3287 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3288 serial, connection);
3292 verbose_message_received (connection, message);
3293 _dbus_verbose (" (after sending %s)\n", "auto start");
3295 /* we should get zero or two ServiceOwnerChanged signals */
3296 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3298 GotServiceInfo message_kind;
3300 if (!check_base_service_activated (context, connection,
3301 message, &base_service))
3304 base_service_message = message;
3307 /* We may need to block here for the test service to exit or finish up */
3308 block_connection_until_message_from_bus (context, connection, "service to exit");
3310 /* Should get a service creation notification for the activated
3311 * service name, or a service deletion on the base service name
3313 message = dbus_connection_borrow_message (connection);
3314 if (message == NULL)
3316 _dbus_warn ("No message after auto activation "
3317 "(should be a service announcement)");
3318 dbus_connection_return_message (connection, message);
3323 message_kind = check_got_service_info (message);
3325 dbus_connection_return_message (connection, message);
3328 switch (message_kind)
3330 case GOT_SERVICE_CREATED:
3331 message = pop_message_waiting_for_memory (connection);
3332 if (message == NULL)
3334 _dbus_warn ("Failed to pop message we just put back! "
3335 "should have been a NameOwnerChanged (creation)\n");
3339 /* Check that ServiceOwnerChanged (creation) was correctly received */
3340 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3341 base_service, message))
3344 dbus_message_unref (message);
3349 case GOT_SERVICE_DELETED:
3351 /* The service started up and got a base address, but then
3352 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3354 CheckServiceOwnerChangedData socd;
3356 socd.expected_kind = SERVICE_DELETED;
3357 socd.expected_service_name = base_service;
3358 socd.failed = FALSE;
3359 socd.skip_connection = NULL;
3360 bus_test_clients_foreach (check_service_owner_changed_foreach,
3370 case GOT_SOMETHING_ELSE:
3371 _dbus_warn ("Unexpected message after auto activation\n");
3376 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3377 * come the method reply (or error) from the initial method call
3380 /* Note: if this test is run in OOM mode, it will block when the bus
3381 * doesn't send a reply due to OOM.
3383 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3385 message = pop_message_waiting_for_memory (connection);
3386 if (message == NULL)
3388 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3392 if (dbus_message_get_reply_serial (message) != serial)
3394 _dbus_warn ("Wrong reply serial\n");
3398 if (!dbus_message_get_args (message, NULL,
3399 DBUS_TYPE_STRING, &argv[0],
3400 DBUS_TYPE_STRING, &argv[1],
3401 DBUS_TYPE_STRING, &argv[2],
3402 DBUS_TYPE_STRING, &argv[3],
3403 DBUS_TYPE_STRING, &argv[4],
3404 DBUS_TYPE_STRING, &argv[5],
3405 DBUS_TYPE_STRING, &argv[6],
3408 _dbus_warn ("Error getting arguments from return");
3412 /* don't worry about arg[0] as it may be different
3413 depending on the path to the tests
3415 if (strcmp("-test", argv[1]) != 0)
3417 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)",
3422 if (strcmp("that", argv[2]) != 0)
3424 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)",
3429 if (strcmp("we get", argv[3]) != 0)
3431 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)",
3436 if (strcmp("back", argv[4]) != 0)
3438 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)",
3443 if (strcmp("--what", argv[5]) != 0)
3445 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)",
3450 if (strcmp("we put in", argv[6]) != 0)
3452 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)",
3453 "we put in", argv[6]);
3457 dbus_message_unref (message);
3460 if (!check_send_exit_to_service (context, connection,
3461 SHELL_SUCCESS_SERVICE_NAME,
3469 dbus_message_unref (message);
3471 if (base_service_message)
3472 dbus_message_unref (base_service_message);
3480 BusContext *context;
3484 check_oom_check1_func (void *data)
3486 Check1Data *d = data;
3488 if (! (* d->func) (d->context))
3491 if (!check_no_leftovers (d->context))
3493 _dbus_warn ("Messages were left over, should be covered by test suite\n");
3501 check1_try_iterations (BusContext *context,
3502 const char *description,
3508 d.context = context;
3510 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
3512 _dbus_assert_not_reached ("test failed");
3518 BusContext *context;
3519 DBusConnection *connection;
3523 check_oom_check2_func (void *data)
3525 Check2Data *d = data;
3527 if (! (* d->func) (d->context, d->connection))
3530 if (!check_no_leftovers (d->context))
3532 _dbus_warn ("Messages were left over, should be covered by test suite");
3540 check2_try_iterations (BusContext *context,
3541 DBusConnection *connection,
3542 const char *description,
3548 d.context = context;
3549 d.connection = connection;
3551 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
3554 _dbus_warn ("%s failed during oom\n", description);
3555 _dbus_assert_not_reached ("test failed");
3560 bus_dispatch_test (const DBusString *test_data_dir)
3562 BusContext *context;
3563 DBusConnection *foo;
3564 DBusConnection *bar;
3565 DBusConnection *baz;
3568 dbus_error_init (&error);
3570 context = bus_context_new_test (test_data_dir,
3571 "valid-config-files/debug-allow-all.conf");
3572 if (context == NULL)
3575 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3577 _dbus_assert_not_reached ("could not alloc connection");
3579 if (!bus_setup_debug_client (foo))
3580 _dbus_assert_not_reached ("could not set up connection");
3582 spin_connection_until_authenticated (context, foo);
3584 if (!check_hello_message (context, foo))
3585 _dbus_assert_not_reached ("hello message failed");
3587 if (!check_double_hello_message (context, foo))
3588 _dbus_assert_not_reached ("double hello message failed");
3590 if (!check_add_match_all (context, foo))
3591 _dbus_assert_not_reached ("AddMatch message failed");
3593 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
3595 _dbus_assert_not_reached ("could not alloc connection");
3597 if (!bus_setup_debug_client (bar))
3598 _dbus_assert_not_reached ("could not set up connection");
3600 spin_connection_until_authenticated (context, bar);
3602 if (!check_hello_message (context, bar))
3603 _dbus_assert_not_reached ("hello message failed");
3605 if (!check_add_match_all (context, bar))
3606 _dbus_assert_not_reached ("AddMatch message failed");
3608 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
3610 _dbus_assert_not_reached ("could not alloc connection");
3612 if (!bus_setup_debug_client (baz))
3613 _dbus_assert_not_reached ("could not set up connection");
3615 spin_connection_until_authenticated (context, baz);
3617 if (!check_hello_message (context, baz))
3618 _dbus_assert_not_reached ("hello message failed");
3620 if (!check_add_match_all (context, baz))
3621 _dbus_assert_not_reached ("AddMatch message failed");
3623 if (!check_get_connection_unix_user (context, baz))
3624 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
3626 if (!check_get_connection_unix_process_id (context, baz))
3627 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
3629 if (!check_no_leftovers (context))
3631 _dbus_warn ("Messages were left over after setting up initial connections");
3632 _dbus_assert_not_reached ("initial connection setup failed");
3635 check1_try_iterations (context, "create_and_hello",
3636 check_hello_connection);
3638 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
3639 check_nonexistent_service_no_auto_start);
3641 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
3642 check_segfault_service_no_auto_start);
3644 check2_try_iterations (context, foo, "existent_service_no_auto_start",
3645 check_existent_service_no_auto_start);
3647 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
3648 check_nonexistent_service_auto_start);
3650 check2_try_iterations (context, foo, "segfault_service_auto_start",
3651 check_segfault_service_auto_start);
3653 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
3654 check_shell_fail_service_auto_start);
3657 /* Note: need to resolve some issues with the testing code in order to run
3658 * this in oom (handle that we sometimes don't get replies back from the bus
3659 * when oom happens, without blocking the test).
3661 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
3662 check_existent_service_auto_start);
3665 if (!check_existent_service_auto_start (context, foo))
3666 _dbus_assert_not_reached ("existent service auto start failed");
3668 if (!check_shell_service_success_auto_start (context, foo))
3669 _dbus_assert_not_reached ("shell success service auto start failed");
3671 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
3673 kill_client_connection_unchecked (foo);
3674 kill_client_connection_unchecked (bar);
3675 kill_client_connection_unchecked (baz);
3677 bus_context_unref (context);
3683 bus_dispatch_sha1_test (const DBusString *test_data_dir)
3685 BusContext *context;
3686 DBusConnection *foo;
3689 dbus_error_init (&error);
3691 /* Test SHA1 authentication */
3692 _dbus_verbose ("Testing SHA1 context\n");
3694 context = bus_context_new_test (test_data_dir,
3695 "valid-config-files/debug-allow-all-sha1.conf");
3696 if (context == NULL)
3699 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3701 _dbus_assert_not_reached ("could not alloc connection");
3703 if (!bus_setup_debug_client (foo))
3704 _dbus_assert_not_reached ("could not set up connection");
3706 spin_connection_until_authenticated (context, foo);
3708 if (!check_hello_message (context, foo))
3709 _dbus_assert_not_reached ("hello message failed");
3711 if (!check_add_match_all (context, foo))
3712 _dbus_assert_not_reached ("addmatch message failed");
3714 if (!check_no_leftovers (context))
3716 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
3717 _dbus_assert_not_reached ("initial connection setup failed");
3720 check1_try_iterations (context, "create_and_hello_sha1",
3721 check_hello_connection);
3723 kill_client_connection_unchecked (foo);
3725 bus_context_unref (context);
3730 #endif /* DBUS_BUILD_TESTS */