1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2004 Imendio HB
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "connection.h"
30 #include "activation.h"
35 #include <dbus/dbus-internals.h>
39 send_one_message (DBusConnection *connection,
41 DBusConnection *sender,
42 DBusConnection *addressed_recipient,
44 BusTransaction *transaction,
47 if (!bus_context_check_security_policy (context, transaction,
53 return TRUE; /* silently don't send it */
55 if (!bus_transaction_send (transaction,
67 bus_dispatch_matches (BusTransaction *transaction,
68 DBusConnection *sender,
69 DBusConnection *addressed_recipient,
74 BusConnections *connections;
76 BusMatchmaker *matchmaker;
80 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
82 /* sender and recipient can both be NULL for the bus driver,
83 * or for signals with no particular recipient
86 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
87 _dbus_assert (dbus_message_get_sender (message) != NULL);
89 connections = bus_transaction_get_connections (transaction);
91 dbus_error_init (&tmp_error);
92 context = bus_transaction_get_context (transaction);
93 matchmaker = bus_context_get_matchmaker (context);
96 if (!bus_matchmaker_get_recipients (matchmaker, connections,
97 sender, addressed_recipient, message,
104 link = _dbus_list_get_first_link (&recipients);
107 DBusConnection *dest;
111 if (!send_one_message (dest, context, sender, addressed_recipient,
112 message, transaction, &tmp_error))
115 link = _dbus_list_get_next_link (&recipients, link);
118 _dbus_list_clear (&recipients);
120 if (dbus_error_is_set (&tmp_error))
122 dbus_move_error (&tmp_error, error);
129 static DBusHandlerResult
130 bus_dispatch (DBusConnection *connection,
131 DBusMessage *message)
133 const char *sender, *service_name;
135 BusTransaction *transaction;
137 DBusHandlerResult result;
138 DBusConnection *addressed_recipient;
140 result = DBUS_HANDLER_RESULT_HANDLED;
143 addressed_recipient = NULL;
144 dbus_error_init (&error);
146 context = bus_connection_get_context (connection);
147 _dbus_assert (context != NULL);
149 /* If we can't even allocate an OOM error, we just go to sleep
152 while (!bus_connection_preallocate_oom_error (connection))
153 _dbus_wait_for_memory ();
155 /* Ref connection in case we disconnect it at some point in here */
156 dbus_connection_ref (connection);
158 service_name = dbus_message_get_destination (message);
160 #ifdef DBUS_ENABLE_VERBOSE_MODE
162 const char *interface_name, *member_name, *error_name;
164 interface_name = dbus_message_get_interface (message);
165 member_name = dbus_message_get_member (message);
166 error_name = dbus_message_get_error_name (message);
168 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
169 interface_name ? interface_name : "(no interface)",
170 member_name ? member_name : "(no member)",
171 error_name ? error_name : "(no error name)",
172 service_name ? service_name : "peer");
174 #endif /* DBUS_ENABLE_VERBOSE_MODE */
176 /* If service_name is NULL, if it's a signal we send it to all
177 * connections with a match rule. If it's not a signal, there
178 * are some special cases here but mostly we just bail out.
180 if (service_name == NULL)
182 if (dbus_message_is_signal (message,
183 DBUS_INTERFACE_LOCAL,
186 bus_connection_disconnected (connection);
190 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
192 /* DBusConnection also handles some of these automatically, we leave
195 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
200 /* Create our transaction */
201 transaction = bus_transaction_new (context);
202 if (transaction == NULL)
204 BUS_SET_OOM (&error);
208 /* Assign a sender to the message */
209 if (bus_connection_is_active (connection))
211 sender = bus_connection_get_name (connection);
212 _dbus_assert (sender != NULL);
214 if (!dbus_message_set_sender (message, sender))
216 BUS_SET_OOM (&error);
220 /* We need to refetch the service name here, because
221 * dbus_message_set_sender can cause the header to be
222 * reallocated, and thus the service_name pointer will become
225 service_name = dbus_message_get_destination (message);
229 strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
231 if (!bus_context_check_security_policy (context, transaction,
232 connection, NULL, NULL, message, &error))
234 _dbus_verbose ("Security policy rejected message\n");
238 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
239 if (!bus_driver_handle_message (connection, transaction, message, &error))
242 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
244 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
245 dbus_connection_close (connection);
248 else if (service_name != NULL) /* route to named service */
250 DBusString service_string;
252 BusRegistry *registry;
254 _dbus_assert (service_name != NULL);
256 registry = bus_connection_get_registry (connection);
258 _dbus_string_init_const (&service_string, service_name);
259 service = bus_registry_lookup (registry, &service_string);
261 if (service == NULL && dbus_message_get_auto_start (message))
263 BusActivation *activation;
264 /* We can't do the security policy check here, since the addressed
265 * recipient service doesn't exist yet. We do it before sending the
266 * message after the service has been created.
268 activation = bus_connection_get_activation (connection);
270 if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
271 message, service_name, &error))
273 _DBUS_ASSERT_ERROR_IS_SET (&error);
274 _dbus_verbose ("bus_activation_activate_service() failed\n");
275 ("Failed: %s\n", error.name);
281 else if (service == NULL)
283 dbus_set_error (&error,
284 DBUS_ERROR_NAME_HAS_NO_OWNER,
285 "Name \"%s\" does not exist",
291 addressed_recipient = bus_service_get_primary_owner (service);
292 _dbus_assert (addressed_recipient != NULL);
294 if (!bus_context_check_security_policy (context, transaction,
295 connection, addressed_recipient,
300 /* Dispatch the message */
301 if (!bus_transaction_send (transaction, addressed_recipient, message))
303 BUS_SET_OOM (&error);
309 /* Now match the messages against any match rules, which will send
310 * out signals and such. addressed_recipient may == NULL.
312 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
316 if (dbus_error_is_set (&error))
318 if (!dbus_connection_get_is_connected (connection))
320 /* If we disconnected it, we won't bother to send it any error
323 _dbus_verbose ("Not sending error to connection we disconnected\n");
325 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
327 bus_connection_send_oom_error (connection, message);
329 /* cancel transaction due to OOM */
330 if (transaction != NULL)
332 bus_transaction_cancel_and_free (transaction);
338 /* Try to send the real error, if no mem to do that, send
341 _dbus_assert (transaction != NULL);
342 if (!bus_transaction_send_error_reply (transaction, connection,
345 bus_connection_send_oom_error (connection, message);
347 /* cancel transaction due to OOM */
348 if (transaction != NULL)
350 bus_transaction_cancel_and_free (transaction);
357 dbus_error_free (&error);
360 if (transaction != NULL)
362 bus_transaction_execute_and_free (transaction);
365 dbus_connection_unref (connection);
370 static DBusHandlerResult
371 bus_dispatch_message_filter (DBusConnection *connection,
372 DBusMessage *message,
375 return bus_dispatch (connection, message);
379 bus_dispatch_add_connection (DBusConnection *connection)
381 if (!dbus_connection_add_filter (connection,
382 bus_dispatch_message_filter,
390 bus_dispatch_remove_connection (DBusConnection *connection)
392 /* Here we tell the bus driver that we want to get off. */
393 bus_driver_remove_connection (connection);
395 dbus_connection_remove_filter (connection,
396 bus_dispatch_message_filter,
400 #ifdef DBUS_BUILD_TESTS
404 /* This is used to know whether we need to block in order to finish
405 * sending a message, or whether the initial dbus_connection_send()
406 * already flushed the queue.
408 #define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
410 typedef dbus_bool_t (* Check1Func) (BusContext *context);
411 typedef dbus_bool_t (* Check2Func) (BusContext *context,
412 DBusConnection *connection);
414 static dbus_bool_t check_no_leftovers (BusContext *context);
417 block_connection_until_message_from_bus (BusContext *context,
418 DBusConnection *connection,
419 const char *what_is_expected)
421 _dbus_verbose ("expecting: %s\n", what_is_expected);
423 while (dbus_connection_get_dispatch_status (connection) ==
424 DBUS_DISPATCH_COMPLETE &&
425 dbus_connection_get_is_connected (connection))
427 bus_test_run_bus_loop (context, TRUE);
428 bus_test_run_clients_loop (FALSE);
433 spin_connection_until_authenticated (BusContext *context,
434 DBusConnection *connection)
436 _dbus_verbose ("Spinning to auth connection %p\n", connection);
437 while (!dbus_connection_get_is_authenticated (connection) &&
438 dbus_connection_get_is_connected (connection))
440 bus_test_run_bus_loop (context, FALSE);
441 bus_test_run_clients_loop (FALSE);
443 _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
446 /* compensate for fact that pop_message() can return #NULL due to OOM */
448 pop_message_waiting_for_memory (DBusConnection *connection)
450 while (dbus_connection_get_dispatch_status (connection) ==
451 DBUS_DISPATCH_NEED_MEMORY)
452 _dbus_wait_for_memory ();
454 return dbus_connection_pop_message (connection);
458 borrow_message_waiting_for_memory (DBusConnection *connection)
460 while (dbus_connection_get_dispatch_status (connection) ==
461 DBUS_DISPATCH_NEED_MEMORY)
462 _dbus_wait_for_memory ();
464 return dbus_connection_borrow_message (connection);
468 warn_unexpected_real (DBusConnection *connection,
469 DBusMessage *message,
470 const char *expected,
471 const char *function,
475 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
477 dbus_message_get_interface (message) ?
478 dbus_message_get_interface (message) : "(unset)",
479 dbus_message_get_member (message) ?
480 dbus_message_get_member (message) : "(unset)",
481 dbus_message_get_error_name (message) ?
482 dbus_message_get_error_name (message) : "(unset)",
486 _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
487 function, line, connection, expected);
490 #define warn_unexpected(connection, message, expected) \
491 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
494 verbose_message_received (DBusConnection *connection,
495 DBusMessage *message)
497 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
498 dbus_message_get_interface (message) ?
499 dbus_message_get_interface (message) : "(unset)",
500 dbus_message_get_member (message) ?
501 dbus_message_get_member (message) : "(unset)",
502 dbus_message_get_error_name (message) ?
503 dbus_message_get_error_name (message) : "(unset)",
516 ServiceInfoKind expected_kind;
517 const char *expected_service_name;
519 DBusConnection *skip_connection;
520 } CheckServiceOwnerChangedData;
523 check_service_owner_changed_foreach (DBusConnection *connection,
526 CheckServiceOwnerChangedData *d = data;
527 DBusMessage *message;
529 const char *service_name, *old_owner, *new_owner;
531 if (d->expected_kind == SERVICE_CREATED
532 && connection == d->skip_connection)
535 dbus_error_init (&error);
538 message = pop_message_waiting_for_memory (connection);
541 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
542 connection, "NameOwnerChanged");
545 else if (!dbus_message_is_signal (message,
549 warn_unexpected (connection, message, "NameOwnerChanged");
555 reget_service_info_data:
560 dbus_message_get_args (message, &error,
561 DBUS_TYPE_STRING, &service_name,
562 DBUS_TYPE_STRING, &old_owner,
563 DBUS_TYPE_STRING, &new_owner,
566 if (dbus_error_is_set (&error))
568 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
570 dbus_error_free (&error);
571 _dbus_wait_for_memory ();
572 goto reget_service_info_data;
576 _dbus_warn ("Did not get the expected arguments\n");
581 if ((d->expected_kind == SERVICE_CREATED && ( old_owner[0] || !new_owner[0]))
582 || (d->expected_kind == OWNER_CHANGED && (!old_owner[0] || !new_owner[0]))
583 || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] || new_owner[0])))
585 _dbus_warn ("inconsistent NameOwnerChanged arguments");
589 if (strcmp (service_name, d->expected_service_name) != 0)
591 _dbus_warn ("expected info on service %s, got info on %s\n",
592 d->expected_service_name,
597 if (*service_name == ':' && new_owner[0]
598 && strcmp (service_name, new_owner) != 0)
600 _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
601 service_name, old_owner, new_owner);
609 dbus_error_free (&error);
612 dbus_message_unref (message);
619 kill_client_connection (BusContext *context,
620 DBusConnection *connection)
624 CheckServiceOwnerChangedData socd;
626 _dbus_verbose ("killing connection %p\n", connection);
628 s = dbus_bus_get_unique_name (connection);
629 _dbus_assert (s != NULL);
631 while ((base_service = _dbus_strdup (s)) == NULL)
632 _dbus_wait_for_memory ();
634 dbus_connection_ref (connection);
636 /* kick in the disconnect handler that unrefs the connection */
637 dbus_connection_close (connection);
639 bus_test_run_everything (context);
641 _dbus_assert (bus_test_client_listed (connection));
643 /* Run disconnect handler in test.c */
644 if (bus_connection_dispatch_one_message (connection))
645 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
647 _dbus_assert (!dbus_connection_get_is_connected (connection));
648 dbus_connection_unref (connection);
650 _dbus_assert (!bus_test_client_listed (connection));
652 socd.expected_kind = SERVICE_DELETED;
653 socd.expected_service_name = base_service;
655 socd.skip_connection = NULL;
657 bus_test_clients_foreach (check_service_owner_changed_foreach,
660 dbus_free (base_service);
663 _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
665 if (!check_no_leftovers (context))
666 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
670 kill_client_connection_unchecked (DBusConnection *connection)
672 /* This kills the connection without expecting it to affect
673 * the rest of the bus.
675 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
677 dbus_connection_ref (connection);
678 dbus_connection_close (connection);
679 /* dispatching disconnect handler will unref once */
680 if (bus_connection_dispatch_one_message (connection))
681 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
683 _dbus_assert (!bus_test_client_listed (connection));
684 dbus_connection_unref (connection);
690 } CheckNoMessagesData;
693 check_no_messages_foreach (DBusConnection *connection,
696 CheckNoMessagesData *d = data;
697 DBusMessage *message;
699 message = pop_message_waiting_for_memory (connection);
702 warn_unexpected (connection, message, "no messages");
708 dbus_message_unref (message);
713 check_no_leftovers (BusContext *context)
715 CheckNoMessagesData nmd;
718 bus_test_clients_foreach (check_no_messages_foreach,
723 _dbus_verbose ("%s: leftover message found\n",
724 _DBUS_FUNCTION_NAME);
731 /* returns TRUE if the correct thing happens,
732 * but the correct thing may include OOM errors.
735 check_hello_message (BusContext *context,
736 DBusConnection *connection)
738 DBusMessage *message;
739 DBusMessage *name_message;
740 dbus_uint32_t serial;
744 const char *acquired;
747 dbus_error_init (&error);
753 _dbus_verbose ("check_hello_message for %p\n", connection);
755 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
763 dbus_connection_ref (connection); /* because we may get disconnected */
765 if (!dbus_connection_send (connection, message, &serial))
767 dbus_message_unref (message);
768 dbus_connection_unref (connection);
772 _dbus_assert (dbus_message_has_signature (message, ""));
774 dbus_message_unref (message);
777 if (!dbus_connection_get_is_connected (connection))
779 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
781 dbus_connection_unref (connection);
786 /* send our message */
787 bus_test_run_clients_loop (SEND_PENDING (connection));
789 if (!dbus_connection_get_is_connected (connection))
791 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
793 dbus_connection_unref (connection);
798 block_connection_until_message_from_bus (context, connection, "reply to Hello");
800 if (!dbus_connection_get_is_connected (connection))
802 _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
804 dbus_connection_unref (connection);
809 dbus_connection_unref (connection);
811 message = pop_message_waiting_for_memory (connection);
814 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
815 "Hello", serial, connection);
819 verbose_message_received (connection, message);
821 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
823 _dbus_warn ("Message has wrong sender %s\n",
824 dbus_message_get_sender (message) ?
825 dbus_message_get_sender (message) : "(none)");
829 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
831 if (dbus_message_is_error (message,
832 DBUS_ERROR_NO_MEMORY))
834 ; /* good, this is a valid response */
838 warn_unexpected (connection, message, "not this error");
845 CheckServiceOwnerChangedData socd;
847 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
849 ; /* good, expected */
853 warn_unexpected (connection, message, "method return for Hello");
858 retry_get_hello_name:
859 if (!dbus_message_get_args (message, &error,
860 DBUS_TYPE_STRING, &name,
863 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
865 _dbus_verbose ("no memory to get service name arg from hello\n");
866 dbus_error_free (&error);
867 _dbus_wait_for_memory ();
868 goto retry_get_hello_name;
872 _dbus_assert (dbus_error_is_set (&error));
873 _dbus_warn ("Did not get the expected single string argument to hello\n");
878 _dbus_verbose ("Got hello name: %s\n", name);
880 while (!dbus_bus_set_unique_name (connection, name))
881 _dbus_wait_for_memory ();
883 socd.expected_kind = SERVICE_CREATED;
884 socd.expected_service_name = name;
886 socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
887 bus_test_clients_foreach (check_service_owner_changed_foreach,
893 name_message = message;
894 /* Client should also have gotten ServiceAcquired */
896 message = pop_message_waiting_for_memory (connection);
899 _dbus_warn ("Expecting %s, got nothing\n",
903 if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
906 _dbus_warn ("Expecting %s, got smthg else\n",
911 retry_get_acquired_name:
912 if (!dbus_message_get_args (message, &error,
913 DBUS_TYPE_STRING, &acquired,
916 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
918 _dbus_verbose ("no memory to get service name arg from acquired\n");
919 dbus_error_free (&error);
920 _dbus_wait_for_memory ();
921 goto retry_get_acquired_name;
925 _dbus_assert (dbus_error_is_set (&error));
926 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
931 _dbus_verbose ("Got acquired name: %s\n", acquired);
933 if (strcmp (acquired, name) != 0)
935 _dbus_warn ("Acquired name is %s but expected %s\n",
942 if (!check_no_leftovers (context))
948 _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
950 dbus_error_free (&error);
953 dbus_message_unref (message);
956 dbus_message_unref (name_message);
961 /* returns TRUE if the correct thing happens,
962 * but the correct thing may include OOM errors.
965 check_double_hello_message (BusContext *context,
966 DBusConnection *connection)
968 DBusMessage *message;
969 dbus_uint32_t serial;
974 dbus_error_init (&error);
977 _dbus_verbose ("check_double_hello_message for %p\n", connection);
979 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
987 if (!dbus_connection_send (connection, message, &serial))
989 dbus_message_unref (message);
993 dbus_message_unref (message);
996 /* send our message */
997 bus_test_run_clients_loop (SEND_PENDING (connection));
999 dbus_connection_ref (connection); /* because we may get disconnected */
1000 block_connection_until_message_from_bus (context, connection, "reply to Hello");
1002 if (!dbus_connection_get_is_connected (connection))
1004 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1006 dbus_connection_unref (connection);
1011 dbus_connection_unref (connection);
1013 message = pop_message_waiting_for_memory (connection);
1014 if (message == NULL)
1016 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1017 "Hello", serial, connection);
1021 verbose_message_received (connection, message);
1023 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1025 _dbus_warn ("Message has wrong sender %s\n",
1026 dbus_message_get_sender (message) ?
1027 dbus_message_get_sender (message) : "(none)");
1031 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1033 warn_unexpected (connection, message, "method return for Hello");
1037 if (!check_no_leftovers (context))
1043 dbus_error_free (&error);
1046 dbus_message_unref (message);
1051 /* returns TRUE if the correct thing happens,
1052 * but the correct thing may include OOM errors.
1055 check_get_connection_unix_user (BusContext *context,
1056 DBusConnection *connection)
1058 DBusMessage *message;
1059 dbus_uint32_t serial;
1062 const char *base_service_name;
1066 dbus_error_init (&error);
1069 _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1071 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1073 DBUS_INTERFACE_DBUS,
1074 "GetConnectionUnixUser");
1076 if (message == NULL)
1079 base_service_name = dbus_bus_get_unique_name (connection);
1081 if (!dbus_message_append_args (message,
1082 DBUS_TYPE_STRING, &base_service_name,
1085 dbus_message_unref (message);
1089 if (!dbus_connection_send (connection, message, &serial))
1091 dbus_message_unref (message);
1095 /* send our message */
1096 bus_test_run_clients_loop (SEND_PENDING (connection));
1098 dbus_message_unref (message);
1101 dbus_connection_ref (connection); /* because we may get disconnected */
1102 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1104 if (!dbus_connection_get_is_connected (connection))
1106 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1108 dbus_connection_unref (connection);
1113 dbus_connection_unref (connection);
1115 message = pop_message_waiting_for_memory (connection);
1116 if (message == NULL)
1118 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1119 "GetConnectionUnixUser", serial, connection);
1123 verbose_message_received (connection, message);
1125 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1127 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1129 ; /* good, this is a valid response */
1133 warn_unexpected (connection, message, "not this error");
1140 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1142 ; /* good, expected */
1146 warn_unexpected (connection, message,
1147 "method_return for GetConnectionUnixUser");
1154 if (!dbus_message_get_args (message, &error,
1155 DBUS_TYPE_UINT32, &uid,
1158 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1160 _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1161 dbus_error_free (&error);
1162 _dbus_wait_for_memory ();
1163 goto retry_get_property;
1167 _dbus_assert (dbus_error_is_set (&error));
1168 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1174 if (!check_no_leftovers (context))
1180 dbus_error_free (&error);
1183 dbus_message_unref (message);
1188 /* returns TRUE if the correct thing happens,
1189 * but the correct thing may include OOM errors.
1192 check_get_connection_unix_process_id (BusContext *context,
1193 DBusConnection *connection)
1195 DBusMessage *message;
1196 dbus_uint32_t serial;
1199 const char *base_service_name;
1203 dbus_error_init (&error);
1206 _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1208 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1210 DBUS_INTERFACE_DBUS,
1211 "GetConnectionUnixProcessID");
1213 if (message == NULL)
1216 base_service_name = dbus_bus_get_unique_name (connection);
1218 if (!dbus_message_append_args (message,
1219 DBUS_TYPE_STRING, &base_service_name,
1222 dbus_message_unref (message);
1226 if (!dbus_connection_send (connection, message, &serial))
1228 dbus_message_unref (message);
1232 /* send our message */
1233 bus_test_run_clients_loop (SEND_PENDING (connection));
1235 dbus_message_unref (message);
1238 dbus_connection_ref (connection); /* because we may get disconnected */
1239 block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
1241 if (!dbus_connection_get_is_connected (connection))
1243 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1245 dbus_connection_unref (connection);
1250 dbus_connection_unref (connection);
1252 message = pop_message_waiting_for_memory (connection);
1253 if (message == NULL)
1255 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1256 "GetConnectionUnixProcessID", serial, connection);
1260 verbose_message_received (connection, message);
1262 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1264 if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1266 ; /* good, this is a valid response */
1270 warn_unexpected (connection, message, "not this error");
1277 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1279 ; /* good, expected */
1283 warn_unexpected (connection, message,
1284 "method_return for GetConnectionUnixProcessID");
1291 if (!dbus_message_get_args (message, &error,
1292 DBUS_TYPE_UINT32, &pid,
1295 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1297 _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1298 dbus_error_free (&error);
1299 _dbus_wait_for_memory ();
1300 goto retry_get_property;
1304 _dbus_assert (dbus_error_is_set (&error));
1305 _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1310 /* test if returned pid is the same as our own pid
1312 * @todo It would probably be good to restructure the tests
1313 * in a way so our parent is the bus that we're testing
1314 * cause then we can test that the pid returned matches
1317 if (pid != (dbus_uint32_t) _dbus_getpid ())
1319 _dbus_assert (dbus_error_is_set (&error));
1320 _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1326 if (!check_no_leftovers (context))
1332 dbus_error_free (&error);
1335 dbus_message_unref (message);
1340 /* returns TRUE if the correct thing happens,
1341 * but the correct thing may include OOM errors.
1344 check_add_match_all (BusContext *context,
1345 DBusConnection *connection)
1347 DBusMessage *message;
1349 dbus_uint32_t serial;
1351 const char *empty = "";
1354 dbus_error_init (&error);
1357 _dbus_verbose ("check_add_match_all for %p\n", connection);
1359 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1361 DBUS_INTERFACE_DBUS,
1364 if (message == NULL)
1367 /* empty string match rule matches everything */
1368 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
1371 dbus_message_unref (message);
1375 if (!dbus_connection_send (connection, message, &serial))
1377 dbus_message_unref (message);
1381 dbus_message_unref (message);
1384 dbus_connection_ref (connection); /* because we may get disconnected */
1386 /* send our message */
1387 bus_test_run_clients_loop (SEND_PENDING (connection));
1389 if (!dbus_connection_get_is_connected (connection))
1391 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1393 dbus_connection_unref (connection);
1398 block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
1400 if (!dbus_connection_get_is_connected (connection))
1402 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1404 dbus_connection_unref (connection);
1409 dbus_connection_unref (connection);
1411 message = pop_message_waiting_for_memory (connection);
1412 if (message == NULL)
1414 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1415 "AddMatch", serial, connection);
1419 verbose_message_received (connection, message);
1421 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1423 _dbus_warn ("Message has wrong sender %s\n",
1424 dbus_message_get_sender (message) ?
1425 dbus_message_get_sender (message) : "(none)");
1429 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1431 if (dbus_message_is_error (message,
1432 DBUS_ERROR_NO_MEMORY))
1434 ; /* good, this is a valid response */
1438 warn_unexpected (connection, message, "not this error");
1445 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1447 ; /* good, expected */
1448 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1452 warn_unexpected (connection, message, "method return for AddMatch");
1458 if (!check_no_leftovers (context))
1464 dbus_error_free (&error);
1467 dbus_message_unref (message);
1472 /* returns TRUE if the correct thing happens,
1473 * but the correct thing may include OOM errors.
1476 check_hello_connection (BusContext *context)
1478 DBusConnection *connection;
1481 dbus_error_init (&error);
1483 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
1484 if (connection == NULL)
1486 _DBUS_ASSERT_ERROR_IS_SET (&error);
1487 dbus_error_free (&error);
1491 if (!bus_setup_debug_client (connection))
1493 dbus_connection_close (connection);
1494 dbus_connection_unref (connection);
1498 spin_connection_until_authenticated (context, connection);
1500 if (!check_hello_message (context, connection))
1503 if (dbus_bus_get_unique_name (connection) == NULL)
1505 /* We didn't successfully register, so we can't
1506 * do the usual kill_client_connection() checks
1508 kill_client_connection_unchecked (connection);
1512 if (!check_add_match_all (context, connection))
1515 kill_client_connection (context, connection);
1521 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1523 /* returns TRUE if the correct thing happens,
1524 * but the correct thing may include OOM errors.
1527 check_nonexistent_service_no_auto_start (BusContext *context,
1528 DBusConnection *connection)
1530 DBusMessage *message;
1531 dbus_uint32_t serial;
1533 const char *nonexistent = NONEXISTENT_SERVICE_NAME;
1534 dbus_uint32_t flags;
1536 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1538 DBUS_INTERFACE_DBUS,
1539 "StartServiceByName");
1541 if (message == NULL)
1544 dbus_message_set_auto_start (message, FALSE);
1547 if (!dbus_message_append_args (message,
1548 DBUS_TYPE_STRING, &nonexistent,
1549 DBUS_TYPE_UINT32, &flags,
1552 dbus_message_unref (message);
1556 if (!dbus_connection_send (connection, message, &serial))
1558 dbus_message_unref (message);
1562 dbus_message_unref (message);
1565 bus_test_run_everything (context);
1566 block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
1567 bus_test_run_everything (context);
1569 if (!dbus_connection_get_is_connected (connection))
1571 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1577 message = pop_message_waiting_for_memory (connection);
1578 if (message == NULL)
1580 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1581 "StartServiceByName", serial, connection);
1585 verbose_message_received (connection, message);
1587 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1589 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1591 _dbus_warn ("Message has wrong sender %s\n",
1592 dbus_message_get_sender (message) ?
1593 dbus_message_get_sender (message) : "(none)");
1597 if (dbus_message_is_error (message,
1598 DBUS_ERROR_NO_MEMORY))
1600 ; /* good, this is a valid response */
1602 else if (dbus_message_is_error (message,
1603 DBUS_ERROR_SERVICE_UNKNOWN))
1605 ; /* good, this is expected also */
1609 warn_unexpected (connection, message, "not this error");
1615 _dbus_warn ("Did not expect to successfully activate %s\n",
1616 NONEXISTENT_SERVICE_NAME);
1624 dbus_message_unref (message);
1629 /* returns TRUE if the correct thing happens,
1630 * but the correct thing may include OOM errors.
1633 check_nonexistent_service_auto_start (BusContext *context,
1634 DBusConnection *connection)
1636 DBusMessage *message;
1637 dbus_uint32_t serial;
1640 message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1641 "/org/freedesktop/TestSuite",
1642 "org.freedesktop.TestSuite",
1645 if (message == NULL)
1648 if (!dbus_connection_send (connection, message, &serial))
1650 dbus_message_unref (message);
1654 dbus_message_unref (message);
1657 bus_test_run_everything (context);
1658 block_connection_until_message_from_bus (context, connection, "reply to Echo");
1659 bus_test_run_everything (context);
1661 if (!dbus_connection_get_is_connected (connection))
1663 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
1669 message = pop_message_waiting_for_memory (connection);
1671 if (message == NULL)
1673 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1674 "Echo message (auto activation)", serial, connection);
1678 verbose_message_received (connection, message);
1680 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1682 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1684 _dbus_warn ("Message has wrong sender %s\n",
1685 dbus_message_get_sender (message) ?
1686 dbus_message_get_sender (message) : "(none)");
1690 if (dbus_message_is_error (message,
1691 DBUS_ERROR_NO_MEMORY))
1693 ; /* good, this is a valid response */
1695 else if (dbus_message_is_error (message,
1696 DBUS_ERROR_SERVICE_UNKNOWN))
1698 ; /* good, this is expected also */
1702 warn_unexpected (connection, message, "not this error");
1708 _dbus_warn ("Did not expect to successfully activate %s\n",
1709 NONEXISTENT_SERVICE_NAME);
1717 dbus_message_unref (message);
1723 check_base_service_activated (BusContext *context,
1724 DBusConnection *connection,
1725 DBusMessage *initial_message,
1726 const char **base_service_p)
1728 DBusMessage *message;
1731 const char *base_service, *base_service_from_bus, *old_owner;
1735 dbus_error_init (&error);
1736 base_service = NULL;
1738 base_service_from_bus = NULL;
1740 message = initial_message;
1741 dbus_message_ref (message);
1743 if (dbus_message_is_signal (message,
1744 DBUS_INTERFACE_DBUS,
1745 "NameOwnerChanged"))
1747 CheckServiceOwnerChangedData socd;
1749 reget_service_name_arg:
1750 base_service = NULL;
1752 base_service_from_bus = NULL;
1754 if (!dbus_message_get_args (message, &error,
1755 DBUS_TYPE_STRING, &base_service,
1756 DBUS_TYPE_STRING, &old_owner,
1757 DBUS_TYPE_STRING, &base_service_from_bus,
1760 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1762 dbus_error_free (&error);
1763 _dbus_wait_for_memory ();
1764 goto reget_service_name_arg;
1768 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1769 "NameOwnerChanged (creation)",
1775 if (*base_service != ':')
1777 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1782 if (strcmp (base_service, base_service_from_bus) != 0)
1784 _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1785 base_service, base_service_from_bus);
1791 _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1796 socd.expected_kind = SERVICE_CREATED;
1797 socd.expected_service_name = base_service;
1798 socd.failed = FALSE;
1799 socd.skip_connection = connection;
1800 bus_test_clients_foreach (check_service_owner_changed_foreach,
1808 warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
1814 *base_service_p = base_service;
1820 dbus_message_unref (message);
1821 dbus_error_free (&error);
1827 check_service_activated (BusContext *context,
1828 DBusConnection *connection,
1829 const char *activated_name,
1830 const char *base_service_name,
1831 DBusMessage *initial_message)
1833 DBusMessage *message;
1836 dbus_uint32_t activation_result;
1840 dbus_error_init (&error);
1842 message = initial_message;
1843 dbus_message_ref (message);
1845 if (dbus_message_is_signal (message,
1846 DBUS_INTERFACE_DBUS,
1847 "NameOwnerChanged"))
1849 CheckServiceOwnerChangedData socd;
1850 const char *service_name, *base_service_from_bus, *old_owner;
1852 reget_service_name_arg:
1853 service_name = NULL;
1855 base_service_from_bus = NULL;
1857 if (!dbus_message_get_args (message, &error,
1858 DBUS_TYPE_STRING, &service_name,
1859 DBUS_TYPE_STRING, &old_owner,
1860 DBUS_TYPE_STRING, &base_service_from_bus,
1863 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1865 dbus_error_free (&error);
1866 _dbus_wait_for_memory ();
1867 goto reget_service_name_arg;
1871 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1872 "NameOwnerChanged (creation)",
1878 if (strcmp (service_name, activated_name) != 0)
1880 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1881 activated_name, service_name);
1885 if (strcmp (base_service_name, base_service_from_bus) != 0)
1887 _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1888 base_service_from_bus, base_service_name);
1894 _dbus_warn ("expected a %s, got a %s\n",
1895 "NameOwnerChanged (creation)",
1896 "NameOwnerChanged (change)");
1900 socd.expected_kind = SERVICE_CREATED;
1901 socd.skip_connection = connection;
1902 socd.failed = FALSE;
1903 socd.expected_service_name = service_name;
1904 bus_test_clients_foreach (check_service_owner_changed_foreach,
1910 dbus_message_unref (message);
1911 service_name = NULL;
1913 base_service_from_bus = NULL;
1915 message = pop_message_waiting_for_memory (connection);
1916 if (message == NULL)
1918 _dbus_warn ("Expected a reply to %s, got nothing\n",
1919 "StartServiceByName");
1925 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
1930 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1932 warn_unexpected (connection, message, "reply to StartServiceByName");
1937 activation_result = 0;
1938 if (!dbus_message_get_args (message, &error,
1939 DBUS_TYPE_UINT32, &activation_result,
1942 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1944 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1945 "StartServiceByName", error.message);
1949 dbus_error_free (&error);
1953 if (activation_result == DBUS_START_REPLY_SUCCESS)
1955 else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
1959 _dbus_warn ("Activation result was %u, no good.\n",
1965 dbus_message_unref (message);
1968 if (!check_no_leftovers (context))
1970 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1978 dbus_message_unref (message);
1979 dbus_error_free (&error);
1985 check_service_auto_activated (BusContext *context,
1986 DBusConnection *connection,
1987 const char *activated_name,
1988 const char *base_service_name,
1989 DBusMessage *initial_message)
1991 DBusMessage *message;
1997 dbus_error_init (&error);
1999 message = initial_message;
2000 dbus_message_ref (message);
2002 if (dbus_message_is_signal (message,
2003 DBUS_INTERFACE_DBUS,
2004 "NameOwnerChanged"))
2006 const char *service_name;
2007 CheckServiceOwnerChangedData socd;
2009 reget_service_name_arg:
2010 if (!dbus_message_get_args (message, &error,
2011 DBUS_TYPE_STRING, &service_name,
2014 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2016 dbus_error_free (&error);
2017 _dbus_wait_for_memory ();
2018 goto reget_service_name_arg;
2022 _dbus_warn ("Message %s doesn't have a service name: %s\n",
2025 dbus_error_free (&error);
2030 if (strcmp (service_name, activated_name) != 0)
2032 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
2033 activated_name, service_name);
2037 socd.expected_kind = SERVICE_CREATED;
2038 socd.expected_service_name = service_name;
2039 socd.failed = FALSE;
2040 socd.skip_connection = connection;
2041 bus_test_clients_foreach (check_service_owner_changed_foreach,
2047 /* Note that this differs from regular activation in that we don't get a
2048 * reply to ActivateService here.
2051 dbus_message_unref (message);
2053 service_name = NULL;
2057 warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
2066 dbus_message_unref (message);
2072 check_service_deactivated (BusContext *context,
2073 DBusConnection *connection,
2074 const char *activated_name,
2075 const char *base_service)
2078 CheckServiceOwnerChangedData socd;
2082 /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2083 * service and the activated_name. The base service
2084 * notification is required to come last.
2086 socd.expected_kind = SERVICE_DELETED;
2087 socd.expected_service_name = activated_name;
2088 socd.failed = FALSE;
2089 socd.skip_connection = NULL;
2090 bus_test_clients_foreach (check_service_owner_changed_foreach,
2096 socd.expected_kind = SERVICE_DELETED;
2097 socd.expected_service_name = base_service;
2098 socd.failed = FALSE;
2099 socd.skip_connection = NULL;
2100 bus_test_clients_foreach (check_service_owner_changed_foreach,
2113 check_send_exit_to_service (BusContext *context,
2114 DBusConnection *connection,
2115 const char *service_name,
2116 const char *base_service)
2118 dbus_bool_t got_error;
2119 DBusMessage *message;
2120 dbus_uint32_t serial;
2123 _dbus_verbose ("Sending exit message to the test service\n");
2127 /* Kill off the test service by sending it a quit message */
2128 message = dbus_message_new_method_call (service_name,
2129 "/org/freedesktop/TestSuite",
2130 "org.freedesktop.TestSuite",
2133 if (message == NULL)
2135 /* Do this again; we still need the service to exit... */
2136 if (!check_send_exit_to_service (context, connection,
2137 service_name, base_service))
2143 if (!dbus_connection_send (connection, message, &serial))
2145 dbus_message_unref (message);
2147 /* Do this again; we still need the service to exit... */
2148 if (!check_send_exit_to_service (context, connection,
2149 service_name, base_service))
2155 dbus_message_unref (message);
2159 bus_test_run_clients_loop (SEND_PENDING (connection));
2161 /* read it in and write it out to test service */
2162 bus_test_run_bus_loop (context, FALSE);
2164 /* see if we got an error during message bus dispatching */
2165 bus_test_run_clients_loop (FALSE);
2166 message = borrow_message_waiting_for_memory (connection);
2167 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2170 dbus_connection_return_message (connection, message);
2176 /* If no error, wait for the test service to exit */
2177 block_connection_until_message_from_bus (context, connection, "test service to exit");
2179 bus_test_run_everything (context);
2184 message = pop_message_waiting_for_memory (connection);
2185 _dbus_assert (message != NULL);
2187 if (dbus_message_get_reply_serial (message) != serial)
2189 warn_unexpected (connection, message,
2190 "error with the correct reply serial");
2194 if (!dbus_message_is_error (message,
2195 DBUS_ERROR_NO_MEMORY))
2197 warn_unexpected (connection, message,
2198 "a no memory error from asking test service to exit");
2202 _dbus_verbose ("Got error %s when asking test service to exit\n",
2203 dbus_message_get_error_name (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))
2212 if (!check_service_deactivated (context, connection,
2213 service_name, base_service))
2216 /* Should now have a NoReply error from the Exit() method
2217 * call; it should have come after all the deactivation
2220 message = pop_message_waiting_for_memory (connection);
2222 if (message == NULL)
2224 warn_unexpected (connection, NULL,
2225 "reply to Exit() method call");
2228 if (!dbus_message_is_error (message,
2229 DBUS_ERROR_NO_REPLY))
2231 warn_unexpected (connection, message,
2232 "NoReply error from Exit() method call");
2236 if (dbus_message_get_reply_serial (message) != serial)
2238 warn_unexpected (connection, message,
2239 "error with the correct reply serial");
2243 _dbus_verbose ("Got error %s after test service exited\n",
2244 dbus_message_get_error_name (message));
2246 if (!check_no_leftovers (context))
2248 _dbus_warn ("Messages were left over after %s\n",
2249 _DBUS_FUNCTION_NAME);
2258 dbus_message_unref (message);
2264 check_got_error (BusContext *context,
2265 DBusConnection *connection,
2266 const char *first_error_name,
2269 DBusMessage *message;
2272 dbus_bool_t error_found;
2273 const char *error_name;
2277 message = pop_message_waiting_for_memory (connection);
2278 if (message == NULL)
2280 _dbus_warn ("Did not get an expected error\n");
2284 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2286 warn_unexpected (connection, message, "an error");
2291 error_found = FALSE;
2293 va_start (ap, first_error_name);
2294 error_name = first_error_name;
2295 while (error_name != NULL)
2297 if (dbus_message_is_error (message, error_name))
2302 error_name = va_arg (ap, char*);
2308 _dbus_warn ("Expected error %s or other, got %s instead\n",
2310 dbus_message_get_error_name (message));
2318 dbus_message_unref (message);
2325 GOT_SERVICE_CREATED,
2326 GOT_SERVICE_DELETED,
2331 static GotServiceInfo
2332 check_got_service_info (DBusMessage *message)
2334 GotServiceInfo message_kind;
2336 if (dbus_message_is_signal (message,
2337 DBUS_INTERFACE_DBUS,
2338 "NameOwnerChanged"))
2341 const char *service_name, *old_owner, *new_owner;
2342 dbus_error_init (&error);
2344 reget_service_info_data:
2345 service_name = NULL;
2349 dbus_message_get_args (message, &error,
2350 DBUS_TYPE_STRING, &service_name,
2351 DBUS_TYPE_STRING, &old_owner,
2352 DBUS_TYPE_STRING, &new_owner,
2354 if (dbus_error_is_set (&error))
2356 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2358 dbus_error_free (&error);
2359 goto reget_service_info_data;
2363 _dbus_warn ("unexpected arguments for NameOwnerChanged message");
2364 message_kind = GOT_SOMETHING_ELSE;
2367 else if (!old_owner[0])
2368 message_kind = GOT_SERVICE_CREATED;
2369 else if (!new_owner[0])
2370 message_kind = GOT_SERVICE_DELETED;
2372 message_kind = GOT_SOMETHING_ELSE;
2374 dbus_error_free (&error);
2376 else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2377 message_kind = GOT_ERROR;
2379 message_kind = GOT_SOMETHING_ELSE;
2381 return message_kind;
2384 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2386 /* returns TRUE if the correct thing happens,
2387 * but the correct thing may include OOM errors.
2390 check_existent_service_no_auto_start (BusContext *context,
2391 DBusConnection *connection)
2393 DBusMessage *message;
2394 DBusMessage *base_service_message;
2395 const char *base_service;
2396 dbus_uint32_t serial;
2398 const char *existent = EXISTENT_SERVICE_NAME;
2399 dbus_uint32_t flags;
2401 base_service_message = NULL;
2403 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2405 DBUS_INTERFACE_DBUS,
2406 "StartServiceByName");
2408 if (message == NULL)
2411 dbus_message_set_auto_start (message, FALSE);
2414 if (!dbus_message_append_args (message,
2415 DBUS_TYPE_STRING, &existent,
2416 DBUS_TYPE_UINT32, &flags,
2419 dbus_message_unref (message);
2423 if (!dbus_connection_send (connection, message, &serial))
2425 dbus_message_unref (message);
2429 dbus_message_unref (message);
2432 bus_test_run_everything (context);
2434 /* now wait for the message bus to hear back from the activated
2437 block_connection_until_message_from_bus (context, connection, "activated service to connect");
2439 bus_test_run_everything (context);
2441 if (!dbus_connection_get_is_connected (connection))
2443 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2449 message = pop_message_waiting_for_memory (connection);
2450 if (message == NULL)
2452 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2453 "StartServiceByName", serial, connection);
2457 verbose_message_received (connection, message);
2458 _dbus_verbose (" (after sending %s)\n", "StartServiceByName");
2460 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2462 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2464 _dbus_warn ("Message has wrong sender %s\n",
2465 dbus_message_get_sender (message) ?
2466 dbus_message_get_sender (message) : "(none)");
2470 if (dbus_message_is_error (message,
2471 DBUS_ERROR_NO_MEMORY))
2473 ; /* good, this is a valid response */
2475 else if (dbus_message_is_error (message,
2476 DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2477 dbus_message_is_error (message,
2478 DBUS_ERROR_SPAWN_EXEC_FAILED))
2480 ; /* good, this is expected also */
2484 _dbus_warn ("Did not expect error %s\n",
2485 dbus_message_get_error_name (message));
2491 GotServiceInfo message_kind;
2493 if (!check_base_service_activated (context, connection,
2494 message, &base_service))
2497 base_service_message = message;
2500 /* We may need to block here for the test service to exit or finish up */
2501 block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
2503 message = dbus_connection_borrow_message (connection);
2504 if (message == NULL)
2506 _dbus_warn ("Did not receive any messages after base service creation notification\n");
2510 message_kind = check_got_service_info (message);
2512 dbus_connection_return_message (connection, message);
2515 switch (message_kind)
2517 case GOT_SOMETHING_ELSE:
2518 _dbus_warn ("Unexpected message after ActivateService "
2519 "(should be an error or a service announcement");
2523 if (!check_got_error (context, connection,
2524 DBUS_ERROR_SPAWN_CHILD_EXITED,
2525 DBUS_ERROR_NO_MEMORY,
2528 /* A service deleted should be coming along now after this error.
2529 * We can also get the error *after* the service deleted.
2534 case GOT_SERVICE_DELETED:
2536 /* The service started up and got a base address, but then
2537 * failed to register under EXISTENT_SERVICE_NAME
2539 CheckServiceOwnerChangedData socd;
2541 socd.expected_kind = SERVICE_DELETED;
2542 socd.expected_service_name = base_service;
2543 socd.failed = FALSE;
2544 socd.skip_connection = NULL;
2546 bus_test_clients_foreach (check_service_owner_changed_foreach,
2552 /* Now we should get an error about the service exiting
2553 * if we didn't get it before.
2555 if (message_kind != GOT_ERROR)
2557 block_connection_until_message_from_bus (context, connection, "error about service exiting");
2559 /* and process everything again */
2560 bus_test_run_everything (context);
2562 if (!check_got_error (context, connection,
2563 DBUS_ERROR_SPAWN_CHILD_EXITED,
2570 case GOT_SERVICE_CREATED:
2571 message = pop_message_waiting_for_memory (connection);
2572 if (message == NULL)
2574 _dbus_warn ("Failed to pop message we just put back! "
2575 "should have been a NameOwnerChanged (creation)\n");
2579 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2580 base_service, message))
2583 dbus_message_unref (message);
2586 if (!check_no_leftovers (context))
2588 _dbus_warn ("Messages were left over after successful activation\n");
2592 if (!check_send_exit_to_service (context, connection,
2593 EXISTENT_SERVICE_NAME, base_service))
2604 dbus_message_unref (message);
2606 if (base_service_message)
2607 dbus_message_unref (base_service_message);
2612 /* returns TRUE if the correct thing happens,
2613 * but the correct thing may include OOM errors.
2616 check_segfault_service_no_auto_start (BusContext *context,
2617 DBusConnection *connection)
2619 DBusMessage *message;
2620 dbus_uint32_t serial;
2622 const char *segv_service;
2623 dbus_uint32_t flags;
2625 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
2627 DBUS_INTERFACE_DBUS,
2628 "StartServiceByName");
2630 if (message == NULL)
2633 dbus_message_set_auto_start (message, FALSE);
2635 segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
2637 if (!dbus_message_append_args (message,
2638 DBUS_TYPE_STRING, &segv_service,
2639 DBUS_TYPE_UINT32, &flags,
2642 dbus_message_unref (message);
2646 if (!dbus_connection_send (connection, message, &serial))
2648 dbus_message_unref (message);
2652 dbus_message_unref (message);
2655 bus_test_run_everything (context);
2656 block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
2657 bus_test_run_everything (context);
2659 if (!dbus_connection_get_is_connected (connection))
2661 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2667 message = pop_message_waiting_for_memory (connection);
2668 if (message == NULL)
2670 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2671 "StartServiceByName", serial, connection);
2675 verbose_message_received (connection, message);
2677 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2679 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2681 _dbus_warn ("Message has wrong sender %s\n",
2682 dbus_message_get_sender (message) ?
2683 dbus_message_get_sender (message) : "(none)");
2687 if (dbus_message_is_error (message,
2688 DBUS_ERROR_NO_MEMORY))
2690 ; /* good, this is a valid response */
2692 else if (dbus_message_is_error (message,
2693 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2695 ; /* good, this is expected also */
2699 warn_unexpected (connection, message, "not this error");
2706 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2714 dbus_message_unref (message);
2720 /* returns TRUE if the correct thing happens,
2721 * but the correct thing may include OOM errors.
2724 check_segfault_service_auto_start (BusContext *context,
2725 DBusConnection *connection)
2727 DBusMessage *message;
2728 dbus_uint32_t serial;
2731 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2732 "/org/freedesktop/TestSuite",
2733 "org.freedesktop.TestSuite",
2736 if (message == NULL)
2739 if (!dbus_connection_send (connection, message, &serial))
2741 dbus_message_unref (message);
2745 dbus_message_unref (message);
2748 bus_test_run_everything (context);
2749 block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
2750 bus_test_run_everything (context);
2752 if (!dbus_connection_get_is_connected (connection))
2754 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2760 message = pop_message_waiting_for_memory (connection);
2761 if (message == NULL)
2763 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2764 "Echo message (auto activation)", serial, connection);
2768 verbose_message_received (connection, message);
2770 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2772 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
2774 _dbus_warn ("Message has wrong sender %s\n",
2775 dbus_message_get_sender (message) ?
2776 dbus_message_get_sender (message) : "(none)");
2780 if (dbus_message_is_error (message,
2781 DBUS_ERROR_NO_MEMORY))
2783 ; /* good, this is a valid response */
2785 else if (dbus_message_is_error (message,
2786 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2788 ; /* good, this is expected also */
2792 warn_unexpected (connection, message, "not this error");
2799 _dbus_warn ("Did not expect to successfully activate segfault service\n");
2807 dbus_message_unref (message);
2812 #define TEST_ECHO_MESSAGE "Test echo message"
2813 #define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
2815 /* returns TRUE if the correct thing happens,
2816 * but the correct thing may include OOM errors.
2819 check_existent_hello_from_self (BusContext *context,
2820 DBusConnection *connection)
2822 DBusMessage *message;
2823 dbus_uint32_t serial;
2825 const char *base_service;
2828 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2829 "/org/freedesktop/TestSuite",
2830 "org.freedesktop.TestSuite",
2831 "RunHelloFromSelf");
2833 if (message == NULL)
2836 text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
2837 if (!dbus_message_append_args (message,
2838 DBUS_TYPE_STRING, &text,
2841 dbus_message_unref (message);
2845 if (!dbus_connection_send (connection, message, &serial))
2847 dbus_message_unref (message);
2851 dbus_message_unref (message);
2854 bus_test_run_everything (context);
2856 /* Note: if this test is run in OOM mode, it will block when the bus
2857 * doesn't send a reply due to OOM.
2859 block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
2861 message = pop_message_waiting_for_memory (connection);
2862 if (message == NULL)
2864 _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
2868 if (dbus_message_get_reply_serial (message) != serial)
2870 _dbus_warn ("Wrong reply serial\n");
2874 dbus_message_unref (message);
2881 dbus_message_unref (message);
2887 /* returns TRUE if the correct thing happens,
2888 * but the correct thing may include OOM errors.
2891 check_existent_service_auto_start (BusContext *context,
2892 DBusConnection *connection)
2894 DBusMessage *message;
2895 DBusMessage *base_service_message;
2896 dbus_uint32_t serial;
2898 const char *base_service;
2901 base_service_message = NULL;
2903 message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2904 "/org/freedesktop/TestSuite",
2905 "org.freedesktop.TestSuite",
2908 if (message == NULL)
2911 text = TEST_ECHO_MESSAGE;
2912 if (!dbus_message_append_args (message,
2913 DBUS_TYPE_STRING, &text,
2916 dbus_message_unref (message);
2920 if (!dbus_connection_send (connection, message, &serial))
2922 dbus_message_unref (message);
2926 dbus_message_unref (message);
2929 bus_test_run_everything (context);
2931 /* now wait for the message bus to hear back from the activated
2934 block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
2935 bus_test_run_everything (context);
2937 if (!dbus_connection_get_is_connected (connection))
2939 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
2945 message = pop_message_waiting_for_memory (connection);
2946 if (message == NULL)
2948 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
2949 serial, connection);
2953 verbose_message_received (connection, message);
2954 _dbus_verbose (" (after sending %s)\n", "auto start");
2956 /* we should get zero or two ServiceOwnerChanged signals */
2957 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
2959 GotServiceInfo message_kind;
2961 if (!check_base_service_activated (context, connection,
2962 message, &base_service))
2965 base_service_message = message;
2968 /* We may need to block here for the test service to exit or finish up */
2969 block_connection_until_message_from_bus (context, connection, "service to exit");
2971 /* Should get a service creation notification for the activated
2972 * service name, or a service deletion on the base service name
2974 message = dbus_connection_borrow_message (connection);
2975 if (message == NULL)
2977 _dbus_warn ("No message after auto activation "
2978 "(should be a service announcement)");
2979 dbus_connection_return_message (connection, message);
2984 message_kind = check_got_service_info (message);
2986 dbus_connection_return_message (connection, message);
2989 switch (message_kind)
2991 case GOT_SERVICE_CREATED:
2992 message = pop_message_waiting_for_memory (connection);
2993 if (message == NULL)
2995 _dbus_warn ("Failed to pop message we just put back! "
2996 "should have been a NameOwnerChanged (creation)\n");
3000 /* Check that ServiceOwnerChanged (creation) was correctly received */
3001 if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
3002 base_service, message))
3005 dbus_message_unref (message);
3010 case GOT_SERVICE_DELETED:
3012 /* The service started up and got a base address, but then
3013 * failed to register under EXISTENT_SERVICE_NAME
3015 CheckServiceOwnerChangedData socd;
3017 socd.expected_kind = SERVICE_DELETED;
3018 socd.expected_service_name = base_service;
3019 socd.failed = FALSE;
3020 socd.skip_connection = NULL;
3021 bus_test_clients_foreach (check_service_owner_changed_foreach,
3031 case GOT_SOMETHING_ELSE:
3032 _dbus_warn ("Unexpected message after auto activation\n");
3037 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3038 * come the method reply (or error) from the initial method call
3041 /* Note: if this test is run in OOM mode, it will block when the bus
3042 * doesn't send a reply due to OOM.
3044 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3046 message = pop_message_waiting_for_memory (connection);
3047 if (message == NULL)
3049 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3053 if (dbus_message_get_reply_serial (message) != serial)
3055 _dbus_warn ("Wrong reply serial\n");
3059 dbus_message_unref (message);
3062 if (!check_existent_hello_from_self (context, connection))
3065 if (!check_send_exit_to_service (context, connection,
3066 EXISTENT_SERVICE_NAME,
3074 dbus_message_unref (message);
3076 if (base_service_message)
3077 dbus_message_unref (base_service_message);
3082 #define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
3084 /* returns TRUE if the correct thing happens,
3085 * but the correct thing may include OOM errors.
3088 check_shell_fail_service_auto_start (BusContext *context,
3089 DBusConnection *connection)
3091 DBusMessage *message;
3092 dbus_uint32_t serial;
3095 message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
3096 "/org/freedesktop/TestSuite",
3097 "org.freedesktop.TestSuite",
3100 if (message == NULL)
3103 if (!dbus_connection_send (connection, message, &serial))
3105 dbus_message_unref (message);
3109 dbus_message_unref (message);
3112 bus_test_run_everything (context);
3113 block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
3114 bus_test_run_everything (context);
3116 if (!dbus_connection_get_is_connected (connection))
3118 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3124 message = pop_message_waiting_for_memory (connection);
3125 if (message == NULL)
3127 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
3128 "Echo message (auto activation)", serial, connection);
3132 verbose_message_received (connection, message);
3134 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
3136 if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
3138 _dbus_warn ("Message has wrong sender %s\n",
3139 dbus_message_get_sender (message) ?
3140 dbus_message_get_sender (message) : "(none)");
3144 if (dbus_message_is_error (message,
3145 DBUS_ERROR_NO_MEMORY))
3147 ; /* good, this is a valid response */
3149 else if (dbus_message_is_error (message,
3150 DBUS_ERROR_INVALID_ARGS))
3152 _dbus_verbose("got invalid args\n");
3153 ; /* good, this is expected also */
3157 warn_unexpected (connection, message, "not this error");
3164 _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
3172 dbus_message_unref (message);
3177 #define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
3179 /* returns TRUE if the correct thing happens,
3180 * but the correct thing may include OOM errors.
3183 check_shell_service_success_auto_start (BusContext *context,
3184 DBusConnection *connection)
3186 DBusMessage *message;
3187 DBusMessage *base_service_message;
3188 dbus_uint32_t serial;
3190 const char *base_service;
3191 const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
3193 base_service_message = NULL;
3195 message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
3196 "/org/freedesktop/TestSuite",
3197 "org.freedesktop.TestSuite",
3200 if (message == NULL)
3203 if (!dbus_connection_send (connection, message, &serial))
3205 dbus_message_unref (message);
3209 dbus_message_unref (message);
3212 bus_test_run_everything (context);
3214 /* now wait for the message bus to hear back from the activated
3217 block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
3218 bus_test_run_everything (context);
3220 if (!dbus_connection_get_is_connected (connection))
3222 _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
3228 message = pop_message_waiting_for_memory (connection);
3229 if (message == NULL)
3231 _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
3232 serial, connection);
3236 verbose_message_received (connection, message);
3237 _dbus_verbose (" (after sending %s)\n", "auto start");
3239 /* we should get zero or two ServiceOwnerChanged signals */
3240 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
3242 GotServiceInfo message_kind;
3244 if (!check_base_service_activated (context, connection,
3245 message, &base_service))
3248 base_service_message = message;
3251 /* We may need to block here for the test service to exit or finish up */
3252 block_connection_until_message_from_bus (context, connection, "service to exit");
3254 /* Should get a service creation notification for the activated
3255 * service name, or a service deletion on the base service name
3257 message = dbus_connection_borrow_message (connection);
3258 if (message == NULL)
3260 _dbus_warn ("No message after auto activation "
3261 "(should be a service announcement)");
3262 dbus_connection_return_message (connection, message);
3267 message_kind = check_got_service_info (message);
3269 dbus_connection_return_message (connection, message);
3272 switch (message_kind)
3274 case GOT_SERVICE_CREATED:
3275 message = pop_message_waiting_for_memory (connection);
3276 if (message == NULL)
3278 _dbus_warn ("Failed to pop message we just put back! "
3279 "should have been a NameOwnerChanged (creation)\n");
3283 /* Check that ServiceOwnerChanged (creation) was correctly received */
3284 if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
3285 base_service, message))
3288 dbus_message_unref (message);
3293 case GOT_SERVICE_DELETED:
3295 /* The service started up and got a base address, but then
3296 * failed to register under SHELL_SUCCESS_SERVICE_NAME
3298 CheckServiceOwnerChangedData socd;
3300 socd.expected_kind = SERVICE_DELETED;
3301 socd.expected_service_name = base_service;
3302 socd.failed = FALSE;
3303 socd.skip_connection = NULL;
3304 bus_test_clients_foreach (check_service_owner_changed_foreach,
3314 case GOT_SOMETHING_ELSE:
3315 _dbus_warn ("Unexpected message after auto activation\n");
3320 /* OK, now we've dealt with ServiceOwnerChanged signals, now should
3321 * come the method reply (or error) from the initial method call
3324 /* Note: if this test is run in OOM mode, it will block when the bus
3325 * doesn't send a reply due to OOM.
3327 block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
3329 message = pop_message_waiting_for_memory (connection);
3330 if (message == NULL)
3332 _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
3336 if (dbus_message_get_reply_serial (message) != serial)
3338 _dbus_warn ("Wrong reply serial\n");
3342 if (!dbus_message_get_args (message, NULL,
3343 DBUS_TYPE_STRING, &argv[0],
3344 DBUS_TYPE_STRING, &argv[1],
3345 DBUS_TYPE_STRING, &argv[2],
3346 DBUS_TYPE_STRING, &argv[3],
3347 DBUS_TYPE_STRING, &argv[4],
3348 DBUS_TYPE_STRING, &argv[5],
3349 DBUS_TYPE_STRING, &argv[6],
3352 _dbus_warn ("Error getting arguments from return");
3356 /* don't worry about arg[0] as it may be different
3357 depending on the path to the tests
3359 if (strcmp("-test", argv[1]) != 0)
3361 _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)",
3366 if (strcmp("that", argv[2]) != 0)
3368 _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)",
3373 if (strcmp("we get", argv[3]) != 0)
3375 _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)",
3380 if (strcmp("back", argv[4]) != 0)
3382 _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)",
3387 if (strcmp("--what", argv[5]) != 0)
3389 _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)",
3394 if (strcmp("we put in", argv[6]) != 0)
3396 _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)",
3397 "we put in", argv[6]);
3401 dbus_message_unref (message);
3404 if (!check_send_exit_to_service (context, connection,
3405 SHELL_SUCCESS_SERVICE_NAME,
3413 dbus_message_unref (message);
3415 if (base_service_message)
3416 dbus_message_unref (base_service_message);
3424 BusContext *context;
3428 check_oom_check1_func (void *data)
3430 Check1Data *d = data;
3432 if (! (* d->func) (d->context))
3435 if (!check_no_leftovers (d->context))
3437 _dbus_warn ("Messages were left over, should be covered by test suite\n");
3445 check1_try_iterations (BusContext *context,
3446 const char *description,
3452 d.context = context;
3454 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
3456 _dbus_assert_not_reached ("test failed");
3462 BusContext *context;
3463 DBusConnection *connection;
3467 check_oom_check2_func (void *data)
3469 Check2Data *d = data;
3471 if (! (* d->func) (d->context, d->connection))
3474 if (!check_no_leftovers (d->context))
3476 _dbus_warn ("Messages were left over, should be covered by test suite");
3484 check2_try_iterations (BusContext *context,
3485 DBusConnection *connection,
3486 const char *description,
3492 d.context = context;
3493 d.connection = connection;
3495 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
3498 _dbus_warn ("%s failed during oom\n", description);
3499 _dbus_assert_not_reached ("test failed");
3504 bus_dispatch_test (const DBusString *test_data_dir)
3506 BusContext *context;
3507 DBusConnection *foo;
3508 DBusConnection *bar;
3509 DBusConnection *baz;
3512 dbus_error_init (&error);
3514 context = bus_context_new_test (test_data_dir,
3515 "valid-config-files/debug-allow-all.conf");
3516 if (context == NULL)
3519 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3521 _dbus_assert_not_reached ("could not alloc connection");
3523 if (!bus_setup_debug_client (foo))
3524 _dbus_assert_not_reached ("could not set up connection");
3526 spin_connection_until_authenticated (context, foo);
3528 if (!check_hello_message (context, foo))
3529 _dbus_assert_not_reached ("hello message failed");
3531 if (!check_double_hello_message (context, foo))
3532 _dbus_assert_not_reached ("double hello message failed");
3534 if (!check_add_match_all (context, foo))
3535 _dbus_assert_not_reached ("AddMatch message failed");
3537 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
3539 _dbus_assert_not_reached ("could not alloc connection");
3541 if (!bus_setup_debug_client (bar))
3542 _dbus_assert_not_reached ("could not set up connection");
3544 spin_connection_until_authenticated (context, bar);
3546 if (!check_hello_message (context, bar))
3547 _dbus_assert_not_reached ("hello message failed");
3549 if (!check_add_match_all (context, bar))
3550 _dbus_assert_not_reached ("AddMatch message failed");
3552 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
3554 _dbus_assert_not_reached ("could not alloc connection");
3556 if (!bus_setup_debug_client (baz))
3557 _dbus_assert_not_reached ("could not set up connection");
3559 spin_connection_until_authenticated (context, baz);
3561 if (!check_hello_message (context, baz))
3562 _dbus_assert_not_reached ("hello message failed");
3564 if (!check_add_match_all (context, baz))
3565 _dbus_assert_not_reached ("AddMatch message failed");
3567 if (!check_get_connection_unix_user (context, baz))
3568 _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
3570 if (!check_get_connection_unix_process_id (context, baz))
3571 _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
3573 if (!check_no_leftovers (context))
3575 _dbus_warn ("Messages were left over after setting up initial connections");
3576 _dbus_assert_not_reached ("initial connection setup failed");
3579 check1_try_iterations (context, "create_and_hello",
3580 check_hello_connection);
3582 check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
3583 check_nonexistent_service_no_auto_start);
3585 check2_try_iterations (context, foo, "segfault_service_no_auto_start",
3586 check_segfault_service_no_auto_start);
3588 check2_try_iterations (context, foo, "existent_service_no_auto_start",
3589 check_existent_service_no_auto_start);
3591 check2_try_iterations (context, foo, "nonexistent_service_auto_start",
3592 check_nonexistent_service_auto_start);
3594 check2_try_iterations (context, foo, "segfault_service_auto_start",
3595 check_segfault_service_auto_start);
3597 check2_try_iterations (context, foo, "shell_fail_service_auto_start",
3598 check_shell_fail_service_auto_start);
3601 /* Note: need to resolve some issues with the testing code in order to run
3602 * this in oom (handle that we sometimes don't get replies back from the bus
3603 * when oom happens, without blocking the test).
3605 check2_try_iterations (context, foo, "existent_service_auto_auto_start",
3606 check_existent_service_auto_start);
3609 if (!check_existent_service_auto_start (context, foo))
3610 _dbus_assert_not_reached ("existent service auto start failed");
3612 if (!check_shell_service_success_auto_start (context, foo))
3613 _dbus_assert_not_reached ("shell success service auto start failed");
3615 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
3617 kill_client_connection_unchecked (foo);
3618 kill_client_connection_unchecked (bar);
3619 kill_client_connection_unchecked (baz);
3621 bus_context_unref (context);
3627 bus_dispatch_sha1_test (const DBusString *test_data_dir)
3629 BusContext *context;
3630 DBusConnection *foo;
3633 dbus_error_init (&error);
3635 /* Test SHA1 authentication */
3636 _dbus_verbose ("Testing SHA1 context\n");
3638 context = bus_context_new_test (test_data_dir,
3639 "valid-config-files/debug-allow-all-sha1.conf");
3640 if (context == NULL)
3643 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3645 _dbus_assert_not_reached ("could not alloc connection");
3647 if (!bus_setup_debug_client (foo))
3648 _dbus_assert_not_reached ("could not set up connection");
3650 spin_connection_until_authenticated (context, foo);
3652 if (!check_hello_message (context, foo))
3653 _dbus_assert_not_reached ("hello message failed");
3655 if (!check_add_match_all (context, foo))
3656 _dbus_assert_not_reached ("addmatch message failed");
3658 if (!check_no_leftovers (context))
3660 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
3661 _dbus_assert_not_reached ("initial connection setup failed");
3664 check1_try_iterations (context, "create_and_hello_sha1",
3665 check_hello_connection);
3667 kill_client_connection_unchecked (foo);
3669 bus_context_unref (context);
3674 #endif /* DBUS_BUILD_TESTS */