16102aa0685399895f1c3eb8e103e70bfef0f200
[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 dbus_int32_t message_handler_slot = -1;
36
37 typedef struct
38 {
39   BusContext     *context;
40   DBusConnection *sender;
41   DBusMessage    *message;
42   BusTransaction *transaction;
43   DBusError      *error;
44 } SendMessageData;
45
46 static dbus_bool_t
47 send_one_message (DBusConnection *connection, void *data)
48 {
49   SendMessageData *d = data;
50
51   if (!bus_context_check_security_policy (d->context,
52                                           d->sender,
53                                           connection,
54                                           d->message,
55                                           NULL))
56     return TRUE; /* silently don't send it */
57   
58   if (!bus_transaction_send (d->transaction,
59                              connection,
60                              d->message))
61     {
62       BUS_SET_OOM (d->error);
63       return FALSE;
64     }
65
66   return TRUE;
67 }
68
69 dbus_bool_t
70 bus_dispatch_broadcast_message (BusTransaction *transaction,
71                                 DBusConnection *sender,
72                                 DBusMessage    *message,
73                                 DBusError      *error)
74 {
75   DBusError tmp_error;
76   SendMessageData d;
77   BusConnections *connections;
78
79   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
80   
81   _dbus_assert (dbus_message_get_sender (message) != NULL);
82
83   connections = bus_transaction_get_connections (transaction);
84   
85   dbus_error_init (&tmp_error);
86   d.sender = sender;
87   d.context = bus_transaction_get_context (transaction);
88   d.message = message;
89   d.transaction = transaction;
90   d.error = &tmp_error;
91   
92   bus_connections_foreach_active (connections, send_one_message, &d);
93
94   if (dbus_error_is_set (&tmp_error))
95     {
96       dbus_move_error (&tmp_error, error);
97       return FALSE;
98     }
99   else
100     return TRUE;
101 }
102
103 static DBusHandlerResult
104 bus_dispatch (DBusConnection *connection,
105               DBusMessage    *message)
106 {
107   const char *sender, *service_name;
108   DBusError error;
109   BusTransaction *transaction;
110   BusContext *context;
111   DBusHandlerResult result;
112
113   result = DBUS_HANDLER_RESULT_HANDLED;
114   
115   transaction = NULL;
116   dbus_error_init (&error);
117   
118   context = bus_connection_get_context (connection);
119   _dbus_assert (context != NULL);
120   
121   /* If we can't even allocate an OOM error, we just go to sleep
122    * until we can.
123    */
124   while (!bus_connection_preallocate_oom_error (connection))
125     _dbus_wait_for_memory ();
126   
127   /* Ref connection in case we disconnect it at some point in here */
128   dbus_connection_ref (connection);
129   
130   service_name = dbus_message_get_destination (message);
131
132 #ifdef DBUS_ENABLE_VERBOSE_MODE
133   {
134     const char *interface_name, *member_name, *error_name;
135
136     interface_name = dbus_message_get_interface (message);
137     member_name = dbus_message_get_member (message);
138     error_name = dbus_message_get_error_name (message);
139     
140     _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
141                    interface_name ? interface_name : "(no interface)",
142                    member_name ? member_name : "(no member)",
143                    error_name ? error_name : "(no error name)",
144                    service_name ? service_name : "peer");
145   }
146 #endif /* DBUS_ENABLE_VERBOSE_MODE */
147   
148   /* If service_name is NULL, this is a message to the bus daemon, not
149    * intended to actually go "on the bus"; e.g. a peer-to-peer
150    * ping. Handle these immediately, especially disconnection
151    * messages. There are no security policy checks on these.
152    */
153   if (service_name == NULL)
154     {      
155       if (dbus_message_is_signal (message,
156                                   DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
157                                   "Disconnected"))
158         bus_connection_disconnected (connection);
159
160       /* DBusConnection also handles some of these automatically, we leave
161        * it to do so.
162        */
163       result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
164       goto out;
165     }
166
167   _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
168   
169   /* Create our transaction */
170   transaction = bus_transaction_new (context);
171   if (transaction == NULL)
172     {
173       BUS_SET_OOM (&error);
174       goto out;
175     }
176   
177   /* Assign a sender to the message */
178   if (bus_connection_is_active (connection))
179     {
180       sender = bus_connection_get_name (connection);
181       _dbus_assert (sender != NULL);
182
183       if (!dbus_message_set_sender (message, sender))
184         {
185           BUS_SET_OOM (&error);
186           goto out;
187         }
188
189       /* We need to refetch the service name here, because
190        * dbus_message_set_sender can cause the header to be
191        * reallocated, and thus the service_name pointer will become
192        * invalid.
193        */
194       service_name = dbus_message_get_destination (message);
195     }
196
197   if (strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0) /* to bus driver */
198     {
199       if (!bus_context_check_security_policy (context,
200                                               connection, NULL, message, &error))
201         {
202           _dbus_verbose ("Security policy rejected message\n");
203           goto out;
204         }
205
206       _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
207       if (!bus_driver_handle_message (connection, transaction, message, &error))
208         goto out;
209     }
210   else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
211     {
212       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
213       dbus_connection_disconnect (connection);
214     }
215   /* FIXME what if we un-special-case this service and just have a flag
216    * on services that all service owners will get messages to it, not just
217    * the primary owner.
218    */
219   else if (strcmp (service_name, DBUS_SERVICE_ORG_FREEDESKTOP_BROADCAST) == 0) /* spam! */
220     {
221       if (!bus_dispatch_broadcast_message (transaction, connection, message, &error))
222         goto out;
223     }
224   else  /* route to named service */
225     {
226       DBusString service_string;
227       BusService *service;
228       BusRegistry *registry;
229
230       registry = bus_connection_get_registry (connection);
231       
232       _dbus_string_init_const (&service_string, service_name);
233       service = bus_registry_lookup (registry, &service_string);
234
235       if (service == NULL)
236         {
237           dbus_set_error (&error,
238                           DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
239                           "Service \"%s\" does not exist",
240                           service_name);
241           goto out;
242         }
243       else
244         {
245           DBusConnection *recipient;
246           
247           recipient = bus_service_get_primary_owner (service);
248           _dbus_assert (recipient != NULL);
249           
250           if (!bus_context_check_security_policy (context,
251                                                   connection, recipient, message, &error))
252             goto out;
253           
254           /* Dispatch the message */
255           if (!bus_transaction_send (transaction, recipient, message))
256             {
257               BUS_SET_OOM (&error);
258               goto out;
259             }
260         }
261     }
262   
263  out:
264   if (dbus_error_is_set (&error))
265     {
266       if (!dbus_connection_get_is_connected (connection))
267         {
268           /* If we disconnected it, we won't bother to send it any error
269            * messages.
270            */
271           _dbus_verbose ("Not sending error to connection we disconnected\n");
272         }
273       else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
274         {
275           bus_connection_send_oom_error (connection, message);
276
277           /* cancel transaction due to OOM */
278           if (transaction != NULL)
279             {
280               bus_transaction_cancel_and_free (transaction);
281               transaction = NULL;
282             }
283         }
284       else
285         {
286           /* Try to send the real error, if no mem to do that, send
287            * the OOM error
288            */
289           _dbus_assert (transaction != NULL);
290           
291           if (!bus_transaction_send_error_reply (transaction, connection,
292                                                  &error, message))
293             {
294               bus_connection_send_oom_error (connection, message);
295               
296               /* cancel transaction due to OOM */
297               if (transaction != NULL)
298                 {
299                   bus_transaction_cancel_and_free (transaction);
300                   transaction = NULL;
301                 }
302             }
303         }
304       
305       dbus_error_free (&error);
306     }
307
308   if (transaction != NULL)
309     {
310       bus_transaction_execute_and_free (transaction);
311     }
312
313   dbus_connection_unref (connection);
314
315   return result;
316 }
317
318 static DBusHandlerResult
319 bus_dispatch_message_handler (DBusMessageHandler *handler,
320                               DBusConnection     *connection,
321                               DBusMessage        *message,
322                               void               *user_data)
323 {
324   return bus_dispatch (connection, message);
325 }
326
327 static void
328 free_message_handler (void *data)
329 {
330   DBusMessageHandler *handler = data;
331   
332   _dbus_assert (message_handler_slot >= 0);
333   
334   dbus_message_handler_unref (handler);
335   dbus_connection_free_data_slot (&message_handler_slot);
336 }
337
338 dbus_bool_t
339 bus_dispatch_add_connection (DBusConnection *connection)
340 {
341   DBusMessageHandler *handler;
342
343   if (!dbus_connection_allocate_data_slot (&message_handler_slot))
344     return FALSE;
345   
346   handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);  
347   if (handler == NULL)
348     {
349       dbus_connection_free_data_slot (&message_handler_slot);
350       return FALSE;
351     }    
352   
353   if (!dbus_connection_add_filter (connection, handler))
354     {
355       dbus_message_handler_unref (handler);
356       dbus_connection_free_data_slot (&message_handler_slot);
357       
358       return FALSE;
359     }
360
361   _dbus_assert (message_handler_slot >= 0);
362   
363   if (!dbus_connection_set_data (connection,
364                                  message_handler_slot,
365                                  handler,
366                                  free_message_handler))
367     {
368       dbus_message_handler_unref (handler);
369       dbus_connection_free_data_slot (&message_handler_slot);
370
371       return FALSE;
372     }
373
374   return TRUE;
375 }
376
377 void
378 bus_dispatch_remove_connection (DBusConnection *connection)
379 {
380   /* Here we tell the bus driver that we want to get off. */
381   bus_driver_remove_connection (connection);
382
383   dbus_connection_set_data (connection,
384                             message_handler_slot,
385                             NULL, NULL);
386 }
387
388 #ifdef DBUS_BUILD_TESTS
389
390 typedef dbus_bool_t (* Check1Func) (BusContext     *context);
391 typedef dbus_bool_t (* Check2Func) (BusContext     *context,
392                                     DBusConnection *connection);
393
394 static dbus_bool_t check_no_leftovers (BusContext *context);
395
396 static void
397 block_connection_until_message_from_bus (BusContext     *context,
398                                          DBusConnection *connection)
399 {
400   while (dbus_connection_get_dispatch_status (connection) ==
401          DBUS_DISPATCH_COMPLETE &&
402          dbus_connection_get_is_connected (connection))
403     {
404       bus_test_run_bus_loop (context, TRUE);
405       bus_test_run_clients_loop (FALSE);
406     }
407 }
408
409 /* compensate for fact that pop_message() can return #NULL due to OOM */
410 static DBusMessage*
411 pop_message_waiting_for_memory (DBusConnection *connection)
412 {
413   while (dbus_connection_get_dispatch_status (connection) ==
414          DBUS_DISPATCH_NEED_MEMORY)
415     _dbus_wait_for_memory ();
416
417   return dbus_connection_pop_message (connection);
418 }
419
420 static void
421 warn_unexpected_real (DBusConnection *connection,
422                       DBusMessage    *message,
423                       const char     *expected,
424                       const char     *function,
425                       int             line)
426 {
427   _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
428               function, line,
429               dbus_message_get_interface (message) ?
430               dbus_message_get_interface (message) : "(unset)",
431               dbus_message_get_member (message) ?
432               dbus_message_get_member (message) : "(unset)",
433               dbus_message_get_error_name (message) ?
434               dbus_message_get_error_name (message) : "(unset)",
435               connection,
436               expected);
437 }
438
439 #define warn_unexpected(connection, message, expected) \
440   warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
441
442 static void
443 verbose_message_received (DBusConnection *connection,
444                           DBusMessage    *message)
445 {
446   _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
447                  dbus_message_get_interface (message) ?
448                  dbus_message_get_interface (message) : "(unset)",
449                  dbus_message_get_member (message) ?
450                  dbus_message_get_member (message) : "(unset)",
451                  dbus_message_get_error_name (message) ?
452                  dbus_message_get_error_name (message) : "(unset)",
453                  connection);
454 }
455
456 typedef struct
457 {
458   const char *expected_service_name;
459   dbus_bool_t failed;
460 } CheckServiceDeletedData;
461
462 static dbus_bool_t
463 check_service_deleted_foreach (DBusConnection *connection,
464                                void           *data)
465 {
466   CheckServiceDeletedData *d = data;
467   DBusMessage *message;
468   DBusError error;
469   char *service_name;
470
471   dbus_error_init (&error);
472   d->failed = TRUE;
473   service_name = NULL;
474   
475   message = pop_message_waiting_for_memory (connection);
476   if (message == NULL)
477     {
478       _dbus_warn ("Did not receive a message on %p, expecting %s\n",
479                   connection, "ServiceDeleted");
480       goto out;
481     }
482   else if (!dbus_message_is_signal (message,
483                                     DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
484                                     "ServiceDeleted"))
485     {
486       warn_unexpected (connection, message, "ServiceDeleted");
487
488       goto out;
489     }
490   else
491     {
492       if (!dbus_message_get_args (message, &error,
493                                   DBUS_TYPE_STRING, &service_name,
494                                   DBUS_TYPE_INVALID))
495         {
496           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
497             {
498               _dbus_verbose ("no memory to get service name arg\n");
499             }
500           else
501             {
502               _dbus_assert (dbus_error_is_set (&error));
503               _dbus_warn ("Did not get the expected single string argument\n");
504               goto out;
505             }
506         }
507       else if (strcmp (service_name, d->expected_service_name) != 0)
508         {
509           _dbus_warn ("expected deletion of service %s, got deletion of %s\n",
510                       d->expected_service_name,
511                       service_name);
512           goto out;
513         }
514     }
515
516   d->failed = FALSE;
517   
518  out:
519   dbus_free (service_name);
520   dbus_error_free (&error);
521   
522   if (message)
523     dbus_message_unref (message);
524
525   return !d->failed;
526 }
527
528 static void
529 kill_client_connection (BusContext     *context,
530                         DBusConnection *connection)
531 {
532   char *base_service;
533   const char *s;
534   CheckServiceDeletedData csdd;
535
536   _dbus_verbose ("killing connection %p\n", connection);
537   
538   s = dbus_bus_get_base_service (connection);
539   _dbus_assert (s != NULL);
540
541   while ((base_service = _dbus_strdup (s)) == NULL)
542     _dbus_wait_for_memory ();
543
544   dbus_connection_ref (connection);
545   
546   /* kick in the disconnect handler that unrefs the connection */
547   dbus_connection_disconnect (connection);
548
549   bus_test_run_everything (context);
550   
551   _dbus_assert (bus_test_client_listed (connection));
552   
553   /* Run disconnect handler in test.c */
554   if (bus_connection_dispatch_one_message (connection))
555     _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
556   
557   _dbus_assert (!dbus_connection_get_is_connected (connection));
558   dbus_connection_unref (connection);
559   connection = NULL;
560   _dbus_assert (!bus_test_client_listed (connection));
561   
562   csdd.expected_service_name = base_service;
563   csdd.failed = FALSE;
564
565   bus_test_clients_foreach (check_service_deleted_foreach,
566                             &csdd);
567
568   dbus_free (base_service);
569   
570   if (csdd.failed)
571     _dbus_assert_not_reached ("didn't get the expected ServiceDeleted messages");
572   
573   if (!check_no_leftovers (context))
574     _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
575 }
576
577 static void
578 kill_client_connection_unchecked (DBusConnection *connection)
579 {
580   /* This kills the connection without expecting it to affect
581    * the rest of the bus.
582    */  
583   _dbus_verbose ("Unchecked kill of connection %p\n", connection);
584
585   dbus_connection_ref (connection);
586   dbus_connection_disconnect (connection);
587   /* dispatching disconnect handler will unref once */
588   if (bus_connection_dispatch_one_message (connection))
589     _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
590
591   _dbus_assert (!bus_test_client_listed (connection));
592   dbus_connection_unref (connection);
593 }
594
595 typedef struct
596 {
597   dbus_bool_t failed;
598 } CheckNoMessagesData;
599
600 static dbus_bool_t
601 check_no_messages_foreach (DBusConnection *connection,
602                            void           *data)
603 {
604   CheckNoMessagesData *d = data;
605   DBusMessage *message;
606
607   message = pop_message_waiting_for_memory (connection);
608   if (message != NULL)
609     {
610       warn_unexpected (connection, message, "no messages");
611
612       d->failed = TRUE;
613     }
614
615   if (message)
616     dbus_message_unref (message);
617   return !d->failed;
618 }
619
620 typedef struct
621 {
622   DBusConnection *skip_connection;
623   const char *expected_service_name;
624   dbus_bool_t failed;
625 } CheckServiceCreatedData;
626
627 static dbus_bool_t
628 check_service_created_foreach (DBusConnection *connection,
629                                void           *data)
630 {
631   CheckServiceCreatedData *d = data;
632   DBusMessage *message;
633   DBusError error;
634   char *service_name;
635
636   if (connection == d->skip_connection)
637     return TRUE;
638
639   dbus_error_init (&error);
640   d->failed = TRUE;
641   service_name = NULL;
642   
643   message = pop_message_waiting_for_memory (connection);
644   if (message == NULL)
645     {
646       _dbus_warn ("Did not receive a message on %p, expecting %s\n",
647                   connection, "ServiceCreated");
648       goto out;
649     }
650   else if (!dbus_message_is_signal (message,
651                                     DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
652                                     "ServiceCreated"))
653     {
654       warn_unexpected (connection, message, "ServiceCreated");
655       goto out;
656     }
657   else
658     {
659       if (!dbus_message_get_args (message, &error,
660                                   DBUS_TYPE_STRING, &service_name,
661                                   DBUS_TYPE_INVALID))
662         {
663           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
664             {
665               _dbus_verbose ("no memory to get service name arg\n");
666             }
667           else
668             {
669               _dbus_assert (dbus_error_is_set (&error));
670               _dbus_warn ("Did not get the expected single string argument\n");
671               goto out;
672             }
673         }
674       else if (strcmp (service_name, d->expected_service_name) != 0)
675         {
676           _dbus_warn ("expected creation of service %s, got creation of %s\n",
677                       d->expected_service_name,
678                       service_name);
679           goto out;
680         }
681     }
682
683   d->failed = FALSE;
684   
685  out:
686   dbus_free (service_name);
687   dbus_error_free (&error);
688   
689   if (message)
690     dbus_message_unref (message);
691
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_int32_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   message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
731                                           "Hello",
732                                           DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
733
734   if (message == NULL)
735     return TRUE;
736
737   if (!dbus_connection_send (connection, message, &serial))
738     {
739       dbus_message_unref (message);
740       return TRUE;
741     }
742
743   dbus_message_unref (message);
744   message = NULL;
745
746   /* send our message */
747   bus_test_run_clients_loop (TRUE);
748
749   dbus_connection_ref (connection); /* because we may get disconnected */
750   block_connection_until_message_from_bus (context, connection);
751
752   if (!dbus_connection_get_is_connected (connection))
753     {
754       _dbus_verbose ("connection was disconnected\n");
755       
756       dbus_connection_unref (connection);
757       
758       return TRUE;
759     }
760
761   dbus_connection_unref (connection);
762   
763   message = pop_message_waiting_for_memory (connection);
764   if (message == NULL)
765     {
766       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
767                   "Hello", serial, connection);
768       goto out;
769     }
770
771   verbose_message_received (connection, message);
772
773   if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
774     {
775       _dbus_warn ("Message has wrong sender %s\n",
776                   dbus_message_get_sender (message) ?
777                   dbus_message_get_sender (message) : "(none)");
778       goto out;
779     }
780   
781   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
782     {
783       if (dbus_message_is_error (message,
784                                  DBUS_ERROR_NO_MEMORY))
785         {
786           ; /* good, this is a valid response */
787         }
788       else
789         {
790           warn_unexpected (connection, message, "not this error");
791
792           goto out;
793         }
794     }
795   else
796     {
797       CheckServiceCreatedData scd;
798       
799       if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
800         {
801           ; /* good, expected */
802         }
803       else
804         {
805           warn_unexpected (connection, message, "method return for Hello");
806
807           goto out;
808         }
809
810     retry_get_hello_name:
811       if (!dbus_message_get_args (message, &error,
812                                   DBUS_TYPE_STRING, &name,
813                                   DBUS_TYPE_INVALID))
814         {
815           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
816             {
817               _dbus_verbose ("no memory to get service name arg from hello\n");
818               dbus_error_free (&error);
819               _dbus_wait_for_memory ();
820               goto retry_get_hello_name;
821             }
822           else
823             {
824               _dbus_assert (dbus_error_is_set (&error));
825               _dbus_warn ("Did not get the expected single string argument to hello\n");
826               goto out;
827             }
828         }
829
830       _dbus_verbose ("Got hello name: %s\n", name);
831
832       while (!dbus_bus_set_base_service (connection, name))
833         _dbus_wait_for_memory ();
834       
835       scd.skip_connection = NULL;
836       scd.failed = FALSE;
837       scd.expected_service_name = name;
838       bus_test_clients_foreach (check_service_created_foreach,
839                                 &scd);
840       
841       if (scd.failed)
842         goto out;
843       
844       /* Client should also have gotten ServiceAcquired */
845       dbus_message_unref (message);
846       message = pop_message_waiting_for_memory (connection);
847       if (message == NULL)
848         {
849           _dbus_warn ("Expecting %s, got nothing\n",
850                       "ServiceAcquired");
851           goto out;
852         }
853       
854     retry_get_acquired_name:
855       if (!dbus_message_get_args (message, &error,
856                                   DBUS_TYPE_STRING, &acquired,
857                                   DBUS_TYPE_INVALID))
858         {
859           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
860             {
861               _dbus_verbose ("no memory to get service name arg from acquired\n");
862               dbus_error_free (&error);
863               _dbus_wait_for_memory ();
864               goto retry_get_acquired_name;
865             }
866           else
867             {
868               _dbus_assert (dbus_error_is_set (&error));
869               _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
870               goto out;
871             }
872         }
873
874       _dbus_verbose ("Got acquired name: %s\n", acquired);
875
876       if (strcmp (acquired, name) != 0)
877         {
878           _dbus_warn ("Acquired name is %s but expected %s\n",
879                       acquired, name);
880           goto out;
881         }
882     }
883
884   if (!check_no_leftovers (context))
885     goto out;
886   
887   retval = TRUE;
888   
889  out:
890   dbus_error_free (&error);
891   
892   dbus_free (name);
893   dbus_free (acquired);
894   
895   if (message)
896     dbus_message_unref (message);
897   
898   return retval;
899 }
900
901 /* returns TRUE if the correct thing happens,
902  * but the correct thing may include OOM errors.
903  */
904 static dbus_bool_t
905 check_hello_connection (BusContext *context)
906 {
907   DBusConnection *connection;
908   DBusError error;
909
910   dbus_error_init (&error);
911
912   connection = dbus_connection_open ("debug-pipe:name=test-server", &error);
913   if (connection == NULL)
914     {
915       _DBUS_ASSERT_ERROR_IS_SET (&error);
916       dbus_error_free (&error);
917       return TRUE;
918     }
919
920   if (!bus_setup_debug_client (connection))
921     {
922       dbus_connection_disconnect (connection);
923       dbus_connection_unref (connection);
924       return TRUE;
925     }
926
927   if (!check_hello_message (context, connection))
928     return FALSE;
929
930   if (dbus_bus_get_base_service (connection) == NULL)
931     {
932       /* We didn't successfully register, so we can't
933        * do the usual kill_client_connection() checks
934        */
935       kill_client_connection_unchecked (connection);
936     }
937   else
938     {
939       kill_client_connection (context, connection);
940     }
941
942   return TRUE;
943 }
944
945 #define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
946
947 /* returns TRUE if the correct thing happens,
948  * but the correct thing may include OOM errors.
949  */
950 static dbus_bool_t
951 check_nonexistent_service_activation (BusContext     *context,
952                                       DBusConnection *connection)
953 {
954   DBusMessage *message;
955   dbus_int32_t serial;
956   dbus_bool_t retval;
957   DBusError error;
958   
959   dbus_error_init (&error);
960   
961   message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
962                                           "ActivateService",
963                                           DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
964
965   if (message == NULL)
966     return TRUE;
967
968   if (!dbus_message_append_args (message,
969                                  DBUS_TYPE_STRING, NONEXISTENT_SERVICE_NAME,
970                                  DBUS_TYPE_UINT32, 0,
971                                  DBUS_TYPE_INVALID))
972     {
973       dbus_message_unref (message);
974       return TRUE;
975     }
976   
977   if (!dbus_connection_send (connection, message, &serial))
978     {
979       dbus_message_unref (message);
980       return TRUE;
981     }
982
983   dbus_message_unref (message);
984   message = NULL;
985
986   bus_test_run_everything (context);
987   block_connection_until_message_from_bus (context, connection);
988   bus_test_run_everything (context);
989
990   if (!dbus_connection_get_is_connected (connection))
991     {
992       _dbus_verbose ("connection was disconnected\n");
993       return TRUE;
994     }
995   
996   retval = FALSE;
997   
998   message = pop_message_waiting_for_memory (connection);
999   if (message == NULL)
1000     {
1001       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1002                   "ActivateService", serial, connection);
1003       goto out;
1004     }
1005
1006   verbose_message_received (connection, message);
1007
1008   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1009     {
1010       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1011         {
1012           _dbus_warn ("Message has wrong sender %s\n",
1013                       dbus_message_get_sender (message) ?
1014                       dbus_message_get_sender (message) : "(none)");
1015           goto out;
1016         }
1017       
1018       if (dbus_message_is_error (message,
1019                                  DBUS_ERROR_NO_MEMORY))
1020         {
1021           ; /* good, this is a valid response */
1022         }
1023       else if (dbus_message_is_error (message,
1024                                       DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND))
1025         {
1026           ; /* good, this is expected also */
1027         }
1028       else
1029         {
1030           warn_unexpected (connection, message, "not this error");
1031           goto out;
1032         }
1033     }
1034   else
1035     {
1036       _dbus_warn ("Did not expect to successfully activate %s\n",
1037                   NONEXISTENT_SERVICE_NAME);
1038       goto out;
1039     }
1040
1041   retval = TRUE;
1042   
1043  out:
1044   if (message)
1045     dbus_message_unref (message);
1046   
1047   return retval;
1048 }
1049
1050 static dbus_bool_t
1051 check_base_service_activated (BusContext     *context,
1052                               DBusConnection *connection,
1053                               DBusMessage    *initial_message,
1054                               char          **base_service_p)
1055 {
1056   DBusMessage *message;
1057   dbus_bool_t retval;
1058   DBusError error;
1059   char *base_service;
1060   
1061   base_service = NULL;
1062   retval = FALSE;
1063   
1064   dbus_error_init (&error);
1065
1066   message = initial_message;
1067   dbus_message_ref (message);  
1068
1069   if (dbus_message_is_signal (message,
1070                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1071                               "ServiceCreated"))
1072     {
1073       char *service_name;
1074       CheckServiceCreatedData scd;
1075
1076     reget_service_name_arg:
1077       if (!dbus_message_get_args (message, &error,
1078                                   DBUS_TYPE_STRING, &service_name,
1079                                   DBUS_TYPE_INVALID))
1080         {
1081           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1082             {
1083               dbus_error_free (&error);
1084               _dbus_wait_for_memory ();
1085               goto reget_service_name_arg;
1086             }
1087           else
1088             {
1089               _dbus_warn ("Message %s doesn't have a service name: %s\n",
1090                           "ServiceCreated",
1091                           error.message);
1092               dbus_error_free (&error);
1093               goto out;
1094             }
1095         }
1096
1097       if (*service_name != ':')
1098         {
1099           _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
1100                       service_name);
1101           goto out;
1102         }
1103               
1104       base_service = service_name;
1105       service_name = NULL;
1106       
1107       scd.skip_connection = connection;
1108       scd.failed = FALSE;
1109       scd.expected_service_name = base_service;
1110       bus_test_clients_foreach (check_service_created_foreach,
1111                                 &scd);
1112       
1113       if (scd.failed)
1114         goto out;
1115     }
1116   else
1117     {
1118       warn_unexpected (connection, message, "ServiceCreated for base service");
1119
1120       goto out;
1121     }
1122
1123   retval = TRUE;
1124
1125   if (base_service_p)
1126     {
1127       *base_service_p = base_service;
1128       base_service = NULL;
1129     }
1130   
1131  out:
1132   if (message)
1133     dbus_message_unref (message);
1134
1135   if (base_service)
1136     dbus_free (base_service);
1137   
1138   return retval;
1139 }
1140
1141 static dbus_bool_t
1142 check_service_activated (BusContext     *context,
1143                          DBusConnection *connection,
1144                          const char     *activated_name,
1145                          const char     *base_service_name,
1146                          DBusMessage    *initial_message)
1147 {
1148   DBusMessage *message;
1149   dbus_bool_t retval;
1150   DBusError error;
1151   dbus_uint32_t activation_result;
1152   
1153   retval = FALSE;
1154   
1155   dbus_error_init (&error);
1156
1157   message = initial_message;
1158   dbus_message_ref (message);
1159
1160   if (dbus_message_is_signal (message,
1161                               DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1162                               "ServiceCreated"))
1163     {
1164       char *service_name;
1165       CheckServiceCreatedData scd;
1166
1167     reget_service_name_arg:
1168       if (!dbus_message_get_args (message, &error,
1169                                   DBUS_TYPE_STRING, &service_name,
1170                                   DBUS_TYPE_INVALID))
1171         {
1172           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1173             {
1174               dbus_error_free (&error);
1175               _dbus_wait_for_memory ();
1176               goto reget_service_name_arg;
1177             }
1178           else
1179             {
1180               _dbus_warn ("Message %s doesn't have a service name: %s\n",
1181                           "ServiceCreated",
1182                           error.message);
1183               dbus_error_free (&error);
1184               goto out;
1185             }
1186         }
1187
1188       if (strcmp (service_name, activated_name) != 0)
1189         {
1190           _dbus_warn ("Expected to see service %s created, saw %s instead\n",
1191                       activated_name, service_name);
1192           dbus_free (service_name);
1193           goto out;
1194         }
1195       
1196       scd.skip_connection = connection;
1197       scd.failed = FALSE;
1198       scd.expected_service_name = service_name;
1199       bus_test_clients_foreach (check_service_created_foreach,
1200                                 &scd);
1201           
1202       dbus_free (service_name);
1203
1204       if (scd.failed)
1205         goto out;
1206           
1207       dbus_message_unref (message);
1208       message = pop_message_waiting_for_memory (connection);
1209       if (message == NULL)
1210         {
1211           _dbus_warn ("Expected a reply to %s, got nothing\n",
1212                       "ActivateService");
1213           goto out;
1214         }
1215     }
1216   else
1217     {
1218       warn_unexpected (connection, message, "ServiceCreated for the activated name");
1219       
1220       goto out;
1221     }
1222   
1223   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
1224     {
1225       warn_unexpected (connection, message, "reply to ActivateService");
1226
1227       goto out;
1228     }
1229
1230   activation_result = 0;
1231   if (!dbus_message_get_args (message, &error,
1232                               DBUS_TYPE_UINT32, &activation_result,
1233                               DBUS_TYPE_INVALID))
1234     {
1235       if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1236         {
1237           _dbus_warn ("Did not have activation result first argument to %s: %s\n",
1238                       "ActivateService", error.message);
1239           dbus_error_free (&error);
1240           goto out;
1241         }
1242
1243       dbus_error_free (&error);
1244     }
1245   else
1246     {
1247       if (activation_result == DBUS_ACTIVATION_REPLY_ACTIVATED)
1248         ; /* Good */
1249       else if (activation_result == DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE)
1250         ; /* Good also */
1251       else
1252         {
1253           _dbus_warn ("Activation result was 0x%x, no good.\n",
1254                       activation_result);
1255           goto out;
1256         }
1257     }
1258
1259   dbus_message_unref (message);
1260   message = NULL;
1261       
1262   if (!check_no_leftovers (context))
1263     {
1264       _dbus_warn ("Messages were left over after verifying existent activation results\n");
1265       goto out;
1266     }
1267
1268   retval = TRUE;
1269   
1270  out:
1271   if (message)
1272     dbus_message_unref (message);
1273   
1274   return retval;
1275 }
1276
1277 static dbus_bool_t
1278 check_service_deactivated (BusContext     *context,
1279                            DBusConnection *connection,
1280                            const char     *activated_name,
1281                            const char     *base_service)
1282 {
1283   DBusMessage *message;
1284   dbus_bool_t retval;
1285   DBusError error;
1286   CheckServiceDeletedData csdd;
1287
1288   message = NULL;
1289   retval = FALSE;
1290   
1291   dbus_error_init (&error);
1292
1293   /* Now we are expecting ServiceDeleted messages for the base
1294    * service and the activated_name.  The base service
1295    * notification is required to come last.
1296    */
1297   csdd.expected_service_name = activated_name;
1298   csdd.failed = FALSE;
1299   bus_test_clients_foreach (check_service_deleted_foreach,
1300                             &csdd);      
1301
1302   if (csdd.failed)
1303     goto out;
1304       
1305   csdd.expected_service_name = base_service;
1306   csdd.failed = FALSE;
1307   bus_test_clients_foreach (check_service_deleted_foreach,
1308                             &csdd);
1309
1310   if (csdd.failed)
1311     goto out;
1312       
1313   if (!check_no_leftovers (context))
1314     {
1315       _dbus_warn ("Messages were left over after verifying results of service exiting\n");
1316       goto out;
1317     }
1318
1319   retval = TRUE;
1320   
1321  out:
1322   if (message)
1323     dbus_message_unref (message);
1324   
1325   return retval;
1326 }
1327
1328 static dbus_bool_t
1329 check_send_exit_to_service (BusContext     *context,
1330                             DBusConnection *connection,
1331                             const char     *service_name,
1332                             const char     *base_service)
1333 {
1334   dbus_bool_t got_error;
1335   DBusMessage *message;
1336   dbus_int32_t serial;
1337   dbus_bool_t retval;
1338   
1339   _dbus_verbose ("Sending exit message to the test service\n");
1340
1341   retval = FALSE;
1342   
1343   /* Kill off the test service by sending it a quit message */
1344   message = dbus_message_new_method_call ("org.freedesktop.TestSuite",
1345                                           "Exit",
1346                                           service_name);
1347       
1348   if (message == NULL)
1349     {
1350       /* Do this again; we still need the service to exit... */
1351       if (!check_send_exit_to_service (context, connection,
1352                                        service_name, base_service))
1353         goto out;
1354       
1355       return TRUE;
1356     }
1357       
1358   if (!dbus_connection_send (connection, message, &serial))
1359     {
1360       dbus_message_unref (message);
1361
1362       /* Do this again; we still need the service to exit... */
1363       if (!check_send_exit_to_service (context, connection,
1364                                        service_name, base_service))
1365         goto out;
1366       
1367       return TRUE;
1368     }
1369
1370   dbus_message_unref (message);
1371   message = NULL;
1372
1373   /* send message */
1374   bus_test_run_clients_loop (TRUE);
1375
1376   /* read it in and write it out to test service */
1377   bus_test_run_bus_loop (context, FALSE);
1378
1379   /* see if we got an error during message bus dispatching */
1380   bus_test_run_clients_loop (FALSE);
1381   message = dbus_connection_borrow_message (connection);
1382   got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1383   if (message)
1384     {
1385       dbus_connection_return_message (connection, message);
1386       message = NULL;
1387     }
1388           
1389   if (!got_error)
1390     {
1391       /* If no error, wait for the test service to exit */
1392       block_connection_until_message_from_bus (context, connection);
1393               
1394       bus_test_run_everything (context);
1395     }
1396
1397   if (got_error)
1398     {
1399       message = pop_message_waiting_for_memory (connection);
1400       _dbus_assert (message != NULL);
1401
1402       if (!dbus_message_is_error (message,
1403                                   DBUS_ERROR_NO_MEMORY))
1404         {
1405           warn_unexpected (connection, message,
1406                            "a no memory error from asking test service to exit");
1407           goto out;
1408         }
1409
1410       _dbus_verbose ("Got error %s when asking test service to exit\n",
1411                      dbus_message_get_error_name (message));
1412
1413       /* Do this again; we still need the service to exit... */
1414       if (!check_send_exit_to_service (context, connection,
1415                                        service_name, base_service))
1416         goto out;
1417     }
1418   else
1419     {
1420       if (!check_service_deactivated (context, connection,
1421                                       service_name, base_service))
1422         goto out;
1423     }
1424
1425   retval = TRUE;
1426   
1427  out:
1428   if (message)
1429     dbus_message_unref (message);
1430   
1431   return retval;
1432 }
1433
1434 static dbus_bool_t
1435 check_got_error (BusContext     *context,
1436                  DBusConnection *connection,
1437                  const char     *first_error_name,
1438                  ...)
1439 {
1440   DBusMessage *message;
1441   dbus_bool_t retval;
1442   va_list ap;
1443   dbus_bool_t error_found;
1444   const char *error_name;
1445   
1446   retval = FALSE;
1447   
1448   message = pop_message_waiting_for_memory (connection);
1449   if (message == NULL)
1450     {
1451       _dbus_warn ("Did not get an expected error\n");
1452       goto out;
1453     }
1454
1455   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
1456     {
1457       warn_unexpected (connection, message, "an error");
1458
1459       goto out;
1460     }
1461
1462   error_found = FALSE;
1463
1464   va_start (ap, first_error_name);
1465   error_name = first_error_name;
1466   while (error_name != NULL)
1467     {
1468       if (dbus_message_is_error (message, error_name))
1469         {
1470           error_found = TRUE;
1471           break;
1472         }
1473       error_name = va_arg (ap, char*);
1474     }
1475   va_end (ap);
1476
1477   if (!error_found)
1478     {
1479       _dbus_warn ("Expected error %s or other, got %s instead\n",
1480                   first_error_name,
1481                   dbus_message_get_error_name (message));
1482       goto out;
1483     }
1484
1485   retval = TRUE;
1486   
1487  out:
1488   if (message)
1489     dbus_message_unref (message);
1490   
1491   return retval;
1492 }
1493           
1494 #define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
1495
1496 /* returns TRUE if the correct thing happens,
1497  * but the correct thing may include OOM errors.
1498  */
1499 static dbus_bool_t
1500 check_existent_service_activation (BusContext     *context,
1501                                    DBusConnection *connection)
1502 {
1503   DBusMessage *message;
1504   dbus_int32_t serial;
1505   dbus_bool_t retval;
1506   DBusError error;
1507   char *base_service;
1508
1509   base_service = NULL;
1510   
1511   dbus_error_init (&error);
1512   
1513   message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1514                                           "ActivateService",
1515                                           DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1516
1517   if (message == NULL)
1518     return TRUE;
1519
1520   if (!dbus_message_append_args (message,
1521                                  DBUS_TYPE_STRING, EXISTENT_SERVICE_NAME,
1522                                  DBUS_TYPE_UINT32, 0,
1523                                  DBUS_TYPE_INVALID))
1524     {
1525       dbus_message_unref (message);
1526       return TRUE;
1527     }
1528   
1529   if (!dbus_connection_send (connection, message, &serial))
1530     {
1531       dbus_message_unref (message);
1532       return TRUE;
1533     }
1534
1535   dbus_message_unref (message);
1536   message = NULL;
1537
1538   bus_test_run_everything (context);
1539
1540   /* now wait for the message bus to hear back from the activated
1541    * service.
1542    */
1543   block_connection_until_message_from_bus (context, connection);
1544
1545   bus_test_run_everything (context);
1546
1547   if (!dbus_connection_get_is_connected (connection))
1548     {
1549       _dbus_verbose ("connection was disconnected\n");
1550       return TRUE;
1551     }
1552   
1553   retval = FALSE;
1554   
1555   message = pop_message_waiting_for_memory (connection);
1556   if (message == NULL)
1557     {
1558       _dbus_warn ("Did not receive any messages after %s %d on %p\n",
1559                   "ActivateService", serial, connection);
1560       goto out;
1561     }
1562
1563   verbose_message_received (connection, message);
1564   _dbus_verbose ("  (after sending %s)\n", "ActivateService");
1565
1566   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1567     {
1568       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1569         {
1570           _dbus_warn ("Message has wrong sender %s\n",
1571                       dbus_message_get_sender (message) ?
1572                       dbus_message_get_sender (message) : "(none)");
1573           goto out;
1574         }
1575       
1576       if (dbus_message_is_error (message,
1577                                  DBUS_ERROR_NO_MEMORY))
1578         {
1579           ; /* good, this is a valid response */
1580         }
1581       else if (dbus_message_is_error (message,
1582                                       DBUS_ERROR_SPAWN_CHILD_EXITED))
1583         {
1584           ; /* good, this is expected also */
1585         }
1586       else
1587         {
1588           _dbus_warn ("Did not expect error %s\n",
1589                       dbus_message_get_error_name (message));
1590           goto out;
1591         }
1592     }
1593   else
1594     {
1595       dbus_bool_t got_service_deleted;
1596       dbus_bool_t got_error;
1597       
1598       if (!check_base_service_activated (context, connection,
1599                                          message, &base_service))
1600         goto out;
1601
1602       dbus_message_unref (message);
1603       message = NULL;
1604
1605       /* We may need to block here for the test service to exit or finish up */
1606       block_connection_until_message_from_bus (context, connection);
1607       
1608       message = dbus_connection_borrow_message (connection);
1609       if (message == NULL)
1610         {
1611           _dbus_warn ("Did not receive any messages after base service creation notification\n");
1612           goto out;
1613         }
1614
1615       got_service_deleted = dbus_message_is_signal (message,
1616                                                     DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1617                                                     "ServiceDeleted");
1618       got_error = dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
1619       
1620       dbus_connection_return_message (connection, message);
1621       message = NULL;
1622
1623       if (got_error)
1624         {
1625           if (!check_got_error (context, connection,
1626                                 DBUS_ERROR_SPAWN_CHILD_EXITED,
1627                                 DBUS_ERROR_NO_MEMORY,
1628                                 NULL))
1629             goto out;
1630
1631           /* A service deleted should be coming along now after this error.
1632            * We can also get the error *after* the service deleted.
1633            */
1634           got_service_deleted = TRUE;
1635         }
1636       
1637       if (got_service_deleted)
1638         {
1639           /* The service started up and got a base address, but then
1640            * failed to register under EXISTENT_SERVICE_NAME
1641            */
1642           CheckServiceDeletedData csdd;
1643           
1644           csdd.expected_service_name = base_service;
1645           csdd.failed = FALSE;
1646           bus_test_clients_foreach (check_service_deleted_foreach,
1647                                     &csdd);
1648
1649           if (csdd.failed)
1650             goto out;
1651
1652           /* Now we should get an error about the service exiting
1653            * if we didn't get it before.
1654            */
1655           if (!got_error)
1656             {
1657               block_connection_until_message_from_bus (context, connection);
1658               
1659               /* and process everything again */
1660               bus_test_run_everything (context);
1661               
1662               if (!check_got_error (context, connection,
1663                                     DBUS_ERROR_SPAWN_CHILD_EXITED,
1664                                     NULL))
1665                 goto out;
1666             }
1667         }
1668       else
1669         {
1670           message = pop_message_waiting_for_memory (connection);
1671           if (message == NULL)
1672             {
1673               _dbus_warn ("Failed to pop message we just put back! should have been a ServiceCreated\n");
1674               goto out;
1675             }
1676           
1677           if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
1678                                         base_service, message))
1679             goto out;
1680           
1681           dbus_message_unref (message);
1682           message = NULL;
1683
1684
1685           if (!check_no_leftovers (context))
1686             {
1687               _dbus_warn ("Messages were left over after successful activation\n");
1688               goto out;
1689             }
1690
1691           if (!check_send_exit_to_service (context, connection,
1692                                            EXISTENT_SERVICE_NAME, base_service))
1693             goto out;
1694         }
1695     }
1696   
1697   retval = TRUE;
1698   
1699  out:
1700   if (message)
1701     dbus_message_unref (message);
1702
1703   if (base_service)
1704     dbus_free (base_service);
1705   
1706   return retval;
1707 }
1708
1709 /* returns TRUE if the correct thing happens,
1710  * but the correct thing may include OOM errors.
1711  */
1712 static dbus_bool_t
1713 check_segfault_service_activation (BusContext     *context,
1714                                    DBusConnection *connection)
1715 {
1716   DBusMessage *message;
1717   dbus_int32_t serial;
1718   dbus_bool_t retval;
1719   DBusError error;
1720   
1721   dbus_error_init (&error);
1722   
1723   message = dbus_message_new_method_call (DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
1724                                           "ActivateService",
1725                                           DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1726
1727   if (message == NULL)
1728     return TRUE;
1729
1730   if (!dbus_message_append_args (message,
1731                                  DBUS_TYPE_STRING,
1732                                  "org.freedesktop.DBus.TestSuiteSegfaultService",
1733                                  DBUS_TYPE_UINT32, 0,
1734                                  DBUS_TYPE_INVALID))
1735     {
1736       dbus_message_unref (message);
1737       return TRUE;
1738     }
1739   
1740   if (!dbus_connection_send (connection, message, &serial))
1741     {
1742       dbus_message_unref (message);
1743       return TRUE;
1744     }
1745
1746   dbus_message_unref (message);
1747   message = NULL;
1748
1749   bus_test_run_everything (context);
1750   block_connection_until_message_from_bus (context, connection);
1751   bus_test_run_everything (context);
1752
1753   if (!dbus_connection_get_is_connected (connection))
1754     {
1755       _dbus_verbose ("connection was disconnected\n");
1756       return TRUE;
1757     }
1758   
1759   retval = FALSE;
1760   
1761   message = pop_message_waiting_for_memory (connection);
1762   if (message == NULL)
1763     {
1764       _dbus_warn ("Did not receive a reply to %s %d on %p\n",
1765                   "ActivateService", serial, connection);
1766       goto out;
1767     }
1768
1769   verbose_message_received (connection, message);
1770
1771   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1772     {
1773       if (!dbus_message_has_sender (message, DBUS_SERVICE_ORG_FREEDESKTOP_DBUS))
1774         {
1775           _dbus_warn ("Message has wrong sender %s\n",
1776                       dbus_message_get_sender (message) ?
1777                       dbus_message_get_sender (message) : "(none)");
1778           goto out;
1779         }
1780       
1781       if (dbus_message_is_error (message,
1782                                  DBUS_ERROR_NO_MEMORY))
1783         {
1784           ; /* good, this is a valid response */
1785         }
1786       else if (dbus_message_is_error (message,
1787                                       DBUS_ERROR_SPAWN_CHILD_SIGNALED))
1788         {
1789           ; /* good, this is expected also */
1790         }
1791       else
1792         {
1793           warn_unexpected (connection, message, "not this error");
1794
1795           goto out;
1796         }
1797     }
1798   else
1799     {
1800       _dbus_warn ("Did not expect to successfully activate segfault service\n");
1801       goto out;
1802     }
1803
1804   retval = TRUE;
1805   
1806  out:
1807   if (message)
1808     dbus_message_unref (message);
1809   
1810   return retval;
1811 }
1812
1813 typedef struct
1814 {
1815   Check1Func func;
1816   BusContext *context;
1817 } Check1Data;
1818
1819 static dbus_bool_t
1820 check_oom_check1_func (void *data)
1821 {
1822   Check1Data *d = data;
1823
1824   if (! (* d->func) (d->context))
1825     return FALSE;
1826   
1827   if (!check_no_leftovers (d->context))
1828     {
1829       _dbus_warn ("Messages were left over, should be covered by test suite\n");
1830       return FALSE;
1831     }
1832
1833   return TRUE;
1834 }
1835
1836 static void
1837 check1_try_iterations (BusContext *context,
1838                        const char *description,
1839                        Check1Func  func)
1840 {
1841   Check1Data d;
1842
1843   d.func = func;
1844   d.context = context;
1845
1846   if (!_dbus_test_oom_handling (description, check_oom_check1_func,
1847                                 &d))
1848     _dbus_assert_not_reached ("test failed");
1849 }
1850
1851 typedef struct
1852 {
1853   Check2Func func;
1854   BusContext *context;
1855   DBusConnection *connection;
1856 } Check2Data;
1857
1858 static dbus_bool_t
1859 check_oom_check2_func (void *data)
1860 {
1861   Check2Data *d = data;
1862
1863   if (! (* d->func) (d->context, d->connection))
1864     return FALSE;
1865   
1866   if (!check_no_leftovers (d->context))
1867     {
1868       _dbus_warn ("Messages were left over, should be covered by test suite");
1869       return FALSE;
1870     }
1871
1872   return TRUE;
1873 }
1874
1875 static void
1876 check2_try_iterations (BusContext     *context,
1877                        DBusConnection *connection,
1878                        const char     *description,
1879                        Check2Func      func)
1880 {
1881   Check2Data d;
1882
1883   d.func = func;
1884   d.context = context;
1885   d.connection = connection;
1886   
1887   if (!_dbus_test_oom_handling (description, check_oom_check2_func,
1888                                 &d))
1889     _dbus_assert_not_reached ("test failed");
1890 }
1891
1892 dbus_bool_t
1893 bus_dispatch_test (const DBusString *test_data_dir)
1894 {
1895   BusContext *context;
1896   DBusConnection *foo;
1897   DBusConnection *bar;
1898   DBusConnection *baz;
1899   DBusError error;
1900
1901   dbus_error_init (&error);
1902   
1903   context = bus_context_new_test (test_data_dir,
1904                                   "valid-config-files/debug-allow-all.conf");
1905   if (context == NULL)
1906     return FALSE;
1907   
1908   foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1909   if (foo == NULL)
1910     _dbus_assert_not_reached ("could not alloc connection");
1911
1912   if (!bus_setup_debug_client (foo))
1913     _dbus_assert_not_reached ("could not set up connection");
1914
1915   if (!check_hello_message (context, foo))
1916     _dbus_assert_not_reached ("hello message failed");
1917   
1918   bar = dbus_connection_open ("debug-pipe:name=test-server", &error);
1919   if (bar == NULL)
1920     _dbus_assert_not_reached ("could not alloc connection");
1921
1922   if (!bus_setup_debug_client (bar))
1923     _dbus_assert_not_reached ("could not set up connection");
1924
1925   if (!check_hello_message (context, bar))
1926     _dbus_assert_not_reached ("hello message failed");
1927   
1928   baz = dbus_connection_open ("debug-pipe:name=test-server", &error);
1929   if (baz == NULL)
1930     _dbus_assert_not_reached ("could not alloc connection");
1931
1932   if (!bus_setup_debug_client (baz))
1933     _dbus_assert_not_reached ("could not set up connection");
1934
1935   if (!check_hello_message (context, baz))
1936     _dbus_assert_not_reached ("hello message failed");
1937
1938   if (!check_no_leftovers (context))
1939     {
1940       _dbus_warn ("Messages were left over after setting up initial connections");
1941       _dbus_assert_not_reached ("initial connection setup failed");
1942     }
1943   
1944   check1_try_iterations (context, "create_and_hello",
1945                          check_hello_connection);
1946   
1947   check2_try_iterations (context, foo, "nonexistent_service_activation",
1948                          check_nonexistent_service_activation);
1949
1950   check2_try_iterations (context, foo, "segfault_service_activation",
1951                          check_segfault_service_activation);
1952   
1953   check2_try_iterations (context, foo, "existent_service_activation",
1954                          check_existent_service_activation);
1955   
1956   _dbus_verbose ("Disconnecting foo, bar, and baz\n");
1957
1958   kill_client_connection_unchecked (foo);
1959   kill_client_connection_unchecked (bar);
1960   kill_client_connection_unchecked (baz);
1961
1962   bus_context_unref (context);
1963   
1964   return TRUE;
1965 }
1966
1967 dbus_bool_t
1968 bus_dispatch_sha1_test (const DBusString *test_data_dir)
1969 {
1970   BusContext *context;
1971   DBusConnection *foo;
1972   DBusError error;
1973
1974   dbus_error_init (&error);
1975   
1976   /* Test SHA1 authentication */
1977   _dbus_verbose ("Testing SHA1 context\n");
1978   
1979   context = bus_context_new_test (test_data_dir,
1980                                   "valid-config-files/debug-allow-all-sha1.conf");
1981   if (context == NULL)
1982     return FALSE;
1983
1984   foo = dbus_connection_open ("debug-pipe:name=test-server", &error);
1985   if (foo == NULL)
1986     _dbus_assert_not_reached ("could not alloc connection");
1987
1988   if (!bus_setup_debug_client (foo))
1989     _dbus_assert_not_reached ("could not set up connection");
1990
1991   if (!check_hello_message (context, foo))
1992     _dbus_assert_not_reached ("hello message failed");
1993
1994   if (!check_no_leftovers (context))
1995     {
1996       _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
1997       _dbus_assert_not_reached ("initial connection setup failed");
1998     }
1999   
2000   check1_try_iterations (context, "create_and_hello_sha1",
2001                          check_hello_connection);
2002
2003   kill_client_connection_unchecked (foo);
2004
2005   bus_context_unref (context);
2006
2007   return TRUE;
2008 }
2009
2010 #endif /* DBUS_BUILD_TESTS */