1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "connection.h"
31 #include "activation.h"
36 #include <dbus/dbus-internals.h>
39 #ifdef HAVE_UNIX_FD_PASSING
40 #include <dbus/dbus-sysdeps-unix.h>
45 #define TEST_CONNECTION "debug-pipe:name=test-server"
47 #define TEST_CONNECTION "tcp:host=localhost,port=1234"
51 send_one_message (DBusConnection *connection,
53 DBusConnection *sender,
54 DBusConnection *addressed_recipient,
56 BusTransaction *transaction,
59 if (!bus_context_check_security_policy (context, transaction,
65 return TRUE; /* silently don't send it */
67 if (dbus_message_contains_unix_fds(message) &&
68 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
69 return TRUE; /* silently don't send it */
71 if (!bus_transaction_send (transaction,
83 bus_dispatch_matches (BusTransaction *transaction,
84 DBusConnection *sender,
85 DBusConnection *addressed_recipient,
90 BusConnections *connections;
92 BusMatchmaker *matchmaker;
96 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
98 /* sender and recipient can both be NULL for the bus driver,
99 * or for signals with no particular recipient
102 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
103 _dbus_assert (dbus_message_get_sender (message) != NULL);
105 context = bus_transaction_get_context (transaction);
107 /* First, send the message to the addressed_recipient, if there is one. */
108 if (addressed_recipient != NULL)
110 if (!bus_context_check_security_policy (context, transaction,
111 sender, addressed_recipient,
116 if (dbus_message_contains_unix_fds (message) &&
117 !dbus_connection_can_send_type (addressed_recipient,
120 dbus_set_error (error,
121 DBUS_ERROR_NOT_SUPPORTED,
122 "Tried to send message with Unix file descriptors"
123 "to a client that doesn't support that.");
127 /* Dispatch the message */
128 if (!bus_transaction_send (transaction, addressed_recipient, message))
135 /* Now dispatch to others who look interested in this message */
136 connections = bus_transaction_get_connections (transaction);
137 dbus_error_init (&tmp_error);
138 matchmaker = bus_context_get_matchmaker (context);
141 if (!bus_matchmaker_get_recipients (matchmaker, connections,
142 sender, addressed_recipient, message,
149 link = _dbus_list_get_first_link (&recipients);
152 DBusConnection *dest;
156 if (!send_one_message (dest, context, sender, addressed_recipient,
157 message, transaction, &tmp_error))
160 link = _dbus_list_get_next_link (&recipients, link);
163 _dbus_list_clear (&recipients);
165 if (dbus_error_is_set (&tmp_error))
167 dbus_move_error (&tmp_error, error);
174 static DBusHandlerResult
175 bus_dispatch (DBusConnection *connection,
176 DBusMessage *message)
178 const char *sender, *service_name;
180 BusTransaction *transaction;
182 DBusHandlerResult result;
183 DBusConnection *addressed_recipient;
185 result = DBUS_HANDLER_RESULT_HANDLED;
188 addressed_recipient = NULL;
189 dbus_error_init (&error);
191 context = bus_connection_get_context (connection);
192 _dbus_assert (context != NULL);
194 /* If we can't even allocate an OOM error, we just go to sleep
197 while (!bus_connection_preallocate_oom_error (connection))
198 _dbus_wait_for_memory ();
200 /* Ref connection in case we disconnect it at some point in here */
201 dbus_connection_ref (connection);
203 service_name = dbus_message_get_destination (message);
205 #ifdef DBUS_ENABLE_VERBOSE_MODE
207 const char *interface_name, *member_name, *error_name;
209 interface_name = dbus_message_get_interface (message);
210 member_name = dbus_message_get_member (message);
211 error_name = dbus_message_get_error_name (message);
213 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
214 interface_name ? interface_name : "(no interface)",
215 member_name ? member_name : "(no member)",
216 error_name ? error_name : "(no error name)",
217 service_name ? service_name : "peer");
219 #endif /* DBUS_ENABLE_VERBOSE_MODE */
221 /* If service_name is NULL, if it's a signal we send it to all
222 * connections with a match rule. If it's not a signal, there
223 * are some special cases here but mostly we just bail out.
225 if (service_name == NULL)
227 if (dbus_message_is_signal (message,
228 DBUS_INTERFACE_LOCAL,
231 bus_connection_disconnected (connection);
235 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
237 /* DBusConnection also handles some of these automatically, we leave
240 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245 /* Create our transaction */
246 transaction = bus_transaction_new (context);
247 if (transaction == NULL)
249 BUS_SET_OOM (&error);
253 /* Assign a sender to the message */
254 if (bus_connection_is_active (connection))
256 sender = bus_connection_get_name (connection);
257 _dbus_assert (sender != NULL);
259 if (!dbus_message_set_sender (message, sender))
261 BUS_SET_OOM (&error);
265 /* We need to refetch the service name here, because
266 * dbus_message_set_sender can cause the header to be
267 * reallocated, and thus the service_name pointer will become
270 service_name = dbus_message_get_destination (message);
274 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
276 if (!bus_context_check_security_policy (context, transaction,
277 connection, NULL, NULL, message, &error))
279 _dbus_verbose ("Security policy rejected message\n");
283 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
284 if (!bus_driver_handle_message (connection, transaction, message, &error))
287 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
289 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
290 dbus_connection_close (connection);
293 else if (service_name != NULL) /* route to named service */
295 DBusString service_string;
297 BusRegistry *registry;
299 _dbus_assert (service_name != NULL);
301 registry = bus_connection_get_registry (connection);
303 _dbus_string_init_const (&service_string, service_name);
304 service = bus_registry_lookup (registry, &service_string);
306 if (service == NULL && dbus_message_get_auto_start (message))
308 BusActivation *activation;
309 /* We can't do the security policy check here, since the addressed
310 * recipient service doesn't exist yet. We do it before sending the
311 * message after the service has been created.
313 activation = bus_connection_get_activation (connection);
315 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
316 message, service_name, &error))
318 _DBUS_ASSERT_ERROR_IS_SET (&error);
319 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
325 else if (service == NULL)
327 dbus_set_error (&error,
328 DBUS_ERROR_NAME_HAS_NO_OWNER,
329 "Name \"%s\" does not exist",
335 addressed_recipient = bus_service_get_primary_owners_connection (service);
336 _dbus_assert (addressed_recipient != NULL);
340 /* Now send the message to its destination (or not, if
341 * addressed_recipient == NULL), and match it against other connections'
344 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
348 if (dbus_error_is_set (&error))
350 if (!dbus_connection_get_is_connected (connection))
352 /* If we disconnected it, we won't bother to send it any error
355 _dbus_verbose ("Not sending error to connection we disconnected\n");
357 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
359 bus_connection_send_oom_error (connection, message);
361 /* cancel transaction due to OOM */
362 if (transaction != NULL)
364 bus_transaction_cancel_and_free (transaction);
370 /* Try to send the real error, if no mem to do that, send
373 _dbus_assert (transaction != NULL);
374 if (!bus_transaction_send_error_reply (transaction, connection,
377 bus_connection_send_oom_error (connection, message);
379 /* cancel transaction due to OOM */
380 if (transaction != NULL)
382 bus_transaction_cancel_and_free (transaction);
389 dbus_error_free (&error);
392 if (transaction != NULL)
394 bus_transaction_execute_and_free (transaction);
397 dbus_connection_unref (connection);
402 static DBusHandlerResult
403 bus_dispatch_message_filter (DBusConnection *connection,
404 DBusMessage *message,
407 return bus_dispatch (connection, message);
411 bus_dispatch_add_connection (DBusConnection *connection)
413 if (!dbus_connection_add_filter (connection,
414 bus_dispatch_message_filter,
422 bus_dispatch_remove_connection (DBusConnection *connection)
424 /* Here we tell the bus driver that we want to get off. */
425 bus_driver_remove_connection (connection);
427 dbus_connection_remove_filter (connection,
428 bus_dispatch_message_filter,
432 #ifdef DBUS_BUILD_TESTS
436 /* This is used to know whether we need to block in order to finish
437 * sending a message, or whether the initial dbus_connection_send()
438 * already flushed the queue.
440 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
442 typedef dbus_bool_t (* Check1Func) (BusContext *context);
443 typedef dbus_bool_t (* Check2Func) (BusContext *context,
444 DBusConnection *connection);
446 static dbus_bool_t check_no_leftovers (BusContext *context);
449 block_connection_until_message_from_bus (BusContext *context,
450 DBusConnection *connection,
451 const char *what_is_expected)
453 _dbus_verbose ("expecting: %s\n", what_is_expected);
455 while (dbus_connection_get_dispatch_status (connection) ==
456 DBUS_DISPATCH_COMPLETE &&
457 dbus_connection_get_is_connected (connection))
459 bus_test_run_bus_loop (context, TRUE);
460 bus_test_run_clients_loop (FALSE);
465 spin_connection_until_authenticated (BusContext *context,
466 DBusConnection *connection)
468 _dbus_verbose ("Spinning to auth connection %p\n", connection);
469 while (!dbus_connection_get_is_authenticated (connection) &&
470 dbus_connection_get_is_connected (connection))
472 bus_test_run_bus_loop (context, FALSE);
473 bus_test_run_clients_loop (FALSE);
475 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
478 /* compensate for fact that pop_message() can return #NULL due to OOM */
480 pop_message_waiting_for_memory (DBusConnection *connection)
482 while (dbus_connection_get_dispatch_status (connection) ==
483 DBUS_DISPATCH_NEED_MEMORY)
484 _dbus_wait_for_memory ();
486 return dbus_connection_pop_message (connection);
490 borrow_message_waiting_for_memory (DBusConnection *connection)
492 while (dbus_connection_get_dispatch_status (connection) ==
493 DBUS_DISPATCH_NEED_MEMORY)
494 _dbus_wait_for_memory ();
496 return dbus_connection_borrow_message (connection);
500 warn_unexpected_real (DBusConnection *connection,
501 DBusMessage *message,
502 const char *expected,
503 const char *function,
507 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
509 dbus_message_get_interface (message) ?
510 dbus_message_get_interface (message) : "(unset)",
511 dbus_message_get_member (message) ?
512 dbus_message_get_member (message) : "(unset)",
513 dbus_message_get_error_name (message) ?
514 dbus_message_get_error_name (message) : "(unset)",
518 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
519 function, line, connection, expected);
522 #define warn_unexpected(connection, message, expected) \
523 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
526 verbose_message_received (DBusConnection *connection,
527 DBusMessage *message)
529 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
530 dbus_message_get_interface (message) ?
531 dbus_message_get_interface (message) : "(unset)",
532 dbus_message_get_member (message) ?
533 dbus_message_get_member (message) : "(unset)",
534 dbus_message_get_error_name (message) ?
535 dbus_message_get_error_name (message) : "(unset)",
548 ServiceInfoKind expected_kind;
549 const char *expected_service_name;
551 DBusConnection *skip_connection;
552 } CheckServiceOwnerChangedData;
555 check_service_owner_changed_foreach (DBusConnection *connection,
558 CheckServiceOwnerChangedData *d = data;
559 DBusMessage *message;
561 const char *service_name, *old_owner, *new_owner;
563 if (d->expected_kind == SERVICE_CREATED
564 && connection == d->skip_connection)
567 dbus_error_init (&error);
570 message = pop_message_waiting_for_memory (connection);
573 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
574 connection, "NameOwnerChanged");
577 else if (!dbus_message_is_signal (message,
581 warn_unexpected (connection, message, "NameOwnerChanged");
587 reget_service_info_data:
592 dbus_message_get_args (message, &error,
593 DBUS_TYPE_STRING, &service_name,
594 DBUS_TYPE_STRING, &old_owner,
595 DBUS_TYPE_STRING, &new_owner,
598 if (dbus_error_is_set (&error))
600 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
602 dbus_error_free (&error);
603 _dbus_wait_for_memory ();
604 goto reget_service_info_data;
608 _dbus_warn ("Did not get the expected arguments\n");
613 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
614 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
615 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
617 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
621 if (strcmp (service_name, d->expected_service_name) != 0)
623 _dbus_warn ("expected info on service %s, got info on %s\n",
624 d->expected_service_name,
629 if (*service_name == ':' && new_owner[0]
630 && strcmp (service_name, new_owner) != 0)
632 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
633 service_name, old_owner, new_owner);
641 dbus_error_free (&error);
644 dbus_message_unref (message);
651 kill_client_connection (BusContext *context,
652 DBusConnection *connection)
656 CheckServiceOwnerChangedData socd;
658 _dbus_verbose ("killing connection %p\n", connection);
660 s = dbus_bus_get_unique_name (connection);
661 _dbus_assert (s != NULL);
663 while ((base_service = _dbus_strdup (s)) == NULL)
664 _dbus_wait_for_memory ();
666 dbus_connection_ref (connection);
668 /* kick in the disconnect handler that unrefs the connection */
669 dbus_connection_close (connection);
671 bus_test_run_everything (context);
673 _dbus_assert (bus_test_client_listed (connection));
675 /* Run disconnect handler in test.c */
676 if (bus_connection_dispatch_one_message (connection))
677 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
679 _dbus_assert (!dbus_connection_get_is_connected (connection));
680 dbus_connection_unref (connection);
682 _dbus_assert (!bus_test_client_listed (connection));
684 socd.expected_kind = SERVICE_DELETED;
685 socd.expected_service_name = base_service;
687 socd.skip_connection = NULL;
689 bus_test_clients_foreach (check_service_owner_changed_foreach,
692 dbus_free (base_service);
695 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
697 if (!check_no_leftovers (context))
698 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
702 kill_client_connection_unchecked (DBusConnection *connection)
704 /* This kills the connection without expecting it to affect
705 * the rest of the bus.
707 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
709 dbus_connection_ref (connection);
710 dbus_connection_close (connection);
711 /* dispatching disconnect handler will unref once */
712 if (bus_connection_dispatch_one_message (connection))
713 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
715 _dbus_assert (!bus_test_client_listed (connection));
716 dbus_connection_unref (connection);
722 } CheckNoMessagesData;
725 check_no_messages_foreach (DBusConnection *connection,
728 CheckNoMessagesData *d = data;
729 DBusMessage *message;
731 message = pop_message_waiting_for_memory (connection);
734 warn_unexpected (connection, message, "no messages");
740 dbus_message_unref (message);
745 check_no_leftovers (BusContext *context)
747 CheckNoMessagesData nmd;
750 bus_test_clients_foreach (check_no_messages_foreach,
755 _dbus_verbose ("%s: leftover message found\n",
756 _DBUS_FUNCTION_NAME);
763 /* returns TRUE if the correct thing happens,
764 * but the correct thing may include OOM errors.
767 check_hello_message (BusContext *context,
768 DBusConnection *connection)
770 DBusMessage *message;
771 DBusMessage *name_message;
772 dbus_uint32_t serial;
776 const char *acquired;
779 dbus_error_init (&error);
785 _dbus_verbose ("check_hello_message for %p\n", connection);
787 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
795 dbus_connection_ref (connection); /* because we may get disconnected */
797 if (!dbus_connection_send (connection, message, &serial))
799 dbus_message_unref (message);
800 dbus_connection_unref (connection);
804 _dbus_assert (dbus_message_has_signature (message, ""));
806 dbus_message_unref (message);
809 if (!dbus_connection_get_is_connected (connection))
811 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
813 dbus_connection_unref (connection);
818 /* send our message */
819 bus_test_run_clients_loop (SEND_PENDING (connection));
821 if (!dbus_connection_get_is_connected (connection))
823 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
825 dbus_connection_unref (connection);
830 block_connection_until_message_from_bus (context, connection, "reply to Hello");
832 if (!dbus_connection_get_is_connected (connection))
834 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
836 dbus_connection_unref (connection);
841 dbus_connection_unref (connection);
843 message = pop_message_waiting_for_memory (connection);
846 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
847 "Hello", serial, connection);
851 verbose_message_received (connection, message);
853 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
855 _dbus_warn ("Message has wrong sender %s\n",
856 dbus_message_get_sender (message) ?
857 dbus_message_get_sender (message) : "(none)");
861 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
863 if (dbus_message_is_error (message,
864 DBUS_ERROR_NO_MEMORY))
866 ; /* good, this is a valid response */
870 warn_unexpected (connection, message, "not this error");
877 CheckServiceOwnerChangedData socd;
879 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
881 ; /* good, expected */
885 warn_unexpected (connection, message, "method return for Hello");
890 retry_get_hello_name:
891 if (!dbus_message_get_args (message, &error,
892 DBUS_TYPE_STRING, &name,
895 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
897 _dbus_verbose ("no memory to get service name arg from hello\n");
898 dbus_error_free (&error);
899 _dbus_wait_for_memory ();
900 goto retry_get_hello_name;
904 _dbus_assert (dbus_error_is_set (&error));
905 _dbus_warn ("Did not get the expected single string argument to hello\n");
910 _dbus_verbose ("Got hello name: %s\n", name);
912 while (!dbus_bus_set_unique_name (connection, name))
913 _dbus_wait_for_memory ();
915 socd.expected_kind = SERVICE_CREATED;
916 socd.expected_service_name = name;
918 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
919 bus_test_clients_foreach (check_service_owner_changed_foreach,
925 name_message = message;
926 /* Client should also have gotten ServiceAcquired */
928 message = pop_message_waiting_for_memory (connection);
931 _dbus_warn ("Expecting %s, got nothing\n",
935 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
938 _dbus_warn ("Expecting %s, got smthg else\n",
943 retry_get_acquired_name:
944 if (!dbus_message_get_args (message, &error,
945 DBUS_TYPE_STRING, &acquired,
948 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
950 _dbus_verbose ("no memory to get service name arg from acquired\n");
951 dbus_error_free (&error);
952 _dbus_wait_for_memory ();
953 goto retry_get_acquired_name;
957 _dbus_assert (dbus_error_is_set (&error));
958 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
963 _dbus_verbose ("Got acquired name: %s\n", acquired);
965 if (strcmp (acquired, name) != 0)
967 _dbus_warn ("Acquired name is %s but expected %s\n",
974 if (!check_no_leftovers (context))
980 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
982 dbus_error_free (&error);
985 dbus_message_unref (message);
988 dbus_message_unref (name_message);
993 /* returns TRUE if the correct thing happens,
994 * but the correct thing may include OOM errors.
997 check_double_hello_message (BusContext *context,
998 DBusConnection *connection)
1000 DBusMessage *message;
1001 dbus_uint32_t serial;
1006 dbus_error_init (&error);
1009 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1011 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1013 DBUS_INTERFACE_DBUS,
1016 if (message == NULL)
1019 if (!dbus_connection_send (connection, message, &serial))
1021 dbus_message_unref (message);
1025 dbus_message_unref (message);
1028 /* send our message */
1029 bus_test_run_clients_loop (SEND_PENDING (connection));
1031 dbus_connection_ref (connection); /* because we may get disconnected */
1032 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1034 if (!dbus_connection_get_is_connected (connection))
1036 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1038 dbus_connection_unref (connection);
1043 dbus_connection_unref (connection);
1045 message = pop_message_waiting_for_memory (connection);
1046 if (message == NULL)
1048 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1049 "Hello", serial, connection);
1053 verbose_message_received (connection, message);
1055 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1057 _dbus_warn ("Message has wrong sender %s\n",
1058 dbus_message_get_sender (message) ?
1059 dbus_message_get_sender (message) : "(none)");
1063 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1065 warn_unexpected (connection, message, "method return for Hello");
1069 if (!check_no_leftovers (context))
1075 dbus_error_free (&error);
1078 dbus_message_unref (message);
1083 /* returns TRUE if the correct thing happens,
1084 * but the correct thing may include OOM errors.
1087 check_get_connection_unix_user (BusContext *context,
1088 DBusConnection *connection)
1090 DBusMessage *message;
1091 dbus_uint32_t serial;
1094 const char *base_service_name;
1098 dbus_error_init (&error);
1101 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1103 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1105 DBUS_INTERFACE_DBUS,
1106 "GetConnectionUnixUser");
1108 if (message == NULL)
1111 base_service_name = dbus_bus_get_unique_name (connection);
1113 if (!dbus_message_append_args (message,
1114 DBUS_TYPE_STRING, &base_service_name,
1117 dbus_message_unref (message);
1121 if (!dbus_connection_send (connection, message, &serial))
1123 dbus_message_unref (message);
1127 /* send our message */
1128 bus_test_run_clients_loop (SEND_PENDING (connection));
1130 dbus_message_unref (message);
1133 dbus_connection_ref (connection); /* because we may get disconnected */
1134 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1136 if (!dbus_connection_get_is_connected (connection))
1138 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1140 dbus_connection_unref (connection);
1145 dbus_connection_unref (connection);
1147 message = pop_message_waiting_for_memory (connection);
1148 if (message == NULL)
1150 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1151 "GetConnectionUnixUser", serial, connection);
1155 verbose_message_received (connection, message);
1157 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1159 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1161 ; /* good, this is a valid response */
1165 warn_unexpected (connection, message, "not this error");
1172 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1174 ; /* good, expected */
1178 warn_unexpected (connection, message,
1179 "method_return for GetConnectionUnixUser");
1186 if (!dbus_message_get_args (message, &error,
1187 DBUS_TYPE_UINT32, &uid,
1190 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1192 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1193 dbus_error_free (&error);
1194 _dbus_wait_for_memory ();
1195 goto retry_get_property;
1199 _dbus_assert (dbus_error_is_set (&error));
1200 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1206 if (!check_no_leftovers (context))
1212 dbus_error_free (&error);
1215 dbus_message_unref (message);
1220 /* returns TRUE if the correct thing happens,
1221 * but the correct thing may include OOM errors.
1224 check_get_connection_unix_process_id (BusContext *context,
1225 DBusConnection *connection)
1227 DBusMessage *message;
1228 dbus_uint32_t serial;
1231 const char *base_service_name;
1235 dbus_error_init (&error);
1238 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1240 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1242 DBUS_INTERFACE_DBUS,
1243 "GetConnectionUnixProcessID");
1245 if (message == NULL)
1248 base_service_name = dbus_bus_get_unique_name (connection);
1250 if (!dbus_message_append_args (message,
1251 DBUS_TYPE_STRING, &base_service_name,
1254 dbus_message_unref (message);
1258 if (!dbus_connection_send (connection, message, &serial))
1260 dbus_message_unref (message);
1264 /* send our message */
1265 bus_test_run_clients_loop (SEND_PENDING (connection));
1267 dbus_message_unref (message);
1270 dbus_connection_ref (connection); /* because we may get disconnected */
1271 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1273 if (!dbus_connection_get_is_connected (connection))
1275 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1277 dbus_connection_unref (connection);
1282 dbus_connection_unref (connection);
1284 message = pop_message_waiting_for_memory (connection);
1285 if (message == NULL)
1287 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1288 "GetConnectionUnixProcessID", serial, connection);
1292 verbose_message_received (connection, message);
1294 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1296 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1298 ; /* good, this is a valid response */
1301 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1303 /* We are expecting this error, since we know in the test suite we aren't
1304 * talking to a client running on UNIX
1306 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1311 warn_unexpected (connection, message, "not this error");
1319 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1322 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1324 ; /* good, expected */
1328 warn_unexpected (connection, message,
1329 "method_return for GetConnectionUnixProcessID");
1336 if (!dbus_message_get_args (message, &error,
1337 DBUS_TYPE_UINT32, &pid,
1340 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1342 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1343 dbus_error_free (&error);
1344 _dbus_wait_for_memory ();
1345 goto retry_get_property;
1349 _dbus_assert (dbus_error_is_set (&error));
1350 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1356 /* test if returned pid is the same as our own pid
1358 * @todo It would probably be good to restructure the tests
1359 * in a way so our parent is the bus that we're testing
1360 * cause then we can test that the pid returned matches
1363 if (pid != (dbus_uint32_t) _dbus_getpid ())
1365 _dbus_assert (dbus_error_is_set (&error));
1366 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1370 #endif /* !DBUS_WIN */
1373 if (!check_no_leftovers (context))
1379 dbus_error_free (&error);
1382 dbus_message_unref (message);
1387 /* returns TRUE if the correct thing happens,
1388 * but the correct thing may include OOM errors.
1391 check_add_match_all (BusContext *context,
1392 DBusConnection *connection)
1394 DBusMessage *message;
1396 dbus_uint32_t serial;
1398 const char *empty = "";
1401 dbus_error_init (&error);
1404 _dbus_verbose ("check_add_match_all for %p\n", connection);
1406 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1408 DBUS_INTERFACE_DBUS,
1411 if (message == NULL)
1414 /* empty string match rule matches everything */
1415 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1418 dbus_message_unref (message);
1422 if (!dbus_connection_send (connection, message, &serial))
1424 dbus_message_unref (message);
1428 dbus_message_unref (message);
1431 dbus_connection_ref (connection); /* because we may get disconnected */
1433 /* send our message */
1434 bus_test_run_clients_loop (SEND_PENDING (connection));
1436 if (!dbus_connection_get_is_connected (connection))
1438 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1440 dbus_connection_unref (connection);
1445 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1447 if (!dbus_connection_get_is_connected (connection))
1449 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1451 dbus_connection_unref (connection);
1456 dbus_connection_unref (connection);
1458 message = pop_message_waiting_for_memory (connection);
1459 if (message == NULL)
1461 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1462 "AddMatch", serial, connection);
1466 verbose_message_received (connection, message);
1468 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1470 _dbus_warn ("Message has wrong sender %s\n",
1471 dbus_message_get_sender (message) ?
1472 dbus_message_get_sender (message) : "(none)");
1476 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1478 if (dbus_message_is_error (message,
1479 DBUS_ERROR_NO_MEMORY))
1481 ; /* good, this is a valid response */
1485 warn_unexpected (connection, message, "not this error");
1492 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1494 ; /* good, expected */
1495 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1499 warn_unexpected (connection, message, "method return for AddMatch");
1505 if (!check_no_leftovers (context))
1511 dbus_error_free (&error);
1514 dbus_message_unref (message);
1519 /* returns TRUE if the correct thing happens,
1520 * but the correct thing may include OOM errors.
1523 check_hello_connection (BusContext *context)
1525 DBusConnection *connection;
1528 dbus_error_init (&error);
1530 connection = dbus_connection_open_private (TEST_CONNECTION, &error);
1531 if (connection == NULL)
1533 _DBUS_ASSERT_ERROR_IS_SET (&error);
1534 dbus_error_free (&error);
1538 if (!bus_setup_debug_client (connection))
1540 dbus_connection_close (connection);
1541 dbus_connection_unref (connection);
1545 spin_connection_until_authenticated (context, connection);
1547 if (!check_hello_message (context, connection))
1550 if (dbus_bus_get_unique_name (connection) == NULL)
1552 /* We didn't successfully register, so we can't
1553 * do the usual kill_client_connection() checks
1555 kill_client_connection_unchecked (connection);
1559 if (!check_add_match_all (context, connection))
1562 kill_client_connection (context, connection);
1568 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1570 /* returns TRUE if the correct thing happens,
1571 * but the correct thing may include OOM errors.
1574 check_nonexistent_service_no_auto_start (BusContext *context,
1575 DBusConnection *connection)
1577 DBusMessage *message;
1578 dbus_uint32_t serial;
1580 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1581 dbus_uint32_t flags;
1583 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1585 DBUS_INTERFACE_DBUS,
1586 "StartServiceByName");
1588 if (message == NULL)
1591 dbus_message_set_auto_start (message, FALSE);
1594 if (!dbus_message_append_args (message,
1595 DBUS_TYPE_STRING, &nonexistent,
1596 DBUS_TYPE_UINT32, &flags,
1599 dbus_message_unref (message);
1603 if (!dbus_connection_send (connection, message, &serial))
1605 dbus_message_unref (message);
1609 dbus_message_unref (message);
1612 bus_test_run_everything (context);
1613 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1614 bus_test_run_everything (context);
1616 if (!dbus_connection_get_is_connected (connection))
1618 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1624 message = pop_message_waiting_for_memory (connection);
1625 if (message == NULL)
1627 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1628 "StartServiceByName", serial, connection);
1632 verbose_message_received (connection, message);
1634 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1636 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1638 _dbus_warn ("Message has wrong sender %s\n",
1639 dbus_message_get_sender (message) ?
1640 dbus_message_get_sender (message) : "(none)");
1644 if (dbus_message_is_error (message,
1645 DBUS_ERROR_NO_MEMORY))
1647 ; /* good, this is a valid response */
1649 else if (dbus_message_is_error (message,
1650 DBUS_ERROR_SERVICE_UNKNOWN))
1652 ; /* good, this is expected also */
1656 warn_unexpected (connection, message, "not this error");
1662 _dbus_warn ("Did not expect to successfully activate %s\n",
1663 NONEXISTENT_SERVICE_NAME);
1671 dbus_message_unref (message);
1676 /* returns TRUE if the correct thing happens,
1677 * but the correct thing may include OOM errors.
1680 check_nonexistent_service_auto_start (BusContext *context,
1681 DBusConnection *connection)
1683 DBusMessage *message;
1684 dbus_uint32_t serial;
1687 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1688 "/org/freedesktop/TestSuite",
1689 "org.freedesktop.TestSuite",
1692 if (message == NULL)
1695 if (!dbus_connection_send (connection, message, &serial))
1697 dbus_message_unref (message);
1701 dbus_message_unref (message);
1704 bus_test_run_everything (context);
1705 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1706 bus_test_run_everything (context);
1708 if (!dbus_connection_get_is_connected (connection))
1710 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1716 message = pop_message_waiting_for_memory (connection);
1718 if (message == NULL)
1720 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1721 "Echo message (auto activation)", serial, connection);
1725 verbose_message_received (connection, message);
1727 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1729 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1731 _dbus_warn ("Message has wrong sender %s\n",
1732 dbus_message_get_sender (message) ?
1733 dbus_message_get_sender (message) : "(none)");
1737 if (dbus_message_is_error (message,
1738 DBUS_ERROR_NO_MEMORY))
1740 ; /* good, this is a valid response */
1742 else if (dbus_message_is_error (message,
1743 DBUS_ERROR_SERVICE_UNKNOWN))
1745 ; /* good, this is expected also */
1749 warn_unexpected (connection, message, "not this error");
1755 _dbus_warn ("Did not expect to successfully activate %s\n",
1756 NONEXISTENT_SERVICE_NAME);
1764 dbus_message_unref (message);
1770 check_base_service_activated (BusContext *context,
1771 DBusConnection *connection,
1772 DBusMessage *initial_message,
1773 const char **base_service_p)
1775 DBusMessage *message;
1778 const char *base_service, *base_service_from_bus, *old_owner;
1782 dbus_error_init (&error);
1783 base_service = NULL;
1785 base_service_from_bus = NULL;
1787 message = initial_message;
1788 dbus_message_ref (message);
1790 if (dbus_message_is_signal (message,
1791 DBUS_INTERFACE_DBUS,
1792 "NameOwnerChanged"))
1794 CheckServiceOwnerChangedData socd;
1796 reget_service_name_arg:
1797 base_service = NULL;
1799 base_service_from_bus = NULL;
1801 if (!dbus_message_get_args (message, &error,
1802 DBUS_TYPE_STRING, &base_service,
1803 DBUS_TYPE_STRING, &old_owner,
1804 DBUS_TYPE_STRING, &base_service_from_bus,
1807 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1809 dbus_error_free (&error);
1810 _dbus_wait_for_memory ();
1811 goto reget_service_name_arg;
1815 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1816 "NameOwnerChanged (creation)",
1822 if (*base_service != ':')
1824 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1829 if (strcmp (base_service, base_service_from_bus) != 0)
1831 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1832 base_service, base_service_from_bus);
1838 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1843 socd.expected_kind = SERVICE_CREATED;
1844 socd.expected_service_name = base_service;
1845 socd.failed = FALSE;
1846 socd.skip_connection = connection;
1847 bus_test_clients_foreach (check_service_owner_changed_foreach,
1855 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1861 *base_service_p = base_service;
1867 dbus_message_unref (message);
1868 dbus_error_free (&error);
1874 check_service_activated (BusContext *context,
1875 DBusConnection *connection,
1876 const char *activated_name,
1877 const char *base_service_name,
1878 DBusMessage *initial_message)
1880 DBusMessage *message;
1883 dbus_uint32_t activation_result;
1887 dbus_error_init (&error);
1889 message = initial_message;
1890 dbus_message_ref (message);
1892 if (dbus_message_is_signal (message,
1893 DBUS_INTERFACE_DBUS,
1894 "NameOwnerChanged"))
1896 CheckServiceOwnerChangedData socd;
1897 const char *service_name, *base_service_from_bus, *old_owner;
1899 reget_service_name_arg:
1900 service_name = NULL;
1902 base_service_from_bus = NULL;
1904 if (!dbus_message_get_args (message, &error,
1905 DBUS_TYPE_STRING, &service_name,
1906 DBUS_TYPE_STRING, &old_owner,
1907 DBUS_TYPE_STRING, &base_service_from_bus,
1910 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1912 dbus_error_free (&error);
1913 _dbus_wait_for_memory ();
1914 goto reget_service_name_arg;
1918 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1919 "NameOwnerChanged (creation)",
1925 if (strcmp (service_name, activated_name) != 0)
1927 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1928 activated_name, service_name);
1932 if (strcmp (base_service_name, base_service_from_bus) != 0)
1934 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1935 base_service_from_bus, base_service_name);
1941 _dbus_warn ("expected a %s, got a %s\n",
1942 "NameOwnerChanged (creation)",
1943 "NameOwnerChanged (change)");
1947 socd.expected_kind = SERVICE_CREATED;
1948 socd.skip_connection = connection;
1949 socd.failed = FALSE;
1950 socd.expected_service_name = service_name;
1951 bus_test_clients_foreach (check_service_owner_changed_foreach,
1957 dbus_message_unref (message);
1958 service_name = NULL;
1960 base_service_from_bus = NULL;
1962 message = pop_message_waiting_for_memory (connection);
1963 if (message == NULL)
1965 _dbus_warn ("Expected a reply to %s, got nothing\n",
1966 "StartServiceByName");
1972 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1977 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1979 warn_unexpected (connection, message, "reply to StartServiceByName");
1984 activation_result = 0;
1985 if (!dbus_message_get_args (message, &error,
1986 DBUS_TYPE_UINT32, &activation_result,
1989 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1991 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1992 "StartServiceByName", error.message);
1996 dbus_error_free (&error);
2000 if (activation_result == DBUS_START_REPLY_SUCCESS)
2002 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2006 _dbus_warn ("Activation result was %u, no good.\n",
2012 dbus_message_unref (message);
2015 if (!check_no_leftovers (context))
2017 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2025 dbus_message_unref (message);
2026 dbus_error_free (&error);
2032 check_service_auto_activated (BusContext *context,
2033 DBusConnection *connection,
2034 const char *activated_name,
2035 const char *base_service_name,
2036 DBusMessage *initial_message)
2038 DBusMessage *message;
2044 dbus_error_init (&error);
2046 message = initial_message;
2047 dbus_message_ref (message);
2049 if (dbus_message_is_signal (message,
2050 DBUS_INTERFACE_DBUS,
2051 "NameOwnerChanged"))
2053 const char *service_name;
2054 CheckServiceOwnerChangedData socd;
2056 reget_service_name_arg:
2057 if (!dbus_message_get_args (message, &error,
2058 DBUS_TYPE_STRING, &service_name,
2061 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2063 dbus_error_free (&error);
2064 _dbus_wait_for_memory ();
2065 goto reget_service_name_arg;
2069 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2072 dbus_error_free (&error);
2077 if (strcmp (service_name, activated_name) != 0)
2079 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2080 activated_name, service_name);
2084 socd.expected_kind = SERVICE_CREATED;
2085 socd.expected_service_name = service_name;
2086 socd.failed = FALSE;
2087 socd.skip_connection = connection;
2088 bus_test_clients_foreach (check_service_owner_changed_foreach,
2094 /* Note that this differs from regular activation in that we don't get a
2095 * reply to ActivateService here.
2098 dbus_message_unref (message);
2100 service_name = NULL;
2104 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2113 dbus_message_unref (message);
2119 check_service_deactivated (BusContext *context,
2120 DBusConnection *connection,
2121 const char *activated_name,
2122 const char *base_service)
2125 CheckServiceOwnerChangedData socd;
2129 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2130 * service and the activated_name. The base service
2131 * notification is required to come last.
2133 socd.expected_kind = SERVICE_DELETED;
2134 socd.expected_service_name = activated_name;
2135 socd.failed = FALSE;
2136 socd.skip_connection = NULL;
2137 bus_test_clients_foreach (check_service_owner_changed_foreach,
2143 socd.expected_kind = SERVICE_DELETED;
2144 socd.expected_service_name = base_service;
2145 socd.failed = FALSE;
2146 socd.skip_connection = NULL;
2147 bus_test_clients_foreach (check_service_owner_changed_foreach,
2160 check_send_exit_to_service (BusContext *context,
2161 DBusConnection *connection,
2162 const char *service_name,
2163 const char *base_service)
2165 dbus_bool_t got_error;
2166 DBusMessage *message;
2167 dbus_uint32_t serial;
2170 _dbus_verbose ("Sending exit message to the test service\n");
2174 /* Kill off the test service by sending it a quit message */
2175 message = dbus_message_new_method_call (service_name,
2176 "/org/freedesktop/TestSuite",
2177 "org.freedesktop.TestSuite",
2180 if (message == NULL)
2182 /* Do this again; we still need the service to exit... */
2183 if (!check_send_exit_to_service (context, connection,
2184 service_name, base_service))
2190 if (!dbus_connection_send (connection, message, &serial))
2192 dbus_message_unref (message);
2194 /* Do this again; we still need the service to exit... */
2195 if (!check_send_exit_to_service (context, connection,
2196 service_name, base_service))
2202 dbus_message_unref (message);
2206 bus_test_run_clients_loop (SEND_PENDING (connection));
2208 /* read it in and write it out to test service */
2209 bus_test_run_bus_loop (context, FALSE);
2211 /* see if we got an error during message bus dispatching */
2212 bus_test_run_clients_loop (FALSE);
2213 message = borrow_message_waiting_for_memory (connection);
2214 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2217 dbus_connection_return_message (connection, message);
2223 /* If no error, wait for the test service to exit */
2224 block_connection_until_message_from_bus (context, connection, "test service to exit");
2226 bus_test_run_everything (context);
2231 message = pop_message_waiting_for_memory (connection);
2232 _dbus_assert (message != NULL);
2234 if (dbus_message_get_reply_serial (message) != serial)
2236 warn_unexpected (connection, message,
2237 "error with the correct reply serial");
2241 if (!dbus_message_is_error (message,
2242 DBUS_ERROR_NO_MEMORY))
2244 warn_unexpected (connection, message,
2245 "a no memory error from asking test service to exit");
2249 _dbus_verbose ("Got error %s when asking test service to exit\n",
2250 dbus_message_get_error_name (message));
2252 /* Do this again; we still need the service to exit... */
2253 if (!check_send_exit_to_service (context, connection,
2254 service_name, base_service))
2259 if (!check_service_deactivated (context, connection,
2260 service_name, base_service))
2263 /* Should now have a NoReply error from the Exit() method
2264 * call; it should have come after all the deactivation
2267 message = pop_message_waiting_for_memory (connection);
2269 if (message == NULL)
2271 warn_unexpected (connection, NULL,
2272 "reply to Exit() method call");
2275 if (!dbus_message_is_error (message,
2276 DBUS_ERROR_NO_REPLY))
2278 warn_unexpected (connection, message,
2279 "NoReply error from Exit() method call");
2283 if (dbus_message_get_reply_serial (message) != serial)
2285 warn_unexpected (connection, message,
2286 "error with the correct reply serial");
2290 _dbus_verbose ("Got error %s after test service exited\n",
2291 dbus_message_get_error_name (message));
2293 if (!check_no_leftovers (context))
2295 _dbus_warn ("Messages were left over after %s\n",
2296 _DBUS_FUNCTION_NAME);
2305 dbus_message_unref (message);
2311 check_got_error (BusContext *context,
2312 DBusConnection *connection,
2313 const char *first_error_name,
2316 DBusMessage *message;
2319 dbus_bool_t error_found;
2320 const char *error_name;
2324 message = pop_message_waiting_for_memory (connection);
2325 if (message == NULL)
2327 _dbus_warn ("Did not get an expected error\n");
2331 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2333 warn_unexpected (connection, message, "an error");
2338 error_found = FALSE;
2340 va_start (ap, first_error_name);
2341 error_name = first_error_name;
2342 while (error_name != NULL)
2344 if (dbus_message_is_error (message, error_name))
2349 error_name = va_arg (ap, char*);
2355 _dbus_warn ("Expected error %s or other, got %s instead\n",
2357 dbus_message_get_error_name (message));
2365 dbus_message_unref (message);
2372 GOT_SERVICE_CREATED,
2373 GOT_SERVICE_DELETED,
2378 static GotServiceInfo
2379 check_got_service_info (DBusMessage *message)
2381 GotServiceInfo message_kind;
2383 if (dbus_message_is_signal (message,
2384 DBUS_INTERFACE_DBUS,
2385 "NameOwnerChanged"))
2388 const char *service_name, *old_owner, *new_owner;
2389 dbus_error_init (&error);
2391 reget_service_info_data:
2392 service_name = NULL;
2396 dbus_message_get_args (message, &error,
2397 DBUS_TYPE_STRING, &service_name,
2398 DBUS_TYPE_STRING, &old_owner,
2399 DBUS_TYPE_STRING, &new_owner,
2401 if (dbus_error_is_set (&error))
2403 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2405 dbus_error_free (&error);
2406 goto reget_service_info_data;
2410 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2411 message_kind = GOT_SOMETHING_ELSE;
2414 else if (!old_owner[0])
2415 message_kind = GOT_SERVICE_CREATED;
2416 else if (!new_owner[0])
2417 message_kind = GOT_SERVICE_DELETED;
2419 message_kind = GOT_SOMETHING_ELSE;
2421 dbus_error_free (&error);
2423 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2424 message_kind = GOT_ERROR;
2426 message_kind = GOT_SOMETHING_ELSE;
2428 return message_kind;
2431 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2433 /* returns TRUE if the correct thing happens,
2434 * but the correct thing may include OOM errors.
2437 check_existent_service_no_auto_start (BusContext *context,
2438 DBusConnection *connection)
2440 DBusMessage *message;
2441 DBusMessage *base_service_message;
2442 const char *base_service;
2443 dbus_uint32_t serial;
2445 const char *existent = EXISTENT_SERVICE_NAME;
2446 dbus_uint32_t flags;
2448 base_service_message = NULL;
2450 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2452 DBUS_INTERFACE_DBUS,
2453 "StartServiceByName");
2455 if (message == NULL)
2458 dbus_message_set_auto_start (message, FALSE);
2461 if (!dbus_message_append_args (message,
2462 DBUS_TYPE_STRING, &existent,
2463 DBUS_TYPE_UINT32, &flags,
2466 dbus_message_unref (message);
2470 if (!dbus_connection_send (connection, message, &serial))
2472 dbus_message_unref (message);
2476 dbus_message_unref (message);
2479 bus_test_run_everything (context);
2481 /* now wait for the message bus to hear back from the activated
2484 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2486 bus_test_run_everything (context);
2488 if (!dbus_connection_get_is_connected (connection))
2490 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2496 message = pop_message_waiting_for_memory (connection);
2497 if (message == NULL)
2499 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2500 "StartServiceByName", serial, connection);
2504 verbose_message_received (connection, message);
2505 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2507 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2509 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2511 _dbus_warn ("Message has wrong sender %s\n",
2512 dbus_message_get_sender (message) ?
2513 dbus_message_get_sender (message) : "(none)");
2517 if (dbus_message_is_error (message,
2518 DBUS_ERROR_NO_MEMORY))
2520 ; /* good, this is a valid response */
2522 else if (dbus_message_is_error (message,
2523 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2524 dbus_message_is_error (message,
2525 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2526 dbus_message_is_error (message,
2527 DBUS_ERROR_SPAWN_EXEC_FAILED))
2529 ; /* good, this is expected also */
2533 _dbus_warn ("Did not expect error %s\n",
2534 dbus_message_get_error_name (message));
2540 GotServiceInfo message_kind;
2542 if (!check_base_service_activated (context, connection,
2543 message, &base_service))
2546 base_service_message = message;
2549 /* We may need to block here for the test service to exit or finish up */
2550 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2552 message = dbus_connection_borrow_message (connection);
2553 if (message == NULL)
2555 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2559 message_kind = check_got_service_info (message);
2561 dbus_connection_return_message (connection, message);
2564 switch (message_kind)
2566 case GOT_SOMETHING_ELSE:
2567 _dbus_warn ("Unexpected message after ActivateService "
2568 "(should be an error or a service announcement");
2572 if (!check_got_error (context, connection,
2573 DBUS_ERROR_SPAWN_CHILD_EXITED,
2574 DBUS_ERROR_NO_MEMORY,
2577 /* A service deleted should be coming along now after this error.
2578 * We can also get the error *after* the service deleted.
2583 case GOT_SERVICE_DELETED:
2585 /* The service started up and got a base address, but then
2586 * failed to register under EXISTENT_SERVICE_NAME
2588 CheckServiceOwnerChangedData socd;
2590 socd.expected_kind = SERVICE_DELETED;
2591 socd.expected_service_name = base_service;
2592 socd.failed = FALSE;
2593 socd.skip_connection = NULL;
2595 bus_test_clients_foreach (check_service_owner_changed_foreach,
2601 /* Now we should get an error about the service exiting
2602 * if we didn't get it before.
2604 if (message_kind != GOT_ERROR)
2606 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2608 /* and process everything again */
2609 bus_test_run_everything (context);
2611 if (!check_got_error (context, connection,
2612 DBUS_ERROR_SPAWN_CHILD_EXITED,
2613 DBUS_ERROR_NO_MEMORY,
2620 case GOT_SERVICE_CREATED:
2621 message = pop_message_waiting_for_memory (connection);
2622 if (message == NULL)
2624 _dbus_warn ("Failed to pop message we just put back! "
2625 "should have been a NameOwnerChanged (creation)\n");
2629 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2630 base_service, message))
2633 dbus_message_unref (message);
2636 if (!check_no_leftovers (context))
2638 _dbus_warn ("Messages were left over after successful activation\n");
2642 if (!check_send_exit_to_service (context, connection,
2643 EXISTENT_SERVICE_NAME, base_service))
2654 dbus_message_unref (message);
2656 if (base_service_message)
2657 dbus_message_unref (base_service_message);
2662 #ifndef DBUS_WIN_FIXME
2663 /* returns TRUE if the correct thing happens,
2664 * but the correct thing may include OOM errors.
2667 check_segfault_service_no_auto_start (BusContext *context,
2668 DBusConnection *connection)
2670 DBusMessage *message;
2671 dbus_uint32_t serial;
2673 const char *segv_service;
2674 dbus_uint32_t flags;
2676 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2678 DBUS_INTERFACE_DBUS,
2679 "StartServiceByName");
2681 if (message == NULL)
2684 dbus_message_set_auto_start (message, FALSE);
2686 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2688 if (!dbus_message_append_args (message,
2689 DBUS_TYPE_STRING, &segv_service,
2690 DBUS_TYPE_UINT32, &flags,
2693 dbus_message_unref (message);
2697 if (!dbus_connection_send (connection, message, &serial))
2699 dbus_message_unref (message);
2703 dbus_message_unref (message);
2706 bus_test_run_everything (context);
2707 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2708 bus_test_run_everything (context);
2710 if (!dbus_connection_get_is_connected (connection))
2712 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2718 message = pop_message_waiting_for_memory (connection);
2719 if (message == NULL)
2721 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2722 "StartServiceByName", serial, connection);
2726 verbose_message_received (connection, message);
2728 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2730 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2732 _dbus_warn ("Message has wrong sender %s\n",
2733 dbus_message_get_sender (message) ?
2734 dbus_message_get_sender (message) : "(none)");
2738 if (dbus_message_is_error (message,
2739 DBUS_ERROR_NO_MEMORY))
2741 ; /* good, this is a valid response */
2743 else if (dbus_message_is_error (message,
2746 const char *servicehelper;
2747 servicehelper = bus_context_get_servicehelper (context);
2748 /* make sure this only happens with the launch helper */
2749 _dbus_assert (servicehelper != NULL);
2751 else if (dbus_message_is_error (message,
2752 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2754 ; /* good, this is expected also */
2758 warn_unexpected (connection, message, "not this error");
2765 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2773 dbus_message_unref (message);
2779 /* returns TRUE if the correct thing happens,
2780 * but the correct thing may include OOM errors.
2783 check_segfault_service_auto_start (BusContext *context,
2784 DBusConnection *connection)
2786 DBusMessage *message;
2787 dbus_uint32_t serial;
2790 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2791 "/org/freedesktop/TestSuite",
2792 "org.freedesktop.TestSuite",
2795 if (message == NULL)
2798 if (!dbus_connection_send (connection, message, &serial))
2800 dbus_message_unref (message);
2804 dbus_message_unref (message);
2807 bus_test_run_everything (context);
2808 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2809 bus_test_run_everything (context);
2811 if (!dbus_connection_get_is_connected (connection))
2813 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2819 message = pop_message_waiting_for_memory (connection);
2820 if (message == NULL)
2822 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2823 "Echo message (auto activation)", serial, connection);
2827 verbose_message_received (connection, message);
2829 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2831 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2833 _dbus_warn ("Message has wrong sender %s\n",
2834 dbus_message_get_sender (message) ?
2835 dbus_message_get_sender (message) : "(none)");
2839 if (dbus_message_is_error (message,
2840 DBUS_ERROR_NO_MEMORY))
2842 ; /* good, this is a valid response */
2844 else if (dbus_message_is_error (message,
2845 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2847 ; /* good, this is expected also */
2851 warn_unexpected (connection, message, "not this error");
2858 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2866 dbus_message_unref (message);
2872 #define TEST_ECHO_MESSAGE "Test echo message"
2873 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2875 /* returns TRUE if the correct thing happens,
2876 * but the correct thing may include OOM errors.
2879 check_existent_hello_from_self (BusContext *context,
2880 DBusConnection *connection)
2882 DBusMessage *message;
2883 dbus_uint32_t serial;
2886 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2887 "/org/freedesktop/TestSuite",
2888 "org.freedesktop.TestSuite",
2889 "RunHelloFromSelf");
2891 if (message == NULL)
2894 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2895 if (!dbus_message_append_args (message,
2896 DBUS_TYPE_STRING, &text,
2899 dbus_message_unref (message);
2903 if (!dbus_connection_send (connection, message, &serial))
2905 dbus_message_unref (message);
2909 dbus_message_unref (message);
2912 bus_test_run_everything (context);
2914 /* Note: if this test is run in OOM mode, it will block when the bus
2915 * doesn't send a reply due to OOM.
2917 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2919 message = pop_message_waiting_for_memory (connection);
2920 if (message == NULL)
2922 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2926 if (dbus_message_get_reply_serial (message) != serial)
2928 _dbus_warn ("Wrong reply serial\n");
2929 dbus_message_unref (message);
2933 dbus_message_unref (message);
2939 /* returns TRUE if the correct thing happens,
2940 * but the correct thing may include OOM errors.
2943 check_existent_ping (BusContext *context,
2944 DBusConnection *connection)
2946 DBusMessage *message;
2947 dbus_uint32_t serial;
2948 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2949 "/org/freedesktop/TestSuite",
2950 "org.freedesktop.DBus.Peer",
2953 if (message == NULL)
2956 if (!dbus_connection_send (connection, message, &serial))
2958 dbus_message_unref (message);
2962 dbus_message_unref (message);
2965 bus_test_run_everything (context);
2967 /* Note: if this test is run in OOM mode, it will block when the bus
2968 * doesn't send a reply due to OOM.
2970 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2972 message = pop_message_waiting_for_memory (connection);
2973 if (message == NULL)
2975 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2979 if (dbus_message_get_reply_serial (message) != serial)
2981 _dbus_warn ("Wrong reply serial\n");
2982 dbus_message_unref (message);
2986 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2988 _dbus_warn ("Unexpected message return during Ping\n");
2989 dbus_message_unref (message);
2993 dbus_message_unref (message);
2999 /* returns TRUE if the correct thing happens,
3000 * but the correct thing may include OOM errors.
3003 check_existent_get_machine_id (BusContext *context,
3004 DBusConnection *connection)
3006 DBusMessage *message;
3007 dbus_uint32_t serial;
3008 const char *machine_id;
3010 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3011 "/org/freedesktop/TestSuite",
3012 "org.freedesktop.DBus.Peer",
3015 if (message == NULL)
3018 if (!dbus_connection_send (connection, message, &serial))
3020 dbus_message_unref (message);
3024 dbus_message_unref (message);
3027 bus_test_run_everything (context);
3029 /* Note: if this test is run in OOM mode, it will block when the bus
3030 * doesn't send a reply due to OOM.
3032 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3034 message = pop_message_waiting_for_memory (connection);
3035 if (message == NULL)
3037 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3041 if (dbus_message_get_reply_serial (message) != serial)
3043 _dbus_warn ("Wrong reply serial\n");
3044 dbus_message_unref (message);
3048 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3050 _dbus_warn ("Unexpected message return during GetMachineId\n");
3051 dbus_message_unref (message);
3056 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3058 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3059 dbus_message_unref (message);
3063 if (machine_id == NULL || strlen (machine_id) != 32)
3065 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3066 dbus_message_unref (message);
3070 /* We can't check that the machine id is correct because during make check it is
3071 * just made up for each process separately
3074 dbus_message_unref (message);
3080 /* returns TRUE if the correct thing happens,
3081 * but the correct thing may include OOM errors.
3084 check_existent_service_auto_start (BusContext *context,
3085 DBusConnection *connection)
3087 DBusMessage *message;
3088 DBusMessage *base_service_message;
3089 dbus_uint32_t serial;
3091 const char *base_service;
3094 base_service_message = NULL;
3096 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3097 "/org/freedesktop/TestSuite",
3098 "org.freedesktop.TestSuite",
3101 if (message == NULL)
3104 text = TEST_ECHO_MESSAGE;
3105 if (!dbus_message_append_args (message,
3106 DBUS_TYPE_STRING, &text,
3109 dbus_message_unref (message);
3113 if (!dbus_connection_send (connection, message, &serial))
3115 dbus_message_unref (message);
3119 dbus_message_unref (message);
3122 bus_test_run_everything (context);
3124 /* now wait for the message bus to hear back from the activated
3127 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3128 bus_test_run_everything (context);
3130 if (!dbus_connection_get_is_connected (connection))
3132 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3138 message = pop_message_waiting_for_memory (connection);
3139 if (message == NULL)
3141 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3142 serial, connection);
3146 verbose_message_received (connection, message);
3147 _dbus_verbose (" (after sending %s)\n", "auto start");
3149 /* we should get zero or two ServiceOwnerChanged signals */
3150 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3152 GotServiceInfo message_kind;
3154 if (!check_base_service_activated (context, connection,
3155 message, &base_service))
3158 base_service_message = message;
3161 /* We may need to block here for the test service to exit or finish up */
3162 block_connection_until_message_from_bus (context, connection, "service to exit");
3164 /* Should get a service creation notification for the activated
3165 * service name, or a service deletion on the base service name
3167 message = dbus_connection_borrow_message (connection);
3168 if (message == NULL)
3170 _dbus_warn ("No message after auto activation "
3171 "(should be a service announcement)\n");
3172 dbus_connection_return_message (connection, message);
3177 message_kind = check_got_service_info (message);
3179 dbus_connection_return_message (connection, message);
3182 switch (message_kind)
3184 case GOT_SERVICE_CREATED:
3185 message = pop_message_waiting_for_memory (connection);
3186 if (message == NULL)
3188 _dbus_warn ("Failed to pop message we just put back! "
3189 "should have been a NameOwnerChanged (creation)\n");
3193 /* Check that ServiceOwnerChanged (creation) was correctly received */
3194 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3195 base_service, message))
3198 dbus_message_unref (message);
3203 case GOT_SERVICE_DELETED:
3205 /* The service started up and got a base address, but then
3206 * failed to register under EXISTENT_SERVICE_NAME
3208 CheckServiceOwnerChangedData socd;
3210 socd.expected_kind = SERVICE_DELETED;
3211 socd.expected_service_name = base_service;
3212 socd.failed = FALSE;
3213 socd.skip_connection = NULL;
3214 bus_test_clients_foreach (check_service_owner_changed_foreach,
3224 case GOT_SOMETHING_ELSE:
3225 _dbus_warn ("Unexpected message after auto activation\n");
3230 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3231 * come the method reply (or error) from the initial method call
3234 /* Note: if this test is run in OOM mode, it will block when the bus
3235 * doesn't send a reply due to OOM.
3237 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3239 message = pop_message_waiting_for_memory (connection);
3240 if (message == NULL)
3242 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3246 if (dbus_message_get_reply_serial (message) != serial)
3248 _dbus_warn ("Wrong reply serial\n");
3252 dbus_message_unref (message);
3255 if (!check_existent_ping (context, connection))
3258 if (!check_existent_get_machine_id (context, connection))
3261 if (!check_existent_hello_from_self (context, connection))
3264 if (!check_send_exit_to_service (context, connection,
3265 EXISTENT_SERVICE_NAME,
3273 dbus_message_unref (message);
3275 if (base_service_message)
3276 dbus_message_unref (base_service_message);
3281 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3283 /* returns TRUE if the correct thing happens,
3284 * but the correct thing may include OOM errors.
3287 check_launch_service_file_missing (BusContext *context,
3288 DBusConnection *connection)
3290 DBusMessage *message;
3291 dbus_uint32_t serial;
3294 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3295 "/org/freedesktop/TestSuite",
3296 "org.freedesktop.TestSuite",
3299 if (message == NULL)
3302 if (!dbus_connection_send (connection, message, &serial))
3304 dbus_message_unref (message);
3308 dbus_message_unref (message);
3311 bus_test_run_everything (context);
3312 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3313 bus_test_run_everything (context);
3315 if (!dbus_connection_get_is_connected (connection))
3317 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3323 message = pop_message_waiting_for_memory (connection);
3324 if (message == NULL)
3326 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3327 "Echo message (auto activation)", serial, connection);
3331 verbose_message_received (connection, message);
3333 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3335 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3337 _dbus_warn ("Message has wrong sender %s\n",
3338 dbus_message_get_sender (message) ?
3339 dbus_message_get_sender (message) : "(none)");
3343 if (dbus_message_is_error (message,
3344 DBUS_ERROR_NO_MEMORY))
3346 ; /* good, this is a valid response */
3348 else if (dbus_message_is_error (message,
3349 DBUS_ERROR_SERVICE_UNKNOWN))
3351 _dbus_verbose("got service unknown\n");
3352 ; /* good, this is expected (only valid when using launch helper) */
3356 warn_unexpected (connection, message, "not this error");
3363 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3371 dbus_message_unref (message);
3376 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3378 /* returns TRUE if the correct thing happens,
3379 * but the correct thing may include OOM errors.
3382 check_launch_service_user_missing (BusContext *context,
3383 DBusConnection *connection)
3385 DBusMessage *message;
3386 dbus_uint32_t serial;
3389 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3390 "/org/freedesktop/TestSuite",
3391 "org.freedesktop.TestSuite",
3394 if (message == NULL)
3397 if (!dbus_connection_send (connection, message, &serial))
3399 dbus_message_unref (message);
3403 dbus_message_unref (message);
3406 bus_test_run_everything (context);
3407 block_connection_until_message_from_bus (context, connection,
3408 "reply to service which should fail to auto-start (missing User)");
3409 bus_test_run_everything (context);
3411 if (!dbus_connection_get_is_connected (connection))
3413 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3419 message = pop_message_waiting_for_memory (connection);
3420 if (message == NULL)
3422 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3423 "Echo message (auto activation)", serial, connection);
3427 verbose_message_received (connection, message);
3429 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3431 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3433 _dbus_warn ("Message has wrong sender %s\n",
3434 dbus_message_get_sender (message) ?
3435 dbus_message_get_sender (message) : "(none)");
3439 if (dbus_message_is_error (message,
3440 DBUS_ERROR_NO_MEMORY))
3442 ; /* good, this is a valid response */
3444 else if (dbus_message_is_error (message,
3445 DBUS_ERROR_SPAWN_FILE_INVALID))
3447 _dbus_verbose("got service file invalid\n");
3448 ; /* good, this is expected (only valid when using launch helper) */
3452 warn_unexpected (connection, message, "not this error");
3459 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3467 dbus_message_unref (message);
3472 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3474 /* returns TRUE if the correct thing happens,
3475 * but the correct thing may include OOM errors.
3478 check_launch_service_exec_missing (BusContext *context,
3479 DBusConnection *connection)
3481 DBusMessage *message;
3482 dbus_uint32_t serial;
3485 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3486 "/org/freedesktop/TestSuite",
3487 "org.freedesktop.TestSuite",
3490 if (message == NULL)
3493 if (!dbus_connection_send (connection, message, &serial))
3495 dbus_message_unref (message);
3499 dbus_message_unref (message);
3502 bus_test_run_everything (context);
3503 block_connection_until_message_from_bus (context, connection,
3504 "reply to service which should fail to auto-start (missing Exec)");
3505 bus_test_run_everything (context);
3507 if (!dbus_connection_get_is_connected (connection))
3509 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3515 message = pop_message_waiting_for_memory (connection);
3516 if (message == NULL)
3518 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3519 "Echo message (auto activation)", serial, connection);
3523 verbose_message_received (connection, message);
3525 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3527 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3529 _dbus_warn ("Message has wrong sender %s\n",
3530 dbus_message_get_sender (message) ?
3531 dbus_message_get_sender (message) : "(none)");
3535 if (dbus_message_is_error (message,
3536 DBUS_ERROR_NO_MEMORY))
3538 ; /* good, this is a valid response */
3540 else if (dbus_message_is_error (message,
3541 DBUS_ERROR_SERVICE_UNKNOWN))
3543 _dbus_verbose("could not activate as invalid service file was not added\n");
3544 ; /* good, this is expected as we shouldn't have been added to
3545 * the activation list with a missing Exec key */
3547 else if (dbus_message_is_error (message,
3548 DBUS_ERROR_SPAWN_FILE_INVALID))
3550 _dbus_verbose("got service file invalid\n");
3551 ; /* good, this is allowed, and is the message passed back from the
3556 warn_unexpected (connection, message, "not this error");
3563 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3571 dbus_message_unref (message);
3576 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3578 /* returns TRUE if the correct thing happens,
3579 * but the correct thing may include OOM errors.
3582 check_launch_service_service_missing (BusContext *context,
3583 DBusConnection *connection)
3585 DBusMessage *message;
3586 dbus_uint32_t serial;
3589 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3590 "/org/freedesktop/TestSuite",
3591 "org.freedesktop.TestSuite",
3594 if (message == NULL)
3597 if (!dbus_connection_send (connection, message, &serial))
3599 dbus_message_unref (message);
3603 dbus_message_unref (message);
3606 bus_test_run_everything (context);
3607 block_connection_until_message_from_bus (context, connection,
3608 "reply to service which should fail to auto-start (missing Service)");
3609 bus_test_run_everything (context);
3611 if (!dbus_connection_get_is_connected (connection))
3613 _dbus_warn ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3619 message = pop_message_waiting_for_memory (connection);
3620 if (message == NULL)
3622 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3623 "Echo message (auto activation)", serial, connection);
3627 verbose_message_received (connection, message);
3629 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3631 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3633 _dbus_warn ("Message has wrong sender %s\n",
3634 dbus_message_get_sender (message) ?
3635 dbus_message_get_sender (message) : "(none)");
3639 if (dbus_message_is_error (message,
3640 DBUS_ERROR_NO_MEMORY))
3642 ; /* good, this is a valid response */
3644 else if (dbus_message_is_error (message,
3645 DBUS_ERROR_SERVICE_UNKNOWN))
3647 _dbus_verbose("could not activate as invalid service file was not added\n");
3648 ; /* good, this is expected as we shouldn't have been added to
3649 * the activation list with a missing Exec key */
3651 else if (dbus_message_is_error (message,
3652 DBUS_ERROR_SPAWN_FILE_INVALID))
3654 _dbus_verbose("got service file invalid\n");
3655 ; /* good, this is allowed, and is the message passed back from the
3660 warn_unexpected (connection, message, "not this error");
3667 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3675 dbus_message_unref (message);
3680 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3682 /* returns TRUE if the correct thing happens,
3683 * but the correct thing may include OOM errors.
3686 check_shell_fail_service_auto_start (BusContext *context,
3687 DBusConnection *connection)
3689 DBusMessage *message;
3690 dbus_uint32_t serial;
3693 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3694 "/org/freedesktop/TestSuite",
3695 "org.freedesktop.TestSuite",
3698 if (message == NULL)
3701 if (!dbus_connection_send (connection, message, &serial))
3703 dbus_message_unref (message);
3707 dbus_message_unref (message);
3710 bus_test_run_everything (context);
3711 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3712 bus_test_run_everything (context);
3714 if (!dbus_connection_get_is_connected (connection))
3716 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3722 message = pop_message_waiting_for_memory (connection);
3723 if (message == NULL)
3725 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3726 "Echo message (auto activation)", serial, connection);
3730 verbose_message_received (connection, message);
3732 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3734 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3736 _dbus_warn ("Message has wrong sender %s\n",
3737 dbus_message_get_sender (message) ?
3738 dbus_message_get_sender (message) : "(none)");
3742 if (dbus_message_is_error (message,
3743 DBUS_ERROR_NO_MEMORY))
3745 ; /* good, this is a valid response */
3747 else if (dbus_message_is_error (message,
3748 DBUS_ERROR_INVALID_ARGS))
3750 _dbus_verbose("got invalid args\n");
3751 ; /* good, this is expected also */
3755 warn_unexpected (connection, message, "not this error");
3762 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3770 dbus_message_unref (message);
3775 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3777 /* returns TRUE if the correct thing happens,
3778 * but the correct thing may include OOM errors.
3781 check_shell_service_success_auto_start (BusContext *context,
3782 DBusConnection *connection)
3784 DBusMessage *message;
3785 DBusMessage *base_service_message;
3786 dbus_uint32_t serial;
3788 const char *base_service;
3789 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3791 base_service_message = NULL;
3793 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3794 "/org/freedesktop/TestSuite",
3795 "org.freedesktop.TestSuite",
3798 if (message == NULL)
3801 if (!dbus_connection_send (connection, message, &serial))
3803 dbus_message_unref (message);
3807 dbus_message_unref (message);
3810 bus_test_run_everything (context);
3812 /* now wait for the message bus to hear back from the activated
3815 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3816 bus_test_run_everything (context);
3818 if (!dbus_connection_get_is_connected (connection))
3820 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3826 message = pop_message_waiting_for_memory (connection);
3827 if (message == NULL)
3829 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3830 serial, connection);
3834 verbose_message_received (connection, message);
3835 _dbus_verbose (" (after sending %s)\n", "auto start");
3837 /* we should get zero or two ServiceOwnerChanged signals */
3838 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3840 GotServiceInfo message_kind;
3842 if (!check_base_service_activated (context, connection,
3843 message, &base_service))
3846 base_service_message = message;
3849 /* We may need to block here for the test service to exit or finish up */
3850 block_connection_until_message_from_bus (context, connection, "service to exit");
3852 /* Should get a service creation notification for the activated
3853 * service name, or a service deletion on the base service name
3855 message = dbus_connection_borrow_message (connection);
3856 if (message == NULL)
3858 _dbus_warn ("No message after auto activation "
3859 "(should be a service announcement)\n");
3860 dbus_connection_return_message (connection, message);
3865 message_kind = check_got_service_info (message);
3867 dbus_connection_return_message (connection, message);
3870 switch (message_kind)
3872 case GOT_SERVICE_CREATED:
3873 message = pop_message_waiting_for_memory (connection);
3874 if (message == NULL)
3876 _dbus_warn ("Failed to pop message we just put back! "
3877 "should have been a NameOwnerChanged (creation)\n");
3881 /* Check that ServiceOwnerChanged (creation) was correctly received */
3882 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3883 base_service, message))
3886 dbus_message_unref (message);
3891 case GOT_SERVICE_DELETED:
3893 /* The service started up and got a base address, but then
3894 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3896 CheckServiceOwnerChangedData socd;
3898 socd.expected_kind = SERVICE_DELETED;
3899 socd.expected_service_name = base_service;
3900 socd.failed = FALSE;
3901 socd.skip_connection = NULL;
3902 bus_test_clients_foreach (check_service_owner_changed_foreach,
3912 case GOT_SOMETHING_ELSE:
3913 _dbus_warn ("Unexpected message after auto activation\n");
3918 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3919 * come the method reply (or error) from the initial method call
3922 /* Note: if this test is run in OOM mode, it will block when the bus
3923 * doesn't send a reply due to OOM.
3925 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3927 message = pop_message_waiting_for_memory (connection);
3928 if (message == NULL)
3930 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3934 if (dbus_message_get_reply_serial (message) != serial)
3936 _dbus_warn ("Wrong reply serial\n");
3940 if (!dbus_message_get_args (message, NULL,
3941 DBUS_TYPE_STRING, &argv[0],
3942 DBUS_TYPE_STRING, &argv[1],
3943 DBUS_TYPE_STRING, &argv[2],
3944 DBUS_TYPE_STRING, &argv[3],
3945 DBUS_TYPE_STRING, &argv[4],
3946 DBUS_TYPE_STRING, &argv[5],
3947 DBUS_TYPE_STRING, &argv[6],
3950 _dbus_warn ("Error getting arguments from return\n");
3954 /* don't worry about arg[0] as it may be different
3955 depending on the path to the tests
3957 if (strcmp("-test", argv[1]) != 0)
3959 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3964 if (strcmp("that", argv[2]) != 0)
3966 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3971 if (strcmp("we get", argv[3]) != 0)
3973 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3978 if (strcmp("back", argv[4]) != 0)
3980 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3985 if (strcmp("--what", argv[5]) != 0)
3987 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
3992 if (strcmp("we put in", argv[6]) != 0)
3994 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
3995 "we put in", argv[6]);
3999 dbus_message_unref (message);
4002 if (!check_send_exit_to_service (context, connection,
4003 SHELL_SUCCESS_SERVICE_NAME,
4011 dbus_message_unref (message);
4013 if (base_service_message)
4014 dbus_message_unref (base_service_message);
4022 BusContext *context;
4026 check_oom_check1_func (void *data)
4028 Check1Data *d = data;
4030 if (! (* d->func) (d->context))
4033 if (!check_no_leftovers (d->context))
4035 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4043 check1_try_iterations (BusContext *context,
4044 const char *description,
4050 d.context = context;
4052 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4054 _dbus_assert_not_reached ("test failed");
4058 check_get_services (BusContext *context,
4059 DBusConnection *connection,
4064 DBusMessage *message;
4065 dbus_uint32_t serial;
4072 dbus_error_init (&error);
4075 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4077 DBUS_INTERFACE_DBUS,
4080 if (message == NULL)
4083 if (!dbus_connection_send (connection, message, &serial))
4085 dbus_message_unref (message);
4089 /* send our message */
4090 bus_test_run_clients_loop (SEND_PENDING (connection));
4092 dbus_message_unref (message);
4095 dbus_connection_ref (connection); /* because we may get disconnected */
4096 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4098 if (!dbus_connection_get_is_connected (connection))
4100 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4102 dbus_connection_unref (connection);
4107 dbus_connection_unref (connection);
4109 message = pop_message_waiting_for_memory (connection);
4110 if (message == NULL)
4112 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4113 method, serial, connection);
4117 verbose_message_received (connection, message);
4119 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4121 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4123 ; /* good, this is a valid response */
4127 warn_unexpected (connection, message, "not this error");
4134 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4136 ; /* good, expected */
4140 warn_unexpected (connection, message,
4141 "method_return for ListActivatableNames/ListNames");
4148 if (!dbus_message_get_args (message, &error,
4154 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4156 _dbus_verbose ("no memory to list services by %s\n", method);
4157 dbus_error_free (&error);
4158 _dbus_wait_for_memory ();
4159 goto retry_get_property;
4163 _dbus_assert (dbus_error_is_set (&error));
4164 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4173 if (!check_no_leftovers (context))
4179 dbus_error_free (&error);
4182 dbus_message_unref (message);
4187 /* returns TRUE if the correct thing happens,
4188 * but the correct thing may include OOM errors.
4191 check_list_services (BusContext *context,
4192 DBusConnection *connection)
4194 DBusMessage *message;
4195 DBusMessage *base_service_message;
4196 const char *base_service;
4197 dbus_uint32_t serial;
4199 const char *existent = EXISTENT_SERVICE_NAME;
4200 dbus_uint32_t flags;
4204 _dbus_verbose ("check_list_services for %p\n", connection);
4206 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4211 if (!_dbus_string_array_contains ((const char **)services, existent))
4213 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4214 dbus_free_string_array (services);
4218 dbus_free_string_array (services);
4220 base_service_message = NULL;
4222 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4224 DBUS_INTERFACE_DBUS,
4225 "StartServiceByName");
4227 if (message == NULL)
4230 dbus_message_set_auto_start (message, FALSE);
4233 if (!dbus_message_append_args (message,
4234 DBUS_TYPE_STRING, &existent,
4235 DBUS_TYPE_UINT32, &flags,
4238 dbus_message_unref (message);
4242 if (!dbus_connection_send (connection, message, &serial))
4244 dbus_message_unref (message);
4248 dbus_message_unref (message);
4251 bus_test_run_everything (context);
4253 /* now wait for the message bus to hear back from the activated
4256 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4258 bus_test_run_everything (context);
4260 if (!dbus_connection_get_is_connected (connection))
4262 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
4268 message = pop_message_waiting_for_memory (connection);
4269 if (message == NULL)
4271 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4272 "StartServiceByName", serial, connection);
4276 verbose_message_received (connection, message);
4277 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4279 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4281 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4283 _dbus_warn ("Message has wrong sender %s\n",
4284 dbus_message_get_sender (message) ?
4285 dbus_message_get_sender (message) : "(none)");
4289 if (dbus_message_is_error (message,
4290 DBUS_ERROR_NO_MEMORY))
4292 ; /* good, this is a valid response */
4294 else if (dbus_message_is_error (message,
4295 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4296 dbus_message_is_error (message,
4297 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4298 dbus_message_is_error (message,
4299 DBUS_ERROR_SPAWN_EXEC_FAILED))
4301 ; /* good, this is expected also */
4305 _dbus_warn ("Did not expect error %s\n",
4306 dbus_message_get_error_name (message));
4312 GotServiceInfo message_kind;
4314 if (!check_base_service_activated (context, connection,
4315 message, &base_service))
4318 base_service_message = message;
4321 /* We may need to block here for the test service to exit or finish up */
4322 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4324 message = dbus_connection_borrow_message (connection);
4325 if (message == NULL)
4327 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4331 message_kind = check_got_service_info (message);
4333 dbus_connection_return_message (connection, message);
4336 switch (message_kind)
4338 case GOT_SOMETHING_ELSE:
4340 case GOT_SERVICE_DELETED:
4341 _dbus_warn ("Unexpected message after ActivateService "
4342 "(should be an error or a service announcement)\n");
4345 case GOT_SERVICE_CREATED:
4346 message = pop_message_waiting_for_memory (connection);
4347 if (message == NULL)
4349 _dbus_warn ("Failed to pop message we just put back! "
4350 "should have been a NameOwnerChanged (creation)\n");
4354 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4355 base_service, message))
4358 dbus_message_unref (message);
4361 if (!check_no_leftovers (context))
4363 _dbus_warn ("Messages were left over after successful activation\n");
4371 if (!check_get_services (context, connection, "ListNames", &services, &len))
4376 if (!_dbus_string_array_contains ((const char **)services, existent))
4378 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4382 dbus_free_string_array (services);
4384 if (!check_send_exit_to_service (context, connection,
4385 EXISTENT_SERVICE_NAME, base_service))
4392 dbus_message_unref (message);
4394 if (base_service_message)
4395 dbus_message_unref (base_service_message);
4403 BusContext *context;
4404 DBusConnection *connection;
4408 check_oom_check2_func (void *data)
4410 Check2Data *d = data;
4412 if (! (* d->func) (d->context, d->connection))
4415 if (!check_no_leftovers (d->context))
4417 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4425 check2_try_iterations (BusContext *context,
4426 DBusConnection *connection,
4427 const char *description,
4433 d.context = context;
4434 d.connection = connection;
4436 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4439 _dbus_warn ("%s failed during oom\n", description);
4440 _dbus_assert_not_reached ("test failed");
4445 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4446 const char *filename)
4451 if (!_dbus_string_init (&full))
4454 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4456 _dbus_string_free (&full);
4460 _dbus_string_init_const (&file, filename);
4462 if (!_dbus_concat_dir_and_file (&full, &file))
4464 _dbus_string_free (&full);
4468 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4469 _dbus_string_get_const_data (&full));
4471 _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4473 _dbus_string_free (&full);
4479 bus_dispatch_test_conf (const DBusString *test_data_dir,
4480 const char *filename,
4481 dbus_bool_t use_launcher)
4483 BusContext *context;
4484 DBusConnection *foo;
4485 DBusConnection *bar;
4486 DBusConnection *baz;
4489 /* save the config name for the activation helper */
4490 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4491 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4493 dbus_error_init (&error);
4495 context = bus_context_new_test (test_data_dir, filename);
4496 if (context == NULL)
4499 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4501 _dbus_assert_not_reached ("could not alloc connection");
4503 if (!bus_setup_debug_client (foo))
4504 _dbus_assert_not_reached ("could not set up connection");
4506 spin_connection_until_authenticated (context, foo);
4508 if (!check_hello_message (context, foo))
4509 _dbus_assert_not_reached ("hello message failed");
4511 if (!check_double_hello_message (context, foo))
4512 _dbus_assert_not_reached ("double hello message failed");
4514 if (!check_add_match_all (context, foo))
4515 _dbus_assert_not_reached ("AddMatch message failed");
4517 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4519 _dbus_assert_not_reached ("could not alloc connection");
4521 if (!bus_setup_debug_client (bar))
4522 _dbus_assert_not_reached ("could not set up connection");
4524 spin_connection_until_authenticated (context, bar);
4526 if (!check_hello_message (context, bar))
4527 _dbus_assert_not_reached ("hello message failed");
4529 if (!check_add_match_all (context, bar))
4530 _dbus_assert_not_reached ("AddMatch message failed");
4532 baz = dbus_connection_open_private (TEST_CONNECTION, &error);
4534 _dbus_assert_not_reached ("could not alloc connection");
4536 if (!bus_setup_debug_client (baz))
4537 _dbus_assert_not_reached ("could not set up connection");
4539 spin_connection_until_authenticated (context, baz);
4541 if (!check_hello_message (context, baz))
4542 _dbus_assert_not_reached ("hello message failed");
4544 if (!check_add_match_all (context, baz))
4545 _dbus_assert_not_reached ("AddMatch message failed");
4547 #ifdef DBUS_WIN_FIXME
4548 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4549 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4551 if (!check_get_connection_unix_user (context, baz))
4552 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4554 if (!check_get_connection_unix_process_id (context, baz))
4555 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4558 if (!check_list_services (context, baz))
4559 _dbus_assert_not_reached ("ListActivatableNames message failed");
4561 if (!check_no_leftovers (context))
4563 _dbus_warn ("Messages were left over after setting up initial connections\n");
4564 _dbus_assert_not_reached ("initial connection setup failed");
4567 check1_try_iterations (context, "create_and_hello",
4568 check_hello_connection);
4570 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4571 check_nonexistent_service_no_auto_start);
4573 #ifdef DBUS_WIN_FIXME
4574 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4576 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4577 check_segfault_service_no_auto_start);
4580 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4581 check_existent_service_no_auto_start);
4583 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4584 check_nonexistent_service_auto_start);
4587 #ifdef DBUS_WIN_FIXME
4588 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4590 /* only do the segfault test if we are not using the launcher */
4591 check2_try_iterations (context, foo, "segfault_service_auto_start",
4592 check_segfault_service_auto_start);
4595 /* only do the shell fail test if we are not using the launcher */
4596 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4597 check_shell_fail_service_auto_start);
4599 /* specific to launcher */
4601 if (!check_launch_service_file_missing (context, foo))
4602 _dbus_assert_not_reached ("did not get service file not found error");
4605 /* Note: need to resolve some issues with the testing code in order to run
4606 * this in oom (handle that we sometimes don't get replies back from the bus
4607 * when oom happens, without blocking the test).
4609 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4610 check_existent_service_auto_start);
4613 if (!check_existent_service_auto_start (context, foo))
4614 _dbus_assert_not_reached ("existent service auto start failed");
4616 if (!check_shell_service_success_auto_start (context, foo))
4617 _dbus_assert_not_reached ("shell success service auto start failed");
4619 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4621 kill_client_connection_unchecked (foo);
4622 kill_client_connection_unchecked (bar);
4623 kill_client_connection_unchecked (baz);
4625 bus_context_unref (context);
4631 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4632 const char *filename)
4634 BusContext *context;
4635 DBusConnection *foo;
4638 /* save the config name for the activation helper */
4639 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4640 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4642 dbus_error_init (&error);
4644 context = bus_context_new_test (test_data_dir, filename);
4645 if (context == NULL)
4648 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4650 _dbus_assert_not_reached ("could not alloc connection");
4652 if (!bus_setup_debug_client (foo))
4653 _dbus_assert_not_reached ("could not set up connection");
4655 spin_connection_until_authenticated (context, foo);
4657 if (!check_hello_message (context, foo))
4658 _dbus_assert_not_reached ("hello message failed");
4660 if (!check_double_hello_message (context, foo))
4661 _dbus_assert_not_reached ("double hello message failed");
4663 if (!check_add_match_all (context, foo))
4664 _dbus_assert_not_reached ("AddMatch message failed");
4666 /* this only tests the activation.c user check */
4667 if (!check_launch_service_user_missing (context, foo))
4668 _dbus_assert_not_reached ("user missing did not trigger error");
4670 /* this only tests the desktop.c exec check */
4671 if (!check_launch_service_exec_missing (context, foo))
4672 _dbus_assert_not_reached ("exec missing did not trigger error");
4674 /* this only tests the desktop.c service check */
4675 if (!check_launch_service_service_missing (context, foo))
4676 _dbus_assert_not_reached ("service missing did not trigger error");
4678 _dbus_verbose ("Disconnecting foo\n");
4680 kill_client_connection_unchecked (foo);
4682 bus_context_unref (context);
4688 bus_dispatch_test (const DBusString *test_data_dir)
4690 /* run normal activation tests */
4691 _dbus_verbose ("Normal activation tests\n");
4692 if (!bus_dispatch_test_conf (test_data_dir,
4693 "valid-config-files/debug-allow-all.conf", FALSE))
4696 /* run launch-helper activation tests */
4697 _dbus_verbose ("Launch helper activation tests\n");
4698 if (!bus_dispatch_test_conf (test_data_dir,
4699 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4702 /* run select launch-helper activation tests on broken service files */
4703 if (!bus_dispatch_test_conf_fail (test_data_dir,
4704 "valid-config-files-system/debug-allow-all-fail.conf"))
4711 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4713 BusContext *context;
4714 DBusConnection *foo;
4717 dbus_error_init (&error);
4719 /* Test SHA1 authentication */
4720 _dbus_verbose ("Testing SHA1 context\n");
4722 context = bus_context_new_test (test_data_dir,
4723 "valid-config-files/debug-allow-all-sha1.conf");
4724 if (context == NULL)
4727 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4729 _dbus_assert_not_reached ("could not alloc connection");
4731 if (!bus_setup_debug_client (foo))
4732 _dbus_assert_not_reached ("could not set up connection");
4734 spin_connection_until_authenticated (context, foo);
4736 if (!check_hello_message (context, foo))
4737 _dbus_assert_not_reached ("hello message failed");
4739 if (!check_add_match_all (context, foo))
4740 _dbus_assert_not_reached ("addmatch message failed");
4742 if (!check_no_leftovers (context))
4744 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4745 _dbus_assert_not_reached ("initial connection setup failed");
4748 check1_try_iterations (context, "create_and_hello_sha1",
4749 check_hello_connection);
4751 kill_client_connection_unchecked (foo);
4753 bus_context_unref (context);
4758 #ifdef HAVE_UNIX_FD_PASSING
4761 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4763 BusContext *context;
4764 DBusConnection *foo, *bar;
4768 int one[2], two[2], x, y, z;
4771 dbus_error_init (&error);
4773 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4774 if (context == NULL)
4775 _dbus_assert_not_reached ("could not alloc context");
4777 foo = dbus_connection_open_private (TEST_CONNECTION, &error);
4779 _dbus_assert_not_reached ("could not alloc connection");
4781 if (!bus_setup_debug_client (foo))
4782 _dbus_assert_not_reached ("could not set up connection");
4784 spin_connection_until_authenticated (context, foo);
4786 if (!check_hello_message (context, foo))
4787 _dbus_assert_not_reached ("hello message failed");
4789 if (!check_add_match_all (context, foo))
4790 _dbus_assert_not_reached ("AddMatch message failed");
4792 bar = dbus_connection_open_private (TEST_CONNECTION, &error);
4794 _dbus_assert_not_reached ("could not alloc connection");
4796 if (!bus_setup_debug_client (bar))
4797 _dbus_assert_not_reached ("could not set up connection");
4799 spin_connection_until_authenticated (context, bar);
4801 if (!check_hello_message (context, bar))
4802 _dbus_assert_not_reached ("hello message failed");
4804 if (!check_add_match_all (context, bar))
4805 _dbus_assert_not_reached ("AddMatch message failed");
4807 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4808 _dbus_assert_not_reached ("could not alloc message");
4810 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4811 _dbus_assert_not_reached("Failed to allocate pipe #1");
4813 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4814 _dbus_assert_not_reached("Failed to allocate pipe #2");
4816 if (!dbus_message_append_args(m,
4817 DBUS_TYPE_UNIX_FD, one,
4818 DBUS_TYPE_UNIX_FD, two,
4819 DBUS_TYPE_UNIX_FD, two,
4821 _dbus_assert_not_reached("Failed to attach fds.");
4823 if (!_dbus_close(one[0], &error))
4824 _dbus_assert_not_reached("Failed to close pipe #1 ");
4825 if (!_dbus_close(two[0], &error))
4826 _dbus_assert_not_reached("Failed to close pipe #2 ");
4828 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4829 _dbus_assert_not_reached("Connection cannot do fd passing");
4831 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4832 _dbus_assert_not_reached("Connection cannot do fd passing");
4834 if (!dbus_connection_send (foo, m, NULL))
4835 _dbus_assert_not_reached("Failed to send fds");
4837 dbus_message_unref(m);
4839 bus_test_run_clients_loop (SEND_PENDING (foo));
4841 bus_test_run_everything (context);
4843 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4845 if (!(m = pop_message_waiting_for_memory (foo)))
4846 _dbus_assert_not_reached("Failed to receive msg");
4848 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4849 _dbus_assert_not_reached("bogus message received");
4851 dbus_message_unref(m);
4853 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4855 if (!(m = pop_message_waiting_for_memory (bar)))
4856 _dbus_assert_not_reached("Failed to receive msg");
4858 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4859 _dbus_assert_not_reached("bogus message received");
4861 if (!dbus_message_get_args(m,
4863 DBUS_TYPE_UNIX_FD, &x,
4864 DBUS_TYPE_UNIX_FD, &y,
4865 DBUS_TYPE_UNIX_FD, &z,
4867 _dbus_assert_not_reached("Failed to parse fds.");
4869 dbus_message_unref(m);
4871 if (write(x, "X", 1) != 1)
4872 _dbus_assert_not_reached("Failed to write to pipe #1");
4873 if (write(y, "Y", 1) != 1)
4874 _dbus_assert_not_reached("Failed to write to pipe #2");
4875 if (write(z, "Z", 1) != 1)
4876 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4878 if (!_dbus_close(x, &error))
4879 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4880 if (!_dbus_close(y, &error))
4881 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4882 if (!_dbus_close(z, &error))
4883 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4885 if (read(one[1], &r, 1) != 1 || r != 'X')
4886 _dbus_assert_not_reached("Failed to read value from pipe.");
4887 if (read(two[1], &r, 1) != 1 || r != 'Y')
4888 _dbus_assert_not_reached("Failed to read value from pipe.");
4889 if (read(two[1], &r, 1) != 1 || r != 'Z')
4890 _dbus_assert_not_reached("Failed to read value from pipe.");
4892 if (!_dbus_close(one[1], &error))
4893 _dbus_assert_not_reached("Failed to close pipe #1 ");
4894 if (!_dbus_close(two[1], &error))
4895 _dbus_assert_not_reached("Failed to close pipe #2 ");
4897 _dbus_verbose ("Disconnecting foo\n");
4898 kill_client_connection_unchecked (foo);
4900 _dbus_verbose ("Disconnecting bar\n");
4901 kill_client_connection_unchecked (bar);
4903 bus_context_unref (context);
4909 #endif /* DBUS_BUILD_TESTS */