1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "connection.h"
30 #include "activation.h"
35 #include <dbus/dbus-internals.h>
38 #ifdef HAVE_UNIX_FD_PASSING
39 #include <dbus/dbus-sysdeps-unix.h>
44 send_one_message (DBusConnection *connection,
46 DBusConnection *sender,
47 DBusConnection *addressed_recipient,
49 BusTransaction *transaction,
52 if (!bus_context_check_security_policy (context, transaction,
58 return TRUE; /* silently don't send it */
60 if (dbus_message_contains_unix_fds(message) &&
61 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
62 return TRUE; /* silently don't send it */
64 if (!bus_transaction_send (transaction,
76 bus_dispatch_matches (BusTransaction *transaction,
77 DBusConnection *sender,
78 DBusConnection *addressed_recipient,
83 BusConnections *connections;
85 BusMatchmaker *matchmaker;
89 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
91 /* sender and recipient can both be NULL for the bus driver,
92 * or for signals with no particular recipient
95 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
96 _dbus_assert (dbus_message_get_sender (message) != NULL);
98 connections = bus_transaction_get_connections (transaction);
100 dbus_error_init (&tmp_error);
101 context = bus_transaction_get_context (transaction);
102 matchmaker = bus_context_get_matchmaker (context);
105 if (!bus_matchmaker_get_recipients (matchmaker, connections,
106 sender, addressed_recipient, message,
113 link = _dbus_list_get_first_link (&recipients);
116 DBusConnection *dest;
120 if (!send_one_message (dest, context, sender, addressed_recipient,
121 message, transaction, &tmp_error))
124 link = _dbus_list_get_next_link (&recipients, link);
127 _dbus_list_clear (&recipients);
129 if (dbus_error_is_set (&tmp_error))
131 dbus_move_error (&tmp_error, error);
138 static DBusHandlerResult
139 bus_dispatch (DBusConnection *connection,
140 DBusMessage *message)
142 const char *sender, *service_name;
144 BusTransaction *transaction;
146 DBusHandlerResult result;
147 DBusConnection *addressed_recipient;
149 result = DBUS_HANDLER_RESULT_HANDLED;
152 addressed_recipient = NULL;
153 dbus_error_init (&error);
155 context = bus_connection_get_context (connection);
156 _dbus_assert (context != NULL);
158 /* If we can't even allocate an OOM error, we just go to sleep
161 while (!bus_connection_preallocate_oom_error (connection))
162 _dbus_wait_for_memory ();
164 /* Ref connection in case we disconnect it at some point in here */
165 dbus_connection_ref (connection);
167 service_name = dbus_message_get_destination (message);
169 #ifdef DBUS_ENABLE_VERBOSE_MODE
171 const char *interface_name, *member_name, *error_name;
173 interface_name = dbus_message_get_interface (message);
174 member_name = dbus_message_get_member (message);
175 error_name = dbus_message_get_error_name (message);
177 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
178 interface_name ? interface_name : "(no interface)",
179 member_name ? member_name : "(no member)",
180 error_name ? error_name : "(no error name)",
181 service_name ? service_name : "peer");
183 #endif /* DBUS_ENABLE_VERBOSE_MODE */
185 /* If service_name is NULL, if it's a signal we send it to all
186 * connections with a match rule. If it's not a signal, there
187 * are some special cases here but mostly we just bail out.
189 if (service_name == NULL)
191 if (dbus_message_is_signal (message,
192 DBUS_INTERFACE_LOCAL,
195 bus_connection_disconnected (connection);
199 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
201 /* DBusConnection also handles some of these automatically, we leave
204 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
209 /* Create our transaction */
210 transaction = bus_transaction_new (context);
211 if (transaction == NULL)
213 BUS_SET_OOM (&error);
217 /* Assign a sender to the message */
218 if (bus_connection_is_active (connection))
220 sender = bus_connection_get_name (connection);
221 _dbus_assert (sender != NULL);
223 if (!dbus_message_set_sender (message, sender))
225 BUS_SET_OOM (&error);
229 /* We need to refetch the service name here, because
230 * dbus_message_set_sender can cause the header to be
231 * reallocated, and thus the service_name pointer will become
234 service_name = dbus_message_get_destination (message);
238 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
240 if (!bus_context_check_security_policy (context, transaction,
241 connection, NULL, NULL, message, &error))
243 _dbus_verbose ("Security policy rejected message\n");
247 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
248 if (!bus_driver_handle_message (connection, transaction, message, &error))
251 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
253 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
254 dbus_connection_close (connection);
257 else if (service_name != NULL) /* route to named service */
259 DBusString service_string;
261 BusRegistry *registry;
263 _dbus_assert (service_name != NULL);
265 registry = bus_connection_get_registry (connection);
267 _dbus_string_init_const (&service_string, service_name);
268 service = bus_registry_lookup (registry, &service_string);
270 if (service == NULL && dbus_message_get_auto_start (message))
272 BusActivation *activation;
273 /* We can't do the security policy check here, since the addressed
274 * recipient service doesn't exist yet. We do it before sending the
275 * message after the service has been created.
277 activation = bus_connection_get_activation (connection);
279 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
280 message, service_name, &error))
282 _DBUS_ASSERT_ERROR_IS_SET (&error);
283 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
289 else if (service == NULL)
291 dbus_set_error (&error,
292 DBUS_ERROR_NAME_HAS_NO_OWNER,
293 "Name \"%s\" does not exist",
299 addressed_recipient = bus_service_get_primary_owners_connection (service);
300 _dbus_assert (addressed_recipient != NULL);
302 if (!bus_context_check_security_policy (context, transaction,
303 connection, addressed_recipient,
308 if (dbus_message_contains_unix_fds(message) &&
309 !dbus_connection_can_send_type(addressed_recipient, DBUS_TYPE_UNIX_FD))
311 dbus_set_error(&error,
312 DBUS_ERROR_NOT_SUPPORTED,
313 "Tried to send message with Unix file descriptors"
314 "to a client that doesn't support that.");
318 /* Dispatch the message */
319 if (!bus_transaction_send (transaction, addressed_recipient, message))
321 BUS_SET_OOM (&error);
327 /* Now match the messages against any match rules, which will send
328 * out signals and such. addressed_recipient may == NULL.
330 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
334 if (dbus_error_is_set (&error))
336 if (!dbus_connection_get_is_connected (connection))
338 /* If we disconnected it, we won't bother to send it any error
341 _dbus_verbose ("Not sending error to connection we disconnected\n");
343 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
345 bus_connection_send_oom_error (connection, message);
347 /* cancel transaction due to OOM */
348 if (transaction != NULL)
350 bus_transaction_cancel_and_free (transaction);
356 /* Try to send the real error, if no mem to do that, send
359 _dbus_assert (transaction != NULL);
360 if (!bus_transaction_send_error_reply (transaction, connection,
363 bus_connection_send_oom_error (connection, message);
365 /* cancel transaction due to OOM */
366 if (transaction != NULL)
368 bus_transaction_cancel_and_free (transaction);
375 dbus_error_free (&error);
378 if (transaction != NULL)
380 bus_transaction_execute_and_free (transaction);
383 dbus_connection_unref (connection);
388 static DBusHandlerResult
389 bus_dispatch_message_filter (DBusConnection *connection,
390 DBusMessage *message,
393 return bus_dispatch (connection, message);
397 bus_dispatch_add_connection (DBusConnection *connection)
399 if (!dbus_connection_add_filter (connection,
400 bus_dispatch_message_filter,
408 bus_dispatch_remove_connection (DBusConnection *connection)
410 /* Here we tell the bus driver that we want to get off. */
411 bus_driver_remove_connection (connection);
413 dbus_connection_remove_filter (connection,
414 bus_dispatch_message_filter,
418 #ifdef DBUS_BUILD_TESTS
422 /* This is used to know whether we need to block in order to finish
423 * sending a message, or whether the initial dbus_connection_send()
424 * already flushed the queue.
426 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
428 typedef dbus_bool_t (* Check1Func) (BusContext *context);
429 typedef dbus_bool_t (* Check2Func) (BusContext *context,
430 DBusConnection *connection);
432 static dbus_bool_t check_no_leftovers (BusContext *context);
435 block_connection_until_message_from_bus (BusContext *context,
436 DBusConnection *connection,
437 const char *what_is_expected)
439 _dbus_verbose ("expecting: %s\n", what_is_expected);
441 while (dbus_connection_get_dispatch_status (connection) ==
442 DBUS_DISPATCH_COMPLETE &&
443 dbus_connection_get_is_connected (connection))
445 bus_test_run_bus_loop (context, TRUE);
446 bus_test_run_clients_loop (FALSE);
451 spin_connection_until_authenticated (BusContext *context,
452 DBusConnection *connection)
454 _dbus_verbose ("Spinning to auth connection %p\n", connection);
455 while (!dbus_connection_get_is_authenticated (connection) &&
456 dbus_connection_get_is_connected (connection))
458 bus_test_run_bus_loop (context, FALSE);
459 bus_test_run_clients_loop (FALSE);
461 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
464 /* compensate for fact that pop_message() can return #NULL due to OOM */
466 pop_message_waiting_for_memory (DBusConnection *connection)
468 while (dbus_connection_get_dispatch_status (connection) ==
469 DBUS_DISPATCH_NEED_MEMORY)
470 _dbus_wait_for_memory ();
472 return dbus_connection_pop_message (connection);
476 borrow_message_waiting_for_memory (DBusConnection *connection)
478 while (dbus_connection_get_dispatch_status (connection) ==
479 DBUS_DISPATCH_NEED_MEMORY)
480 _dbus_wait_for_memory ();
482 return dbus_connection_borrow_message (connection);
486 warn_unexpected_real (DBusConnection *connection,
487 DBusMessage *message,
488 const char *expected,
489 const char *function,
493 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
495 dbus_message_get_interface (message) ?
496 dbus_message_get_interface (message) : "(unset)",
497 dbus_message_get_member (message) ?
498 dbus_message_get_member (message) : "(unset)",
499 dbus_message_get_error_name (message) ?
500 dbus_message_get_error_name (message) : "(unset)",
504 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
505 function, line, connection, expected);
508 #define warn_unexpected(connection, message, expected) \
509 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
512 verbose_message_received (DBusConnection *connection,
513 DBusMessage *message)
515 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
516 dbus_message_get_interface (message) ?
517 dbus_message_get_interface (message) : "(unset)",
518 dbus_message_get_member (message) ?
519 dbus_message_get_member (message) : "(unset)",
520 dbus_message_get_error_name (message) ?
521 dbus_message_get_error_name (message) : "(unset)",
534 ServiceInfoKind expected_kind;
535 const char *expected_service_name;
537 DBusConnection *skip_connection;
538 } CheckServiceOwnerChangedData;
541 check_service_owner_changed_foreach (DBusConnection *connection,
544 CheckServiceOwnerChangedData *d = data;
545 DBusMessage *message;
547 const char *service_name, *old_owner, *new_owner;
549 if (d->expected_kind == SERVICE_CREATED
550 && connection == d->skip_connection)
553 dbus_error_init (&error);
556 message = pop_message_waiting_for_memory (connection);
559 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
560 connection, "NameOwnerChanged");
563 else if (!dbus_message_is_signal (message,
567 warn_unexpected (connection, message, "NameOwnerChanged");
573 reget_service_info_data:
578 dbus_message_get_args (message, &error,
579 DBUS_TYPE_STRING, &service_name,
580 DBUS_TYPE_STRING, &old_owner,
581 DBUS_TYPE_STRING, &new_owner,
584 if (dbus_error_is_set (&error))
586 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
588 dbus_error_free (&error);
589 _dbus_wait_for_memory ();
590 goto reget_service_info_data;
594 _dbus_warn ("Did not get the expected arguments\n");
599 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
600 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
601 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
603 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
607 if (strcmp (service_name, d->expected_service_name) != 0)
609 _dbus_warn ("expected info on service %s, got info on %s\n",
610 d->expected_service_name,
615 if (*service_name == ':' && new_owner[0]
616 && strcmp (service_name, new_owner) != 0)
618 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
619 service_name, old_owner, new_owner);
627 dbus_error_free (&error);
630 dbus_message_unref (message);
637 kill_client_connection (BusContext *context,
638 DBusConnection *connection)
642 CheckServiceOwnerChangedData socd;
644 _dbus_verbose ("killing connection %p\n", connection);
646 s = dbus_bus_get_unique_name (connection);
647 _dbus_assert (s != NULL);
649 while ((base_service = _dbus_strdup (s)) == NULL)
650 _dbus_wait_for_memory ();
652 dbus_connection_ref (connection);
654 /* kick in the disconnect handler that unrefs the connection */
655 dbus_connection_close (connection);
657 bus_test_run_everything (context);
659 _dbus_assert (bus_test_client_listed (connection));
661 /* Run disconnect handler in test.c */
662 if (bus_connection_dispatch_one_message (connection))
663 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
665 _dbus_assert (!dbus_connection_get_is_connected (connection));
666 dbus_connection_unref (connection);
668 _dbus_assert (!bus_test_client_listed (connection));
670 socd.expected_kind = SERVICE_DELETED;
671 socd.expected_service_name = base_service;
673 socd.skip_connection = NULL;
675 bus_test_clients_foreach (check_service_owner_changed_foreach,
678 dbus_free (base_service);
681 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
683 if (!check_no_leftovers (context))
684 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
688 kill_client_connection_unchecked (DBusConnection *connection)
690 /* This kills the connection without expecting it to affect
691 * the rest of the bus.
693 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
695 dbus_connection_ref (connection);
696 dbus_connection_close (connection);
697 /* dispatching disconnect handler will unref once */
698 if (bus_connection_dispatch_one_message (connection))
699 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
701 _dbus_assert (!bus_test_client_listed (connection));
702 dbus_connection_unref (connection);
708 } CheckNoMessagesData;
711 check_no_messages_foreach (DBusConnection *connection,
714 CheckNoMessagesData *d = data;
715 DBusMessage *message;
717 message = pop_message_waiting_for_memory (connection);
720 warn_unexpected (connection, message, "no messages");
726 dbus_message_unref (message);
731 check_no_leftovers (BusContext *context)
733 CheckNoMessagesData nmd;
736 bus_test_clients_foreach (check_no_messages_foreach,
741 _dbus_verbose ("%s: leftover message found\n",
742 _DBUS_FUNCTION_NAME);
749 /* returns TRUE if the correct thing happens,
750 * but the correct thing may include OOM errors.
753 check_hello_message (BusContext *context,
754 DBusConnection *connection)
756 DBusMessage *message;
757 DBusMessage *name_message;
758 dbus_uint32_t serial;
762 const char *acquired;
765 dbus_error_init (&error);
771 _dbus_verbose ("check_hello_message for %p\n", connection);
773 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
781 dbus_connection_ref (connection); /* because we may get disconnected */
783 if (!dbus_connection_send (connection, message, &serial))
785 dbus_message_unref (message);
786 dbus_connection_unref (connection);
790 _dbus_assert (dbus_message_has_signature (message, ""));
792 dbus_message_unref (message);
795 if (!dbus_connection_get_is_connected (connection))
797 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
799 dbus_connection_unref (connection);
804 /* send our message */
805 bus_test_run_clients_loop (SEND_PENDING (connection));
807 if (!dbus_connection_get_is_connected (connection))
809 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
811 dbus_connection_unref (connection);
816 block_connection_until_message_from_bus (context, connection, "reply to Hello");
818 if (!dbus_connection_get_is_connected (connection))
820 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
822 dbus_connection_unref (connection);
827 dbus_connection_unref (connection);
829 message = pop_message_waiting_for_memory (connection);
832 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
833 "Hello", serial, connection);
837 verbose_message_received (connection, message);
839 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
841 _dbus_warn ("Message has wrong sender %s\n",
842 dbus_message_get_sender (message) ?
843 dbus_message_get_sender (message) : "(none)");
847 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
849 if (dbus_message_is_error (message,
850 DBUS_ERROR_NO_MEMORY))
852 ; /* good, this is a valid response */
856 warn_unexpected (connection, message, "not this error");
863 CheckServiceOwnerChangedData socd;
865 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
867 ; /* good, expected */
871 warn_unexpected (connection, message, "method return for Hello");
876 retry_get_hello_name:
877 if (!dbus_message_get_args (message, &error,
878 DBUS_TYPE_STRING, &name,
881 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
883 _dbus_verbose ("no memory to get service name arg from hello\n");
884 dbus_error_free (&error);
885 _dbus_wait_for_memory ();
886 goto retry_get_hello_name;
890 _dbus_assert (dbus_error_is_set (&error));
891 _dbus_warn ("Did not get the expected single string argument to hello\n");
896 _dbus_verbose ("Got hello name: %s\n", name);
898 while (!dbus_bus_set_unique_name (connection, name))
899 _dbus_wait_for_memory ();
901 socd.expected_kind = SERVICE_CREATED;
902 socd.expected_service_name = name;
904 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
905 bus_test_clients_foreach (check_service_owner_changed_foreach,
911 name_message = message;
912 /* Client should also have gotten ServiceAcquired */
914 message = pop_message_waiting_for_memory (connection);
917 _dbus_warn ("Expecting %s, got nothing\n",
921 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
924 _dbus_warn ("Expecting %s, got smthg else\n",
929 retry_get_acquired_name:
930 if (!dbus_message_get_args (message, &error,
931 DBUS_TYPE_STRING, &acquired,
934 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
936 _dbus_verbose ("no memory to get service name arg from acquired\n");
937 dbus_error_free (&error);
938 _dbus_wait_for_memory ();
939 goto retry_get_acquired_name;
943 _dbus_assert (dbus_error_is_set (&error));
944 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
949 _dbus_verbose ("Got acquired name: %s\n", acquired);
951 if (strcmp (acquired, name) != 0)
953 _dbus_warn ("Acquired name is %s but expected %s\n",
960 if (!check_no_leftovers (context))
966 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
968 dbus_error_free (&error);
971 dbus_message_unref (message);
974 dbus_message_unref (name_message);
979 /* returns TRUE if the correct thing happens,
980 * but the correct thing may include OOM errors.
983 check_double_hello_message (BusContext *context,
984 DBusConnection *connection)
986 DBusMessage *message;
987 dbus_uint32_t serial;
992 dbus_error_init (&error);
995 _dbus_verbose ("check_double_hello_message for %p\n", connection);
997 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1002 if (message == NULL)
1005 if (!dbus_connection_send (connection, message, &serial))
1007 dbus_message_unref (message);
1011 dbus_message_unref (message);
1014 /* send our message */
1015 bus_test_run_clients_loop (SEND_PENDING (connection));
1017 dbus_connection_ref (connection); /* because we may get disconnected */
1018 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1020 if (!dbus_connection_get_is_connected (connection))
1022 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1024 dbus_connection_unref (connection);
1029 dbus_connection_unref (connection);
1031 message = pop_message_waiting_for_memory (connection);
1032 if (message == NULL)
1034 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1035 "Hello", serial, connection);
1039 verbose_message_received (connection, message);
1041 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1043 _dbus_warn ("Message has wrong sender %s\n",
1044 dbus_message_get_sender (message) ?
1045 dbus_message_get_sender (message) : "(none)");
1049 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1051 warn_unexpected (connection, message, "method return for Hello");
1055 if (!check_no_leftovers (context))
1061 dbus_error_free (&error);
1064 dbus_message_unref (message);
1069 /* returns TRUE if the correct thing happens,
1070 * but the correct thing may include OOM errors.
1073 check_get_connection_unix_user (BusContext *context,
1074 DBusConnection *connection)
1076 DBusMessage *message;
1077 dbus_uint32_t serial;
1080 const char *base_service_name;
1084 dbus_error_init (&error);
1087 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1089 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1091 DBUS_INTERFACE_DBUS,
1092 "GetConnectionUnixUser");
1094 if (message == NULL)
1097 base_service_name = dbus_bus_get_unique_name (connection);
1099 if (!dbus_message_append_args (message,
1100 DBUS_TYPE_STRING, &base_service_name,
1103 dbus_message_unref (message);
1107 if (!dbus_connection_send (connection, message, &serial))
1109 dbus_message_unref (message);
1113 /* send our message */
1114 bus_test_run_clients_loop (SEND_PENDING (connection));
1116 dbus_message_unref (message);
1119 dbus_connection_ref (connection); /* because we may get disconnected */
1120 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1122 if (!dbus_connection_get_is_connected (connection))
1124 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1126 dbus_connection_unref (connection);
1131 dbus_connection_unref (connection);
1133 message = pop_message_waiting_for_memory (connection);
1134 if (message == NULL)
1136 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1137 "GetConnectionUnixUser", serial, connection);
1141 verbose_message_received (connection, message);
1143 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1145 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1147 ; /* good, this is a valid response */
1151 warn_unexpected (connection, message, "not this error");
1158 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1160 ; /* good, expected */
1164 warn_unexpected (connection, message,
1165 "method_return for GetConnectionUnixUser");
1172 if (!dbus_message_get_args (message, &error,
1173 DBUS_TYPE_UINT32, &uid,
1176 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1178 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1179 dbus_error_free (&error);
1180 _dbus_wait_for_memory ();
1181 goto retry_get_property;
1185 _dbus_assert (dbus_error_is_set (&error));
1186 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1192 if (!check_no_leftovers (context))
1198 dbus_error_free (&error);
1201 dbus_message_unref (message);
1206 /* returns TRUE if the correct thing happens,
1207 * but the correct thing may include OOM errors.
1210 check_get_connection_unix_process_id (BusContext *context,
1211 DBusConnection *connection)
1213 DBusMessage *message;
1214 dbus_uint32_t serial;
1217 const char *base_service_name;
1221 dbus_error_init (&error);
1224 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1226 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1228 DBUS_INTERFACE_DBUS,
1229 "GetConnectionUnixProcessID");
1231 if (message == NULL)
1234 base_service_name = dbus_bus_get_unique_name (connection);
1236 if (!dbus_message_append_args (message,
1237 DBUS_TYPE_STRING, &base_service_name,
1240 dbus_message_unref (message);
1244 if (!dbus_connection_send (connection, message, &serial))
1246 dbus_message_unref (message);
1250 /* send our message */
1251 bus_test_run_clients_loop (SEND_PENDING (connection));
1253 dbus_message_unref (message);
1256 dbus_connection_ref (connection); /* because we may get disconnected */
1257 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1259 if (!dbus_connection_get_is_connected (connection))
1261 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1263 dbus_connection_unref (connection);
1268 dbus_connection_unref (connection);
1270 message = pop_message_waiting_for_memory (connection);
1271 if (message == NULL)
1273 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1274 "GetConnectionUnixProcessID", serial, connection);
1278 verbose_message_received (connection, message);
1280 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1282 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1284 ; /* good, this is a valid response */
1287 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1289 /* We are expecting this error, since we know in the test suite we aren't
1290 * talking to a client running on UNIX
1292 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1297 warn_unexpected (connection, message, "not this error");
1305 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1308 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1310 ; /* good, expected */
1314 warn_unexpected (connection, message,
1315 "method_return for GetConnectionUnixProcessID");
1322 if (!dbus_message_get_args (message, &error,
1323 DBUS_TYPE_UINT32, &pid,
1326 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1328 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1329 dbus_error_free (&error);
1330 _dbus_wait_for_memory ();
1331 goto retry_get_property;
1335 _dbus_assert (dbus_error_is_set (&error));
1336 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1342 /* test if returned pid is the same as our own pid
1344 * @todo It would probably be good to restructure the tests
1345 * in a way so our parent is the bus that we're testing
1346 * cause then we can test that the pid returned matches
1349 if (pid != (dbus_uint32_t) _dbus_getpid ())
1351 _dbus_assert (dbus_error_is_set (&error));
1352 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1356 #endif /* !DBUS_WIN */
1359 if (!check_no_leftovers (context))
1365 dbus_error_free (&error);
1368 dbus_message_unref (message);
1373 /* returns TRUE if the correct thing happens,
1374 * but the correct thing may include OOM errors.
1377 check_add_match_all (BusContext *context,
1378 DBusConnection *connection)
1380 DBusMessage *message;
1382 dbus_uint32_t serial;
1384 const char *empty = "";
1387 dbus_error_init (&error);
1390 _dbus_verbose ("check_add_match_all for %p\n", connection);
1392 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1394 DBUS_INTERFACE_DBUS,
1397 if (message == NULL)
1400 /* empty string match rule matches everything */
1401 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1404 dbus_message_unref (message);
1408 if (!dbus_connection_send (connection, message, &serial))
1410 dbus_message_unref (message);
1414 dbus_message_unref (message);
1417 dbus_connection_ref (connection); /* because we may get disconnected */
1419 /* send our message */
1420 bus_test_run_clients_loop (SEND_PENDING (connection));
1422 if (!dbus_connection_get_is_connected (connection))
1424 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1426 dbus_connection_unref (connection);
1431 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1433 if (!dbus_connection_get_is_connected (connection))
1435 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1437 dbus_connection_unref (connection);
1442 dbus_connection_unref (connection);
1444 message = pop_message_waiting_for_memory (connection);
1445 if (message == NULL)
1447 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1448 "AddMatch", serial, connection);
1452 verbose_message_received (connection, message);
1454 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1456 _dbus_warn ("Message has wrong sender %s\n",
1457 dbus_message_get_sender (message) ?
1458 dbus_message_get_sender (message) : "(none)");
1462 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1464 if (dbus_message_is_error (message,
1465 DBUS_ERROR_NO_MEMORY))
1467 ; /* good, this is a valid response */
1471 warn_unexpected (connection, message, "not this error");
1478 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1480 ; /* good, expected */
1481 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1485 warn_unexpected (connection, message, "method return for AddMatch");
1491 if (!check_no_leftovers (context))
1497 dbus_error_free (&error);
1500 dbus_message_unref (message);
1505 /* returns TRUE if the correct thing happens,
1506 * but the correct thing may include OOM errors.
1509 check_hello_connection (BusContext *context)
1511 DBusConnection *connection;
1514 dbus_error_init (&error);
1516 connection = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
1517 if (connection == NULL)
1519 _DBUS_ASSERT_ERROR_IS_SET (&error);
1520 dbus_error_free (&error);
1524 if (!bus_setup_debug_client (connection))
1526 dbus_connection_close (connection);
1527 dbus_connection_unref (connection);
1531 spin_connection_until_authenticated (context, connection);
1533 if (!check_hello_message (context, connection))
1536 if (dbus_bus_get_unique_name (connection) == NULL)
1538 /* We didn't successfully register, so we can't
1539 * do the usual kill_client_connection() checks
1541 kill_client_connection_unchecked (connection);
1545 if (!check_add_match_all (context, connection))
1548 kill_client_connection (context, connection);
1554 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1556 /* returns TRUE if the correct thing happens,
1557 * but the correct thing may include OOM errors.
1560 check_nonexistent_service_no_auto_start (BusContext *context,
1561 DBusConnection *connection)
1563 DBusMessage *message;
1564 dbus_uint32_t serial;
1566 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1567 dbus_uint32_t flags;
1569 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1571 DBUS_INTERFACE_DBUS,
1572 "StartServiceByName");
1574 if (message == NULL)
1577 dbus_message_set_auto_start (message, FALSE);
1580 if (!dbus_message_append_args (message,
1581 DBUS_TYPE_STRING, &nonexistent,
1582 DBUS_TYPE_UINT32, &flags,
1585 dbus_message_unref (message);
1589 if (!dbus_connection_send (connection, message, &serial))
1591 dbus_message_unref (message);
1595 dbus_message_unref (message);
1598 bus_test_run_everything (context);
1599 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1600 bus_test_run_everything (context);
1602 if (!dbus_connection_get_is_connected (connection))
1604 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1610 message = pop_message_waiting_for_memory (connection);
1611 if (message == NULL)
1613 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1614 "StartServiceByName", serial, connection);
1618 verbose_message_received (connection, message);
1620 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1622 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1624 _dbus_warn ("Message has wrong sender %s\n",
1625 dbus_message_get_sender (message) ?
1626 dbus_message_get_sender (message) : "(none)");
1630 if (dbus_message_is_error (message,
1631 DBUS_ERROR_NO_MEMORY))
1633 ; /* good, this is a valid response */
1635 else if (dbus_message_is_error (message,
1636 DBUS_ERROR_SERVICE_UNKNOWN))
1638 ; /* good, this is expected also */
1642 warn_unexpected (connection, message, "not this error");
1648 _dbus_warn ("Did not expect to successfully activate %s\n",
1649 NONEXISTENT_SERVICE_NAME);
1657 dbus_message_unref (message);
1662 /* returns TRUE if the correct thing happens,
1663 * but the correct thing may include OOM errors.
1666 check_nonexistent_service_auto_start (BusContext *context,
1667 DBusConnection *connection)
1669 DBusMessage *message;
1670 dbus_uint32_t serial;
1673 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1674 "/org/freedesktop/TestSuite",
1675 "org.freedesktop.TestSuite",
1678 if (message == NULL)
1681 if (!dbus_connection_send (connection, message, &serial))
1683 dbus_message_unref (message);
1687 dbus_message_unref (message);
1690 bus_test_run_everything (context);
1691 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1692 bus_test_run_everything (context);
1694 if (!dbus_connection_get_is_connected (connection))
1696 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1702 message = pop_message_waiting_for_memory (connection);
1704 if (message == NULL)
1706 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1707 "Echo message (auto activation)", serial, connection);
1711 verbose_message_received (connection, message);
1713 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1715 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1717 _dbus_warn ("Message has wrong sender %s\n",
1718 dbus_message_get_sender (message) ?
1719 dbus_message_get_sender (message) : "(none)");
1723 if (dbus_message_is_error (message,
1724 DBUS_ERROR_NO_MEMORY))
1726 ; /* good, this is a valid response */
1728 else if (dbus_message_is_error (message,
1729 DBUS_ERROR_SERVICE_UNKNOWN))
1731 ; /* good, this is expected also */
1735 warn_unexpected (connection, message, "not this error");
1741 _dbus_warn ("Did not expect to successfully activate %s\n",
1742 NONEXISTENT_SERVICE_NAME);
1750 dbus_message_unref (message);
1756 check_base_service_activated (BusContext *context,
1757 DBusConnection *connection,
1758 DBusMessage *initial_message,
1759 const char **base_service_p)
1761 DBusMessage *message;
1764 const char *base_service, *base_service_from_bus, *old_owner;
1768 dbus_error_init (&error);
1769 base_service = NULL;
1771 base_service_from_bus = NULL;
1773 message = initial_message;
1774 dbus_message_ref (message);
1776 if (dbus_message_is_signal (message,
1777 DBUS_INTERFACE_DBUS,
1778 "NameOwnerChanged"))
1780 CheckServiceOwnerChangedData socd;
1782 reget_service_name_arg:
1783 base_service = NULL;
1785 base_service_from_bus = NULL;
1787 if (!dbus_message_get_args (message, &error,
1788 DBUS_TYPE_STRING, &base_service,
1789 DBUS_TYPE_STRING, &old_owner,
1790 DBUS_TYPE_STRING, &base_service_from_bus,
1793 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1795 dbus_error_free (&error);
1796 _dbus_wait_for_memory ();
1797 goto reget_service_name_arg;
1801 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1802 "NameOwnerChanged (creation)",
1808 if (*base_service != ':')
1810 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1815 if (strcmp (base_service, base_service_from_bus) != 0)
1817 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1818 base_service, base_service_from_bus);
1824 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1829 socd.expected_kind = SERVICE_CREATED;
1830 socd.expected_service_name = base_service;
1831 socd.failed = FALSE;
1832 socd.skip_connection = connection;
1833 bus_test_clients_foreach (check_service_owner_changed_foreach,
1841 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1847 *base_service_p = base_service;
1853 dbus_message_unref (message);
1854 dbus_error_free (&error);
1860 check_service_activated (BusContext *context,
1861 DBusConnection *connection,
1862 const char *activated_name,
1863 const char *base_service_name,
1864 DBusMessage *initial_message)
1866 DBusMessage *message;
1869 dbus_uint32_t activation_result;
1873 dbus_error_init (&error);
1875 message = initial_message;
1876 dbus_message_ref (message);
1878 if (dbus_message_is_signal (message,
1879 DBUS_INTERFACE_DBUS,
1880 "NameOwnerChanged"))
1882 CheckServiceOwnerChangedData socd;
1883 const char *service_name, *base_service_from_bus, *old_owner;
1885 reget_service_name_arg:
1886 service_name = NULL;
1888 base_service_from_bus = NULL;
1890 if (!dbus_message_get_args (message, &error,
1891 DBUS_TYPE_STRING, &service_name,
1892 DBUS_TYPE_STRING, &old_owner,
1893 DBUS_TYPE_STRING, &base_service_from_bus,
1896 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1898 dbus_error_free (&error);
1899 _dbus_wait_for_memory ();
1900 goto reget_service_name_arg;
1904 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1905 "NameOwnerChanged (creation)",
1911 if (strcmp (service_name, activated_name) != 0)
1913 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1914 activated_name, service_name);
1918 if (strcmp (base_service_name, base_service_from_bus) != 0)
1920 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1921 base_service_from_bus, base_service_name);
1927 _dbus_warn ("expected a %s, got a %s\n",
1928 "NameOwnerChanged (creation)",
1929 "NameOwnerChanged (change)");
1933 socd.expected_kind = SERVICE_CREATED;
1934 socd.skip_connection = connection;
1935 socd.failed = FALSE;
1936 socd.expected_service_name = service_name;
1937 bus_test_clients_foreach (check_service_owner_changed_foreach,
1943 dbus_message_unref (message);
1944 service_name = NULL;
1946 base_service_from_bus = NULL;
1948 message = pop_message_waiting_for_memory (connection);
1949 if (message == NULL)
1951 _dbus_warn ("Expected a reply to %s, got nothing\n",
1952 "StartServiceByName");
1958 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1963 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1965 warn_unexpected (connection, message, "reply to StartServiceByName");
1970 activation_result = 0;
1971 if (!dbus_message_get_args (message, &error,
1972 DBUS_TYPE_UINT32, &activation_result,
1975 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1977 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1978 "StartServiceByName", error.message);
1982 dbus_error_free (&error);
1986 if (activation_result == DBUS_START_REPLY_SUCCESS)
1988 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1992 _dbus_warn ("Activation result was %u, no good.\n",
1998 dbus_message_unref (message);
2001 if (!check_no_leftovers (context))
2003 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2011 dbus_message_unref (message);
2012 dbus_error_free (&error);
2018 check_service_auto_activated (BusContext *context,
2019 DBusConnection *connection,
2020 const char *activated_name,
2021 const char *base_service_name,
2022 DBusMessage *initial_message)
2024 DBusMessage *message;
2030 dbus_error_init (&error);
2032 message = initial_message;
2033 dbus_message_ref (message);
2035 if (dbus_message_is_signal (message,
2036 DBUS_INTERFACE_DBUS,
2037 "NameOwnerChanged"))
2039 const char *service_name;
2040 CheckServiceOwnerChangedData socd;
2042 reget_service_name_arg:
2043 if (!dbus_message_get_args (message, &error,
2044 DBUS_TYPE_STRING, &service_name,
2047 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2049 dbus_error_free (&error);
2050 _dbus_wait_for_memory ();
2051 goto reget_service_name_arg;
2055 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2058 dbus_error_free (&error);
2063 if (strcmp (service_name, activated_name) != 0)
2065 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2066 activated_name, service_name);
2070 socd.expected_kind = SERVICE_CREATED;
2071 socd.expected_service_name = service_name;
2072 socd.failed = FALSE;
2073 socd.skip_connection = connection;
2074 bus_test_clients_foreach (check_service_owner_changed_foreach,
2080 /* Note that this differs from regular activation in that we don't get a
2081 * reply to ActivateService here.
2084 dbus_message_unref (message);
2086 service_name = NULL;
2090 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2099 dbus_message_unref (message);
2105 check_service_deactivated (BusContext *context,
2106 DBusConnection *connection,
2107 const char *activated_name,
2108 const char *base_service)
2111 CheckServiceOwnerChangedData socd;
2115 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2116 * service and the activated_name. The base service
2117 * notification is required to come last.
2119 socd.expected_kind = SERVICE_DELETED;
2120 socd.expected_service_name = activated_name;
2121 socd.failed = FALSE;
2122 socd.skip_connection = NULL;
2123 bus_test_clients_foreach (check_service_owner_changed_foreach,
2129 socd.expected_kind = SERVICE_DELETED;
2130 socd.expected_service_name = base_service;
2131 socd.failed = FALSE;
2132 socd.skip_connection = NULL;
2133 bus_test_clients_foreach (check_service_owner_changed_foreach,
2146 check_send_exit_to_service (BusContext *context,
2147 DBusConnection *connection,
2148 const char *service_name,
2149 const char *base_service)
2151 dbus_bool_t got_error;
2152 DBusMessage *message;
2153 dbus_uint32_t serial;
2156 _dbus_verbose ("Sending exit message to the test service\n");
2160 /* Kill off the test service by sending it a quit message */
2161 message = dbus_message_new_method_call (service_name,
2162 "/org/freedesktop/TestSuite",
2163 "org.freedesktop.TestSuite",
2166 if (message == NULL)
2168 /* Do this again; we still need the service to exit... */
2169 if (!check_send_exit_to_service (context, connection,
2170 service_name, base_service))
2176 if (!dbus_connection_send (connection, message, &serial))
2178 dbus_message_unref (message);
2180 /* Do this again; we still need the service to exit... */
2181 if (!check_send_exit_to_service (context, connection,
2182 service_name, base_service))
2188 dbus_message_unref (message);
2192 bus_test_run_clients_loop (SEND_PENDING (connection));
2194 /* read it in and write it out to test service */
2195 bus_test_run_bus_loop (context, FALSE);
2197 /* see if we got an error during message bus dispatching */
2198 bus_test_run_clients_loop (FALSE);
2199 message = borrow_message_waiting_for_memory (connection);
2200 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2203 dbus_connection_return_message (connection, message);
2209 /* If no error, wait for the test service to exit */
2210 block_connection_until_message_from_bus (context, connection, "test service to exit");
2212 bus_test_run_everything (context);
2217 message = pop_message_waiting_for_memory (connection);
2218 _dbus_assert (message != NULL);
2220 if (dbus_message_get_reply_serial (message) != serial)
2222 warn_unexpected (connection, message,
2223 "error with the correct reply serial");
2227 if (!dbus_message_is_error (message,
2228 DBUS_ERROR_NO_MEMORY))
2230 warn_unexpected (connection, message,
2231 "a no memory error from asking test service to exit");
2235 _dbus_verbose ("Got error %s when asking test service to exit\n",
2236 dbus_message_get_error_name (message));
2238 /* Do this again; we still need the service to exit... */
2239 if (!check_send_exit_to_service (context, connection,
2240 service_name, base_service))
2245 if (!check_service_deactivated (context, connection,
2246 service_name, base_service))
2249 /* Should now have a NoReply error from the Exit() method
2250 * call; it should have come after all the deactivation
2253 message = pop_message_waiting_for_memory (connection);
2255 if (message == NULL)
2257 warn_unexpected (connection, NULL,
2258 "reply to Exit() method call");
2261 if (!dbus_message_is_error (message,
2262 DBUS_ERROR_NO_REPLY))
2264 warn_unexpected (connection, message,
2265 "NoReply error from Exit() method call");
2269 if (dbus_message_get_reply_serial (message) != serial)
2271 warn_unexpected (connection, message,
2272 "error with the correct reply serial");
2276 _dbus_verbose ("Got error %s after test service exited\n",
2277 dbus_message_get_error_name (message));
2279 if (!check_no_leftovers (context))
2281 _dbus_warn ("Messages were left over after %s\n",
2282 _DBUS_FUNCTION_NAME);
2291 dbus_message_unref (message);
2297 check_got_error (BusContext *context,
2298 DBusConnection *connection,
2299 const char *first_error_name,
2302 DBusMessage *message;
2305 dbus_bool_t error_found;
2306 const char *error_name;
2310 message = pop_message_waiting_for_memory (connection);
2311 if (message == NULL)
2313 _dbus_warn ("Did not get an expected error\n");
2317 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2319 warn_unexpected (connection, message, "an error");
2324 error_found = FALSE;
2326 va_start (ap, first_error_name);
2327 error_name = first_error_name;
2328 while (error_name != NULL)
2330 if (dbus_message_is_error (message, error_name))
2335 error_name = va_arg (ap, char*);
2341 _dbus_warn ("Expected error %s or other, got %s instead\n",
2343 dbus_message_get_error_name (message));
2351 dbus_message_unref (message);
2358 GOT_SERVICE_CREATED,
2359 GOT_SERVICE_DELETED,
2364 static GotServiceInfo
2365 check_got_service_info (DBusMessage *message)
2367 GotServiceInfo message_kind;
2369 if (dbus_message_is_signal (message,
2370 DBUS_INTERFACE_DBUS,
2371 "NameOwnerChanged"))
2374 const char *service_name, *old_owner, *new_owner;
2375 dbus_error_init (&error);
2377 reget_service_info_data:
2378 service_name = NULL;
2382 dbus_message_get_args (message, &error,
2383 DBUS_TYPE_STRING, &service_name,
2384 DBUS_TYPE_STRING, &old_owner,
2385 DBUS_TYPE_STRING, &new_owner,
2387 if (dbus_error_is_set (&error))
2389 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2391 dbus_error_free (&error);
2392 goto reget_service_info_data;
2396 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2397 message_kind = GOT_SOMETHING_ELSE;
2400 else if (!old_owner[0])
2401 message_kind = GOT_SERVICE_CREATED;
2402 else if (!new_owner[0])
2403 message_kind = GOT_SERVICE_DELETED;
2405 message_kind = GOT_SOMETHING_ELSE;
2407 dbus_error_free (&error);
2409 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2410 message_kind = GOT_ERROR;
2412 message_kind = GOT_SOMETHING_ELSE;
2414 return message_kind;
2417 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2419 /* returns TRUE if the correct thing happens,
2420 * but the correct thing may include OOM errors.
2423 check_existent_service_no_auto_start (BusContext *context,
2424 DBusConnection *connection)
2426 DBusMessage *message;
2427 DBusMessage *base_service_message;
2428 const char *base_service;
2429 dbus_uint32_t serial;
2431 const char *existent = EXISTENT_SERVICE_NAME;
2432 dbus_uint32_t flags;
2434 base_service_message = NULL;
2436 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2438 DBUS_INTERFACE_DBUS,
2439 "StartServiceByName");
2441 if (message == NULL)
2444 dbus_message_set_auto_start (message, FALSE);
2447 if (!dbus_message_append_args (message,
2448 DBUS_TYPE_STRING, &existent,
2449 DBUS_TYPE_UINT32, &flags,
2452 dbus_message_unref (message);
2456 if (!dbus_connection_send (connection, message, &serial))
2458 dbus_message_unref (message);
2462 dbus_message_unref (message);
2465 bus_test_run_everything (context);
2467 /* now wait for the message bus to hear back from the activated
2470 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2472 bus_test_run_everything (context);
2474 if (!dbus_connection_get_is_connected (connection))
2476 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2482 message = pop_message_waiting_for_memory (connection);
2483 if (message == NULL)
2485 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2486 "StartServiceByName", serial, connection);
2490 verbose_message_received (connection, message);
2491 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2493 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2495 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2497 _dbus_warn ("Message has wrong sender %s\n",
2498 dbus_message_get_sender (message) ?
2499 dbus_message_get_sender (message) : "(none)");
2503 if (dbus_message_is_error (message,
2504 DBUS_ERROR_NO_MEMORY))
2506 ; /* good, this is a valid response */
2508 else if (dbus_message_is_error (message,
2509 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2510 dbus_message_is_error (message,
2511 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2512 dbus_message_is_error (message,
2513 DBUS_ERROR_SPAWN_EXEC_FAILED))
2515 ; /* good, this is expected also */
2519 _dbus_warn ("Did not expect error %s\n",
2520 dbus_message_get_error_name (message));
2526 GotServiceInfo message_kind;
2528 if (!check_base_service_activated (context, connection,
2529 message, &base_service))
2532 base_service_message = message;
2535 /* We may need to block here for the test service to exit or finish up */
2536 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2538 message = dbus_connection_borrow_message (connection);
2539 if (message == NULL)
2541 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2545 message_kind = check_got_service_info (message);
2547 dbus_connection_return_message (connection, message);
2550 switch (message_kind)
2552 case GOT_SOMETHING_ELSE:
2553 _dbus_warn ("Unexpected message after ActivateService "
2554 "(should be an error or a service announcement");
2558 if (!check_got_error (context, connection,
2559 DBUS_ERROR_SPAWN_CHILD_EXITED,
2560 DBUS_ERROR_NO_MEMORY,
2563 /* A service deleted should be coming along now after this error.
2564 * We can also get the error *after* the service deleted.
2569 case GOT_SERVICE_DELETED:
2571 /* The service started up and got a base address, but then
2572 * failed to register under EXISTENT_SERVICE_NAME
2574 CheckServiceOwnerChangedData socd;
2576 socd.expected_kind = SERVICE_DELETED;
2577 socd.expected_service_name = base_service;
2578 socd.failed = FALSE;
2579 socd.skip_connection = NULL;
2581 bus_test_clients_foreach (check_service_owner_changed_foreach,
2587 /* Now we should get an error about the service exiting
2588 * if we didn't get it before.
2590 if (message_kind != GOT_ERROR)
2592 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2594 /* and process everything again */
2595 bus_test_run_everything (context);
2597 if (!check_got_error (context, connection,
2598 DBUS_ERROR_SPAWN_CHILD_EXITED,
2599 DBUS_ERROR_NO_MEMORY,
2606 case GOT_SERVICE_CREATED:
2607 message = pop_message_waiting_for_memory (connection);
2608 if (message == NULL)
2610 _dbus_warn ("Failed to pop message we just put back! "
2611 "should have been a NameOwnerChanged (creation)\n");
2615 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2616 base_service, message))
2619 dbus_message_unref (message);
2622 if (!check_no_leftovers (context))
2624 _dbus_warn ("Messages were left over after successful activation\n");
2628 if (!check_send_exit_to_service (context, connection,
2629 EXISTENT_SERVICE_NAME, base_service))
2640 dbus_message_unref (message);
2642 if (base_service_message)
2643 dbus_message_unref (base_service_message);
2648 #ifndef DBUS_WIN_FIXME
2649 /* returns TRUE if the correct thing happens,
2650 * but the correct thing may include OOM errors.
2653 check_segfault_service_no_auto_start (BusContext *context,
2654 DBusConnection *connection)
2656 DBusMessage *message;
2657 dbus_uint32_t serial;
2659 const char *segv_service;
2660 dbus_uint32_t flags;
2662 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2664 DBUS_INTERFACE_DBUS,
2665 "StartServiceByName");
2667 if (message == NULL)
2670 dbus_message_set_auto_start (message, FALSE);
2672 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2674 if (!dbus_message_append_args (message,
2675 DBUS_TYPE_STRING, &segv_service,
2676 DBUS_TYPE_UINT32, &flags,
2679 dbus_message_unref (message);
2683 if (!dbus_connection_send (connection, message, &serial))
2685 dbus_message_unref (message);
2689 dbus_message_unref (message);
2692 bus_test_run_everything (context);
2693 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2694 bus_test_run_everything (context);
2696 if (!dbus_connection_get_is_connected (connection))
2698 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2704 message = pop_message_waiting_for_memory (connection);
2705 if (message == NULL)
2707 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2708 "StartServiceByName", serial, connection);
2712 verbose_message_received (connection, message);
2714 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2716 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2718 _dbus_warn ("Message has wrong sender %s\n",
2719 dbus_message_get_sender (message) ?
2720 dbus_message_get_sender (message) : "(none)");
2724 if (dbus_message_is_error (message,
2725 DBUS_ERROR_NO_MEMORY))
2727 ; /* good, this is a valid response */
2729 else if (dbus_message_is_error (message,
2732 const char *servicehelper;
2733 servicehelper = bus_context_get_servicehelper (context);
2734 /* make sure this only happens with the launch helper */
2735 _dbus_assert (servicehelper != NULL);
2737 else if (dbus_message_is_error (message,
2738 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2740 ; /* good, this is expected also */
2744 warn_unexpected (connection, message, "not this error");
2751 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2759 dbus_message_unref (message);
2765 /* returns TRUE if the correct thing happens,
2766 * but the correct thing may include OOM errors.
2769 check_segfault_service_auto_start (BusContext *context,
2770 DBusConnection *connection)
2772 DBusMessage *message;
2773 dbus_uint32_t serial;
2776 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2777 "/org/freedesktop/TestSuite",
2778 "org.freedesktop.TestSuite",
2781 if (message == NULL)
2784 if (!dbus_connection_send (connection, message, &serial))
2786 dbus_message_unref (message);
2790 dbus_message_unref (message);
2793 bus_test_run_everything (context);
2794 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2795 bus_test_run_everything (context);
2797 if (!dbus_connection_get_is_connected (connection))
2799 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2805 message = pop_message_waiting_for_memory (connection);
2806 if (message == NULL)
2808 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2809 "Echo message (auto activation)", serial, connection);
2813 verbose_message_received (connection, message);
2815 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2817 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2819 _dbus_warn ("Message has wrong sender %s\n",
2820 dbus_message_get_sender (message) ?
2821 dbus_message_get_sender (message) : "(none)");
2825 if (dbus_message_is_error (message,
2826 DBUS_ERROR_NO_MEMORY))
2828 ; /* good, this is a valid response */
2830 else if (dbus_message_is_error (message,
2831 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2833 ; /* good, this is expected also */
2837 warn_unexpected (connection, message, "not this error");
2844 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2852 dbus_message_unref (message);
2858 #define TEST_ECHO_MESSAGE "Test echo message"
2859 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2861 /* returns TRUE if the correct thing happens,
2862 * but the correct thing may include OOM errors.
2865 check_existent_hello_from_self (BusContext *context,
2866 DBusConnection *connection)
2868 DBusMessage *message;
2869 dbus_uint32_t serial;
2872 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2873 "/org/freedesktop/TestSuite",
2874 "org.freedesktop.TestSuite",
2875 "RunHelloFromSelf");
2877 if (message == NULL)
2880 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2881 if (!dbus_message_append_args (message,
2882 DBUS_TYPE_STRING, &text,
2885 dbus_message_unref (message);
2889 if (!dbus_connection_send (connection, message, &serial))
2891 dbus_message_unref (message);
2895 dbus_message_unref (message);
2898 bus_test_run_everything (context);
2900 /* Note: if this test is run in OOM mode, it will block when the bus
2901 * doesn't send a reply due to OOM.
2903 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2905 message = pop_message_waiting_for_memory (connection);
2906 if (message == NULL)
2908 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2912 if (dbus_message_get_reply_serial (message) != serial)
2914 _dbus_warn ("Wrong reply serial\n");
2915 dbus_message_unref (message);
2919 dbus_message_unref (message);
2925 /* returns TRUE if the correct thing happens,
2926 * but the correct thing may include OOM errors.
2929 check_existent_ping (BusContext *context,
2930 DBusConnection *connection)
2932 DBusMessage *message;
2933 dbus_uint32_t serial;
2934 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2935 "/org/freedesktop/TestSuite",
2936 "org.freedesktop.DBus.Peer",
2939 if (message == NULL)
2942 if (!dbus_connection_send (connection, message, &serial))
2944 dbus_message_unref (message);
2948 dbus_message_unref (message);
2951 bus_test_run_everything (context);
2953 /* Note: if this test is run in OOM mode, it will block when the bus
2954 * doesn't send a reply due to OOM.
2956 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2958 message = pop_message_waiting_for_memory (connection);
2959 if (message == NULL)
2961 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2965 if (dbus_message_get_reply_serial (message) != serial)
2967 _dbus_warn ("Wrong reply serial\n");
2968 dbus_message_unref (message);
2972 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2974 _dbus_warn ("Unexpected message return during Ping\n");
2975 dbus_message_unref (message);
2979 dbus_message_unref (message);
2985 /* returns TRUE if the correct thing happens,
2986 * but the correct thing may include OOM errors.
2989 check_existent_get_machine_id (BusContext *context,
2990 DBusConnection *connection)
2992 DBusMessage *message;
2993 dbus_uint32_t serial;
2994 const char *machine_id;
2996 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2997 "/org/freedesktop/TestSuite",
2998 "org.freedesktop.DBus.Peer",
3001 if (message == NULL)
3004 if (!dbus_connection_send (connection, message, &serial))
3006 dbus_message_unref (message);
3010 dbus_message_unref (message);
3013 bus_test_run_everything (context);
3015 /* Note: if this test is run in OOM mode, it will block when the bus
3016 * doesn't send a reply due to OOM.
3018 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3020 message = pop_message_waiting_for_memory (connection);
3021 if (message == NULL)
3023 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3027 if (dbus_message_get_reply_serial (message) != serial)
3029 _dbus_warn ("Wrong reply serial\n");
3030 dbus_message_unref (message);
3034 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3036 _dbus_warn ("Unexpected message return during GetMachineId\n");
3037 dbus_message_unref (message);
3042 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3044 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3045 dbus_message_unref (message);
3049 if (machine_id == NULL || strlen (machine_id) != 32)
3051 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3052 dbus_message_unref (message);
3056 /* We can't check that the machine id is correct because during make check it is
3057 * just made up for each process separately
3060 dbus_message_unref (message);
3066 /* returns TRUE if the correct thing happens,
3067 * but the correct thing may include OOM errors.
3070 check_existent_service_auto_start (BusContext *context,
3071 DBusConnection *connection)
3073 DBusMessage *message;
3074 DBusMessage *base_service_message;
3075 dbus_uint32_t serial;
3077 const char *base_service;
3080 base_service_message = NULL;
3082 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3083 "/org/freedesktop/TestSuite",
3084 "org.freedesktop.TestSuite",
3087 if (message == NULL)
3090 text = TEST_ECHO_MESSAGE;
3091 if (!dbus_message_append_args (message,
3092 DBUS_TYPE_STRING, &text,
3095 dbus_message_unref (message);
3099 if (!dbus_connection_send (connection, message, &serial))
3101 dbus_message_unref (message);
3105 dbus_message_unref (message);
3108 bus_test_run_everything (context);
3110 /* now wait for the message bus to hear back from the activated
3113 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3114 bus_test_run_everything (context);
3116 if (!dbus_connection_get_is_connected (connection))
3118 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3124 message = pop_message_waiting_for_memory (connection);
3125 if (message == NULL)
3127 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3128 serial, connection);
3132 verbose_message_received (connection, message);
3133 _dbus_verbose (" (after sending %s)\n", "auto start");
3135 /* we should get zero or two ServiceOwnerChanged signals */
3136 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3138 GotServiceInfo message_kind;
3140 if (!check_base_service_activated (context, connection,
3141 message, &base_service))
3144 base_service_message = message;
3147 /* We may need to block here for the test service to exit or finish up */
3148 block_connection_until_message_from_bus (context, connection, "service to exit");
3150 /* Should get a service creation notification for the activated
3151 * service name, or a service deletion on the base service name
3153 message = dbus_connection_borrow_message (connection);
3154 if (message == NULL)
3156 _dbus_warn ("No message after auto activation "
3157 "(should be a service announcement)\n");
3158 dbus_connection_return_message (connection, message);
3163 message_kind = check_got_service_info (message);
3165 dbus_connection_return_message (connection, message);
3168 switch (message_kind)
3170 case GOT_SERVICE_CREATED:
3171 message = pop_message_waiting_for_memory (connection);
3172 if (message == NULL)
3174 _dbus_warn ("Failed to pop message we just put back! "
3175 "should have been a NameOwnerChanged (creation)\n");
3179 /* Check that ServiceOwnerChanged (creation) was correctly received */
3180 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3181 base_service, message))
3184 dbus_message_unref (message);
3189 case GOT_SERVICE_DELETED:
3191 /* The service started up and got a base address, but then
3192 * failed to register under EXISTENT_SERVICE_NAME
3194 CheckServiceOwnerChangedData socd;
3196 socd.expected_kind = SERVICE_DELETED;
3197 socd.expected_service_name = base_service;
3198 socd.failed = FALSE;
3199 socd.skip_connection = NULL;
3200 bus_test_clients_foreach (check_service_owner_changed_foreach,
3210 case GOT_SOMETHING_ELSE:
3211 _dbus_warn ("Unexpected message after auto activation\n");
3216 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3217 * come the method reply (or error) from the initial method call
3220 /* Note: if this test is run in OOM mode, it will block when the bus
3221 * doesn't send a reply due to OOM.
3223 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3225 message = pop_message_waiting_for_memory (connection);
3226 if (message == NULL)
3228 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3232 if (dbus_message_get_reply_serial (message) != serial)
3234 _dbus_warn ("Wrong reply serial\n");
3238 dbus_message_unref (message);
3241 if (!check_existent_ping (context, connection))
3244 if (!check_existent_get_machine_id (context, connection))
3247 if (!check_existent_hello_from_self (context, connection))
3250 if (!check_send_exit_to_service (context, connection,
3251 EXISTENT_SERVICE_NAME,
3259 dbus_message_unref (message);
3261 if (base_service_message)
3262 dbus_message_unref (base_service_message);
3267 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3269 /* returns TRUE if the correct thing happens,
3270 * but the correct thing may include OOM errors.
3273 check_launch_service_file_missing (BusContext *context,
3274 DBusConnection *connection)
3276 DBusMessage *message;
3277 dbus_uint32_t serial;
3280 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3281 "/org/freedesktop/TestSuite",
3282 "org.freedesktop.TestSuite",
3285 if (message == NULL)
3288 if (!dbus_connection_send (connection, message, &serial))
3290 dbus_message_unref (message);
3294 dbus_message_unref (message);
3297 bus_test_run_everything (context);
3298 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3299 bus_test_run_everything (context);
3301 if (!dbus_connection_get_is_connected (connection))
3303 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3309 message = pop_message_waiting_for_memory (connection);
3310 if (message == NULL)
3312 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3313 "Echo message (auto activation)", serial, connection);
3317 verbose_message_received (connection, message);
3319 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3321 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3323 _dbus_warn ("Message has wrong sender %s\n",
3324 dbus_message_get_sender (message) ?
3325 dbus_message_get_sender (message) : "(none)");
3329 if (dbus_message_is_error (message,
3330 DBUS_ERROR_NO_MEMORY))
3332 ; /* good, this is a valid response */
3334 else if (dbus_message_is_error (message,
3335 DBUS_ERROR_SERVICE_UNKNOWN))
3337 _dbus_verbose("got service unknown\n");
3338 ; /* good, this is expected (only valid when using launch helper) */
3342 warn_unexpected (connection, message, "not this error");
3349 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3357 dbus_message_unref (message);
3362 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3364 /* returns TRUE if the correct thing happens,
3365 * but the correct thing may include OOM errors.
3368 check_launch_service_user_missing (BusContext *context,
3369 DBusConnection *connection)
3371 DBusMessage *message;
3372 dbus_uint32_t serial;
3375 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3376 "/org/freedesktop/TestSuite",
3377 "org.freedesktop.TestSuite",
3380 if (message == NULL)
3383 if (!dbus_connection_send (connection, message, &serial))
3385 dbus_message_unref (message);
3389 dbus_message_unref (message);
3392 bus_test_run_everything (context);
3393 block_connection_until_message_from_bus (context, connection,
3394 "reply to service which should fail to auto-start (missing User)");
3395 bus_test_run_everything (context);
3397 if (!dbus_connection_get_is_connected (connection))
3399 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3405 message = pop_message_waiting_for_memory (connection);
3406 if (message == NULL)
3408 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3409 "Echo message (auto activation)", serial, connection);
3413 verbose_message_received (connection, message);
3415 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3417 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3419 _dbus_warn ("Message has wrong sender %s\n",
3420 dbus_message_get_sender (message) ?
3421 dbus_message_get_sender (message) : "(none)");
3425 if (dbus_message_is_error (message,
3426 DBUS_ERROR_NO_MEMORY))
3428 ; /* good, this is a valid response */
3430 else if (dbus_message_is_error (message,
3431 DBUS_ERROR_SPAWN_FILE_INVALID))
3433 _dbus_verbose("got service file invalid\n");
3434 ; /* good, this is expected (only valid when using launch helper) */
3438 warn_unexpected (connection, message, "not this error");
3445 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3453 dbus_message_unref (message);
3458 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3460 /* returns TRUE if the correct thing happens,
3461 * but the correct thing may include OOM errors.
3464 check_launch_service_exec_missing (BusContext *context,
3465 DBusConnection *connection)
3467 DBusMessage *message;
3468 dbus_uint32_t serial;
3471 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3472 "/org/freedesktop/TestSuite",
3473 "org.freedesktop.TestSuite",
3476 if (message == NULL)
3479 if (!dbus_connection_send (connection, message, &serial))
3481 dbus_message_unref (message);
3485 dbus_message_unref (message);
3488 bus_test_run_everything (context);
3489 block_connection_until_message_from_bus (context, connection,
3490 "reply to service which should fail to auto-start (missing Exec)");
3491 bus_test_run_everything (context);
3493 if (!dbus_connection_get_is_connected (connection))
3495 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3501 message = pop_message_waiting_for_memory (connection);
3502 if (message == NULL)
3504 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3505 "Echo message (auto activation)", serial, connection);
3509 verbose_message_received (connection, message);
3511 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3513 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3515 _dbus_warn ("Message has wrong sender %s\n",
3516 dbus_message_get_sender (message) ?
3517 dbus_message_get_sender (message) : "(none)");
3521 if (dbus_message_is_error (message,
3522 DBUS_ERROR_NO_MEMORY))
3524 ; /* good, this is a valid response */
3526 else if (dbus_message_is_error (message,
3527 DBUS_ERROR_SERVICE_UNKNOWN))
3529 _dbus_verbose("could not activate as invalid service file was not added\n");
3530 ; /* good, this is expected as we shouldn't have been added to
3531 * the activation list with a missing Exec key */
3533 else if (dbus_message_is_error (message,
3534 DBUS_ERROR_SPAWN_FILE_INVALID))
3536 _dbus_verbose("got service file invalid\n");
3537 ; /* good, this is allowed, and is the message passed back from the
3542 warn_unexpected (connection, message, "not this error");
3549 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3557 dbus_message_unref (message);
3562 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3564 /* returns TRUE if the correct thing happens,
3565 * but the correct thing may include OOM errors.
3568 check_launch_service_service_missing (BusContext *context,
3569 DBusConnection *connection)
3571 DBusMessage *message;
3572 dbus_uint32_t serial;
3575 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3576 "/org/freedesktop/TestSuite",
3577 "org.freedesktop.TestSuite",
3580 if (message == NULL)
3583 if (!dbus_connection_send (connection, message, &serial))
3585 dbus_message_unref (message);
3589 dbus_message_unref (message);
3592 bus_test_run_everything (context);
3593 block_connection_until_message_from_bus (context, connection,
3594 "reply to service which should fail to auto-start (missing Service)");
3595 bus_test_run_everything (context);
3597 if (!dbus_connection_get_is_connected (connection))
3599 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3605 message = pop_message_waiting_for_memory (connection);
3606 if (message == NULL)
3608 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3609 "Echo message (auto activation)", serial, connection);
3613 verbose_message_received (connection, message);
3615 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3617 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3619 _dbus_warn ("Message has wrong sender %s\n",
3620 dbus_message_get_sender (message) ?
3621 dbus_message_get_sender (message) : "(none)");
3625 if (dbus_message_is_error (message,
3626 DBUS_ERROR_NO_MEMORY))
3628 ; /* good, this is a valid response */
3630 else if (dbus_message_is_error (message,
3631 DBUS_ERROR_SERVICE_UNKNOWN))
3633 _dbus_verbose("could not activate as invalid service file was not added\n");
3634 ; /* good, this is expected as we shouldn't have been added to
3635 * the activation list with a missing Exec key */
3637 else if (dbus_message_is_error (message,
3638 DBUS_ERROR_SPAWN_FILE_INVALID))
3640 _dbus_verbose("got service file invalid\n");
3641 ; /* good, this is allowed, and is the message passed back from the
3646 warn_unexpected (connection, message, "not this error");
3653 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3661 dbus_message_unref (message);
3666 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3668 /* returns TRUE if the correct thing happens,
3669 * but the correct thing may include OOM errors.
3672 check_shell_fail_service_auto_start (BusContext *context,
3673 DBusConnection *connection)
3675 DBusMessage *message;
3676 dbus_uint32_t serial;
3679 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3680 "/org/freedesktop/TestSuite",
3681 "org.freedesktop.TestSuite",
3684 if (message == NULL)
3687 if (!dbus_connection_send (connection, message, &serial))
3689 dbus_message_unref (message);
3693 dbus_message_unref (message);
3696 bus_test_run_everything (context);
3697 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3698 bus_test_run_everything (context);
3700 if (!dbus_connection_get_is_connected (connection))
3702 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3708 message = pop_message_waiting_for_memory (connection);
3709 if (message == NULL)
3711 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3712 "Echo message (auto activation)", serial, connection);
3716 verbose_message_received (connection, message);
3718 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3720 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3722 _dbus_warn ("Message has wrong sender %s\n",
3723 dbus_message_get_sender (message) ?
3724 dbus_message_get_sender (message) : "(none)");
3728 if (dbus_message_is_error (message,
3729 DBUS_ERROR_NO_MEMORY))
3731 ; /* good, this is a valid response */
3733 else if (dbus_message_is_error (message,
3734 DBUS_ERROR_INVALID_ARGS))
3736 _dbus_verbose("got invalid args\n");
3737 ; /* good, this is expected also */
3741 warn_unexpected (connection, message, "not this error");
3748 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3756 dbus_message_unref (message);
3761 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3763 /* returns TRUE if the correct thing happens,
3764 * but the correct thing may include OOM errors.
3767 check_shell_service_success_auto_start (BusContext *context,
3768 DBusConnection *connection)
3770 DBusMessage *message;
3771 DBusMessage *base_service_message;
3772 dbus_uint32_t serial;
3774 const char *base_service;
3775 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3777 base_service_message = NULL;
3779 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3780 "/org/freedesktop/TestSuite",
3781 "org.freedesktop.TestSuite",
3784 if (message == NULL)
3787 if (!dbus_connection_send (connection, message, &serial))
3789 dbus_message_unref (message);
3793 dbus_message_unref (message);
3796 bus_test_run_everything (context);
3798 /* now wait for the message bus to hear back from the activated
3801 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3802 bus_test_run_everything (context);
3804 if (!dbus_connection_get_is_connected (connection))
3806 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3812 message = pop_message_waiting_for_memory (connection);
3813 if (message == NULL)
3815 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3816 serial, connection);
3820 verbose_message_received (connection, message);
3821 _dbus_verbose (" (after sending %s)\n", "auto start");
3823 /* we should get zero or two ServiceOwnerChanged signals */
3824 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3826 GotServiceInfo message_kind;
3828 if (!check_base_service_activated (context, connection,
3829 message, &base_service))
3832 base_service_message = message;
3835 /* We may need to block here for the test service to exit or finish up */
3836 block_connection_until_message_from_bus (context, connection, "service to exit");
3838 /* Should get a service creation notification for the activated
3839 * service name, or a service deletion on the base service name
3841 message = dbus_connection_borrow_message (connection);
3842 if (message == NULL)
3844 _dbus_warn ("No message after auto activation "
3845 "(should be a service announcement)\n");
3846 dbus_connection_return_message (connection, message);
3851 message_kind = check_got_service_info (message);
3853 dbus_connection_return_message (connection, message);
3856 switch (message_kind)
3858 case GOT_SERVICE_CREATED:
3859 message = pop_message_waiting_for_memory (connection);
3860 if (message == NULL)
3862 _dbus_warn ("Failed to pop message we just put back! "
3863 "should have been a NameOwnerChanged (creation)\n");
3867 /* Check that ServiceOwnerChanged (creation) was correctly received */
3868 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3869 base_service, message))
3872 dbus_message_unref (message);
3877 case GOT_SERVICE_DELETED:
3879 /* The service started up and got a base address, but then
3880 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3882 CheckServiceOwnerChangedData socd;
3884 socd.expected_kind = SERVICE_DELETED;
3885 socd.expected_service_name = base_service;
3886 socd.failed = FALSE;
3887 socd.skip_connection = NULL;
3888 bus_test_clients_foreach (check_service_owner_changed_foreach,
3898 case GOT_SOMETHING_ELSE:
3899 _dbus_warn ("Unexpected message after auto activation\n");
3904 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3905 * come the method reply (or error) from the initial method call
3908 /* Note: if this test is run in OOM mode, it will block when the bus
3909 * doesn't send a reply due to OOM.
3911 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3913 message = pop_message_waiting_for_memory (connection);
3914 if (message == NULL)
3916 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3920 if (dbus_message_get_reply_serial (message) != serial)
3922 _dbus_warn ("Wrong reply serial\n");
3926 if (!dbus_message_get_args (message, NULL,
3927 DBUS_TYPE_STRING, &argv[0],
3928 DBUS_TYPE_STRING, &argv[1],
3929 DBUS_TYPE_STRING, &argv[2],
3930 DBUS_TYPE_STRING, &argv[3],
3931 DBUS_TYPE_STRING, &argv[4],
3932 DBUS_TYPE_STRING, &argv[5],
3933 DBUS_TYPE_STRING, &argv[6],
3936 _dbus_warn ("Error getting arguments from return\n");
3940 /* don't worry about arg[0] as it may be different
3941 depending on the path to the tests
3943 if (strcmp("-test", argv[1]) != 0)
3945 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3950 if (strcmp("that", argv[2]) != 0)
3952 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3957 if (strcmp("we get", argv[3]) != 0)
3959 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3964 if (strcmp("back", argv[4]) != 0)
3966 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3971 if (strcmp("--what", argv[5]) != 0)
3973 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3978 if (strcmp("we put in", argv[6]) != 0)
3980 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3981 "we put in", argv[6]);
3985 dbus_message_unref (message);
3988 if (!check_send_exit_to_service (context, connection,
3989 SHELL_SUCCESS_SERVICE_NAME,
3997 dbus_message_unref (message);
3999 if (base_service_message)
4000 dbus_message_unref (base_service_message);
4008 BusContext *context;
4012 check_oom_check1_func (void *data)
4014 Check1Data *d = data;
4016 if (! (* d->func) (d->context))
4019 if (!check_no_leftovers (d->context))
4021 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4029 check1_try_iterations (BusContext *context,
4030 const char *description,
4036 d.context = context;
4038 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4040 _dbus_assert_not_reached ("test failed");
4044 check_get_services (BusContext *context,
4045 DBusConnection *connection,
4050 DBusMessage *message;
4051 dbus_uint32_t serial;
4058 dbus_error_init (&error);
4061 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4063 DBUS_INTERFACE_DBUS,
4066 if (message == NULL)
4069 if (!dbus_connection_send (connection, message, &serial))
4071 dbus_message_unref (message);
4075 /* send our message */
4076 bus_test_run_clients_loop (SEND_PENDING (connection));
4078 dbus_message_unref (message);
4081 dbus_connection_ref (connection); /* because we may get disconnected */
4082 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4084 if (!dbus_connection_get_is_connected (connection))
4086 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4088 dbus_connection_unref (connection);
4093 dbus_connection_unref (connection);
4095 message = pop_message_waiting_for_memory (connection);
4096 if (message == NULL)
4098 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4099 method, serial, connection);
4103 verbose_message_received (connection, message);
4105 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4107 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4109 ; /* good, this is a valid response */
4113 warn_unexpected (connection, message, "not this error");
4120 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4122 ; /* good, expected */
4126 warn_unexpected (connection, message,
4127 "method_return for ListActivatableNames/ListNames");
4134 if (!dbus_message_get_args (message, &error,
4140 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4142 _dbus_verbose ("no memory to list services by %s\n", method);
4143 dbus_error_free (&error);
4144 _dbus_wait_for_memory ();
4145 goto retry_get_property;
4149 _dbus_assert (dbus_error_is_set (&error));
4150 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4159 if (!check_no_leftovers (context))
4165 dbus_error_free (&error);
4168 dbus_message_unref (message);
4173 /* returns TRUE if the correct thing happens,
4174 * but the correct thing may include OOM errors.
4177 check_list_services (BusContext *context,
4178 DBusConnection *connection)
4180 DBusMessage *message;
4181 DBusMessage *base_service_message;
4182 const char *base_service;
4183 dbus_uint32_t serial;
4185 const char *existent = EXISTENT_SERVICE_NAME;
4186 dbus_uint32_t flags;
4190 _dbus_verbose ("check_list_services for %p\n", connection);
4192 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4197 if (!_dbus_string_array_contains ((const char **)services, existent))
4199 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4203 dbus_free_string_array (services);
4205 base_service_message = NULL;
4207 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4209 DBUS_INTERFACE_DBUS,
4210 "StartServiceByName");
4212 if (message == NULL)
4215 dbus_message_set_auto_start (message, FALSE);
4218 if (!dbus_message_append_args (message,
4219 DBUS_TYPE_STRING, &existent,
4220 DBUS_TYPE_UINT32, &flags,
4223 dbus_message_unref (message);
4227 if (!dbus_connection_send (connection, message, &serial))
4229 dbus_message_unref (message);
4233 dbus_message_unref (message);
4236 bus_test_run_everything (context);
4238 /* now wait for the message bus to hear back from the activated
4241 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4243 bus_test_run_everything (context);
4245 if (!dbus_connection_get_is_connected (connection))
4247 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4253 message = pop_message_waiting_for_memory (connection);
4254 if (message == NULL)
4256 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4257 "StartServiceByName", serial, connection);
4261 verbose_message_received (connection, message);
4262 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4264 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4266 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4268 _dbus_warn ("Message has wrong sender %s\n",
4269 dbus_message_get_sender (message) ?
4270 dbus_message_get_sender (message) : "(none)");
4274 if (dbus_message_is_error (message,
4275 DBUS_ERROR_NO_MEMORY))
4277 ; /* good, this is a valid response */
4279 else if (dbus_message_is_error (message,
4280 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4281 dbus_message_is_error (message,
4282 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4283 dbus_message_is_error (message,
4284 DBUS_ERROR_SPAWN_EXEC_FAILED))
4286 ; /* good, this is expected also */
4290 _dbus_warn ("Did not expect error %s\n",
4291 dbus_message_get_error_name (message));
4297 GotServiceInfo message_kind;
4299 if (!check_base_service_activated (context, connection,
4300 message, &base_service))
4303 base_service_message = message;
4306 /* We may need to block here for the test service to exit or finish up */
4307 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4309 message = dbus_connection_borrow_message (connection);
4310 if (message == NULL)
4312 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4316 message_kind = check_got_service_info (message);
4318 dbus_connection_return_message (connection, message);
4321 switch (message_kind)
4323 case GOT_SOMETHING_ELSE:
4325 case GOT_SERVICE_DELETED:
4326 _dbus_warn ("Unexpected message after ActivateService "
4327 "(should be an error or a service announcement)\n");
4330 case GOT_SERVICE_CREATED:
4331 message = pop_message_waiting_for_memory (connection);
4332 if (message == NULL)
4334 _dbus_warn ("Failed to pop message we just put back! "
4335 "should have been a NameOwnerChanged (creation)\n");
4339 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4340 base_service, message))
4343 dbus_message_unref (message);
4346 if (!check_no_leftovers (context))
4348 _dbus_warn ("Messages were left over after successful activation\n");
4356 if (!check_get_services (context, connection, "ListNames", &services, &len))
4361 if (!_dbus_string_array_contains ((const char **)services, existent))
4363 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4367 dbus_free_string_array (services);
4369 if (!check_send_exit_to_service (context, connection,
4370 EXISTENT_SERVICE_NAME, base_service))
4377 dbus_message_unref (message);
4379 if (base_service_message)
4380 dbus_message_unref (base_service_message);
4388 BusContext *context;
4389 DBusConnection *connection;
4393 check_oom_check2_func (void *data)
4395 Check2Data *d = data;
4397 if (! (* d->func) (d->context, d->connection))
4400 if (!check_no_leftovers (d->context))
4402 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4410 check2_try_iterations (BusContext *context,
4411 DBusConnection *connection,
4412 const char *description,
4418 d.context = context;
4419 d.connection = connection;
4421 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4424 _dbus_warn ("%s failed during oom\n", description);
4425 _dbus_assert_not_reached ("test failed");
4430 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4431 const char *filename)
4436 if (!_dbus_string_init (&full))
4439 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4441 _dbus_string_free (&full);
4445 _dbus_string_init_const (&file, filename);
4447 if (!_dbus_concat_dir_and_file (&full, &file))
4449 _dbus_string_free (&full);
4453 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4454 _dbus_string_get_const_data (&full));
4456 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4458 _dbus_string_free (&full);
4464 bus_dispatch_test_conf (const DBusString *test_data_dir,
4465 const char *filename,
4466 dbus_bool_t use_launcher)
4468 BusContext *context;
4469 DBusConnection *foo;
4470 DBusConnection *bar;
4471 DBusConnection *baz;
4474 /* save the config name for the activation helper */
4475 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4476 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4478 dbus_error_init (&error);
4480 context = bus_context_new_test (test_data_dir, filename);
4481 if (context == NULL)
4484 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4486 _dbus_assert_not_reached ("could not alloc connection");
4488 if (!bus_setup_debug_client (foo))
4489 _dbus_assert_not_reached ("could not set up connection");
4491 spin_connection_until_authenticated (context, foo);
4493 if (!check_hello_message (context, foo))
4494 _dbus_assert_not_reached ("hello message failed");
4496 if (!check_double_hello_message (context, foo))
4497 _dbus_assert_not_reached ("double hello message failed");
4499 if (!check_add_match_all (context, foo))
4500 _dbus_assert_not_reached ("AddMatch message failed");
4502 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4504 _dbus_assert_not_reached ("could not alloc connection");
4506 if (!bus_setup_debug_client (bar))
4507 _dbus_assert_not_reached ("could not set up connection");
4509 spin_connection_until_authenticated (context, bar);
4511 if (!check_hello_message (context, bar))
4512 _dbus_assert_not_reached ("hello message failed");
4514 if (!check_add_match_all (context, bar))
4515 _dbus_assert_not_reached ("AddMatch message failed");
4517 baz = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4519 _dbus_assert_not_reached ("could not alloc connection");
4521 if (!bus_setup_debug_client (baz))
4522 _dbus_assert_not_reached ("could not set up connection");
4524 spin_connection_until_authenticated (context, baz);
4526 if (!check_hello_message (context, baz))
4527 _dbus_assert_not_reached ("hello message failed");
4529 if (!check_add_match_all (context, baz))
4530 _dbus_assert_not_reached ("AddMatch message failed");
4532 if (!check_get_connection_unix_user (context, baz))
4533 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4535 if (!check_get_connection_unix_process_id (context, baz))
4536 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4538 if (!check_list_services (context, baz))
4539 _dbus_assert_not_reached ("ListActivatableNames message failed");
4541 if (!check_no_leftovers (context))
4543 _dbus_warn ("Messages were left over after setting up initial connections\n");
4544 _dbus_assert_not_reached ("initial connection setup failed");
4547 check1_try_iterations (context, "create_and_hello",
4548 check_hello_connection);
4550 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4551 check_nonexistent_service_no_auto_start);
4553 #ifdef DBUS_WIN_FIXME
4554 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4556 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4557 check_segfault_service_no_auto_start);
4560 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4561 check_existent_service_no_auto_start);
4563 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4564 check_nonexistent_service_auto_start);
4567 #ifdef DBUS_WIN_FIXME
4568 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4570 /* only do the segfault test if we are not using the launcher */
4571 check2_try_iterations (context, foo, "segfault_service_auto_start",
4572 check_segfault_service_auto_start);
4575 /* only do the shell fail test if we are not using the launcher */
4576 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4577 check_shell_fail_service_auto_start);
4579 /* specific to launcher */
4581 if (!check_launch_service_file_missing (context, foo))
4582 _dbus_assert_not_reached ("did not get service file not found error");
4585 /* Note: need to resolve some issues with the testing code in order to run
4586 * this in oom (handle that we sometimes don't get replies back from the bus
4587 * when oom happens, without blocking the test).
4589 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4590 check_existent_service_auto_start);
4593 if (!check_existent_service_auto_start (context, foo))
4594 _dbus_assert_not_reached ("existent service auto start failed");
4596 if (!check_shell_service_success_auto_start (context, foo))
4597 _dbus_assert_not_reached ("shell success service auto start failed");
4599 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4601 kill_client_connection_unchecked (foo);
4602 kill_client_connection_unchecked (bar);
4603 kill_client_connection_unchecked (baz);
4605 bus_context_unref (context);
4611 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4612 const char *filename)
4614 BusContext *context;
4615 DBusConnection *foo;
4618 /* save the config name for the activation helper */
4619 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4620 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4622 dbus_error_init (&error);
4624 context = bus_context_new_test (test_data_dir, filename);
4625 if (context == NULL)
4628 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4630 _dbus_assert_not_reached ("could not alloc connection");
4632 if (!bus_setup_debug_client (foo))
4633 _dbus_assert_not_reached ("could not set up connection");
4635 spin_connection_until_authenticated (context, foo);
4637 if (!check_hello_message (context, foo))
4638 _dbus_assert_not_reached ("hello message failed");
4640 if (!check_double_hello_message (context, foo))
4641 _dbus_assert_not_reached ("double hello message failed");
4643 if (!check_add_match_all (context, foo))
4644 _dbus_assert_not_reached ("AddMatch message failed");
4646 /* this only tests the activation.c user check */
4647 if (!check_launch_service_user_missing (context, foo))
4648 _dbus_assert_not_reached ("user missing did not trigger error");
4650 /* this only tests the desktop.c exec check */
4651 if (!check_launch_service_exec_missing (context, foo))
4652 _dbus_assert_not_reached ("exec missing did not trigger error");
4654 /* this only tests the desktop.c service check */
4655 if (!check_launch_service_service_missing (context, foo))
4656 _dbus_assert_not_reached ("service missing did not trigger error");
4658 _dbus_verbose ("Disconnecting foo\n");
4660 kill_client_connection_unchecked (foo);
4662 bus_context_unref (context);
4668 bus_dispatch_test (const DBusString *test_data_dir)
4670 /* run normal activation tests */
4671 _dbus_verbose ("Normal activation tests\n");
4672 if (!bus_dispatch_test_conf (test_data_dir,
4673 "valid-config-files/debug-allow-all.conf", FALSE))
4676 /* run launch-helper activation tests */
4677 _dbus_verbose ("Launch helper activation tests\n");
4678 if (!bus_dispatch_test_conf (test_data_dir,
4679 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4682 /* run select launch-helper activation tests on broken service files */
4683 if (!bus_dispatch_test_conf_fail (test_data_dir,
4684 "valid-config-files-system/debug-allow-all-fail.conf"))
4691 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4693 BusContext *context;
4694 DBusConnection *foo;
4697 dbus_error_init (&error);
4699 /* Test SHA1 authentication */
4700 _dbus_verbose ("Testing SHA1 context\n");
4702 context = bus_context_new_test (test_data_dir,
4703 "valid-config-files/debug-allow-all-sha1.conf");
4704 if (context == NULL)
4707 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4709 _dbus_assert_not_reached ("could not alloc connection");
4711 if (!bus_setup_debug_client (foo))
4712 _dbus_assert_not_reached ("could not set up connection");
4714 spin_connection_until_authenticated (context, foo);
4716 if (!check_hello_message (context, foo))
4717 _dbus_assert_not_reached ("hello message failed");
4719 if (!check_add_match_all (context, foo))
4720 _dbus_assert_not_reached ("addmatch message failed");
4722 if (!check_no_leftovers (context))
4724 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4725 _dbus_assert_not_reached ("initial connection setup failed");
4728 check1_try_iterations (context, "create_and_hello_sha1",
4729 check_hello_connection);
4731 kill_client_connection_unchecked (foo);
4733 bus_context_unref (context);
4738 #ifdef HAVE_UNIX_FD_PASSING
4741 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4743 BusContext *context;
4744 DBusConnection *foo, *bar;
4748 int one[2], two[2], x, y, z;
4751 dbus_error_init (&error);
4753 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4754 if (context == NULL)
4755 _dbus_assert_not_reached ("could not alloc context");
4757 foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4759 _dbus_assert_not_reached ("could not alloc connection");
4761 if (!bus_setup_debug_client (foo))
4762 _dbus_assert_not_reached ("could not set up connection");
4764 spin_connection_until_authenticated (context, foo);
4766 if (!check_hello_message (context, foo))
4767 _dbus_assert_not_reached ("hello message failed");
4769 if (!check_add_match_all (context, foo))
4770 _dbus_assert_not_reached ("AddMatch message failed");
4772 bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
4774 _dbus_assert_not_reached ("could not alloc connection");
4776 if (!bus_setup_debug_client (bar))
4777 _dbus_assert_not_reached ("could not set up connection");
4779 spin_connection_until_authenticated (context, bar);
4781 if (!check_hello_message (context, bar))
4782 _dbus_assert_not_reached ("hello message failed");
4784 if (!check_add_match_all (context, bar))
4785 _dbus_assert_not_reached ("AddMatch message failed");
4787 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4788 _dbus_assert_not_reached ("could not alloc message");
4790 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4791 _dbus_assert_not_reached("Failed to allocate pipe #1");
4793 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4794 _dbus_assert_not_reached("Failed to allocate pipe #2");
4796 if (!dbus_message_append_args(m,
4797 DBUS_TYPE_UNIX_FD, one,
4798 DBUS_TYPE_UNIX_FD, two,
4799 DBUS_TYPE_UNIX_FD, two,
4801 _dbus_assert_not_reached("Failed to attach fds.");
4803 if (!_dbus_close(one[0], &error))
4804 _dbus_assert_not_reached("Failed to close pipe #1 ");
4805 if (!_dbus_close(two[0], &error))
4806 _dbus_assert_not_reached("Failed to close pipe #2 ");
4808 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4809 _dbus_assert_not_reached("Connection cannot do fd passing");
4811 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4812 _dbus_assert_not_reached("Connection cannot do fd passing");
4814 if (!dbus_connection_send (foo, m, NULL))
4815 _dbus_assert_not_reached("Failed to send fds");
4817 dbus_message_unref(m);
4819 bus_test_run_clients_loop (SEND_PENDING (foo));
4821 bus_test_run_everything (context);
4823 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4825 if (!(m = pop_message_waiting_for_memory (foo)))
4826 _dbus_assert_not_reached("Failed to receive msg");
4828 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4829 _dbus_assert_not_reached("bogus message received");
4831 dbus_message_unref(m);
4833 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4835 if (!(m = pop_message_waiting_for_memory (bar)))
4836 _dbus_assert_not_reached("Failed to receive msg");
4838 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4839 _dbus_assert_not_reached("bogus message received");
4841 if (!dbus_message_get_args(m,
4843 DBUS_TYPE_UNIX_FD, &x,
4844 DBUS_TYPE_UNIX_FD, &y,
4845 DBUS_TYPE_UNIX_FD, &z,
4847 _dbus_assert_not_reached("Failed to parse fds.");
4849 dbus_message_unref(m);
4851 if (write(x, "X", 1) != 1)
4852 _dbus_assert_not_reached("Failed to write to pipe #1");
4853 if (write(y, "Y", 1) != 1)
4854 _dbus_assert_not_reached("Failed to write to pipe #2");
4855 if (write(z, "Z", 1) != 1)
4856 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4858 if (!_dbus_close(x, &error))
4859 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4860 if (!_dbus_close(y, &error))
4861 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4862 if (!_dbus_close(z, &error))
4863 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4865 if (read(one[1], &r, 1) != 1 || r != 'X')
4866 _dbus_assert_not_reached("Failed to read value from pipe.");
4867 if (read(two[1], &r, 1) != 1 || r != 'Y')
4868 _dbus_assert_not_reached("Failed to read value from pipe.");
4869 if (read(two[1], &r, 1) != 1 || r != 'Z')
4870 _dbus_assert_not_reached("Failed to read value from pipe.");
4872 if (!_dbus_close(one[1], &error))
4873 _dbus_assert_not_reached("Failed to close pipe #1 ");
4874 if (!_dbus_close(two[1], &error))
4875 _dbus_assert_not_reached("Failed to close pipe #2 ");
4877 _dbus_verbose ("Disconnecting foo\n");
4878 kill_client_connection_unchecked (foo);
4880 _dbus_verbose ("Disconnecting bar\n");
4881 kill_client_connection_unchecked (bar);
4883 bus_context_unref (context);
4889 #endif /* DBUS_BUILD_TESTS */