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
7 * Copyright (C) 2013 Samsung Electronics
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connection.h"
32 #include "activation.h"
37 #include <dbus/dbus-internals.h>
38 #include <dbus/dbus-misc.h>
40 #ifdef ENABLE_KDBUS_TRANSPORT
44 #ifdef HAVE_UNIX_FD_PASSING
45 #include <dbus/dbus-sysdeps-unix.h>
49 /* This is hard-coded in the files in valid-config-files-*. We have to use
50 * the debug-pipe transport because the tests in this file require that
51 * dbus_connection_open_private() does not block. */
52 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
55 send_one_message (DBusConnection *connection,
57 DBusConnection *sender,
58 DBusConnection *addressed_recipient,
60 BusTransaction *transaction,
63 if (!bus_context_check_security_policy (context, transaction,
69 return TRUE; /* silently don't send it */
71 if (dbus_message_contains_unix_fds(message) &&
72 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
73 return TRUE; /* silently don't send it */
75 if (!bus_transaction_send (transaction,
87 bus_dispatch_matches (BusTransaction *transaction,
88 DBusConnection *sender,
89 DBusConnection *addressed_recipient,
94 BusConnections *connections;
96 BusMatchmaker *matchmaker;
100 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
102 /* sender and recipient can both be NULL for the bus driver,
103 * or for signals with no particular recipient
106 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
107 _dbus_assert (dbus_message_get_sender (message) != NULL);
109 context = bus_transaction_get_context (transaction);
111 /* First, send the message to the addressed_recipient, if there is one. */
112 if (addressed_recipient != NULL)
114 if (!bus_context_check_security_policy (context, transaction,
115 sender, addressed_recipient,
120 if (dbus_message_contains_unix_fds (message) &&
121 !dbus_connection_can_send_type (addressed_recipient,
124 dbus_set_error (error,
125 DBUS_ERROR_NOT_SUPPORTED,
126 "Tried to send message with Unix file descriptors"
127 "to a client that doesn't support that.");
131 /* Dispatch the message */
132 if (!bus_transaction_send (transaction, addressed_recipient, message))
139 /* Now dispatch to others who look interested in this message */
140 connections = bus_transaction_get_connections (transaction);
141 dbus_error_init (&tmp_error);
142 matchmaker = bus_context_get_matchmaker (context);
145 if (!bus_matchmaker_get_recipients (matchmaker, connections,
146 sender, addressed_recipient, message,
153 link = _dbus_list_get_first_link (&recipients);
156 DBusConnection *dest;
160 if (!send_one_message (dest, context, sender, addressed_recipient,
161 message, transaction, &tmp_error))
164 link = _dbus_list_get_next_link (&recipients, link);
167 _dbus_list_clear (&recipients);
169 if (dbus_error_is_set (&tmp_error))
171 dbus_move_error (&tmp_error, error);
178 static DBusHandlerResult
179 bus_dispatch (DBusConnection *connection,
180 DBusMessage *message)
182 const char *sender, *service_name;
184 BusTransaction *transaction;
186 DBusHandlerResult result;
187 DBusConnection *addressed_recipient;
189 result = DBUS_HANDLER_RESULT_HANDLED;
192 addressed_recipient = NULL;
193 dbus_error_init (&error);
195 context = bus_connection_get_context (connection);
196 _dbus_assert (context != NULL);
198 /* If we can't even allocate an OOM error, we just go to sleep
201 while (!bus_connection_preallocate_oom_error (connection))
202 _dbus_wait_for_memory ();
204 /* Ref connection in case we disconnect it at some point in here */
205 dbus_connection_ref (connection);
207 service_name = dbus_message_get_destination (message);
209 #ifdef DBUS_ENABLE_VERBOSE_MODE
211 const char *interface_name, *member_name, *error_name;
213 interface_name = dbus_message_get_interface (message);
214 member_name = dbus_message_get_member (message);
215 error_name = dbus_message_get_error_name (message);
217 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
218 interface_name ? interface_name : "(no interface)",
219 member_name ? member_name : "(no member)",
220 error_name ? error_name : "(no error name)",
221 service_name ? service_name : "peer");
223 #endif /* DBUS_ENABLE_VERBOSE_MODE */
225 /* Create our transaction */
226 transaction = bus_transaction_new (context);
227 if (transaction == NULL)
229 BUS_SET_OOM (&error);
233 /* If service_name is NULL, if it's a signal we send it to all
234 * connections with a match rule. If it's not a signal, there
235 * are some special cases here but mostly we just bail out.
237 if (service_name == NULL)
239 if (dbus_message_is_signal (message,
240 DBUS_INTERFACE_LOCAL,
243 bus_connection_disconnected (connection);
247 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
249 /* DBusConnection also handles some of these automatically, we leave
252 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
256 #ifdef ENABLE_KDBUS_TRANSPORT
257 if(bus_context_is_kdbus(context))
259 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
261 handleNameOwnerChanged(message, transaction, connection);
268 #ifdef ENABLE_KDBUS_TRANSPORT
269 /* Assign a sender to the message */
270 if(bus_context_is_kdbus(context) == FALSE) //if using kdbus, sender must be set on library side
273 if (bus_connection_is_active (connection))
275 sender = bus_connection_get_name (connection);
276 _dbus_assert (sender != NULL);
278 if (!dbus_message_set_sender (message, sender))
280 BUS_SET_OOM (&error);
284 /* We need to refetch the service name here, because
285 * dbus_message_set_sender can cause the header to be
286 * reallocated, and thus the service_name pointer will become
289 service_name = dbus_message_get_destination (message);
294 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
296 if (!bus_context_check_security_policy (context, transaction,
297 connection, NULL, NULL, message, &error))
299 _dbus_verbose ("Security policy rejected message\n");
303 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
304 if (!bus_driver_handle_message (connection, transaction, message, &error))
307 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
309 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
310 dbus_connection_close (connection);
313 else if (service_name != NULL) /* route to named service */
315 DBusString service_string;
317 BusRegistry *registry;
319 _dbus_assert (service_name != NULL);
321 registry = bus_connection_get_registry (connection);
323 _dbus_string_init_const (&service_string, service_name);
324 service = bus_registry_lookup (registry, &service_string);
326 #ifdef ENABLE_KDBUS_TRANSPORT
327 if (dbus_message_get_auto_start (message) && (service == NULL || bus_service_get_is_kdbus_starter(service)))
329 if (service == NULL && dbus_message_get_auto_start (message))
332 BusActivation *activation;
333 /* We can't do the security policy check here, since the addressed
334 * recipient service doesn't exist yet. We do it before sending the
335 * message after the service has been created.
337 activation = bus_connection_get_activation (connection);
339 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
340 message, service_name, &error))
342 _DBUS_ASSERT_ERROR_IS_SET (&error);
343 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
349 else if (service == NULL)
351 dbus_set_error (&error,
352 DBUS_ERROR_NAME_HAS_NO_OWNER,
353 "Name \"%s\" does not exist",
359 addressed_recipient = bus_service_get_primary_owners_connection (service);
360 _dbus_assert (addressed_recipient != NULL);
364 /* Now send the message to its destination (or not, if
365 * addressed_recipient == NULL), and match it against other connections'
368 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
372 if (dbus_error_is_set (&error))
374 if (!dbus_connection_get_is_connected (connection))
376 /* If we disconnected it, we won't bother to send it any error
379 _dbus_verbose ("Not sending error to connection we disconnected\n");
381 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
383 bus_connection_send_oom_error (connection, message);
385 /* cancel transaction due to OOM */
386 if (transaction != NULL)
388 bus_transaction_cancel_and_free (transaction);
394 /* Try to send the real error, if no mem to do that, send
397 _dbus_assert (transaction != NULL);
398 if (!bus_transaction_send_error_reply (transaction, connection,
401 bus_connection_send_oom_error (connection, message);
403 /* cancel transaction due to OOM */
404 if (transaction != NULL)
406 bus_transaction_cancel_and_free (transaction);
413 dbus_error_free (&error);
416 if (transaction != NULL)
418 bus_transaction_execute_and_free (transaction);
421 dbus_connection_unref (connection);
426 static DBusHandlerResult
427 bus_dispatch_message_filter (DBusConnection *connection,
428 DBusMessage *message,
431 return bus_dispatch (connection, message);
435 bus_dispatch_add_connection (DBusConnection *connection)
437 if (!dbus_connection_add_filter (connection,
438 bus_dispatch_message_filter,
446 bus_dispatch_remove_connection (DBusConnection *connection)
448 /* Here we tell the bus driver that we want to get off. */
449 bus_driver_remove_connection (connection);
451 dbus_connection_remove_filter (connection,
452 bus_dispatch_message_filter,
456 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
460 /* This is used to know whether we need to block in order to finish
461 * sending a message, or whether the initial dbus_connection_send()
462 * already flushed the queue.
464 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
466 typedef dbus_bool_t (* Check1Func) (BusContext *context);
467 typedef dbus_bool_t (* Check2Func) (BusContext *context,
468 DBusConnection *connection);
470 static dbus_bool_t check_no_leftovers (BusContext *context);
473 block_connection_until_message_from_bus (BusContext *context,
474 DBusConnection *connection,
475 const char *what_is_expected)
477 _dbus_verbose ("expecting: %s\n", what_is_expected);
479 while (dbus_connection_get_dispatch_status (connection) ==
480 DBUS_DISPATCH_COMPLETE &&
481 dbus_connection_get_is_connected (connection))
483 bus_test_run_bus_loop (context, TRUE);
484 bus_test_run_clients_loop (FALSE);
489 spin_connection_until_authenticated (BusContext *context,
490 DBusConnection *connection)
492 _dbus_verbose ("Spinning to auth connection %p\n", connection);
493 while (!dbus_connection_get_is_authenticated (connection) &&
494 dbus_connection_get_is_connected (connection))
496 bus_test_run_bus_loop (context, FALSE);
497 bus_test_run_clients_loop (FALSE);
499 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
502 /* compensate for fact that pop_message() can return #NULL due to OOM */
504 pop_message_waiting_for_memory (DBusConnection *connection)
506 while (dbus_connection_get_dispatch_status (connection) ==
507 DBUS_DISPATCH_NEED_MEMORY)
508 _dbus_wait_for_memory ();
510 return dbus_connection_pop_message (connection);
514 borrow_message_waiting_for_memory (DBusConnection *connection)
516 while (dbus_connection_get_dispatch_status (connection) ==
517 DBUS_DISPATCH_NEED_MEMORY)
518 _dbus_wait_for_memory ();
520 return dbus_connection_borrow_message (connection);
524 warn_unexpected_real (DBusConnection *connection,
525 DBusMessage *message,
526 const char *expected,
527 const char *function,
531 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
533 dbus_message_get_interface (message) ?
534 dbus_message_get_interface (message) : "(unset)",
535 dbus_message_get_member (message) ?
536 dbus_message_get_member (message) : "(unset)",
537 dbus_message_get_error_name (message) ?
538 dbus_message_get_error_name (message) : "(unset)",
542 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
543 function, line, connection, expected);
546 #define warn_unexpected(connection, message, expected) \
547 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
550 verbose_message_received (DBusConnection *connection,
551 DBusMessage *message)
553 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
554 dbus_message_get_interface (message) ?
555 dbus_message_get_interface (message) : "(unset)",
556 dbus_message_get_member (message) ?
557 dbus_message_get_member (message) : "(unset)",
558 dbus_message_get_error_name (message) ?
559 dbus_message_get_error_name (message) : "(unset)",
572 ServiceInfoKind expected_kind;
573 const char *expected_service_name;
575 DBusConnection *skip_connection;
576 } CheckServiceOwnerChangedData;
579 check_service_owner_changed_foreach (DBusConnection *connection,
582 CheckServiceOwnerChangedData *d = data;
583 DBusMessage *message;
585 const char *service_name, *old_owner, *new_owner;
587 if (d->expected_kind == SERVICE_CREATED
588 && connection == d->skip_connection)
591 dbus_error_init (&error);
594 message = pop_message_waiting_for_memory (connection);
597 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
598 connection, "NameOwnerChanged");
601 else if (!dbus_message_is_signal (message,
605 warn_unexpected (connection, message, "NameOwnerChanged");
611 reget_service_info_data:
616 dbus_message_get_args (message, &error,
617 DBUS_TYPE_STRING, &service_name,
618 DBUS_TYPE_STRING, &old_owner,
619 DBUS_TYPE_STRING, &new_owner,
622 if (dbus_error_is_set (&error))
624 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
626 dbus_error_free (&error);
627 _dbus_wait_for_memory ();
628 goto reget_service_info_data;
632 _dbus_warn ("Did not get the expected arguments\n");
637 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
638 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
639 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
641 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
645 if (strcmp (service_name, d->expected_service_name) != 0)
647 _dbus_warn ("expected info on service %s, got info on %s\n",
648 d->expected_service_name,
653 if (*service_name == ':' && new_owner[0]
654 && strcmp (service_name, new_owner) != 0)
656 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
657 service_name, old_owner, new_owner);
665 dbus_error_free (&error);
668 dbus_message_unref (message);
675 kill_client_connection (BusContext *context,
676 DBusConnection *connection)
680 CheckServiceOwnerChangedData socd;
682 _dbus_verbose ("killing connection %p\n", connection);
684 s = dbus_bus_get_unique_name (connection);
685 _dbus_assert (s != NULL);
687 while ((base_service = _dbus_strdup (s)) == NULL)
688 _dbus_wait_for_memory ();
690 dbus_connection_ref (connection);
692 /* kick in the disconnect handler that unrefs the connection */
693 dbus_connection_close (connection);
695 bus_test_run_everything (context);
697 _dbus_assert (bus_test_client_listed (connection));
699 /* Run disconnect handler in test.c */
700 if (bus_connection_dispatch_one_message (connection))
701 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
703 _dbus_assert (!dbus_connection_get_is_connected (connection));
704 dbus_connection_unref (connection);
706 _dbus_assert (!bus_test_client_listed (connection));
708 socd.expected_kind = SERVICE_DELETED;
709 socd.expected_service_name = base_service;
711 socd.skip_connection = NULL;
713 bus_test_clients_foreach (check_service_owner_changed_foreach,
716 dbus_free (base_service);
719 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
721 if (!check_no_leftovers (context))
722 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
726 kill_client_connection_unchecked (DBusConnection *connection)
728 /* This kills the connection without expecting it to affect
729 * the rest of the bus.
731 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
733 dbus_connection_ref (connection);
734 dbus_connection_close (connection);
735 /* dispatching disconnect handler will unref once */
736 if (bus_connection_dispatch_one_message (connection))
737 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
739 _dbus_assert (!bus_test_client_listed (connection));
740 dbus_connection_unref (connection);
746 } CheckNoMessagesData;
749 check_no_messages_foreach (DBusConnection *connection,
752 CheckNoMessagesData *d = data;
753 DBusMessage *message;
755 message = pop_message_waiting_for_memory (connection);
758 warn_unexpected (connection, message, "no messages");
764 dbus_message_unref (message);
769 check_no_leftovers (BusContext *context)
771 CheckNoMessagesData nmd;
774 bus_test_clients_foreach (check_no_messages_foreach,
779 _dbus_verbose ("leftover message found\n");
786 /* returns TRUE if the correct thing happens,
787 * but the correct thing may include OOM errors.
790 check_hello_message (BusContext *context,
791 DBusConnection *connection)
793 DBusMessage *message;
794 DBusMessage *name_message;
795 dbus_uint32_t serial;
799 const char *acquired;
802 dbus_error_init (&error);
808 _dbus_verbose ("check_hello_message for %p\n", connection);
810 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
818 dbus_connection_ref (connection); /* because we may get disconnected */
820 if (!dbus_connection_send (connection, message, &serial))
822 dbus_message_unref (message);
823 dbus_connection_unref (connection);
827 _dbus_assert (dbus_message_has_signature (message, ""));
829 dbus_message_unref (message);
832 if (!dbus_connection_get_is_connected (connection))
834 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
836 dbus_connection_unref (connection);
841 /* send our message */
842 bus_test_run_clients_loop (SEND_PENDING (connection));
844 if (!dbus_connection_get_is_connected (connection))
846 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
848 dbus_connection_unref (connection);
853 block_connection_until_message_from_bus (context, connection, "reply to Hello");
855 if (!dbus_connection_get_is_connected (connection))
857 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
859 dbus_connection_unref (connection);
864 dbus_connection_unref (connection);
866 message = pop_message_waiting_for_memory (connection);
869 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
870 "Hello", serial, connection);
874 verbose_message_received (connection, message);
876 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
878 _dbus_warn ("Message has wrong sender %s\n",
879 dbus_message_get_sender (message) ?
880 dbus_message_get_sender (message) : "(none)");
884 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
886 if (dbus_message_is_error (message,
887 DBUS_ERROR_NO_MEMORY))
889 ; /* good, this is a valid response */
893 warn_unexpected (connection, message, "not this error");
900 CheckServiceOwnerChangedData socd;
902 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
904 ; /* good, expected */
908 warn_unexpected (connection, message, "method return for Hello");
913 retry_get_hello_name:
914 if (!dbus_message_get_args (message, &error,
915 DBUS_TYPE_STRING, &name,
918 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
920 _dbus_verbose ("no memory to get service name arg from hello\n");
921 dbus_error_free (&error);
922 _dbus_wait_for_memory ();
923 goto retry_get_hello_name;
927 _dbus_assert (dbus_error_is_set (&error));
928 _dbus_warn ("Did not get the expected single string argument to hello\n");
933 _dbus_verbose ("Got hello name: %s\n", name);
935 while (!dbus_bus_set_unique_name (connection, name))
936 _dbus_wait_for_memory ();
938 socd.expected_kind = SERVICE_CREATED;
939 socd.expected_service_name = name;
941 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
942 bus_test_clients_foreach (check_service_owner_changed_foreach,
948 name_message = message;
949 /* Client should also have gotten ServiceAcquired */
951 message = pop_message_waiting_for_memory (connection);
954 _dbus_warn ("Expecting %s, got nothing\n",
958 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
961 _dbus_warn ("Expecting %s, got smthg else\n",
966 retry_get_acquired_name:
967 if (!dbus_message_get_args (message, &error,
968 DBUS_TYPE_STRING, &acquired,
971 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
973 _dbus_verbose ("no memory to get service name arg from acquired\n");
974 dbus_error_free (&error);
975 _dbus_wait_for_memory ();
976 goto retry_get_acquired_name;
980 _dbus_assert (dbus_error_is_set (&error));
981 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
986 _dbus_verbose ("Got acquired name: %s\n", acquired);
988 if (strcmp (acquired, name) != 0)
990 _dbus_warn ("Acquired name is %s but expected %s\n",
997 if (!check_no_leftovers (context))
1003 _dbus_verbose ("ending - retval = %d\n", retval);
1005 dbus_error_free (&error);
1008 dbus_message_unref (message);
1011 dbus_message_unref (name_message);
1016 /* returns TRUE if the correct thing happens,
1017 * but the correct thing may include OOM errors.
1020 check_double_hello_message (BusContext *context,
1021 DBusConnection *connection)
1023 DBusMessage *message;
1024 dbus_uint32_t serial;
1029 dbus_error_init (&error);
1032 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1034 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1036 DBUS_INTERFACE_DBUS,
1039 if (message == NULL)
1042 if (!dbus_connection_send (connection, message, &serial))
1044 dbus_message_unref (message);
1048 dbus_message_unref (message);
1051 /* send our message */
1052 bus_test_run_clients_loop (SEND_PENDING (connection));
1054 dbus_connection_ref (connection); /* because we may get disconnected */
1055 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1057 if (!dbus_connection_get_is_connected (connection))
1059 _dbus_verbose ("connection was disconnected\n");
1061 dbus_connection_unref (connection);
1066 dbus_connection_unref (connection);
1068 message = pop_message_waiting_for_memory (connection);
1069 if (message == NULL)
1071 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1072 "Hello", serial, connection);
1076 verbose_message_received (connection, message);
1078 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1080 _dbus_warn ("Message has wrong sender %s\n",
1081 dbus_message_get_sender (message) ?
1082 dbus_message_get_sender (message) : "(none)");
1086 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1088 warn_unexpected (connection, message, "method return for Hello");
1092 if (!check_no_leftovers (context))
1098 dbus_error_free (&error);
1101 dbus_message_unref (message);
1106 /* returns TRUE if the correct thing happens,
1107 * but the correct thing may include OOM errors.
1110 check_get_connection_unix_user (BusContext *context,
1111 DBusConnection *connection)
1113 DBusMessage *message;
1114 dbus_uint32_t serial;
1117 const char *base_service_name;
1121 dbus_error_init (&error);
1124 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1126 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1128 DBUS_INTERFACE_DBUS,
1129 "GetConnectionUnixUser");
1131 if (message == NULL)
1134 base_service_name = dbus_bus_get_unique_name (connection);
1136 if (!dbus_message_append_args (message,
1137 DBUS_TYPE_STRING, &base_service_name,
1140 dbus_message_unref (message);
1144 if (!dbus_connection_send (connection, message, &serial))
1146 dbus_message_unref (message);
1150 /* send our message */
1151 bus_test_run_clients_loop (SEND_PENDING (connection));
1153 dbus_message_unref (message);
1156 dbus_connection_ref (connection); /* because we may get disconnected */
1157 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1159 if (!dbus_connection_get_is_connected (connection))
1161 _dbus_verbose ("connection was disconnected\n");
1163 dbus_connection_unref (connection);
1168 dbus_connection_unref (connection);
1170 message = pop_message_waiting_for_memory (connection);
1171 if (message == NULL)
1173 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1174 "GetConnectionUnixUser", serial, connection);
1178 verbose_message_received (connection, message);
1180 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1182 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1184 ; /* good, this is a valid response */
1188 warn_unexpected (connection, message, "not this error");
1195 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1197 ; /* good, expected */
1201 warn_unexpected (connection, message,
1202 "method_return for GetConnectionUnixUser");
1209 if (!dbus_message_get_args (message, &error,
1210 DBUS_TYPE_UINT32, &uid,
1213 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1215 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1216 dbus_error_free (&error);
1217 _dbus_wait_for_memory ();
1218 goto retry_get_property;
1222 _dbus_assert (dbus_error_is_set (&error));
1223 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1229 if (!check_no_leftovers (context))
1235 dbus_error_free (&error);
1238 dbus_message_unref (message);
1243 /* returns TRUE if the correct thing happens,
1244 * but the correct thing may include OOM errors.
1247 check_get_connection_unix_process_id (BusContext *context,
1248 DBusConnection *connection)
1250 DBusMessage *message;
1251 dbus_uint32_t serial;
1254 const char *base_service_name;
1258 dbus_error_init (&error);
1261 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1263 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1265 DBUS_INTERFACE_DBUS,
1266 "GetConnectionUnixProcessID");
1268 if (message == NULL)
1271 base_service_name = dbus_bus_get_unique_name (connection);
1273 if (!dbus_message_append_args (message,
1274 DBUS_TYPE_STRING, &base_service_name,
1277 dbus_message_unref (message);
1281 if (!dbus_connection_send (connection, message, &serial))
1283 dbus_message_unref (message);
1287 /* send our message */
1288 bus_test_run_clients_loop (SEND_PENDING (connection));
1290 dbus_message_unref (message);
1293 dbus_connection_ref (connection); /* because we may get disconnected */
1294 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1296 if (!dbus_connection_get_is_connected (connection))
1298 _dbus_verbose ("connection was disconnected\n");
1300 dbus_connection_unref (connection);
1305 dbus_connection_unref (connection);
1307 message = pop_message_waiting_for_memory (connection);
1308 if (message == NULL)
1310 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1311 "GetConnectionUnixProcessID", serial, connection);
1315 verbose_message_received (connection, message);
1317 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1319 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1321 ; /* good, this is a valid response */
1324 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1326 /* We are expecting this error, since we know in the test suite we aren't
1327 * talking to a client running on UNIX
1329 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1334 warn_unexpected (connection, message, "not this error");
1342 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1345 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1347 ; /* good, expected */
1351 warn_unexpected (connection, message,
1352 "method_return for GetConnectionUnixProcessID");
1359 if (!dbus_message_get_args (message, &error,
1360 DBUS_TYPE_UINT32, &pid,
1363 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1365 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1366 dbus_error_free (&error);
1367 _dbus_wait_for_memory ();
1368 goto retry_get_property;
1372 _dbus_assert (dbus_error_is_set (&error));
1373 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1379 /* test if returned pid is the same as our own pid
1381 * @todo It would probably be good to restructure the tests
1382 * in a way so our parent is the bus that we're testing
1383 * cause then we can test that the pid returned matches
1386 if (pid != (dbus_uint32_t) _dbus_getpid ())
1388 _dbus_assert (dbus_error_is_set (&error));
1389 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1393 #endif /* !DBUS_WIN */
1396 if (!check_no_leftovers (context))
1402 dbus_error_free (&error);
1405 dbus_message_unref (message);
1410 /* returns TRUE if the correct thing happens,
1411 * but the correct thing may include OOM errors.
1414 check_add_match_all (BusContext *context,
1415 DBusConnection *connection)
1417 DBusMessage *message;
1419 dbus_uint32_t serial;
1421 const char *empty = "";
1424 dbus_error_init (&error);
1427 _dbus_verbose ("check_add_match_all for %p\n", connection);
1429 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1431 DBUS_INTERFACE_DBUS,
1434 if (message == NULL)
1437 /* empty string match rule matches everything */
1438 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1441 dbus_message_unref (message);
1445 if (!dbus_connection_send (connection, message, &serial))
1447 dbus_message_unref (message);
1451 dbus_message_unref (message);
1454 dbus_connection_ref (connection); /* because we may get disconnected */
1456 /* send our message */
1457 bus_test_run_clients_loop (SEND_PENDING (connection));
1459 if (!dbus_connection_get_is_connected (connection))
1461 _dbus_verbose ("connection was disconnected\n");
1463 dbus_connection_unref (connection);
1468 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1470 if (!dbus_connection_get_is_connected (connection))
1472 _dbus_verbose ("connection was disconnected\n");
1474 dbus_connection_unref (connection);
1479 dbus_connection_unref (connection);
1481 message = pop_message_waiting_for_memory (connection);
1482 if (message == NULL)
1484 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1485 "AddMatch", serial, connection);
1489 verbose_message_received (connection, message);
1491 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1493 _dbus_warn ("Message has wrong sender %s\n",
1494 dbus_message_get_sender (message) ?
1495 dbus_message_get_sender (message) : "(none)");
1499 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1501 if (dbus_message_is_error (message,
1502 DBUS_ERROR_NO_MEMORY))
1504 ; /* good, this is a valid response */
1508 warn_unexpected (connection, message, "not this error");
1515 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1517 ; /* good, expected */
1518 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1522 warn_unexpected (connection, message, "method return for AddMatch");
1528 if (!check_no_leftovers (context))
1534 dbus_error_free (&error);
1537 dbus_message_unref (message);
1542 /* returns TRUE if the correct thing happens,
1543 * but the correct thing may include OOM errors.
1546 check_hello_connection (BusContext *context)
1548 DBusConnection *connection;
1551 dbus_error_init (&error);
1553 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1554 if (connection == NULL)
1556 _DBUS_ASSERT_ERROR_IS_SET (&error);
1557 dbus_error_free (&error);
1561 if (!bus_setup_debug_client (connection))
1563 dbus_connection_close (connection);
1564 dbus_connection_unref (connection);
1568 spin_connection_until_authenticated (context, connection);
1570 if (!check_hello_message (context, connection))
1573 if (dbus_bus_get_unique_name (connection) == NULL)
1575 /* We didn't successfully register, so we can't
1576 * do the usual kill_client_connection() checks
1578 kill_client_connection_unchecked (connection);
1582 if (!check_add_match_all (context, connection))
1585 kill_client_connection (context, connection);
1591 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1593 /* returns TRUE if the correct thing happens,
1594 * but the correct thing may include OOM errors.
1597 check_nonexistent_service_no_auto_start (BusContext *context,
1598 DBusConnection *connection)
1600 DBusMessage *message;
1601 dbus_uint32_t serial;
1603 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1604 dbus_uint32_t flags;
1606 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1608 DBUS_INTERFACE_DBUS,
1609 "StartServiceByName");
1611 if (message == NULL)
1614 dbus_message_set_auto_start (message, FALSE);
1617 if (!dbus_message_append_args (message,
1618 DBUS_TYPE_STRING, &nonexistent,
1619 DBUS_TYPE_UINT32, &flags,
1622 dbus_message_unref (message);
1626 if (!dbus_connection_send (connection, message, &serial))
1628 dbus_message_unref (message);
1632 dbus_message_unref (message);
1635 bus_test_run_everything (context);
1636 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1637 bus_test_run_everything (context);
1639 if (!dbus_connection_get_is_connected (connection))
1641 _dbus_verbose ("connection was disconnected\n");
1647 message = pop_message_waiting_for_memory (connection);
1648 if (message == NULL)
1650 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1651 "StartServiceByName", serial, connection);
1655 verbose_message_received (connection, message);
1657 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1659 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1661 _dbus_warn ("Message has wrong sender %s\n",
1662 dbus_message_get_sender (message) ?
1663 dbus_message_get_sender (message) : "(none)");
1667 if (dbus_message_is_error (message,
1668 DBUS_ERROR_NO_MEMORY))
1670 ; /* good, this is a valid response */
1672 else if (dbus_message_is_error (message,
1673 DBUS_ERROR_SERVICE_UNKNOWN))
1675 ; /* good, this is expected also */
1679 warn_unexpected (connection, message, "not this error");
1685 _dbus_warn ("Did not expect to successfully activate %s\n",
1686 NONEXISTENT_SERVICE_NAME);
1694 dbus_message_unref (message);
1699 /* returns TRUE if the correct thing happens,
1700 * but the correct thing may include OOM errors.
1703 check_nonexistent_service_auto_start (BusContext *context,
1704 DBusConnection *connection)
1706 DBusMessage *message;
1707 dbus_uint32_t serial;
1710 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1711 "/org/freedesktop/TestSuite",
1712 "org.freedesktop.TestSuite",
1715 if (message == NULL)
1718 if (!dbus_connection_send (connection, message, &serial))
1720 dbus_message_unref (message);
1724 dbus_message_unref (message);
1727 bus_test_run_everything (context);
1728 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1729 bus_test_run_everything (context);
1731 if (!dbus_connection_get_is_connected (connection))
1733 _dbus_verbose ("connection was disconnected\n");
1739 message = pop_message_waiting_for_memory (connection);
1741 if (message == NULL)
1743 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1744 "Echo message (auto activation)", serial, connection);
1748 verbose_message_received (connection, message);
1750 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1752 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1754 _dbus_warn ("Message has wrong sender %s\n",
1755 dbus_message_get_sender (message) ?
1756 dbus_message_get_sender (message) : "(none)");
1760 if (dbus_message_is_error (message,
1761 DBUS_ERROR_NO_MEMORY))
1763 ; /* good, this is a valid response */
1765 else if (dbus_message_is_error (message,
1766 DBUS_ERROR_SERVICE_UNKNOWN))
1768 ; /* good, this is expected also */
1772 warn_unexpected (connection, message, "not this error");
1778 _dbus_warn ("Did not expect to successfully activate %s\n",
1779 NONEXISTENT_SERVICE_NAME);
1787 dbus_message_unref (message);
1793 check_base_service_activated (BusContext *context,
1794 DBusConnection *connection,
1795 DBusMessage *initial_message,
1796 const char **base_service_p)
1798 DBusMessage *message;
1801 const char *base_service, *base_service_from_bus, *old_owner;
1805 dbus_error_init (&error);
1806 base_service = NULL;
1808 base_service_from_bus = NULL;
1810 message = initial_message;
1811 dbus_message_ref (message);
1813 if (dbus_message_is_signal (message,
1814 DBUS_INTERFACE_DBUS,
1815 "NameOwnerChanged"))
1817 CheckServiceOwnerChangedData socd;
1819 reget_service_name_arg:
1820 base_service = NULL;
1822 base_service_from_bus = NULL;
1824 if (!dbus_message_get_args (message, &error,
1825 DBUS_TYPE_STRING, &base_service,
1826 DBUS_TYPE_STRING, &old_owner,
1827 DBUS_TYPE_STRING, &base_service_from_bus,
1830 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1832 dbus_error_free (&error);
1833 _dbus_wait_for_memory ();
1834 goto reget_service_name_arg;
1838 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1839 "NameOwnerChanged (creation)",
1845 if (*base_service != ':')
1847 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1852 if (strcmp (base_service, base_service_from_bus) != 0)
1854 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1855 base_service, base_service_from_bus);
1861 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1866 socd.expected_kind = SERVICE_CREATED;
1867 socd.expected_service_name = base_service;
1868 socd.failed = FALSE;
1869 socd.skip_connection = connection;
1870 bus_test_clients_foreach (check_service_owner_changed_foreach,
1878 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1884 *base_service_p = base_service;
1890 dbus_message_unref (message);
1891 dbus_error_free (&error);
1897 check_service_activated (BusContext *context,
1898 DBusConnection *connection,
1899 const char *activated_name,
1900 const char *base_service_name,
1901 DBusMessage *initial_message)
1903 DBusMessage *message;
1906 dbus_uint32_t activation_result;
1910 dbus_error_init (&error);
1912 message = initial_message;
1913 dbus_message_ref (message);
1915 if (dbus_message_is_signal (message,
1916 DBUS_INTERFACE_DBUS,
1917 "NameOwnerChanged"))
1919 CheckServiceOwnerChangedData socd;
1920 const char *service_name, *base_service_from_bus, *old_owner;
1922 reget_service_name_arg:
1923 service_name = NULL;
1925 base_service_from_bus = NULL;
1927 if (!dbus_message_get_args (message, &error,
1928 DBUS_TYPE_STRING, &service_name,
1929 DBUS_TYPE_STRING, &old_owner,
1930 DBUS_TYPE_STRING, &base_service_from_bus,
1933 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1935 dbus_error_free (&error);
1936 _dbus_wait_for_memory ();
1937 goto reget_service_name_arg;
1941 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1942 "NameOwnerChanged (creation)",
1948 if (strcmp (service_name, activated_name) != 0)
1950 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1951 activated_name, service_name);
1955 if (strcmp (base_service_name, base_service_from_bus) != 0)
1957 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1958 base_service_from_bus, base_service_name);
1964 _dbus_warn ("expected a %s, got a %s\n",
1965 "NameOwnerChanged (creation)",
1966 "NameOwnerChanged (change)");
1970 socd.expected_kind = SERVICE_CREATED;
1971 socd.skip_connection = connection;
1972 socd.failed = FALSE;
1973 socd.expected_service_name = service_name;
1974 bus_test_clients_foreach (check_service_owner_changed_foreach,
1980 dbus_message_unref (message);
1981 service_name = NULL;
1983 base_service_from_bus = NULL;
1985 message = pop_message_waiting_for_memory (connection);
1986 if (message == NULL)
1988 _dbus_warn ("Expected a reply to %s, got nothing\n",
1989 "StartServiceByName");
1995 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2000 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2002 warn_unexpected (connection, message, "reply to StartServiceByName");
2007 activation_result = 0;
2008 if (!dbus_message_get_args (message, &error,
2009 DBUS_TYPE_UINT32, &activation_result,
2012 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2014 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
2015 "StartServiceByName", error.message);
2019 dbus_error_free (&error);
2023 if (activation_result == DBUS_START_REPLY_SUCCESS)
2025 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2029 _dbus_warn ("Activation result was %u, no good.\n",
2035 dbus_message_unref (message);
2038 if (!check_no_leftovers (context))
2040 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2048 dbus_message_unref (message);
2049 dbus_error_free (&error);
2055 check_service_auto_activated (BusContext *context,
2056 DBusConnection *connection,
2057 const char *activated_name,
2058 const char *base_service_name,
2059 DBusMessage *initial_message)
2061 DBusMessage *message;
2067 dbus_error_init (&error);
2069 message = initial_message;
2070 dbus_message_ref (message);
2072 if (dbus_message_is_signal (message,
2073 DBUS_INTERFACE_DBUS,
2074 "NameOwnerChanged"))
2076 const char *service_name;
2077 CheckServiceOwnerChangedData socd;
2079 reget_service_name_arg:
2080 if (!dbus_message_get_args (message, &error,
2081 DBUS_TYPE_STRING, &service_name,
2084 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2086 dbus_error_free (&error);
2087 _dbus_wait_for_memory ();
2088 goto reget_service_name_arg;
2092 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2095 dbus_error_free (&error);
2100 if (strcmp (service_name, activated_name) != 0)
2102 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2103 activated_name, service_name);
2107 socd.expected_kind = SERVICE_CREATED;
2108 socd.expected_service_name = service_name;
2109 socd.failed = FALSE;
2110 socd.skip_connection = connection;
2111 bus_test_clients_foreach (check_service_owner_changed_foreach,
2117 /* Note that this differs from regular activation in that we don't get a
2118 * reply to ActivateService here.
2121 dbus_message_unref (message);
2123 service_name = NULL;
2127 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2136 dbus_message_unref (message);
2142 check_service_deactivated (BusContext *context,
2143 DBusConnection *connection,
2144 const char *activated_name,
2145 const char *base_service)
2148 CheckServiceOwnerChangedData socd;
2152 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2153 * service and the activated_name. The base service
2154 * notification is required to come last.
2156 socd.expected_kind = SERVICE_DELETED;
2157 socd.expected_service_name = activated_name;
2158 socd.failed = FALSE;
2159 socd.skip_connection = NULL;
2160 bus_test_clients_foreach (check_service_owner_changed_foreach,
2166 socd.expected_kind = SERVICE_DELETED;
2167 socd.expected_service_name = base_service;
2168 socd.failed = FALSE;
2169 socd.skip_connection = NULL;
2170 bus_test_clients_foreach (check_service_owner_changed_foreach,
2183 check_send_exit_to_service (BusContext *context,
2184 DBusConnection *connection,
2185 const char *service_name,
2186 const char *base_service)
2188 dbus_bool_t got_error;
2189 DBusMessage *message;
2190 dbus_uint32_t serial;
2193 _dbus_verbose ("Sending exit message to the test service\n");
2197 /* Kill off the test service by sending it a quit message */
2198 message = dbus_message_new_method_call (service_name,
2199 "/org/freedesktop/TestSuite",
2200 "org.freedesktop.TestSuite",
2203 if (message == NULL)
2205 /* Do this again; we still need the service to exit... */
2206 if (!check_send_exit_to_service (context, connection,
2207 service_name, base_service))
2213 if (!dbus_connection_send (connection, message, &serial))
2215 dbus_message_unref (message);
2217 /* Do this again; we still need the service to exit... */
2218 if (!check_send_exit_to_service (context, connection,
2219 service_name, base_service))
2225 dbus_message_unref (message);
2229 bus_test_run_clients_loop (SEND_PENDING (connection));
2231 /* read it in and write it out to test service */
2232 bus_test_run_bus_loop (context, FALSE);
2234 /* see if we got an error during message bus dispatching */
2235 bus_test_run_clients_loop (FALSE);
2236 message = borrow_message_waiting_for_memory (connection);
2237 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2240 dbus_connection_return_message (connection, message);
2246 /* If no error, wait for the test service to exit */
2247 block_connection_until_message_from_bus (context, connection, "test service to exit");
2249 bus_test_run_everything (context);
2254 message = pop_message_waiting_for_memory (connection);
2255 _dbus_assert (message != NULL);
2257 if (dbus_message_get_reply_serial (message) != serial)
2259 warn_unexpected (connection, message,
2260 "error with the correct reply serial");
2264 if (!dbus_message_is_error (message,
2265 DBUS_ERROR_NO_MEMORY))
2267 warn_unexpected (connection, message,
2268 "a no memory error from asking test service to exit");
2272 _dbus_verbose ("Got error %s when asking test service to exit\n",
2273 dbus_message_get_error_name (message));
2275 /* Do this again; we still need the service to exit... */
2276 if (!check_send_exit_to_service (context, connection,
2277 service_name, base_service))
2282 if (!check_service_deactivated (context, connection,
2283 service_name, base_service))
2286 /* Should now have a NoReply error from the Exit() method
2287 * call; it should have come after all the deactivation
2290 message = pop_message_waiting_for_memory (connection);
2292 if (message == NULL)
2294 warn_unexpected (connection, NULL,
2295 "reply to Exit() method call");
2298 if (!dbus_message_is_error (message,
2299 DBUS_ERROR_NO_REPLY))
2301 warn_unexpected (connection, message,
2302 "NoReply error from Exit() method call");
2306 if (dbus_message_get_reply_serial (message) != serial)
2308 warn_unexpected (connection, message,
2309 "error with the correct reply serial");
2313 _dbus_verbose ("Got error %s after test service exited\n",
2314 dbus_message_get_error_name (message));
2316 if (!check_no_leftovers (context))
2318 _dbus_warn ("Messages were left over after %s\n",
2319 _DBUS_FUNCTION_NAME);
2328 dbus_message_unref (message);
2334 check_got_error (BusContext *context,
2335 DBusConnection *connection,
2336 const char *first_error_name,
2339 DBusMessage *message;
2342 dbus_bool_t error_found;
2343 const char *error_name;
2347 message = pop_message_waiting_for_memory (connection);
2348 if (message == NULL)
2350 _dbus_warn ("Did not get an expected error\n");
2354 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2356 warn_unexpected (connection, message, "an error");
2361 error_found = FALSE;
2363 va_start (ap, first_error_name);
2364 error_name = first_error_name;
2365 while (error_name != NULL)
2367 if (dbus_message_is_error (message, error_name))
2372 error_name = va_arg (ap, char*);
2378 _dbus_warn ("Expected error %s or other, got %s instead\n",
2380 dbus_message_get_error_name (message));
2388 dbus_message_unref (message);
2395 GOT_SERVICE_CREATED,
2396 GOT_SERVICE_DELETED,
2401 static GotServiceInfo
2402 check_got_service_info (DBusMessage *message)
2404 GotServiceInfo message_kind;
2406 if (dbus_message_is_signal (message,
2407 DBUS_INTERFACE_DBUS,
2408 "NameOwnerChanged"))
2411 const char *service_name, *old_owner, *new_owner;
2412 dbus_error_init (&error);
2414 reget_service_info_data:
2415 service_name = NULL;
2419 dbus_message_get_args (message, &error,
2420 DBUS_TYPE_STRING, &service_name,
2421 DBUS_TYPE_STRING, &old_owner,
2422 DBUS_TYPE_STRING, &new_owner,
2424 if (dbus_error_is_set (&error))
2426 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2428 dbus_error_free (&error);
2429 goto reget_service_info_data;
2433 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2434 message_kind = GOT_SOMETHING_ELSE;
2437 else if (!old_owner[0])
2438 message_kind = GOT_SERVICE_CREATED;
2439 else if (!new_owner[0])
2440 message_kind = GOT_SERVICE_DELETED;
2442 message_kind = GOT_SOMETHING_ELSE;
2444 dbus_error_free (&error);
2446 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2447 message_kind = GOT_ERROR;
2449 message_kind = GOT_SOMETHING_ELSE;
2451 return message_kind;
2454 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2456 /* returns TRUE if the correct thing happens,
2457 * but the correct thing may include OOM errors.
2460 check_existent_service_no_auto_start (BusContext *context,
2461 DBusConnection *connection)
2463 DBusMessage *message;
2464 DBusMessage *base_service_message;
2465 const char *base_service;
2466 dbus_uint32_t serial;
2468 const char *existent = EXISTENT_SERVICE_NAME;
2469 dbus_uint32_t flags;
2471 base_service_message = NULL;
2473 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2475 DBUS_INTERFACE_DBUS,
2476 "StartServiceByName");
2478 if (message == NULL)
2481 dbus_message_set_auto_start (message, FALSE);
2484 if (!dbus_message_append_args (message,
2485 DBUS_TYPE_STRING, &existent,
2486 DBUS_TYPE_UINT32, &flags,
2489 dbus_message_unref (message);
2493 if (!dbus_connection_send (connection, message, &serial))
2495 dbus_message_unref (message);
2499 dbus_message_unref (message);
2502 bus_test_run_everything (context);
2504 /* now wait for the message bus to hear back from the activated
2507 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2509 bus_test_run_everything (context);
2511 if (!dbus_connection_get_is_connected (connection))
2513 _dbus_verbose ("connection was disconnected\n");
2519 message = pop_message_waiting_for_memory (connection);
2520 if (message == NULL)
2522 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2523 "StartServiceByName", serial, connection);
2527 verbose_message_received (connection, message);
2528 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2530 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2532 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2534 _dbus_warn ("Message has wrong sender %s\n",
2535 dbus_message_get_sender (message) ?
2536 dbus_message_get_sender (message) : "(none)");
2540 if (dbus_message_is_error (message,
2541 DBUS_ERROR_NO_MEMORY))
2543 ; /* good, this is a valid response */
2545 else if (dbus_message_is_error (message,
2546 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2547 dbus_message_is_error (message,
2548 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2549 dbus_message_is_error (message,
2550 DBUS_ERROR_SPAWN_EXEC_FAILED))
2552 ; /* good, this is expected also */
2556 _dbus_warn ("Did not expect error %s\n",
2557 dbus_message_get_error_name (message));
2563 GotServiceInfo message_kind;
2565 if (!check_base_service_activated (context, connection,
2566 message, &base_service))
2569 base_service_message = message;
2572 /* We may need to block here for the test service to exit or finish up */
2573 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2575 message = dbus_connection_borrow_message (connection);
2576 if (message == NULL)
2578 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2582 message_kind = check_got_service_info (message);
2584 dbus_connection_return_message (connection, message);
2587 switch (message_kind)
2589 case GOT_SOMETHING_ELSE:
2590 _dbus_warn ("Unexpected message after ActivateService "
2591 "(should be an error or a service announcement");
2595 if (!check_got_error (context, connection,
2596 DBUS_ERROR_SPAWN_CHILD_EXITED,
2597 DBUS_ERROR_NO_MEMORY,
2600 /* A service deleted should be coming along now after this error.
2601 * We can also get the error *after* the service deleted.
2606 case GOT_SERVICE_DELETED:
2608 /* The service started up and got a base address, but then
2609 * failed to register under EXISTENT_SERVICE_NAME
2611 CheckServiceOwnerChangedData socd;
2613 socd.expected_kind = SERVICE_DELETED;
2614 socd.expected_service_name = base_service;
2615 socd.failed = FALSE;
2616 socd.skip_connection = NULL;
2618 bus_test_clients_foreach (check_service_owner_changed_foreach,
2624 /* Now we should get an error about the service exiting
2625 * if we didn't get it before.
2627 if (message_kind != GOT_ERROR)
2629 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2631 /* and process everything again */
2632 bus_test_run_everything (context);
2634 if (!check_got_error (context, connection,
2635 DBUS_ERROR_SPAWN_CHILD_EXITED,
2636 DBUS_ERROR_NO_MEMORY,
2643 case GOT_SERVICE_CREATED:
2644 message = pop_message_waiting_for_memory (connection);
2645 if (message == NULL)
2647 _dbus_warn ("Failed to pop message we just put back! "
2648 "should have been a NameOwnerChanged (creation)\n");
2652 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2653 base_service, message))
2656 dbus_message_unref (message);
2659 if (!check_no_leftovers (context))
2661 _dbus_warn ("Messages were left over after successful activation\n");
2665 if (!check_send_exit_to_service (context, connection,
2666 EXISTENT_SERVICE_NAME, base_service))
2677 dbus_message_unref (message);
2679 if (base_service_message)
2680 dbus_message_unref (base_service_message);
2685 #ifndef DBUS_WIN_FIXME
2686 /* returns TRUE if the correct thing happens,
2687 * but the correct thing may include OOM errors.
2690 check_segfault_service_no_auto_start (BusContext *context,
2691 DBusConnection *connection)
2693 DBusMessage *message;
2694 dbus_uint32_t serial;
2696 const char *segv_service;
2697 dbus_uint32_t flags;
2699 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2701 DBUS_INTERFACE_DBUS,
2702 "StartServiceByName");
2704 if (message == NULL)
2707 dbus_message_set_auto_start (message, FALSE);
2709 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2711 if (!dbus_message_append_args (message,
2712 DBUS_TYPE_STRING, &segv_service,
2713 DBUS_TYPE_UINT32, &flags,
2716 dbus_message_unref (message);
2720 if (!dbus_connection_send (connection, message, &serial))
2722 dbus_message_unref (message);
2726 dbus_message_unref (message);
2729 bus_test_run_everything (context);
2730 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2731 bus_test_run_everything (context);
2733 if (!dbus_connection_get_is_connected (connection))
2735 _dbus_verbose ("connection was disconnected\n");
2741 message = pop_message_waiting_for_memory (connection);
2742 if (message == NULL)
2744 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2745 "StartServiceByName", serial, connection);
2749 verbose_message_received (connection, message);
2751 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2753 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2755 _dbus_warn ("Message has wrong sender %s\n",
2756 dbus_message_get_sender (message) ?
2757 dbus_message_get_sender (message) : "(none)");
2761 if (dbus_message_is_error (message,
2762 DBUS_ERROR_NO_MEMORY))
2764 ; /* good, this is a valid response */
2766 else if (dbus_message_is_error (message,
2769 const char *servicehelper;
2770 servicehelper = bus_context_get_servicehelper (context);
2771 /* make sure this only happens with the launch helper */
2772 _dbus_assert (servicehelper != NULL);
2774 else if (dbus_message_is_error (message,
2775 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2777 ; /* good, this is expected also */
2781 warn_unexpected (connection, message, "not this error");
2788 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2796 dbus_message_unref (message);
2802 /* returns TRUE if the correct thing happens,
2803 * but the correct thing may include OOM errors.
2806 check_segfault_service_auto_start (BusContext *context,
2807 DBusConnection *connection)
2809 DBusMessage *message;
2810 dbus_uint32_t serial;
2813 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2814 "/org/freedesktop/TestSuite",
2815 "org.freedesktop.TestSuite",
2818 if (message == NULL)
2821 if (!dbus_connection_send (connection, message, &serial))
2823 dbus_message_unref (message);
2827 dbus_message_unref (message);
2830 bus_test_run_everything (context);
2831 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2832 bus_test_run_everything (context);
2834 if (!dbus_connection_get_is_connected (connection))
2836 _dbus_verbose ("connection was disconnected\n");
2842 message = pop_message_waiting_for_memory (connection);
2843 if (message == NULL)
2845 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2846 "Echo message (auto activation)", serial, connection);
2850 verbose_message_received (connection, message);
2852 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2854 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2856 _dbus_warn ("Message has wrong sender %s\n",
2857 dbus_message_get_sender (message) ?
2858 dbus_message_get_sender (message) : "(none)");
2862 if (dbus_message_is_error (message,
2863 DBUS_ERROR_NO_MEMORY))
2865 ; /* good, this is a valid response */
2867 else if (dbus_message_is_error (message,
2868 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2870 ; /* good, this is expected also */
2874 warn_unexpected (connection, message, "not this error");
2881 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2889 dbus_message_unref (message);
2895 #define TEST_ECHO_MESSAGE "Test echo message"
2896 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2898 /* returns TRUE if the correct thing happens,
2899 * but the correct thing may include OOM errors.
2902 check_existent_hello_from_self (BusContext *context,
2903 DBusConnection *connection)
2905 DBusMessage *message;
2906 dbus_uint32_t serial;
2909 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2910 "/org/freedesktop/TestSuite",
2911 "org.freedesktop.TestSuite",
2912 "RunHelloFromSelf");
2914 if (message == NULL)
2917 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2918 if (!dbus_message_append_args (message,
2919 DBUS_TYPE_STRING, &text,
2922 dbus_message_unref (message);
2926 if (!dbus_connection_send (connection, message, &serial))
2928 dbus_message_unref (message);
2932 dbus_message_unref (message);
2935 bus_test_run_everything (context);
2937 /* Note: if this test is run in OOM mode, it will block when the bus
2938 * doesn't send a reply due to OOM.
2940 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2942 message = pop_message_waiting_for_memory (connection);
2943 if (message == NULL)
2945 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2949 if (dbus_message_get_reply_serial (message) != serial)
2951 _dbus_warn ("Wrong reply serial\n");
2952 dbus_message_unref (message);
2956 dbus_message_unref (message);
2962 /* returns TRUE if the correct thing happens,
2963 * but the correct thing may include OOM errors.
2966 check_existent_ping (BusContext *context,
2967 DBusConnection *connection)
2969 DBusMessage *message;
2970 dbus_uint32_t serial;
2971 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2972 "/org/freedesktop/TestSuite",
2973 "org.freedesktop.DBus.Peer",
2976 if (message == NULL)
2979 if (!dbus_connection_send (connection, message, &serial))
2981 dbus_message_unref (message);
2985 dbus_message_unref (message);
2988 bus_test_run_everything (context);
2990 /* Note: if this test is run in OOM mode, it will block when the bus
2991 * doesn't send a reply due to OOM.
2993 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2995 message = pop_message_waiting_for_memory (connection);
2996 if (message == NULL)
2998 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
3002 if (dbus_message_get_reply_serial (message) != serial)
3004 _dbus_warn ("Wrong reply serial\n");
3005 dbus_message_unref (message);
3009 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3011 _dbus_warn ("Unexpected message return during Ping\n");
3012 dbus_message_unref (message);
3016 dbus_message_unref (message);
3022 /* returns TRUE if the correct thing happens,
3023 * but the correct thing may include OOM errors.
3026 check_existent_get_machine_id (BusContext *context,
3027 DBusConnection *connection)
3029 DBusMessage *message;
3030 dbus_uint32_t serial;
3031 const char *machine_id;
3033 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3034 "/org/freedesktop/TestSuite",
3035 "org.freedesktop.DBus.Peer",
3038 if (message == NULL)
3041 if (!dbus_connection_send (connection, message, &serial))
3043 dbus_message_unref (message);
3047 dbus_message_unref (message);
3050 bus_test_run_everything (context);
3052 /* Note: if this test is run in OOM mode, it will block when the bus
3053 * doesn't send a reply due to OOM.
3055 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3057 message = pop_message_waiting_for_memory (connection);
3058 if (message == NULL)
3060 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3064 if (dbus_message_get_reply_serial (message) != serial)
3066 _dbus_warn ("Wrong reply serial\n");
3067 dbus_message_unref (message);
3071 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3073 _dbus_warn ("Unexpected message return during GetMachineId\n");
3074 dbus_message_unref (message);
3079 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3081 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3082 dbus_message_unref (message);
3086 if (machine_id == NULL || strlen (machine_id) != 32)
3088 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3089 dbus_message_unref (message);
3093 /* We can't check that the machine id is correct because during make check it is
3094 * just made up for each process separately
3097 dbus_message_unref (message);
3103 /* returns TRUE if the correct thing happens,
3104 * but the correct thing may include OOM errors.
3107 check_existent_service_auto_start (BusContext *context,
3108 DBusConnection *connection)
3110 DBusMessage *message;
3111 DBusMessage *base_service_message;
3112 dbus_uint32_t serial;
3114 const char *base_service;
3117 base_service_message = NULL;
3119 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3120 "/org/freedesktop/TestSuite",
3121 "org.freedesktop.TestSuite",
3124 if (message == NULL)
3127 text = TEST_ECHO_MESSAGE;
3128 if (!dbus_message_append_args (message,
3129 DBUS_TYPE_STRING, &text,
3132 dbus_message_unref (message);
3136 if (!dbus_connection_send (connection, message, &serial))
3138 dbus_message_unref (message);
3142 dbus_message_unref (message);
3145 bus_test_run_everything (context);
3147 /* now wait for the message bus to hear back from the activated
3150 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3151 bus_test_run_everything (context);
3153 if (!dbus_connection_get_is_connected (connection))
3155 _dbus_verbose ("connection was disconnected\n");
3161 message = pop_message_waiting_for_memory (connection);
3162 if (message == NULL)
3164 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3165 serial, connection);
3169 verbose_message_received (connection, message);
3170 _dbus_verbose (" (after sending %s)\n", "auto start");
3172 /* we should get zero or two ServiceOwnerChanged signals */
3173 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3175 GotServiceInfo message_kind;
3177 if (!check_base_service_activated (context, connection,
3178 message, &base_service))
3181 base_service_message = message;
3184 /* We may need to block here for the test service to exit or finish up */
3185 block_connection_until_message_from_bus (context, connection, "service to exit");
3187 /* Should get a service creation notification for the activated
3188 * service name, or a service deletion on the base service name
3190 message = dbus_connection_borrow_message (connection);
3191 if (message == NULL)
3193 _dbus_warn ("No message after auto activation "
3194 "(should be a service announcement)\n");
3195 dbus_connection_return_message (connection, message);
3200 message_kind = check_got_service_info (message);
3202 dbus_connection_return_message (connection, message);
3205 switch (message_kind)
3207 case GOT_SERVICE_CREATED:
3208 message = pop_message_waiting_for_memory (connection);
3209 if (message == NULL)
3211 _dbus_warn ("Failed to pop message we just put back! "
3212 "should have been a NameOwnerChanged (creation)\n");
3216 /* Check that ServiceOwnerChanged (creation) was correctly received */
3217 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3218 base_service, message))
3221 dbus_message_unref (message);
3226 case GOT_SERVICE_DELETED:
3228 /* The service started up and got a base address, but then
3229 * failed to register under EXISTENT_SERVICE_NAME
3231 CheckServiceOwnerChangedData socd;
3233 socd.expected_kind = SERVICE_DELETED;
3234 socd.expected_service_name = base_service;
3235 socd.failed = FALSE;
3236 socd.skip_connection = NULL;
3237 bus_test_clients_foreach (check_service_owner_changed_foreach,
3247 case GOT_SOMETHING_ELSE:
3248 _dbus_warn ("Unexpected message after auto activation\n");
3253 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3254 * come the method reply (or error) from the initial method call
3257 /* Note: if this test is run in OOM mode, it will block when the bus
3258 * doesn't send a reply due to OOM.
3260 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3262 message = pop_message_waiting_for_memory (connection);
3263 if (message == NULL)
3265 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3269 if (dbus_message_get_reply_serial (message) != serial)
3271 _dbus_warn ("Wrong reply serial\n");
3275 dbus_message_unref (message);
3278 if (!check_existent_ping (context, connection))
3281 if (!check_existent_get_machine_id (context, connection))
3284 if (!check_existent_hello_from_self (context, connection))
3287 if (!check_send_exit_to_service (context, connection,
3288 EXISTENT_SERVICE_NAME,
3296 dbus_message_unref (message);
3298 if (base_service_message)
3299 dbus_message_unref (base_service_message);
3304 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3306 /* returns TRUE if the correct thing happens,
3307 * but the correct thing may include OOM errors.
3310 check_launch_service_file_missing (BusContext *context,
3311 DBusConnection *connection)
3313 DBusMessage *message;
3314 dbus_uint32_t serial;
3317 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3318 "/org/freedesktop/TestSuite",
3319 "org.freedesktop.TestSuite",
3322 if (message == NULL)
3325 if (!dbus_connection_send (connection, message, &serial))
3327 dbus_message_unref (message);
3331 dbus_message_unref (message);
3334 bus_test_run_everything (context);
3335 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3336 bus_test_run_everything (context);
3338 if (!dbus_connection_get_is_connected (connection))
3340 _dbus_verbose ("connection was disconnected\n");
3346 message = pop_message_waiting_for_memory (connection);
3347 if (message == NULL)
3349 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3350 "Echo message (auto activation)", serial, connection);
3354 verbose_message_received (connection, message);
3356 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3358 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3360 _dbus_warn ("Message has wrong sender %s\n",
3361 dbus_message_get_sender (message) ?
3362 dbus_message_get_sender (message) : "(none)");
3366 if (dbus_message_is_error (message,
3367 DBUS_ERROR_NO_MEMORY))
3369 ; /* good, this is a valid response */
3371 else if (dbus_message_is_error (message,
3372 DBUS_ERROR_SERVICE_UNKNOWN))
3374 _dbus_verbose("got service unknown\n");
3375 ; /* good, this is expected (only valid when using launch helper) */
3379 warn_unexpected (connection, message, "not this error");
3386 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3394 dbus_message_unref (message);
3399 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3401 /* returns TRUE if the correct thing happens,
3402 * but the correct thing may include OOM errors.
3405 check_launch_service_user_missing (BusContext *context,
3406 DBusConnection *connection)
3408 DBusMessage *message;
3409 dbus_uint32_t serial;
3412 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3413 "/org/freedesktop/TestSuite",
3414 "org.freedesktop.TestSuite",
3417 if (message == NULL)
3420 if (!dbus_connection_send (connection, message, &serial))
3422 dbus_message_unref (message);
3426 dbus_message_unref (message);
3429 bus_test_run_everything (context);
3430 block_connection_until_message_from_bus (context, connection,
3431 "reply to service which should fail to auto-start (missing User)");
3432 bus_test_run_everything (context);
3434 if (!dbus_connection_get_is_connected (connection))
3436 _dbus_warn ("connection was disconnected\n");
3442 message = pop_message_waiting_for_memory (connection);
3443 if (message == NULL)
3445 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3446 "Echo message (auto activation)", serial, connection);
3450 verbose_message_received (connection, message);
3452 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3454 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3456 _dbus_warn ("Message has wrong sender %s\n",
3457 dbus_message_get_sender (message) ?
3458 dbus_message_get_sender (message) : "(none)");
3462 if (dbus_message_is_error (message,
3463 DBUS_ERROR_NO_MEMORY))
3465 ; /* good, this is a valid response */
3467 else if (dbus_message_is_error (message,
3468 DBUS_ERROR_SPAWN_FILE_INVALID))
3470 _dbus_verbose("got service file invalid\n");
3471 ; /* good, this is expected (only valid when using launch helper) */
3475 warn_unexpected (connection, message, "not this error");
3482 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3490 dbus_message_unref (message);
3495 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3497 /* returns TRUE if the correct thing happens,
3498 * but the correct thing may include OOM errors.
3501 check_launch_service_exec_missing (BusContext *context,
3502 DBusConnection *connection)
3504 DBusMessage *message;
3505 dbus_uint32_t serial;
3508 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3509 "/org/freedesktop/TestSuite",
3510 "org.freedesktop.TestSuite",
3513 if (message == NULL)
3516 if (!dbus_connection_send (connection, message, &serial))
3518 dbus_message_unref (message);
3522 dbus_message_unref (message);
3525 bus_test_run_everything (context);
3526 block_connection_until_message_from_bus (context, connection,
3527 "reply to service which should fail to auto-start (missing Exec)");
3528 bus_test_run_everything (context);
3530 if (!dbus_connection_get_is_connected (connection))
3532 _dbus_warn ("connection was disconnected\n");
3538 message = pop_message_waiting_for_memory (connection);
3539 if (message == NULL)
3541 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3542 "Echo message (auto activation)", serial, connection);
3546 verbose_message_received (connection, message);
3548 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3550 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3552 _dbus_warn ("Message has wrong sender %s\n",
3553 dbus_message_get_sender (message) ?
3554 dbus_message_get_sender (message) : "(none)");
3558 if (dbus_message_is_error (message,
3559 DBUS_ERROR_NO_MEMORY))
3561 ; /* good, this is a valid response */
3563 else if (dbus_message_is_error (message,
3564 DBUS_ERROR_SERVICE_UNKNOWN))
3566 _dbus_verbose("could not activate as invalid service file was not added\n");
3567 ; /* good, this is expected as we shouldn't have been added to
3568 * the activation list with a missing Exec key */
3570 else if (dbus_message_is_error (message,
3571 DBUS_ERROR_SPAWN_FILE_INVALID))
3573 _dbus_verbose("got service file invalid\n");
3574 ; /* good, this is allowed, and is the message passed back from the
3579 warn_unexpected (connection, message, "not this error");
3586 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3594 dbus_message_unref (message);
3599 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3601 /* returns TRUE if the correct thing happens,
3602 * but the correct thing may include OOM errors.
3605 check_launch_service_service_missing (BusContext *context,
3606 DBusConnection *connection)
3608 DBusMessage *message;
3609 dbus_uint32_t serial;
3612 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3613 "/org/freedesktop/TestSuite",
3614 "org.freedesktop.TestSuite",
3617 if (message == NULL)
3620 if (!dbus_connection_send (connection, message, &serial))
3622 dbus_message_unref (message);
3626 dbus_message_unref (message);
3629 bus_test_run_everything (context);
3630 block_connection_until_message_from_bus (context, connection,
3631 "reply to service which should fail to auto-start (missing Service)");
3632 bus_test_run_everything (context);
3634 if (!dbus_connection_get_is_connected (connection))
3636 _dbus_warn ("connection was disconnected\n");
3642 message = pop_message_waiting_for_memory (connection);
3643 if (message == NULL)
3645 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3646 "Echo message (auto activation)", serial, connection);
3650 verbose_message_received (connection, message);
3652 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3654 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3656 _dbus_warn ("Message has wrong sender %s\n",
3657 dbus_message_get_sender (message) ?
3658 dbus_message_get_sender (message) : "(none)");
3662 if (dbus_message_is_error (message,
3663 DBUS_ERROR_NO_MEMORY))
3665 ; /* good, this is a valid response */
3667 else if (dbus_message_is_error (message,
3668 DBUS_ERROR_SERVICE_UNKNOWN))
3670 _dbus_verbose("could not activate as invalid service file was not added\n");
3671 ; /* good, this is expected as we shouldn't have been added to
3672 * the activation list with a missing Exec key */
3674 else if (dbus_message_is_error (message,
3675 DBUS_ERROR_SPAWN_FILE_INVALID))
3677 _dbus_verbose("got service file invalid\n");
3678 ; /* good, this is allowed, and is the message passed back from the
3683 warn_unexpected (connection, message, "not this error");
3690 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3698 dbus_message_unref (message);
3703 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3705 /* returns TRUE if the correct thing happens,
3706 * but the correct thing may include OOM errors.
3709 check_shell_fail_service_auto_start (BusContext *context,
3710 DBusConnection *connection)
3712 DBusMessage *message;
3713 dbus_uint32_t serial;
3716 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3717 "/org/freedesktop/TestSuite",
3718 "org.freedesktop.TestSuite",
3721 if (message == NULL)
3724 if (!dbus_connection_send (connection, message, &serial))
3726 dbus_message_unref (message);
3730 dbus_message_unref (message);
3733 bus_test_run_everything (context);
3734 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3735 bus_test_run_everything (context);
3737 if (!dbus_connection_get_is_connected (connection))
3739 _dbus_verbose ("connection was disconnected\n");
3745 message = pop_message_waiting_for_memory (connection);
3746 if (message == NULL)
3748 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3749 "Echo message (auto activation)", serial, connection);
3753 verbose_message_received (connection, message);
3755 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3757 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3759 _dbus_warn ("Message has wrong sender %s\n",
3760 dbus_message_get_sender (message) ?
3761 dbus_message_get_sender (message) : "(none)");
3765 if (dbus_message_is_error (message,
3766 DBUS_ERROR_NO_MEMORY))
3768 ; /* good, this is a valid response */
3770 else if (dbus_message_is_error (message,
3771 DBUS_ERROR_INVALID_ARGS))
3773 _dbus_verbose("got invalid args\n");
3774 ; /* good, this is expected also */
3778 warn_unexpected (connection, message, "not this error");
3785 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3793 dbus_message_unref (message);
3798 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3800 /* returns TRUE if the correct thing happens,
3801 * but the correct thing may include OOM errors.
3804 check_shell_service_success_auto_start (BusContext *context,
3805 DBusConnection *connection)
3807 DBusMessage *message;
3808 DBusMessage *base_service_message;
3809 dbus_uint32_t serial;
3811 const char *base_service;
3812 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3814 base_service_message = NULL;
3816 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3817 "/org/freedesktop/TestSuite",
3818 "org.freedesktop.TestSuite",
3821 if (message == NULL)
3824 if (!dbus_connection_send (connection, message, &serial))
3826 dbus_message_unref (message);
3830 dbus_message_unref (message);
3833 bus_test_run_everything (context);
3835 /* now wait for the message bus to hear back from the activated
3838 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3839 bus_test_run_everything (context);
3841 if (!dbus_connection_get_is_connected (connection))
3843 _dbus_verbose ("connection was disconnected\n");
3849 message = pop_message_waiting_for_memory (connection);
3850 if (message == NULL)
3852 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3853 serial, connection);
3857 verbose_message_received (connection, message);
3858 _dbus_verbose (" (after sending %s)\n", "auto start");
3860 /* we should get zero or two ServiceOwnerChanged signals */
3861 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3863 GotServiceInfo message_kind;
3865 if (!check_base_service_activated (context, connection,
3866 message, &base_service))
3869 base_service_message = message;
3872 /* We may need to block here for the test service to exit or finish up */
3873 block_connection_until_message_from_bus (context, connection, "service to exit");
3875 /* Should get a service creation notification for the activated
3876 * service name, or a service deletion on the base service name
3878 message = dbus_connection_borrow_message (connection);
3879 if (message == NULL)
3881 _dbus_warn ("No message after auto activation "
3882 "(should be a service announcement)\n");
3883 dbus_connection_return_message (connection, message);
3888 message_kind = check_got_service_info (message);
3890 dbus_connection_return_message (connection, message);
3893 switch (message_kind)
3895 case GOT_SERVICE_CREATED:
3896 message = pop_message_waiting_for_memory (connection);
3897 if (message == NULL)
3899 _dbus_warn ("Failed to pop message we just put back! "
3900 "should have been a NameOwnerChanged (creation)\n");
3904 /* Check that ServiceOwnerChanged (creation) was correctly received */
3905 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3906 base_service, message))
3909 dbus_message_unref (message);
3914 case GOT_SERVICE_DELETED:
3916 /* The service started up and got a base address, but then
3917 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3919 CheckServiceOwnerChangedData socd;
3921 socd.expected_kind = SERVICE_DELETED;
3922 socd.expected_service_name = base_service;
3923 socd.failed = FALSE;
3924 socd.skip_connection = NULL;
3925 bus_test_clients_foreach (check_service_owner_changed_foreach,
3935 case GOT_SOMETHING_ELSE:
3936 _dbus_warn ("Unexpected message after auto activation\n");
3941 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3942 * come the method reply (or error) from the initial method call
3945 /* Note: if this test is run in OOM mode, it will block when the bus
3946 * doesn't send a reply due to OOM.
3948 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3950 message = pop_message_waiting_for_memory (connection);
3951 if (message == NULL)
3953 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3957 if (dbus_message_get_reply_serial (message) != serial)
3959 _dbus_warn ("Wrong reply serial\n");
3963 if (!dbus_message_get_args (message, NULL,
3964 DBUS_TYPE_STRING, &argv[0],
3965 DBUS_TYPE_STRING, &argv[1],
3966 DBUS_TYPE_STRING, &argv[2],
3967 DBUS_TYPE_STRING, &argv[3],
3968 DBUS_TYPE_STRING, &argv[4],
3969 DBUS_TYPE_STRING, &argv[5],
3970 DBUS_TYPE_STRING, &argv[6],
3973 _dbus_warn ("Error getting arguments from return\n");
3977 /* don't worry about arg[0] as it may be different
3978 depending on the path to the tests
3980 if (strcmp("-test", argv[1]) != 0)
3982 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3987 if (strcmp("that", argv[2]) != 0)
3989 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3994 if (strcmp("we get", argv[3]) != 0)
3996 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
4001 if (strcmp("back", argv[4]) != 0)
4003 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
4008 if (strcmp("--what", argv[5]) != 0)
4010 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
4015 if (strcmp("we put in", argv[6]) != 0)
4017 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
4018 "we put in", argv[6]);
4022 dbus_message_unref (message);
4025 if (!check_send_exit_to_service (context, connection,
4026 SHELL_SUCCESS_SERVICE_NAME,
4034 dbus_message_unref (message);
4036 if (base_service_message)
4037 dbus_message_unref (base_service_message);
4045 BusContext *context;
4049 check_oom_check1_func (void *data)
4051 Check1Data *d = data;
4053 if (! (* d->func) (d->context))
4056 if (!check_no_leftovers (d->context))
4058 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4066 check1_try_iterations (BusContext *context,
4067 const char *description,
4073 d.context = context;
4075 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4077 _dbus_assert_not_reached ("test failed");
4081 check_get_services (BusContext *context,
4082 DBusConnection *connection,
4087 DBusMessage *message;
4088 dbus_uint32_t serial;
4095 dbus_error_init (&error);
4098 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4100 DBUS_INTERFACE_DBUS,
4103 if (message == NULL)
4106 if (!dbus_connection_send (connection, message, &serial))
4108 dbus_message_unref (message);
4112 /* send our message */
4113 bus_test_run_clients_loop (SEND_PENDING (connection));
4115 dbus_message_unref (message);
4118 dbus_connection_ref (connection); /* because we may get disconnected */
4119 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4121 if (!dbus_connection_get_is_connected (connection))
4123 _dbus_verbose ("connection was disconnected\n");
4125 dbus_connection_unref (connection);
4130 dbus_connection_unref (connection);
4132 message = pop_message_waiting_for_memory (connection);
4133 if (message == NULL)
4135 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4136 method, serial, connection);
4140 verbose_message_received (connection, message);
4142 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4144 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4146 ; /* good, this is a valid response */
4150 warn_unexpected (connection, message, "not this error");
4157 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4159 ; /* good, expected */
4163 warn_unexpected (connection, message,
4164 "method_return for ListActivatableNames/ListNames");
4171 if (!dbus_message_get_args (message, &error,
4177 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4179 _dbus_verbose ("no memory to list services by %s\n", method);
4180 dbus_error_free (&error);
4181 _dbus_wait_for_memory ();
4182 goto retry_get_property;
4186 _dbus_assert (dbus_error_is_set (&error));
4187 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4196 if (!check_no_leftovers (context))
4202 dbus_error_free (&error);
4205 dbus_message_unref (message);
4210 /* returns TRUE if the correct thing happens,
4211 * but the correct thing may include OOM errors.
4214 check_list_services (BusContext *context,
4215 DBusConnection *connection)
4217 DBusMessage *message;
4218 DBusMessage *base_service_message;
4219 const char *base_service;
4220 dbus_uint32_t serial;
4222 const char *existent = EXISTENT_SERVICE_NAME;
4223 dbus_uint32_t flags;
4227 _dbus_verbose ("check_list_services for %p\n", connection);
4229 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4234 if (!_dbus_string_array_contains ((const char **)services, existent))
4236 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4237 dbus_free_string_array (services);
4241 dbus_free_string_array (services);
4243 base_service_message = NULL;
4245 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4247 DBUS_INTERFACE_DBUS,
4248 "StartServiceByName");
4250 if (message == NULL)
4253 dbus_message_set_auto_start (message, FALSE);
4256 if (!dbus_message_append_args (message,
4257 DBUS_TYPE_STRING, &existent,
4258 DBUS_TYPE_UINT32, &flags,
4261 dbus_message_unref (message);
4265 if (!dbus_connection_send (connection, message, &serial))
4267 dbus_message_unref (message);
4271 dbus_message_unref (message);
4274 bus_test_run_everything (context);
4276 /* now wait for the message bus to hear back from the activated
4279 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4281 bus_test_run_everything (context);
4283 if (!dbus_connection_get_is_connected (connection))
4285 _dbus_verbose ("connection was disconnected\n");
4291 message = pop_message_waiting_for_memory (connection);
4292 if (message == NULL)
4294 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4295 "StartServiceByName", serial, connection);
4299 verbose_message_received (connection, message);
4300 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4302 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4304 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4306 _dbus_warn ("Message has wrong sender %s\n",
4307 dbus_message_get_sender (message) ?
4308 dbus_message_get_sender (message) : "(none)");
4312 if (dbus_message_is_error (message,
4313 DBUS_ERROR_NO_MEMORY))
4315 ; /* good, this is a valid response */
4317 else if (dbus_message_is_error (message,
4318 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4319 dbus_message_is_error (message,
4320 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4321 dbus_message_is_error (message,
4322 DBUS_ERROR_SPAWN_EXEC_FAILED))
4324 ; /* good, this is expected also */
4328 _dbus_warn ("Did not expect error %s\n",
4329 dbus_message_get_error_name (message));
4335 GotServiceInfo message_kind;
4337 if (!check_base_service_activated (context, connection,
4338 message, &base_service))
4341 base_service_message = message;
4344 /* We may need to block here for the test service to exit or finish up */
4345 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4347 message = dbus_connection_borrow_message (connection);
4348 if (message == NULL)
4350 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4354 message_kind = check_got_service_info (message);
4356 dbus_connection_return_message (connection, message);
4359 switch (message_kind)
4361 case GOT_SOMETHING_ELSE:
4363 case GOT_SERVICE_DELETED:
4364 _dbus_warn ("Unexpected message after ActivateService "
4365 "(should be an error or a service announcement)\n");
4368 case GOT_SERVICE_CREATED:
4369 message = pop_message_waiting_for_memory (connection);
4370 if (message == NULL)
4372 _dbus_warn ("Failed to pop message we just put back! "
4373 "should have been a NameOwnerChanged (creation)\n");
4377 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4378 base_service, message))
4381 dbus_message_unref (message);
4384 if (!check_no_leftovers (context))
4386 _dbus_warn ("Messages were left over after successful activation\n");
4394 if (!check_get_services (context, connection, "ListNames", &services, &len))
4399 if (!_dbus_string_array_contains ((const char **)services, existent))
4401 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4405 dbus_free_string_array (services);
4407 if (!check_send_exit_to_service (context, connection,
4408 EXISTENT_SERVICE_NAME, base_service))
4415 dbus_message_unref (message);
4417 if (base_service_message)
4418 dbus_message_unref (base_service_message);
4426 BusContext *context;
4427 DBusConnection *connection;
4431 check_oom_check2_func (void *data)
4433 Check2Data *d = data;
4435 if (! (* d->func) (d->context, d->connection))
4438 if (!check_no_leftovers (d->context))
4440 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4448 check2_try_iterations (BusContext *context,
4449 DBusConnection *connection,
4450 const char *description,
4456 d.context = context;
4457 d.connection = connection;
4459 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4462 _dbus_warn ("%s failed during oom\n", description);
4463 _dbus_assert_not_reached ("test failed");
4468 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4469 const char *filename)
4474 if (!_dbus_string_init (&full))
4477 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4479 _dbus_string_free (&full);
4483 _dbus_string_init_const (&file, filename);
4485 if (!_dbus_concat_dir_and_file (&full, &file))
4487 _dbus_string_free (&full);
4491 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4492 _dbus_string_get_const_data (&full));
4494 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4496 _dbus_string_free (&full);
4502 bus_dispatch_test_conf (const DBusString *test_data_dir,
4503 const char *filename,
4504 dbus_bool_t use_launcher)
4506 BusContext *context;
4507 DBusConnection *foo;
4508 DBusConnection *bar;
4509 DBusConnection *baz;
4512 /* save the config name for the activation helper */
4513 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4514 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4516 dbus_error_init (&error);
4518 context = bus_context_new_test (test_data_dir, filename);
4519 if (context == NULL)
4522 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4524 _dbus_assert_not_reached ("could not alloc connection");
4526 if (!bus_setup_debug_client (foo))
4527 _dbus_assert_not_reached ("could not set up connection");
4529 spin_connection_until_authenticated (context, foo);
4531 if (!check_hello_message (context, foo))
4532 _dbus_assert_not_reached ("hello message failed");
4534 if (!check_double_hello_message (context, foo))
4535 _dbus_assert_not_reached ("double hello message failed");
4537 if (!check_add_match_all (context, foo))
4538 _dbus_assert_not_reached ("AddMatch message failed");
4540 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4542 _dbus_assert_not_reached ("could not alloc connection");
4544 if (!bus_setup_debug_client (bar))
4545 _dbus_assert_not_reached ("could not set up connection");
4547 spin_connection_until_authenticated (context, bar);
4549 if (!check_hello_message (context, bar))
4550 _dbus_assert_not_reached ("hello message failed");
4552 if (!check_add_match_all (context, bar))
4553 _dbus_assert_not_reached ("AddMatch message failed");
4555 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4557 _dbus_assert_not_reached ("could not alloc connection");
4559 if (!bus_setup_debug_client (baz))
4560 _dbus_assert_not_reached ("could not set up connection");
4562 spin_connection_until_authenticated (context, baz);
4564 if (!check_hello_message (context, baz))
4565 _dbus_assert_not_reached ("hello message failed");
4567 if (!check_add_match_all (context, baz))
4568 _dbus_assert_not_reached ("AddMatch message failed");
4570 #ifdef DBUS_WIN_FIXME
4571 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4572 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4574 if (!check_get_connection_unix_user (context, baz))
4575 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4577 if (!check_get_connection_unix_process_id (context, baz))
4578 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4581 if (!check_list_services (context, baz))
4582 _dbus_assert_not_reached ("ListActivatableNames message failed");
4584 if (!check_no_leftovers (context))
4586 _dbus_warn ("Messages were left over after setting up initial connections\n");
4587 _dbus_assert_not_reached ("initial connection setup failed");
4590 check1_try_iterations (context, "create_and_hello",
4591 check_hello_connection);
4593 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4594 check_nonexistent_service_no_auto_start);
4596 #ifdef DBUS_WIN_FIXME
4597 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4599 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4600 check_segfault_service_no_auto_start);
4603 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4604 check_existent_service_no_auto_start);
4606 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4607 check_nonexistent_service_auto_start);
4610 #ifdef DBUS_WIN_FIXME
4611 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4613 /* only do the segfault test if we are not using the launcher */
4614 check2_try_iterations (context, foo, "segfault_service_auto_start",
4615 check_segfault_service_auto_start);
4618 /* only do the shell fail test if we are not using the launcher */
4619 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4620 check_shell_fail_service_auto_start);
4622 /* specific to launcher */
4624 if (!check_launch_service_file_missing (context, foo))
4625 _dbus_assert_not_reached ("did not get service file not found error");
4628 /* Note: need to resolve some issues with the testing code in order to run
4629 * this in oom (handle that we sometimes don't get replies back from the bus
4630 * when oom happens, without blocking the test).
4632 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4633 check_existent_service_auto_start);
4636 if (!check_existent_service_auto_start (context, foo))
4637 _dbus_assert_not_reached ("existent service auto start failed");
4639 if (!check_shell_service_success_auto_start (context, foo))
4640 _dbus_assert_not_reached ("shell success service auto start failed");
4642 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4644 kill_client_connection_unchecked (foo);
4645 kill_client_connection_unchecked (bar);
4646 kill_client_connection_unchecked (baz);
4648 bus_context_unref (context);
4654 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4655 const char *filename)
4657 BusContext *context;
4658 DBusConnection *foo;
4661 /* save the config name for the activation helper */
4662 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4663 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4665 dbus_error_init (&error);
4667 context = bus_context_new_test (test_data_dir, filename);
4668 if (context == NULL)
4671 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4673 _dbus_assert_not_reached ("could not alloc connection");
4675 if (!bus_setup_debug_client (foo))
4676 _dbus_assert_not_reached ("could not set up connection");
4678 spin_connection_until_authenticated (context, foo);
4680 if (!check_hello_message (context, foo))
4681 _dbus_assert_not_reached ("hello message failed");
4683 if (!check_double_hello_message (context, foo))
4684 _dbus_assert_not_reached ("double hello message failed");
4686 if (!check_add_match_all (context, foo))
4687 _dbus_assert_not_reached ("AddMatch message failed");
4689 /* this only tests the activation.c user check */
4690 if (!check_launch_service_user_missing (context, foo))
4691 _dbus_assert_not_reached ("user missing did not trigger error");
4693 /* this only tests the desktop.c exec check */
4694 if (!check_launch_service_exec_missing (context, foo))
4695 _dbus_assert_not_reached ("exec missing did not trigger error");
4697 /* this only tests the desktop.c service check */
4698 if (!check_launch_service_service_missing (context, foo))
4699 _dbus_assert_not_reached ("service missing did not trigger error");
4701 _dbus_verbose ("Disconnecting foo\n");
4703 kill_client_connection_unchecked (foo);
4705 bus_context_unref (context);
4711 bus_dispatch_test (const DBusString *test_data_dir)
4713 /* run normal activation tests */
4714 _dbus_verbose ("Normal activation tests\n");
4715 if (!bus_dispatch_test_conf (test_data_dir,
4716 "valid-config-files/debug-allow-all.conf", FALSE))
4720 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4722 /* run launch-helper activation tests */
4723 _dbus_verbose ("Launch helper activation tests\n");
4724 if (!bus_dispatch_test_conf (test_data_dir,
4725 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4728 /* run select launch-helper activation tests on broken service files */
4729 if (!bus_dispatch_test_conf_fail (test_data_dir,
4730 "valid-config-files-system/debug-allow-all-fail.conf"))
4738 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4740 BusContext *context;
4741 DBusConnection *foo;
4744 dbus_error_init (&error);
4746 /* Test SHA1 authentication */
4747 _dbus_verbose ("Testing SHA1 context\n");
4749 context = bus_context_new_test (test_data_dir,
4750 "valid-config-files/debug-allow-all-sha1.conf");
4751 if (context == NULL)
4754 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4756 _dbus_assert_not_reached ("could not alloc connection");
4758 if (!bus_setup_debug_client (foo))
4759 _dbus_assert_not_reached ("could not set up connection");
4761 spin_connection_until_authenticated (context, foo);
4763 if (!check_hello_message (context, foo))
4764 _dbus_assert_not_reached ("hello message failed");
4766 if (!check_add_match_all (context, foo))
4767 _dbus_assert_not_reached ("addmatch message failed");
4769 if (!check_no_leftovers (context))
4771 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4772 _dbus_assert_not_reached ("initial connection setup failed");
4775 check1_try_iterations (context, "create_and_hello_sha1",
4776 check_hello_connection);
4778 kill_client_connection_unchecked (foo);
4780 bus_context_unref (context);
4785 #ifdef HAVE_UNIX_FD_PASSING
4788 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4790 BusContext *context;
4791 DBusConnection *foo, *bar;
4794 int one[2], two[2], x, y, z;
4797 dbus_error_init (&error);
4799 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4800 if (context == NULL)
4801 _dbus_assert_not_reached ("could not alloc context");
4803 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4805 _dbus_assert_not_reached ("could not alloc connection");
4807 if (!bus_setup_debug_client (foo))
4808 _dbus_assert_not_reached ("could not set up connection");
4810 spin_connection_until_authenticated (context, foo);
4812 if (!check_hello_message (context, foo))
4813 _dbus_assert_not_reached ("hello message failed");
4815 if (!check_add_match_all (context, foo))
4816 _dbus_assert_not_reached ("AddMatch message failed");
4818 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4820 _dbus_assert_not_reached ("could not alloc connection");
4822 if (!bus_setup_debug_client (bar))
4823 _dbus_assert_not_reached ("could not set up connection");
4825 spin_connection_until_authenticated (context, bar);
4827 if (!check_hello_message (context, bar))
4828 _dbus_assert_not_reached ("hello message failed");
4830 if (!check_add_match_all (context, bar))
4831 _dbus_assert_not_reached ("AddMatch message failed");
4833 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4834 _dbus_assert_not_reached ("could not alloc message");
4836 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4837 _dbus_assert_not_reached("Failed to allocate pipe #1");
4839 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4840 _dbus_assert_not_reached("Failed to allocate pipe #2");
4842 if (!dbus_message_append_args(m,
4843 DBUS_TYPE_UNIX_FD, one,
4844 DBUS_TYPE_UNIX_FD, two,
4845 DBUS_TYPE_UNIX_FD, two,
4847 _dbus_assert_not_reached("Failed to attach fds.");
4849 if (!_dbus_close(one[0], &error))
4850 _dbus_assert_not_reached("Failed to close pipe #1 ");
4851 if (!_dbus_close(two[0], &error))
4852 _dbus_assert_not_reached("Failed to close pipe #2 ");
4854 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4855 _dbus_assert_not_reached("Connection cannot do fd passing");
4857 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4858 _dbus_assert_not_reached("Connection cannot do fd passing");
4860 if (!dbus_connection_send (foo, m, NULL))
4861 _dbus_assert_not_reached("Failed to send fds");
4863 dbus_message_unref(m);
4865 bus_test_run_clients_loop (SEND_PENDING (foo));
4867 bus_test_run_everything (context);
4869 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4871 if (!(m = pop_message_waiting_for_memory (foo)))
4872 _dbus_assert_not_reached("Failed to receive msg");
4874 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4875 _dbus_assert_not_reached("bogus message received");
4877 dbus_message_unref(m);
4879 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4881 if (!(m = pop_message_waiting_for_memory (bar)))
4882 _dbus_assert_not_reached("Failed to receive msg");
4884 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4885 _dbus_assert_not_reached("bogus message received");
4887 if (!dbus_message_get_args(m,
4889 DBUS_TYPE_UNIX_FD, &x,
4890 DBUS_TYPE_UNIX_FD, &y,
4891 DBUS_TYPE_UNIX_FD, &z,
4893 _dbus_assert_not_reached("Failed to parse fds.");
4895 dbus_message_unref(m);
4897 if (write(x, "X", 1) != 1)
4898 _dbus_assert_not_reached("Failed to write to pipe #1");
4899 if (write(y, "Y", 1) != 1)
4900 _dbus_assert_not_reached("Failed to write to pipe #2");
4901 if (write(z, "Z", 1) != 1)
4902 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4904 if (!_dbus_close(x, &error))
4905 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4906 if (!_dbus_close(y, &error))
4907 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4908 if (!_dbus_close(z, &error))
4909 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4911 if (read(one[1], &r, 1) != 1 || r != 'X')
4912 _dbus_assert_not_reached("Failed to read value from pipe.");
4913 if (read(two[1], &r, 1) != 1 || r != 'Y')
4914 _dbus_assert_not_reached("Failed to read value from pipe.");
4915 if (read(two[1], &r, 1) != 1 || r != 'Z')
4916 _dbus_assert_not_reached("Failed to read value from pipe.");
4918 if (!_dbus_close(one[1], &error))
4919 _dbus_assert_not_reached("Failed to close pipe #1 ");
4920 if (!_dbus_close(two[1], &error))
4921 _dbus_assert_not_reached("Failed to close pipe #2 ");
4923 _dbus_verbose ("Disconnecting foo\n");
4924 kill_client_connection_unchecked (foo);
4926 _dbus_verbose ("Disconnecting bar\n");
4927 kill_client_connection_unchecked (bar);
4929 bus_context_unref (context);
4935 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */