1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "connection.h"
31 #include "activation.h"
36 #include <dbus/dbus-internals.h>
39 #ifdef HAVE_UNIX_FD_PASSING
40 #include <dbus/dbus-sysdeps-unix.h>
44 /* This is hard-coded in the files in valid-config-files-*. We have to use
45 * the debug-pipe transport because the tests in this file require that
46 * dbus_connection_open_private() does not block. */
47 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
50 send_one_message (DBusConnection *connection,
52 DBusConnection *sender,
53 DBusConnection *addressed_recipient,
55 BusTransaction *transaction,
58 if (!bus_context_check_security_policy (context, transaction,
64 return TRUE; /* silently don't send it */
66 if (dbus_message_contains_unix_fds(message) &&
67 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
68 return TRUE; /* silently don't send it */
70 if (!bus_transaction_send (transaction,
82 bus_dispatch_matches (BusTransaction *transaction,
83 DBusConnection *sender,
84 DBusConnection *addressed_recipient,
89 BusConnections *connections;
91 BusMatchmaker *matchmaker;
95 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
97 /* sender and recipient can both be NULL for the bus driver,
98 * or for signals with no particular recipient
101 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
102 _dbus_assert (dbus_message_get_sender (message) != NULL);
104 context = bus_transaction_get_context (transaction);
106 /* First, send the message to the addressed_recipient, if there is one. */
107 if (addressed_recipient != NULL)
109 if (!bus_context_check_security_policy (context, transaction,
110 sender, addressed_recipient,
115 if (dbus_message_contains_unix_fds (message) &&
116 !dbus_connection_can_send_type (addressed_recipient,
119 dbus_set_error (error,
120 DBUS_ERROR_NOT_SUPPORTED,
121 "Tried to send message with Unix file descriptors"
122 "to a client that doesn't support that.");
126 /* Dispatch the message */
127 if (!bus_transaction_send (transaction, addressed_recipient, message))
134 /* Now dispatch to others who look interested in this message */
135 connections = bus_transaction_get_connections (transaction);
136 dbus_error_init (&tmp_error);
137 matchmaker = bus_context_get_matchmaker (context);
140 if (!bus_matchmaker_get_recipients (matchmaker, connections,
141 sender, addressed_recipient, message,
148 link = _dbus_list_get_first_link (&recipients);
151 DBusConnection *dest;
155 if (!send_one_message (dest, context, sender, addressed_recipient,
156 message, transaction, &tmp_error))
159 link = _dbus_list_get_next_link (&recipients, link);
162 _dbus_list_clear (&recipients);
164 if (dbus_error_is_set (&tmp_error))
166 dbus_move_error (&tmp_error, error);
173 static DBusHandlerResult
174 bus_dispatch (DBusConnection *connection,
175 DBusMessage *message)
177 const char *sender, *service_name;
179 BusTransaction *transaction;
181 DBusHandlerResult result;
182 DBusConnection *addressed_recipient;
184 result = DBUS_HANDLER_RESULT_HANDLED;
187 addressed_recipient = NULL;
188 dbus_error_init (&error);
190 context = bus_connection_get_context (connection);
191 _dbus_assert (context != NULL);
193 /* If we can't even allocate an OOM error, we just go to sleep
196 while (!bus_connection_preallocate_oom_error (connection))
197 _dbus_wait_for_memory ();
199 /* Ref connection in case we disconnect it at some point in here */
200 dbus_connection_ref (connection);
202 service_name = dbus_message_get_destination (message);
204 #ifdef DBUS_ENABLE_VERBOSE_MODE
206 const char *interface_name, *member_name, *error_name;
208 interface_name = dbus_message_get_interface (message);
209 member_name = dbus_message_get_member (message);
210 error_name = dbus_message_get_error_name (message);
212 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
213 interface_name ? interface_name : "(no interface)",
214 member_name ? member_name : "(no member)",
215 error_name ? error_name : "(no error name)",
216 service_name ? service_name : "peer");
218 #endif /* DBUS_ENABLE_VERBOSE_MODE */
220 /* If service_name is NULL, if it's a signal we send it to all
221 * connections with a match rule. If it's not a signal, there
222 * are some special cases here but mostly we just bail out.
224 if (service_name == NULL)
226 if (dbus_message_is_signal (message,
227 DBUS_INTERFACE_LOCAL,
230 bus_connection_disconnected (connection);
234 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
236 /* DBusConnection also handles some of these automatically, we leave
239 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
244 /* Create our transaction */
245 transaction = bus_transaction_new (context);
246 if (transaction == NULL)
248 BUS_SET_OOM (&error);
252 /* Assign a sender to the message */
253 if (bus_connection_is_active (connection))
255 sender = bus_connection_get_name (connection);
256 _dbus_assert (sender != NULL);
258 if (!dbus_message_set_sender (message, sender))
260 BUS_SET_OOM (&error);
264 /* We need to refetch the service name here, because
265 * dbus_message_set_sender can cause the header to be
266 * reallocated, and thus the service_name pointer will become
269 service_name = dbus_message_get_destination (message);
273 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
275 if (!bus_context_check_security_policy (context, transaction,
276 connection, NULL, NULL, message, &error))
278 _dbus_verbose ("Security policy rejected message\n");
282 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
283 if (!bus_driver_handle_message (connection, transaction, message, &error))
286 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
288 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
289 dbus_connection_close (connection);
292 else if (service_name != NULL) /* route to named service */
294 DBusString service_string;
296 BusRegistry *registry;
298 _dbus_assert (service_name != NULL);
300 registry = bus_connection_get_registry (connection);
302 _dbus_string_init_const (&service_string, service_name);
303 service = bus_registry_lookup (registry, &service_string);
305 if (service == NULL && dbus_message_get_auto_start (message))
307 BusActivation *activation;
308 /* We can't do the security policy check here, since the addressed
309 * recipient service doesn't exist yet. We do it before sending the
310 * message after the service has been created.
312 activation = bus_connection_get_activation (connection);
314 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
315 message, service_name, &error))
317 _DBUS_ASSERT_ERROR_IS_SET (&error);
318 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
324 else if (service == NULL)
326 dbus_set_error (&error,
327 DBUS_ERROR_NAME_HAS_NO_OWNER,
328 "Name \"%s\" does not exist",
334 addressed_recipient = bus_service_get_primary_owners_connection (service);
335 _dbus_assert (addressed_recipient != NULL);
339 /* Now send the message to its destination (or not, if
340 * addressed_recipient == NULL), and match it against other connections'
343 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
347 if (dbus_error_is_set (&error))
349 if (!dbus_connection_get_is_connected (connection))
351 /* If we disconnected it, we won't bother to send it any error
354 _dbus_verbose ("Not sending error to connection we disconnected\n");
356 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
358 bus_connection_send_oom_error (connection, message);
360 /* cancel transaction due to OOM */
361 if (transaction != NULL)
363 bus_transaction_cancel_and_free (transaction);
369 /* Try to send the real error, if no mem to do that, send
372 _dbus_assert (transaction != NULL);
373 if (!bus_transaction_send_error_reply (transaction, connection,
376 bus_connection_send_oom_error (connection, message);
378 /* cancel transaction due to OOM */
379 if (transaction != NULL)
381 bus_transaction_cancel_and_free (transaction);
388 dbus_error_free (&error);
391 if (transaction != NULL)
393 bus_transaction_execute_and_free (transaction);
396 dbus_connection_unref (connection);
401 static DBusHandlerResult
402 bus_dispatch_message_filter (DBusConnection *connection,
403 DBusMessage *message,
406 return bus_dispatch (connection, message);
410 bus_dispatch_add_connection (DBusConnection *connection)
412 if (!dbus_connection_add_filter (connection,
413 bus_dispatch_message_filter,
421 bus_dispatch_remove_connection (DBusConnection *connection)
423 /* Here we tell the bus driver that we want to get off. */
424 bus_driver_remove_connection (connection);
426 dbus_connection_remove_filter (connection,
427 bus_dispatch_message_filter,
431 #ifdef DBUS_BUILD_TESTS
435 /* This is used to know whether we need to block in order to finish
436 * sending a message, or whether the initial dbus_connection_send()
437 * already flushed the queue.
439 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
441 typedef dbus_bool_t (* Check1Func) (BusContext *context);
442 typedef dbus_bool_t (* Check2Func) (BusContext *context,
443 DBusConnection *connection);
445 static dbus_bool_t check_no_leftovers (BusContext *context);
448 block_connection_until_message_from_bus (BusContext *context,
449 DBusConnection *connection,
450 const char *what_is_expected)
452 _dbus_verbose ("expecting: %s\n", what_is_expected);
454 while (dbus_connection_get_dispatch_status (connection) ==
455 DBUS_DISPATCH_COMPLETE &&
456 dbus_connection_get_is_connected (connection))
458 bus_test_run_bus_loop (context, TRUE);
459 bus_test_run_clients_loop (FALSE);
464 spin_connection_until_authenticated (BusContext *context,
465 DBusConnection *connection)
467 _dbus_verbose ("Spinning to auth connection %p\n", connection);
468 while (!dbus_connection_get_is_authenticated (connection) &&
469 dbus_connection_get_is_connected (connection))
471 bus_test_run_bus_loop (context, FALSE);
472 bus_test_run_clients_loop (FALSE);
474 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
477 /* compensate for fact that pop_message() can return #NULL due to OOM */
479 pop_message_waiting_for_memory (DBusConnection *connection)
481 while (dbus_connection_get_dispatch_status (connection) ==
482 DBUS_DISPATCH_NEED_MEMORY)
483 _dbus_wait_for_memory ();
485 return dbus_connection_pop_message (connection);
489 borrow_message_waiting_for_memory (DBusConnection *connection)
491 while (dbus_connection_get_dispatch_status (connection) ==
492 DBUS_DISPATCH_NEED_MEMORY)
493 _dbus_wait_for_memory ();
495 return dbus_connection_borrow_message (connection);
499 warn_unexpected_real (DBusConnection *connection,
500 DBusMessage *message,
501 const char *expected,
502 const char *function,
506 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
508 dbus_message_get_interface (message) ?
509 dbus_message_get_interface (message) : "(unset)",
510 dbus_message_get_member (message) ?
511 dbus_message_get_member (message) : "(unset)",
512 dbus_message_get_error_name (message) ?
513 dbus_message_get_error_name (message) : "(unset)",
517 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
518 function, line, connection, expected);
521 #define warn_unexpected(connection, message, expected) \
522 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
525 verbose_message_received (DBusConnection *connection,
526 DBusMessage *message)
528 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
529 dbus_message_get_interface (message) ?
530 dbus_message_get_interface (message) : "(unset)",
531 dbus_message_get_member (message) ?
532 dbus_message_get_member (message) : "(unset)",
533 dbus_message_get_error_name (message) ?
534 dbus_message_get_error_name (message) : "(unset)",
547 ServiceInfoKind expected_kind;
548 const char *expected_service_name;
550 DBusConnection *skip_connection;
551 } CheckServiceOwnerChangedData;
554 check_service_owner_changed_foreach (DBusConnection *connection,
557 CheckServiceOwnerChangedData *d = data;
558 DBusMessage *message;
560 const char *service_name, *old_owner, *new_owner;
562 if (d->expected_kind == SERVICE_CREATED
563 && connection == d->skip_connection)
566 dbus_error_init (&error);
569 message = pop_message_waiting_for_memory (connection);
572 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
573 connection, "NameOwnerChanged");
576 else if (!dbus_message_is_signal (message,
580 warn_unexpected (connection, message, "NameOwnerChanged");
586 reget_service_info_data:
591 dbus_message_get_args (message, &error,
592 DBUS_TYPE_STRING, &service_name,
593 DBUS_TYPE_STRING, &old_owner,
594 DBUS_TYPE_STRING, &new_owner,
597 if (dbus_error_is_set (&error))
599 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
601 dbus_error_free (&error);
602 _dbus_wait_for_memory ();
603 goto reget_service_info_data;
607 _dbus_warn ("Did not get the expected arguments\n");
612 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
613 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
614 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
616 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
620 if (strcmp (service_name, d->expected_service_name) != 0)
622 _dbus_warn ("expected info on service %s, got info on %s\n",
623 d->expected_service_name,
628 if (*service_name == ':' && new_owner[0]
629 && strcmp (service_name, new_owner) != 0)
631 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
632 service_name, old_owner, new_owner);
640 dbus_error_free (&error);
643 dbus_message_unref (message);
650 kill_client_connection (BusContext *context,
651 DBusConnection *connection)
655 CheckServiceOwnerChangedData socd;
657 _dbus_verbose ("killing connection %p\n", connection);
659 s = dbus_bus_get_unique_name (connection);
660 _dbus_assert (s != NULL);
662 while ((base_service = _dbus_strdup (s)) == NULL)
663 _dbus_wait_for_memory ();
665 dbus_connection_ref (connection);
667 /* kick in the disconnect handler that unrefs the connection */
668 dbus_connection_close (connection);
670 bus_test_run_everything (context);
672 _dbus_assert (bus_test_client_listed (connection));
674 /* Run disconnect handler in test.c */
675 if (bus_connection_dispatch_one_message (connection))
676 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
678 _dbus_assert (!dbus_connection_get_is_connected (connection));
679 dbus_connection_unref (connection);
681 _dbus_assert (!bus_test_client_listed (connection));
683 socd.expected_kind = SERVICE_DELETED;
684 socd.expected_service_name = base_service;
686 socd.skip_connection = NULL;
688 bus_test_clients_foreach (check_service_owner_changed_foreach,
691 dbus_free (base_service);
694 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
696 if (!check_no_leftovers (context))
697 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
701 kill_client_connection_unchecked (DBusConnection *connection)
703 /* This kills the connection without expecting it to affect
704 * the rest of the bus.
706 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
708 dbus_connection_ref (connection);
709 dbus_connection_close (connection);
710 /* dispatching disconnect handler will unref once */
711 if (bus_connection_dispatch_one_message (connection))
712 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
714 _dbus_assert (!bus_test_client_listed (connection));
715 dbus_connection_unref (connection);
721 } CheckNoMessagesData;
724 check_no_messages_foreach (DBusConnection *connection,
727 CheckNoMessagesData *d = data;
728 DBusMessage *message;
730 message = pop_message_waiting_for_memory (connection);
733 warn_unexpected (connection, message, "no messages");
739 dbus_message_unref (message);
744 check_no_leftovers (BusContext *context)
746 CheckNoMessagesData nmd;
749 bus_test_clients_foreach (check_no_messages_foreach,
754 _dbus_verbose ("leftover message found\n");
761 /* returns TRUE if the correct thing happens,
762 * but the correct thing may include OOM errors.
765 check_hello_message (BusContext *context,
766 DBusConnection *connection)
768 DBusMessage *message;
769 DBusMessage *name_message;
770 dbus_uint32_t serial;
774 const char *acquired;
777 dbus_error_init (&error);
783 _dbus_verbose ("check_hello_message for %p\n", connection);
785 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
793 dbus_connection_ref (connection); /* because we may get disconnected */
795 if (!dbus_connection_send (connection, message, &serial))
797 dbus_message_unref (message);
798 dbus_connection_unref (connection);
802 _dbus_assert (dbus_message_has_signature (message, ""));
804 dbus_message_unref (message);
807 if (!dbus_connection_get_is_connected (connection))
809 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
811 dbus_connection_unref (connection);
816 /* send our message */
817 bus_test_run_clients_loop (SEND_PENDING (connection));
819 if (!dbus_connection_get_is_connected (connection))
821 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
823 dbus_connection_unref (connection);
828 block_connection_until_message_from_bus (context, connection, "reply to Hello");
830 if (!dbus_connection_get_is_connected (connection))
832 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
834 dbus_connection_unref (connection);
839 dbus_connection_unref (connection);
841 message = pop_message_waiting_for_memory (connection);
844 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
845 "Hello", serial, connection);
849 verbose_message_received (connection, message);
851 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
853 _dbus_warn ("Message has wrong sender %s\n",
854 dbus_message_get_sender (message) ?
855 dbus_message_get_sender (message) : "(none)");
859 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
861 if (dbus_message_is_error (message,
862 DBUS_ERROR_NO_MEMORY))
864 ; /* good, this is a valid response */
868 warn_unexpected (connection, message, "not this error");
875 CheckServiceOwnerChangedData socd;
877 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
879 ; /* good, expected */
883 warn_unexpected (connection, message, "method return for Hello");
888 retry_get_hello_name:
889 if (!dbus_message_get_args (message, &error,
890 DBUS_TYPE_STRING, &name,
893 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
895 _dbus_verbose ("no memory to get service name arg from hello\n");
896 dbus_error_free (&error);
897 _dbus_wait_for_memory ();
898 goto retry_get_hello_name;
902 _dbus_assert (dbus_error_is_set (&error));
903 _dbus_warn ("Did not get the expected single string argument to hello\n");
908 _dbus_verbose ("Got hello name: %s\n", name);
910 while (!dbus_bus_set_unique_name (connection, name))
911 _dbus_wait_for_memory ();
913 socd.expected_kind = SERVICE_CREATED;
914 socd.expected_service_name = name;
916 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
917 bus_test_clients_foreach (check_service_owner_changed_foreach,
923 name_message = message;
924 /* Client should also have gotten ServiceAcquired */
926 message = pop_message_waiting_for_memory (connection);
929 _dbus_warn ("Expecting %s, got nothing\n",
933 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
936 _dbus_warn ("Expecting %s, got smthg else\n",
941 retry_get_acquired_name:
942 if (!dbus_message_get_args (message, &error,
943 DBUS_TYPE_STRING, &acquired,
946 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
948 _dbus_verbose ("no memory to get service name arg from acquired\n");
949 dbus_error_free (&error);
950 _dbus_wait_for_memory ();
951 goto retry_get_acquired_name;
955 _dbus_assert (dbus_error_is_set (&error));
956 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
961 _dbus_verbose ("Got acquired name: %s\n", acquired);
963 if (strcmp (acquired, name) != 0)
965 _dbus_warn ("Acquired name is %s but expected %s\n",
972 if (!check_no_leftovers (context))
978 _dbus_verbose ("ending - retval = %d\n", retval);
980 dbus_error_free (&error);
983 dbus_message_unref (message);
986 dbus_message_unref (name_message);
991 /* returns TRUE if the correct thing happens,
992 * but the correct thing may include OOM errors.
995 check_double_hello_message (BusContext *context,
996 DBusConnection *connection)
998 DBusMessage *message;
999 dbus_uint32_t serial;
1004 dbus_error_init (&error);
1007 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1009 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1011 DBUS_INTERFACE_DBUS,
1014 if (message == NULL)
1017 if (!dbus_connection_send (connection, message, &serial))
1019 dbus_message_unref (message);
1023 dbus_message_unref (message);
1026 /* send our message */
1027 bus_test_run_clients_loop (SEND_PENDING (connection));
1029 dbus_connection_ref (connection); /* because we may get disconnected */
1030 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1032 if (!dbus_connection_get_is_connected (connection))
1034 _dbus_verbose ("connection was disconnected\n");
1036 dbus_connection_unref (connection);
1041 dbus_connection_unref (connection);
1043 message = pop_message_waiting_for_memory (connection);
1044 if (message == NULL)
1046 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1047 "Hello", serial, connection);
1051 verbose_message_received (connection, message);
1053 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1055 _dbus_warn ("Message has wrong sender %s\n",
1056 dbus_message_get_sender (message) ?
1057 dbus_message_get_sender (message) : "(none)");
1061 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1063 warn_unexpected (connection, message, "method return for Hello");
1067 if (!check_no_leftovers (context))
1073 dbus_error_free (&error);
1076 dbus_message_unref (message);
1081 /* returns TRUE if the correct thing happens,
1082 * but the correct thing may include OOM errors.
1085 check_get_connection_unix_user (BusContext *context,
1086 DBusConnection *connection)
1088 DBusMessage *message;
1089 dbus_uint32_t serial;
1092 const char *base_service_name;
1096 dbus_error_init (&error);
1099 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1101 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1103 DBUS_INTERFACE_DBUS,
1104 "GetConnectionUnixUser");
1106 if (message == NULL)
1109 base_service_name = dbus_bus_get_unique_name (connection);
1111 if (!dbus_message_append_args (message,
1112 DBUS_TYPE_STRING, &base_service_name,
1115 dbus_message_unref (message);
1119 if (!dbus_connection_send (connection, message, &serial))
1121 dbus_message_unref (message);
1125 /* send our message */
1126 bus_test_run_clients_loop (SEND_PENDING (connection));
1128 dbus_message_unref (message);
1131 dbus_connection_ref (connection); /* because we may get disconnected */
1132 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1134 if (!dbus_connection_get_is_connected (connection))
1136 _dbus_verbose ("connection was disconnected\n");
1138 dbus_connection_unref (connection);
1143 dbus_connection_unref (connection);
1145 message = pop_message_waiting_for_memory (connection);
1146 if (message == NULL)
1148 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1149 "GetConnectionUnixUser", serial, connection);
1153 verbose_message_received (connection, message);
1155 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1157 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1159 ; /* good, this is a valid response */
1163 warn_unexpected (connection, message, "not this error");
1170 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1172 ; /* good, expected */
1176 warn_unexpected (connection, message,
1177 "method_return for GetConnectionUnixUser");
1184 if (!dbus_message_get_args (message, &error,
1185 DBUS_TYPE_UINT32, &uid,
1188 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1190 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1191 dbus_error_free (&error);
1192 _dbus_wait_for_memory ();
1193 goto retry_get_property;
1197 _dbus_assert (dbus_error_is_set (&error));
1198 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1204 if (!check_no_leftovers (context))
1210 dbus_error_free (&error);
1213 dbus_message_unref (message);
1218 /* returns TRUE if the correct thing happens,
1219 * but the correct thing may include OOM errors.
1222 check_get_connection_unix_process_id (BusContext *context,
1223 DBusConnection *connection)
1225 DBusMessage *message;
1226 dbus_uint32_t serial;
1229 const char *base_service_name;
1233 dbus_error_init (&error);
1236 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1238 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1240 DBUS_INTERFACE_DBUS,
1241 "GetConnectionUnixProcessID");
1243 if (message == NULL)
1246 base_service_name = dbus_bus_get_unique_name (connection);
1248 if (!dbus_message_append_args (message,
1249 DBUS_TYPE_STRING, &base_service_name,
1252 dbus_message_unref (message);
1256 if (!dbus_connection_send (connection, message, &serial))
1258 dbus_message_unref (message);
1262 /* send our message */
1263 bus_test_run_clients_loop (SEND_PENDING (connection));
1265 dbus_message_unref (message);
1268 dbus_connection_ref (connection); /* because we may get disconnected */
1269 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1271 if (!dbus_connection_get_is_connected (connection))
1273 _dbus_verbose ("connection was disconnected\n");
1275 dbus_connection_unref (connection);
1280 dbus_connection_unref (connection);
1282 message = pop_message_waiting_for_memory (connection);
1283 if (message == NULL)
1285 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1286 "GetConnectionUnixProcessID", serial, connection);
1290 verbose_message_received (connection, message);
1292 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1294 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1296 ; /* good, this is a valid response */
1299 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1301 /* We are expecting this error, since we know in the test suite we aren't
1302 * talking to a client running on UNIX
1304 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1309 warn_unexpected (connection, message, "not this error");
1317 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1320 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1322 ; /* good, expected */
1326 warn_unexpected (connection, message,
1327 "method_return for GetConnectionUnixProcessID");
1334 if (!dbus_message_get_args (message, &error,
1335 DBUS_TYPE_UINT32, &pid,
1338 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1340 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1341 dbus_error_free (&error);
1342 _dbus_wait_for_memory ();
1343 goto retry_get_property;
1347 _dbus_assert (dbus_error_is_set (&error));
1348 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1354 /* test if returned pid is the same as our own pid
1356 * @todo It would probably be good to restructure the tests
1357 * in a way so our parent is the bus that we're testing
1358 * cause then we can test that the pid returned matches
1361 if (pid != (dbus_uint32_t) _dbus_getpid ())
1363 _dbus_assert (dbus_error_is_set (&error));
1364 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1368 #endif /* !DBUS_WIN */
1371 if (!check_no_leftovers (context))
1377 dbus_error_free (&error);
1380 dbus_message_unref (message);
1385 /* returns TRUE if the correct thing happens,
1386 * but the correct thing may include OOM errors.
1389 check_add_match_all (BusContext *context,
1390 DBusConnection *connection)
1392 DBusMessage *message;
1394 dbus_uint32_t serial;
1396 const char *empty = "";
1399 dbus_error_init (&error);
1402 _dbus_verbose ("check_add_match_all for %p\n", connection);
1404 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1406 DBUS_INTERFACE_DBUS,
1409 if (message == NULL)
1412 /* empty string match rule matches everything */
1413 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1416 dbus_message_unref (message);
1420 if (!dbus_connection_send (connection, message, &serial))
1422 dbus_message_unref (message);
1426 dbus_message_unref (message);
1429 dbus_connection_ref (connection); /* because we may get disconnected */
1431 /* send our message */
1432 bus_test_run_clients_loop (SEND_PENDING (connection));
1434 if (!dbus_connection_get_is_connected (connection))
1436 _dbus_verbose ("connection was disconnected\n");
1438 dbus_connection_unref (connection);
1443 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1445 if (!dbus_connection_get_is_connected (connection))
1447 _dbus_verbose ("connection was disconnected\n");
1449 dbus_connection_unref (connection);
1454 dbus_connection_unref (connection);
1456 message = pop_message_waiting_for_memory (connection);
1457 if (message == NULL)
1459 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1460 "AddMatch", serial, connection);
1464 verbose_message_received (connection, message);
1466 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1468 _dbus_warn ("Message has wrong sender %s\n",
1469 dbus_message_get_sender (message) ?
1470 dbus_message_get_sender (message) : "(none)");
1474 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1476 if (dbus_message_is_error (message,
1477 DBUS_ERROR_NO_MEMORY))
1479 ; /* good, this is a valid response */
1483 warn_unexpected (connection, message, "not this error");
1490 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1492 ; /* good, expected */
1493 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1497 warn_unexpected (connection, message, "method return for AddMatch");
1503 if (!check_no_leftovers (context))
1509 dbus_error_free (&error);
1512 dbus_message_unref (message);
1517 /* returns TRUE if the correct thing happens,
1518 * but the correct thing may include OOM errors.
1521 check_hello_connection (BusContext *context)
1523 DBusConnection *connection;
1526 dbus_error_init (&error);
1528 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1529 if (connection == NULL)
1531 _DBUS_ASSERT_ERROR_IS_SET (&error);
1532 dbus_error_free (&error);
1536 if (!bus_setup_debug_client (connection))
1538 dbus_connection_close (connection);
1539 dbus_connection_unref (connection);
1543 spin_connection_until_authenticated (context, connection);
1545 if (!check_hello_message (context, connection))
1548 if (dbus_bus_get_unique_name (connection) == NULL)
1550 /* We didn't successfully register, so we can't
1551 * do the usual kill_client_connection() checks
1553 kill_client_connection_unchecked (connection);
1557 if (!check_add_match_all (context, connection))
1560 kill_client_connection (context, connection);
1566 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1568 /* returns TRUE if the correct thing happens,
1569 * but the correct thing may include OOM errors.
1572 check_nonexistent_service_no_auto_start (BusContext *context,
1573 DBusConnection *connection)
1575 DBusMessage *message;
1576 dbus_uint32_t serial;
1578 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1579 dbus_uint32_t flags;
1581 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1583 DBUS_INTERFACE_DBUS,
1584 "StartServiceByName");
1586 if (message == NULL)
1589 dbus_message_set_auto_start (message, FALSE);
1592 if (!dbus_message_append_args (message,
1593 DBUS_TYPE_STRING, &nonexistent,
1594 DBUS_TYPE_UINT32, &flags,
1597 dbus_message_unref (message);
1601 if (!dbus_connection_send (connection, message, &serial))
1603 dbus_message_unref (message);
1607 dbus_message_unref (message);
1610 bus_test_run_everything (context);
1611 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1612 bus_test_run_everything (context);
1614 if (!dbus_connection_get_is_connected (connection))
1616 _dbus_verbose ("connection was disconnected\n");
1622 message = pop_message_waiting_for_memory (connection);
1623 if (message == NULL)
1625 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1626 "StartServiceByName", serial, connection);
1630 verbose_message_received (connection, message);
1632 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1634 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1636 _dbus_warn ("Message has wrong sender %s\n",
1637 dbus_message_get_sender (message) ?
1638 dbus_message_get_sender (message) : "(none)");
1642 if (dbus_message_is_error (message,
1643 DBUS_ERROR_NO_MEMORY))
1645 ; /* good, this is a valid response */
1647 else if (dbus_message_is_error (message,
1648 DBUS_ERROR_SERVICE_UNKNOWN))
1650 ; /* good, this is expected also */
1654 warn_unexpected (connection, message, "not this error");
1660 _dbus_warn ("Did not expect to successfully activate %s\n",
1661 NONEXISTENT_SERVICE_NAME);
1669 dbus_message_unref (message);
1674 /* returns TRUE if the correct thing happens,
1675 * but the correct thing may include OOM errors.
1678 check_nonexistent_service_auto_start (BusContext *context,
1679 DBusConnection *connection)
1681 DBusMessage *message;
1682 dbus_uint32_t serial;
1685 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1686 "/org/freedesktop/TestSuite",
1687 "org.freedesktop.TestSuite",
1690 if (message == NULL)
1693 if (!dbus_connection_send (connection, message, &serial))
1695 dbus_message_unref (message);
1699 dbus_message_unref (message);
1702 bus_test_run_everything (context);
1703 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1704 bus_test_run_everything (context);
1706 if (!dbus_connection_get_is_connected (connection))
1708 _dbus_verbose ("connection was disconnected\n");
1714 message = pop_message_waiting_for_memory (connection);
1716 if (message == NULL)
1718 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1719 "Echo message (auto activation)", serial, connection);
1723 verbose_message_received (connection, message);
1725 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1727 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1729 _dbus_warn ("Message has wrong sender %s\n",
1730 dbus_message_get_sender (message) ?
1731 dbus_message_get_sender (message) : "(none)");
1735 if (dbus_message_is_error (message,
1736 DBUS_ERROR_NO_MEMORY))
1738 ; /* good, this is a valid response */
1740 else if (dbus_message_is_error (message,
1741 DBUS_ERROR_SERVICE_UNKNOWN))
1743 ; /* good, this is expected also */
1747 warn_unexpected (connection, message, "not this error");
1753 _dbus_warn ("Did not expect to successfully activate %s\n",
1754 NONEXISTENT_SERVICE_NAME);
1762 dbus_message_unref (message);
1768 check_base_service_activated (BusContext *context,
1769 DBusConnection *connection,
1770 DBusMessage *initial_message,
1771 const char **base_service_p)
1773 DBusMessage *message;
1776 const char *base_service, *base_service_from_bus, *old_owner;
1780 dbus_error_init (&error);
1781 base_service = NULL;
1783 base_service_from_bus = NULL;
1785 message = initial_message;
1786 dbus_message_ref (message);
1788 if (dbus_message_is_signal (message,
1789 DBUS_INTERFACE_DBUS,
1790 "NameOwnerChanged"))
1792 CheckServiceOwnerChangedData socd;
1794 reget_service_name_arg:
1795 base_service = NULL;
1797 base_service_from_bus = NULL;
1799 if (!dbus_message_get_args (message, &error,
1800 DBUS_TYPE_STRING, &base_service,
1801 DBUS_TYPE_STRING, &old_owner,
1802 DBUS_TYPE_STRING, &base_service_from_bus,
1805 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1807 dbus_error_free (&error);
1808 _dbus_wait_for_memory ();
1809 goto reget_service_name_arg;
1813 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1814 "NameOwnerChanged (creation)",
1820 if (*base_service != ':')
1822 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1827 if (strcmp (base_service, base_service_from_bus) != 0)
1829 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1830 base_service, base_service_from_bus);
1836 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1841 socd.expected_kind = SERVICE_CREATED;
1842 socd.expected_service_name = base_service;
1843 socd.failed = FALSE;
1844 socd.skip_connection = connection;
1845 bus_test_clients_foreach (check_service_owner_changed_foreach,
1853 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1859 *base_service_p = base_service;
1865 dbus_message_unref (message);
1866 dbus_error_free (&error);
1872 check_service_activated (BusContext *context,
1873 DBusConnection *connection,
1874 const char *activated_name,
1875 const char *base_service_name,
1876 DBusMessage *initial_message)
1878 DBusMessage *message;
1881 dbus_uint32_t activation_result;
1885 dbus_error_init (&error);
1887 message = initial_message;
1888 dbus_message_ref (message);
1890 if (dbus_message_is_signal (message,
1891 DBUS_INTERFACE_DBUS,
1892 "NameOwnerChanged"))
1894 CheckServiceOwnerChangedData socd;
1895 const char *service_name, *base_service_from_bus, *old_owner;
1897 reget_service_name_arg:
1898 service_name = NULL;
1900 base_service_from_bus = NULL;
1902 if (!dbus_message_get_args (message, &error,
1903 DBUS_TYPE_STRING, &service_name,
1904 DBUS_TYPE_STRING, &old_owner,
1905 DBUS_TYPE_STRING, &base_service_from_bus,
1908 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1910 dbus_error_free (&error);
1911 _dbus_wait_for_memory ();
1912 goto reget_service_name_arg;
1916 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1917 "NameOwnerChanged (creation)",
1923 if (strcmp (service_name, activated_name) != 0)
1925 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1926 activated_name, service_name);
1930 if (strcmp (base_service_name, base_service_from_bus) != 0)
1932 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1933 base_service_from_bus, base_service_name);
1939 _dbus_warn ("expected a %s, got a %s\n",
1940 "NameOwnerChanged (creation)",
1941 "NameOwnerChanged (change)");
1945 socd.expected_kind = SERVICE_CREATED;
1946 socd.skip_connection = connection;
1947 socd.failed = FALSE;
1948 socd.expected_service_name = service_name;
1949 bus_test_clients_foreach (check_service_owner_changed_foreach,
1955 dbus_message_unref (message);
1956 service_name = NULL;
1958 base_service_from_bus = NULL;
1960 message = pop_message_waiting_for_memory (connection);
1961 if (message == NULL)
1963 _dbus_warn ("Expected a reply to %s, got nothing\n",
1964 "StartServiceByName");
1970 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1975 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1977 warn_unexpected (connection, message, "reply to StartServiceByName");
1982 activation_result = 0;
1983 if (!dbus_message_get_args (message, &error,
1984 DBUS_TYPE_UINT32, &activation_result,
1987 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1989 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1990 "StartServiceByName", error.message);
1994 dbus_error_free (&error);
1998 if (activation_result == DBUS_START_REPLY_SUCCESS)
2000 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2004 _dbus_warn ("Activation result was %u, no good.\n",
2010 dbus_message_unref (message);
2013 if (!check_no_leftovers (context))
2015 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2023 dbus_message_unref (message);
2024 dbus_error_free (&error);
2030 check_service_auto_activated (BusContext *context,
2031 DBusConnection *connection,
2032 const char *activated_name,
2033 const char *base_service_name,
2034 DBusMessage *initial_message)
2036 DBusMessage *message;
2042 dbus_error_init (&error);
2044 message = initial_message;
2045 dbus_message_ref (message);
2047 if (dbus_message_is_signal (message,
2048 DBUS_INTERFACE_DBUS,
2049 "NameOwnerChanged"))
2051 const char *service_name;
2052 CheckServiceOwnerChangedData socd;
2054 reget_service_name_arg:
2055 if (!dbus_message_get_args (message, &error,
2056 DBUS_TYPE_STRING, &service_name,
2059 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2061 dbus_error_free (&error);
2062 _dbus_wait_for_memory ();
2063 goto reget_service_name_arg;
2067 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2070 dbus_error_free (&error);
2075 if (strcmp (service_name, activated_name) != 0)
2077 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2078 activated_name, service_name);
2082 socd.expected_kind = SERVICE_CREATED;
2083 socd.expected_service_name = service_name;
2084 socd.failed = FALSE;
2085 socd.skip_connection = connection;
2086 bus_test_clients_foreach (check_service_owner_changed_foreach,
2092 /* Note that this differs from regular activation in that we don't get a
2093 * reply to ActivateService here.
2096 dbus_message_unref (message);
2098 service_name = NULL;
2102 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2111 dbus_message_unref (message);
2117 check_service_deactivated (BusContext *context,
2118 DBusConnection *connection,
2119 const char *activated_name,
2120 const char *base_service)
2123 CheckServiceOwnerChangedData socd;
2127 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2128 * service and the activated_name. The base service
2129 * notification is required to come last.
2131 socd.expected_kind = SERVICE_DELETED;
2132 socd.expected_service_name = activated_name;
2133 socd.failed = FALSE;
2134 socd.skip_connection = NULL;
2135 bus_test_clients_foreach (check_service_owner_changed_foreach,
2141 socd.expected_kind = SERVICE_DELETED;
2142 socd.expected_service_name = base_service;
2143 socd.failed = FALSE;
2144 socd.skip_connection = NULL;
2145 bus_test_clients_foreach (check_service_owner_changed_foreach,
2158 check_send_exit_to_service (BusContext *context,
2159 DBusConnection *connection,
2160 const char *service_name,
2161 const char *base_service)
2163 dbus_bool_t got_error;
2164 DBusMessage *message;
2165 dbus_uint32_t serial;
2168 _dbus_verbose ("Sending exit message to the test service\n");
2172 /* Kill off the test service by sending it a quit message */
2173 message = dbus_message_new_method_call (service_name,
2174 "/org/freedesktop/TestSuite",
2175 "org.freedesktop.TestSuite",
2178 if (message == NULL)
2180 /* Do this again; we still need the service to exit... */
2181 if (!check_send_exit_to_service (context, connection,
2182 service_name, base_service))
2188 if (!dbus_connection_send (connection, message, &serial))
2190 dbus_message_unref (message);
2192 /* Do this again; we still need the service to exit... */
2193 if (!check_send_exit_to_service (context, connection,
2194 service_name, base_service))
2200 dbus_message_unref (message);
2204 bus_test_run_clients_loop (SEND_PENDING (connection));
2206 /* read it in and write it out to test service */
2207 bus_test_run_bus_loop (context, FALSE);
2209 /* see if we got an error during message bus dispatching */
2210 bus_test_run_clients_loop (FALSE);
2211 message = borrow_message_waiting_for_memory (connection);
2212 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2215 dbus_connection_return_message (connection, message);
2221 /* If no error, wait for the test service to exit */
2222 block_connection_until_message_from_bus (context, connection, "test service to exit");
2224 bus_test_run_everything (context);
2229 message = pop_message_waiting_for_memory (connection);
2230 _dbus_assert (message != NULL);
2232 if (dbus_message_get_reply_serial (message) != serial)
2234 warn_unexpected (connection, message,
2235 "error with the correct reply serial");
2239 if (!dbus_message_is_error (message,
2240 DBUS_ERROR_NO_MEMORY))
2242 warn_unexpected (connection, message,
2243 "a no memory error from asking test service to exit");
2247 _dbus_verbose ("Got error %s when asking test service to exit\n",
2248 dbus_message_get_error_name (message));
2250 /* Do this again; we still need the service to exit... */
2251 if (!check_send_exit_to_service (context, connection,
2252 service_name, base_service))
2257 if (!check_service_deactivated (context, connection,
2258 service_name, base_service))
2261 /* Should now have a NoReply error from the Exit() method
2262 * call; it should have come after all the deactivation
2265 message = pop_message_waiting_for_memory (connection);
2267 if (message == NULL)
2269 warn_unexpected (connection, NULL,
2270 "reply to Exit() method call");
2273 if (!dbus_message_is_error (message,
2274 DBUS_ERROR_NO_REPLY))
2276 warn_unexpected (connection, message,
2277 "NoReply error from Exit() method call");
2281 if (dbus_message_get_reply_serial (message) != serial)
2283 warn_unexpected (connection, message,
2284 "error with the correct reply serial");
2288 _dbus_verbose ("Got error %s after test service exited\n",
2289 dbus_message_get_error_name (message));
2291 if (!check_no_leftovers (context))
2293 _dbus_warn ("Messages were left over after %s\n",
2294 _DBUS_FUNCTION_NAME);
2303 dbus_message_unref (message);
2309 check_got_error (BusContext *context,
2310 DBusConnection *connection,
2311 const char *first_error_name,
2314 DBusMessage *message;
2317 dbus_bool_t error_found;
2318 const char *error_name;
2322 message = pop_message_waiting_for_memory (connection);
2323 if (message == NULL)
2325 _dbus_warn ("Did not get an expected error\n");
2329 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2331 warn_unexpected (connection, message, "an error");
2336 error_found = FALSE;
2338 va_start (ap, first_error_name);
2339 error_name = first_error_name;
2340 while (error_name != NULL)
2342 if (dbus_message_is_error (message, error_name))
2347 error_name = va_arg (ap, char*);
2353 _dbus_warn ("Expected error %s or other, got %s instead\n",
2355 dbus_message_get_error_name (message));
2363 dbus_message_unref (message);
2370 GOT_SERVICE_CREATED,
2371 GOT_SERVICE_DELETED,
2376 static GotServiceInfo
2377 check_got_service_info (DBusMessage *message)
2379 GotServiceInfo message_kind;
2381 if (dbus_message_is_signal (message,
2382 DBUS_INTERFACE_DBUS,
2383 "NameOwnerChanged"))
2386 const char *service_name, *old_owner, *new_owner;
2387 dbus_error_init (&error);
2389 reget_service_info_data:
2390 service_name = NULL;
2394 dbus_message_get_args (message, &error,
2395 DBUS_TYPE_STRING, &service_name,
2396 DBUS_TYPE_STRING, &old_owner,
2397 DBUS_TYPE_STRING, &new_owner,
2399 if (dbus_error_is_set (&error))
2401 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2403 dbus_error_free (&error);
2404 goto reget_service_info_data;
2408 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2409 message_kind = GOT_SOMETHING_ELSE;
2412 else if (!old_owner[0])
2413 message_kind = GOT_SERVICE_CREATED;
2414 else if (!new_owner[0])
2415 message_kind = GOT_SERVICE_DELETED;
2417 message_kind = GOT_SOMETHING_ELSE;
2419 dbus_error_free (&error);
2421 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2422 message_kind = GOT_ERROR;
2424 message_kind = GOT_SOMETHING_ELSE;
2426 return message_kind;
2429 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2431 /* returns TRUE if the correct thing happens,
2432 * but the correct thing may include OOM errors.
2435 check_existent_service_no_auto_start (BusContext *context,
2436 DBusConnection *connection)
2438 DBusMessage *message;
2439 DBusMessage *base_service_message;
2440 const char *base_service;
2441 dbus_uint32_t serial;
2443 const char *existent = EXISTENT_SERVICE_NAME;
2444 dbus_uint32_t flags;
2446 base_service_message = NULL;
2448 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2450 DBUS_INTERFACE_DBUS,
2451 "StartServiceByName");
2453 if (message == NULL)
2456 dbus_message_set_auto_start (message, FALSE);
2459 if (!dbus_message_append_args (message,
2460 DBUS_TYPE_STRING, &existent,
2461 DBUS_TYPE_UINT32, &flags,
2464 dbus_message_unref (message);
2468 if (!dbus_connection_send (connection, message, &serial))
2470 dbus_message_unref (message);
2474 dbus_message_unref (message);
2477 bus_test_run_everything (context);
2479 /* now wait for the message bus to hear back from the activated
2482 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2484 bus_test_run_everything (context);
2486 if (!dbus_connection_get_is_connected (connection))
2488 _dbus_verbose ("connection was disconnected\n");
2494 message = pop_message_waiting_for_memory (connection);
2495 if (message == NULL)
2497 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2498 "StartServiceByName", serial, connection);
2502 verbose_message_received (connection, message);
2503 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2505 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2507 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2509 _dbus_warn ("Message has wrong sender %s\n",
2510 dbus_message_get_sender (message) ?
2511 dbus_message_get_sender (message) : "(none)");
2515 if (dbus_message_is_error (message,
2516 DBUS_ERROR_NO_MEMORY))
2518 ; /* good, this is a valid response */
2520 else if (dbus_message_is_error (message,
2521 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2522 dbus_message_is_error (message,
2523 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2524 dbus_message_is_error (message,
2525 DBUS_ERROR_SPAWN_EXEC_FAILED))
2527 ; /* good, this is expected also */
2531 _dbus_warn ("Did not expect error %s\n",
2532 dbus_message_get_error_name (message));
2538 GotServiceInfo message_kind;
2540 if (!check_base_service_activated (context, connection,
2541 message, &base_service))
2544 base_service_message = message;
2547 /* We may need to block here for the test service to exit or finish up */
2548 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2550 message = dbus_connection_borrow_message (connection);
2551 if (message == NULL)
2553 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2557 message_kind = check_got_service_info (message);
2559 dbus_connection_return_message (connection, message);
2562 switch (message_kind)
2564 case GOT_SOMETHING_ELSE:
2565 _dbus_warn ("Unexpected message after ActivateService "
2566 "(should be an error or a service announcement");
2570 if (!check_got_error (context, connection,
2571 DBUS_ERROR_SPAWN_CHILD_EXITED,
2572 DBUS_ERROR_NO_MEMORY,
2575 /* A service deleted should be coming along now after this error.
2576 * We can also get the error *after* the service deleted.
2581 case GOT_SERVICE_DELETED:
2583 /* The service started up and got a base address, but then
2584 * failed to register under EXISTENT_SERVICE_NAME
2586 CheckServiceOwnerChangedData socd;
2588 socd.expected_kind = SERVICE_DELETED;
2589 socd.expected_service_name = base_service;
2590 socd.failed = FALSE;
2591 socd.skip_connection = NULL;
2593 bus_test_clients_foreach (check_service_owner_changed_foreach,
2599 /* Now we should get an error about the service exiting
2600 * if we didn't get it before.
2602 if (message_kind != GOT_ERROR)
2604 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2606 /* and process everything again */
2607 bus_test_run_everything (context);
2609 if (!check_got_error (context, connection,
2610 DBUS_ERROR_SPAWN_CHILD_EXITED,
2611 DBUS_ERROR_NO_MEMORY,
2618 case GOT_SERVICE_CREATED:
2619 message = pop_message_waiting_for_memory (connection);
2620 if (message == NULL)
2622 _dbus_warn ("Failed to pop message we just put back! "
2623 "should have been a NameOwnerChanged (creation)\n");
2627 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2628 base_service, message))
2631 dbus_message_unref (message);
2634 if (!check_no_leftovers (context))
2636 _dbus_warn ("Messages were left over after successful activation\n");
2640 if (!check_send_exit_to_service (context, connection,
2641 EXISTENT_SERVICE_NAME, base_service))
2652 dbus_message_unref (message);
2654 if (base_service_message)
2655 dbus_message_unref (base_service_message);
2660 #ifndef DBUS_WIN_FIXME
2661 /* returns TRUE if the correct thing happens,
2662 * but the correct thing may include OOM errors.
2665 check_segfault_service_no_auto_start (BusContext *context,
2666 DBusConnection *connection)
2668 DBusMessage *message;
2669 dbus_uint32_t serial;
2671 const char *segv_service;
2672 dbus_uint32_t flags;
2674 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2676 DBUS_INTERFACE_DBUS,
2677 "StartServiceByName");
2679 if (message == NULL)
2682 dbus_message_set_auto_start (message, FALSE);
2684 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2686 if (!dbus_message_append_args (message,
2687 DBUS_TYPE_STRING, &segv_service,
2688 DBUS_TYPE_UINT32, &flags,
2691 dbus_message_unref (message);
2695 if (!dbus_connection_send (connection, message, &serial))
2697 dbus_message_unref (message);
2701 dbus_message_unref (message);
2704 bus_test_run_everything (context);
2705 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2706 bus_test_run_everything (context);
2708 if (!dbus_connection_get_is_connected (connection))
2710 _dbus_verbose ("connection was disconnected\n");
2716 message = pop_message_waiting_for_memory (connection);
2717 if (message == NULL)
2719 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2720 "StartServiceByName", serial, connection);
2724 verbose_message_received (connection, message);
2726 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2728 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2730 _dbus_warn ("Message has wrong sender %s\n",
2731 dbus_message_get_sender (message) ?
2732 dbus_message_get_sender (message) : "(none)");
2736 if (dbus_message_is_error (message,
2737 DBUS_ERROR_NO_MEMORY))
2739 ; /* good, this is a valid response */
2741 else if (dbus_message_is_error (message,
2744 const char *servicehelper;
2745 servicehelper = bus_context_get_servicehelper (context);
2746 /* make sure this only happens with the launch helper */
2747 _dbus_assert (servicehelper != NULL);
2749 else if (dbus_message_is_error (message,
2750 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2752 ; /* good, this is expected also */
2756 warn_unexpected (connection, message, "not this error");
2763 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2771 dbus_message_unref (message);
2777 /* returns TRUE if the correct thing happens,
2778 * but the correct thing may include OOM errors.
2781 check_segfault_service_auto_start (BusContext *context,
2782 DBusConnection *connection)
2784 DBusMessage *message;
2785 dbus_uint32_t serial;
2788 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2789 "/org/freedesktop/TestSuite",
2790 "org.freedesktop.TestSuite",
2793 if (message == NULL)
2796 if (!dbus_connection_send (connection, message, &serial))
2798 dbus_message_unref (message);
2802 dbus_message_unref (message);
2805 bus_test_run_everything (context);
2806 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2807 bus_test_run_everything (context);
2809 if (!dbus_connection_get_is_connected (connection))
2811 _dbus_verbose ("connection was disconnected\n");
2817 message = pop_message_waiting_for_memory (connection);
2818 if (message == NULL)
2820 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2821 "Echo message (auto activation)", serial, connection);
2825 verbose_message_received (connection, message);
2827 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2829 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2831 _dbus_warn ("Message has wrong sender %s\n",
2832 dbus_message_get_sender (message) ?
2833 dbus_message_get_sender (message) : "(none)");
2837 if (dbus_message_is_error (message,
2838 DBUS_ERROR_NO_MEMORY))
2840 ; /* good, this is a valid response */
2842 else if (dbus_message_is_error (message,
2843 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2845 ; /* good, this is expected also */
2849 warn_unexpected (connection, message, "not this error");
2856 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2864 dbus_message_unref (message);
2870 #define TEST_ECHO_MESSAGE "Test echo message"
2871 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2873 /* returns TRUE if the correct thing happens,
2874 * but the correct thing may include OOM errors.
2877 check_existent_hello_from_self (BusContext *context,
2878 DBusConnection *connection)
2880 DBusMessage *message;
2881 dbus_uint32_t serial;
2884 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2885 "/org/freedesktop/TestSuite",
2886 "org.freedesktop.TestSuite",
2887 "RunHelloFromSelf");
2889 if (message == NULL)
2892 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2893 if (!dbus_message_append_args (message,
2894 DBUS_TYPE_STRING, &text,
2897 dbus_message_unref (message);
2901 if (!dbus_connection_send (connection, message, &serial))
2903 dbus_message_unref (message);
2907 dbus_message_unref (message);
2910 bus_test_run_everything (context);
2912 /* Note: if this test is run in OOM mode, it will block when the bus
2913 * doesn't send a reply due to OOM.
2915 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2917 message = pop_message_waiting_for_memory (connection);
2918 if (message == NULL)
2920 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2924 if (dbus_message_get_reply_serial (message) != serial)
2926 _dbus_warn ("Wrong reply serial\n");
2927 dbus_message_unref (message);
2931 dbus_message_unref (message);
2937 /* returns TRUE if the correct thing happens,
2938 * but the correct thing may include OOM errors.
2941 check_existent_ping (BusContext *context,
2942 DBusConnection *connection)
2944 DBusMessage *message;
2945 dbus_uint32_t serial;
2946 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2947 "/org/freedesktop/TestSuite",
2948 "org.freedesktop.DBus.Peer",
2951 if (message == NULL)
2954 if (!dbus_connection_send (connection, message, &serial))
2956 dbus_message_unref (message);
2960 dbus_message_unref (message);
2963 bus_test_run_everything (context);
2965 /* Note: if this test is run in OOM mode, it will block when the bus
2966 * doesn't send a reply due to OOM.
2968 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2970 message = pop_message_waiting_for_memory (connection);
2971 if (message == NULL)
2973 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2977 if (dbus_message_get_reply_serial (message) != serial)
2979 _dbus_warn ("Wrong reply serial\n");
2980 dbus_message_unref (message);
2984 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2986 _dbus_warn ("Unexpected message return during Ping\n");
2987 dbus_message_unref (message);
2991 dbus_message_unref (message);
2997 /* returns TRUE if the correct thing happens,
2998 * but the correct thing may include OOM errors.
3001 check_existent_get_machine_id (BusContext *context,
3002 DBusConnection *connection)
3004 DBusMessage *message;
3005 dbus_uint32_t serial;
3006 const char *machine_id;
3008 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3009 "/org/freedesktop/TestSuite",
3010 "org.freedesktop.DBus.Peer",
3013 if (message == NULL)
3016 if (!dbus_connection_send (connection, message, &serial))
3018 dbus_message_unref (message);
3022 dbus_message_unref (message);
3025 bus_test_run_everything (context);
3027 /* Note: if this test is run in OOM mode, it will block when the bus
3028 * doesn't send a reply due to OOM.
3030 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3032 message = pop_message_waiting_for_memory (connection);
3033 if (message == NULL)
3035 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3039 if (dbus_message_get_reply_serial (message) != serial)
3041 _dbus_warn ("Wrong reply serial\n");
3042 dbus_message_unref (message);
3046 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3048 _dbus_warn ("Unexpected message return during GetMachineId\n");
3049 dbus_message_unref (message);
3054 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3056 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3057 dbus_message_unref (message);
3061 if (machine_id == NULL || strlen (machine_id) != 32)
3063 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3064 dbus_message_unref (message);
3068 /* We can't check that the machine id is correct because during make check it is
3069 * just made up for each process separately
3072 dbus_message_unref (message);
3078 /* returns TRUE if the correct thing happens,
3079 * but the correct thing may include OOM errors.
3082 check_existent_service_auto_start (BusContext *context,
3083 DBusConnection *connection)
3085 DBusMessage *message;
3086 DBusMessage *base_service_message;
3087 dbus_uint32_t serial;
3089 const char *base_service;
3092 base_service_message = NULL;
3094 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3095 "/org/freedesktop/TestSuite",
3096 "org.freedesktop.TestSuite",
3099 if (message == NULL)
3102 text = TEST_ECHO_MESSAGE;
3103 if (!dbus_message_append_args (message,
3104 DBUS_TYPE_STRING, &text,
3107 dbus_message_unref (message);
3111 if (!dbus_connection_send (connection, message, &serial))
3113 dbus_message_unref (message);
3117 dbus_message_unref (message);
3120 bus_test_run_everything (context);
3122 /* now wait for the message bus to hear back from the activated
3125 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3126 bus_test_run_everything (context);
3128 if (!dbus_connection_get_is_connected (connection))
3130 _dbus_verbose ("connection was disconnected\n");
3136 message = pop_message_waiting_for_memory (connection);
3137 if (message == NULL)
3139 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3140 serial, connection);
3144 verbose_message_received (connection, message);
3145 _dbus_verbose (" (after sending %s)\n", "auto start");
3147 /* we should get zero or two ServiceOwnerChanged signals */
3148 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3150 GotServiceInfo message_kind;
3152 if (!check_base_service_activated (context, connection,
3153 message, &base_service))
3156 base_service_message = message;
3159 /* We may need to block here for the test service to exit or finish up */
3160 block_connection_until_message_from_bus (context, connection, "service to exit");
3162 /* Should get a service creation notification for the activated
3163 * service name, or a service deletion on the base service name
3165 message = dbus_connection_borrow_message (connection);
3166 if (message == NULL)
3168 _dbus_warn ("No message after auto activation "
3169 "(should be a service announcement)\n");
3170 dbus_connection_return_message (connection, message);
3175 message_kind = check_got_service_info (message);
3177 dbus_connection_return_message (connection, message);
3180 switch (message_kind)
3182 case GOT_SERVICE_CREATED:
3183 message = pop_message_waiting_for_memory (connection);
3184 if (message == NULL)
3186 _dbus_warn ("Failed to pop message we just put back! "
3187 "should have been a NameOwnerChanged (creation)\n");
3191 /* Check that ServiceOwnerChanged (creation) was correctly received */
3192 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3193 base_service, message))
3196 dbus_message_unref (message);
3201 case GOT_SERVICE_DELETED:
3203 /* The service started up and got a base address, but then
3204 * failed to register under EXISTENT_SERVICE_NAME
3206 CheckServiceOwnerChangedData socd;
3208 socd.expected_kind = SERVICE_DELETED;
3209 socd.expected_service_name = base_service;
3210 socd.failed = FALSE;
3211 socd.skip_connection = NULL;
3212 bus_test_clients_foreach (check_service_owner_changed_foreach,
3222 case GOT_SOMETHING_ELSE:
3223 _dbus_warn ("Unexpected message after auto activation\n");
3228 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3229 * come the method reply (or error) from the initial method call
3232 /* Note: if this test is run in OOM mode, it will block when the bus
3233 * doesn't send a reply due to OOM.
3235 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3237 message = pop_message_waiting_for_memory (connection);
3238 if (message == NULL)
3240 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3244 if (dbus_message_get_reply_serial (message) != serial)
3246 _dbus_warn ("Wrong reply serial\n");
3250 dbus_message_unref (message);
3253 if (!check_existent_ping (context, connection))
3256 if (!check_existent_get_machine_id (context, connection))
3259 if (!check_existent_hello_from_self (context, connection))
3262 if (!check_send_exit_to_service (context, connection,
3263 EXISTENT_SERVICE_NAME,
3271 dbus_message_unref (message);
3273 if (base_service_message)
3274 dbus_message_unref (base_service_message);
3279 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3281 /* returns TRUE if the correct thing happens,
3282 * but the correct thing may include OOM errors.
3285 check_launch_service_file_missing (BusContext *context,
3286 DBusConnection *connection)
3288 DBusMessage *message;
3289 dbus_uint32_t serial;
3292 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3293 "/org/freedesktop/TestSuite",
3294 "org.freedesktop.TestSuite",
3297 if (message == NULL)
3300 if (!dbus_connection_send (connection, message, &serial))
3302 dbus_message_unref (message);
3306 dbus_message_unref (message);
3309 bus_test_run_everything (context);
3310 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3311 bus_test_run_everything (context);
3313 if (!dbus_connection_get_is_connected (connection))
3315 _dbus_verbose ("connection was disconnected\n");
3321 message = pop_message_waiting_for_memory (connection);
3322 if (message == NULL)
3324 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3325 "Echo message (auto activation)", serial, connection);
3329 verbose_message_received (connection, message);
3331 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3333 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3335 _dbus_warn ("Message has wrong sender %s\n",
3336 dbus_message_get_sender (message) ?
3337 dbus_message_get_sender (message) : "(none)");
3341 if (dbus_message_is_error (message,
3342 DBUS_ERROR_NO_MEMORY))
3344 ; /* good, this is a valid response */
3346 else if (dbus_message_is_error (message,
3347 DBUS_ERROR_SERVICE_UNKNOWN))
3349 _dbus_verbose("got service unknown\n");
3350 ; /* good, this is expected (only valid when using launch helper) */
3354 warn_unexpected (connection, message, "not this error");
3361 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3369 dbus_message_unref (message);
3374 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3376 /* returns TRUE if the correct thing happens,
3377 * but the correct thing may include OOM errors.
3380 check_launch_service_user_missing (BusContext *context,
3381 DBusConnection *connection)
3383 DBusMessage *message;
3384 dbus_uint32_t serial;
3387 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3388 "/org/freedesktop/TestSuite",
3389 "org.freedesktop.TestSuite",
3392 if (message == NULL)
3395 if (!dbus_connection_send (connection, message, &serial))
3397 dbus_message_unref (message);
3401 dbus_message_unref (message);
3404 bus_test_run_everything (context);
3405 block_connection_until_message_from_bus (context, connection,
3406 "reply to service which should fail to auto-start (missing User)");
3407 bus_test_run_everything (context);
3409 if (!dbus_connection_get_is_connected (connection))
3411 _dbus_warn ("connection was disconnected\n");
3417 message = pop_message_waiting_for_memory (connection);
3418 if (message == NULL)
3420 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3421 "Echo message (auto activation)", serial, connection);
3425 verbose_message_received (connection, message);
3427 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3429 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3431 _dbus_warn ("Message has wrong sender %s\n",
3432 dbus_message_get_sender (message) ?
3433 dbus_message_get_sender (message) : "(none)");
3437 if (dbus_message_is_error (message,
3438 DBUS_ERROR_NO_MEMORY))
3440 ; /* good, this is a valid response */
3442 else if (dbus_message_is_error (message,
3443 DBUS_ERROR_SPAWN_FILE_INVALID))
3445 _dbus_verbose("got service file invalid\n");
3446 ; /* good, this is expected (only valid when using launch helper) */
3450 warn_unexpected (connection, message, "not this error");
3457 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3465 dbus_message_unref (message);
3470 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3472 /* returns TRUE if the correct thing happens,
3473 * but the correct thing may include OOM errors.
3476 check_launch_service_exec_missing (BusContext *context,
3477 DBusConnection *connection)
3479 DBusMessage *message;
3480 dbus_uint32_t serial;
3483 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3484 "/org/freedesktop/TestSuite",
3485 "org.freedesktop.TestSuite",
3488 if (message == NULL)
3491 if (!dbus_connection_send (connection, message, &serial))
3493 dbus_message_unref (message);
3497 dbus_message_unref (message);
3500 bus_test_run_everything (context);
3501 block_connection_until_message_from_bus (context, connection,
3502 "reply to service which should fail to auto-start (missing Exec)");
3503 bus_test_run_everything (context);
3505 if (!dbus_connection_get_is_connected (connection))
3507 _dbus_warn ("connection was disconnected\n");
3513 message = pop_message_waiting_for_memory (connection);
3514 if (message == NULL)
3516 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3517 "Echo message (auto activation)", serial, connection);
3521 verbose_message_received (connection, message);
3523 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3525 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3527 _dbus_warn ("Message has wrong sender %s\n",
3528 dbus_message_get_sender (message) ?
3529 dbus_message_get_sender (message) : "(none)");
3533 if (dbus_message_is_error (message,
3534 DBUS_ERROR_NO_MEMORY))
3536 ; /* good, this is a valid response */
3538 else if (dbus_message_is_error (message,
3539 DBUS_ERROR_SERVICE_UNKNOWN))
3541 _dbus_verbose("could not activate as invalid service file was not added\n");
3542 ; /* good, this is expected as we shouldn't have been added to
3543 * the activation list with a missing Exec key */
3545 else if (dbus_message_is_error (message,
3546 DBUS_ERROR_SPAWN_FILE_INVALID))
3548 _dbus_verbose("got service file invalid\n");
3549 ; /* good, this is allowed, and is the message passed back from the
3554 warn_unexpected (connection, message, "not this error");
3561 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3569 dbus_message_unref (message);
3574 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3576 /* returns TRUE if the correct thing happens,
3577 * but the correct thing may include OOM errors.
3580 check_launch_service_service_missing (BusContext *context,
3581 DBusConnection *connection)
3583 DBusMessage *message;
3584 dbus_uint32_t serial;
3587 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3588 "/org/freedesktop/TestSuite",
3589 "org.freedesktop.TestSuite",
3592 if (message == NULL)
3595 if (!dbus_connection_send (connection, message, &serial))
3597 dbus_message_unref (message);
3601 dbus_message_unref (message);
3604 bus_test_run_everything (context);
3605 block_connection_until_message_from_bus (context, connection,
3606 "reply to service which should fail to auto-start (missing Service)");
3607 bus_test_run_everything (context);
3609 if (!dbus_connection_get_is_connected (connection))
3611 _dbus_warn ("connection was disconnected\n");
3617 message = pop_message_waiting_for_memory (connection);
3618 if (message == NULL)
3620 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3621 "Echo message (auto activation)", serial, connection);
3625 verbose_message_received (connection, message);
3627 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3629 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3631 _dbus_warn ("Message has wrong sender %s\n",
3632 dbus_message_get_sender (message) ?
3633 dbus_message_get_sender (message) : "(none)");
3637 if (dbus_message_is_error (message,
3638 DBUS_ERROR_NO_MEMORY))
3640 ; /* good, this is a valid response */
3642 else if (dbus_message_is_error (message,
3643 DBUS_ERROR_SERVICE_UNKNOWN))
3645 _dbus_verbose("could not activate as invalid service file was not added\n");
3646 ; /* good, this is expected as we shouldn't have been added to
3647 * the activation list with a missing Exec key */
3649 else if (dbus_message_is_error (message,
3650 DBUS_ERROR_SPAWN_FILE_INVALID))
3652 _dbus_verbose("got service file invalid\n");
3653 ; /* good, this is allowed, and is the message passed back from the
3658 warn_unexpected (connection, message, "not this error");
3665 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3673 dbus_message_unref (message);
3678 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3680 /* returns TRUE if the correct thing happens,
3681 * but the correct thing may include OOM errors.
3684 check_shell_fail_service_auto_start (BusContext *context,
3685 DBusConnection *connection)
3687 DBusMessage *message;
3688 dbus_uint32_t serial;
3691 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3692 "/org/freedesktop/TestSuite",
3693 "org.freedesktop.TestSuite",
3696 if (message == NULL)
3699 if (!dbus_connection_send (connection, message, &serial))
3701 dbus_message_unref (message);
3705 dbus_message_unref (message);
3708 bus_test_run_everything (context);
3709 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3710 bus_test_run_everything (context);
3712 if (!dbus_connection_get_is_connected (connection))
3714 _dbus_verbose ("connection was disconnected\n");
3720 message = pop_message_waiting_for_memory (connection);
3721 if (message == NULL)
3723 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3724 "Echo message (auto activation)", serial, connection);
3728 verbose_message_received (connection, message);
3730 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3732 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3734 _dbus_warn ("Message has wrong sender %s\n",
3735 dbus_message_get_sender (message) ?
3736 dbus_message_get_sender (message) : "(none)");
3740 if (dbus_message_is_error (message,
3741 DBUS_ERROR_NO_MEMORY))
3743 ; /* good, this is a valid response */
3745 else if (dbus_message_is_error (message,
3746 DBUS_ERROR_INVALID_ARGS))
3748 _dbus_verbose("got invalid args\n");
3749 ; /* good, this is expected also */
3753 warn_unexpected (connection, message, "not this error");
3760 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3768 dbus_message_unref (message);
3773 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3775 /* returns TRUE if the correct thing happens,
3776 * but the correct thing may include OOM errors.
3779 check_shell_service_success_auto_start (BusContext *context,
3780 DBusConnection *connection)
3782 DBusMessage *message;
3783 DBusMessage *base_service_message;
3784 dbus_uint32_t serial;
3786 const char *base_service;
3787 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3789 base_service_message = NULL;
3791 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3792 "/org/freedesktop/TestSuite",
3793 "org.freedesktop.TestSuite",
3796 if (message == NULL)
3799 if (!dbus_connection_send (connection, message, &serial))
3801 dbus_message_unref (message);
3805 dbus_message_unref (message);
3808 bus_test_run_everything (context);
3810 /* now wait for the message bus to hear back from the activated
3813 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3814 bus_test_run_everything (context);
3816 if (!dbus_connection_get_is_connected (connection))
3818 _dbus_verbose ("connection was disconnected\n");
3824 message = pop_message_waiting_for_memory (connection);
3825 if (message == NULL)
3827 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3828 serial, connection);
3832 verbose_message_received (connection, message);
3833 _dbus_verbose (" (after sending %s)\n", "auto start");
3835 /* we should get zero or two ServiceOwnerChanged signals */
3836 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3838 GotServiceInfo message_kind;
3840 if (!check_base_service_activated (context, connection,
3841 message, &base_service))
3844 base_service_message = message;
3847 /* We may need to block here for the test service to exit or finish up */
3848 block_connection_until_message_from_bus (context, connection, "service to exit");
3850 /* Should get a service creation notification for the activated
3851 * service name, or a service deletion on the base service name
3853 message = dbus_connection_borrow_message (connection);
3854 if (message == NULL)
3856 _dbus_warn ("No message after auto activation "
3857 "(should be a service announcement)\n");
3858 dbus_connection_return_message (connection, message);
3863 message_kind = check_got_service_info (message);
3865 dbus_connection_return_message (connection, message);
3868 switch (message_kind)
3870 case GOT_SERVICE_CREATED:
3871 message = pop_message_waiting_for_memory (connection);
3872 if (message == NULL)
3874 _dbus_warn ("Failed to pop message we just put back! "
3875 "should have been a NameOwnerChanged (creation)\n");
3879 /* Check that ServiceOwnerChanged (creation) was correctly received */
3880 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3881 base_service, message))
3884 dbus_message_unref (message);
3889 case GOT_SERVICE_DELETED:
3891 /* The service started up and got a base address, but then
3892 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3894 CheckServiceOwnerChangedData socd;
3896 socd.expected_kind = SERVICE_DELETED;
3897 socd.expected_service_name = base_service;
3898 socd.failed = FALSE;
3899 socd.skip_connection = NULL;
3900 bus_test_clients_foreach (check_service_owner_changed_foreach,
3910 case GOT_SOMETHING_ELSE:
3911 _dbus_warn ("Unexpected message after auto activation\n");
3916 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3917 * come the method reply (or error) from the initial method call
3920 /* Note: if this test is run in OOM mode, it will block when the bus
3921 * doesn't send a reply due to OOM.
3923 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3925 message = pop_message_waiting_for_memory (connection);
3926 if (message == NULL)
3928 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3932 if (dbus_message_get_reply_serial (message) != serial)
3934 _dbus_warn ("Wrong reply serial\n");
3938 if (!dbus_message_get_args (message, NULL,
3939 DBUS_TYPE_STRING, &argv[0],
3940 DBUS_TYPE_STRING, &argv[1],
3941 DBUS_TYPE_STRING, &argv[2],
3942 DBUS_TYPE_STRING, &argv[3],
3943 DBUS_TYPE_STRING, &argv[4],
3944 DBUS_TYPE_STRING, &argv[5],
3945 DBUS_TYPE_STRING, &argv[6],
3948 _dbus_warn ("Error getting arguments from return\n");
3952 /* don't worry about arg[0] as it may be different
3953 depending on the path to the tests
3955 if (strcmp("-test", argv[1]) != 0)
3957 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3962 if (strcmp("that", argv[2]) != 0)
3964 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3969 if (strcmp("we get", argv[3]) != 0)
3971 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3976 if (strcmp("back", argv[4]) != 0)
3978 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3983 if (strcmp("--what", argv[5]) != 0)
3985 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3990 if (strcmp("we put in", argv[6]) != 0)
3992 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3993 "we put in", argv[6]);
3997 dbus_message_unref (message);
4000 if (!check_send_exit_to_service (context, connection,
4001 SHELL_SUCCESS_SERVICE_NAME,
4009 dbus_message_unref (message);
4011 if (base_service_message)
4012 dbus_message_unref (base_service_message);
4020 BusContext *context;
4024 check_oom_check1_func (void *data)
4026 Check1Data *d = data;
4028 if (! (* d->func) (d->context))
4031 if (!check_no_leftovers (d->context))
4033 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4041 check1_try_iterations (BusContext *context,
4042 const char *description,
4048 d.context = context;
4050 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4052 _dbus_assert_not_reached ("test failed");
4056 check_get_services (BusContext *context,
4057 DBusConnection *connection,
4062 DBusMessage *message;
4063 dbus_uint32_t serial;
4070 dbus_error_init (&error);
4073 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4075 DBUS_INTERFACE_DBUS,
4078 if (message == NULL)
4081 if (!dbus_connection_send (connection, message, &serial))
4083 dbus_message_unref (message);
4087 /* send our message */
4088 bus_test_run_clients_loop (SEND_PENDING (connection));
4090 dbus_message_unref (message);
4093 dbus_connection_ref (connection); /* because we may get disconnected */
4094 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4096 if (!dbus_connection_get_is_connected (connection))
4098 _dbus_verbose ("connection was disconnected\n");
4100 dbus_connection_unref (connection);
4105 dbus_connection_unref (connection);
4107 message = pop_message_waiting_for_memory (connection);
4108 if (message == NULL)
4110 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4111 method, serial, connection);
4115 verbose_message_received (connection, message);
4117 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4119 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4121 ; /* good, this is a valid response */
4125 warn_unexpected (connection, message, "not this error");
4132 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4134 ; /* good, expected */
4138 warn_unexpected (connection, message,
4139 "method_return for ListActivatableNames/ListNames");
4146 if (!dbus_message_get_args (message, &error,
4152 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4154 _dbus_verbose ("no memory to list services by %s\n", method);
4155 dbus_error_free (&error);
4156 _dbus_wait_for_memory ();
4157 goto retry_get_property;
4161 _dbus_assert (dbus_error_is_set (&error));
4162 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4171 if (!check_no_leftovers (context))
4177 dbus_error_free (&error);
4180 dbus_message_unref (message);
4185 /* returns TRUE if the correct thing happens,
4186 * but the correct thing may include OOM errors.
4189 check_list_services (BusContext *context,
4190 DBusConnection *connection)
4192 DBusMessage *message;
4193 DBusMessage *base_service_message;
4194 const char *base_service;
4195 dbus_uint32_t serial;
4197 const char *existent = EXISTENT_SERVICE_NAME;
4198 dbus_uint32_t flags;
4202 _dbus_verbose ("check_list_services for %p\n", connection);
4204 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4209 if (!_dbus_string_array_contains ((const char **)services, existent))
4211 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4212 dbus_free_string_array (services);
4216 dbus_free_string_array (services);
4218 base_service_message = NULL;
4220 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4222 DBUS_INTERFACE_DBUS,
4223 "StartServiceByName");
4225 if (message == NULL)
4228 dbus_message_set_auto_start (message, FALSE);
4231 if (!dbus_message_append_args (message,
4232 DBUS_TYPE_STRING, &existent,
4233 DBUS_TYPE_UINT32, &flags,
4236 dbus_message_unref (message);
4240 if (!dbus_connection_send (connection, message, &serial))
4242 dbus_message_unref (message);
4246 dbus_message_unref (message);
4249 bus_test_run_everything (context);
4251 /* now wait for the message bus to hear back from the activated
4254 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4256 bus_test_run_everything (context);
4258 if (!dbus_connection_get_is_connected (connection))
4260 _dbus_verbose ("connection was disconnected\n");
4266 message = pop_message_waiting_for_memory (connection);
4267 if (message == NULL)
4269 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4270 "StartServiceByName", serial, connection);
4274 verbose_message_received (connection, message);
4275 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4277 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4279 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4281 _dbus_warn ("Message has wrong sender %s\n",
4282 dbus_message_get_sender (message) ?
4283 dbus_message_get_sender (message) : "(none)");
4287 if (dbus_message_is_error (message,
4288 DBUS_ERROR_NO_MEMORY))
4290 ; /* good, this is a valid response */
4292 else if (dbus_message_is_error (message,
4293 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4294 dbus_message_is_error (message,
4295 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4296 dbus_message_is_error (message,
4297 DBUS_ERROR_SPAWN_EXEC_FAILED))
4299 ; /* good, this is expected also */
4303 _dbus_warn ("Did not expect error %s\n",
4304 dbus_message_get_error_name (message));
4310 GotServiceInfo message_kind;
4312 if (!check_base_service_activated (context, connection,
4313 message, &base_service))
4316 base_service_message = message;
4319 /* We may need to block here for the test service to exit or finish up */
4320 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4322 message = dbus_connection_borrow_message (connection);
4323 if (message == NULL)
4325 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4329 message_kind = check_got_service_info (message);
4331 dbus_connection_return_message (connection, message);
4334 switch (message_kind)
4336 case GOT_SOMETHING_ELSE:
4338 case GOT_SERVICE_DELETED:
4339 _dbus_warn ("Unexpected message after ActivateService "
4340 "(should be an error or a service announcement)\n");
4343 case GOT_SERVICE_CREATED:
4344 message = pop_message_waiting_for_memory (connection);
4345 if (message == NULL)
4347 _dbus_warn ("Failed to pop message we just put back! "
4348 "should have been a NameOwnerChanged (creation)\n");
4352 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4353 base_service, message))
4356 dbus_message_unref (message);
4359 if (!check_no_leftovers (context))
4361 _dbus_warn ("Messages were left over after successful activation\n");
4369 if (!check_get_services (context, connection, "ListNames", &services, &len))
4374 if (!_dbus_string_array_contains ((const char **)services, existent))
4376 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4380 dbus_free_string_array (services);
4382 if (!check_send_exit_to_service (context, connection,
4383 EXISTENT_SERVICE_NAME, base_service))
4390 dbus_message_unref (message);
4392 if (base_service_message)
4393 dbus_message_unref (base_service_message);
4401 BusContext *context;
4402 DBusConnection *connection;
4406 check_oom_check2_func (void *data)
4408 Check2Data *d = data;
4410 if (! (* d->func) (d->context, d->connection))
4413 if (!check_no_leftovers (d->context))
4415 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4423 check2_try_iterations (BusContext *context,
4424 DBusConnection *connection,
4425 const char *description,
4431 d.context = context;
4432 d.connection = connection;
4434 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4437 _dbus_warn ("%s failed during oom\n", description);
4438 _dbus_assert_not_reached ("test failed");
4443 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4444 const char *filename)
4449 if (!_dbus_string_init (&full))
4452 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4454 _dbus_string_free (&full);
4458 _dbus_string_init_const (&file, filename);
4460 if (!_dbus_concat_dir_and_file (&full, &file))
4462 _dbus_string_free (&full);
4466 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4467 _dbus_string_get_const_data (&full));
4469 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4471 _dbus_string_free (&full);
4477 bus_dispatch_test_conf (const DBusString *test_data_dir,
4478 const char *filename,
4479 dbus_bool_t use_launcher)
4481 BusContext *context;
4482 DBusConnection *foo;
4483 DBusConnection *bar;
4484 DBusConnection *baz;
4487 /* save the config name for the activation helper */
4488 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4489 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4491 dbus_error_init (&error);
4493 context = bus_context_new_test (test_data_dir, filename);
4494 if (context == NULL)
4497 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4499 _dbus_assert_not_reached ("could not alloc connection");
4501 if (!bus_setup_debug_client (foo))
4502 _dbus_assert_not_reached ("could not set up connection");
4504 spin_connection_until_authenticated (context, foo);
4506 if (!check_hello_message (context, foo))
4507 _dbus_assert_not_reached ("hello message failed");
4509 if (!check_double_hello_message (context, foo))
4510 _dbus_assert_not_reached ("double hello message failed");
4512 if (!check_add_match_all (context, foo))
4513 _dbus_assert_not_reached ("AddMatch message failed");
4515 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4517 _dbus_assert_not_reached ("could not alloc connection");
4519 if (!bus_setup_debug_client (bar))
4520 _dbus_assert_not_reached ("could not set up connection");
4522 spin_connection_until_authenticated (context, bar);
4524 if (!check_hello_message (context, bar))
4525 _dbus_assert_not_reached ("hello message failed");
4527 if (!check_add_match_all (context, bar))
4528 _dbus_assert_not_reached ("AddMatch message failed");
4530 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4532 _dbus_assert_not_reached ("could not alloc connection");
4534 if (!bus_setup_debug_client (baz))
4535 _dbus_assert_not_reached ("could not set up connection");
4537 spin_connection_until_authenticated (context, baz);
4539 if (!check_hello_message (context, baz))
4540 _dbus_assert_not_reached ("hello message failed");
4542 if (!check_add_match_all (context, baz))
4543 _dbus_assert_not_reached ("AddMatch message failed");
4545 #ifdef DBUS_WIN_FIXME
4546 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4547 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4549 if (!check_get_connection_unix_user (context, baz))
4550 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4552 if (!check_get_connection_unix_process_id (context, baz))
4553 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4556 if (!check_list_services (context, baz))
4557 _dbus_assert_not_reached ("ListActivatableNames message failed");
4559 if (!check_no_leftovers (context))
4561 _dbus_warn ("Messages were left over after setting up initial connections\n");
4562 _dbus_assert_not_reached ("initial connection setup failed");
4565 check1_try_iterations (context, "create_and_hello",
4566 check_hello_connection);
4568 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4569 check_nonexistent_service_no_auto_start);
4571 #ifdef DBUS_WIN_FIXME
4572 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4574 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4575 check_segfault_service_no_auto_start);
4578 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4579 check_existent_service_no_auto_start);
4581 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4582 check_nonexistent_service_auto_start);
4585 #ifdef DBUS_WIN_FIXME
4586 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4588 /* only do the segfault test if we are not using the launcher */
4589 check2_try_iterations (context, foo, "segfault_service_auto_start",
4590 check_segfault_service_auto_start);
4593 /* only do the shell fail test if we are not using the launcher */
4594 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4595 check_shell_fail_service_auto_start);
4597 /* specific to launcher */
4599 if (!check_launch_service_file_missing (context, foo))
4600 _dbus_assert_not_reached ("did not get service file not found error");
4603 /* Note: need to resolve some issues with the testing code in order to run
4604 * this in oom (handle that we sometimes don't get replies back from the bus
4605 * when oom happens, without blocking the test).
4607 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4608 check_existent_service_auto_start);
4611 if (!check_existent_service_auto_start (context, foo))
4612 _dbus_assert_not_reached ("existent service auto start failed");
4614 if (!check_shell_service_success_auto_start (context, foo))
4615 _dbus_assert_not_reached ("shell success service auto start failed");
4617 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4619 kill_client_connection_unchecked (foo);
4620 kill_client_connection_unchecked (bar);
4621 kill_client_connection_unchecked (baz);
4623 bus_context_unref (context);
4629 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4630 const char *filename)
4632 BusContext *context;
4633 DBusConnection *foo;
4636 /* save the config name for the activation helper */
4637 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4638 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4640 dbus_error_init (&error);
4642 context = bus_context_new_test (test_data_dir, filename);
4643 if (context == NULL)
4646 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4648 _dbus_assert_not_reached ("could not alloc connection");
4650 if (!bus_setup_debug_client (foo))
4651 _dbus_assert_not_reached ("could not set up connection");
4653 spin_connection_until_authenticated (context, foo);
4655 if (!check_hello_message (context, foo))
4656 _dbus_assert_not_reached ("hello message failed");
4658 if (!check_double_hello_message (context, foo))
4659 _dbus_assert_not_reached ("double hello message failed");
4661 if (!check_add_match_all (context, foo))
4662 _dbus_assert_not_reached ("AddMatch message failed");
4664 /* this only tests the activation.c user check */
4665 if (!check_launch_service_user_missing (context, foo))
4666 _dbus_assert_not_reached ("user missing did not trigger error");
4668 /* this only tests the desktop.c exec check */
4669 if (!check_launch_service_exec_missing (context, foo))
4670 _dbus_assert_not_reached ("exec missing did not trigger error");
4672 /* this only tests the desktop.c service check */
4673 if (!check_launch_service_service_missing (context, foo))
4674 _dbus_assert_not_reached ("service missing did not trigger error");
4676 _dbus_verbose ("Disconnecting foo\n");
4678 kill_client_connection_unchecked (foo);
4680 bus_context_unref (context);
4686 bus_dispatch_test (const DBusString *test_data_dir)
4688 /* run normal activation tests */
4689 _dbus_verbose ("Normal activation tests\n");
4690 if (!bus_dispatch_test_conf (test_data_dir,
4691 "valid-config-files/debug-allow-all.conf", FALSE))
4695 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4697 /* run launch-helper activation tests */
4698 _dbus_verbose ("Launch helper activation tests\n");
4699 if (!bus_dispatch_test_conf (test_data_dir,
4700 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4703 /* run select launch-helper activation tests on broken service files */
4704 if (!bus_dispatch_test_conf_fail (test_data_dir,
4705 "valid-config-files-system/debug-allow-all-fail.conf"))
4713 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4715 BusContext *context;
4716 DBusConnection *foo;
4719 dbus_error_init (&error);
4721 /* Test SHA1 authentication */
4722 _dbus_verbose ("Testing SHA1 context\n");
4724 context = bus_context_new_test (test_data_dir,
4725 "valid-config-files/debug-allow-all-sha1.conf");
4726 if (context == NULL)
4729 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4731 _dbus_assert_not_reached ("could not alloc connection");
4733 if (!bus_setup_debug_client (foo))
4734 _dbus_assert_not_reached ("could not set up connection");
4736 spin_connection_until_authenticated (context, foo);
4738 if (!check_hello_message (context, foo))
4739 _dbus_assert_not_reached ("hello message failed");
4741 if (!check_add_match_all (context, foo))
4742 _dbus_assert_not_reached ("addmatch message failed");
4744 if (!check_no_leftovers (context))
4746 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4747 _dbus_assert_not_reached ("initial connection setup failed");
4750 check1_try_iterations (context, "create_and_hello_sha1",
4751 check_hello_connection);
4753 kill_client_connection_unchecked (foo);
4755 bus_context_unref (context);
4760 #ifdef HAVE_UNIX_FD_PASSING
4763 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4765 BusContext *context;
4766 DBusConnection *foo, *bar;
4769 int one[2], two[2], x, y, z;
4772 dbus_error_init (&error);
4774 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4775 if (context == NULL)
4776 _dbus_assert_not_reached ("could not alloc context");
4778 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4780 _dbus_assert_not_reached ("could not alloc connection");
4782 if (!bus_setup_debug_client (foo))
4783 _dbus_assert_not_reached ("could not set up connection");
4785 spin_connection_until_authenticated (context, foo);
4787 if (!check_hello_message (context, foo))
4788 _dbus_assert_not_reached ("hello message failed");
4790 if (!check_add_match_all (context, foo))
4791 _dbus_assert_not_reached ("AddMatch message failed");
4793 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4795 _dbus_assert_not_reached ("could not alloc connection");
4797 if (!bus_setup_debug_client (bar))
4798 _dbus_assert_not_reached ("could not set up connection");
4800 spin_connection_until_authenticated (context, bar);
4802 if (!check_hello_message (context, bar))
4803 _dbus_assert_not_reached ("hello message failed");
4805 if (!check_add_match_all (context, bar))
4806 _dbus_assert_not_reached ("AddMatch message failed");
4808 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4809 _dbus_assert_not_reached ("could not alloc message");
4811 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4812 _dbus_assert_not_reached("Failed to allocate pipe #1");
4814 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4815 _dbus_assert_not_reached("Failed to allocate pipe #2");
4817 if (!dbus_message_append_args(m,
4818 DBUS_TYPE_UNIX_FD, one,
4819 DBUS_TYPE_UNIX_FD, two,
4820 DBUS_TYPE_UNIX_FD, two,
4822 _dbus_assert_not_reached("Failed to attach fds.");
4824 if (!_dbus_close(one[0], &error))
4825 _dbus_assert_not_reached("Failed to close pipe #1 ");
4826 if (!_dbus_close(two[0], &error))
4827 _dbus_assert_not_reached("Failed to close pipe #2 ");
4829 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4830 _dbus_assert_not_reached("Connection cannot do fd passing");
4832 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4833 _dbus_assert_not_reached("Connection cannot do fd passing");
4835 if (!dbus_connection_send (foo, m, NULL))
4836 _dbus_assert_not_reached("Failed to send fds");
4838 dbus_message_unref(m);
4840 bus_test_run_clients_loop (SEND_PENDING (foo));
4842 bus_test_run_everything (context);
4844 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4846 if (!(m = pop_message_waiting_for_memory (foo)))
4847 _dbus_assert_not_reached("Failed to receive msg");
4849 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4850 _dbus_assert_not_reached("bogus message received");
4852 dbus_message_unref(m);
4854 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4856 if (!(m = pop_message_waiting_for_memory (bar)))
4857 _dbus_assert_not_reached("Failed to receive msg");
4859 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4860 _dbus_assert_not_reached("bogus message received");
4862 if (!dbus_message_get_args(m,
4864 DBUS_TYPE_UNIX_FD, &x,
4865 DBUS_TYPE_UNIX_FD, &y,
4866 DBUS_TYPE_UNIX_FD, &z,
4868 _dbus_assert_not_reached("Failed to parse fds.");
4870 dbus_message_unref(m);
4872 if (write(x, "X", 1) != 1)
4873 _dbus_assert_not_reached("Failed to write to pipe #1");
4874 if (write(y, "Y", 1) != 1)
4875 _dbus_assert_not_reached("Failed to write to pipe #2");
4876 if (write(z, "Z", 1) != 1)
4877 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4879 if (!_dbus_close(x, &error))
4880 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4881 if (!_dbus_close(y, &error))
4882 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4883 if (!_dbus_close(z, &error))
4884 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4886 if (read(one[1], &r, 1) != 1 || r != 'X')
4887 _dbus_assert_not_reached("Failed to read value from pipe.");
4888 if (read(two[1], &r, 1) != 1 || r != 'Y')
4889 _dbus_assert_not_reached("Failed to read value from pipe.");
4890 if (read(two[1], &r, 1) != 1 || r != 'Z')
4891 _dbus_assert_not_reached("Failed to read value from pipe.");
4893 if (!_dbus_close(one[1], &error))
4894 _dbus_assert_not_reached("Failed to close pipe #1 ");
4895 if (!_dbus_close(two[1], &error))
4896 _dbus_assert_not_reached("Failed to close pipe #2 ");
4898 _dbus_verbose ("Disconnecting foo\n");
4899 kill_client_connection_unchecked (foo);
4901 _dbus_verbose ("Disconnecting bar\n");
4902 kill_client_connection_unchecked (bar);
4904 bus_context_unref (context);
4910 #endif /* DBUS_BUILD_TESTS */