-
[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  *
7  * Licensed under the Academic Free License version 1.2
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dispatch.h"
26 #include "connection.h"
27 #include "driver.h"
28 #include "services.h"
29 #include "utils.h"
30 #include "bus.h"
31 #include "test.h"
32 #include <dbus/dbus-internals.h>
33 #include <string.h>
34
35 static int message_handler_slot = -1;
36 static int message_handler_slot_refcount;
37
38 typedef struct
39 {
40   DBusMessage    *message;
41   BusTransaction *transaction;
42   DBusError      *error;
43 } SendMessageData;
44
45 static dbus_bool_t
46 send_one_message (DBusConnection *connection, void *data)
47 {
48   SendMessageData *d = data;
49   
50   if (!bus_connection_is_active (connection))
51     return TRUE;
52
53   if (!bus_transaction_send_message (d->transaction,
54                                      connection,
55                                      d->message))
56     {
57       BUS_SET_OOM (d->error);
58       return FALSE;
59     }
60
61   return TRUE;
62 }
63
64 dbus_bool_t
65 bus_dispatch_broadcast_message (BusTransaction *transaction,
66                                 DBusMessage    *message,
67                                 DBusError      *error)
68 {
69   DBusError tmp_error;
70   SendMessageData d;
71   BusConnections *connections;
72
73   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
74   
75   _dbus_assert (dbus_message_get_sender (message) != NULL);
76
77   connections = bus_transaction_get_connections (transaction);
78   
79   dbus_error_init (&tmp_error);
80   d.message = message;
81   d.transaction = transaction;
82   d.error = &tmp_error;
83   
84   bus_connections_foreach (connections, send_one_message, &d);
85
86   if (dbus_error_is_set (&tmp_error))
87     {
88       dbus_move_error (&tmp_error, error);
89       return FALSE;
90     }
91   else
92     return TRUE;
93 }
94
95 static dbus_bool_t
96 send_service_nonexistent_error (BusTransaction *transaction,
97                                 DBusConnection *connection,
98                                 const char     *service_name,
99                                 DBusMessage    *in_reply_to,
100                                 DBusError      *error)
101 {
102   DBusMessage *error_reply;
103   DBusString error_message;
104   const char *error_str;
105
106   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
107   
108   /* Trying to send a message to a non-existant service,
109    * bounce back an error message.
110    */
111           
112   if (!_dbus_string_init (&error_message))
113     {
114       BUS_SET_OOM (error);
115       return FALSE;
116     }
117
118   if (!_dbus_string_append (&error_message, "Service \"") ||
119       !_dbus_string_append (&error_message, service_name) ||
120       !_dbus_string_append (&error_message, "\" does not exist"))
121     {
122       _dbus_string_free (&error_message);
123       BUS_SET_OOM (error);
124       return FALSE;
125     }
126               
127   error_str = _dbus_string_get_const_data (&error_message);
128   error_reply = dbus_message_new_error_reply (in_reply_to,
129                                               DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
130                                               error_str);
131
132   _dbus_string_free (&error_message);
133               
134   if (error_reply == NULL)
135     {
136       BUS_SET_OOM (error);
137       return FALSE;
138     }
139
140   if (!dbus_message_set_sender (error_reply, DBUS_SERVICE_DBUS))
141     {
142       dbus_message_unref (error_reply);
143       BUS_SET_OOM (error);
144       return FALSE;
145     }      
146   
147   if (!bus_transaction_send_message (transaction, connection, error_reply))
148     {
149       dbus_message_unref (error_reply);
150       BUS_SET_OOM (error);
151       return FALSE;
152     }
153               
154   dbus_message_unref (error_reply);
155
156   return TRUE;
157 }
158
159 static void
160 bus_dispatch (DBusConnection *connection,
161               DBusMessage    *message)
162 {
163   const char *sender, *service_name, *message_name;
164   DBusError error;
165   BusTransaction *transaction;
166   BusContext *context;
167   
168   transaction = NULL;
169   dbus_error_init (&error);
170   
171   context = bus_connection_get_context (connection);
172   _dbus_assert (context != NULL);
173   
174   /* If we can't even allocate an OOM error, we just go to sleep
175    * until we can.
176    */
177   while (!bus_connection_preallocate_oom_error (connection))
178     _dbus_wait_for_memory ();
179   
180   /* Ref connection in case we disconnect it at some point in here */
181   dbus_connection_ref (connection);
182
183   service_name = dbus_message_get_service (message);
184   message_name = dbus_message_get_name (message);
185
186   _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
187
188   _dbus_verbose ("DISPATCH: %s to %s\n",
189                  message_name, service_name ? service_name : "peer");
190   
191   /* If service_name is NULL, this is a message to the bus daemon, not intended
192    * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
193    * immediately, especially disconnection messages.
194    */
195   if (service_name == NULL)
196     {      
197       if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
198         bus_connection_disconnected (connection);
199
200       /* DBusConnection also handles some of these automatically, we leave
201        * it to do so.
202        */
203       goto out;
204     }
205
206   _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
207   
208   /* Create our transaction */
209   transaction = bus_transaction_new (context);
210   if (transaction == NULL)
211     {
212       BUS_SET_OOM (&error);
213       goto out;
214     }
215   
216   /* Assign a sender to the message */
217   if (bus_connection_is_active (connection))
218     {
219       sender = bus_connection_get_name (connection);
220       _dbus_assert (sender != NULL);
221
222       if (!dbus_message_set_sender (message, sender))
223         {
224           BUS_SET_OOM (&error);
225           goto out;
226         }
227
228       /* We need to refetch the service name here, because
229        * dbus_message_set_sender can cause the header to be
230        * reallocated, and thus the service_name pointer will become
231        * invalid.
232        */
233       service_name = dbus_message_get_service (message);
234     }
235
236   if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
237     {
238       if (!bus_driver_handle_message (connection, transaction, message, &error))
239         goto out;
240     }
241   else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
242     {
243       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
244       dbus_connection_disconnect (connection);
245     }
246   /* FIXME what if we un-special-case this service and just have a flag
247    * on services that all service owners will get messages to it, not just
248    * the primary owner.
249    */
250   else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
251     {
252       if (!bus_dispatch_broadcast_message (transaction, message, &error))
253         goto out;
254     }
255   else  /* route to named service */
256     {
257       DBusString service_string;
258       BusService *service;
259       BusRegistry *registry;
260
261       registry = bus_connection_get_registry (connection);
262       
263       _dbus_string_init_const (&service_string, service_name);
264       service = bus_registry_lookup (registry, &service_string);
265
266       if (service == NULL)
267         {
268           if (!send_service_nonexistent_error (transaction, connection,
269                                                service_name,
270                                                message, &error))
271             goto out;
272         }
273       else
274         {
275           _dbus_assert (bus_service_get_primary_owner (service) != NULL);
276       
277           /* Dispatch the message */
278           if (!bus_transaction_send_message (transaction,
279                                              bus_service_get_primary_owner (service),
280                                              message))
281             {
282               BUS_SET_OOM (&error);
283               goto out;
284             }
285         }
286     }
287   
288  out:
289   if (dbus_error_is_set (&error))
290     {
291       if (!dbus_connection_get_is_connected (connection))
292         {
293           /* If we disconnected it, we won't bother to send it any error
294            * messages.
295            */
296         }
297       else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
298         {
299           bus_connection_send_oom_error (connection, message);
300
301           /* cancel transaction due to OOM */
302           if (transaction != NULL)
303             {
304               bus_transaction_cancel_and_free (transaction);
305               transaction = NULL;
306             }
307         }
308       else
309         {
310           /* Try to send the real error, if no mem to do that, send
311            * the OOM error
312            */
313           _dbus_assert (transaction != NULL);
314           
315           if (!bus_transaction_send_error_reply (transaction, connection,
316                                                  &error, message))
317             {
318               bus_connection_send_oom_error (connection, message);
319
320               /* cancel transaction due to OOM */
321               if (transaction != NULL)
322                 {
323                   bus_transaction_cancel_and_free (transaction);
324                   transaction = NULL;
325                 }
326             }
327         }
328       
329       dbus_error_free (&error);
330     }
331
332   if (transaction != NULL)
333     {
334       bus_transaction_execute_and_free (transaction);
335     }
336
337   dbus_connection_unref (connection);
338 }
339
340 static DBusHandlerResult
341 bus_dispatch_message_handler (DBusMessageHandler *handler,
342                               DBusConnection     *connection,
343                               DBusMessage        *message,
344                               void               *user_data)
345 {
346   bus_dispatch (connection, message);
347   
348   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
349 }
350
351 static dbus_bool_t
352 message_handler_slot_ref (void)
353 {
354   if (message_handler_slot < 0)
355     {
356       message_handler_slot = dbus_connection_allocate_data_slot ();
357       
358       if (message_handler_slot < 0)
359         return FALSE;
360
361       _dbus_assert (message_handler_slot_refcount == 0);
362     }  
363
364   message_handler_slot_refcount += 1;
365
366   return TRUE;
367 }
368
369 static void
370 message_handler_slot_unref (void)
371 {
372   _dbus_assert (message_handler_slot_refcount > 0);
373
374   message_handler_slot_refcount -= 1;
375   
376   if (message_handler_slot_refcount == 0)
377     {
378       dbus_connection_free_data_slot (message_handler_slot);
379       message_handler_slot = -1;
380     }
381 }
382
383 static void
384 free_message_handler (void *data)
385 {
386   DBusMessageHandler *handler = data;
387   
388   _dbus_assert (message_handler_slot >= 0);
389   _dbus_assert (message_handler_slot_refcount > 0);
390   
391   dbus_message_handler_unref (handler);
392   message_handler_slot_unref ();
393 }
394
395 dbus_bool_t
396 bus_dispatch_add_connection (DBusConnection *connection)
397 {
398   DBusMessageHandler *handler;
399
400   if (!message_handler_slot_ref ())
401     return FALSE;
402   
403   handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);  
404   if (handler == NULL)
405     {
406       message_handler_slot_unref ();
407       return FALSE;
408     }    
409   
410   if (!dbus_connection_add_filter (connection, handler))
411     {
412       dbus_message_handler_unref (handler);
413       message_handler_slot_unref ();
414       
415       return FALSE;
416     }
417
418   _dbus_assert (message_handler_slot >= 0);
419   _dbus_assert (message_handler_slot_refcount > 0);
420   
421   if (!dbus_connection_set_data (connection,
422                                  message_handler_slot,
423                                  handler,
424                                  free_message_handler))
425     {
426       dbus_message_handler_unref (handler);
427       message_handler_slot_unref ();
428
429       return FALSE;
430     }
431
432   return TRUE;
433 }
434
435 void
436 bus_dispatch_remove_connection (DBusConnection *connection)
437 {
438   /* Here we tell the bus driver that we want to get off. */
439   bus_driver_remove_connection (connection);
440
441   dbus_connection_set_data (connection,
442                             message_handler_slot,
443                             NULL, NULL);
444 }
445
446 #ifdef DBUS_BUILD_TESTS
447
448 typedef dbus_bool_t (* Check1Func) (BusContext     *context);
449 typedef dbus_bool_t (* Check2Func) (BusContext     *context,
450                                     DBusConnection *connection);
451
452 static dbus_bool_t check_no_leftovers (BusContext *context);
453
454 typedef struct
455 {
456   const char *expected_service_name;
457   dbus_bool_t failed;
458 } CheckServiceDeletedData;
459
460 static dbus_bool_t
461 check_service_deleted_foreach (DBusConnection *connection,
462                                void           *data)
463 {
464   CheckServiceDeletedData *d = data;
465   DBusMessage *message;
466   DBusError error;
467   char *service_name;
468
469   dbus_error_init (&error);
470   d->failed = TRUE;
471   service_name = NULL;
472   
473   message = dbus_connection_pop_message (connection);
474   if (message == NULL)
475     {
476       _dbus_warn ("Did not receive a message on %p, expecting %s\n",
477                   connection, DBUS_MESSAGE_SERVICE_DELETED);
478       goto out;
479     }
480   else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED))
481     {
482       _dbus_warn ("Received message %s on %p, expecting %s\n",
483                   dbus_message_get_name (message),
484                   connection, DBUS_MESSAGE_SERVICE_DELETED);
485       goto out;
486     }
487   else
488     {
489       if (!dbus_message_get_args (message, &error,
490                                   DBUS_TYPE_STRING, &service_name,
491                                   DBUS_TYPE_INVALID))
492         {
493           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
494             {
495               _dbus_verbose ("no memory to get service name arg\n");
496             }
497           else
498             {
499               _dbus_assert (dbus_error_is_set (&error));
500               _dbus_warn ("Did not get the expected single string argument\n");
501               goto out;
502             }
503         }
504       else if (strcmp (service_name, d->expected_service_name) != 0)
505         {
506           _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
507                       d->expected_service_name,
508                       service_name);
509           goto out;
510         }
511     }
512
513   d->failed = FALSE;
514   
515  out:
516   dbus_free (service_name);
517   dbus_error_free (&error);
518   
519   if (message)
520     dbus_message_unref (message);
521
522   return !d->failed;
523 }
524
525 static void
526 kill_client_connection (BusContext     *context,
527                         DBusConnection *connection)
528 {
529   char *base_service;
530   const char *s;
531   CheckServiceDeletedData csdd;
532
533   _dbus_verbose ("killing connection %p\n", connection);
534   
535   s = dbus_bus_get_base_service (connection);
536   _dbus_assert (s != NULL);
537
538   while ((base_service = _dbus_strdup (s)) == NULL)
539     _dbus_wait_for_memory ();
540
541   dbus_connection_ref (connection);
542   
543   /* kick in the disconnect handler that unrefs the connection */
544   dbus_connection_disconnect (connection);
545
546   bus_test_run_everything (context);
547   
548   _dbus_assert (bus_test_client_listed (connection));
549   
550   /* Run disconnect handler in test.c */
551   if (bus_connection_dispatch_one_message (connection))
552     _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
553   
554   _dbus_assert (!dbus_connection_get_is_connected (connection));
555   dbus_connection_unref (connection);
556   connection = NULL;
557   _dbus_assert (!bus_test_client_listed (connection));
558   
559   csdd.expected_service_name = base_service;
560   csdd.failed = FALSE;
561
562   bus_test_clients_foreach (check_service_deleted_foreach,
563                             &csdd);
564
565   dbus_free (base_service);
566   
567   if (csdd.failed)
568     _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
569   
570   if (!check_no_leftovers (context))
571     _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
572 }
573
574 static void
575 kill_client_connection_unchecked (DBusConnection *connection)
576 {
577   /* This kills the connection without expecting it to affect
578    * the rest of the bus.
579    */  
580   _dbus_verbose ("Unchecked kill of connection %p\n", connection);
581
582   dbus_connection_ref (connection);
583   dbus_connection_disconnect (connection);
584   /* dispatching disconnect handler will unref once */
585   if (bus_connection_dispatch_one_message (connection))
586     _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
587   dbus_connection_unref (connection);
588   _dbus_assert (!bus_test_client_listed (connection));
589 }
590
591 typedef struct
592 {
593   dbus_bool_t failed;
594 } CheckNoMessagesData;
595
596 static dbus_bool_t
597 check_no_messages_foreach (DBusConnection *connection,
598                            void           *data)
599 {
600   CheckNoMessagesData *d = data;
601   DBusMessage *message;
602
603   message = dbus_connection_pop_message (connection);
604   if (message != NULL)
605     {
606       _dbus_warn ("Received message %s on %p, expecting no messages\n",
607                   dbus_message_get_name (message), connection);
608       d->failed = TRUE;
609     }
610
611   if (message)
612     dbus_message_unref (message);
613   return !d->failed;
614 }
615
616 typedef struct
617 {
618   DBusConnection *skip_connection;
619   const char *expected_service_name;
620   dbus_bool_t failed;
621 } CheckServiceCreatedData;
622
623 static dbus_bool_t
624 check_service_created_foreach (DBusConnection *connection,
625                                void           *data)
626 {
627   CheckServiceCreatedData *d = data;
628   DBusMessage *message;
629   DBusError error;
630   char *service_name;
631
632   if (connection == d->skip_connection)
633     return TRUE;
634
635   dbus_error_init (&error);
636   d->failed = TRUE;
637   service_name = NULL;
638   
639   message = dbus_connection_pop_message (connection);
640   if (message == NULL)
641     {
642       _dbus_warn ("Did not receive a message on %p, expecting %s\n",
643                   connection, DBUS_MESSAGE_SERVICE_CREATED);
644       goto out;
645     }
646   else if (!dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
647     {
648       _dbus_warn ("Received message %s on %p, expecting %s\n",
649                   dbus_message_get_name (message),
650                   connection, DBUS_MESSAGE_SERVICE_CREATED);
651       goto out;
652     }
653   else
654     {
655       if (!dbus_message_get_args (message, &error,
656                                   DBUS_TYPE_STRING, &service_name,
657                                   DBUS_TYPE_INVALID))
658         {
659           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
660             {
661               _dbus_verbose ("no memory to get service name arg\n");
662             }
663           else
664             {
665               _dbus_assert (dbus_error_is_set (&error));
666               _dbus_warn ("Did not get the expected single string argument\n");
667               goto out;
668             }
669         }
670       else if (strcmp (service_name, d->expected_service_name) != 0)
671         {
672           _dbus_warn ("expected creation of service %s, got creation of %s\n",
673                       d->expected_service_name,
674                       service_name);
675           goto out;
676         }
677     }
678
679   d->failed = FALSE;
680   
681  out:
682   dbus_free (service_name);
683   dbus_error_free (&error);
684   
685   if (message)
686     dbus_message_unref (message);
687
688   return !d->failed;
689 }
690
691 static dbus_bool_t
692 check_no_leftovers (BusContext *context)
693 {
694   CheckNoMessagesData nmd;
695
696   nmd.failed = FALSE;
697   bus_test_clients_foreach (check_no_messages_foreach,
698                             &nmd);
699   
700   if (nmd.failed)
701     return FALSE;
702   else
703     return TRUE;
704 }
705
706 /* returns TRUE if the correct thing happens,
707  * but the correct thing may include OOM errors.
708  */
709 static dbus_bool_t
710 check_hello_message (BusContext     *context,
711                      DBusConnection *connection)
712 {
713   DBusMessage *message;
714   dbus_int32_t serial;
715   dbus_bool_t retval;
716   DBusError error;
717   char *name;
718   char *acquired;
719   
720   dbus_error_init (&error);
721   name = NULL;
722   acquired = NULL;
723   
724   message = dbus_message_new (DBUS_SERVICE_DBUS,
725                               DBUS_MESSAGE_HELLO);
726
727   if (message == NULL)
728     return TRUE;
729
730   if (!dbus_connection_send (connection, message, &serial))
731     {
732       dbus_message_unref (message);
733       return TRUE;
734     }
735
736   dbus_message_unref (message);
737   message = NULL;
738
739   bus_test_run_everything (context);
740
741   if (!dbus_connection_get_is_connected (connection))
742     {
743       _dbus_verbose ("connection was disconnected\n");
744       return TRUE;
745     }
746   
747   retval = FALSE;
748   
749   message = dbus_connection_pop_message (connection);
750   if (message == NULL)
751     {
752       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
753                   DBUS_MESSAGE_HELLO, serial, connection);
754       goto out;
755     }
756
757   _dbus_verbose ("Received %s on %p\n",
758                  dbus_message_get_name (message), connection);
759
760   if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
761     {
762       _dbus_warn ("Message has wrong sender %s\n",
763                   dbus_message_get_sender (message) ?
764                   dbus_message_get_sender (message) : "(none)");
765       goto out;
766     }
767   
768   if (dbus_message_get_is_error (message))
769     {
770       if (dbus_message_name_is (message,
771                                 DBUS_ERROR_NO_MEMORY))
772         {
773           ; /* good, this is a valid response */
774         }
775       else
776         {
777           _dbus_warn ("Did not expect error %s\n",
778                       dbus_message_get_name (message));
779           goto out;
780         }
781     }
782   else
783     {
784       CheckServiceCreatedData scd;
785       
786       if (dbus_message_name_is (message,
787                                 DBUS_MESSAGE_HELLO))
788         {
789           ; /* good, expected */
790         }
791       else
792         {
793           _dbus_warn ("Did not expect reply %s\n",
794                       dbus_message_get_name (message));
795           goto out;
796         }
797
798     retry_get_hello_name:
799       if (!dbus_message_get_args (message, &error,
800                                   DBUS_TYPE_STRING, &name,
801                                   DBUS_TYPE_INVALID))
802         {
803           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
804             {
805               _dbus_verbose ("no memory to get service name arg from hello\n");
806               dbus_error_free (&error);
807               _dbus_wait_for_memory ();
808               goto retry_get_hello_name;
809             }
810           else
811             {
812               _dbus_assert (dbus_error_is_set (&error));
813               _dbus_warn ("Did not get the expected single string argument to hello\n");
814               goto out;
815             }
816         }
817
818       _dbus_verbose ("Got hello name: %s\n", name);
819
820       while (!dbus_bus_set_base_service (connection, name))
821         _dbus_wait_for_memory ();
822       
823       scd.skip_connection = NULL;
824       scd.failed = FALSE;
825       scd.expected_service_name = name;
826       bus_test_clients_foreach (check_service_created_foreach,
827                                 &scd);
828       
829       if (scd.failed)
830         goto out;
831       
832       /* Client should also have gotten ServiceAcquired */
833       dbus_message_unref (message);
834       message = dbus_connection_pop_message (connection);
835       if (message == NULL)
836         {
837           _dbus_warn ("Expecting %s, got nothing\n",
838                       DBUS_MESSAGE_SERVICE_ACQUIRED);
839           goto out;
840         }
841       
842     retry_get_acquired_name:
843       if (!dbus_message_get_args (message, &error,
844                                   DBUS_TYPE_STRING, &acquired,
845                                   DBUS_TYPE_INVALID))
846         {
847           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
848             {
849               _dbus_verbose ("no memory to get service name arg from acquired\n");
850               dbus_error_free (&error);
851               _dbus_wait_for_memory ();
852               goto retry_get_acquired_name;
853             }
854           else
855             {
856               _dbus_assert (dbus_error_is_set (&error));
857               _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
858               goto out;
859             }
860         }
861
862       _dbus_verbose ("Got acquired name: %s\n", acquired);
863
864       if (strcmp (acquired, name) != 0)
865         {
866           _dbus_warn ("Acquired name is %s but expected %s\n",
867                       acquired, name);
868           goto out;
869         }
870     }
871
872   if (!check_no_leftovers (context))
873     goto out;
874   
875   retval = TRUE;
876   
877  out:
878   dbus_error_free (&error);
879   
880   dbus_free (name);
881   dbus_free (acquired);
882   
883   if (message)
884     dbus_message_unref (message);
885   
886   return retval;
887 }
888
889 /* returns TRUE if the correct thing happens,
890  * but the correct thing may include OOM errors.
891  */
892 static dbus_bool_t
893 check_hello_connection (BusContext *context)
894 {
895   DBusConnection *connection;
896   DBusError error;
897
898   dbus_error_init (&error);
899
900   connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
901   if (connection == NULL)
902     {
903       _DBUS_ASSERT_ERROR_IS_SET (&error);
904       dbus_error_free (&error);
905       return TRUE;
906     }
907
908   if (!bus_setup_debug_client (connection))
909     {
910       dbus_connection_disconnect (connection);
911       dbus_connection_unref (connection);
912       return TRUE;
913     }
914
915   if (!check_hello_message (context, connection))
916     return FALSE;
917
918   if (dbus_bus_get_base_service (connection) == NULL)
919     {
920       /* We didn't successfully register, so we can't
921        * do the usual kill_client_connection() checks
922        */
923       kill_client_connection_unchecked (connection);
924     }
925   else
926     {
927       kill_client_connection (context, connection);
928     }
929
930   return TRUE;
931 }
932
933 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
934
935 /* returns TRUE if the correct thing happens,
936  * but the correct thing may include OOM errors.
937  */
938 static dbus_bool_t
939 check_nonexistent_service_activation (BusContext     *context,
940                                       DBusConnection *connection)
941 {
942   DBusMessage *message;
943   dbus_int32_t serial;
944   dbus_bool_t retval;
945   DBusError error;
946   
947   dbus_error_init (&error);
948   
949   message = dbus_message_new (DBUS_SERVICE_DBUS,
950                               DBUS_MESSAGE_ACTIVATE_SERVICE);
951
952   if (message == NULL)
953     return TRUE;
954
955   if (!dbus_message_append_args (message,
956                                  DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
957                                  DBUS_TYPE_UINT32, 0,
958                                  DBUS_TYPE_INVALID))
959     {
960       dbus_message_unref (message);
961       return TRUE;
962     }
963   
964   if (!dbus_connection_send (connection, message, &serial))
965     {
966       dbus_message_unref (message);
967       return TRUE;
968     }
969
970   dbus_message_unref (message);
971   message = NULL;
972
973   bus_test_run_everything (context);
974
975   if (!dbus_connection_get_is_connected (connection))
976     {
977       _dbus_verbose ("connection was disconnected\n");
978       return TRUE;
979     }
980   
981   retval = FALSE;
982   
983   message = dbus_connection_pop_message (connection);
984   if (message == NULL)
985     {
986       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
987                   DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
988       goto out;
989     }
990
991   _dbus_verbose ("Received %s on %p\n",
992                  dbus_message_get_name (message), connection);
993
994   if (dbus_message_get_is_error (message))
995     {
996       if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
997         {
998           _dbus_warn ("Message has wrong sender %s\n",
999                       dbus_message_get_sender (message) ?
1000                       dbus_message_get_sender (message) : "(none)");
1001           goto out;
1002         }
1003       
1004       if (dbus_message_name_is (message,
1005                                 DBUS_ERROR_NO_MEMORY))
1006         {
1007           ; /* good, this is a valid response */
1008         }
1009       else if (dbus_message_name_is (message,
1010                                      DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1011         {
1012           ; /* good, this is expected also */
1013         }
1014       else
1015         {
1016           _dbus_warn ("Did not expect error %s\n",
1017                       dbus_message_get_name (message));
1018           goto out;
1019         }
1020     }
1021   else
1022     {
1023       _dbus_warn ("Did not expect to successfully activate %s\n",
1024                   NONEXISTENT_SERVICE_NAME);
1025       goto out;
1026     }
1027
1028   retval = TRUE;
1029   
1030  out:
1031   if (message)
1032     dbus_message_unref (message);
1033   
1034   return retval;
1035 }
1036
1037 static dbus_bool_t
1038 check_base_service_activated (BusContext     *context,
1039                               DBusConnection *connection,
1040                               DBusMessage    *initial_message,
1041                               char          **base_service_p)
1042 {
1043   DBusMessage *message;
1044   dbus_bool_t retval;
1045   DBusError error;
1046   char *base_service;
1047   
1048   base_service = NULL;
1049   retval = FALSE;
1050   
1051   dbus_error_init (&error);
1052
1053   message = initial_message;
1054   dbus_message_ref (message);  
1055
1056   if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
1057     {
1058       char *service_name;
1059       CheckServiceCreatedData scd;
1060           
1061       if (!dbus_message_get_args (message, &error,
1062                                   DBUS_TYPE_STRING, &service_name,
1063                                   DBUS_TYPE_INVALID))
1064         {
1065           _dbus_warn ("Message %s doesn't have a service name: %s\n",
1066                       dbus_message_get_name (message),
1067                       error.message);
1068           dbus_error_free (&error);
1069           goto out;
1070         }
1071
1072       if (*service_name != ':')
1073         {
1074           _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1075                       service_name);
1076           goto out;
1077         }
1078               
1079       base_service = service_name;
1080       service_name = NULL;
1081       
1082       scd.skip_connection = connection;
1083       scd.failed = FALSE;
1084       scd.expected_service_name = base_service;
1085       bus_test_clients_foreach (check_service_created_foreach,
1086                                 &scd);
1087       
1088       if (scd.failed)
1089         goto out;
1090     }
1091
1092   retval = TRUE;
1093
1094   if (base_service_p)
1095     {
1096       *base_service_p = base_service;
1097       base_service = NULL;
1098     }
1099   
1100  out:
1101   if (message)
1102     dbus_message_unref (message);
1103
1104   if (base_service)
1105     dbus_free (base_service);
1106   
1107   return retval;
1108 }
1109
1110 static dbus_bool_t
1111 check_service_activated (BusContext     *context,
1112                          DBusConnection *connection,
1113                          const char     *activated_name,
1114                          const char     *base_service_name,
1115                          DBusMessage    *initial_message)
1116 {
1117   DBusMessage *message;
1118   dbus_bool_t retval;
1119   DBusError error;
1120   dbus_uint32_t activation_result;
1121   
1122   retval = FALSE;
1123   
1124   dbus_error_init (&error);
1125
1126   message = initial_message;
1127   dbus_message_ref (message);
1128
1129   if (dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_CREATED))
1130     {
1131       char *service_name;
1132       CheckServiceCreatedData scd;
1133           
1134       if (!dbus_message_get_args (message, &error,
1135                                   DBUS_TYPE_STRING, &service_name,
1136                                   DBUS_TYPE_INVALID))
1137         {
1138           _dbus_warn ("Message %s doesn't have a service name: %s\n",
1139                       dbus_message_get_name (message),
1140                       error.message);
1141           dbus_error_free (&error);
1142           goto out;
1143         }
1144
1145       if (strcmp (service_name, activated_name) != 0)
1146         {
1147           _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1148                       activated_name, service_name);
1149           dbus_free (service_name);
1150           goto out;
1151         }
1152       
1153       scd.skip_connection = connection;
1154       scd.failed = FALSE;
1155       scd.expected_service_name = service_name;
1156       bus_test_clients_foreach (check_service_created_foreach,
1157                                 &scd);
1158           
1159       dbus_free (service_name);
1160
1161       if (scd.failed)
1162         goto out;
1163           
1164       dbus_message_unref (message);
1165       message = dbus_connection_pop_message (connection);
1166       if (message == NULL)
1167         {
1168           _dbus_warn ("Expected a reply to %s, got nothing\n",
1169                       DBUS_MESSAGE_ACTIVATE_SERVICE);
1170           goto out;
1171         }
1172     }
1173       
1174   if (!dbus_message_name_is (message, DBUS_MESSAGE_ACTIVATE_SERVICE))
1175     {
1176       _dbus_warn ("Expected reply to %s, got message %s instead\n",
1177                   DBUS_MESSAGE_ACTIVATE_SERVICE,
1178                   dbus_message_get_name (message));
1179       goto out;
1180     }
1181
1182   activation_result = 0;
1183   if (!dbus_message_get_args (message, &error,
1184                               DBUS_TYPE_UINT32, &activation_result,
1185                               DBUS_TYPE_INVALID))
1186     {
1187       if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1188         {
1189           _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1190                       DBUS_MESSAGE_ACTIVATE_SERVICE, error.message);
1191           dbus_error_free (&error);
1192           goto out;
1193         }
1194
1195       dbus_error_free (&error);
1196     }
1197   else
1198     {
1199       if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1200         ; /* Good */
1201       else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1202         ; /* Good also */
1203       else
1204         {
1205           _dbus_warn ("Activation result was 0x%x, no good.\n",
1206                       activation_result);
1207           goto out;
1208         }
1209     }
1210
1211   dbus_message_unref (message);
1212   message = NULL;
1213       
1214   if (!check_no_leftovers (context))
1215     {
1216       _dbus_warn ("Messages were left over after verifying existent activation results\n");
1217       goto out;
1218     }
1219
1220   retval = TRUE;
1221   
1222  out:
1223   if (message)
1224     dbus_message_unref (message);
1225   
1226   return retval;
1227 }
1228
1229 static dbus_bool_t
1230 check_service_deactivated (BusContext     *context,
1231                            DBusConnection *connection,
1232                            const char     *activated_name,
1233                            const char     *base_service)
1234 {
1235   DBusMessage *message;
1236   dbus_bool_t retval;
1237   DBusError error;
1238   CheckServiceDeletedData csdd;
1239
1240   message = NULL;
1241   retval = FALSE;
1242   
1243   dbus_error_init (&error);
1244
1245   /* Now we are expecting ServiceDeleted messages for the base
1246    * service and the activated_name.  The base service
1247    * notification is required to come last.
1248    */
1249   csdd.expected_service_name = activated_name;
1250   csdd.failed = FALSE;
1251   bus_test_clients_foreach (check_service_deleted_foreach,
1252                             &csdd);      
1253
1254   if (csdd.failed)
1255     goto out;
1256       
1257   csdd.expected_service_name = base_service;
1258   csdd.failed = FALSE;
1259   bus_test_clients_foreach (check_service_deleted_foreach,
1260                             &csdd);
1261
1262   if (csdd.failed)
1263     goto out;
1264       
1265   if (!check_no_leftovers (context))
1266     {
1267       _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1268       goto out;
1269     }
1270
1271   retval = TRUE;
1272   
1273  out:
1274   if (message)
1275     dbus_message_unref (message);
1276   
1277   return retval;
1278 }
1279
1280 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1281
1282 /* returns TRUE if the correct thing happens,
1283  * but the correct thing may include OOM errors.
1284  */
1285 static dbus_bool_t
1286 check_existent_service_activation (BusContext     *context,
1287                                    DBusConnection *connection)
1288 {
1289   DBusMessage *message;
1290   dbus_int32_t serial;
1291   dbus_bool_t retval;
1292   DBusError error;
1293   char *base_service;
1294
1295   base_service = NULL;
1296   
1297   dbus_error_init (&error);
1298   
1299   message = dbus_message_new (DBUS_SERVICE_DBUS,
1300                               DBUS_MESSAGE_ACTIVATE_SERVICE);
1301
1302   if (message == NULL)
1303     return TRUE;
1304
1305   if (!dbus_message_append_args (message,
1306                                  DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1307                                  DBUS_TYPE_UINT32, 0,
1308                                  DBUS_TYPE_INVALID))
1309     {
1310       dbus_message_unref (message);
1311       return TRUE;
1312     }
1313   
1314   if (!dbus_connection_send (connection, message, &serial))
1315     {
1316       dbus_message_unref (message);
1317       return TRUE;
1318     }
1319
1320   dbus_message_unref (message);
1321   message = NULL;
1322
1323   bus_test_run_everything (context);
1324
1325   if (dbus_connection_get_dispatch_status (connection) ==
1326       DBUS_DISPATCH_COMPLETE)
1327     /* now wait for the message bus to hear back from the activated service */
1328     bus_test_run_bus_loop (context, TRUE);
1329   
1330   /* and process everything again */
1331   bus_test_run_everything (context);
1332
1333   if (!dbus_connection_get_is_connected (connection))
1334     {
1335       _dbus_verbose ("connection was disconnected\n");
1336       return TRUE;
1337     }
1338   
1339   retval = FALSE;
1340   
1341   message = dbus_connection_pop_message (connection);
1342   if (message == NULL)
1343     {
1344       _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1345                   DBUS_MESSAGE_ACTIVATE_SERVICE, serial, connection);
1346       goto out;
1347     }
1348
1349   _dbus_verbose ("Received %s on %p\n",
1350                  dbus_message_get_name (message), connection);
1351
1352   if (dbus_message_get_is_error (message))
1353     {
1354       if (!dbus_message_sender_is (message, DBUS_SERVICE_DBUS))
1355         {
1356           _dbus_warn ("Message has wrong sender %s\n",
1357                       dbus_message_get_sender (message) ?
1358                       dbus_message_get_sender (message) : "(none)");
1359           goto out;
1360         }
1361       
1362       if (dbus_message_name_is (message,
1363                                 DBUS_ERROR_NO_MEMORY))
1364         {
1365           ; /* good, this is a valid response */
1366         }
1367       else if (dbus_message_name_is (message,
1368                                      DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1369         {
1370           ; /* good, this is expected also */
1371         }
1372       else if (dbus_message_name_is (message,
1373                                      DBUS_ERROR_SPAWN_CHILD_EXITED))
1374         {
1375           ; /* good, this is expected also (child will exit if for example we don't
1376              * have memory to register it)
1377              */
1378         }
1379       else
1380         {
1381           _dbus_warn ("Did not expect error %s\n",
1382                       dbus_message_get_name (message));
1383           goto out;
1384         }
1385     }
1386   else
1387     {
1388       dbus_bool_t got_service_deleted;
1389       
1390       if (!check_base_service_activated (context, connection,
1391                                          message, &base_service))
1392         goto out;
1393
1394       dbus_message_unref (message);
1395       message = NULL;
1396
1397       /* We may need to block here for the test service to exit or finish up */
1398       if (dbus_connection_get_dispatch_status (connection) ==
1399           DBUS_DISPATCH_COMPLETE)
1400         bus_test_run_bus_loop (context, TRUE);
1401       
1402       message = dbus_connection_borrow_message (connection);
1403       if (message == NULL)
1404         {
1405           _dbus_warn ("Did not receive any messages after base service creation notification\n");
1406           goto out;
1407         }
1408
1409       got_service_deleted = dbus_message_name_is (message, DBUS_MESSAGE_SERVICE_DELETED);
1410
1411       dbus_connection_return_message (connection, message);
1412       message = NULL;
1413       
1414       if (got_service_deleted)
1415         {
1416           /* The service started up and got a base address, but then
1417            * failed to register under EXISTENT_SERVICE_NAME
1418            */
1419           CheckServiceDeletedData csdd;
1420           
1421           csdd.expected_service_name = base_service;
1422           csdd.failed = FALSE;
1423           bus_test_clients_foreach (check_service_deleted_foreach,
1424                                     &csdd);
1425
1426           if (csdd.failed)
1427             goto out;
1428
1429           /* Now we should get an error about the service exiting */
1430           if (dbus_connection_get_dispatch_status (connection) ==
1431               DBUS_DISPATCH_COMPLETE)
1432             /* wait for the message bus to hear back from the activated service exiting */
1433             bus_test_run_bus_loop (context, TRUE);
1434           
1435           /* and process everything again */
1436           bus_test_run_everything (context);
1437           
1438           message = dbus_connection_pop_message (connection);
1439           if (message == NULL)
1440             {
1441               _dbus_warn ("Did not get an error from the service %s exiting\n",
1442                           EXISTENT_SERVICE_NAME);
1443               goto out;
1444             }
1445
1446           if (!dbus_message_get_is_error (message))
1447             {
1448               _dbus_warn ("Expected an error due to service exiting, got %s\n",
1449                           dbus_message_get_name (message));
1450               goto out;
1451             }
1452
1453           if (!dbus_message_name_is (message,
1454                                      DBUS_ERROR_SPAWN_CHILD_EXITED))
1455             {
1456               _dbus_warn ("Expected error %s on service exit, got %s instead\n",
1457                           DBUS_ERROR_SPAWN_CHILD_EXITED,
1458                           dbus_message_get_name (message));
1459               goto out;
1460             }
1461         }
1462       else
1463         {
1464           dbus_bool_t got_error;
1465           
1466           message = dbus_connection_pop_message (connection);
1467           if (message == NULL)
1468             {
1469               _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1470               goto out;
1471             }
1472           
1473           if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1474                                         base_service, message))
1475             goto out;
1476           
1477           dbus_message_unref (message);
1478           message = NULL;
1479
1480
1481           if (!check_no_leftovers (context))
1482             {
1483               _dbus_warn ("Messages were left over after successful activation\n");
1484               goto out;
1485             }
1486           
1487           /* Now kill off the test service by sending it a quit message */
1488           message = dbus_message_new (EXISTENT_SERVICE_NAME,
1489                                       "org.freedesktop.DBus.TestSuiteExit");
1490       
1491           if (message == NULL)
1492             {
1493               dbus_free (base_service);
1494               return TRUE;
1495             }
1496       
1497           if (!dbus_connection_send (connection, message, &serial))
1498             {
1499               dbus_message_unref (message);
1500               dbus_free (base_service);
1501               return TRUE;
1502             }
1503
1504           dbus_message_unref (message);
1505           message = NULL;
1506
1507           /* send message */
1508           bus_test_run_clients_loop (TRUE);
1509
1510           /* read it in and write it out to test service */
1511           bus_test_run_bus_loop (context, FALSE);
1512
1513           /* see if we got an error */
1514           bus_test_run_clients_loop (FALSE);
1515           message = dbus_connection_borrow_message (connection);
1516           got_error = message != NULL && dbus_message_get_is_error (message);
1517           if (message)
1518             dbus_connection_return_message (connection, message);
1519           
1520           if (!got_error)
1521             {
1522               if (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_COMPLETE)
1523                 /* now wait for the message bus to hear back from the activated service exiting */
1524                 bus_test_run_bus_loop (context, TRUE);
1525               
1526               /* and process everything again */
1527               bus_test_run_everything (context);
1528             }
1529
1530           if (got_error)
1531             {
1532               message = dbus_connection_pop_message (connection);
1533               _dbus_assert (message != NULL);
1534
1535               if (!dbus_message_get_is_error (message))
1536                 {
1537                   _dbus_warn ("expecting an error reply to asking test service to exit, got %s\n",
1538                               dbus_message_get_name (message));
1539                   goto out;
1540                 }
1541               else if (!dbus_message_name_is (message, DBUS_ERROR_NO_MEMORY))
1542                 {
1543                   _dbus_warn ("not expecting error %s when asking test service to exit\n",
1544                               dbus_message_get_name (message));
1545                   goto out;
1546                 }
1547             }
1548           else
1549             {
1550               if (!check_service_deactivated (context, connection,
1551                                               EXISTENT_SERVICE_NAME, base_service))
1552                 goto out;
1553             }
1554         }
1555     }
1556
1557   retval = TRUE;
1558   
1559  out:
1560   if (message)
1561     dbus_message_unref (message);
1562
1563   if (base_service)
1564     dbus_free (base_service);
1565   
1566   return retval;
1567 }
1568
1569 typedef struct
1570 {
1571   Check1Func func;
1572   BusContext *context;
1573 } Check1Data;
1574
1575 static dbus_bool_t
1576 check_oom_check1_func (void *data)
1577 {
1578   Check1Data *d = data;
1579
1580   if (! (* d->func) (d->context))
1581     return FALSE;
1582   
1583   if (!check_no_leftovers (d->context))
1584     {
1585       _dbus_warn ("Messages were left over, should be covered by test suite\n");
1586       return FALSE;
1587     }
1588
1589   return TRUE;
1590 }
1591
1592 static void
1593 check1_try_iterations (BusContext *context,
1594                        const char *description,
1595                        Check1Func  func)
1596 {
1597   Check1Data d;
1598
1599   d.func = func;
1600   d.context = context;
1601
1602   if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1603                                 &d))
1604     _dbus_assert_not_reached ("test failed");
1605 }
1606
1607 typedef struct
1608 {
1609   Check2Func func;
1610   BusContext *context;
1611   DBusConnection *connection;
1612 } Check2Data;
1613
1614 static dbus_bool_t
1615 check_oom_check2_func (void *data)
1616 {
1617   Check2Data *d = data;
1618
1619   if (! (* d->func) (d->context, d->connection))
1620     return FALSE;
1621   
1622   if (!check_no_leftovers (d->context))
1623     {
1624       _dbus_warn ("Messages were left over, should be covered by test suite");
1625       return FALSE;
1626     }
1627
1628   return TRUE;
1629 }
1630
1631 static void
1632 check2_try_iterations (BusContext     *context,
1633                        DBusConnection *connection,
1634                        const char     *description,
1635                        Check2Func      func)
1636 {
1637   Check2Data d;
1638
1639   d.func = func;
1640   d.context = context;
1641   d.connection = connection;
1642   
1643   if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1644                                 &d))
1645     _dbus_assert_not_reached ("test failed");
1646 }
1647
1648 dbus_bool_t
1649 bus_dispatch_test (const DBusString *test_data_dir)
1650 {
1651   BusContext *context;
1652   DBusConnection *foo;
1653   DBusConnection *bar;
1654   DBusConnection *baz;
1655   DBusError error;
1656   
1657   context = bus_context_new_test (test_data_dir,
1658                                   "valid-config-files/debug-allow-all.conf");
1659   if (context == NULL)
1660     return FALSE;
1661
1662   dbus_error_init (&error);
1663   
1664   foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1665   if (foo == NULL)
1666     _dbus_assert_not_reached ("could not alloc connection");
1667
1668   if (!bus_setup_debug_client (foo))
1669     _dbus_assert_not_reached ("could not set up connection");
1670
1671   if (!check_hello_message (context, foo))
1672     _dbus_assert_not_reached ("hello message failed");
1673   
1674   bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1675   if (bar == NULL)
1676     _dbus_assert_not_reached ("could not alloc connection");
1677
1678   if (!bus_setup_debug_client (bar))
1679     _dbus_assert_not_reached ("could not set up connection");
1680
1681   if (!check_hello_message (context, bar))
1682     _dbus_assert_not_reached ("hello message failed");
1683   
1684   baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1685   if (baz == NULL)
1686     _dbus_assert_not_reached ("could not alloc connection");
1687
1688   if (!bus_setup_debug_client (baz))
1689     _dbus_assert_not_reached ("could not set up connection");
1690
1691   if (!check_hello_message (context, baz))
1692     _dbus_assert_not_reached ("hello message failed");
1693
1694 #if 0
1695   check2_try_iterations (context, foo, "existent_service_activation",
1696                          check_existent_service_activation);
1697 #endif
1698   
1699   check2_try_iterations (context, foo, "nonexistent_service_activation",
1700                          check_nonexistent_service_activation);
1701
1702   check1_try_iterations (context, "create_and_hello",
1703                          check_hello_connection);
1704   
1705   _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1706
1707   kill_client_connection_unchecked (foo);
1708   kill_client_connection_unchecked (bar);
1709   kill_client_connection_unchecked (baz);
1710
1711   bus_context_unref (context);
1712   
1713   return TRUE;
1714 }
1715 #endif /* DBUS_BUILD_TESTS */