1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "connection.h"
31 #include "activation.h"
36 #include <dbus/dbus-internals.h>
37 #include <dbus/dbus-misc.h>
41 #ifdef HAVE_UNIX_FD_PASSING
42 #include <dbus/dbus-sysdeps-unix.h>
46 /* This is hard-coded in the files in valid-config-files-*. We have to use
47 * the debug-pipe transport because the tests in this file require that
48 * dbus_connection_open_private() does not block. */
49 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
52 send_one_message (DBusConnection *connection,
54 DBusConnection *sender,
55 DBusConnection *addressed_recipient,
57 BusTransaction *transaction,
60 if (!bus_context_check_security_policy (context, transaction,
66 return TRUE; /* silently don't send it */
68 if (dbus_message_contains_unix_fds(message) &&
69 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
70 return TRUE; /* silently don't send it */
72 if (!bus_transaction_send (transaction,
84 bus_dispatch_matches (BusTransaction *transaction,
85 DBusConnection *sender,
86 DBusConnection *addressed_recipient,
91 BusConnections *connections;
93 BusMatchmaker *matchmaker;
97 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
99 /* sender and recipient can both be NULL for the bus driver,
100 * or for signals with no particular recipient
103 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
104 _dbus_assert (dbus_message_get_sender (message) != NULL);
106 context = bus_transaction_get_context (transaction);
108 /* First, send the message to the addressed_recipient, if there is one. */
109 if (addressed_recipient != NULL)
111 if (!bus_context_check_security_policy (context, transaction,
112 sender, addressed_recipient,
117 if (dbus_message_contains_unix_fds (message) &&
118 !dbus_connection_can_send_type (addressed_recipient,
121 dbus_set_error (error,
122 DBUS_ERROR_NOT_SUPPORTED,
123 "Tried to send message with Unix file descriptors"
124 "to a client that doesn't support that.");
128 /* Dispatch the message */
129 if (!bus_transaction_send (transaction, addressed_recipient, message))
136 /* Now dispatch to others who look interested in this message */
137 connections = bus_transaction_get_connections (transaction);
138 dbus_error_init (&tmp_error);
139 matchmaker = bus_context_get_matchmaker (context);
142 if (!bus_matchmaker_get_recipients (matchmaker, connections,
143 sender, addressed_recipient, message,
150 link = _dbus_list_get_first_link (&recipients);
153 DBusConnection *dest;
157 if (!send_one_message (dest, context, sender, addressed_recipient,
158 message, transaction, &tmp_error))
161 link = _dbus_list_get_next_link (&recipients, link);
164 _dbus_list_clear (&recipients);
166 if (dbus_error_is_set (&tmp_error))
168 dbus_move_error (&tmp_error, error);
175 static DBusHandlerResult
176 bus_dispatch (DBusConnection *connection,
177 DBusMessage *message)
179 const char *sender, *service_name;
181 BusTransaction *transaction;
183 DBusHandlerResult result;
184 DBusConnection *addressed_recipient;
186 result = DBUS_HANDLER_RESULT_HANDLED;
189 addressed_recipient = NULL;
190 dbus_error_init (&error);
192 context = bus_connection_get_context (connection);
193 _dbus_assert (context != NULL);
195 /* If we can't even allocate an OOM error, we just go to sleep
198 while (!bus_connection_preallocate_oom_error (connection))
199 _dbus_wait_for_memory ();
201 /* Ref connection in case we disconnect it at some point in here */
202 dbus_connection_ref (connection);
204 service_name = dbus_message_get_destination (message);
206 #ifdef DBUS_ENABLE_VERBOSE_MODE
208 const char *interface_name, *member_name, *error_name;
210 interface_name = dbus_message_get_interface (message);
211 member_name = dbus_message_get_member (message);
212 error_name = dbus_message_get_error_name (message);
214 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
215 interface_name ? interface_name : "(no interface)",
216 member_name ? member_name : "(no member)",
217 error_name ? error_name : "(no error name)",
218 service_name ? service_name : "peer");
220 #endif /* DBUS_ENABLE_VERBOSE_MODE */
222 /* Create our transaction */
223 transaction = bus_transaction_new (context);
224 if (transaction == NULL)
226 BUS_SET_OOM (&error);
230 /* If service_name is NULL, if it's a signal we send it to all
231 * connections with a match rule. If it's not a signal, there
232 * are some special cases here but mostly we just bail out.
234 if (service_name == NULL)
236 if (dbus_message_is_signal (message,
237 DBUS_INTERFACE_LOCAL,
240 bus_connection_disconnected (connection);
244 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
246 /* DBusConnection also handles some of these automatically, we leave
249 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
253 if(bus_context_is_kdbus(context))
255 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
257 handleNameOwnerChanged(message, transaction, connection);
263 /* Assign a sender to the message */
264 if(bus_context_is_kdbus(context) == FALSE) //if using kdbus, sender must be set on library side
265 if (bus_connection_is_active (connection))
267 sender = bus_connection_get_name (connection);
268 _dbus_assert (sender != NULL);
270 if (!dbus_message_set_sender (message, sender))
272 BUS_SET_OOM (&error);
276 /* We need to refetch the service name here, because
277 * dbus_message_set_sender can cause the header to be
278 * reallocated, and thus the service_name pointer will become
281 service_name = dbus_message_get_destination (message);
285 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
287 if (!bus_context_check_security_policy (context, transaction,
288 connection, NULL, NULL, message, &error))
290 _dbus_verbose ("Security policy rejected message\n");
294 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
295 if (!bus_driver_handle_message (connection, transaction, message, &error))
298 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
300 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
301 dbus_connection_close (connection);
304 else if (service_name != NULL) /* route to named service */
306 DBusString service_string;
308 BusRegistry *registry;
310 _dbus_assert (service_name != NULL);
312 registry = bus_connection_get_registry (connection);
314 _dbus_string_init_const (&service_string, service_name);
315 service = bus_registry_lookup (registry, &service_string);
317 if (service == NULL && dbus_message_get_auto_start (message))
319 BusActivation *activation;
320 /* We can't do the security policy check here, since the addressed
321 * recipient service doesn't exist yet. We do it before sending the
322 * message after the service has been created.
324 activation = bus_connection_get_activation (connection);
326 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
327 message, service_name, &error))
329 _DBUS_ASSERT_ERROR_IS_SET (&error);
330 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
336 else if (service == NULL)
338 dbus_set_error (&error,
339 DBUS_ERROR_NAME_HAS_NO_OWNER,
340 "Name \"%s\" does not exist",
346 addressed_recipient = bus_service_get_primary_owners_connection (service);
347 _dbus_assert (addressed_recipient != NULL);
351 /* Now send the message to its destination (or not, if
352 * addressed_recipient == NULL), and match it against other connections'
355 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
359 if (dbus_error_is_set (&error))
361 if (!dbus_connection_get_is_connected (connection))
363 /* If we disconnected it, we won't bother to send it any error
366 _dbus_verbose ("Not sending error to connection we disconnected\n");
368 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
370 bus_connection_send_oom_error (connection, message);
372 /* cancel transaction due to OOM */
373 if (transaction != NULL)
375 bus_transaction_cancel_and_free (transaction);
381 /* Try to send the real error, if no mem to do that, send
384 _dbus_assert (transaction != NULL);
385 if (!bus_transaction_send_error_reply (transaction, connection,
388 bus_connection_send_oom_error (connection, message);
390 /* cancel transaction due to OOM */
391 if (transaction != NULL)
393 bus_transaction_cancel_and_free (transaction);
400 dbus_error_free (&error);
403 if (transaction != NULL)
405 bus_transaction_execute_and_free (transaction);
408 dbus_connection_unref (connection);
413 static DBusHandlerResult
414 bus_dispatch_message_filter (DBusConnection *connection,
415 DBusMessage *message,
418 return bus_dispatch (connection, message);
422 bus_dispatch_add_connection (DBusConnection *connection)
424 if (!dbus_connection_add_filter (connection,
425 bus_dispatch_message_filter,
433 bus_dispatch_remove_connection (DBusConnection *connection)
435 /* Here we tell the bus driver that we want to get off. */
436 bus_driver_remove_connection (connection);
438 dbus_connection_remove_filter (connection,
439 bus_dispatch_message_filter,
443 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
447 /* This is used to know whether we need to block in order to finish
448 * sending a message, or whether the initial dbus_connection_send()
449 * already flushed the queue.
451 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
453 typedef dbus_bool_t (* Check1Func) (BusContext *context);
454 typedef dbus_bool_t (* Check2Func) (BusContext *context,
455 DBusConnection *connection);
457 static dbus_bool_t check_no_leftovers (BusContext *context);
460 block_connection_until_message_from_bus (BusContext *context,
461 DBusConnection *connection,
462 const char *what_is_expected)
464 _dbus_verbose ("expecting: %s\n", what_is_expected);
466 while (dbus_connection_get_dispatch_status (connection) ==
467 DBUS_DISPATCH_COMPLETE &&
468 dbus_connection_get_is_connected (connection))
470 bus_test_run_bus_loop (context, TRUE);
471 bus_test_run_clients_loop (FALSE);
476 spin_connection_until_authenticated (BusContext *context,
477 DBusConnection *connection)
479 _dbus_verbose ("Spinning to auth connection %p\n", connection);
480 while (!dbus_connection_get_is_authenticated (connection) &&
481 dbus_connection_get_is_connected (connection))
483 bus_test_run_bus_loop (context, FALSE);
484 bus_test_run_clients_loop (FALSE);
486 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
489 /* compensate for fact that pop_message() can return #NULL due to OOM */
491 pop_message_waiting_for_memory (DBusConnection *connection)
493 while (dbus_connection_get_dispatch_status (connection) ==
494 DBUS_DISPATCH_NEED_MEMORY)
495 _dbus_wait_for_memory ();
497 return dbus_connection_pop_message (connection);
501 borrow_message_waiting_for_memory (DBusConnection *connection)
503 while (dbus_connection_get_dispatch_status (connection) ==
504 DBUS_DISPATCH_NEED_MEMORY)
505 _dbus_wait_for_memory ();
507 return dbus_connection_borrow_message (connection);
511 warn_unexpected_real (DBusConnection *connection,
512 DBusMessage *message,
513 const char *expected,
514 const char *function,
518 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
520 dbus_message_get_interface (message) ?
521 dbus_message_get_interface (message) : "(unset)",
522 dbus_message_get_member (message) ?
523 dbus_message_get_member (message) : "(unset)",
524 dbus_message_get_error_name (message) ?
525 dbus_message_get_error_name (message) : "(unset)",
529 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
530 function, line, connection, expected);
533 #define warn_unexpected(connection, message, expected) \
534 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
537 verbose_message_received (DBusConnection *connection,
538 DBusMessage *message)
540 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
541 dbus_message_get_interface (message) ?
542 dbus_message_get_interface (message) : "(unset)",
543 dbus_message_get_member (message) ?
544 dbus_message_get_member (message) : "(unset)",
545 dbus_message_get_error_name (message) ?
546 dbus_message_get_error_name (message) : "(unset)",
559 ServiceInfoKind expected_kind;
560 const char *expected_service_name;
562 DBusConnection *skip_connection;
563 } CheckServiceOwnerChangedData;
566 check_service_owner_changed_foreach (DBusConnection *connection,
569 CheckServiceOwnerChangedData *d = data;
570 DBusMessage *message;
572 const char *service_name, *old_owner, *new_owner;
574 if (d->expected_kind == SERVICE_CREATED
575 && connection == d->skip_connection)
578 dbus_error_init (&error);
581 message = pop_message_waiting_for_memory (connection);
584 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
585 connection, "NameOwnerChanged");
588 else if (!dbus_message_is_signal (message,
592 warn_unexpected (connection, message, "NameOwnerChanged");
598 reget_service_info_data:
603 dbus_message_get_args (message, &error,
604 DBUS_TYPE_STRING, &service_name,
605 DBUS_TYPE_STRING, &old_owner,
606 DBUS_TYPE_STRING, &new_owner,
609 if (dbus_error_is_set (&error))
611 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
613 dbus_error_free (&error);
614 _dbus_wait_for_memory ();
615 goto reget_service_info_data;
619 _dbus_warn ("Did not get the expected arguments\n");
624 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
625 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
626 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
628 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
632 if (strcmp (service_name, d->expected_service_name) != 0)
634 _dbus_warn ("expected info on service %s, got info on %s\n",
635 d->expected_service_name,
640 if (*service_name == ':' && new_owner[0]
641 && strcmp (service_name, new_owner) != 0)
643 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
644 service_name, old_owner, new_owner);
652 dbus_error_free (&error);
655 dbus_message_unref (message);
662 kill_client_connection (BusContext *context,
663 DBusConnection *connection)
667 CheckServiceOwnerChangedData socd;
669 _dbus_verbose ("killing connection %p\n", connection);
671 s = dbus_bus_get_unique_name (connection);
672 _dbus_assert (s != NULL);
674 while ((base_service = _dbus_strdup (s)) == NULL)
675 _dbus_wait_for_memory ();
677 dbus_connection_ref (connection);
679 /* kick in the disconnect handler that unrefs the connection */
680 dbus_connection_close (connection);
682 bus_test_run_everything (context);
684 _dbus_assert (bus_test_client_listed (connection));
686 /* Run disconnect handler in test.c */
687 if (bus_connection_dispatch_one_message (connection))
688 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
690 _dbus_assert (!dbus_connection_get_is_connected (connection));
691 dbus_connection_unref (connection);
693 _dbus_assert (!bus_test_client_listed (connection));
695 socd.expected_kind = SERVICE_DELETED;
696 socd.expected_service_name = base_service;
698 socd.skip_connection = NULL;
700 bus_test_clients_foreach (check_service_owner_changed_foreach,
703 dbus_free (base_service);
706 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
708 if (!check_no_leftovers (context))
709 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
713 kill_client_connection_unchecked (DBusConnection *connection)
715 /* This kills the connection without expecting it to affect
716 * the rest of the bus.
718 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
720 dbus_connection_ref (connection);
721 dbus_connection_close (connection);
722 /* dispatching disconnect handler will unref once */
723 if (bus_connection_dispatch_one_message (connection))
724 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
726 _dbus_assert (!bus_test_client_listed (connection));
727 dbus_connection_unref (connection);
733 } CheckNoMessagesData;
736 check_no_messages_foreach (DBusConnection *connection,
739 CheckNoMessagesData *d = data;
740 DBusMessage *message;
742 message = pop_message_waiting_for_memory (connection);
745 warn_unexpected (connection, message, "no messages");
751 dbus_message_unref (message);
756 check_no_leftovers (BusContext *context)
758 CheckNoMessagesData nmd;
761 bus_test_clients_foreach (check_no_messages_foreach,
766 _dbus_verbose ("leftover message found\n");
773 /* returns TRUE if the correct thing happens,
774 * but the correct thing may include OOM errors.
777 check_hello_message (BusContext *context,
778 DBusConnection *connection)
780 DBusMessage *message;
781 DBusMessage *name_message;
782 dbus_uint32_t serial;
786 const char *acquired;
789 dbus_error_init (&error);
795 _dbus_verbose ("check_hello_message for %p\n", connection);
797 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
805 dbus_connection_ref (connection); /* because we may get disconnected */
807 if (!dbus_connection_send (connection, message, &serial))
809 dbus_message_unref (message);
810 dbus_connection_unref (connection);
814 _dbus_assert (dbus_message_has_signature (message, ""));
816 dbus_message_unref (message);
819 if (!dbus_connection_get_is_connected (connection))
821 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
823 dbus_connection_unref (connection);
828 /* send our message */
829 bus_test_run_clients_loop (SEND_PENDING (connection));
831 if (!dbus_connection_get_is_connected (connection))
833 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
835 dbus_connection_unref (connection);
840 block_connection_until_message_from_bus (context, connection, "reply to Hello");
842 if (!dbus_connection_get_is_connected (connection))
844 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
846 dbus_connection_unref (connection);
851 dbus_connection_unref (connection);
853 message = pop_message_waiting_for_memory (connection);
856 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
857 "Hello", serial, connection);
861 verbose_message_received (connection, message);
863 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
865 _dbus_warn ("Message has wrong sender %s\n",
866 dbus_message_get_sender (message) ?
867 dbus_message_get_sender (message) : "(none)");
871 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
873 if (dbus_message_is_error (message,
874 DBUS_ERROR_NO_MEMORY))
876 ; /* good, this is a valid response */
880 warn_unexpected (connection, message, "not this error");
887 CheckServiceOwnerChangedData socd;
889 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
891 ; /* good, expected */
895 warn_unexpected (connection, message, "method return for Hello");
900 retry_get_hello_name:
901 if (!dbus_message_get_args (message, &error,
902 DBUS_TYPE_STRING, &name,
905 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
907 _dbus_verbose ("no memory to get service name arg from hello\n");
908 dbus_error_free (&error);
909 _dbus_wait_for_memory ();
910 goto retry_get_hello_name;
914 _dbus_assert (dbus_error_is_set (&error));
915 _dbus_warn ("Did not get the expected single string argument to hello\n");
920 _dbus_verbose ("Got hello name: %s\n", name);
922 while (!dbus_bus_set_unique_name (connection, name))
923 _dbus_wait_for_memory ();
925 socd.expected_kind = SERVICE_CREATED;
926 socd.expected_service_name = name;
928 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
929 bus_test_clients_foreach (check_service_owner_changed_foreach,
935 name_message = message;
936 /* Client should also have gotten ServiceAcquired */
938 message = pop_message_waiting_for_memory (connection);
941 _dbus_warn ("Expecting %s, got nothing\n",
945 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
948 _dbus_warn ("Expecting %s, got smthg else\n",
953 retry_get_acquired_name:
954 if (!dbus_message_get_args (message, &error,
955 DBUS_TYPE_STRING, &acquired,
958 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
960 _dbus_verbose ("no memory to get service name arg from acquired\n");
961 dbus_error_free (&error);
962 _dbus_wait_for_memory ();
963 goto retry_get_acquired_name;
967 _dbus_assert (dbus_error_is_set (&error));
968 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
973 _dbus_verbose ("Got acquired name: %s\n", acquired);
975 if (strcmp (acquired, name) != 0)
977 _dbus_warn ("Acquired name is %s but expected %s\n",
984 if (!check_no_leftovers (context))
990 _dbus_verbose ("ending - retval = %d\n", retval);
992 dbus_error_free (&error);
995 dbus_message_unref (message);
998 dbus_message_unref (name_message);
1003 /* returns TRUE if the correct thing happens,
1004 * but the correct thing may include OOM errors.
1007 check_double_hello_message (BusContext *context,
1008 DBusConnection *connection)
1010 DBusMessage *message;
1011 dbus_uint32_t serial;
1016 dbus_error_init (&error);
1019 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1021 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1023 DBUS_INTERFACE_DBUS,
1026 if (message == NULL)
1029 if (!dbus_connection_send (connection, message, &serial))
1031 dbus_message_unref (message);
1035 dbus_message_unref (message);
1038 /* send our message */
1039 bus_test_run_clients_loop (SEND_PENDING (connection));
1041 dbus_connection_ref (connection); /* because we may get disconnected */
1042 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1044 if (!dbus_connection_get_is_connected (connection))
1046 _dbus_verbose ("connection was disconnected\n");
1048 dbus_connection_unref (connection);
1053 dbus_connection_unref (connection);
1055 message = pop_message_waiting_for_memory (connection);
1056 if (message == NULL)
1058 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1059 "Hello", serial, connection);
1063 verbose_message_received (connection, message);
1065 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1067 _dbus_warn ("Message has wrong sender %s\n",
1068 dbus_message_get_sender (message) ?
1069 dbus_message_get_sender (message) : "(none)");
1073 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1075 warn_unexpected (connection, message, "method return for Hello");
1079 if (!check_no_leftovers (context))
1085 dbus_error_free (&error);
1088 dbus_message_unref (message);
1093 /* returns TRUE if the correct thing happens,
1094 * but the correct thing may include OOM errors.
1097 check_get_connection_unix_user (BusContext *context,
1098 DBusConnection *connection)
1100 DBusMessage *message;
1101 dbus_uint32_t serial;
1104 const char *base_service_name;
1108 dbus_error_init (&error);
1111 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1113 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1115 DBUS_INTERFACE_DBUS,
1116 "GetConnectionUnixUser");
1118 if (message == NULL)
1121 base_service_name = dbus_bus_get_unique_name (connection);
1123 if (!dbus_message_append_args (message,
1124 DBUS_TYPE_STRING, &base_service_name,
1127 dbus_message_unref (message);
1131 if (!dbus_connection_send (connection, message, &serial))
1133 dbus_message_unref (message);
1137 /* send our message */
1138 bus_test_run_clients_loop (SEND_PENDING (connection));
1140 dbus_message_unref (message);
1143 dbus_connection_ref (connection); /* because we may get disconnected */
1144 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1146 if (!dbus_connection_get_is_connected (connection))
1148 _dbus_verbose ("connection was disconnected\n");
1150 dbus_connection_unref (connection);
1155 dbus_connection_unref (connection);
1157 message = pop_message_waiting_for_memory (connection);
1158 if (message == NULL)
1160 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1161 "GetConnectionUnixUser", serial, connection);
1165 verbose_message_received (connection, message);
1167 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1169 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1171 ; /* good, this is a valid response */
1175 warn_unexpected (connection, message, "not this error");
1182 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1184 ; /* good, expected */
1188 warn_unexpected (connection, message,
1189 "method_return for GetConnectionUnixUser");
1196 if (!dbus_message_get_args (message, &error,
1197 DBUS_TYPE_UINT32, &uid,
1200 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1202 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1203 dbus_error_free (&error);
1204 _dbus_wait_for_memory ();
1205 goto retry_get_property;
1209 _dbus_assert (dbus_error_is_set (&error));
1210 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1216 if (!check_no_leftovers (context))
1222 dbus_error_free (&error);
1225 dbus_message_unref (message);
1230 /* returns TRUE if the correct thing happens,
1231 * but the correct thing may include OOM errors.
1234 check_get_connection_unix_process_id (BusContext *context,
1235 DBusConnection *connection)
1237 DBusMessage *message;
1238 dbus_uint32_t serial;
1241 const char *base_service_name;
1245 dbus_error_init (&error);
1248 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1250 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1252 DBUS_INTERFACE_DBUS,
1253 "GetConnectionUnixProcessID");
1255 if (message == NULL)
1258 base_service_name = dbus_bus_get_unique_name (connection);
1260 if (!dbus_message_append_args (message,
1261 DBUS_TYPE_STRING, &base_service_name,
1264 dbus_message_unref (message);
1268 if (!dbus_connection_send (connection, message, &serial))
1270 dbus_message_unref (message);
1274 /* send our message */
1275 bus_test_run_clients_loop (SEND_PENDING (connection));
1277 dbus_message_unref (message);
1280 dbus_connection_ref (connection); /* because we may get disconnected */
1281 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1283 if (!dbus_connection_get_is_connected (connection))
1285 _dbus_verbose ("connection was disconnected\n");
1287 dbus_connection_unref (connection);
1292 dbus_connection_unref (connection);
1294 message = pop_message_waiting_for_memory (connection);
1295 if (message == NULL)
1297 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1298 "GetConnectionUnixProcessID", serial, connection);
1302 verbose_message_received (connection, message);
1304 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1306 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1308 ; /* good, this is a valid response */
1311 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1313 /* We are expecting this error, since we know in the test suite we aren't
1314 * talking to a client running on UNIX
1316 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1321 warn_unexpected (connection, message, "not this error");
1329 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1332 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1334 ; /* good, expected */
1338 warn_unexpected (connection, message,
1339 "method_return for GetConnectionUnixProcessID");
1346 if (!dbus_message_get_args (message, &error,
1347 DBUS_TYPE_UINT32, &pid,
1350 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1352 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1353 dbus_error_free (&error);
1354 _dbus_wait_for_memory ();
1355 goto retry_get_property;
1359 _dbus_assert (dbus_error_is_set (&error));
1360 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1366 /* test if returned pid is the same as our own pid
1368 * @todo It would probably be good to restructure the tests
1369 * in a way so our parent is the bus that we're testing
1370 * cause then we can test that the pid returned matches
1373 if (pid != (dbus_uint32_t) _dbus_getpid ())
1375 _dbus_assert (dbus_error_is_set (&error));
1376 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1380 #endif /* !DBUS_WIN */
1383 if (!check_no_leftovers (context))
1389 dbus_error_free (&error);
1392 dbus_message_unref (message);
1397 /* returns TRUE if the correct thing happens,
1398 * but the correct thing may include OOM errors.
1401 check_add_match_all (BusContext *context,
1402 DBusConnection *connection)
1404 DBusMessage *message;
1406 dbus_uint32_t serial;
1408 const char *empty = "";
1411 dbus_error_init (&error);
1414 _dbus_verbose ("check_add_match_all for %p\n", connection);
1416 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1418 DBUS_INTERFACE_DBUS,
1421 if (message == NULL)
1424 /* empty string match rule matches everything */
1425 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1428 dbus_message_unref (message);
1432 if (!dbus_connection_send (connection, message, &serial))
1434 dbus_message_unref (message);
1438 dbus_message_unref (message);
1441 dbus_connection_ref (connection); /* because we may get disconnected */
1443 /* send our message */
1444 bus_test_run_clients_loop (SEND_PENDING (connection));
1446 if (!dbus_connection_get_is_connected (connection))
1448 _dbus_verbose ("connection was disconnected\n");
1450 dbus_connection_unref (connection);
1455 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1457 if (!dbus_connection_get_is_connected (connection))
1459 _dbus_verbose ("connection was disconnected\n");
1461 dbus_connection_unref (connection);
1466 dbus_connection_unref (connection);
1468 message = pop_message_waiting_for_memory (connection);
1469 if (message == NULL)
1471 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1472 "AddMatch", serial, connection);
1476 verbose_message_received (connection, message);
1478 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1480 _dbus_warn ("Message has wrong sender %s\n",
1481 dbus_message_get_sender (message) ?
1482 dbus_message_get_sender (message) : "(none)");
1486 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1488 if (dbus_message_is_error (message,
1489 DBUS_ERROR_NO_MEMORY))
1491 ; /* good, this is a valid response */
1495 warn_unexpected (connection, message, "not this error");
1502 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1504 ; /* good, expected */
1505 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1509 warn_unexpected (connection, message, "method return for AddMatch");
1515 if (!check_no_leftovers (context))
1521 dbus_error_free (&error);
1524 dbus_message_unref (message);
1529 /* returns TRUE if the correct thing happens,
1530 * but the correct thing may include OOM errors.
1533 check_hello_connection (BusContext *context)
1535 DBusConnection *connection;
1538 dbus_error_init (&error);
1540 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1541 if (connection == NULL)
1543 _DBUS_ASSERT_ERROR_IS_SET (&error);
1544 dbus_error_free (&error);
1548 if (!bus_setup_debug_client (connection))
1550 dbus_connection_close (connection);
1551 dbus_connection_unref (connection);
1555 spin_connection_until_authenticated (context, connection);
1557 if (!check_hello_message (context, connection))
1560 if (dbus_bus_get_unique_name (connection) == NULL)
1562 /* We didn't successfully register, so we can't
1563 * do the usual kill_client_connection() checks
1565 kill_client_connection_unchecked (connection);
1569 if (!check_add_match_all (context, connection))
1572 kill_client_connection (context, connection);
1578 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1580 /* returns TRUE if the correct thing happens,
1581 * but the correct thing may include OOM errors.
1584 check_nonexistent_service_no_auto_start (BusContext *context,
1585 DBusConnection *connection)
1587 DBusMessage *message;
1588 dbus_uint32_t serial;
1590 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1591 dbus_uint32_t flags;
1593 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1595 DBUS_INTERFACE_DBUS,
1596 "StartServiceByName");
1598 if (message == NULL)
1601 dbus_message_set_auto_start (message, FALSE);
1604 if (!dbus_message_append_args (message,
1605 DBUS_TYPE_STRING, &nonexistent,
1606 DBUS_TYPE_UINT32, &flags,
1609 dbus_message_unref (message);
1613 if (!dbus_connection_send (connection, message, &serial))
1615 dbus_message_unref (message);
1619 dbus_message_unref (message);
1622 bus_test_run_everything (context);
1623 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1624 bus_test_run_everything (context);
1626 if (!dbus_connection_get_is_connected (connection))
1628 _dbus_verbose ("connection was disconnected\n");
1634 message = pop_message_waiting_for_memory (connection);
1635 if (message == NULL)
1637 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1638 "StartServiceByName", serial, connection);
1642 verbose_message_received (connection, message);
1644 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1646 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1648 _dbus_warn ("Message has wrong sender %s\n",
1649 dbus_message_get_sender (message) ?
1650 dbus_message_get_sender (message) : "(none)");
1654 if (dbus_message_is_error (message,
1655 DBUS_ERROR_NO_MEMORY))
1657 ; /* good, this is a valid response */
1659 else if (dbus_message_is_error (message,
1660 DBUS_ERROR_SERVICE_UNKNOWN))
1662 ; /* good, this is expected also */
1666 warn_unexpected (connection, message, "not this error");
1672 _dbus_warn ("Did not expect to successfully activate %s\n",
1673 NONEXISTENT_SERVICE_NAME);
1681 dbus_message_unref (message);
1686 /* returns TRUE if the correct thing happens,
1687 * but the correct thing may include OOM errors.
1690 check_nonexistent_service_auto_start (BusContext *context,
1691 DBusConnection *connection)
1693 DBusMessage *message;
1694 dbus_uint32_t serial;
1697 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1698 "/org/freedesktop/TestSuite",
1699 "org.freedesktop.TestSuite",
1702 if (message == NULL)
1705 if (!dbus_connection_send (connection, message, &serial))
1707 dbus_message_unref (message);
1711 dbus_message_unref (message);
1714 bus_test_run_everything (context);
1715 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1716 bus_test_run_everything (context);
1718 if (!dbus_connection_get_is_connected (connection))
1720 _dbus_verbose ("connection was disconnected\n");
1726 message = pop_message_waiting_for_memory (connection);
1728 if (message == NULL)
1730 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1731 "Echo message (auto activation)", serial, connection);
1735 verbose_message_received (connection, message);
1737 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1739 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1741 _dbus_warn ("Message has wrong sender %s\n",
1742 dbus_message_get_sender (message) ?
1743 dbus_message_get_sender (message) : "(none)");
1747 if (dbus_message_is_error (message,
1748 DBUS_ERROR_NO_MEMORY))
1750 ; /* good, this is a valid response */
1752 else if (dbus_message_is_error (message,
1753 DBUS_ERROR_SERVICE_UNKNOWN))
1755 ; /* good, this is expected also */
1759 warn_unexpected (connection, message, "not this error");
1765 _dbus_warn ("Did not expect to successfully activate %s\n",
1766 NONEXISTENT_SERVICE_NAME);
1774 dbus_message_unref (message);
1780 check_base_service_activated (BusContext *context,
1781 DBusConnection *connection,
1782 DBusMessage *initial_message,
1783 const char **base_service_p)
1785 DBusMessage *message;
1788 const char *base_service, *base_service_from_bus, *old_owner;
1792 dbus_error_init (&error);
1793 base_service = NULL;
1795 base_service_from_bus = NULL;
1797 message = initial_message;
1798 dbus_message_ref (message);
1800 if (dbus_message_is_signal (message,
1801 DBUS_INTERFACE_DBUS,
1802 "NameOwnerChanged"))
1804 CheckServiceOwnerChangedData socd;
1806 reget_service_name_arg:
1807 base_service = NULL;
1809 base_service_from_bus = NULL;
1811 if (!dbus_message_get_args (message, &error,
1812 DBUS_TYPE_STRING, &base_service,
1813 DBUS_TYPE_STRING, &old_owner,
1814 DBUS_TYPE_STRING, &base_service_from_bus,
1817 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1819 dbus_error_free (&error);
1820 _dbus_wait_for_memory ();
1821 goto reget_service_name_arg;
1825 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1826 "NameOwnerChanged (creation)",
1832 if (*base_service != ':')
1834 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1839 if (strcmp (base_service, base_service_from_bus) != 0)
1841 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1842 base_service, base_service_from_bus);
1848 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1853 socd.expected_kind = SERVICE_CREATED;
1854 socd.expected_service_name = base_service;
1855 socd.failed = FALSE;
1856 socd.skip_connection = connection;
1857 bus_test_clients_foreach (check_service_owner_changed_foreach,
1865 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1871 *base_service_p = base_service;
1877 dbus_message_unref (message);
1878 dbus_error_free (&error);
1884 check_service_activated (BusContext *context,
1885 DBusConnection *connection,
1886 const char *activated_name,
1887 const char *base_service_name,
1888 DBusMessage *initial_message)
1890 DBusMessage *message;
1893 dbus_uint32_t activation_result;
1897 dbus_error_init (&error);
1899 message = initial_message;
1900 dbus_message_ref (message);
1902 if (dbus_message_is_signal (message,
1903 DBUS_INTERFACE_DBUS,
1904 "NameOwnerChanged"))
1906 CheckServiceOwnerChangedData socd;
1907 const char *service_name, *base_service_from_bus, *old_owner;
1909 reget_service_name_arg:
1910 service_name = NULL;
1912 base_service_from_bus = NULL;
1914 if (!dbus_message_get_args (message, &error,
1915 DBUS_TYPE_STRING, &service_name,
1916 DBUS_TYPE_STRING, &old_owner,
1917 DBUS_TYPE_STRING, &base_service_from_bus,
1920 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1922 dbus_error_free (&error);
1923 _dbus_wait_for_memory ();
1924 goto reget_service_name_arg;
1928 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1929 "NameOwnerChanged (creation)",
1935 if (strcmp (service_name, activated_name) != 0)
1937 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1938 activated_name, service_name);
1942 if (strcmp (base_service_name, base_service_from_bus) != 0)
1944 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1945 base_service_from_bus, base_service_name);
1951 _dbus_warn ("expected a %s, got a %s\n",
1952 "NameOwnerChanged (creation)",
1953 "NameOwnerChanged (change)");
1957 socd.expected_kind = SERVICE_CREATED;
1958 socd.skip_connection = connection;
1959 socd.failed = FALSE;
1960 socd.expected_service_name = service_name;
1961 bus_test_clients_foreach (check_service_owner_changed_foreach,
1967 dbus_message_unref (message);
1968 service_name = NULL;
1970 base_service_from_bus = NULL;
1972 message = pop_message_waiting_for_memory (connection);
1973 if (message == NULL)
1975 _dbus_warn ("Expected a reply to %s, got nothing\n",
1976 "StartServiceByName");
1982 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1987 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1989 warn_unexpected (connection, message, "reply to StartServiceByName");
1994 activation_result = 0;
1995 if (!dbus_message_get_args (message, &error,
1996 DBUS_TYPE_UINT32, &activation_result,
1999 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2001 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
2002 "StartServiceByName", error.message);
2006 dbus_error_free (&error);
2010 if (activation_result == DBUS_START_REPLY_SUCCESS)
2012 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2016 _dbus_warn ("Activation result was %u, no good.\n",
2022 dbus_message_unref (message);
2025 if (!check_no_leftovers (context))
2027 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2035 dbus_message_unref (message);
2036 dbus_error_free (&error);
2042 check_service_auto_activated (BusContext *context,
2043 DBusConnection *connection,
2044 const char *activated_name,
2045 const char *base_service_name,
2046 DBusMessage *initial_message)
2048 DBusMessage *message;
2054 dbus_error_init (&error);
2056 message = initial_message;
2057 dbus_message_ref (message);
2059 if (dbus_message_is_signal (message,
2060 DBUS_INTERFACE_DBUS,
2061 "NameOwnerChanged"))
2063 const char *service_name;
2064 CheckServiceOwnerChangedData socd;
2066 reget_service_name_arg:
2067 if (!dbus_message_get_args (message, &error,
2068 DBUS_TYPE_STRING, &service_name,
2071 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2073 dbus_error_free (&error);
2074 _dbus_wait_for_memory ();
2075 goto reget_service_name_arg;
2079 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2082 dbus_error_free (&error);
2087 if (strcmp (service_name, activated_name) != 0)
2089 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2090 activated_name, service_name);
2094 socd.expected_kind = SERVICE_CREATED;
2095 socd.expected_service_name = service_name;
2096 socd.failed = FALSE;
2097 socd.skip_connection = connection;
2098 bus_test_clients_foreach (check_service_owner_changed_foreach,
2104 /* Note that this differs from regular activation in that we don't get a
2105 * reply to ActivateService here.
2108 dbus_message_unref (message);
2110 service_name = NULL;
2114 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2123 dbus_message_unref (message);
2129 check_service_deactivated (BusContext *context,
2130 DBusConnection *connection,
2131 const char *activated_name,
2132 const char *base_service)
2135 CheckServiceOwnerChangedData socd;
2139 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2140 * service and the activated_name. The base service
2141 * notification is required to come last.
2143 socd.expected_kind = SERVICE_DELETED;
2144 socd.expected_service_name = activated_name;
2145 socd.failed = FALSE;
2146 socd.skip_connection = NULL;
2147 bus_test_clients_foreach (check_service_owner_changed_foreach,
2153 socd.expected_kind = SERVICE_DELETED;
2154 socd.expected_service_name = base_service;
2155 socd.failed = FALSE;
2156 socd.skip_connection = NULL;
2157 bus_test_clients_foreach (check_service_owner_changed_foreach,
2170 check_send_exit_to_service (BusContext *context,
2171 DBusConnection *connection,
2172 const char *service_name,
2173 const char *base_service)
2175 dbus_bool_t got_error;
2176 DBusMessage *message;
2177 dbus_uint32_t serial;
2180 _dbus_verbose ("Sending exit message to the test service\n");
2184 /* Kill off the test service by sending it a quit message */
2185 message = dbus_message_new_method_call (service_name,
2186 "/org/freedesktop/TestSuite",
2187 "org.freedesktop.TestSuite",
2190 if (message == NULL)
2192 /* Do this again; we still need the service to exit... */
2193 if (!check_send_exit_to_service (context, connection,
2194 service_name, base_service))
2200 if (!dbus_connection_send (connection, message, &serial))
2202 dbus_message_unref (message);
2204 /* Do this again; we still need the service to exit... */
2205 if (!check_send_exit_to_service (context, connection,
2206 service_name, base_service))
2212 dbus_message_unref (message);
2216 bus_test_run_clients_loop (SEND_PENDING (connection));
2218 /* read it in and write it out to test service */
2219 bus_test_run_bus_loop (context, FALSE);
2221 /* see if we got an error during message bus dispatching */
2222 bus_test_run_clients_loop (FALSE);
2223 message = borrow_message_waiting_for_memory (connection);
2224 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2227 dbus_connection_return_message (connection, message);
2233 /* If no error, wait for the test service to exit */
2234 block_connection_until_message_from_bus (context, connection, "test service to exit");
2236 bus_test_run_everything (context);
2241 message = pop_message_waiting_for_memory (connection);
2242 _dbus_assert (message != NULL);
2244 if (dbus_message_get_reply_serial (message) != serial)
2246 warn_unexpected (connection, message,
2247 "error with the correct reply serial");
2251 if (!dbus_message_is_error (message,
2252 DBUS_ERROR_NO_MEMORY))
2254 warn_unexpected (connection, message,
2255 "a no memory error from asking test service to exit");
2259 _dbus_verbose ("Got error %s when asking test service to exit\n",
2260 dbus_message_get_error_name (message));
2262 /* Do this again; we still need the service to exit... */
2263 if (!check_send_exit_to_service (context, connection,
2264 service_name, base_service))
2269 if (!check_service_deactivated (context, connection,
2270 service_name, base_service))
2273 /* Should now have a NoReply error from the Exit() method
2274 * call; it should have come after all the deactivation
2277 message = pop_message_waiting_for_memory (connection);
2279 if (message == NULL)
2281 warn_unexpected (connection, NULL,
2282 "reply to Exit() method call");
2285 if (!dbus_message_is_error (message,
2286 DBUS_ERROR_NO_REPLY))
2288 warn_unexpected (connection, message,
2289 "NoReply error from Exit() method call");
2293 if (dbus_message_get_reply_serial (message) != serial)
2295 warn_unexpected (connection, message,
2296 "error with the correct reply serial");
2300 _dbus_verbose ("Got error %s after test service exited\n",
2301 dbus_message_get_error_name (message));
2303 if (!check_no_leftovers (context))
2305 _dbus_warn ("Messages were left over after %s\n",
2306 _DBUS_FUNCTION_NAME);
2315 dbus_message_unref (message);
2321 check_got_error (BusContext *context,
2322 DBusConnection *connection,
2323 const char *first_error_name,
2326 DBusMessage *message;
2329 dbus_bool_t error_found;
2330 const char *error_name;
2334 message = pop_message_waiting_for_memory (connection);
2335 if (message == NULL)
2337 _dbus_warn ("Did not get an expected error\n");
2341 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2343 warn_unexpected (connection, message, "an error");
2348 error_found = FALSE;
2350 va_start (ap, first_error_name);
2351 error_name = first_error_name;
2352 while (error_name != NULL)
2354 if (dbus_message_is_error (message, error_name))
2359 error_name = va_arg (ap, char*);
2365 _dbus_warn ("Expected error %s or other, got %s instead\n",
2367 dbus_message_get_error_name (message));
2375 dbus_message_unref (message);
2382 GOT_SERVICE_CREATED,
2383 GOT_SERVICE_DELETED,
2388 static GotServiceInfo
2389 check_got_service_info (DBusMessage *message)
2391 GotServiceInfo message_kind;
2393 if (dbus_message_is_signal (message,
2394 DBUS_INTERFACE_DBUS,
2395 "NameOwnerChanged"))
2398 const char *service_name, *old_owner, *new_owner;
2399 dbus_error_init (&error);
2401 reget_service_info_data:
2402 service_name = NULL;
2406 dbus_message_get_args (message, &error,
2407 DBUS_TYPE_STRING, &service_name,
2408 DBUS_TYPE_STRING, &old_owner,
2409 DBUS_TYPE_STRING, &new_owner,
2411 if (dbus_error_is_set (&error))
2413 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2415 dbus_error_free (&error);
2416 goto reget_service_info_data;
2420 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2421 message_kind = GOT_SOMETHING_ELSE;
2424 else if (!old_owner[0])
2425 message_kind = GOT_SERVICE_CREATED;
2426 else if (!new_owner[0])
2427 message_kind = GOT_SERVICE_DELETED;
2429 message_kind = GOT_SOMETHING_ELSE;
2431 dbus_error_free (&error);
2433 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2434 message_kind = GOT_ERROR;
2436 message_kind = GOT_SOMETHING_ELSE;
2438 return message_kind;
2441 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2443 /* returns TRUE if the correct thing happens,
2444 * but the correct thing may include OOM errors.
2447 check_existent_service_no_auto_start (BusContext *context,
2448 DBusConnection *connection)
2450 DBusMessage *message;
2451 DBusMessage *base_service_message;
2452 const char *base_service;
2453 dbus_uint32_t serial;
2455 const char *existent = EXISTENT_SERVICE_NAME;
2456 dbus_uint32_t flags;
2458 base_service_message = NULL;
2460 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2462 DBUS_INTERFACE_DBUS,
2463 "StartServiceByName");
2465 if (message == NULL)
2468 dbus_message_set_auto_start (message, FALSE);
2471 if (!dbus_message_append_args (message,
2472 DBUS_TYPE_STRING, &existent,
2473 DBUS_TYPE_UINT32, &flags,
2476 dbus_message_unref (message);
2480 if (!dbus_connection_send (connection, message, &serial))
2482 dbus_message_unref (message);
2486 dbus_message_unref (message);
2489 bus_test_run_everything (context);
2491 /* now wait for the message bus to hear back from the activated
2494 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2496 bus_test_run_everything (context);
2498 if (!dbus_connection_get_is_connected (connection))
2500 _dbus_verbose ("connection was disconnected\n");
2506 message = pop_message_waiting_for_memory (connection);
2507 if (message == NULL)
2509 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2510 "StartServiceByName", serial, connection);
2514 verbose_message_received (connection, message);
2515 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2517 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2519 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2521 _dbus_warn ("Message has wrong sender %s\n",
2522 dbus_message_get_sender (message) ?
2523 dbus_message_get_sender (message) : "(none)");
2527 if (dbus_message_is_error (message,
2528 DBUS_ERROR_NO_MEMORY))
2530 ; /* good, this is a valid response */
2532 else if (dbus_message_is_error (message,
2533 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2534 dbus_message_is_error (message,
2535 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2536 dbus_message_is_error (message,
2537 DBUS_ERROR_SPAWN_EXEC_FAILED))
2539 ; /* good, this is expected also */
2543 _dbus_warn ("Did not expect error %s\n",
2544 dbus_message_get_error_name (message));
2550 GotServiceInfo message_kind;
2552 if (!check_base_service_activated (context, connection,
2553 message, &base_service))
2556 base_service_message = message;
2559 /* We may need to block here for the test service to exit or finish up */
2560 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2562 message = dbus_connection_borrow_message (connection);
2563 if (message == NULL)
2565 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2569 message_kind = check_got_service_info (message);
2571 dbus_connection_return_message (connection, message);
2574 switch (message_kind)
2576 case GOT_SOMETHING_ELSE:
2577 _dbus_warn ("Unexpected message after ActivateService "
2578 "(should be an error or a service announcement");
2582 if (!check_got_error (context, connection,
2583 DBUS_ERROR_SPAWN_CHILD_EXITED,
2584 DBUS_ERROR_NO_MEMORY,
2587 /* A service deleted should be coming along now after this error.
2588 * We can also get the error *after* the service deleted.
2593 case GOT_SERVICE_DELETED:
2595 /* The service started up and got a base address, but then
2596 * failed to register under EXISTENT_SERVICE_NAME
2598 CheckServiceOwnerChangedData socd;
2600 socd.expected_kind = SERVICE_DELETED;
2601 socd.expected_service_name = base_service;
2602 socd.failed = FALSE;
2603 socd.skip_connection = NULL;
2605 bus_test_clients_foreach (check_service_owner_changed_foreach,
2611 /* Now we should get an error about the service exiting
2612 * if we didn't get it before.
2614 if (message_kind != GOT_ERROR)
2616 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2618 /* and process everything again */
2619 bus_test_run_everything (context);
2621 if (!check_got_error (context, connection,
2622 DBUS_ERROR_SPAWN_CHILD_EXITED,
2623 DBUS_ERROR_NO_MEMORY,
2630 case GOT_SERVICE_CREATED:
2631 message = pop_message_waiting_for_memory (connection);
2632 if (message == NULL)
2634 _dbus_warn ("Failed to pop message we just put back! "
2635 "should have been a NameOwnerChanged (creation)\n");
2639 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2640 base_service, message))
2643 dbus_message_unref (message);
2646 if (!check_no_leftovers (context))
2648 _dbus_warn ("Messages were left over after successful activation\n");
2652 if (!check_send_exit_to_service (context, connection,
2653 EXISTENT_SERVICE_NAME, base_service))
2664 dbus_message_unref (message);
2666 if (base_service_message)
2667 dbus_message_unref (base_service_message);
2672 #ifndef DBUS_WIN_FIXME
2673 /* returns TRUE if the correct thing happens,
2674 * but the correct thing may include OOM errors.
2677 check_segfault_service_no_auto_start (BusContext *context,
2678 DBusConnection *connection)
2680 DBusMessage *message;
2681 dbus_uint32_t serial;
2683 const char *segv_service;
2684 dbus_uint32_t flags;
2686 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2688 DBUS_INTERFACE_DBUS,
2689 "StartServiceByName");
2691 if (message == NULL)
2694 dbus_message_set_auto_start (message, FALSE);
2696 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2698 if (!dbus_message_append_args (message,
2699 DBUS_TYPE_STRING, &segv_service,
2700 DBUS_TYPE_UINT32, &flags,
2703 dbus_message_unref (message);
2707 if (!dbus_connection_send (connection, message, &serial))
2709 dbus_message_unref (message);
2713 dbus_message_unref (message);
2716 bus_test_run_everything (context);
2717 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2718 bus_test_run_everything (context);
2720 if (!dbus_connection_get_is_connected (connection))
2722 _dbus_verbose ("connection was disconnected\n");
2728 message = pop_message_waiting_for_memory (connection);
2729 if (message == NULL)
2731 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2732 "StartServiceByName", serial, connection);
2736 verbose_message_received (connection, message);
2738 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2740 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2742 _dbus_warn ("Message has wrong sender %s\n",
2743 dbus_message_get_sender (message) ?
2744 dbus_message_get_sender (message) : "(none)");
2748 if (dbus_message_is_error (message,
2749 DBUS_ERROR_NO_MEMORY))
2751 ; /* good, this is a valid response */
2753 else if (dbus_message_is_error (message,
2756 const char *servicehelper;
2757 servicehelper = bus_context_get_servicehelper (context);
2758 /* make sure this only happens with the launch helper */
2759 _dbus_assert (servicehelper != NULL);
2761 else if (dbus_message_is_error (message,
2762 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2764 ; /* good, this is expected also */
2768 warn_unexpected (connection, message, "not this error");
2775 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2783 dbus_message_unref (message);
2789 /* returns TRUE if the correct thing happens,
2790 * but the correct thing may include OOM errors.
2793 check_segfault_service_auto_start (BusContext *context,
2794 DBusConnection *connection)
2796 DBusMessage *message;
2797 dbus_uint32_t serial;
2800 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2801 "/org/freedesktop/TestSuite",
2802 "org.freedesktop.TestSuite",
2805 if (message == NULL)
2808 if (!dbus_connection_send (connection, message, &serial))
2810 dbus_message_unref (message);
2814 dbus_message_unref (message);
2817 bus_test_run_everything (context);
2818 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2819 bus_test_run_everything (context);
2821 if (!dbus_connection_get_is_connected (connection))
2823 _dbus_verbose ("connection was disconnected\n");
2829 message = pop_message_waiting_for_memory (connection);
2830 if (message == NULL)
2832 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2833 "Echo message (auto activation)", serial, connection);
2837 verbose_message_received (connection, message);
2839 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2841 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2843 _dbus_warn ("Message has wrong sender %s\n",
2844 dbus_message_get_sender (message) ?
2845 dbus_message_get_sender (message) : "(none)");
2849 if (dbus_message_is_error (message,
2850 DBUS_ERROR_NO_MEMORY))
2852 ; /* good, this is a valid response */
2854 else if (dbus_message_is_error (message,
2855 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2857 ; /* good, this is expected also */
2861 warn_unexpected (connection, message, "not this error");
2868 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2876 dbus_message_unref (message);
2882 #define TEST_ECHO_MESSAGE "Test echo message"
2883 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2885 /* returns TRUE if the correct thing happens,
2886 * but the correct thing may include OOM errors.
2889 check_existent_hello_from_self (BusContext *context,
2890 DBusConnection *connection)
2892 DBusMessage *message;
2893 dbus_uint32_t serial;
2896 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2897 "/org/freedesktop/TestSuite",
2898 "org.freedesktop.TestSuite",
2899 "RunHelloFromSelf");
2901 if (message == NULL)
2904 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2905 if (!dbus_message_append_args (message,
2906 DBUS_TYPE_STRING, &text,
2909 dbus_message_unref (message);
2913 if (!dbus_connection_send (connection, message, &serial))
2915 dbus_message_unref (message);
2919 dbus_message_unref (message);
2922 bus_test_run_everything (context);
2924 /* Note: if this test is run in OOM mode, it will block when the bus
2925 * doesn't send a reply due to OOM.
2927 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2929 message = pop_message_waiting_for_memory (connection);
2930 if (message == NULL)
2932 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2936 if (dbus_message_get_reply_serial (message) != serial)
2938 _dbus_warn ("Wrong reply serial\n");
2939 dbus_message_unref (message);
2943 dbus_message_unref (message);
2949 /* returns TRUE if the correct thing happens,
2950 * but the correct thing may include OOM errors.
2953 check_existent_ping (BusContext *context,
2954 DBusConnection *connection)
2956 DBusMessage *message;
2957 dbus_uint32_t serial;
2958 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2959 "/org/freedesktop/TestSuite",
2960 "org.freedesktop.DBus.Peer",
2963 if (message == NULL)
2966 if (!dbus_connection_send (connection, message, &serial))
2968 dbus_message_unref (message);
2972 dbus_message_unref (message);
2975 bus_test_run_everything (context);
2977 /* Note: if this test is run in OOM mode, it will block when the bus
2978 * doesn't send a reply due to OOM.
2980 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2982 message = pop_message_waiting_for_memory (connection);
2983 if (message == NULL)
2985 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2989 if (dbus_message_get_reply_serial (message) != serial)
2991 _dbus_warn ("Wrong reply serial\n");
2992 dbus_message_unref (message);
2996 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2998 _dbus_warn ("Unexpected message return during Ping\n");
2999 dbus_message_unref (message);
3003 dbus_message_unref (message);
3009 /* returns TRUE if the correct thing happens,
3010 * but the correct thing may include OOM errors.
3013 check_existent_get_machine_id (BusContext *context,
3014 DBusConnection *connection)
3016 DBusMessage *message;
3017 dbus_uint32_t serial;
3018 const char *machine_id;
3020 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3021 "/org/freedesktop/TestSuite",
3022 "org.freedesktop.DBus.Peer",
3025 if (message == NULL)
3028 if (!dbus_connection_send (connection, message, &serial))
3030 dbus_message_unref (message);
3034 dbus_message_unref (message);
3037 bus_test_run_everything (context);
3039 /* Note: if this test is run in OOM mode, it will block when the bus
3040 * doesn't send a reply due to OOM.
3042 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3044 message = pop_message_waiting_for_memory (connection);
3045 if (message == NULL)
3047 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3051 if (dbus_message_get_reply_serial (message) != serial)
3053 _dbus_warn ("Wrong reply serial\n");
3054 dbus_message_unref (message);
3058 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3060 _dbus_warn ("Unexpected message return during GetMachineId\n");
3061 dbus_message_unref (message);
3066 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3068 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3069 dbus_message_unref (message);
3073 if (machine_id == NULL || strlen (machine_id) != 32)
3075 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3076 dbus_message_unref (message);
3080 /* We can't check that the machine id is correct because during make check it is
3081 * just made up for each process separately
3084 dbus_message_unref (message);
3090 /* returns TRUE if the correct thing happens,
3091 * but the correct thing may include OOM errors.
3094 check_existent_service_auto_start (BusContext *context,
3095 DBusConnection *connection)
3097 DBusMessage *message;
3098 DBusMessage *base_service_message;
3099 dbus_uint32_t serial;
3101 const char *base_service;
3104 base_service_message = NULL;
3106 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3107 "/org/freedesktop/TestSuite",
3108 "org.freedesktop.TestSuite",
3111 if (message == NULL)
3114 text = TEST_ECHO_MESSAGE;
3115 if (!dbus_message_append_args (message,
3116 DBUS_TYPE_STRING, &text,
3119 dbus_message_unref (message);
3123 if (!dbus_connection_send (connection, message, &serial))
3125 dbus_message_unref (message);
3129 dbus_message_unref (message);
3132 bus_test_run_everything (context);
3134 /* now wait for the message bus to hear back from the activated
3137 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3138 bus_test_run_everything (context);
3140 if (!dbus_connection_get_is_connected (connection))
3142 _dbus_verbose ("connection was disconnected\n");
3148 message = pop_message_waiting_for_memory (connection);
3149 if (message == NULL)
3151 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3152 serial, connection);
3156 verbose_message_received (connection, message);
3157 _dbus_verbose (" (after sending %s)\n", "auto start");
3159 /* we should get zero or two ServiceOwnerChanged signals */
3160 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3162 GotServiceInfo message_kind;
3164 if (!check_base_service_activated (context, connection,
3165 message, &base_service))
3168 base_service_message = message;
3171 /* We may need to block here for the test service to exit or finish up */
3172 block_connection_until_message_from_bus (context, connection, "service to exit");
3174 /* Should get a service creation notification for the activated
3175 * service name, or a service deletion on the base service name
3177 message = dbus_connection_borrow_message (connection);
3178 if (message == NULL)
3180 _dbus_warn ("No message after auto activation "
3181 "(should be a service announcement)\n");
3182 dbus_connection_return_message (connection, message);
3187 message_kind = check_got_service_info (message);
3189 dbus_connection_return_message (connection, message);
3192 switch (message_kind)
3194 case GOT_SERVICE_CREATED:
3195 message = pop_message_waiting_for_memory (connection);
3196 if (message == NULL)
3198 _dbus_warn ("Failed to pop message we just put back! "
3199 "should have been a NameOwnerChanged (creation)\n");
3203 /* Check that ServiceOwnerChanged (creation) was correctly received */
3204 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3205 base_service, message))
3208 dbus_message_unref (message);
3213 case GOT_SERVICE_DELETED:
3215 /* The service started up and got a base address, but then
3216 * failed to register under EXISTENT_SERVICE_NAME
3218 CheckServiceOwnerChangedData socd;
3220 socd.expected_kind = SERVICE_DELETED;
3221 socd.expected_service_name = base_service;
3222 socd.failed = FALSE;
3223 socd.skip_connection = NULL;
3224 bus_test_clients_foreach (check_service_owner_changed_foreach,
3234 case GOT_SOMETHING_ELSE:
3235 _dbus_warn ("Unexpected message after auto activation\n");
3240 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3241 * come the method reply (or error) from the initial method call
3244 /* Note: if this test is run in OOM mode, it will block when the bus
3245 * doesn't send a reply due to OOM.
3247 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3249 message = pop_message_waiting_for_memory (connection);
3250 if (message == NULL)
3252 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3256 if (dbus_message_get_reply_serial (message) != serial)
3258 _dbus_warn ("Wrong reply serial\n");
3262 dbus_message_unref (message);
3265 if (!check_existent_ping (context, connection))
3268 if (!check_existent_get_machine_id (context, connection))
3271 if (!check_existent_hello_from_self (context, connection))
3274 if (!check_send_exit_to_service (context, connection,
3275 EXISTENT_SERVICE_NAME,
3283 dbus_message_unref (message);
3285 if (base_service_message)
3286 dbus_message_unref (base_service_message);
3291 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3293 /* returns TRUE if the correct thing happens,
3294 * but the correct thing may include OOM errors.
3297 check_launch_service_file_missing (BusContext *context,
3298 DBusConnection *connection)
3300 DBusMessage *message;
3301 dbus_uint32_t serial;
3304 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3305 "/org/freedesktop/TestSuite",
3306 "org.freedesktop.TestSuite",
3309 if (message == NULL)
3312 if (!dbus_connection_send (connection, message, &serial))
3314 dbus_message_unref (message);
3318 dbus_message_unref (message);
3321 bus_test_run_everything (context);
3322 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3323 bus_test_run_everything (context);
3325 if (!dbus_connection_get_is_connected (connection))
3327 _dbus_verbose ("connection was disconnected\n");
3333 message = pop_message_waiting_for_memory (connection);
3334 if (message == NULL)
3336 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3337 "Echo message (auto activation)", serial, connection);
3341 verbose_message_received (connection, message);
3343 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3345 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3347 _dbus_warn ("Message has wrong sender %s\n",
3348 dbus_message_get_sender (message) ?
3349 dbus_message_get_sender (message) : "(none)");
3353 if (dbus_message_is_error (message,
3354 DBUS_ERROR_NO_MEMORY))
3356 ; /* good, this is a valid response */
3358 else if (dbus_message_is_error (message,
3359 DBUS_ERROR_SERVICE_UNKNOWN))
3361 _dbus_verbose("got service unknown\n");
3362 ; /* good, this is expected (only valid when using launch helper) */
3366 warn_unexpected (connection, message, "not this error");
3373 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3381 dbus_message_unref (message);
3386 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3388 /* returns TRUE if the correct thing happens,
3389 * but the correct thing may include OOM errors.
3392 check_launch_service_user_missing (BusContext *context,
3393 DBusConnection *connection)
3395 DBusMessage *message;
3396 dbus_uint32_t serial;
3399 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3400 "/org/freedesktop/TestSuite",
3401 "org.freedesktop.TestSuite",
3404 if (message == NULL)
3407 if (!dbus_connection_send (connection, message, &serial))
3409 dbus_message_unref (message);
3413 dbus_message_unref (message);
3416 bus_test_run_everything (context);
3417 block_connection_until_message_from_bus (context, connection,
3418 "reply to service which should fail to auto-start (missing User)");
3419 bus_test_run_everything (context);
3421 if (!dbus_connection_get_is_connected (connection))
3423 _dbus_warn ("connection was disconnected\n");
3429 message = pop_message_waiting_for_memory (connection);
3430 if (message == NULL)
3432 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3433 "Echo message (auto activation)", serial, connection);
3437 verbose_message_received (connection, message);
3439 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3441 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3443 _dbus_warn ("Message has wrong sender %s\n",
3444 dbus_message_get_sender (message) ?
3445 dbus_message_get_sender (message) : "(none)");
3449 if (dbus_message_is_error (message,
3450 DBUS_ERROR_NO_MEMORY))
3452 ; /* good, this is a valid response */
3454 else if (dbus_message_is_error (message,
3455 DBUS_ERROR_SPAWN_FILE_INVALID))
3457 _dbus_verbose("got service file invalid\n");
3458 ; /* good, this is expected (only valid when using launch helper) */
3462 warn_unexpected (connection, message, "not this error");
3469 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3477 dbus_message_unref (message);
3482 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3484 /* returns TRUE if the correct thing happens,
3485 * but the correct thing may include OOM errors.
3488 check_launch_service_exec_missing (BusContext *context,
3489 DBusConnection *connection)
3491 DBusMessage *message;
3492 dbus_uint32_t serial;
3495 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3496 "/org/freedesktop/TestSuite",
3497 "org.freedesktop.TestSuite",
3500 if (message == NULL)
3503 if (!dbus_connection_send (connection, message, &serial))
3505 dbus_message_unref (message);
3509 dbus_message_unref (message);
3512 bus_test_run_everything (context);
3513 block_connection_until_message_from_bus (context, connection,
3514 "reply to service which should fail to auto-start (missing Exec)");
3515 bus_test_run_everything (context);
3517 if (!dbus_connection_get_is_connected (connection))
3519 _dbus_warn ("connection was disconnected\n");
3525 message = pop_message_waiting_for_memory (connection);
3526 if (message == NULL)
3528 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3529 "Echo message (auto activation)", serial, connection);
3533 verbose_message_received (connection, message);
3535 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3537 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3539 _dbus_warn ("Message has wrong sender %s\n",
3540 dbus_message_get_sender (message) ?
3541 dbus_message_get_sender (message) : "(none)");
3545 if (dbus_message_is_error (message,
3546 DBUS_ERROR_NO_MEMORY))
3548 ; /* good, this is a valid response */
3550 else if (dbus_message_is_error (message,
3551 DBUS_ERROR_SERVICE_UNKNOWN))
3553 _dbus_verbose("could not activate as invalid service file was not added\n");
3554 ; /* good, this is expected as we shouldn't have been added to
3555 * the activation list with a missing Exec key */
3557 else if (dbus_message_is_error (message,
3558 DBUS_ERROR_SPAWN_FILE_INVALID))
3560 _dbus_verbose("got service file invalid\n");
3561 ; /* good, this is allowed, and is the message passed back from the
3566 warn_unexpected (connection, message, "not this error");
3573 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3581 dbus_message_unref (message);
3586 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3588 /* returns TRUE if the correct thing happens,
3589 * but the correct thing may include OOM errors.
3592 check_launch_service_service_missing (BusContext *context,
3593 DBusConnection *connection)
3595 DBusMessage *message;
3596 dbus_uint32_t serial;
3599 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3600 "/org/freedesktop/TestSuite",
3601 "org.freedesktop.TestSuite",
3604 if (message == NULL)
3607 if (!dbus_connection_send (connection, message, &serial))
3609 dbus_message_unref (message);
3613 dbus_message_unref (message);
3616 bus_test_run_everything (context);
3617 block_connection_until_message_from_bus (context, connection,
3618 "reply to service which should fail to auto-start (missing Service)");
3619 bus_test_run_everything (context);
3621 if (!dbus_connection_get_is_connected (connection))
3623 _dbus_warn ("connection was disconnected\n");
3629 message = pop_message_waiting_for_memory (connection);
3630 if (message == NULL)
3632 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3633 "Echo message (auto activation)", serial, connection);
3637 verbose_message_received (connection, message);
3639 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3641 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3643 _dbus_warn ("Message has wrong sender %s\n",
3644 dbus_message_get_sender (message) ?
3645 dbus_message_get_sender (message) : "(none)");
3649 if (dbus_message_is_error (message,
3650 DBUS_ERROR_NO_MEMORY))
3652 ; /* good, this is a valid response */
3654 else if (dbus_message_is_error (message,
3655 DBUS_ERROR_SERVICE_UNKNOWN))
3657 _dbus_verbose("could not activate as invalid service file was not added\n");
3658 ; /* good, this is expected as we shouldn't have been added to
3659 * the activation list with a missing Exec key */
3661 else if (dbus_message_is_error (message,
3662 DBUS_ERROR_SPAWN_FILE_INVALID))
3664 _dbus_verbose("got service file invalid\n");
3665 ; /* good, this is allowed, and is the message passed back from the
3670 warn_unexpected (connection, message, "not this error");
3677 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3685 dbus_message_unref (message);
3690 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3692 /* returns TRUE if the correct thing happens,
3693 * but the correct thing may include OOM errors.
3696 check_shell_fail_service_auto_start (BusContext *context,
3697 DBusConnection *connection)
3699 DBusMessage *message;
3700 dbus_uint32_t serial;
3703 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3704 "/org/freedesktop/TestSuite",
3705 "org.freedesktop.TestSuite",
3708 if (message == NULL)
3711 if (!dbus_connection_send (connection, message, &serial))
3713 dbus_message_unref (message);
3717 dbus_message_unref (message);
3720 bus_test_run_everything (context);
3721 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3722 bus_test_run_everything (context);
3724 if (!dbus_connection_get_is_connected (connection))
3726 _dbus_verbose ("connection was disconnected\n");
3732 message = pop_message_waiting_for_memory (connection);
3733 if (message == NULL)
3735 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3736 "Echo message (auto activation)", serial, connection);
3740 verbose_message_received (connection, message);
3742 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3744 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3746 _dbus_warn ("Message has wrong sender %s\n",
3747 dbus_message_get_sender (message) ?
3748 dbus_message_get_sender (message) : "(none)");
3752 if (dbus_message_is_error (message,
3753 DBUS_ERROR_NO_MEMORY))
3755 ; /* good, this is a valid response */
3757 else if (dbus_message_is_error (message,
3758 DBUS_ERROR_INVALID_ARGS))
3760 _dbus_verbose("got invalid args\n");
3761 ; /* good, this is expected also */
3765 warn_unexpected (connection, message, "not this error");
3772 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3780 dbus_message_unref (message);
3785 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3787 /* returns TRUE if the correct thing happens,
3788 * but the correct thing may include OOM errors.
3791 check_shell_service_success_auto_start (BusContext *context,
3792 DBusConnection *connection)
3794 DBusMessage *message;
3795 DBusMessage *base_service_message;
3796 dbus_uint32_t serial;
3798 const char *base_service;
3799 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3801 base_service_message = NULL;
3803 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3804 "/org/freedesktop/TestSuite",
3805 "org.freedesktop.TestSuite",
3808 if (message == NULL)
3811 if (!dbus_connection_send (connection, message, &serial))
3813 dbus_message_unref (message);
3817 dbus_message_unref (message);
3820 bus_test_run_everything (context);
3822 /* now wait for the message bus to hear back from the activated
3825 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3826 bus_test_run_everything (context);
3828 if (!dbus_connection_get_is_connected (connection))
3830 _dbus_verbose ("connection was disconnected\n");
3836 message = pop_message_waiting_for_memory (connection);
3837 if (message == NULL)
3839 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3840 serial, connection);
3844 verbose_message_received (connection, message);
3845 _dbus_verbose (" (after sending %s)\n", "auto start");
3847 /* we should get zero or two ServiceOwnerChanged signals */
3848 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3850 GotServiceInfo message_kind;
3852 if (!check_base_service_activated (context, connection,
3853 message, &base_service))
3856 base_service_message = message;
3859 /* We may need to block here for the test service to exit or finish up */
3860 block_connection_until_message_from_bus (context, connection, "service to exit");
3862 /* Should get a service creation notification for the activated
3863 * service name, or a service deletion on the base service name
3865 message = dbus_connection_borrow_message (connection);
3866 if (message == NULL)
3868 _dbus_warn ("No message after auto activation "
3869 "(should be a service announcement)\n");
3870 dbus_connection_return_message (connection, message);
3875 message_kind = check_got_service_info (message);
3877 dbus_connection_return_message (connection, message);
3880 switch (message_kind)
3882 case GOT_SERVICE_CREATED:
3883 message = pop_message_waiting_for_memory (connection);
3884 if (message == NULL)
3886 _dbus_warn ("Failed to pop message we just put back! "
3887 "should have been a NameOwnerChanged (creation)\n");
3891 /* Check that ServiceOwnerChanged (creation) was correctly received */
3892 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3893 base_service, message))
3896 dbus_message_unref (message);
3901 case GOT_SERVICE_DELETED:
3903 /* The service started up and got a base address, but then
3904 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3906 CheckServiceOwnerChangedData socd;
3908 socd.expected_kind = SERVICE_DELETED;
3909 socd.expected_service_name = base_service;
3910 socd.failed = FALSE;
3911 socd.skip_connection = NULL;
3912 bus_test_clients_foreach (check_service_owner_changed_foreach,
3922 case GOT_SOMETHING_ELSE:
3923 _dbus_warn ("Unexpected message after auto activation\n");
3928 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3929 * come the method reply (or error) from the initial method call
3932 /* Note: if this test is run in OOM mode, it will block when the bus
3933 * doesn't send a reply due to OOM.
3935 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3937 message = pop_message_waiting_for_memory (connection);
3938 if (message == NULL)
3940 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3944 if (dbus_message_get_reply_serial (message) != serial)
3946 _dbus_warn ("Wrong reply serial\n");
3950 if (!dbus_message_get_args (message, NULL,
3951 DBUS_TYPE_STRING, &argv[0],
3952 DBUS_TYPE_STRING, &argv[1],
3953 DBUS_TYPE_STRING, &argv[2],
3954 DBUS_TYPE_STRING, &argv[3],
3955 DBUS_TYPE_STRING, &argv[4],
3956 DBUS_TYPE_STRING, &argv[5],
3957 DBUS_TYPE_STRING, &argv[6],
3960 _dbus_warn ("Error getting arguments from return\n");
3964 /* don't worry about arg[0] as it may be different
3965 depending on the path to the tests
3967 if (strcmp("-test", argv[1]) != 0)
3969 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3974 if (strcmp("that", argv[2]) != 0)
3976 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3981 if (strcmp("we get", argv[3]) != 0)
3983 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3988 if (strcmp("back", argv[4]) != 0)
3990 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3995 if (strcmp("--what", argv[5]) != 0)
3997 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
4002 if (strcmp("we put in", argv[6]) != 0)
4004 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
4005 "we put in", argv[6]);
4009 dbus_message_unref (message);
4012 if (!check_send_exit_to_service (context, connection,
4013 SHELL_SUCCESS_SERVICE_NAME,
4021 dbus_message_unref (message);
4023 if (base_service_message)
4024 dbus_message_unref (base_service_message);
4032 BusContext *context;
4036 check_oom_check1_func (void *data)
4038 Check1Data *d = data;
4040 if (! (* d->func) (d->context))
4043 if (!check_no_leftovers (d->context))
4045 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4053 check1_try_iterations (BusContext *context,
4054 const char *description,
4060 d.context = context;
4062 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4064 _dbus_assert_not_reached ("test failed");
4068 check_get_services (BusContext *context,
4069 DBusConnection *connection,
4074 DBusMessage *message;
4075 dbus_uint32_t serial;
4082 dbus_error_init (&error);
4085 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4087 DBUS_INTERFACE_DBUS,
4090 if (message == NULL)
4093 if (!dbus_connection_send (connection, message, &serial))
4095 dbus_message_unref (message);
4099 /* send our message */
4100 bus_test_run_clients_loop (SEND_PENDING (connection));
4102 dbus_message_unref (message);
4105 dbus_connection_ref (connection); /* because we may get disconnected */
4106 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4108 if (!dbus_connection_get_is_connected (connection))
4110 _dbus_verbose ("connection was disconnected\n");
4112 dbus_connection_unref (connection);
4117 dbus_connection_unref (connection);
4119 message = pop_message_waiting_for_memory (connection);
4120 if (message == NULL)
4122 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4123 method, serial, connection);
4127 verbose_message_received (connection, message);
4129 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4131 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4133 ; /* good, this is a valid response */
4137 warn_unexpected (connection, message, "not this error");
4144 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4146 ; /* good, expected */
4150 warn_unexpected (connection, message,
4151 "method_return for ListActivatableNames/ListNames");
4158 if (!dbus_message_get_args (message, &error,
4164 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4166 _dbus_verbose ("no memory to list services by %s\n", method);
4167 dbus_error_free (&error);
4168 _dbus_wait_for_memory ();
4169 goto retry_get_property;
4173 _dbus_assert (dbus_error_is_set (&error));
4174 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4183 if (!check_no_leftovers (context))
4189 dbus_error_free (&error);
4192 dbus_message_unref (message);
4197 /* returns TRUE if the correct thing happens,
4198 * but the correct thing may include OOM errors.
4201 check_list_services (BusContext *context,
4202 DBusConnection *connection)
4204 DBusMessage *message;
4205 DBusMessage *base_service_message;
4206 const char *base_service;
4207 dbus_uint32_t serial;
4209 const char *existent = EXISTENT_SERVICE_NAME;
4210 dbus_uint32_t flags;
4214 _dbus_verbose ("check_list_services for %p\n", connection);
4216 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4221 if (!_dbus_string_array_contains ((const char **)services, existent))
4223 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4224 dbus_free_string_array (services);
4228 dbus_free_string_array (services);
4230 base_service_message = NULL;
4232 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4234 DBUS_INTERFACE_DBUS,
4235 "StartServiceByName");
4237 if (message == NULL)
4240 dbus_message_set_auto_start (message, FALSE);
4243 if (!dbus_message_append_args (message,
4244 DBUS_TYPE_STRING, &existent,
4245 DBUS_TYPE_UINT32, &flags,
4248 dbus_message_unref (message);
4252 if (!dbus_connection_send (connection, message, &serial))
4254 dbus_message_unref (message);
4258 dbus_message_unref (message);
4261 bus_test_run_everything (context);
4263 /* now wait for the message bus to hear back from the activated
4266 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4268 bus_test_run_everything (context);
4270 if (!dbus_connection_get_is_connected (connection))
4272 _dbus_verbose ("connection was disconnected\n");
4278 message = pop_message_waiting_for_memory (connection);
4279 if (message == NULL)
4281 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4282 "StartServiceByName", serial, connection);
4286 verbose_message_received (connection, message);
4287 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4289 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4291 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4293 _dbus_warn ("Message has wrong sender %s\n",
4294 dbus_message_get_sender (message) ?
4295 dbus_message_get_sender (message) : "(none)");
4299 if (dbus_message_is_error (message,
4300 DBUS_ERROR_NO_MEMORY))
4302 ; /* good, this is a valid response */
4304 else if (dbus_message_is_error (message,
4305 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4306 dbus_message_is_error (message,
4307 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4308 dbus_message_is_error (message,
4309 DBUS_ERROR_SPAWN_EXEC_FAILED))
4311 ; /* good, this is expected also */
4315 _dbus_warn ("Did not expect error %s\n",
4316 dbus_message_get_error_name (message));
4322 GotServiceInfo message_kind;
4324 if (!check_base_service_activated (context, connection,
4325 message, &base_service))
4328 base_service_message = message;
4331 /* We may need to block here for the test service to exit or finish up */
4332 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4334 message = dbus_connection_borrow_message (connection);
4335 if (message == NULL)
4337 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4341 message_kind = check_got_service_info (message);
4343 dbus_connection_return_message (connection, message);
4346 switch (message_kind)
4348 case GOT_SOMETHING_ELSE:
4350 case GOT_SERVICE_DELETED:
4351 _dbus_warn ("Unexpected message after ActivateService "
4352 "(should be an error or a service announcement)\n");
4355 case GOT_SERVICE_CREATED:
4356 message = pop_message_waiting_for_memory (connection);
4357 if (message == NULL)
4359 _dbus_warn ("Failed to pop message we just put back! "
4360 "should have been a NameOwnerChanged (creation)\n");
4364 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4365 base_service, message))
4368 dbus_message_unref (message);
4371 if (!check_no_leftovers (context))
4373 _dbus_warn ("Messages were left over after successful activation\n");
4381 if (!check_get_services (context, connection, "ListNames", &services, &len))
4386 if (!_dbus_string_array_contains ((const char **)services, existent))
4388 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4392 dbus_free_string_array (services);
4394 if (!check_send_exit_to_service (context, connection,
4395 EXISTENT_SERVICE_NAME, base_service))
4402 dbus_message_unref (message);
4404 if (base_service_message)
4405 dbus_message_unref (base_service_message);
4413 BusContext *context;
4414 DBusConnection *connection;
4418 check_oom_check2_func (void *data)
4420 Check2Data *d = data;
4422 if (! (* d->func) (d->context, d->connection))
4425 if (!check_no_leftovers (d->context))
4427 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4435 check2_try_iterations (BusContext *context,
4436 DBusConnection *connection,
4437 const char *description,
4443 d.context = context;
4444 d.connection = connection;
4446 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4449 _dbus_warn ("%s failed during oom\n", description);
4450 _dbus_assert_not_reached ("test failed");
4455 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4456 const char *filename)
4461 if (!_dbus_string_init (&full))
4464 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4466 _dbus_string_free (&full);
4470 _dbus_string_init_const (&file, filename);
4472 if (!_dbus_concat_dir_and_file (&full, &file))
4474 _dbus_string_free (&full);
4478 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4479 _dbus_string_get_const_data (&full));
4481 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4483 _dbus_string_free (&full);
4489 bus_dispatch_test_conf (const DBusString *test_data_dir,
4490 const char *filename,
4491 dbus_bool_t use_launcher)
4493 BusContext *context;
4494 DBusConnection *foo;
4495 DBusConnection *bar;
4496 DBusConnection *baz;
4499 /* save the config name for the activation helper */
4500 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4501 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4503 dbus_error_init (&error);
4505 context = bus_context_new_test (test_data_dir, filename);
4506 if (context == NULL)
4509 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4511 _dbus_assert_not_reached ("could not alloc connection");
4513 if (!bus_setup_debug_client (foo))
4514 _dbus_assert_not_reached ("could not set up connection");
4516 spin_connection_until_authenticated (context, foo);
4518 if (!check_hello_message (context, foo))
4519 _dbus_assert_not_reached ("hello message failed");
4521 if (!check_double_hello_message (context, foo))
4522 _dbus_assert_not_reached ("double hello message failed");
4524 if (!check_add_match_all (context, foo))
4525 _dbus_assert_not_reached ("AddMatch message failed");
4527 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4529 _dbus_assert_not_reached ("could not alloc connection");
4531 if (!bus_setup_debug_client (bar))
4532 _dbus_assert_not_reached ("could not set up connection");
4534 spin_connection_until_authenticated (context, bar);
4536 if (!check_hello_message (context, bar))
4537 _dbus_assert_not_reached ("hello message failed");
4539 if (!check_add_match_all (context, bar))
4540 _dbus_assert_not_reached ("AddMatch message failed");
4542 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4544 _dbus_assert_not_reached ("could not alloc connection");
4546 if (!bus_setup_debug_client (baz))
4547 _dbus_assert_not_reached ("could not set up connection");
4549 spin_connection_until_authenticated (context, baz);
4551 if (!check_hello_message (context, baz))
4552 _dbus_assert_not_reached ("hello message failed");
4554 if (!check_add_match_all (context, baz))
4555 _dbus_assert_not_reached ("AddMatch message failed");
4557 #ifdef DBUS_WIN_FIXME
4558 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4559 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4561 if (!check_get_connection_unix_user (context, baz))
4562 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4564 if (!check_get_connection_unix_process_id (context, baz))
4565 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4568 if (!check_list_services (context, baz))
4569 _dbus_assert_not_reached ("ListActivatableNames message failed");
4571 if (!check_no_leftovers (context))
4573 _dbus_warn ("Messages were left over after setting up initial connections\n");
4574 _dbus_assert_not_reached ("initial connection setup failed");
4577 check1_try_iterations (context, "create_and_hello",
4578 check_hello_connection);
4580 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4581 check_nonexistent_service_no_auto_start);
4583 #ifdef DBUS_WIN_FIXME
4584 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4586 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4587 check_segfault_service_no_auto_start);
4590 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4591 check_existent_service_no_auto_start);
4593 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4594 check_nonexistent_service_auto_start);
4597 #ifdef DBUS_WIN_FIXME
4598 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4600 /* only do the segfault test if we are not using the launcher */
4601 check2_try_iterations (context, foo, "segfault_service_auto_start",
4602 check_segfault_service_auto_start);
4605 /* only do the shell fail test if we are not using the launcher */
4606 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4607 check_shell_fail_service_auto_start);
4609 /* specific to launcher */
4611 if (!check_launch_service_file_missing (context, foo))
4612 _dbus_assert_not_reached ("did not get service file not found error");
4615 /* Note: need to resolve some issues with the testing code in order to run
4616 * this in oom (handle that we sometimes don't get replies back from the bus
4617 * when oom happens, without blocking the test).
4619 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4620 check_existent_service_auto_start);
4623 if (!check_existent_service_auto_start (context, foo))
4624 _dbus_assert_not_reached ("existent service auto start failed");
4626 if (!check_shell_service_success_auto_start (context, foo))
4627 _dbus_assert_not_reached ("shell success service auto start failed");
4629 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4631 kill_client_connection_unchecked (foo);
4632 kill_client_connection_unchecked (bar);
4633 kill_client_connection_unchecked (baz);
4635 bus_context_unref (context);
4641 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4642 const char *filename)
4644 BusContext *context;
4645 DBusConnection *foo;
4648 /* save the config name for the activation helper */
4649 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4650 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4652 dbus_error_init (&error);
4654 context = bus_context_new_test (test_data_dir, filename);
4655 if (context == NULL)
4658 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4660 _dbus_assert_not_reached ("could not alloc connection");
4662 if (!bus_setup_debug_client (foo))
4663 _dbus_assert_not_reached ("could not set up connection");
4665 spin_connection_until_authenticated (context, foo);
4667 if (!check_hello_message (context, foo))
4668 _dbus_assert_not_reached ("hello message failed");
4670 if (!check_double_hello_message (context, foo))
4671 _dbus_assert_not_reached ("double hello message failed");
4673 if (!check_add_match_all (context, foo))
4674 _dbus_assert_not_reached ("AddMatch message failed");
4676 /* this only tests the activation.c user check */
4677 if (!check_launch_service_user_missing (context, foo))
4678 _dbus_assert_not_reached ("user missing did not trigger error");
4680 /* this only tests the desktop.c exec check */
4681 if (!check_launch_service_exec_missing (context, foo))
4682 _dbus_assert_not_reached ("exec missing did not trigger error");
4684 /* this only tests the desktop.c service check */
4685 if (!check_launch_service_service_missing (context, foo))
4686 _dbus_assert_not_reached ("service missing did not trigger error");
4688 _dbus_verbose ("Disconnecting foo\n");
4690 kill_client_connection_unchecked (foo);
4692 bus_context_unref (context);
4698 bus_dispatch_test (const DBusString *test_data_dir)
4700 /* run normal activation tests */
4701 _dbus_verbose ("Normal activation tests\n");
4702 if (!bus_dispatch_test_conf (test_data_dir,
4703 "valid-config-files/debug-allow-all.conf", FALSE))
4707 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4709 /* run launch-helper activation tests */
4710 _dbus_verbose ("Launch helper activation tests\n");
4711 if (!bus_dispatch_test_conf (test_data_dir,
4712 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4715 /* run select launch-helper activation tests on broken service files */
4716 if (!bus_dispatch_test_conf_fail (test_data_dir,
4717 "valid-config-files-system/debug-allow-all-fail.conf"))
4725 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4727 BusContext *context;
4728 DBusConnection *foo;
4731 dbus_error_init (&error);
4733 /* Test SHA1 authentication */
4734 _dbus_verbose ("Testing SHA1 context\n");
4736 context = bus_context_new_test (test_data_dir,
4737 "valid-config-files/debug-allow-all-sha1.conf");
4738 if (context == NULL)
4741 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4743 _dbus_assert_not_reached ("could not alloc connection");
4745 if (!bus_setup_debug_client (foo))
4746 _dbus_assert_not_reached ("could not set up connection");
4748 spin_connection_until_authenticated (context, foo);
4750 if (!check_hello_message (context, foo))
4751 _dbus_assert_not_reached ("hello message failed");
4753 if (!check_add_match_all (context, foo))
4754 _dbus_assert_not_reached ("addmatch message failed");
4756 if (!check_no_leftovers (context))
4758 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4759 _dbus_assert_not_reached ("initial connection setup failed");
4762 check1_try_iterations (context, "create_and_hello_sha1",
4763 check_hello_connection);
4765 kill_client_connection_unchecked (foo);
4767 bus_context_unref (context);
4772 #ifdef HAVE_UNIX_FD_PASSING
4775 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4777 BusContext *context;
4778 DBusConnection *foo, *bar;
4781 int one[2], two[2], x, y, z;
4784 dbus_error_init (&error);
4786 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4787 if (context == NULL)
4788 _dbus_assert_not_reached ("could not alloc context");
4790 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4792 _dbus_assert_not_reached ("could not alloc connection");
4794 if (!bus_setup_debug_client (foo))
4795 _dbus_assert_not_reached ("could not set up connection");
4797 spin_connection_until_authenticated (context, foo);
4799 if (!check_hello_message (context, foo))
4800 _dbus_assert_not_reached ("hello message failed");
4802 if (!check_add_match_all (context, foo))
4803 _dbus_assert_not_reached ("AddMatch message failed");
4805 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4807 _dbus_assert_not_reached ("could not alloc connection");
4809 if (!bus_setup_debug_client (bar))
4810 _dbus_assert_not_reached ("could not set up connection");
4812 spin_connection_until_authenticated (context, bar);
4814 if (!check_hello_message (context, bar))
4815 _dbus_assert_not_reached ("hello message failed");
4817 if (!check_add_match_all (context, bar))
4818 _dbus_assert_not_reached ("AddMatch message failed");
4820 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4821 _dbus_assert_not_reached ("could not alloc message");
4823 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4824 _dbus_assert_not_reached("Failed to allocate pipe #1");
4826 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4827 _dbus_assert_not_reached("Failed to allocate pipe #2");
4829 if (!dbus_message_append_args(m,
4830 DBUS_TYPE_UNIX_FD, one,
4831 DBUS_TYPE_UNIX_FD, two,
4832 DBUS_TYPE_UNIX_FD, two,
4834 _dbus_assert_not_reached("Failed to attach fds.");
4836 if (!_dbus_close(one[0], &error))
4837 _dbus_assert_not_reached("Failed to close pipe #1 ");
4838 if (!_dbus_close(two[0], &error))
4839 _dbus_assert_not_reached("Failed to close pipe #2 ");
4841 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4842 _dbus_assert_not_reached("Connection cannot do fd passing");
4844 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4845 _dbus_assert_not_reached("Connection cannot do fd passing");
4847 if (!dbus_connection_send (foo, m, NULL))
4848 _dbus_assert_not_reached("Failed to send fds");
4850 dbus_message_unref(m);
4852 bus_test_run_clients_loop (SEND_PENDING (foo));
4854 bus_test_run_everything (context);
4856 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4858 if (!(m = pop_message_waiting_for_memory (foo)))
4859 _dbus_assert_not_reached("Failed to receive msg");
4861 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4862 _dbus_assert_not_reached("bogus message received");
4864 dbus_message_unref(m);
4866 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4868 if (!(m = pop_message_waiting_for_memory (bar)))
4869 _dbus_assert_not_reached("Failed to receive msg");
4871 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4872 _dbus_assert_not_reached("bogus message received");
4874 if (!dbus_message_get_args(m,
4876 DBUS_TYPE_UNIX_FD, &x,
4877 DBUS_TYPE_UNIX_FD, &y,
4878 DBUS_TYPE_UNIX_FD, &z,
4880 _dbus_assert_not_reached("Failed to parse fds.");
4882 dbus_message_unref(m);
4884 if (write(x, "X", 1) != 1)
4885 _dbus_assert_not_reached("Failed to write to pipe #1");
4886 if (write(y, "Y", 1) != 1)
4887 _dbus_assert_not_reached("Failed to write to pipe #2");
4888 if (write(z, "Z", 1) != 1)
4889 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4891 if (!_dbus_close(x, &error))
4892 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4893 if (!_dbus_close(y, &error))
4894 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4895 if (!_dbus_close(z, &error))
4896 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4898 if (read(one[1], &r, 1) != 1 || r != 'X')
4899 _dbus_assert_not_reached("Failed to read value from pipe.");
4900 if (read(two[1], &r, 1) != 1 || r != 'Y')
4901 _dbus_assert_not_reached("Failed to read value from pipe.");
4902 if (read(two[1], &r, 1) != 1 || r != 'Z')
4903 _dbus_assert_not_reached("Failed to read value from pipe.");
4905 if (!_dbus_close(one[1], &error))
4906 _dbus_assert_not_reached("Failed to close pipe #1 ");
4907 if (!_dbus_close(two[1], &error))
4908 _dbus_assert_not_reached("Failed to close pipe #2 ");
4910 _dbus_verbose ("Disconnecting foo\n");
4911 kill_client_connection_unchecked (foo);
4913 _dbus_verbose ("Disconnecting bar\n");
4914 kill_client_connection_unchecked (bar);
4916 bus_context_unref (context);
4922 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */