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>
37 #include <dbus/dbus-misc.h>
40 #ifdef HAVE_UNIX_FD_PASSING
41 #include <dbus/dbus-sysdeps-unix.h>
45 /* This is hard-coded in the files in valid-config-files-*. We have to use
46 * the debug-pipe transport because the tests in this file require that
47 * dbus_connection_open_private() does not block. */
48 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
51 send_one_message (DBusConnection *connection,
53 DBusConnection *sender,
54 DBusConnection *addressed_recipient,
56 BusTransaction *transaction,
59 if (!bus_context_check_security_policy (context, transaction,
65 return TRUE; /* silently don't send it */
67 if (dbus_message_contains_unix_fds(message) &&
68 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
69 return TRUE; /* silently don't send it */
71 if (!bus_transaction_send (transaction,
83 bus_dispatch_matches (BusTransaction *transaction,
84 DBusConnection *sender,
85 DBusConnection *addressed_recipient,
90 BusConnections *connections;
92 BusMatchmaker *matchmaker;
96 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
98 /* sender and recipient can both be NULL for the bus driver,
99 * or for signals with no particular recipient
102 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
103 _dbus_assert (dbus_message_get_sender (message) != NULL);
105 context = bus_transaction_get_context (transaction);
107 /* First, send the message to the addressed_recipient, if there is one. */
108 if (addressed_recipient != NULL)
110 if (!bus_context_check_security_policy (context, transaction,
111 sender, addressed_recipient,
116 if (dbus_message_contains_unix_fds (message) &&
117 !dbus_connection_can_send_type (addressed_recipient,
120 dbus_set_error (error,
121 DBUS_ERROR_NOT_SUPPORTED,
122 "Tried to send message with Unix file descriptors"
123 "to a client that doesn't support that.");
127 /* Dispatch the message */
128 if (!bus_transaction_send (transaction, addressed_recipient, message))
135 /* Now dispatch to others who look interested in this message */
136 connections = bus_transaction_get_connections (transaction);
137 dbus_error_init (&tmp_error);
138 matchmaker = bus_context_get_matchmaker (context);
141 if (!bus_matchmaker_get_recipients (matchmaker, connections,
142 sender, addressed_recipient, message,
149 link = _dbus_list_get_first_link (&recipients);
152 DBusConnection *dest;
156 if (!send_one_message (dest, context, sender, addressed_recipient,
157 message, transaction, &tmp_error))
160 link = _dbus_list_get_next_link (&recipients, link);
163 _dbus_list_clear (&recipients);
165 if (dbus_error_is_set (&tmp_error))
167 dbus_move_error (&tmp_error, error);
174 static DBusHandlerResult
175 bus_dispatch (DBusConnection *connection,
176 DBusMessage *message)
178 const char *sender, *service_name;
180 BusTransaction *transaction;
182 DBusHandlerResult result;
183 DBusConnection *addressed_recipient;
185 result = DBUS_HANDLER_RESULT_HANDLED;
188 addressed_recipient = NULL;
189 dbus_error_init (&error);
191 context = bus_connection_get_context (connection);
192 _dbus_assert (context != NULL);
194 /* If we can't even allocate an OOM error, we just go to sleep
197 while (!bus_connection_preallocate_oom_error (connection))
198 _dbus_wait_for_memory ();
200 /* Ref connection in case we disconnect it at some point in here */
201 dbus_connection_ref (connection);
203 service_name = dbus_message_get_destination (message);
205 #ifdef DBUS_ENABLE_VERBOSE_MODE
207 const char *interface_name, *member_name, *error_name;
209 interface_name = dbus_message_get_interface (message);
210 member_name = dbus_message_get_member (message);
211 error_name = dbus_message_get_error_name (message);
213 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
214 interface_name ? interface_name : "(no interface)",
215 member_name ? member_name : "(no member)",
216 error_name ? error_name : "(no error name)",
217 service_name ? service_name : "peer");
219 #endif /* DBUS_ENABLE_VERBOSE_MODE */
221 /* If service_name is NULL, if it's a signal we send it to all
222 * connections with a match rule. If it's not a signal, there
223 * are some special cases here but mostly we just bail out.
225 if (service_name == NULL)
227 if (dbus_message_is_signal (message,
228 DBUS_INTERFACE_LOCAL,
231 bus_connection_disconnected (connection);
235 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
237 /* DBusConnection also handles some of these automatically, we leave
240 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245 /* Create our transaction */
246 transaction = bus_transaction_new (context);
247 if (transaction == NULL)
249 BUS_SET_OOM (&error);
253 /* Assign a sender to the message */
254 if (bus_connection_is_active (connection))
256 sender = bus_connection_get_name (connection);
257 _dbus_assert (sender != NULL);
259 if (!dbus_message_set_sender (message, sender))
261 BUS_SET_OOM (&error);
265 /* We need to refetch the service name here, because
266 * dbus_message_set_sender can cause the header to be
267 * reallocated, and thus the service_name pointer will become
270 service_name = dbus_message_get_destination (message);
274 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
276 if (!bus_context_check_security_policy (context, transaction,
277 connection, NULL, NULL, message, &error))
279 _dbus_verbose ("Security policy rejected message\n");
283 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
284 if (!bus_driver_handle_message (connection, transaction, message, &error))
287 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
289 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
290 dbus_connection_close (connection);
293 else if (service_name != NULL) /* route to named service */
295 DBusString service_string;
297 BusRegistry *registry;
299 _dbus_assert (service_name != NULL);
301 registry = bus_connection_get_registry (connection);
303 _dbus_string_init_const (&service_string, service_name);
304 service = bus_registry_lookup (registry, &service_string);
306 if (service == NULL && dbus_message_get_auto_start (message))
308 BusActivation *activation;
309 /* We can't do the security policy check here, since the addressed
310 * recipient service doesn't exist yet. We do it before sending the
311 * message after the service has been created.
313 activation = bus_connection_get_activation (connection);
315 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
316 message, service_name, &error))
318 _DBUS_ASSERT_ERROR_IS_SET (&error);
319 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
325 else if (service == NULL)
327 dbus_set_error (&error,
328 DBUS_ERROR_NAME_HAS_NO_OWNER,
329 "Name \"%s\" does not exist",
335 addressed_recipient = bus_service_get_primary_owners_connection (service);
336 _dbus_assert (addressed_recipient != NULL);
340 /* Now send the message to its destination (or not, if
341 * addressed_recipient == NULL), and match it against other connections'
344 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
348 if (dbus_error_is_set (&error))
350 if (!dbus_connection_get_is_connected (connection))
352 /* If we disconnected it, we won't bother to send it any error
355 _dbus_verbose ("Not sending error to connection we disconnected\n");
357 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
359 bus_connection_send_oom_error (connection, message);
361 /* cancel transaction due to OOM */
362 if (transaction != NULL)
364 bus_transaction_cancel_and_free (transaction);
370 /* Try to send the real error, if no mem to do that, send
373 _dbus_assert (transaction != NULL);
374 if (!bus_transaction_send_error_reply (transaction, connection,
377 bus_connection_send_oom_error (connection, message);
379 /* cancel transaction due to OOM */
380 if (transaction != NULL)
382 bus_transaction_cancel_and_free (transaction);
389 dbus_error_free (&error);
392 if (transaction != NULL)
394 bus_transaction_execute_and_free (transaction);
397 dbus_connection_unref (connection);
402 static DBusHandlerResult
403 bus_dispatch_message_filter (DBusConnection *connection,
404 DBusMessage *message,
407 return bus_dispatch (connection, message);
411 bus_dispatch_add_connection (DBusConnection *connection)
413 if (!dbus_connection_add_filter (connection,
414 bus_dispatch_message_filter,
422 bus_dispatch_remove_connection (DBusConnection *connection)
424 /* Here we tell the bus driver that we want to get off. */
425 bus_driver_remove_connection (connection);
427 dbus_connection_remove_filter (connection,
428 bus_dispatch_message_filter,
432 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
436 /* This is used to know whether we need to block in order to finish
437 * sending a message, or whether the initial dbus_connection_send()
438 * already flushed the queue.
440 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
442 typedef dbus_bool_t (* Check1Func) (BusContext *context);
443 typedef dbus_bool_t (* Check2Func) (BusContext *context,
444 DBusConnection *connection);
446 static dbus_bool_t check_no_leftovers (BusContext *context);
449 block_connection_until_message_from_bus (BusContext *context,
450 DBusConnection *connection,
451 const char *what_is_expected)
453 _dbus_verbose ("expecting: %s\n", what_is_expected);
455 while (dbus_connection_get_dispatch_status (connection) ==
456 DBUS_DISPATCH_COMPLETE &&
457 dbus_connection_get_is_connected (connection))
459 bus_test_run_bus_loop (context, TRUE);
460 bus_test_run_clients_loop (FALSE);
465 spin_connection_until_authenticated (BusContext *context,
466 DBusConnection *connection)
468 _dbus_verbose ("Spinning to auth connection %p\n", connection);
469 while (!dbus_connection_get_is_authenticated (connection) &&
470 dbus_connection_get_is_connected (connection))
472 bus_test_run_bus_loop (context, FALSE);
473 bus_test_run_clients_loop (FALSE);
475 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
478 /* compensate for fact that pop_message() can return #NULL due to OOM */
480 pop_message_waiting_for_memory (DBusConnection *connection)
482 while (dbus_connection_get_dispatch_status (connection) ==
483 DBUS_DISPATCH_NEED_MEMORY)
484 _dbus_wait_for_memory ();
486 return dbus_connection_pop_message (connection);
490 borrow_message_waiting_for_memory (DBusConnection *connection)
492 while (dbus_connection_get_dispatch_status (connection) ==
493 DBUS_DISPATCH_NEED_MEMORY)
494 _dbus_wait_for_memory ();
496 return dbus_connection_borrow_message (connection);
500 warn_unexpected_real (DBusConnection *connection,
501 DBusMessage *message,
502 const char *expected,
503 const char *function,
507 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
509 dbus_message_get_interface (message) ?
510 dbus_message_get_interface (message) : "(unset)",
511 dbus_message_get_member (message) ?
512 dbus_message_get_member (message) : "(unset)",
513 dbus_message_get_error_name (message) ?
514 dbus_message_get_error_name (message) : "(unset)",
518 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
519 function, line, connection, expected);
522 #define warn_unexpected(connection, message, expected) \
523 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
526 verbose_message_received (DBusConnection *connection,
527 DBusMessage *message)
529 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
530 dbus_message_get_interface (message) ?
531 dbus_message_get_interface (message) : "(unset)",
532 dbus_message_get_member (message) ?
533 dbus_message_get_member (message) : "(unset)",
534 dbus_message_get_error_name (message) ?
535 dbus_message_get_error_name (message) : "(unset)",
548 ServiceInfoKind expected_kind;
549 const char *expected_service_name;
551 DBusConnection *skip_connection;
552 } CheckServiceOwnerChangedData;
555 check_service_owner_changed_foreach (DBusConnection *connection,
558 CheckServiceOwnerChangedData *d = data;
559 DBusMessage *message;
561 const char *service_name, *old_owner, *new_owner;
563 if (d->expected_kind == SERVICE_CREATED
564 && connection == d->skip_connection)
567 dbus_error_init (&error);
570 message = pop_message_waiting_for_memory (connection);
573 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
574 connection, "NameOwnerChanged");
577 else if (!dbus_message_is_signal (message,
581 warn_unexpected (connection, message, "NameOwnerChanged");
587 reget_service_info_data:
592 dbus_message_get_args (message, &error,
593 DBUS_TYPE_STRING, &service_name,
594 DBUS_TYPE_STRING, &old_owner,
595 DBUS_TYPE_STRING, &new_owner,
598 if (dbus_error_is_set (&error))
600 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
602 dbus_error_free (&error);
603 _dbus_wait_for_memory ();
604 goto reget_service_info_data;
608 _dbus_warn ("Did not get the expected arguments\n");
613 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
614 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
615 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
617 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
621 if (strcmp (service_name, d->expected_service_name) != 0)
623 _dbus_warn ("expected info on service %s, got info on %s\n",
624 d->expected_service_name,
629 if (*service_name == ':' && new_owner[0]
630 && strcmp (service_name, new_owner) != 0)
632 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
633 service_name, old_owner, new_owner);
641 dbus_error_free (&error);
644 dbus_message_unref (message);
651 kill_client_connection (BusContext *context,
652 DBusConnection *connection)
656 CheckServiceOwnerChangedData socd;
658 _dbus_verbose ("killing connection %p\n", connection);
660 s = dbus_bus_get_unique_name (connection);
661 _dbus_assert (s != NULL);
663 while ((base_service = _dbus_strdup (s)) == NULL)
664 _dbus_wait_for_memory ();
666 dbus_connection_ref (connection);
668 /* kick in the disconnect handler that unrefs the connection */
669 dbus_connection_close (connection);
671 bus_test_run_everything (context);
673 _dbus_assert (bus_test_client_listed (connection));
675 /* Run disconnect handler in test.c */
676 if (bus_connection_dispatch_one_message (connection))
677 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
679 _dbus_assert (!dbus_connection_get_is_connected (connection));
680 dbus_connection_unref (connection);
682 _dbus_assert (!bus_test_client_listed (connection));
684 socd.expected_kind = SERVICE_DELETED;
685 socd.expected_service_name = base_service;
687 socd.skip_connection = NULL;
689 bus_test_clients_foreach (check_service_owner_changed_foreach,
692 dbus_free (base_service);
695 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
697 if (!check_no_leftovers (context))
698 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
702 kill_client_connection_unchecked (DBusConnection *connection)
704 /* This kills the connection without expecting it to affect
705 * the rest of the bus.
707 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
709 dbus_connection_ref (connection);
710 dbus_connection_close (connection);
711 /* dispatching disconnect handler will unref once */
712 if (bus_connection_dispatch_one_message (connection))
713 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
715 _dbus_assert (!bus_test_client_listed (connection));
716 dbus_connection_unref (connection);
722 } CheckNoMessagesData;
725 check_no_messages_foreach (DBusConnection *connection,
728 CheckNoMessagesData *d = data;
729 DBusMessage *message;
731 message = pop_message_waiting_for_memory (connection);
734 warn_unexpected (connection, message, "no messages");
740 dbus_message_unref (message);
745 check_no_leftovers (BusContext *context)
747 CheckNoMessagesData nmd;
750 bus_test_clients_foreach (check_no_messages_foreach,
755 _dbus_verbose ("leftover message found\n");
762 /* returns TRUE if the correct thing happens,
763 * but the correct thing may include OOM errors.
766 check_hello_message (BusContext *context,
767 DBusConnection *connection)
769 DBusMessage *message;
770 DBusMessage *name_message;
771 dbus_uint32_t serial;
775 const char *acquired;
778 dbus_error_init (&error);
784 _dbus_verbose ("check_hello_message for %p\n", connection);
786 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
794 dbus_connection_ref (connection); /* because we may get disconnected */
796 if (!dbus_connection_send (connection, message, &serial))
798 dbus_message_unref (message);
799 dbus_connection_unref (connection);
803 _dbus_assert (dbus_message_has_signature (message, ""));
805 dbus_message_unref (message);
808 if (!dbus_connection_get_is_connected (connection))
810 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
812 dbus_connection_unref (connection);
817 /* send our message */
818 bus_test_run_clients_loop (SEND_PENDING (connection));
820 if (!dbus_connection_get_is_connected (connection))
822 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
824 dbus_connection_unref (connection);
829 block_connection_until_message_from_bus (context, connection, "reply to Hello");
831 if (!dbus_connection_get_is_connected (connection))
833 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
835 dbus_connection_unref (connection);
840 dbus_connection_unref (connection);
842 message = pop_message_waiting_for_memory (connection);
845 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
846 "Hello", serial, connection);
850 verbose_message_received (connection, message);
852 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
854 _dbus_warn ("Message has wrong sender %s\n",
855 dbus_message_get_sender (message) ?
856 dbus_message_get_sender (message) : "(none)");
860 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
862 if (dbus_message_is_error (message,
863 DBUS_ERROR_NO_MEMORY))
865 ; /* good, this is a valid response */
869 warn_unexpected (connection, message, "not this error");
876 CheckServiceOwnerChangedData socd;
878 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
880 ; /* good, expected */
884 warn_unexpected (connection, message, "method return for Hello");
889 retry_get_hello_name:
890 if (!dbus_message_get_args (message, &error,
891 DBUS_TYPE_STRING, &name,
894 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
896 _dbus_verbose ("no memory to get service name arg from hello\n");
897 dbus_error_free (&error);
898 _dbus_wait_for_memory ();
899 goto retry_get_hello_name;
903 _dbus_assert (dbus_error_is_set (&error));
904 _dbus_warn ("Did not get the expected single string argument to hello\n");
909 _dbus_verbose ("Got hello name: %s\n", name);
911 while (!dbus_bus_set_unique_name (connection, name))
912 _dbus_wait_for_memory ();
914 socd.expected_kind = SERVICE_CREATED;
915 socd.expected_service_name = name;
917 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
918 bus_test_clients_foreach (check_service_owner_changed_foreach,
924 name_message = message;
925 /* Client should also have gotten ServiceAcquired */
927 message = pop_message_waiting_for_memory (connection);
930 _dbus_warn ("Expecting %s, got nothing\n",
934 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
937 _dbus_warn ("Expecting %s, got smthg else\n",
942 retry_get_acquired_name:
943 if (!dbus_message_get_args (message, &error,
944 DBUS_TYPE_STRING, &acquired,
947 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
949 _dbus_verbose ("no memory to get service name arg from acquired\n");
950 dbus_error_free (&error);
951 _dbus_wait_for_memory ();
952 goto retry_get_acquired_name;
956 _dbus_assert (dbus_error_is_set (&error));
957 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
962 _dbus_verbose ("Got acquired name: %s\n", acquired);
964 if (strcmp (acquired, name) != 0)
966 _dbus_warn ("Acquired name is %s but expected %s\n",
973 if (!check_no_leftovers (context))
979 _dbus_verbose ("ending - retval = %d\n", retval);
981 dbus_error_free (&error);
984 dbus_message_unref (message);
987 dbus_message_unref (name_message);
992 /* returns TRUE if the correct thing happens,
993 * but the correct thing may include OOM errors.
996 check_double_hello_message (BusContext *context,
997 DBusConnection *connection)
999 DBusMessage *message;
1000 dbus_uint32_t serial;
1005 dbus_error_init (&error);
1008 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1010 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1012 DBUS_INTERFACE_DBUS,
1015 if (message == NULL)
1018 if (!dbus_connection_send (connection, message, &serial))
1020 dbus_message_unref (message);
1024 dbus_message_unref (message);
1027 /* send our message */
1028 bus_test_run_clients_loop (SEND_PENDING (connection));
1030 dbus_connection_ref (connection); /* because we may get disconnected */
1031 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1033 if (!dbus_connection_get_is_connected (connection))
1035 _dbus_verbose ("connection was disconnected\n");
1037 dbus_connection_unref (connection);
1042 dbus_connection_unref (connection);
1044 message = pop_message_waiting_for_memory (connection);
1045 if (message == NULL)
1047 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1048 "Hello", serial, connection);
1052 verbose_message_received (connection, message);
1054 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1056 _dbus_warn ("Message has wrong sender %s\n",
1057 dbus_message_get_sender (message) ?
1058 dbus_message_get_sender (message) : "(none)");
1062 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1064 warn_unexpected (connection, message, "method return for Hello");
1068 if (!check_no_leftovers (context))
1074 dbus_error_free (&error);
1077 dbus_message_unref (message);
1082 /* returns TRUE if the correct thing happens,
1083 * but the correct thing may include OOM errors.
1086 check_get_connection_unix_user (BusContext *context,
1087 DBusConnection *connection)
1089 DBusMessage *message;
1090 dbus_uint32_t serial;
1093 const char *base_service_name;
1097 dbus_error_init (&error);
1100 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1102 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1104 DBUS_INTERFACE_DBUS,
1105 "GetConnectionUnixUser");
1107 if (message == NULL)
1110 base_service_name = dbus_bus_get_unique_name (connection);
1112 if (!dbus_message_append_args (message,
1113 DBUS_TYPE_STRING, &base_service_name,
1116 dbus_message_unref (message);
1120 if (!dbus_connection_send (connection, message, &serial))
1122 dbus_message_unref (message);
1126 /* send our message */
1127 bus_test_run_clients_loop (SEND_PENDING (connection));
1129 dbus_message_unref (message);
1132 dbus_connection_ref (connection); /* because we may get disconnected */
1133 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1135 if (!dbus_connection_get_is_connected (connection))
1137 _dbus_verbose ("connection was disconnected\n");
1139 dbus_connection_unref (connection);
1144 dbus_connection_unref (connection);
1146 message = pop_message_waiting_for_memory (connection);
1147 if (message == NULL)
1149 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1150 "GetConnectionUnixUser", serial, connection);
1154 verbose_message_received (connection, message);
1156 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1158 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1160 ; /* good, this is a valid response */
1164 warn_unexpected (connection, message, "not this error");
1171 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1173 ; /* good, expected */
1177 warn_unexpected (connection, message,
1178 "method_return for GetConnectionUnixUser");
1185 if (!dbus_message_get_args (message, &error,
1186 DBUS_TYPE_UINT32, &uid,
1189 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1191 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1192 dbus_error_free (&error);
1193 _dbus_wait_for_memory ();
1194 goto retry_get_property;
1198 _dbus_assert (dbus_error_is_set (&error));
1199 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1205 if (!check_no_leftovers (context))
1211 dbus_error_free (&error);
1214 dbus_message_unref (message);
1219 /* returns TRUE if the correct thing happens,
1220 * but the correct thing may include OOM errors.
1223 check_get_connection_unix_process_id (BusContext *context,
1224 DBusConnection *connection)
1226 DBusMessage *message;
1227 dbus_uint32_t serial;
1230 const char *base_service_name;
1234 dbus_error_init (&error);
1237 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1239 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1241 DBUS_INTERFACE_DBUS,
1242 "GetConnectionUnixProcessID");
1244 if (message == NULL)
1247 base_service_name = dbus_bus_get_unique_name (connection);
1249 if (!dbus_message_append_args (message,
1250 DBUS_TYPE_STRING, &base_service_name,
1253 dbus_message_unref (message);
1257 if (!dbus_connection_send (connection, message, &serial))
1259 dbus_message_unref (message);
1263 /* send our message */
1264 bus_test_run_clients_loop (SEND_PENDING (connection));
1266 dbus_message_unref (message);
1269 dbus_connection_ref (connection); /* because we may get disconnected */
1270 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1272 if (!dbus_connection_get_is_connected (connection))
1274 _dbus_verbose ("connection was disconnected\n");
1276 dbus_connection_unref (connection);
1281 dbus_connection_unref (connection);
1283 message = pop_message_waiting_for_memory (connection);
1284 if (message == NULL)
1286 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1287 "GetConnectionUnixProcessID", serial, connection);
1291 verbose_message_received (connection, message);
1293 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1295 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1297 ; /* good, this is a valid response */
1300 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1302 /* We are expecting this error, since we know in the test suite we aren't
1303 * talking to a client running on UNIX
1305 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1310 warn_unexpected (connection, message, "not this error");
1318 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1321 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1323 ; /* good, expected */
1327 warn_unexpected (connection, message,
1328 "method_return for GetConnectionUnixProcessID");
1335 if (!dbus_message_get_args (message, &error,
1336 DBUS_TYPE_UINT32, &pid,
1339 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1341 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1342 dbus_error_free (&error);
1343 _dbus_wait_for_memory ();
1344 goto retry_get_property;
1348 _dbus_assert (dbus_error_is_set (&error));
1349 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1355 /* test if returned pid is the same as our own pid
1357 * @todo It would probably be good to restructure the tests
1358 * in a way so our parent is the bus that we're testing
1359 * cause then we can test that the pid returned matches
1362 if (pid != (dbus_uint32_t) _dbus_getpid ())
1364 _dbus_assert (dbus_error_is_set (&error));
1365 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1369 #endif /* !DBUS_WIN */
1372 if (!check_no_leftovers (context))
1378 dbus_error_free (&error);
1381 dbus_message_unref (message);
1386 /* returns TRUE if the correct thing happens,
1387 * but the correct thing may include OOM errors.
1390 check_add_match_all (BusContext *context,
1391 DBusConnection *connection)
1393 DBusMessage *message;
1395 dbus_uint32_t serial;
1397 const char *empty = "";
1400 dbus_error_init (&error);
1403 _dbus_verbose ("check_add_match_all for %p\n", connection);
1405 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1407 DBUS_INTERFACE_DBUS,
1410 if (message == NULL)
1413 /* empty string match rule matches everything */
1414 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1417 dbus_message_unref (message);
1421 if (!dbus_connection_send (connection, message, &serial))
1423 dbus_message_unref (message);
1427 dbus_message_unref (message);
1430 dbus_connection_ref (connection); /* because we may get disconnected */
1432 /* send our message */
1433 bus_test_run_clients_loop (SEND_PENDING (connection));
1435 if (!dbus_connection_get_is_connected (connection))
1437 _dbus_verbose ("connection was disconnected\n");
1439 dbus_connection_unref (connection);
1444 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1446 if (!dbus_connection_get_is_connected (connection))
1448 _dbus_verbose ("connection was disconnected\n");
1450 dbus_connection_unref (connection);
1455 dbus_connection_unref (connection);
1457 message = pop_message_waiting_for_memory (connection);
1458 if (message == NULL)
1460 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1461 "AddMatch", serial, connection);
1465 verbose_message_received (connection, message);
1467 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1469 _dbus_warn ("Message has wrong sender %s\n",
1470 dbus_message_get_sender (message) ?
1471 dbus_message_get_sender (message) : "(none)");
1475 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1477 if (dbus_message_is_error (message,
1478 DBUS_ERROR_NO_MEMORY))
1480 ; /* good, this is a valid response */
1484 warn_unexpected (connection, message, "not this error");
1491 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1493 ; /* good, expected */
1494 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1498 warn_unexpected (connection, message, "method return for AddMatch");
1504 if (!check_no_leftovers (context))
1510 dbus_error_free (&error);
1513 dbus_message_unref (message);
1518 /* returns TRUE if the correct thing happens,
1519 * but the correct thing may include OOM errors.
1522 check_hello_connection (BusContext *context)
1524 DBusConnection *connection;
1527 dbus_error_init (&error);
1529 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1530 if (connection == NULL)
1532 _DBUS_ASSERT_ERROR_IS_SET (&error);
1533 dbus_error_free (&error);
1537 if (!bus_setup_debug_client (connection))
1539 dbus_connection_close (connection);
1540 dbus_connection_unref (connection);
1544 spin_connection_until_authenticated (context, connection);
1546 if (!check_hello_message (context, connection))
1549 if (dbus_bus_get_unique_name (connection) == NULL)
1551 /* We didn't successfully register, so we can't
1552 * do the usual kill_client_connection() checks
1554 kill_client_connection_unchecked (connection);
1558 if (!check_add_match_all (context, connection))
1561 kill_client_connection (context, connection);
1567 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1569 /* returns TRUE if the correct thing happens,
1570 * but the correct thing may include OOM errors.
1573 check_nonexistent_service_no_auto_start (BusContext *context,
1574 DBusConnection *connection)
1576 DBusMessage *message;
1577 dbus_uint32_t serial;
1579 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1580 dbus_uint32_t flags;
1582 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1584 DBUS_INTERFACE_DBUS,
1585 "StartServiceByName");
1587 if (message == NULL)
1590 dbus_message_set_auto_start (message, FALSE);
1593 if (!dbus_message_append_args (message,
1594 DBUS_TYPE_STRING, &nonexistent,
1595 DBUS_TYPE_UINT32, &flags,
1598 dbus_message_unref (message);
1602 if (!dbus_connection_send (connection, message, &serial))
1604 dbus_message_unref (message);
1608 dbus_message_unref (message);
1611 bus_test_run_everything (context);
1612 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1613 bus_test_run_everything (context);
1615 if (!dbus_connection_get_is_connected (connection))
1617 _dbus_verbose ("connection was disconnected\n");
1623 message = pop_message_waiting_for_memory (connection);
1624 if (message == NULL)
1626 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1627 "StartServiceByName", serial, connection);
1631 verbose_message_received (connection, message);
1633 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1635 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1637 _dbus_warn ("Message has wrong sender %s\n",
1638 dbus_message_get_sender (message) ?
1639 dbus_message_get_sender (message) : "(none)");
1643 if (dbus_message_is_error (message,
1644 DBUS_ERROR_NO_MEMORY))
1646 ; /* good, this is a valid response */
1648 else if (dbus_message_is_error (message,
1649 DBUS_ERROR_SERVICE_UNKNOWN))
1651 ; /* good, this is expected also */
1655 warn_unexpected (connection, message, "not this error");
1661 _dbus_warn ("Did not expect to successfully activate %s\n",
1662 NONEXISTENT_SERVICE_NAME);
1670 dbus_message_unref (message);
1675 /* returns TRUE if the correct thing happens,
1676 * but the correct thing may include OOM errors.
1679 check_nonexistent_service_auto_start (BusContext *context,
1680 DBusConnection *connection)
1682 DBusMessage *message;
1683 dbus_uint32_t serial;
1686 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1687 "/org/freedesktop/TestSuite",
1688 "org.freedesktop.TestSuite",
1691 if (message == NULL)
1694 if (!dbus_connection_send (connection, message, &serial))
1696 dbus_message_unref (message);
1700 dbus_message_unref (message);
1703 bus_test_run_everything (context);
1704 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1705 bus_test_run_everything (context);
1707 if (!dbus_connection_get_is_connected (connection))
1709 _dbus_verbose ("connection was disconnected\n");
1715 message = pop_message_waiting_for_memory (connection);
1717 if (message == NULL)
1719 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1720 "Echo message (auto activation)", serial, connection);
1724 verbose_message_received (connection, message);
1726 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1728 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1730 _dbus_warn ("Message has wrong sender %s\n",
1731 dbus_message_get_sender (message) ?
1732 dbus_message_get_sender (message) : "(none)");
1736 if (dbus_message_is_error (message,
1737 DBUS_ERROR_NO_MEMORY))
1739 ; /* good, this is a valid response */
1741 else if (dbus_message_is_error (message,
1742 DBUS_ERROR_SERVICE_UNKNOWN))
1744 ; /* good, this is expected also */
1748 warn_unexpected (connection, message, "not this error");
1754 _dbus_warn ("Did not expect to successfully activate %s\n",
1755 NONEXISTENT_SERVICE_NAME);
1763 dbus_message_unref (message);
1769 check_base_service_activated (BusContext *context,
1770 DBusConnection *connection,
1771 DBusMessage *initial_message,
1772 const char **base_service_p)
1774 DBusMessage *message;
1777 const char *base_service, *base_service_from_bus, *old_owner;
1781 dbus_error_init (&error);
1782 base_service = NULL;
1784 base_service_from_bus = NULL;
1786 message = initial_message;
1787 dbus_message_ref (message);
1789 if (dbus_message_is_signal (message,
1790 DBUS_INTERFACE_DBUS,
1791 "NameOwnerChanged"))
1793 CheckServiceOwnerChangedData socd;
1795 reget_service_name_arg:
1796 base_service = NULL;
1798 base_service_from_bus = NULL;
1800 if (!dbus_message_get_args (message, &error,
1801 DBUS_TYPE_STRING, &base_service,
1802 DBUS_TYPE_STRING, &old_owner,
1803 DBUS_TYPE_STRING, &base_service_from_bus,
1806 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1808 dbus_error_free (&error);
1809 _dbus_wait_for_memory ();
1810 goto reget_service_name_arg;
1814 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1815 "NameOwnerChanged (creation)",
1821 if (*base_service != ':')
1823 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1828 if (strcmp (base_service, base_service_from_bus) != 0)
1830 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1831 base_service, base_service_from_bus);
1837 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1842 socd.expected_kind = SERVICE_CREATED;
1843 socd.expected_service_name = base_service;
1844 socd.failed = FALSE;
1845 socd.skip_connection = connection;
1846 bus_test_clients_foreach (check_service_owner_changed_foreach,
1854 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1860 *base_service_p = base_service;
1866 dbus_message_unref (message);
1867 dbus_error_free (&error);
1873 check_service_activated (BusContext *context,
1874 DBusConnection *connection,
1875 const char *activated_name,
1876 const char *base_service_name,
1877 DBusMessage *initial_message)
1879 DBusMessage *message;
1882 dbus_uint32_t activation_result;
1886 dbus_error_init (&error);
1888 message = initial_message;
1889 dbus_message_ref (message);
1891 if (dbus_message_is_signal (message,
1892 DBUS_INTERFACE_DBUS,
1893 "NameOwnerChanged"))
1895 CheckServiceOwnerChangedData socd;
1896 const char *service_name, *base_service_from_bus, *old_owner;
1898 reget_service_name_arg:
1899 service_name = NULL;
1901 base_service_from_bus = NULL;
1903 if (!dbus_message_get_args (message, &error,
1904 DBUS_TYPE_STRING, &service_name,
1905 DBUS_TYPE_STRING, &old_owner,
1906 DBUS_TYPE_STRING, &base_service_from_bus,
1909 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1911 dbus_error_free (&error);
1912 _dbus_wait_for_memory ();
1913 goto reget_service_name_arg;
1917 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1918 "NameOwnerChanged (creation)",
1924 if (strcmp (service_name, activated_name) != 0)
1926 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1927 activated_name, service_name);
1931 if (strcmp (base_service_name, base_service_from_bus) != 0)
1933 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1934 base_service_from_bus, base_service_name);
1940 _dbus_warn ("expected a %s, got a %s\n",
1941 "NameOwnerChanged (creation)",
1942 "NameOwnerChanged (change)");
1946 socd.expected_kind = SERVICE_CREATED;
1947 socd.skip_connection = connection;
1948 socd.failed = FALSE;
1949 socd.expected_service_name = service_name;
1950 bus_test_clients_foreach (check_service_owner_changed_foreach,
1956 dbus_message_unref (message);
1957 service_name = NULL;
1959 base_service_from_bus = NULL;
1961 message = pop_message_waiting_for_memory (connection);
1962 if (message == NULL)
1964 _dbus_warn ("Expected a reply to %s, got nothing\n",
1965 "StartServiceByName");
1971 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1976 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1978 warn_unexpected (connection, message, "reply to StartServiceByName");
1983 activation_result = 0;
1984 if (!dbus_message_get_args (message, &error,
1985 DBUS_TYPE_UINT32, &activation_result,
1988 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1990 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1991 "StartServiceByName", error.message);
1995 dbus_error_free (&error);
1999 if (activation_result == DBUS_START_REPLY_SUCCESS)
2001 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2005 _dbus_warn ("Activation result was %u, no good.\n",
2011 dbus_message_unref (message);
2014 if (!check_no_leftovers (context))
2016 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2024 dbus_message_unref (message);
2025 dbus_error_free (&error);
2031 check_service_auto_activated (BusContext *context,
2032 DBusConnection *connection,
2033 const char *activated_name,
2034 const char *base_service_name,
2035 DBusMessage *initial_message)
2037 DBusMessage *message;
2043 dbus_error_init (&error);
2045 message = initial_message;
2046 dbus_message_ref (message);
2048 if (dbus_message_is_signal (message,
2049 DBUS_INTERFACE_DBUS,
2050 "NameOwnerChanged"))
2052 const char *service_name;
2053 CheckServiceOwnerChangedData socd;
2055 reget_service_name_arg:
2056 if (!dbus_message_get_args (message, &error,
2057 DBUS_TYPE_STRING, &service_name,
2060 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2062 dbus_error_free (&error);
2063 _dbus_wait_for_memory ();
2064 goto reget_service_name_arg;
2068 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2071 dbus_error_free (&error);
2076 if (strcmp (service_name, activated_name) != 0)
2078 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2079 activated_name, service_name);
2083 socd.expected_kind = SERVICE_CREATED;
2084 socd.expected_service_name = service_name;
2085 socd.failed = FALSE;
2086 socd.skip_connection = connection;
2087 bus_test_clients_foreach (check_service_owner_changed_foreach,
2093 /* Note that this differs from regular activation in that we don't get a
2094 * reply to ActivateService here.
2097 dbus_message_unref (message);
2099 service_name = NULL;
2103 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2112 dbus_message_unref (message);
2118 check_service_deactivated (BusContext *context,
2119 DBusConnection *connection,
2120 const char *activated_name,
2121 const char *base_service)
2124 CheckServiceOwnerChangedData socd;
2128 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2129 * service and the activated_name. The base service
2130 * notification is required to come last.
2132 socd.expected_kind = SERVICE_DELETED;
2133 socd.expected_service_name = activated_name;
2134 socd.failed = FALSE;
2135 socd.skip_connection = NULL;
2136 bus_test_clients_foreach (check_service_owner_changed_foreach,
2142 socd.expected_kind = SERVICE_DELETED;
2143 socd.expected_service_name = base_service;
2144 socd.failed = FALSE;
2145 socd.skip_connection = NULL;
2146 bus_test_clients_foreach (check_service_owner_changed_foreach,
2159 check_send_exit_to_service (BusContext *context,
2160 DBusConnection *connection,
2161 const char *service_name,
2162 const char *base_service)
2164 dbus_bool_t got_error;
2165 DBusMessage *message;
2166 dbus_uint32_t serial;
2169 _dbus_verbose ("Sending exit message to the test service\n");
2173 /* Kill off the test service by sending it a quit message */
2174 message = dbus_message_new_method_call (service_name,
2175 "/org/freedesktop/TestSuite",
2176 "org.freedesktop.TestSuite",
2179 if (message == NULL)
2181 /* Do this again; we still need the service to exit... */
2182 if (!check_send_exit_to_service (context, connection,
2183 service_name, base_service))
2189 if (!dbus_connection_send (connection, message, &serial))
2191 dbus_message_unref (message);
2193 /* Do this again; we still need the service to exit... */
2194 if (!check_send_exit_to_service (context, connection,
2195 service_name, base_service))
2201 dbus_message_unref (message);
2205 bus_test_run_clients_loop (SEND_PENDING (connection));
2207 /* read it in and write it out to test service */
2208 bus_test_run_bus_loop (context, FALSE);
2210 /* see if we got an error during message bus dispatching */
2211 bus_test_run_clients_loop (FALSE);
2212 message = borrow_message_waiting_for_memory (connection);
2213 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2216 dbus_connection_return_message (connection, message);
2222 /* If no error, wait for the test service to exit */
2223 block_connection_until_message_from_bus (context, connection, "test service to exit");
2225 bus_test_run_everything (context);
2230 message = pop_message_waiting_for_memory (connection);
2231 _dbus_assert (message != NULL);
2233 if (dbus_message_get_reply_serial (message) != serial)
2235 warn_unexpected (connection, message,
2236 "error with the correct reply serial");
2240 if (!dbus_message_is_error (message,
2241 DBUS_ERROR_NO_MEMORY))
2243 warn_unexpected (connection, message,
2244 "a no memory error from asking test service to exit");
2248 _dbus_verbose ("Got error %s when asking test service to exit\n",
2249 dbus_message_get_error_name (message));
2251 /* Do this again; we still need the service to exit... */
2252 if (!check_send_exit_to_service (context, connection,
2253 service_name, base_service))
2258 if (!check_service_deactivated (context, connection,
2259 service_name, base_service))
2262 /* Should now have a NoReply error from the Exit() method
2263 * call; it should have come after all the deactivation
2266 message = pop_message_waiting_for_memory (connection);
2268 if (message == NULL)
2270 warn_unexpected (connection, NULL,
2271 "reply to Exit() method call");
2274 if (!dbus_message_is_error (message,
2275 DBUS_ERROR_NO_REPLY))
2277 warn_unexpected (connection, message,
2278 "NoReply error from Exit() method call");
2282 if (dbus_message_get_reply_serial (message) != serial)
2284 warn_unexpected (connection, message,
2285 "error with the correct reply serial");
2289 _dbus_verbose ("Got error %s after test service exited\n",
2290 dbus_message_get_error_name (message));
2292 if (!check_no_leftovers (context))
2294 _dbus_warn ("Messages were left over after %s\n",
2295 _DBUS_FUNCTION_NAME);
2304 dbus_message_unref (message);
2310 check_got_error (BusContext *context,
2311 DBusConnection *connection,
2312 const char *first_error_name,
2315 DBusMessage *message;
2318 dbus_bool_t error_found;
2319 const char *error_name;
2323 message = pop_message_waiting_for_memory (connection);
2324 if (message == NULL)
2326 _dbus_warn ("Did not get an expected error\n");
2330 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2332 warn_unexpected (connection, message, "an error");
2337 error_found = FALSE;
2339 va_start (ap, first_error_name);
2340 error_name = first_error_name;
2341 while (error_name != NULL)
2343 if (dbus_message_is_error (message, error_name))
2348 error_name = va_arg (ap, char*);
2354 _dbus_warn ("Expected error %s or other, got %s instead\n",
2356 dbus_message_get_error_name (message));
2364 dbus_message_unref (message);
2371 GOT_SERVICE_CREATED,
2372 GOT_SERVICE_DELETED,
2377 static GotServiceInfo
2378 check_got_service_info (DBusMessage *message)
2380 GotServiceInfo message_kind;
2382 if (dbus_message_is_signal (message,
2383 DBUS_INTERFACE_DBUS,
2384 "NameOwnerChanged"))
2387 const char *service_name, *old_owner, *new_owner;
2388 dbus_error_init (&error);
2390 reget_service_info_data:
2391 service_name = NULL;
2395 dbus_message_get_args (message, &error,
2396 DBUS_TYPE_STRING, &service_name,
2397 DBUS_TYPE_STRING, &old_owner,
2398 DBUS_TYPE_STRING, &new_owner,
2400 if (dbus_error_is_set (&error))
2402 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2404 dbus_error_free (&error);
2405 goto reget_service_info_data;
2409 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2410 message_kind = GOT_SOMETHING_ELSE;
2413 else if (!old_owner[0])
2414 message_kind = GOT_SERVICE_CREATED;
2415 else if (!new_owner[0])
2416 message_kind = GOT_SERVICE_DELETED;
2418 message_kind = GOT_SOMETHING_ELSE;
2420 dbus_error_free (&error);
2422 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2423 message_kind = GOT_ERROR;
2425 message_kind = GOT_SOMETHING_ELSE;
2427 return message_kind;
2430 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2432 /* returns TRUE if the correct thing happens,
2433 * but the correct thing may include OOM errors.
2436 check_existent_service_no_auto_start (BusContext *context,
2437 DBusConnection *connection)
2439 DBusMessage *message;
2440 DBusMessage *base_service_message;
2441 const char *base_service;
2442 dbus_uint32_t serial;
2444 const char *existent = EXISTENT_SERVICE_NAME;
2445 dbus_uint32_t flags;
2447 base_service_message = NULL;
2449 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2451 DBUS_INTERFACE_DBUS,
2452 "StartServiceByName");
2454 if (message == NULL)
2457 dbus_message_set_auto_start (message, FALSE);
2460 if (!dbus_message_append_args (message,
2461 DBUS_TYPE_STRING, &existent,
2462 DBUS_TYPE_UINT32, &flags,
2465 dbus_message_unref (message);
2469 if (!dbus_connection_send (connection, message, &serial))
2471 dbus_message_unref (message);
2475 dbus_message_unref (message);
2478 bus_test_run_everything (context);
2480 /* now wait for the message bus to hear back from the activated
2483 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2485 bus_test_run_everything (context);
2487 if (!dbus_connection_get_is_connected (connection))
2489 _dbus_verbose ("connection was disconnected\n");
2495 message = pop_message_waiting_for_memory (connection);
2496 if (message == NULL)
2498 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2499 "StartServiceByName", serial, connection);
2503 verbose_message_received (connection, message);
2504 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2506 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2508 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2510 _dbus_warn ("Message has wrong sender %s\n",
2511 dbus_message_get_sender (message) ?
2512 dbus_message_get_sender (message) : "(none)");
2516 if (dbus_message_is_error (message,
2517 DBUS_ERROR_NO_MEMORY))
2519 ; /* good, this is a valid response */
2521 else if (dbus_message_is_error (message,
2522 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2523 dbus_message_is_error (message,
2524 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2525 dbus_message_is_error (message,
2526 DBUS_ERROR_SPAWN_EXEC_FAILED))
2528 ; /* good, this is expected also */
2532 _dbus_warn ("Did not expect error %s\n",
2533 dbus_message_get_error_name (message));
2539 GotServiceInfo message_kind;
2541 if (!check_base_service_activated (context, connection,
2542 message, &base_service))
2545 base_service_message = message;
2548 /* We may need to block here for the test service to exit or finish up */
2549 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2551 message = dbus_connection_borrow_message (connection);
2552 if (message == NULL)
2554 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2558 message_kind = check_got_service_info (message);
2560 dbus_connection_return_message (connection, message);
2563 switch (message_kind)
2565 case GOT_SOMETHING_ELSE:
2566 _dbus_warn ("Unexpected message after ActivateService "
2567 "(should be an error or a service announcement");
2571 if (!check_got_error (context, connection,
2572 DBUS_ERROR_SPAWN_CHILD_EXITED,
2573 DBUS_ERROR_NO_MEMORY,
2576 /* A service deleted should be coming along now after this error.
2577 * We can also get the error *after* the service deleted.
2582 case GOT_SERVICE_DELETED:
2584 /* The service started up and got a base address, but then
2585 * failed to register under EXISTENT_SERVICE_NAME
2587 CheckServiceOwnerChangedData socd;
2589 socd.expected_kind = SERVICE_DELETED;
2590 socd.expected_service_name = base_service;
2591 socd.failed = FALSE;
2592 socd.skip_connection = NULL;
2594 bus_test_clients_foreach (check_service_owner_changed_foreach,
2600 /* Now we should get an error about the service exiting
2601 * if we didn't get it before.
2603 if (message_kind != GOT_ERROR)
2605 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2607 /* and process everything again */
2608 bus_test_run_everything (context);
2610 if (!check_got_error (context, connection,
2611 DBUS_ERROR_SPAWN_CHILD_EXITED,
2612 DBUS_ERROR_NO_MEMORY,
2619 case GOT_SERVICE_CREATED:
2620 message = pop_message_waiting_for_memory (connection);
2621 if (message == NULL)
2623 _dbus_warn ("Failed to pop message we just put back! "
2624 "should have been a NameOwnerChanged (creation)\n");
2628 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2629 base_service, message))
2632 dbus_message_unref (message);
2635 if (!check_no_leftovers (context))
2637 _dbus_warn ("Messages were left over after successful activation\n");
2641 if (!check_send_exit_to_service (context, connection,
2642 EXISTENT_SERVICE_NAME, base_service))
2653 dbus_message_unref (message);
2655 if (base_service_message)
2656 dbus_message_unref (base_service_message);
2661 #ifndef DBUS_WIN_FIXME
2662 /* returns TRUE if the correct thing happens,
2663 * but the correct thing may include OOM errors.
2666 check_segfault_service_no_auto_start (BusContext *context,
2667 DBusConnection *connection)
2669 DBusMessage *message;
2670 dbus_uint32_t serial;
2672 const char *segv_service;
2673 dbus_uint32_t flags;
2675 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2677 DBUS_INTERFACE_DBUS,
2678 "StartServiceByName");
2680 if (message == NULL)
2683 dbus_message_set_auto_start (message, FALSE);
2685 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2687 if (!dbus_message_append_args (message,
2688 DBUS_TYPE_STRING, &segv_service,
2689 DBUS_TYPE_UINT32, &flags,
2692 dbus_message_unref (message);
2696 if (!dbus_connection_send (connection, message, &serial))
2698 dbus_message_unref (message);
2702 dbus_message_unref (message);
2705 bus_test_run_everything (context);
2706 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2707 bus_test_run_everything (context);
2709 if (!dbus_connection_get_is_connected (connection))
2711 _dbus_verbose ("connection was disconnected\n");
2717 message = pop_message_waiting_for_memory (connection);
2718 if (message == NULL)
2720 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2721 "StartServiceByName", serial, connection);
2725 verbose_message_received (connection, message);
2727 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2729 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2731 _dbus_warn ("Message has wrong sender %s\n",
2732 dbus_message_get_sender (message) ?
2733 dbus_message_get_sender (message) : "(none)");
2737 if (dbus_message_is_error (message,
2738 DBUS_ERROR_NO_MEMORY))
2740 ; /* good, this is a valid response */
2742 else if (dbus_message_is_error (message,
2745 const char *servicehelper;
2746 servicehelper = bus_context_get_servicehelper (context);
2747 /* make sure this only happens with the launch helper */
2748 _dbus_assert (servicehelper != NULL);
2750 else if (dbus_message_is_error (message,
2751 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2753 ; /* good, this is expected also */
2757 warn_unexpected (connection, message, "not this error");
2764 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2772 dbus_message_unref (message);
2778 /* returns TRUE if the correct thing happens,
2779 * but the correct thing may include OOM errors.
2782 check_segfault_service_auto_start (BusContext *context,
2783 DBusConnection *connection)
2785 DBusMessage *message;
2786 dbus_uint32_t serial;
2789 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2790 "/org/freedesktop/TestSuite",
2791 "org.freedesktop.TestSuite",
2794 if (message == NULL)
2797 if (!dbus_connection_send (connection, message, &serial))
2799 dbus_message_unref (message);
2803 dbus_message_unref (message);
2806 bus_test_run_everything (context);
2807 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2808 bus_test_run_everything (context);
2810 if (!dbus_connection_get_is_connected (connection))
2812 _dbus_verbose ("connection was disconnected\n");
2818 message = pop_message_waiting_for_memory (connection);
2819 if (message == NULL)
2821 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2822 "Echo message (auto activation)", serial, connection);
2826 verbose_message_received (connection, message);
2828 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2830 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2832 _dbus_warn ("Message has wrong sender %s\n",
2833 dbus_message_get_sender (message) ?
2834 dbus_message_get_sender (message) : "(none)");
2838 if (dbus_message_is_error (message,
2839 DBUS_ERROR_NO_MEMORY))
2841 ; /* good, this is a valid response */
2843 else if (dbus_message_is_error (message,
2844 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2846 ; /* good, this is expected also */
2850 warn_unexpected (connection, message, "not this error");
2857 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2865 dbus_message_unref (message);
2871 #define TEST_ECHO_MESSAGE "Test echo message"
2872 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2874 /* returns TRUE if the correct thing happens,
2875 * but the correct thing may include OOM errors.
2878 check_existent_hello_from_self (BusContext *context,
2879 DBusConnection *connection)
2881 DBusMessage *message;
2882 dbus_uint32_t serial;
2885 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2886 "/org/freedesktop/TestSuite",
2887 "org.freedesktop.TestSuite",
2888 "RunHelloFromSelf");
2890 if (message == NULL)
2893 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2894 if (!dbus_message_append_args (message,
2895 DBUS_TYPE_STRING, &text,
2898 dbus_message_unref (message);
2902 if (!dbus_connection_send (connection, message, &serial))
2904 dbus_message_unref (message);
2908 dbus_message_unref (message);
2911 bus_test_run_everything (context);
2913 /* Note: if this test is run in OOM mode, it will block when the bus
2914 * doesn't send a reply due to OOM.
2916 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2918 message = pop_message_waiting_for_memory (connection);
2919 if (message == NULL)
2921 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2925 if (dbus_message_get_reply_serial (message) != serial)
2927 _dbus_warn ("Wrong reply serial\n");
2928 dbus_message_unref (message);
2932 dbus_message_unref (message);
2938 /* returns TRUE if the correct thing happens,
2939 * but the correct thing may include OOM errors.
2942 check_existent_ping (BusContext *context,
2943 DBusConnection *connection)
2945 DBusMessage *message;
2946 dbus_uint32_t serial;
2947 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2948 "/org/freedesktop/TestSuite",
2949 "org.freedesktop.DBus.Peer",
2952 if (message == NULL)
2955 if (!dbus_connection_send (connection, message, &serial))
2957 dbus_message_unref (message);
2961 dbus_message_unref (message);
2964 bus_test_run_everything (context);
2966 /* Note: if this test is run in OOM mode, it will block when the bus
2967 * doesn't send a reply due to OOM.
2969 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2971 message = pop_message_waiting_for_memory (connection);
2972 if (message == NULL)
2974 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2978 if (dbus_message_get_reply_serial (message) != serial)
2980 _dbus_warn ("Wrong reply serial\n");
2981 dbus_message_unref (message);
2985 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2987 _dbus_warn ("Unexpected message return during Ping\n");
2988 dbus_message_unref (message);
2992 dbus_message_unref (message);
2998 /* returns TRUE if the correct thing happens,
2999 * but the correct thing may include OOM errors.
3002 check_existent_get_machine_id (BusContext *context,
3003 DBusConnection *connection)
3005 DBusMessage *message;
3006 dbus_uint32_t serial;
3007 const char *machine_id;
3009 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3010 "/org/freedesktop/TestSuite",
3011 "org.freedesktop.DBus.Peer",
3014 if (message == NULL)
3017 if (!dbus_connection_send (connection, message, &serial))
3019 dbus_message_unref (message);
3023 dbus_message_unref (message);
3026 bus_test_run_everything (context);
3028 /* Note: if this test is run in OOM mode, it will block when the bus
3029 * doesn't send a reply due to OOM.
3031 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3033 message = pop_message_waiting_for_memory (connection);
3034 if (message == NULL)
3036 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3040 if (dbus_message_get_reply_serial (message) != serial)
3042 _dbus_warn ("Wrong reply serial\n");
3043 dbus_message_unref (message);
3047 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3049 _dbus_warn ("Unexpected message return during GetMachineId\n");
3050 dbus_message_unref (message);
3055 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3057 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3058 dbus_message_unref (message);
3062 if (machine_id == NULL || strlen (machine_id) != 32)
3064 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3065 dbus_message_unref (message);
3069 /* We can't check that the machine id is correct because during make check it is
3070 * just made up for each process separately
3073 dbus_message_unref (message);
3079 /* returns TRUE if the correct thing happens,
3080 * but the correct thing may include OOM errors.
3083 check_existent_service_auto_start (BusContext *context,
3084 DBusConnection *connection)
3086 DBusMessage *message;
3087 DBusMessage *base_service_message;
3088 dbus_uint32_t serial;
3090 const char *base_service;
3093 base_service_message = NULL;
3095 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3096 "/org/freedesktop/TestSuite",
3097 "org.freedesktop.TestSuite",
3100 if (message == NULL)
3103 text = TEST_ECHO_MESSAGE;
3104 if (!dbus_message_append_args (message,
3105 DBUS_TYPE_STRING, &text,
3108 dbus_message_unref (message);
3112 if (!dbus_connection_send (connection, message, &serial))
3114 dbus_message_unref (message);
3118 dbus_message_unref (message);
3121 bus_test_run_everything (context);
3123 /* now wait for the message bus to hear back from the activated
3126 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3127 bus_test_run_everything (context);
3129 if (!dbus_connection_get_is_connected (connection))
3131 _dbus_verbose ("connection was disconnected\n");
3137 message = pop_message_waiting_for_memory (connection);
3138 if (message == NULL)
3140 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3141 serial, connection);
3145 verbose_message_received (connection, message);
3146 _dbus_verbose (" (after sending %s)\n", "auto start");
3148 /* we should get zero or two ServiceOwnerChanged signals */
3149 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3151 GotServiceInfo message_kind;
3153 if (!check_base_service_activated (context, connection,
3154 message, &base_service))
3157 base_service_message = message;
3160 /* We may need to block here for the test service to exit or finish up */
3161 block_connection_until_message_from_bus (context, connection, "service to exit");
3163 /* Should get a service creation notification for the activated
3164 * service name, or a service deletion on the base service name
3166 message = dbus_connection_borrow_message (connection);
3167 if (message == NULL)
3169 _dbus_warn ("No message after auto activation "
3170 "(should be a service announcement)\n");
3171 dbus_connection_return_message (connection, message);
3176 message_kind = check_got_service_info (message);
3178 dbus_connection_return_message (connection, message);
3181 switch (message_kind)
3183 case GOT_SERVICE_CREATED:
3184 message = pop_message_waiting_for_memory (connection);
3185 if (message == NULL)
3187 _dbus_warn ("Failed to pop message we just put back! "
3188 "should have been a NameOwnerChanged (creation)\n");
3192 /* Check that ServiceOwnerChanged (creation) was correctly received */
3193 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3194 base_service, message))
3197 dbus_message_unref (message);
3202 case GOT_SERVICE_DELETED:
3204 /* The service started up and got a base address, but then
3205 * failed to register under EXISTENT_SERVICE_NAME
3207 CheckServiceOwnerChangedData socd;
3209 socd.expected_kind = SERVICE_DELETED;
3210 socd.expected_service_name = base_service;
3211 socd.failed = FALSE;
3212 socd.skip_connection = NULL;
3213 bus_test_clients_foreach (check_service_owner_changed_foreach,
3223 case GOT_SOMETHING_ELSE:
3224 _dbus_warn ("Unexpected message after auto activation\n");
3229 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3230 * come the method reply (or error) from the initial method call
3233 /* Note: if this test is run in OOM mode, it will block when the bus
3234 * doesn't send a reply due to OOM.
3236 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3238 message = pop_message_waiting_for_memory (connection);
3239 if (message == NULL)
3241 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3245 if (dbus_message_get_reply_serial (message) != serial)
3247 _dbus_warn ("Wrong reply serial\n");
3251 dbus_message_unref (message);
3254 if (!check_existent_ping (context, connection))
3257 if (!check_existent_get_machine_id (context, connection))
3260 if (!check_existent_hello_from_self (context, connection))
3263 if (!check_send_exit_to_service (context, connection,
3264 EXISTENT_SERVICE_NAME,
3272 dbus_message_unref (message);
3274 if (base_service_message)
3275 dbus_message_unref (base_service_message);
3280 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3282 /* returns TRUE if the correct thing happens,
3283 * but the correct thing may include OOM errors.
3286 check_launch_service_file_missing (BusContext *context,
3287 DBusConnection *connection)
3289 DBusMessage *message;
3290 dbus_uint32_t serial;
3293 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3294 "/org/freedesktop/TestSuite",
3295 "org.freedesktop.TestSuite",
3298 if (message == NULL)
3301 if (!dbus_connection_send (connection, message, &serial))
3303 dbus_message_unref (message);
3307 dbus_message_unref (message);
3310 bus_test_run_everything (context);
3311 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3312 bus_test_run_everything (context);
3314 if (!dbus_connection_get_is_connected (connection))
3316 _dbus_verbose ("connection was disconnected\n");
3322 message = pop_message_waiting_for_memory (connection);
3323 if (message == NULL)
3325 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3326 "Echo message (auto activation)", serial, connection);
3330 verbose_message_received (connection, message);
3332 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3334 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3336 _dbus_warn ("Message has wrong sender %s\n",
3337 dbus_message_get_sender (message) ?
3338 dbus_message_get_sender (message) : "(none)");
3342 if (dbus_message_is_error (message,
3343 DBUS_ERROR_NO_MEMORY))
3345 ; /* good, this is a valid response */
3347 else if (dbus_message_is_error (message,
3348 DBUS_ERROR_SERVICE_UNKNOWN))
3350 _dbus_verbose("got service unknown\n");
3351 ; /* good, this is expected (only valid when using launch helper) */
3355 warn_unexpected (connection, message, "not this error");
3362 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3370 dbus_message_unref (message);
3375 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3377 /* returns TRUE if the correct thing happens,
3378 * but the correct thing may include OOM errors.
3381 check_launch_service_user_missing (BusContext *context,
3382 DBusConnection *connection)
3384 DBusMessage *message;
3385 dbus_uint32_t serial;
3388 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3389 "/org/freedesktop/TestSuite",
3390 "org.freedesktop.TestSuite",
3393 if (message == NULL)
3396 if (!dbus_connection_send (connection, message, &serial))
3398 dbus_message_unref (message);
3402 dbus_message_unref (message);
3405 bus_test_run_everything (context);
3406 block_connection_until_message_from_bus (context, connection,
3407 "reply to service which should fail to auto-start (missing User)");
3408 bus_test_run_everything (context);
3410 if (!dbus_connection_get_is_connected (connection))
3412 _dbus_warn ("connection was disconnected\n");
3418 message = pop_message_waiting_for_memory (connection);
3419 if (message == NULL)
3421 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3422 "Echo message (auto activation)", serial, connection);
3426 verbose_message_received (connection, message);
3428 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3430 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3432 _dbus_warn ("Message has wrong sender %s\n",
3433 dbus_message_get_sender (message) ?
3434 dbus_message_get_sender (message) : "(none)");
3438 if (dbus_message_is_error (message,
3439 DBUS_ERROR_NO_MEMORY))
3441 ; /* good, this is a valid response */
3443 else if (dbus_message_is_error (message,
3444 DBUS_ERROR_SPAWN_FILE_INVALID))
3446 _dbus_verbose("got service file invalid\n");
3447 ; /* good, this is expected (only valid when using launch helper) */
3451 warn_unexpected (connection, message, "not this error");
3458 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3466 dbus_message_unref (message);
3471 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3473 /* returns TRUE if the correct thing happens,
3474 * but the correct thing may include OOM errors.
3477 check_launch_service_exec_missing (BusContext *context,
3478 DBusConnection *connection)
3480 DBusMessage *message;
3481 dbus_uint32_t serial;
3484 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3485 "/org/freedesktop/TestSuite",
3486 "org.freedesktop.TestSuite",
3489 if (message == NULL)
3492 if (!dbus_connection_send (connection, message, &serial))
3494 dbus_message_unref (message);
3498 dbus_message_unref (message);
3501 bus_test_run_everything (context);
3502 block_connection_until_message_from_bus (context, connection,
3503 "reply to service which should fail to auto-start (missing Exec)");
3504 bus_test_run_everything (context);
3506 if (!dbus_connection_get_is_connected (connection))
3508 _dbus_warn ("connection was disconnected\n");
3514 message = pop_message_waiting_for_memory (connection);
3515 if (message == NULL)
3517 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3518 "Echo message (auto activation)", serial, connection);
3522 verbose_message_received (connection, message);
3524 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3526 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3528 _dbus_warn ("Message has wrong sender %s\n",
3529 dbus_message_get_sender (message) ?
3530 dbus_message_get_sender (message) : "(none)");
3534 if (dbus_message_is_error (message,
3535 DBUS_ERROR_NO_MEMORY))
3537 ; /* good, this is a valid response */
3539 else if (dbus_message_is_error (message,
3540 DBUS_ERROR_SERVICE_UNKNOWN))
3542 _dbus_verbose("could not activate as invalid service file was not added\n");
3543 ; /* good, this is expected as we shouldn't have been added to
3544 * the activation list with a missing Exec key */
3546 else if (dbus_message_is_error (message,
3547 DBUS_ERROR_SPAWN_FILE_INVALID))
3549 _dbus_verbose("got service file invalid\n");
3550 ; /* good, this is allowed, and is the message passed back from the
3555 warn_unexpected (connection, message, "not this error");
3562 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3570 dbus_message_unref (message);
3575 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3577 /* returns TRUE if the correct thing happens,
3578 * but the correct thing may include OOM errors.
3581 check_launch_service_service_missing (BusContext *context,
3582 DBusConnection *connection)
3584 DBusMessage *message;
3585 dbus_uint32_t serial;
3588 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3589 "/org/freedesktop/TestSuite",
3590 "org.freedesktop.TestSuite",
3593 if (message == NULL)
3596 if (!dbus_connection_send (connection, message, &serial))
3598 dbus_message_unref (message);
3602 dbus_message_unref (message);
3605 bus_test_run_everything (context);
3606 block_connection_until_message_from_bus (context, connection,
3607 "reply to service which should fail to auto-start (missing Service)");
3608 bus_test_run_everything (context);
3610 if (!dbus_connection_get_is_connected (connection))
3612 _dbus_warn ("connection was disconnected\n");
3618 message = pop_message_waiting_for_memory (connection);
3619 if (message == NULL)
3621 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3622 "Echo message (auto activation)", serial, connection);
3626 verbose_message_received (connection, message);
3628 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3630 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3632 _dbus_warn ("Message has wrong sender %s\n",
3633 dbus_message_get_sender (message) ?
3634 dbus_message_get_sender (message) : "(none)");
3638 if (dbus_message_is_error (message,
3639 DBUS_ERROR_NO_MEMORY))
3641 ; /* good, this is a valid response */
3643 else if (dbus_message_is_error (message,
3644 DBUS_ERROR_SERVICE_UNKNOWN))
3646 _dbus_verbose("could not activate as invalid service file was not added\n");
3647 ; /* good, this is expected as we shouldn't have been added to
3648 * the activation list with a missing Exec key */
3650 else if (dbus_message_is_error (message,
3651 DBUS_ERROR_SPAWN_FILE_INVALID))
3653 _dbus_verbose("got service file invalid\n");
3654 ; /* good, this is allowed, and is the message passed back from the
3659 warn_unexpected (connection, message, "not this error");
3666 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3674 dbus_message_unref (message);
3679 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3681 /* returns TRUE if the correct thing happens,
3682 * but the correct thing may include OOM errors.
3685 check_shell_fail_service_auto_start (BusContext *context,
3686 DBusConnection *connection)
3688 DBusMessage *message;
3689 dbus_uint32_t serial;
3692 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3693 "/org/freedesktop/TestSuite",
3694 "org.freedesktop.TestSuite",
3697 if (message == NULL)
3700 if (!dbus_connection_send (connection, message, &serial))
3702 dbus_message_unref (message);
3706 dbus_message_unref (message);
3709 bus_test_run_everything (context);
3710 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3711 bus_test_run_everything (context);
3713 if (!dbus_connection_get_is_connected (connection))
3715 _dbus_verbose ("connection was disconnected\n");
3721 message = pop_message_waiting_for_memory (connection);
3722 if (message == NULL)
3724 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3725 "Echo message (auto activation)", serial, connection);
3729 verbose_message_received (connection, message);
3731 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3733 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3735 _dbus_warn ("Message has wrong sender %s\n",
3736 dbus_message_get_sender (message) ?
3737 dbus_message_get_sender (message) : "(none)");
3741 if (dbus_message_is_error (message,
3742 DBUS_ERROR_NO_MEMORY))
3744 ; /* good, this is a valid response */
3746 else if (dbus_message_is_error (message,
3747 DBUS_ERROR_INVALID_ARGS))
3749 _dbus_verbose("got invalid args\n");
3750 ; /* good, this is expected also */
3754 warn_unexpected (connection, message, "not this error");
3761 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3769 dbus_message_unref (message);
3774 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3776 /* returns TRUE if the correct thing happens,
3777 * but the correct thing may include OOM errors.
3780 check_shell_service_success_auto_start (BusContext *context,
3781 DBusConnection *connection)
3783 DBusMessage *message;
3784 DBusMessage *base_service_message;
3785 dbus_uint32_t serial;
3787 const char *base_service;
3788 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3790 base_service_message = NULL;
3792 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3793 "/org/freedesktop/TestSuite",
3794 "org.freedesktop.TestSuite",
3797 if (message == NULL)
3800 if (!dbus_connection_send (connection, message, &serial))
3802 dbus_message_unref (message);
3806 dbus_message_unref (message);
3809 bus_test_run_everything (context);
3811 /* now wait for the message bus to hear back from the activated
3814 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3815 bus_test_run_everything (context);
3817 if (!dbus_connection_get_is_connected (connection))
3819 _dbus_verbose ("connection was disconnected\n");
3825 message = pop_message_waiting_for_memory (connection);
3826 if (message == NULL)
3828 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3829 serial, connection);
3833 verbose_message_received (connection, message);
3834 _dbus_verbose (" (after sending %s)\n", "auto start");
3836 /* we should get zero or two ServiceOwnerChanged signals */
3837 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3839 GotServiceInfo message_kind;
3841 if (!check_base_service_activated (context, connection,
3842 message, &base_service))
3845 base_service_message = message;
3848 /* We may need to block here for the test service to exit or finish up */
3849 block_connection_until_message_from_bus (context, connection, "service to exit");
3851 /* Should get a service creation notification for the activated
3852 * service name, or a service deletion on the base service name
3854 message = dbus_connection_borrow_message (connection);
3855 if (message == NULL)
3857 _dbus_warn ("No message after auto activation "
3858 "(should be a service announcement)\n");
3859 dbus_connection_return_message (connection, message);
3864 message_kind = check_got_service_info (message);
3866 dbus_connection_return_message (connection, message);
3869 switch (message_kind)
3871 case GOT_SERVICE_CREATED:
3872 message = pop_message_waiting_for_memory (connection);
3873 if (message == NULL)
3875 _dbus_warn ("Failed to pop message we just put back! "
3876 "should have been a NameOwnerChanged (creation)\n");
3880 /* Check that ServiceOwnerChanged (creation) was correctly received */
3881 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3882 base_service, message))
3885 dbus_message_unref (message);
3890 case GOT_SERVICE_DELETED:
3892 /* The service started up and got a base address, but then
3893 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3895 CheckServiceOwnerChangedData socd;
3897 socd.expected_kind = SERVICE_DELETED;
3898 socd.expected_service_name = base_service;
3899 socd.failed = FALSE;
3900 socd.skip_connection = NULL;
3901 bus_test_clients_foreach (check_service_owner_changed_foreach,
3911 case GOT_SOMETHING_ELSE:
3912 _dbus_warn ("Unexpected message after auto activation\n");
3917 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3918 * come the method reply (or error) from the initial method call
3921 /* Note: if this test is run in OOM mode, it will block when the bus
3922 * doesn't send a reply due to OOM.
3924 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3926 message = pop_message_waiting_for_memory (connection);
3927 if (message == NULL)
3929 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3933 if (dbus_message_get_reply_serial (message) != serial)
3935 _dbus_warn ("Wrong reply serial\n");
3939 if (!dbus_message_get_args (message, NULL,
3940 DBUS_TYPE_STRING, &argv[0],
3941 DBUS_TYPE_STRING, &argv[1],
3942 DBUS_TYPE_STRING, &argv[2],
3943 DBUS_TYPE_STRING, &argv[3],
3944 DBUS_TYPE_STRING, &argv[4],
3945 DBUS_TYPE_STRING, &argv[5],
3946 DBUS_TYPE_STRING, &argv[6],
3949 _dbus_warn ("Error getting arguments from return\n");
3953 /* don't worry about arg[0] as it may be different
3954 depending on the path to the tests
3956 if (strcmp("-test", argv[1]) != 0)
3958 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3963 if (strcmp("that", argv[2]) != 0)
3965 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3970 if (strcmp("we get", argv[3]) != 0)
3972 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3977 if (strcmp("back", argv[4]) != 0)
3979 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3984 if (strcmp("--what", argv[5]) != 0)
3986 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3991 if (strcmp("we put in", argv[6]) != 0)
3993 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3994 "we put in", argv[6]);
3998 dbus_message_unref (message);
4001 if (!check_send_exit_to_service (context, connection,
4002 SHELL_SUCCESS_SERVICE_NAME,
4010 dbus_message_unref (message);
4012 if (base_service_message)
4013 dbus_message_unref (base_service_message);
4021 BusContext *context;
4025 check_oom_check1_func (void *data)
4027 Check1Data *d = data;
4029 if (! (* d->func) (d->context))
4032 if (!check_no_leftovers (d->context))
4034 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4042 check1_try_iterations (BusContext *context,
4043 const char *description,
4049 d.context = context;
4051 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4053 _dbus_assert_not_reached ("test failed");
4057 check_get_services (BusContext *context,
4058 DBusConnection *connection,
4063 DBusMessage *message;
4064 dbus_uint32_t serial;
4071 dbus_error_init (&error);
4074 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4076 DBUS_INTERFACE_DBUS,
4079 if (message == NULL)
4082 if (!dbus_connection_send (connection, message, &serial))
4084 dbus_message_unref (message);
4088 /* send our message */
4089 bus_test_run_clients_loop (SEND_PENDING (connection));
4091 dbus_message_unref (message);
4094 dbus_connection_ref (connection); /* because we may get disconnected */
4095 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4097 if (!dbus_connection_get_is_connected (connection))
4099 _dbus_verbose ("connection was disconnected\n");
4101 dbus_connection_unref (connection);
4106 dbus_connection_unref (connection);
4108 message = pop_message_waiting_for_memory (connection);
4109 if (message == NULL)
4111 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4112 method, serial, connection);
4116 verbose_message_received (connection, message);
4118 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4120 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4122 ; /* good, this is a valid response */
4126 warn_unexpected (connection, message, "not this error");
4133 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4135 ; /* good, expected */
4139 warn_unexpected (connection, message,
4140 "method_return for ListActivatableNames/ListNames");
4147 if (!dbus_message_get_args (message, &error,
4153 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4155 _dbus_verbose ("no memory to list services by %s\n", method);
4156 dbus_error_free (&error);
4157 _dbus_wait_for_memory ();
4158 goto retry_get_property;
4162 _dbus_assert (dbus_error_is_set (&error));
4163 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4172 if (!check_no_leftovers (context))
4178 dbus_error_free (&error);
4181 dbus_message_unref (message);
4186 /* returns TRUE if the correct thing happens,
4187 * but the correct thing may include OOM errors.
4190 check_list_services (BusContext *context,
4191 DBusConnection *connection)
4193 DBusMessage *message;
4194 DBusMessage *base_service_message;
4195 const char *base_service;
4196 dbus_uint32_t serial;
4198 const char *existent = EXISTENT_SERVICE_NAME;
4199 dbus_uint32_t flags;
4203 _dbus_verbose ("check_list_services for %p\n", connection);
4205 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4210 if (!_dbus_string_array_contains ((const char **)services, existent))
4212 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4213 dbus_free_string_array (services);
4217 dbus_free_string_array (services);
4219 base_service_message = NULL;
4221 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4223 DBUS_INTERFACE_DBUS,
4224 "StartServiceByName");
4226 if (message == NULL)
4229 dbus_message_set_auto_start (message, FALSE);
4232 if (!dbus_message_append_args (message,
4233 DBUS_TYPE_STRING, &existent,
4234 DBUS_TYPE_UINT32, &flags,
4237 dbus_message_unref (message);
4241 if (!dbus_connection_send (connection, message, &serial))
4243 dbus_message_unref (message);
4247 dbus_message_unref (message);
4250 bus_test_run_everything (context);
4252 /* now wait for the message bus to hear back from the activated
4255 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4257 bus_test_run_everything (context);
4259 if (!dbus_connection_get_is_connected (connection))
4261 _dbus_verbose ("connection was disconnected\n");
4267 message = pop_message_waiting_for_memory (connection);
4268 if (message == NULL)
4270 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4271 "StartServiceByName", serial, connection);
4275 verbose_message_received (connection, message);
4276 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4278 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4280 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4282 _dbus_warn ("Message has wrong sender %s\n",
4283 dbus_message_get_sender (message) ?
4284 dbus_message_get_sender (message) : "(none)");
4288 if (dbus_message_is_error (message,
4289 DBUS_ERROR_NO_MEMORY))
4291 ; /* good, this is a valid response */
4293 else if (dbus_message_is_error (message,
4294 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4295 dbus_message_is_error (message,
4296 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4297 dbus_message_is_error (message,
4298 DBUS_ERROR_SPAWN_EXEC_FAILED))
4300 ; /* good, this is expected also */
4304 _dbus_warn ("Did not expect error %s\n",
4305 dbus_message_get_error_name (message));
4311 GotServiceInfo message_kind;
4313 if (!check_base_service_activated (context, connection,
4314 message, &base_service))
4317 base_service_message = message;
4320 /* We may need to block here for the test service to exit or finish up */
4321 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4323 message = dbus_connection_borrow_message (connection);
4324 if (message == NULL)
4326 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4330 message_kind = check_got_service_info (message);
4332 dbus_connection_return_message (connection, message);
4335 switch (message_kind)
4337 case GOT_SOMETHING_ELSE:
4339 case GOT_SERVICE_DELETED:
4340 _dbus_warn ("Unexpected message after ActivateService "
4341 "(should be an error or a service announcement)\n");
4344 case GOT_SERVICE_CREATED:
4345 message = pop_message_waiting_for_memory (connection);
4346 if (message == NULL)
4348 _dbus_warn ("Failed to pop message we just put back! "
4349 "should have been a NameOwnerChanged (creation)\n");
4353 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4354 base_service, message))
4357 dbus_message_unref (message);
4360 if (!check_no_leftovers (context))
4362 _dbus_warn ("Messages were left over after successful activation\n");
4370 if (!check_get_services (context, connection, "ListNames", &services, &len))
4375 if (!_dbus_string_array_contains ((const char **)services, existent))
4377 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4381 dbus_free_string_array (services);
4383 if (!check_send_exit_to_service (context, connection,
4384 EXISTENT_SERVICE_NAME, base_service))
4391 dbus_message_unref (message);
4393 if (base_service_message)
4394 dbus_message_unref (base_service_message);
4402 BusContext *context;
4403 DBusConnection *connection;
4407 check_oom_check2_func (void *data)
4409 Check2Data *d = data;
4411 if (! (* d->func) (d->context, d->connection))
4414 if (!check_no_leftovers (d->context))
4416 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4424 check2_try_iterations (BusContext *context,
4425 DBusConnection *connection,
4426 const char *description,
4432 d.context = context;
4433 d.connection = connection;
4435 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4438 _dbus_warn ("%s failed during oom\n", description);
4439 _dbus_assert_not_reached ("test failed");
4444 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4445 const char *filename)
4450 if (!_dbus_string_init (&full))
4453 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4455 _dbus_string_free (&full);
4459 _dbus_string_init_const (&file, filename);
4461 if (!_dbus_concat_dir_and_file (&full, &file))
4463 _dbus_string_free (&full);
4467 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4468 _dbus_string_get_const_data (&full));
4470 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4472 _dbus_string_free (&full);
4478 bus_dispatch_test_conf (const DBusString *test_data_dir,
4479 const char *filename,
4480 dbus_bool_t use_launcher)
4482 BusContext *context;
4483 DBusConnection *foo;
4484 DBusConnection *bar;
4485 DBusConnection *baz;
4488 /* save the config name for the activation helper */
4489 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4490 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4492 dbus_error_init (&error);
4494 context = bus_context_new_test (test_data_dir, filename);
4495 if (context == NULL)
4498 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4500 _dbus_assert_not_reached ("could not alloc connection");
4502 if (!bus_setup_debug_client (foo))
4503 _dbus_assert_not_reached ("could not set up connection");
4505 spin_connection_until_authenticated (context, foo);
4507 if (!check_hello_message (context, foo))
4508 _dbus_assert_not_reached ("hello message failed");
4510 if (!check_double_hello_message (context, foo))
4511 _dbus_assert_not_reached ("double hello message failed");
4513 if (!check_add_match_all (context, foo))
4514 _dbus_assert_not_reached ("AddMatch message failed");
4516 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4518 _dbus_assert_not_reached ("could not alloc connection");
4520 if (!bus_setup_debug_client (bar))
4521 _dbus_assert_not_reached ("could not set up connection");
4523 spin_connection_until_authenticated (context, bar);
4525 if (!check_hello_message (context, bar))
4526 _dbus_assert_not_reached ("hello message failed");
4528 if (!check_add_match_all (context, bar))
4529 _dbus_assert_not_reached ("AddMatch message failed");
4531 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4533 _dbus_assert_not_reached ("could not alloc connection");
4535 if (!bus_setup_debug_client (baz))
4536 _dbus_assert_not_reached ("could not set up connection");
4538 spin_connection_until_authenticated (context, baz);
4540 if (!check_hello_message (context, baz))
4541 _dbus_assert_not_reached ("hello message failed");
4543 if (!check_add_match_all (context, baz))
4544 _dbus_assert_not_reached ("AddMatch message failed");
4546 #ifdef DBUS_WIN_FIXME
4547 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4548 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4550 if (!check_get_connection_unix_user (context, baz))
4551 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4553 if (!check_get_connection_unix_process_id (context, baz))
4554 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4557 if (!check_list_services (context, baz))
4558 _dbus_assert_not_reached ("ListActivatableNames message failed");
4560 if (!check_no_leftovers (context))
4562 _dbus_warn ("Messages were left over after setting up initial connections\n");
4563 _dbus_assert_not_reached ("initial connection setup failed");
4566 check1_try_iterations (context, "create_and_hello",
4567 check_hello_connection);
4569 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4570 check_nonexistent_service_no_auto_start);
4572 #ifdef DBUS_WIN_FIXME
4573 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4575 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4576 check_segfault_service_no_auto_start);
4579 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4580 check_existent_service_no_auto_start);
4582 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4583 check_nonexistent_service_auto_start);
4586 #ifdef DBUS_WIN_FIXME
4587 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4589 /* only do the segfault test if we are not using the launcher */
4590 check2_try_iterations (context, foo, "segfault_service_auto_start",
4591 check_segfault_service_auto_start);
4594 /* only do the shell fail test if we are not using the launcher */
4595 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4596 check_shell_fail_service_auto_start);
4598 /* specific to launcher */
4600 if (!check_launch_service_file_missing (context, foo))
4601 _dbus_assert_not_reached ("did not get service file not found error");
4604 /* Note: need to resolve some issues with the testing code in order to run
4605 * this in oom (handle that we sometimes don't get replies back from the bus
4606 * when oom happens, without blocking the test).
4608 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4609 check_existent_service_auto_start);
4612 if (!check_existent_service_auto_start (context, foo))
4613 _dbus_assert_not_reached ("existent service auto start failed");
4615 if (!check_shell_service_success_auto_start (context, foo))
4616 _dbus_assert_not_reached ("shell success service auto start failed");
4618 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4620 kill_client_connection_unchecked (foo);
4621 kill_client_connection_unchecked (bar);
4622 kill_client_connection_unchecked (baz);
4624 bus_context_unref (context);
4630 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4631 const char *filename)
4633 BusContext *context;
4634 DBusConnection *foo;
4637 /* save the config name for the activation helper */
4638 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4639 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4641 dbus_error_init (&error);
4643 context = bus_context_new_test (test_data_dir, filename);
4644 if (context == NULL)
4647 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4649 _dbus_assert_not_reached ("could not alloc connection");
4651 if (!bus_setup_debug_client (foo))
4652 _dbus_assert_not_reached ("could not set up connection");
4654 spin_connection_until_authenticated (context, foo);
4656 if (!check_hello_message (context, foo))
4657 _dbus_assert_not_reached ("hello message failed");
4659 if (!check_double_hello_message (context, foo))
4660 _dbus_assert_not_reached ("double hello message failed");
4662 if (!check_add_match_all (context, foo))
4663 _dbus_assert_not_reached ("AddMatch message failed");
4665 /* this only tests the activation.c user check */
4666 if (!check_launch_service_user_missing (context, foo))
4667 _dbus_assert_not_reached ("user missing did not trigger error");
4669 /* this only tests the desktop.c exec check */
4670 if (!check_launch_service_exec_missing (context, foo))
4671 _dbus_assert_not_reached ("exec missing did not trigger error");
4673 /* this only tests the desktop.c service check */
4674 if (!check_launch_service_service_missing (context, foo))
4675 _dbus_assert_not_reached ("service missing did not trigger error");
4677 _dbus_verbose ("Disconnecting foo\n");
4679 kill_client_connection_unchecked (foo);
4681 bus_context_unref (context);
4687 bus_dispatch_test (const DBusString *test_data_dir)
4689 /* run normal activation tests */
4690 _dbus_verbose ("Normal activation tests\n");
4691 if (!bus_dispatch_test_conf (test_data_dir,
4692 "valid-config-files/debug-allow-all.conf", FALSE))
4696 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4698 /* run launch-helper activation tests */
4699 _dbus_verbose ("Launch helper activation tests\n");
4700 if (!bus_dispatch_test_conf (test_data_dir,
4701 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4704 /* run select launch-helper activation tests on broken service files */
4705 if (!bus_dispatch_test_conf_fail (test_data_dir,
4706 "valid-config-files-system/debug-allow-all-fail.conf"))
4714 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4716 BusContext *context;
4717 DBusConnection *foo;
4720 dbus_error_init (&error);
4722 /* Test SHA1 authentication */
4723 _dbus_verbose ("Testing SHA1 context\n");
4725 context = bus_context_new_test (test_data_dir,
4726 "valid-config-files/debug-allow-all-sha1.conf");
4727 if (context == NULL)
4730 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4732 _dbus_assert_not_reached ("could not alloc connection");
4734 if (!bus_setup_debug_client (foo))
4735 _dbus_assert_not_reached ("could not set up connection");
4737 spin_connection_until_authenticated (context, foo);
4739 if (!check_hello_message (context, foo))
4740 _dbus_assert_not_reached ("hello message failed");
4742 if (!check_add_match_all (context, foo))
4743 _dbus_assert_not_reached ("addmatch message failed");
4745 if (!check_no_leftovers (context))
4747 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4748 _dbus_assert_not_reached ("initial connection setup failed");
4751 check1_try_iterations (context, "create_and_hello_sha1",
4752 check_hello_connection);
4754 kill_client_connection_unchecked (foo);
4756 bus_context_unref (context);
4761 #ifdef HAVE_UNIX_FD_PASSING
4764 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4766 BusContext *context;
4767 DBusConnection *foo, *bar;
4770 int one[2], two[2], x, y, z;
4773 dbus_error_init (&error);
4775 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4776 if (context == NULL)
4777 _dbus_assert_not_reached ("could not alloc context");
4779 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4781 _dbus_assert_not_reached ("could not alloc connection");
4783 if (!bus_setup_debug_client (foo))
4784 _dbus_assert_not_reached ("could not set up connection");
4786 spin_connection_until_authenticated (context, foo);
4788 if (!check_hello_message (context, foo))
4789 _dbus_assert_not_reached ("hello message failed");
4791 if (!check_add_match_all (context, foo))
4792 _dbus_assert_not_reached ("AddMatch message failed");
4794 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4796 _dbus_assert_not_reached ("could not alloc connection");
4798 if (!bus_setup_debug_client (bar))
4799 _dbus_assert_not_reached ("could not set up connection");
4801 spin_connection_until_authenticated (context, bar);
4803 if (!check_hello_message (context, bar))
4804 _dbus_assert_not_reached ("hello message failed");
4806 if (!check_add_match_all (context, bar))
4807 _dbus_assert_not_reached ("AddMatch message failed");
4809 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4810 _dbus_assert_not_reached ("could not alloc message");
4812 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4813 _dbus_assert_not_reached("Failed to allocate pipe #1");
4815 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4816 _dbus_assert_not_reached("Failed to allocate pipe #2");
4818 if (!dbus_message_append_args(m,
4819 DBUS_TYPE_UNIX_FD, one,
4820 DBUS_TYPE_UNIX_FD, two,
4821 DBUS_TYPE_UNIX_FD, two,
4823 _dbus_assert_not_reached("Failed to attach fds.");
4825 if (!_dbus_close(one[0], &error))
4826 _dbus_assert_not_reached("Failed to close pipe #1 ");
4827 if (!_dbus_close(two[0], &error))
4828 _dbus_assert_not_reached("Failed to close pipe #2 ");
4830 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4831 _dbus_assert_not_reached("Connection cannot do fd passing");
4833 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4834 _dbus_assert_not_reached("Connection cannot do fd passing");
4836 if (!dbus_connection_send (foo, m, NULL))
4837 _dbus_assert_not_reached("Failed to send fds");
4839 dbus_message_unref(m);
4841 bus_test_run_clients_loop (SEND_PENDING (foo));
4843 bus_test_run_everything (context);
4845 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4847 if (!(m = pop_message_waiting_for_memory (foo)))
4848 _dbus_assert_not_reached("Failed to receive msg");
4850 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4851 _dbus_assert_not_reached("bogus message received");
4853 dbus_message_unref(m);
4855 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4857 if (!(m = pop_message_waiting_for_memory (bar)))
4858 _dbus_assert_not_reached("Failed to receive msg");
4860 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4861 _dbus_assert_not_reached("bogus message received");
4863 if (!dbus_message_get_args(m,
4865 DBUS_TYPE_UNIX_FD, &x,
4866 DBUS_TYPE_UNIX_FD, &y,
4867 DBUS_TYPE_UNIX_FD, &z,
4869 _dbus_assert_not_reached("Failed to parse fds.");
4871 dbus_message_unref(m);
4873 if (write(x, "X", 1) != 1)
4874 _dbus_assert_not_reached("Failed to write to pipe #1");
4875 if (write(y, "Y", 1) != 1)
4876 _dbus_assert_not_reached("Failed to write to pipe #2");
4877 if (write(z, "Z", 1) != 1)
4878 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4880 if (!_dbus_close(x, &error))
4881 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4882 if (!_dbus_close(y, &error))
4883 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4884 if (!_dbus_close(z, &error))
4885 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4887 if (read(one[1], &r, 1) != 1 || r != 'X')
4888 _dbus_assert_not_reached("Failed to read value from pipe.");
4889 if (read(two[1], &r, 1) != 1 || r != 'Y')
4890 _dbus_assert_not_reached("Failed to read value from pipe.");
4891 if (read(two[1], &r, 1) != 1 || r != 'Z')
4892 _dbus_assert_not_reached("Failed to read value from pipe.");
4894 if (!_dbus_close(one[1], &error))
4895 _dbus_assert_not_reached("Failed to close pipe #1 ");
4896 if (!_dbus_close(two[1], &error))
4897 _dbus_assert_not_reached("Failed to close pipe #2 ");
4899 _dbus_verbose ("Disconnecting foo\n");
4900 kill_client_connection_unchecked (foo);
4902 _dbus_verbose ("Disconnecting bar\n");
4903 kill_client_connection_unchecked (bar);
4905 bus_context_unref (context);
4911 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */