dbus/test/name-test Makefile.am, NONE, 1.1 run-test.sh, NONE, 1.1 test-names.c, NONE, 1.1

John Palmieri johnp at freedesktop.org
Tue Nov 22 12:37:02 PST 2005


Update of /cvs/dbus/dbus/test/name-test
In directory gabe:/tmp/cvs-serv18986/test/name-test

Added Files:
	Makefile.am run-test.sh test-names.c 
Log Message:
	* configure.in: Add test/name-test/Makefile to the generated
	Makefile list

	* dbus/dbus-shared.h (#define DBUS_NAME_FLAG_ALLOW_REPLACEMENT):
	New flag which replaces DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
	(#define DBUS_NAME_FLAG_DO_NOT_QUEUE): New flag for specifying
	not to queue an ower if it can't be the primary owner

	* bus/bus.h: Add new internal BusOwner struct

	* bus/driver.c (bus_driver_handle_hello): Send flags (0 for default)
	to bus_registry_ensure and don't set the prohibit_replacement flag
	since they are now set per BusOwner and not per name.
	(bus_driver_handle_list_queued_owners): bus method (ListQueuedOwners) 
	that returns the list of connections in a name's connection queue
	
	* bus/services.c (struct BusService): remove prohibit_replacement field
	(struct BusOwner): new struct for keeping track of queued connections
	and their associated flags for the queue
	(struct BusRegistry): add a BusOwner memory pool
	(bus_registry_new): initialize the BusOwner memory pool
	(bus_registry_unref): free the BusOwner memory pool
	(_bus_service_find_owner_link): new internal method for
	searching the queue for a specific connection
	(bus_owner_set_flags): new method for adding setting the flags on a
	bus owner
	(bus_owner_new): new method that creates a BusOwner object from the
	pool and sets its flags
	(bus_owner_ref, bus_owner_unref): ref counting for BusOwner objects
	(bus_registry_ensure): Add the flags parameter
	(bus_registry_acquire_service): Switch from using raw connections to
	using the BusOwner struct
	Add new state machine for dealing with the new set of flags
	(bus_registry_set_service_context_table, struct OwnershipCancelData, 
	cancel_ownership, free_ownership_cancel_data, 
	add_cancel_ownership_to_transaction, struct OwnershipRestoreData, 
	restore_ownership, free_ownership_restore_data, 
	add_restore_ownership_to_transaction): Switch to using BusOwner 
	instead of raw connections
	(bus_service_add_owner): Add flags parameter
	Switch to using BusOwner instead of raw connections
	Add state machine for dealing with the new set of flags
	(bus_service_swap_owner): Swaps the first and second owners in the
	queue.  Used to make sure proper signals are sent when a service looses 
	or gains primary ownership.  We never insert an owner at the top of the
	queue.  Instead we insert it in the second position and then swap.
	(bus_service_remove_owner): Remove the owner from the queue sending
	out the NameLost and NameOwnerChanged signals if the we were the 
	primary owner
	(bus_service_get_primary_owners_connection): New method that extracts
	the connection from the primary owner
	(bus_service_get_primary_owner): Returns the BusOwner instead of the 
	connection
	(bus_service_get_allow_replacement): Changed from the old 
	bus_service_get_prohibit_replacement method.  Checks the flags of the 
	primary owner and returns if it can be replaced or not
	(bus_service_set_prohibit_replacement): removed
	(bus_service_has_owner): returns TRUE if and owner with
	the specified connection exists in the queue
	
	* dbus/dbus-bus.c (dbus_bus_connection_get_unique_name): New helper
	method that only compiles if tests are enabled.  Allows us to get the 
	unique name of a connection so we can check it against the queue when
	doing regression tests

	* bus/activation.c (bus_activation_send_pending_auto_activate),
	bus/dispatch.c (bus_dispatch),  
	bus/driver.c (bus_driver_handle_get_service_owner, 
	bus_driver_handle_get_connection_unix_user, 
	bus_driver_handle_get_connection_unix_process_id,
	bus_driver_handle_get_connection_selinux_security_context),
	bus/signals.c (connection_is_primary_owner): 
	use bus_service_get_primary_owners_connection instead of
	bus_service_get_primary_owner

	* dbus/dbus-sysdeps.c (_dbus_connect_unix_socket, 
	_dbus_listen_unix_socket): Calculate the length of the socket
	path and use that instead of using a fixed length which was
	causing socket names to contain many trailing Nul bytes.

	* dbus/dbus-glib-lowlevel.h, glib/dbus-gobject.c
	(dbus_g_method_get_sender): New method for extracting the sender 
	from a DBusGMethodInvocation
	(dbus_g_method_return_get_reply): changed name to 
	dbus_g_method_get_reply
	(dbus_g_method_return_send_reply): changed name to
	dbus_g_method_send reply

	* doc/dbus-specification.xml: New docs that describe how the new
	queueing system works and talks about the changes to the how
	we specify socket names

	* glib/examples/example-service.c, 
	glib/examples/example-signal-emitter.c,
	glib/examples/statemachine/statemachine-server.c:
	Changed the RequestName flags to the new system

	* test/name-test/ (test-names.c, run-test.sh, Makefile.am): New
	regression test suite for testing various states of the new
	queueing system



--- NEW FILE: Makefile.am ---
INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) -DDBUS_COMPILATION

## note that TESTS has special meaning (stuff to use in make check)
## so if adding tests not to be run in make check, don't add them to 
## TESTS
if DBUS_BUILD_TESTS
TESTS_ENVIRONMENT=DBUS_TOP_BUILDDIR=$(ABSOLUTE_TOP_BUILDDIR)
TESTS=run-test.sh
else
TESTS=
endif

EXTRA_DIST=run-test.sh 

if DBUS_BUILD_TESTS

## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we
## build even when not doing "make check"
noinst_PROGRAMS=test-names 

test_names_SOURCES=				\
	test-names.c                             

test_names_LDADD=$(top_builddir)/dbus/libdbus-1.la $(top_builddir)/dbus/libdbus-convenience.la
endif


--- NEW FILE: run-test.sh ---
#! /bin/bash

function die()
{
    if ! test -z "$DBUS_SESSION_BUS_PID" ; then
        echo "killing message bus "$DBUS_SESSION_BUS_PID >&2
        kill -9 $DBUS_SESSION_BUS_PID
    fi
    echo $SCRIPTNAME: $* >&2

    rm $DBUS_TOP_BUILDDIR/python/dbus
 
    exit 1
}


SCRIPTNAME=$0
MODE=$1

## so the tests can complain if you fail to use the script to launch them
export DBUS_TEST_NAME_RUN_TEST_SCRIPT=1

# Rerun ourselves with tmp session bus if we're not already
if test -z "$DBUS_TEST_NAME_IN_RUN_TEST"; then
  DBUS_TEST_NAME_IN_RUN_TEST=1
  export DBUS_TEST_NAME_IN_RUN_TEST
  exec $DBUS_TOP_BUILDDIR/tools/run-with-tmp-session-bus.sh $SCRIPTNAME $MODE
fi 
echo "running test-names"
libtool --mode=execute $DEBUG $DBUS_TOP_BUILDDIR/test/name-test/test-names || die "test-client failed"

--- NEW FILE: test-names.c ---
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dbus/dbus.h>
#include <dbus/dbus-connection-internal.h>

#define REMOVE_CONNECTION 0
#define ADD_CONNECTION 1
#define ALLOW_REPLACEMENT DBUS_NAME_FLAG_ALLOW_REPLACEMENT
#define REPLACE_EXISTING DBUS_NAME_FLAG_REPLACE_EXISTING
#define DO_NOT_QUEUE DBUS_NAME_FLAG_DO_NOT_QUEUE

#define PRIMARY_OWNER DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
#define IN_QUEUE DBUS_REQUEST_NAME_REPLY_IN_QUEUE
#define EXISTS DBUS_REQUEST_NAME_REPLY_EXISTS
#define ALREADY_OWNER DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER

#define RELEASED DBUS_RELEASE_NAME_REPLY_RELEASED 
#define NON_EXISTANT DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
#define NOT_OWNER DBUS_RELEASE_NAME_REPLY_NOT_OWNER

#define NUM_CONN 4 
#define TEST_NAME "org.freedesktop.DBus.TestSuite.NameTest"
#define NUM_TRIES_TIL_FAIL 15

extern const char *dbus_bus_connection_get_unique_name (DBusConnection *connection);

typedef struct {
  int command;
  
  int connection_number; 
  dbus_uint32_t flags;

  dbus_uint32_t expected_result;

  int expected_queue[NUM_CONN];
} CommandAndResult;

CommandAndResult test_data[] = { 
        {ADD_CONNECTION, 0, ALLOW_REPLACEMENT | REPLACE_EXISTING, 
	PRIMARY_OWNER, {0,-1,-1,-1}},
	{ADD_CONNECTION, 0, REPLACE_EXISTING,
	ALREADY_OWNER, {0,-1,-1,-1}},
	{ADD_CONNECTION, 1, ALLOW_REPLACEMENT | REPLACE_EXISTING,
	IN_QUEUE, {0,1,-1,-1}},
	{REMOVE_CONNECTION, 0, 0,
	RELEASED, {1,-1,-1,-1}},
	{ADD_CONNECTION, 0, REPLACE_EXISTING | DO_NOT_QUEUE,
	PRIMARY_OWNER, {0,1,-1,-1}},
	{ADD_CONNECTION, 2, ALLOW_REPLACEMENT,
	IN_QUEUE, {0,1,2,-1}},
	{ADD_CONNECTION, 2, ALLOW_REPLACEMENT | REPLACE_EXISTING,
	IN_QUEUE, {0,2,1,-1}},
	{ADD_CONNECTION, 0, ALLOW_REPLACEMENT | DO_NOT_QUEUE,
	ALREADY_OWNER, {0,2,1,-1}},
	{ADD_CONNECTION, 1, ALLOW_REPLACEMENT | REPLACE_EXISTING,
	PRIMARY_OWNER, {1,2,-1,-1}},
	{ADD_CONNECTION, 0, REPLACE_EXISTING,
	PRIMARY_OWNER, {0,1,2,-1}},
	{ADD_CONNECTION, 2, DO_NOT_QUEUE,
	EXISTS, {0,1,-1,-1}},
	{REMOVE_CONNECTION, 2, 0,
	NOT_OWNER, {0,1,-1,-1}},
	{ADD_CONNECTION, 3, 0,
	IN_QUEUE, {0,1,3,-1}},
	{ADD_CONNECTION, 0, ALLOW_REPLACEMENT,
	ALREADY_OWNER, {0,1,3,-1}},
	{ADD_CONNECTION, 2, ALLOW_REPLACEMENT,
	IN_QUEUE, {0,1,3,2}}
};

static dbus_bool_t
check_connection (DBusConnection *conn, 
                  int iteration, 
                  DBusConnection *uniq_conn[NUM_CONN])
{
  DBusMessage *reply;
  DBusMessage *method;
  DBusError error;
  char **list;
  int len, i;
  const char *name;

  reply = NULL;
  method = NULL;
  list = NULL;

  dbus_error_init (&error);

  name = TEST_NAME;
  method = dbus_message_new_method_call (DBUS_SERVICE_DBUS, 
                                         DBUS_PATH_DBUS,
                                         DBUS_INTERFACE_DBUS,
                                         "ListQueuedOwners");

  if (method == NULL)
    goto out;

  if (!dbus_message_append_args (method, 
                                 DBUS_TYPE_STRING, &name, 
                                 DBUS_TYPE_INVALID))
    {
      fprintf (stderr, "Error appending args\n") ;
      goto out;
    }

  reply = dbus_connection_send_with_reply_and_block (conn,
                                                     method,
                                                     -1,
                                                     &error);

  if (reply == NULL)
    {
      fprintf (stderr, "Error calling ListQueuedOwners: %s\n", error.message);
      dbus_error_free (&error);
      goto out;
    }


 
  if (!dbus_message_get_args (reply, 
                              &error, 
                              DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, 
                              &list, &len,
                              DBUS_TYPE_INVALID))
    {
      fprintf (stderr, "Error getting args: %s\n", error.message);
      dbus_error_free (&error);
      goto out;
    }

  printf ("Iteration %i: ", iteration);

  if (len > NUM_CONN)
    {
      fprintf (stderr, "There are %i connections in the queue,"
                       " we are only expecting up to %i connections!\n",
		       len,
		       NUM_CONN);
      goto out;
    }
  
  for (i = 0; i < len; i++)
    {
      int expected_conn_num;
      const char *expected_uname;

      if (i > 0)
        printf (", ");

      printf ("%s", list[i]);

      expected_conn_num = test_data[iteration].expected_queue[i];

      if (expected_conn_num == -1)
        {
          fprintf (stderr, 
                   "\nDid not expect this last connection" 
                   " to be in the queue!\n");
          goto out;
        }

      expected_uname = 
             dbus_bus_connection_get_unique_name (uniq_conn[expected_conn_num]);

      if (strcmp (list[i], expected_uname) != 0)
        {
          fprintf (stderr, 
                   "\n%s expected but %s is in the queue!\n",
                   expected_uname, 
                   list[i]);

          goto out;
        }
    }
  
  printf ("\n");

  dbus_message_unref (method);
  dbus_message_unref (reply);
  dbus_free_string_array (list);
  return TRUE;
 
 out:
  if (method != NULL)
    dbus_message_unref (method);

  if (reply != NULL)
    dbus_message_unref (reply);

  if (list != NULL)
    dbus_free_string_array (list);
  
  return FALSE;
}

static void
clear_message_queue (DBusConnection *conn)
{
  int tries;
  DBusMessage *msg;

  for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
    {
      _dbus_connection_lock (conn);
      _dbus_connection_do_iteration_unlocked (conn,
                                              DBUS_ITERATION_DO_READING |
                                              DBUS_ITERATION_DO_WRITING |
                                              DBUS_ITERATION_BLOCK,
                                              0);
      _dbus_connection_unlock (conn);
      msg = dbus_connection_pop_message (conn);
   } 
}

static dbus_bool_t
match_acquired_or_lost_signal (DBusConnection *conn, const char *member, const char *name)
{
  int tries;
  DBusMessage *msg;
  const char *interface = "org.freedesktop.DBus";

  for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
    {
      _dbus_connection_lock (conn);
      _dbus_connection_do_iteration_unlocked (conn,
                                              DBUS_ITERATION_DO_READING |
                                              DBUS_ITERATION_DO_WRITING |
                                              DBUS_ITERATION_BLOCK,
                                              0);
      _dbus_connection_unlock (conn);
      msg = dbus_connection_pop_message (conn);
      if (msg != NULL)
        {
          if (dbus_message_is_signal (msg, 
              interface,
              member))
            {
              const char *n;
              DBusError error;
              dbus_error_init (&error);

              dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID);

              if (dbus_error_is_set (&error))
                {
                  fprintf (stderr, "Error getting args: %s\n", error.message);
                  dbus_error_free (&error);
                  dbus_message_unref (msg);
                  return FALSE;
                }

              if (strcmp (n, name) == 0)
                {
                  dbus_message_unref (msg); 
                  break;
                }
            }
          dbus_message_unref (msg);
        }
    }

  if (tries == NUM_TRIES_TIL_FAIL)
    {
      fprintf (stderr, "Did not recive the expected %s.%s signal!!!\n", interface, member);
      return FALSE;
    }
  
  return TRUE;
}

static dbus_bool_t
match_name_owner_changed_signal (DBusConnection *conn, const char *bus_name, const char *lost_name, const char *acquired_name)
{
  int tries;
  DBusMessage *msg;
 
  for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
    {
      _dbus_connection_lock (conn);
      _dbus_connection_do_iteration_unlocked (conn,
                                              DBUS_ITERATION_DO_READING |
                                              DBUS_ITERATION_DO_WRITING |
                                              DBUS_ITERATION_BLOCK,
                                              0);
      _dbus_connection_unlock (conn);
      msg = dbus_connection_pop_message (conn);
    
      if (msg != NULL)
        {
          if (dbus_message_is_signal (msg, 
              "org.freedesktop.DBus",
              "NameOwnerChanged"))
            {
              const char *n;
              const char *ln;
              const char *an;
              DBusError error;
              dbus_error_init (&error);

              dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_STRING, &ln, DBUS_TYPE_STRING, &an, DBUS_TYPE_INVALID);

              if (dbus_error_is_set (&error))
                {
                  fprintf (stderr, "Error getting args: %s\n", error.message);
                  dbus_error_free (&error);
                  dbus_message_unref (msg);
                  return FALSE;
                }

              if (strcmp (n, bus_name) == 0)
                {
                  if ((lost_name == NULL && strcmp (ln, "") == 0)
                        || strcmp (lost_name, ln) == 0)
                    {
                      if ((acquired_name == NULL && strcmp (an, "") == 0)
                            || strcmp (acquired_name, an) == 0)
                        {
                          dbus_message_unref (msg); 
                          break;
                        }
                      else
                        {
                          fprintf (stderr, "Error: name %s was expected to be acquired but we got %s instead\n", acquired_name, an);
                          dbus_message_unref (msg);
                          return FALSE;
                        }
                    }
                  else
                    {
                      fprintf (stderr, "Error: name %s was expected to be lost but we got %s instead\n", lost_name, ln);
                      dbus_message_unref (msg);
                      return FALSE;
                    }
                }
            }
          dbus_message_unref (msg);
        }
    }

  if (tries == NUM_TRIES_TIL_FAIL)
    {
      fprintf (stderr, "Did not recive the expected NameOwnerChanged signal!!!\n");
      return FALSE;
    }
  
  return TRUE;
}


static dbus_bool_t 
check_signals (DBusConnection *monitor,
               int iteration,
               DBusConnection *conn[NUM_CONN])
{
  DBusConnection *lost_conn = NULL;
  DBusConnection *acquired_conn = NULL;
  const char *lost_name;
  const char *acquired_name;
  
  if (iteration == 0)
    {
      int i;
      i = test_data[iteration].expected_queue[0];

      if (i >= 0)
        acquired_conn = conn[i];
    }
  else
    {
      int i;
      i = test_data[iteration - 1].expected_queue[0];

      if (i >= 0)
        lost_conn = conn[i];

      i = test_data[iteration].expected_queue[0];

      if (i >= 0)
        acquired_conn = conn[i];

      if (acquired_conn == lost_conn)
        acquired_conn = lost_conn = NULL;
    }

    lost_name = lost_conn == NULL? NULL : 
                         dbus_bus_connection_get_unique_name (lost_conn);

    acquired_name = acquired_conn == NULL? NULL :
                         dbus_bus_connection_get_unique_name (acquired_conn);

    if (lost_name != NULL)
      if (!match_acquired_or_lost_signal (lost_conn,
                                         "NameLost",
                                         TEST_NAME))
        return FALSE;

    if (acquired_name != NULL)
      if (!match_acquired_or_lost_signal (acquired_conn,
                                         "NameAcquired",
                                         TEST_NAME))
        return FALSE;

    if (acquired_name != NULL || lost_name != NULL)
      if (!match_name_owner_changed_signal (monitor,
                                            TEST_NAME,
                                            lost_name,
                                            acquired_name))
        return FALSE;
    
    return TRUE;
}

