1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 #define TEST_CONNECTION "debug-pipe:name=test-server"
46 #define TEST_CONNECTION "tcp:host=localhost,port=1234"
50 send_one_message (DBusConnection *connection,
52 DBusConnection *sender,
53 DBusConnection *addressed_recipient,
55 BusTransaction *transaction,
58 if (!bus_context_check_security_policy (context, transaction,
64 return TRUE; /* silently don't send it */
66 if (dbus_message_contains_unix_fds(message) &&
67 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
68 return TRUE; /* silently don't send it */
70 if (!bus_transaction_send (transaction,
82 bus_dispatch_matches (BusTransaction *transaction,
83 DBusConnection *sender,
84 DBusConnection *addressed_recipient,
89 BusConnections *connections;
91 BusMatchmaker *matchmaker;
95 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
97 /* sender and recipient can both be NULL for the bus driver,
98 * or for signals with no particular recipient
101 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
102 _dbus_assert (dbus_message_get_sender (message) != NULL);
104 connections = bus_transaction_get_connections (transaction);
106 dbus_error_init (&tmp_error);
107 context = bus_transaction_get_context (transaction);
108 matchmaker = bus_context_get_matchmaker (context);
111 if (!bus_matchmaker_get_recipients (matchmaker, connections,
112 sender, addressed_recipient, message,
119 link = _dbus_list_get_first_link (&recipients);
122 DBusConnection *dest;
126 if (!send_one_message (dest, context, sender, addressed_recipient,
127 message, transaction, &tmp_error))
130 link = _dbus_list_get_next_link (&recipients, link);
133 _dbus_list_clear (&recipients);
135 if (dbus_error_is_set (&tmp_error))
137 dbus_move_error (&tmp_error, error);
144 static DBusHandlerResult
145 bus_dispatch (DBusConnection *connection,
146 DBusMessage *message)
148 const char *sender, *service_name;
150 BusTransaction *transaction;
152 DBusHandlerResult result;
153 DBusConnection *addressed_recipient;
155 result = DBUS_HANDLER_RESULT_HANDLED;
158 addressed_recipient = NULL;
159 dbus_error_init (&error);
161 context = bus_connection_get_context (connection);
162 _dbus_assert (context != NULL);
164 /* If we can't even allocate an OOM error, we just go to sleep
167 while (!bus_connection_preallocate_oom_error (connection))
168 _dbus_wait_for_memory ();
170 /* Ref connection in case we disconnect it at some point in here */
171 dbus_connection_ref (connection);
173 service_name = dbus_message_get_destination (message);
175 #ifdef DBUS_ENABLE_VERBOSE_MODE
177 const char *interface_name, *member_name, *error_name;
179 interface_name = dbus_message_get_interface (message);
180 member_name = dbus_message_get_member (message);
181 error_name = dbus_message_get_error_name (message);
183 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
184 interface_name ? interface_name : "(no interface)",
185 member_name ? member_name : "(no member)",
186 error_name ? error_name : "(no error name)",
187 service_name ? service_name : "peer");
189 #endif /* DBUS_ENABLE_VERBOSE_MODE */
191 /* If service_name is NULL, if it's a signal we send it to all
192 * connections with a match rule. If it's not a signal, there
193 * are some special cases here but mostly we just bail out.
195 if (service_name == NULL)
197 if (dbus_message_is_signal (message,
198 DBUS_INTERFACE_LOCAL,
201 bus_connection_disconnected (connection);
205 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
207 /* DBusConnection also handles some of these automatically, we leave
210 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
215 /* Create our transaction */
216 transaction = bus_transaction_new (context);
217 if (transaction == NULL)
219 BUS_SET_OOM (&error);
223 /* Assign a sender to the message */
224 if (bus_connection_is_active (connection))
226 sender = bus_connection_get_name (connection);
227 _dbus_assert (sender != NULL);
229 if (!dbus_message_set_sender (message, sender))
231 BUS_SET_OOM (&error);
235 /* We need to refetch the service name here, because
236 * dbus_message_set_sender can cause the header to be
237 * reallocated, and thus the service_name pointer will become
240 service_name = dbus_message_get_destination (message);
244 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
246 if (!bus_context_check_security_policy (context, transaction,
247 connection, NULL, NULL, message, &error))
249 _dbus_verbose ("Security policy rejected message\n");
253 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
254 if (!bus_driver_handle_message (connection, transaction, message, &error))
257 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
259 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
260 dbus_connection_close (connection);
263 else if (service_name != NULL) /* route to named service */
265 DBusString service_string;
267 BusRegistry *registry;
269 _dbus_assert (service_name != NULL);
271 registry = bus_connection_get_registry (connection);
273 _dbus_string_init_const (&service_string, service_name);
274 service = bus_registry_lookup (registry, &service_string);
276 if (service == NULL && dbus_message_get_auto_start (message))
278 BusActivation *activation;
279 /* We can't do the security policy check here, since the addressed
280 * recipient service doesn't exist yet. We do it before sending the
281 * message after the service has been created.
283 activation = bus_connection_get_activation (connection);
285 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
286 message, service_name, &error))
288 _DBUS_ASSERT_ERROR_IS_SET (&error);
289 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
295 else if (service == NULL)
297 dbus_set_error (&error,
298 DBUS_ERROR_NAME_HAS_NO_OWNER,
299 "Name \"%s\" does not exist",
305 addressed_recipient = bus_service_get_primary_owners_connection (service);
306 _dbus_assert (addressed_recipient != NULL);
308 if (!bus_context_check_security_policy (context, transaction,
309 connection, addressed_recipient,
314 if (dbus_message_contains_unix_fds(message) &&
315 !dbus_connection_can_send_type(addressed_recipient, DBUS_TYPE_UNIX_FD))
317 dbus_set_error(&error,
318 DBUS_ERROR_NOT_SUPPORTED,
319 "Tried to send message with Unix file descriptors"
320 "to a client that doesn't support that.");
324 /* Dispatch the message */
325 if (!bus_transaction_send (transaction, addressed_recipient, message))
327 BUS_SET_OOM (&error);
333 /* Now match the messages against any match rules, which will send
334 * out signals and such. addressed_recipient may == NULL.
336 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
340 if (dbus_error_is_set (&error))
342 if (!dbus_connection_get_is_connected (connection))
344 /* If we disconnected it, we won't bother to send it any error
347 _dbus_verbose ("Not sending error to connection we disconnected\n");
349 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
351 bus_connection_send_oom_error (connection, message);
353 /* cancel transaction due to OOM */
354 if (transaction != NULL)
356 bus_transaction_cancel_and_free (transaction);
362 /* Try to send the real error, if no mem to do that, send
365 _dbus_assert (transaction != NULL);
366 if (!bus_transaction_send_error_reply (transaction, connection,
369 bus_connection_send_oom_error (connection, message);
371 /* cancel transaction due to OOM */
372 if (transaction != NULL)
374 bus_transaction_cancel_and_free (transaction);
381 dbus_error_free (&error);
384 if (transaction != NULL)
386 bus_transaction_execute_and_free (transaction);
389 dbus_connection_unref (connection);
394 static DBusHandlerResult
395 bus_dispatch_message_filter (DBusConnection *connection,
396 DBusMessage *message,
399 return bus_dispatch (connection, message);
403 bus_dispatch_add_connection (DBusConnection *connection)
405 if (!dbus_connection_add_filter (connection,
406 bus_dispatch_message_filter,
414 bus_dispatch_remove_connection (DBusConnection *connection)
416 /* Here we tell the bus driver that we want to get off. */
417 bus_driver_remove_connection (connection);
419 dbus_connection_remove_filter (connection,
420 bus_dispatch_message_filter,
424 #ifdef DBUS_BUILD_TESTS
428 /* This is used to know whether we need to block in order to finish
429 * sending a message, or whether the initial dbus_connection_send()
430 * already flushed the queue.
432 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
434 typedef dbus_bool_t (* Check1Func) (BusContext *context);
435 typedef dbus_bool_t (* Check2Func) (BusContext *context,
436 DBusConnection *connection);
438 static dbus_bool_t check_no_leftovers (BusContext *context);
441 block_connection_until_message_from_bus (BusContext *context,
442 DBusConnection *connection,
443 const char *what_is_expected)
445 _dbus_verbose ("expecting: %s\n", what_is_expected);
447 while (dbus_connection_get_dispatch_status (connection) ==
448 DBUS_DISPATCH_COMPLETE &&
449 dbus_connection_get_is_connected (connection))
451 bus_test_run_bus_loop (context, TRUE);
452 bus_test_run_clients_loop (FALSE);
457 spin_connection_until_authenticated (BusContext *context,
458 DBusConnection *connection)
460 _dbus_verbose ("Spinning to auth connection %p\n", connection);
461 while (!dbus_connection_get_is_authenticated (connection) &&
462 dbus_connection_get_is_connected (connection))
464 bus_test_run_bus_loop (context, FALSE);
465 bus_test_run_clients_loop (FALSE);
467 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
470 /* compensate for fact that pop_message() can return #NULL due to OOM */
472 pop_message_waiting_for_memory (DBusConnection *connection)
474 while (dbus_connection_get_dispatch_status (connection) ==
475 DBUS_DISPATCH_NEED_MEMORY)
476 _dbus_wait_for_memory ();
478 return dbus_connection_pop_message (connection);
482 borrow_message_waiting_for_memory (DBusConnection *connection)
484 while (dbus_connection_get_dispatch_status (connection) ==
485 DBUS_DISPATCH_NEED_MEMORY)
486 _dbus_wait_for_memory ();
488 return dbus_connection_borrow_message (connection);
492 warn_unexpected_real (DBusConnection *connection,
493 DBusMessage *message,
494 const char *expected,
495 const char *function,
499 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
501 dbus_message_get_interface (message) ?
502 dbus_message_get_interface (message) : "(unset)",
503 dbus_message_get_member (message) ?
504 dbus_message_get_member (message) : "(unset)",
505 dbus_message_get_error_name (message) ?
506 dbus_message_get_error_name (message) : "(unset)",
510 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
511 function, line, connection, expected);
514 #define warn_unexpected(connection, message, expected) \
515 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
518 verbose_message_received (DBusConnection *connection,
519 DBusMessage *message)
521 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
522 dbus_message_get_interface (message) ?
523 dbus_message_get_interface (message) : "(unset)",
524 dbus_message_get_member (message) ?
525 dbus_message_get_member (message) : "(unset)",
526 dbus_message_get_error_name (message) ?
527 dbus_message_get_error_name (message) : "(unset)",
540 ServiceInfoKind expected_kind;
541 const char *expected_service_name;
543 DBusConnection *skip_connection;
544 } CheckServiceOwnerChangedData;
547 check_service_owner_changed_foreach (DBusConnection *connection,
550 CheckServiceOwnerChangedData *d = data;
551 DBusMessage *message;
553 const char *service_name, *old_owner, *new_owner;
555 if (d->expected_kind == SERVICE_CREATED
556 && connection == d->skip_connection)
559 dbus_error_init (&error);
562 message = pop_message_waiting_for_memory (connection);
565 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
566 connection, "NameOwnerChanged");
569 else if (!dbus_message_is_signal (message,
573 warn_unexpected (connection, message, "NameOwnerChanged");
579 reget_service_info_data:
584 dbus_message_get_args (message, &error,
585 DBUS_TYPE_STRING, &service_name,
586 DBUS_TYPE_STRING, &old_owner,
587 DBUS_TYPE_STRING, &new_owner,
590 if (dbus_error_is_set (&error))
592 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
594 dbus_error_free (&error);
595 _dbus_wait_for_memory ();
596 goto reget_service_info_data;
600 _dbus_warn ("Did not get the expected arguments\n");
605 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
606 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
607 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
609 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
613 if (strcmp (service_name, d->expected_service_name) != 0)
615 _dbus_warn ("expected info on service %s, got info on %s\n",
616 d->expected_service_name,
621 if (*service_name == ':' && new_owner[0]
622 && strcmp (service_name, new_owner) != 0)
624 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
625 service_name, old_owner, new_owner);
633 dbus_error_free (&error);
636 dbus_message_unref (message);
643 kill_client_connection (BusContext *context,
644 DBusConnection *connection)
648 CheckServiceOwnerChangedData socd;
650 _dbus_verbose ("killing connection %p\n", connection);
652 s = dbus_bus_get_unique_name (connection);
653 _dbus_assert (s != NULL);
655 while ((base_service = _dbus_strdup (s)) == NULL)
656 _dbus_wait_for_memory ();
658 dbus_connection_ref (connection);
660 /* kick in the disconnect handler that unrefs the connection */
661 dbus_connection_close (connection);
663 bus_test_run_everything (context);
665 _dbus_assert (bus_test_client_listed (connection));
667 /* Run disconnect handler in test.c */
668 if (bus_connection_dispatch_one_message (connection))
669 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
671 _dbus_assert (!dbus_connection_get_is_connected (connection));
672 dbus_connection_unref (connection);
674 _dbus_assert (!bus_test_client_listed (connection));
676 socd.expected_kind = SERVICE_DELETED;
677 socd.expected_service_name = base_service;
679 socd.skip_connection = NULL;
681 bus_test_clients_foreach (check_service_owner_changed_foreach,
684 dbus_free (base_service);
687 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
689 if (!check_no_leftovers (context))
690 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
694 kill_client_connection_unchecked (DBusConnection *connection)
696 /* This kills the connection without expecting it to affect
697 * the rest of the bus.
699 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
701 dbus_connection_ref (connection);
702 dbus_connection_close (connection);
703 /* dispatching disconnect handler will unref once */
704 if (bus_connection_dispatch_one_message (connection))
705 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
707 _dbus_assert (!bus_test_client_listed (connection));
708 dbus_connection_unref (connection);
714 } CheckNoMessagesData;
717 check_no_messages_foreach (DBusConnection *connection,
720 CheckNoMessagesData *d = data;
721 DBusMessage *message;
723 message = pop_message_waiting_for_memory (connection);
726 warn_unexpected (connection, message, "no messages");
732 dbus_message_unref (message);
737 check_no_leftovers (BusContext *context)
739 CheckNoMessagesData nmd;
742 bus_test_clients_foreach (check_no_messages_foreach,
747 _dbus_verbose ("%s: leftover message found\n",
748 _DBUS_FUNCTION_NAME);
755 /* returns TRUE if the correct thing happens,
756 * but the correct thing may include OOM errors.
759 check_hello_message (BusContext *context,
760 DBusConnection *connection)
762 DBusMessage *message;
763 DBusMessage *name_message;
764 dbus_uint32_t serial;
768 const char *acquired;
771 dbus_error_init (&error);
777 _dbus_verbose ("check_hello_message for %p\n", connection);
779 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
787 dbus_connection_ref (connection); /* because we may get disconnected */
789 if (!dbus_connection_send (connection, message, &serial))
791 dbus_message_unref (message);
792 dbus_connection_unref (connection);
796 _dbus_assert (dbus_message_has_signature (message, ""));
798 dbus_message_unref (message);
801 if (!dbus_connection_get_is_connected (connection))
803 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
805 dbus_connection_unref (connection);
810 /* send our message */
811 bus_test_run_clients_loop (SEND_PENDING (connection));
813 if (!dbus_connection_get_is_connected (connection))
815 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
817 dbus_connection_unref (connection);
822 block_connection_until_message_from_bus (context, connection, "reply to Hello");
824 if (!dbus_connection_get_is_connected (connection))
826 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
828 dbus_connection_unref (connection);
833 dbus_connection_unref (connection);
835 message = pop_message_waiting_for_memory (connection);
838 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
839 "Hello", serial, connection);
843 verbose_message_received (connection, message);
845 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
847 _dbus_warn ("Message has wrong sender %s\n",
848 dbus_message_get_sender (message) ?
849 dbus_message_get_sender (message) : "(none)");
853 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
855 if (dbus_message_is_error (message,
856 DBUS_ERROR_NO_MEMORY))
858 ; /* good, this is a valid response */
862 warn_unexpected (connection, message, "not this error");
869 CheckServiceOwnerChangedData socd;
871 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
873 ; /* good, expected */
877 warn_unexpected (connection, message, "method return for Hello");
882 retry_get_hello_name:
883 if (!dbus_message_get_args (message, &error,
884 DBUS_TYPE_STRING, &name,
887 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
889 _dbus_verbose ("no memory to get service name arg from hello\n");
890 dbus_error_free (&error);
891 _dbus_wait_for_memory ();
892 goto retry_get_hello_name;
896 _dbus_assert (dbus_error_is_set (&error));
897 _dbus_warn ("Did not get the expected single string argument to hello\n");
902 _dbus_verbose ("Got hello name: %s\n", name);
904 while (!dbus_bus_set_unique_name (connection, name))
905 _dbus_wait_for_memory ();
907 socd.expected_kind = SERVICE_CREATED;
908 socd.expected_service_name = name;
910 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
911 bus_test_clients_foreach (check_service_owner_changed_foreach,
917 name_message = message;
918 /* Client should also have gotten ServiceAcquired */
920 message = pop_message_waiting_for_memory (connection);
923 _dbus_warn ("Expecting %s, got nothing\n",
927 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
930 _dbus_warn ("Expecting %s, got smthg else\n",
935 retry_get_acquired_name:
936 if (!dbus_message_get_args (message, &error,
937 DBUS_TYPE_STRING, &acquired,
940 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
942 _dbus_verbose ("no memory to get service name arg from acquired\n");
943 dbus_error_free (&error);
944 _dbus_wait_for_memory ();
945 goto retry_get_acquired_name;
949 _dbus_assert (dbus_error_is_set (&error));
950 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
955 _dbus_verbose ("Got acquired name: %s\n", acquired);
957 if (strcmp (acquired, name) != 0)
959 _dbus_warn ("Acquired name is %s but expected %s\n",
966 if (!check_no_leftovers (context))
972 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
974 dbus_error_free (&error);
977 dbus_message_unref (message);
980 dbus_message_unref (name_message);
985 /* returns TRUE if the correct thing happens,
986 * but the correct thing may include OOM errors.
989 check_double_hello_message (BusContext *context,
990 DBusConnection *connection)
992 DBusMessage *message;
993 dbus_uint32_t serial;
998 dbus_error_init (&error);
1001 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1003 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1005 DBUS_INTERFACE_DBUS,
1008 if (message == NULL)
1011 if (!dbus_connection_send (connection, message, &serial))
1013 dbus_message_unref (message);
1017 dbus_message_unref (message);
1020 /* send our message */
1021 bus_test_run_clients_loop (SEND_PENDING (connection));
1023 dbus_connection_ref (connection); /* because we may get disconnected */
1024 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1026 if (!dbus_connection_get_is_connected (connection))
1028 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1030 dbus_connection_unref (connection);
1035 dbus_connection_unref (connection);
1037 message = pop_message_waiting_for_memory (connection);
1038 if (message == NULL)
1040 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1041 "Hello", serial, connection);
1045 verbose_message_received (connection, message);
1047 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1049 _dbus_warn ("Message has wrong sender %s\n",
1050 dbus_message_get_sender (message) ?
1051 dbus_message_get_sender (message) : "(none)");
1055 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1057 warn_unexpected (connection, message, "method return for Hello");
1061 if (!check_no_leftovers (context))
1067 dbus_error_free (&error);
1070 dbus_message_unref (message);
1075 /* returns TRUE if the correct thing happens,
1076 * but the correct thing may include OOM errors.
1079 check_get_connection_unix_user (BusContext *context,
1080 DBusConnection *connection)
1082 DBusMessage *message;
1083 dbus_uint32_t serial;
1086 const char *base_service_name;
1090 dbus_error_init (&error);
1093 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1095 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1097 DBUS_INTERFACE_DBUS,
1098 "GetConnectionUnixUser");
1100 if (message == NULL)
1103 base_service_name = dbus_bus_get_unique_name (connection);
1105 if (!dbus_message_append_args (message,
1106 DBUS_TYPE_STRING, &base_service_name,
1109 dbus_message_unref (message);
1113 if (!dbus_connection_send (connection, message, &serial))
1115 dbus_message_unref (message);
1119 /* send our message */
1120 bus_test_run_clients_loop (SEND_PENDING (connection));
1122 dbus_message_unref (message);
1125 dbus_connection_ref (connection); /* because we may get disconnected */
1126 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1128 if (!dbus_connection_get_is_connected (connection))
1130 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1132 dbus_connection_unref (connection);
1137 dbus_connection_unref (connection);
1139 message = pop_message_waiting_for_memory (connection);
1140 if (message == NULL)
1142 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1143 "GetConnectionUnixUser", serial, connection);
1147 verbose_message_received (connection, message);
1149 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1151 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1153 ; /* good, this is a valid response */
1157 warn_unexpected (connection, message, "not this error");
1164 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1166 ; /* good, expected */
1170 warn_unexpected (connection, message,
1171 "method_return for GetConnectionUnixUser");
1178 if (!dbus_message_get_args (message, &error,
1179 DBUS_TYPE_UINT32, &uid,
1182 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1184 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1185 dbus_error_free (&error);
1186 _dbus_wait_for_memory ();
1187 goto retry_get_property;
1191 _dbus_assert (dbus_error_is_set (&error));
1192 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1198 if (!check_no_leftovers (context))
1204 dbus_error_free (&error);
1207 dbus_message_unref (message);
1212 /* returns TRUE if the correct thing happens,
1213 * but the correct thing may include OOM errors.
1216 check_get_connection_unix_process_id (BusContext *context,
1217 DBusConnection *connection)
1219 DBusMessage *message;
1220 dbus_uint32_t serial;
1223 const char *base_service_name;
1227 dbus_error_init (&error);
1230 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1232 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1234 DBUS_INTERFACE_DBUS,
1235 "GetConnectionUnixProcessID");
1237 if (message == NULL)
1240 base_service_name = dbus_bus_get_unique_name (connection);
1242 if (!dbus_message_append_args (message,
1243 DBUS_TYPE_STRING, &base_service_name,
1246 dbus_message_unref (message);
1250 if (!dbus_connection_send (connection, message, &serial))
1252 dbus_message_unref (message);
1256 /* send our message */
1257 bus_test_run_clients_loop (SEND_PENDING (connection));
1259 dbus_message_unref (message);
1262 dbus_connection_ref (connection); /* because we may get disconnected */
1263 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1265 if (!dbus_connection_get_is_connected (connection))
1267 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1269 dbus_connection_unref (connection);
1274 dbus_connection_unref (connection);
1276 message = pop_message_waiting_for_memory (connection);
1277 if (message == NULL)
1279 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1280 "GetConnectionUnixProcessID", serial, connection);
1284 verbose_message_received (connection, message);
1286 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1288 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1290 ; /* good, this is a valid response */
1293 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1295 /* We are expecting this error, since we know in the test suite we aren't
1296 * talking to a client running on UNIX
1298 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1303 warn_unexpected (connection, message, "not this error");
1311 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1314 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1316 ; /* good, expected */
1320 warn_unexpected (connection, message,
1321 "method_return for GetConnectionUnixProcessID");
1328 if (!dbus_message_get_args (message, &error,
1329 DBUS_TYPE_UINT32, &pid,
1332 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1334 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1335 dbus_error_free (&error);
1336 _dbus_wait_for_memory ();
1337 goto retry_get_property;
1341 _dbus_assert (dbus_error_is_set (&error));
1342 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1348 /* test if returned pid is the same as our own pid
1350 * @todo It would probably be good to restructure the tests
1351 * in a way so our parent is the bus that we're testing
1352 * cause then we can test that the pid returned matches
1355 if (pid != (dbus_uint32_t) _dbus_getpid ())
1357 _dbus_assert (dbus_error_is_set (&error));
1358 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1362 #endif /* !DBUS_WIN */
1365 if (!check_no_leftovers (context))
1371 dbus_error_free (&error);
1374 dbus_message_unref (message);
1379 /* returns TRUE if the correct thing happens,
1380 * but the correct thing may include OOM errors.
1383 check_add_match_all (BusContext *context,
1384 DBusConnection *connection)
1386 DBusMessage *message;
1388 dbus_uint32_t serial;
1390 const char *empty = "";
1393 dbus_error_init (&error);
1396 _dbus_verbose ("check_add_match_all for %p\n", connection);
1398 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1400 DBUS_INTERFACE_DBUS,
1403 if (message == NULL)
1406 /* empty string match rule matches everything */
1407 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1410 dbus_message_unref (message);
1414 if (!dbus_connection_send (connection, message, &serial))
1416 dbus_message_unref (message);
1420 dbus_message_unref (message);
1423 dbus_connection_ref (connection); /* because we may get disconnected */
1425 /* send our message */
1426 bus_test_run_clients_loop (SEND_PENDING (connection));
1428 if (!dbus_connection_get_is_connected (connection))
1430 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1432 dbus_connection_unref (connection);
1437 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1439 if (!dbus_connection_get_is_connected (connection))
1441 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1443 dbus_connection_unref (connection);
1448 dbus_connection_unref (connection);
1450 message = pop_message_waiting_for_memory (connection);
1451 if (message == NULL)
1453 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1454 "AddMatch", serial, connection);
1458 verbose_message_received (connection, message);
1460 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1462 _dbus_warn ("Message has wrong sender %s\n",
1463 dbus_message_get_sender (message) ?
1464 dbus_message_get_sender (message) : "(none)");
1468 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1470 if (dbus_message_is_error (message,
1471 DBUS_ERROR_NO_MEMORY))
1473 ; /* good, this is a valid response */
1477 warn_unexpected (connection, message, "not this error");
1484 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1486 ; /* good, expected */
1487 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1491 warn_unexpected (connection, message, "method return for AddMatch");
1497 if (!check_no_leftovers (context))
1503 dbus_error_free (&error);
1506 dbus_message_unref (message);
1511 /* returns TRUE if the correct thing happens,
1512 * but the correct thing may include OOM errors.
1515 check_hello_connection (BusContext *context)
1517 DBusConnection *connection;
1520 dbus_error_init (&error);
1522 connection = dbus_connection_open_private (TEST_CONNECTION, &error);
1523 if (connection == NULL)
1525 _DBUS_ASSERT_ERROR_IS_SET (&error);
1526 dbus_error_free (&error);
1530 if (!bus_setup_debug_client (connection))
1532 dbus_connection_close (connection);
1533 dbus_connection_unref (connection);
1537 spin_connection_until_authenticated (context, connection);
1539 if (!check_hello_message (context, connection))
1542 if (dbus_bus_get_unique_name (connection) == NULL)
1544 /* We didn't successfully register, so we can't
1545 * do the usual kill_client_connection() checks
1547 kill_client_connection_unchecked (connection);
1551 if (!check_add_match_all (context, connection))
1554 kill_client_connection (context, connection);
1560 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1562 /* returns TRUE if the correct thing happens,
1563 * but the correct thing may include OOM errors.
1566 check_nonexistent_service_no_auto_start (BusContext *context,
1567 DBusConnection *connection)
1569 DBusMessage *message;
1570 dbus_uint32_t serial;
1572 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1573 dbus_uint32_t flags;
1575 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1577 DBUS_INTERFACE_DBUS,
1578 "StartServiceByName");
1580 if (message == NULL)
1583 dbus_message_set_auto_start (message, FALSE);
1586 if (!dbus_message_append_args (message,
1587 DBUS_TYPE_STRING, &nonexistent,
1588 DBUS_TYPE_UINT32, &flags,
1591 dbus_message_unref (message);
1595 if (!dbus_connection_send (connection, message, &serial))
1597 dbus_message_unref (message);
1601 dbus_message_unref (message);
1604 bus_test_run_everything (context);
1605 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1606 bus_test_run_everything (context);
1608 if (!dbus_connection_get_is_connected (connection))
1610 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1616 message = pop_message_waiting_for_memory (connection);
1617 if (message == NULL)
1619 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1620 "StartServiceByName", serial, connection);
1624 verbose_message_received (connection, message);
1626 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1628 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1630 _dbus_warn ("Message has wrong sender %s\n",
1631 dbus_message_get_sender (message) ?
1632 dbus_message_get_sender (message) : "(none)");
1636 if (dbus_message_is_error (message,
1637 DBUS_ERROR_NO_MEMORY))
1639 ; /* good, this is a valid response */
1641 else if (dbus_message_is_error (message,
1642 DBUS_ERROR_SERVICE_UNKNOWN))
1644 ; /* good, this is expected also */
1648 warn_unexpected (connection, message, "not this error");
1654 _dbus_warn ("Did not expect to successfully activate %s\n",
1655 NONEXISTENT_SERVICE_NAME);
1663 dbus_message_unref (message);
1668 /* returns TRUE if the correct thing happens,
1669 * but the correct thing may include OOM errors.
1672 check_nonexistent_service_auto_start (BusContext *context,
1673 DBusConnection *connection)
1675 DBusMessage *message;
1676 dbus_uint32_t serial;
1679 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1680 "/org/freedesktop/TestSuite",
1681 "org.freedesktop.TestSuite",
1684 if (message == NULL)
1687 if (!dbus_connection_send (connection, message, &serial))
1689 dbus_message_unref (message);
1693 dbus_message_unref (message);
1696 bus_test_run_everything (context);
1697 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1698 bus_test_run_everything (context);
1700 if (!dbus_connection_get_is_connected (connection))
1702 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1708 message = pop_message_waiting_for_memory (connection);
1710 if (message == NULL)
1712 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1713 "Echo message (auto activation)", serial, connection);
1717 verbose_message_received (connection, message);
1719 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1721 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1723 _dbus_warn ("Message has wrong sender %s\n",
1724 dbus_message_get_sender (message) ?
1725 dbus_message_get_sender (message) : "(none)");
1729 if (dbus_message_is_error (message,
1730 DBUS_ERROR_NO_MEMORY))
1732 ; /* good, this is a valid response */
1734 else if (dbus_message_is_error (message,
1735 DBUS_ERROR_SERVICE_UNKNOWN))
1737 ; /* good, this is expected also */
1741 warn_unexpected (connection, message, "not this error");
1747 _dbus_warn ("Did not expect to successfully activate %s\n",
1748 NONEXISTENT_SERVICE_NAME);
1756 dbus_message_unref (message);
1762 check_base_service_activated (BusContext *context,
1763 DBusConnection *connection,
1764 DBusMessage *initial_message,
1765 const char **base_service_p)
1767 DBusMessage *message;
1770 const char *base_service, *base_service_from_bus, *old_owner;
1774 dbus_error_init (&error);
1775 base_service = NULL;
1777 base_service_from_bus = NULL;
1779 message = initial_message;
1780 dbus_message_ref (message);
1782 if (dbus_message_is_signal (message,
1783 DBUS_INTERFACE_DBUS,
1784 "NameOwnerChanged"))
1786 CheckServiceOwnerChangedData socd;
1788 reget_service_name_arg:
1789 base_service = NULL;
1791 base_service_from_bus = NULL;
1793 if (!dbus_message_get_args (message, &error,
1794 DBUS_TYPE_STRING, &base_service,
1795 DBUS_TYPE_STRING, &old_owner,
1796 DBUS_TYPE_STRING, &base_service_from_bus,
1799 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1801 dbus_error_free (&error);
1802 _dbus_wait_for_memory ();
1803 goto reget_service_name_arg;
1807 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1808 "NameOwnerChanged (creation)",
1814 if (*base_service != ':')
1816 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1821 if (strcmp (base_service, base_service_from_bus) != 0)
1823 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1824 base_service, base_service_from_bus);
1830 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1835 socd.expected_kind = SERVICE_CREATED;
1836 socd.expected_service_name = base_service;
1837 socd.failed = FALSE;
1838 socd.skip_connection = connection;
1839 bus_test_clients_foreach (check_service_owner_changed_foreach,
1847 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1853 *base_service_p = base_service;
1859 dbus_message_unref (message);
1860 dbus_error_free (&error);
1866 check_service_activated (BusContext *context,
1867 DBusConnection *connection,
1868 const char *activated_name,
1869 const char *base_service_name,
1870 DBusMessage *initial_message)
1872 DBusMessage *message;
1875 dbus_uint32_t activation_result;
1879 dbus_error_init (&error);
1881 message = initial_message;
1882 dbus_message_ref (message);
1884 if (dbus_message_is_signal (message,
1885 DBUS_INTERFACE_DBUS,
1886 "NameOwnerChanged"))
1888 CheckServiceOwnerChangedData socd;
1889 const char *service_name, *base_service_from_bus, *old_owner;
1891 reget_service_name_arg:
1892 service_name = NULL;
1894 base_service_from_bus = NULL;
1896 if (!dbus_message_get_args (message, &error,
1897 DBUS_TYPE_STRING, &service_name,
1898 DBUS_TYPE_STRING, &old_owner,
1899 DBUS_TYPE_STRING, &base_service_from_bus,
1902 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1904 dbus_error_free (&error);
1905 _dbus_wait_for_memory ();
1906 goto reget_service_name_arg;
1910 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1911 "NameOwnerChanged (creation)",
1917 if (strcmp (service_name, activated_name) != 0)
1919 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1920 activated_name, service_name);
1924 if (strcmp (base_service_name, base_service_from_bus) != 0)
1926 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1927 base_service_from_bus, base_service_name);
1933 _dbus_warn ("expected a %s, got a %s\n",
1934 "NameOwnerChanged (creation)",
1935 "NameOwnerChanged (change)");
1939 socd.expected_kind = SERVICE_CREATED;
1940 socd.skip_connection = connection;
1941 socd.failed = FALSE;
1942 socd.expected_service_name = service_name;
1943 bus_test_clients_foreach (check_service_owner_changed_foreach,
1949 dbus_message_unref (message);
1950 service_name = NULL;
1952 base_service_from_bus = NULL;
1954 message = pop_message_waiting_for_memory (connection);
1955 if (message == NULL)
1957 _dbus_warn ("Expected a reply to %s, got nothing\n",
1958 "StartServiceByName");
1964 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1969 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1971 warn_unexpected (connection, message, "reply to StartServiceByName");
1976 activation_result = 0;
1977 if (!dbus_message_get_args (message, &error,
1978 DBUS_TYPE_UINT32, &activation_result,
1981 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1983 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1984 "StartServiceByName", error.message);
1988 dbus_error_free (&error);
1992 if (activation_result == DBUS_START_REPLY_SUCCESS)
1994 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1998 _dbus_warn ("Activation result was %u, no good.\n",
2004 dbus_message_unref (message);
2007 if (!check_no_leftovers (context))
2009 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2017 dbus_message_unref (message);
2018 dbus_error_free (&error);
2024 check_service_auto_activated (BusContext *context,
2025 DBusConnection *connection,
2026 const char *activated_name,
2027 const char *base_service_name,
2028 DBusMessage *initial_message)
2030 DBusMessage *message;
2036 dbus_error_init (&error);
2038 message = initial_message;
2039 dbus_message_ref (message);
2041 if (dbus_message_is_signal (message,
2042 DBUS_INTERFACE_DBUS,
2043 "NameOwnerChanged"))
2045 const char *service_name;
2046 CheckServiceOwnerChangedData socd;
2048 reget_service_name_arg:
2049 if (!dbus_message_get_args (message, &error,
2050 DBUS_TYPE_STRING, &service_name,
2053 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2055 dbus_error_free (&error);
2056 _dbus_wait_for_memory ();
2057 goto reget_service_name_arg;
2061 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2064 dbus_error_free (&error);
2069 if (strcmp (service_name, activated_name) != 0)
2071 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2072 activated_name, service_name);
2076 socd.expected_kind = SERVICE_CREATED;
2077 socd.expected_service_name = service_name;
2078 socd.failed = FALSE;
2079 socd.skip_connection = connection;
2080 bus_test_clients_foreach (check_service_owner_changed_foreach,
2086 /* Note that this differs from regular activation in that we don't get a
2087 * reply to ActivateService here.
2090 dbus_message_unref (message);
2092 service_name = NULL;
2096 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2105 dbus_message_unref (message);
2111 check_service_deactivated (BusContext *context,
2112 DBusConnection *connection,
2113 const char *activated_name,
2114 const char *base_service)
2117 CheckServiceOwnerChangedData socd;
2121 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2122 * service and the activated_name. The base service
2123 * notification is required to come last.
2125 socd.expected_kind = SERVICE_DELETED;
2126 socd.expected_service_name = activated_name;
2127 socd.failed = FALSE;
2128 socd.skip_connection = NULL;
2129 bus_test_clients_foreach (check_service_owner_changed_foreach,
2135 socd.expected_kind = SERVICE_DELETED;
2136 socd.expected_service_name = base_service;
2137 socd.failed = FALSE;
2138 socd.skip_connection = NULL;
2139 bus_test_clients_foreach (check_service_owner_changed_foreach,
2152 check_send_exit_to_service (BusContext *context,
2153 DBusConnection *connection,
2154 const char *service_name,
2155 const char *base_service)
2157 dbus_bool_t got_error;
2158 DBusMessage *message;
2159 dbus_uint32_t serial;
2162 _dbus_verbose ("Sending exit message to the test service\n");
2166 /* Kill off the test service by sending it a quit message */
2167 message = dbus_message_new_method_call (service_name,
2168 "/org/freedesktop/TestSuite",
2169 "org.freedesktop.TestSuite",
2172 if (message == NULL)
2174 /* Do this again; we still need the service to exit... */
2175 if (!check_send_exit_to_service (context, connection,
2176 service_name, base_service))
2182 if (!dbus_connection_send (connection, message, &serial))
2184 dbus_message_unref (message);
2186 /* Do this again; we still need the service to exit... */
2187 if (!check_send_exit_to_service (context, connection,
2188 service_name, base_service))
2194 dbus_message_unref (message);
2198 bus_test_run_clients_loop (SEND_PENDING (connection));
2200 /* read it in and write it out to test service */
2201 bus_test_run_bus_loop (context, FALSE);
2203 /* see if we got an error during message bus dispatching */
2204 bus_test_run_clients_loop (FALSE);
2205 message = borrow_message_waiting_for_memory (connection);
2206 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2209 dbus_connection_return_message (connection, message);
2215 /* If no error, wait for the test service to exit */
2216 block_connection_until_message_from_bus (context, connection, "test service to exit");
2218 bus_test_run_everything (context);
2223 message = pop_message_waiting_for_memory (connection);
2224 _dbus_assert (message != NULL);
2226 if (dbus_message_get_reply_serial (message) != serial)
2228 warn_unexpected (connection, message,
2229 "error with the correct reply serial");
2233 if (!dbus_message_is_error (message,
2234 DBUS_ERROR_NO_MEMORY))
2236 warn_unexpected (connection, message,
2237 "a no memory error from asking test service to exit");
2241 _dbus_verbose ("Got error %s when asking test service to exit\n",
2242 dbus_message_get_error_name (message));
2244 /* Do this again; we still need the service to exit... */
2245 if (!check_send_exit_to_service (context, connection,
2246 service_name, base_service))
2251 if (!check_service_deactivated (context, connection,
2252 service_name, base_service))
2255 /* Should now have a NoReply error from the Exit() method
2256 * call; it should have come after all the deactivation
2259 message = pop_message_waiting_for_memory (connection);
2261 if (message == NULL)
2263 warn_unexpected (connection, NULL,
2264 "reply to Exit() method call");
2267 if (!dbus_message_is_error (message,
2268 DBUS_ERROR_NO_REPLY))
2270 warn_unexpected (connection, message,
2271 "NoReply error from Exit() method call");
2275 if (dbus_message_get_reply_serial (message) != serial)
2277 warn_unexpected (connection, message,
2278 "error with the correct reply serial");
2282 _dbus_verbose ("Got error %s after test service exited\n",
2283 dbus_message_get_error_name (message));
2285 if (!check_no_leftovers (context))
2287 _dbus_warn ("Messages were left over after %s\n",
2288 _DBUS_FUNCTION_NAME);
2297 dbus_message_unref (message);
2303 check_got_error (BusContext *context,
2304 DBusConnection *connection,
2305 const char *first_error_name,
2308 DBusMessage *message;
2311 dbus_bool_t error_found;
2312 const char *error_name;
2316 message = pop_message_waiting_for_memory (connection);
2317 if (message == NULL)
2319 _dbus_warn ("Did not get an expected error\n");
2323 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2325 warn_unexpected (connection, message, "an error");
2330 error_found = FALSE;
2332 va_start (ap, first_error_name);
2333 error_name = first_error_name;
2334 while (error_name != NULL)
2336 if (dbus_message_is_error (message, error_name))
2341 error_name = va_arg (ap, char*);
2347 _dbus_warn ("Expected error %s or other, got %s instead\n",
2349 dbus_message_get_error_name (message));
2357 dbus_message_unref (message);
2364 GOT_SERVICE_CREATED,
2365 GOT_SERVICE_DELETED,
2370 static GotServiceInfo
2371 check_got_service_info (DBusMessage *message)
2373 GotServiceInfo message_kind;
2375 if (dbus_message_is_signal (message,
2376 DBUS_INTERFACE_DBUS,
2377 "NameOwnerChanged"))
2380 const char *service_name, *old_owner, *new_owner;
2381 dbus_error_init (&error);
2383 reget_service_info_data:
2384 service_name = NULL;
2388 dbus_message_get_args (message, &error,
2389 DBUS_TYPE_STRING, &service_name,
2390 DBUS_TYPE_STRING, &old_owner,
2391 DBUS_TYPE_STRING, &new_owner,
2393 if (dbus_error_is_set (&error))
2395 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2397 dbus_error_free (&error);
2398 goto reget_service_info_data;
2402 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2403 message_kind = GOT_SOMETHING_ELSE;
2406 else if (!old_owner[0])
2407 message_kind = GOT_SERVICE_CREATED;
2408 else if (!new_owner[0])
2409 message_kind = GOT_SERVICE_DELETED;
2411 message_kind = GOT_SOMETHING_ELSE;
2413 dbus_error_free (&error);
2415 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2416 message_kind = GOT_ERROR;
2418 message_kind = GOT_SOMETHING_ELSE;
2420 return message_kind;
2423 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2425 /* returns TRUE if the correct thing happens,
2426 * but the correct thing may include OOM errors.
2429 check_existent_service_no_auto_start (BusContext *context,
2430 DBusConnection *connection)
2432 DBusMessage *message;
2433 DBusMessage *base_service_message;
2434 const char *base_service;
2435 dbus_uint32_t serial;
2437 const char *existent = EXISTENT_SERVICE_NAME;
2438 dbus_uint32_t flags;
2440 base_service_message = NULL;
2442 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2444 DBUS_INTERFACE_DBUS,
2445 "StartServiceByName");
2447 if (message == NULL)
2450 dbus_message_set_auto_start (message, FALSE);
2453 if (!dbus_message_append_args (message,
2454 DBUS_TYPE_STRING, &existent,
2455 DBUS_TYPE_UINT32, &flags,
2458 dbus_message_unref (message);
2462 if (!dbus_connection_send (connection, message, &serial))
2464 dbus_message_unref (message);
2468 dbus_message_unref (message);
2471 bus_test_run_everything (context);
2473 /* now wait for the message bus to hear back from the activated
2476 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2478 bus_test_run_everything (context);
2480 if (!dbus_connection_get_is_connected (connection))
2482 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2488 message = pop_message_waiting_for_memory (connection);
2489 if (message == NULL)
2491 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2492 "StartServiceByName", serial, connection);
2496 verbose_message_received (connection, message);
2497 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2499 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2501 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2503 _dbus_warn ("Message has wrong sender %s\n",
2504 dbus_message_get_sender (message) ?
2505 dbus_message_get_sender (message) : "(none)");
2509 if (dbus_message_is_error (message,
2510 DBUS_ERROR_NO_MEMORY))
2512 ; /* good, this is a valid response */
2514 else if (dbus_message_is_error (message,
2515 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2516 dbus_message_is_error (message,
2517 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2518 dbus_message_is_error (message,
2519 DBUS_ERROR_SPAWN_EXEC_FAILED))
2521 ; /* good, this is expected also */
2525 _dbus_warn ("Did not expect error %s\n",
2526 dbus_message_get_error_name (message));
2532 GotServiceInfo message_kind;
2534 if (!check_base_service_activated (context, connection,
2535 message, &base_service))
2538 base_service_message = message;
2541 /* We may need to block here for the test service to exit or finish up */
2542 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2544 message = dbus_connection_borrow_message (connection);
2545 if (message == NULL)
2547 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2551 message_kind = check_got_service_info (message);
2553 dbus_connection_return_message (connection, message);
2556 switch (message_kind)
2558 case GOT_SOMETHING_ELSE:
2559 _dbus_warn ("Unexpected message after ActivateService "
2560 "(should be an error or a service announcement");
2564 if (!check_got_error (context, connection,
2565 DBUS_ERROR_SPAWN_CHILD_EXITED,
2566 DBUS_ERROR_NO_MEMORY,
2569 /* A service deleted should be coming along now after this error.
2570 * We can also get the error *after* the service deleted.
2575 case GOT_SERVICE_DELETED:
2577 /* The service started up and got a base address, but then
2578 * failed to register under EXISTENT_SERVICE_NAME
2580 CheckServiceOwnerChangedData socd;
2582 socd.expected_kind = SERVICE_DELETED;
2583 socd.expected_service_name = base_service;
2584 socd.failed = FALSE;
2585 socd.skip_connection = NULL;
2587 bus_test_clients_foreach (check_service_owner_changed_foreach,
2593 /* Now we should get an error about the service exiting
2594 * if we didn't get it before.
2596 if (message_kind != GOT_ERROR)
2598 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2600 /* and process everything again */
2601 bus_test_run_everything (context);
2603 if (!check_got_error (context, connection,
2604 DBUS_ERROR_SPAWN_CHILD_EXITED,
2605 DBUS_ERROR_NO_MEMORY,
2612 case GOT_SERVICE_CREATED:
2613 message = pop_message_waiting_for_memory (connection);
2614 if (message == NULL)
2616 _dbus_warn ("Failed to pop message we just put back! "
2617 "should have been a NameOwnerChanged (creation)\n");
2621 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2622 base_service, message))
2625 dbus_message_unref (message);
2628 if (!check_no_leftovers (context))
2630 _dbus_warn ("Messages were left over after successful activation\n");
2634 if (!check_send_exit_to_service (context, connection,
2635 EXISTENT_SERVICE_NAME, base_service))
2646 dbus_message_unref (message);
2648 if (base_service_message)
2649 dbus_message_unref (base_service_message);
2654 #ifndef DBUS_WIN_FIXME
2655 /* returns TRUE if the correct thing happens,
2656 * but the correct thing may include OOM errors.
2659 check_segfault_service_no_auto_start (BusContext *context,
2660 DBusConnection *connection)
2662 DBusMessage *message;
2663 dbus_uint32_t serial;
2665 const char *segv_service;
2666 dbus_uint32_t flags;
2668 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2670 DBUS_INTERFACE_DBUS,
2671 "StartServiceByName");
2673 if (message == NULL)
2676 dbus_message_set_auto_start (message, FALSE);
2678 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2680 if (!dbus_message_append_args (message,
2681 DBUS_TYPE_STRING, &segv_service,
2682 DBUS_TYPE_UINT32, &flags,
2685 dbus_message_unref (message);
2689 if (!dbus_connection_send (connection, message, &serial))
2691 dbus_message_unref (message);
2695 dbus_message_unref (message);
2698 bus_test_run_everything (context);
2699 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2700 bus_test_run_everything (context);
2702 if (!dbus_connection_get_is_connected (connection))
2704 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2710 message = pop_message_waiting_for_memory (connection);
2711 if (message == NULL)
2713 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2714 "StartServiceByName", serial, connection);
2718 verbose_message_received (connection, message);
2720 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2722 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2724 _dbus_warn ("Message has wrong sender %s\n",
2725 dbus_message_get_sender (message) ?
2726 dbus_message_get_sender (message) : "(none)");
2730 if (dbus_message_is_error (message,
2731 DBUS_ERROR_NO_MEMORY))
2733 ; /* good, this is a valid response */
2735 else if (dbus_message_is_error (message,
2738 const char *servicehelper;
2739 servicehelper = bus_context_get_servicehelper (context);
2740 /* make sure this only happens with the launch helper */
2741 _dbus_assert (servicehelper != NULL);
2743 else if (dbus_message_is_error (message,
2744 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2746 ; /* good, this is expected also */
2750 warn_unexpected (connection, message, "not this error");
2757 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2765 dbus_message_unref (message);
2771 /* returns TRUE if the correct thing happens,
2772 * but the correct thing may include OOM errors.
2775 check_segfault_service_auto_start (BusContext *context,
2776 DBusConnection *connection)
2778 DBusMessage *message;
2779 dbus_uint32_t serial;
2782 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2783 "/org/freedesktop/TestSuite",
2784 "org.freedesktop.TestSuite",
2787 if (message == NULL)
2790 if (!dbus_connection_send (connection, message, &serial))
2792 dbus_message_unref (message);
2796 dbus_message_unref (message);
2799 bus_test_run_everything (context);
2800 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2801 bus_test_run_everything (context);
2803 if (!dbus_connection_get_is_connected (connection))
2805 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2811 message = pop_message_waiting_for_memory (connection);
2812 if (message == NULL)
2814 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2815 "Echo message (auto activation)", serial, connection);
2819 verbose_message_received (connection, message);
2821 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2823 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2825 _dbus_warn ("Message has wrong sender %s\n",
2826 dbus_message_get_sender (message) ?
2827 dbus_message_get_sender (message) : "(none)");
2831 if (dbus_message_is_error (message,
2832 DBUS_ERROR_NO_MEMORY))
2834 ; /* good, this is a valid response */
2836 else if (dbus_message_is_error (message,
2837 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2839 ; /* good, this is expected also */
2843 warn_unexpected (connection, message, "not this error");
2850 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2858 dbus_message_unref (message);
2864 #define TEST_ECHO_MESSAGE "Test echo message"
2865 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2867 /* returns TRUE if the correct thing happens,
2868 * but the correct thing may include OOM errors.
2871 check_existent_hello_from_self (BusContext *context,
2872 DBusConnection *connection)
2874 DBusMessage *message;
2875 dbus_uint32_t serial;
2878 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2879 "/org/freedesktop/TestSuite",
2880 "org.freedesktop.TestSuite",
2881 "RunHelloFromSelf");
2883 if (message == NULL)
2886 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2887 if (!dbus_message_append_args (message,
2888 DBUS_TYPE_STRING, &text,
2891 dbus_message_unref (message);
2895 if (!dbus_connection_send (connection, message, &serial))
2897 dbus_message_unref (message);
2901 dbus_message_unref (message);
2904 bus_test_run_everything (context);
2906 /* Note: if this test is run in OOM mode, it will block when the bus
2907 * doesn't send a reply due to OOM.
2909 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2911 message = pop_message_waiting_for_memory (connection);
2912 if (message == NULL)
2914 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2918 if (dbus_message_get_reply_serial (message) != serial)
2920 _dbus_warn ("Wrong reply serial\n");
2921 dbus_message_unref (message);
2925 dbus_message_unref (message);
2931 /* returns TRUE if the correct thing happens,
2932 * but the correct thing may include OOM errors.
2935 check_existent_ping (BusContext *context,
2936 DBusConnection *connection)
2938 DBusMessage *message;
2939 dbus_uint32_t serial;
2940 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2941 "/org/freedesktop/TestSuite",
2942 "org.freedesktop.DBus.Peer",
2945 if (message == NULL)
2948 if (!dbus_connection_send (connection, message, &serial))
2950 dbus_message_unref (message);
2954 dbus_message_unref (message);
2957 bus_test_run_everything (context);
2959 /* Note: if this test is run in OOM mode, it will block when the bus
2960 * doesn't send a reply due to OOM.
2962 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2964 message = pop_message_waiting_for_memory (connection);
2965 if (message == NULL)
2967 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2971 if (dbus_message_get_reply_serial (message) != serial)
2973 _dbus_warn ("Wrong reply serial\n");
2974 dbus_message_unref (message);
2978 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2980 _dbus_warn ("Unexpected message return during Ping\n");
2981 dbus_message_unref (message);
2985 dbus_message_unref (message);
2991 /* returns TRUE if the correct thing happens,
2992 * but the correct thing may include OOM errors.
2995 check_existent_get_machine_id (BusContext *context,
2996 DBusConnection *connection)
2998 DBusMessage *message;
2999 dbus_uint32_t serial;
3000 const char *machine_id;
3002 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3003 "/org/freedesktop/TestSuite",
3004 "org.freedesktop.DBus.Peer",
3007 if (message == NULL)
3010 if (!dbus_connection_send (connection, message, &serial))
3012 dbus_message_unref (message);
3016 dbus_message_unref (message);
3019 bus_test_run_everything (context);
3021 /* Note: if this test is run in OOM mode, it will block when the bus
3022 * doesn't send a reply due to OOM.
3024 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3026 message = pop_message_waiting_for_memory (connection);
3027 if (message == NULL)
3029 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3033 if (dbus_message_get_reply_serial (message) != serial)
3035 _dbus_warn ("Wrong reply serial\n");
3036 dbus_message_unref (message);
3040 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3042 _dbus_warn ("Unexpected message return during GetMachineId\n");
3043 dbus_message_unref (message);
3048 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3050 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3051 dbus_message_unref (message);
3055 if (machine_id == NULL || strlen (machine_id) != 32)
3057 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3058 dbus_message_unref (message);
3062 /* We can't check that the machine id is correct because during make check it is
3063 * just made up for each process separately
3066 dbus_message_unref (message);
3072 /* returns TRUE if the correct thing happens,
3073 * but the correct thing may include OOM errors.
3076 check_existent_service_auto_start (BusContext *context,
3077 DBusConnection *connection)
3079 DBusMessage *message;
3080 DBusMessage *base_service_message;
3081 dbus_uint32_t serial;
3083 const char *base_service;
3086 base_service_message = NULL;
3088 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3089 "/org/freedesktop/TestSuite",
3090 "org.freedesktop.TestSuite",
3093 if (message == NULL)
3096 text = TEST_ECHO_MESSAGE;
3097 if (!dbus_message_append_args (message,
3098 DBUS_TYPE_STRING, &text,
3101 dbus_message_unref (message);
3105 if (!dbus_connection_send (connection, message, &serial))
3107 dbus_message_unref (message);
3111 dbus_message_unref (message);
3114 bus_test_run_everything (context);
3116 /* now wait for the message bus to hear back from the activated
3119 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3120 bus_test_run_everything (context);
3122 if (!dbus_connection_get_is_connected (connection))
3124 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3130 message = pop_message_waiting_for_memory (connection);
3131 if (message == NULL)
3133 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3134 serial, connection);
3138 verbose_message_received (connection, message);
3139 _dbus_verbose (" (after sending %s)\n", "auto start");
3141 /* we should get zero or two ServiceOwnerChanged signals */
3142 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3144 GotServiceInfo message_kind;
3146 if (!check_base_service_activated (context, connection,
3147 message, &base_service))
3150 base_service_message = message;
3153 /* We may need to block here for the test service to exit or finish up */
3154 block_connection_until_message_from_bus (context, connection, "service to exit");
3156 /* Should get a service creation notification for the activated
3157 * service name, or a service deletion on the base service name
3159 message = dbus_connection_borrow_message (connection);
3160 if (message == NULL)
3162 _dbus_warn ("No message after auto activation "
3163 "(should be a service announcement)\n");
3164 dbus_connection_return_message (connection, message);
3169 message_kind = check_got_service_info (message);
3171 dbus_connection_return_message (connection, message);
3174 switch (message_kind)
3176 case GOT_SERVICE_CREATED:
3177 message = pop_message_waiting_for_memory (connection);
3178 if (message == NULL)
3180 _dbus_warn ("Failed to pop message we just put back! "
3181 "should have been a NameOwnerChanged (creation)\n");
3185 /* Check that ServiceOwnerChanged (creation) was correctly received */
3186 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3187 base_service, message))
3190 dbus_message_unref (message);
3195 case GOT_SERVICE_DELETED:
3197 /* The service started up and got a base address, but then
3198 * failed to register under EXISTENT_SERVICE_NAME
3200 CheckServiceOwnerChangedData socd;
3202 socd.expected_kind = SERVICE_DELETED;
3203 socd.expected_service_name = base_service;
3204 socd.failed = FALSE;
3205 socd.skip_connection = NULL;
3206 bus_test_clients_foreach (check_service_owner_changed_foreach,
3216 case GOT_SOMETHING_ELSE:
3217 _dbus_warn ("Unexpected message after auto activation\n");
3222 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3223 * come the method reply (or error) from the initial method call
3226 /* Note: if this test is run in OOM mode, it will block when the bus
3227 * doesn't send a reply due to OOM.
3229 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3231 message = pop_message_waiting_for_memory (connection);
3232 if (message == NULL)
3234 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3238 if (dbus_message_get_reply_serial (message) != serial)
3240 _dbus_warn ("Wrong reply serial\n");
3244 dbus_message_unref (message);
3247 if (!check_existent_ping (context, connection))
3250 if (!check_existent_get_machine_id (context, connection))
3253 if (!check_existent_hello_from_self (context, connection))
3256 if (!check_send_exit_to_service (context, connection,
3257 EXISTENT_SERVICE_NAME,
3265 dbus_message_unref (message);
3267 if (base_service_message)
3268 dbus_message_unref (base_service_message);
3273 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3275 /* returns TRUE if the correct thing happens,
3276 * but the correct thing may include OOM errors.
3279 check_launch_service_file_missing (BusContext *context,
3280 DBusConnection *connection)
3282 DBusMessage *message;
3283 dbus_uint32_t serial;
3286 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3287 "/org/freedesktop/TestSuite",
3288 "org.freedesktop.TestSuite",
3291 if (message == NULL)
3294 if (!dbus_connection_send (connection, message, &serial))
3296 dbus_message_unref (message);
3300 dbus_message_unref (message);
3303 bus_test_run_everything (context);
3304 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3305 bus_test_run_everything (context);
3307 if (!dbus_connection_get_is_connected (connection))
3309 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3315 message = pop_message_waiting_for_memory (connection);
3316 if (message == NULL)
3318 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3319 "Echo message (auto activation)", serial, connection);
3323 verbose_message_received (connection, message);
3325 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3327 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3329 _dbus_warn ("Message has wrong sender %s\n",
3330 dbus_message_get_sender (message) ?
3331 dbus_message_get_sender (message) : "(none)");
3335 if (dbus_message_is_error (message,
3336 DBUS_ERROR_NO_MEMORY))
3338 ; /* good, this is a valid response */
3340 else if (dbus_message_is_error (message,
3341 DBUS_ERROR_SERVICE_UNKNOWN))
3343 _dbus_verbose("got service unknown\n");
3344 ; /* good, this is expected (only valid when using launch helper) */
3348 warn_unexpected (connection, message, "not this error");
3355 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3363 dbus_message_unref (message);
3368 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3370 /* returns TRUE if the correct thing happens,
3371 * but the correct thing may include OOM errors.
3374 check_launch_service_user_missing (BusContext *context,
3375 DBusConnection *connection)
3377 DBusMessage *message;
3378 dbus_uint32_t serial;
3381 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3382 "/org/freedesktop/TestSuite",
3383 "org.freedesktop.TestSuite",
3386 if (message == NULL)
3389 if (!dbus_connection_send (connection, message, &serial))
3391 dbus_message_unref (message);
3395 dbus_message_unref (message);
3398 bus_test_run_everything (context);
3399 block_connection_until_message_from_bus (context, connection,
3400 "reply to service which should fail to auto-start (missing User)");
3401 bus_test_run_everything (context);
3403 if (!dbus_connection_get_is_connected (connection))
3405 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3411 message = pop_message_waiting_for_memory (connection);
3412 if (message == NULL)
3414 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3415 "Echo message (auto activation)", serial, connection);
3419 verbose_message_received (connection, message);
3421 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3423 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3425 _dbus_warn ("Message has wrong sender %s\n",
3426 dbus_message_get_sender (message) ?
3427 dbus_message_get_sender (message) : "(none)");
3431 if (dbus_message_is_error (message,
3432 DBUS_ERROR_NO_MEMORY))
3434 ; /* good, this is a valid response */
3436 else if (dbus_message_is_error (message,
3437 DBUS_ERROR_SPAWN_FILE_INVALID))
3439 _dbus_verbose("got service file invalid\n");
3440 ; /* good, this is expected (only valid when using launch helper) */
3444 warn_unexpected (connection, message, "not this error");
3451 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3459 dbus_message_unref (message);
3464 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3466 /* returns TRUE if the correct thing happens,
3467 * but the correct thing may include OOM errors.
3470 check_launch_service_exec_missing (BusContext *context,
3471 DBusConnection *connection)
3473 DBusMessage *message;
3474 dbus_uint32_t serial;
3477 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3478 "/org/freedesktop/TestSuite",
3479 "org.freedesktop.TestSuite",
3482 if (message == NULL)
3485 if (!dbus_connection_send (connection, message, &serial))
3487 dbus_message_unref (message);
3491 dbus_message_unref (message);
3494 bus_test_run_everything (context);
3495 block_connection_until_message_from_bus (context, connection,
3496 "reply to service which should fail to auto-start (missing Exec)");
3497 bus_test_run_everything (context);
3499 if (!dbus_connection_get_is_connected (connection))
3501 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3507 message = pop_message_waiting_for_memory (connection);
3508 if (message == NULL)
3510 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3511 "Echo message (auto activation)", serial, connection);
3515 verbose_message_received (connection, message);
3517 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3519 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3521 _dbus_warn ("Message has wrong sender %s\n",
3522 dbus_message_get_sender (message) ?
3523 dbus_message_get_sender (message) : "(none)");
3527 if (dbus_message_is_error (message,
3528 DBUS_ERROR_NO_MEMORY))
3530 ; /* good, this is a valid response */
3532 else if (dbus_message_is_error (message,
3533 DBUS_ERROR_SERVICE_UNKNOWN))
3535 _dbus_verbose("could not activate as invalid service file was not added\n");
3536 ; /* good, this is expected as we shouldn't have been added to
3537 * the activation list with a missing Exec key */
3539 else if (dbus_message_is_error (message,
3540 DBUS_ERROR_SPAWN_FILE_INVALID))
3542 _dbus_verbose("got service file invalid\n");
3543 ; /* good, this is allowed, and is the message passed back from the
3548 warn_unexpected (connection, message, "not this error");
3555 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3563 dbus_message_unref (message);
3568 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3570 /* returns TRUE if the correct thing happens,
3571 * but the correct thing may include OOM errors.
3574 check_launch_service_service_missing (BusContext *context,
3575 DBusConnection *connection)
3577 DBusMessage *message;
3578 dbus_uint32_t serial;
3581 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3582 "/org/freedesktop/TestSuite",
3583 "org.freedesktop.TestSuite",
3586 if (message == NULL)
3589 if (!dbus_connection_send (connection, message, &serial))
3591 dbus_message_unref (message);
3595 dbus_message_unref (message);
3598 bus_test_run_everything (context);
3599 block_connection_until_message_from_bus (context, connection,
3600 "reply to service which should fail to auto-start (missing Service)");
3601 bus_test_run_everything (context);
3603 if (!dbus_connection_get_is_connected (connection))
3605 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3611 message = pop_message_waiting_for_memory (connection);
3612 if (message == NULL)
3614 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3615 "Echo message (auto activation)", serial, connection);
3619 verbose_message_received (connection, message);
3621 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3623 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3625 _dbus_warn ("Message has wrong sender %s\n",
3626 dbus_message_get_sender (message) ?
3627 dbus_message_get_sender (message) : "(none)");
3631 if (dbus_message_is_error (message,
3632 DBUS_ERROR_NO_MEMORY))
3634 ; /* good, this is a valid response */
3636 else if (dbus_message_is_error (message,
3637 DBUS_ERROR_SERVICE_UNKNOWN))
3639 _dbus_verbose("could not activate as invalid service file was not added\n");
3640 ; /* good, this is expected as we shouldn't have been added to
3641 * the activation list with a missing Exec key */
3643 else if (dbus_message_is_error (message,
3644 DBUS_ERROR_SPAWN_FILE_INVALID))
3646 _dbus_verbose("got service file invalid\n");
3647 ; /* good, this is allowed, and is the message passed back from the
3652 warn_unexpected (connection, message, "not this error");
3659 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3667 dbus_message_unref (message);
3672 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3674 /* returns TRUE if the correct thing happens,
3675 * but the correct thing may include OOM errors.
3678 check_shell_fail_service_auto_start (BusContext *context,
3679 DBusConnection *connection)
3681 DBusMessage *message;
3682 dbus_uint32_t serial;
3685 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3686 "/org/freedesktop/TestSuite",
3687 "org.freedesktop.TestSuite",
3690 if (message == NULL)
3693 if (!dbus_connection_send (connection, message, &serial))
3695 dbus_message_unref (message);
3699 dbus_message_unref (message);
3702 bus_test_run_everything (context);
3703 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3704 bus_test_run_everything (context);
3706 if (!dbus_connection_get_is_connected (connection))
3708 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3714 message = pop_message_waiting_for_memory (connection);
3715 if (message == NULL)
3717 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3718 "Echo message (auto activation)", serial, connection);
3722 verbose_message_received (connection, message);
3724 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3726 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3728 _dbus_warn ("Message has wrong sender %s\n",
3729 dbus_message_get_sender (message) ?
3730 dbus_message_get_sender (message) : "(none)");
3734 if (dbus_message_is_error (message,
3735 DBUS_ERROR_NO_MEMORY))
3737 ; /* good, this is a valid response */
3739 else if (dbus_message_is_error (message,
3740 DBUS_ERROR_INVALID_ARGS))
3742 _dbus_verbose("got invalid args\n");
3743 ; /* good, this is expected also */
3747 warn_unexpected (connection, message, "not this error");
3754 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3762 dbus_message_unref (message);
3767 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3769 /* returns TRUE if the correct thing happens,
3770 * but the correct thing may include OOM errors.
3773 check_shell_service_success_auto_start (BusContext *context,
3774 DBusConnection *connection)
3776 DBusMessage *message;
3777 DBusMessage *base_service_message;
3778 dbus_uint32_t serial;
3780 const char *base_service;
3781 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3783 base_service_message = NULL;
3785 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3786 "/org/freedesktop/TestSuite",
3787 "org.freedesktop.TestSuite",
3790 if (message == NULL)
3793 if (!dbus_connection_send (connection, message, &serial))
3795 dbus_message_unref (message);
3799 dbus_message_unref (message);
3802 bus_test_run_everything (context);
3804 /* now wait for the message bus to hear back from the activated
3807 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3808 bus_test_run_everything (context);
3810 if (!dbus_connection_get_is_connected (connection))
3812 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3818 message = pop_message_waiting_for_memory (connection);
3819 if (message == NULL)
3821 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3822 serial, connection);
3826 verbose_message_received (connection, message);
3827 _dbus_verbose (" (after sending %s)\n", "auto start");
3829 /* we should get zero or two ServiceOwnerChanged signals */
3830 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3832 GotServiceInfo message_kind;
3834 if (!check_base_service_activated (context, connection,
3835 message, &base_service))
3838 base_service_message = message;
3841 /* We may need to block here for the test service to exit or finish up */
3842 block_connection_until_message_from_bus (context, connection, "service to exit");
3844 /* Should get a service creation notification for the activated
3845 * service name, or a service deletion on the base service name
3847 message = dbus_connection_borrow_message (connection);
3848 if (message == NULL)
3850 _dbus_warn ("No message after auto activation "
3851 "(should be a service announcement)\n");
3852 dbus_connection_return_message (connection, message);
3857 message_kind = check_got_service_info (message);
3859 dbus_connection_return_message (connection, message);
3862 switch (message_kind)
3864 case GOT_SERVICE_CREATED:
3865 message = pop_message_waiting_for_memory (connection);
3866 if (message == NULL)
3868 _dbus_warn ("Failed to pop message we just put back! "
3869 "should have been a NameOwnerChanged (creation)\n");
3873 /* Check that ServiceOwnerChanged (creation) was correctly received */
3874 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3875 base_service, message))
3878 dbus_message_unref (message);
3883 case GOT_SERVICE_DELETED:
3885 /* The service started up and got a base address, but then
3886 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3888 CheckServiceOwnerChangedData socd;
3890 socd.expected_kind = SERVICE_DELETED;
3891 socd.expected_service_name = base_service;
3892 socd.failed = FALSE;
3893 socd.skip_connection = NULL;
3894 bus_test_clients_foreach (check_service_owner_changed_foreach,
3904 case GOT_SOMETHING_ELSE:
3905 _dbus_warn ("Unexpected message after auto activation\n");
3910 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3911 * come the method reply (or error) from the initial method call
3914 /* Note: if this test is run in OOM mode, it will block when the bus
3915 * doesn't send a reply due to OOM.
3917 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3919 message = pop_message_waiting_for_memory (connection);
3920 if (message == NULL)
3922 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3926 if (dbus_message_get_reply_serial (message) != serial)
3928 _dbus_warn ("Wrong reply serial\n");
3932 if (!dbus_message_get_args (message, NULL,
3933 DBUS_TYPE_STRING, &argv[0],
3934 DBUS_TYPE_STRING, &argv[1],
3935 DBUS_TYPE_STRING, &argv[2],
3936 DBUS_TYPE_STRING, &argv[3],
3937 DBUS_TYPE_STRING, &argv[4],
3938 DBUS_TYPE_STRING, &argv[5],
3939 DBUS_TYPE_STRING, &argv[6],
3942 _dbus_warn ("Error getting arguments from return\n");
3946 /* don't worry about arg[0] as it may be different
3947 depending on the path to the tests
3949 if (strcmp("-test", argv[1]) != 0)
3951 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3956 if (strcmp("that", argv[2]) != 0)
3958 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3963 if (strcmp("we get", argv[3]) != 0)
3965 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3970 if (strcmp("back", argv[4]) != 0)
3972 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3977 if (strcmp("--what", argv[5]) != 0)
3979 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3984 if (strcmp("we put in", argv[6]) != 0)
3986 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3987 "we put in", argv[6]);
3991 dbus_message_unref (message);
3994 if (!check_send_exit_to_service (context, connection,
3995 SHELL_SUCCESS_SERVICE_NAME,
4003 dbus_message_unref (message);
4005 if (base_service_message)
4006 dbus_message_unref (base_service_message);
4014 BusContext *context;
4018 check_oom_check1_func (void *data)
4020 Check1Data *d = data;
4022 if (! (* d->func) (d->context))
4025 if (!check_no_leftovers (d->context))
4027 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4035 check1_try_iterations (BusContext *context,
4036 const char *description,
4042 d.context = context;
4044 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4046 _dbus_assert_not_reached ("test failed");
4050 check_get_services (BusContext *context,
4051 DBusConnection *connection,
4056 DBusMessage *message;
4057 dbus_uint32_t serial;
4064 dbus_error_init (&error);
4067 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4069 DBUS_INTERFACE_DBUS,
4072 if (message == NULL)
4075 if (!dbus_connection_send (connection, message, &serial))
4077 dbus_message_unref (message);
4081 /* send our message */
4082 bus_test_run_clients_loop (SEND_PENDING (connection));
4084 dbus_message_unref (message);
4087 dbus_connection_ref (connection); /* because we may get disconnected */
4088 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4090 if (!dbus_connection_get_is_connected (connection))
4092 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4094 dbus_connection_unref (connection);
4099 dbus_connection_unref (connection);
4101 message = pop_message_waiting_for_memory (connection);
4102 if (message == NULL)
4104 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4105 method, serial, connection);
4109 verbose_message_received (connection, message);
4111 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4113 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4115 ; /* good, this is a valid response */
4119 warn_unexpected (connection, message, "not this error");
4126 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4128 ; /* good, expected */
4132 warn_unexpected (connection, message,
4133 "method_return for ListActivatableNames/ListNames");
4140 if (!dbus_message_get_args (message, &error,
4146 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4148 _dbus_verbose ("no memory to list services by %s\n", method);
4149 dbus_error_free (&error);
4150 _dbus_wait_for_memory ();
4151 goto retry_get_property;
4155 _dbus_assert (dbus_error_is_set (&error));
4156 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4165 if (!check_no_leftovers (context))
4171 dbus_error_free (&error);
4174 dbus_message_unref (message);
4179 /* returns TRUE if the correct thing happens,
4180 * but the correct thing may include OOM errors.
4183 check_list_services (BusContext *context,
4184 DBusConnection *connection)
4186 DBusMessage *message;
4187 DBusMessage *base_service_message;
4188 const char *base_service;
4189 dbus_uint32_t serial;
4191 const char *existent = EXISTENT_SERVICE_NAME;
4192 dbus_uint32_t flags;
4196 _dbus_verbose ("check_list_services for %p\n", connection);
4198 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4203 if (!_dbus_string_array_contains ((const char **)services, existent))
4205 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4206 dbus_free_string_array (services);
4210 dbus_free_string_array (services);
4212 base_service_message = NULL;
4214 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4216 DBUS_INTERFACE_DBUS,
4217 "StartServiceByName");
4219 if (message == NULL)
4222 dbus_message_set_auto_start (message, FALSE);
4225 if (!dbus_message_append_args (message,
4226 DBUS_TYPE_STRING, &existent,
4227 DBUS_TYPE_UINT32, &flags,
4230 dbus_message_unref (message);
4234 if (!dbus_connection_send (connection, message, &serial))
4236 dbus_message_unref (message);
4240 dbus_message_unref (message);
4243 bus_test_run_everything (context);
4245 /* now wait for the message bus to hear back from the activated
4248 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4250 bus_test_run_everything (context);
4252 if (!dbus_connection_get_is_connected (connection))
4254 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4260 message = pop_message_waiting_for_memory (connection);
4261 if (message == NULL)
4263 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4264 "StartServiceByName", serial, connection);
4268 verbose_message_received (connection, message);
4269 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4271 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4273 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4275 _dbus_warn ("Message has wrong sender %s\n",
4276 dbus_message_get_sender (message) ?
4277 dbus_message_get_sender (message) : "(none)");
4281 if (dbus_message_is_error (message,
4282 DBUS_ERROR_NO_MEMORY))
4284 ; /* good, this is a valid response */
4286 else if (dbus_message_is_error (message,
4287 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4288 dbus_message_is_error (message,
4289 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4290 dbus_message_is_error (message,
4291 DBUS_ERROR_SPAWN_EXEC_FAILED))
4293 ; /* good, this is expected also */
4297 _dbus_warn ("Did not expect error %s\n",
4298 dbus_message_get_error_name (message));
4304 GotServiceInfo message_kind;
4306 if (!check_base_service_activated (context, connection,
4307 message, &base_service))
4310 base_service_message = message;
4313 /* We may need to block here for the test service to exit or finish up */
4314 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4316 message = dbus_connection_borrow_message (connection);
4317 if (message == NULL)
4319 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4323 message_kind = check_got_service_info (message);
4325 dbus_connection_return_message (connection, message);
4328 switch (message_kind)
4330 case GOT_SOMETHING_ELSE:
4332 case GOT_SERVICE_DELETED:
4333 _dbus_warn ("Unexpected message after ActivateService "
4334 "(should be an error or a service announcement)\n");
4337 case GOT_SERVICE_CREATED:
4338 message = pop_message_waiting_for_memory (connection);
4339 if (message == NULL)
4341 _dbus_warn ("Failed to pop message we just put back! "
4342 "should have been a NameOwnerChanged (creation)\n");
4346 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4347 base_service, message))
4350 dbus_message_unref (message);
4353 if (!check_no_leftovers (context))
4355 _dbus_warn ("Messages were left over after successful activation\n");
4363 if (!check_get_services (context, connection, "ListNames", &services, &len))
4368 if (!_dbus_string_array_contains ((const char **)services, existent))
4370 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4374 dbus_free_string_array (services);
4376 if (!check_send_exit_to_service (context, connection,
4377 EXISTENT_SERVICE_NAME, base_service))
4384 dbus_message_unref (message);
4386 if (base_service_message)
4387 dbus_message_unref (base_service_message);
4395 BusContext *context;
4396 DBusConnection *connection;
4400 check_oom_check2_func (void *data)
4402 Check2Data *d = data;
4404 if (! (* d->func) (d->context, d->connection))
4407 if (!check_no_leftovers (d->context))
4409 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4417 check2_try_iterations (BusContext *context,
4418 DBusConnection *connection,
4419 const char *description,
4425 d.context = context;
4426 d.connection = connection;
4428 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4431 _dbus_warn ("%s failed during oom\n", description);
4432 _dbus_assert_not_reached ("test failed");
4437 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4438 const char *filename)
4443 if (!_dbus_string_init (&full))
4446 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4448 _dbus_string_free (&full);
4452 _dbus_string_init_const (&file, filename);
4454 if (!_dbus_concat_dir_and_file (&full, &file))
4456 _dbus_string_free (&full);
4460 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4461 _dbus_string_get_const_data (&full));
4463 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4465 _dbus_string_free (&full);
4471 bus_dispatch_test_conf (const DBusString *test_data_dir,
4472 const char *filename,
4473 dbus_bool_t use_launcher)
4475 BusContext *context;
4476 DBusConnection *foo;
4477 DBusConnection *bar;
4478 DBusConnection *baz;
4481 /* save the config name for the activation helper */
4482 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4483 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4485 dbus_error_init (&error);
4487 context = bus_context_new_test (test_data_dir, filename);
4488 if (context == NULL)
4491 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4493 _dbus_assert_not_reached ("could not alloc connection");
4495 if (!bus_setup_debug_client (foo))
4496 _dbus_assert_not_reached ("could not set up connection");
4498 spin_connection_until_authenticated (context, foo);
4500 if (!check_hello_message (context, foo))
4501 _dbus_assert_not_reached ("hello message failed");
4503 if (!check_double_hello_message (context, foo))
4504 _dbus_assert_not_reached ("double hello message failed");
4506 if (!check_add_match_all (context, foo))
4507 _dbus_assert_not_reached ("AddMatch message failed");
4509 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4511 _dbus_assert_not_reached ("could not alloc connection");
4513 if (!bus_setup_debug_client (bar))
4514 _dbus_assert_not_reached ("could not set up connection");
4516 spin_connection_until_authenticated (context, bar);
4518 if (!check_hello_message (context, bar))
4519 _dbus_assert_not_reached ("hello message failed");
4521 if (!check_add_match_all (context, bar))
4522 _dbus_assert_not_reached ("AddMatch message failed");
4524 baz = dbus_connection_open_private (TEST_CONNECTION, &error);
4526 _dbus_assert_not_reached ("could not alloc connection");
4528 if (!bus_setup_debug_client (baz))
4529 _dbus_assert_not_reached ("could not set up connection");
4531 spin_connection_until_authenticated (context, baz);
4533 if (!check_hello_message (context, baz))
4534 _dbus_assert_not_reached ("hello message failed");
4536 if (!check_add_match_all (context, baz))
4537 _dbus_assert_not_reached ("AddMatch message failed");
4539 #ifdef DBUS_WIN_FIXME
4540 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4541 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4543 if (!check_get_connection_unix_user (context, baz))
4544 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4546 if (!check_get_connection_unix_process_id (context, baz))
4547 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4550 if (!check_list_services (context, baz))
4551 _dbus_assert_not_reached ("ListActivatableNames message failed");
4553 if (!check_no_leftovers (context))
4555 _dbus_warn ("Messages were left over after setting up initial connections\n");
4556 _dbus_assert_not_reached ("initial connection setup failed");
4559 check1_try_iterations (context, "create_and_hello",
4560 check_hello_connection);
4562 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4563 check_nonexistent_service_no_auto_start);
4565 #ifdef DBUS_WIN_FIXME
4566 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4568 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4569 check_segfault_service_no_auto_start);
4572 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4573 check_existent_service_no_auto_start);
4575 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4576 check_nonexistent_service_auto_start);
4579 #ifdef DBUS_WIN_FIXME
4580 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4582 /* only do the segfault test if we are not using the launcher */
4583 check2_try_iterations (context, foo, "segfault_service_auto_start",
4584 check_segfault_service_auto_start);
4587 /* only do the shell fail test if we are not using the launcher */
4588 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4589 check_shell_fail_service_auto_start);
4591 /* specific to launcher */
4593 if (!check_launch_service_file_missing (context, foo))
4594 _dbus_assert_not_reached ("did not get service file not found error");
4597 /* Note: need to resolve some issues with the testing code in order to run
4598 * this in oom (handle that we sometimes don't get replies back from the bus
4599 * when oom happens, without blocking the test).
4601 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4602 check_existent_service_auto_start);
4605 if (!check_existent_service_auto_start (context, foo))
4606 _dbus_assert_not_reached ("existent service auto start failed");
4608 if (!check_shell_service_success_auto_start (context, foo))
4609 _dbus_assert_not_reached ("shell success service auto start failed");
4611 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4613 kill_client_connection_unchecked (foo);
4614 kill_client_connection_unchecked (bar);
4615 kill_client_connection_unchecked (baz);
4617 bus_context_unref (context);
4623 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4624 const char *filename)
4626 BusContext *context;
4627 DBusConnection *foo;
4630 /* save the config name for the activation helper */
4631 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4632 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4634 dbus_error_init (&error);
4636 context = bus_context_new_test (test_data_dir, filename);
4637 if (context == NULL)
4640 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4642 _dbus_assert_not_reached ("could not alloc connection");
4644 if (!bus_setup_debug_client (foo))
4645 _dbus_assert_not_reached ("could not set up connection");
4647 spin_connection_until_authenticated (context, foo);
4649 if (!check_hello_message (context, foo))
4650 _dbus_assert_not_reached ("hello message failed");
4652 if (!check_double_hello_message (context, foo))
4653 _dbus_assert_not_reached ("double hello message failed");
4655 if (!check_add_match_all (context, foo))
4656 _dbus_assert_not_reached ("AddMatch message failed");
4658 /* this only tests the activation.c user check */
4659 if (!check_launch_service_user_missing (context, foo))
4660 _dbus_assert_not_reached ("user missing did not trigger error");
4662 /* this only tests the desktop.c exec check */
4663 if (!check_launch_service_exec_missing (context, foo))
4664 _dbus_assert_not_reached ("exec missing did not trigger error");
4666 /* this only tests the desktop.c service check */
4667 if (!check_launch_service_service_missing (context, foo))
4668 _dbus_assert_not_reached ("service missing did not trigger error");
4670 _dbus_verbose ("Disconnecting foo\n");
4672 kill_client_connection_unchecked (foo);
4674 bus_context_unref (context);
4680 bus_dispatch_test (const DBusString *test_data_dir)
4682 /* run normal activation tests */
4683 _dbus_verbose ("Normal activation tests\n");
4684 if (!bus_dispatch_test_conf (test_data_dir,
4685 "valid-config-files/debug-allow-all.conf", FALSE))
4688 /* run launch-helper activation tests */
4689 _dbus_verbose ("Launch helper activation tests\n");
4690 if (!bus_dispatch_test_conf (test_data_dir,
4691 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4694 /* run select launch-helper activation tests on broken service files */
4695 if (!bus_dispatch_test_conf_fail (test_data_dir,
4696 "valid-config-files-system/debug-allow-all-fail.conf"))
4703 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4705 BusContext *context;
4706 DBusConnection *foo;
4709 dbus_error_init (&error);
4711 /* Test SHA1 authentication */
4712 _dbus_verbose ("Testing SHA1 context\n");
4714 context = bus_context_new_test (test_data_dir,
4715 "valid-config-files/debug-allow-all-sha1.conf");
4716 if (context == NULL)
4719 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4721 _dbus_assert_not_reached ("could not alloc connection");
4723 if (!bus_setup_debug_client (foo))
4724 _dbus_assert_not_reached ("could not set up connection");
4726 spin_connection_until_authenticated (context, foo);
4728 if (!check_hello_message (context, foo))
4729 _dbus_assert_not_reached ("hello message failed");
4731 if (!check_add_match_all (context, foo))
4732 _dbus_assert_not_reached ("addmatch message failed");
4734 if (!check_no_leftovers (context))
4736 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4737 _dbus_assert_not_reached ("initial connection setup failed");
4740 check1_try_iterations (context, "create_and_hello_sha1",
4741 check_hello_connection);
4743 kill_client_connection_unchecked (foo);
4745 bus_context_unref (context);
4750 #ifdef HAVE_UNIX_FD_PASSING
4753 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4755 BusContext *context;
4756 DBusConnection *foo, *bar;
4760 int one[2], two[2], x, y, z;
4763 dbus_error_init (&error);
4765 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4766 if (context == NULL)
4767 _dbus_assert_not_reached ("could not alloc context");
4769 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4771 _dbus_assert_not_reached ("could not alloc connection");
4773 if (!bus_setup_debug_client (foo))
4774 _dbus_assert_not_reached ("could not set up connection");
4776 spin_connection_until_authenticated (context, foo);
4778 if (!check_hello_message (context, foo))
4779 _dbus_assert_not_reached ("hello message failed");
4781 if (!check_add_match_all (context, foo))
4782 _dbus_assert_not_reached ("AddMatch message failed");
4784 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4786 _dbus_assert_not_reached ("could not alloc connection");
4788 if (!bus_setup_debug_client (bar))
4789 _dbus_assert_not_reached ("could not set up connection");
4791 spin_connection_until_authenticated (context, bar);
4793 if (!check_hello_message (context, bar))
4794 _dbus_assert_not_reached ("hello message failed");
4796 if (!check_add_match_all (context, bar))
4797 _dbus_assert_not_reached ("AddMatch message failed");
4799 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4800 _dbus_assert_not_reached ("could not alloc message");
4802 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4803 _dbus_assert_not_reached("Failed to allocate pipe #1");
4805 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4806 _dbus_assert_not_reached("Failed to allocate pipe #2");
4808 if (!dbus_message_append_args(m,
4809 DBUS_TYPE_UNIX_FD, one,
4810 DBUS_TYPE_UNIX_FD, two,
4811 DBUS_TYPE_UNIX_FD, two,
4813 _dbus_assert_not_reached("Failed to attach fds.");
4815 if (!_dbus_close(one[0], &error))
4816 _dbus_assert_not_reached("Failed to close pipe #1 ");
4817 if (!_dbus_close(two[0], &error))
4818 _dbus_assert_not_reached("Failed to close pipe #2 ");
4820 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4821 _dbus_assert_not_reached("Connection cannot do fd passing");
4823 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4824 _dbus_assert_not_reached("Connection cannot do fd passing");
4826 if (!dbus_connection_send (foo, m, NULL))
4827 _dbus_assert_not_reached("Failed to send fds");
4829 dbus_message_unref(m);
4831 bus_test_run_clients_loop (SEND_PENDING (foo));
4833 bus_test_run_everything (context);
4835 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4837 if (!(m = pop_message_waiting_for_memory (foo)))
4838 _dbus_assert_not_reached("Failed to receive msg");
4840 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4841 _dbus_assert_not_reached("bogus message received");
4843 dbus_message_unref(m);
4845 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4847 if (!(m = pop_message_waiting_for_memory (bar)))
4848 _dbus_assert_not_reached("Failed to receive msg");
4850 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4851 _dbus_assert_not_reached("bogus message received");
4853 if (!dbus_message_get_args(m,
4855 DBUS_TYPE_UNIX_FD, &x,
4856 DBUS_TYPE_UNIX_FD, &y,
4857 DBUS_TYPE_UNIX_FD, &z,
4859 _dbus_assert_not_reached("Failed to parse fds.");
4861 dbus_message_unref(m);
4863 if (write(x, "X", 1) != 1)
4864 _dbus_assert_not_reached("Failed to write to pipe #1");
4865 if (write(y, "Y", 1) != 1)
4866 _dbus_assert_not_reached("Failed to write to pipe #2");
4867 if (write(z, "Z", 1) != 1)
4868 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4870 if (!_dbus_close(x, &error))
4871 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4872 if (!_dbus_close(y, &error))
4873 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4874 if (!_dbus_close(z, &error))
4875 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4877 if (read(one[1], &r, 1) != 1 || r != 'X')
4878 _dbus_assert_not_reached("Failed to read value from pipe.");
4879 if (read(two[1], &r, 1) != 1 || r != 'Y')
4880 _dbus_assert_not_reached("Failed to read value from pipe.");
4881 if (read(two[1], &r, 1) != 1 || r != 'Z')
4882 _dbus_assert_not_reached("Failed to read value from pipe.");
4884 if (!_dbus_close(one[1], &error))
4885 _dbus_assert_not_reached("Failed to close pipe #1 ");
4886 if (!_dbus_close(two[1], &error))
4887 _dbus_assert_not_reached("Failed to close pipe #2 ");
4889 _dbus_verbose ("Disconnecting foo\n");
4890 kill_client_connection_unchecked (foo);
4892 _dbus_verbose ("Disconnecting bar\n");
4893 kill_client_connection_unchecked (bar);
4895 bus_context_unref (context);
4901 #endif /* DBUS_BUILD_TESTS */