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 #ifndef TEST_CONNECTION
47 move to build system as already done for cmake
50 #define TEST_CONNECTION "debug-pipe:name=test-server"
52 #define TEST_CONNECTION "tcp:host=localhost,port=1234"
57 send_one_message (DBusConnection *connection,
59 DBusConnection *sender,
60 DBusConnection *addressed_recipient,
62 BusTransaction *transaction,
65 if (!bus_context_check_security_policy (context, transaction,
71 return TRUE; /* silently don't send it */
73 if (dbus_message_contains_unix_fds(message) &&
74 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
75 return TRUE; /* silently don't send it */
77 if (!bus_transaction_send (transaction,
89 bus_dispatch_matches (BusTransaction *transaction,
90 DBusConnection *sender,
91 DBusConnection *addressed_recipient,
96 BusConnections *connections;
98 BusMatchmaker *matchmaker;
102 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
104 /* sender and recipient can both be NULL for the bus driver,
105 * or for signals with no particular recipient
108 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
109 _dbus_assert (dbus_message_get_sender (message) != NULL);
111 context = bus_transaction_get_context (transaction);
113 /* First, send the message to the addressed_recipient, if there is one. */
114 if (addressed_recipient != NULL)
116 if (!bus_context_check_security_policy (context, transaction,
117 sender, addressed_recipient,
122 if (dbus_message_contains_unix_fds (message) &&
123 !dbus_connection_can_send_type (addressed_recipient,
126 dbus_set_error (error,
127 DBUS_ERROR_NOT_SUPPORTED,
128 "Tried to send message with Unix file descriptors"
129 "to a client that doesn't support that.");
133 /* Dispatch the message */
134 if (!bus_transaction_send (transaction, addressed_recipient, message))
141 /* Now dispatch to others who look interested in this message */
142 connections = bus_transaction_get_connections (transaction);
143 dbus_error_init (&tmp_error);
144 matchmaker = bus_context_get_matchmaker (context);
147 if (!bus_matchmaker_get_recipients (matchmaker, connections,
148 sender, addressed_recipient, message,
155 link = _dbus_list_get_first_link (&recipients);
158 DBusConnection *dest;
162 if (!send_one_message (dest, context, sender, addressed_recipient,
163 message, transaction, &tmp_error))
166 link = _dbus_list_get_next_link (&recipients, link);
169 _dbus_list_clear (&recipients);
171 if (dbus_error_is_set (&tmp_error))
173 dbus_move_error (&tmp_error, error);
180 static DBusHandlerResult
181 bus_dispatch (DBusConnection *connection,
182 DBusMessage *message)
184 const char *sender, *service_name;
186 BusTransaction *transaction;
188 DBusHandlerResult result;
189 DBusConnection *addressed_recipient;
191 result = DBUS_HANDLER_RESULT_HANDLED;
194 addressed_recipient = NULL;
195 dbus_error_init (&error);
197 context = bus_connection_get_context (connection);
198 _dbus_assert (context != NULL);
200 /* If we can't even allocate an OOM error, we just go to sleep
203 while (!bus_connection_preallocate_oom_error (connection))
204 _dbus_wait_for_memory ();
206 /* Ref connection in case we disconnect it at some point in here */
207 dbus_connection_ref (connection);
209 service_name = dbus_message_get_destination (message);
211 #ifdef DBUS_ENABLE_VERBOSE_MODE
213 const char *interface_name, *member_name, *error_name;
215 interface_name = dbus_message_get_interface (message);
216 member_name = dbus_message_get_member (message);
217 error_name = dbus_message_get_error_name (message);
219 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
220 interface_name ? interface_name : "(no interface)",
221 member_name ? member_name : "(no member)",
222 error_name ? error_name : "(no error name)",
223 service_name ? service_name : "peer");
225 #endif /* DBUS_ENABLE_VERBOSE_MODE */
227 /* If service_name is NULL, if it's a signal we send it to all
228 * connections with a match rule. If it's not a signal, there
229 * are some special cases here but mostly we just bail out.
231 if (service_name == NULL)
233 if (dbus_message_is_signal (message,
234 DBUS_INTERFACE_LOCAL,
237 bus_connection_disconnected (connection);
241 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
243 /* DBusConnection also handles some of these automatically, we leave
246 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
251 /* Create our transaction */
252 transaction = bus_transaction_new (context);
253 if (transaction == NULL)
255 BUS_SET_OOM (&error);
259 /* Assign a sender to the message */
260 if (bus_connection_is_active (connection))
262 sender = bus_connection_get_name (connection);
263 _dbus_assert (sender != NULL);
265 if (!dbus_message_set_sender (message, sender))
267 BUS_SET_OOM (&error);
271 /* We need to refetch the service name here, because
272 * dbus_message_set_sender can cause the header to be
273 * reallocated, and thus the service_name pointer will become
276 service_name = dbus_message_get_destination (message);
280 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
282 if (!bus_context_check_security_policy (context, transaction,
283 connection, NULL, NULL, message, &error))
285 _dbus_verbose ("Security policy rejected message\n");
289 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
290 if (!bus_driver_handle_message (connection, transaction, message, &error))
293 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
295 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
296 dbus_connection_close (connection);
299 else if (service_name != NULL) /* route to named service */
301 DBusString service_string;
303 BusRegistry *registry;
305 _dbus_assert (service_name != NULL);
307 registry = bus_connection_get_registry (connection);
309 _dbus_string_init_const (&service_string, service_name);
310 service = bus_registry_lookup (registry, &service_string);
312 if (service == NULL && dbus_message_get_auto_start (message))
314 BusActivation *activation;
315 /* We can't do the security policy check here, since the addressed
316 * recipient service doesn't exist yet. We do it before sending the
317 * message after the service has been created.
319 activation = bus_connection_get_activation (connection);
321 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
322 message, service_name, &error))
324 _DBUS_ASSERT_ERROR_IS_SET (&error);
325 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
331 else if (service == NULL)
333 dbus_set_error (&error,
334 DBUS_ERROR_NAME_HAS_NO_OWNER,
335 "Name \"%s\" does not exist",
341 addressed_recipient = bus_service_get_primary_owners_connection (service);
342 _dbus_assert (addressed_recipient != NULL);
346 /* Now send the message to its destination (or not, if
347 * addressed_recipient == NULL), and match it against other connections'
350 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
354 if (dbus_error_is_set (&error))
356 if (!dbus_connection_get_is_connected (connection))
358 /* If we disconnected it, we won't bother to send it any error
361 _dbus_verbose ("Not sending error to connection we disconnected\n");
363 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
365 bus_connection_send_oom_error (connection, message);
367 /* cancel transaction due to OOM */
368 if (transaction != NULL)
370 bus_transaction_cancel_and_free (transaction);
376 /* Try to send the real error, if no mem to do that, send
379 _dbus_assert (transaction != NULL);
380 if (!bus_transaction_send_error_reply (transaction, connection,
383 bus_connection_send_oom_error (connection, message);
385 /* cancel transaction due to OOM */
386 if (transaction != NULL)
388 bus_transaction_cancel_and_free (transaction);
395 dbus_error_free (&error);
398 if (transaction != NULL)
400 bus_transaction_execute_and_free (transaction);
403 dbus_connection_unref (connection);
408 static DBusHandlerResult
409 bus_dispatch_message_filter (DBusConnection *connection,
410 DBusMessage *message,
413 return bus_dispatch (connection, message);
417 bus_dispatch_add_connection (DBusConnection *connection)
419 if (!dbus_connection_add_filter (connection,
420 bus_dispatch_message_filter,
428 bus_dispatch_remove_connection (DBusConnection *connection)
430 /* Here we tell the bus driver that we want to get off. */
431 bus_driver_remove_connection (connection);
433 dbus_connection_remove_filter (connection,
434 bus_dispatch_message_filter,
438 #ifdef DBUS_BUILD_TESTS
442 /* This is used to know whether we need to block in order to finish
443 * sending a message, or whether the initial dbus_connection_send()
444 * already flushed the queue.
446 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
448 typedef dbus_bool_t (* Check1Func) (BusContext *context);
449 typedef dbus_bool_t (* Check2Func) (BusContext *context,
450 DBusConnection *connection);
452 static dbus_bool_t check_no_leftovers (BusContext *context);
455 block_connection_until_message_from_bus (BusContext *context,
456 DBusConnection *connection,
457 const char *what_is_expected)
459 _dbus_verbose ("expecting: %s\n", what_is_expected);
461 while (dbus_connection_get_dispatch_status (connection) ==
462 DBUS_DISPATCH_COMPLETE &&
463 dbus_connection_get_is_connected (connection))
465 bus_test_run_bus_loop (context, TRUE);
466 bus_test_run_clients_loop (FALSE);
471 spin_connection_until_authenticated (BusContext *context,
472 DBusConnection *connection)
474 _dbus_verbose ("Spinning to auth connection %p\n", connection);
475 while (!dbus_connection_get_is_authenticated (connection) &&
476 dbus_connection_get_is_connected (connection))
478 bus_test_run_bus_loop (context, FALSE);
479 bus_test_run_clients_loop (FALSE);
481 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
484 /* compensate for fact that pop_message() can return #NULL due to OOM */
486 pop_message_waiting_for_memory (DBusConnection *connection)
488 while (dbus_connection_get_dispatch_status (connection) ==
489 DBUS_DISPATCH_NEED_MEMORY)
490 _dbus_wait_for_memory ();
492 return dbus_connection_pop_message (connection);
496 borrow_message_waiting_for_memory (DBusConnection *connection)
498 while (dbus_connection_get_dispatch_status (connection) ==
499 DBUS_DISPATCH_NEED_MEMORY)
500 _dbus_wait_for_memory ();
502 return dbus_connection_borrow_message (connection);
506 warn_unexpected_real (DBusConnection *connection,
507 DBusMessage *message,
508 const char *expected,
509 const char *function,
513 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
515 dbus_message_get_interface (message) ?
516 dbus_message_get_interface (message) : "(unset)",
517 dbus_message_get_member (message) ?
518 dbus_message_get_member (message) : "(unset)",
519 dbus_message_get_error_name (message) ?
520 dbus_message_get_error_name (message) : "(unset)",
524 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
525 function, line, connection, expected);
528 #define warn_unexpected(connection, message, expected) \
529 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
532 verbose_message_received (DBusConnection *connection,
533 DBusMessage *message)
535 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
536 dbus_message_get_interface (message) ?
537 dbus_message_get_interface (message) : "(unset)",
538 dbus_message_get_member (message) ?
539 dbus_message_get_member (message) : "(unset)",
540 dbus_message_get_error_name (message) ?
541 dbus_message_get_error_name (message) : "(unset)",
554 ServiceInfoKind expected_kind;
555 const char *expected_service_name;
557 DBusConnection *skip_connection;
558 } CheckServiceOwnerChangedData;
561 check_service_owner_changed_foreach (DBusConnection *connection,
564 CheckServiceOwnerChangedData *d = data;
565 DBusMessage *message;
567 const char *service_name, *old_owner, *new_owner;
569 if (d->expected_kind == SERVICE_CREATED
570 && connection == d->skip_connection)
573 dbus_error_init (&error);
576 message = pop_message_waiting_for_memory (connection);
579 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
580 connection, "NameOwnerChanged");
583 else if (!dbus_message_is_signal (message,
587 warn_unexpected (connection, message, "NameOwnerChanged");
593 reget_service_info_data:
598 dbus_message_get_args (message, &error,
599 DBUS_TYPE_STRING, &service_name,
600 DBUS_TYPE_STRING, &old_owner,
601 DBUS_TYPE_STRING, &new_owner,
604 if (dbus_error_is_set (&error))
606 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
608 dbus_error_free (&error);
609 _dbus_wait_for_memory ();
610 goto reget_service_info_data;
614 _dbus_warn ("Did not get the expected arguments\n");
619 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
620 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
621 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
623 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
627 if (strcmp (service_name, d->expected_service_name) != 0)
629 _dbus_warn ("expected info on service %s, got info on %s\n",
630 d->expected_service_name,
635 if (*service_name == ':' && new_owner[0]
636 && strcmp (service_name, new_owner) != 0)
638 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
639 service_name, old_owner, new_owner);
647 dbus_error_free (&error);
650 dbus_message_unref (message);
657 kill_client_connection (BusContext *context,
658 DBusConnection *connection)
662 CheckServiceOwnerChangedData socd;
664 _dbus_verbose ("killing connection %p\n", connection);
666 s = dbus_bus_get_unique_name (connection);
667 _dbus_assert (s != NULL);
669 while ((base_service = _dbus_strdup (s)) == NULL)
670 _dbus_wait_for_memory ();
672 dbus_connection_ref (connection);
674 /* kick in the disconnect handler that unrefs the connection */
675 dbus_connection_close (connection);
677 bus_test_run_everything (context);
679 _dbus_assert (bus_test_client_listed (connection));
681 /* Run disconnect handler in test.c */
682 if (bus_connection_dispatch_one_message (connection))
683 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
685 _dbus_assert (!dbus_connection_get_is_connected (connection));
686 dbus_connection_unref (connection);
688 _dbus_assert (!bus_test_client_listed (connection));
690 socd.expected_kind = SERVICE_DELETED;
691 socd.expected_service_name = base_service;
693 socd.skip_connection = NULL;
695 bus_test_clients_foreach (check_service_owner_changed_foreach,
698 dbus_free (base_service);
701 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
703 if (!check_no_leftovers (context))
704 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
708 kill_client_connection_unchecked (DBusConnection *connection)
710 /* This kills the connection without expecting it to affect
711 * the rest of the bus.
713 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
715 dbus_connection_ref (connection);
716 dbus_connection_close (connection);
717 /* dispatching disconnect handler will unref once */
718 if (bus_connection_dispatch_one_message (connection))
719 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
721 _dbus_assert (!bus_test_client_listed (connection));
722 dbus_connection_unref (connection);
728 } CheckNoMessagesData;
731 check_no_messages_foreach (DBusConnection *connection,
734 CheckNoMessagesData *d = data;
735 DBusMessage *message;
737 message = pop_message_waiting_for_memory (connection);
740 warn_unexpected (connection, message, "no messages");
746 dbus_message_unref (message);
751 check_no_leftovers (BusContext *context)
753 CheckNoMessagesData nmd;
756 bus_test_clients_foreach (check_no_messages_foreach,
761 _dbus_verbose ("leftover message found\n");
768 /* returns TRUE if the correct thing happens,
769 * but the correct thing may include OOM errors.
772 check_hello_message (BusContext *context,
773 DBusConnection *connection)
775 DBusMessage *message;
776 DBusMessage *name_message;
777 dbus_uint32_t serial;
781 const char *acquired;
784 dbus_error_init (&error);
790 _dbus_verbose ("check_hello_message for %p\n", connection);
792 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
800 dbus_connection_ref (connection); /* because we may get disconnected */
802 if (!dbus_connection_send (connection, message, &serial))
804 dbus_message_unref (message);
805 dbus_connection_unref (connection);
809 _dbus_assert (dbus_message_has_signature (message, ""));
811 dbus_message_unref (message);
814 if (!dbus_connection_get_is_connected (connection))
816 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
818 dbus_connection_unref (connection);
823 /* send our message */
824 bus_test_run_clients_loop (SEND_PENDING (connection));
826 if (!dbus_connection_get_is_connected (connection))
828 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
830 dbus_connection_unref (connection);
835 block_connection_until_message_from_bus (context, connection, "reply to Hello");
837 if (!dbus_connection_get_is_connected (connection))
839 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
841 dbus_connection_unref (connection);
846 dbus_connection_unref (connection);
848 message = pop_message_waiting_for_memory (connection);
851 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
852 "Hello", serial, connection);
856 verbose_message_received (connection, message);
858 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
860 _dbus_warn ("Message has wrong sender %s\n",
861 dbus_message_get_sender (message) ?
862 dbus_message_get_sender (message) : "(none)");
866 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
868 if (dbus_message_is_error (message,
869 DBUS_ERROR_NO_MEMORY))
871 ; /* good, this is a valid response */
875 warn_unexpected (connection, message, "not this error");
882 CheckServiceOwnerChangedData socd;
884 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
886 ; /* good, expected */
890 warn_unexpected (connection, message, "method return for Hello");
895 retry_get_hello_name:
896 if (!dbus_message_get_args (message, &error,
897 DBUS_TYPE_STRING, &name,
900 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
902 _dbus_verbose ("no memory to get service name arg from hello\n");
903 dbus_error_free (&error);
904 _dbus_wait_for_memory ();
905 goto retry_get_hello_name;
909 _dbus_assert (dbus_error_is_set (&error));
910 _dbus_warn ("Did not get the expected single string argument to hello\n");
915 _dbus_verbose ("Got hello name: %s\n", name);
917 while (!dbus_bus_set_unique_name (connection, name))
918 _dbus_wait_for_memory ();
920 socd.expected_kind = SERVICE_CREATED;
921 socd.expected_service_name = name;
923 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
924 bus_test_clients_foreach (check_service_owner_changed_foreach,
930 name_message = message;
931 /* Client should also have gotten ServiceAcquired */
933 message = pop_message_waiting_for_memory (connection);
936 _dbus_warn ("Expecting %s, got nothing\n",
940 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
943 _dbus_warn ("Expecting %s, got smthg else\n",
948 retry_get_acquired_name:
949 if (!dbus_message_get_args (message, &error,
950 DBUS_TYPE_STRING, &acquired,
953 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
955 _dbus_verbose ("no memory to get service name arg from acquired\n");
956 dbus_error_free (&error);
957 _dbus_wait_for_memory ();
958 goto retry_get_acquired_name;
962 _dbus_assert (dbus_error_is_set (&error));
963 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
968 _dbus_verbose ("Got acquired name: %s\n", acquired);
970 if (strcmp (acquired, name) != 0)
972 _dbus_warn ("Acquired name is %s but expected %s\n",
979 if (!check_no_leftovers (context))
985 _dbus_verbose ("ending - retval = %d\n", retval);
987 dbus_error_free (&error);
990 dbus_message_unref (message);
993 dbus_message_unref (name_message);
998 /* returns TRUE if the correct thing happens,
999 * but the correct thing may include OOM errors.
1002 check_double_hello_message (BusContext *context,
1003 DBusConnection *connection)
1005 DBusMessage *message;
1006 dbus_uint32_t serial;
1011 dbus_error_init (&error);
1014 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1016 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1018 DBUS_INTERFACE_DBUS,
1021 if (message == NULL)
1024 if (!dbus_connection_send (connection, message, &serial))
1026 dbus_message_unref (message);
1030 dbus_message_unref (message);
1033 /* send our message */
1034 bus_test_run_clients_loop (SEND_PENDING (connection));
1036 dbus_connection_ref (connection); /* because we may get disconnected */
1037 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1039 if (!dbus_connection_get_is_connected (connection))
1041 _dbus_verbose ("connection was disconnected\n");
1043 dbus_connection_unref (connection);
1048 dbus_connection_unref (connection);
1050 message = pop_message_waiting_for_memory (connection);
1051 if (message == NULL)
1053 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1054 "Hello", serial, connection);
1058 verbose_message_received (connection, message);
1060 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1062 _dbus_warn ("Message has wrong sender %s\n",
1063 dbus_message_get_sender (message) ?
1064 dbus_message_get_sender (message) : "(none)");
1068 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1070 warn_unexpected (connection, message, "method return for Hello");
1074 if (!check_no_leftovers (context))
1080 dbus_error_free (&error);
1083 dbus_message_unref (message);
1088 /* returns TRUE if the correct thing happens,
1089 * but the correct thing may include OOM errors.
1092 check_get_connection_unix_user (BusContext *context,
1093 DBusConnection *connection)
1095 DBusMessage *message;
1096 dbus_uint32_t serial;
1099 const char *base_service_name;
1103 dbus_error_init (&error);
1106 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1108 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1110 DBUS_INTERFACE_DBUS,
1111 "GetConnectionUnixUser");
1113 if (message == NULL)
1116 base_service_name = dbus_bus_get_unique_name (connection);
1118 if (!dbus_message_append_args (message,
1119 DBUS_TYPE_STRING, &base_service_name,
1122 dbus_message_unref (message);
1126 if (!dbus_connection_send (connection, message, &serial))
1128 dbus_message_unref (message);
1132 /* send our message */
1133 bus_test_run_clients_loop (SEND_PENDING (connection));
1135 dbus_message_unref (message);
1138 dbus_connection_ref (connection); /* because we may get disconnected */
1139 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1141 if (!dbus_connection_get_is_connected (connection))
1143 _dbus_verbose ("connection was disconnected\n");
1145 dbus_connection_unref (connection);
1150 dbus_connection_unref (connection);
1152 message = pop_message_waiting_for_memory (connection);
1153 if (message == NULL)
1155 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1156 "GetConnectionUnixUser", serial, connection);
1160 verbose_message_received (connection, message);
1162 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1164 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1166 ; /* good, this is a valid response */
1170 warn_unexpected (connection, message, "not this error");
1177 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1179 ; /* good, expected */
1183 warn_unexpected (connection, message,
1184 "method_return for GetConnectionUnixUser");
1191 if (!dbus_message_get_args (message, &error,
1192 DBUS_TYPE_UINT32, &uid,
1195 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1197 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1198 dbus_error_free (&error);
1199 _dbus_wait_for_memory ();
1200 goto retry_get_property;
1204 _dbus_assert (dbus_error_is_set (&error));
1205 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1211 if (!check_no_leftovers (context))
1217 dbus_error_free (&error);
1220 dbus_message_unref (message);
1225 /* returns TRUE if the correct thing happens,
1226 * but the correct thing may include OOM errors.
1229 check_get_connection_unix_process_id (BusContext *context,
1230 DBusConnection *connection)
1232 DBusMessage *message;
1233 dbus_uint32_t serial;
1236 const char *base_service_name;
1240 dbus_error_init (&error);
1243 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1245 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1247 DBUS_INTERFACE_DBUS,
1248 "GetConnectionUnixProcessID");
1250 if (message == NULL)
1253 base_service_name = dbus_bus_get_unique_name (connection);
1255 if (!dbus_message_append_args (message,
1256 DBUS_TYPE_STRING, &base_service_name,
1259 dbus_message_unref (message);
1263 if (!dbus_connection_send (connection, message, &serial))
1265 dbus_message_unref (message);
1269 /* send our message */
1270 bus_test_run_clients_loop (SEND_PENDING (connection));
1272 dbus_message_unref (message);
1275 dbus_connection_ref (connection); /* because we may get disconnected */
1276 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1278 if (!dbus_connection_get_is_connected (connection))
1280 _dbus_verbose ("connection was disconnected\n");
1282 dbus_connection_unref (connection);
1287 dbus_connection_unref (connection);
1289 message = pop_message_waiting_for_memory (connection);
1290 if (message == NULL)
1292 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1293 "GetConnectionUnixProcessID", serial, connection);
1297 verbose_message_received (connection, message);
1299 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1301 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1303 ; /* good, this is a valid response */
1306 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1308 /* We are expecting this error, since we know in the test suite we aren't
1309 * talking to a client running on UNIX
1311 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1316 warn_unexpected (connection, message, "not this error");
1324 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1327 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1329 ; /* good, expected */
1333 warn_unexpected (connection, message,
1334 "method_return for GetConnectionUnixProcessID");
1341 if (!dbus_message_get_args (message, &error,
1342 DBUS_TYPE_UINT32, &pid,
1345 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1347 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1348 dbus_error_free (&error);
1349 _dbus_wait_for_memory ();
1350 goto retry_get_property;
1354 _dbus_assert (dbus_error_is_set (&error));
1355 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1361 /* test if returned pid is the same as our own pid
1363 * @todo It would probably be good to restructure the tests
1364 * in a way so our parent is the bus that we're testing
1365 * cause then we can test that the pid returned matches
1368 if (pid != (dbus_uint32_t) _dbus_getpid ())
1370 _dbus_assert (dbus_error_is_set (&error));
1371 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1375 #endif /* !DBUS_WIN */
1378 if (!check_no_leftovers (context))
1384 dbus_error_free (&error);
1387 dbus_message_unref (message);
1392 /* returns TRUE if the correct thing happens,
1393 * but the correct thing may include OOM errors.
1396 check_add_match_all (BusContext *context,
1397 DBusConnection *connection)
1399 DBusMessage *message;
1401 dbus_uint32_t serial;
1403 const char *empty = "";
1406 dbus_error_init (&error);
1409 _dbus_verbose ("check_add_match_all for %p\n", connection);
1411 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1413 DBUS_INTERFACE_DBUS,
1416 if (message == NULL)
1419 /* empty string match rule matches everything */
1420 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1423 dbus_message_unref (message);
1427 if (!dbus_connection_send (connection, message, &serial))
1429 dbus_message_unref (message);
1433 dbus_message_unref (message);
1436 dbus_connection_ref (connection); /* because we may get disconnected */
1438 /* send our message */
1439 bus_test_run_clients_loop (SEND_PENDING (connection));
1441 if (!dbus_connection_get_is_connected (connection))
1443 _dbus_verbose ("connection was disconnected\n");
1445 dbus_connection_unref (connection);
1450 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1452 if (!dbus_connection_get_is_connected (connection))
1454 _dbus_verbose ("connection was disconnected\n");
1456 dbus_connection_unref (connection);
1461 dbus_connection_unref (connection);
1463 message = pop_message_waiting_for_memory (connection);
1464 if (message == NULL)
1466 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1467 "AddMatch", serial, connection);
1471 verbose_message_received (connection, message);
1473 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1475 _dbus_warn ("Message has wrong sender %s\n",
1476 dbus_message_get_sender (message) ?
1477 dbus_message_get_sender (message) : "(none)");
1481 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1483 if (dbus_message_is_error (message,
1484 DBUS_ERROR_NO_MEMORY))
1486 ; /* good, this is a valid response */
1490 warn_unexpected (connection, message, "not this error");
1497 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1499 ; /* good, expected */
1500 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1504 warn_unexpected (connection, message, "method return for AddMatch");
1510 if (!check_no_leftovers (context))
1516 dbus_error_free (&error);
1519 dbus_message_unref (message);
1524 /* returns TRUE if the correct thing happens,
1525 * but the correct thing may include OOM errors.
1528 check_hello_connection (BusContext *context)
1530 DBusConnection *connection;
1533 dbus_error_init (&error);
1535 connection = dbus_connection_open_private (TEST_CONNECTION, &error);
1536 if (connection == NULL)
1538 _DBUS_ASSERT_ERROR_IS_SET (&error);
1539 dbus_error_free (&error);
1543 if (!bus_setup_debug_client (connection))
1545 dbus_connection_close (connection);
1546 dbus_connection_unref (connection);
1550 spin_connection_until_authenticated (context, connection);
1552 if (!check_hello_message (context, connection))
1555 if (dbus_bus_get_unique_name (connection) == NULL)
1557 /* We didn't successfully register, so we can't
1558 * do the usual kill_client_connection() checks
1560 kill_client_connection_unchecked (connection);
1564 if (!check_add_match_all (context, connection))
1567 kill_client_connection (context, connection);
1573 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1575 /* returns TRUE if the correct thing happens,
1576 * but the correct thing may include OOM errors.
1579 check_nonexistent_service_no_auto_start (BusContext *context,
1580 DBusConnection *connection)
1582 DBusMessage *message;
1583 dbus_uint32_t serial;
1585 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1586 dbus_uint32_t flags;
1588 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1590 DBUS_INTERFACE_DBUS,
1591 "StartServiceByName");
1593 if (message == NULL)
1596 dbus_message_set_auto_start (message, FALSE);
1599 if (!dbus_message_append_args (message,
1600 DBUS_TYPE_STRING, &nonexistent,
1601 DBUS_TYPE_UINT32, &flags,
1604 dbus_message_unref (message);
1608 if (!dbus_connection_send (connection, message, &serial))
1610 dbus_message_unref (message);
1614 dbus_message_unref (message);
1617 bus_test_run_everything (context);
1618 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1619 bus_test_run_everything (context);
1621 if (!dbus_connection_get_is_connected (connection))
1623 _dbus_verbose ("connection was disconnected\n");
1629 message = pop_message_waiting_for_memory (connection);
1630 if (message == NULL)
1632 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1633 "StartServiceByName", serial, connection);
1637 verbose_message_received (connection, message);
1639 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1641 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1643 _dbus_warn ("Message has wrong sender %s\n",
1644 dbus_message_get_sender (message) ?
1645 dbus_message_get_sender (message) : "(none)");
1649 if (dbus_message_is_error (message,
1650 DBUS_ERROR_NO_MEMORY))
1652 ; /* good, this is a valid response */
1654 else if (dbus_message_is_error (message,
1655 DBUS_ERROR_SERVICE_UNKNOWN))
1657 ; /* good, this is expected also */
1661 warn_unexpected (connection, message, "not this error");
1667 _dbus_warn ("Did not expect to successfully activate %s\n",
1668 NONEXISTENT_SERVICE_NAME);
1676 dbus_message_unref (message);
1681 /* returns TRUE if the correct thing happens,
1682 * but the correct thing may include OOM errors.
1685 check_nonexistent_service_auto_start (BusContext *context,
1686 DBusConnection *connection)
1688 DBusMessage *message;
1689 dbus_uint32_t serial;
1692 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1693 "/org/freedesktop/TestSuite",
1694 "org.freedesktop.TestSuite",
1697 if (message == NULL)
1700 if (!dbus_connection_send (connection, message, &serial))
1702 dbus_message_unref (message);
1706 dbus_message_unref (message);
1709 bus_test_run_everything (context);
1710 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1711 bus_test_run_everything (context);
1713 if (!dbus_connection_get_is_connected (connection))
1715 _dbus_verbose ("connection was disconnected\n");
1721 message = pop_message_waiting_for_memory (connection);
1723 if (message == NULL)
1725 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1726 "Echo message (auto activation)", serial, connection);
1730 verbose_message_received (connection, message);
1732 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1734 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1736 _dbus_warn ("Message has wrong sender %s\n",
1737 dbus_message_get_sender (message) ?
1738 dbus_message_get_sender (message) : "(none)");
1742 if (dbus_message_is_error (message,
1743 DBUS_ERROR_NO_MEMORY))
1745 ; /* good, this is a valid response */
1747 else if (dbus_message_is_error (message,
1748 DBUS_ERROR_SERVICE_UNKNOWN))
1750 ; /* good, this is expected also */
1754 warn_unexpected (connection, message, "not this error");
1760 _dbus_warn ("Did not expect to successfully activate %s\n",
1761 NONEXISTENT_SERVICE_NAME);
1769 dbus_message_unref (message);
1775 check_base_service_activated (BusContext *context,
1776 DBusConnection *connection,
1777 DBusMessage *initial_message,
1778 const char **base_service_p)
1780 DBusMessage *message;
1783 const char *base_service, *base_service_from_bus, *old_owner;
1787 dbus_error_init (&error);
1788 base_service = NULL;
1790 base_service_from_bus = NULL;
1792 message = initial_message;
1793 dbus_message_ref (message);
1795 if (dbus_message_is_signal (message,
1796 DBUS_INTERFACE_DBUS,
1797 "NameOwnerChanged"))
1799 CheckServiceOwnerChangedData socd;
1801 reget_service_name_arg:
1802 base_service = NULL;
1804 base_service_from_bus = NULL;
1806 if (!dbus_message_get_args (message, &error,
1807 DBUS_TYPE_STRING, &base_service,
1808 DBUS_TYPE_STRING, &old_owner,
1809 DBUS_TYPE_STRING, &base_service_from_bus,
1812 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1814 dbus_error_free (&error);
1815 _dbus_wait_for_memory ();
1816 goto reget_service_name_arg;
1820 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1821 "NameOwnerChanged (creation)",
1827 if (*base_service != ':')
1829 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1834 if (strcmp (base_service, base_service_from_bus) != 0)
1836 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1837 base_service, base_service_from_bus);
1843 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1848 socd.expected_kind = SERVICE_CREATED;
1849 socd.expected_service_name = base_service;
1850 socd.failed = FALSE;
1851 socd.skip_connection = connection;
1852 bus_test_clients_foreach (check_service_owner_changed_foreach,
1860 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1866 *base_service_p = base_service;
1872 dbus_message_unref (message);
1873 dbus_error_free (&error);
1879 check_service_activated (BusContext *context,
1880 DBusConnection *connection,
1881 const char *activated_name,
1882 const char *base_service_name,
1883 DBusMessage *initial_message)
1885 DBusMessage *message;
1888 dbus_uint32_t activation_result;
1892 dbus_error_init (&error);
1894 message = initial_message;
1895 dbus_message_ref (message);
1897 if (dbus_message_is_signal (message,
1898 DBUS_INTERFACE_DBUS,
1899 "NameOwnerChanged"))
1901 CheckServiceOwnerChangedData socd;
1902 const char *service_name, *base_service_from_bus, *old_owner;
1904 reget_service_name_arg:
1905 service_name = NULL;
1907 base_service_from_bus = NULL;
1909 if (!dbus_message_get_args (message, &error,
1910 DBUS_TYPE_STRING, &service_name,
1911 DBUS_TYPE_STRING, &old_owner,
1912 DBUS_TYPE_STRING, &base_service_from_bus,
1915 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1917 dbus_error_free (&error);
1918 _dbus_wait_for_memory ();
1919 goto reget_service_name_arg;
1923 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1924 "NameOwnerChanged (creation)",
1930 if (strcmp (service_name, activated_name) != 0)
1932 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1933 activated_name, service_name);
1937 if (strcmp (base_service_name, base_service_from_bus) != 0)
1939 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1940 base_service_from_bus, base_service_name);
1946 _dbus_warn ("expected a %s, got a %s\n",
1947 "NameOwnerChanged (creation)",
1948 "NameOwnerChanged (change)");
1952 socd.expected_kind = SERVICE_CREATED;
1953 socd.skip_connection = connection;
1954 socd.failed = FALSE;
1955 socd.expected_service_name = service_name;
1956 bus_test_clients_foreach (check_service_owner_changed_foreach,
1962 dbus_message_unref (message);
1963 service_name = NULL;
1965 base_service_from_bus = NULL;
1967 message = pop_message_waiting_for_memory (connection);
1968 if (message == NULL)
1970 _dbus_warn ("Expected a reply to %s, got nothing\n",
1971 "StartServiceByName");
1977 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1982 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1984 warn_unexpected (connection, message, "reply to StartServiceByName");
1989 activation_result = 0;
1990 if (!dbus_message_get_args (message, &error,
1991 DBUS_TYPE_UINT32, &activation_result,
1994 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1996 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1997 "StartServiceByName", error.message);
2001 dbus_error_free (&error);
2005 if (activation_result == DBUS_START_REPLY_SUCCESS)
2007 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2011 _dbus_warn ("Activation result was %u, no good.\n",
2017 dbus_message_unref (message);
2020 if (!check_no_leftovers (context))
2022 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2030 dbus_message_unref (message);
2031 dbus_error_free (&error);
2037 check_service_auto_activated (BusContext *context,
2038 DBusConnection *connection,
2039 const char *activated_name,
2040 const char *base_service_name,
2041 DBusMessage *initial_message)
2043 DBusMessage *message;
2049 dbus_error_init (&error);
2051 message = initial_message;
2052 dbus_message_ref (message);
2054 if (dbus_message_is_signal (message,
2055 DBUS_INTERFACE_DBUS,
2056 "NameOwnerChanged"))
2058 const char *service_name;
2059 CheckServiceOwnerChangedData socd;
2061 reget_service_name_arg:
2062 if (!dbus_message_get_args (message, &error,
2063 DBUS_TYPE_STRING, &service_name,
2066 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2068 dbus_error_free (&error);
2069 _dbus_wait_for_memory ();
2070 goto reget_service_name_arg;
2074 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2077 dbus_error_free (&error);
2082 if (strcmp (service_name, activated_name) != 0)
2084 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2085 activated_name, service_name);
2089 socd.expected_kind = SERVICE_CREATED;
2090 socd.expected_service_name = service_name;
2091 socd.failed = FALSE;
2092 socd.skip_connection = connection;
2093 bus_test_clients_foreach (check_service_owner_changed_foreach,
2099 /* Note that this differs from regular activation in that we don't get a
2100 * reply to ActivateService here.
2103 dbus_message_unref (message);
2105 service_name = NULL;
2109 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2118 dbus_message_unref (message);
2124 check_service_deactivated (BusContext *context,
2125 DBusConnection *connection,
2126 const char *activated_name,
2127 const char *base_service)
2130 CheckServiceOwnerChangedData socd;
2134 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2135 * service and the activated_name. The base service
2136 * notification is required to come last.
2138 socd.expected_kind = SERVICE_DELETED;
2139 socd.expected_service_name = activated_name;
2140 socd.failed = FALSE;
2141 socd.skip_connection = NULL;
2142 bus_test_clients_foreach (check_service_owner_changed_foreach,
2148 socd.expected_kind = SERVICE_DELETED;
2149 socd.expected_service_name = base_service;
2150 socd.failed = FALSE;
2151 socd.skip_connection = NULL;
2152 bus_test_clients_foreach (check_service_owner_changed_foreach,
2165 check_send_exit_to_service (BusContext *context,
2166 DBusConnection *connection,
2167 const char *service_name,
2168 const char *base_service)
2170 dbus_bool_t got_error;
2171 DBusMessage *message;
2172 dbus_uint32_t serial;
2175 _dbus_verbose ("Sending exit message to the test service\n");
2179 /* Kill off the test service by sending it a quit message */
2180 message = dbus_message_new_method_call (service_name,
2181 "/org/freedesktop/TestSuite",
2182 "org.freedesktop.TestSuite",
2185 if (message == NULL)
2187 /* Do this again; we still need the service to exit... */
2188 if (!check_send_exit_to_service (context, connection,
2189 service_name, base_service))
2195 if (!dbus_connection_send (connection, message, &serial))
2197 dbus_message_unref (message);
2199 /* Do this again; we still need the service to exit... */
2200 if (!check_send_exit_to_service (context, connection,
2201 service_name, base_service))
2207 dbus_message_unref (message);
2211 bus_test_run_clients_loop (SEND_PENDING (connection));
2213 /* read it in and write it out to test service */
2214 bus_test_run_bus_loop (context, FALSE);
2216 /* see if we got an error during message bus dispatching */
2217 bus_test_run_clients_loop (FALSE);
2218 message = borrow_message_waiting_for_memory (connection);
2219 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2222 dbus_connection_return_message (connection, message);
2228 /* If no error, wait for the test service to exit */
2229 block_connection_until_message_from_bus (context, connection, "test service to exit");
2231 bus_test_run_everything (context);
2236 message = pop_message_waiting_for_memory (connection);
2237 _dbus_assert (message != NULL);
2239 if (dbus_message_get_reply_serial (message) != serial)
2241 warn_unexpected (connection, message,
2242 "error with the correct reply serial");
2246 if (!dbus_message_is_error (message,
2247 DBUS_ERROR_NO_MEMORY))
2249 warn_unexpected (connection, message,
2250 "a no memory error from asking test service to exit");
2254 _dbus_verbose ("Got error %s when asking test service to exit\n",
2255 dbus_message_get_error_name (message));
2257 /* Do this again; we still need the service to exit... */
2258 if (!check_send_exit_to_service (context, connection,
2259 service_name, base_service))
2264 if (!check_service_deactivated (context, connection,
2265 service_name, base_service))
2268 /* Should now have a NoReply error from the Exit() method
2269 * call; it should have come after all the deactivation
2272 message = pop_message_waiting_for_memory (connection);
2274 if (message == NULL)
2276 warn_unexpected (connection, NULL,
2277 "reply to Exit() method call");
2280 if (!dbus_message_is_error (message,
2281 DBUS_ERROR_NO_REPLY))
2283 warn_unexpected (connection, message,
2284 "NoReply error from Exit() method call");
2288 if (dbus_message_get_reply_serial (message) != serial)
2290 warn_unexpected (connection, message,
2291 "error with the correct reply serial");
2295 _dbus_verbose ("Got error %s after test service exited\n",
2296 dbus_message_get_error_name (message));
2298 if (!check_no_leftovers (context))
2300 _dbus_warn ("Messages were left over after %s\n",
2301 _DBUS_FUNCTION_NAME);
2310 dbus_message_unref (message);
2316 check_got_error (BusContext *context,
2317 DBusConnection *connection,
2318 const char *first_error_name,
2321 DBusMessage *message;
2324 dbus_bool_t error_found;
2325 const char *error_name;
2329 message = pop_message_waiting_for_memory (connection);
2330 if (message == NULL)
2332 _dbus_warn ("Did not get an expected error\n");
2336 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2338 warn_unexpected (connection, message, "an error");
2343 error_found = FALSE;
2345 va_start (ap, first_error_name);
2346 error_name = first_error_name;
2347 while (error_name != NULL)
2349 if (dbus_message_is_error (message, error_name))
2354 error_name = va_arg (ap, char*);
2360 _dbus_warn ("Expected error %s or other, got %s instead\n",
2362 dbus_message_get_error_name (message));
2370 dbus_message_unref (message);
2377 GOT_SERVICE_CREATED,
2378 GOT_SERVICE_DELETED,
2383 static GotServiceInfo
2384 check_got_service_info (DBusMessage *message)
2386 GotServiceInfo message_kind;
2388 if (dbus_message_is_signal (message,
2389 DBUS_INTERFACE_DBUS,
2390 "NameOwnerChanged"))
2393 const char *service_name, *old_owner, *new_owner;
2394 dbus_error_init (&error);
2396 reget_service_info_data:
2397 service_name = NULL;
2401 dbus_message_get_args (message, &error,
2402 DBUS_TYPE_STRING, &service_name,
2403 DBUS_TYPE_STRING, &old_owner,
2404 DBUS_TYPE_STRING, &new_owner,
2406 if (dbus_error_is_set (&error))
2408 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2410 dbus_error_free (&error);
2411 goto reget_service_info_data;
2415 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2416 message_kind = GOT_SOMETHING_ELSE;
2419 else if (!old_owner[0])
2420 message_kind = GOT_SERVICE_CREATED;
2421 else if (!new_owner[0])
2422 message_kind = GOT_SERVICE_DELETED;
2424 message_kind = GOT_SOMETHING_ELSE;
2426 dbus_error_free (&error);
2428 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2429 message_kind = GOT_ERROR;
2431 message_kind = GOT_SOMETHING_ELSE;
2433 return message_kind;
2436 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2438 /* returns TRUE if the correct thing happens,
2439 * but the correct thing may include OOM errors.
2442 check_existent_service_no_auto_start (BusContext *context,
2443 DBusConnection *connection)
2445 DBusMessage *message;
2446 DBusMessage *base_service_message;
2447 const char *base_service;
2448 dbus_uint32_t serial;
2450 const char *existent = EXISTENT_SERVICE_NAME;
2451 dbus_uint32_t flags;
2453 base_service_message = NULL;
2455 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2457 DBUS_INTERFACE_DBUS,
2458 "StartServiceByName");
2460 if (message == NULL)
2463 dbus_message_set_auto_start (message, FALSE);
2466 if (!dbus_message_append_args (message,
2467 DBUS_TYPE_STRING, &existent,
2468 DBUS_TYPE_UINT32, &flags,
2471 dbus_message_unref (message);
2475 if (!dbus_connection_send (connection, message, &serial))
2477 dbus_message_unref (message);
2481 dbus_message_unref (message);
2484 bus_test_run_everything (context);
2486 /* now wait for the message bus to hear back from the activated
2489 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2491 bus_test_run_everything (context);
2493 if (!dbus_connection_get_is_connected (connection))
2495 _dbus_verbose ("connection was disconnected\n");
2501 message = pop_message_waiting_for_memory (connection);
2502 if (message == NULL)
2504 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2505 "StartServiceByName", serial, connection);
2509 verbose_message_received (connection, message);
2510 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2512 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2514 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2516 _dbus_warn ("Message has wrong sender %s\n",
2517 dbus_message_get_sender (message) ?
2518 dbus_message_get_sender (message) : "(none)");
2522 if (dbus_message_is_error (message,
2523 DBUS_ERROR_NO_MEMORY))
2525 ; /* good, this is a valid response */
2527 else if (dbus_message_is_error (message,
2528 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2529 dbus_message_is_error (message,
2530 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2531 dbus_message_is_error (message,
2532 DBUS_ERROR_SPAWN_EXEC_FAILED))
2534 ; /* good, this is expected also */
2538 _dbus_warn ("Did not expect error %s\n",
2539 dbus_message_get_error_name (message));
2545 GotServiceInfo message_kind;
2547 if (!check_base_service_activated (context, connection,
2548 message, &base_service))
2551 base_service_message = message;
2554 /* We may need to block here for the test service to exit or finish up */
2555 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2557 message = dbus_connection_borrow_message (connection);
2558 if (message == NULL)
2560 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2564 message_kind = check_got_service_info (message);
2566 dbus_connection_return_message (connection, message);
2569 switch (message_kind)
2571 case GOT_SOMETHING_ELSE:
2572 _dbus_warn ("Unexpected message after ActivateService "
2573 "(should be an error or a service announcement");
2577 if (!check_got_error (context, connection,
2578 DBUS_ERROR_SPAWN_CHILD_EXITED,
2579 DBUS_ERROR_NO_MEMORY,
2582 /* A service deleted should be coming along now after this error.
2583 * We can also get the error *after* the service deleted.
2588 case GOT_SERVICE_DELETED:
2590 /* The service started up and got a base address, but then
2591 * failed to register under EXISTENT_SERVICE_NAME
2593 CheckServiceOwnerChangedData socd;
2595 socd.expected_kind = SERVICE_DELETED;
2596 socd.expected_service_name = base_service;
2597 socd.failed = FALSE;
2598 socd.skip_connection = NULL;
2600 bus_test_clients_foreach (check_service_owner_changed_foreach,
2606 /* Now we should get an error about the service exiting
2607 * if we didn't get it before.
2609 if (message_kind != GOT_ERROR)
2611 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2613 /* and process everything again */
2614 bus_test_run_everything (context);
2616 if (!check_got_error (context, connection,
2617 DBUS_ERROR_SPAWN_CHILD_EXITED,
2618 DBUS_ERROR_NO_MEMORY,
2625 case GOT_SERVICE_CREATED:
2626 message = pop_message_waiting_for_memory (connection);
2627 if (message == NULL)
2629 _dbus_warn ("Failed to pop message we just put back! "
2630 "should have been a NameOwnerChanged (creation)\n");
2634 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2635 base_service, message))
2638 dbus_message_unref (message);
2641 if (!check_no_leftovers (context))
2643 _dbus_warn ("Messages were left over after successful activation\n");
2647 if (!check_send_exit_to_service (context, connection,
2648 EXISTENT_SERVICE_NAME, base_service))
2659 dbus_message_unref (message);
2661 if (base_service_message)
2662 dbus_message_unref (base_service_message);
2667 #ifndef DBUS_WIN_FIXME
2668 /* returns TRUE if the correct thing happens,
2669 * but the correct thing may include OOM errors.
2672 check_segfault_service_no_auto_start (BusContext *context,
2673 DBusConnection *connection)
2675 DBusMessage *message;
2676 dbus_uint32_t serial;
2678 const char *segv_service;
2679 dbus_uint32_t flags;
2681 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2683 DBUS_INTERFACE_DBUS,
2684 "StartServiceByName");
2686 if (message == NULL)
2689 dbus_message_set_auto_start (message, FALSE);
2691 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2693 if (!dbus_message_append_args (message,
2694 DBUS_TYPE_STRING, &segv_service,
2695 DBUS_TYPE_UINT32, &flags,
2698 dbus_message_unref (message);
2702 if (!dbus_connection_send (connection, message, &serial))
2704 dbus_message_unref (message);
2708 dbus_message_unref (message);
2711 bus_test_run_everything (context);
2712 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2713 bus_test_run_everything (context);
2715 if (!dbus_connection_get_is_connected (connection))
2717 _dbus_verbose ("connection was disconnected\n");
2723 message = pop_message_waiting_for_memory (connection);
2724 if (message == NULL)
2726 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2727 "StartServiceByName", serial, connection);
2731 verbose_message_received (connection, message);
2733 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2735 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2737 _dbus_warn ("Message has wrong sender %s\n",
2738 dbus_message_get_sender (message) ?
2739 dbus_message_get_sender (message) : "(none)");
2743 if (dbus_message_is_error (message,
2744 DBUS_ERROR_NO_MEMORY))
2746 ; /* good, this is a valid response */
2748 else if (dbus_message_is_error (message,
2751 const char *servicehelper;
2752 servicehelper = bus_context_get_servicehelper (context);
2753 /* make sure this only happens with the launch helper */
2754 _dbus_assert (servicehelper != NULL);
2756 else if (dbus_message_is_error (message,
2757 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2759 ; /* good, this is expected also */
2763 warn_unexpected (connection, message, "not this error");
2770 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2778 dbus_message_unref (message);
2784 /* returns TRUE if the correct thing happens,
2785 * but the correct thing may include OOM errors.
2788 check_segfault_service_auto_start (BusContext *context,
2789 DBusConnection *connection)
2791 DBusMessage *message;
2792 dbus_uint32_t serial;
2795 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2796 "/org/freedesktop/TestSuite",
2797 "org.freedesktop.TestSuite",
2800 if (message == NULL)
2803 if (!dbus_connection_send (connection, message, &serial))
2805 dbus_message_unref (message);
2809 dbus_message_unref (message);
2812 bus_test_run_everything (context);
2813 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2814 bus_test_run_everything (context);
2816 if (!dbus_connection_get_is_connected (connection))
2818 _dbus_verbose ("connection was disconnected\n");
2824 message = pop_message_waiting_for_memory (connection);
2825 if (message == NULL)
2827 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2828 "Echo message (auto activation)", serial, connection);
2832 verbose_message_received (connection, message);
2834 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2836 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2838 _dbus_warn ("Message has wrong sender %s\n",
2839 dbus_message_get_sender (message) ?
2840 dbus_message_get_sender (message) : "(none)");
2844 if (dbus_message_is_error (message,
2845 DBUS_ERROR_NO_MEMORY))
2847 ; /* good, this is a valid response */
2849 else if (dbus_message_is_error (message,
2850 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2852 ; /* good, this is expected also */
2856 warn_unexpected (connection, message, "not this error");
2863 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2871 dbus_message_unref (message);
2877 #define TEST_ECHO_MESSAGE "Test echo message"
2878 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2880 /* returns TRUE if the correct thing happens,
2881 * but the correct thing may include OOM errors.
2884 check_existent_hello_from_self (BusContext *context,
2885 DBusConnection *connection)
2887 DBusMessage *message;
2888 dbus_uint32_t serial;
2891 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2892 "/org/freedesktop/TestSuite",
2893 "org.freedesktop.TestSuite",
2894 "RunHelloFromSelf");
2896 if (message == NULL)
2899 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2900 if (!dbus_message_append_args (message,
2901 DBUS_TYPE_STRING, &text,
2904 dbus_message_unref (message);
2908 if (!dbus_connection_send (connection, message, &serial))
2910 dbus_message_unref (message);
2914 dbus_message_unref (message);
2917 bus_test_run_everything (context);
2919 /* Note: if this test is run in OOM mode, it will block when the bus
2920 * doesn't send a reply due to OOM.
2922 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2924 message = pop_message_waiting_for_memory (connection);
2925 if (message == NULL)
2927 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2931 if (dbus_message_get_reply_serial (message) != serial)
2933 _dbus_warn ("Wrong reply serial\n");
2934 dbus_message_unref (message);
2938 dbus_message_unref (message);
2944 /* returns TRUE if the correct thing happens,
2945 * but the correct thing may include OOM errors.
2948 check_existent_ping (BusContext *context,
2949 DBusConnection *connection)
2951 DBusMessage *message;
2952 dbus_uint32_t serial;
2953 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2954 "/org/freedesktop/TestSuite",
2955 "org.freedesktop.DBus.Peer",
2958 if (message == NULL)
2961 if (!dbus_connection_send (connection, message, &serial))
2963 dbus_message_unref (message);
2967 dbus_message_unref (message);
2970 bus_test_run_everything (context);
2972 /* Note: if this test is run in OOM mode, it will block when the bus
2973 * doesn't send a reply due to OOM.
2975 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2977 message = pop_message_waiting_for_memory (connection);
2978 if (message == NULL)
2980 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2984 if (dbus_message_get_reply_serial (message) != serial)
2986 _dbus_warn ("Wrong reply serial\n");
2987 dbus_message_unref (message);
2991 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2993 _dbus_warn ("Unexpected message return during Ping\n");
2994 dbus_message_unref (message);
2998 dbus_message_unref (message);
3004 /* returns TRUE if the correct thing happens,
3005 * but the correct thing may include OOM errors.
3008 check_existent_get_machine_id (BusContext *context,
3009 DBusConnection *connection)
3011 DBusMessage *message;
3012 dbus_uint32_t serial;
3013 const char *machine_id;
3015 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3016 "/org/freedesktop/TestSuite",
3017 "org.freedesktop.DBus.Peer",
3020 if (message == NULL)
3023 if (!dbus_connection_send (connection, message, &serial))
3025 dbus_message_unref (message);
3029 dbus_message_unref (message);
3032 bus_test_run_everything (context);
3034 /* Note: if this test is run in OOM mode, it will block when the bus
3035 * doesn't send a reply due to OOM.
3037 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3039 message = pop_message_waiting_for_memory (connection);
3040 if (message == NULL)
3042 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3046 if (dbus_message_get_reply_serial (message) != serial)
3048 _dbus_warn ("Wrong reply serial\n");
3049 dbus_message_unref (message);
3053 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3055 _dbus_warn ("Unexpected message return during GetMachineId\n");
3056 dbus_message_unref (message);
3061 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3063 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3064 dbus_message_unref (message);
3068 if (machine_id == NULL || strlen (machine_id) != 32)
3070 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3071 dbus_message_unref (message);
3075 /* We can't check that the machine id is correct because during make check it is
3076 * just made up for each process separately
3079 dbus_message_unref (message);
3085 /* returns TRUE if the correct thing happens,
3086 * but the correct thing may include OOM errors.
3089 check_existent_service_auto_start (BusContext *context,
3090 DBusConnection *connection)
3092 DBusMessage *message;
3093 DBusMessage *base_service_message;
3094 dbus_uint32_t serial;
3096 const char *base_service;
3099 base_service_message = NULL;
3101 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3102 "/org/freedesktop/TestSuite",
3103 "org.freedesktop.TestSuite",
3106 if (message == NULL)
3109 text = TEST_ECHO_MESSAGE;
3110 if (!dbus_message_append_args (message,
3111 DBUS_TYPE_STRING, &text,
3114 dbus_message_unref (message);
3118 if (!dbus_connection_send (connection, message, &serial))
3120 dbus_message_unref (message);
3124 dbus_message_unref (message);
3127 bus_test_run_everything (context);
3129 /* now wait for the message bus to hear back from the activated
3132 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3133 bus_test_run_everything (context);
3135 if (!dbus_connection_get_is_connected (connection))
3137 _dbus_verbose ("connection was disconnected\n");
3143 message = pop_message_waiting_for_memory (connection);
3144 if (message == NULL)
3146 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3147 serial, connection);
3151 verbose_message_received (connection, message);
3152 _dbus_verbose (" (after sending %s)\n", "auto start");
3154 /* we should get zero or two ServiceOwnerChanged signals */
3155 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3157 GotServiceInfo message_kind;
3159 if (!check_base_service_activated (context, connection,
3160 message, &base_service))
3163 base_service_message = message;
3166 /* We may need to block here for the test service to exit or finish up */
3167 block_connection_until_message_from_bus (context, connection, "service to exit");
3169 /* Should get a service creation notification for the activated
3170 * service name, or a service deletion on the base service name
3172 message = dbus_connection_borrow_message (connection);
3173 if (message == NULL)
3175 _dbus_warn ("No message after auto activation "
3176 "(should be a service announcement)\n");
3177 dbus_connection_return_message (connection, message);
3182 message_kind = check_got_service_info (message);
3184 dbus_connection_return_message (connection, message);
3187 switch (message_kind)
3189 case GOT_SERVICE_CREATED:
3190 message = pop_message_waiting_for_memory (connection);
3191 if (message == NULL)
3193 _dbus_warn ("Failed to pop message we just put back! "
3194 "should have been a NameOwnerChanged (creation)\n");
3198 /* Check that ServiceOwnerChanged (creation) was correctly received */
3199 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3200 base_service, message))
3203 dbus_message_unref (message);
3208 case GOT_SERVICE_DELETED:
3210 /* The service started up and got a base address, but then
3211 * failed to register under EXISTENT_SERVICE_NAME
3213 CheckServiceOwnerChangedData socd;
3215 socd.expected_kind = SERVICE_DELETED;
3216 socd.expected_service_name = base_service;
3217 socd.failed = FALSE;
3218 socd.skip_connection = NULL;
3219 bus_test_clients_foreach (check_service_owner_changed_foreach,
3229 case GOT_SOMETHING_ELSE:
3230 _dbus_warn ("Unexpected message after auto activation\n");
3235 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3236 * come the method reply (or error) from the initial method call
3239 /* Note: if this test is run in OOM mode, it will block when the bus
3240 * doesn't send a reply due to OOM.
3242 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3244 message = pop_message_waiting_for_memory (connection);
3245 if (message == NULL)
3247 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3251 if (dbus_message_get_reply_serial (message) != serial)
3253 _dbus_warn ("Wrong reply serial\n");
3257 dbus_message_unref (message);
3260 if (!check_existent_ping (context, connection))
3263 if (!check_existent_get_machine_id (context, connection))
3266 if (!check_existent_hello_from_self (context, connection))
3269 if (!check_send_exit_to_service (context, connection,
3270 EXISTENT_SERVICE_NAME,
3278 dbus_message_unref (message);
3280 if (base_service_message)
3281 dbus_message_unref (base_service_message);
3286 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3288 /* returns TRUE if the correct thing happens,
3289 * but the correct thing may include OOM errors.
3292 check_launch_service_file_missing (BusContext *context,
3293 DBusConnection *connection)
3295 DBusMessage *message;
3296 dbus_uint32_t serial;
3299 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3300 "/org/freedesktop/TestSuite",
3301 "org.freedesktop.TestSuite",
3304 if (message == NULL)
3307 if (!dbus_connection_send (connection, message, &serial))
3309 dbus_message_unref (message);
3313 dbus_message_unref (message);
3316 bus_test_run_everything (context);
3317 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3318 bus_test_run_everything (context);
3320 if (!dbus_connection_get_is_connected (connection))
3322 _dbus_verbose ("connection was disconnected\n");
3328 message = pop_message_waiting_for_memory (connection);
3329 if (message == NULL)
3331 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3332 "Echo message (auto activation)", serial, connection);
3336 verbose_message_received (connection, message);
3338 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3340 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3342 _dbus_warn ("Message has wrong sender %s\n",
3343 dbus_message_get_sender (message) ?
3344 dbus_message_get_sender (message) : "(none)");
3348 if (dbus_message_is_error (message,
3349 DBUS_ERROR_NO_MEMORY))
3351 ; /* good, this is a valid response */
3353 else if (dbus_message_is_error (message,
3354 DBUS_ERROR_SERVICE_UNKNOWN))
3356 _dbus_verbose("got service unknown\n");
3357 ; /* good, this is expected (only valid when using launch helper) */
3361 warn_unexpected (connection, message, "not this error");
3368 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3376 dbus_message_unref (message);
3381 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3383 /* returns TRUE if the correct thing happens,
3384 * but the correct thing may include OOM errors.
3387 check_launch_service_user_missing (BusContext *context,
3388 DBusConnection *connection)
3390 DBusMessage *message;
3391 dbus_uint32_t serial;
3394 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3395 "/org/freedesktop/TestSuite",
3396 "org.freedesktop.TestSuite",
3399 if (message == NULL)
3402 if (!dbus_connection_send (connection, message, &serial))
3404 dbus_message_unref (message);
3408 dbus_message_unref (message);
3411 bus_test_run_everything (context);
3412 block_connection_until_message_from_bus (context, connection,
3413 "reply to service which should fail to auto-start (missing User)");
3414 bus_test_run_everything (context);
3416 if (!dbus_connection_get_is_connected (connection))
3418 _dbus_warn ("connection was disconnected\n");
3424 message = pop_message_waiting_for_memory (connection);
3425 if (message == NULL)
3427 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3428 "Echo message (auto activation)", serial, connection);
3432 verbose_message_received (connection, message);
3434 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3436 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3438 _dbus_warn ("Message has wrong sender %s\n",
3439 dbus_message_get_sender (message) ?
3440 dbus_message_get_sender (message) : "(none)");
3444 if (dbus_message_is_error (message,
3445 DBUS_ERROR_NO_MEMORY))
3447 ; /* good, this is a valid response */
3449 else if (dbus_message_is_error (message,
3450 DBUS_ERROR_SPAWN_FILE_INVALID))
3452 _dbus_verbose("got service file invalid\n");
3453 ; /* good, this is expected (only valid when using launch helper) */
3457 warn_unexpected (connection, message, "not this error");
3464 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3472 dbus_message_unref (message);
3477 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3479 /* returns TRUE if the correct thing happens,
3480 * but the correct thing may include OOM errors.
3483 check_launch_service_exec_missing (BusContext *context,
3484 DBusConnection *connection)
3486 DBusMessage *message;
3487 dbus_uint32_t serial;
3490 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3491 "/org/freedesktop/TestSuite",
3492 "org.freedesktop.TestSuite",
3495 if (message == NULL)
3498 if (!dbus_connection_send (connection, message, &serial))
3500 dbus_message_unref (message);
3504 dbus_message_unref (message);
3507 bus_test_run_everything (context);
3508 block_connection_until_message_from_bus (context, connection,
3509 "reply to service which should fail to auto-start (missing Exec)");
3510 bus_test_run_everything (context);
3512 if (!dbus_connection_get_is_connected (connection))
3514 _dbus_warn ("connection was disconnected\n");
3520 message = pop_message_waiting_for_memory (connection);
3521 if (message == NULL)
3523 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3524 "Echo message (auto activation)", serial, connection);
3528 verbose_message_received (connection, message);
3530 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3532 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3534 _dbus_warn ("Message has wrong sender %s\n",
3535 dbus_message_get_sender (message) ?
3536 dbus_message_get_sender (message) : "(none)");
3540 if (dbus_message_is_error (message,
3541 DBUS_ERROR_NO_MEMORY))
3543 ; /* good, this is a valid response */
3545 else if (dbus_message_is_error (message,
3546 DBUS_ERROR_SERVICE_UNKNOWN))
3548 _dbus_verbose("could not activate as invalid service file was not added\n");
3549 ; /* good, this is expected as we shouldn't have been added to
3550 * the activation list with a missing Exec key */
3552 else if (dbus_message_is_error (message,
3553 DBUS_ERROR_SPAWN_FILE_INVALID))
3555 _dbus_verbose("got service file invalid\n");
3556 ; /* good, this is allowed, and is the message passed back from the
3561 warn_unexpected (connection, message, "not this error");
3568 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3576 dbus_message_unref (message);
3581 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3583 /* returns TRUE if the correct thing happens,
3584 * but the correct thing may include OOM errors.
3587 check_launch_service_service_missing (BusContext *context,
3588 DBusConnection *connection)
3590 DBusMessage *message;
3591 dbus_uint32_t serial;
3594 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3595 "/org/freedesktop/TestSuite",
3596 "org.freedesktop.TestSuite",
3599 if (message == NULL)
3602 if (!dbus_connection_send (connection, message, &serial))
3604 dbus_message_unref (message);
3608 dbus_message_unref (message);
3611 bus_test_run_everything (context);
3612 block_connection_until_message_from_bus (context, connection,
3613 "reply to service which should fail to auto-start (missing Service)");
3614 bus_test_run_everything (context);
3616 if (!dbus_connection_get_is_connected (connection))
3618 _dbus_warn ("connection was disconnected\n");
3624 message = pop_message_waiting_for_memory (connection);
3625 if (message == NULL)
3627 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3628 "Echo message (auto activation)", serial, connection);
3632 verbose_message_received (connection, message);
3634 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3636 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3638 _dbus_warn ("Message has wrong sender %s\n",
3639 dbus_message_get_sender (message) ?
3640 dbus_message_get_sender (message) : "(none)");
3644 if (dbus_message_is_error (message,
3645 DBUS_ERROR_NO_MEMORY))
3647 ; /* good, this is a valid response */
3649 else if (dbus_message_is_error (message,
3650 DBUS_ERROR_SERVICE_UNKNOWN))
3652 _dbus_verbose("could not activate as invalid service file was not added\n");
3653 ; /* good, this is expected as we shouldn't have been added to
3654 * the activation list with a missing Exec key */
3656 else if (dbus_message_is_error (message,
3657 DBUS_ERROR_SPAWN_FILE_INVALID))
3659 _dbus_verbose("got service file invalid\n");
3660 ; /* good, this is allowed, and is the message passed back from the
3665 warn_unexpected (connection, message, "not this error");
3672 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3680 dbus_message_unref (message);
3685 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3687 /* returns TRUE if the correct thing happens,
3688 * but the correct thing may include OOM errors.
3691 check_shell_fail_service_auto_start (BusContext *context,
3692 DBusConnection *connection)
3694 DBusMessage *message;
3695 dbus_uint32_t serial;
3698 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3699 "/org/freedesktop/TestSuite",
3700 "org.freedesktop.TestSuite",
3703 if (message == NULL)
3706 if (!dbus_connection_send (connection, message, &serial))
3708 dbus_message_unref (message);
3712 dbus_message_unref (message);
3715 bus_test_run_everything (context);
3716 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3717 bus_test_run_everything (context);
3719 if (!dbus_connection_get_is_connected (connection))
3721 _dbus_verbose ("connection was disconnected\n");
3727 message = pop_message_waiting_for_memory (connection);
3728 if (message == NULL)
3730 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3731 "Echo message (auto activation)", serial, connection);
3735 verbose_message_received (connection, message);
3737 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3739 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3741 _dbus_warn ("Message has wrong sender %s\n",
3742 dbus_message_get_sender (message) ?
3743 dbus_message_get_sender (message) : "(none)");
3747 if (dbus_message_is_error (message,
3748 DBUS_ERROR_NO_MEMORY))
3750 ; /* good, this is a valid response */
3752 else if (dbus_message_is_error (message,
3753 DBUS_ERROR_INVALID_ARGS))
3755 _dbus_verbose("got invalid args\n");
3756 ; /* good, this is expected also */
3760 warn_unexpected (connection, message, "not this error");
3767 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3775 dbus_message_unref (message);
3780 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3782 /* returns TRUE if the correct thing happens,
3783 * but the correct thing may include OOM errors.
3786 check_shell_service_success_auto_start (BusContext *context,
3787 DBusConnection *connection)
3789 DBusMessage *message;
3790 DBusMessage *base_service_message;
3791 dbus_uint32_t serial;
3793 const char *base_service;
3794 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3796 base_service_message = NULL;
3798 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3799 "/org/freedesktop/TestSuite",
3800 "org.freedesktop.TestSuite",
3803 if (message == NULL)
3806 if (!dbus_connection_send (connection, message, &serial))
3808 dbus_message_unref (message);
3812 dbus_message_unref (message);
3815 bus_test_run_everything (context);
3817 /* now wait for the message bus to hear back from the activated
3820 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3821 bus_test_run_everything (context);
3823 if (!dbus_connection_get_is_connected (connection))
3825 _dbus_verbose ("connection was disconnected\n");
3831 message = pop_message_waiting_for_memory (connection);
3832 if (message == NULL)
3834 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3835 serial, connection);
3839 verbose_message_received (connection, message);
3840 _dbus_verbose (" (after sending %s)\n", "auto start");
3842 /* we should get zero or two ServiceOwnerChanged signals */
3843 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3845 GotServiceInfo message_kind;
3847 if (!check_base_service_activated (context, connection,
3848 message, &base_service))
3851 base_service_message = message;
3854 /* We may need to block here for the test service to exit or finish up */
3855 block_connection_until_message_from_bus (context, connection, "service to exit");
3857 /* Should get a service creation notification for the activated
3858 * service name, or a service deletion on the base service name
3860 message = dbus_connection_borrow_message (connection);
3861 if (message == NULL)
3863 _dbus_warn ("No message after auto activation "
3864 "(should be a service announcement)\n");
3865 dbus_connection_return_message (connection, message);
3870 message_kind = check_got_service_info (message);
3872 dbus_connection_return_message (connection, message);
3875 switch (message_kind)
3877 case GOT_SERVICE_CREATED:
3878 message = pop_message_waiting_for_memory (connection);
3879 if (message == NULL)
3881 _dbus_warn ("Failed to pop message we just put back! "
3882 "should have been a NameOwnerChanged (creation)\n");
3886 /* Check that ServiceOwnerChanged (creation) was correctly received */
3887 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3888 base_service, message))
3891 dbus_message_unref (message);
3896 case GOT_SERVICE_DELETED:
3898 /* The service started up and got a base address, but then
3899 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3901 CheckServiceOwnerChangedData socd;
3903 socd.expected_kind = SERVICE_DELETED;
3904 socd.expected_service_name = base_service;
3905 socd.failed = FALSE;
3906 socd.skip_connection = NULL;
3907 bus_test_clients_foreach (check_service_owner_changed_foreach,
3917 case GOT_SOMETHING_ELSE:
3918 _dbus_warn ("Unexpected message after auto activation\n");
3923 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3924 * come the method reply (or error) from the initial method call
3927 /* Note: if this test is run in OOM mode, it will block when the bus
3928 * doesn't send a reply due to OOM.
3930 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3932 message = pop_message_waiting_for_memory (connection);
3933 if (message == NULL)
3935 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3939 if (dbus_message_get_reply_serial (message) != serial)
3941 _dbus_warn ("Wrong reply serial\n");
3945 if (!dbus_message_get_args (message, NULL,
3946 DBUS_TYPE_STRING, &argv[0],
3947 DBUS_TYPE_STRING, &argv[1],
3948 DBUS_TYPE_STRING, &argv[2],
3949 DBUS_TYPE_STRING, &argv[3],
3950 DBUS_TYPE_STRING, &argv[4],
3951 DBUS_TYPE_STRING, &argv[5],
3952 DBUS_TYPE_STRING, &argv[6],
3955 _dbus_warn ("Error getting arguments from return\n");
3959 /* don't worry about arg[0] as it may be different
3960 depending on the path to the tests
3962 if (strcmp("-test", argv[1]) != 0)
3964 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3969 if (strcmp("that", argv[2]) != 0)
3971 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3976 if (strcmp("we get", argv[3]) != 0)
3978 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3983 if (strcmp("back", argv[4]) != 0)
3985 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3990 if (strcmp("--what", argv[5]) != 0)
3992 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3997 if (strcmp("we put in", argv[6]) != 0)
3999 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
4000 "we put in", argv[6]);
4004 dbus_message_unref (message);
4007 if (!check_send_exit_to_service (context, connection,
4008 SHELL_SUCCESS_SERVICE_NAME,
4016 dbus_message_unref (message);
4018 if (base_service_message)
4019 dbus_message_unref (base_service_message);
4027 BusContext *context;
4031 check_oom_check1_func (void *data)
4033 Check1Data *d = data;
4035 if (! (* d->func) (d->context))
4038 if (!check_no_leftovers (d->context))
4040 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4048 check1_try_iterations (BusContext *context,
4049 const char *description,
4055 d.context = context;
4057 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4059 _dbus_assert_not_reached ("test failed");
4063 check_get_services (BusContext *context,
4064 DBusConnection *connection,
4069 DBusMessage *message;
4070 dbus_uint32_t serial;
4077 dbus_error_init (&error);
4080 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4082 DBUS_INTERFACE_DBUS,
4085 if (message == NULL)
4088 if (!dbus_connection_send (connection, message, &serial))
4090 dbus_message_unref (message);
4094 /* send our message */
4095 bus_test_run_clients_loop (SEND_PENDING (connection));
4097 dbus_message_unref (message);
4100 dbus_connection_ref (connection); /* because we may get disconnected */
4101 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4103 if (!dbus_connection_get_is_connected (connection))
4105 _dbus_verbose ("connection was disconnected\n");
4107 dbus_connection_unref (connection);
4112 dbus_connection_unref (connection);
4114 message = pop_message_waiting_for_memory (connection);
4115 if (message == NULL)
4117 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4118 method, serial, connection);
4122 verbose_message_received (connection, message);
4124 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4126 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4128 ; /* good, this is a valid response */
4132 warn_unexpected (connection, message, "not this error");
4139 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4141 ; /* good, expected */
4145 warn_unexpected (connection, message,
4146 "method_return for ListActivatableNames/ListNames");
4153 if (!dbus_message_get_args (message, &error,
4159 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4161 _dbus_verbose ("no memory to list services by %s\n", method);
4162 dbus_error_free (&error);
4163 _dbus_wait_for_memory ();
4164 goto retry_get_property;
4168 _dbus_assert (dbus_error_is_set (&error));
4169 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4178 if (!check_no_leftovers (context))
4184 dbus_error_free (&error);
4187 dbus_message_unref (message);
4192 /* returns TRUE if the correct thing happens,
4193 * but the correct thing may include OOM errors.
4196 check_list_services (BusContext *context,
4197 DBusConnection *connection)
4199 DBusMessage *message;
4200 DBusMessage *base_service_message;
4201 const char *base_service;
4202 dbus_uint32_t serial;
4204 const char *existent = EXISTENT_SERVICE_NAME;
4205 dbus_uint32_t flags;
4209 _dbus_verbose ("check_list_services for %p\n", connection);
4211 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4216 if (!_dbus_string_array_contains ((const char **)services, existent))
4218 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4219 dbus_free_string_array (services);
4223 dbus_free_string_array (services);
4225 base_service_message = NULL;
4227 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4229 DBUS_INTERFACE_DBUS,
4230 "StartServiceByName");
4232 if (message == NULL)
4235 dbus_message_set_auto_start (message, FALSE);
4238 if (!dbus_message_append_args (message,
4239 DBUS_TYPE_STRING, &existent,
4240 DBUS_TYPE_UINT32, &flags,
4243 dbus_message_unref (message);
4247 if (!dbus_connection_send (connection, message, &serial))
4249 dbus_message_unref (message);
4253 dbus_message_unref (message);
4256 bus_test_run_everything (context);
4258 /* now wait for the message bus to hear back from the activated
4261 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4263 bus_test_run_everything (context);
4265 if (!dbus_connection_get_is_connected (connection))
4267 _dbus_verbose ("connection was disconnected\n");
4273 message = pop_message_waiting_for_memory (connection);
4274 if (message == NULL)
4276 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4277 "StartServiceByName", serial, connection);
4281 verbose_message_received (connection, message);
4282 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4284 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4286 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4288 _dbus_warn ("Message has wrong sender %s\n",
4289 dbus_message_get_sender (message) ?
4290 dbus_message_get_sender (message) : "(none)");
4294 if (dbus_message_is_error (message,
4295 DBUS_ERROR_NO_MEMORY))
4297 ; /* good, this is a valid response */
4299 else if (dbus_message_is_error (message,
4300 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4301 dbus_message_is_error (message,
4302 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4303 dbus_message_is_error (message,
4304 DBUS_ERROR_SPAWN_EXEC_FAILED))
4306 ; /* good, this is expected also */
4310 _dbus_warn ("Did not expect error %s\n",
4311 dbus_message_get_error_name (message));
4317 GotServiceInfo message_kind;
4319 if (!check_base_service_activated (context, connection,
4320 message, &base_service))
4323 base_service_message = message;
4326 /* We may need to block here for the test service to exit or finish up */
4327 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4329 message = dbus_connection_borrow_message (connection);
4330 if (message == NULL)
4332 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4336 message_kind = check_got_service_info (message);
4338 dbus_connection_return_message (connection, message);
4341 switch (message_kind)
4343 case GOT_SOMETHING_ELSE:
4345 case GOT_SERVICE_DELETED:
4346 _dbus_warn ("Unexpected message after ActivateService "
4347 "(should be an error or a service announcement)\n");
4350 case GOT_SERVICE_CREATED:
4351 message = pop_message_waiting_for_memory (connection);
4352 if (message == NULL)
4354 _dbus_warn ("Failed to pop message we just put back! "
4355 "should have been a NameOwnerChanged (creation)\n");
4359 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4360 base_service, message))
4363 dbus_message_unref (message);
4366 if (!check_no_leftovers (context))
4368 _dbus_warn ("Messages were left over after successful activation\n");
4376 if (!check_get_services (context, connection, "ListNames", &services, &len))
4381 if (!_dbus_string_array_contains ((const char **)services, existent))
4383 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4387 dbus_free_string_array (services);
4389 if (!check_send_exit_to_service (context, connection,
4390 EXISTENT_SERVICE_NAME, base_service))
4397 dbus_message_unref (message);
4399 if (base_service_message)
4400 dbus_message_unref (base_service_message);
4408 BusContext *context;
4409 DBusConnection *connection;
4413 check_oom_check2_func (void *data)
4415 Check2Data *d = data;
4417 if (! (* d->func) (d->context, d->connection))
4420 if (!check_no_leftovers (d->context))
4422 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4430 check2_try_iterations (BusContext *context,
4431 DBusConnection *connection,
4432 const char *description,
4438 d.context = context;
4439 d.connection = connection;
4441 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4444 _dbus_warn ("%s failed during oom\n", description);
4445 _dbus_assert_not_reached ("test failed");
4450 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4451 const char *filename)
4456 if (!_dbus_string_init (&full))
4459 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4461 _dbus_string_free (&full);
4465 _dbus_string_init_const (&file, filename);
4467 if (!_dbus_concat_dir_and_file (&full, &file))
4469 _dbus_string_free (&full);
4473 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4474 _dbus_string_get_const_data (&full));
4476 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4478 _dbus_string_free (&full);
4484 bus_dispatch_test_conf (const DBusString *test_data_dir,
4485 const char *filename,
4486 dbus_bool_t use_launcher)
4488 BusContext *context;
4489 DBusConnection *foo;
4490 DBusConnection *bar;
4491 DBusConnection *baz;
4494 /* save the config name for the activation helper */
4495 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4496 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4498 dbus_error_init (&error);
4500 context = bus_context_new_test (test_data_dir, filename);
4501 if (context == NULL)
4504 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4506 _dbus_assert_not_reached ("could not alloc connection");
4508 if (!bus_setup_debug_client (foo))
4509 _dbus_assert_not_reached ("could not set up connection");
4511 spin_connection_until_authenticated (context, foo);
4513 if (!check_hello_message (context, foo))
4514 _dbus_assert_not_reached ("hello message failed");
4516 if (!check_double_hello_message (context, foo))
4517 _dbus_assert_not_reached ("double hello message failed");
4519 if (!check_add_match_all (context, foo))
4520 _dbus_assert_not_reached ("AddMatch message failed");
4522 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4524 _dbus_assert_not_reached ("could not alloc connection");
4526 if (!bus_setup_debug_client (bar))
4527 _dbus_assert_not_reached ("could not set up connection");
4529 spin_connection_until_authenticated (context, bar);
4531 if (!check_hello_message (context, bar))
4532 _dbus_assert_not_reached ("hello message failed");
4534 if (!check_add_match_all (context, bar))
4535 _dbus_assert_not_reached ("AddMatch message failed");
4537 baz = dbus_connection_open_private (TEST_CONNECTION, &error);
4539 _dbus_assert_not_reached ("could not alloc connection");
4541 if (!bus_setup_debug_client (baz))
4542 _dbus_assert_not_reached ("could not set up connection");
4544 spin_connection_until_authenticated (context, baz);
4546 if (!check_hello_message (context, baz))
4547 _dbus_assert_not_reached ("hello message failed");
4549 if (!check_add_match_all (context, baz))
4550 _dbus_assert_not_reached ("AddMatch message failed");
4552 #ifdef DBUS_WIN_FIXME
4553 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4554 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4556 if (!check_get_connection_unix_user (context, baz))
4557 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4559 if (!check_get_connection_unix_process_id (context, baz))
4560 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4563 if (!check_list_services (context, baz))
4564 _dbus_assert_not_reached ("ListActivatableNames message failed");
4566 if (!check_no_leftovers (context))
4568 _dbus_warn ("Messages were left over after setting up initial connections\n");
4569 _dbus_assert_not_reached ("initial connection setup failed");
4572 check1_try_iterations (context, "create_and_hello",
4573 check_hello_connection);
4575 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4576 check_nonexistent_service_no_auto_start);
4578 #ifdef DBUS_WIN_FIXME
4579 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4581 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4582 check_segfault_service_no_auto_start);
4585 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4586 check_existent_service_no_auto_start);
4588 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4589 check_nonexistent_service_auto_start);
4592 #ifdef DBUS_WIN_FIXME
4593 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4595 /* only do the segfault test if we are not using the launcher */
4596 check2_try_iterations (context, foo, "segfault_service_auto_start",
4597 check_segfault_service_auto_start);
4600 /* only do the shell fail test if we are not using the launcher */
4601 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4602 check_shell_fail_service_auto_start);
4604 /* specific to launcher */
4606 if (!check_launch_service_file_missing (context, foo))
4607 _dbus_assert_not_reached ("did not get service file not found error");
4610 /* Note: need to resolve some issues with the testing code in order to run
4611 * this in oom (handle that we sometimes don't get replies back from the bus
4612 * when oom happens, without blocking the test).
4614 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4615 check_existent_service_auto_start);
4618 if (!check_existent_service_auto_start (context, foo))
4619 _dbus_assert_not_reached ("existent service auto start failed");
4621 if (!check_shell_service_success_auto_start (context, foo))
4622 _dbus_assert_not_reached ("shell success service auto start failed");
4624 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4626 kill_client_connection_unchecked (foo);
4627 kill_client_connection_unchecked (bar);
4628 kill_client_connection_unchecked (baz);
4630 bus_context_unref (context);
4636 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4637 const char *filename)
4639 BusContext *context;
4640 DBusConnection *foo;
4643 /* save the config name for the activation helper */
4644 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4645 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4647 dbus_error_init (&error);
4649 context = bus_context_new_test (test_data_dir, filename);
4650 if (context == NULL)
4653 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4655 _dbus_assert_not_reached ("could not alloc connection");
4657 if (!bus_setup_debug_client (foo))
4658 _dbus_assert_not_reached ("could not set up connection");
4660 spin_connection_until_authenticated (context, foo);
4662 if (!check_hello_message (context, foo))
4663 _dbus_assert_not_reached ("hello message failed");
4665 if (!check_double_hello_message (context, foo))
4666 _dbus_assert_not_reached ("double hello message failed");
4668 if (!check_add_match_all (context, foo))
4669 _dbus_assert_not_reached ("AddMatch message failed");
4671 /* this only tests the activation.c user check */
4672 if (!check_launch_service_user_missing (context, foo))
4673 _dbus_assert_not_reached ("user missing did not trigger error");
4675 /* this only tests the desktop.c exec check */
4676 if (!check_launch_service_exec_missing (context, foo))
4677 _dbus_assert_not_reached ("exec missing did not trigger error");
4679 /* this only tests the desktop.c service check */
4680 if (!check_launch_service_service_missing (context, foo))
4681 _dbus_assert_not_reached ("service missing did not trigger error");
4683 _dbus_verbose ("Disconnecting foo\n");
4685 kill_client_connection_unchecked (foo);
4687 bus_context_unref (context);
4693 bus_dispatch_test (const DBusString *test_data_dir)
4695 /* run normal activation tests */
4696 _dbus_verbose ("Normal activation tests\n");
4697 if (!bus_dispatch_test_conf (test_data_dir,
4698 "valid-config-files/debug-allow-all.conf", FALSE))
4702 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4704 /* run launch-helper activation tests */
4705 _dbus_verbose ("Launch helper activation tests\n");
4706 if (!bus_dispatch_test_conf (test_data_dir,
4707 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4710 /* run select launch-helper activation tests on broken service files */
4711 if (!bus_dispatch_test_conf_fail (test_data_dir,
4712 "valid-config-files-system/debug-allow-all-fail.conf"))
4720 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4722 BusContext *context;
4723 DBusConnection *foo;
4726 dbus_error_init (&error);
4728 /* Test SHA1 authentication */
4729 _dbus_verbose ("Testing SHA1 context\n");
4731 context = bus_context_new_test (test_data_dir,
4732 "valid-config-files/debug-allow-all-sha1.conf");
4733 if (context == NULL)
4736 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4738 _dbus_assert_not_reached ("could not alloc connection");
4740 if (!bus_setup_debug_client (foo))
4741 _dbus_assert_not_reached ("could not set up connection");
4743 spin_connection_until_authenticated (context, foo);
4745 if (!check_hello_message (context, foo))
4746 _dbus_assert_not_reached ("hello message failed");
4748 if (!check_add_match_all (context, foo))
4749 _dbus_assert_not_reached ("addmatch message failed");
4751 if (!check_no_leftovers (context))
4753 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4754 _dbus_assert_not_reached ("initial connection setup failed");
4757 check1_try_iterations (context, "create_and_hello_sha1",
4758 check_hello_connection);
4760 kill_client_connection_unchecked (foo);
4762 bus_context_unref (context);
4767 #ifdef HAVE_UNIX_FD_PASSING
4770 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4772 BusContext *context;
4773 DBusConnection *foo, *bar;
4776 int one[2], two[2], x, y, z;
4779 dbus_error_init (&error);
4781 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4782 if (context == NULL)
4783 _dbus_assert_not_reached ("could not alloc context");
4785 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4787 _dbus_assert_not_reached ("could not alloc connection");
4789 if (!bus_setup_debug_client (foo))
4790 _dbus_assert_not_reached ("could not set up connection");
4792 spin_connection_until_authenticated (context, foo);
4794 if (!check_hello_message (context, foo))
4795 _dbus_assert_not_reached ("hello message failed");
4797 if (!check_add_match_all (context, foo))
4798 _dbus_assert_not_reached ("AddMatch message failed");
4800 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4802 _dbus_assert_not_reached ("could not alloc connection");
4804 if (!bus_setup_debug_client (bar))
4805 _dbus_assert_not_reached ("could not set up connection");
4807 spin_connection_until_authenticated (context, bar);
4809 if (!check_hello_message (context, bar))
4810 _dbus_assert_not_reached ("hello message failed");
4812 if (!check_add_match_all (context, bar))
4813 _dbus_assert_not_reached ("AddMatch message failed");
4815 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4816 _dbus_assert_not_reached ("could not alloc message");
4818 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4819 _dbus_assert_not_reached("Failed to allocate pipe #1");
4821 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4822 _dbus_assert_not_reached("Failed to allocate pipe #2");
4824 if (!dbus_message_append_args(m,
4825 DBUS_TYPE_UNIX_FD, one,
4826 DBUS_TYPE_UNIX_FD, two,
4827 DBUS_TYPE_UNIX_FD, two,
4829 _dbus_assert_not_reached("Failed to attach fds.");
4831 if (!_dbus_close(one[0], &error))
4832 _dbus_assert_not_reached("Failed to close pipe #1 ");
4833 if (!_dbus_close(two[0], &error))
4834 _dbus_assert_not_reached("Failed to close pipe #2 ");
4836 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4837 _dbus_assert_not_reached("Connection cannot do fd passing");
4839 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4840 _dbus_assert_not_reached("Connection cannot do fd passing");
4842 if (!dbus_connection_send (foo, m, NULL))
4843 _dbus_assert_not_reached("Failed to send fds");
4845 dbus_message_unref(m);
4847 bus_test_run_clients_loop (SEND_PENDING (foo));
4849 bus_test_run_everything (context);
4851 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4853 if (!(m = pop_message_waiting_for_memory (foo)))
4854 _dbus_assert_not_reached("Failed to receive msg");
4856 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4857 _dbus_assert_not_reached("bogus message received");
4859 dbus_message_unref(m);
4861 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4863 if (!(m = pop_message_waiting_for_memory (bar)))
4864 _dbus_assert_not_reached("Failed to receive msg");
4866 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4867 _dbus_assert_not_reached("bogus message received");
4869 if (!dbus_message_get_args(m,
4871 DBUS_TYPE_UNIX_FD, &x,
4872 DBUS_TYPE_UNIX_FD, &y,
4873 DBUS_TYPE_UNIX_FD, &z,
4875 _dbus_assert_not_reached("Failed to parse fds.");
4877 dbus_message_unref(m);
4879 if (write(x, "X", 1) != 1)
4880 _dbus_assert_not_reached("Failed to write to pipe #1");
4881 if (write(y, "Y", 1) != 1)
4882 _dbus_assert_not_reached("Failed to write to pipe #2");
4883 if (write(z, "Z", 1) != 1)
4884 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4886 if (!_dbus_close(x, &error))
4887 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4888 if (!_dbus_close(y, &error))
4889 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4890 if (!_dbus_close(z, &error))
4891 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4893 if (read(one[1], &r, 1) != 1 || r != 'X')
4894 _dbus_assert_not_reached("Failed to read value from pipe.");
4895 if (read(two[1], &r, 1) != 1 || r != 'Y')
4896 _dbus_assert_not_reached("Failed to read value from pipe.");
4897 if (read(two[1], &r, 1) != 1 || r != 'Z')
4898 _dbus_assert_not_reached("Failed to read value from pipe.");
4900 if (!_dbus_close(one[1], &error))
4901 _dbus_assert_not_reached("Failed to close pipe #1 ");
4902 if (!_dbus_close(two[1], &error))
4903 _dbus_assert_not_reached("Failed to close pipe #2 ");
4905 _dbus_verbose ("Disconnecting foo\n");
4906 kill_client_connection_unchecked (foo);
4908 _dbus_verbose ("Disconnecting bar\n");
4909 kill_client_connection_unchecked (bar);
4911 bus_context_unref (context);
4917 #endif /* DBUS_BUILD_TESTS */