int
main (int argc, char *argv[])
{
  DBusConnection *conn[NUM_CONN];
  DBusConnection *monitor;
  DBusError error;
  int i;
  int test_data_len;

  test_data_len = sizeof (test_data) / sizeof (CommandAndResult);
  
  dbus_error_init (&error);

  conn[0] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 0 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }
  
  if (!match_acquired_or_lost_signal (conn[0],
                                "NameAcquired",
                                dbus_bus_connection_get_unique_name (conn[0])))
    return 1;
  
  conn[1] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 1 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[1],
                                "NameAcquired",
                                dbus_bus_connection_get_unique_name (conn[1])))
    return 1;


  conn[2] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 2 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[2],
                                "NameAcquired",
                                dbus_bus_connection_get_unique_name (conn[2])))
    return 1;


  conn[3] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 3 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[3],
                                "NameAcquired",
                                dbus_bus_connection_get_unique_name (conn[3])))
    return 1;


  monitor = dbus_bus_get (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open monitoring connection to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (monitor,
                                "NameAcquired",
                                dbus_bus_connection_get_unique_name (monitor)))
    return 1;

  dbus_bus_add_match (monitor, "", &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to set filter on monitoring connection: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }


  for (i = 0; i < NUM_CONN; i++) 
    dbus_connection_set_exit_on_disconnect (conn[i], FALSE);

  for (i = 0; i < test_data_len; i++)
    {
      dbus_uint32_t result;
      result = 0;

      if (test_data[i].command == ADD_CONNECTION)
        {
          result = dbus_bus_request_name (conn[test_data[i].connection_number], 
                                          TEST_NAME, 
                                          test_data[i].flags,
                                          &error);

          if (dbus_error_is_set (&error))
            {
              fprintf (stderr, "Error on addition in iteration %i: %s\n", i, error.message);
              dbus_error_free (&error);
              return 1;
            }
        } 
      else if (test_data[i].command == REMOVE_CONNECTION)
        {
          result = dbus_bus_release_name (conn[test_data[i].connection_number], 
                                          TEST_NAME, 
                                          &error);  
          if (dbus_error_is_set (&error))
            {
              fprintf (stderr, "*** Failed to remove connection %i in iteration %i: %s\n",
                       test_data[i].connection_number,
                       i,
                       error.message);
              dbus_error_free (&error);
              return 1;
            }
        }
      else
        {
          fprintf (stderr, "Command #%i not a valid command!\n", test_data[i].command);
          return 1;
        }


      if (result != test_data[i].expected_result)
        {
          fprintf (stderr, "Results recived (%i) are not the expected results (%i) in iteration %i\n",
                   result,
                   test_data[i].expected_result,
                   i);
          return 1;
        }

      if (!check_connection (monitor, i, conn))
        {
          fprintf (stderr, "Failed at iteration %i\n", i);
          return 1;
        }

      if (!check_signals (monitor, i, conn))
        {
          fprintf (stderr, "Failed at iteration %i\n", i);
          return 1;
        }
    }

    return 0;
}



More information about the dbus-commit mailing list