os/ossrv/ofdbus/dbus/bus/dispatch.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* -*- mode: C; c-file-style: "gnu" -*- */
sl@0
     2
/* dispatch.c  Message dispatcher
sl@0
     3
 *
sl@0
     4
 * Copyright (C) 2003  CodeFactory AB
sl@0
     5
 * Copyright (C) 2003, 2004, 2005  Red Hat, Inc.
sl@0
     6
 * Copyright (C) 2004  Imendio HB
sl@0
     7
 * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     8
 * Licensed under the Academic Free License version 2.1
sl@0
     9
 * 
sl@0
    10
 * This program is free software; you can redistribute it and/or modify
sl@0
    11
 * it under the terms of the GNU General Public License as published by
sl@0
    12
 * the Free Software Foundation; either version 2 of the License, or
sl@0
    13
 * (at your option) any later version.
sl@0
    14
 *
sl@0
    15
 * This program is distributed in the hope that it will be useful,
sl@0
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
sl@0
    18
 * GNU General Public License for more details.
sl@0
    19
 * 
sl@0
    20
 * You should have received a copy of the GNU General Public License
sl@0
    21
 * along with this program; if not, write to the Free Software
sl@0
    22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
sl@0
    23
 *
sl@0
    24
 */
sl@0
    25
sl@0
    26
#include "dispatch.h"
sl@0
    27
#include "connection.h"
sl@0
    28
#include "driver.h"
sl@0
    29
#include "services.h"
sl@0
    30
#include "activation.h"
sl@0
    31
#include "utils.h"
sl@0
    32
#include "bus.h"
sl@0
    33
#include "signals.h"
sl@0
    34
#include "test.h"
sl@0
    35
#ifndef __SYMBIAN32__
sl@0
    36
#include <dbus/dbus-internals.h>
sl@0
    37
#else
sl@0
    38
#include "dbus-internals.h"
sl@0
    39
#endif //__SYMBIAN32__
sl@0
    40
#include <string.h>
sl@0
    41
sl@0
    42
#ifdef __SYMBIAN32__
sl@0
    43
#include "config.h"
sl@0
    44
#endif //__SYMBIAN32__
sl@0
    45
sl@0
    46
static dbus_bool_t
sl@0
    47
send_one_message (DBusConnection *connection,
sl@0
    48
                  BusContext     *context,
sl@0
    49
                  DBusConnection *sender,
sl@0
    50
                  DBusConnection *addressed_recipient,
sl@0
    51
                  DBusMessage    *message,
sl@0
    52
                  BusTransaction *transaction,
sl@0
    53
                  DBusError      *error)
sl@0
    54
{
sl@0
    55
  if (!bus_context_check_security_policy (context, transaction,
sl@0
    56
                                          sender,
sl@0
    57
                                          addressed_recipient,
sl@0
    58
                                          connection,
sl@0
    59
                                          message,
sl@0
    60
                                          NULL))
sl@0
    61
    return TRUE; /* silently don't send it */
sl@0
    62
  
sl@0
    63
  if (!bus_transaction_send (transaction,
sl@0
    64
                             connection,
sl@0
    65
                             message))
sl@0
    66
    {
sl@0
    67
      BUS_SET_OOM (error);
sl@0
    68
      return FALSE;
sl@0
    69
    }
sl@0
    70
sl@0
    71
  return TRUE;
sl@0
    72
}
sl@0
    73
sl@0
    74
dbus_bool_t
sl@0
    75
bus_dispatch_matches (BusTransaction *transaction,
sl@0
    76
                      DBusConnection *sender,
sl@0
    77
                      DBusConnection *addressed_recipient,
sl@0
    78
                      DBusMessage    *message,
sl@0
    79
                      DBusError      *error)
sl@0
    80
{
sl@0
    81
  DBusError tmp_error;
sl@0
    82
  BusConnections *connections;
sl@0
    83
  DBusList *recipients;
sl@0
    84
  BusMatchmaker *matchmaker;
sl@0
    85
  DBusList *link;
sl@0
    86
  BusContext *context;
sl@0
    87
sl@0
    88
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
sl@0
    89
sl@0
    90
  /* sender and recipient can both be NULL for the bus driver,
sl@0
    91
   * or for signals with no particular recipient
sl@0
    92
   */
sl@0
    93
sl@0
    94
  _dbus_assert (sender == NULL || bus_connection_is_active (sender));
sl@0
    95
  _dbus_assert (dbus_message_get_sender (message) != NULL);
sl@0
    96
sl@0
    97
  connections = bus_transaction_get_connections (transaction);
sl@0
    98
  
sl@0
    99
  dbus_error_init (&tmp_error);
sl@0
   100
  context = bus_transaction_get_context (transaction);
sl@0
   101
  matchmaker = bus_context_get_matchmaker (context);
sl@0
   102
sl@0
   103
  recipients = NULL;
sl@0
   104
  if (!bus_matchmaker_get_recipients (matchmaker, connections,
sl@0
   105
                                      sender, addressed_recipient, message,
sl@0
   106
                                      &recipients))
sl@0
   107
    {
sl@0
   108
      BUS_SET_OOM (error);
sl@0
   109
      return FALSE;
sl@0
   110
    }
sl@0
   111
sl@0
   112
  link = _dbus_list_get_first_link (&recipients);
sl@0
   113
  while (link != NULL)
sl@0
   114
    {
sl@0
   115
      DBusConnection *dest;
sl@0
   116
sl@0
   117
      dest = link->data;
sl@0
   118
sl@0
   119
      if (!send_one_message (dest, context, sender, addressed_recipient,
sl@0
   120
                             message, transaction, &tmp_error))
sl@0
   121
        break;
sl@0
   122
sl@0
   123
      link = _dbus_list_get_next_link (&recipients, link);
sl@0
   124
    }
sl@0
   125
sl@0
   126
  _dbus_list_clear (&recipients);
sl@0
   127
  
sl@0
   128
  if (dbus_error_is_set (&tmp_error))
sl@0
   129
    {
sl@0
   130
      dbus_move_error (&tmp_error, error);
sl@0
   131
      return FALSE;
sl@0
   132
    }
sl@0
   133
  else
sl@0
   134
    return TRUE;
sl@0
   135
}
sl@0
   136
sl@0
   137
static DBusHandlerResult
sl@0
   138
bus_dispatch (DBusConnection *connection,
sl@0
   139
              DBusMessage    *message)
sl@0
   140
{
sl@0
   141
  const char *sender, *service_name;
sl@0
   142
  DBusError error;
sl@0
   143
  BusTransaction *transaction;
sl@0
   144
  BusContext *context;
sl@0
   145
  DBusHandlerResult result;
sl@0
   146
  DBusConnection *addressed_recipient;
sl@0
   147
  
sl@0
   148
  result = DBUS_HANDLER_RESULT_HANDLED;
sl@0
   149
  
sl@0
   150
  transaction = NULL;
sl@0
   151
  addressed_recipient = NULL;
sl@0
   152
  dbus_error_init (&error);
sl@0
   153
  
sl@0
   154
  context = bus_connection_get_context (connection);
sl@0
   155
  _dbus_assert (context != NULL);
sl@0
   156
  
sl@0
   157
  /* If we can't even allocate an OOM error, we just go to sleep
sl@0
   158
   * until we can.
sl@0
   159
   */
sl@0
   160
  while (!bus_connection_preallocate_oom_error (connection))
sl@0
   161
    _dbus_wait_for_memory ();
sl@0
   162
  
sl@0
   163
  /* Ref connection in case we disconnect it at some point in here */
sl@0
   164
  dbus_connection_ref (connection);
sl@0
   165
  
sl@0
   166
  service_name = dbus_message_get_destination (message);
sl@0
   167
sl@0
   168
#ifdef DBUS_ENABLE_VERBOSE_MODE
sl@0
   169
  {
sl@0
   170
    const char *interface_name, *member_name, *error_name;
sl@0
   171
sl@0
   172
    interface_name = dbus_message_get_interface (message);
sl@0
   173
    member_name = dbus_message_get_member (message);
sl@0
   174
    error_name = dbus_message_get_error_name (message);
sl@0
   175
    
sl@0
   176
    _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
sl@0
   177
                   interface_name ? interface_name : "(no interface)",
sl@0
   178
                   member_name ? member_name : "(no member)",
sl@0
   179
                   error_name ? error_name : "(no error name)",
sl@0
   180
                   service_name ? service_name : "peer");
sl@0
   181
  }
sl@0
   182
#endif /* DBUS_ENABLE_VERBOSE_MODE */
sl@0
   183
  
sl@0
   184
  /* If service_name is NULL, if it's a signal we send it to all
sl@0
   185
   * connections with a match rule. If it's not a signal, there
sl@0
   186
   * are some special cases here but mostly we just bail out.
sl@0
   187
   */
sl@0
   188
  if (service_name == NULL)
sl@0
   189
    {
sl@0
   190
      if (dbus_message_is_signal (message,
sl@0
   191
                                  DBUS_INTERFACE_LOCAL,
sl@0
   192
                                  "Disconnected"))
sl@0
   193
        {
sl@0
   194
          bus_connection_disconnected (connection);
sl@0
   195
          goto out;
sl@0
   196
        }
sl@0
   197
sl@0
   198
      if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
sl@0
   199
        {
sl@0
   200
          /* DBusConnection also handles some of these automatically, we leave
sl@0
   201
           * it to do so.
sl@0
   202
           */
sl@0
   203
          result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sl@0
   204
          goto out;
sl@0
   205
        }
sl@0
   206
    }
sl@0
   207
  
sl@0
   208
  /* Create our transaction */
sl@0
   209
  transaction = bus_transaction_new (context);
sl@0
   210
  if (transaction == NULL)
sl@0
   211
    {
sl@0
   212
      BUS_SET_OOM (&error);
sl@0
   213
      goto out;
sl@0
   214
    }
sl@0
   215
  
sl@0
   216
  /* Assign a sender to the message */
sl@0
   217
  if (bus_connection_is_active (connection))
sl@0
   218
    {
sl@0
   219
      sender = bus_connection_get_name (connection);
sl@0
   220
      _dbus_assert (sender != NULL);
sl@0
   221
sl@0
   222
      if (!dbus_message_set_sender (message, sender))
sl@0
   223
        {
sl@0
   224
          BUS_SET_OOM (&error);
sl@0
   225
          goto out;
sl@0
   226
        }
sl@0
   227
sl@0
   228
      /* We need to refetch the service name here, because
sl@0
   229
       * dbus_message_set_sender can cause the header to be
sl@0
   230
       * reallocated, and thus the service_name pointer will become
sl@0
   231
       * invalid.
sl@0
   232
       */
sl@0
   233
      service_name = dbus_message_get_destination (message);
sl@0
   234
    }
sl@0
   235
  
sl@0
   236
  if (service_name &&
sl@0
   237
      strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
sl@0
   238
    {
sl@0
   239
      if (!bus_context_check_security_policy (context, transaction,
sl@0
   240
                                              connection, NULL, NULL, message, &error))
sl@0
   241
        {
sl@0
   242
          _dbus_verbose ("Security policy rejected message\n");
sl@0
   243
          goto out;
sl@0
   244
        }
sl@0
   245
sl@0
   246
      _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
sl@0
   247
      if (!bus_driver_handle_message (connection, transaction, message, &error))
sl@0
   248
        goto out;
sl@0
   249
    }
sl@0
   250
  else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
sl@0
   251
    {
sl@0
   252
      _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
sl@0
   253
      dbus_connection_close (connection);
sl@0
   254
      goto out;
sl@0
   255
    }
sl@0
   256
  else if (service_name != NULL) /* route to named service */
sl@0
   257
    {
sl@0
   258
      DBusString service_string;
sl@0
   259
      BusService *service;
sl@0
   260
      BusRegistry *registry;
sl@0
   261
sl@0
   262
      _dbus_assert (service_name != NULL);
sl@0
   263
      
sl@0
   264
      registry = bus_connection_get_registry (connection);
sl@0
   265
      
sl@0
   266
      _dbus_string_init_const (&service_string, service_name);
sl@0
   267
      service = bus_registry_lookup (registry, &service_string);
sl@0
   268
sl@0
   269
      if (service == NULL && dbus_message_get_auto_start (message))
sl@0
   270
        {
sl@0
   271
          BusActivation *activation;
sl@0
   272
          /* We can't do the security policy check here, since the addressed
sl@0
   273
           * recipient service doesn't exist yet. We do it before sending the
sl@0
   274
           * message after the service has been created.
sl@0
   275
           */
sl@0
   276
          activation = bus_connection_get_activation (connection);
sl@0
   277
sl@0
   278
          if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
sl@0
   279
                                                message, service_name, &error))
sl@0
   280
            {
sl@0
   281
              _DBUS_ASSERT_ERROR_IS_SET (&error);
sl@0
   282
              _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
sl@0
   283
              goto out;
sl@0
   284
            }
sl@0
   285
          
sl@0
   286
          goto out;
sl@0
   287
        }
sl@0
   288
      else if (service == NULL)
sl@0
   289
        {
sl@0
   290
          dbus_set_error (&error,
sl@0
   291
                          DBUS_ERROR_NAME_HAS_NO_OWNER,
sl@0
   292
                          "Name \"%s\" does not exist",
sl@0
   293
                          service_name);
sl@0
   294
          goto out;
sl@0
   295
        }
sl@0
   296
      else
sl@0
   297
        {
sl@0
   298
          addressed_recipient = bus_service_get_primary_owners_connection (service);
sl@0
   299
          _dbus_assert (addressed_recipient != NULL);
sl@0
   300
          
sl@0
   301
          if (!bus_context_check_security_policy (context, transaction,
sl@0
   302
                                                  connection, addressed_recipient,
sl@0
   303
                                                  addressed_recipient,
sl@0
   304
                                                  message, &error))
sl@0
   305
            goto out;
sl@0
   306
          
sl@0
   307
          /* Dispatch the message */
sl@0
   308
          if (!bus_transaction_send (transaction, addressed_recipient, message))
sl@0
   309
            {
sl@0
   310
              BUS_SET_OOM (&error);
sl@0
   311
              goto out;
sl@0
   312
            }
sl@0
   313
        }
sl@0
   314
    }
sl@0
   315
sl@0
   316
  /* Now match the messages against any match rules, which will send
sl@0
   317
   * out signals and such. addressed_recipient may == NULL.
sl@0
   318
   */
sl@0
   319
  if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
sl@0
   320
    goto out;
sl@0
   321
  
sl@0
   322
 out:
sl@0
   323
  if (dbus_error_is_set (&error))
sl@0
   324
    {
sl@0
   325
      if (!dbus_connection_get_is_connected (connection))
sl@0
   326
        {
sl@0
   327
          /* If we disconnected it, we won't bother to send it any error
sl@0
   328
           * messages.
sl@0
   329
           */
sl@0
   330
          _dbus_verbose ("Not sending error to connection we disconnected\n");
sl@0
   331
        }
sl@0
   332
      else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
   333
        {
sl@0
   334
          bus_connection_send_oom_error (connection, message);
sl@0
   335
sl@0
   336
          /* cancel transaction due to OOM */
sl@0
   337
          if (transaction != NULL)
sl@0
   338
            {
sl@0
   339
              bus_transaction_cancel_and_free (transaction);
sl@0
   340
              transaction = NULL;
sl@0
   341
            }
sl@0
   342
        }
sl@0
   343
      else
sl@0
   344
        {
sl@0
   345
          /* Try to send the real error, if no mem to do that, send
sl@0
   346
           * the OOM error
sl@0
   347
           */
sl@0
   348
          _dbus_assert (transaction != NULL);
sl@0
   349
          if (!bus_transaction_send_error_reply (transaction, connection,
sl@0
   350
                                                 &error, message))
sl@0
   351
            {
sl@0
   352
              bus_connection_send_oom_error (connection, message);
sl@0
   353
              
sl@0
   354
              /* cancel transaction due to OOM */
sl@0
   355
              if (transaction != NULL)
sl@0
   356
                {
sl@0
   357
                  bus_transaction_cancel_and_free (transaction);
sl@0
   358
                  transaction = NULL;
sl@0
   359
                }
sl@0
   360
            }
sl@0
   361
        }
sl@0
   362
     
sl@0
   363
      
sl@0
   364
      dbus_error_free (&error);
sl@0
   365
    }
sl@0
   366
sl@0
   367
  if (transaction != NULL)
sl@0
   368
    {
sl@0
   369
      bus_transaction_execute_and_free (transaction);
sl@0
   370
    }
sl@0
   371
sl@0
   372
  dbus_connection_unref (connection);
sl@0
   373
sl@0
   374
  return result;
sl@0
   375
}
sl@0
   376
sl@0
   377
static DBusHandlerResult
sl@0
   378
bus_dispatch_message_filter (DBusConnection     *connection,
sl@0
   379
                             DBusMessage        *message,
sl@0
   380
                             void               *user_data)
sl@0
   381
{
sl@0
   382
  return bus_dispatch (connection, message);
sl@0
   383
}
sl@0
   384
sl@0
   385
dbus_bool_t
sl@0
   386
bus_dispatch_add_connection (DBusConnection *connection)
sl@0
   387
{  
sl@0
   388
  if (!dbus_connection_add_filter (connection,
sl@0
   389
                                   bus_dispatch_message_filter,
sl@0
   390
                                   NULL, NULL))
sl@0
   391
    return FALSE;
sl@0
   392
  
sl@0
   393
  return TRUE;
sl@0
   394
}
sl@0
   395
sl@0
   396
void
sl@0
   397
bus_dispatch_remove_connection (DBusConnection *connection)
sl@0
   398
{
sl@0
   399
  /* Here we tell the bus driver that we want to get off. */
sl@0
   400
  bus_driver_remove_connection (connection);
sl@0
   401
sl@0
   402
  dbus_connection_remove_filter (connection,
sl@0
   403
                                 bus_dispatch_message_filter,
sl@0
   404
                                 NULL);
sl@0
   405
}
sl@0
   406
sl@0
   407
#ifdef DBUS_BUILD_TESTS
sl@0
   408
sl@0
   409
#include <stdio.h>
sl@0
   410
sl@0
   411
/* This is used to know whether we need to block in order to finish
sl@0
   412
 * sending a message, or whether the initial dbus_connection_send()
sl@0
   413
 * already flushed the queue.
sl@0
   414
 */
sl@0
   415
#define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))
sl@0
   416
sl@0
   417
typedef dbus_bool_t (* Check1Func) (BusContext     *context);
sl@0
   418
typedef dbus_bool_t (* Check2Func) (BusContext     *context,
sl@0
   419
                                    DBusConnection *connection);
sl@0
   420
sl@0
   421
static dbus_bool_t check_no_leftovers (BusContext *context);
sl@0
   422
sl@0
   423
static void
sl@0
   424
block_connection_until_message_from_bus (BusContext     *context,
sl@0
   425
                                         DBusConnection *connection,
sl@0
   426
                                         const char     *what_is_expected)
sl@0
   427
{
sl@0
   428
  _dbus_verbose ("expecting: %s\n", what_is_expected);
sl@0
   429
  
sl@0
   430
  while (dbus_connection_get_dispatch_status (connection) ==
sl@0
   431
         DBUS_DISPATCH_COMPLETE &&
sl@0
   432
         dbus_connection_get_is_connected (connection))
sl@0
   433
    {
sl@0
   434
      #ifndef __SYMBIAN32__
sl@0
   435
      bus_test_run_bus_loop (context, TRUE);
sl@0
   436
      #else										//_dbus_loop_iterate() blocks indefinitly on setting argument to TRUE on Symbian,it must be investigated 
sl@0
   437
      	bus_test_run_bus_loop (context, FALSE);
sl@0
   438
      #endif
sl@0
   439
      bus_test_run_clients_loop (FALSE);
sl@0
   440
    }
sl@0
   441
}
sl@0
   442
sl@0
   443
static void
sl@0
   444
spin_connection_until_authenticated (BusContext     *context,
sl@0
   445
                                     DBusConnection *connection)
sl@0
   446
{
sl@0
   447
  _dbus_verbose ("Spinning to auth connection %p\n", connection);
sl@0
   448
  while (!dbus_connection_get_is_authenticated (connection) &&
sl@0
   449
         dbus_connection_get_is_connected (connection))
sl@0
   450
    {
sl@0
   451
      bus_test_run_bus_loop (context, FALSE);
sl@0
   452
      bus_test_run_clients_loop (FALSE);
sl@0
   453
    }
sl@0
   454
  _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
sl@0
   455
}
sl@0
   456
sl@0
   457
/* compensate for fact that pop_message() can return #NULL due to OOM */
sl@0
   458
static DBusMessage*
sl@0
   459
pop_message_waiting_for_memory (DBusConnection *connection)
sl@0
   460
{
sl@0
   461
  while (dbus_connection_get_dispatch_status (connection) ==
sl@0
   462
         DBUS_DISPATCH_NEED_MEMORY)
sl@0
   463
    _dbus_wait_for_memory ();
sl@0
   464
sl@0
   465
  return dbus_connection_pop_message (connection);
sl@0
   466
}
sl@0
   467
sl@0
   468
static DBusMessage*
sl@0
   469
borrow_message_waiting_for_memory (DBusConnection *connection)
sl@0
   470
{
sl@0
   471
  while (dbus_connection_get_dispatch_status (connection) ==
sl@0
   472
         DBUS_DISPATCH_NEED_MEMORY)
sl@0
   473
    _dbus_wait_for_memory ();
sl@0
   474
sl@0
   475
  return dbus_connection_borrow_message (connection);
sl@0
   476
}
sl@0
   477
sl@0
   478
static void
sl@0
   479
warn_unexpected_real (DBusConnection *connection,
sl@0
   480
                      DBusMessage    *message,
sl@0
   481
                      const char     *expected,
sl@0
   482
                      const char     *function,
sl@0
   483
                      int             line)
sl@0
   484
{
sl@0
   485
  if (message)
sl@0
   486
    _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
sl@0
   487
                function, line,
sl@0
   488
                dbus_message_get_interface (message) ?
sl@0
   489
                dbus_message_get_interface (message) : "(unset)",
sl@0
   490
                dbus_message_get_member (message) ?
sl@0
   491
                dbus_message_get_member (message) : "(unset)",
sl@0
   492
                dbus_message_get_error_name (message) ?
sl@0
   493
                dbus_message_get_error_name (message) : "(unset)",
sl@0
   494
                connection,
sl@0
   495
                expected);
sl@0
   496
  else
sl@0
   497
    _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
sl@0
   498
                function, line, connection, expected);
sl@0
   499
}
sl@0
   500
sl@0
   501
#define warn_unexpected(connection, message, expected) \
sl@0
   502
  warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)
sl@0
   503
sl@0
   504
static void
sl@0
   505
verbose_message_received (DBusConnection *connection,
sl@0
   506
                          DBusMessage    *message)
sl@0
   507
{
sl@0
   508
  _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
sl@0
   509
                 dbus_message_get_interface (message) ?
sl@0
   510
                 dbus_message_get_interface (message) : "(unset)",
sl@0
   511
                 dbus_message_get_member (message) ?
sl@0
   512
                 dbus_message_get_member (message) : "(unset)",
sl@0
   513
                 dbus_message_get_error_name (message) ?
sl@0
   514
                 dbus_message_get_error_name (message) : "(unset)",
sl@0
   515
                 connection);
sl@0
   516
}
sl@0
   517
sl@0
   518
typedef enum
sl@0
   519
{
sl@0
   520
  SERVICE_CREATED,
sl@0
   521
  OWNER_CHANGED,
sl@0
   522
  SERVICE_DELETED
sl@0
   523
} ServiceInfoKind;
sl@0
   524
sl@0
   525
typedef struct
sl@0
   526
{
sl@0
   527
  ServiceInfoKind expected_kind;
sl@0
   528
  const char *expected_service_name;
sl@0
   529
  dbus_bool_t failed;
sl@0
   530
  DBusConnection *skip_connection;
sl@0
   531
} CheckServiceOwnerChangedData;
sl@0
   532
sl@0
   533
static dbus_bool_t
sl@0
   534
check_service_owner_changed_foreach (DBusConnection *connection,
sl@0
   535
                                     void           *data)
sl@0
   536
{
sl@0
   537
  CheckServiceOwnerChangedData *d = data;
sl@0
   538
  DBusMessage *message;
sl@0
   539
  DBusError error;
sl@0
   540
  const char *service_name, *old_owner, *new_owner;
sl@0
   541
sl@0
   542
  if (d->expected_kind == SERVICE_CREATED 
sl@0
   543
      && connection == d->skip_connection)
sl@0
   544
    return TRUE;
sl@0
   545
sl@0
   546
  dbus_error_init (&error);
sl@0
   547
  d->failed = TRUE;
sl@0
   548
  
sl@0
   549
  message = pop_message_waiting_for_memory (connection);
sl@0
   550
  if (message == NULL)
sl@0
   551
    {
sl@0
   552
      _dbus_warn ("Did not receive a message on %p, expecting %s\n",
sl@0
   553
                  connection, "NameOwnerChanged");
sl@0
   554
      goto out;
sl@0
   555
    }
sl@0
   556
  else if (!dbus_message_is_signal (message,
sl@0
   557
                                    DBUS_INTERFACE_DBUS,
sl@0
   558
                                    "NameOwnerChanged"))
sl@0
   559
    {
sl@0
   560
      warn_unexpected (connection, message, "NameOwnerChanged");
sl@0
   561
sl@0
   562
      goto out;
sl@0
   563
    }
sl@0
   564
  else
sl@0
   565
    {
sl@0
   566
    reget_service_info_data:
sl@0
   567
      service_name = NULL;
sl@0
   568
      old_owner = NULL;
sl@0
   569
      new_owner = NULL;
sl@0
   570
sl@0
   571
      dbus_message_get_args (message, &error,
sl@0
   572
                             DBUS_TYPE_STRING, &service_name,
sl@0
   573
                             DBUS_TYPE_STRING, &old_owner,
sl@0
   574
                             DBUS_TYPE_STRING, &new_owner,
sl@0
   575
                             DBUS_TYPE_INVALID);
sl@0
   576
sl@0
   577
      if (dbus_error_is_set (&error))
sl@0
   578
        {
sl@0
   579
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
   580
            {
sl@0
   581
              dbus_error_free (&error);
sl@0
   582
              _dbus_wait_for_memory ();              
sl@0
   583
              goto reget_service_info_data;
sl@0
   584
            }
sl@0
   585
          else
sl@0
   586
            {
sl@0
   587
              _dbus_warn ("Did not get the expected arguments\n");
sl@0
   588
              goto out;
sl@0
   589
            }
sl@0
   590
        }
sl@0
   591
sl@0
   592
      if ((d->expected_kind == SERVICE_CREATED    && ( old_owner[0] || !new_owner[0]))
sl@0
   593
          || (d->expected_kind == OWNER_CHANGED   && (!old_owner[0] || !new_owner[0]))
sl@0
   594
          || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] ||  new_owner[0])))
sl@0
   595
        {
sl@0
   596
          _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
sl@0
   597
          goto out;
sl@0
   598
        }
sl@0
   599
sl@0
   600
      if (strcmp (service_name, d->expected_service_name) != 0)
sl@0
   601
        {
sl@0
   602
          _dbus_warn ("expected info on service %s, got info on %s\n",
sl@0
   603
                      d->expected_service_name,
sl@0
   604
                      service_name);
sl@0
   605
          goto out;
sl@0
   606
        }
sl@0
   607
sl@0
   608
      if (*service_name == ':' && new_owner[0] 
sl@0
   609
          && strcmp (service_name, new_owner) != 0)
sl@0
   610
        {
sl@0
   611
          _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
sl@0
   612
                      service_name, old_owner, new_owner);
sl@0
   613
          goto out;
sl@0
   614
        }
sl@0
   615
    }
sl@0
   616
sl@0
   617
  d->failed = FALSE;
sl@0
   618
  
sl@0
   619
 out:
sl@0
   620
  dbus_error_free (&error);
sl@0
   621
  
sl@0
   622
  if (message)
sl@0
   623
    dbus_message_unref (message);
sl@0
   624
sl@0
   625
  return !d->failed;
sl@0
   626
}
sl@0
   627
sl@0
   628
sl@0
   629
static void
sl@0
   630
kill_client_connection (BusContext     *context,
sl@0
   631
                        DBusConnection *connection)
sl@0
   632
{
sl@0
   633
  char *base_service;
sl@0
   634
  const char *s;
sl@0
   635
  CheckServiceOwnerChangedData socd;
sl@0
   636
sl@0
   637
  _dbus_verbose ("killing connection %p\n", connection);
sl@0
   638
  
sl@0
   639
  s = dbus_bus_get_unique_name (connection);
sl@0
   640
  _dbus_assert (s != NULL);
sl@0
   641
sl@0
   642
  while ((base_service = _dbus_strdup (s)) == NULL)
sl@0
   643
    _dbus_wait_for_memory ();
sl@0
   644
sl@0
   645
  dbus_connection_ref (connection);
sl@0
   646
  
sl@0
   647
  /* kick in the disconnect handler that unrefs the connection */
sl@0
   648
  dbus_connection_close (connection);
sl@0
   649
sl@0
   650
  bus_test_run_everything (context);
sl@0
   651
  
sl@0
   652
  _dbus_assert (bus_test_client_listed (connection));
sl@0
   653
  
sl@0
   654
  /* Run disconnect handler in test.c */
sl@0
   655
  if (bus_connection_dispatch_one_message (connection))
sl@0
   656
    _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
sl@0
   657
  
sl@0
   658
  _dbus_assert (!dbus_connection_get_is_connected (connection));
sl@0
   659
  dbus_connection_unref (connection);
sl@0
   660
  connection = NULL;
sl@0
   661
  _dbus_assert (!bus_test_client_listed (connection));
sl@0
   662
  
sl@0
   663
  socd.expected_kind = SERVICE_DELETED;
sl@0
   664
  socd.expected_service_name = base_service;
sl@0
   665
  socd.failed = FALSE;
sl@0
   666
  socd.skip_connection = NULL;
sl@0
   667
  
sl@0
   668
  bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
   669
                            &socd);
sl@0
   670
sl@0
   671
  dbus_free (base_service);
sl@0
   672
  
sl@0
   673
  if (socd.failed)
sl@0
   674
    _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
sl@0
   675
  
sl@0
   676
  if (!check_no_leftovers (context))
sl@0
   677
    _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
sl@0
   678
}
sl@0
   679
sl@0
   680
static void
sl@0
   681
kill_client_connection_unchecked (DBusConnection *connection)
sl@0
   682
{
sl@0
   683
  /* This kills the connection without expecting it to affect
sl@0
   684
   * the rest of the bus.
sl@0
   685
   */  
sl@0
   686
  _dbus_verbose ("Unchecked kill of connection %p\n", connection);
sl@0
   687
sl@0
   688
  dbus_connection_ref (connection);
sl@0
   689
  dbus_connection_close (connection);
sl@0
   690
  /* dispatching disconnect handler will unref once */
sl@0
   691
  if (bus_connection_dispatch_one_message (connection))
sl@0
   692
    _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
sl@0
   693
sl@0
   694
  _dbus_assert (!bus_test_client_listed (connection));
sl@0
   695
  dbus_connection_unref (connection);
sl@0
   696
}
sl@0
   697
sl@0
   698
typedef struct
sl@0
   699
{
sl@0
   700
  dbus_bool_t failed;
sl@0
   701
} CheckNoMessagesData;
sl@0
   702
sl@0
   703
static dbus_bool_t
sl@0
   704
check_no_messages_foreach (DBusConnection *connection,
sl@0
   705
                           void           *data)
sl@0
   706
{
sl@0
   707
  CheckNoMessagesData *d = data;
sl@0
   708
  DBusMessage *message;
sl@0
   709
sl@0
   710
  message = pop_message_waiting_for_memory (connection);
sl@0
   711
  if (message != NULL)
sl@0
   712
    {
sl@0
   713
      warn_unexpected (connection, message, "no messages");
sl@0
   714
sl@0
   715
      d->failed = TRUE;
sl@0
   716
    }
sl@0
   717
sl@0
   718
  if (message)
sl@0
   719
    dbus_message_unref (message);
sl@0
   720
  return !d->failed;
sl@0
   721
}
sl@0
   722
sl@0
   723
static dbus_bool_t
sl@0
   724
check_no_leftovers (BusContext *context)
sl@0
   725
{
sl@0
   726
  CheckNoMessagesData nmd;
sl@0
   727
sl@0
   728
  nmd.failed = FALSE;
sl@0
   729
  bus_test_clients_foreach (check_no_messages_foreach,
sl@0
   730
                            &nmd);
sl@0
   731
  
sl@0
   732
  if (nmd.failed)
sl@0
   733
    {
sl@0
   734
      _dbus_verbose ("%s: leftover message found\n",
sl@0
   735
                     _DBUS_FUNCTION_NAME);
sl@0
   736
      return FALSE;
sl@0
   737
    }
sl@0
   738
  else
sl@0
   739
    return TRUE;
sl@0
   740
}
sl@0
   741
sl@0
   742
/* returns TRUE if the correct thing happens,
sl@0
   743
 * but the correct thing may include OOM errors.
sl@0
   744
 */
sl@0
   745
static dbus_bool_t
sl@0
   746
check_hello_message (BusContext     *context,
sl@0
   747
                     DBusConnection *connection)
sl@0
   748
{
sl@0
   749
  DBusMessage *message;
sl@0
   750
  DBusMessage *name_message;
sl@0
   751
  dbus_uint32_t serial;
sl@0
   752
  dbus_bool_t retval;
sl@0
   753
  DBusError error;
sl@0
   754
  const char *name;
sl@0
   755
  const char *acquired;
sl@0
   756
sl@0
   757
  retval = FALSE;
sl@0
   758
  dbus_error_init (&error);
sl@0
   759
  name = NULL;
sl@0
   760
  acquired = NULL;
sl@0
   761
  message = NULL;
sl@0
   762
  name_message = NULL;
sl@0
   763
sl@0
   764
  _dbus_verbose ("check_hello_message for %p\n", connection);
sl@0
   765
  
sl@0
   766
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
   767
                                          DBUS_PATH_DBUS,
sl@0
   768
                                          DBUS_INTERFACE_DBUS,
sl@0
   769
                                          "Hello");
sl@0
   770
sl@0
   771
  if (message == NULL)
sl@0
   772
    return TRUE;
sl@0
   773
sl@0
   774
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
   775
  
sl@0
   776
  if (!dbus_connection_send (connection, message, &serial))
sl@0
   777
    {
sl@0
   778
      dbus_message_unref (message);
sl@0
   779
      dbus_connection_unref (connection);
sl@0
   780
      return TRUE;
sl@0
   781
    }
sl@0
   782
sl@0
   783
  _dbus_assert (dbus_message_has_signature (message, ""));
sl@0
   784
  
sl@0
   785
  dbus_message_unref (message);
sl@0
   786
  message = NULL;
sl@0
   787
sl@0
   788
  if (!dbus_connection_get_is_connected (connection))
sl@0
   789
    {
sl@0
   790
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
sl@0
   791
      
sl@0
   792
      dbus_connection_unref (connection);
sl@0
   793
      
sl@0
   794
      return TRUE;
sl@0
   795
    }
sl@0
   796
  
sl@0
   797
  /* send our message */
sl@0
   798
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
   799
sl@0
   800
  if (!dbus_connection_get_is_connected (connection))
sl@0
   801
    {
sl@0
   802
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
sl@0
   803
      
sl@0
   804
      dbus_connection_unref (connection);
sl@0
   805
      
sl@0
   806
      return TRUE;
sl@0
   807
    }
sl@0
   808
  
sl@0
   809
  block_connection_until_message_from_bus (context, connection, "reply to Hello");
sl@0
   810
sl@0
   811
  if (!dbus_connection_get_is_connected (connection))
sl@0
   812
    {
sl@0
   813
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
sl@0
   814
      
sl@0
   815
      dbus_connection_unref (connection);
sl@0
   816
      
sl@0
   817
      return TRUE;
sl@0
   818
    }
sl@0
   819
sl@0
   820
  dbus_connection_unref (connection);
sl@0
   821
  
sl@0
   822
  message = pop_message_waiting_for_memory (connection);
sl@0
   823
  if (message == NULL)
sl@0
   824
    {
sl@0
   825
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
   826
                  "Hello", serial, connection);
sl@0
   827
      goto out;
sl@0
   828
    }
sl@0
   829
sl@0
   830
  verbose_message_received (connection, message);
sl@0
   831
sl@0
   832
  if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
   833
    {
sl@0
   834
      _dbus_warn ("Message has wrong sender %s\n",
sl@0
   835
                  dbus_message_get_sender (message) ?
sl@0
   836
                  dbus_message_get_sender (message) : "(none)");
sl@0
   837
      goto out;
sl@0
   838
    }
sl@0
   839
  
sl@0
   840
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
   841
    {
sl@0
   842
      if (dbus_message_is_error (message,
sl@0
   843
                                 DBUS_ERROR_NO_MEMORY))
sl@0
   844
        {
sl@0
   845
          ; /* good, this is a valid response */
sl@0
   846
        }
sl@0
   847
      else
sl@0
   848
        {
sl@0
   849
          warn_unexpected (connection, message, "not this error");
sl@0
   850
sl@0
   851
          goto out;
sl@0
   852
        }
sl@0
   853
    }
sl@0
   854
  else
sl@0
   855
    {
sl@0
   856
      CheckServiceOwnerChangedData socd;
sl@0
   857
      
sl@0
   858
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
   859
        {
sl@0
   860
          ; /* good, expected */
sl@0
   861
        }
sl@0
   862
      else
sl@0
   863
        {
sl@0
   864
          warn_unexpected (connection, message, "method return for Hello");
sl@0
   865
sl@0
   866
          goto out;
sl@0
   867
        }
sl@0
   868
sl@0
   869
    retry_get_hello_name:
sl@0
   870
      if (!dbus_message_get_args (message, &error,
sl@0
   871
                                  DBUS_TYPE_STRING, &name,
sl@0
   872
                                  DBUS_TYPE_INVALID))
sl@0
   873
        {
sl@0
   874
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
   875
            {
sl@0
   876
              _dbus_verbose ("no memory to get service name arg from hello\n");
sl@0
   877
              dbus_error_free (&error);
sl@0
   878
              _dbus_wait_for_memory ();
sl@0
   879
              goto retry_get_hello_name;
sl@0
   880
            }
sl@0
   881
          else
sl@0
   882
            {
sl@0
   883
              _dbus_assert (dbus_error_is_set (&error));
sl@0
   884
              _dbus_warn ("Did not get the expected single string argument to hello\n");
sl@0
   885
              goto out;
sl@0
   886
            }
sl@0
   887
        }
sl@0
   888
sl@0
   889
      _dbus_verbose ("Got hello name: %s\n", name);
sl@0
   890
sl@0
   891
      while (!dbus_bus_set_unique_name (connection, name))
sl@0
   892
        _dbus_wait_for_memory ();
sl@0
   893
      
sl@0
   894
      socd.expected_kind = SERVICE_CREATED;
sl@0
   895
      socd.expected_service_name = name;
sl@0
   896
      socd.failed = FALSE;
sl@0
   897
      socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
sl@0
   898
      bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
   899
                                &socd);
sl@0
   900
      
sl@0
   901
      if (socd.failed)
sl@0
   902
        goto out;
sl@0
   903
sl@0
   904
      name_message = message;
sl@0
   905
      /* Client should also have gotten ServiceAcquired */
sl@0
   906
sl@0
   907
      message = pop_message_waiting_for_memory (connection);
sl@0
   908
      if (message == NULL)
sl@0
   909
        {
sl@0
   910
          _dbus_warn ("Expecting %s, got nothing\n",
sl@0
   911
                      "NameAcquired");
sl@0
   912
          goto out;
sl@0
   913
        }
sl@0
   914
      if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
sl@0
   915
                                    "NameAcquired"))
sl@0
   916
        {
sl@0
   917
          _dbus_warn ("Expecting %s, got smthg else\n",
sl@0
   918
                      "NameAcquired");
sl@0
   919
          goto out;
sl@0
   920
        }
sl@0
   921
      
sl@0
   922
    retry_get_acquired_name:
sl@0
   923
      if (!dbus_message_get_args (message, &error,
sl@0
   924
                                  DBUS_TYPE_STRING, &acquired,
sl@0
   925
                                  DBUS_TYPE_INVALID))
sl@0
   926
        {
sl@0
   927
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
   928
            {
sl@0
   929
              _dbus_verbose ("no memory to get service name arg from acquired\n");
sl@0
   930
              dbus_error_free (&error);
sl@0
   931
              _dbus_wait_for_memory ();
sl@0
   932
              goto retry_get_acquired_name;
sl@0
   933
            }
sl@0
   934
          else
sl@0
   935
            {
sl@0
   936
              _dbus_assert (dbus_error_is_set (&error));
sl@0
   937
              _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
sl@0
   938
              goto out;
sl@0
   939
            }
sl@0
   940
        }
sl@0
   941
sl@0
   942
      _dbus_verbose ("Got acquired name: %s\n", acquired);
sl@0
   943
sl@0
   944
      if (strcmp (acquired, name) != 0)
sl@0
   945
        {
sl@0
   946
          _dbus_warn ("Acquired name is %s but expected %s\n",
sl@0
   947
                      acquired, name);
sl@0
   948
          goto out;
sl@0
   949
        }
sl@0
   950
      acquired = NULL;
sl@0
   951
    }
sl@0
   952
sl@0
   953
  if (!check_no_leftovers (context))
sl@0
   954
    goto out;
sl@0
   955
  
sl@0
   956
  retval = TRUE;
sl@0
   957
  
sl@0
   958
 out:
sl@0
   959
  _dbus_verbose ("ending %s retval = %d\n", _DBUS_FUNCTION_NAME, retval);
sl@0
   960
  
sl@0
   961
  dbus_error_free (&error);
sl@0
   962
  
sl@0
   963
  if (message)
sl@0
   964
    dbus_message_unref (message);
sl@0
   965
sl@0
   966
  if (name_message)
sl@0
   967
    dbus_message_unref (name_message);
sl@0
   968
  
sl@0
   969
  return retval;
sl@0
   970
}
sl@0
   971
sl@0
   972
/* returns TRUE if the correct thing happens,
sl@0
   973
 * but the correct thing may include OOM errors.
sl@0
   974
 */
sl@0
   975
static dbus_bool_t
sl@0
   976
check_double_hello_message (BusContext     *context,
sl@0
   977
                            DBusConnection *connection)
sl@0
   978
{
sl@0
   979
  DBusMessage *message;
sl@0
   980
  dbus_uint32_t serial;
sl@0
   981
  dbus_bool_t retval;
sl@0
   982
  DBusError error;
sl@0
   983
sl@0
   984
  retval = FALSE;
sl@0
   985
  dbus_error_init (&error);
sl@0
   986
  message = NULL;
sl@0
   987
sl@0
   988
  _dbus_verbose ("check_double_hello_message for %p\n", connection);
sl@0
   989
  
sl@0
   990
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
   991
                                          DBUS_PATH_DBUS,
sl@0
   992
                                          DBUS_INTERFACE_DBUS,
sl@0
   993
                                          "Hello");
sl@0
   994
sl@0
   995
  if (message == NULL)
sl@0
   996
    return TRUE;
sl@0
   997
  
sl@0
   998
  if (!dbus_connection_send (connection, message, &serial))
sl@0
   999
    {
sl@0
  1000
      dbus_message_unref (message);
sl@0
  1001
      return TRUE;
sl@0
  1002
    }
sl@0
  1003
sl@0
  1004
  dbus_message_unref (message);
sl@0
  1005
  message = NULL;
sl@0
  1006
sl@0
  1007
  /* send our message */
sl@0
  1008
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  1009
sl@0
  1010
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
  1011
  block_connection_until_message_from_bus (context, connection, "reply to Hello");
sl@0
  1012
sl@0
  1013
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1014
    {
sl@0
  1015
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1016
      
sl@0
  1017
      dbus_connection_unref (connection);
sl@0
  1018
      
sl@0
  1019
      return TRUE;
sl@0
  1020
    }
sl@0
  1021
sl@0
  1022
  dbus_connection_unref (connection);
sl@0
  1023
  
sl@0
  1024
  message = pop_message_waiting_for_memory (connection);
sl@0
  1025
  if (message == NULL)
sl@0
  1026
    {
sl@0
  1027
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1028
                  "Hello", serial, connection);
sl@0
  1029
      goto out;
sl@0
  1030
    }
sl@0
  1031
sl@0
  1032
  verbose_message_received (connection, message);
sl@0
  1033
sl@0
  1034
  if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  1035
    {
sl@0
  1036
      _dbus_warn ("Message has wrong sender %s\n",
sl@0
  1037
                  dbus_message_get_sender (message) ?
sl@0
  1038
                  dbus_message_get_sender (message) : "(none)");
sl@0
  1039
      goto out;
sl@0
  1040
    }
sl@0
  1041
  
sl@0
  1042
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1043
    {
sl@0
  1044
      warn_unexpected (connection, message, "method return for Hello");
sl@0
  1045
      goto out;
sl@0
  1046
    }
sl@0
  1047
sl@0
  1048
  if (!check_no_leftovers (context))
sl@0
  1049
    goto out;
sl@0
  1050
  
sl@0
  1051
  retval = TRUE;
sl@0
  1052
  
sl@0
  1053
 out:
sl@0
  1054
  dbus_error_free (&error);
sl@0
  1055
  
sl@0
  1056
  if (message)
sl@0
  1057
    dbus_message_unref (message);
sl@0
  1058
  
sl@0
  1059
  return retval;
sl@0
  1060
}
sl@0
  1061
sl@0
  1062
/* returns TRUE if the correct thing happens,
sl@0
  1063
 * but the correct thing may include OOM errors.
sl@0
  1064
 */
sl@0
  1065
static dbus_bool_t
sl@0
  1066
check_get_connection_unix_user (BusContext     *context,
sl@0
  1067
                                DBusConnection *connection)
sl@0
  1068
{
sl@0
  1069
  DBusMessage *message;
sl@0
  1070
  dbus_uint32_t serial;
sl@0
  1071
  dbus_bool_t retval;
sl@0
  1072
  DBusError error;
sl@0
  1073
  const char *base_service_name;
sl@0
  1074
  dbus_uint32_t uid;
sl@0
  1075
sl@0
  1076
  retval = FALSE;
sl@0
  1077
  dbus_error_init (&error);
sl@0
  1078
  message = NULL;
sl@0
  1079
sl@0
  1080
  _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
sl@0
  1081
  
sl@0
  1082
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  1083
                                          DBUS_PATH_DBUS,
sl@0
  1084
                                          DBUS_INTERFACE_DBUS,
sl@0
  1085
                                          "GetConnectionUnixUser");
sl@0
  1086
sl@0
  1087
  if (message == NULL)
sl@0
  1088
    return TRUE;
sl@0
  1089
sl@0
  1090
  base_service_name = dbus_bus_get_unique_name (connection);
sl@0
  1091
sl@0
  1092
  if (!dbus_message_append_args (message, 
sl@0
  1093
                                 DBUS_TYPE_STRING, &base_service_name,
sl@0
  1094
                                 DBUS_TYPE_INVALID))
sl@0
  1095
    {
sl@0
  1096
      dbus_message_unref (message);
sl@0
  1097
      return TRUE;
sl@0
  1098
    }
sl@0
  1099
sl@0
  1100
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  1101
    {
sl@0
  1102
      dbus_message_unref (message);
sl@0
  1103
      return TRUE;
sl@0
  1104
    }
sl@0
  1105
sl@0
  1106
  /* send our message */
sl@0
  1107
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  1108
sl@0
  1109
  dbus_message_unref (message);
sl@0
  1110
  message = NULL;
sl@0
  1111
sl@0
  1112
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
  1113
  block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
sl@0
  1114
sl@0
  1115
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1116
    {
sl@0
  1117
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1118
      
sl@0
  1119
      dbus_connection_unref (connection);
sl@0
  1120
      
sl@0
  1121
      return TRUE;
sl@0
  1122
    }
sl@0
  1123
sl@0
  1124
  dbus_connection_unref (connection);
sl@0
  1125
sl@0
  1126
  message = pop_message_waiting_for_memory (connection);
sl@0
  1127
  if (message == NULL)
sl@0
  1128
    {
sl@0
  1129
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1130
                  "GetConnectionUnixUser", serial, connection);
sl@0
  1131
      goto out;
sl@0
  1132
    }
sl@0
  1133
sl@0
  1134
  verbose_message_received (connection, message);
sl@0
  1135
sl@0
  1136
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1137
    {
sl@0
  1138
      if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
sl@0
  1139
        {
sl@0
  1140
          ; /* good, this is a valid response */
sl@0
  1141
        }
sl@0
  1142
      else
sl@0
  1143
        {
sl@0
  1144
          warn_unexpected (connection, message, "not this error");
sl@0
  1145
sl@0
  1146
          goto out;
sl@0
  1147
        }
sl@0
  1148
    }
sl@0
  1149
  else
sl@0
  1150
    {
sl@0
  1151
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  1152
        {
sl@0
  1153
          ; /* good, expected */
sl@0
  1154
        }
sl@0
  1155
      else
sl@0
  1156
        {
sl@0
  1157
          warn_unexpected (connection, message,
sl@0
  1158
                           "method_return for GetConnectionUnixUser");
sl@0
  1159
sl@0
  1160
          goto out;
sl@0
  1161
        }
sl@0
  1162
sl@0
  1163
    retry_get_property:
sl@0
  1164
sl@0
  1165
      if (!dbus_message_get_args (message, &error,
sl@0
  1166
                                  DBUS_TYPE_UINT32, &uid,
sl@0
  1167
                                  DBUS_TYPE_INVALID))
sl@0
  1168
        {
sl@0
  1169
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  1170
            {
sl@0
  1171
              _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
sl@0
  1172
              dbus_error_free (&error);
sl@0
  1173
              _dbus_wait_for_memory ();
sl@0
  1174
              goto retry_get_property;
sl@0
  1175
            }
sl@0
  1176
          else
sl@0
  1177
            {
sl@0
  1178
              _dbus_assert (dbus_error_is_set (&error));
sl@0
  1179
              _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
sl@0
  1180
              goto out;
sl@0
  1181
            }
sl@0
  1182
        }
sl@0
  1183
    }
sl@0
  1184
sl@0
  1185
  if (!check_no_leftovers (context))
sl@0
  1186
    goto out;
sl@0
  1187
sl@0
  1188
  retval = TRUE;
sl@0
  1189
sl@0
  1190
 out:
sl@0
  1191
  dbus_error_free (&error);
sl@0
  1192
  
sl@0
  1193
  if (message)
sl@0
  1194
    dbus_message_unref (message);
sl@0
  1195
  
sl@0
  1196
  return retval;
sl@0
  1197
}
sl@0
  1198
sl@0
  1199
/* returns TRUE if the correct thing happens,
sl@0
  1200
 * but the correct thing may include OOM errors.
sl@0
  1201
 */
sl@0
  1202
static dbus_bool_t
sl@0
  1203
check_get_connection_unix_process_id (BusContext     *context,
sl@0
  1204
                                      DBusConnection *connection)
sl@0
  1205
{
sl@0
  1206
  DBusMessage *message;
sl@0
  1207
  dbus_uint32_t serial;
sl@0
  1208
  dbus_bool_t retval;
sl@0
  1209
  DBusError error;
sl@0
  1210
  const char *base_service_name;
sl@0
  1211
  dbus_uint32_t pid;
sl@0
  1212
sl@0
  1213
  retval = FALSE;
sl@0
  1214
  dbus_error_init (&error);
sl@0
  1215
  message = NULL;
sl@0
  1216
sl@0
  1217
  _dbus_verbose ("check_get_connection_unix_process_id for %p\n", connection);
sl@0
  1218
  
sl@0
  1219
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  1220
                                          DBUS_PATH_DBUS,
sl@0
  1221
                                          DBUS_INTERFACE_DBUS,
sl@0
  1222
                                          "GetConnectionUnixProcessID");
sl@0
  1223
sl@0
  1224
  if (message == NULL)
sl@0
  1225
    return TRUE;
sl@0
  1226
sl@0
  1227
  base_service_name = dbus_bus_get_unique_name (connection);
sl@0
  1228
sl@0
  1229
  if (!dbus_message_append_args (message, 
sl@0
  1230
                                 DBUS_TYPE_STRING, &base_service_name,
sl@0
  1231
                                 DBUS_TYPE_INVALID))
sl@0
  1232
    {
sl@0
  1233
      dbus_message_unref (message);
sl@0
  1234
      return TRUE;
sl@0
  1235
    }
sl@0
  1236
sl@0
  1237
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  1238
    {
sl@0
  1239
      dbus_message_unref (message);
sl@0
  1240
      return TRUE;
sl@0
  1241
    }
sl@0
  1242
sl@0
  1243
  /* send our message */
sl@0
  1244
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  1245
sl@0
  1246
  dbus_message_unref (message);
sl@0
  1247
  message = NULL;
sl@0
  1248
sl@0
  1249
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
  1250
  block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixProcessID");
sl@0
  1251
sl@0
  1252
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1253
    {
sl@0
  1254
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1255
      
sl@0
  1256
      dbus_connection_unref (connection);
sl@0
  1257
      
sl@0
  1258
      return TRUE;
sl@0
  1259
    }
sl@0
  1260
sl@0
  1261
  dbus_connection_unref (connection);
sl@0
  1262
sl@0
  1263
  message = pop_message_waiting_for_memory (connection);
sl@0
  1264
  if (message == NULL)
sl@0
  1265
    {
sl@0
  1266
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1267
                  "GetConnectionUnixProcessID", serial, connection);
sl@0
  1268
      goto out;
sl@0
  1269
    }
sl@0
  1270
sl@0
  1271
  verbose_message_received (connection, message);
sl@0
  1272
sl@0
  1273
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1274
    {
sl@0
  1275
      if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
sl@0
  1276
        {
sl@0
  1277
          ; /* good, this is a valid response */
sl@0
  1278
        }
sl@0
  1279
      else
sl@0
  1280
        {
sl@0
  1281
          warn_unexpected (connection, message, "not this error");
sl@0
  1282
sl@0
  1283
          goto out;
sl@0
  1284
        }
sl@0
  1285
    }
sl@0
  1286
  else
sl@0
  1287
    {
sl@0
  1288
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  1289
        {
sl@0
  1290
          ; /* good, expected */
sl@0
  1291
        }
sl@0
  1292
      else
sl@0
  1293
        {
sl@0
  1294
          warn_unexpected (connection, message,
sl@0
  1295
                           "method_return for GetConnectionUnixProcessID");
sl@0
  1296
sl@0
  1297
          goto out;
sl@0
  1298
        }
sl@0
  1299
sl@0
  1300
    retry_get_property:
sl@0
  1301
sl@0
  1302
      if (!dbus_message_get_args (message, &error,
sl@0
  1303
                                  DBUS_TYPE_UINT32, &pid,
sl@0
  1304
                                  DBUS_TYPE_INVALID))
sl@0
  1305
        {
sl@0
  1306
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  1307
            {
sl@0
  1308
              _dbus_verbose ("no memory to get pid by GetConnectionUnixProcessID\n");
sl@0
  1309
              dbus_error_free (&error);
sl@0
  1310
              _dbus_wait_for_memory ();
sl@0
  1311
              goto retry_get_property;
sl@0
  1312
            }
sl@0
  1313
          else
sl@0
  1314
            {
sl@0
  1315
              _dbus_assert (dbus_error_is_set (&error));
sl@0
  1316
              _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixProcessID\n");
sl@0
  1317
              goto out;
sl@0
  1318
            }
sl@0
  1319
        } else {
sl@0
  1320
sl@0
  1321
          /* test if returned pid is the same as our own pid
sl@0
  1322
           *
sl@0
  1323
           * @todo It would probably be good to restructure the tests
sl@0
  1324
           *       in a way so our parent is the bus that we're testing
sl@0
  1325
           *       cause then we can test that the pid returned matches
sl@0
  1326
           *       getppid()
sl@0
  1327
           */
sl@0
  1328
          if (pid != (dbus_uint32_t) _dbus_getpid ())
sl@0
  1329
            {
sl@0
  1330
              _dbus_assert (dbus_error_is_set (&error));
sl@0
  1331
              _dbus_warn ("Result from GetConnectionUnixProcessID is not our own pid\n");
sl@0
  1332
              goto out;
sl@0
  1333
            }
sl@0
  1334
        }
sl@0
  1335
    }
sl@0
  1336
sl@0
  1337
  if (!check_no_leftovers (context))
sl@0
  1338
    goto out;
sl@0
  1339
sl@0
  1340
  retval = TRUE;
sl@0
  1341
sl@0
  1342
 out:
sl@0
  1343
  dbus_error_free (&error);
sl@0
  1344
  
sl@0
  1345
  if (message)
sl@0
  1346
    dbus_message_unref (message);
sl@0
  1347
  
sl@0
  1348
  return retval;
sl@0
  1349
}
sl@0
  1350
sl@0
  1351
/* returns TRUE if the correct thing happens,
sl@0
  1352
 * but the correct thing may include OOM errors.
sl@0
  1353
 */
sl@0
  1354
static dbus_bool_t
sl@0
  1355
check_add_match_all (BusContext     *context,
sl@0
  1356
                     DBusConnection *connection)
sl@0
  1357
{
sl@0
  1358
  DBusMessage *message;
sl@0
  1359
  dbus_bool_t retval;
sl@0
  1360
  dbus_uint32_t serial;
sl@0
  1361
  DBusError error;
sl@0
  1362
  const char *empty = "";
sl@0
  1363
sl@0
  1364
  retval = FALSE;
sl@0
  1365
  dbus_error_init (&error);
sl@0
  1366
  message = NULL;
sl@0
  1367
sl@0
  1368
  _dbus_verbose ("check_add_match_all for %p\n", connection);
sl@0
  1369
  
sl@0
  1370
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  1371
                                          DBUS_PATH_DBUS,
sl@0
  1372
                                          DBUS_INTERFACE_DBUS,
sl@0
  1373
                                          "AddMatch");
sl@0
  1374
sl@0
  1375
  if (message == NULL)
sl@0
  1376
    return TRUE;
sl@0
  1377
sl@0
  1378
  /* empty string match rule matches everything */
sl@0
  1379
  if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &empty,
sl@0
  1380
                                 DBUS_TYPE_INVALID))
sl@0
  1381
    {
sl@0
  1382
      dbus_message_unref (message);
sl@0
  1383
      return TRUE;
sl@0
  1384
    }
sl@0
  1385
  
sl@0
  1386
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  1387
    {
sl@0
  1388
      dbus_message_unref (message);
sl@0
  1389
      return TRUE;
sl@0
  1390
    }
sl@0
  1391
sl@0
  1392
  dbus_message_unref (message);
sl@0
  1393
  message = NULL;
sl@0
  1394
sl@0
  1395
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
  1396
  
sl@0
  1397
  /* send our message */
sl@0
  1398
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  1399
sl@0
  1400
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1401
    {
sl@0
  1402
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1403
      
sl@0
  1404
      dbus_connection_unref (connection);
sl@0
  1405
      
sl@0
  1406
      return TRUE;
sl@0
  1407
    }
sl@0
  1408
  
sl@0
  1409
  block_connection_until_message_from_bus (context, connection, "reply to AddMatch");
sl@0
  1410
sl@0
  1411
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1412
    {
sl@0
  1413
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1414
      
sl@0
  1415
      dbus_connection_unref (connection);
sl@0
  1416
      
sl@0
  1417
      return TRUE;
sl@0
  1418
    }
sl@0
  1419
sl@0
  1420
  dbus_connection_unref (connection);
sl@0
  1421
  
sl@0
  1422
  message = pop_message_waiting_for_memory (connection);
sl@0
  1423
  if (message == NULL)
sl@0
  1424
    {
sl@0
  1425
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1426
                  "AddMatch", serial, connection);
sl@0
  1427
      goto out;
sl@0
  1428
    }
sl@0
  1429
sl@0
  1430
  verbose_message_received (connection, message);
sl@0
  1431
sl@0
  1432
  if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  1433
    {
sl@0
  1434
      _dbus_warn ("Message has wrong sender %s\n",
sl@0
  1435
                  dbus_message_get_sender (message) ?
sl@0
  1436
                  dbus_message_get_sender (message) : "(none)");
sl@0
  1437
      goto out;
sl@0
  1438
    }
sl@0
  1439
  
sl@0
  1440
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1441
    {
sl@0
  1442
      if (dbus_message_is_error (message,
sl@0
  1443
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  1444
        {
sl@0
  1445
          ; /* good, this is a valid response */
sl@0
  1446
        }
sl@0
  1447
      else
sl@0
  1448
        {
sl@0
  1449
          warn_unexpected (connection, message, "not this error");
sl@0
  1450
sl@0
  1451
          goto out;
sl@0
  1452
        }
sl@0
  1453
    }
sl@0
  1454
  else
sl@0
  1455
    {
sl@0
  1456
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  1457
        {
sl@0
  1458
          ; /* good, expected */
sl@0
  1459
          _dbus_assert (dbus_message_get_reply_serial (message) == serial);
sl@0
  1460
        }
sl@0
  1461
      else
sl@0
  1462
        {
sl@0
  1463
          warn_unexpected (connection, message, "method return for AddMatch");
sl@0
  1464
sl@0
  1465
          goto out;
sl@0
  1466
        }
sl@0
  1467
    }
sl@0
  1468
sl@0
  1469
  if (!check_no_leftovers (context))
sl@0
  1470
    goto out;
sl@0
  1471
  
sl@0
  1472
  retval = TRUE;
sl@0
  1473
  
sl@0
  1474
 out:
sl@0
  1475
  dbus_error_free (&error);
sl@0
  1476
  
sl@0
  1477
  if (message)
sl@0
  1478
    dbus_message_unref (message);
sl@0
  1479
  
sl@0
  1480
  return retval;
sl@0
  1481
}
sl@0
  1482
sl@0
  1483
/* returns TRUE if the correct thing happens,
sl@0
  1484
 * but the correct thing may include OOM errors.
sl@0
  1485
 */
sl@0
  1486
static dbus_bool_t
sl@0
  1487
check_hello_connection (BusContext *context)
sl@0
  1488
{
sl@0
  1489
  DBusConnection *connection;
sl@0
  1490
  DBusError error;
sl@0
  1491
sl@0
  1492
  dbus_error_init (&error);
sl@0
  1493
sl@0
  1494
  connection = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
sl@0
  1495
  if (connection == NULL)
sl@0
  1496
    {
sl@0
  1497
      _DBUS_ASSERT_ERROR_IS_SET (&error);
sl@0
  1498
      dbus_error_free (&error);
sl@0
  1499
      return TRUE;
sl@0
  1500
    }
sl@0
  1501
sl@0
  1502
  if (!bus_setup_debug_client (connection))
sl@0
  1503
    {
sl@0
  1504
      dbus_connection_close (connection);
sl@0
  1505
      dbus_connection_unref (connection);
sl@0
  1506
      return TRUE;
sl@0
  1507
    }
sl@0
  1508
sl@0
  1509
  spin_connection_until_authenticated (context, connection);
sl@0
  1510
  
sl@0
  1511
  if (!check_hello_message (context, connection))
sl@0
  1512
    return FALSE;
sl@0
  1513
  
sl@0
  1514
  if (dbus_bus_get_unique_name (connection) == NULL)
sl@0
  1515
    {
sl@0
  1516
      /* We didn't successfully register, so we can't
sl@0
  1517
       * do the usual kill_client_connection() checks
sl@0
  1518
       */
sl@0
  1519
      kill_client_connection_unchecked (connection);
sl@0
  1520
    }
sl@0
  1521
  else
sl@0
  1522
    {
sl@0
  1523
      if (!check_add_match_all (context, connection))
sl@0
  1524
        return FALSE;
sl@0
  1525
      
sl@0
  1526
      kill_client_connection (context, connection);
sl@0
  1527
    }
sl@0
  1528
sl@0
  1529
  return TRUE;
sl@0
  1530
}
sl@0
  1531
sl@0
  1532
#define NONEXISTENT_SERVICE_NAME "test.this.service.does.not.exist.ewuoiurjdfxcvn"
sl@0
  1533
sl@0
  1534
/* returns TRUE if the correct thing happens,
sl@0
  1535
 * but the correct thing may include OOM errors.
sl@0
  1536
 */
sl@0
  1537
static dbus_bool_t
sl@0
  1538
check_nonexistent_service_no_auto_start (BusContext     *context,
sl@0
  1539
                                         DBusConnection *connection)
sl@0
  1540
{
sl@0
  1541
  DBusMessage *message;
sl@0
  1542
  dbus_uint32_t serial;
sl@0
  1543
  dbus_bool_t retval;
sl@0
  1544
  const char *nonexistent = NONEXISTENT_SERVICE_NAME;
sl@0
  1545
  dbus_uint32_t flags;
sl@0
  1546
  
sl@0
  1547
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  1548
                                          DBUS_PATH_DBUS,
sl@0
  1549
                                          DBUS_INTERFACE_DBUS,
sl@0
  1550
                                          "StartServiceByName");
sl@0
  1551
  
sl@0
  1552
  if (message == NULL)
sl@0
  1553
    return TRUE;
sl@0
  1554
sl@0
  1555
  dbus_message_set_auto_start (message, FALSE);
sl@0
  1556
  
sl@0
  1557
  flags = 0;
sl@0
  1558
  if (!dbus_message_append_args (message,
sl@0
  1559
                                 DBUS_TYPE_STRING, &nonexistent,
sl@0
  1560
                                 DBUS_TYPE_UINT32, &flags,
sl@0
  1561
                                 DBUS_TYPE_INVALID))
sl@0
  1562
    {
sl@0
  1563
      dbus_message_unref (message);
sl@0
  1564
      return TRUE;
sl@0
  1565
    }
sl@0
  1566
  
sl@0
  1567
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  1568
    {
sl@0
  1569
      dbus_message_unref (message);
sl@0
  1570
      return TRUE;
sl@0
  1571
    }
sl@0
  1572
sl@0
  1573
  dbus_message_unref (message);
sl@0
  1574
  message = NULL;
sl@0
  1575
sl@0
  1576
  bus_test_run_everything (context);
sl@0
  1577
  block_connection_until_message_from_bus (context, connection, "reply to ActivateService on nonexistent");
sl@0
  1578
  bus_test_run_everything (context);
sl@0
  1579
sl@0
  1580
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1581
    {
sl@0
  1582
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1583
      return TRUE;
sl@0
  1584
    }
sl@0
  1585
  
sl@0
  1586
  retval = FALSE;
sl@0
  1587
  
sl@0
  1588
  message = pop_message_waiting_for_memory (connection);
sl@0
  1589
  if (message == NULL)
sl@0
  1590
    {
sl@0
  1591
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1592
                  "StartServiceByName", serial, connection);
sl@0
  1593
      goto out;
sl@0
  1594
    }
sl@0
  1595
sl@0
  1596
  verbose_message_received (connection, message);
sl@0
  1597
sl@0
  1598
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1599
    {
sl@0
  1600
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  1601
        {
sl@0
  1602
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  1603
                      dbus_message_get_sender (message) ?
sl@0
  1604
                      dbus_message_get_sender (message) : "(none)");
sl@0
  1605
          goto out;
sl@0
  1606
        }
sl@0
  1607
      
sl@0
  1608
      if (dbus_message_is_error (message,
sl@0
  1609
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  1610
        {
sl@0
  1611
          ; /* good, this is a valid response */
sl@0
  1612
        }
sl@0
  1613
      else if (dbus_message_is_error (message,
sl@0
  1614
                                      DBUS_ERROR_SERVICE_UNKNOWN))
sl@0
  1615
        {
sl@0
  1616
          ; /* good, this is expected also */
sl@0
  1617
        }
sl@0
  1618
      else
sl@0
  1619
        {
sl@0
  1620
          warn_unexpected (connection, message, "not this error");
sl@0
  1621
          goto out;
sl@0
  1622
        }
sl@0
  1623
    }
sl@0
  1624
  else
sl@0
  1625
    {
sl@0
  1626
      _dbus_warn ("Did not expect to successfully activate %s\n",
sl@0
  1627
                  NONEXISTENT_SERVICE_NAME);
sl@0
  1628
      goto out;
sl@0
  1629
    }
sl@0
  1630
sl@0
  1631
  retval = TRUE;
sl@0
  1632
  
sl@0
  1633
 out:
sl@0
  1634
  if (message)
sl@0
  1635
    dbus_message_unref (message);
sl@0
  1636
  
sl@0
  1637
  return retval;
sl@0
  1638
}
sl@0
  1639
sl@0
  1640
/* returns TRUE if the correct thing happens,
sl@0
  1641
 * but the correct thing may include OOM errors.
sl@0
  1642
 */
sl@0
  1643
static dbus_bool_t
sl@0
  1644
check_nonexistent_service_auto_start (BusContext     *context,
sl@0
  1645
                                      DBusConnection *connection)
sl@0
  1646
{
sl@0
  1647
  DBusMessage *message;
sl@0
  1648
  dbus_uint32_t serial;
sl@0
  1649
  dbus_bool_t retval;
sl@0
  1650
    
sl@0
  1651
  message = dbus_message_new_method_call (NONEXISTENT_SERVICE_NAME,
sl@0
  1652
                                          "/org/freedesktop/TestSuite",
sl@0
  1653
                                          "org.freedesktop.TestSuite",
sl@0
  1654
                                          "Echo");
sl@0
  1655
  
sl@0
  1656
  if (message == NULL)
sl@0
  1657
    return TRUE;
sl@0
  1658
 
sl@0
  1659
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  1660
    {
sl@0
  1661
      dbus_message_unref (message);
sl@0
  1662
      return TRUE;
sl@0
  1663
    }
sl@0
  1664
sl@0
  1665
  dbus_message_unref (message);
sl@0
  1666
  message = NULL;
sl@0
  1667
sl@0
  1668
  bus_test_run_everything (context);
sl@0
  1669
  block_connection_until_message_from_bus (context, connection, "reply to Echo");
sl@0
  1670
  bus_test_run_everything (context);
sl@0
  1671
sl@0
  1672
  if (!dbus_connection_get_is_connected (connection))
sl@0
  1673
    {
sl@0
  1674
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  1675
      return TRUE;
sl@0
  1676
    }
sl@0
  1677
  
sl@0
  1678
  retval = FALSE;
sl@0
  1679
  
sl@0
  1680
  message = pop_message_waiting_for_memory (connection);
sl@0
  1681
sl@0
  1682
  if (message == NULL)
sl@0
  1683
    {
sl@0
  1684
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  1685
                  "Echo message (auto activation)", serial, connection);
sl@0
  1686
      goto out;
sl@0
  1687
    }
sl@0
  1688
sl@0
  1689
  verbose_message_received (connection, message);
sl@0
  1690
sl@0
  1691
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  1692
    {
sl@0
  1693
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  1694
        {
sl@0
  1695
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  1696
                      dbus_message_get_sender (message) ?
sl@0
  1697
                      dbus_message_get_sender (message) : "(none)");
sl@0
  1698
          goto out;
sl@0
  1699
        }
sl@0
  1700
      
sl@0
  1701
      if (dbus_message_is_error (message,
sl@0
  1702
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  1703
        {
sl@0
  1704
          ; /* good, this is a valid response */
sl@0
  1705
        }
sl@0
  1706
      else if (dbus_message_is_error (message,
sl@0
  1707
                                      DBUS_ERROR_SERVICE_UNKNOWN))
sl@0
  1708
        {
sl@0
  1709
          ; /* good, this is expected also */
sl@0
  1710
        }
sl@0
  1711
      else
sl@0
  1712
        {
sl@0
  1713
          warn_unexpected (connection, message, "not this error");
sl@0
  1714
          goto out;
sl@0
  1715
        }
sl@0
  1716
    }
sl@0
  1717
  else
sl@0
  1718
    {
sl@0
  1719
      _dbus_warn ("Did not expect to successfully activate %s\n",
sl@0
  1720
                  NONEXISTENT_SERVICE_NAME);
sl@0
  1721
      goto out;
sl@0
  1722
    }
sl@0
  1723
sl@0
  1724
  retval = TRUE;
sl@0
  1725
  
sl@0
  1726
 out:
sl@0
  1727
  if (message)
sl@0
  1728
    dbus_message_unref (message);
sl@0
  1729
  
sl@0
  1730
  return retval;
sl@0
  1731
}
sl@0
  1732
sl@0
  1733
static dbus_bool_t
sl@0
  1734
check_base_service_activated (BusContext     *context,
sl@0
  1735
                              DBusConnection *connection,
sl@0
  1736
                              DBusMessage    *initial_message,
sl@0
  1737
                              const char    **base_service_p)
sl@0
  1738
{
sl@0
  1739
  DBusMessage *message;
sl@0
  1740
  dbus_bool_t retval;
sl@0
  1741
  DBusError error;
sl@0
  1742
  const char *base_service, *base_service_from_bus, *old_owner;
sl@0
  1743
  
sl@0
  1744
  retval = FALSE;
sl@0
  1745
  
sl@0
  1746
  dbus_error_init (&error);
sl@0
  1747
  base_service = NULL;
sl@0
  1748
  old_owner = NULL;
sl@0
  1749
  base_service_from_bus = NULL;
sl@0
  1750
sl@0
  1751
  message = initial_message;
sl@0
  1752
  dbus_message_ref (message);  
sl@0
  1753
sl@0
  1754
  if (dbus_message_is_signal (message,
sl@0
  1755
                              DBUS_INTERFACE_DBUS,
sl@0
  1756
                              "NameOwnerChanged"))
sl@0
  1757
    {
sl@0
  1758
      CheckServiceOwnerChangedData socd;
sl@0
  1759
sl@0
  1760
    reget_service_name_arg:
sl@0
  1761
      base_service = NULL;
sl@0
  1762
      old_owner = NULL;
sl@0
  1763
      base_service_from_bus = NULL;
sl@0
  1764
sl@0
  1765
      if (!dbus_message_get_args (message, &error,
sl@0
  1766
                                  DBUS_TYPE_STRING, &base_service,
sl@0
  1767
                                  DBUS_TYPE_STRING, &old_owner,
sl@0
  1768
                                  DBUS_TYPE_STRING, &base_service_from_bus,
sl@0
  1769
                                  DBUS_TYPE_INVALID))
sl@0
  1770
        {
sl@0
  1771
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  1772
            {
sl@0
  1773
              dbus_error_free (&error);
sl@0
  1774
              _dbus_wait_for_memory ();
sl@0
  1775
              goto reget_service_name_arg;
sl@0
  1776
            }
sl@0
  1777
          else
sl@0
  1778
            {
sl@0
  1779
              _dbus_warn ("Message %s doesn't have a service name: %s\n",
sl@0
  1780
                          "NameOwnerChanged (creation)",
sl@0
  1781
                          error.message);
sl@0
  1782
              goto out;
sl@0
  1783
            }
sl@0
  1784
        }
sl@0
  1785
sl@0
  1786
      if (*base_service != ':')
sl@0
  1787
        {
sl@0
  1788
          _dbus_warn ("Expected base service activation, got \"%s\" instead\n",
sl@0
  1789
                      base_service);
sl@0
  1790
          goto out;
sl@0
  1791
        }
sl@0
  1792
         
sl@0
  1793
      if (strcmp (base_service, base_service_from_bus) != 0)
sl@0
  1794
        {
sl@0
  1795
          _dbus_warn ("Expected base service activation, got \"%s\" instead with owner \"%s\"\n",
sl@0
  1796
                      base_service, base_service_from_bus);
sl@0
  1797
          goto out;
sl@0
  1798
        }
sl@0
  1799
sl@0
  1800
      if (old_owner[0])
sl@0
  1801
        {
sl@0
  1802
          _dbus_warn ("Received an old_owner argument during base service activation, \"%s\"\n",
sl@0
  1803
                      old_owner);
sl@0
  1804
          goto out;
sl@0
  1805
        }
sl@0
  1806
     
sl@0
  1807
      socd.expected_kind = SERVICE_CREATED;
sl@0
  1808
      socd.expected_service_name = base_service;
sl@0
  1809
      socd.failed = FALSE;
sl@0
  1810
      socd.skip_connection = connection;
sl@0
  1811
      bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  1812
                                &socd);
sl@0
  1813
      
sl@0
  1814
      if (socd.failed)
sl@0
  1815
        goto out;
sl@0
  1816
    }
sl@0
  1817
  else
sl@0
  1818
    {
sl@0
  1819
      warn_unexpected (connection, message, "NameOwnerChanged (creation) for base service");
sl@0
  1820
sl@0
  1821
      goto out;
sl@0
  1822
    }
sl@0
  1823
sl@0
  1824
  if (base_service_p)
sl@0
  1825
    *base_service_p = base_service;
sl@0
  1826
sl@0
  1827
  retval = TRUE;
sl@0
  1828
  
sl@0
  1829
 out:
sl@0
  1830
  if (message)
sl@0
  1831
    dbus_message_unref (message);
sl@0
  1832
  dbus_error_free (&error);
sl@0
  1833
sl@0
  1834
  return retval;
sl@0
  1835
}
sl@0
  1836
sl@0
  1837
static dbus_bool_t
sl@0
  1838
check_service_activated (BusContext     *context,
sl@0
  1839
                         DBusConnection *connection,
sl@0
  1840
                         const char     *activated_name,
sl@0
  1841
                         const char     *base_service_name,
sl@0
  1842
                         DBusMessage    *initial_message)
sl@0
  1843
{
sl@0
  1844
  DBusMessage *message;
sl@0
  1845
  dbus_bool_t retval;
sl@0
  1846
  DBusError error;
sl@0
  1847
  dbus_uint32_t activation_result;
sl@0
  1848
  
sl@0
  1849
  retval = FALSE;
sl@0
  1850
  
sl@0
  1851
  dbus_error_init (&error);
sl@0
  1852
sl@0
  1853
  message = initial_message;
sl@0
  1854
  dbus_message_ref (message);
sl@0
  1855
sl@0
  1856
  if (dbus_message_is_signal (message,
sl@0
  1857
                              DBUS_INTERFACE_DBUS,
sl@0
  1858
                              "NameOwnerChanged"))
sl@0
  1859
    {
sl@0
  1860
      CheckServiceOwnerChangedData socd;
sl@0
  1861
      const char *service_name, *base_service_from_bus, *old_owner;
sl@0
  1862
sl@0
  1863
    reget_service_name_arg:
sl@0
  1864
      service_name = NULL;
sl@0
  1865
      old_owner = NULL;
sl@0
  1866
      base_service_from_bus = NULL;
sl@0
  1867
sl@0
  1868
      if (!dbus_message_get_args (message, &error,
sl@0
  1869
                                  DBUS_TYPE_STRING, &service_name,
sl@0
  1870
                                   DBUS_TYPE_STRING, &old_owner,
sl@0
  1871
                                  DBUS_TYPE_STRING, &base_service_from_bus,
sl@0
  1872
                                  DBUS_TYPE_INVALID))
sl@0
  1873
        {
sl@0
  1874
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  1875
            {
sl@0
  1876
              dbus_error_free (&error);
sl@0
  1877
              _dbus_wait_for_memory ();
sl@0
  1878
              goto reget_service_name_arg;
sl@0
  1879
            }
sl@0
  1880
          else
sl@0
  1881
            {
sl@0
  1882
              _dbus_warn ("Message %s doesn't have a service name: %s\n",
sl@0
  1883
                          "NameOwnerChanged (creation)",
sl@0
  1884
                          error.message);
sl@0
  1885
              goto out;
sl@0
  1886
            }
sl@0
  1887
        }
sl@0
  1888
sl@0
  1889
      if (strcmp (service_name, activated_name) != 0)
sl@0
  1890
        {
sl@0
  1891
          _dbus_warn ("Expected to see service %s created, saw %s instead\n",
sl@0
  1892
                      activated_name, service_name);
sl@0
  1893
          goto out;
sl@0
  1894
        }
sl@0
  1895
sl@0
  1896
      if (strcmp (base_service_name, base_service_from_bus) != 0)
sl@0
  1897
        {
sl@0
  1898
          _dbus_warn ("NameOwnerChanged reports wrong base service: %s owner, expected %s instead\n",
sl@0
  1899
                      base_service_from_bus, base_service_name);
sl@0
  1900
          goto out;
sl@0
  1901
        }
sl@0
  1902
sl@0
  1903
      if (old_owner[0])
sl@0
  1904
        {
sl@0
  1905
          _dbus_warn ("expected a %s, got a %s\n",
sl@0
  1906
                      "NameOwnerChanged (creation)",
sl@0
  1907
                      "NameOwnerChanged (change)");
sl@0
  1908
          goto out;
sl@0
  1909
        }
sl@0
  1910
sl@0
  1911
      socd.expected_kind = SERVICE_CREATED;
sl@0
  1912
      socd.skip_connection = connection;
sl@0
  1913
      socd.failed = FALSE;
sl@0
  1914
      socd.expected_service_name = service_name;
sl@0
  1915
      bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  1916
                                &socd);
sl@0
  1917
          
sl@0
  1918
      if (socd.failed)
sl@0
  1919
        goto out;
sl@0
  1920
          
sl@0
  1921
      dbus_message_unref (message);
sl@0
  1922
      service_name = NULL;
sl@0
  1923
      old_owner = NULL;
sl@0
  1924
      base_service_from_bus = NULL;
sl@0
  1925
      
sl@0
  1926
      message = pop_message_waiting_for_memory (connection);
sl@0
  1927
      if (message == NULL)
sl@0
  1928
        {
sl@0
  1929
          _dbus_warn ("Expected a reply to %s, got nothing\n",
sl@0
  1930
                      "StartServiceByName");
sl@0
  1931
          goto out;
sl@0
  1932
        }
sl@0
  1933
    }
sl@0
  1934
  else
sl@0
  1935
    {
sl@0
  1936
      warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
sl@0
  1937
      
sl@0
  1938
      goto out;
sl@0
  1939
    }
sl@0
  1940
  
sl@0
  1941
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  1942
    {
sl@0
  1943
      warn_unexpected (connection, message, "reply to StartServiceByName");
sl@0
  1944
sl@0
  1945
      goto out;
sl@0
  1946
    }
sl@0
  1947
sl@0
  1948
  activation_result = 0;
sl@0
  1949
  if (!dbus_message_get_args (message, &error,
sl@0
  1950
                              DBUS_TYPE_UINT32, &activation_result,
sl@0
  1951
                              DBUS_TYPE_INVALID))
sl@0
  1952
    {
sl@0
  1953
      if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  1954
        {
sl@0
  1955
          _dbus_warn ("Did not have activation result first argument to %s: %s\n",
sl@0
  1956
                      "StartServiceByName", error.message);
sl@0
  1957
          goto out;
sl@0
  1958
        }
sl@0
  1959
sl@0
  1960
      dbus_error_free (&error);
sl@0
  1961
    }
sl@0
  1962
  else
sl@0
  1963
    {
sl@0
  1964
      if (activation_result == DBUS_START_REPLY_SUCCESS)
sl@0
  1965
        ; /* Good */
sl@0
  1966
      else if (activation_result == DBUS_START_REPLY_ALREADY_RUNNING)
sl@0
  1967
        ; /* Good also */
sl@0
  1968
      else
sl@0
  1969
        {
sl@0
  1970
          _dbus_warn ("Activation result was %u, no good.\n",
sl@0
  1971
                      activation_result);
sl@0
  1972
          goto out;
sl@0
  1973
        }
sl@0
  1974
    }
sl@0
  1975
sl@0
  1976
  dbus_message_unref (message);
sl@0
  1977
  message = NULL;
sl@0
  1978
      
sl@0
  1979
  if (!check_no_leftovers (context))
sl@0
  1980
    {
sl@0
  1981
      _dbus_warn ("Messages were left over after verifying existent activation results\n");
sl@0
  1982
      goto out;
sl@0
  1983
    }
sl@0
  1984
sl@0
  1985
  retval = TRUE;
sl@0
  1986
  
sl@0
  1987
 out:
sl@0
  1988
  if (message)
sl@0
  1989
    dbus_message_unref (message);
sl@0
  1990
  dbus_error_free (&error);
sl@0
  1991
  
sl@0
  1992
  return retval;
sl@0
  1993
}
sl@0
  1994
sl@0
  1995
static dbus_bool_t
sl@0
  1996
check_service_auto_activated (BusContext     *context,
sl@0
  1997
                              DBusConnection *connection,
sl@0
  1998
                              const char     *activated_name,
sl@0
  1999
                              const char     *base_service_name,
sl@0
  2000
                              DBusMessage    *initial_message)
sl@0
  2001
{
sl@0
  2002
  DBusMessage *message;
sl@0
  2003
  dbus_bool_t retval;
sl@0
  2004
  DBusError error;
sl@0
  2005
  
sl@0
  2006
  retval = FALSE;
sl@0
  2007
  
sl@0
  2008
  dbus_error_init (&error);
sl@0
  2009
sl@0
  2010
  message = initial_message;
sl@0
  2011
  dbus_message_ref (message);
sl@0
  2012
sl@0
  2013
  if (dbus_message_is_signal (message,
sl@0
  2014
                              DBUS_INTERFACE_DBUS,
sl@0
  2015
                              "NameOwnerChanged"))
sl@0
  2016
    {
sl@0
  2017
      const char *service_name;
sl@0
  2018
      CheckServiceOwnerChangedData socd;
sl@0
  2019
      
sl@0
  2020
    reget_service_name_arg:
sl@0
  2021
      if (!dbus_message_get_args (message, &error,
sl@0
  2022
                                  DBUS_TYPE_STRING, &service_name,
sl@0
  2023
                                  DBUS_TYPE_INVALID))
sl@0
  2024
        {
sl@0
  2025
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  2026
            {
sl@0
  2027
              dbus_error_free (&error);
sl@0
  2028
              _dbus_wait_for_memory ();
sl@0
  2029
              goto reget_service_name_arg;
sl@0
  2030
            }
sl@0
  2031
          else
sl@0
  2032
            {
sl@0
  2033
              _dbus_warn ("Message %s doesn't have a service name: %s\n",
sl@0
  2034
                          "NameOwnerChanged",
sl@0
  2035
                          error.message);
sl@0
  2036
              dbus_error_free (&error);
sl@0
  2037
              goto out;
sl@0
  2038
            }
sl@0
  2039
        }
sl@0
  2040
      
sl@0
  2041
      if (strcmp (service_name, activated_name) != 0)
sl@0
  2042
        {
sl@0
  2043
          _dbus_warn ("Expected to see service %s created, saw %s instead\n",
sl@0
  2044
                      activated_name, service_name);
sl@0
  2045
          goto out;
sl@0
  2046
        }
sl@0
  2047
      
sl@0
  2048
      socd.expected_kind = SERVICE_CREATED;
sl@0
  2049
      socd.expected_service_name = service_name;
sl@0
  2050
      socd.failed = FALSE;
sl@0
  2051
      socd.skip_connection = connection; 
sl@0
  2052
      bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  2053
                                &socd);
sl@0
  2054
      
sl@0
  2055
      if (socd.failed)
sl@0
  2056
        goto out;
sl@0
  2057
      
sl@0
  2058
      /* Note that this differs from regular activation in that we don't get a
sl@0
  2059
       * reply to ActivateService here.
sl@0
  2060
       */
sl@0
  2061
      
sl@0
  2062
      dbus_message_unref (message);
sl@0
  2063
      message = NULL;
sl@0
  2064
      service_name = NULL;
sl@0
  2065
    }
sl@0
  2066
  else
sl@0
  2067
    {
sl@0
  2068
      warn_unexpected (connection, message, "NameOwnerChanged for the activated name");
sl@0
  2069
      
sl@0
  2070
      goto out;
sl@0
  2071
    }
sl@0
  2072
  
sl@0
  2073
  retval = TRUE;
sl@0
  2074
  
sl@0
  2075
 out:
sl@0
  2076
  if (message)
sl@0
  2077
    dbus_message_unref (message);
sl@0
  2078
  
sl@0
  2079
  return retval;
sl@0
  2080
}
sl@0
  2081
sl@0
  2082
static dbus_bool_t
sl@0
  2083
check_service_deactivated (BusContext     *context,
sl@0
  2084
                           DBusConnection *connection,
sl@0
  2085
                           const char     *activated_name,
sl@0
  2086
                           const char     *base_service)
sl@0
  2087
{
sl@0
  2088
  dbus_bool_t retval;
sl@0
  2089
  CheckServiceOwnerChangedData socd;
sl@0
  2090
sl@0
  2091
  retval = FALSE;
sl@0
  2092
  
sl@0
  2093
  /* Now we are expecting ServiceOwnerChanged (deletion) messages for the base
sl@0
  2094
   * service and the activated_name.  The base service
sl@0
  2095
   * notification is required to come last.
sl@0
  2096
   */
sl@0
  2097
  socd.expected_kind = SERVICE_DELETED;
sl@0
  2098
  socd.expected_service_name = activated_name;
sl@0
  2099
  socd.failed = FALSE;
sl@0
  2100
  socd.skip_connection = NULL;
sl@0
  2101
  bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  2102
                            &socd);      
sl@0
  2103
sl@0
  2104
  if (socd.failed)
sl@0
  2105
    goto out;
sl@0
  2106
      
sl@0
  2107
  socd.expected_kind = SERVICE_DELETED;
sl@0
  2108
  socd.expected_service_name = base_service;
sl@0
  2109
  socd.failed = FALSE;
sl@0
  2110
  socd.skip_connection = NULL;
sl@0
  2111
  bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  2112
                            &socd);
sl@0
  2113
sl@0
  2114
  if (socd.failed)
sl@0
  2115
    goto out;
sl@0
  2116
sl@0
  2117
  retval = TRUE;
sl@0
  2118
  
sl@0
  2119
 out:
sl@0
  2120
  return retval;
sl@0
  2121
}
sl@0
  2122
sl@0
  2123
static dbus_bool_t
sl@0
  2124
check_send_exit_to_service (BusContext     *context,
sl@0
  2125
                            DBusConnection *connection,
sl@0
  2126
                            const char     *service_name,
sl@0
  2127
                            const char     *base_service)
sl@0
  2128
{
sl@0
  2129
  dbus_bool_t got_error;
sl@0
  2130
  DBusMessage *message;
sl@0
  2131
  dbus_uint32_t serial;
sl@0
  2132
  dbus_bool_t retval;
sl@0
  2133
  
sl@0
  2134
  _dbus_verbose ("Sending exit message to the test service\n");
sl@0
  2135
sl@0
  2136
  retval = FALSE;
sl@0
  2137
  
sl@0
  2138
  /* Kill off the test service by sending it a quit message */
sl@0
  2139
  message = dbus_message_new_method_call (service_name,
sl@0
  2140
                                          "/org/freedesktop/TestSuite",
sl@0
  2141
                                          "org.freedesktop.TestSuite",
sl@0
  2142
                                          "Exit");
sl@0
  2143
      
sl@0
  2144
  if (message == NULL)
sl@0
  2145
    {
sl@0
  2146
      /* Do this again; we still need the service to exit... */
sl@0
  2147
      if (!check_send_exit_to_service (context, connection,
sl@0
  2148
                                       service_name, base_service))
sl@0
  2149
        goto out;
sl@0
  2150
      
sl@0
  2151
      return TRUE;
sl@0
  2152
    }
sl@0
  2153
      
sl@0
  2154
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2155
    {
sl@0
  2156
      dbus_message_unref (message);
sl@0
  2157
sl@0
  2158
      /* Do this again; we still need the service to exit... */
sl@0
  2159
      if (!check_send_exit_to_service (context, connection,
sl@0
  2160
                                       service_name, base_service))
sl@0
  2161
        goto out;
sl@0
  2162
      
sl@0
  2163
      return TRUE;
sl@0
  2164
    }
sl@0
  2165
sl@0
  2166
  dbus_message_unref (message);
sl@0
  2167
  message = NULL;
sl@0
  2168
sl@0
  2169
  /* send message */
sl@0
  2170
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  2171
sl@0
  2172
  /* read it in and write it out to test service */
sl@0
  2173
  bus_test_run_bus_loop (context, FALSE);
sl@0
  2174
sl@0
  2175
  /* see if we got an error during message bus dispatching */
sl@0
  2176
  bus_test_run_clients_loop (FALSE);
sl@0
  2177
  message = borrow_message_waiting_for_memory (connection);
sl@0
  2178
  got_error = message != NULL && dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR;
sl@0
  2179
  if (message)
sl@0
  2180
    {
sl@0
  2181
      dbus_connection_return_message (connection, message);
sl@0
  2182
      message = NULL;
sl@0
  2183
    }
sl@0
  2184
          
sl@0
  2185
  if (!got_error)
sl@0
  2186
    {
sl@0
  2187
      /* If no error, wait for the test service to exit */
sl@0
  2188
      block_connection_until_message_from_bus (context, connection, "test service to exit");
sl@0
  2189
              
sl@0
  2190
      bus_test_run_everything (context);
sl@0
  2191
    }
sl@0
  2192
sl@0
  2193
  if (got_error)
sl@0
  2194
    {
sl@0
  2195
      message = pop_message_waiting_for_memory (connection);
sl@0
  2196
      _dbus_assert (message != NULL);
sl@0
  2197
sl@0
  2198
      if (dbus_message_get_reply_serial (message) != serial)
sl@0
  2199
        {
sl@0
  2200
          warn_unexpected (connection, message,
sl@0
  2201
                           "error with the correct reply serial");
sl@0
  2202
          goto out;
sl@0
  2203
        }
sl@0
  2204
      
sl@0
  2205
      if (!dbus_message_is_error (message,
sl@0
  2206
                                  DBUS_ERROR_NO_MEMORY))
sl@0
  2207
        {
sl@0
  2208
          warn_unexpected (connection, message,
sl@0
  2209
                           "a no memory error from asking test service to exit");
sl@0
  2210
          goto out;
sl@0
  2211
        }
sl@0
  2212
sl@0
  2213
      _dbus_verbose ("Got error %s when asking test service to exit\n",
sl@0
  2214
                     dbus_message_get_error_name (message));
sl@0
  2215
sl@0
  2216
      /* Do this again; we still need the service to exit... */
sl@0
  2217
      if (!check_send_exit_to_service (context, connection,
sl@0
  2218
                                       service_name, base_service))
sl@0
  2219
        goto out;
sl@0
  2220
    }
sl@0
  2221
  else
sl@0
  2222
    {
sl@0
  2223
      if (!check_service_deactivated (context, connection,
sl@0
  2224
                                      service_name, base_service))
sl@0
  2225
        goto out;
sl@0
  2226
sl@0
  2227
      /* Should now have a NoReply error from the Exit() method
sl@0
  2228
       * call; it should have come after all the deactivation
sl@0
  2229
       * stuff.
sl@0
  2230
       */
sl@0
  2231
      message = pop_message_waiting_for_memory (connection);
sl@0
  2232
          
sl@0
  2233
      if (message == NULL)
sl@0
  2234
        {
sl@0
  2235
          warn_unexpected (connection, NULL,
sl@0
  2236
                           "reply to Exit() method call");
sl@0
  2237
          goto out;
sl@0
  2238
        }
sl@0
  2239
      if (!dbus_message_is_error (message,
sl@0
  2240
                                  DBUS_ERROR_NO_REPLY))
sl@0
  2241
        {
sl@0
  2242
          warn_unexpected (connection, message,
sl@0
  2243
                           "NoReply error from Exit() method call");
sl@0
  2244
          goto out;
sl@0
  2245
        }
sl@0
  2246
sl@0
  2247
      if (dbus_message_get_reply_serial (message) != serial)
sl@0
  2248
        {
sl@0
  2249
          warn_unexpected (connection, message,
sl@0
  2250
                           "error with the correct reply serial");
sl@0
  2251
          goto out;
sl@0
  2252
        }
sl@0
  2253
          
sl@0
  2254
      _dbus_verbose ("Got error %s after test service exited\n",
sl@0
  2255
                     dbus_message_get_error_name (message));
sl@0
  2256
      
sl@0
  2257
      if (!check_no_leftovers (context))
sl@0
  2258
        {
sl@0
  2259
          _dbus_warn ("Messages were left over after %s\n",
sl@0
  2260
                      _DBUS_FUNCTION_NAME);
sl@0
  2261
          goto out;
sl@0
  2262
        }
sl@0
  2263
    }
sl@0
  2264
  
sl@0
  2265
  retval = TRUE;
sl@0
  2266
  
sl@0
  2267
 out:
sl@0
  2268
  if (message)
sl@0
  2269
    dbus_message_unref (message);
sl@0
  2270
  
sl@0
  2271
  return retval;
sl@0
  2272
}
sl@0
  2273
sl@0
  2274
static dbus_bool_t
sl@0
  2275
check_got_error (BusContext     *context,
sl@0
  2276
                 DBusConnection *connection,
sl@0
  2277
                 const char     *first_error_name,
sl@0
  2278
                 ...)
sl@0
  2279
{
sl@0
  2280
  DBusMessage *message;
sl@0
  2281
  dbus_bool_t retval;
sl@0
  2282
  va_list ap;
sl@0
  2283
  dbus_bool_t error_found;
sl@0
  2284
  const char *error_name;
sl@0
  2285
  
sl@0
  2286
  retval = FALSE;
sl@0
  2287
  
sl@0
  2288
  message = pop_message_waiting_for_memory (connection);
sl@0
  2289
  if (message == NULL)
sl@0
  2290
    {
sl@0
  2291
      _dbus_warn ("Did not get an expected error\n");
sl@0
  2292
      goto out;
sl@0
  2293
    }
sl@0
  2294
sl@0
  2295
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
sl@0
  2296
    {
sl@0
  2297
      warn_unexpected (connection, message, "an error");
sl@0
  2298
sl@0
  2299
      goto out;
sl@0
  2300
    }
sl@0
  2301
sl@0
  2302
  error_found = FALSE;
sl@0
  2303
sl@0
  2304
  va_start (ap, first_error_name);
sl@0
  2305
  error_name = first_error_name;
sl@0
  2306
  while (error_name != NULL)
sl@0
  2307
    {
sl@0
  2308
      if (dbus_message_is_error (message, error_name))
sl@0
  2309
        {
sl@0
  2310
          error_found = TRUE;
sl@0
  2311
          break;
sl@0
  2312
        }
sl@0
  2313
      error_name = va_arg (ap, char*);
sl@0
  2314
    }
sl@0
  2315
  va_end (ap);
sl@0
  2316
sl@0
  2317
  if (!error_found)
sl@0
  2318
    {
sl@0
  2319
      _dbus_warn ("Expected error %s or other, got %s instead\n",
sl@0
  2320
                  first_error_name,
sl@0
  2321
                  dbus_message_get_error_name (message));
sl@0
  2322
      goto out;
sl@0
  2323
    }
sl@0
  2324
sl@0
  2325
  retval = TRUE;
sl@0
  2326
  
sl@0
  2327
 out:
sl@0
  2328
  if (message)
sl@0
  2329
    dbus_message_unref (message);
sl@0
  2330
  
sl@0
  2331
  return retval;
sl@0
  2332
}
sl@0
  2333
          
sl@0
  2334
typedef enum
sl@0
  2335
{ 
sl@0
  2336
  GOT_SERVICE_CREATED,
sl@0
  2337
  GOT_SERVICE_DELETED,
sl@0
  2338
  GOT_ERROR,
sl@0
  2339
  GOT_SOMETHING_ELSE 
sl@0
  2340
} GotServiceInfo;
sl@0
  2341
sl@0
  2342
static GotServiceInfo
sl@0
  2343
check_got_service_info (DBusMessage *message)
sl@0
  2344
{
sl@0
  2345
  GotServiceInfo message_kind;
sl@0
  2346
sl@0
  2347
  if (dbus_message_is_signal (message,
sl@0
  2348
                              DBUS_INTERFACE_DBUS,
sl@0
  2349
                              "NameOwnerChanged"))
sl@0
  2350
    {
sl@0
  2351
      DBusError error;
sl@0
  2352
      const char *service_name, *old_owner, *new_owner;
sl@0
  2353
      dbus_error_init (&error);
sl@0
  2354
sl@0
  2355
    reget_service_info_data:
sl@0
  2356
      service_name = NULL;
sl@0
  2357
      old_owner = NULL;
sl@0
  2358
      new_owner = NULL;
sl@0
  2359
sl@0
  2360
      dbus_message_get_args (message, &error,
sl@0
  2361
                             DBUS_TYPE_STRING, &service_name,
sl@0
  2362
                             DBUS_TYPE_STRING, &old_owner,
sl@0
  2363
                             DBUS_TYPE_STRING, &new_owner,
sl@0
  2364
                             DBUS_TYPE_INVALID);
sl@0
  2365
      if (dbus_error_is_set (&error))
sl@0
  2366
        {
sl@0
  2367
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  2368
            {
sl@0
  2369
              dbus_error_free (&error);
sl@0
  2370
              goto reget_service_info_data;
sl@0
  2371
            }
sl@0
  2372
          else
sl@0
  2373
            {
sl@0
  2374
              _dbus_warn ("unexpected arguments for NameOwnerChanged message\n");
sl@0
  2375
              message_kind = GOT_SOMETHING_ELSE;
sl@0
  2376
            }
sl@0
  2377
        }
sl@0
  2378
      else if (!old_owner[0])
sl@0
  2379
        message_kind = GOT_SERVICE_CREATED;
sl@0
  2380
      else if (!new_owner[0])
sl@0
  2381
        message_kind = GOT_SERVICE_DELETED;
sl@0
  2382
      else
sl@0
  2383
        message_kind = GOT_SOMETHING_ELSE;
sl@0
  2384
sl@0
  2385
      dbus_error_free (&error);
sl@0
  2386
    }
sl@0
  2387
  else if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  2388
    message_kind = GOT_ERROR;
sl@0
  2389
  else
sl@0
  2390
    message_kind = GOT_SOMETHING_ELSE;
sl@0
  2391
sl@0
  2392
  return message_kind;
sl@0
  2393
}
sl@0
  2394
sl@0
  2395
#define EXISTENT_SERVICE_NAME "org.freedesktop.DBus.TestSuiteEchoService"
sl@0
  2396
sl@0
  2397
/* returns TRUE if the correct thing happens,
sl@0
  2398
 * but the correct thing may include OOM errors.
sl@0
  2399
 */
sl@0
  2400
static dbus_bool_t
sl@0
  2401
check_existent_service_no_auto_start (BusContext     *context,
sl@0
  2402
                                      DBusConnection *connection)
sl@0
  2403
{
sl@0
  2404
  DBusMessage *message;
sl@0
  2405
  DBusMessage *base_service_message;
sl@0
  2406
  const char *base_service;
sl@0
  2407
  dbus_uint32_t serial;
sl@0
  2408
  dbus_bool_t retval;
sl@0
  2409
  const char *existent = EXISTENT_SERVICE_NAME;
sl@0
  2410
  dbus_uint32_t flags;
sl@0
  2411
sl@0
  2412
  base_service_message = NULL;
sl@0
  2413
  
sl@0
  2414
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  2415
                                          DBUS_PATH_DBUS,
sl@0
  2416
                                          DBUS_INTERFACE_DBUS,
sl@0
  2417
                                          "StartServiceByName");
sl@0
  2418
sl@0
  2419
  if (message == NULL)
sl@0
  2420
    return TRUE;
sl@0
  2421
sl@0
  2422
  dbus_message_set_auto_start (message, FALSE);
sl@0
  2423
  
sl@0
  2424
  flags = 0;
sl@0
  2425
  if (!dbus_message_append_args (message,
sl@0
  2426
                                 DBUS_TYPE_STRING, &existent,
sl@0
  2427
                                 DBUS_TYPE_UINT32, &flags,
sl@0
  2428
                                 DBUS_TYPE_INVALID))
sl@0
  2429
    {
sl@0
  2430
      dbus_message_unref (message);
sl@0
  2431
      return TRUE;
sl@0
  2432
    }
sl@0
  2433
  
sl@0
  2434
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2435
    {
sl@0
  2436
      dbus_message_unref (message);
sl@0
  2437
      return TRUE;
sl@0
  2438
    }
sl@0
  2439
sl@0
  2440
  dbus_message_unref (message);
sl@0
  2441
  message = NULL;
sl@0
  2442
sl@0
  2443
  bus_test_run_everything (context);
sl@0
  2444
sl@0
  2445
  /* now wait for the message bus to hear back from the activated
sl@0
  2446
   * service.
sl@0
  2447
   */
sl@0
  2448
  block_connection_until_message_from_bus (context, connection, "activated service to connect");
sl@0
  2449
sl@0
  2450
  bus_test_run_everything (context);
sl@0
  2451
sl@0
  2452
  if (!dbus_connection_get_is_connected (connection))
sl@0
  2453
    {
sl@0
  2454
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  2455
      return TRUE;
sl@0
  2456
    }
sl@0
  2457
  
sl@0
  2458
  retval = FALSE;
sl@0
  2459
  
sl@0
  2460
  message = pop_message_waiting_for_memory (connection);
sl@0
  2461
  if (message == NULL)
sl@0
  2462
    {
sl@0
  2463
      _dbus_warn ("Did not receive any messages after %s %d on %p\n",
sl@0
  2464
                  "StartServiceByName", serial, connection);
sl@0
  2465
      goto out;
sl@0
  2466
    }
sl@0
  2467
sl@0
  2468
  verbose_message_received (connection, message);
sl@0
  2469
  _dbus_verbose ("  (after sending %s)\n", "StartServiceByName");
sl@0
  2470
sl@0
  2471
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  2472
    {
sl@0
  2473
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  2474
        {
sl@0
  2475
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  2476
                      dbus_message_get_sender (message) ?
sl@0
  2477
                      dbus_message_get_sender (message) : "(none)");
sl@0
  2478
          goto out;
sl@0
  2479
        }
sl@0
  2480
      
sl@0
  2481
      if (dbus_message_is_error (message,
sl@0
  2482
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  2483
        {
sl@0
  2484
          ; /* good, this is a valid response */
sl@0
  2485
        }
sl@0
  2486
      else if (dbus_message_is_error (message,
sl@0
  2487
                                      DBUS_ERROR_SPAWN_CHILD_EXITED) ||
sl@0
  2488
               dbus_message_is_error (message,
sl@0
  2489
                                      DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
sl@0
  2490
               dbus_message_is_error (message,
sl@0
  2491
                                      DBUS_ERROR_SPAWN_EXEC_FAILED))
sl@0
  2492
        {
sl@0
  2493
          ; /* good, this is expected also */
sl@0
  2494
        }
sl@0
  2495
      else
sl@0
  2496
        {
sl@0
  2497
          _dbus_warn ("Did not expect error %s\n",
sl@0
  2498
                      dbus_message_get_error_name (message));
sl@0
  2499
          goto out;
sl@0
  2500
        }
sl@0
  2501
    }
sl@0
  2502
  else
sl@0
  2503
    {
sl@0
  2504
      GotServiceInfo message_kind;
sl@0
  2505
      
sl@0
  2506
      if (!check_base_service_activated (context, connection,
sl@0
  2507
                                         message, &base_service))
sl@0
  2508
        goto out;
sl@0
  2509
sl@0
  2510
      base_service_message = message;
sl@0
  2511
      message = NULL;
sl@0
  2512
sl@0
  2513
      /* We may need to block here for the test service to exit or finish up */
sl@0
  2514
      block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
sl@0
  2515
      
sl@0
  2516
      message = dbus_connection_borrow_message (connection);
sl@0
  2517
      if (message == NULL)
sl@0
  2518
        {
sl@0
  2519
          _dbus_warn ("Did not receive any messages after base service creation notification\n");
sl@0
  2520
          goto out;
sl@0
  2521
        }
sl@0
  2522
sl@0
  2523
      message_kind = check_got_service_info (message);
sl@0
  2524
sl@0
  2525
      dbus_connection_return_message (connection, message);
sl@0
  2526
      message = NULL;
sl@0
  2527
sl@0
  2528
      switch (message_kind)
sl@0
  2529
        {
sl@0
  2530
        case GOT_SOMETHING_ELSE:
sl@0
  2531
          _dbus_warn ("Unexpected message after ActivateService "
sl@0
  2532
                      "(should be an error or a service announcement");
sl@0
  2533
          goto out;
sl@0
  2534
sl@0
  2535
        case GOT_ERROR:
sl@0
  2536
          if (!check_got_error (context, connection,
sl@0
  2537
                                DBUS_ERROR_SPAWN_CHILD_EXITED,
sl@0
  2538
                                DBUS_ERROR_NO_MEMORY,
sl@0
  2539
                                NULL))
sl@0
  2540
            goto out;
sl@0
  2541
          /* A service deleted should be coming along now after this error.
sl@0
  2542
           * We can also get the error *after* the service deleted.
sl@0
  2543
           */
sl@0
  2544
sl@0
  2545
          /* fall through */
sl@0
  2546
sl@0
  2547
        case GOT_SERVICE_DELETED:
sl@0
  2548
          {
sl@0
  2549
            /* The service started up and got a base address, but then
sl@0
  2550
             * failed to register under EXISTENT_SERVICE_NAME
sl@0
  2551
             */
sl@0
  2552
            CheckServiceOwnerChangedData socd;
sl@0
  2553
sl@0
  2554
            socd.expected_kind = SERVICE_DELETED;
sl@0
  2555
            socd.expected_service_name = base_service;
sl@0
  2556
            socd.failed = FALSE;
sl@0
  2557
            socd.skip_connection = NULL;
sl@0
  2558
            
sl@0
  2559
            bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  2560
                                      &socd);
sl@0
  2561
sl@0
  2562
            if (socd.failed)
sl@0
  2563
              goto out;
sl@0
  2564
sl@0
  2565
            /* Now we should get an error about the service exiting
sl@0
  2566
             * if we didn't get it before.
sl@0
  2567
             */
sl@0
  2568
            if (message_kind != GOT_ERROR)
sl@0
  2569
              {
sl@0
  2570
                block_connection_until_message_from_bus (context, connection, "error about service exiting");
sl@0
  2571
		
sl@0
  2572
                /* and process everything again */
sl@0
  2573
                bus_test_run_everything (context);
sl@0
  2574
              
sl@0
  2575
                if (!check_got_error (context, connection,
sl@0
  2576
                                      DBUS_ERROR_SPAWN_CHILD_EXITED,
sl@0
  2577
				      DBUS_ERROR_NO_MEMORY,
sl@0
  2578
                                      NULL))
sl@0
  2579
                  goto out;
sl@0
  2580
              }
sl@0
  2581
            break;
sl@0
  2582
          }
sl@0
  2583
sl@0
  2584
        case GOT_SERVICE_CREATED:
sl@0
  2585
          message = pop_message_waiting_for_memory (connection);
sl@0
  2586
          if (message == NULL)
sl@0
  2587
            {
sl@0
  2588
              _dbus_warn ("Failed to pop message we just put back! "
sl@0
  2589
                          "should have been a NameOwnerChanged (creation)\n");
sl@0
  2590
              goto out;
sl@0
  2591
            }
sl@0
  2592
          
sl@0
  2593
          if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
sl@0
  2594
                                        base_service, message))
sl@0
  2595
            goto out;
sl@0
  2596
          
sl@0
  2597
          dbus_message_unref (message);
sl@0
  2598
          message = NULL;
sl@0
  2599
sl@0
  2600
          if (!check_no_leftovers (context))
sl@0
  2601
            {
sl@0
  2602
              _dbus_warn ("Messages were left over after successful activation\n");
sl@0
  2603
              goto out;
sl@0
  2604
            }
sl@0
  2605
sl@0
  2606
	  if (!check_send_exit_to_service (context, connection,
sl@0
  2607
                                           EXISTENT_SERVICE_NAME, base_service))
sl@0
  2608
	    goto out;
sl@0
  2609
sl@0
  2610
          break;
sl@0
  2611
        }
sl@0
  2612
    }
sl@0
  2613
sl@0
  2614
  retval = TRUE;
sl@0
  2615
  
sl@0
  2616
 out:
sl@0
  2617
  if (message)
sl@0
  2618
    dbus_message_unref (message);
sl@0
  2619
sl@0
  2620
  if (base_service_message)
sl@0
  2621
    dbus_message_unref (base_service_message);
sl@0
  2622
  
sl@0
  2623
  return retval;
sl@0
  2624
}
sl@0
  2625
sl@0
  2626
/* returns TRUE if the correct thing happens,
sl@0
  2627
 * but the correct thing may include OOM errors.
sl@0
  2628
 */
sl@0
  2629
static dbus_bool_t
sl@0
  2630
check_segfault_service_no_auto_start (BusContext     *context,
sl@0
  2631
                                      DBusConnection *connection)
sl@0
  2632
{
sl@0
  2633
  DBusMessage *message;
sl@0
  2634
  dbus_uint32_t serial;
sl@0
  2635
  dbus_bool_t retval;
sl@0
  2636
  const char *segv_service;
sl@0
  2637
  dbus_uint32_t flags;
sl@0
  2638
  
sl@0
  2639
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  2640
                                          DBUS_PATH_DBUS,
sl@0
  2641
                                          DBUS_INTERFACE_DBUS,
sl@0
  2642
                                          "StartServiceByName");
sl@0
  2643
sl@0
  2644
  if (message == NULL)
sl@0
  2645
    return TRUE;
sl@0
  2646
sl@0
  2647
  dbus_message_set_auto_start (message, FALSE);
sl@0
  2648
  
sl@0
  2649
  segv_service = "org.freedesktop.DBus.TestSuiteSegfaultService";
sl@0
  2650
  flags = 0;
sl@0
  2651
  if (!dbus_message_append_args (message,
sl@0
  2652
                                 DBUS_TYPE_STRING, &segv_service,
sl@0
  2653
                                 DBUS_TYPE_UINT32, &flags,
sl@0
  2654
                                 DBUS_TYPE_INVALID))
sl@0
  2655
    {
sl@0
  2656
      dbus_message_unref (message);
sl@0
  2657
      return TRUE;
sl@0
  2658
    }
sl@0
  2659
  
sl@0
  2660
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2661
    {
sl@0
  2662
      dbus_message_unref (message);
sl@0
  2663
      return TRUE;
sl@0
  2664
    }
sl@0
  2665
sl@0
  2666
  dbus_message_unref (message);
sl@0
  2667
  message = NULL;
sl@0
  2668
sl@0
  2669
  bus_test_run_everything (context);
sl@0
  2670
  block_connection_until_message_from_bus (context, connection, "reply to activating segfault service");
sl@0
  2671
  bus_test_run_everything (context);
sl@0
  2672
sl@0
  2673
  if (!dbus_connection_get_is_connected (connection))
sl@0
  2674
    {
sl@0
  2675
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  2676
      return TRUE;
sl@0
  2677
    }
sl@0
  2678
  
sl@0
  2679
  retval = FALSE;
sl@0
  2680
  
sl@0
  2681
  message = pop_message_waiting_for_memory (connection);
sl@0
  2682
  if (message == NULL)
sl@0
  2683
    {
sl@0
  2684
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  2685
                  "StartServiceByName", serial, connection);
sl@0
  2686
      goto out;
sl@0
  2687
    }
sl@0
  2688
sl@0
  2689
  verbose_message_received (connection, message);
sl@0
  2690
sl@0
  2691
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  2692
    {
sl@0
  2693
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  2694
        {
sl@0
  2695
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  2696
                      dbus_message_get_sender (message) ?
sl@0
  2697
                      dbus_message_get_sender (message) : "(none)");
sl@0
  2698
          goto out;
sl@0
  2699
        }
sl@0
  2700
      
sl@0
  2701
      if (dbus_message_is_error (message,
sl@0
  2702
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  2703
        {
sl@0
  2704
          ; /* good, this is a valid response */
sl@0
  2705
        }
sl@0
  2706
      else if (dbus_message_is_error (message,
sl@0
  2707
                                      DBUS_ERROR_SPAWN_CHILD_SIGNALED))
sl@0
  2708
        {
sl@0
  2709
          ; /* good, this is expected also */
sl@0
  2710
        }
sl@0
  2711
      else
sl@0
  2712
        {
sl@0
  2713
          warn_unexpected (connection, message, "not this error");
sl@0
  2714
sl@0
  2715
          goto out;
sl@0
  2716
        }
sl@0
  2717
    }
sl@0
  2718
  else
sl@0
  2719
    {
sl@0
  2720
      _dbus_warn ("Did not expect to successfully activate segfault service\n");
sl@0
  2721
      goto out;
sl@0
  2722
    }
sl@0
  2723
sl@0
  2724
  retval = TRUE;
sl@0
  2725
  
sl@0
  2726
 out:
sl@0
  2727
  if (message)
sl@0
  2728
    dbus_message_unref (message);
sl@0
  2729
  
sl@0
  2730
  return retval;
sl@0
  2731
}
sl@0
  2732
sl@0
  2733
sl@0
  2734
/* returns TRUE if the correct thing happens,
sl@0
  2735
 * but the correct thing may include OOM errors.
sl@0
  2736
 */
sl@0
  2737
static dbus_bool_t
sl@0
  2738
check_segfault_service_auto_start (BusContext     *context,
sl@0
  2739
                                   DBusConnection *connection)
sl@0
  2740
{
sl@0
  2741
  DBusMessage *message;
sl@0
  2742
  dbus_uint32_t serial;
sl@0
  2743
  dbus_bool_t retval;
sl@0
  2744
sl@0
  2745
  message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteSegfaultService",
sl@0
  2746
                                          "/org/freedesktop/TestSuite",
sl@0
  2747
                                          "org.freedesktop.TestSuite",
sl@0
  2748
                                          "Echo");
sl@0
  2749
  
sl@0
  2750
  if (message == NULL)
sl@0
  2751
    return TRUE;
sl@0
  2752
  
sl@0
  2753
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2754
    {
sl@0
  2755
      dbus_message_unref (message);
sl@0
  2756
      return TRUE;
sl@0
  2757
    }
sl@0
  2758
sl@0
  2759
  dbus_message_unref (message);
sl@0
  2760
  message = NULL;
sl@0
  2761
sl@0
  2762
  bus_test_run_everything (context);
sl@0
  2763
  block_connection_until_message_from_bus (context, connection, "reply to Echo on segfault service");
sl@0
  2764
  bus_test_run_everything (context);
sl@0
  2765
sl@0
  2766
  if (!dbus_connection_get_is_connected (connection))
sl@0
  2767
    {
sl@0
  2768
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  2769
      return TRUE;
sl@0
  2770
    }
sl@0
  2771
  
sl@0
  2772
  retval = FALSE;
sl@0
  2773
  
sl@0
  2774
  message = pop_message_waiting_for_memory (connection);
sl@0
  2775
  if (message == NULL)
sl@0
  2776
    {
sl@0
  2777
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  2778
                  "Echo message (auto activation)", serial, connection);
sl@0
  2779
      goto out;
sl@0
  2780
    }
sl@0
  2781
sl@0
  2782
  verbose_message_received (connection, message);
sl@0
  2783
sl@0
  2784
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  2785
    {
sl@0
  2786
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  2787
        {
sl@0
  2788
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  2789
                      dbus_message_get_sender (message) ?
sl@0
  2790
                      dbus_message_get_sender (message) : "(none)");
sl@0
  2791
          goto out;
sl@0
  2792
        }
sl@0
  2793
      
sl@0
  2794
      if (dbus_message_is_error (message,
sl@0
  2795
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  2796
        {
sl@0
  2797
          ; /* good, this is a valid response */
sl@0
  2798
        }
sl@0
  2799
      else if (dbus_message_is_error (message,
sl@0
  2800
                                      DBUS_ERROR_SPAWN_CHILD_SIGNALED))
sl@0
  2801
        {
sl@0
  2802
          ; /* good, this is expected also */
sl@0
  2803
        }
sl@0
  2804
      else
sl@0
  2805
        {
sl@0
  2806
          warn_unexpected (connection, message, "not this error");
sl@0
  2807
sl@0
  2808
          goto out;
sl@0
  2809
        }
sl@0
  2810
    }
sl@0
  2811
  else
sl@0
  2812
    {
sl@0
  2813
      _dbus_warn ("Did not expect to successfully activate segfault service\n");
sl@0
  2814
      goto out;
sl@0
  2815
    }
sl@0
  2816
sl@0
  2817
  retval = TRUE;
sl@0
  2818
  
sl@0
  2819
 out:
sl@0
  2820
  if (message)
sl@0
  2821
    dbus_message_unref (message);
sl@0
  2822
  
sl@0
  2823
  return retval;
sl@0
  2824
}
sl@0
  2825
sl@0
  2826
#define TEST_ECHO_MESSAGE "Test echo message"
sl@0
  2827
#define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
sl@0
  2828
sl@0
  2829
/* returns TRUE if the correct thing happens,
sl@0
  2830
 * but the correct thing may include OOM errors.
sl@0
  2831
 */
sl@0
  2832
static dbus_bool_t
sl@0
  2833
check_existent_hello_from_self (BusContext     *context,
sl@0
  2834
                                DBusConnection *connection)
sl@0
  2835
{
sl@0
  2836
  DBusMessage *message;
sl@0
  2837
  dbus_uint32_t serial;
sl@0
  2838
  const char *text;
sl@0
  2839
sl@0
  2840
  message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
sl@0
  2841
                                          "/org/freedesktop/TestSuite",
sl@0
  2842
                                          "org.freedesktop.TestSuite",
sl@0
  2843
                                          "RunHelloFromSelf");
sl@0
  2844
  
sl@0
  2845
  if (message == NULL)
sl@0
  2846
    return TRUE;
sl@0
  2847
sl@0
  2848
  text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
sl@0
  2849
  if (!dbus_message_append_args (message,
sl@0
  2850
                                 DBUS_TYPE_STRING, &text,
sl@0
  2851
                                 DBUS_TYPE_INVALID))
sl@0
  2852
    {
sl@0
  2853
      dbus_message_unref (message);
sl@0
  2854
      return TRUE;
sl@0
  2855
    }
sl@0
  2856
sl@0
  2857
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2858
    {
sl@0
  2859
      dbus_message_unref (message);
sl@0
  2860
      return TRUE;
sl@0
  2861
    }
sl@0
  2862
sl@0
  2863
  dbus_message_unref (message);
sl@0
  2864
  message = NULL;
sl@0
  2865
sl@0
  2866
  bus_test_run_everything (context);
sl@0
  2867
sl@0
  2868
  /* Note: if this test is run in OOM mode, it will block when the bus
sl@0
  2869
   * doesn't send a reply due to OOM.
sl@0
  2870
   */
sl@0
  2871
  block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
sl@0
  2872
      
sl@0
  2873
  message = pop_message_waiting_for_memory (connection);
sl@0
  2874
  if (message == NULL)
sl@0
  2875
    {
sl@0
  2876
      _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
sl@0
  2877
      return FALSE;
sl@0
  2878
    }
sl@0
  2879
sl@0
  2880
  if (dbus_message_get_reply_serial (message) != serial)
sl@0
  2881
    {
sl@0
  2882
      _dbus_warn ("Wrong reply serial\n");
sl@0
  2883
      dbus_message_unref (message);
sl@0
  2884
      return FALSE;
sl@0
  2885
    }
sl@0
  2886
sl@0
  2887
  dbus_message_unref (message);
sl@0
  2888
  message = NULL;
sl@0
  2889
      
sl@0
  2890
  return TRUE;
sl@0
  2891
}
sl@0
  2892
sl@0
  2893
/* returns TRUE if the correct thing happens,
sl@0
  2894
 * but the correct thing may include OOM errors.
sl@0
  2895
 */
sl@0
  2896
static dbus_bool_t
sl@0
  2897
check_existent_ping (BusContext     *context,
sl@0
  2898
                     DBusConnection *connection)
sl@0
  2899
{
sl@0
  2900
  DBusMessage *message;
sl@0
  2901
  dbus_uint32_t serial;
sl@0
  2902
  message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
sl@0
  2903
                                          "/org/freedesktop/TestSuite",
sl@0
  2904
                                          "org.freedesktop.DBus.Peer",
sl@0
  2905
                                          "Ping");
sl@0
  2906
  
sl@0
  2907
  if (message == NULL)
sl@0
  2908
    return TRUE;
sl@0
  2909
sl@0
  2910
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2911
    {
sl@0
  2912
      dbus_message_unref (message);
sl@0
  2913
      return TRUE;
sl@0
  2914
    }
sl@0
  2915
sl@0
  2916
  dbus_message_unref (message);
sl@0
  2917
  message = NULL;
sl@0
  2918
sl@0
  2919
  bus_test_run_everything (context);
sl@0
  2920
sl@0
  2921
  /* Note: if this test is run in OOM mode, it will block when the bus
sl@0
  2922
   * doesn't send a reply due to OOM.
sl@0
  2923
   */
sl@0
  2924
  block_connection_until_message_from_bus (context, connection, "reply from running Ping");
sl@0
  2925
      
sl@0
  2926
  message = pop_message_waiting_for_memory (connection);
sl@0
  2927
  if (message == NULL)
sl@0
  2928
    {
sl@0
  2929
      _dbus_warn ("Failed to pop message! Should have been reply from Ping message\n");
sl@0
  2930
      return FALSE;
sl@0
  2931
    }
sl@0
  2932
sl@0
  2933
  if (dbus_message_get_reply_serial (message) != serial)
sl@0
  2934
    {
sl@0
  2935
      _dbus_warn ("Wrong reply serial\n");
sl@0
  2936
      dbus_message_unref (message);
sl@0
  2937
      return FALSE;
sl@0
  2938
    }
sl@0
  2939
sl@0
  2940
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  2941
    {
sl@0
  2942
      _dbus_warn ("Unexpected message return during Ping\n");
sl@0
  2943
      dbus_message_unref (message);
sl@0
  2944
      return FALSE;
sl@0
  2945
    }
sl@0
  2946
sl@0
  2947
  dbus_message_unref (message);
sl@0
  2948
  message = NULL;
sl@0
  2949
      
sl@0
  2950
  return TRUE;
sl@0
  2951
}
sl@0
  2952
sl@0
  2953
/* returns TRUE if the correct thing happens,
sl@0
  2954
 * but the correct thing may include OOM errors.
sl@0
  2955
 */
sl@0
  2956
static dbus_bool_t
sl@0
  2957
check_existent_get_machine_id (BusContext     *context,
sl@0
  2958
                               DBusConnection *connection)
sl@0
  2959
{
sl@0
  2960
  DBusMessage *message;
sl@0
  2961
  dbus_uint32_t serial;
sl@0
  2962
  const char *machine_id;
sl@0
  2963
  
sl@0
  2964
  message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
sl@0
  2965
                                          "/org/freedesktop/TestSuite",
sl@0
  2966
                                          "org.freedesktop.DBus.Peer",
sl@0
  2967
                                          "GetMachineId");
sl@0
  2968
  
sl@0
  2969
  if (message == NULL)
sl@0
  2970
    return TRUE;
sl@0
  2971
sl@0
  2972
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  2973
    {
sl@0
  2974
      dbus_message_unref (message);
sl@0
  2975
      return TRUE;
sl@0
  2976
    }
sl@0
  2977
sl@0
  2978
  dbus_message_unref (message);
sl@0
  2979
  message = NULL;
sl@0
  2980
sl@0
  2981
  bus_test_run_everything (context);
sl@0
  2982
sl@0
  2983
  /* Note: if this test is run in OOM mode, it will block when the bus
sl@0
  2984
   * doesn't send a reply due to OOM.
sl@0
  2985
   */
sl@0
  2986
  block_connection_until_message_from_bus (context, connection, "reply from running GetMachineId");
sl@0
  2987
      
sl@0
  2988
  message = pop_message_waiting_for_memory (connection);
sl@0
  2989
  if (message == NULL)
sl@0
  2990
    {
sl@0
  2991
      _dbus_warn ("Failed to pop message! Should have been reply from GetMachineId message\n");
sl@0
  2992
      return FALSE;
sl@0
  2993
    }
sl@0
  2994
sl@0
  2995
  if (dbus_message_get_reply_serial (message) != serial)
sl@0
  2996
    {
sl@0
  2997
      _dbus_warn ("Wrong reply serial\n");
sl@0
  2998
      dbus_message_unref (message);
sl@0
  2999
      return FALSE;
sl@0
  3000
    }
sl@0
  3001
sl@0
  3002
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  3003
    {
sl@0
  3004
      _dbus_warn ("Unexpected message return during GetMachineId\n");
sl@0
  3005
      dbus_message_unref (message);
sl@0
  3006
      return FALSE;
sl@0
  3007
    }
sl@0
  3008
sl@0
  3009
  machine_id = NULL;
sl@0
  3010
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &machine_id, DBUS_TYPE_INVALID))
sl@0
  3011
    {
sl@0
  3012
      _dbus_warn ("Did not get a machine ID in reply to GetMachineId\n");
sl@0
  3013
      dbus_message_unref (message);
sl@0
  3014
      return FALSE;
sl@0
  3015
    }
sl@0
  3016
sl@0
  3017
  if (machine_id == NULL || strlen (machine_id) != 32)
sl@0
  3018
    {
sl@0
  3019
      _dbus_warn ("Machine id looks bogus: '%s'\n", machine_id ? machine_id : "null");
sl@0
  3020
      dbus_message_unref (message);
sl@0
  3021
      return FALSE;
sl@0
  3022
    }
sl@0
  3023
  
sl@0
  3024
  /* We can't check that the machine id is correct because during make check it is
sl@0
  3025
   * just made up for each process separately
sl@0
  3026
   */
sl@0
  3027
  
sl@0
  3028
  dbus_message_unref (message);
sl@0
  3029
  message = NULL;
sl@0
  3030
      
sl@0
  3031
  return TRUE;
sl@0
  3032
}
sl@0
  3033
sl@0
  3034
/* returns TRUE if the correct thing happens,
sl@0
  3035
 * but the correct thing may include OOM errors.
sl@0
  3036
 */
sl@0
  3037
static dbus_bool_t
sl@0
  3038
check_existent_service_auto_start (BusContext     *context,
sl@0
  3039
                                   DBusConnection *connection)
sl@0
  3040
{
sl@0
  3041
  DBusMessage *message;
sl@0
  3042
  DBusMessage *base_service_message;
sl@0
  3043
  dbus_uint32_t serial;
sl@0
  3044
  dbus_bool_t retval;
sl@0
  3045
  const char *base_service;
sl@0
  3046
  const char *text;
sl@0
  3047
sl@0
  3048
  base_service_message = NULL;
sl@0
  3049
sl@0
  3050
  message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
sl@0
  3051
                                          "/org/freedesktop/TestSuite",
sl@0
  3052
                                          "org.freedesktop.TestSuite",
sl@0
  3053
                                          "Echo");
sl@0
  3054
  
sl@0
  3055
  if (message == NULL)
sl@0
  3056
    return TRUE;
sl@0
  3057
sl@0
  3058
  text = TEST_ECHO_MESSAGE;
sl@0
  3059
  if (!dbus_message_append_args (message,
sl@0
  3060
                                 DBUS_TYPE_STRING, &text,
sl@0
  3061
                                 DBUS_TYPE_INVALID))
sl@0
  3062
    {
sl@0
  3063
      dbus_message_unref (message);
sl@0
  3064
      return TRUE;
sl@0
  3065
    }
sl@0
  3066
sl@0
  3067
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  3068
    {
sl@0
  3069
      dbus_message_unref (message);
sl@0
  3070
      return TRUE;
sl@0
  3071
    }
sl@0
  3072
sl@0
  3073
  dbus_message_unref (message);
sl@0
  3074
  message = NULL;
sl@0
  3075
sl@0
  3076
  bus_test_run_everything (context);
sl@0
  3077
sl@0
  3078
  /* now wait for the message bus to hear back from the activated
sl@0
  3079
   * service.
sl@0
  3080
   */
sl@0
  3081
  block_connection_until_message_from_bus (context, connection, "reply to Echo on existent service");
sl@0
  3082
  bus_test_run_everything (context);
sl@0
  3083
sl@0
  3084
  if (!dbus_connection_get_is_connected (connection))
sl@0
  3085
    {
sl@0
  3086
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  3087
      return TRUE;
sl@0
  3088
    }
sl@0
  3089
sl@0
  3090
  retval = FALSE;
sl@0
  3091
  
sl@0
  3092
  message = pop_message_waiting_for_memory (connection);
sl@0
  3093
  if (message == NULL)
sl@0
  3094
    {
sl@0
  3095
      _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
sl@0
  3096
                  serial, connection);
sl@0
  3097
      goto out;
sl@0
  3098
    }
sl@0
  3099
sl@0
  3100
  verbose_message_received (connection, message);
sl@0
  3101
  _dbus_verbose ("  (after sending %s)\n", "auto start");
sl@0
  3102
sl@0
  3103
  /* we should get zero or two ServiceOwnerChanged signals */
sl@0
  3104
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
sl@0
  3105
    {
sl@0
  3106
      GotServiceInfo message_kind;
sl@0
  3107
sl@0
  3108
      if (!check_base_service_activated (context, connection,
sl@0
  3109
                                         message, &base_service))
sl@0
  3110
        goto out;
sl@0
  3111
sl@0
  3112
      base_service_message = message;
sl@0
  3113
      message = NULL;
sl@0
  3114
sl@0
  3115
      /* We may need to block here for the test service to exit or finish up */
sl@0
  3116
      block_connection_until_message_from_bus (context, connection, "service to exit");
sl@0
  3117
sl@0
  3118
      /* Should get a service creation notification for the activated
sl@0
  3119
       * service name, or a service deletion on the base service name
sl@0
  3120
       */
sl@0
  3121
      message = dbus_connection_borrow_message (connection);
sl@0
  3122
      if (message == NULL)
sl@0
  3123
        {
sl@0
  3124
          _dbus_warn ("No message after auto activation "
sl@0
  3125
                      "(should be a service announcement)\n");
sl@0
  3126
          dbus_connection_return_message (connection, message);
sl@0
  3127
          message = NULL;
sl@0
  3128
          goto out;
sl@0
  3129
        }
sl@0
  3130
sl@0
  3131
      message_kind = check_got_service_info (message);
sl@0
  3132
sl@0
  3133
      dbus_connection_return_message (connection, message);
sl@0
  3134
      message = NULL;
sl@0
  3135
sl@0
  3136
      switch (message_kind) 
sl@0
  3137
        {
sl@0
  3138
        case GOT_SERVICE_CREATED:
sl@0
  3139
          message = pop_message_waiting_for_memory (connection);
sl@0
  3140
          if (message == NULL)
sl@0
  3141
            {
sl@0
  3142
              _dbus_warn ("Failed to pop message we just put back! "
sl@0
  3143
                          "should have been a NameOwnerChanged (creation)\n");
sl@0
  3144
              goto out;
sl@0
  3145
            }
sl@0
  3146
            
sl@0
  3147
          /* Check that ServiceOwnerChanged (creation) was correctly received */
sl@0
  3148
          if (!check_service_auto_activated (context, connection, EXISTENT_SERVICE_NAME,
sl@0
  3149
                                             base_service, message))
sl@0
  3150
            goto out;
sl@0
  3151
          
sl@0
  3152
          dbus_message_unref (message);
sl@0
  3153
          message = NULL;
sl@0
  3154
sl@0
  3155
          break;
sl@0
  3156
sl@0
  3157
        case GOT_SERVICE_DELETED:
sl@0
  3158
          {
sl@0
  3159
            /* The service started up and got a base address, but then
sl@0
  3160
             * failed to register under EXISTENT_SERVICE_NAME
sl@0
  3161
             */
sl@0
  3162
            CheckServiceOwnerChangedData socd;
sl@0
  3163
          
sl@0
  3164
            socd.expected_kind = SERVICE_DELETED;
sl@0
  3165
            socd.expected_service_name = base_service;
sl@0
  3166
            socd.failed = FALSE;
sl@0
  3167
            socd.skip_connection = NULL;
sl@0
  3168
            bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  3169
                                      &socd);
sl@0
  3170
sl@0
  3171
            if (socd.failed)
sl@0
  3172
              goto out;
sl@0
  3173
sl@0
  3174
            break;
sl@0
  3175
          }
sl@0
  3176
sl@0
  3177
        case GOT_ERROR:
sl@0
  3178
        case GOT_SOMETHING_ELSE:
sl@0
  3179
          _dbus_warn ("Unexpected message after auto activation\n");
sl@0
  3180
          goto out;
sl@0
  3181
        }
sl@0
  3182
    }
sl@0
  3183
sl@0
  3184
  /* OK, now we've dealt with ServiceOwnerChanged signals, now should
sl@0
  3185
   * come the method reply (or error) from the initial method call
sl@0
  3186
   */
sl@0
  3187
sl@0
  3188
  /* Note: if this test is run in OOM mode, it will block when the bus
sl@0
  3189
   * doesn't send a reply due to OOM.
sl@0
  3190
   */
sl@0
  3191
  block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
sl@0
  3192
      
sl@0
  3193
  message = pop_message_waiting_for_memory (connection);
sl@0
  3194
  if (message == NULL)
sl@0
  3195
    {
sl@0
  3196
      _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
sl@0
  3197
      goto out;
sl@0
  3198
    }
sl@0
  3199
sl@0
  3200
  if (dbus_message_get_reply_serial (message) != serial)
sl@0
  3201
    {
sl@0
  3202
      _dbus_warn ("Wrong reply serial\n");
sl@0
  3203
      goto out;
sl@0
  3204
    }
sl@0
  3205
sl@0
  3206
  dbus_message_unref (message);
sl@0
  3207
  message = NULL;
sl@0
  3208
sl@0
  3209
  if (!check_existent_ping (context, connection))
sl@0
  3210
    goto out;
sl@0
  3211
sl@0
  3212
  if (!check_existent_get_machine_id (context, connection))
sl@0
  3213
    goto out;
sl@0
  3214
  
sl@0
  3215
  if (!check_existent_hello_from_self (context, connection))
sl@0
  3216
    goto out;
sl@0
  3217
sl@0
  3218
  if (!check_send_exit_to_service (context, connection,
sl@0
  3219
                                   EXISTENT_SERVICE_NAME,
sl@0
  3220
                                   base_service))
sl@0
  3221
    goto out;
sl@0
  3222
  
sl@0
  3223
  retval = TRUE;
sl@0
  3224
sl@0
  3225
 out:
sl@0
  3226
  if (message)
sl@0
  3227
    dbus_message_unref (message);
sl@0
  3228
sl@0
  3229
  if (base_service_message)
sl@0
  3230
    dbus_message_unref (base_service_message);
sl@0
  3231
sl@0
  3232
  return retval;
sl@0
  3233
}
sl@0
  3234
sl@0
  3235
#define SHELL_FAIL_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceFail"
sl@0
  3236
sl@0
  3237
/* returns TRUE if the correct thing happens,
sl@0
  3238
 * but the correct thing may include OOM errors.
sl@0
  3239
 */
sl@0
  3240
static dbus_bool_t
sl@0
  3241
check_shell_fail_service_auto_start (BusContext     *context,
sl@0
  3242
                                     DBusConnection *connection)
sl@0
  3243
{
sl@0
  3244
  DBusMessage *message;
sl@0
  3245
  dbus_uint32_t serial;
sl@0
  3246
  dbus_bool_t retval;
sl@0
  3247
sl@0
  3248
  message = dbus_message_new_method_call (SHELL_FAIL_SERVICE_NAME,
sl@0
  3249
                                          "/org/freedesktop/TestSuite",
sl@0
  3250
                                          "org.freedesktop.TestSuite",
sl@0
  3251
                                          "Echo");
sl@0
  3252
  
sl@0
  3253
  if (message == NULL)
sl@0
  3254
    return TRUE;
sl@0
  3255
  
sl@0
  3256
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  3257
    {
sl@0
  3258
      dbus_message_unref (message);
sl@0
  3259
      return TRUE;
sl@0
  3260
    }
sl@0
  3261
sl@0
  3262
  dbus_message_unref (message);
sl@0
  3263
  message = NULL;
sl@0
  3264
sl@0
  3265
  bus_test_run_everything (context);
sl@0
  3266
  block_connection_until_message_from_bus (context, connection, "reply to shell Echo on service which should fail to auto-start");
sl@0
  3267
  bus_test_run_everything (context);
sl@0
  3268
sl@0
  3269
  if (!dbus_connection_get_is_connected (connection))
sl@0
  3270
    {
sl@0
  3271
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  3272
      return TRUE;
sl@0
  3273
    }
sl@0
  3274
  
sl@0
  3275
  retval = FALSE;
sl@0
  3276
  
sl@0
  3277
  message = pop_message_waiting_for_memory (connection);
sl@0
  3278
  if (message == NULL)
sl@0
  3279
    {
sl@0
  3280
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  3281
                  "Echo message (auto activation)", serial, connection);
sl@0
  3282
      goto out;
sl@0
  3283
    }
sl@0
  3284
sl@0
  3285
  verbose_message_received (connection, message);
sl@0
  3286
sl@0
  3287
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  3288
    {
sl@0
  3289
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  3290
        {
sl@0
  3291
          _dbus_warn ("Message has wrong sender %s\n",
sl@0
  3292
                      dbus_message_get_sender (message) ?
sl@0
  3293
                      dbus_message_get_sender (message) : "(none)");
sl@0
  3294
          goto out;
sl@0
  3295
        }
sl@0
  3296
      
sl@0
  3297
      if (dbus_message_is_error (message,
sl@0
  3298
                                 DBUS_ERROR_NO_MEMORY))
sl@0
  3299
        {
sl@0
  3300
          ; /* good, this is a valid response */
sl@0
  3301
        }
sl@0
  3302
      else if (dbus_message_is_error (message,
sl@0
  3303
                                      DBUS_ERROR_INVALID_ARGS))
sl@0
  3304
        {
sl@0
  3305
          _dbus_verbose("got invalid args\n");
sl@0
  3306
          ; /* good, this is expected also */
sl@0
  3307
        }
sl@0
  3308
      else
sl@0
  3309
        {
sl@0
  3310
          warn_unexpected (connection, message, "not this error");
sl@0
  3311
sl@0
  3312
          goto out;
sl@0
  3313
        }
sl@0
  3314
    }
sl@0
  3315
  else
sl@0
  3316
    {
sl@0
  3317
      _dbus_warn ("Did not expect to successfully auto-start shell fail service\n");
sl@0
  3318
      goto out;
sl@0
  3319
    }
sl@0
  3320
sl@0
  3321
  retval = TRUE;
sl@0
  3322
  
sl@0
  3323
 out:
sl@0
  3324
  if (message)
sl@0
  3325
    dbus_message_unref (message);
sl@0
  3326
  
sl@0
  3327
  return retval;
sl@0
  3328
}
sl@0
  3329
sl@0
  3330
#define SHELL_SUCCESS_SERVICE_NAME "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess"
sl@0
  3331
sl@0
  3332
/* returns TRUE if the correct thing happens,
sl@0
  3333
 * but the correct thing may include OOM errors.
sl@0
  3334
 */
sl@0
  3335
static dbus_bool_t
sl@0
  3336
check_shell_service_success_auto_start (BusContext     *context,
sl@0
  3337
                                        DBusConnection *connection)
sl@0
  3338
{
sl@0
  3339
  DBusMessage *message;
sl@0
  3340
  DBusMessage *base_service_message;
sl@0
  3341
  dbus_uint32_t serial;
sl@0
  3342
  dbus_bool_t retval;
sl@0
  3343
  const char *base_service;
sl@0
  3344
  const char *argv[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
sl@0
  3345
sl@0
  3346
  base_service_message = NULL;
sl@0
  3347
sl@0
  3348
  message = dbus_message_new_method_call (SHELL_SUCCESS_SERVICE_NAME,
sl@0
  3349
                                          "/org/freedesktop/TestSuite",
sl@0
  3350
                                          "org.freedesktop.TestSuite",
sl@0
  3351
                                          "Echo");
sl@0
  3352
  
sl@0
  3353
  if (message == NULL)
sl@0
  3354
    return TRUE;
sl@0
  3355
sl@0
  3356
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  3357
    {
sl@0
  3358
      dbus_message_unref (message);
sl@0
  3359
      return TRUE;
sl@0
  3360
    }
sl@0
  3361
sl@0
  3362
  dbus_message_unref (message);
sl@0
  3363
  message = NULL;
sl@0
  3364
sl@0
  3365
  bus_test_run_everything (context);
sl@0
  3366
sl@0
  3367
  /* now wait for the message bus to hear back from the activated
sl@0
  3368
   * service.
sl@0
  3369
   */
sl@0
  3370
  block_connection_until_message_from_bus (context, connection, "reply to Echo on shell success service");
sl@0
  3371
  bus_test_run_everything (context);
sl@0
  3372
sl@0
  3373
  if (!dbus_connection_get_is_connected (connection))
sl@0
  3374
    {
sl@0
  3375
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  3376
      return TRUE;
sl@0
  3377
    }
sl@0
  3378
sl@0
  3379
  retval = FALSE;
sl@0
  3380
  
sl@0
  3381
  message = pop_message_waiting_for_memory (connection);
sl@0
  3382
  if (message == NULL)
sl@0
  3383
    {
sl@0
  3384
      _dbus_warn ("Did not receive any messages after auto start %d on %p\n",
sl@0
  3385
                  serial, connection);
sl@0
  3386
      goto out;
sl@0
  3387
    }
sl@0
  3388
sl@0
  3389
  verbose_message_received (connection, message);
sl@0
  3390
  _dbus_verbose ("  (after sending %s)\n", "auto start");
sl@0
  3391
sl@0
  3392
  /* we should get zero or two ServiceOwnerChanged signals */
sl@0
  3393
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
sl@0
  3394
    {
sl@0
  3395
      GotServiceInfo message_kind;
sl@0
  3396
sl@0
  3397
      if (!check_base_service_activated (context, connection,
sl@0
  3398
                                         message, &base_service))
sl@0
  3399
        goto out;
sl@0
  3400
sl@0
  3401
      base_service_message = message;
sl@0
  3402
      message = NULL;
sl@0
  3403
sl@0
  3404
      /* We may need to block here for the test service to exit or finish up */
sl@0
  3405
      block_connection_until_message_from_bus (context, connection, "service to exit");
sl@0
  3406
sl@0
  3407
      /* Should get a service creation notification for the activated
sl@0
  3408
       * service name, or a service deletion on the base service name
sl@0
  3409
       */
sl@0
  3410
      message = dbus_connection_borrow_message (connection);
sl@0
  3411
      if (message == NULL)
sl@0
  3412
        {
sl@0
  3413
          _dbus_warn ("No message after auto activation "
sl@0
  3414
                      "(should be a service announcement)\n");
sl@0
  3415
          dbus_connection_return_message (connection, message);
sl@0
  3416
          message = NULL;
sl@0
  3417
          goto out;
sl@0
  3418
        }
sl@0
  3419
sl@0
  3420
      message_kind = check_got_service_info (message);
sl@0
  3421
sl@0
  3422
      dbus_connection_return_message (connection, message);
sl@0
  3423
      message = NULL;
sl@0
  3424
sl@0
  3425
      switch (message_kind) 
sl@0
  3426
        {
sl@0
  3427
        case GOT_SERVICE_CREATED:
sl@0
  3428
          message = pop_message_waiting_for_memory (connection);
sl@0
  3429
          if (message == NULL)
sl@0
  3430
            {
sl@0
  3431
              _dbus_warn ("Failed to pop message we just put back! "
sl@0
  3432
                          "should have been a NameOwnerChanged (creation)\n");
sl@0
  3433
              goto out;
sl@0
  3434
            }
sl@0
  3435
            
sl@0
  3436
          /* Check that ServiceOwnerChanged (creation) was correctly received */
sl@0
  3437
          if (!check_service_auto_activated (context, connection, SHELL_SUCCESS_SERVICE_NAME,
sl@0
  3438
                                             base_service, message))
sl@0
  3439
            goto out;
sl@0
  3440
          
sl@0
  3441
          dbus_message_unref (message);
sl@0
  3442
          message = NULL;
sl@0
  3443
sl@0
  3444
          break;
sl@0
  3445
sl@0
  3446
        case GOT_SERVICE_DELETED:
sl@0
  3447
          {
sl@0
  3448
            /* The service started up and got a base address, but then
sl@0
  3449
             * failed to register under SHELL_SUCCESS_SERVICE_NAME
sl@0
  3450
             */
sl@0
  3451
            CheckServiceOwnerChangedData socd;
sl@0
  3452
          
sl@0
  3453
            socd.expected_kind = SERVICE_DELETED;
sl@0
  3454
            socd.expected_service_name = base_service;
sl@0
  3455
            socd.failed = FALSE;
sl@0
  3456
            socd.skip_connection = NULL;
sl@0
  3457
            bus_test_clients_foreach (check_service_owner_changed_foreach,
sl@0
  3458
                                      &socd);
sl@0
  3459
sl@0
  3460
            if (socd.failed)
sl@0
  3461
              goto out;
sl@0
  3462
sl@0
  3463
            break;
sl@0
  3464
          }
sl@0
  3465
sl@0
  3466
        case GOT_ERROR:
sl@0
  3467
        case GOT_SOMETHING_ELSE:
sl@0
  3468
          _dbus_warn ("Unexpected message after auto activation\n");
sl@0
  3469
          goto out;
sl@0
  3470
        }
sl@0
  3471
    }
sl@0
  3472
sl@0
  3473
  /* OK, now we've dealt with ServiceOwnerChanged signals, now should
sl@0
  3474
   * come the method reply (or error) from the initial method call
sl@0
  3475
   */
sl@0
  3476
sl@0
  3477
  /* Note: if this test is run in OOM mode, it will block when the bus
sl@0
  3478
   * doesn't send a reply due to OOM.
sl@0
  3479
   */
sl@0
  3480
  block_connection_until_message_from_bus (context, connection, "reply from echo message after auto-activation");
sl@0
  3481
      
sl@0
  3482
  message = pop_message_waiting_for_memory (connection);
sl@0
  3483
  if (message == NULL)
sl@0
  3484
    {
sl@0
  3485
      _dbus_warn ("Failed to pop message! Should have been reply from echo message\n");
sl@0
  3486
      goto out;
sl@0
  3487
    }
sl@0
  3488
sl@0
  3489
  if (dbus_message_get_reply_serial (message) != serial)
sl@0
  3490
    {
sl@0
  3491
      _dbus_warn ("Wrong reply serial\n");
sl@0
  3492
      goto out;
sl@0
  3493
    }
sl@0
  3494
sl@0
  3495
  if (!dbus_message_get_args (message, NULL,
sl@0
  3496
                                       DBUS_TYPE_STRING, &argv[0], 
sl@0
  3497
                                       DBUS_TYPE_STRING, &argv[1],
sl@0
  3498
                                       DBUS_TYPE_STRING, &argv[2],
sl@0
  3499
                                       DBUS_TYPE_STRING, &argv[3],
sl@0
  3500
                                       DBUS_TYPE_STRING, &argv[4],
sl@0
  3501
                                       DBUS_TYPE_STRING, &argv[5],
sl@0
  3502
                                       DBUS_TYPE_STRING, &argv[6],
sl@0
  3503
                                       DBUS_TYPE_INVALID))
sl@0
  3504
    {
sl@0
  3505
      _dbus_warn ("Error getting arguments from return\n");
sl@0
  3506
      goto out;
sl@0
  3507
    }
sl@0
  3508
sl@0
  3509
   /* don't worry about arg[0] as it may be different 
sl@0
  3510
      depending on the path to the tests
sl@0
  3511
   */
sl@0
  3512
  if (strcmp("-test", argv[1]) != 0)
sl@0
  3513
    {
sl@0
  3514
      _dbus_warn ("Unexpected argv[1] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3515
                  "-test", argv[1]);
sl@0
  3516
      goto out;
sl@0
  3517
    } 
sl@0
  3518
sl@0
  3519
  if (strcmp("that", argv[2]) != 0)
sl@0
  3520
    {
sl@0
  3521
      _dbus_warn ("Unexpected argv[2] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3522
                   "that", argv[2]);
sl@0
  3523
      goto out;
sl@0
  3524
    } 
sl@0
  3525
sl@0
  3526
  if (strcmp("we get", argv[3]) != 0)
sl@0
  3527
    {
sl@0
  3528
      _dbus_warn ("Unexpected argv[3] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3529
                   "we get", argv[3]);
sl@0
  3530
      goto out;
sl@0
  3531
    } 
sl@0
  3532
   
sl@0
  3533
  if (strcmp("back", argv[4]) != 0)
sl@0
  3534
    {
sl@0
  3535
      _dbus_warn ("Unexpected argv[4] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3536
                   "back", argv[4]);
sl@0
  3537
      goto out;
sl@0
  3538
    } 
sl@0
  3539
sl@0
  3540
  if (strcmp("--what", argv[5]) != 0)
sl@0
  3541
    {
sl@0
  3542
      _dbus_warn ("Unexpected argv[5] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3543
                   "--what", argv[5]);
sl@0
  3544
      goto out;
sl@0
  3545
    } 
sl@0
  3546
sl@0
  3547
  if (strcmp("we put in", argv[6]) != 0)
sl@0
  3548
    {
sl@0
  3549
      _dbus_warn ("Unexpected argv[6] in shell success service test (expected: %s, got: %s)\n", 
sl@0
  3550
                   "we put in", argv[6]);
sl@0
  3551
      goto out;
sl@0
  3552
    } 
sl@0
  3553
sl@0
  3554
  dbus_message_unref (message);
sl@0
  3555
  message = NULL;
sl@0
  3556
      
sl@0
  3557
  if (!check_send_exit_to_service (context, connection,
sl@0
  3558
                                   SHELL_SUCCESS_SERVICE_NAME,
sl@0
  3559
                                   base_service))
sl@0
  3560
    goto out;
sl@0
  3561
  
sl@0
  3562
  retval = TRUE;
sl@0
  3563
sl@0
  3564
 out:
sl@0
  3565
  if (message)
sl@0
  3566
    dbus_message_unref (message);
sl@0
  3567
sl@0
  3568
  if (base_service_message)
sl@0
  3569
    dbus_message_unref (base_service_message);
sl@0
  3570
sl@0
  3571
  return retval;
sl@0
  3572
}
sl@0
  3573
sl@0
  3574
typedef struct
sl@0
  3575
{
sl@0
  3576
  Check1Func func;
sl@0
  3577
  BusContext *context;
sl@0
  3578
} Check1Data;
sl@0
  3579
sl@0
  3580
static dbus_bool_t
sl@0
  3581
check_oom_check1_func (void *data)
sl@0
  3582
{
sl@0
  3583
  Check1Data *d = data;
sl@0
  3584
sl@0
  3585
  if (! (* d->func) (d->context))
sl@0
  3586
    return FALSE;
sl@0
  3587
  
sl@0
  3588
  if (!check_no_leftovers (d->context))
sl@0
  3589
    {
sl@0
  3590
      _dbus_warn ("Messages were left over, should be covered by test suite\n");
sl@0
  3591
      return FALSE;
sl@0
  3592
    }
sl@0
  3593
sl@0
  3594
  return TRUE;
sl@0
  3595
}
sl@0
  3596
sl@0
  3597
static void
sl@0
  3598
check1_try_iterations (BusContext *context,
sl@0
  3599
                       const char *description,
sl@0
  3600
                       Check1Func  func)
sl@0
  3601
{
sl@0
  3602
  Check1Data d;
sl@0
  3603
sl@0
  3604
  d.func = func;
sl@0
  3605
  d.context = context;
sl@0
  3606
sl@0
  3607
  if (!_dbus_test_oom_handling (description, check_oom_check1_func,
sl@0
  3608
                                &d))
sl@0
  3609
    _dbus_assert_not_reached ("test failed");
sl@0
  3610
}
sl@0
  3611
sl@0
  3612
static dbus_bool_t
sl@0
  3613
check_get_services (BusContext     *context,
sl@0
  3614
		    DBusConnection *connection,
sl@0
  3615
		    const char     *method,
sl@0
  3616
		    char         ***services,
sl@0
  3617
		    int            *len)
sl@0
  3618
{
sl@0
  3619
  DBusMessage *message;
sl@0
  3620
  dbus_uint32_t serial;
sl@0
  3621
  dbus_bool_t retval;
sl@0
  3622
  DBusError error;
sl@0
  3623
  char **srvs;
sl@0
  3624
  int l;
sl@0
  3625
sl@0
  3626
  retval = FALSE;
sl@0
  3627
  dbus_error_init (&error);
sl@0
  3628
  message = NULL;
sl@0
  3629
sl@0
  3630
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  3631
					  DBUS_PATH_DBUS,
sl@0
  3632
					  DBUS_INTERFACE_DBUS,
sl@0
  3633
					  method);
sl@0
  3634
sl@0
  3635
  if (message == NULL)
sl@0
  3636
    return TRUE;
sl@0
  3637
sl@0
  3638
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  3639
    {
sl@0
  3640
      dbus_message_unref (message);
sl@0
  3641
      return TRUE;
sl@0
  3642
    }
sl@0
  3643
sl@0
  3644
  /* send our message */
sl@0
  3645
  bus_test_run_clients_loop (SEND_PENDING (connection));
sl@0
  3646
sl@0
  3647
  dbus_message_unref (message);
sl@0
  3648
  message = NULL;
sl@0
  3649
sl@0
  3650
  dbus_connection_ref (connection); /* because we may get disconnected */
sl@0
  3651
  block_connection_until_message_from_bus (context, connection, "reply to ListActivatableNames/ListNames");
sl@0
  3652
sl@0
  3653
  if (!dbus_connection_get_is_connected (connection))
sl@0
  3654
    {
sl@0
  3655
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  3656
sl@0
  3657
      dbus_connection_unref (connection);
sl@0
  3658
sl@0
  3659
      return TRUE;
sl@0
  3660
    }
sl@0
  3661
sl@0
  3662
  dbus_connection_unref (connection);
sl@0
  3663
sl@0
  3664
  message = pop_message_waiting_for_memory (connection);
sl@0
  3665
  if (message == NULL)
sl@0
  3666
    {
sl@0
  3667
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
sl@0
  3668
		  method, serial, connection);
sl@0
  3669
      goto out;
sl@0
  3670
    }
sl@0
  3671
sl@0
  3672
  verbose_message_received (connection, message);
sl@0
  3673
sl@0
  3674
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  3675
    {
sl@0
  3676
      if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
sl@0
  3677
	{
sl@0
  3678
	  ; /* good, this is a valid response */
sl@0
  3679
	}
sl@0
  3680
      else
sl@0
  3681
	{
sl@0
  3682
	  warn_unexpected (connection, message, "not this error");
sl@0
  3683
sl@0
  3684
	  goto out;
sl@0
  3685
	}
sl@0
  3686
    }
sl@0
  3687
  else
sl@0
  3688
    {
sl@0
  3689
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
sl@0
  3690
	{
sl@0
  3691
	  ; /* good, expected */
sl@0
  3692
	}
sl@0
  3693
      else
sl@0
  3694
	{
sl@0
  3695
	  warn_unexpected (connection, message,
sl@0
  3696
			   "method_return for ListActivatableNames/ListNames");
sl@0
  3697
sl@0
  3698
	  goto out;
sl@0
  3699
	}
sl@0
  3700
sl@0
  3701
    retry_get_property:
sl@0
  3702
sl@0
  3703
      if (!dbus_message_get_args (message, &error,
sl@0
  3704
				  DBUS_TYPE_ARRAY,
sl@0
  3705
				  DBUS_TYPE_STRING,
sl@0
  3706
				  &srvs, &l,
sl@0
  3707
				  DBUS_TYPE_INVALID))
sl@0
  3708
	{
sl@0
  3709
	  if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
sl@0
  3710
	    {
sl@0
  3711
	      _dbus_verbose ("no memory to list services by %s\n", method);
sl@0
  3712
	      dbus_error_free (&error);
sl@0
  3713
	      _dbus_wait_for_memory ();
sl@0
  3714
	      goto retry_get_property;
sl@0
  3715
	    }
sl@0
  3716
	  else
sl@0
  3717
	    {
sl@0
  3718
	      _dbus_assert (dbus_error_is_set (&error));
sl@0
  3719
	      _dbus_warn ("Did not get the expected DBUS_TYPE_ARRAY from %s\n", method);
sl@0
  3720
	      goto out;
sl@0
  3721
	    }
sl@0
  3722
	} else {
sl@0
  3723
	  *services = srvs;
sl@0
  3724
	  *len = l;
sl@0
  3725
	}
sl@0
  3726
    }
sl@0
  3727
  
sl@0
  3728
  if (!check_no_leftovers (context))
sl@0
  3729
    goto out;
sl@0
  3730
  
sl@0
  3731
  retval = TRUE;
sl@0
  3732
  
sl@0
  3733
 out:
sl@0
  3734
  dbus_error_free (&error);
sl@0
  3735
  
sl@0
  3736
  if (message)
sl@0
  3737
    dbus_message_unref (message);
sl@0
  3738
sl@0
  3739
  return retval;
sl@0
  3740
}
sl@0
  3741
sl@0
  3742
/* returns TRUE if the correct thing happens,
sl@0
  3743
 * but the correct thing may include OOM errors.
sl@0
  3744
 */
sl@0
  3745
static dbus_bool_t
sl@0
  3746
check_list_services (BusContext     *context,
sl@0
  3747
		     DBusConnection *connection)
sl@0
  3748
{
sl@0
  3749
  DBusMessage  *message;
sl@0
  3750
  DBusMessage  *base_service_message;
sl@0
  3751
  const char   *base_service;
sl@0
  3752
  dbus_uint32_t serial;
sl@0
  3753
  dbus_bool_t   retval;
sl@0
  3754
  const char   *existent = EXISTENT_SERVICE_NAME;
sl@0
  3755
  dbus_uint32_t flags;
sl@0
  3756
  char        **services;
sl@0
  3757
  int           len;
sl@0
  3758
sl@0
  3759
  _dbus_verbose ("check_list_services for %p\n", connection);
sl@0
  3760
sl@0
  3761
  if (!check_get_services (context, connection, "ListActivatableNames", &services, &len))
sl@0
  3762
    {
sl@0
  3763
      return TRUE;
sl@0
  3764
    }
sl@0
  3765
sl@0
  3766
  if (!_dbus_string_array_contains ((const char **)services, existent))
sl@0
  3767
    {
sl@0
  3768
      _dbus_warn ("Did not get the expected %s from ListActivatableNames\n", existent);
sl@0
  3769
      return FALSE;
sl@0
  3770
    }
sl@0
  3771
sl@0
  3772
  dbus_free_string_array (services);
sl@0
  3773
sl@0
  3774
  base_service_message = NULL;
sl@0
  3775
sl@0
  3776
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
sl@0
  3777
					  DBUS_PATH_DBUS,
sl@0
  3778
					  DBUS_INTERFACE_DBUS,
sl@0
  3779
					  "StartServiceByName");
sl@0
  3780
sl@0
  3781
  if (message == NULL)
sl@0
  3782
    return TRUE;
sl@0
  3783
sl@0
  3784
  dbus_message_set_auto_start (message, FALSE);
sl@0
  3785
sl@0
  3786
  flags = 0;
sl@0
  3787
  if (!dbus_message_append_args (message,
sl@0
  3788
				 DBUS_TYPE_STRING, &existent,
sl@0
  3789
				 DBUS_TYPE_UINT32, &flags,
sl@0
  3790
				 DBUS_TYPE_INVALID))
sl@0
  3791
    {
sl@0
  3792
      dbus_message_unref (message);
sl@0
  3793
      return TRUE;
sl@0
  3794
    }
sl@0
  3795
sl@0
  3796
  if (!dbus_connection_send (connection, message, &serial))
sl@0
  3797
    {
sl@0
  3798
      dbus_message_unref (message);
sl@0
  3799
      return TRUE;
sl@0
  3800
    }
sl@0
  3801
sl@0
  3802
  dbus_message_unref (message);
sl@0
  3803
  message = NULL;
sl@0
  3804
sl@0
  3805
  bus_test_run_everything (context);
sl@0
  3806
sl@0
  3807
  /* now wait for the message bus to hear back from the activated
sl@0
  3808
   * service.
sl@0
  3809
   */
sl@0
  3810
  block_connection_until_message_from_bus (context, connection, "activated service to connect");
sl@0
  3811
sl@0
  3812
  bus_test_run_everything (context);
sl@0
  3813
sl@0
  3814
  if (!dbus_connection_get_is_connected (connection))
sl@0
  3815
    {
sl@0
  3816
      _dbus_verbose ("connection was disconnected: %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
sl@0
  3817
      return TRUE;
sl@0
  3818
    }
sl@0
  3819
sl@0
  3820
  retval = FALSE;
sl@0
  3821
sl@0
  3822
  message = pop_message_waiting_for_memory (connection);
sl@0
  3823
  if (message == NULL)
sl@0
  3824
    {
sl@0
  3825
      _dbus_warn ("Did not receive any messages after %s %d on %p\n",
sl@0
  3826
		  "StartServiceByName", serial, connection);
sl@0
  3827
      goto out;
sl@0
  3828
    }
sl@0
  3829
sl@0
  3830
  verbose_message_received (connection, message);
sl@0
  3831
  _dbus_verbose ("  (after sending %s)\n", "StartServiceByName");
sl@0
  3832
sl@0
  3833
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
sl@0
  3834
    {
sl@0
  3835
      if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
sl@0
  3836
	{
sl@0
  3837
	  _dbus_warn ("Message has wrong sender %s\n",
sl@0
  3838
		      dbus_message_get_sender (message) ?
sl@0
  3839
		      dbus_message_get_sender (message) : "(none)");
sl@0
  3840
	  goto out;
sl@0
  3841
	}
sl@0
  3842
sl@0
  3843
      if (dbus_message_is_error (message,
sl@0
  3844
				 DBUS_ERROR_NO_MEMORY))
sl@0
  3845
	{
sl@0
  3846
	  ; /* good, this is a valid response */
sl@0
  3847
	}
sl@0
  3848
      else if (dbus_message_is_error (message,
sl@0
  3849
				      DBUS_ERROR_SPAWN_CHILD_EXITED) ||
sl@0
  3850
	       dbus_message_is_error (message,
sl@0
  3851
				      DBUS_ERROR_SPAWN_CHILD_SIGNALED) ||
sl@0
  3852
	       dbus_message_is_error (message,
sl@0
  3853
				      DBUS_ERROR_SPAWN_EXEC_FAILED))
sl@0
  3854
	{
sl@0
  3855
	  ; /* good, this is expected also */
sl@0
  3856
	}
sl@0
  3857
      else
sl@0
  3858
	{
sl@0
  3859
	  _dbus_warn ("Did not expect error %s\n",
sl@0
  3860
		      dbus_message_get_error_name (message));
sl@0
  3861
	  goto out;
sl@0
  3862
	}
sl@0
  3863
    }
sl@0
  3864
  else
sl@0
  3865
    {
sl@0
  3866
      GotServiceInfo message_kind;
sl@0
  3867
sl@0
  3868
      if (!check_base_service_activated (context, connection,
sl@0
  3869
					 message, &base_service))
sl@0
  3870
	goto out;
sl@0
  3871
sl@0
  3872
      base_service_message = message;
sl@0
  3873
      message = NULL;
sl@0
  3874
sl@0
  3875
      /* We may need to block here for the test service to exit or finish up */
sl@0
  3876
      block_connection_until_message_from_bus (context, connection, "test service to exit or finish up");
sl@0
  3877
sl@0
  3878
      message = dbus_connection_borrow_message (connection);
sl@0
  3879
      if (message == NULL)
sl@0
  3880
	{
sl@0
  3881
	  _dbus_warn ("Did not receive any messages after base service creation notification\n");
sl@0
  3882
	  goto out;
sl@0
  3883
	}
sl@0
  3884
sl@0
  3885
      message_kind = check_got_service_info (message);
sl@0
  3886
sl@0
  3887
      dbus_connection_return_message (connection, message);
sl@0
  3888
      message = NULL;
sl@0
  3889
sl@0
  3890
      switch (message_kind)
sl@0
  3891
	{
sl@0
  3892
	case GOT_SOMETHING_ELSE:
sl@0
  3893
	case GOT_ERROR:
sl@0
  3894
	case GOT_SERVICE_DELETED:
sl@0
  3895
	  _dbus_warn ("Unexpected message after ActivateService "
sl@0
  3896
		      "(should be an error or a service announcement)\n");
sl@0
  3897
	  goto out;
sl@0
  3898
sl@0
  3899
	case GOT_SERVICE_CREATED:
sl@0
  3900
	  message = pop_message_waiting_for_memory (connection);
sl@0
  3901
	  if (message == NULL)
sl@0
  3902
	    {
sl@0
  3903
	      _dbus_warn ("Failed to pop message we just put back! "
sl@0
  3904
			  "should have been a NameOwnerChanged (creation)\n");
sl@0
  3905
	      goto out;
sl@0
  3906
	    }
sl@0
  3907
	  
sl@0
  3908
	  if (!check_service_activated (context, connection, EXISTENT_SERVICE_NAME,
sl@0
  3909
					base_service, message))
sl@0
  3910
	    goto out;
sl@0
  3911
sl@0
  3912
	  dbus_message_unref (message);
sl@0
  3913
	  message = NULL;
sl@0
  3914
sl@0
  3915
	  if (!check_no_leftovers (context))
sl@0
  3916
	    {
sl@0
  3917
	      _dbus_warn ("Messages were left over after successful activation\n");
sl@0
  3918
	      goto out;
sl@0
  3919
	    }
sl@0
  3920
sl@0
  3921
	  break;
sl@0
  3922
	}
sl@0
  3923
    }
sl@0
  3924
  
sl@0
  3925
  if (!check_get_services (context, connection, "ListNames", &services, &len))
sl@0
  3926
    {
sl@0
  3927
      return TRUE;
sl@0
  3928
    }
sl@0
  3929
sl@0
  3930
  if (!_dbus_string_array_contains ((const char **)services, existent))
sl@0
  3931
    {
sl@0
  3932
      _dbus_warn ("Did not get the expected %s from ListNames\n", existent);
sl@0
  3933
      goto out;
sl@0
  3934
    }
sl@0
  3935
sl@0
  3936
  dbus_free_string_array (services);
sl@0
  3937
sl@0
  3938
  if (!check_send_exit_to_service (context, connection,
sl@0
  3939
				   EXISTENT_SERVICE_NAME, base_service))
sl@0
  3940
    goto out;
sl@0
  3941
sl@0
  3942
  retval = TRUE;
sl@0
  3943
sl@0
  3944
 out:
sl@0
  3945
  if (message)
sl@0
  3946
    dbus_message_unref (message);
sl@0
  3947
sl@0
  3948
  if (base_service_message)
sl@0
  3949
    dbus_message_unref (base_service_message);
sl@0
  3950
sl@0
  3951
  return retval;
sl@0
  3952
}
sl@0
  3953
sl@0
  3954
typedef struct
sl@0
  3955
{
sl@0
  3956
  Check2Func func;
sl@0
  3957
  BusContext *context;
sl@0
  3958
  DBusConnection *connection;
sl@0
  3959
} Check2Data;
sl@0
  3960
sl@0
  3961
static dbus_bool_t
sl@0
  3962
check_oom_check2_func (void *data)
sl@0
  3963
{
sl@0
  3964
  Check2Data *d = data;
sl@0
  3965
sl@0
  3966
  if (! (* d->func) (d->context, d->connection))
sl@0
  3967
    return FALSE;
sl@0
  3968
  
sl@0
  3969
  if (!check_no_leftovers (d->context))
sl@0
  3970
    {
sl@0
  3971
      _dbus_warn ("Messages were left over, should be covered by test suite\n");
sl@0
  3972
      return FALSE;
sl@0
  3973
    }
sl@0
  3974
sl@0
  3975
  return TRUE;
sl@0
  3976
}
sl@0
  3977
sl@0
  3978
static void
sl@0
  3979
check2_try_iterations (BusContext     *context,
sl@0
  3980
                       DBusConnection *connection,
sl@0
  3981
                       const char     *description,
sl@0
  3982
                       Check2Func      func)
sl@0
  3983
{
sl@0
  3984
  Check2Data d;
sl@0
  3985
sl@0
  3986
  d.func = func;
sl@0
  3987
  d.context = context;
sl@0
  3988
  d.connection = connection;
sl@0
  3989
  
sl@0
  3990
  if (!_dbus_test_oom_handling (description, check_oom_check2_func,
sl@0
  3991
                                &d))
sl@0
  3992
    {
sl@0
  3993
      _dbus_warn ("%s failed during oom\n", description);
sl@0
  3994
      _dbus_assert_not_reached ("test failed");
sl@0
  3995
    }
sl@0
  3996
}
sl@0
  3997
sl@0
  3998
dbus_bool_t
sl@0
  3999
bus_dispatch_test (const DBusString *test_data_dir)
sl@0
  4000
{
sl@0
  4001
  BusContext *context;
sl@0
  4002
  DBusConnection *foo;
sl@0
  4003
  DBusConnection *bar;
sl@0
  4004
  DBusConnection *baz;
sl@0
  4005
  DBusError error;
sl@0
  4006
sl@0
  4007
  dbus_error_init (&error);
sl@0
  4008
  
sl@0
  4009
  context = bus_context_new_test (test_data_dir,
sl@0
  4010
                                  "valid-config-files/debug-allow-all.conf");
sl@0
  4011
  if (context == NULL)
sl@0
  4012
    return FALSE;
sl@0
  4013
#ifndef __SYMBIAN32__
sl@0
  4014
  foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
sl@0
  4015
#else
sl@0
  4016
  foo = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
sl@0
  4017
#endif  
sl@0
  4018
  if (foo == NULL)
sl@0
  4019
    _dbus_assert_not_reached ("could not alloc connection");
sl@0
  4020
sl@0
  4021
  if (!bus_setup_debug_client (foo))
sl@0
  4022
    _dbus_assert_not_reached ("could not set up connection");
sl@0
  4023
sl@0
  4024
  spin_connection_until_authenticated (context, foo);
sl@0
  4025
  
sl@0
  4026
  if (!check_hello_message (context, foo))
sl@0
  4027
    _dbus_assert_not_reached ("hello message failed");
sl@0
  4028
sl@0
  4029
  if (!check_double_hello_message (context, foo))
sl@0
  4030
    _dbus_assert_not_reached ("double hello message failed");
sl@0
  4031
sl@0
  4032
  if (!check_add_match_all (context, foo))
sl@0
  4033
    _dbus_assert_not_reached ("AddMatch message failed");
sl@0
  4034
  #ifndef __SYMBIAN32__
sl@0
  4035
  bar = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
sl@0
  4036
  #else
sl@0
  4037
  bar = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
sl@0
  4038
#endif  
sl@0
  4039
  if (bar == NULL)
sl@0
  4040
    _dbus_assert_not_reached ("could not alloc connection");
sl@0
  4041
sl@0
  4042
  if (!bus_setup_debug_client (bar))
sl@0
  4043
    _dbus_assert_not_reached ("could not set up connection");
sl@0
  4044
sl@0
  4045
  spin_connection_until_authenticated (context, bar);
sl@0
  4046
  
sl@0
  4047
  if (!check_hello_message (context, bar))
sl@0
  4048
    _dbus_assert_not_reached ("hello message failed");
sl@0
  4049
sl@0
  4050
  if (!check_add_match_all (context, bar))
sl@0
  4051
    _dbus_assert_not_reached ("AddMatch message failed");
sl@0
  4052
#ifndef __SYMBIAN32__
sl@0
  4053
  baz = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
sl@0
  4054
   #else
sl@0
  4055
  baz = dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
sl@0
  4056
#endif 
sl@0
  4057
  if (baz == NULL)
sl@0
  4058
    _dbus_assert_not_reached ("could not alloc connection");
sl@0
  4059
sl@0
  4060
  if (!bus_setup_debug_client (baz))
sl@0
  4061
    _dbus_assert_not_reached ("could not set up connection");
sl@0
  4062
sl@0
  4063
  spin_connection_until_authenticated (context, baz);
sl@0
  4064
  
sl@0
  4065
  if (!check_hello_message (context, baz))
sl@0
  4066
    _dbus_assert_not_reached ("hello message failed");
sl@0
  4067
sl@0
  4068
  if (!check_add_match_all (context, baz))
sl@0
  4069
    _dbus_assert_not_reached ("AddMatch message failed");
sl@0
  4070
sl@0
  4071
  #ifndef __SYMBIAN32__		
sl@0
  4072
  if (!check_get_connection_unix_user (context, baz))
sl@0
  4073
    _dbus_assert_not_reached ("GetConnectionUnixUser message failed");
sl@0
  4074
sl@0
  4075
  if (!check_get_connection_unix_process_id (context, baz))
sl@0
  4076
    _dbus_assert_not_reached ("GetConnectionUnixProcessID message failed");
sl@0
  4077
  #endif
sl@0
  4078
sl@0
  4079
  /* Arun changes : some of the services which are not supported*/ 	
sl@0
  4080
  /*
sl@0
  4081
  if (!check_list_services (context, baz))
sl@0
  4082
    _dbus_assert_not_reached ("ListActivatableNames message failed");
sl@0
  4083
  */
sl@0
  4084
  
sl@0
  4085
  if (!check_no_leftovers (context))
sl@0
  4086
    {
sl@0
  4087
      _dbus_warn ("Messages were left over after setting up initial connections\n");
sl@0
  4088
      _dbus_assert_not_reached ("initial connection setup failed");
sl@0
  4089
    }
sl@0
  4090
  
sl@0
  4091
  check1_try_iterations (context, "create_and_hello",
sl@0
  4092
                         check_hello_connection);
sl@0
  4093
  
sl@0
  4094
  /*Arun changes : some of the services which are not supported*/
sl@0
  4095
  /*
sl@0
  4096
  check2_try_iterations (context, foo, "nonexistent_service_no_auto_start",
sl@0
  4097
                         check_nonexistent_service_no_auto_start);
sl@0
  4098
  */
sl@0
  4099
sl@0
  4100
  check2_try_iterations (context, foo, "segfault_service_no_auto_start",
sl@0
  4101
                         check_segfault_service_no_auto_start);
sl@0
  4102
  
sl@0
  4103
//  shilps check2_try_iterations (context, foo, "existent_service_no_auto_start",
sl@0
  4104
  //                       check_existent_service_no_auto_start);
sl@0
  4105
  
sl@0
  4106
  /*Arun changes : some of the services which are not supported*/
sl@0
  4107
  /*
sl@0
  4108
  check2_try_iterations (context, foo, "nonexistent_service_auto_start",
sl@0
  4109
                         check_nonexistent_service_auto_start);
sl@0
  4110
  
sl@0
  4111
  check2_try_iterations (context, foo, "segfault_service_auto_start",
sl@0
  4112
                         check_segfault_service_auto_start);
sl@0
  4113
  */		
sl@0
  4114
sl@0
  4115
  check2_try_iterations (context, foo, "shell_fail_service_auto_start",
sl@0
  4116
                         check_shell_fail_service_auto_start);
sl@0
  4117
sl@0
  4118
#if 0
sl@0
  4119
  /* Note: need to resolve some issues with the testing code in order to run
sl@0
  4120
   * this in oom (handle that we sometimes don't get replies back from the bus
sl@0
  4121
   * when oom happens, without blocking the test).
sl@0
  4122
   */
sl@0
  4123
  check2_try_iterations (context, foo, "existent_service_auto_auto_start",
sl@0
  4124
                         check_existent_service_auto_start);
sl@0
  4125
#else
sl@0
  4126
// shilps if (!check_existent_service_auto_start (context, foo))
sl@0
  4127
  //  _dbus_assert_not_reached ("existent service auto start failed");
sl@0
  4128
#endif
sl@0
  4129
  
sl@0
  4130
sl@0
  4131
 // shilps if (!check_shell_service_success_auto_start (context, foo))
sl@0
  4132
   // _dbus_assert_not_reached ("shell success service auto start failed");
sl@0
  4133
sl@0
  4134
  _dbus_verbose ("Disconnecting foo, bar, and baz\n");
sl@0
  4135
sl@0
  4136
  kill_client_connection_unchecked (foo);
sl@0
  4137
  kill_client_connection_unchecked (bar);
sl@0
  4138
  kill_client_connection_unchecked (baz);
sl@0
  4139
sl@0
  4140
  bus_context_unref (context);
sl@0
  4141
  
sl@0
  4142
  return TRUE;
sl@0
  4143
}
sl@0
  4144
sl@0
  4145
dbus_bool_t
sl@0
  4146
bus_dispatch_sha1_test (const DBusString *test_data_dir)
sl@0
  4147
{
sl@0
  4148
  BusContext *context;
sl@0
  4149
  DBusConnection *foo;
sl@0
  4150
  DBusError error;
sl@0
  4151
sl@0
  4152
  dbus_error_init (&error);
sl@0
  4153
  
sl@0
  4154
  /* Test SHA1 authentication */
sl@0
  4155
  _dbus_verbose ("Testing SHA1 context\n");
sl@0
  4156
  
sl@0
  4157
  context = bus_context_new_test (test_data_dir,
sl@0
  4158
                                  "valid-config-files/debug-allow-all-sha1.conf");
sl@0
  4159
  if (context == NULL)
sl@0
  4160
    return FALSE;
sl@0
  4161
#ifndef __SYMBIAN32__
sl@0
  4162
  foo = dbus_connection_open_private ("debug-pipe:name=test-server", &error);
sl@0
  4163
#else
sl@0
  4164
  foo = dbus_connection_open_private ("tcp:host=localhost,port=12435", &error);
sl@0
  4165
#endif  
sl@0
  4166
  if (foo == NULL)
sl@0
  4167
    _dbus_assert_not_reached ("could not alloc connection");
sl@0
  4168
sl@0
  4169
  if (!bus_setup_debug_client (foo))
sl@0
  4170
    _dbus_assert_not_reached ("could not set up connection");
sl@0
  4171
sl@0
  4172
  spin_connection_until_authenticated (context, foo);
sl@0
  4173
  
sl@0
  4174
  if (!check_hello_message (context, foo))
sl@0
  4175
    _dbus_assert_not_reached ("hello message failed");
sl@0
  4176
sl@0
  4177
  if (!check_add_match_all (context, foo))
sl@0
  4178
    _dbus_assert_not_reached ("addmatch message failed");
sl@0
  4179
  
sl@0
  4180
  if (!check_no_leftovers (context))
sl@0
  4181
    {
sl@0
  4182
      _dbus_warn ("Messages were left over after setting up initial SHA-1 connection\n");
sl@0
  4183
      _dbus_assert_not_reached ("initial connection setup failed");
sl@0
  4184
    }
sl@0
  4185
  
sl@0
  4186
  check1_try_iterations (context, "create_and_hello_sha1",
sl@0
  4187
                         check_hello_connection);
sl@0
  4188
sl@0
  4189
  kill_client_connection_unchecked (foo);
sl@0
  4190
sl@0
  4191
  bus_context_unref (context);
sl@0
  4192
sl@0
  4193
  return TRUE;
sl@0
  4194
}
sl@0
  4195
sl@0
  4196
#endif /* DBUS_BUILD_TESTS */