1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
7 * Copyright (C) 2013 Samsung Electronics
9 * Licensed under the Academic Free License version 2.1
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connection.h"
32 #include "activation.h"
37 #include <dbus/dbus-internals.h>
38 #include <dbus/dbus-misc.h>
42 #ifdef HAVE_UNIX_FD_PASSING
43 #include <dbus/dbus-sysdeps-unix.h>
47 /* This is hard-coded in the files in valid-config-files-*. We have to use
48 * the debug-pipe transport because the tests in this file require that
49 * dbus_connection_open_private() does not block. */
50 #define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
53 send_one_message (DBusConnection *connection,
55 DBusConnection *sender,
56 DBusConnection *addressed_recipient,
58 BusTransaction *transaction,
61 if (!bus_context_check_security_policy (context, transaction,
67 return TRUE; /* silently don't send it */
69 if (dbus_message_contains_unix_fds(message) &&
70 !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
71 return TRUE; /* silently don't send it */
73 if (!bus_transaction_send (transaction,
85 bus_dispatch_matches (BusTransaction *transaction,
86 DBusConnection *sender,
87 DBusConnection *addressed_recipient,
92 BusConnections *connections;
94 BusMatchmaker *matchmaker;
98 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
100 /* sender and recipient can both be NULL for the bus driver,
101 * or for signals with no particular recipient
104 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
105 _dbus_assert (dbus_message_get_sender (message) != NULL);
107 context = bus_transaction_get_context (transaction);
109 /* First, send the message to the addressed_recipient, if there is one. */
110 if (addressed_recipient != NULL)
112 if (!bus_context_check_security_policy (context, transaction,
113 sender, addressed_recipient,
118 if (dbus_message_contains_unix_fds (message) &&
119 !dbus_connection_can_send_type (addressed_recipient,
122 dbus_set_error (error,
123 DBUS_ERROR_NOT_SUPPORTED,
124 "Tried to send message with Unix file descriptors"
125 "to a client that doesn't support that.");
129 /* Dispatch the message */
130 if (!bus_transaction_send (transaction, addressed_recipient, message))
137 /* Now dispatch to others who look interested in this message */
138 connections = bus_transaction_get_connections (transaction);
139 dbus_error_init (&tmp_error);
140 matchmaker = bus_context_get_matchmaker (context);
143 if (!bus_matchmaker_get_recipients (matchmaker, connections,
144 sender, addressed_recipient, message,
151 link = _dbus_list_get_first_link (&recipients);
154 DBusConnection *dest;
158 if (!send_one_message (dest, context, sender, addressed_recipient,
159 message, transaction, &tmp_error))
162 link = _dbus_list_get_next_link (&recipients, link);
165 _dbus_list_clear (&recipients);
167 if (dbus_error_is_set (&tmp_error))
169 dbus_move_error (&tmp_error, error);
176 static DBusHandlerResult
177 bus_dispatch (DBusConnection *connection,
178 DBusMessage *message)
180 const char *sender, *service_name;
182 BusTransaction *transaction;
184 DBusHandlerResult result;
185 DBusConnection *addressed_recipient;
187 result = DBUS_HANDLER_RESULT_HANDLED;
190 addressed_recipient = NULL;
191 dbus_error_init (&error);
193 context = bus_connection_get_context (connection);
194 _dbus_assert (context != NULL);
196 /* If we can't even allocate an OOM error, we just go to sleep
199 while (!bus_connection_preallocate_oom_error (connection))
200 _dbus_wait_for_memory ();
202 /* Ref connection in case we disconnect it at some point in here */
203 dbus_connection_ref (connection);
205 service_name = dbus_message_get_destination (message);
207 #ifdef DBUS_ENABLE_VERBOSE_MODE
209 const char *interface_name, *member_name, *error_name;
211 interface_name = dbus_message_get_interface (message);
212 member_name = dbus_message_get_member (message);
213 error_name = dbus_message_get_error_name (message);
215 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
216 interface_name ? interface_name : "(no interface)",
217 member_name ? member_name : "(no member)",
218 error_name ? error_name : "(no error name)",
219 service_name ? service_name : "peer");
221 #endif /* DBUS_ENABLE_VERBOSE_MODE */
223 /* Create our transaction */
224 transaction = bus_transaction_new (context);
225 if (transaction == NULL)
227 BUS_SET_OOM (&error);
231 /* If service_name is NULL, if it's a signal we send it to all
232 * connections with a match rule. If it's not a signal, there
233 * are some special cases here but mostly we just bail out.
235 if (service_name == NULL)
237 if (dbus_message_is_signal (message,
238 DBUS_INTERFACE_LOCAL,
241 bus_connection_disconnected (connection);
245 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
247 /* DBusConnection also handles some of these automatically, we leave
250 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254 if(bus_context_is_kdbus(context))
256 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
258 handleNameOwnerChanged(message, transaction, connection);
264 /* Assign a sender to the message */
265 if(bus_context_is_kdbus(context) == FALSE) //if using kdbus, sender must be set on library side
266 if (bus_connection_is_active (connection))
268 sender = bus_connection_get_name (connection);
269 _dbus_assert (sender != NULL);
271 if (!dbus_message_set_sender (message, sender))
273 BUS_SET_OOM (&error);
277 /* We need to refetch the service name here, because
278 * dbus_message_set_sender can cause the header to be
279 * reallocated, and thus the service_name pointer will become
282 service_name = dbus_message_get_destination (message);
286 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
288 if (!bus_context_check_security_policy (context, transaction,
289 connection, NULL, NULL, message, &error))
291 _dbus_verbose ("Security policy rejected message\n");
295 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
296 if (!bus_driver_handle_message (connection, transaction, message, &error))
299 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
301 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
302 dbus_connection_close (connection);
305 else if (service_name != NULL) /* route to named service */
307 DBusString service_string;
309 BusRegistry *registry;
311 _dbus_assert (service_name != NULL);
313 registry = bus_connection_get_registry (connection);
315 _dbus_string_init_const (&service_string, service_name);
316 service = bus_registry_lookup (registry, &service_string);
318 if (dbus_message_get_auto_start (message) && (service == NULL || bus_service_get_is_kdbus_starter(service)))
320 BusActivation *activation;
321 /* We can't do the security policy check here, since the addressed
322 * recipient service doesn't exist yet. We do it before sending the
323 * message after the service has been created.
325 activation = bus_connection_get_activation (connection);
327 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
328 message, service_name, &error))
330 _DBUS_ASSERT_ERROR_IS_SET (&error);
331 _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
337 else if (service == NULL)
339 dbus_set_error (&error,
340 DBUS_ERROR_NAME_HAS_NO_OWNER,
341 "Name \"%s\" does not exist",
347 addressed_recipient = bus_service_get_primary_owners_connection (service);
348 _dbus_assert (addressed_recipient != NULL);
352 /* Now send the message to its destination (or not, if
353 * addressed_recipient == NULL), and match it against other connections'
356 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
360 if (dbus_error_is_set (&error))
362 if (!dbus_connection_get_is_connected (connection))
364 /* If we disconnected it, we won't bother to send it any error
367 _dbus_verbose ("Not sending error to connection we disconnected\n");
369 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
371 bus_connection_send_oom_error (connection, message);
373 /* cancel transaction due to OOM */
374 if (transaction != NULL)
376 bus_transaction_cancel_and_free (transaction);
382 /* Try to send the real error, if no mem to do that, send
385 _dbus_assert (transaction != NULL);
386 if (!bus_transaction_send_error_reply (transaction, connection,
389 bus_connection_send_oom_error (connection, message);
391 /* cancel transaction due to OOM */
392 if (transaction != NULL)
394 bus_transaction_cancel_and_free (transaction);
401 dbus_error_free (&error);
404 if (transaction != NULL)
406 bus_transaction_execute_and_free (transaction);
409 dbus_connection_unref (connection);
414 static DBusHandlerResult
415 bus_dispatch_message_filter (DBusConnection *connection,
416 DBusMessage *message,
419 return bus_dispatch (connection, message);
423 bus_dispatch_add_connection (DBusConnection *connection)
425 if (!dbus_connection_add_filter (connection,
426 bus_dispatch_message_filter,
434 bus_dispatch_remove_connection (DBusConnection *connection)
436 /* Here we tell the bus driver that we want to get off. */
437 bus_driver_remove_connection (connection);
439 dbus_connection_remove_filter (connection,
440 bus_dispatch_message_filter,
444 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
448 /* This is used to know whether we need to block in order to finish
449 * sending a message, or whether the initial dbus_connection_send()
450 * already flushed the queue.
452 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
454 typedef dbus_bool_t (* Check1Func) (BusContext *context);
455 typedef dbus_bool_t (* Check2Func) (BusContext *context,
456 DBusConnection *connection);
458 static dbus_bool_t check_no_leftovers (BusContext *context);
461 block_connection_until_message_from_bus (BusContext *context,
462 DBusConnection *connection,
463 const char *what_is_expected)
465 _dbus_verbose ("expecting: %s\n", what_is_expected);
467 while (dbus_connection_get_dispatch_status (connection) ==
468 DBUS_DISPATCH_COMPLETE &&
469 dbus_connection_get_is_connected (connection))
471 bus_test_run_bus_loop (context, TRUE);
472 bus_test_run_clients_loop (FALSE);
477 spin_connection_until_authenticated (BusContext *context,
478 DBusConnection *connection)
480 _dbus_verbose ("Spinning to auth connection %p\n", connection);
481 while (!dbus_connection_get_is_authenticated (connection) &&
482 dbus_connection_get_is_connected (connection))
484 bus_test_run_bus_loop (context, FALSE);
485 bus_test_run_clients_loop (FALSE);
487 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
490 /* compensate for fact that pop_message() can return #NULL due to OOM */
492 pop_message_waiting_for_memory (DBusConnection *connection)
494 while (dbus_connection_get_dispatch_status (connection) ==
495 DBUS_DISPATCH_NEED_MEMORY)
496 _dbus_wait_for_memory ();
498 return dbus_connection_pop_message (connection);
502 borrow_message_waiting_for_memory (DBusConnection *connection)
504 while (dbus_connection_get_dispatch_status (connection) ==
505 DBUS_DISPATCH_NEED_MEMORY)
506 _dbus_wait_for_memory ();
508 return dbus_connection_borrow_message (connection);
512 warn_unexpected_real (DBusConnection *connection,
513 DBusMessage *message,
514 const char *expected,
515 const char *function,
519 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
521 dbus_message_get_interface (message) ?
522 dbus_message_get_interface (message) : "(unset)",
523 dbus_message_get_member (message) ?
524 dbus_message_get_member (message) : "(unset)",
525 dbus_message_get_error_name (message) ?
526 dbus_message_get_error_name (message) : "(unset)",
530 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
531 function, line, connection, expected);
534 #define warn_unexpected(connection, message, expected) \
535 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
538 verbose_message_received (DBusConnection *connection,
539 DBusMessage *message)
541 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
542 dbus_message_get_interface (message) ?
543 dbus_message_get_interface (message) : "(unset)",
544 dbus_message_get_member (message) ?
545 dbus_message_get_member (message) : "(unset)",
546 dbus_message_get_error_name (message) ?
547 dbus_message_get_error_name (message) : "(unset)",
560 ServiceInfoKind expected_kind;
561 const char *expected_service_name;
563 DBusConnection *skip_connection;
564 } CheckServiceOwnerChangedData;
567 check_service_owner_changed_foreach (DBusConnection *connection,
570 CheckServiceOwnerChangedData *d = data;
571 DBusMessage *message;
573 const char *service_name, *old_owner, *new_owner;
575 if (d->expected_kind == SERVICE_CREATED
576 && connection == d->skip_connection)
579 dbus_error_init (&error);
582 message = pop_message_waiting_for_memory (connection);
585 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
586 connection, "NameOwnerChanged");
589 else if (!dbus_message_is_signal (message,
593 warn_unexpected (connection, message, "NameOwnerChanged");
599 reget_service_info_data:
604 dbus_message_get_args (message, &error,
605 DBUS_TYPE_STRING, &service_name,
606 DBUS_TYPE_STRING, &old_owner,
607 DBUS_TYPE_STRING, &new_owner,
610 if (dbus_error_is_set (&error))
612 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
614 dbus_error_free (&error);
615 _dbus_wait_for_memory ();
616 goto reget_service_info_data;
620 _dbus_warn ("Did not get the expected arguments\n");
625 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
626 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
627 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
629 _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
633 if (strcmp (service_name, d->expected_service_name) != 0)
635 _dbus_warn ("expected info on service %s, got info on %s\n",
636 d->expected_service_name,
641 if (*service_name == ':' && new_owner[0]
642 && strcmp (service_name, new_owner) != 0)
644 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
645 service_name, old_owner, new_owner);
653 dbus_error_free (&error);
656 dbus_message_unref (message);
663 kill_client_connection (BusContext *context,
664 DBusConnection *connection)
668 CheckServiceOwnerChangedData socd;
670 _dbus_verbose ("killing connection %p\n", connection);
672 s = dbus_bus_get_unique_name (connection);
673 _dbus_assert (s != NULL);
675 while ((base_service = _dbus_strdup (s)) == NULL)
676 _dbus_wait_for_memory ();
678 dbus_connection_ref (connection);
680 /* kick in the disconnect handler that unrefs the connection */
681 dbus_connection_close (connection);
683 bus_test_run_everything (context);
685 _dbus_assert (bus_test_client_listed (connection));
687 /* Run disconnect handler in test.c */
688 if (bus_connection_dispatch_one_message (connection))
689 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
691 _dbus_assert (!dbus_connection_get_is_connected (connection));
692 dbus_connection_unref (connection);
694 _dbus_assert (!bus_test_client_listed (connection));
696 socd.expected_kind = SERVICE_DELETED;
697 socd.expected_service_name = base_service;
699 socd.skip_connection = NULL;
701 bus_test_clients_foreach (check_service_owner_changed_foreach,
704 dbus_free (base_service);
707 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
709 if (!check_no_leftovers (context))
710 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
714 kill_client_connection_unchecked (DBusConnection *connection)
716 /* This kills the connection without expecting it to affect
717 * the rest of the bus.
719 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
721 dbus_connection_ref (connection);
722 dbus_connection_close (connection);
723 /* dispatching disconnect handler will unref once */
724 if (bus_connection_dispatch_one_message (connection))
725 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
727 _dbus_assert (!bus_test_client_listed (connection));
728 dbus_connection_unref (connection);
734 } CheckNoMessagesData;
737 check_no_messages_foreach (DBusConnection *connection,
740 CheckNoMessagesData *d = data;
741 DBusMessage *message;
743 message = pop_message_waiting_for_memory (connection);
746 warn_unexpected (connection, message, "no messages");
752 dbus_message_unref (message);
757 check_no_leftovers (BusContext *context)
759 CheckNoMessagesData nmd;
762 bus_test_clients_foreach (check_no_messages_foreach,
767 _dbus_verbose ("leftover message found\n");
774 /* returns TRUE if the correct thing happens,
775 * but the correct thing may include OOM errors.
778 check_hello_message (BusContext *context,
779 DBusConnection *connection)
781 DBusMessage *message;
782 DBusMessage *name_message;
783 dbus_uint32_t serial;
787 const char *acquired;
790 dbus_error_init (&error);
796 _dbus_verbose ("check_hello_message for %p\n", connection);
798 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
806 dbus_connection_ref (connection); /* because we may get disconnected */
808 if (!dbus_connection_send (connection, message, &serial))
810 dbus_message_unref (message);
811 dbus_connection_unref (connection);
815 _dbus_assert (dbus_message_has_signature (message, ""));
817 dbus_message_unref (message);
820 if (!dbus_connection_get_is_connected (connection))
822 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
824 dbus_connection_unref (connection);
829 /* send our message */
830 bus_test_run_clients_loop (SEND_PENDING (connection));
832 if (!dbus_connection_get_is_connected (connection))
834 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
836 dbus_connection_unref (connection);
841 block_connection_until_message_from_bus (context, connection, "reply to Hello");
843 if (!dbus_connection_get_is_connected (connection))
845 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
847 dbus_connection_unref (connection);
852 dbus_connection_unref (connection);
854 message = pop_message_waiting_for_memory (connection);
857 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
858 "Hello", serial, connection);
862 verbose_message_received (connection, message);
864 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
866 _dbus_warn ("Message has wrong sender %s\n",
867 dbus_message_get_sender (message) ?
868 dbus_message_get_sender (message) : "(none)");
872 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
874 if (dbus_message_is_error (message,
875 DBUS_ERROR_NO_MEMORY))
877 ; /* good, this is a valid response */
881 warn_unexpected (connection, message, "not this error");
888 CheckServiceOwnerChangedData socd;
890 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
892 ; /* good, expected */
896 warn_unexpected (connection, message, "method return for Hello");
901 retry_get_hello_name:
902 if (!dbus_message_get_args (message, &error,
903 DBUS_TYPE_STRING, &name,
906 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
908 _dbus_verbose ("no memory to get service name arg from hello\n");
909 dbus_error_free (&error);
910 _dbus_wait_for_memory ();
911 goto retry_get_hello_name;
915 _dbus_assert (dbus_error_is_set (&error));
916 _dbus_warn ("Did not get the expected single string argument to hello\n");
921 _dbus_verbose ("Got hello name: %s\n", name);
923 while (!dbus_bus_set_unique_name (connection, name))
924 _dbus_wait_for_memory ();
926 socd.expected_kind = SERVICE_CREATED;
927 socd.expected_service_name = name;
929 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
930 bus_test_clients_foreach (check_service_owner_changed_foreach,
936 name_message = message;
937 /* Client should also have gotten ServiceAcquired */
939 message = pop_message_waiting_for_memory (connection);
942 _dbus_warn ("Expecting %s, got nothing\n",
946 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
949 _dbus_warn ("Expecting %s, got smthg else\n",
954 retry_get_acquired_name:
955 if (!dbus_message_get_args (message, &error,
956 DBUS_TYPE_STRING, &acquired,
959 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
961 _dbus_verbose ("no memory to get service name arg from acquired\n");
962 dbus_error_free (&error);
963 _dbus_wait_for_memory ();
964 goto retry_get_acquired_name;
968 _dbus_assert (dbus_error_is_set (&error));
969 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
974 _dbus_verbose ("Got acquired name: %s\n", acquired);
976 if (strcmp (acquired, name) != 0)
978 _dbus_warn ("Acquired name is %s but expected %s\n",
985 if (!check_no_leftovers (context))
991 _dbus_verbose ("ending - retval = %d\n", retval);
993 dbus_error_free (&error);
996 dbus_message_unref (message);
999 dbus_message_unref (name_message);
1004 /* returns TRUE if the correct thing happens,
1005 * but the correct thing may include OOM errors.
1008 check_double_hello_message (BusContext *context,
1009 DBusConnection *connection)
1011 DBusMessage *message;
1012 dbus_uint32_t serial;
1017 dbus_error_init (&error);
1020 _dbus_verbose ("check_double_hello_message for %p\n", connection);
1022 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1024 DBUS_INTERFACE_DBUS,
1027 if (message == NULL)
1030 if (!dbus_connection_send (connection, message, &serial))
1032 dbus_message_unref (message);
1036 dbus_message_unref (message);
1039 /* send our message */
1040 bus_test_run_clients_loop (SEND_PENDING (connection));
1042 dbus_connection_ref (connection); /* because we may get disconnected */
1043 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1045 if (!dbus_connection_get_is_connected (connection))
1047 _dbus_verbose ("connection was disconnected\n");
1049 dbus_connection_unref (connection);
1054 dbus_connection_unref (connection);
1056 message = pop_message_waiting_for_memory (connection);
1057 if (message == NULL)
1059 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1060 "Hello", serial, connection);
1064 verbose_message_received (connection, message);
1066 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1068 _dbus_warn ("Message has wrong sender %s\n",
1069 dbus_message_get_sender (message) ?
1070 dbus_message_get_sender (message) : "(none)");
1074 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1076 warn_unexpected (connection, message, "method return for Hello");
1080 if (!check_no_leftovers (context))
1086 dbus_error_free (&error);
1089 dbus_message_unref (message);
1094 /* returns TRUE if the correct thing happens,
1095 * but the correct thing may include OOM errors.
1098 check_get_connection_unix_user (BusContext *context,
1099 DBusConnection *connection)
1101 DBusMessage *message;
1102 dbus_uint32_t serial;
1105 const char *base_service_name;
1109 dbus_error_init (&error);
1112 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1114 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1116 DBUS_INTERFACE_DBUS,
1117 "GetConnectionUnixUser");
1119 if (message == NULL)
1122 base_service_name = dbus_bus_get_unique_name (connection);
1124 if (!dbus_message_append_args (message,
1125 DBUS_TYPE_STRING, &base_service_name,
1128 dbus_message_unref (message);
1132 if (!dbus_connection_send (connection, message, &serial))
1134 dbus_message_unref (message);
1138 /* send our message */
1139 bus_test_run_clients_loop (SEND_PENDING (connection));
1141 dbus_message_unref (message);
1144 dbus_connection_ref (connection); /* because we may get disconnected */
1145 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1147 if (!dbus_connection_get_is_connected (connection))
1149 _dbus_verbose ("connection was disconnected\n");
1151 dbus_connection_unref (connection);
1156 dbus_connection_unref (connection);
1158 message = pop_message_waiting_for_memory (connection);
1159 if (message == NULL)
1161 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1162 "GetConnectionUnixUser", serial, connection);
1166 verbose_message_received (connection, message);
1168 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1170 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1172 ; /* good, this is a valid response */
1176 warn_unexpected (connection, message, "not this error");
1183 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1185 ; /* good, expected */
1189 warn_unexpected (connection, message,
1190 "method_return for GetConnectionUnixUser");
1197 if (!dbus_message_get_args (message, &error,
1198 DBUS_TYPE_UINT32, &uid,
1201 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1203 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1204 dbus_error_free (&error);
1205 _dbus_wait_for_memory ();
1206 goto retry_get_property;
1210 _dbus_assert (dbus_error_is_set (&error));
1211 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1217 if (!check_no_leftovers (context))
1223 dbus_error_free (&error);
1226 dbus_message_unref (message);
1231 /* returns TRUE if the correct thing happens,
1232 * but the correct thing may include OOM errors.
1235 check_get_connection_unix_process_id (BusContext *context,
1236 DBusConnection *connection)
1238 DBusMessage *message;
1239 dbus_uint32_t serial;
1242 const char *base_service_name;
1246 dbus_error_init (&error);
1249 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1251 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1253 DBUS_INTERFACE_DBUS,
1254 "GetConnectionUnixProcessID");
1256 if (message == NULL)
1259 base_service_name = dbus_bus_get_unique_name (connection);
1261 if (!dbus_message_append_args (message,
1262 DBUS_TYPE_STRING, &base_service_name,
1265 dbus_message_unref (message);
1269 if (!dbus_connection_send (connection, message, &serial))
1271 dbus_message_unref (message);
1275 /* send our message */
1276 bus_test_run_clients_loop (SEND_PENDING (connection));
1278 dbus_message_unref (message);
1281 dbus_connection_ref (connection); /* because we may get disconnected */
1282 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1284 if (!dbus_connection_get_is_connected (connection))
1286 _dbus_verbose ("connection was disconnected\n");
1288 dbus_connection_unref (connection);
1293 dbus_connection_unref (connection);
1295 message = pop_message_waiting_for_memory (connection);
1296 if (message == NULL)
1298 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1299 "GetConnectionUnixProcessID", serial, connection);
1303 verbose_message_received (connection, message);
1305 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1307 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1309 ; /* good, this is a valid response */
1312 else if (dbus_message_is_error (message, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
1314 /* We are expecting this error, since we know in the test suite we aren't
1315 * talking to a client running on UNIX
1317 _dbus_verbose ("Windows correctly does not support GetConnectionUnixProcessID\n");
1322 warn_unexpected (connection, message, "not this error");
1330 warn_unexpected (connection, message, "GetConnectionUnixProcessID to fail on Windows");
1333 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1335 ; /* good, expected */
1339 warn_unexpected (connection, message,
1340 "method_return for GetConnectionUnixProcessID");
1347 if (!dbus_message_get_args (message, &error,
1348 DBUS_TYPE_UINT32, &pid,
1351 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1353 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1354 dbus_error_free (&error);
1355 _dbus_wait_for_memory ();
1356 goto retry_get_property;
1360 _dbus_assert (dbus_error_is_set (&error));
1361 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1367 /* test if returned pid is the same as our own pid
1369 * @todo It would probably be good to restructure the tests
1370 * in a way so our parent is the bus that we're testing
1371 * cause then we can test that the pid returned matches
1374 if (pid != (dbus_uint32_t) _dbus_getpid ())
1376 _dbus_assert (dbus_error_is_set (&error));
1377 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1381 #endif /* !DBUS_WIN */
1384 if (!check_no_leftovers (context))
1390 dbus_error_free (&error);
1393 dbus_message_unref (message);
1398 /* returns TRUE if the correct thing happens,
1399 * but the correct thing may include OOM errors.
1402 check_add_match_all (BusContext *context,
1403 DBusConnection *connection)
1405 DBusMessage *message;
1407 dbus_uint32_t serial;
1409 const char *empty = "";
1412 dbus_error_init (&error);
1415 _dbus_verbose ("check_add_match_all for %p\n", connection);
1417 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1419 DBUS_INTERFACE_DBUS,
1422 if (message == NULL)
1425 /* empty string match rule matches everything */
1426 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1429 dbus_message_unref (message);
1433 if (!dbus_connection_send (connection, message, &serial))
1435 dbus_message_unref (message);
1439 dbus_message_unref (message);
1442 dbus_connection_ref (connection); /* because we may get disconnected */
1444 /* send our message */
1445 bus_test_run_clients_loop (SEND_PENDING (connection));
1447 if (!dbus_connection_get_is_connected (connection))
1449 _dbus_verbose ("connection was disconnected\n");
1451 dbus_connection_unref (connection);
1456 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1458 if (!dbus_connection_get_is_connected (connection))
1460 _dbus_verbose ("connection was disconnected\n");
1462 dbus_connection_unref (connection);
1467 dbus_connection_unref (connection);
1469 message = pop_message_waiting_for_memory (connection);
1470 if (message == NULL)
1472 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1473 "AddMatch", serial, connection);
1477 verbose_message_received (connection, message);
1479 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1481 _dbus_warn ("Message has wrong sender %s\n",
1482 dbus_message_get_sender (message) ?
1483 dbus_message_get_sender (message) : "(none)");
1487 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1489 if (dbus_message_is_error (message,
1490 DBUS_ERROR_NO_MEMORY))
1492 ; /* good, this is a valid response */
1496 warn_unexpected (connection, message, "not this error");
1503 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1505 ; /* good, expected */
1506 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1510 warn_unexpected (connection, message, "method return for AddMatch");
1516 if (!check_no_leftovers (context))
1522 dbus_error_free (&error);
1525 dbus_message_unref (message);
1530 /* returns TRUE if the correct thing happens,
1531 * but the correct thing may include OOM errors.
1534 check_hello_connection (BusContext *context)
1536 DBusConnection *connection;
1539 dbus_error_init (&error);
1541 connection = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
1542 if (connection == NULL)
1544 _DBUS_ASSERT_ERROR_IS_SET (&error);
1545 dbus_error_free (&error);
1549 if (!bus_setup_debug_client (connection))
1551 dbus_connection_close (connection);
1552 dbus_connection_unref (connection);
1556 spin_connection_until_authenticated (context, connection);
1558 if (!check_hello_message (context, connection))
1561 if (dbus_bus_get_unique_name (connection) == NULL)
1563 /* We didn't successfully register, so we can't
1564 * do the usual kill_client_connection() checks
1566 kill_client_connection_unchecked (connection);
1570 if (!check_add_match_all (context, connection))
1573 kill_client_connection (context, connection);
1579 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1581 /* returns TRUE if the correct thing happens,
1582 * but the correct thing may include OOM errors.
1585 check_nonexistent_service_no_auto_start (BusContext *context,
1586 DBusConnection *connection)
1588 DBusMessage *message;
1589 dbus_uint32_t serial;
1591 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1592 dbus_uint32_t flags;
1594 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1596 DBUS_INTERFACE_DBUS,
1597 "StartServiceByName");
1599 if (message == NULL)
1602 dbus_message_set_auto_start (message, FALSE);
1605 if (!dbus_message_append_args (message,
1606 DBUS_TYPE_STRING, &nonexistent,
1607 DBUS_TYPE_UINT32, &flags,
1610 dbus_message_unref (message);
1614 if (!dbus_connection_send (connection, message, &serial))
1616 dbus_message_unref (message);
1620 dbus_message_unref (message);
1623 bus_test_run_everything (context);
1624 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1625 bus_test_run_everything (context);
1627 if (!dbus_connection_get_is_connected (connection))
1629 _dbus_verbose ("connection was disconnected\n");
1635 message = pop_message_waiting_for_memory (connection);
1636 if (message == NULL)
1638 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1639 "StartServiceByName", serial, connection);
1643 verbose_message_received (connection, message);
1645 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1647 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1649 _dbus_warn ("Message has wrong sender %s\n",
1650 dbus_message_get_sender (message) ?
1651 dbus_message_get_sender (message) : "(none)");
1655 if (dbus_message_is_error (message,
1656 DBUS_ERROR_NO_MEMORY))
1658 ; /* good, this is a valid response */
1660 else if (dbus_message_is_error (message,
1661 DBUS_ERROR_SERVICE_UNKNOWN))
1663 ; /* good, this is expected also */
1667 warn_unexpected (connection, message, "not this error");
1673 _dbus_warn ("Did not expect to successfully activate %s\n",
1674 NONEXISTENT_SERVICE_NAME);
1682 dbus_message_unref (message);
1687 /* returns TRUE if the correct thing happens,
1688 * but the correct thing may include OOM errors.
1691 check_nonexistent_service_auto_start (BusContext *context,
1692 DBusConnection *connection)
1694 DBusMessage *message;
1695 dbus_uint32_t serial;
1698 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1699 "/org/freedesktop/TestSuite",
1700 "org.freedesktop.TestSuite",
1703 if (message == NULL)
1706 if (!dbus_connection_send (connection, message, &serial))
1708 dbus_message_unref (message);
1712 dbus_message_unref (message);
1715 bus_test_run_everything (context);
1716 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1717 bus_test_run_everything (context);
1719 if (!dbus_connection_get_is_connected (connection))
1721 _dbus_verbose ("connection was disconnected\n");
1727 message = pop_message_waiting_for_memory (connection);
1729 if (message == NULL)
1731 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1732 "Echo message (auto activation)", serial, connection);
1736 verbose_message_received (connection, message);
1738 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1740 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1742 _dbus_warn ("Message has wrong sender %s\n",
1743 dbus_message_get_sender (message) ?
1744 dbus_message_get_sender (message) : "(none)");
1748 if (dbus_message_is_error (message,
1749 DBUS_ERROR_NO_MEMORY))
1751 ; /* good, this is a valid response */
1753 else if (dbus_message_is_error (message,
1754 DBUS_ERROR_SERVICE_UNKNOWN))
1756 ; /* good, this is expected also */
1760 warn_unexpected (connection, message, "not this error");
1766 _dbus_warn ("Did not expect to successfully activate %s\n",
1767 NONEXISTENT_SERVICE_NAME);
1775 dbus_message_unref (message);
1781 check_base_service_activated (BusContext *context,
1782 DBusConnection *connection,
1783 DBusMessage *initial_message,
1784 const char **base_service_p)
1786 DBusMessage *message;
1789 const char *base_service, *base_service_from_bus, *old_owner;
1793 dbus_error_init (&error);
1794 base_service = NULL;
1796 base_service_from_bus = NULL;
1798 message = initial_message;
1799 dbus_message_ref (message);
1801 if (dbus_message_is_signal (message,
1802 DBUS_INTERFACE_DBUS,
1803 "NameOwnerChanged"))
1805 CheckServiceOwnerChangedData socd;
1807 reget_service_name_arg:
1808 base_service = NULL;
1810 base_service_from_bus = NULL;
1812 if (!dbus_message_get_args (message, &error,
1813 DBUS_TYPE_STRING, &base_service,
1814 DBUS_TYPE_STRING, &old_owner,
1815 DBUS_TYPE_STRING, &base_service_from_bus,
1818 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1820 dbus_error_free (&error);
1821 _dbus_wait_for_memory ();
1822 goto reget_service_name_arg;
1826 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1827 "NameOwnerChanged (creation)",
1833 if (*base_service != ':')
1835 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1840 if (strcmp (base_service, base_service_from_bus) != 0)
1842 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1843 base_service, base_service_from_bus);
1849 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1854 socd.expected_kind = SERVICE_CREATED;
1855 socd.expected_service_name = base_service;
1856 socd.failed = FALSE;
1857 socd.skip_connection = connection;
1858 bus_test_clients_foreach (check_service_owner_changed_foreach,
1866 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1872 *base_service_p = base_service;
1878 dbus_message_unref (message);
1879 dbus_error_free (&error);
1885 check_service_activated (BusContext *context,
1886 DBusConnection *connection,
1887 const char *activated_name,
1888 const char *base_service_name,
1889 DBusMessage *initial_message)
1891 DBusMessage *message;
1894 dbus_uint32_t activation_result;
1898 dbus_error_init (&error);
1900 message = initial_message;
1901 dbus_message_ref (message);
1903 if (dbus_message_is_signal (message,
1904 DBUS_INTERFACE_DBUS,
1905 "NameOwnerChanged"))
1907 CheckServiceOwnerChangedData socd;
1908 const char *service_name, *base_service_from_bus, *old_owner;
1910 reget_service_name_arg:
1911 service_name = NULL;
1913 base_service_from_bus = NULL;
1915 if (!dbus_message_get_args (message, &error,
1916 DBUS_TYPE_STRING, &service_name,
1917 DBUS_TYPE_STRING, &old_owner,
1918 DBUS_TYPE_STRING, &base_service_from_bus,
1921 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1923 dbus_error_free (&error);
1924 _dbus_wait_for_memory ();
1925 goto reget_service_name_arg;
1929 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1930 "NameOwnerChanged (creation)",
1936 if (strcmp (service_name, activated_name) != 0)
1938 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1939 activated_name, service_name);
1943 if (strcmp (base_service_name, base_service_from_bus) != 0)
1945 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1946 base_service_from_bus, base_service_name);
1952 _dbus_warn ("expected a %s, got a %s\n",
1953 "NameOwnerChanged (creation)",
1954 "NameOwnerChanged (change)");
1958 socd.expected_kind = SERVICE_CREATED;
1959 socd.skip_connection = connection;
1960 socd.failed = FALSE;
1961 socd.expected_service_name = service_name;
1962 bus_test_clients_foreach (check_service_owner_changed_foreach,
1968 dbus_message_unref (message);
1969 service_name = NULL;
1971 base_service_from_bus = NULL;
1973 message = pop_message_waiting_for_memory (connection);
1974 if (message == NULL)
1976 _dbus_warn ("Expected a reply to %s, got nothing\n",
1977 "StartServiceByName");
1983 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1988 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1990 warn_unexpected (connection, message, "reply to StartServiceByName");
1995 activation_result = 0;
1996 if (!dbus_message_get_args (message, &error,
1997 DBUS_TYPE_UINT32, &activation_result,
2000 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2002 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
2003 "StartServiceByName", error.message);
2007 dbus_error_free (&error);
2011 if (activation_result == DBUS_START_REPLY_SUCCESS)
2013 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
2017 _dbus_warn ("Activation result was %u, no good.\n",
2023 dbus_message_unref (message);
2026 if (!check_no_leftovers (context))
2028 _dbus_warn ("Messages were left over after verifying existent activation results\n");
2036 dbus_message_unref (message);
2037 dbus_error_free (&error);
2043 check_service_auto_activated (BusContext *context,
2044 DBusConnection *connection,
2045 const char *activated_name,
2046 const char *base_service_name,
2047 DBusMessage *initial_message)
2049 DBusMessage *message;
2055 dbus_error_init (&error);
2057 message = initial_message;
2058 dbus_message_ref (message);
2060 if (dbus_message_is_signal (message,
2061 DBUS_INTERFACE_DBUS,
2062 "NameOwnerChanged"))
2064 const char *service_name;
2065 CheckServiceOwnerChangedData socd;
2067 reget_service_name_arg:
2068 if (!dbus_message_get_args (message, &error,
2069 DBUS_TYPE_STRING, &service_name,
2072 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2074 dbus_error_free (&error);
2075 _dbus_wait_for_memory ();
2076 goto reget_service_name_arg;
2080 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2083 dbus_error_free (&error);
2088 if (strcmp (service_name, activated_name) != 0)
2090 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2091 activated_name, service_name);
2095 socd.expected_kind = SERVICE_CREATED;
2096 socd.expected_service_name = service_name;
2097 socd.failed = FALSE;
2098 socd.skip_connection = connection;
2099 bus_test_clients_foreach (check_service_owner_changed_foreach,
2105 /* Note that this differs from regular activation in that we don't get a
2106 * reply to ActivateService here.
2109 dbus_message_unref (message);
2111 service_name = NULL;
2115 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2124 dbus_message_unref (message);
2130 check_service_deactivated (BusContext *context,
2131 DBusConnection *connection,
2132 const char *activated_name,
2133 const char *base_service)
2136 CheckServiceOwnerChangedData socd;
2140 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2141 * service and the activated_name. The base service
2142 * notification is required to come last.
2144 socd.expected_kind = SERVICE_DELETED;
2145 socd.expected_service_name = activated_name;
2146 socd.failed = FALSE;
2147 socd.skip_connection = NULL;
2148 bus_test_clients_foreach (check_service_owner_changed_foreach,
2154 socd.expected_kind = SERVICE_DELETED;
2155 socd.expected_service_name = base_service;
2156 socd.failed = FALSE;
2157 socd.skip_connection = NULL;
2158 bus_test_clients_foreach (check_service_owner_changed_foreach,
2171 check_send_exit_to_service (BusContext *context,
2172 DBusConnection *connection,
2173 const char *service_name,
2174 const char *base_service)
2176 dbus_bool_t got_error;
2177 DBusMessage *message;
2178 dbus_uint32_t serial;
2181 _dbus_verbose ("Sending exit message to the test service\n");
2185 /* Kill off the test service by sending it a quit message */
2186 message = dbus_message_new_method_call (service_name,
2187 "/org/freedesktop/TestSuite",
2188 "org.freedesktop.TestSuite",
2191 if (message == NULL)
2193 /* Do this again; we still need the service to exit... */
2194 if (!check_send_exit_to_service (context, connection,
2195 service_name, base_service))
2201 if (!dbus_connection_send (connection, message, &serial))
2203 dbus_message_unref (message);
2205 /* Do this again; we still need the service to exit... */
2206 if (!check_send_exit_to_service (context, connection,
2207 service_name, base_service))
2213 dbus_message_unref (message);
2217 bus_test_run_clients_loop (SEND_PENDING (connection));
2219 /* read it in and write it out to test service */
2220 bus_test_run_bus_loop (context, FALSE);
2222 /* see if we got an error during message bus dispatching */
2223 bus_test_run_clients_loop (FALSE);
2224 message = borrow_message_waiting_for_memory (connection);
2225 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2228 dbus_connection_return_message (connection, message);
2234 /* If no error, wait for the test service to exit */
2235 block_connection_until_message_from_bus (context, connection, "test service to exit");
2237 bus_test_run_everything (context);
2242 message = pop_message_waiting_for_memory (connection);
2243 _dbus_assert (message != NULL);
2245 if (dbus_message_get_reply_serial (message) != serial)
2247 warn_unexpected (connection, message,
2248 "error with the correct reply serial");
2252 if (!dbus_message_is_error (message,
2253 DBUS_ERROR_NO_MEMORY))
2255 warn_unexpected (connection, message,
2256 "a no memory error from asking test service to exit");
2260 _dbus_verbose ("Got error %s when asking test service to exit\n",
2261 dbus_message_get_error_name (message));
2263 /* Do this again; we still need the service to exit... */
2264 if (!check_send_exit_to_service (context, connection,
2265 service_name, base_service))
2270 if (!check_service_deactivated (context, connection,
2271 service_name, base_service))
2274 /* Should now have a NoReply error from the Exit() method
2275 * call; it should have come after all the deactivation
2278 message = pop_message_waiting_for_memory (connection);
2280 if (message == NULL)
2282 warn_unexpected (connection, NULL,
2283 "reply to Exit() method call");
2286 if (!dbus_message_is_error (message,
2287 DBUS_ERROR_NO_REPLY))
2289 warn_unexpected (connection, message,
2290 "NoReply error from Exit() method call");
2294 if (dbus_message_get_reply_serial (message) != serial)
2296 warn_unexpected (connection, message,
2297 "error with the correct reply serial");
2301 _dbus_verbose ("Got error %s after test service exited\n",
2302 dbus_message_get_error_name (message));
2304 if (!check_no_leftovers (context))
2306 _dbus_warn ("Messages were left over after %s\n",
2307 _DBUS_FUNCTION_NAME);
2316 dbus_message_unref (message);
2322 check_got_error (BusContext *context,
2323 DBusConnection *connection,
2324 const char *first_error_name,
2327 DBusMessage *message;
2330 dbus_bool_t error_found;
2331 const char *error_name;
2335 message = pop_message_waiting_for_memory (connection);
2336 if (message == NULL)
2338 _dbus_warn ("Did not get an expected error\n");
2342 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2344 warn_unexpected (connection, message, "an error");
2349 error_found = FALSE;
2351 va_start (ap, first_error_name);
2352 error_name = first_error_name;
2353 while (error_name != NULL)
2355 if (dbus_message_is_error (message, error_name))
2360 error_name = va_arg (ap, char*);
2366 _dbus_warn ("Expected error %s or other, got %s instead\n",
2368 dbus_message_get_error_name (message));
2376 dbus_message_unref (message);
2383 GOT_SERVICE_CREATED,
2384 GOT_SERVICE_DELETED,
2389 static GotServiceInfo
2390 check_got_service_info (DBusMessage *message)
2392 GotServiceInfo message_kind;
2394 if (dbus_message_is_signal (message,
2395 DBUS_INTERFACE_DBUS,
2396 "NameOwnerChanged"))
2399 const char *service_name, *old_owner, *new_owner;
2400 dbus_error_init (&error);
2402 reget_service_info_data:
2403 service_name = NULL;
2407 dbus_message_get_args (message, &error,
2408 DBUS_TYPE_STRING, &service_name,
2409 DBUS_TYPE_STRING, &old_owner,
2410 DBUS_TYPE_STRING, &new_owner,
2412 if (dbus_error_is_set (&error))
2414 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2416 dbus_error_free (&error);
2417 goto reget_service_info_data;
2421 _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
2422 message_kind = GOT_SOMETHING_ELSE;
2425 else if (!old_owner[0])
2426 message_kind = GOT_SERVICE_CREATED;
2427 else if (!new_owner[0])
2428 message_kind = GOT_SERVICE_DELETED;
2430 message_kind = GOT_SOMETHING_ELSE;
2432 dbus_error_free (&error);
2434 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2435 message_kind = GOT_ERROR;
2437 message_kind = GOT_SOMETHING_ELSE;
2439 return message_kind;
2442 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2444 /* returns TRUE if the correct thing happens,
2445 * but the correct thing may include OOM errors.
2448 check_existent_service_no_auto_start (BusContext *context,
2449 DBusConnection *connection)
2451 DBusMessage *message;
2452 DBusMessage *base_service_message;
2453 const char *base_service;
2454 dbus_uint32_t serial;
2456 const char *existent = EXISTENT_SERVICE_NAME;
2457 dbus_uint32_t flags;
2459 base_service_message = NULL;
2461 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2463 DBUS_INTERFACE_DBUS,
2464 "StartServiceByName");
2466 if (message == NULL)
2469 dbus_message_set_auto_start (message, FALSE);
2472 if (!dbus_message_append_args (message,
2473 DBUS_TYPE_STRING, &existent,
2474 DBUS_TYPE_UINT32, &flags,
2477 dbus_message_unref (message);
2481 if (!dbus_connection_send (connection, message, &serial))
2483 dbus_message_unref (message);
2487 dbus_message_unref (message);
2490 bus_test_run_everything (context);
2492 /* now wait for the message bus to hear back from the activated
2495 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2497 bus_test_run_everything (context);
2499 if (!dbus_connection_get_is_connected (connection))
2501 _dbus_verbose ("connection was disconnected\n");
2507 message = pop_message_waiting_for_memory (connection);
2508 if (message == NULL)
2510 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2511 "StartServiceByName", serial, connection);
2515 verbose_message_received (connection, message);
2516 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2518 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2520 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2522 _dbus_warn ("Message has wrong sender %s\n",
2523 dbus_message_get_sender (message) ?
2524 dbus_message_get_sender (message) : "(none)");
2528 if (dbus_message_is_error (message,
2529 DBUS_ERROR_NO_MEMORY))
2531 ; /* good, this is a valid response */
2533 else if (dbus_message_is_error (message,
2534 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2535 dbus_message_is_error (message,
2536 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
2537 dbus_message_is_error (message,
2538 DBUS_ERROR_SPAWN_EXEC_FAILED))
2540 ; /* good, this is expected also */
2544 _dbus_warn ("Did not expect error %s\n",
2545 dbus_message_get_error_name (message));
2551 GotServiceInfo message_kind;
2553 if (!check_base_service_activated (context, connection,
2554 message, &base_service))
2557 base_service_message = message;
2560 /* We may need to block here for the test service to exit or finish up */
2561 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2563 message = dbus_connection_borrow_message (connection);
2564 if (message == NULL)
2566 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2570 message_kind = check_got_service_info (message);
2572 dbus_connection_return_message (connection, message);
2575 switch (message_kind)
2577 case GOT_SOMETHING_ELSE:
2578 _dbus_warn ("Unexpected message after ActivateService "
2579 "(should be an error or a service announcement");
2583 if (!check_got_error (context, connection,
2584 DBUS_ERROR_SPAWN_CHILD_EXITED,
2585 DBUS_ERROR_NO_MEMORY,
2588 /* A service deleted should be coming along now after this error.
2589 * We can also get the error *after* the service deleted.
2594 case GOT_SERVICE_DELETED:
2596 /* The service started up and got a base address, but then
2597 * failed to register under EXISTENT_SERVICE_NAME
2599 CheckServiceOwnerChangedData socd;
2601 socd.expected_kind = SERVICE_DELETED;
2602 socd.expected_service_name = base_service;
2603 socd.failed = FALSE;
2604 socd.skip_connection = NULL;
2606 bus_test_clients_foreach (check_service_owner_changed_foreach,
2612 /* Now we should get an error about the service exiting
2613 * if we didn't get it before.
2615 if (message_kind != GOT_ERROR)
2617 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2619 /* and process everything again */
2620 bus_test_run_everything (context);
2622 if (!check_got_error (context, connection,
2623 DBUS_ERROR_SPAWN_CHILD_EXITED,
2624 DBUS_ERROR_NO_MEMORY,
2631 case GOT_SERVICE_CREATED:
2632 message = pop_message_waiting_for_memory (connection);
2633 if (message == NULL)
2635 _dbus_warn ("Failed to pop message we just put back! "
2636 "should have been a NameOwnerChanged (creation)\n");
2640 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2641 base_service, message))
2644 dbus_message_unref (message);
2647 if (!check_no_leftovers (context))
2649 _dbus_warn ("Messages were left over after successful activation\n");
2653 if (!check_send_exit_to_service (context, connection,
2654 EXISTENT_SERVICE_NAME, base_service))
2665 dbus_message_unref (message);
2667 if (base_service_message)
2668 dbus_message_unref (base_service_message);
2673 #ifndef DBUS_WIN_FIXME
2674 /* returns TRUE if the correct thing happens,
2675 * but the correct thing may include OOM errors.
2678 check_segfault_service_no_auto_start (BusContext *context,
2679 DBusConnection *connection)
2681 DBusMessage *message;
2682 dbus_uint32_t serial;
2684 const char *segv_service;
2685 dbus_uint32_t flags;
2687 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2689 DBUS_INTERFACE_DBUS,
2690 "StartServiceByName");
2692 if (message == NULL)
2695 dbus_message_set_auto_start (message, FALSE);
2697 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2699 if (!dbus_message_append_args (message,
2700 DBUS_TYPE_STRING, &segv_service,
2701 DBUS_TYPE_UINT32, &flags,
2704 dbus_message_unref (message);
2708 if (!dbus_connection_send (connection, message, &serial))
2710 dbus_message_unref (message);
2714 dbus_message_unref (message);
2717 bus_test_run_everything (context);
2718 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2719 bus_test_run_everything (context);
2721 if (!dbus_connection_get_is_connected (connection))
2723 _dbus_verbose ("connection was disconnected\n");
2729 message = pop_message_waiting_for_memory (connection);
2730 if (message == NULL)
2732 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2733 "StartServiceByName", serial, connection);
2737 verbose_message_received (connection, message);
2739 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2741 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2743 _dbus_warn ("Message has wrong sender %s\n",
2744 dbus_message_get_sender (message) ?
2745 dbus_message_get_sender (message) : "(none)");
2749 if (dbus_message_is_error (message,
2750 DBUS_ERROR_NO_MEMORY))
2752 ; /* good, this is a valid response */
2754 else if (dbus_message_is_error (message,
2757 const char *servicehelper;
2758 servicehelper = bus_context_get_servicehelper (context);
2759 /* make sure this only happens with the launch helper */
2760 _dbus_assert (servicehelper != NULL);
2762 else if (dbus_message_is_error (message,
2763 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2765 ; /* good, this is expected also */
2769 warn_unexpected (connection, message, "not this error");
2776 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2784 dbus_message_unref (message);
2790 /* returns TRUE if the correct thing happens,
2791 * but the correct thing may include OOM errors.
2794 check_segfault_service_auto_start (BusContext *context,
2795 DBusConnection *connection)
2797 DBusMessage *message;
2798 dbus_uint32_t serial;
2801 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2802 "/org/freedesktop/TestSuite",
2803 "org.freedesktop.TestSuite",
2806 if (message == NULL)
2809 if (!dbus_connection_send (connection, message, &serial))
2811 dbus_message_unref (message);
2815 dbus_message_unref (message);
2818 bus_test_run_everything (context);
2819 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2820 bus_test_run_everything (context);
2822 if (!dbus_connection_get_is_connected (connection))
2824 _dbus_verbose ("connection was disconnected\n");
2830 message = pop_message_waiting_for_memory (connection);
2831 if (message == NULL)
2833 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2834 "Echo message (auto activation)", serial, connection);
2838 verbose_message_received (connection, message);
2840 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2842 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2844 _dbus_warn ("Message has wrong sender %s\n",
2845 dbus_message_get_sender (message) ?
2846 dbus_message_get_sender (message) : "(none)");
2850 if (dbus_message_is_error (message,
2851 DBUS_ERROR_NO_MEMORY))
2853 ; /* good, this is a valid response */
2855 else if (dbus_message_is_error (message,
2856 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2858 ; /* good, this is expected also */
2862 warn_unexpected (connection, message, "not this error");
2869 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2877 dbus_message_unref (message);
2883 #define TEST_ECHO_MESSAGE "Test echo message"
2884 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2886 /* returns TRUE if the correct thing happens,
2887 * but the correct thing may include OOM errors.
2890 check_existent_hello_from_self (BusContext *context,
2891 DBusConnection *connection)
2893 DBusMessage *message;
2894 dbus_uint32_t serial;
2897 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2898 "/org/freedesktop/TestSuite",
2899 "org.freedesktop.TestSuite",
2900 "RunHelloFromSelf");
2902 if (message == NULL)
2905 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2906 if (!dbus_message_append_args (message,
2907 DBUS_TYPE_STRING, &text,
2910 dbus_message_unref (message);
2914 if (!dbus_connection_send (connection, message, &serial))
2916 dbus_message_unref (message);
2920 dbus_message_unref (message);
2923 bus_test_run_everything (context);
2925 /* Note: if this test is run in OOM mode, it will block when the bus
2926 * doesn't send a reply due to OOM.
2928 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2930 message = pop_message_waiting_for_memory (connection);
2931 if (message == NULL)
2933 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2937 if (dbus_message_get_reply_serial (message) != serial)
2939 _dbus_warn ("Wrong reply serial\n");
2940 dbus_message_unref (message);
2944 dbus_message_unref (message);
2950 /* returns TRUE if the correct thing happens,
2951 * but the correct thing may include OOM errors.
2954 check_existent_ping (BusContext *context,
2955 DBusConnection *connection)
2957 DBusMessage *message;
2958 dbus_uint32_t serial;
2959 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2960 "/org/freedesktop/TestSuite",
2961 "org.freedesktop.DBus.Peer",
2964 if (message == NULL)
2967 if (!dbus_connection_send (connection, message, &serial))
2969 dbus_message_unref (message);
2973 dbus_message_unref (message);
2976 bus_test_run_everything (context);
2978 /* Note: if this test is run in OOM mode, it will block when the bus
2979 * doesn't send a reply due to OOM.
2981 block_connection_until_message_from_bus (context, connection, "reply from running Ping");
2983 message = pop_message_waiting_for_memory (connection);
2984 if (message == NULL)
2986 _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
2990 if (dbus_message_get_reply_serial (message) != serial)
2992 _dbus_warn ("Wrong reply serial\n");
2993 dbus_message_unref (message);
2997 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
2999 _dbus_warn ("Unexpected message return during Ping\n");
3000 dbus_message_unref (message);
3004 dbus_message_unref (message);
3010 /* returns TRUE if the correct thing happens,
3011 * but the correct thing may include OOM errors.
3014 check_existent_get_machine_id (BusContext *context,
3015 DBusConnection *connection)
3017 DBusMessage *message;
3018 dbus_uint32_t serial;
3019 const char *machine_id;
3021 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3022 "/org/freedesktop/TestSuite",
3023 "org.freedesktop.DBus.Peer",
3026 if (message == NULL)
3029 if (!dbus_connection_send (connection, message, &serial))
3031 dbus_message_unref (message);
3035 dbus_message_unref (message);
3038 bus_test_run_everything (context);
3040 /* Note: if this test is run in OOM mode, it will block when the bus
3041 * doesn't send a reply due to OOM.
3043 block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
3045 message = pop_message_waiting_for_memory (connection);
3046 if (message == NULL)
3048 _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
3052 if (dbus_message_get_reply_serial (message) != serial)
3054 _dbus_warn ("Wrong reply serial\n");
3055 dbus_message_unref (message);
3059 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
3061 _dbus_warn ("Unexpected message return during GetMachineId\n");
3062 dbus_message_unref (message);
3067 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
3069 _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
3070 dbus_message_unref (message);
3074 if (machine_id == NULL || strlen (machine_id) != 32)
3076 _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
3077 dbus_message_unref (message);
3081 /* We can't check that the machine id is correct because during make check it is
3082 * just made up for each process separately
3085 dbus_message_unref (message);
3091 /* returns TRUE if the correct thing happens,
3092 * but the correct thing may include OOM errors.
3095 check_existent_service_auto_start (BusContext *context,
3096 DBusConnection *connection)
3098 DBusMessage *message;
3099 DBusMessage *base_service_message;
3100 dbus_uint32_t serial;
3102 const char *base_service;
3105 base_service_message = NULL;
3107 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
3108 "/org/freedesktop/TestSuite",
3109 "org.freedesktop.TestSuite",
3112 if (message == NULL)
3115 text = TEST_ECHO_MESSAGE;
3116 if (!dbus_message_append_args (message,
3117 DBUS_TYPE_STRING, &text,
3120 dbus_message_unref (message);
3124 if (!dbus_connection_send (connection, message, &serial))
3126 dbus_message_unref (message);
3130 dbus_message_unref (message);
3133 bus_test_run_everything (context);
3135 /* now wait for the message bus to hear back from the activated
3138 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
3139 bus_test_run_everything (context);
3141 if (!dbus_connection_get_is_connected (connection))
3143 _dbus_verbose ("connection was disconnected\n");
3149 message = pop_message_waiting_for_memory (connection);
3150 if (message == NULL)
3152 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3153 serial, connection);
3157 verbose_message_received (connection, message);
3158 _dbus_verbose (" (after sending %s)\n", "auto start");
3160 /* we should get zero or two ServiceOwnerChanged signals */
3161 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3163 GotServiceInfo message_kind;
3165 if (!check_base_service_activated (context, connection,
3166 message, &base_service))
3169 base_service_message = message;
3172 /* We may need to block here for the test service to exit or finish up */
3173 block_connection_until_message_from_bus (context, connection, "service to exit");
3175 /* Should get a service creation notification for the activated
3176 * service name, or a service deletion on the base service name
3178 message = dbus_connection_borrow_message (connection);
3179 if (message == NULL)
3181 _dbus_warn ("No message after auto activation "
3182 "(should be a service announcement)\n");
3183 dbus_connection_return_message (connection, message);
3188 message_kind = check_got_service_info (message);
3190 dbus_connection_return_message (connection, message);
3193 switch (message_kind)
3195 case GOT_SERVICE_CREATED:
3196 message = pop_message_waiting_for_memory (connection);
3197 if (message == NULL)
3199 _dbus_warn ("Failed to pop message we just put back! "
3200 "should have been a NameOwnerChanged (creation)\n");
3204 /* Check that ServiceOwnerChanged (creation) was correctly received */
3205 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3206 base_service, message))
3209 dbus_message_unref (message);
3214 case GOT_SERVICE_DELETED:
3216 /* The service started up and got a base address, but then
3217 * failed to register under EXISTENT_SERVICE_NAME
3219 CheckServiceOwnerChangedData socd;
3221 socd.expected_kind = SERVICE_DELETED;
3222 socd.expected_service_name = base_service;
3223 socd.failed = FALSE;
3224 socd.skip_connection = NULL;
3225 bus_test_clients_foreach (check_service_owner_changed_foreach,
3235 case GOT_SOMETHING_ELSE:
3236 _dbus_warn ("Unexpected message after auto activation\n");
3241 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3242 * come the method reply (or error) from the initial method call
3245 /* Note: if this test is run in OOM mode, it will block when the bus
3246 * doesn't send a reply due to OOM.
3248 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3250 message = pop_message_waiting_for_memory (connection);
3251 if (message == NULL)
3253 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3257 if (dbus_message_get_reply_serial (message) != serial)
3259 _dbus_warn ("Wrong reply serial\n");
3263 dbus_message_unref (message);
3266 if (!check_existent_ping (context, connection))
3269 if (!check_existent_get_machine_id (context, connection))
3272 if (!check_existent_hello_from_self (context, connection))
3275 if (!check_send_exit_to_service (context, connection,
3276 EXISTENT_SERVICE_NAME,
3284 dbus_message_unref (message);
3286 if (base_service_message)
3287 dbus_message_unref (base_service_message);
3292 #define SERVICE_FILE_MISSING_NAME "org.freedesktop.DBus.TestSuiteEchoServiceDotServiceFileDoesNotExist"
3294 /* returns TRUE if the correct thing happens,
3295 * but the correct thing may include OOM errors.
3298 check_launch_service_file_missing (BusContext *context,
3299 DBusConnection *connection)
3301 DBusMessage *message;
3302 dbus_uint32_t serial;
3305 message = dbus_message_new_method_call (SERVICE_FILE_MISSING_NAME,
3306 "/org/freedesktop/TestSuite",
3307 "org.freedesktop.TestSuite",
3310 if (message == NULL)
3313 if (!dbus_connection_send (connection, message, &serial))
3315 dbus_message_unref (message);
3319 dbus_message_unref (message);
3322 bus_test_run_everything (context);
3323 block_connection_until_message_from_bus (context, connection, "reply to service file missing should fail to auto-start");
3324 bus_test_run_everything (context);
3326 if (!dbus_connection_get_is_connected (connection))
3328 _dbus_verbose ("connection was disconnected\n");
3334 message = pop_message_waiting_for_memory (connection);
3335 if (message == NULL)
3337 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3338 "Echo message (auto activation)", serial, connection);
3342 verbose_message_received (connection, message);
3344 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3346 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3348 _dbus_warn ("Message has wrong sender %s\n",
3349 dbus_message_get_sender (message) ?
3350 dbus_message_get_sender (message) : "(none)");
3354 if (dbus_message_is_error (message,
3355 DBUS_ERROR_NO_MEMORY))
3357 ; /* good, this is a valid response */
3359 else if (dbus_message_is_error (message,
3360 DBUS_ERROR_SERVICE_UNKNOWN))
3362 _dbus_verbose("got service unknown\n");
3363 ; /* good, this is expected (only valid when using launch helper) */
3367 warn_unexpected (connection, message, "not this error");
3374 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3382 dbus_message_unref (message);
3387 #define SERVICE_USER_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoUser"
3389 /* returns TRUE if the correct thing happens,
3390 * but the correct thing may include OOM errors.
3393 check_launch_service_user_missing (BusContext *context,
3394 DBusConnection *connection)
3396 DBusMessage *message;
3397 dbus_uint32_t serial;
3400 message = dbus_message_new_method_call (SERVICE_USER_MISSING_NAME,
3401 "/org/freedesktop/TestSuite",
3402 "org.freedesktop.TestSuite",
3405 if (message == NULL)
3408 if (!dbus_connection_send (connection, message, &serial))
3410 dbus_message_unref (message);
3414 dbus_message_unref (message);
3417 bus_test_run_everything (context);
3418 block_connection_until_message_from_bus (context, connection,
3419 "reply to service which should fail to auto-start (missing User)");
3420 bus_test_run_everything (context);
3422 if (!dbus_connection_get_is_connected (connection))
3424 _dbus_warn ("connection was disconnected\n");
3430 message = pop_message_waiting_for_memory (connection);
3431 if (message == NULL)
3433 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3434 "Echo message (auto activation)", serial, connection);
3438 verbose_message_received (connection, message);
3440 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3442 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3444 _dbus_warn ("Message has wrong sender %s\n",
3445 dbus_message_get_sender (message) ?
3446 dbus_message_get_sender (message) : "(none)");
3450 if (dbus_message_is_error (message,
3451 DBUS_ERROR_NO_MEMORY))
3453 ; /* good, this is a valid response */
3455 else if (dbus_message_is_error (message,
3456 DBUS_ERROR_SPAWN_FILE_INVALID))
3458 _dbus_verbose("got service file invalid\n");
3459 ; /* good, this is expected (only valid when using launch helper) */
3463 warn_unexpected (connection, message, "not this error");
3470 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3478 dbus_message_unref (message);
3483 #define SERVICE_EXEC_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoExec"
3485 /* returns TRUE if the correct thing happens,
3486 * but the correct thing may include OOM errors.
3489 check_launch_service_exec_missing (BusContext *context,
3490 DBusConnection *connection)
3492 DBusMessage *message;
3493 dbus_uint32_t serial;
3496 message = dbus_message_new_method_call (SERVICE_EXEC_MISSING_NAME,
3497 "/org/freedesktop/TestSuite",
3498 "org.freedesktop.TestSuite",
3501 if (message == NULL)
3504 if (!dbus_connection_send (connection, message, &serial))
3506 dbus_message_unref (message);
3510 dbus_message_unref (message);
3513 bus_test_run_everything (context);
3514 block_connection_until_message_from_bus (context, connection,
3515 "reply to service which should fail to auto-start (missing Exec)");
3516 bus_test_run_everything (context);
3518 if (!dbus_connection_get_is_connected (connection))
3520 _dbus_warn ("connection was disconnected\n");
3526 message = pop_message_waiting_for_memory (connection);
3527 if (message == NULL)
3529 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3530 "Echo message (auto activation)", serial, connection);
3534 verbose_message_received (connection, message);
3536 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3538 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3540 _dbus_warn ("Message has wrong sender %s\n",
3541 dbus_message_get_sender (message) ?
3542 dbus_message_get_sender (message) : "(none)");
3546 if (dbus_message_is_error (message,
3547 DBUS_ERROR_NO_MEMORY))
3549 ; /* good, this is a valid response */
3551 else if (dbus_message_is_error (message,
3552 DBUS_ERROR_SERVICE_UNKNOWN))
3554 _dbus_verbose("could not activate as invalid service file was not added\n");
3555 ; /* good, this is expected as we shouldn't have been added to
3556 * the activation list with a missing Exec key */
3558 else if (dbus_message_is_error (message,
3559 DBUS_ERROR_SPAWN_FILE_INVALID))
3561 _dbus_verbose("got service file invalid\n");
3562 ; /* good, this is allowed, and is the message passed back from the
3567 warn_unexpected (connection, message, "not this error");
3574 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3582 dbus_message_unref (message);
3587 #define SERVICE_SERVICE_MISSING_NAME "org.freedesktop.DBus.TestSuiteNoService"
3589 /* returns TRUE if the correct thing happens,
3590 * but the correct thing may include OOM errors.
3593 check_launch_service_service_missing (BusContext *context,
3594 DBusConnection *connection)
3596 DBusMessage *message;
3597 dbus_uint32_t serial;
3600 message = dbus_message_new_method_call (SERVICE_SERVICE_MISSING_NAME,
3601 "/org/freedesktop/TestSuite",
3602 "org.freedesktop.TestSuite",
3605 if (message == NULL)
3608 if (!dbus_connection_send (connection, message, &serial))
3610 dbus_message_unref (message);
3614 dbus_message_unref (message);
3617 bus_test_run_everything (context);
3618 block_connection_until_message_from_bus (context, connection,
3619 "reply to service which should fail to auto-start (missing Service)");
3620 bus_test_run_everything (context);
3622 if (!dbus_connection_get_is_connected (connection))
3624 _dbus_warn ("connection was disconnected\n");
3630 message = pop_message_waiting_for_memory (connection);
3631 if (message == NULL)
3633 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3634 "Echo message (auto activation)", serial, connection);
3638 verbose_message_received (connection, message);
3640 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3642 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3644 _dbus_warn ("Message has wrong sender %s\n",
3645 dbus_message_get_sender (message) ?
3646 dbus_message_get_sender (message) : "(none)");
3650 if (dbus_message_is_error (message,
3651 DBUS_ERROR_NO_MEMORY))
3653 ; /* good, this is a valid response */
3655 else if (dbus_message_is_error (message,
3656 DBUS_ERROR_SERVICE_UNKNOWN))
3658 _dbus_verbose("could not activate as invalid service file was not added\n");
3659 ; /* good, this is expected as we shouldn't have been added to
3660 * the activation list with a missing Exec key */
3662 else if (dbus_message_is_error (message,
3663 DBUS_ERROR_SPAWN_FILE_INVALID))
3665 _dbus_verbose("got service file invalid\n");
3666 ; /* good, this is allowed, and is the message passed back from the
3671 warn_unexpected (connection, message, "not this error");
3678 _dbus_warn ("Did not expect to successfully auto-start missing service\n");
3686 dbus_message_unref (message);
3691 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3693 /* returns TRUE if the correct thing happens,
3694 * but the correct thing may include OOM errors.
3697 check_shell_fail_service_auto_start (BusContext *context,
3698 DBusConnection *connection)
3700 DBusMessage *message;
3701 dbus_uint32_t serial;
3704 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3705 "/org/freedesktop/TestSuite",
3706 "org.freedesktop.TestSuite",
3709 if (message == NULL)
3712 if (!dbus_connection_send (connection, message, &serial))
3714 dbus_message_unref (message);
3718 dbus_message_unref (message);
3721 bus_test_run_everything (context);
3722 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3723 bus_test_run_everything (context);
3725 if (!dbus_connection_get_is_connected (connection))
3727 _dbus_verbose ("connection was disconnected\n");
3733 message = pop_message_waiting_for_memory (connection);
3734 if (message == NULL)
3736 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3737 "Echo message (auto activation)", serial, connection);
3741 verbose_message_received (connection, message);
3743 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3745 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3747 _dbus_warn ("Message has wrong sender %s\n",
3748 dbus_message_get_sender (message) ?
3749 dbus_message_get_sender (message) : "(none)");
3753 if (dbus_message_is_error (message,
3754 DBUS_ERROR_NO_MEMORY))
3756 ; /* good, this is a valid response */
3758 else if (dbus_message_is_error (message,
3759 DBUS_ERROR_INVALID_ARGS))
3761 _dbus_verbose("got invalid args\n");
3762 ; /* good, this is expected also */
3766 warn_unexpected (connection, message, "not this error");
3773 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3781 dbus_message_unref (message);
3786 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3788 /* returns TRUE if the correct thing happens,
3789 * but the correct thing may include OOM errors.
3792 check_shell_service_success_auto_start (BusContext *context,
3793 DBusConnection *connection)
3795 DBusMessage *message;
3796 DBusMessage *base_service_message;
3797 dbus_uint32_t serial;
3799 const char *base_service;
3800 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3802 base_service_message = NULL;
3804 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3805 "/org/freedesktop/TestSuite",
3806 "org.freedesktop.TestSuite",
3809 if (message == NULL)
3812 if (!dbus_connection_send (connection, message, &serial))
3814 dbus_message_unref (message);
3818 dbus_message_unref (message);
3821 bus_test_run_everything (context);
3823 /* now wait for the message bus to hear back from the activated
3826 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3827 bus_test_run_everything (context);
3829 if (!dbus_connection_get_is_connected (connection))
3831 _dbus_verbose ("connection was disconnected\n");
3837 message = pop_message_waiting_for_memory (connection);
3838 if (message == NULL)
3840 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3841 serial, connection);
3845 verbose_message_received (connection, message);
3846 _dbus_verbose (" (after sending %s)\n", "auto start");
3848 /* we should get zero or two ServiceOwnerChanged signals */
3849 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3851 GotServiceInfo message_kind;
3853 if (!check_base_service_activated (context, connection,
3854 message, &base_service))
3857 base_service_message = message;
3860 /* We may need to block here for the test service to exit or finish up */
3861 block_connection_until_message_from_bus (context, connection, "service to exit");
3863 /* Should get a service creation notification for the activated
3864 * service name, or a service deletion on the base service name
3866 message = dbus_connection_borrow_message (connection);
3867 if (message == NULL)
3869 _dbus_warn ("No message after auto activation "
3870 "(should be a service announcement)\n");
3871 dbus_connection_return_message (connection, message);
3876 message_kind = check_got_service_info (message);
3878 dbus_connection_return_message (connection, message);
3881 switch (message_kind)
3883 case GOT_SERVICE_CREATED:
3884 message = pop_message_waiting_for_memory (connection);
3885 if (message == NULL)
3887 _dbus_warn ("Failed to pop message we just put back! "
3888 "should have been a NameOwnerChanged (creation)\n");
3892 /* Check that ServiceOwnerChanged (creation) was correctly received */
3893 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3894 base_service, message))
3897 dbus_message_unref (message);
3902 case GOT_SERVICE_DELETED:
3904 /* The service started up and got a base address, but then
3905 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3907 CheckServiceOwnerChangedData socd;
3909 socd.expected_kind = SERVICE_DELETED;
3910 socd.expected_service_name = base_service;
3911 socd.failed = FALSE;
3912 socd.skip_connection = NULL;
3913 bus_test_clients_foreach (check_service_owner_changed_foreach,
3923 case GOT_SOMETHING_ELSE:
3924 _dbus_warn ("Unexpected message after auto activation\n");
3929 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3930 * come the method reply (or error) from the initial method call
3933 /* Note: if this test is run in OOM mode, it will block when the bus
3934 * doesn't send a reply due to OOM.
3936 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3938 message = pop_message_waiting_for_memory (connection);
3939 if (message == NULL)
3941 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3945 if (dbus_message_get_reply_serial (message) != serial)
3947 _dbus_warn ("Wrong reply serial\n");
3951 if (!dbus_message_get_args (message, NULL,
3952 DBUS_TYPE_STRING, &argv[0],
3953 DBUS_TYPE_STRING, &argv[1],
3954 DBUS_TYPE_STRING, &argv[2],
3955 DBUS_TYPE_STRING, &argv[3],
3956 DBUS_TYPE_STRING, &argv[4],
3957 DBUS_TYPE_STRING, &argv[5],
3958 DBUS_TYPE_STRING, &argv[6],
3961 _dbus_warn ("Error getting arguments from return\n");
3965 /* don't worry about arg[0] as it may be different
3966 depending on the path to the tests
3968 if (strcmp("-test", argv[1]) != 0)
3970 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n",
3975 if (strcmp("that", argv[2]) != 0)
3977 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n",
3982 if (strcmp("we get", argv[3]) != 0)
3984 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n",
3989 if (strcmp("back", argv[4]) != 0)
3991 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n",
3996 if (strcmp("--what", argv[5]) != 0)
3998 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n",
4003 if (strcmp("we put in", argv[6]) != 0)
4005 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n",
4006 "we put in", argv[6]);
4010 dbus_message_unref (message);
4013 if (!check_send_exit_to_service (context, connection,
4014 SHELL_SUCCESS_SERVICE_NAME,
4022 dbus_message_unref (message);
4024 if (base_service_message)
4025 dbus_message_unref (base_service_message);
4033 BusContext *context;
4037 check_oom_check1_func (void *data)
4039 Check1Data *d = data;
4041 if (! (* d->func) (d->context))
4044 if (!check_no_leftovers (d->context))
4046 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4054 check1_try_iterations (BusContext *context,
4055 const char *description,
4061 d.context = context;
4063 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
4065 _dbus_assert_not_reached ("test failed");
4069 check_get_services (BusContext *context,
4070 DBusConnection *connection,
4075 DBusMessage *message;
4076 dbus_uint32_t serial;
4083 dbus_error_init (&error);
4086 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4088 DBUS_INTERFACE_DBUS,
4091 if (message == NULL)
4094 if (!dbus_connection_send (connection, message, &serial))
4096 dbus_message_unref (message);
4100 /* send our message */
4101 bus_test_run_clients_loop (SEND_PENDING (connection));
4103 dbus_message_unref (message);
4106 dbus_connection_ref (connection); /* because we may get disconnected */
4107 block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
4109 if (!dbus_connection_get_is_connected (connection))
4111 _dbus_verbose ("connection was disconnected\n");
4113 dbus_connection_unref (connection);
4118 dbus_connection_unref (connection);
4120 message = pop_message_waiting_for_memory (connection);
4121 if (message == NULL)
4123 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
4124 method, serial, connection);
4128 verbose_message_received (connection, message);
4130 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4132 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
4134 ; /* good, this is a valid response */
4138 warn_unexpected (connection, message, "not this error");
4145 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
4147 ; /* good, expected */
4151 warn_unexpected (connection, message,
4152 "method_return for ListActivatableNames/ListNames");
4159 if (!dbus_message_get_args (message, &error,
4165 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4167 _dbus_verbose ("no memory to list services by %s\n", method);
4168 dbus_error_free (&error);
4169 _dbus_wait_for_memory ();
4170 goto retry_get_property;
4174 _dbus_assert (dbus_error_is_set (&error));
4175 _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
4184 if (!check_no_leftovers (context))
4190 dbus_error_free (&error);
4193 dbus_message_unref (message);
4198 /* returns TRUE if the correct thing happens,
4199 * but the correct thing may include OOM errors.
4202 check_list_services (BusContext *context,
4203 DBusConnection *connection)
4205 DBusMessage *message;
4206 DBusMessage *base_service_message;
4207 const char *base_service;
4208 dbus_uint32_t serial;
4210 const char *existent = EXISTENT_SERVICE_NAME;
4211 dbus_uint32_t flags;
4215 _dbus_verbose ("check_list_services for %p\n", connection);
4217 if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
4222 if (!_dbus_string_array_contains ((const char **)services, existent))
4224 _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
4225 dbus_free_string_array (services);
4229 dbus_free_string_array (services);
4231 base_service_message = NULL;
4233 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
4235 DBUS_INTERFACE_DBUS,
4236 "StartServiceByName");
4238 if (message == NULL)
4241 dbus_message_set_auto_start (message, FALSE);
4244 if (!dbus_message_append_args (message,
4245 DBUS_TYPE_STRING, &existent,
4246 DBUS_TYPE_UINT32, &flags,
4249 dbus_message_unref (message);
4253 if (!dbus_connection_send (connection, message, &serial))
4255 dbus_message_unref (message);
4259 dbus_message_unref (message);
4262 bus_test_run_everything (context);
4264 /* now wait for the message bus to hear back from the activated
4267 block_connection_until_message_from_bus (context, connection, "activated service to connect");
4269 bus_test_run_everything (context);
4271 if (!dbus_connection_get_is_connected (connection))
4273 _dbus_verbose ("connection was disconnected\n");
4279 message = pop_message_waiting_for_memory (connection);
4280 if (message == NULL)
4282 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
4283 "StartServiceByName", serial, connection);
4287 verbose_message_received (connection, message);
4288 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
4290 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
4292 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
4294 _dbus_warn ("Message has wrong sender %s\n",
4295 dbus_message_get_sender (message) ?
4296 dbus_message_get_sender (message) : "(none)");
4300 if (dbus_message_is_error (message,
4301 DBUS_ERROR_NO_MEMORY))
4303 ; /* good, this is a valid response */
4305 else if (dbus_message_is_error (message,
4306 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
4307 dbus_message_is_error (message,
4308 DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
4309 dbus_message_is_error (message,
4310 DBUS_ERROR_SPAWN_EXEC_FAILED))
4312 ; /* good, this is expected also */
4316 _dbus_warn ("Did not expect error %s\n",
4317 dbus_message_get_error_name (message));
4323 GotServiceInfo message_kind;
4325 if (!check_base_service_activated (context, connection,
4326 message, &base_service))
4329 base_service_message = message;
4332 /* We may need to block here for the test service to exit or finish up */
4333 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
4335 message = dbus_connection_borrow_message (connection);
4336 if (message == NULL)
4338 _dbus_warn ("Did not receive any messages after base service creation notification\n");
4342 message_kind = check_got_service_info (message);
4344 dbus_connection_return_message (connection, message);
4347 switch (message_kind)
4349 case GOT_SOMETHING_ELSE:
4351 case GOT_SERVICE_DELETED:
4352 _dbus_warn ("Unexpected message after ActivateService "
4353 "(should be an error or a service announcement)\n");
4356 case GOT_SERVICE_CREATED:
4357 message = pop_message_waiting_for_memory (connection);
4358 if (message == NULL)
4360 _dbus_warn ("Failed to pop message we just put back! "
4361 "should have been a NameOwnerChanged (creation)\n");
4365 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
4366 base_service, message))
4369 dbus_message_unref (message);
4372 if (!check_no_leftovers (context))
4374 _dbus_warn ("Messages were left over after successful activation\n");
4382 if (!check_get_services (context, connection, "ListNames", &services, &len))
4387 if (!_dbus_string_array_contains ((const char **)services, existent))
4389 _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
4393 dbus_free_string_array (services);
4395 if (!check_send_exit_to_service (context, connection,
4396 EXISTENT_SERVICE_NAME, base_service))
4403 dbus_message_unref (message);
4405 if (base_service_message)
4406 dbus_message_unref (base_service_message);
4414 BusContext *context;
4415 DBusConnection *connection;
4419 check_oom_check2_func (void *data)
4421 Check2Data *d = data;
4423 if (! (* d->func) (d->context, d->connection))
4426 if (!check_no_leftovers (d->context))
4428 _dbus_warn ("Messages were left over, should be covered by test suite\n");
4436 check2_try_iterations (BusContext *context,
4437 DBusConnection *connection,
4438 const char *description,
4444 d.context = context;
4445 d.connection = connection;
4447 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
4450 _dbus_warn ("%s failed during oom\n", description);
4451 _dbus_assert_not_reached ("test failed");
4456 setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir,
4457 const char *filename)
4462 if (!_dbus_string_init (&full))
4465 if (!_dbus_string_copy (test_data_dir, 0, &full, 0))
4467 _dbus_string_free (&full);
4471 _dbus_string_init_const (&file, filename);
4473 if (!_dbus_concat_dir_and_file (&full, &file))
4475 _dbus_string_free (&full);
4479 _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n",
4480 _dbus_string_get_const_data (&full));
4482 dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full));
4484 _dbus_string_free (&full);
4490 bus_dispatch_test_conf (const DBusString *test_data_dir,
4491 const char *filename,
4492 dbus_bool_t use_launcher)
4494 BusContext *context;
4495 DBusConnection *foo;
4496 DBusConnection *bar;
4497 DBusConnection *baz;
4500 /* save the config name for the activation helper */
4501 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4502 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4504 dbus_error_init (&error);
4506 context = bus_context_new_test (test_data_dir, filename);
4507 if (context == NULL)
4510 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4512 _dbus_assert_not_reached ("could not alloc connection");
4514 if (!bus_setup_debug_client (foo))
4515 _dbus_assert_not_reached ("could not set up connection");
4517 spin_connection_until_authenticated (context, foo);
4519 if (!check_hello_message (context, foo))
4520 _dbus_assert_not_reached ("hello message failed");
4522 if (!check_double_hello_message (context, foo))
4523 _dbus_assert_not_reached ("double hello message failed");
4525 if (!check_add_match_all (context, foo))
4526 _dbus_assert_not_reached ("AddMatch message failed");
4528 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4530 _dbus_assert_not_reached ("could not alloc connection");
4532 if (!bus_setup_debug_client (bar))
4533 _dbus_assert_not_reached ("could not set up connection");
4535 spin_connection_until_authenticated (context, bar);
4537 if (!check_hello_message (context, bar))
4538 _dbus_assert_not_reached ("hello message failed");
4540 if (!check_add_match_all (context, bar))
4541 _dbus_assert_not_reached ("AddMatch message failed");
4543 baz = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4545 _dbus_assert_not_reached ("could not alloc connection");
4547 if (!bus_setup_debug_client (baz))
4548 _dbus_assert_not_reached ("could not set up connection");
4550 spin_connection_until_authenticated (context, baz);
4552 if (!check_hello_message (context, baz))
4553 _dbus_assert_not_reached ("hello message failed");
4555 if (!check_add_match_all (context, baz))
4556 _dbus_assert_not_reached ("AddMatch message failed");
4558 #ifdef DBUS_WIN_FIXME
4559 _dbus_warn("TODO: testing of GetConnectionUnixUser message skipped for now\n");
4560 _dbus_warn("TODO: testing of GetConnectionUnixProcessID message skipped for now\n");
4562 if (!check_get_connection_unix_user (context, baz))
4563 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
4565 if (!check_get_connection_unix_process_id (context, baz))
4566 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
4569 if (!check_list_services (context, baz))
4570 _dbus_assert_not_reached ("ListActivatableNames message failed");
4572 if (!check_no_leftovers (context))
4574 _dbus_warn ("Messages were left over after setting up initial connections\n");
4575 _dbus_assert_not_reached ("initial connection setup failed");
4578 check1_try_iterations (context, "create_and_hello",
4579 check_hello_connection);
4581 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
4582 check_nonexistent_service_no_auto_start);
4584 #ifdef DBUS_WIN_FIXME
4585 _dbus_warn("TODO: dispatch.c segfault_service_no_auto_start test\n");
4587 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
4588 check_segfault_service_no_auto_start);
4591 check2_try_iterations (context, foo, "existent_service_no_auto_start",
4592 check_existent_service_no_auto_start);
4594 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
4595 check_nonexistent_service_auto_start);
4598 #ifdef DBUS_WIN_FIXME
4599 _dbus_warn("TODO: dispatch.c segfault_service_auto_start test\n");
4601 /* only do the segfault test if we are not using the launcher */
4602 check2_try_iterations (context, foo, "segfault_service_auto_start",
4603 check_segfault_service_auto_start);
4606 /* only do the shell fail test if we are not using the launcher */
4607 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
4608 check_shell_fail_service_auto_start);
4610 /* specific to launcher */
4612 if (!check_launch_service_file_missing (context, foo))
4613 _dbus_assert_not_reached ("did not get service file not found error");
4616 /* Note: need to resolve some issues with the testing code in order to run
4617 * this in oom (handle that we sometimes don't get replies back from the bus
4618 * when oom happens, without blocking the test).
4620 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
4621 check_existent_service_auto_start);
4624 if (!check_existent_service_auto_start (context, foo))
4625 _dbus_assert_not_reached ("existent service auto start failed");
4627 if (!check_shell_service_success_auto_start (context, foo))
4628 _dbus_assert_not_reached ("shell success service auto start failed");
4630 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
4632 kill_client_connection_unchecked (foo);
4633 kill_client_connection_unchecked (bar);
4634 kill_client_connection_unchecked (baz);
4636 bus_context_unref (context);
4642 bus_dispatch_test_conf_fail (const DBusString *test_data_dir,
4643 const char *filename)
4645 BusContext *context;
4646 DBusConnection *foo;
4649 /* save the config name for the activation helper */
4650 if (!setenv_TEST_LAUNCH_HELPER_CONFIG (test_data_dir, filename))
4651 _dbus_assert_not_reached ("no memory setting TEST_LAUNCH_HELPER_CONFIG");
4653 dbus_error_init (&error);
4655 context = bus_context_new_test (test_data_dir, filename);
4656 if (context == NULL)
4659 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4661 _dbus_assert_not_reached ("could not alloc connection");
4663 if (!bus_setup_debug_client (foo))
4664 _dbus_assert_not_reached ("could not set up connection");
4666 spin_connection_until_authenticated (context, foo);
4668 if (!check_hello_message (context, foo))
4669 _dbus_assert_not_reached ("hello message failed");
4671 if (!check_double_hello_message (context, foo))
4672 _dbus_assert_not_reached ("double hello message failed");
4674 if (!check_add_match_all (context, foo))
4675 _dbus_assert_not_reached ("AddMatch message failed");
4677 /* this only tests the activation.c user check */
4678 if (!check_launch_service_user_missing (context, foo))
4679 _dbus_assert_not_reached ("user missing did not trigger error");
4681 /* this only tests the desktop.c exec check */
4682 if (!check_launch_service_exec_missing (context, foo))
4683 _dbus_assert_not_reached ("exec missing did not trigger error");
4685 /* this only tests the desktop.c service check */
4686 if (!check_launch_service_service_missing (context, foo))
4687 _dbus_assert_not_reached ("service missing did not trigger error");
4689 _dbus_verbose ("Disconnecting foo\n");
4691 kill_client_connection_unchecked (foo);
4693 bus_context_unref (context);
4699 bus_dispatch_test (const DBusString *test_data_dir)
4701 /* run normal activation tests */
4702 _dbus_verbose ("Normal activation tests\n");
4703 if (!bus_dispatch_test_conf (test_data_dir,
4704 "valid-config-files/debug-allow-all.conf", FALSE))
4708 _dbus_warn("Info: Launch helper activation tests skipped because launch-helper is not supported yet\n");
4710 /* run launch-helper activation tests */
4711 _dbus_verbose ("Launch helper activation tests\n");
4712 if (!bus_dispatch_test_conf (test_data_dir,
4713 "valid-config-files-system/debug-allow-all-pass.conf", TRUE))
4716 /* run select launch-helper activation tests on broken service files */
4717 if (!bus_dispatch_test_conf_fail (test_data_dir,
4718 "valid-config-files-system/debug-allow-all-fail.conf"))
4726 bus_dispatch_sha1_test (const DBusString *test_data_dir)
4728 BusContext *context;
4729 DBusConnection *foo;
4732 dbus_error_init (&error);
4734 /* Test SHA1 authentication */
4735 _dbus_verbose ("Testing SHA1 context\n");
4737 context = bus_context_new_test (test_data_dir,
4738 "valid-config-files/debug-allow-all-sha1.conf");
4739 if (context == NULL)
4742 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4744 _dbus_assert_not_reached ("could not alloc connection");
4746 if (!bus_setup_debug_client (foo))
4747 _dbus_assert_not_reached ("could not set up connection");
4749 spin_connection_until_authenticated (context, foo);
4751 if (!check_hello_message (context, foo))
4752 _dbus_assert_not_reached ("hello message failed");
4754 if (!check_add_match_all (context, foo))
4755 _dbus_assert_not_reached ("addmatch message failed");
4757 if (!check_no_leftovers (context))
4759 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
4760 _dbus_assert_not_reached ("initial connection setup failed");
4763 check1_try_iterations (context, "create_and_hello_sha1",
4764 check_hello_connection);
4766 kill_client_connection_unchecked (foo);
4768 bus_context_unref (context);
4773 #ifdef HAVE_UNIX_FD_PASSING
4776 bus_unix_fds_passing_test(const DBusString *test_data_dir)
4778 BusContext *context;
4779 DBusConnection *foo, *bar;
4782 int one[2], two[2], x, y, z;
4785 dbus_error_init (&error);
4787 context = bus_context_new_test (test_data_dir, "valid-config-files/debug-allow-all.conf");
4788 if (context == NULL)
4789 _dbus_assert_not_reached ("could not alloc context");
4791 foo = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4793 _dbus_assert_not_reached ("could not alloc connection");
4795 if (!bus_setup_debug_client (foo))
4796 _dbus_assert_not_reached ("could not set up connection");
4798 spin_connection_until_authenticated (context, foo);
4800 if (!check_hello_message (context, foo))
4801 _dbus_assert_not_reached ("hello message failed");
4803 if (!check_add_match_all (context, foo))
4804 _dbus_assert_not_reached ("AddMatch message failed");
4806 bar = dbus_connection_open_private (TEST_DEBUG_PIPE, &error);
4808 _dbus_assert_not_reached ("could not alloc connection");
4810 if (!bus_setup_debug_client (bar))
4811 _dbus_assert_not_reached ("could not set up connection");
4813 spin_connection_until_authenticated (context, bar);
4815 if (!check_hello_message (context, bar))
4816 _dbus_assert_not_reached ("hello message failed");
4818 if (!check_add_match_all (context, bar))
4819 _dbus_assert_not_reached ("AddMatch message failed");
4821 if (!(m = dbus_message_new_signal("/", "a.b.c", "d")))
4822 _dbus_assert_not_reached ("could not alloc message");
4824 if (!(_dbus_full_duplex_pipe(one, one+1, TRUE, &error)))
4825 _dbus_assert_not_reached("Failed to allocate pipe #1");
4827 if (!(_dbus_full_duplex_pipe(two, two+1, TRUE, &error)))
4828 _dbus_assert_not_reached("Failed to allocate pipe #2");
4830 if (!dbus_message_append_args(m,
4831 DBUS_TYPE_UNIX_FD, one,
4832 DBUS_TYPE_UNIX_FD, two,
4833 DBUS_TYPE_UNIX_FD, two,
4835 _dbus_assert_not_reached("Failed to attach fds.");
4837 if (!_dbus_close(one[0], &error))
4838 _dbus_assert_not_reached("Failed to close pipe #1 ");
4839 if (!_dbus_close(two[0], &error))
4840 _dbus_assert_not_reached("Failed to close pipe #2 ");
4842 if (!(dbus_connection_can_send_type(foo, DBUS_TYPE_UNIX_FD)))
4843 _dbus_assert_not_reached("Connection cannot do fd passing");
4845 if (!(dbus_connection_can_send_type(bar, DBUS_TYPE_UNIX_FD)))
4846 _dbus_assert_not_reached("Connection cannot do fd passing");
4848 if (!dbus_connection_send (foo, m, NULL))
4849 _dbus_assert_not_reached("Failed to send fds");
4851 dbus_message_unref(m);
4853 bus_test_run_clients_loop (SEND_PENDING (foo));
4855 bus_test_run_everything (context);
4857 block_connection_until_message_from_bus (context, foo, "unix fd reception on foo");
4859 if (!(m = pop_message_waiting_for_memory (foo)))
4860 _dbus_assert_not_reached("Failed to receive msg");
4862 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4863 _dbus_assert_not_reached("bogus message received");
4865 dbus_message_unref(m);
4867 block_connection_until_message_from_bus (context, bar, "unix fd reception on bar");
4869 if (!(m = pop_message_waiting_for_memory (bar)))
4870 _dbus_assert_not_reached("Failed to receive msg");
4872 if (!dbus_message_is_signal(m, "a.b.c", "d"))
4873 _dbus_assert_not_reached("bogus message received");
4875 if (!dbus_message_get_args(m,
4877 DBUS_TYPE_UNIX_FD, &x,
4878 DBUS_TYPE_UNIX_FD, &y,
4879 DBUS_TYPE_UNIX_FD, &z,
4881 _dbus_assert_not_reached("Failed to parse fds.");
4883 dbus_message_unref(m);
4885 if (write(x, "X", 1) != 1)
4886 _dbus_assert_not_reached("Failed to write to pipe #1");
4887 if (write(y, "Y", 1) != 1)
4888 _dbus_assert_not_reached("Failed to write to pipe #2");
4889 if (write(z, "Z", 1) != 1)
4890 _dbus_assert_not_reached("Failed to write to pipe #2/2nd fd");
4892 if (!_dbus_close(x, &error))
4893 _dbus_assert_not_reached("Failed to close pipe #1/other side ");
4894 if (!_dbus_close(y, &error))
4895 _dbus_assert_not_reached("Failed to close pipe #2/other side ");
4896 if (!_dbus_close(z, &error))
4897 _dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
4899 if (read(one[1], &r, 1) != 1 || r != 'X')
4900 _dbus_assert_not_reached("Failed to read value from pipe.");
4901 if (read(two[1], &r, 1) != 1 || r != 'Y')
4902 _dbus_assert_not_reached("Failed to read value from pipe.");
4903 if (read(two[1], &r, 1) != 1 || r != 'Z')
4904 _dbus_assert_not_reached("Failed to read value from pipe.");
4906 if (!_dbus_close(one[1], &error))
4907 _dbus_assert_not_reached("Failed to close pipe #1 ");
4908 if (!_dbus_close(two[1], &error))
4909 _dbus_assert_not_reached("Failed to close pipe #2 ");
4911 _dbus_verbose ("Disconnecting foo\n");
4912 kill_client_connection_unchecked (foo);
4914 _dbus_verbose ("Disconnecting bar\n");
4915 kill_client_connection_unchecked (bar);
4917 bus_context_unref (context);
4923 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */