1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "connection.h"
33 #include <dbus/dbus-internals.h>
37 send_one_message (DBusConnection *connection,
39 DBusConnection *sender,
40 DBusConnection *addressed_recipient,
42 BusTransaction *transaction,
45 if (!bus_context_check_security_policy (context,
51 return TRUE; /* silently don't send it */
53 if (!bus_transaction_send (transaction,
65 bus_dispatch_matches (BusTransaction *transaction,
66 DBusConnection *sender,
67 DBusConnection *addressed_recipient,
72 BusConnections *connections;
74 BusMatchmaker *matchmaker;
78 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
80 /* sender and recipient can both be NULL for the bus driver,
81 * or for signals with no particular recipient
84 _dbus_assert (sender == NULL || bus_connection_is_active (sender));
85 _dbus_assert (dbus_message_get_sender (message) != NULL);
87 connections = bus_transaction_get_connections (transaction);
89 dbus_error_init (&tmp_error);
90 context = bus_transaction_get_context (transaction);
91 matchmaker = bus_context_get_matchmaker (context);
94 if (!bus_matchmaker_get_recipients (matchmaker,
95 bus_context_get_connections (context),
96 sender, addressed_recipient, message,
103 link = _dbus_list_get_first_link (&recipients);
106 DBusConnection *dest;
110 if (!send_one_message (dest, context, sender, addressed_recipient,
111 message, transaction, &tmp_error))
114 link = _dbus_list_get_next_link (&recipients, link);
117 _dbus_list_clear (&recipients);
119 if (dbus_error_is_set (&tmp_error))
121 dbus_move_error (&tmp_error, error);
128 static DBusHandlerResult
129 bus_dispatch (DBusConnection *connection,
130 DBusMessage *message)
132 const char *sender, *service_name;
134 BusTransaction *transaction;
136 DBusHandlerResult result;
137 DBusConnection *addressed_recipient;
139 result = DBUS_HANDLER_RESULT_HANDLED;
142 addressed_recipient = NULL;
143 dbus_error_init (&error);
145 context = bus_connection_get_context (connection);
146 _dbus_assert (context != NULL);
148 /* If we can't even allocate an OOM error, we just go to sleep
151 while (!bus_connection_preallocate_oom_error (connection))
152 _dbus_wait_for_memory ();
154 /* Ref connection in case we disconnect it at some point in here */
155 dbus_connection_ref (connection);
157 service_name = dbus_message_get_destination (message);
159 #ifdef DBUS_ENABLE_VERBOSE_MODE
161 const char *interface_name, *member_name, *error_name;
163 interface_name = dbus_message_get_interface (message);
164 member_name = dbus_message_get_member (message);
165 error_name = dbus_message_get_error_name (message);
167 _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
168 interface_name ? interface_name : "(no interface)",
169 member_name ? member_name : "(no member)",
170 error_name ? error_name : "(no error name)",
171 service_name ? service_name : "peer");
173 #endif /* DBUS_ENABLE_VERBOSE_MODE */
175 /* If service_name is NULL, if it's a signal we send it to all
176 * connections with a match rule. If it's not a signal, it goes to
177 * the bus daemon but doesn't go "on the bus"; e.g. a peer-to-peer
178 * ping. Handle these immediately, especially disconnection
179 * messages. There are no security policy checks on these.
181 if (service_name == NULL)
183 if (dbus_message_is_signal (message,
184 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
187 bus_connection_disconnected (connection);
191 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
193 /* DBusConnection also handles some of these automatically, we leave
196 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
201 /* Create our transaction */
202 transaction = bus_transaction_new (context);
203 if (transaction == NULL)
205 BUS_SET_OOM (&error);
209 /* Assign a sender to the message */
210 if (bus_connection_is_active (connection))
212 sender = bus_connection_get_name (connection);
213 _dbus_assert (sender != NULL);
215 if (!dbus_message_set_sender (message, sender))
217 BUS_SET_OOM (&error);
221 /* We need to refetch the service name here, because
222 * dbus_message_set_sender can cause the header to be
223 * reallocated, and thus the service_name pointer will become
226 service_name = dbus_message_get_destination (message);
230 strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0) /* to bus driver */
232 if (!bus_context_check_security_policy (context,
233 connection, NULL, NULL, message, &error))
235 _dbus_verbose ("Security policy rejected message\n");
239 _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
240 if (!bus_driver_handle_message (connection, transaction, message, &error))
243 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
245 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
246 dbus_connection_disconnect (connection);
249 else if (service_name != NULL) /* route to named service */
251 DBusString service_string;
253 BusRegistry *registry;
255 _dbus_assert (service_name != NULL);
257 registry = bus_connection_get_registry (connection);
259 _dbus_string_init_const (&service_string, service_name);
260 service = bus_registry_lookup (registry, &service_string);
264 dbus_set_error (&error,
265 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
266 "Service \"%s\" does not exist",
272 addressed_recipient = bus_service_get_primary_owner (service);
273 _dbus_assert (addressed_recipient != NULL);
275 if (!bus_context_check_security_policy (context,
276 connection, addressed_recipient,
281 /* Dispatch the message */
282 if (!bus_transaction_send (transaction, addressed_recipient, message))
284 BUS_SET_OOM (&error);
290 /* Now match the messages against any match rules, which will send
291 * out signals and such. addressed_recipient may == NULL.
293 if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
297 if (dbus_error_is_set (&error))
299 if (!dbus_connection_get_is_connected (connection))
301 /* If we disconnected it, we won't bother to send it any error
304 _dbus_verbose ("Not sending error to connection we disconnected\n");
306 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
308 bus_connection_send_oom_error (connection, message);
310 /* cancel transaction due to OOM */
311 if (transaction != NULL)
313 bus_transaction_cancel_and_free (transaction);
319 /* Try to send the real error, if no mem to do that, send
322 _dbus_assert (transaction != NULL);
324 if (!bus_transaction_send_error_reply (transaction, connection,
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 dbus_error_free (&error);
341 if (transaction != NULL)
343 bus_transaction_execute_and_free (transaction);
346 dbus_connection_unref (connection);
351 static DBusHandlerResult
352 bus_dispatch_message_filter (DBusConnection *connection,
353 DBusMessage *message,
356 return bus_dispatch (connection, message);
360 bus_dispatch_add_connection (DBusConnection *connection)
362 if (!dbus_connection_add_filter (connection,
363 bus_dispatch_message_filter,
371 bus_dispatch_remove_connection (DBusConnection *connection)
373 /* Here we tell the bus driver that we want to get off. */
374 bus_driver_remove_connection (connection);
376 dbus_connection_remove_filter (connection,
377 bus_dispatch_message_filter,
381 #ifdef DBUS_BUILD_TESTS
383 typedef dbus_bool_t (* Check1Func) (BusContext *context);
384 typedef dbus_bool_t (* Check2Func) (BusContext *context,
385 DBusConnection *connection);
387 static dbus_bool_t check_no_leftovers (BusContext *context);
390 block_connection_until_message_from_bus (BusContext *context,
391 DBusConnection *connection)
393 while (dbus_connection_get_dispatch_status (connection) ==
394 DBUS_DISPATCH_COMPLETE &&
395 dbus_connection_get_is_connected (connection))
397 bus_test_run_bus_loop (context, TRUE);
398 bus_test_run_clients_loop (FALSE);
402 /* compensate for fact that pop_message() can return #NULL due to OOM */
404 pop_message_waiting_for_memory (DBusConnection *connection)
406 while (dbus_connection_get_dispatch_status (connection) ==
407 DBUS_DISPATCH_NEED_MEMORY)
408 _dbus_wait_for_memory ();
410 return dbus_connection_pop_message (connection);
414 warn_unexpected_real (DBusConnection *connection,
415 DBusMessage *message,
416 const char *expected,
417 const char *function,
420 _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
422 dbus_message_get_interface (message) ?
423 dbus_message_get_interface (message) : "(unset)",
424 dbus_message_get_member (message) ?
425 dbus_message_get_member (message) : "(unset)",
426 dbus_message_get_error_name (message) ?
427 dbus_message_get_error_name (message) : "(unset)",
432 #define warn_unexpected(connection, message, expected) \
433 warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
436 verbose_message_received (DBusConnection *connection,
437 DBusMessage *message)
439 _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
440 dbus_message_get_interface (message) ?
441 dbus_message_get_interface (message) : "(unset)",
442 dbus_message_get_member (message) ?
443 dbus_message_get_member (message) : "(unset)",
444 dbus_message_get_error_name (message) ?
445 dbus_message_get_error_name (message) : "(unset)",
451 const char *expected_service_name;
453 } CheckServiceDeletedData;
456 check_service_deleted_foreach (DBusConnection *connection,
459 CheckServiceDeletedData *d = data;
460 DBusMessage *message;
464 dbus_error_init (&error);
468 message = pop_message_waiting_for_memory (connection);
471 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
472 connection, "ServiceDeleted");
475 else if (!dbus_message_is_signal (message,
476 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
479 warn_unexpected (connection, message, "ServiceDeleted");
485 if (!dbus_message_get_args (message, &error,
486 DBUS_TYPE_STRING, &service_name,
489 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
491 _dbus_verbose ("no memory to get service name arg\n");
495 _dbus_assert (dbus_error_is_set (&error));
496 _dbus_warn ("Did not get the expected single string argument\n");
500 else if (strcmp (service_name, d->expected_service_name) != 0)
502 _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
503 d->expected_service_name,
512 dbus_free (service_name);
513 dbus_error_free (&error);
516 dbus_message_unref (message);
522 kill_client_connection (BusContext *context,
523 DBusConnection *connection)
527 CheckServiceDeletedData csdd;
529 _dbus_verbose ("killing connection %p\n", connection);
531 s = dbus_bus_get_base_service (connection);
532 _dbus_assert (s != NULL);
534 while ((base_service = _dbus_strdup (s)) == NULL)
535 _dbus_wait_for_memory ();
537 dbus_connection_ref (connection);
539 /* kick in the disconnect handler that unrefs the connection */
540 dbus_connection_disconnect (connection);
542 bus_test_run_everything (context);
544 _dbus_assert (bus_test_client_listed (connection));
546 /* Run disconnect handler in test.c */
547 if (bus_connection_dispatch_one_message (connection))
548 _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
550 _dbus_assert (!dbus_connection_get_is_connected (connection));
551 dbus_connection_unref (connection);
553 _dbus_assert (!bus_test_client_listed (connection));
555 csdd.expected_service_name = base_service;
558 bus_test_clients_foreach (check_service_deleted_foreach,
561 dbus_free (base_service);
564 _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
566 if (!check_no_leftovers (context))
567 _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
571 kill_client_connection_unchecked (DBusConnection *connection)
573 /* This kills the connection without expecting it to affect
574 * the rest of the bus.
576 _dbus_verbose ("Unchecked kill of connection %p\n", connection);
578 dbus_connection_ref (connection);
579 dbus_connection_disconnect (connection);
580 /* dispatching disconnect handler will unref once */
581 if (bus_connection_dispatch_one_message (connection))
582 _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
584 _dbus_assert (!bus_test_client_listed (connection));
585 dbus_connection_unref (connection);
591 } CheckNoMessagesData;
594 check_no_messages_foreach (DBusConnection *connection,
597 CheckNoMessagesData *d = data;
598 DBusMessage *message;
600 message = pop_message_waiting_for_memory (connection);
603 warn_unexpected (connection, message, "no messages");
609 dbus_message_unref (message);
615 DBusConnection *skip_connection;
616 const char *expected_service_name;
618 } CheckServiceCreatedData;
621 check_service_created_foreach (DBusConnection *connection,
624 CheckServiceCreatedData *d = data;
625 DBusMessage *message;
629 if (connection == d->skip_connection)
632 dbus_error_init (&error);
636 message = pop_message_waiting_for_memory (connection);
639 _dbus_warn ("Did not receive a message on %p, expecting %s\n",
640 connection, "ServiceCreated");
643 else if (!dbus_message_is_signal (message,
644 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
647 warn_unexpected (connection, message, "ServiceCreated");
652 if (!dbus_message_get_args (message, &error,
653 DBUS_TYPE_STRING, &service_name,
656 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
658 _dbus_verbose ("no memory to get service name arg\n");
662 _dbus_assert (dbus_error_is_set (&error));
663 _dbus_warn ("Did not get the expected single string argument\n");
667 else if (strcmp (service_name, d->expected_service_name) != 0)
669 _dbus_warn ("expected creation of service %s, got creation of %s\n",
670 d->expected_service_name,
679 dbus_free (service_name);
680 dbus_error_free (&error);
683 dbus_message_unref (message);
689 check_no_leftovers (BusContext *context)
691 CheckNoMessagesData nmd;
694 bus_test_clients_foreach (check_no_messages_foreach,
703 /* returns TRUE if the correct thing happens,
704 * but the correct thing may include OOM errors.
707 check_hello_message (BusContext *context,
708 DBusConnection *connection)
710 DBusMessage *message;
711 dbus_uint32_t serial;
718 dbus_error_init (&error);
723 _dbus_verbose ("check_hello_message for %p\n", connection);
725 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
726 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
727 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
733 if (!dbus_connection_send (connection, message, &serial))
735 dbus_message_unref (message);
739 dbus_message_unref (message);
742 /* send our message */
743 bus_test_run_clients_loop (TRUE);
745 dbus_connection_ref (connection); /* because we may get disconnected */
746 block_connection_until_message_from_bus (context, connection);
748 if (!dbus_connection_get_is_connected (connection))
750 _dbus_verbose ("connection was disconnected\n");
752 dbus_connection_unref (connection);
757 dbus_connection_unref (connection);
759 message = pop_message_waiting_for_memory (connection);
762 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
763 "Hello", serial, connection);
767 verbose_message_received (connection, message);
769 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
771 _dbus_warn ("Message has wrong sender %s\n",
772 dbus_message_get_sender (message) ?
773 dbus_message_get_sender (message) : "(none)");
777 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
779 if (dbus_message_is_error (message,
780 DBUS_ERROR_NO_MEMORY))
782 ; /* good, this is a valid response */
786 warn_unexpected (connection, message, "not this error");
793 CheckServiceCreatedData scd;
795 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
797 ; /* good, expected */
801 warn_unexpected (connection, message, "method return for Hello");
806 retry_get_hello_name:
807 if (!dbus_message_get_args (message, &error,
808 DBUS_TYPE_STRING, &name,
811 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
813 _dbus_verbose ("no memory to get service name arg from hello\n");
814 dbus_error_free (&error);
815 _dbus_wait_for_memory ();
816 goto retry_get_hello_name;
820 _dbus_assert (dbus_error_is_set (&error));
821 _dbus_warn ("Did not get the expected single string argument to hello\n");
826 _dbus_verbose ("Got hello name: %s\n", name);
828 while (!dbus_bus_set_base_service (connection, name))
829 _dbus_wait_for_memory ();
831 scd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
833 scd.expected_service_name = name;
834 bus_test_clients_foreach (check_service_created_foreach,
840 /* Client should also have gotten ServiceAcquired */
841 dbus_message_unref (message);
842 message = pop_message_waiting_for_memory (connection);
845 _dbus_warn ("Expecting %s, got nothing\n",
850 retry_get_acquired_name:
851 if (!dbus_message_get_args (message, &error,
852 DBUS_TYPE_STRING, &acquired,
855 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
857 _dbus_verbose ("no memory to get service name arg from acquired\n");
858 dbus_error_free (&error);
859 _dbus_wait_for_memory ();
860 goto retry_get_acquired_name;
864 _dbus_assert (dbus_error_is_set (&error));
865 _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
870 _dbus_verbose ("Got acquired name: %s\n", acquired);
872 if (strcmp (acquired, name) != 0)
874 _dbus_warn ("Acquired name is %s but expected %s\n",
880 if (!check_no_leftovers (context))
886 dbus_error_free (&error);
889 dbus_free (acquired);
892 dbus_message_unref (message);
897 /* returns TRUE if the correct thing happens,
898 * but the correct thing may include OOM errors.
901 check_add_match_all (BusContext *context,
902 DBusConnection *connection)
904 DBusMessage *message;
906 dbus_uint32_t serial;
910 dbus_error_init (&error);
913 _dbus_verbose ("check_add_match_all for %p\n", connection);
915 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
916 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
917 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
923 /* empty string match rule matches everything */
924 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, "",
927 dbus_message_unref (message);
931 if (!dbus_connection_send (connection, message, &serial))
933 dbus_message_unref (message);
937 dbus_message_unref (message);
940 /* send our message */
941 bus_test_run_clients_loop (TRUE);
943 dbus_connection_ref (connection); /* because we may get disconnected */
944 block_connection_until_message_from_bus (context, connection);
946 if (!dbus_connection_get_is_connected (connection))
948 _dbus_verbose ("connection was disconnected\n");
950 dbus_connection_unref (connection);
955 dbus_connection_unref (connection);
957 message = pop_message_waiting_for_memory (connection);
960 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
961 "AddMatch", serial, connection);
965 verbose_message_received (connection, message);
967 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
969 _dbus_warn ("Message has wrong sender %s\n",
970 dbus_message_get_sender (message) ?
971 dbus_message_get_sender (message) : "(none)");
975 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
977 if (dbus_message_is_error (message,
978 DBUS_ERROR_NO_MEMORY))
980 ; /* good, this is a valid response */
984 warn_unexpected (connection, message, "not this error");
991 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
993 ; /* good, expected */
994 _dbus_assert (dbus_message_get_reply_serial (message) == serial);
998 warn_unexpected (connection, message, "method return for AddMatch");
1004 if (!check_no_leftovers (context))
1010 dbus_error_free (&error);
1013 dbus_message_unref (message);
1018 /* returns TRUE if the correct thing happens,
1019 * but the correct thing may include OOM errors.
1022 check_hello_connection (BusContext *context)
1024 DBusConnection *connection;
1027 dbus_error_init (&error);
1029 connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
1030 if (connection == NULL)
1032 _DBUS_ASSERT_ERROR_IS_SET (&error);
1033 dbus_error_free (&error);
1037 if (!bus_setup_debug_client (connection))
1039 dbus_connection_disconnect (connection);
1040 dbus_connection_unref (connection);
1044 if (!check_hello_message (context, connection))
1047 if (dbus_bus_get_base_service (connection) == NULL)
1049 /* We didn't successfully register, so we can't
1050 * do the usual kill_client_connection() checks
1052 kill_client_connection_unchecked (connection);
1056 if (!check_add_match_all (context, connection))
1059 kill_client_connection (context, connection);
1065 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1067 /* returns TRUE if the correct thing happens,
1068 * but the correct thing may include OOM errors.
1071 check_nonexistent_service_activation (BusContext *context,
1072 DBusConnection *connection)
1074 DBusMessage *message;
1075 dbus_uint32_t serial;
1079 dbus_error_init (&error);
1081 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1082 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1083 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1086 if (message == NULL)
1089 if (!dbus_message_append_args (message,
1090 DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
1091 DBUS_TYPE_UINT32, 0,
1094 dbus_message_unref (message);
1098 if (!dbus_connection_send (connection, message, &serial))
1100 dbus_message_unref (message);
1104 dbus_message_unref (message);
1107 bus_test_run_everything (context);
1108 block_connection_until_message_from_bus (context, connection);
1109 bus_test_run_everything (context);
1111 if (!dbus_connection_get_is_connected (connection))
1113 _dbus_verbose ("connection was disconnected\n");
1119 message = pop_message_waiting_for_memory (connection);
1120 if (message == NULL)
1122 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1123 "ActivateService", serial, connection);
1127 verbose_message_received (connection, message);
1129 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1131 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1133 _dbus_warn ("Message has wrong sender %s\n",
1134 dbus_message_get_sender (message) ?
1135 dbus_message_get_sender (message) : "(none)");
1139 if (dbus_message_is_error (message,
1140 DBUS_ERROR_NO_MEMORY))
1142 ; /* good, this is a valid response */
1144 else if (dbus_message_is_error (message,
1145 DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1147 ; /* good, this is expected also */
1151 warn_unexpected (connection, message, "not this error");
1157 _dbus_warn ("Did not expect to successfully activate %s\n",
1158 NONEXISTENT_SERVICE_NAME);
1166 dbus_message_unref (message);
1172 check_base_service_activated (BusContext *context,
1173 DBusConnection *connection,
1174 DBusMessage *initial_message,
1175 char **base_service_p)
1177 DBusMessage *message;
1182 base_service = NULL;
1185 dbus_error_init (&error);
1187 message = initial_message;
1188 dbus_message_ref (message);
1190 if (dbus_message_is_signal (message,
1191 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1195 CheckServiceCreatedData scd;
1197 reget_service_name_arg:
1198 if (!dbus_message_get_args (message, &error,
1199 DBUS_TYPE_STRING, &service_name,
1202 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1204 dbus_error_free (&error);
1205 _dbus_wait_for_memory ();
1206 goto reget_service_name_arg;
1210 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1213 dbus_error_free (&error);
1218 if (*service_name != ':')
1220 _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1225 base_service = service_name;
1226 service_name = NULL;
1228 scd.skip_connection = connection;
1230 scd.expected_service_name = base_service;
1231 bus_test_clients_foreach (check_service_created_foreach,
1239 warn_unexpected (connection, message, "ServiceCreated for base service");
1248 *base_service_p = base_service;
1249 base_service = NULL;
1254 dbus_message_unref (message);
1257 dbus_free (base_service);
1263 check_service_activated (BusContext *context,
1264 DBusConnection *connection,
1265 const char *activated_name,
1266 const char *base_service_name,
1267 DBusMessage *initial_message)
1269 DBusMessage *message;
1272 dbus_uint32_t activation_result;
1276 dbus_error_init (&error);
1278 message = initial_message;
1279 dbus_message_ref (message);
1281 if (dbus_message_is_signal (message,
1282 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1286 CheckServiceCreatedData scd;
1288 reget_service_name_arg:
1289 if (!dbus_message_get_args (message, &error,
1290 DBUS_TYPE_STRING, &service_name,
1293 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1295 dbus_error_free (&error);
1296 _dbus_wait_for_memory ();
1297 goto reget_service_name_arg;
1301 _dbus_warn ("Message %s doesn't have a service name: %s\n",
1304 dbus_error_free (&error);
1309 if (strcmp (service_name, activated_name) != 0)
1311 _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1312 activated_name, service_name);
1313 dbus_free (service_name);
1317 scd.skip_connection = connection;
1319 scd.expected_service_name = service_name;
1320 bus_test_clients_foreach (check_service_created_foreach,
1323 dbus_free (service_name);
1328 dbus_message_unref (message);
1329 message = pop_message_waiting_for_memory (connection);
1330 if (message == NULL)
1332 _dbus_warn ("Expected a reply to %s, got nothing\n",
1339 warn_unexpected (connection, message, "ServiceCreated for the activated name");
1344 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1346 warn_unexpected (connection, message, "reply to ActivateService");
1351 activation_result = 0;
1352 if (!dbus_message_get_args (message, &error,
1353 DBUS_TYPE_UINT32, &activation_result,
1356 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1358 _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1359 "ActivateService", error.message);
1360 dbus_error_free (&error);
1364 dbus_error_free (&error);
1368 if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1370 else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1374 _dbus_warn ("Activation result was 0x%x, no good.\n",
1380 dbus_message_unref (message);
1383 if (!check_no_leftovers (context))
1385 _dbus_warn ("Messages were left over after verifying existent activation results\n");
1393 dbus_message_unref (message);
1399 check_service_deactivated (BusContext *context,
1400 DBusConnection *connection,
1401 const char *activated_name,
1402 const char *base_service)
1404 DBusMessage *message;
1407 CheckServiceDeletedData csdd;
1412 dbus_error_init (&error);
1414 /* Now we are expecting ServiceDeleted messages for the base
1415 * service and the activated_name. The base service
1416 * notification is required to come last.
1418 csdd.expected_service_name = activated_name;
1419 csdd.failed = FALSE;
1420 bus_test_clients_foreach (check_service_deleted_foreach,
1426 csdd.expected_service_name = base_service;
1427 csdd.failed = FALSE;
1428 bus_test_clients_foreach (check_service_deleted_foreach,
1434 if (!check_no_leftovers (context))
1436 _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1444 dbus_message_unref (message);
1450 check_send_exit_to_service (BusContext *context,
1451 DBusConnection *connection,
1452 const char *service_name,
1453 const char *base_service)
1455 dbus_bool_t got_error;
1456 DBusMessage *message;
1457 dbus_uint32_t serial;
1460 _dbus_verbose ("Sending exit message to the test service\n");
1464 /* Kill off the test service by sending it a quit message */
1465 message = dbus_message_new_method_call (service_name,
1466 "/org/freedesktop/TestSuite",
1467 "org.freedesktop.TestSuite",
1470 if (message == NULL)
1472 /* Do this again; we still need the service to exit... */
1473 if (!check_send_exit_to_service (context, connection,
1474 service_name, base_service))
1480 if (!dbus_connection_send (connection, message, &serial))
1482 dbus_message_unref (message);
1484 /* Do this again; we still need the service to exit... */
1485 if (!check_send_exit_to_service (context, connection,
1486 service_name, base_service))
1492 dbus_message_unref (message);
1496 bus_test_run_clients_loop (TRUE);
1498 /* read it in and write it out to test service */
1499 bus_test_run_bus_loop (context, FALSE);
1501 /* see if we got an error during message bus dispatching */
1502 bus_test_run_clients_loop (FALSE);
1503 message = dbus_connection_borrow_message (connection);
1504 got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1507 dbus_connection_return_message (connection, message);
1513 /* If no error, wait for the test service to exit */
1514 block_connection_until_message_from_bus (context, connection);
1516 bus_test_run_everything (context);
1521 message = pop_message_waiting_for_memory (connection);
1522 _dbus_assert (message != NULL);
1524 if (!dbus_message_is_error (message,
1525 DBUS_ERROR_NO_MEMORY))
1527 warn_unexpected (connection, message,
1528 "a no memory error from asking test service to exit");
1532 _dbus_verbose ("Got error %s when asking test service to exit\n",
1533 dbus_message_get_error_name (message));
1535 /* Do this again; we still need the service to exit... */
1536 if (!check_send_exit_to_service (context, connection,
1537 service_name, base_service))
1542 if (!check_service_deactivated (context, connection,
1543 service_name, base_service))
1551 dbus_message_unref (message);
1557 check_got_error (BusContext *context,
1558 DBusConnection *connection,
1559 const char *first_error_name,
1562 DBusMessage *message;
1565 dbus_bool_t error_found;
1566 const char *error_name;
1570 message = pop_message_waiting_for_memory (connection);
1571 if (message == NULL)
1573 _dbus_warn ("Did not get an expected error\n");
1577 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1579 warn_unexpected (connection, message, "an error");
1584 error_found = FALSE;
1586 va_start (ap, first_error_name);
1587 error_name = first_error_name;
1588 while (error_name != NULL)
1590 if (dbus_message_is_error (message, error_name))
1595 error_name = va_arg (ap, char*);
1601 _dbus_warn ("Expected error %s or other, got %s instead\n",
1603 dbus_message_get_error_name (message));
1611 dbus_message_unref (message);
1616 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1618 /* returns TRUE if the correct thing happens,
1619 * but the correct thing may include OOM errors.
1622 check_existent_service_activation (BusContext *context,
1623 DBusConnection *connection)
1625 DBusMessage *message;
1626 dbus_uint32_t serial;
1631 base_service = NULL;
1633 dbus_error_init (&error);
1635 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1636 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1637 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1640 if (message == NULL)
1643 if (!dbus_message_append_args (message,
1644 DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1645 DBUS_TYPE_UINT32, 0,
1648 dbus_message_unref (message);
1652 if (!dbus_connection_send (connection, message, &serial))
1654 dbus_message_unref (message);
1658 dbus_message_unref (message);
1661 bus_test_run_everything (context);
1663 /* now wait for the message bus to hear back from the activated
1666 block_connection_until_message_from_bus (context, connection);
1668 bus_test_run_everything (context);
1670 if (!dbus_connection_get_is_connected (connection))
1672 _dbus_verbose ("connection was disconnected\n");
1678 message = pop_message_waiting_for_memory (connection);
1679 if (message == NULL)
1681 _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1682 "ActivateService", serial, connection);
1686 verbose_message_received (connection, message);
1687 _dbus_verbose (" (after sending %s)\n", "ActivateService");
1689 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1691 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1693 _dbus_warn ("Message has wrong sender %s\n",
1694 dbus_message_get_sender (message) ?
1695 dbus_message_get_sender (message) : "(none)");
1699 if (dbus_message_is_error (message,
1700 DBUS_ERROR_NO_MEMORY))
1702 ; /* good, this is a valid response */
1704 else if (dbus_message_is_error (message,
1705 DBUS_ERROR_SPAWN_CHILD_EXITED))
1707 ; /* good, this is expected also */
1711 _dbus_warn ("Did not expect error %s\n",
1712 dbus_message_get_error_name (message));
1718 dbus_bool_t got_service_deleted;
1719 dbus_bool_t got_error;
1721 if (!check_base_service_activated (context, connection,
1722 message, &base_service))
1725 dbus_message_unref (message);
1728 /* We may need to block here for the test service to exit or finish up */
1729 block_connection_until_message_from_bus (context, connection);
1731 message = dbus_connection_borrow_message (connection);
1732 if (message == NULL)
1734 _dbus_warn ("Did not receive any messages after base service creation notification\n");
1738 got_service_deleted = dbus_message_is_signal (message,
1739 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1741 got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1743 dbus_connection_return_message (connection, message);
1748 if (!check_got_error (context, connection,
1749 DBUS_ERROR_SPAWN_CHILD_EXITED,
1750 DBUS_ERROR_NO_MEMORY,
1754 /* A service deleted should be coming along now after this error.
1755 * We can also get the error *after* the service deleted.
1757 got_service_deleted = TRUE;
1760 if (got_service_deleted)
1762 /* The service started up and got a base address, but then
1763 * failed to register under EXISTENT_SERVICE_NAME
1765 CheckServiceDeletedData csdd;
1767 csdd.expected_service_name = base_service;
1768 csdd.failed = FALSE;
1769 bus_test_clients_foreach (check_service_deleted_foreach,
1775 /* Now we should get an error about the service exiting
1776 * if we didn't get it before.
1780 block_connection_until_message_from_bus (context, connection);
1782 /* and process everything again */
1783 bus_test_run_everything (context);
1785 if (!check_got_error (context, connection,
1786 DBUS_ERROR_SPAWN_CHILD_EXITED,
1793 message = pop_message_waiting_for_memory (connection);
1794 if (message == NULL)
1796 _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1800 if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1801 base_service, message))
1804 dbus_message_unref (message);
1808 if (!check_no_leftovers (context))
1810 _dbus_warn ("Messages were left over after successful activation\n");
1814 if (!check_send_exit_to_service (context, connection,
1815 EXISTENT_SERVICE_NAME, base_service))
1824 dbus_message_unref (message);
1827 dbus_free (base_service);
1832 /* returns TRUE if the correct thing happens,
1833 * but the correct thing may include OOM errors.
1836 check_segfault_service_activation (BusContext *context,
1837 DBusConnection *connection)
1839 DBusMessage *message;
1840 dbus_uint32_t serial;
1844 dbus_error_init (&error);
1846 message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1847 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1848 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1851 if (message == NULL)
1854 if (!dbus_message_append_args (message,
1856 "org.freedesktop.DBus.TestSuiteSegfaultService",
1857 DBUS_TYPE_UINT32, 0,
1860 dbus_message_unref (message);
1864 if (!dbus_connection_send (connection, message, &serial))
1866 dbus_message_unref (message);
1870 dbus_message_unref (message);
1873 bus_test_run_everything (context);
1874 block_connection_until_message_from_bus (context, connection);
1875 bus_test_run_everything (context);
1877 if (!dbus_connection_get_is_connected (connection))
1879 _dbus_verbose ("connection was disconnected\n");
1885 message = pop_message_waiting_for_memory (connection);
1886 if (message == NULL)
1888 _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1889 "ActivateService", serial, connection);
1893 verbose_message_received (connection, message);
1895 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1897 if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1899 _dbus_warn ("Message has wrong sender %s\n",
1900 dbus_message_get_sender (message) ?
1901 dbus_message_get_sender (message) : "(none)");
1905 if (dbus_message_is_error (message,
1906 DBUS_ERROR_NO_MEMORY))
1908 ; /* good, this is a valid response */
1910 else if (dbus_message_is_error (message,
1911 DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1913 ; /* good, this is expected also */
1917 warn_unexpected (connection, message, "not this error");
1924 _dbus_warn ("Did not expect to successfully activate segfault service\n");
1932 dbus_message_unref (message);
1940 BusContext *context;
1944 check_oom_check1_func (void *data)
1946 Check1Data *d = data;
1948 if (! (* d->func) (d->context))
1951 if (!check_no_leftovers (d->context))
1953 _dbus_warn ("Messages were left over, should be covered by test suite\n");
1961 check1_try_iterations (BusContext *context,
1962 const char *description,
1968 d.context = context;
1970 if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1972 _dbus_assert_not_reached ("test failed");
1978 BusContext *context;
1979 DBusConnection *connection;
1983 check_oom_check2_func (void *data)
1985 Check2Data *d = data;
1987 if (! (* d->func) (d->context, d->connection))
1990 if (!check_no_leftovers (d->context))
1992 _dbus_warn ("Messages were left over, should be covered by test suite");
2000 check2_try_iterations (BusContext *context,
2001 DBusConnection *connection,
2002 const char *description,
2008 d.context = context;
2009 d.connection = connection;
2011 if (!_dbus_test_oom_handling (description, check_oom_check2_func,
2013 _dbus_assert_not_reached ("test failed");
2017 bus_dispatch_test (const DBusString *test_data_dir)
2019 BusContext *context;
2020 DBusConnection *foo;
2021 DBusConnection *bar;
2022 DBusConnection *baz;
2025 dbus_error_init (&error);
2027 context = bus_context_new_test (test_data_dir,
2028 "valid-config-files/debug-allow-all.conf");
2029 if (context == NULL)
2032 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
2034 _dbus_assert_not_reached ("could not alloc connection");
2036 if (!bus_setup_debug_client (foo))
2037 _dbus_assert_not_reached ("could not set up connection");
2039 if (!check_hello_message (context, foo))
2040 _dbus_assert_not_reached ("hello message failed");
2042 if (!check_add_match_all (context, foo))
2043 _dbus_assert_not_reached ("AddMatch message failed");
2045 bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
2047 _dbus_assert_not_reached ("could not alloc connection");
2049 if (!bus_setup_debug_client (bar))
2050 _dbus_assert_not_reached ("could not set up connection");
2052 if (!check_hello_message (context, bar))
2053 _dbus_assert_not_reached ("hello message failed");
2055 if (!check_add_match_all (context, bar))
2056 _dbus_assert_not_reached ("AddMatch message failed");
2058 baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
2060 _dbus_assert_not_reached ("could not alloc connection");
2062 if (!bus_setup_debug_client (baz))
2063 _dbus_assert_not_reached ("could not set up connection");
2065 if (!check_hello_message (context, baz))
2066 _dbus_assert_not_reached ("hello message failed");
2068 if (!check_add_match_all (context, baz))
2069 _dbus_assert_not_reached ("AddMatch message failed");
2071 if (!check_no_leftovers (context))
2073 _dbus_warn ("Messages were left over after setting up initial connections");
2074 _dbus_assert_not_reached ("initial connection setup failed");
2077 check1_try_iterations (context, "create_and_hello",
2078 check_hello_connection);
2080 check2_try_iterations (context, foo, "nonexistent_service_activation",
2081 check_nonexistent_service_activation);
2083 check2_try_iterations (context, foo, "segfault_service_activation",
2084 check_segfault_service_activation);
2086 check2_try_iterations (context, foo, "existent_service_activation",
2087 check_existent_service_activation);
2089 _dbus_verbose ("Disconnecting foo, bar, and baz\n");
2091 kill_client_connection_unchecked (foo);
2092 kill_client_connection_unchecked (bar);
2093 kill_client_connection_unchecked (baz);
2095 bus_context_unref (context);
2101 bus_dispatch_sha1_test (const DBusString *test_data_dir)
2103 BusContext *context;
2104 DBusConnection *foo;
2107 dbus_error_init (&error);
2109 /* Test SHA1 authentication */
2110 _dbus_verbose ("Testing SHA1 context\n");
2112 context = bus_context_new_test (test_data_dir,
2113 "valid-config-files/debug-allow-all-sha1.conf");
2114 if (context == NULL)
2117 foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
2119 _dbus_assert_not_reached ("could not alloc connection");
2121 if (!bus_setup_debug_client (foo))
2122 _dbus_assert_not_reached ("could not set up connection");
2124 if (!check_hello_message (context, foo))
2125 _dbus_assert_not_reached ("hello message failed");
2127 if (!check_add_match_all (context, foo))
2128 _dbus_assert_not_reached ("addmatch message failed");
2130 if (!check_no_leftovers (context))
2132 _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
2133 _dbus_assert_not_reached ("initial connection setup failed");
2136 check1_try_iterations (context, "create_and_hello_sha1",
2137 check_hello_connection);
2139 kill_client_connection_unchecked (foo);
2141 bus_context_unref (context);
2146 #endif /* DBUS_BUILD_TESTS */