* doc/dbus-specification.xml: document ServiceOwnerChanged signal.
[platform/upstream/dbus.git] / bus / dispatch.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c  Message dispatcher
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat, Inc.
6  * Copyright (C) 2004  Imendio HB
7  *
8  * Licensed under the Academic Free License version 2.1
9  * 
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.
14  *
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.
19  * 
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
23  *
24  */
25
26 #include "dispatch.h"
27 #include "connection.h"
28 #include "driver.h"
29 #include "services.h"
30 #include "activation.h"
31 #include "utils.h"
32 #include "bus.h"
33 #include "signals.h"
34 #include "test.h"
35 #include <dbus/dbus-internals.h>
36 #include <string.h>
37
38 static dbus_bool_t
39 send_one_message (DBusConnection *connection,
40                   BusContext     *context,
41                   DBusConnection *sender,
42                   DBusConnection *addressed_recipient,
43                   DBusMessage    *message,
44                   BusTransaction *transaction,
45                   DBusError      *error)
46 {
47   if (!bus_context_check_security_policy (context, transaction,
48                                           sender,
49                                           addressed_recipient,
50                                           connection,
51                                           message,
52                                           NULL))
53     return TRUE; /* silently don't send it */
54   
55   if (!bus_transaction_send (transaction,
56                              connection,
57                              message))
58     {
59       BUS_SET_OOM (error);
60       return FALSE;
61     }
62
63   return TRUE;
64 }
65
66 dbus_bool_t
67 bus_dispatch_matches (BusTransaction *transaction,
68                       DBusConnection *sender,
69                       DBusConnection *addressed_recipient,
70                       DBusMessage    *message,
71                       DBusError      *error)
72 {
73   DBusError tmp_error;
74   BusConnections *connections;
75   DBusList *recipients;
76   BusMatchmaker *matchmaker;
77   DBusList *link;
78   BusContext *context;
79
80   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
81
82   /* sender and recipient can both be NULL for the bus driver,
83    * or for signals with no particular recipient
84    */
85
86   _dbus_assert (sender == NULL || bus_connection_is_active (sender));
87   _dbus_assert (dbus_message_get_sender (message) != NULL);
88
89   connections = bus_transaction_get_connections (transaction);
90   
91   dbus_error_init (&tmp_error);
92   context = bus_transaction_get_context (transaction);
93   matchmaker = bus_context_get_matchmaker (context);
94
95   recipients = NULL;
96   if (!bus_matchmaker_get_recipients (matchmaker, connections,
97                                       sender, addressed_recipient, message,
98                                       &recipients))
99     {
100       BUS_SET_OOM (error);
101       return FALSE;
102     }
103
104   link = _dbus_list_get_first_link (&recipients);
105   while (link != NULL)
106     {
107       DBusConnection *dest;
108
109       dest = link->data;
110
111       if (!send_one_message (dest, context, sender, addressed_recipient,
112                              message, transaction, &tmp_error))
113         break;
114
115       link = _dbus_list_get_next_link (&recipients, link);
116     }
117
118   _dbus_list_clear (&recipients);
119   
120   if (dbus_error_is_set (&tmp_error))
121     {
122       dbus_move_error (&tmp_error, error);
123       return FALSE;
124     }
125   else
126     return TRUE;
127 }
128
129 static DBusHandlerResult
130 bus_dispatch (DBusConnection *connection,
131               DBusMessage    *message)
132 {
133   const char *sender, *service_name;
134   DBusError error;
135   BusTransaction *transaction;
136   BusContext *context;
137   DBusHandlerResult result;
138   DBusConnection *addressed_recipient;
139   
140   result = DBUS_HANDLER_RESULT_HANDLED;
141   
142   transaction = NULL;
143   addressed_recipient = NULL;
144   dbus_error_init (&error);
145   
146   context = bus_connection_get_context (connection);
147   _dbus_assert (context != NULL);
148   
149   /* If we can't even allocate an OOM error, we just go to sleep
150    * until we can.
151    */
152   while (!bus_connection_preallocate_oom_error (connection))
153     _dbus_wait_for_memory ();
154   
155   /* Ref connection in case we disconnect it at some point in here */
156   dbus_connection_ref (connection);
157   
158   service_name = dbus_message_get_destination (message);
159
160 #ifdef DBUS_ENABLE_VERBOSE_MODE
161   {
162     const char *interface_name, *member_name, *error_name;
163
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);
167     
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");
173   }
174 #endif /* DBUS_ENABLE_VERBOSE_MODE */
175   
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.
179    */
180   if (service_name == NULL)
181     {
182       if (dbus_message_is_signal (message,
183                                   DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
184                                   "Disconnected"))
185         {
186           bus_connection_disconnected (connection);
187           goto out;
188         }
189
190       if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
191         {
192           /* DBusConnection also handles some of these automatically, we leave
193            * it to do so.
194            */
195           result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
196           goto out;
197         }
198     }
199   
200   /* Create our transaction */
201   transaction = bus_transaction_new (context);
202   if (transaction == NULL)
203     {
204       BUS_SET_OOM (&error);
205       goto out;
206     }
207   
208   /* Assign a sender to the message */
209   if (bus_connection_is_active (connection))
210     {
211       sender = bus_connection_get_name (connection);
212       _dbus_assert (sender != NULL);
213
214       if (!dbus_message_set_sender (message, sender))
215         {
216           BUS_SET_OOM (&error);
217           goto out;
218         }
219
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
223        * invalid.
224        */
225       service_name = dbus_message_get_destination (message);
226     }
227   
228   if (service_name &&
229       strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0) /* to bus driver */
230     {
231       if (!bus_context_check_security_policy (context, transaction,
232                                               connection, NULL, NULL, message, &error))
233         {
234           _dbus_verbose ("Security policy rejected message\n");
235           goto out;
236         }
237
238       _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
239       if (!bus_driver_handle_message (connection, transaction, message, &error))
240         goto out;
241     }
242   else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
243     {
244       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
245       dbus_connection_disconnect (connection);
246       goto out;
247     }
248   else if (service_name != NULL) /* route to named service */
249     {
250       DBusString service_string;
251       BusService *service;
252       BusRegistry *registry;
253
254       _dbus_assert (service_name != NULL);
255       
256       registry = bus_connection_get_registry (connection);
257       
258       _dbus_string_init_const (&service_string, service_name);
259       service = bus_registry_lookup (registry, &service_string);
260
261       if (service == NULL && dbus_message_get_auto_activation (message))
262         {
263           BusActivation *activation;
264
265           /* We can't do the security policy check here, since the addressed
266            * recipient service doesn't exist yet. We do it before sending the
267            * message after the service has been created.
268            */
269           activation = bus_connection_get_activation (connection);
270
271           if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
272                                                 message, service_name, &error))
273             {
274               _DBUS_ASSERT_ERROR_IS_SET (&error);
275               _dbus_verbose ("bus_activation_activate_service() failed\n");
276               goto out;
277             }
278           
279           goto out;
280         }
281       else if (service == NULL)
282         {
283           dbus_set_error (&error,
284                           DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
285                           "Service \"%s\" does not exist",
286                           service_name);
287           goto out;
288         }
289       else
290         {          
291           addressed_recipient = bus_service_get_primary_owner (service);
292           _dbus_assert (addressed_recipient != NULL);
293           
294           if (!bus_context_check_security_policy (context, transaction,
295                                                   connection, addressed_recipient,
296                                                   addressed_recipient,
297                                                   message, &error))
298             goto out;
299           
300           /* Dispatch the message */
301           if (!bus_transaction_send (transaction, addressed_recipient, message))
302             {
303               BUS_SET_OOM (&error);
304               goto out;
305             }
306         }
307     }
308
309   /* Now match the messages against any match rules, which will send
310    * out signals and such. addressed_recipient may == NULL.
311    */
312   if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
313     goto out;
314   
315  out:
316   if (dbus_error_is_set (&error))
317     {
318       if (!dbus_connection_get_is_connected (connection))
319         {
320           /* If we disconnected it, we won't bother to send it any error
321            * messages.
322            */
323           _dbus_verbose ("Not sending error to connection we disconnected\n");
324         }
325       else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
326         {
327           bus_connection_send_oom_error (connection, message);
328
329           /* cancel transaction due to OOM */
330           if (transaction != NULL)
331             {
332               bus_transaction_cancel_and_free (transaction);
333               transaction = NULL;
334             }
335         }
336       else
337         {
338           /* Try to send the real error, if no mem to do that, send
339            * the OOM error
340            */
341           _dbus_assert (transaction != NULL);
342           
343           if (!bus_transaction_send_error_reply (transaction, connection,
344                                                  &error, message))
345             {
346               bus_connection_send_oom_error (connection, message);
347               
348               /* cancel transaction due to OOM */
349               if (transaction != NULL)
350                 {
351                   bus_transaction_cancel_and_free (transaction);
352                   transaction = NULL;
353                 }
354             }
355         }
356       
357       dbus_error_free (&error);
358     }
359
360   if (transaction != NULL)
361     {
362       bus_transaction_execute_and_free (transaction);
363     }
364
365   dbus_connection_unref (connection);
366
367   return result;
368 }
369
370 static DBusHandlerResult
371 bus_dispatch_message_filter (DBusConnection     *connection,
372                              DBusMessage        *message,
373                              void               *user_data)
374 {
375   return bus_dispatch (connection, message);
376 }
377
378 dbus_bool_t
379 bus_dispatch_add_connection (DBusConnection *connection)
380 {  
381   if (!dbus_connection_add_filter (connection,
382                                    bus_dispatch_message_filter,
383                                    NULL, NULL))
384     return FALSE;
385   
386   return TRUE;
387 }
388
389 void
390 bus_dispatch_remove_connection (DBusConnection *connection)
391 {
392   /* Here we tell the bus driver that we want to get off. */
393   bus_driver_remove_connection (connection);
394
395   dbus_connection_remove_filter (connection,
396                                  bus_dispatch_message_filter,
397                                  NULL);
398 }
399
400 #ifdef DBUS_BUILD_TESTS
401
402 #include <stdio.h>
403
404 typedef dbus_bool_t (* Check1Func) (BusContext     *context);
405 typedef dbus_bool_t (* Check2Func) (BusContext     *context,
406                                     DBusConnection *connection);
407
408 static dbus_bool_t check_no_leftovers (BusContext *context);
409
410 static void
411 block_connection_until_message_from_bus (BusContext     *context,
412                                          DBusConnection *connection)
413 {
414   while (dbus_connection_get_dispatch_status (connection) ==
415          DBUS_DISPATCH_COMPLETE &&
416          dbus_connection_get_is_connected (connection))
417     {
418       bus_test_run_bus_loop (context, TRUE);
419       bus_test_run_clients_loop (FALSE);
420     }
421 }
422
423 /* compensate for fact that pop_message() can return #NULL due to OOM */
424 static DBusMessage*
425 pop_message_waiting_for_memory (DBusConnection *connection)
426 {
427   while (dbus_connection_get_dispatch_status (connection) ==
428          DBUS_DISPATCH_NEED_MEMORY)
429     _dbus_wait_for_memory ();
430
431   return dbus_connection_pop_message (connection);
432 }
433
434 static DBusMessage*
435 borrow_message_waiting_for_memory (DBusConnection *connection)
436 {
437   while (dbus_connection_get_dispatch_status (connection) ==
438          DBUS_DISPATCH_NEED_MEMORY)
439     _dbus_wait_for_memory ();
440
441   return dbus_connection_borrow_message (connection);
442 }
443
444 static void
445 warn_unexpected_real (DBusConnection *connection,
446                       DBusMessage    *message,
447                       const char     *expected,
448                       const char     *function,
449                       int             line)
450 {
451   if (message)
452     _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
453                 function, line,
454                 dbus_message_get_interface (message) ?
455                 dbus_message_get_interface (message) : "(unset)",
456                 dbus_message_get_member (message) ?
457                 dbus_message_get_member (message) : "(unset)",
458                 dbus_message_get_error_name (message) ?
459                 dbus_message_get_error_name (message) : "(unset)",
460                 connection,
461                 expected);
462   else
463     _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
464                 function, line, connection, expected);
465 }
466
467 #define warn_unexpected(connection, message, expected) \
468   warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
469
470 static void
471 verbose_message_received (DBusConnection *connection,
472                           DBusMessage    *message)
473 {
474   _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
475                  dbus_message_get_interface (message) ?
476                  dbus_message_get_interface (message) : "(unset)",
477                  dbus_message_get_member (message) ?
478                  dbus_message_get_member (message) : "(unset)",
479                  dbus_message_get_error_name (message) ?
480                  dbus_message_get_error_name (message) : "(unset)",
481                  connection);
482 }
483
484 typedef enum
485 {
486   SERVICE_CREATED,
487   OWNER_CHANGED,
488   SERVICE_DELETED
489 } ServiceInfoKind;
490
491 typedef struct
492 {
493   ServiceInfoKind expected_kind;
494   const char *expected_service_name;
495   dbus_bool_t failed;
496   DBusConnection *skip_connection;
497 } CheckServiceOwnerChangedData;
498
499 static dbus_bool_t
500 check_service_owner_changed_foreach (DBusConnection *connection,
501                                      void           *data)
502 {
503   CheckServiceOwnerChangedData *d = data;
504   DBusMessage *message;
505   DBusError error;
506   char *service_name, *old_owner, *new_owner;
507
508   if (d->expected_kind == SERVICE_CREATED 
509       && connection == d->skip_connection)
510     return TRUE;
511
512   dbus_error_init (&error);
513   d->failed = TRUE;
514   
515   message = pop_message_waiting_for_memory (connection);
516   if (message == NULL)
517     {
518       _dbus_warn ("Did not receive a message on %p, expecting %s\n",
519                   connection, "ServiceOwnerChanged");
520       goto out;
521     }
522   else if (!dbus_message_is_signal (message,
523                                     DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
524                                     "ServiceOwnerChanged"))
525     {
526       warn_unexpected (connection, message, "ServiceOwnerChanged");
527
528       goto out;
529     }
530   else
531     {
532     reget_service_info_data:
533       service_name = NULL;
534       old_owner = NULL;
535       new_owner = NULL;
536
537       dbus_message_get_args (message, &error,
538                              DBUS_TYPE_STRING, &service_name,
539                              DBUS_TYPE_STRING, &old_owner,
540                              DBUS_TYPE_STRING, &new_owner,
541                              DBUS_TYPE_INVALID);
542
543       if (dbus_error_is_set (&error))
544         {
545           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
546             {
547               dbus_free (service_name);
548               dbus_free (old_owner);
549               dbus_free (new_owner);
550               dbus_error_free (&error);
551               _dbus_wait_for_memory ();              
552               goto reget_service_info_data;
553             }
554           else
555             {
556               _dbus_warn ("Did not get the expected arguments\n");
557               goto out;
558             }
559         }
560
561       if ((d->expected_kind == SERVICE_CREATED    && ( old_owner[0] || !new_owner[0]))
562           || (d->expected_kind == OWNER_CHANGED   && (!old_owner[0] || !new_owner[0]))
563           || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] ||  new_owner[0])))
564         {
565           _dbus_warn ("inconsistent ServiceOwnerChanged arguments");
566           goto out;
567         }
568
569       if (strcmp (service_name, d->expected_service_name) != 0)
570         {
571           _dbus_warn ("expected info on service %s, got info on %s\n",
572                       d->expected_service_name,
573                       service_name);
574           goto out;
575         }
576
577       if (*service_name == ':' && new_owner[0] 
578           && strcmp (service_name, new_owner) != 0)
579         {
580           _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
581                       service_name, old_owner, new_owner);
582           goto out;
583         }
584     }
585
586   d->failed = FALSE;
587   
588  out:
589   dbus_free (service_name);
590   dbus_free (old_owner);
591   dbus_free (new_owner);
592   dbus_error_free (&error);
593   
594   if (message)
595     dbus_message_unref (message);
596
597   return !d->failed;
598 }
599
600
601 static void
602 kill_client_connection (BusContext     *context,
603                         DBusConnection *connection)
604 {
605   char *base_service;
606   const char *s;
607   CheckServiceOwnerChangedData socd;
608
609   _dbus_verbose ("killing connection %p\n", connection);
610   
611   s = dbus_bus_get_base_service (connection);
612   _dbus_assert (s != NULL);
613
614   while ((base_service = _dbus_strdup (s)) == NULL)
615     _dbus_wait_for_memory ();
616
617   dbus_connection_ref (connection);
618   
619   /* kick in the disconnect handler that unrefs the connection */
620   dbus_connection_disconnect (connection);
621
622   bus_test_run_everything (context);
623   
624   _dbus_assert (bus_test_client_listed (connection));
625   
626   /* Run disconnect handler in test.c */
627   if (bus_connection_dispatch_one_message (connection))
628     _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
629   
630   _dbus_assert (!dbus_connection_get_is_connected (connection));
631   dbus_connection_unref (connection);
632   connection = NULL;
633   _dbus_assert (!bus_test_client_listed (connection));
634   
635   socd.expected_kind = SERVICE_DELETED;
636   socd.expected_service_name = base_service;
637   socd.failed = FALSE;
638   socd.skip_connection = NULL;
639   
640   bus_test_clients_foreach (check_service_owner_changed_foreach,
641                             &socd);
642
643   dbus_free (base_service);
644   
645   if (socd.failed)
646     _dbus_assert_not_reached ("didn't get the expected ServiceOwnerChanged (deletion) messages");
647   
648   if (!check_no_leftovers (context))
649     _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
650 }
651
652 static void
653 kill_client_connection_unchecked (DBusConnection *connection)
654 {
655   /* This kills the connection without expecting it to affect
656    * the rest of the bus.
657    */  
658   _dbus_verbose ("Unchecked kill of connection %p\n", connection);
659
660   dbus_connection_ref (connection);
661   dbus_connection_disconnect (connection);
662   /* dispatching disconnect handler will unref once */
663   if (bus_connection_dispatch_one_message (connection))
664     _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
665
666   _dbus_assert (!bus_test_client_listed (connection));
667   dbus_connection_unref (connection);
668 }
669
670 typedef struct
671 {
672   dbus_bool_t failed;
673 } CheckNoMessagesData;
674
675 static dbus_bool_t
676 check_no_messages_foreach (DBusConnection *connection,
677                            void           *data)
678 {
679   CheckNoMessagesData *d = data;
680   DBusMessage *message;
681
682   message = pop_message_waiting_for_memory (connection);
683   if (message != NULL)
684     {
685       warn_unexpected (connection, message, "no messages");
686
687       d->failed = TRUE;
688     }
689
690   if (message)
691     dbus_message_unref (message);
692   return !d->failed;
693 }
694
695 static dbus_bool_t
696 check_no_leftovers (BusContext *context)
697 {
698   CheckNoMessagesData nmd;
699
700   nmd.failed = FALSE;
701   bus_test_clients_foreach (check_no_messages_foreach,
702                             &nmd);
703   
704   if (nmd.failed)
705     return FALSE;
706   else
707     return TRUE;
708 }
709
710 /* returns TRUE if the correct thing happens,
711  * but the correct thing may include OOM errors.
712  */
713 static dbus_bool_t
714 check_hello_message (BusContext     *context,
715                      DBusConnection *connection)
716 {
717   DBusMessage *message;
718   dbus_uint32_t serial;
719   dbus_bool_t retval;
720   DBusError error;
721   char *name;
722   char *acquired;
723
724   retval = FALSE;
725   dbus_error_init (&error);
726   name = NULL;
727   acquired = NULL;
728   message = NULL;
729
730   _dbus_verbose ("check_hello_message for %p\n", connection);
731   
732   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
733                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
734                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
735                                           "Hello");
736
737   if (message == NULL)
738     return TRUE;
739
740   if (!dbus_connection_send (connection, message, &serial))
741     {
742       dbus_message_unref (message);
743       return TRUE;
744     }
745
746   dbus_message_unref (message);
747   message = NULL;
748
749   /* send our message */
750   bus_test_run_clients_loop (TRUE);
751
752   dbus_connection_ref (connection); /* because we may get disconnected */
753   block_connection_until_message_from_bus (context, connection);
754
755   if (!dbus_connection_get_is_connected (connection))
756     {
757       _dbus_verbose ("connection was disconnected\n");
758       
759       dbus_connection_unref (connection);
760       
761       return TRUE;
762     }
763
764   dbus_connection_unref (connection);
765   
766   message = pop_message_waiting_for_memory (connection);
767   if (message == NULL)
768     {
769       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
770                   "Hello", serial, connection);
771       goto out;
772     }
773
774   verbose_message_received (connection, message);
775
776   if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
777     {
778       _dbus_warn ("Message has wrong sender %s\n",
779                   dbus_message_get_sender (message) ?
780                   dbus_message_get_sender (message) : "(none)");
781       goto out;
782     }
783   
784   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
785     {
786       if (dbus_message_is_error (message,
787                                  DBUS_ERROR_NO_MEMORY))
788         {
789           ; /* good, this is a valid response */
790         }
791       else
792         {
793           warn_unexpected (connection, message, "not this error");
794
795           goto out;
796         }
797     }
798   else
799     {
800       CheckServiceOwnerChangedData socd;
801       
802       if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
803         {
804           ; /* good, expected */
805         }
806       else
807         {
808           warn_unexpected (connection, message, "method return for Hello");
809
810           goto out;
811         }
812
813     retry_get_hello_name:
814       if (!dbus_message_get_args (message, &error,
815                                   DBUS_TYPE_STRING, &name,
816                                   DBUS_TYPE_INVALID))
817         {
818           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
819             {
820               _dbus_verbose ("no memory to get service name arg from hello\n");
821               dbus_error_free (&error);
822               _dbus_wait_for_memory ();
823               goto retry_get_hello_name;
824             }
825           else
826             {
827               _dbus_assert (dbus_error_is_set (&error));
828               _dbus_warn ("Did not get the expected single string argument to hello\n");
829               goto out;
830             }
831         }
832
833       _dbus_verbose ("Got hello name: %s\n", name);
834
835       while (!dbus_bus_set_base_service (connection, name))
836         _dbus_wait_for_memory ();
837       
838       socd.expected_kind = SERVICE_CREATED;
839       socd.expected_service_name = name;
840       socd.failed = FALSE;
841       socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
842       bus_test_clients_foreach (check_service_owner_changed_foreach,
843                                 &socd);
844       
845       if (socd.failed)
846         goto out;
847       
848       /* Client should also have gotten ServiceAcquired */
849       dbus_message_unref (message);
850       message = pop_message_waiting_for_memory (connection);
851       if (message == NULL)
852         {
853           _dbus_warn ("Expecting %s, got nothing\n",
854                       "ServiceAcquired");
855           goto out;
856         }
857       if (! dbus_message_is_signal (message, DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
858                                     "ServiceAcquired"))
859         {
860           _dbus_warn ("Expecting %s, got smthg else\n",
861                       "ServiceAcquired");
862           goto out;
863         }
864       
865     retry_get_acquired_name:
866       if (!dbus_message_get_args (message, &error,
867                                   DBUS_TYPE_STRING, &acquired,
868                                   DBUS_TYPE_INVALID))
869         {
870           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
871             {
872               _dbus_verbose ("no memory to get service name arg from acquired\n");
873               dbus_error_free (&error);
874               _dbus_wait_for_memory ();
875               goto retry_get_acquired_name;
876             }
877           else
878             {
879               _dbus_assert (dbus_error_is_set (&error));
880               _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
881               goto out;
882             }
883         }
884
885       _dbus_verbose ("Got acquired name: %s\n", acquired);
886
887       if (strcmp (acquired, name) != 0)
888         {
889           _dbus_warn ("Acquired name is %s but expected %s\n",
890                       acquired, name);
891           goto out;
892         }
893     }
894
895   if (!check_no_leftovers (context))
896     goto out;
897   
898   retval = TRUE;
899   
900  out:
901   dbus_error_free (&error);
902   
903   dbus_free (name);
904   dbus_free (acquired);
905   
906   if (message)
907     dbus_message_unref (message);
908   
909   return retval;
910 }
911
912 /* returns TRUE if the correct thing happens,
913  * but the correct thing may include OOM errors.
914  */
915 static dbus_bool_t
916 check_double_hello_message (BusContext     *context,
917                             DBusConnection *connection)
918 {
919   DBusMessage *message;
920   dbus_uint32_t serial;
921   dbus_bool_t retval;
922   DBusError error;
923
924   retval = FALSE;
925   dbus_error_init (&error);
926   message = NULL;
927
928   _dbus_verbose ("check_double_hello_message for %p\n", connection);
929   
930   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
931                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
932                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
933                                           "Hello");
934
935   if (message == NULL)
936     return TRUE;
937
938   if (!dbus_connection_send (connection, message, &serial))
939     {
940       dbus_message_unref (message);
941       return TRUE;
942     }
943
944   dbus_message_unref (message);
945   message = NULL;
946
947   /* send our message */
948   bus_test_run_clients_loop (TRUE);
949
950   dbus_connection_ref (connection); /* because we may get disconnected */
951   block_connection_until_message_from_bus (context, connection);
952
953   if (!dbus_connection_get_is_connected (connection))
954     {
955       _dbus_verbose ("connection was disconnected\n");
956       
957       dbus_connection_unref (connection);
958       
959       return TRUE;
960     }
961
962   dbus_connection_unref (connection);
963   
964   message = pop_message_waiting_for_memory (connection);
965   if (message == NULL)
966     {
967       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
968                   "Hello", serial, connection);
969       goto out;
970     }
971
972   verbose_message_received (connection, message);
973
974   if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
975     {
976       _dbus_warn ("Message has wrong sender %s\n",
977                   dbus_message_get_sender (message) ?
978                   dbus_message_get_sender (message) : "(none)");
979       goto out;
980     }
981   
982   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
983     {
984       warn_unexpected (connection, message, "method return for Hello");
985       goto out;
986     }
987
988   if (!check_no_leftovers (context))
989     goto out;
990   
991   retval = TRUE;
992   
993  out:
994   dbus_error_free (&error);
995   
996   if (message)
997     dbus_message_unref (message);
998   
999   return retval;
1000 }
1001
1002 /* returns TRUE if the correct thing happens,
1003  * but the correct thing may include OOM errors.
1004  */
1005 static dbus_bool_t
1006 check_get_connection_unix_user (BusContext     *context,
1007                                 DBusConnection *connection)
1008 {
1009   DBusMessage *message;
1010   dbus_uint32_t serial;
1011   dbus_bool_t retval;
1012   DBusError error;
1013   const char *base_service_name;
1014   dbus_uint32_t uid;
1015
1016   retval = FALSE;
1017   dbus_error_init (&error);
1018   message = NULL;
1019
1020   _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1021   
1022   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1023                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1024                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1025                                           "GetConnectionUnixUser");
1026
1027   if (message == NULL)
1028     return TRUE;
1029
1030   base_service_name = dbus_bus_get_base_service (connection);
1031
1032   if (!dbus_message_append_args (message, 
1033                                  DBUS_TYPE_STRING, base_service_name,
1034                                  DBUS_TYPE_INVALID))
1035     {
1036       dbus_message_unref (message);
1037       return TRUE;
1038     }
1039
1040   if (!dbus_connection_send (connection, message, &serial))
1041     {
1042       dbus_message_unref (message);
1043       return TRUE;
1044     }
1045
1046   /* send our message */
1047   bus_test_run_clients_loop (TRUE);
1048
1049   dbus_message_unref (message);
1050   message = NULL;
1051
1052   dbus_connection_ref (connection); /* because we may get disconnected */
1053   block_connection_until_message_from_bus (context, connection);
1054
1055   if (!dbus_connection_get_is_connected (connection))
1056     {
1057       _dbus_verbose ("connection was disconnected\n");
1058       
1059       dbus_connection_unref (connection);
1060       
1061       return TRUE;
1062     }
1063
1064   dbus_connection_unref (connection);
1065
1066   message = pop_message_waiting_for_memory (connection);
1067   if (message == NULL)
1068     {
1069       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1070                   "GetConnectionUnixUser", serial, connection);
1071       goto out;
1072     }
1073
1074   verbose_message_received (connection, message);
1075
1076   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1077     {
1078       if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1079         {
1080           ; /* good, this is a valid response */
1081         }
1082       else
1083         {
1084           warn_unexpected (connection, message, "not this error");
1085
1086           goto out;
1087         }
1088     }
1089   else
1090     {
1091       if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1092         {
1093           ; /* good, expected */
1094         }
1095       else
1096         {
1097           warn_unexpected (connection, message,
1098                            "method_return for GetConnectionUnixUser");
1099
1100           goto out;
1101         }
1102
1103     retry_get_property:
1104
1105       if (!dbus_message_get_args (message, &error,
1106                                   DBUS_TYPE_UINT32, &uid,
1107                                   DBUS_TYPE_INVALID))
1108         {
1109           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1110             {
1111               _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1112               dbus_error_free (&error);
1113               _dbus_wait_for_memory ();
1114               goto retry_get_property;
1115             }
1116           else
1117             {
1118               _dbus_assert (dbus_error_is_set (&error));
1119               _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1120               goto out;
1121             }
1122         }
1123     }
1124
1125   if (!check_no_leftovers (context))
1126     goto out;
1127
1128   retval = TRUE;
1129
1130  out:
1131   dbus_error_free (&error);
1132   
1133   if (message)
1134     dbus_message_unref (message);
1135   
1136   return retval;
1137 }
1138
1139 /* returns TRUE if the correct thing happens,
1140  * but the correct thing may include OOM errors.
1141  */
1142 static dbus_bool_t
1143 check_get_connection_unix_process_id (BusContext     *context,
1144                                       DBusConnection *connection)
1145 {
1146   DBusMessage *message;
1147   dbus_uint32_t serial;
1148   dbus_bool_t retval;
1149   DBusError error;
1150   const char *base_service_name;
1151   dbus_uint32_t pid;
1152
1153   retval = FALSE;
1154   dbus_error_init (&error);
1155   message = NULL;
1156
1157   _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
1158   
1159   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1160                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1161                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1162                                           "GetConnectionUnixProcessID");
1163
1164   if (message == NULL)
1165     return TRUE;
1166
1167   base_service_name = dbus_bus_get_base_service (connection);
1168
1169   if (!dbus_message_append_args (message, 
1170                                  DBUS_TYPE_STRING, base_service_name,
1171                                  DBUS_TYPE_INVALID))
1172     {
1173       dbus_message_unref (message);
1174       return TRUE;
1175     }
1176
1177   if (!dbus_connection_send (connection, message, &serial))
1178     {
1179       dbus_message_unref (message);
1180       return TRUE;
1181     }
1182
1183   /* send our message */
1184   bus_test_run_clients_loop (TRUE);
1185
1186   dbus_message_unref (message);
1187   message = NULL;
1188
1189   dbus_connection_ref (connection); /* because we may get disconnected */
1190   block_connection_until_message_from_bus (context, connection);
1191
1192   if (!dbus_connection_get_is_connected (connection))
1193     {
1194       _dbus_verbose ("connection was disconnected\n");
1195       
1196       dbus_connection_unref (connection);
1197       
1198       return TRUE;
1199     }
1200
1201   dbus_connection_unref (connection);
1202
1203   message = pop_message_waiting_for_memory (connection);
1204   if (message == NULL)
1205     {
1206       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1207                   "GetConnectionUnixProcessID", serial, connection);
1208       goto out;
1209     }
1210
1211   verbose_message_received (connection, message);
1212
1213   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1214     {
1215       if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
1216         {
1217           ; /* good, this is a valid response */
1218         }
1219       else
1220         {
1221           warn_unexpected (connection, message, "not this error");
1222
1223           goto out;
1224         }
1225     }
1226   else
1227     {
1228       if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1229         {
1230           ; /* good, expected */
1231         }
1232       else
1233         {
1234           warn_unexpected (connection, message,
1235                            "method_return for GetConnectionUnixProcessID");
1236
1237           goto out;
1238         }
1239
1240     retry_get_property:
1241
1242       if (!dbus_message_get_args (message, &error,
1243                                   DBUS_TYPE_UINT32, &pid,
1244                                   DBUS_TYPE_INVALID))
1245         {
1246           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1247             {
1248               _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
1249               dbus_error_free (&error);
1250               _dbus_wait_for_memory ();
1251               goto retry_get_property;
1252             }
1253           else
1254             {
1255               _dbus_assert (dbus_error_is_set (&error));
1256               _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
1257               goto out;
1258             }
1259         } else {
1260
1261           /* test if returned pid is the same as our own pid
1262            *
1263            * @todo It would probably be good to restructure the tests
1264            *       in a way so our parent is the bus that we're testing
1265            *       cause then we can test that the pid returned matches
1266            *       getppid()
1267            */
1268           if (pid != (dbus_uint32_t) _dbus_getpid ())
1269             {
1270               _dbus_assert (dbus_error_is_set (&error));
1271               _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
1272               goto out;
1273             }
1274         }
1275     }
1276
1277   if (!check_no_leftovers (context))
1278     goto out;
1279
1280   retval = TRUE;
1281
1282  out:
1283   dbus_error_free (&error);
1284   
1285   if (message)
1286     dbus_message_unref (message);
1287   
1288   return retval;
1289 }
1290
1291 /* returns TRUE if the correct thing happens,
1292  * but the correct thing may include OOM errors.
1293  */
1294 static dbus_bool_t
1295 check_add_match_all (BusContext     *context,
1296                      DBusConnection *connection)
1297 {
1298   DBusMessage *message;
1299   dbus_bool_t retval;
1300   dbus_uint32_t serial;
1301   DBusError error;
1302
1303   retval = FALSE;
1304   dbus_error_init (&error);
1305   message = NULL;
1306
1307   _dbus_verbose ("check_add_match_all for %p\n", connection);
1308   
1309   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1310                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1311                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1312                                           "AddMatch");
1313
1314   if (message == NULL)
1315     return TRUE;
1316
1317   /* empty string match rule matches everything */
1318   if (!dbus_message_append_args (message, DBUS_TYPE_STRING, "",
1319                                  DBUS_TYPE_INVALID))
1320     {
1321       dbus_message_unref (message);
1322       return TRUE;
1323     }
1324   
1325   if (!dbus_connection_send (connection, message, &serial))
1326     {
1327       dbus_message_unref (message);
1328       return TRUE;
1329     }
1330
1331   dbus_message_unref (message);
1332   message = NULL;
1333
1334   /* send our message */
1335   bus_test_run_clients_loop (TRUE);
1336
1337   dbus_connection_ref (connection); /* because we may get disconnected */
1338   block_connection_until_message_from_bus (context, connection);
1339
1340   if (!dbus_connection_get_is_connected (connection))
1341     {
1342       _dbus_verbose ("connection was disconnected\n");
1343       
1344       dbus_connection_unref (connection);
1345       
1346       return TRUE;
1347     }
1348
1349   dbus_connection_unref (connection);
1350   
1351   message = pop_message_waiting_for_memory (connection);
1352   if (message == NULL)
1353     {
1354       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1355                   "AddMatch", serial, connection);
1356       goto out;
1357     }
1358
1359   verbose_message_received (connection, message);
1360
1361   if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1362     {
1363       _dbus_warn ("Message has wrong sender %s\n",
1364                   dbus_message_get_sender (message) ?
1365                   dbus_message_get_sender (message) : "(none)");
1366       goto out;
1367     }
1368   
1369   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1370     {
1371       if (dbus_message_is_error (message,
1372                                  DBUS_ERROR_NO_MEMORY))
1373         {
1374           ; /* good, this is a valid response */
1375         }
1376       else
1377         {
1378           warn_unexpected (connection, message, "not this error");
1379
1380           goto out;
1381         }
1382     }
1383   else
1384     {
1385       if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1386         {
1387           ; /* good, expected */
1388           _dbus_assert (dbus_message_get_reply_serial (message) == serial);
1389         }
1390       else
1391         {
1392           warn_unexpected (connection, message, "method return for AddMatch");
1393
1394           goto out;
1395         }
1396     }
1397
1398   if (!check_no_leftovers (context))
1399     goto out;
1400   
1401   retval = TRUE;
1402   
1403  out:
1404   dbus_error_free (&error);
1405   
1406   if (message)
1407     dbus_message_unref (message);
1408   
1409   return retval;
1410 }
1411
1412 /* returns TRUE if the correct thing happens,
1413  * but the correct thing may include OOM errors.
1414  */
1415 static dbus_bool_t
1416 check_hello_connection (BusContext *context)
1417 {
1418   DBusConnection *connection;
1419   DBusError error;
1420
1421   dbus_error_init (&error);
1422
1423   connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
1424   if (connection == NULL)
1425     {
1426       _DBUS_ASSERT_ERROR_IS_SET (&error);
1427       dbus_error_free (&error);
1428       return TRUE;
1429     }
1430
1431   if (!bus_setup_debug_client (connection))
1432     {
1433       dbus_connection_disconnect (connection);
1434       dbus_connection_unref (connection);
1435       return TRUE;
1436     }
1437
1438   if (!check_hello_message (context, connection))
1439     return FALSE;
1440   
1441   if (dbus_bus_get_base_service (connection) == NULL)
1442     {
1443       /* We didn't successfully register, so we can't
1444        * do the usual kill_client_connection() checks
1445        */
1446       kill_client_connection_unchecked (connection);
1447     }
1448   else
1449     {
1450       if (!check_add_match_all (context, connection))
1451         return FALSE;
1452       
1453       kill_client_connection (context, connection);
1454     }
1455
1456   return TRUE;
1457 }
1458
1459 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
1460
1461 /* returns TRUE if the correct thing happens,
1462  * but the correct thing may include OOM errors.
1463  */
1464 static dbus_bool_t
1465 check_nonexistent_service_activation (BusContext     *context,
1466                                       DBusConnection *connection)
1467 {
1468   DBusMessage *message;
1469   dbus_uint32_t serial;
1470   dbus_bool_t retval;
1471   
1472   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1473                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
1474                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1475                                           "ActivateService");
1476
1477   if (message == NULL)
1478     return TRUE;
1479
1480   if (!dbus_message_append_args (message,
1481                                  DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
1482                                  DBUS_TYPE_UINT32, 0,
1483                                  DBUS_TYPE_INVALID))
1484     {
1485       dbus_message_unref (message);
1486       return TRUE;
1487     }
1488   
1489   if (!dbus_connection_send (connection, message, &serial))
1490     {
1491       dbus_message_unref (message);
1492       return TRUE;
1493     }
1494
1495   dbus_message_unref (message);
1496   message = NULL;
1497
1498   bus_test_run_everything (context);
1499   block_connection_until_message_from_bus (context, connection);
1500   bus_test_run_everything (context);
1501
1502   if (!dbus_connection_get_is_connected (connection))
1503     {
1504       _dbus_verbose ("connection was disconnected\n");
1505       return TRUE;
1506     }
1507   
1508   retval = FALSE;
1509   
1510   message = pop_message_waiting_for_memory (connection);
1511   if (message == NULL)
1512     {
1513       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1514                   "ActivateService", serial, connection);
1515       goto out;
1516     }
1517
1518   verbose_message_received (connection, message);
1519
1520   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1521     {
1522       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1523         {
1524           _dbus_warn ("Message has wrong sender %s\n",
1525                       dbus_message_get_sender (message) ?
1526                       dbus_message_get_sender (message) : "(none)");
1527           goto out;
1528         }
1529       
1530       if (dbus_message_is_error (message,
1531                                  DBUS_ERROR_NO_MEMORY))
1532         {
1533           ; /* good, this is a valid response */
1534         }
1535       else if (dbus_message_is_error (message,
1536                                       DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1537         {
1538           ; /* good, this is expected also */
1539         }
1540       else
1541         {
1542           warn_unexpected (connection, message, "not this error");
1543           goto out;
1544         }
1545     }
1546   else
1547     {
1548       _dbus_warn ("Did not expect to successfully activate %s\n",
1549                   NONEXISTENT_SERVICE_NAME);
1550       goto out;
1551     }
1552
1553   retval = TRUE;
1554   
1555  out:
1556   if (message)
1557     dbus_message_unref (message);
1558   
1559   return retval;
1560 }
1561
1562 /* returns TRUE if the correct thing happens,
1563  * but the correct thing may include OOM errors.
1564  */
1565 static dbus_bool_t
1566 check_nonexistent_service_auto_activation (BusContext     *context,
1567                                            DBusConnection *connection)
1568 {
1569   DBusMessage *message;
1570   dbus_uint32_t serial;
1571   dbus_bool_t retval;
1572     
1573   message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
1574                                           "/org/freedesktop/TestSuite",
1575                                           "org.freedesktop.TestSuite",
1576                                           "Echo");
1577   
1578   if (message == NULL)
1579     return TRUE;
1580
1581   dbus_message_set_auto_activation (message, TRUE);
1582  
1583   if (!dbus_connection_send (connection, message, &serial))
1584     {
1585       dbus_message_unref (message);
1586       return TRUE;
1587     }
1588
1589   dbus_message_unref (message);
1590   message = NULL;
1591
1592   bus_test_run_everything (context);
1593   block_connection_until_message_from_bus (context, connection);
1594   bus_test_run_everything (context);
1595
1596   if (!dbus_connection_get_is_connected (connection))
1597     {
1598       _dbus_verbose ("connection was disconnected\n");
1599       return TRUE;
1600     }
1601   
1602   retval = FALSE;
1603   
1604   message = pop_message_waiting_for_memory (connection);
1605
1606   if (message == NULL)
1607     {
1608       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1609                   "Echo message (auto activation)", serial, connection);
1610       goto out;
1611     }
1612
1613   verbose_message_received (connection, message);
1614
1615   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1616     {
1617       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1618         {
1619           _dbus_warn ("Message has wrong sender %s\n",
1620                       dbus_message_get_sender (message) ?
1621                       dbus_message_get_sender (message) : "(none)");
1622           goto out;
1623         }
1624       
1625       if (dbus_message_is_error (message,
1626                                  DBUS_ERROR_NO_MEMORY))
1627         {
1628           ; /* good, this is a valid response */
1629         }
1630       else if (dbus_message_is_error (message,
1631                                       DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1632         {
1633           ; /* good, this is expected also */
1634         }
1635       else
1636         {
1637           warn_unexpected (connection, message, "not this error");
1638           goto out;
1639         }
1640     }
1641   else
1642     {
1643       _dbus_warn ("Did not expect to successfully activate %s\n",
1644                   NONEXISTENT_SERVICE_NAME);
1645       goto out;
1646     }
1647
1648   retval = TRUE;
1649   
1650  out:
1651   if (message)
1652     dbus_message_unref (message);
1653   
1654   return retval;
1655 }
1656
1657 static dbus_bool_t
1658 check_base_service_activated (BusContext     *context,
1659                               DBusConnection *connection,
1660                               DBusMessage    *initial_message,
1661                               char          **base_service_p)
1662 {
1663   DBusMessage *message;
1664   dbus_bool_t retval;
1665   DBusError error;
1666   char *base_service, *base_service_from_bus, *old_owner;
1667   
1668   retval = FALSE;
1669   
1670   dbus_error_init (&error);
1671   base_service = NULL;
1672   old_owner = NULL;
1673   base_service_from_bus = NULL;
1674
1675   message = initial_message;
1676   dbus_message_ref (message);  
1677
1678   if (dbus_message_is_signal (message,
1679                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1680                               "ServiceOwnerChanged"))
1681     {
1682       CheckServiceOwnerChangedData socd;
1683
1684     reget_service_name_arg:
1685       base_service = NULL;
1686       old_owner = NULL;
1687       base_service_from_bus = NULL;
1688
1689       if (!dbus_message_get_args (message, &error,
1690                                   DBUS_TYPE_STRING, &base_service,
1691                                   DBUS_TYPE_STRING, &old_owner,
1692                                   DBUS_TYPE_STRING, &base_service_from_bus,
1693                                   DBUS_TYPE_INVALID))
1694         {
1695           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1696             {
1697               dbus_error_free (&error);
1698               dbus_free (base_service);
1699               dbus_free (old_owner);
1700               dbus_free (base_service_from_bus);
1701               _dbus_wait_for_memory ();
1702               goto reget_service_name_arg;
1703             }
1704           else
1705             {
1706               _dbus_warn ("Message %s doesn't have a service name: %s\n",
1707                           "ServiceOwnerChanged (creation)",
1708                           error.message);
1709               goto out;
1710             }
1711         }
1712
1713       if (*base_service != ':')
1714         {
1715           _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1716                       base_service);
1717           goto out;
1718         }
1719          
1720       if (strcmp (base_service, base_service_from_bus) != 0)
1721         {
1722           _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
1723                       base_service, base_service_from_bus);
1724           goto out;
1725         }
1726
1727       if (old_owner[0])
1728         {
1729           _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
1730                       old_owner);
1731           goto out;
1732         }
1733      
1734       socd.expected_kind = SERVICE_CREATED;
1735       socd.expected_service_name = base_service;
1736       socd.failed = FALSE;
1737       socd.skip_connection = connection;
1738       bus_test_clients_foreach (check_service_owner_changed_foreach,
1739                                 &socd);
1740       
1741       if (socd.failed)
1742         goto out;
1743     }
1744   else
1745     {
1746       warn_unexpected (connection, message, "ServiceOwnerChanged (creation) for base service");
1747
1748       goto out;
1749     }
1750
1751   retval = TRUE;
1752
1753   if (base_service_p)
1754     {
1755       *base_service_p = base_service;
1756       base_service = NULL;
1757     }
1758   
1759  out:
1760   if (message)
1761     dbus_message_unref (message);
1762   dbus_free (base_service);
1763   dbus_free (base_service_from_bus);
1764   dbus_free (old_owner);
1765   dbus_error_free (&error);
1766
1767   return retval;
1768 }
1769
1770 static dbus_bool_t
1771 check_service_activated (BusContext     *context,
1772                          DBusConnection *connection,
1773                          const char     *activated_name,
1774                          const char     *base_service_name,
1775                          DBusMessage    *initial_message)
1776 {
1777   DBusMessage *message;
1778   dbus_bool_t retval;
1779   DBusError error;
1780   dbus_uint32_t activation_result;
1781   
1782   retval = FALSE;
1783   
1784   dbus_error_init (&error);
1785
1786   message = initial_message;
1787   dbus_message_ref (message);
1788
1789   if (dbus_message_is_signal (message,
1790                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1791                               "ServiceOwnerChanged"))
1792     {
1793       CheckServiceOwnerChangedData socd;
1794       char *service_name, *base_service_from_bus, *old_owner;
1795
1796     reget_service_name_arg:
1797       service_name = NULL;
1798       old_owner = NULL;
1799       base_service_from_bus = NULL;
1800
1801       if (!dbus_message_get_args (message, &error,
1802                                   DBUS_TYPE_STRING, &service_name,
1803                                   DBUS_TYPE_STRING, &old_owner,
1804                                   DBUS_TYPE_STRING, &base_service_from_bus,
1805                                   DBUS_TYPE_INVALID))
1806         {
1807           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1808             {
1809               dbus_error_free (&error);
1810               dbus_free (service_name);
1811               dbus_free (old_owner);
1812               dbus_free (base_service_from_bus);
1813               _dbus_wait_for_memory ();
1814               goto reget_service_name_arg;
1815             }
1816           else
1817             {
1818               _dbus_warn ("Message %s doesn't have a service name: %s\n",
1819                           "ServiceOwnerChanged (creation)",
1820                           error.message);
1821               dbus_free (service_name);
1822               dbus_free (old_owner);
1823               dbus_free (base_service_from_bus);
1824               goto out;
1825             }
1826         }
1827
1828       if (strcmp (service_name, activated_name) != 0)
1829         {
1830           _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1831                       activated_name, service_name);
1832           dbus_free (service_name);
1833           dbus_free (old_owner);
1834           dbus_free (base_service_from_bus);
1835           goto out;
1836         }
1837
1838       if (strcmp (base_service_name, base_service_from_bus) != 0)
1839         {
1840           _dbus_warn ("ServiceOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
1841                       base_service_from_bus, base_service_name);
1842           dbus_free (service_name);
1843           dbus_free (old_owner);
1844           dbus_free (base_service_from_bus);
1845           goto out;
1846         }
1847       dbus_free (base_service_from_bus);
1848
1849       if (old_owner[0])
1850         {
1851           _dbus_warn ("expected a %s, got a %s\n",
1852                       "ServiceOwnerChanged (creation)",
1853                       "ServiceOwnerChanged (change)");
1854           dbus_free (service_name);
1855           dbus_free (old_owner);
1856           goto out;
1857         }
1858       dbus_free (old_owner);
1859
1860       socd.expected_kind = SERVICE_CREATED;
1861       socd.skip_connection = connection;
1862       socd.failed = FALSE;
1863       socd.expected_service_name = service_name;
1864       bus_test_clients_foreach (check_service_owner_changed_foreach,
1865                                 &socd);
1866
1867       dbus_free (service_name);
1868           
1869       if (socd.failed)
1870         goto out;
1871           
1872       dbus_message_unref (message);
1873       message = pop_message_waiting_for_memory (connection);
1874       if (message == NULL)
1875         {
1876           _dbus_warn ("Expected a reply to %s, got nothing\n",
1877                       "ActivateService");
1878           goto out;
1879         }
1880     }
1881   else
1882     {
1883       warn_unexpected (connection, message, "ServiceOwnerChanged for the activated name");
1884       
1885       goto out;
1886     }
1887   
1888   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1889     {
1890       warn_unexpected (connection, message, "reply to ActivateService");
1891
1892       goto out;
1893     }
1894
1895   activation_result = 0;
1896   if (!dbus_message_get_args (message, &error,
1897                               DBUS_TYPE_UINT32, &activation_result,
1898                               DBUS_TYPE_INVALID))
1899     {
1900       if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1901         {
1902           _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1903                       "ActivateService", error.message);
1904           goto out;
1905         }
1906
1907       dbus_error_free (&error);
1908     }
1909   else
1910     {
1911       if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1912         ; /* Good */
1913       else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1914         ; /* Good also */
1915       else
1916         {
1917           _dbus_warn ("Activation result was 0x%x, no good.\n",
1918                       activation_result);
1919           goto out;
1920         }
1921     }
1922
1923   dbus_message_unref (message);
1924   message = NULL;
1925       
1926   if (!check_no_leftovers (context))
1927     {
1928       _dbus_warn ("Messages were left over after verifying existent activation results\n");
1929       goto out;
1930     }
1931
1932   retval = TRUE;
1933   
1934  out:
1935   if (message)
1936     dbus_message_unref (message);
1937   dbus_error_free (&error);
1938   
1939   return retval;
1940 }
1941
1942 static dbus_bool_t
1943 check_service_auto_activated (BusContext     *context,
1944                               DBusConnection *connection,
1945                               const char     *activated_name,
1946                               const char     *base_service_name,
1947                               DBusMessage    *initial_message)
1948 {
1949   DBusMessage *message;
1950   dbus_bool_t retval;
1951   DBusError error;
1952   
1953   retval = FALSE;
1954   
1955   dbus_error_init (&error);
1956
1957   message = initial_message;
1958   dbus_message_ref (message);
1959
1960   if (dbus_message_is_signal (message,
1961                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1962                               "ServiceOwnerChanged"))
1963     {
1964       char *service_name;
1965       CheckServiceOwnerChangedData socd;
1966       
1967     reget_service_name_arg:
1968       if (!dbus_message_get_args (message, &error,
1969                                   DBUS_TYPE_STRING, &service_name,
1970                                   DBUS_TYPE_INVALID))
1971         {
1972           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1973             {
1974               dbus_error_free (&error);
1975               _dbus_wait_for_memory ();
1976               goto reget_service_name_arg;
1977             }
1978           else
1979             {
1980               _dbus_warn ("Message %s doesn't have a service name: %s\n",
1981                           "ServiceOwnerChanged",
1982                           error.message);
1983               dbus_error_free (&error);
1984               goto out;
1985             }
1986         }
1987       
1988       if (strcmp (service_name, activated_name) != 0)
1989         {
1990           _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1991                       activated_name, service_name);
1992           dbus_free (service_name);
1993           goto out;
1994         }
1995       
1996       socd.expected_kind = SERVICE_CREATED;
1997       socd.expected_service_name = service_name;
1998       socd.failed = FALSE;
1999       socd.skip_connection = connection; 
2000       bus_test_clients_foreach (check_service_owner_changed_foreach,
2001                                 &socd);
2002       
2003       dbus_free (service_name);
2004       
2005       if (socd.failed)
2006         goto out;
2007       
2008       /* Note that this differs from regular activation in that we don't get a
2009        * reply to ActivateService here.
2010        */
2011       
2012       dbus_message_unref (message);
2013       message = NULL;
2014     }
2015   else
2016     {
2017       warn_unexpected (connection, message, "ServiceOwnerChanged for the activated name");
2018       
2019       goto out;
2020     }
2021   
2022   retval = TRUE;
2023   
2024  out:
2025   if (message)
2026     dbus_message_unref (message);
2027   
2028   return retval;
2029 }
2030
2031 static dbus_bool_t
2032 check_service_deactivated (BusContext     *context,
2033                            DBusConnection *connection,
2034                            const char     *activated_name,
2035                            const char     *base_service)
2036 {
2037   dbus_bool_t retval;
2038   CheckServiceOwnerChangedData socd;
2039
2040   retval = FALSE;
2041   
2042   /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
2043    * service and the activated_name.  The base service
2044    * notification is required to come last.
2045    */
2046   socd.expected_kind = SERVICE_DELETED;
2047   socd.expected_service_name = activated_name;
2048   socd.failed = FALSE;
2049   socd.skip_connection = NULL;
2050   bus_test_clients_foreach (check_service_owner_changed_foreach,
2051                             &socd);      
2052
2053   if (socd.failed)
2054     goto out;
2055       
2056   socd.expected_kind = SERVICE_DELETED;
2057   socd.expected_service_name = base_service;
2058   socd.failed = FALSE;
2059   socd.skip_connection = NULL;
2060   bus_test_clients_foreach (check_service_owner_changed_foreach,
2061                             &socd);
2062
2063   if (socd.failed)
2064     goto out;
2065
2066   retval = TRUE;
2067   
2068  out:
2069   return retval;
2070 }
2071
2072 static dbus_bool_t
2073 check_send_exit_to_service (BusContext     *context,
2074                             DBusConnection *connection,
2075                             const char     *service_name,
2076                             const char     *base_service)
2077 {
2078   dbus_bool_t got_error;
2079   DBusMessage *message;
2080   dbus_uint32_t serial;
2081   dbus_bool_t retval;
2082   
2083   _dbus_verbose ("Sending exit message to the test service\n");
2084
2085   retval = FALSE;
2086   
2087   /* Kill off the test service by sending it a quit message */
2088   message = dbus_message_new_method_call (service_name,
2089                                           "/org/freedesktop/TestSuite",
2090                                           "org.freedesktop.TestSuite",
2091                                           "Exit");
2092       
2093   if (message == NULL)
2094     {
2095       /* Do this again; we still need the service to exit... */
2096       if (!check_send_exit_to_service (context, connection,
2097                                        service_name, base_service))
2098         goto out;
2099       
2100       return TRUE;
2101     }
2102       
2103   if (!dbus_connection_send (connection, message, &serial))
2104     {
2105       dbus_message_unref (message);
2106
2107       /* Do this again; we still need the service to exit... */
2108       if (!check_send_exit_to_service (context, connection,
2109                                        service_name, base_service))
2110         goto out;
2111       
2112       return TRUE;
2113     }
2114
2115   dbus_message_unref (message);
2116   message = NULL;
2117
2118   /* send message */
2119   bus_test_run_clients_loop (TRUE);
2120
2121   /* read it in and write it out to test service */
2122   bus_test_run_bus_loop (context, FALSE);
2123
2124   /* see if we got an error during message bus dispatching */
2125   bus_test_run_clients_loop (FALSE);
2126   message = borrow_message_waiting_for_memory (connection);
2127   got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
2128   if (message)
2129     {
2130       dbus_connection_return_message (connection, message);
2131       message = NULL;
2132     }
2133           
2134   if (!got_error)
2135     {
2136       /* If no error, wait for the test service to exit */
2137       block_connection_until_message_from_bus (context, connection);
2138               
2139       bus_test_run_everything (context);
2140     }
2141
2142   if (got_error)
2143     {
2144       message = pop_message_waiting_for_memory (connection);
2145       _dbus_assert (message != NULL);
2146
2147       if (dbus_message_get_reply_serial (message) != serial)
2148         {
2149           warn_unexpected (connection, message,
2150                            "error with the correct reply serial");
2151           goto out;
2152         }
2153       
2154       if (!dbus_message_is_error (message,
2155                                   DBUS_ERROR_NO_MEMORY))
2156         {
2157           warn_unexpected (connection, message,
2158                            "a no memory error from asking test service to exit");
2159           goto out;
2160         }
2161
2162       _dbus_verbose ("Got error %s when asking test service to exit\n",
2163                      dbus_message_get_error_name (message));
2164
2165       /* Do this again; we still need the service to exit... */
2166       if (!check_send_exit_to_service (context, connection,
2167                                        service_name, base_service))
2168         goto out;
2169     }
2170   else
2171     {
2172       if (!check_service_deactivated (context, connection,
2173                                       service_name, base_service))
2174         goto out;
2175
2176       /* Should now have a NoReply error from the Exit() method
2177        * call; it should have come after all the deactivation
2178        * stuff.
2179        */
2180       message = pop_message_waiting_for_memory (connection);
2181           
2182       if (message == NULL)
2183         {
2184           warn_unexpected (connection, NULL,
2185                            "reply to Exit() method call");
2186           goto out;
2187         }
2188       if (!dbus_message_is_error (message,
2189                                   DBUS_ERROR_NO_REPLY))
2190         {
2191           warn_unexpected (connection, NULL,
2192                            "NoReply error from Exit() method call");
2193           goto out;
2194         }
2195
2196       if (dbus_message_get_reply_serial (message) != serial)
2197         {
2198           warn_unexpected (connection, message,
2199                            "error with the correct reply serial");
2200           goto out;
2201         }
2202           
2203       _dbus_verbose ("Got error %s after test service exited\n",
2204                      dbus_message_get_error_name (message));
2205       
2206       if (!check_no_leftovers (context))
2207         {
2208           _dbus_warn ("Messages were left over after %s\n",
2209                       _DBUS_FUNCTION_NAME);
2210           goto out;
2211         }
2212     }
2213   
2214   retval = TRUE;
2215   
2216  out:
2217   if (message)
2218     dbus_message_unref (message);
2219   
2220   return retval;
2221 }
2222
2223 static dbus_bool_t
2224 check_got_error (BusContext     *context,
2225                  DBusConnection *connection,
2226                  const char     *first_error_name,
2227                  ...)
2228 {
2229   DBusMessage *message;
2230   dbus_bool_t retval;
2231   va_list ap;
2232   dbus_bool_t error_found;
2233   const char *error_name;
2234   
2235   retval = FALSE;
2236   
2237   message = pop_message_waiting_for_memory (connection);
2238   if (message == NULL)
2239     {
2240       _dbus_warn ("Did not get an expected error\n");
2241       goto out;
2242     }
2243
2244   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
2245     {
2246       warn_unexpected (connection, message, "an error");
2247
2248       goto out;
2249     }
2250
2251   error_found = FALSE;
2252
2253   va_start (ap, first_error_name);
2254   error_name = first_error_name;
2255   while (error_name != NULL)
2256     {
2257       if (dbus_message_is_error (message, error_name))
2258         {
2259           error_found = TRUE;
2260           break;
2261         }
2262       error_name = va_arg (ap, char*);
2263     }
2264   va_end (ap);
2265
2266   if (!error_found)
2267     {
2268       _dbus_warn ("Expected error %s or other, got %s instead\n",
2269                   first_error_name,
2270                   dbus_message_get_error_name (message));
2271       goto out;
2272     }
2273
2274   retval = TRUE;
2275   
2276  out:
2277   if (message)
2278     dbus_message_unref (message);
2279   
2280   return retval;
2281 }
2282           
2283 typedef enum
2284
2285   GOT_SERVICE_CREATED,
2286   GOT_SERVICE_DELETED,
2287   GOT_ERROR,
2288   GOT_SOMETHING_ELSE 
2289 } GotServiceInfo;
2290
2291 static GotServiceInfo
2292 check_got_service_info (DBusMessage *message)
2293 {
2294   GotServiceInfo message_kind;
2295
2296   if (dbus_message_is_signal (message,
2297                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
2298                               "ServiceOwnerChanged"))
2299     {
2300       DBusError error;
2301       char *service_name, *old_owner, *new_owner;
2302       dbus_error_init (&error);
2303
2304     reget_service_info_data:
2305       service_name = NULL;
2306       old_owner = NULL;
2307       new_owner = NULL;
2308
2309       dbus_message_get_args (message, &error,
2310                              DBUS_TYPE_STRING, &service_name,
2311                              DBUS_TYPE_STRING, &old_owner,
2312                              DBUS_TYPE_STRING, &new_owner,
2313                              DBUS_TYPE_INVALID);
2314       if (dbus_error_is_set (&error))
2315         {
2316           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2317             {
2318               dbus_error_free (&error);
2319               dbus_free (service_name);
2320               dbus_free (old_owner);
2321               dbus_free (new_owner);
2322               goto reget_service_info_data;
2323             }
2324           else
2325             {
2326               _dbus_warn ("unexpected arguments for ServiceOwnerChanged message");
2327               message_kind = GOT_SOMETHING_ELSE;
2328             }
2329         }
2330       else if (!old_owner[0])
2331         message_kind = GOT_SERVICE_CREATED;
2332       else if (!new_owner[0])
2333         message_kind = GOT_SERVICE_DELETED;
2334       else
2335         message_kind = GOT_SOMETHING_ELSE;
2336
2337       dbus_free (service_name);
2338       dbus_free (old_owner);
2339       dbus_free (new_owner);
2340       dbus_error_free (&error);
2341     }
2342   else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2343     message_kind = GOT_ERROR;
2344   else
2345     message_kind = GOT_SOMETHING_ELSE;
2346
2347   return message_kind;
2348 }
2349
2350 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
2351
2352 /* returns TRUE if the correct thing happens,
2353  * but the correct thing may include OOM errors.
2354  */
2355 static dbus_bool_t
2356 check_existent_service_activation (BusContext     *context,
2357                                    DBusConnection *connection)
2358 {
2359   DBusMessage *message;
2360   dbus_uint32_t serial;
2361   dbus_bool_t retval;
2362   char *base_service;
2363
2364   base_service = NULL;
2365   
2366   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
2367                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
2368                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
2369                                           "ActivateService");
2370
2371   if (message == NULL)
2372     return TRUE;
2373
2374   if (!dbus_message_append_args (message,
2375                                  DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
2376                                  DBUS_TYPE_UINT32, 0,
2377                                  DBUS_TYPE_INVALID))
2378     {
2379       dbus_message_unref (message);
2380       return TRUE;
2381     }
2382   
2383   if (!dbus_connection_send (connection, message, &serial))
2384     {
2385       dbus_message_unref (message);
2386       return TRUE;
2387     }
2388
2389   dbus_message_unref (message);
2390   message = NULL;
2391
2392   bus_test_run_everything (context);
2393
2394   /* now wait for the message bus to hear back from the activated
2395    * service.
2396    */
2397   block_connection_until_message_from_bus (context, connection);
2398
2399   bus_test_run_everything (context);
2400
2401   if (!dbus_connection_get_is_connected (connection))
2402     {
2403       _dbus_verbose ("connection was disconnected\n");
2404       return TRUE;
2405     }
2406   
2407   retval = FALSE;
2408   
2409   message = pop_message_waiting_for_memory (connection);
2410   if (message == NULL)
2411     {
2412       _dbus_warn ("Did not receive any messages after %s %d on %p\n",
2413                   "ActivateService", serial, connection);
2414       goto out;
2415     }
2416
2417   verbose_message_received (connection, message);
2418   _dbus_verbose ("  (after sending %s)\n", "ActivateService");
2419
2420   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2421     {
2422       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
2423         {
2424           _dbus_warn ("Message has wrong sender %s\n",
2425                       dbus_message_get_sender (message) ?
2426                       dbus_message_get_sender (message) : "(none)");
2427           goto out;
2428         }
2429       
2430       if (dbus_message_is_error (message,
2431                                  DBUS_ERROR_NO_MEMORY))
2432         {
2433           ; /* good, this is a valid response */
2434         }
2435       else if (dbus_message_is_error (message,
2436                                       DBUS_ERROR_SPAWN_CHILD_EXITED) ||
2437                dbus_message_is_error (message,
2438                                       DBUS_ERROR_SPAWN_EXEC_FAILED))
2439         {
2440           ; /* good, this is expected also */
2441         }
2442       else
2443         {
2444           _dbus_warn ("Did not expect error %s\n",
2445                       dbus_message_get_error_name (message));
2446           goto out;
2447         }
2448     }
2449   else
2450     {
2451       GotServiceInfo message_kind;
2452       
2453       if (!check_base_service_activated (context, connection,
2454                                          message, &base_service))
2455         goto out;
2456
2457       dbus_message_unref (message);
2458       message = NULL;
2459
2460       /* We may need to block here for the test service to exit or finish up */
2461       block_connection_until_message_from_bus (context, connection);
2462       
2463       message = dbus_connection_borrow_message (connection);
2464       if (message == NULL)
2465         {
2466           _dbus_warn ("Did not receive any messages after base service creation notification\n");
2467           goto out;
2468         }
2469
2470       message_kind = check_got_service_info (message);
2471
2472       dbus_connection_return_message (connection, message);
2473       message = NULL;
2474
2475       switch (message_kind)
2476         {
2477         case GOT_SOMETHING_ELSE:
2478           _dbus_warn ("Unexpected message after ActivateService "
2479                       "(should be an error or a service announcement");
2480           goto out;
2481
2482         case GOT_ERROR:
2483           if (!check_got_error (context, connection,
2484                                 DBUS_ERROR_SPAWN_CHILD_EXITED,
2485                                 DBUS_ERROR_NO_MEMORY,
2486                                 NULL))
2487             goto out;
2488           /* A service deleted should be coming along now after this error.
2489            * We can also get the error *after* the service deleted.
2490            */
2491
2492           /* fall through */
2493
2494         case GOT_SERVICE_DELETED:
2495           {
2496             /* The service started up and got a base address, but then
2497              * failed to register under EXISTENT_SERVICE_NAME
2498              */
2499             CheckServiceOwnerChangedData socd;
2500
2501             socd.expected_kind = SERVICE_DELETED;
2502             socd.expected_service_name = base_service;
2503             socd.failed = FALSE;
2504             socd.skip_connection = NULL;
2505             
2506             bus_test_clients_foreach (check_service_owner_changed_foreach,
2507                                       &socd);
2508
2509             if (socd.failed)
2510               goto out;
2511
2512             /* Now we should get an error about the service exiting
2513              * if we didn't get it before.
2514              */
2515             if (message_kind != GOT_ERROR)
2516               {
2517                 block_connection_until_message_from_bus (context, connection);
2518               
2519                 /* and process everything again */
2520                 bus_test_run_everything (context);
2521               
2522                 if (!check_got_error (context, connection,
2523                                       DBUS_ERROR_SPAWN_CHILD_EXITED,
2524                                       NULL))
2525                   goto out;
2526               }
2527             break;
2528           }
2529
2530         case GOT_SERVICE_CREATED:
2531           message = pop_message_waiting_for_memory (connection);
2532           if (message == NULL)
2533             {
2534               _dbus_warn ("Failed to pop message we just put back! "
2535                           "should have been a ServiceOwnerChanged (creation)\n");
2536               goto out;
2537             }
2538           
2539           if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
2540                                         base_service, message))
2541             goto out;
2542           
2543           dbus_message_unref (message);
2544           message = NULL;
2545
2546           if (!check_no_leftovers (context))
2547             {
2548               _dbus_warn ("Messages were left over after successful activation\n");
2549               goto out;
2550             }
2551
2552           if (!check_send_exit_to_service (context, connection,
2553                                            EXISTENT_SERVICE_NAME, base_service))
2554             goto out;
2555
2556           break;
2557         }
2558     }
2559
2560   retval = TRUE;
2561   
2562  out:
2563   if (message)
2564     dbus_message_unref (message);
2565
2566   if (base_service)
2567     dbus_free (base_service);
2568   
2569   return retval;
2570 }
2571
2572 /* returns TRUE if the correct thing happens,
2573  * but the correct thing may include OOM errors.
2574  */
2575 static dbus_bool_t
2576 check_segfault_service_activation (BusContext     *context,
2577                                    DBusConnection *connection)
2578 {
2579   DBusMessage *message;
2580   dbus_uint32_t serial;
2581   dbus_bool_t retval;
2582   
2583   message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
2584                                           DBUS_PATH_ORG_FREEDESKTOP_DBUS,
2585                                           DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
2586                                           "ActivateService");
2587
2588   if (message == NULL)
2589     return TRUE;
2590
2591   if (!dbus_message_append_args (message,
2592                                  DBUS_TYPE_STRING,
2593                                  "org.freedesktop.DBus.TestSuiteSegfaultService",
2594                                  DBUS_TYPE_UINT32, 0,
2595                                  DBUS_TYPE_INVALID))
2596     {
2597       dbus_message_unref (message);
2598       return TRUE;
2599     }
2600   
2601   if (!dbus_connection_send (connection, message, &serial))
2602     {
2603       dbus_message_unref (message);
2604       return TRUE;
2605     }
2606
2607   dbus_message_unref (message);
2608   message = NULL;
2609
2610   bus_test_run_everything (context);
2611   block_connection_until_message_from_bus (context, connection);
2612   bus_test_run_everything (context);
2613
2614   if (!dbus_connection_get_is_connected (connection))
2615     {
2616       _dbus_verbose ("connection was disconnected\n");
2617       return TRUE;
2618     }
2619   
2620   retval = FALSE;
2621   
2622   message = pop_message_waiting_for_memory (connection);
2623   if (message == NULL)
2624     {
2625       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2626                   "ActivateService", serial, connection);
2627       goto out;
2628     }
2629
2630   verbose_message_received (connection, message);
2631
2632   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2633     {
2634       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
2635         {
2636           _dbus_warn ("Message has wrong sender %s\n",
2637                       dbus_message_get_sender (message) ?
2638                       dbus_message_get_sender (message) : "(none)");
2639           goto out;
2640         }
2641       
2642       if (dbus_message_is_error (message,
2643                                  DBUS_ERROR_NO_MEMORY))
2644         {
2645           ; /* good, this is a valid response */
2646         }
2647       else if (dbus_message_is_error (message,
2648                                       DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2649         {
2650           ; /* good, this is expected also */
2651         }
2652       else
2653         {
2654           warn_unexpected (connection, message, "not this error");
2655
2656           goto out;
2657         }
2658     }
2659   else
2660     {
2661       _dbus_warn ("Did not expect to successfully activate segfault service\n");
2662       goto out;
2663     }
2664
2665   retval = TRUE;
2666   
2667  out:
2668   if (message)
2669     dbus_message_unref (message);
2670   
2671   return retval;
2672 }
2673
2674
2675 /* returns TRUE if the correct thing happens,
2676  * but the correct thing may include OOM errors.
2677  */
2678 static dbus_bool_t
2679 check_segfault_service_auto_activation (BusContext     *context,
2680                                         DBusConnection *connection)
2681 {
2682   DBusMessage *message;
2683   dbus_uint32_t serial;
2684   dbus_bool_t retval;
2685
2686   message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
2687                                           "/org/freedesktop/TestSuite",
2688                                           "org.freedesktop.TestSuite",
2689                                           "Echo");
2690   
2691   if (message == NULL)
2692     return TRUE;
2693
2694   dbus_message_set_auto_activation (message, TRUE);
2695   
2696   if (!dbus_connection_send (connection, message, &serial))
2697     {
2698       dbus_message_unref (message);
2699       return TRUE;
2700     }
2701
2702   dbus_message_unref (message);
2703   message = NULL;
2704
2705   bus_test_run_everything (context);
2706   block_connection_until_message_from_bus (context, connection);
2707   bus_test_run_everything (context);
2708
2709   if (!dbus_connection_get_is_connected (connection))
2710     {
2711       _dbus_verbose ("connection was disconnected\n");
2712       return TRUE;
2713     }
2714   
2715   retval = FALSE;
2716   
2717   message = pop_message_waiting_for_memory (connection);
2718   if (message == NULL)
2719     {
2720       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
2721                   "Echo message (auto activation)", serial, connection);
2722       goto out;
2723     }
2724
2725   verbose_message_received (connection, message);
2726
2727   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
2728     {
2729       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
2730         {
2731           _dbus_warn ("Message has wrong sender %s\n",
2732                       dbus_message_get_sender (message) ?
2733                       dbus_message_get_sender (message) : "(none)");
2734           goto out;
2735         }
2736       
2737       if (dbus_message_is_error (message,
2738                                  DBUS_ERROR_NO_MEMORY))
2739         {
2740           ; /* good, this is a valid response */
2741         }
2742       else if (dbus_message_is_error (message,
2743                                       DBUS_ERROR_SPAWN_CHILD_SIGNALED))
2744         {
2745           ; /* good, this is expected also */
2746         }
2747       else
2748         {
2749           warn_unexpected (connection, message, "not this error");
2750
2751           goto out;
2752         }
2753     }
2754   else
2755     {
2756       _dbus_warn ("Did not expect to successfully activate segfault service\n");
2757       goto out;
2758     }
2759
2760   retval = TRUE;
2761   
2762  out:
2763   if (message)
2764     dbus_message_unref (message);
2765   
2766   return retval;
2767 }
2768
2769 #define TEST_ECHO_MESSAGE "Test echo message"
2770
2771 /* returns TRUE if the correct thing happens,
2772  * but the correct thing may include OOM errors.
2773  */
2774 static dbus_bool_t
2775 check_existent_service_auto_activation (BusContext     *context,
2776                                         DBusConnection *connection)
2777 {
2778   DBusMessage *message;
2779   dbus_uint32_t serial;
2780   dbus_bool_t retval;
2781   char *base_service;
2782
2783   base_service = NULL;
2784
2785   message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
2786                                           "/org/freedesktop/TestSuite",
2787                                           "org.freedesktop.TestSuite",
2788                                           "Echo");
2789   
2790   if (message == NULL)
2791     return TRUE;
2792
2793   dbus_message_set_auto_activation (message, TRUE);
2794
2795   if (!dbus_message_append_args (message,
2796                                  DBUS_TYPE_STRING, TEST_ECHO_MESSAGE,
2797                                  DBUS_TYPE_INVALID))
2798     {
2799       dbus_message_unref (message);
2800       return TRUE;
2801     }
2802
2803   if (!dbus_connection_send (connection, message, &serial))
2804     {
2805       dbus_message_unref (message);
2806       return TRUE;
2807     }
2808
2809   dbus_message_unref (message);
2810   message = NULL;
2811
2812   bus_test_run_everything (context);
2813
2814   /* now wait for the message bus to hear back from the activated
2815    * service.
2816    */
2817   block_connection_until_message_from_bus (context, connection);
2818   bus_test_run_everything (context);
2819
2820   if (!dbus_connection_get_is_connected (connection))
2821     {
2822       _dbus_verbose ("connection was disconnected\n");
2823       return TRUE;
2824     }
2825
2826   retval = FALSE;
2827   
2828   message = pop_message_waiting_for_memory (connection);
2829   if (message == NULL)
2830     {
2831       _dbus_warn ("Did not receive any messages after auto activation %d on %p\n",
2832                   serial, connection);
2833       goto out;
2834     }
2835
2836   verbose_message_received (connection, message);
2837   _dbus_verbose ("  (after sending %s)\n", "auto activation");
2838
2839   /* we should get zero or two ServiceOwnerChanged signals */
2840   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
2841     {
2842       GotServiceInfo message_kind;
2843
2844       if (!check_base_service_activated (context, connection,
2845                                          message, &base_service))
2846         goto out;
2847
2848       dbus_message_unref (message);
2849       message = NULL;
2850
2851       /* We may need to block here for the test service to exit or finish up */
2852       block_connection_until_message_from_bus (context, connection);
2853
2854       /* Should get a service creation notification for the activated
2855        * service name, or a service deletion on the base service name
2856        */
2857       message = dbus_connection_borrow_message (connection);
2858       if (message == NULL)
2859         {
2860           _dbus_warn ("No message after auto activation "
2861                       "(should be a service announcement)");
2862           dbus_connection_return_message (connection, message);
2863           message = NULL;
2864           goto out;
2865         }
2866
2867       message_kind = check_got_service_info (message);
2868
2869       dbus_connection_return_message (connection, message);
2870       message = NULL;
2871
2872       switch (message_kind) 
2873         {
2874         case GOT_SERVICE_CREATED:
2875           message = pop_message_waiting_for_memory (connection);
2876           if (message == NULL)
2877             {
2878               _dbus_warn ("Failed to pop message we just put back! "
2879                           "should have been a ServiceOwnerChanged (creation)\n");
2880               goto out;
2881             }
2882             
2883           /* Check that ServiceOwnerChanged (creation) was correctly received */
2884           if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
2885                                              base_service, message))
2886             goto out;
2887           
2888           dbus_message_unref (message);
2889           message = NULL;
2890
2891           break;
2892
2893         case GOT_SERVICE_DELETED:
2894           {
2895             /* The service started up and got a base address, but then
2896              * failed to register under EXISTENT_SERVICE_NAME
2897              */
2898             CheckServiceOwnerChangedData socd;
2899           
2900             socd.expected_kind = SERVICE_DELETED;
2901             socd.expected_service_name = base_service;
2902             socd.failed = FALSE;
2903             socd.skip_connection = NULL;
2904             bus_test_clients_foreach (check_service_owner_changed_foreach,
2905                                       &socd);
2906
2907             if (socd.failed)
2908               goto out;
2909
2910             break;
2911           }
2912
2913         case GOT_ERROR:
2914         case GOT_SOMETHING_ELSE:
2915           _dbus_warn ("Unexpected message after auto activation\n");
2916           goto out;
2917         }
2918     }
2919
2920   /* OK, now we've dealt with ServiceOwnerChanged signals, now should
2921    * come the method reply (or error) from the initial method call
2922    */
2923
2924   /* Note: if this test is run in OOM mode, it will block when the bus
2925    * doesn't send a reply due to OOM.
2926    */
2927   block_connection_until_message_from_bus (context, connection);
2928       
2929   message = pop_message_waiting_for_memory (connection);
2930   if (message == NULL)
2931     {
2932       _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
2933       goto out;
2934     }
2935
2936   if (dbus_message_get_reply_serial (message) != serial)
2937     {
2938       _dbus_warn ("Wrong reply serial\n");
2939       goto out;
2940     }
2941
2942   dbus_message_unref (message);
2943   message = NULL;
2944       
2945   if (!check_send_exit_to_service (context, connection,
2946                                    EXISTENT_SERVICE_NAME,
2947                                    base_service))
2948     goto out;
2949   
2950   retval = TRUE;
2951
2952  out:
2953   if (message)
2954     dbus_message_unref (message);
2955
2956   if (base_service)
2957     dbus_free (base_service);
2958
2959   return retval;
2960 }
2961
2962 typedef struct
2963 {
2964   Check1Func func;
2965   BusContext *context;
2966 } Check1Data;
2967
2968 static dbus_bool_t
2969 check_oom_check1_func (void *data)
2970 {
2971   Check1Data *d = data;
2972
2973   if (! (* d->func) (d->context))
2974     return FALSE;
2975   
2976   if (!check_no_leftovers (d->context))
2977     {
2978       _dbus_warn ("Messages were left over, should be covered by test suite\n");
2979       return FALSE;
2980     }
2981
2982   return TRUE;
2983 }
2984
2985 static void
2986 check1_try_iterations (BusContext *context,
2987                        const char *description,
2988                        Check1Func  func)
2989 {
2990   Check1Data d;
2991
2992   d.func = func;
2993   d.context = context;
2994
2995   if (!_dbus_test_oom_handling (description, check_oom_check1_func,
2996                                 &d))
2997     _dbus_assert_not_reached ("test failed");
2998 }
2999
3000 typedef struct
3001 {
3002   Check2Func func;
3003   BusContext *context;
3004   DBusConnection *connection;
3005 } Check2Data;
3006
3007 static dbus_bool_t
3008 check_oom_check2_func (void *data)
3009 {
3010   Check2Data *d = data;
3011
3012   if (! (* d->func) (d->context, d->connection))
3013     return FALSE;
3014   
3015   if (!check_no_leftovers (d->context))
3016     {
3017       _dbus_warn ("Messages were left over, should be covered by test suite");
3018       return FALSE;
3019     }
3020
3021   return TRUE;
3022 }
3023
3024 static void
3025 check2_try_iterations (BusContext     *context,
3026                        DBusConnection *connection,
3027                        const char     *description,
3028                        Check2Func      func)
3029 {
3030   Check2Data d;
3031
3032   d.func = func;
3033   d.context = context;
3034   d.connection = connection;
3035   
3036   if (!_dbus_test_oom_handling (description, check_oom_check2_func,
3037                                 &d))
3038     {
3039       _dbus_warn ("%s failed during oom\n", description);
3040       _dbus_assert_not_reached ("test failed");
3041     }
3042 }
3043
3044 dbus_bool_t
3045 bus_dispatch_test (const DBusString *test_data_dir)
3046 {
3047   BusContext *context;
3048   DBusConnection *foo;
3049   DBusConnection *bar;
3050   DBusConnection *baz;
3051   DBusError error;
3052
3053   dbus_error_init (&error);
3054   
3055   context = bus_context_new_test (test_data_dir,
3056                                   "valid-config-files/debug-allow-all.conf");
3057   if (context == NULL)
3058     return FALSE;
3059   
3060   foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3061   if (foo == NULL)
3062     _dbus_assert_not_reached ("could not alloc connection");
3063
3064   if (!bus_setup_debug_client (foo))
3065     _dbus_assert_not_reached ("could not set up connection");
3066
3067   if (!check_hello_message (context, foo))
3068     _dbus_assert_not_reached ("hello message failed");
3069
3070   if (!check_double_hello_message (context, foo))
3071     _dbus_assert_not_reached ("double hello message failed");
3072
3073   if (!check_add_match_all (context, foo))
3074     _dbus_assert_not_reached ("AddMatch message failed");
3075   
3076   bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
3077   if (bar == NULL)
3078     _dbus_assert_not_reached ("could not alloc connection");
3079
3080   if (!bus_setup_debug_client (bar))
3081     _dbus_assert_not_reached ("could not set up connection");
3082
3083   if (!check_hello_message (context, bar))
3084     _dbus_assert_not_reached ("hello message failed");
3085
3086   if (!check_add_match_all (context, bar))
3087     _dbus_assert_not_reached ("AddMatch message failed");
3088   
3089   baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
3090   if (baz == NULL)
3091     _dbus_assert_not_reached ("could not alloc connection");
3092
3093   if (!bus_setup_debug_client (baz))
3094     _dbus_assert_not_reached ("could not set up connection");
3095
3096   if (!check_hello_message (context, baz))
3097     _dbus_assert_not_reached ("hello message failed");
3098
3099   if (!check_add_match_all (context, baz))
3100     _dbus_assert_not_reached ("AddMatch message failed");
3101
3102   if (!check_get_connection_unix_user (context, baz))
3103     _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
3104
3105   if (!check_get_connection_unix_process_id (context, baz))
3106     _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
3107   
3108   if (!check_no_leftovers (context))
3109     {
3110       _dbus_warn ("Messages were left over after setting up initial connections");
3111       _dbus_assert_not_reached ("initial connection setup failed");
3112     }
3113   
3114   check1_try_iterations (context, "create_and_hello",
3115                          check_hello_connection);
3116   
3117   check2_try_iterations (context, foo, "nonexistent_service_activation",
3118                          check_nonexistent_service_activation);
3119
3120   check2_try_iterations (context, foo, "segfault_service_activation",
3121                          check_segfault_service_activation);
3122   
3123   check2_try_iterations (context, foo, "existent_service_activation",
3124                          check_existent_service_activation);
3125   
3126   check2_try_iterations (context, foo, "nonexistent_service_auto_activation",
3127                          check_nonexistent_service_auto_activation);
3128   
3129   check2_try_iterations (context, foo, "segfault_service_auto_activation",
3130                          check_segfault_service_auto_activation);
3131
3132 #if 0
3133   /* Note: need to resolve some issues with the testing code in order to run
3134    * this in oom (handle that we sometimes don't get replies back from the bus
3135    * when oom happens, without blocking the test).
3136    */
3137   check2_try_iterations (context, foo, "existent_service_auto_activation",
3138                          check_existent_service_auto_activation);
3139 #endif
3140   
3141   if (!check_existent_service_auto_activation (context, foo))
3142     _dbus_assert_not_reached ("existent service auto activation failed");
3143
3144   _dbus_verbose ("Disconnecting foo, bar, and baz\n");
3145
3146   kill_client_connection_unchecked (foo);
3147   kill_client_connection_unchecked (bar);
3148   kill_client_connection_unchecked (baz);
3149
3150   bus_context_unref (context);
3151   
3152   return TRUE;
3153 }
3154
3155 dbus_bool_t
3156 bus_dispatch_sha1_test (const DBusString *test_data_dir)
3157 {
3158   BusContext *context;
3159   DBusConnection *foo;
3160   DBusError error;
3161
3162   dbus_error_init (&error);
3163   
3164   /* Test SHA1 authentication */
3165   _dbus_verbose ("Testing SHA1 context\n");
3166   
3167   context = bus_context_new_test (test_data_dir,
3168                                   "valid-config-files/debug-allow-all-sha1.conf");
3169   if (context == NULL)
3170     return FALSE;
3171
3172   foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
3173   if (foo == NULL)
3174     _dbus_assert_not_reached ("could not alloc connection");
3175
3176   if (!bus_setup_debug_client (foo))
3177     _dbus_assert_not_reached ("could not set up connection");
3178
3179   if (!check_hello_message (context, foo))
3180     _dbus_assert_not_reached ("hello message failed");
3181
3182   if (!check_add_match_all (context, foo))
3183     _dbus_assert_not_reached ("addmatch message failed");
3184   
3185   if (!check_no_leftovers (context))
3186     {
3187       _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
3188       _dbus_assert_not_reached ("initial connection setup failed");
3189     }
3190   
3191   check1_try_iterations (context, "create_and_hello_sha1",
3192                          check_hello_connection);
3193
3194   kill_client_connection_unchecked (foo);
3195
3196   bus_context_unref (context);
3197
3198   return TRUE;
3199 }
3200
3201 #endif /* DBUS_BUILD_TESTS */