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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "connection.h"
30 #include "activation.h"
35 #include <dbus/dbus-internals.h>
38 #ifdef HAVE_UNIX_FD_PASSING
39 #include <dbus/dbus-sysdeps-unix.h>
44 send_one_message (DBusConnection *connection,
46 DBusConnection *sender,
47 DBusConnection *addressed_recipient,
49 BusTransaction *transaction,
52 if (!bus_context_check_security_policy (context, transaction,
58 return TRUE; /* silently don't send it */
60 if (!bus_transaction_send (transaction,
72 bus_dispatch_matches (BusTransaction *transaction,
73 DBusConnection *sender,
74 DBusConnection *addressed_recipient,
79 BusConnections *connections;
81 BusMatchmaker *matchmaker;
85 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
87 /* sender and recipient can both be NULL for the bus driver,
88 * or for signals with no particular recipient
91 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
92 _dbus_assert (dbus_message_get_sender (message) != NULL);
94 connections = bus_transaction_get_connections (transaction);
96 dbus_error_init (&tmp_error);
97 context = bus_transaction_get_context (transaction);
98 matchmaker = bus_context_get_matchmaker (context);
101 if (!bus_matchmaker_get_recipients (matchmaker, connections,
102 sender, addressed_recipient, message,
109 link = _dbus_list_get_first_link (&recipients);
112 DBusConnection *dest;
116 if (!send_one_message (dest, context, sender, addressed_recipient,
117 message, transaction, &tmp_error))
120 link = _dbus_list_get_next_link (&recipients, link);
123 _dbus_list_clear (&recipients);
125 if (dbus_error_is_set (&tmp_error))
127 dbus_move_error (&tmp_error, error);
134 static DBusHandlerResult
135 bus_dispatch (DBusConnection *connection,
136 DBusMessage *message)
138 const char *sender, *service_name;
140 BusTransaction *transaction;
142 DBusHandlerResult result;
143 DBusConnection *addressed_recipient;
145 result = DBUS_HANDLER_RESULT_HANDLED;
148 addressed_recipient = NULL;
149 dbus_error_init (&error);
151 context = bus_connection_get_context (connection);
152 _dbus_assert (context != NULL);
154 /* If we can't even allocate an OOM error, we just go to sleep
157 while (!bus_connection_preallocate_oom_error (connection))
158 _dbus_wait_for_memory ();
160 /* Ref connection in case we disconnect it at some point in here */
161 dbus_connection_ref (connection);
163 service_name = dbus_message_get_destination (message);
165 #ifdef DBUS_ENABLE_VERBOSE_MODE
167 const char *interface_name, *member_name, *error_name;
169 interface_name = dbus_message_get_interface (message);
170 member_name = dbus_message_get_member (message);
171 error_name = dbus_message_get_error_name (message);
173 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
174 interface_name ? interface_name : "(no interface)",
175 member_name ? member_name : "(no member)",
176 error_name ? error_name : "(no error name)",
177 service_name ? service_name : "peer");
179 #endif /* DBUS_ENABLE_VERBOSE_MODE */
181 /* If service_name is NULL, if it's a signal we send it to all
182 * connections with a match rule. If it's not a signal, there
183 * are some special cases here but mostly we just bail out.
185 if (service_name == NULL)
187 if (dbus_message_is_signal (message,
188 DBUS_INTERFACE_LOCAL,
191 bus_connection_disconnected (connection);
195 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
197 /* DBusConnection also handles some of these automatically, we leave
200 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205 /* Create our transaction */
206 transaction = bus_transaction_new (context);
207 if (transaction == NULL)
209 BUS_SET_OOM (&error);
213 /* Assign a sender to the message */
214 if (bus_connection_is_active (connection))
216 sender = bus_connection_get_name (connection);
217 _dbus_assert (sender != NULL);
219 if (!dbus_message_set_sender (message, sender))
221 BUS_SET_OOM (&error);
225 /* We need to refetch the service name here, because
226 * dbus_message_set_sender can cause the header to be
227 * reallocated, and thus the service_name pointer will become
230 service_name = dbus_message_get_destination (message);
234 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
236 if (!bus_context_check_security_policy (context, transaction,
237 connection, NULL, NULL, message, &error))
239 _dbus_verbose ("Security policy rejected message\n");
243 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
244 if (!bus_driver_handle_message (connection, transaction, message, &error))
247 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
249 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
250 dbus_connection_close (connection);
253 else if (service_name != NULL) /* route to named service */
255 DBusString service_string;
257 BusRegistry *registry;
259 _dbus_assert (service_name != NULL);
261 registry = bus_connection_get_registry (connection);
263 _dbus_string_init_const (&service_string, service_name);
264 service = bus_registry_lookup (registry, &service_string);
266 if (service == NULL && dbus_message_get_auto_start (message))
268 BusActivation *activation;
269 /* We can't do the security policy check here, since the addressed
270 * recipient service doesn't exist yet. We do it before sending the
271 * message after the service has been created.
273 activation = bus_connection_get_activation (connection);
275 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
276 message, service_name, &error))
278 _DBUS_ASSERT_ERROR_IS_SET (&error);
279 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
285 else if (service == NULL)
287 dbus_set_error (&error,
288 DBUS_ERROR_NAME_HAS_NO_OWNER,
289 "Name \"%s\" does not exist",
295 addressed_recipient = bus_service_get_primary_owners_connection (service);
296 _dbus_assert (addressed_recipient != NULL);
298 if (!bus_context_check_security_policy (context, transaction,
299 connection, addressed_recipient,
304 /* Dispatch the message */
305 if (!bus_transaction_send (transaction, addressed_recipient, message))
307 BUS_SET_OOM (&error);
313 /* Now match the messages against any match rules, which will send
314 * out signals and such. addressed_recipient may == NULL.
316 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
320 if (dbus_error_is_set (&error))
322 if (!dbus_connection_get_is_connected (connection))
324 /* If we disconnected it, we won't bother to send it any error
327 _dbus_verbose ("Not sending error to connection we disconnected\n");
329 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
331 bus_connection_send_oom_error (connection, message);
333 /* cancel transaction due to OOM */
334 if (transaction != NULL)
336 bus_transaction_cancel_and_free (transaction);
342 /* Try to send the real error, if no mem to do that, send
345 _dbus_assert (transaction != NULL);
346 if (!bus_transaction_send_error_reply (transaction, connection,
349 bus_connection_send_oom_error (connection, message);
351 /* cancel transaction due to OOM */
352 if (transaction != NULL)
354 bus_transaction_cancel_and_free (transaction);
361 dbus_error_free (&error);
364 if (transaction != NULL)
366 bus_transaction_execute_and_free (transaction);
369 dbus_connection_unref (connection);
374 static DBusHandlerResult
375 bus_dispatch_message_filter (DBusConnection *connection,
376 DBusMessage *message,
379 return bus_dispatch (connection, message);
383 bus_dispatch_add_connection (DBusConnection *connection)
385 if (!dbus_connection_add_filter (connection,
386 bus_dispatch_message_filter,
394 bus_dispatch_remove_connection (DBusConnection *connection)
396 /* Here we tell the bus driver that we want to get off. */
397 bus_driver_remove_connection (connection);
399 dbus_connection_remove_filter (connection,
400 bus_dispatch_message_filter,
404 #ifdef DBUS_BUILD_TESTS
408 /* This is used to know whether we need to block in order to finish
409 * sending a message, or whether the initial dbus_connection_send()
410 * already flushed the queue.
412 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
414 typedef dbus_bool_t (* Check1Func) (BusContext *context);
415 typedef dbus_bool_t (* Check2Func) (BusContext *context,
416 DBusConnection *connection);
418 static dbus_bool_t check_no_leftovers (BusContext *context);
421 block_connection_until_message_from_bus (BusContext *context,
422 DBusConnection *connection,
423 const char *what_is_expected)
425 _dbus_verbose ("expecting: %s\n", what_is_expected);
427 while (dbus_connection_get_dispatch_status (connection) ==
428 DBUS_DISPATCH_COMPLETE &&
429 dbus_connection_get_is_connected (connection))
431 bus_test_run_bus_loop (context, TRUE);
432 bus_test_run_clients_loop (FALSE);
437 spin_connection_until_authenticated (BusContext *context,
438 DBusConnection *connection)
440 _dbus_verbose ("Spinning to auth connection %p\n", connection);
441 while (!dbus_connection_get_is_authenticated (connection) &&
442 dbus_connection_get_is_connected (connection))
444 bus_test_run_bus_loop (context, FALSE);
445 bus_test_run_clients_loop (FALSE);
447 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
450 /* compensate for fact that pop_message() can return #NULL due to OOM */
452 pop_message_waiting_for_memory (DBusConnection *connection)
454 while (dbus_connection_get_dispatch_status (connection) ==
455 DBUS_DISPATCH_NEED_MEMORY)
456 _dbus_wait_for_memory ();
458 return dbus_connection_pop_message (connection);
462 borrow_message_waiting_for_memory (DBusConnection *connection)
464 while (dbus_connection_get_dispatch_status (connection) ==
465 DBUS_DISPATCH_NEED_MEMORY)
466 _dbus_wait_for_memory ();
468 return dbus_connection_borrow_message (connection);
472 warn_unexpected_real (DBusConnection *connection,
473 DBusMessage *message,
474 const char *expected,
475 const char *function,
479 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
481 dbus_message_get_interface (message) ?
482 dbus_message_get_interface (message) : "(unset)",
483 dbus_message_get_member (message) ?
484 dbus_message_get_member (message) : "(unset)",
485 dbus_message_get_error_name (message) ?
486 dbus_message_get_error_name (message) : "(unset)",
490 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
491 function, line, connection, expected);
494 #define warn_unexpected(connection, message, expected) \
495 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
498 verbose_message_received (DBusConnection *connection,
499 DBusMessage *message)
501 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
502 dbus_message_get_interface (message) ?
503 dbus_message_get_interface (message) : "(unset)",
504 dbus_message_get_member (message) ?
505 dbus_message_get_member (message) : "(unset)",
506 dbus_message_get_error_name (message) ?
507 dbus_message_get_error_name (message) : "(unset)",
520 ServiceInfoKind expected_kind;
521 const char *expected_service_name;
523 DBusConnection *skip_connection;
524 } CheckServiceOwnerChangedData;
527 check_service_owner_changed_foreach (DBusConnection *connection,
530 CheckServiceOwnerChangedData *d = data;
531 DBusMessage *message;
533 const char *service_name, *old_owner, *new_owner;
535 if (d->expected_kind == SERVICE_CREATED
536 && connection == d->skip_connection)
539 dbus_error_init (&error);
542 message = pop_message_waiting_for_memory (connection);
545 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
546 connection, "NameOwnerChanged");
549 else if (!dbus_message_is_signal (message,
553 warn_unexpected (connection, message, "NameOwnerChanged");
559 reget_service_info_data:
564 dbus_message_get_args (message, &error,
565 DBUS_TYPE_STRING, &service_name,
566 DBUS_TYPE_STRING, &old_owner,
567 DBUS_TYPE_STRING, &new_owner,
570 if (dbus_error_is_set (&error))
572 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
574 dbus_error_free (&error);
575 _dbus_wait_for_memory ();
576 goto reget_service_info_data;
580 _dbus_warn ("Did not get the expected arguments\n");
585 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
586 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
587 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
589 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
593 if (strcmp (service_name, d->expected_service_name) != 0)
595 _dbus_warn ("expected info on service %s, got info on %s\n",
596 d->expected_service_name,
601 if (*service_name == ':' && new_owner[0]
602 && strcmp (service_name, new_owner) != 0)
604 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
605 service_name, old_owner, new_owner);
613 dbus_error_free (&error);
616 dbus_message_unref (message);
623 kill_client_connection (BusContext *context,
624 DBusConnection *connection)
628 CheckServiceOwnerChangedData socd;
630 _dbus_verbose ("killing connection %p\n", connection);
632 s = dbus_bus_get_unique_name (connection);
633 _dbus_assert (s != NULL);
635 while ((base_service = _dbus_strdup (s)) == NULL)
636 _dbus_wait_for_memory ();
638 dbus_connection_ref (connection);
640 /* kick in the disconnect handler that unrefs the connection */
641 dbus_connection_close (connection);
643 bus_test_run_everything (context);
645 _dbus_assert (bus_test_client_listed (connection));
647 /* Run disconnect handler in test.c */
648 if (bus_connection_dispatch_one_message (connection))
649 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
651 _dbus_assert (!dbus_connection_get_is_connected (connection));
652 dbus_connection_unref (connection);
654 _dbus_assert (!bus_test_client_listed (connection));
656 socd.expected_kind = SERVICE_DELETED;
657 socd.expected_service_name = base_service;
659 socd.skip_connection = NULL;
661 bus_test_clients_foreach (check_service_owner_changed_foreach,
664 dbus_free (base_service);
667 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
669 if (!check_no_leftovers (context))
670 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
674 kill_client_connection_unchecked (DBusConnection *connection)
676 /* This kills the connection without expecting it to affect
677 * the rest of the bus.
679 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
681 dbus_connection_ref (connection);
682 dbus_connection_close (connection);
683 /* dispatching disconnect handler will unref once */
684 if (bus_connection_dispatch_one_message (connection))
685 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
687 _dbus_assert (!bus_test_client_listed (connection));
688 dbus_connection_unref (connection);
694 } CheckNoMessagesData;
697 check_no_messages_foreach (DBusConnection *connection,
700 CheckNoMessagesData *d = data;
701 DBusMessage *message;
703 message = pop_message_waiting_for_memory (connection);
706 warn_unexpected (connection, message, "no messages");
712 dbus_message_unref (message);
717 check_no_leftovers (BusContext *context)
719 CheckNoMessagesData nmd;
722 bus_test_clients_foreach (check_no_messages_foreach,
727 _dbus_verbose ("%s: leftover message found\n",
728 _DBUS_FUNCTION_NAME);
735 /* returns TRUE if the correct thing happens,
736 * but the correct thing may include OOM errors.
739 check_hello_message (BusContext *context,
740 DBusConnection *connection)
742 DBusMessage *message;
743 DBusMessage *name_message;
744 dbus_uint32_t serial;
748 const char *acquired;
751 dbus_error_init (&error);
757 _dbus_verbose ("check_hello_message for %p\n", connection);
759 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
767 dbus_connection_ref (connection); /* because we may get disconnected */
769 if (!dbus_connection_send (connection, message, &serial))
771 dbus_message_unref (message);
772 dbus_connection_unref (connection);
776 _dbus_assert (dbus_message_has_signature (message, ""));
778 dbus_message_unref (message);
781 if (!dbus_connection_get_is_connected (connection))
783 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
785 dbus_connection_unref (connection);
790 /* send our message */
791 bus_test_run_clients_loop (SEND_PENDING (connection));
793 if (!dbus_connection_get_is_connected (connection))
795 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
797 dbus_connection_unref (connection);
802 block_connection_until_message_from_bus (context, connection, "reply to Hello");
804 if (!dbus_connection_get_is_connected (connection))
806 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
808 dbus_connection_unref (connection);
813 dbus_connection_unref (connection);
815 message = pop_message_waiting_for_memory (connection);
818 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
819 "Hello", serial, connection);
823 verbose_message_received (connection, message);
825 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
827 _dbus_warn ("Message has wrong sender %s\n",
828 dbus_message_get_sender (message) ?
829 dbus_message_get_sender (message) : "(none)");
833 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
835 if (dbus_message_is_error (message,
836 DBUS_ERROR_NO_MEMORY))
838 ; /* good, this is a valid response */
842 warn_unexpected (connection, message, "not this error");
849 CheckServiceOwnerChangedData socd;
851 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
853 ; /* good, expected */
857 warn_unexpected (connection, message, "method return for Hello");
862 retry_get_hello_name:
863 if (!dbus_message_get_args (message, &error,
864 DBUS_TYPE_STRING, &name,
867 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
869 _dbus_verbose ("no memory to get service name arg from hello\n");
870 dbus_error_free (&error);
871 _dbus_wait_for_memory ();
872 goto retry_get_hello_name;
876 _dbus_assert (dbus_error_is_set (&error));
877 _dbus_warn ("Did not get the expected single string argument to hello\n");
882 _dbus_verbose ("Got hello name: %s\n", name);
884 while (!dbus_bus_set_unique_name (connection, name))
885 _dbus_wait_for_memory ();
887 socd.expected_kind = SERVICE_CREATED;
888 socd.expected_service_name = name;
890 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
891 bus_test_clients_foreach (check_service_owner_changed_foreach,
897 name_message = message;
898 /* Client should also have gotten ServiceAcquired */
900 message = pop_message_waiting_for_memory (connection);
903 _dbus_warn ("Expecting %s, got nothing\n",
907 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
910 _dbus_warn ("Expecting %s, got smthg else\n",
915 retry_get_acquired_name:
916 if (!dbus_message_get_args (message, &error,
917 DBUS_TYPE_STRING, &acquired,
920 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
922 _dbus_verbose ("no memory to get service name arg from acquired\n");
923 dbus_error_free (&error);
924 _dbus_wait_for_memory ();
925 goto retry_get_acquired_name;
929 _dbus_assert (dbus_error_is_set (&error));
930 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
935 _dbus_verbose ("Got acquired name: %s\n", acquired);
937 if (strcmp (acquired, name) != 0)
939 _dbus_warn ("Acquired name is %s but expected %s\n",
946 if (!check_no_leftovers (context))
952 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
954 dbus_error_free (&error);
957 dbus_message_unref (message);
960 dbus_message_unref (name_message);
965 /* returns TRUE if the correct thing happens,
966 * but the correct thing may include OOM errors.
969 check_double_hello_message (BusContext *context,
970 DBusConnection *connection)
972 DBusMessage *message;
973 dbus_uint32_t serial;
978 dbus_error_init (&error);
981 _dbus_verbose ("check_double_hello_message for %p\n", connection);
983 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
991 if (!dbus_connection_send (connection, message, &serial))
993 dbus_message_unref (message);
997 dbus_message_unref (message);
1000 /* send our message */
1001 bus_test_run_clients_loop (SEND_PENDING (connection));
1003 dbus_connection_ref (connection); /* because we may get disconnected */
1004 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1006 if (!dbus_connection_get_is_connected (connection))
1008 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1010 dbus_connection_unref (connection);
1015 dbus_connection_unref (connection);
1017 message = pop_message_waiting_for_memory (connection);
1018 if (message == NULL)
1020 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1021 "Hello", serial, connection);
1025 verbose_message_received (connection, message);
1027 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1029 _dbus_warn ("Message has wrong sender %s\n",
1030 dbus_message_get_sender (message) ?
1031 dbus_message_get_sender (message) : "(none)");
1035 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1037 warn_unexpected (connection, message, "method return for Hello");
1041 if (!check_no_leftovers (context))
1047 dbus_error_free (&error);
1050 dbus_message_unref (message);
1055 /* returns TRUE if the correct thing happens,
1056 * but the correct thing may include OOM errors.
1059 check_get_connection_unix_user (BusContext *context,
1060 DBusConnection *connection)
1062 DBusMessage *message;
1063 dbus_uint32_t serial;
1066 const char *base_service_name;
1070 dbus_error_init (&error);
1073 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1075 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1077 DBUS_INTERFACE_DBUS,
1078 "GetConnectionUnixUser");
1080 if (message == NULL)
1083 base_service_name = dbus_bus_get_unique_name (connection);
1085 if (!dbus_message_append_args (message,
1086 DBUS_TYPE_STRING, &base_service_name,
1089 dbus_message_unref (message);
1093 if (!dbus_connection_send (connection, message, &serial))
1095 dbus_message_unref (message);
1099 /* send our message */
1100 bus_test_run_clients_loop (SEND_PENDING (connection));
1102 dbus_message_unref (message);
1105 dbus_connection_ref (connection); /* because we may get disconnected */
1106 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1108 if (!dbus_connection_get_is_connected (connection))
1110 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1112 dbus_connection_unref (connection);
1117 dbus_connection_unref (connection);
1119 message = pop_message_waiting_for_memory (connection);
1120 if (message == NULL)
1122 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1123 "GetConnectionUnixUser", serial, connection);
1127 verbose_message_received (connection, message);
1129 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1131 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1133 ; /* good, this is a valid response */
1137 warn_unexpected (connection, message, "not this error");
1144 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1146 ; /* good, expected */
1150 warn_unexpected (connection, message,
1151 "method_return for GetConnectionUnixUser");
1158 if (!dbus_message_get_args (message, &error,
1159 DBUS_TYPE_UINT32, &uid,
1162 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1164 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1165 dbus_error_free (&error);
1166 _dbus_wait_for_memory ();
1167 goto retry_get_property;
1171 _dbus_assert (dbus_error_is_set (&error));
1172 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1178 if (!check_no_leftovers (context))
1184 dbus_error_free (&error);
1187 dbus_message_unref (message);
1192 /* returns TRUE if the correct thing happens,
1193 * but the correct thing may include OOM errors.
1196 check_get_connection_unix_process_id (BusContext *context,
1197 DBusConnection *connection)
1199 DBusMessage *message;
1200 dbus_uint32_t serial;
1203 const char *base_service_name;
1207 dbus_error_init (&error);
1210 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1212 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1214 DBUS_INTERFACE_DBUS,
1215 "GetConnectionUnixProcessID");
1217 if (message == NULL)
1220 base_service_name = dbus_bus_get_unique_name (connection);
1222 if (!dbus_message_append_args (message,
1223 DBUS_TYPE_STRING, &base_service_name,
1226 dbus_message_unref (message);
1230 if (!dbus_connection_send (connection, message, &serial))
1232 dbus_message_unref (message);
1236 /* send our message */
1237 bus_test_run_clients_loop (SEND_PENDING (connection));
1239 dbus_message_unref (message);
1242 dbus_connection_ref (connection); /* because we may get disconnected */
1243 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1245 if (!dbus_connection_get_is_connected (connection))
1247 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1249 dbus_connection_unref (connection);
1254 dbus_connection_unref (connection);
1256 message = pop_message_waiting_for_memory (connection);
1257 if (message == NULL)
1259 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1260 "GetConnectionUnixProcessID", serial, connection);
1264 verbose_message_received (connection, message);
1266 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1268 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1270 ; /* good, this is a valid response */
1273 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1275 /* We are expecting this error, since we know in the test suite we aren't
1276 * talking to a client running on UNIX
1278 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1283 warn_unexpected (connection, message, "not this error");
1291 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1294 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1296 ; /* good, expected */
1300 warn_unexpected (connection, message,
1301 "method_return for GetConnectionUnixProcessID");
1308 if (!dbus_message_get_args (message, &error,
1309 DBUS_TYPE_UINT32, &pid,
1312 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1314 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1315 dbus_error_free (&error);
1316 _dbus_wait_for_memory ();
1317 goto retry_get_property;
1321 _dbus_assert (dbus_error_is_set (&error));
1322 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1328 /* test if returned pid is the same as our own pid
1330 * @todo It would probably be good to restructure the tests
1331 * in a way so our parent is the bus that we're testing
1332 * cause then we can test that the pid returned matches
1335 if (pid != (dbus_uint32_t) _dbus_getpid ())
1337 _dbus_assert (dbus_error_is_set (&error));
1338 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1342 #endif /* !DBUS_WIN */
1345 if (!check_no_leftovers (context))
1351 dbus_error_free (&error);
1354 dbus_message_unref (message);
1359 /* returns TRUE if the correct thing happens,
1360 * but the correct thing may include OOM errors.
1363 check_add_match_all (BusContext *context,
1364 DBusConnection *connection)
1366 DBusMessage *message;
1368 dbus_uint32_t serial;
1370 const char *empty = "";
1373 dbus_error_init (&error);
1376 _dbus_verbose ("check_add_match_all for %p\n", connection);
1378 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1380 DBUS_INTERFACE_DBUS,
1383 if (message == NULL)
1386 /* empty string match rule matches everything */
1387 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1390 dbus_message_unref (message);
1394 if (!dbus_connection_send (connection, message, &serial))
1396 dbus_message_unref (message);
1400 dbus_message_unref (message);
1403 dbus_connection_ref (connection); /* because we may get disconnected */
1405 /* send our message */
1406 bus_test_run_clients_loop (SEND_PENDING (connection));
1408 if (!dbus_connection_get_is_connected (connection))
1410 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1412 dbus_connection_unref (connection);
1417 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1419 if (!dbus_connection_get_is_connected (connection))
1421 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1423 dbus_connection_unref (connection);
1428 dbus_connection_unref (connection);
1430 message = pop_message_waiting_for_memory (connection);
1431 if (message == NULL)
1433 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1434 "AddMatch", serial, connection);
1438 verbose_message_received (connection, message);
1440 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1442 _dbus_warn ("Message has wrong sender %s\n",
1443 dbus_message_get_sender (message) ?
1444 dbus_message_get_sender (message) : "(none)");
1448 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1450 if (dbus_message_is_error (message,
1451 DBUS_ERROR_NO_MEMORY))
1453 ; /* good, this is a valid response */
1457 warn_unexpected (connection, message, "not this error");
1464 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1466 ; /* good, expected */
1467 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1471 warn_unexpected (connection, message, "method return for AddMatch");
1477 if (!check_no_leftovers (context))
1483 dbus_error_free (&error);
1486 dbus_message_unref (message);
1491 /* returns TRUE if the correct thing happens,
1492 * but the correct thing may include OOM errors.
1495 check_hello_connection (BusContext *context)
1497 DBusConnection *connection;
1500 dbus_error_init (&error);
1502 connection = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
1503 if (connection == NULL)
1505 _DBUS_ASSERT_ERROR_IS_SET (&error);
1506 dbus_error_free (&error);
1510 if (!bus_setup_debug_client (connection))
1512 dbus_connection_close (connection);
1513 dbus_connection_unref (connection);
1517 spin_connection_until_authenticated (context, connection);
1519 if (!check_hello_message (context, connection))
1522 if (dbus_bus_get_unique_name (connection) == NULL)
1524 /* We didn't successfully register, so we can't
1525 * do the usual kill_client_connection() checks
1527 kill_client_connection_unchecked (connection);
1531 if (!check_add_match_all (context, connection))
1534 kill_client_connection (context, connection);
1540 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1542 /* returns TRUE if the correct thing happens,
1543 * but the correct thing may include OOM errors.
1546 check_nonexistent_service_no_auto_start (BusContext *context,
1547 DBusConnection *connection)
1549 DBusMessage *message;
1550 dbus_uint32_t serial;
1552 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1553 dbus_uint32_t flags;
1555 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1557 DBUS_INTERFACE_DBUS,
1558 "StartServiceByName");
1560 if (message == NULL)
1563 dbus_message_set_auto_start (message, FALSE);
1566 if (!dbus_message_append_args (message,
1567 DBUS_TYPE_STRING, &nonexistent,
1568 DBUS_TYPE_UINT32, &flags,
1571 dbus_message_unref (message);
1575 if (!dbus_connection_send (connection, message, &serial))
1577 dbus_message_unref (message);
1581 dbus_message_unref (message);
1584 bus_test_run_everything (context);
1585 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1586 bus_test_run_everything (context);
1588 if (!dbus_connection_get_is_connected (connection))
1590 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1596 message = pop_message_waiting_for_memory (connection);
1597 if (message == NULL)
1599 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1600 "StartServiceByName", serial, connection);
1604 verbose_message_received (connection, message);
1606 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1608 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1610 _dbus_warn ("Message has wrong sender %s\n",
1611 dbus_message_get_sender (message) ?
1612 dbus_message_get_sender (message) : "(none)");
1616 if (dbus_message_is_error (message,
1617 DBUS_ERROR_NO_MEMORY))
1619 ; /* good, this is a valid response */
1621 else if (dbus_message_is_error (message,
1622 DBUS_ERROR_SERVICE_UNKNOWN))
1624 ; /* good, this is expected also */
1628 warn_unexpected (connection, message, "not this error");
1634 _dbus_warn ("Did not expect to successfully activate %s\n",
1635 NONEXISTENT_SERVICE_NAME);
1643 dbus_message_unref (message);
1648 /* returns TRUE if the correct thing happens,
1649 * but the correct thing may include OOM errors.
1652 check_nonexistent_service_auto_start (BusContext *context,
1653 DBusConnection *connection)
1655 DBusMessage *message;
1656 dbus_uint32_t serial;
1659 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1660 "/org/freedesktop/TestSuite",
1661 "org.freedesktop.TestSuite",
1664 if (message == NULL)
1667 if (!dbus_connection_send (connection, message, &serial))
1669 dbus_message_unref (message);
1673 dbus_message_unref (message);
1676 bus_test_run_everything (context);
1677 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1678 bus_test_run_everything (context);
1680 if (!dbus_connection_get_is_connected (connection))
1682 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1688 message = pop_message_waiting_for_memory (connection);
1690 if (message == NULL)
1692 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1693 "Echo message (auto activation)", serial, connection);
1697 verbose_message_received (connection, message);
1699 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1701 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1703 _dbus_warn ("Message has wrong sender %s\n",
1704 dbus_message_get_sender (message) ?
1705 dbus_message_get_sender (message) : "(none)");
1709 if (dbus_message_is_error (message,
1710 DBUS_ERROR_NO_MEMORY))
1712 ; /* good, this is a valid response */
1714 else if (dbus_message_is_error (message,
1715 DBUS_ERROR_SERVICE_UNKNOWN))
1717 ; /* good, this is expected also */
1721 warn_unexpected (connection, message, "not this error");
1727 _dbus_warn ("Did not expect to successfully activate %s\n",
1728 NONEXISTENT_SERVICE_NAME);
1736 dbus_message_unref (message);
1742 check_base_service_activated (BusContext *context,
1743 DBusConnection *connection,
1744 DBusMessage *initial_message,
1745 const char **base_service_p)
1747 DBusMessage *message;
1750 const char *base_service, *base_service_from_bus, *old_owner;
1754 dbus_error_init (&error);
1755 base_service = NULL;
1757 base_service_from_bus = NULL;
1759 message = initial_message;
1760 dbus_message_ref (message);
1762 if (dbus_message_is_signal (message,
1763 DBUS_INTERFACE_DBUS,
1764 "NameOwnerChanged"))
1766 CheckServiceOwnerChangedData socd;
1768 reget_service_name_arg:
1769 base_service = NULL;
1771 base_service_from_bus = NULL;
1773 if (!dbus_message_get_args (message, &error,
1774 DBUS_TYPE_STRING, &base_service,
1775 DBUS_TYPE_STRING, &old_owner,
1776 DBUS_TYPE_STRING, &base_service_from_bus,
1779 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1781 dbus_error_free (&error);
1782 _dbus_wait_for_memory ();
1783 goto reget_service_name_arg;
1787 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1788 "NameOwnerChanged (creation)",
1794 if (*base_service != ':')
1796 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1801 if (strcmp (base_service, base_service_from_bus) != 0)
1803 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1804 base_service, base_service_from_bus);
1810 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1815 socd.expected_kind = SERVICE_CREATED;
1816 socd.expected_service_name = base_service;
1817 socd.failed = FALSE;
1818 socd.skip_connection = connection;
1819 bus_test_clients_foreach (check_service_owner_changed_foreach,
1827 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1833 *base_service_p = base_service;
1839 dbus_message_unref (message);
1840 dbus_error_free (&error);
1846 check_service_activated (BusContext *context,
1847 DBusConnection *connection,
1848 const char *activated_name,
1849 const char *base_service_name,
1850 DBusMessage *initial_message)
1852 DBusMessage *message;
1855 dbus_uint32_t activation_result;
1859 dbus_error_init (&error);
1861 message = initial_message;
1862 dbus_message_ref (message);
1864 if (dbus_message_is_signal (message,
1865 DBUS_INTERFACE_DBUS,
1866 "NameOwnerChanged"))
1868 CheckServiceOwnerChangedData socd;
1869 const char *service_name, *base_service_from_bus, *old_owner;
1871 reget_service_name_arg:
1872 service_name = NULL;
1874 base_service_from_bus = NULL;
1876 if (!dbus_message_get_args (message, &error,
1877 DBUS_TYPE_STRING, &service_name,
1878 DBUS_TYPE_STRING, &old_owner,
1879 DBUS_TYPE_STRING, &base_service_from_bus,
1882 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1884 dbus_error_free (&error);
1885 _dbus_wait_for_memory ();
1886 goto reget_service_name_arg;
1890 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1891 "NameOwnerChanged (creation)",
1897 if (strcmp (service_name, activated_name) != 0)
1899 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1900 activated_name, service_name);
1904 if (strcmp (base_service_name, base_service_from_bus) != 0)
1906 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1907 base_service_from_bus, base_service_name);
1913 _dbus_warn ("expected a %s, got a %s\n",
1914 "NameOwnerChanged (creation)",
1915 "NameOwnerChanged (change)");
1919 socd.expected_kind = SERVICE_CREATED;
1920 socd.skip_connection = connection;
1921 socd.failed = FALSE;
1922 socd.expected_service_name = service_name;
1923 bus_test_clients_foreach (check_service_owner_changed_foreach,
1929 dbus_message_unref (message);
1930 service_name = NULL;
1932 base_service_from_bus = NULL;
1934 message = pop_message_waiting_for_memory (connection);
1935 if (message == NULL)
1937 _dbus_warn ("Expected a reply to %s, got nothing\n",
1938 "StartServiceByName");
1944 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1949 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1951 warn_unexpected (connection, message, "reply to StartServiceByName");
1956 activation_result = 0;
1957 if (!dbus_message_get_args (message, &error,
1958 DBUS_TYPE_UINT32, &activation_result,
1961 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1963 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1964 "StartServiceByName", error.message);
1968 dbus_error_free (&error);
1972 if (activation_result == DBUS_START_REPLY_SUCCESS)
1974 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1978 _dbus_warn ("Activation result was %u, no good.\n",
1984 dbus_message_unref (message);
1987 if (!check_no_leftovers (context))
1989 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1997 dbus_message_unref (message);
1998 dbus_error_free (&error);
2004 check_service_auto_activated (BusContext *context,
2005 DBusConnection *connection,
2006 const char *activated_name,
2007 const char *base_service_name,
2008 DBusMessage *initial_message)
2010 DBusMessage *message;
2016 dbus_error_init (&error);
2018 message = initial_message;
2019 dbus_message_ref (message);
2021 if (dbus_message_is_signal (message,
2022 DBUS_INTERFACE_DBUS,
2023 "NameOwnerChanged"))
2025 const char *service_name;
2026 CheckServiceOwnerChangedData socd;
2028 reget_service_name_arg:
2029 if (!dbus_message_get_args (message, &error,
2030 DBUS_TYPE_STRING, &service_name,
2033 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2035 dbus_error_free (&error);
2036 _dbus_wait_for_memory ();
2037 goto reget_service_name_arg;
2041 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2044 dbus_error_free (&error);
2049 if (strcmp (service_name, activated_name) != 0)
2051 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2052 activated_name, service_name);
2056 socd.expected_kind = SERVICE_CREATED;
2057 socd.expected_service_name = service_name;
2058 socd.failed = FALSE;
2059 socd.skip_connection = connection;
2060 bus_test_clients_foreach (check_service_owner_changed_foreach,
2066 /* Note that this differs from regular activation in that we don't get a
2067 * reply to ActivateService here.
2070 dbus_message_unref (message);
2072 service_name = NULL;
2076 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2085 dbus_message_unref (message);
2091 check_service_deactivated (BusContext *context,
2092 DBusConnection *connection,
2093 const char *activated_name,
2094 const char *base_service)
2097 CheckServiceOwnerChangedData socd;
2101 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2102 * service and the activated_name. The base service
2103 * notification is required to come last.
2105 socd.expected_kind = SERVICE_DELETED;
2106 socd.expected_service_name = activated_name;
2107 socd.failed = FALSE;
2108 socd.skip_connection = NULL;
2109 bus_test_clients_foreach (check_service_owner_changed_foreach,
2115 socd.expected_kind = SERVICE_DELETED;
2116 socd.expected_service_name = base_service;
2117 socd.failed = FALSE;
2118 socd.skip_connection = NULL;
2119 bus_test_clients_foreach (check_service_owner_changed_foreach,
2132 check_send_exit_to_service (BusContext *context,
2133 DBusConnection *connection,
2134 const char *service_name,
2135 const char *base_service)
2137 dbus_bool_t got_error;
2138 DBusMessage *message;
2139 dbus_uint32_t serial;
2142 _dbus_verbose ("Sending exit message to the test service\n");
2146 /* Kill off the test service by sending it a quit message */
2147 message = dbus_message_new_method_call (service_name,
2148 "/org/freedesktop/TestSuite",
2149 "org.freedesktop.TestSuite",
2152 if (message == NULL)
2154 /* Do this again; we still need the service to exit... */
2155 if (!check_send_exit_to_service (context, connection,
2156 service_name, base_service))
2162 if (!dbus_connection_send (connection, message, &serial))
2164 dbus_message_unref (message);
2166 /* Do this again; we still need the service to exit... */
2167 if (!check_send_exit_to_service (context, connection,
2168 service_name, base_service))
2174 dbus_message_unref (message);
2178 bus_test_run_clients_loop (SEND_PENDING (connection));
2180 /* read it in and write it out to test service */
2181 bus_test_run_bus_loop (context, FALSE);
2183 /* see if we got an error during message bus dispatching */
2184 bus_test_run_clients_loop (FALSE);
2185 message = borrow_message_waiting_for_memory (connection);
2186 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2189 dbus_connection_return_message (connection, message);
2195 /* If no error, wait for the test service to exit */
2196 block_connection_until_message_from_bus (context, connection, "test service to exit");
2198 bus_test_run_everything (context);
2203 message = pop_message_waiting_for_memory (connection);
2204 _dbus_assert (message != NULL);
2206 if (dbus_message_get_reply_serial (message) != serial)
2208 warn_unexpected (connection, message,
2209 "error with the correct reply serial");
2213 if (!dbus_message_is_error (message,
2214 DBUS_ERROR_NO_MEMORY))
2216 warn_unexpected (connection, message,
2217 "a no memory error from asking test service to exit");
2221 _dbus_verbose ("Got error %s when asking test service to exit\n",
2222 dbus_message_get_error_name (message));
2224 /* Do this again; we still need the service to exit... */
2225 if (!check_send_exit_to_service (context, connection,
2226 service_name, base_service))
2231 if (!check_service_deactivated (context, connection,
2232 service_name, base_service))
2235 /* Should now have a NoReply error from the Exit() method
2236 * call; it should have come after all the deactivation
2239 message = pop_message_waiting_for_memory (connection);
2241 if (message == NULL)
2243 warn_unexpected (connection, NULL,
2244 "reply to Exit() method call");
2247 if (!dbus_message_is_error (message,
2248 DBUS_ERROR_NO_REPLY))
2250 warn_unexpected (connection, message,
2251 "NoReply error from Exit() method call");
2255 if (dbus_message_get_reply_serial (message) != serial)
2257 warn_unexpected (connection, message,
2258 "error with the correct reply serial");
2262 _dbus_verbose ("Got error %s after test service exited\n",
2263 dbus_message_get_error_name (message));
2265 if (!check_no_leftovers (context))
2267 _dbus_warn ("Messages were left over after %s\n",
2268 _DBUS_FUNCTION_NAME);
2277 dbus_message_unref (message);
2283 check_got_error (BusContext *context,
2284 DBusConnection *connection,
2285 const char *first_error_name,
2288 DBusMessage *message;
2291 dbus_bool_t error_found;
2292 const char *error_name;
2296 message = pop_message_waiting_for_memory (connection);
2297 if (message == NULL)
2299 _dbus_warn ("Did not get an expected error\n");
2303 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2305 warn_unexpected (connection, message, "an error");
2310 error_found = FALSE;
2312 va_start (ap, first_error_name);
2313 error_name = first_error_name;
2314 while (error_name != NULL)
2316 if (dbus_message_is_error (message, error_name))
2321 error_name = va_arg (ap, char*);
2327 _dbus_warn ("Expected error %s or other, got %s instead\n",
2329 dbus_message_get_error_name (message));
2337 dbus_message_unref (message);
2344 GOT_SERVICE_CREATED,
2345 GOT_SERVICE_DELETED,
2350 static GotServiceInfo
2351 check_got_service_info (DBusMessage *message)
2353 GotServiceInfo message_kind;
2355 if (dbus_message_is_signal (message,
2356 DBUS_INTERFACE_DBUS,
2357 "NameOwnerChanged"))
2360 const char *service_name, *old_owner, *new_owner;
2361 dbus_error_init (&error);
2363 reget_service_info_data:
2364 service_name = NULL;
2368 dbus_message_get_args (message, &error,
2369 DBUS_TYPE_STRING, &service_name,
2370 DBUS_TYPE_STRING, &old_owner,
2371 DBUS_TYPE_STRING, &new_owner,
2373 if (dbus_error_is_set (&error))
2375 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2377 dbus_error_free (&error);
2378 goto reget_service_info_data;
2382 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2383 message_kind = GOT_SOMETHING_ELSE;
2386 else if (!old_owner[0])
2387 message_kind = GOT_SERVICE_CREATED;
2388 else if (!new_owner[0])
2389 message_kind = GOT_SERVICE_DELETED;
2391 message_kind = GOT_SOMETHING_ELSE;
2393 dbus_error_free (&error);
2395 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2396 message_kind = GOT_ERROR;
2398 message_kind = GOT_SOMETHING_ELSE;
2400 return message_kind;
2403 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2405 /* returns TRUE if the correct thing happens,
2406 * but the correct thing may include OOM errors.
2409 check_existent_service_no_auto_start (BusContext *context,
2410 DBusConnection *connection)
2412 DBusMessage *message;
2413 DBusMessage *base_service_message;
2414 const char *base_service;
2415 dbus_uint32_t serial;
2417 const char *existent = EXISTENT_SERVICE_NAME;
2418 dbus_uint32_t flags;
2420 base_service_message = NULL;
2422 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2424 DBUS_INTERFACE_DBUS,
2425 "StartServiceByName");
2427 if (message == NULL)
2430 dbus_message_set_auto_start (message, FALSE);
2433 if (!dbus_message_append_args (message,
2434 DBUS_TYPE_STRING, &existent,
2435 DBUS_TYPE_UINT32, &flags,
2438 dbus_message_unref (message);
2442 if (!dbus_connection_send (connection, message, &serial))
2444 dbus_message_unref (message);
2448 dbus_message_unref (message);
2451 bus_test_run_everything (context);
2453 /* now wait for the message bus to hear back from the activated
2456 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2458 bus_test_run_everything (context);
2460 if (!dbus_connection_get_is_connected (connection))
2462 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2468 message = pop_message_waiting_for_memory (connection);
2469 if (message == NULL)
2471 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2472 "StartServiceByName", serial, connection);
2476 verbose_message_received (connection, message);
2477 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2479 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2481 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2483 _dbus_warn ("Message has wrong sender %s\n",
2484 dbus_message_get_sender (message) ?
2485 dbus_message_get_sender (message) : "(none)");
2489 if (dbus_message_is_error (message,
2490 DBUS_ERROR_NO_MEMORY))
2492 ; /* good, this is a valid response */
2494 else if (dbus_message_is_error (message,
2495 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2496 dbus_message_is_error (message,
2497 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2498 dbus_message_is_error (message,
2499 DBUS_ERROR_SPAWN_EXEC_FAILED))
2501 ; /* good, this is expected also */
2505 _dbus_warn ("Did not expect error %s\n",
2506 dbus_message_get_error_name (message));
2512 GotServiceInfo message_kind;
2514 if (!check_base_service_activated (context, connection,
2515 message, &base_service))
2518 base_service_message = message;
2521 /* We may need to block here for the test service to exit or finish up */
2522 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2524 message = dbus_connection_borrow_message (connection);
2525 if (message == NULL)
2527 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2531 message_kind = check_got_service_info (message);
2533 dbus_connection_return_message (connection, message);
2536 switch (message_kind)
2538 case GOT_SOMETHING_ELSE:
2539 _dbus_warn ("Unexpected message after ActivateService "
2540 "(should be an error or a service announcement");
2544 if (!check_got_error (context, connection,
2545 DBUS_ERROR_SPAWN_CHILD_EXITED,
2546 DBUS_ERROR_NO_MEMORY,
2549 /* A service deleted should be coming along now after this error.
2550 * We can also get the error *after* the service deleted.
2555 case GOT_SERVICE_DELETED:
2557 /* The service started up and got a base address, but then
2558 * failed to register under EXISTENT_SERVICE_NAME
2560 CheckServiceOwnerChangedData socd;
2562 socd.expected_kind = SERVICE_DELETED;
2563 socd.expected_service_name = base_service;
2564 socd.failed = FALSE;
2565 socd.skip_connection = NULL;
2567 bus_test_clients_foreach (check_service_owner_changed_foreach,
2573 /* Now we should get an error about the service exiting
2574 * if we didn't get it before.
2576 if (message_kind != GOT_ERROR)
2578 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2580 /* and process everything again */
2581 bus_test_run_everything (context);
2583 if (!check_got_error (context, connection,
2584 DBUS_ERROR_SPAWN_CHILD_EXITED,
2585 DBUS_ERROR_NO_MEMORY,
2592 case GOT_SERVICE_CREATED:
2593 message = pop_message_waiting_for_memory (connection);
2594 if (message == NULL)
2596 _dbus_warn ("Failed to pop message we just put back! "
2597 "should have been a NameOwnerChanged (creation)\n");
2601 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2602 base_service, message))
2605 dbus_message_unref (message);
2608 if (!check_no_leftovers (context))
2610 _dbus_warn ("Messages were left over after successful activation\n");
2614 if (!check_send_exit_to_service (context, connection,
2615 EXISTENT_SERVICE_NAME, base_service))
2626 dbus_message_unref (message);
2628 if (base_service_message)
2629 dbus_message_unref (base_service_message);
2634 #ifndef DBUS_WIN_FIXME
2635 /* returns TRUE if the correct thing happens,
2636 * but the correct thing may include OOM errors.
2639 check_segfault_service_no_auto_start (BusContext *context,
2640 DBusConnection *connection)
2642 DBusMessage *message;
2643 dbus_uint32_t serial;
2645 const char *segv_service;
2646 dbus_uint32_t flags;
2648 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2650 DBUS_INTERFACE_DBUS,
2651 "StartServiceByName");
2653 if (message == NULL)
2656 dbus_message_set_auto_start (message, FALSE);
2658 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2660 if (!dbus_message_append_args (message,
2661 DBUS_TYPE_STRING, &segv_service,
2662 DBUS_TYPE_UINT32, &flags,
2665 dbus_message_unref (message);
2669 if (!dbus_connection_send (connection, message, &serial))
2671 dbus_message_unref (message);
2675 dbus_message_unref (message);
2678 bus_test_run_everything (context);
2679 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2680 bus_test_run_everything (context);
2682 if (!dbus_connection_get_is_connected (connection))
2684 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2690 message = pop_message_waiting_for_memory (connection);
2691 if (message == NULL)
2693 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2694 "StartServiceByName", serial, connection);
2698 verbose_message_received (connection, message);
2700 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2702 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2704 _dbus_warn ("Message has wrong sender %s\n",
2705 dbus_message_get_sender (message) ?
2706 dbus_message_get_sender (message) : "(none)");
2710 if (dbus_message_is_error (message,
2711 DBUS_ERROR_NO_MEMORY))
2713 ; /* good, this is a valid response */
2715 else if (dbus_message_is_error (message,
2718 const char *servicehelper;
2719 servicehelper = bus_context_get_servicehelper (context);
2720 /* make sure this only happens with the launch helper */
2721 _dbus_assert (servicehelper != NULL);
2723 else if (dbus_message_is_error (message,
2724 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2726 ; /* good, this is expected also */
2730 warn_unexpected (connection, message, "not this error");
2737 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2745 dbus_message_unref (message);
2751 /* returns TRUE if the correct thing happens,
2752 * but the correct thing may include OOM errors.
2755 check_segfault_service_auto_start (BusContext *context,
2756 DBusConnection *connection)
2758 DBusMessage *message;
2759 dbus_uint32_t serial;
2762 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2763 "/org/freedesktop/TestSuite",
2764 "org.freedesktop.TestSuite",
2767 if (message == NULL)
2770 if (!dbus_connection_send (connection, message, &serial))
2772 dbus_message_unref (message);
2776 dbus_message_unref (message);
2779 bus_test_run_everything (context);
2780 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2781 bus_test_run_everything (context);
2783 if (!dbus_connection_get_is_connected (connection))
2785 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2791 message = pop_message_waiting_for_memory (connection);
2792 if (message == NULL)
2794 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2795 "Echo message (auto activation)", serial, connection);
2799 verbose_message_received (connection, message);
2801 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2803 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2805 _dbus_warn ("Message has wrong sender %s\n",
2806 dbus_message_get_sender (message) ?
2807 dbus_message_get_sender (message) : "(none)");
2811 if (dbus_message_is_error (message,
2812 DBUS_ERROR_NO_MEMORY))
2814 ; /* good, this is a valid response */
2816 else if (dbus_message_is_error (message,
2817 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2819 ; /* good, this is expected also */
2823 warn_unexpected (connection, message, "not this error");
2830 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2838 dbus_message_unref (message);
2844 #define TEST_ECHO_MESSAGE "Test echo message"
2845 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2847 /* returns TRUE if the correct thing happens,
2848 * but the correct thing may include OOM errors.
2851 check_existent_hello_from_self (BusContext *context,
2852 DBusConnection *connection)
2854 DBusMessage *message;
2855 dbus_uint32_t serial;
2858 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2859 "/org/freedesktop/TestSuite",
2860 "org.freedesktop.TestSuite",
2861 "RunHelloFromSelf");
2863 if (message == NULL)
2866 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2867 if (!dbus_message_append_args (message,
2868 DBUS_TYPE_STRING, &text,
2871 dbus_message_unref (message);
2875 if (!dbus_connection_send (connection, message, &serial))
2877 dbus_message_unref (message);
2881 dbus_message_unref (message);
2884 bus_test_run_everything (context);
2886 /* Note: if this test is run in OOM mode, it will block when the bus
2887 * doesn't send a reply due to OOM.
2889 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2891 message = pop_message_waiting_for_memory (connection);
2892 if (message == NULL)
2894 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2898 if (dbus_message_get_reply_serial (message) != serial)
2900 _dbus_warn ("Wrong reply serial\n");
2901 dbus_message_unref (message);
2905 dbus_message_unref (message);
2911 /* returns TRUE if the correct thing happens,
2912 * but the correct thing may include OOM errors.
2915 check_existent_ping (BusContext *context,
2916 DBusConnection *connection)
2918 DBusMessage *message;
2919 dbus_uint32_t serial;
2920 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2921 "/org/freedesktop/TestSuite",
2922 "org.freedesktop.DBus.Peer",
2925 if (message == NULL)
2928 if (!dbus_connection_send (connection, message, &serial))
2930 dbus_message_unref (message);
2934 dbus_message_unref (message);
2937 bus_test_run_everything (context);
2939 /* Note: if this test is run in OOM mode, it will block when the bus
2940 * doesn't send a reply due to OOM.
2942 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2944 message = pop_message_waiting_for_memory (connection);
2945 if (message == NULL)
2947 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2951 if (dbus_message_get_reply_serial (message) != serial)
2953 _dbus_warn ("Wrong reply serial\n");
2954 dbus_message_unref (message);
2958 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2960 _dbus_warn ("Unexpected message return during Ping\n");
2961 dbus_message_unref (message);
2965 dbus_message_unref (message);
2971 /* returns TRUE if the correct thing happens,
2972 * but the correct thing may include OOM errors.
2975 check_existent_get_machine_id (BusContext *context,
2976 DBusConnection *connection)
2978 DBusMessage *message;
2979 dbus_uint32_t serial;
2980 const char *machine_id;
2982 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2983 "/org/freedesktop/TestSuite",
2984 "org.freedesktop.DBus.Peer",
2987 if (message == NULL)
2990 if (!dbus_connection_send (connection, message, &serial))
2992 dbus_message_unref (message);
2996 dbus_message_unref (message);
2999 bus_test_run_everything (context);
3001 /* Note: if this test is run in OOM mode, it will block when the bus
3002 * doesn't send a reply due to OOM.
3004 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3006 message = pop_message_waiting_for_memory (connection);
3007 if (message == NULL)
3009 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3013 if (dbus_message_get_reply_serial (message) != serial)
3015 _dbus_warn ("Wrong reply serial\n");
3016 dbus_message_unref (message);
3020 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3022 _dbus_warn ("Unexpected message return during GetMachineId\n");
3023 dbus_message_unref (message);
3028 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3030 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3031 dbus_message_unref (message);
3035 if (machine_id == NULL || strlen (machine_id) != 32)
3037 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3038 dbus_message_unref (message);
3042 /* We can't check that the machine id is correct because during make check it is
3043 * just made up for each process separately
3046 dbus_message_unref (message);
3052 /* returns TRUE if the correct thing happens,
3053 * but the correct thing may include OOM errors.
3056 check_existent_service_auto_start (BusContext *context,
3057 DBusConnection *connection)
3059 DBusMessage *message;
3060 DBusMessage *base_service_message;
3061 dbus_uint32_t serial;
3063 const char *base_service;
3066 base_service_message = NULL;
3068 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3069 "/org/freedesktop/TestSuite",
3070 "org.freedesktop.TestSuite",
3073 if (message == NULL)
3076 text = TEST_ECHO_MESSAGE;
3077 if (!dbus_message_append_args (message,
3078 DBUS_TYPE_STRING, &text,
3081 dbus_message_unref (message);
3085 if (!dbus_connection_send (connection, message, &serial))
3087 dbus_message_unref (message);
3091 dbus_message_unref (message);
3094 bus_test_run_everything (context);
3096 /* now wait for the message bus to hear back from the activated
3099 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3100 bus_test_run_everything (context);
3102 if (!dbus_connection_get_is_connected (connection))
3104 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3110 message = pop_message_waiting_for_memory (connection);
3111 if (message == NULL)
3113 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3114 serial, connection);
3118 verbose_message_received (connection, message);
3119 _dbus_verbose (" (after sending %s)\n", "auto start");
3121 /* we should get zero or two ServiceOwnerChanged signals */
3122 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3124 GotServiceInfo message_kind;
3126 if (!check_base_service_activated (context, connection,
3127 message, &base_service))
3130 base_service_message = message;
3133 /* We may need to block here for the test service to exit or finish up */
3134 block_connection_until_message_from_bus (context, connection, "service to exit");
3136 /* Should get a service creation notification for the activated
3137 * service name, or a service deletion on the base service name
3139 message = dbus_connection_borrow_message (connection);
3140 if (message == NULL)
3142 _dbus_warn ("No message after auto activation "
3143 "(should be a service announcement)\n");
3144 dbus_connection_return_message (connection, message);
3149 message_kind = check_got_service_info (message);
3151 dbus_connection_return_message (connection, message);
3154 switch (message_kind)
3156 case GOT_SERVICE_CREATED:
3157 message = pop_message_waiting_for_memory (connection);
3158 if (message == NULL)
3160 _dbus_warn ("Failed to pop message we just put back! "
3161 "should have been a NameOwnerChanged (creation)\n");
3165 /* Check that ServiceOwnerChanged (creation) was correctly received */
3166 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3167 base_service, message))
3170 dbus_message_unref (message);
3175 case GOT_SERVICE_DELETED:
3177 /* The service started up and got a base address, but then
3178 * failed to register under EXISTENT_SERVICE_NAME
3180 CheckServiceOwnerChangedData socd;
3182 socd.expected_kind = SERVICE_DELETED;
3183 socd.expected_service_name = base_service;
3184 socd.failed = FALSE;
3185 socd.skip_connection = NULL;
3186 bus_test_clients_foreach (check_service_owner_changed_foreach,
3196 case GOT_SOMETHING_ELSE:
3197 _dbus_warn ("Unexpected message after auto activation\n");
3202 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3203 * come the method reply (or error) from the initial method call
3206 /* Note: if this test is run in OOM mode, it will block when the bus
3207 * doesn't send a reply due to OOM.
3209 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3211 message = pop_message_waiting_for_memory (connection);
3212 if (message == NULL)
3214 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3218 if (dbus_message_get_reply_serial (message) != serial)
3220 _dbus_warn ("Wrong reply serial\n");
3224 dbus_message_unref (message);
3227 if (!check_existent_ping (context, connection))
3230 if (!check_existent_get_machine_id (context, connection))
3233 if (!check_existent_hello_from_self (context, connection))
3236 if (!check_send_exit_to_service (context, connection,
3237 EXISTENT_SERVICE_NAME,
3245 dbus_message_unref (message);
3247 if (base_service_message)
3248 dbus_message_unref (base_service_message);
3253 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3255 /* returns TRUE if the correct thing happens,
3256 * but the correct thing may include OOM errors.
3259 check_launch_service_file_missing (BusContext *context,
3260 DBusConnection *connection)
3262 DBusMessage *message;
3263 dbus_uint32_t serial;
3266 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3267 "/org/freedesktop/TestSuite",
3268 "org.freedesktop.TestSuite",
3271 if (message == NULL)
3274 if (!dbus_connection_send (connection, message, &serial))
3276 dbus_message_unref (message);
3280 dbus_message_unref (message);
3283 bus_test_run_everything (context);
3284 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3285 bus_test_run_everything (context);
3287 if (!dbus_connection_get_is_connected (connection))
3289 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3295 message = pop_message_waiting_for_memory (connection);
3296 if (message == NULL)
3298 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3299 "Echo message (auto activation)", serial, connection);
3303 verbose_message_received (connection, message);
3305 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3307 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3309 _dbus_warn ("Message has wrong sender %s\n",
3310 dbus_message_get_sender (message) ?
3311 dbus_message_get_sender (message) : "(none)");
3315 if (dbus_message_is_error (message,
3316 DBUS_ERROR_NO_MEMORY))
3318 ; /* good, this is a valid response */
3320 else if (dbus_message_is_error (message,
3321 DBUS_ERROR_SERVICE_UNKNOWN))
3323 _dbus_verbose("got service unknown\n");
3324 ; /* good, this is expected (only valid when using launch helper) */
3328 warn_unexpected (connection, message, "not this error");
3335 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3343 dbus_message_unref (message);
3348 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3350 /* returns TRUE if the correct thing happens,
3351 * but the correct thing may include OOM errors.
3354 check_launch_service_user_missing (BusContext *context,
3355 DBusConnection *connection)
3357 DBusMessage *message;
3358 dbus_uint32_t serial;
3361 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3362 "/org/freedesktop/TestSuite",
3363 "org.freedesktop.TestSuite",
3366 if (message == NULL)
3369 if (!dbus_connection_send (connection, message, &serial))
3371 dbus_message_unref (message);
3375 dbus_message_unref (message);
3378 bus_test_run_everything (context);
3379 block_connection_until_message_from_bus (context, connection,
3380 "reply to service which should fail to auto-start (missing User)");
3381 bus_test_run_everything (context);
3383 if (!dbus_connection_get_is_connected (connection))
3385 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3391 message = pop_message_waiting_for_memory (connection);
3392 if (message == NULL)
3394 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3395 "Echo message (auto activation)", serial, connection);
3399 verbose_message_received (connection, message);
3401 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3403 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3405 _dbus_warn ("Message has wrong sender %s\n",
3406 dbus_message_get_sender (message) ?
3407 dbus_message_get_sender (message) : "(none)");
3411 if (dbus_message_is_error (message,
3412 DBUS_ERROR_NO_MEMORY))
3414 ; /* good, this is a valid response */
3416 else if (dbus_message_is_error (message,
3417 DBUS_ERROR_SPAWN_FILE_INVALID))
3419 _dbus_verbose("got service file invalid\n");
3420 ; /* good, this is expected (only valid when using launch helper) */
3424 warn_unexpected (connection, message, "not this error");
3431 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3439 dbus_message_unref (message);
3444 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3446 /* returns TRUE if the correct thing happens,
3447 * but the correct thing may include OOM errors.
3450 check_launch_service_exec_missing (BusContext *context,
3451 DBusConnection *connection)
3453 DBusMessage *message;
3454 dbus_uint32_t serial;
3457 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3458 "/org/freedesktop/TestSuite",
3459 "org.freedesktop.TestSuite",
3462 if (message == NULL)
3465 if (!dbus_connection_send (connection, message, &serial))
3467 dbus_message_unref (message);
3471 dbus_message_unref (message);
3474 bus_test_run_everything (context);
3475 block_connection_until_message_from_bus (context, connection,
3476 "reply to service which should fail to auto-start (missing Exec)");
3477 bus_test_run_everything (context);
3479 if (!dbus_connection_get_is_connected (connection))
3481 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3487 message = pop_message_waiting_for_memory (connection);
3488 if (message == NULL)
3490 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3491 "Echo message (auto activation)", serial, connection);
3495 verbose_message_received (connection, message);
3497 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3499 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3501 _dbus_warn ("Message has wrong sender %s\n",
3502 dbus_message_get_sender (message) ?
3503 dbus_message_get_sender (message) : "(none)");
3507 if (dbus_message_is_error (message,
3508 DBUS_ERROR_NO_MEMORY))
3510 ; /* good, this is a valid response */
3512 else if (dbus_message_is_error (message,
3513 DBUS_ERROR_SERVICE_UNKNOWN))
3515 _dbus_verbose("could not activate as invalid service file was not added\n");
3516 ; /* good, this is expected as we shouldn't have been added to
3517 * the activation list with a missing Exec key */
3519 else if (dbus_message_is_error (message,
3520 DBUS_ERROR_SPAWN_FILE_INVALID))
3522 _dbus_verbose("got service file invalid\n");
3523 ; /* good, this is allowed, and is the message passed back from the
3528 warn_unexpected (connection, message, "not this error");
3535 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3543 dbus_message_unref (message);
3548 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3550 /* returns TRUE if the correct thing happens,
3551 * but the correct thing may include OOM errors.
3554 check_launch_service_service_missing (BusContext *context,
3555 DBusConnection *connection)
3557 DBusMessage *message;
3558 dbus_uint32_t serial;
3561 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3562 "/org/freedesktop/TestSuite",
3563 "org.freedesktop.TestSuite",
3566 if (message == NULL)
3569 if (!dbus_connection_send (connection, message, &serial))
3571 dbus_message_unref (message);
3575 dbus_message_unref (message);
3578 bus_test_run_everything (context);
3579 block_connection_until_message_from_bus (context, connection,
3580 "reply to service which should fail to auto-start (missing Service)");
3581 bus_test_run_everything (context);
3583 if (!dbus_connection_get_is_connected (connection))
3585 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3591 message = pop_message_waiting_for_memory (connection);
3592 if (message == NULL)
3594 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3595 "Echo message (auto activation)", serial, connection);
3599 verbose_message_received (connection, message);
3601 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3603 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3605 _dbus_warn ("Message has wrong sender %s\n",
3606 dbus_message_get_sender (message) ?
3607 dbus_message_get_sender (message) : "(none)");
3611 if (dbus_message_is_error (message,
3612 DBUS_ERROR_NO_MEMORY))
3614 ; /* good, this is a valid response */
3616 else if (dbus_message_is_error (message,
3617 DBUS_ERROR_SERVICE_UNKNOWN))
3619 _dbus_verbose("could not activate as invalid service file was not added\n");
3620 ; /* good, this is expected as we shouldn't have been added to
3621 * the activation list with a missing Exec key */
3623 else if (dbus_message_is_error (message,
3624 DBUS_ERROR_SPAWN_FILE_INVALID))
3626 _dbus_verbose("got service file invalid\n");
3627 ; /* good, this is allowed, and is the message passed back from the
3632 warn_unexpected (connection, message, "not this error");
3639 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3647 dbus_message_unref (message);
3652 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3654 /* returns TRUE if the correct thing happens,
3655 * but the correct thing may include OOM errors.
3658 check_shell_fail_service_auto_start (BusContext *context,
3659 DBusConnection *connection)
3661 DBusMessage *message;
3662 dbus_uint32_t serial;
3665 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3666 "/org/freedesktop/TestSuite",
3667 "org.freedesktop.TestSuite",
3670 if (message == NULL)
3673 if (!dbus_connection_send (connection, message, &serial))
3675 dbus_message_unref (message);
3679 dbus_message_unref (message);
3682 bus_test_run_everything (context);
3683 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3684 bus_test_run_everything (context);
3686 if (!dbus_connection_get_is_connected (connection))
3688 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3694 message = pop_message_waiting_for_memory (connection);
3695 if (message == NULL)
3697 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3698 "Echo message (auto activation)", serial, connection);
3702 verbose_message_received (connection, message);
3704 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3706 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3708 _dbus_warn ("Message has wrong sender %s\n",
3709 dbus_message_get_sender (message) ?
3710 dbus_message_get_sender (message) : "(none)");
3714 if (dbus_message_is_error (message,
3715 DBUS_ERROR_NO_MEMORY))
3717 ; /* good, this is a valid response */
3719 else if (dbus_message_is_error (message,
3720 DBUS_ERROR_INVALID_ARGS))
3722 _dbus_verbose("got invalid args\n");
3723 ; /* good, this is expected also */
3727 warn_unexpected (connection, message, "not this error");
3734 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3742 dbus_message_unref (message);
3747 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3749 /* returns TRUE if the correct thing happens,
3750 * but the correct thing may include OOM errors.
3753 check_shell_service_success_auto_start (BusContext *context,
3754 DBusConnection *connection)
3756 DBusMessage *message;
3757 DBusMessage *base_service_message;
3758 dbus_uint32_t serial;
3760 const char *base_service;
3761 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3763 base_service_message = NULL;
3765 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3766 "/org/freedesktop/TestSuite",
3767 "org.freedesktop.TestSuite",
3770 if (message == NULL)
3773 if (!dbus_connection_send (connection, message, &serial))
3775 dbus_message_unref (message);
3779 dbus_message_unref (message);
3782 bus_test_run_everything (context);
3784 /* now wait for the message bus to hear back from the activated
3787 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3788 bus_test_run_everything (context);
3790 if (!dbus_connection_get_is_connected (connection))
3792 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3798 message = pop_message_waiting_for_memory (connection);
3799 if (message == NULL)
3801 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3802 serial, connection);
3806 verbose_message_received (connection, message);
3807 _dbus_verbose (" (after sending %s)\n", "auto start");
3809 /* we should get zero or two ServiceOwnerChanged signals */
3810 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3812 GotServiceInfo message_kind;
3814 if (!check_base_service_activated (context, connection,
3815 message, &base_service))
3818 base_service_message = message;
3821 /* We may need to block here for the test service to exit or finish up */
3822 block_connection_until_message_from_bus (context, connection, "service to exit");
3824 /* Should get a service creation notification for the activated
3825 * service name, or a service deletion on the base service name
3827 message = dbus_connection_borrow_message (connection);
3828 if (message == NULL)
3830 _dbus_warn ("No message after auto activation "
3831 "(should be a service announcement)\n");
3832 dbus_connection_return_message (connection, message);
3837 message_kind = check_got_service_info (message);
3839 dbus_connection_return_message (connection, message);
3842 switch (message_kind)
3844 case GOT_SERVICE_CREATED:
3845 message = pop_message_waiting_for_memory (connection);
3846 if (message == NULL)
3848 _dbus_warn ("Failed to pop message we just put back! "
3849 "should have been a NameOwnerChanged (creation)\n");
3853 /* Check that ServiceOwnerChanged (creation) was correctly received */
3854 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3855 base_service, message))
3858 dbus_message_unref (message);
3863 case GOT_SERVICE_DELETED:
3865 /* The service started up and got a base address, but then
3866 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3868 CheckServiceOwnerChangedData socd;
3870 socd.expected_kind = SERVICE_DELETED;
3871 socd.expected_service_name = base_service;
3872 socd.failed = FALSE;
3873 socd.skip_connection = NULL;
3874 bus_test_clients_foreach (check_service_owner_changed_foreach,
3884 case GOT_SOMETHING_ELSE:
3885 _dbus_warn ("Unexpected message after auto activation\n");
3890 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3891 * come the method reply (or error) from the initial method call
3894 /* Note: if this test is run in OOM mode, it will block when the bus
3895 * doesn't send a reply due to OOM.
3897 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3899 message = pop_message_waiting_for_memory (connection);
3900 if (message == NULL)
3902 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3906 if (dbus_message_get_reply_serial (message) != serial)
3908 _dbus_warn ("Wrong reply serial\n");
3912 if (!dbus_message_get_args (message, NULL,
3913 DBUS_TYPE_STRING, &argv[0],
3914 DBUS_TYPE_STRING, &argv[1],
3915 DBUS_TYPE_STRING, &argv[2],
3916 DBUS_TYPE_STRING, &argv[3],
3917 DBUS_TYPE_STRING, &argv[4],
3918 DBUS_TYPE_STRING, &argv[5],
3919 DBUS_TYPE_STRING, &argv[6],
3922 _dbus_warn ("Error getting arguments from return\n");
3926 /* don't worry about arg[0] as it may be different
3927 depending on the path to the tests
3929 if (strcmp("-test", argv[1]) != 0)
3931 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3936 if (strcmp("that", argv[2]) != 0)
3938 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3943 if (strcmp("we get", argv[3]) != 0)
3945 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3950 if (strcmp("back", argv[4]) != 0)
3952 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3957 if (strcmp("--what", argv[5]) != 0)
3959 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3964 if (strcmp("we put in", argv[6]) != 0)
3966 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3967 "we put in", argv[6]);
3971 dbus_message_unref (message);
3974 if (!check_send_exit_to_service (context, connection,
3975 SHELL_SUCCESS_SERVICE_NAME,
3983 dbus_message_unref (message);
3985 if (base_service_message)
3986 dbus_message_unref (base_service_message);
3994 BusContext *context;
3998 check_oom_check1_func (void *data)
4000 Check1Data *d = data;
4002 if (! (* d->func) (d->context))
4005 if (!check_no_leftovers (d->context))
4007 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4015 check1_try_iterations (BusContext *context,
4016 const char *description,
4022 d.context = context;
4024 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4026 _dbus_assert_not_reached ("test failed");
4030 check_get_services (BusContext *context,
4031 DBusConnection *connection,
4036 DBusMessage *message;
4037 dbus_uint32_t serial;
4044 dbus_error_init (&error);
4047 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4049 DBUS_INTERFACE_DBUS,
4052 if (message == NULL)
4055 if (!dbus_connection_send (connection, message, &serial))
4057 dbus_message_unref (message);
4061 /* send our message */
4062 bus_test_run_clients_loop (SEND_PENDING (connection));
4064 dbus_message_unref (message);
4067 dbus_connection_ref (connection); /* because we may get disconnected */
4068 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4070 if (!dbus_connection_get_is_connected (connection))
4072 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4074 dbus_connection_unref (connection);
4079 dbus_connection_unref (connection);
4081 message = pop_message_waiting_for_memory (connection);
4082 if (message == NULL)
4084 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4085 method, serial, connection);
4089 verbose_message_received (connection, message);
4091 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4093 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4095 ; /* good, this is a valid response */
4099 warn_unexpected (connection, message, "not this error");
4106 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4108 ; /* good, expected */
4112 warn_unexpected (connection, message,
4113 "method_return for ListActivatableNames/ListNames");
4120 if (!dbus_message_get_args (message, &error,
4126 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4128 _dbus_verbose ("no memory to list services by %s\n", method);
4129 dbus_error_free (&error);
4130 _dbus_wait_for_memory ();
4131 goto retry_get_property;
4135 _dbus_assert (dbus_error_is_set (&error));
4136 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4145 if (!check_no_leftovers (context))
4151 dbus_error_free (&error);
4154 dbus_message_unref (message);
4159 /* returns TRUE if the correct thing happens,
4160 * but the correct thing may include OOM errors.
4163 check_list_services (BusContext *context,
4164 DBusConnection *connection)
4166 DBusMessage *message;
4167 DBusMessage *base_service_message;
4168 const char *base_service;
4169 dbus_uint32_t serial;
4171 const char *existent = EXISTENT_SERVICE_NAME;
4172 dbus_uint32_t flags;
4176 _dbus_verbose ("check_list_services for %p\n", connection);
4178 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4183 if (!_dbus_string_array_contains ((const char **)services, existent))
4185 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4189 dbus_free_string_array (services);
4191 base_service_message = NULL;
4193 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4195 DBUS_INTERFACE_DBUS,
4196 "StartServiceByName");
4198 if (message == NULL)
4201 dbus_message_set_auto_start (message, FALSE);
4204 if (!dbus_message_append_args (message,
4205 DBUS_TYPE_STRING, &existent,
4206 DBUS_TYPE_UINT32, &flags,
4209 dbus_message_unref (message);
4213 if (!dbus_connection_send (connection, message, &serial))
4215 dbus_message_unref (message);
4219 dbus_message_unref (message);
4222 bus_test_run_everything (context);
4224 /* now wait for the message bus to hear back from the activated
4227 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4229 bus_test_run_everything (context);
4231 if (!dbus_connection_get_is_connected (connection))
4233 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4239 message = pop_message_waiting_for_memory (connection);
4240 if (message == NULL)
4242 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4243 "StartServiceByName", serial, connection);
4247 verbose_message_received (connection, message);
4248 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4250 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4252 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4254 _dbus_warn ("Message has wrong sender %s\n",
4255 dbus_message_get_sender (message) ?
4256 dbus_message_get_sender (message) : "(none)");
4260 if (dbus_message_is_error (message,
4261 DBUS_ERROR_NO_MEMORY))
4263 ; /* good, this is a valid response */
4265 else if (dbus_message_is_error (message,
4266 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4267 dbus_message_is_error (message,
4268 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4269 dbus_message_is_error (message,
4270 DBUS_ERROR_SPAWN_EXEC_FAILED))
4272 ; /* good, this is expected also */
4276 _dbus_warn ("Did not expect error %s\n",
4277 dbus_message_get_error_name (message));
4283 GotServiceInfo message_kind;
4285 if (!check_base_service_activated (context, connection,
4286 message, &base_service))
4289 base_service_message = message;
4292 /* We may need to block here for the test service to exit or finish up */
4293 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4295 message = dbus_connection_borrow_message (connection);
4296 if (message == NULL)
4298 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4302 message_kind = check_got_service_info (message);
4304 dbus_connection_return_message (connection, message);
4307 switch (message_kind)
4309 case GOT_SOMETHING_ELSE:
4311 case GOT_SERVICE_DELETED:
4312 _dbus_warn ("Unexpected message after ActivateService "
4313 "(should be an error or a service announcement)\n");
4316 case GOT_SERVICE_CREATED:
4317 message = pop_message_waiting_for_memory (connection);
4318 if (message == NULL)
4320 _dbus_warn ("Failed to pop message we just put back! "
4321 "should have been a NameOwnerChanged (creation)\n");
4325 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4326 base_service, message))
4329 dbus_message_unref (message);
4332 if (!check_no_leftovers (context))
4334 _dbus_warn ("Messages were left over after successful activation\n");
4342 if (!check_get_services (context, connection, "ListNames", &services, &len))
4347 if (!_dbus_string_array_contains ((const char **)services, existent))
4349 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4353 dbus_free_string_array (services);
4355 if (!check_send_exit_to_service (context, connection,
4356 EXISTENT_SERVICE_NAME, base_service))
4363 dbus_message_unref (message);
4365 if (base_service_message)
4366 dbus_message_unref (base_service_message);
4374 BusContext *context;
4375 DBusConnection *connection;
4379 check_oom_check2_func (void *data)
4381 Check2Data *d = data;
4383 if (! (* d->func) (d->context, d->connection))
4386 if (!check_no_leftovers (d->context))
4388 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4396 check2_try_iterations (BusContext *context,
4397 DBusConnection *connection,
4398 const char *description,
4404 d.context = context;
4405 d.connection = connection;
4407 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4410 _dbus_warn ("%s failed during oom\n", description);
4411 _dbus_assert_not_reached ("test failed");
4416 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4417 const char *filename)
4422 if (!_dbus_string_init (&full))
4425 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4427 _dbus_string_free (&full);
4431 _dbus_string_init_const (&file, filename);
4433 if (!_dbus_concat_dir_and_file (&full, &file))
4435 _dbus_string_free (&full);
4439 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4440 _dbus_string_get_const_data (&full));
4442 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4444 _dbus_string_free (&full);
4450 bus_dispatch_test_conf (const DBusString *test_data_dir,
4451 const char *filename,
4452 dbus_bool_t use_launcher)
4454 BusContext *context;
4455 DBusConnection *foo;
4456 DBusConnection *bar;
4457 DBusConnection *baz;
4460 /* save the config name for the activation helper */
4461 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4462 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4464 dbus_error_init (&error);
4466 context = bus_context_new_test (test_data_dir, filename);
4467 if (context == NULL)
4470 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4472 _dbus_assert_not_reached ("could not alloc connection");
4474 if (!bus_setup_debug_client (foo))
4475 _dbus_assert_not_reached ("could not set up connection");
4477 spin_connection_until_authenticated (context, foo);
4479 if (!check_hello_message (context, foo))
4480 _dbus_assert_not_reached ("hello message failed");
4482 if (!check_double_hello_message (context, foo))
4483 _dbus_assert_not_reached ("double hello message failed");
4485 if (!check_add_match_all (context, foo))
4486 _dbus_assert_not_reached ("AddMatch message failed");
4488 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4490 _dbus_assert_not_reached ("could not alloc connection");
4492 if (!bus_setup_debug_client (bar))
4493 _dbus_assert_not_reached ("could not set up connection");
4495 spin_connection_until_authenticated (context, bar);
4497 if (!check_hello_message (context, bar))
4498 _dbus_assert_not_reached ("hello message failed");
4500 if (!check_add_match_all (context, bar))
4501 _dbus_assert_not_reached ("AddMatch message failed");
4503 baz = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4505 _dbus_assert_not_reached ("could not alloc connection");
4507 if (!bus_setup_debug_client (baz))
4508 _dbus_assert_not_reached ("could not set up connection");
4510 spin_connection_until_authenticated (context, baz);
4512 if (!check_hello_message (context, baz))
4513 _dbus_assert_not_reached ("hello message failed");
4515 if (!check_add_match_all (context, baz))
4516 _dbus_assert_not_reached ("AddMatch message failed");
4518 if (!check_get_connection_unix_user (context, baz))
4519 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4521 if (!check_get_connection_unix_process_id (context, baz))
4522 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4524 if (!check_list_services (context, baz))
4525 _dbus_assert_not_reached ("ListActivatableNames message failed");
4527 if (!check_no_leftovers (context))
4529 _dbus_warn ("Messages were left over after setting up initial connections\n");
4530 _dbus_assert_not_reached ("initial connection setup failed");
4533 check1_try_iterations (context, "create_and_hello",
4534 check_hello_connection);
4536 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4537 check_nonexistent_service_no_auto_start);
4539 #ifdef DBUS_WIN_FIXME
4540 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4542 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4543 check_segfault_service_no_auto_start);
4546 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4547 check_existent_service_no_auto_start);
4549 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4550 check_nonexistent_service_auto_start);
4553 #ifdef DBUS_WIN_FIXME
4554 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4556 /* only do the segfault test if we are not using the launcher */
4557 check2_try_iterations (context, foo, "segfault_service_auto_start",
4558 check_segfault_service_auto_start);
4561 /* only do the shell fail test if we are not using the launcher */
4562 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4563 check_shell_fail_service_auto_start);
4565 /* specific to launcher */
4567 if (!check_launch_service_file_missing (context, foo))
4568 _dbus_assert_not_reached ("did not get service file not found error");
4571 /* Note: need to resolve some issues with the testing code in order to run
4572 * this in oom (handle that we sometimes don't get replies back from the bus
4573 * when oom happens, without blocking the test).
4575 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4576 check_existent_service_auto_start);
4579 if (!check_existent_service_auto_start (context, foo))
4580 _dbus_assert_not_reached ("existent service auto start failed");
4582 if (!check_shell_service_success_auto_start (context, foo))
4583 _dbus_assert_not_reached ("shell success service auto start failed");
4585 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4587 kill_client_connection_unchecked (foo);
4588 kill_client_connection_unchecked (bar);
4589 kill_client_connection_unchecked (baz);
4591 bus_context_unref (context);
4597 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4598 const char *filename)
4600 BusContext *context;
4601 DBusConnection *foo;
4604 /* save the config name for the activation helper */
4605 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4606 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4608 dbus_error_init (&error);
4610 context = bus_context_new_test (test_data_dir, filename);
4611 if (context == NULL)
4614 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4616 _dbus_assert_not_reached ("could not alloc connection");
4618 if (!bus_setup_debug_client (foo))
4619 _dbus_assert_not_reached ("could not set up connection");
4621 spin_connection_until_authenticated (context, foo);
4623 if (!check_hello_message (context, foo))
4624 _dbus_assert_not_reached ("hello message failed");
4626 if (!check_double_hello_message (context, foo))
4627 _dbus_assert_not_reached ("double hello message failed");
4629 if (!check_add_match_all (context, foo))
4630 _dbus_assert_not_reached ("AddMatch message failed");
4632 /* this only tests the activation.c user check */
4633 if (!check_launch_service_user_missing (context, foo))
4634 _dbus_assert_not_reached ("user missing did not trigger error");
4636 /* this only tests the desktop.c exec check */
4637 if (!check_launch_service_exec_missing (context, foo))
4638 _dbus_assert_not_reached ("exec missing did not trigger error");
4640 /* this only tests the desktop.c service check */
4641 if (!check_launch_service_service_missing (context, foo))
4642 _dbus_assert_not_reached ("service missing did not trigger error");
4644 _dbus_verbose ("Disconnecting foo\n");
4646 kill_client_connection_unchecked (foo);
4648 bus_context_unref (context);
4654 bus_dispatch_test (const DBusString *test_data_dir)
4656 /* run normal activation tests */
4657 _dbus_verbose ("Normal activation tests\n");
4658 if (!bus_dispatch_test_conf (test_data_dir,
4659 "valid-config-files/debug-allow-all.conf", FALSE))
4662 /* run launch-helper activation tests */
4663 _dbus_verbose ("Launch helper activation tests\n");
4664 if (!bus_dispatch_test_conf (test_data_dir,
4665 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4668 /* run select launch-helper activation tests on broken service files */
4669 if (!bus_dispatch_test_conf_fail (test_data_dir,
4670 "valid-config-files-system/debug-allow-all-fail.conf"))
4677 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4679 BusContext *context;
4680 DBusConnection *foo;
4683 dbus_error_init (&error);
4685 /* Test SHA1 authentication */
4686 _dbus_verbose ("Testing SHA1 context\n");
4688 context = bus_context_new_test (test_data_dir,
4689 "valid-config-files/debug-allow-all-sha1.conf");
4690 if (context == NULL)
4693 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4695 _dbus_assert_not_reached ("could not alloc connection");
4697 if (!bus_setup_debug_client (foo))
4698 _dbus_assert_not_reached ("could not set up connection");
4700 spin_connection_until_authenticated (context, foo);
4702 if (!check_hello_message (context, foo))
4703 _dbus_assert_not_reached ("hello message failed");
4705 if (!check_add_match_all (context, foo))
4706 _dbus_assert_not_reached ("addmatch message failed");
4708 if (!check_no_leftovers (context))
4710 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4711 _dbus_assert_not_reached ("initial connection setup failed");
4714 check1_try_iterations (context, "create_and_hello_sha1",
4715 check_hello_connection);
4717 kill_client_connection_unchecked (foo);
4719 bus_context_unref (context);
4724 #ifdef HAVE_UNIX_FD_PASSING
4727 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4729 BusContext *context;
4730 DBusConnection *foo, *bar;
4734 int one[2], two[2], x, y, z;
4737 dbus_error_init (&error);
4739 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4740 if (context == NULL)
4741 _dbus_assert_not_reached ("could not alloc context");
4743 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4745 _dbus_assert_not_reached ("could not alloc connection");
4747 if (!bus_setup_debug_client (foo))
4748 _dbus_assert_not_reached ("could not set up connection");
4750 spin_connection_until_authenticated (context, foo);
4752 if (!check_hello_message (context, foo))
4753 _dbus_assert_not_reached ("hello message failed");
4755 if (!check_add_match_all (context, foo))
4756 _dbus_assert_not_reached ("AddMatch message failed");
4758 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4760 _dbus_assert_not_reached ("could not alloc connection");
4762 if (!bus_setup_debug_client (bar))
4763 _dbus_assert_not_reached ("could not set up connection");
4765 spin_connection_until_authenticated (context, bar);
4767 if (!check_hello_message (context, bar))
4768 _dbus_assert_not_reached ("hello message failed");
4770 if (!check_add_match_all (context, bar))
4771 _dbus_assert_not_reached ("AddMatch message failed");
4773 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4774 _dbus_assert_not_reached ("could not alloc message");
4776 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4777 _dbus_assert_not_reached("Failed to allocate pipe #1");
4779 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4780 _dbus_assert_not_reached("Failed to allocate pipe #2");
4782 if (!dbus_message_append_args(m,
4783 DBUS_TYPE_UNIX_FD, one,
4784 DBUS_TYPE_UNIX_FD, two,
4785 DBUS_TYPE_UNIX_FD, two,
4787 _dbus_assert_not_reached("Failed to attach fds.");
4789 if (!_dbus_close(one[0], &error))
4790 _dbus_assert_not_reached("Failed to close pipe #1 ");
4791 if (!_dbus_close(two[0], &error))
4792 _dbus_assert_not_reached("Failed to close pipe #2 ");
4795 if (!dbus_connection_send (foo, m, NULL))
4796 _dbus_assert_not_reached("Failed to send fds");
4798 dbus_message_unref(m);
4800 bus_test_run_clients_loop (SEND_PENDING (foo));
4802 bus_test_run_everything (context);
4804 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4806 if (!(m = pop_message_waiting_for_memory (foo)))
4807 _dbus_assert_not_reached("Failed to receive msg");
4809 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4810 _dbus_assert_not_reached("bogus message received");
4812 dbus_message_unref(m);
4814 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4816 if (!(m = pop_message_waiting_for_memory (bar)))
4817 _dbus_assert_not_reached("Failed to receive msg");
4819 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4820 _dbus_assert_not_reached("bogus message received");
4822 if (!dbus_message_get_args(m,
4824 DBUS_TYPE_UNIX_FD, &x,
4825 DBUS_TYPE_UNIX_FD, &y,
4826 DBUS_TYPE_UNIX_FD, &z,
4828 _dbus_assert_not_reached("Failed to parse fds.");
4830 dbus_message_unref(m);
4832 if (write(x, "X", 1) != 1)
4833 _dbus_assert_not_reached("Failed to write to pipe #1");
4834 if (write(y, "Y", 1) != 1)
4835 _dbus_assert_not_reached("Failed to write to pipe #2");
4836 if (write(z, "Z", 1) != 1)
4837 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4839 if (!_dbus_close(x, &error))
4840 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4841 if (!_dbus_close(y, &error))
4842 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4843 if (!_dbus_close(z, &error))
4844 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4846 if (read(one[1], &r, 1) != 1 || r != 'X')
4847 _dbus_assert_not_reached("Failed to read value from pipe.");
4848 if (read(two[1], &r, 1) != 1 || r != 'Y')
4849 _dbus_assert_not_reached("Failed to read value from pipe.");
4850 if (read(two[1], &r, 1) != 1 || r != 'Z')
4851 _dbus_assert_not_reached("Failed to read value from pipe.");
4853 if (!_dbus_close(one[1], &error))
4854 _dbus_assert_not_reached("Failed to close pipe #1 ");
4855 if (!_dbus_close(two[1], &error))
4856 _dbus_assert_not_reached("Failed to close pipe #2 ");
4858 _dbus_verbose ("Disconnecting foo\n");
4859 kill_client_connection_unchecked (foo);
4861 _dbus_verbose ("Disconnecting bar\n");
4862 kill_client_connection_unchecked (bar);
4864 bus_context_unref (context);
4870 #endif /* DBUS_BUILD_TESTS */