Reduce size of message cache
[platform/upstream/dbus.git] / dbus / dbus-message.c
index fc83a07..880c25e 100644 (file)
 #include "dbus-internals.h"
 #include "dbus-marshal-recursive.h"
 #include "dbus-marshal-validate.h"
+#include "dbus-marshal-byteswap.h"
 #include "dbus-marshal-header.h"
-#include "dbus-message.h"
-#include "dbus-message-internal.h"
+#include "dbus-signature.h"
+#include "dbus-message-private.h"
 #include "dbus-object-tree.h"
 #include "dbus-memory.h"
 #include "dbus-list.h"
-#include "dbus-dataslot.h"
+#include "dbus-threads-internal.h"
 #include <string.h>
 
 /**
  * @{
  */
 
-static dbus_bool_t dbus_message_iter_get_args        (DBusMessageIter *iter,
-                                                      DBusError       *error,
-                                                      int              first_arg_type,
-                                                      ...);
-static dbus_bool_t dbus_message_iter_get_args_valist (DBusMessageIter *iter,
-                                                      DBusError       *error,
-                                                      int              first_arg_type,
-                                                      va_list          var_args);
-
 /* Not thread locked, but strictly const/read-only so should be OK
  */
 /** An static string representing an empty signature */
 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str,  "");
 
-/** How many bits are in the changed_stamp used to validate iterators */
-#define CHANGED_STAMP_BITS 21
-
-/**
- * @brief Internals of DBusMessage
- *
- * Object representing a message received from or to be sent to
- * another application. This is an opaque object, all members
- * are private.
- */
-struct DBusMessage
-{
-  DBusAtomic refcount; /**< Reference count */
-
-  DBusHeader header; /**< Header network data and associated cache */
-
-  DBusString body;   /**< Body network data. */
-
-  char byte_order; /**< Message byte order. */
-
-  unsigned int locked : 1; /**< Message being sent, no modifications allowed. */
-
-  DBusList *size_counters;   /**< 0-N DBusCounter used to track message size. */
-  long size_counter_delta;   /**< Size we incremented the size counters by.   */
-
-  dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< Incremented when iterators are invalidated. */
-
-  DBusDataSlotList slot_list;   /**< Data stored by allocated integer ID */
-
-#ifndef DBUS_DISABLE_CHECKS
-  int generation; /**< _dbus_current_generation when message was created */
-#endif
-};
-
 /* these have wacky values to help trap uninitialized iterators;
  * but has to fit in 3 bits
  */
@@ -121,6 +79,57 @@ struct DBusMessageRealIter
   } u; /**< the type writer or reader that does all the work */
 };
 
+static void
+get_const_signature (DBusHeader        *header,
+                     const DBusString **type_str_p,
+                     int               *type_pos_p)
+{
+  if (_dbus_header_get_field_raw (header,
+                                  DBUS_HEADER_FIELD_SIGNATURE,
+                                  type_str_p,
+                                  type_pos_p))
+    {
+      *type_pos_p += 1; /* skip the signature length which is 1 byte */
+    }
+  else
+    {
+      *type_str_p = &_dbus_empty_signature_str;
+      *type_pos_p = 0;
+    }
+}
+
+/**
+ * Swaps the message to compiler byte order if required
+ *
+ * @param message the message
+ */
+static void
+_dbus_message_byteswap (DBusMessage *message)
+{
+  const DBusString *type_str;
+  int type_pos;
+  
+  if (message->byte_order == DBUS_COMPILER_BYTE_ORDER)
+    return;
+
+  _dbus_verbose ("Swapping message into compiler byte order\n");
+  
+  get_const_signature (&message->header, &type_str, &type_pos);
+  
+  _dbus_marshal_byteswap (type_str, type_pos,
+                          message->byte_order,
+                          DBUS_COMPILER_BYTE_ORDER,
+                          &message->body, 0);
+
+  message->byte_order = DBUS_COMPILER_BYTE_ORDER;
+  
+  _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
+}
+
+#define ensure_byte_order(message)                      \
+ if (message->byte_order != DBUS_COMPILER_BYTE_ORDER)   \
+   _dbus_message_byteswap (message)
+
 /**
  * Gets the data to be sent over the network for this message.
  * The header and then the body should be written out.
@@ -295,25 +304,6 @@ set_or_delete_string_field (DBusMessage *message,
                                          &value);
 }
 
-static void
-get_const_signature (DBusHeader        *header,
-                     const DBusString **type_str_p,
-                     int               *type_pos_p)
-{
-  if (_dbus_header_get_field_raw (header,
-                                  DBUS_HEADER_FIELD_SIGNATURE,
-                                  type_str_p,
-                                  type_pos_p))
-    {
-      *type_pos_p += 1; /* skip the signature length which is 1 byte */
-    }
-  else
-    {
-      *type_str_p = &_dbus_empty_signature_str;
-      *type_pos_p = 0;
-    }
-}
-
 #if 0
 /* Probably we don't need to use this */
 /**
@@ -468,6 +458,8 @@ dbus_message_finalize (DBusMessage *message)
   _dbus_header_free (&message->header);
   _dbus_string_free (&message->body);
 
+  _dbus_assert (message->refcount.value == 0);
+  
   dbus_free (message);
 }
 
@@ -518,7 +510,7 @@ dbus_message_finalize (DBusMessage *message)
  */
 
 /** Avoid caching huge messages */
-#define MAX_MESSAGE_SIZE_TO_CACHE _DBUS_ONE_MEGABYTE
+#define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
 
 /** Avoid caching too many messages */
 #define MAX_MESSAGE_CACHE_SIZE    5
@@ -615,7 +607,7 @@ dbus_message_cache_or_finalize (DBusMessage *message)
 {
   dbus_bool_t was_cached;
   int i;
-
+  
   _dbus_assert (message->refcount.value == 0);
 
   /* This calls application code and has to be done first thing
@@ -669,10 +661,15 @@ dbus_message_cache_or_finalize (DBusMessage *message)
   message_cache[i] = message;
   message_cache_count += 1;
   was_cached = TRUE;
+#ifndef DBUS_DISABLE_CHECKS
+  message->in_cache = TRUE;
+#endif
 
  out:
   _DBUS_UNLOCK (message_cache);
 
+  _dbus_assert (message->refcount.value == 0);
+  
   if (!was_cached)
     dbus_message_finalize (message);
 }
@@ -699,10 +696,13 @@ dbus_message_new_empty_header (void)
       message->generation = _dbus_current_generation;
 #endif
     }
-
+  
   message->refcount.value = 1;
   message->byte_order = DBUS_COMPILER_BYTE_ORDER;
   message->locked = FALSE;
+#ifndef DBUS_DISABLE_CHECKS
+  message->in_cache = FALSE;
+#endif
   message->size_counters = NULL;
   message->size_counter_delta = 0;
   message->changed_stamp = 0;
@@ -773,7 +773,7 @@ dbus_message_new (int message_type)
  * that if multiple methods with the given name exist it is undefined
  * which one will be invoked.
   *
- * @param destination service that the message should be sent to or #NULL
+ * @param destination name that the message should be sent to or #NULL
  * @param path object path the message should be sent to
  * @param interface interface to invoke method on
  * @param method method to invoke
@@ -792,7 +792,7 @@ dbus_message_new_method_call (const char *destination,
   _dbus_return_val_if_fail (path != NULL, NULL);
   _dbus_return_val_if_fail (method != NULL, NULL);
   _dbus_return_val_if_fail (destination == NULL ||
-                            _dbus_check_is_valid_service (destination), NULL);
+                            _dbus_check_is_valid_bus_name (destination), NULL);
   _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
   _dbus_return_val_if_fail (interface == NULL ||
                             _dbus_check_is_valid_interface (interface), NULL);
@@ -1079,7 +1079,8 @@ dbus_message_ref (DBusMessage *message)
 
   _dbus_return_val_if_fail (message != NULL, NULL);
   _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
-
+  _dbus_return_val_if_fail (!message->in_cache, NULL);
+  
   old_refcount = _dbus_atomic_inc (&message->refcount);
   _dbus_assert (old_refcount >= 1);
 
@@ -1099,6 +1100,7 @@ dbus_message_unref (DBusMessage *message)
 
   _dbus_return_if_fail (message != NULL);
   _dbus_return_if_fail (message->generation == _dbus_current_generation);
+  _dbus_return_if_fail (!message->in_cache);
 
   old_refcount = _dbus_atomic_dec (&message->refcount);
 
@@ -1140,25 +1142,15 @@ dbus_message_get_type (DBusMessage *message)
  * rather than this function.
  *
  * To append a basic type, specify its type code followed by the
- * value. For example:
+ * address of the value. For example:
  *
  * @code
- * DBUS_TYPE_INT32, 42,
- * DBUS_TYPE_STRING, "Hello World"
- * @endcode
- * or
- * @code
- * dbus_int32_t val = 42;
- * DBUS_TYPE_INT32, val
- * @endcode
  *
- * Be sure that your provided value is the right size. For example, this
- * won't work:
- * @code
- * DBUS_TYPE_INT64, 42
+ * dbus_int32_t v_INT32 = 42;
+ * const char *v_STRING = "Hello World";
+ * DBUS_TYPE_INT32, &v_INT32,
+ * DBUS_TYPE_STRING, &v_STRING,
  * @endcode
- * Because the "42" will be a 32-bit integer. You need to cast to
- * 64-bit.
  *
  * To append an array of fixed-length basic types, pass in the
  * DBUS_TYPE_ARRAY typecode, the element typecode, the address of
@@ -1180,6 +1172,9 @@ dbus_message_get_type (DBusMessage *message)
  * The last argument to this function must be #DBUS_TYPE_INVALID,
  * marking the end of the argument list.
  *
+ * String/signature/path arrays should be passed in as "const char***
+ * address_of_array" and "int n_elements"
+ *
  * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
  *
  * @todo If this fails due to lack of memory, the message is hosed and
@@ -1239,7 +1234,7 @@ dbus_message_append_args_valist (DBusMessage *message,
 
   while (type != DBUS_TYPE_INVALID)
     {
-      if (_dbus_type_is_basic (type))
+      if (dbus_type_is_basic (type))
         {
           const DBusBasicValue *value;
           value = va_arg (var_args, const DBusBasicValue*);
@@ -1252,26 +1247,11 @@ dbus_message_append_args_valist (DBusMessage *message,
       else if (type == DBUS_TYPE_ARRAY)
         {
           int element_type;
-          const DBusBasicValue **value;
-          int n_elements;
           DBusMessageIter array;
           char buf[2];
 
           element_type = va_arg (var_args, int);
-
-#ifndef DBUS_DISABLE_CHECKS
-          if (!_dbus_type_is_fixed (element_type))
-            {
-              _dbus_warn ("arrays of %s can't be appended with %s for now\n",
-                          _dbus_type_to_string (element_type),
-                          _DBUS_FUNCTION_NAME);
-              goto failed;
-            }
-#endif
-
-          value = va_arg (var_args, const DBusBasicValue**);
-          n_elements = va_arg (var_args, int);
-
+              
           buf[0] = element_type;
           buf[1] = '\0';
           if (!dbus_message_iter_open_container (&iter,
@@ -1279,12 +1259,52 @@ dbus_message_append_args_valist (DBusMessage *message,
                                                  buf,
                                                  &array))
             goto failed;
+          
+          if (dbus_type_is_fixed (element_type))
+            {
+              const DBusBasicValue **value;
+              int n_elements;
+
+              value = va_arg (var_args, const DBusBasicValue**);
+              n_elements = va_arg (var_args, int);
+              
+              if (!dbus_message_iter_append_fixed_array (&array,
+                                                         element_type,
+                                                         value,
+                                                         n_elements))
+                goto failed;
+            }
+          else if (element_type == DBUS_TYPE_STRING ||
+                   element_type == DBUS_TYPE_SIGNATURE ||
+                   element_type == DBUS_TYPE_OBJECT_PATH)
+            {
+              const char ***value_p;
+              const char **value;
+              int n_elements;
+              int i;
+              
+              value_p = va_arg (var_args, const char***);
+              n_elements = va_arg (var_args, int);
 
-          if (!dbus_message_iter_append_fixed_array (&array,
-                                                     element_type,
-                                                     value,
-                                                     n_elements))
-            goto failed;
+              value = *value_p;
+              
+              i = 0;
+              while (i < n_elements)
+                {
+                  if (!dbus_message_iter_append_basic (&array,
+                                                       element_type,
+                                                       &value[i]))
+                    goto failed;
+                  ++i;
+                }
+            }
+          else
+            {
+              _dbus_warn ("arrays of %s can't be appended with %s for now\n",
+                          _dbus_type_to_string (element_type),
+                          _DBUS_FUNCTION_NAME);
+              goto failed;
+            }
 
           if (!dbus_message_iter_close_container (&iter, &array))
             goto failed;
@@ -1318,7 +1338,8 @@ dbus_message_append_args_valist (DBusMessage *message,
  * In addition to those types, arrays of string, object path, and
  * signature are supported; but these are returned as allocated memory
  * and must be freed with dbus_free_string_array(), while the other
- * types are returned as const references.
+ * types are returned as const references. To get a string array
+ * pass in "char ***array_location" and "int *n_elements"
  *
  * The variable argument list should contain the type of the argument
  * followed by a pointer to where the value should be stored. The list
@@ -1380,42 +1401,7 @@ dbus_message_get_args_valist (DBusMessage     *message,
   _dbus_return_val_if_error_is_set (error, FALSE);
 
   dbus_message_iter_init (message, &iter);
-  return dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
-}
-
-/**
- * Reads arguments from a message iterator given a variable argument
- * list. Only arguments of basic type and arrays of fixed-length
- * basic type may be read with this function. See
- * dbus_message_get_args() for more details.
- *
- * @todo this is static for now because there's no corresponding
- * iter_append_args() and I'm not sure we need this function to be
- * public since dbus_message_get_args() is what you usually want
- *
- * @param iter the message iterator
- * @param error error to be filled in on failure
- * @param first_arg_type the first argument type
- * @param ... location for first argument value, then list of type-location pairs
- * @returns #FALSE if the error was set
- */
-static dbus_bool_t
-dbus_message_iter_get_args (DBusMessageIter *iter,
-                           DBusError       *error,
-                           int              first_arg_type,
-                           ...)
-{
-  dbus_bool_t retval;
-  va_list var_args;
-
-  _dbus_return_val_if_fail (iter != NULL, FALSE);
-  _dbus_return_val_if_error_is_set (error, FALSE);
-
-  va_start (var_args, first_arg_type);
-  retval = dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
-  va_end (var_args);
-
-  return retval;
+  return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
 }
 
 static void
@@ -1425,6 +1411,11 @@ _dbus_message_iter_init_common (DBusMessage         *message,
 {
   _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
 
+  /* Since the iterator will read or write who-knows-what from the
+   * message, we need to get in the right byte order
+   */
+  ensure_byte_order (message);
+  
   real->message = message;
   real->changed_stamp = message->changed_stamp;
   real->iter_type = iter_type;
@@ -1461,7 +1452,7 @@ dbus_message_iter_init (DBusMessage     *message,
                           &message->body,
                           0);
 
-  return _dbus_type_reader_has_next (&real->u.reader);
+  return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
 }
 
 #ifndef DBUS_DISABLE_CHECKS
@@ -1481,6 +1472,8 @@ _dbus_message_iter_check (DBusMessageRealIter *iter)
           _dbus_warn ("dbus message changed byte order since iterator was created\n");
           return FALSE;
         }
+      /* because we swap the message into compiler order when you init an iter */
+      _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
     }
   else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
     {
@@ -1489,6 +1482,8 @@ _dbus_message_iter_check (DBusMessageRealIter *iter)
           _dbus_warn ("dbus message changed byte order since append iterator was created\n");
           return FALSE;
         }
+      /* because we swap the message into compiler order when you init an iter */
+      _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
     }
   else
     {
@@ -1618,6 +1613,41 @@ dbus_message_iter_recurse (DBusMessageIter  *iter,
 }
 
 /**
+ * Returns the current signature of a message iterator.  This
+ * is useful primarily for dealing with variants; one can
+ * recurse into a variant and determine the signature of
+ * the variant's value.
+ *
+ * @param iter the message iterator
+ * @returns the contained signature, or NULL if out of memory
+ */
+char *
+dbus_message_iter_get_signature (DBusMessageIter *iter)
+{
+  const DBusString *sig;
+  DBusString retstr;
+  char *ret;
+  int start, len;
+  DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+
+  _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
+
+  if (!_dbus_string_init (&retstr))
+    return NULL;
+
+  _dbus_type_reader_get_signature (&real->u.reader, &sig,
+                                  &start, &len);
+  if (!_dbus_string_append_len (&retstr,
+                               _dbus_string_get_const_data (sig) + start,
+                               len))
+    return NULL;
+  if (!_dbus_string_steal_data (&retstr, &ret))
+    return NULL;
+  _dbus_string_free (&retstr);
+  return ret;
+}
+
+/**
  * Reads a basic-typed value from the message iterator.
  * Basic types are the non-containers such as integer and string.
  *
@@ -1666,11 +1696,30 @@ dbus_message_iter_get_basic (DBusMessageIter  *iter,
 }
 
 /**
+ * Returns the number of elements in the array;
+ *
+ * @param iter the iterator
+ * @returns the number of elements in the array
+ */
+int
+dbus_message_iter_get_array_len (DBusMessageIter *iter)
+{
+  DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
+
+  _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
+
+  return _dbus_type_reader_get_array_length (&real->u.reader);
+}
+
+/**
  * Reads a block of fixed-length values from the message iterator.
  * Fixed-length values are those basic types that are not string-like,
  * such as integers, bool, double. The block read will be from the
  * current position in the array until the end of the array.
  *
+ * This function should only be used if #dbus_type_is_fixed returns
+ * #TRUE for the element type.
+ *
  * The value argument should be the address of a location to store the
  * returned array. So for int32 it should be a "const dbus_int32_t**"
  * The returned value is by reference and should not be freed.
@@ -1688,7 +1737,7 @@ dbus_message_iter_get_fixed_array (DBusMessageIter  *iter,
 
   _dbus_return_if_fail (_dbus_message_iter_check (real));
   _dbus_return_if_fail (value != NULL);
-  _dbus_return_if_fail (_dbus_type_is_fixed (_dbus_type_reader_get_element_type (&real->u.reader)));
+  _dbus_return_if_fail (dbus_type_is_fixed (_dbus_type_reader_get_current_type (&real->u.reader)));
 
   _dbus_type_reader_read_fixed_multi (&real->u.reader,
                                       value, n_elements);
@@ -1700,9 +1749,6 @@ dbus_message_iter_get_fixed_array (DBusMessageIter  *iter,
  * dbus_message_get_args() is the place to go for complete
  * documentation.
  *
- * @todo this is static for now, should be public if
- * dbus_message_iter_get_args_valist() is made public.
- *
  * @see dbus_message_get_args
  * @param iter the message iter
  * @param error error to be filled in
@@ -1710,18 +1756,17 @@ dbus_message_iter_get_fixed_array (DBusMessageIter  *iter,
  * @param var_args return location for first argument, followed by list of type/location pairs
  * @returns #FALSE if error was set
  */
-static dbus_bool_t
-dbus_message_iter_get_args_valist (DBusMessageIter *iter,
-                                  DBusError       *error,
-                                  int              first_arg_type,
-                                  va_list          var_args)
+dbus_bool_t
+_dbus_message_iter_get_args_valist (DBusMessageIter *iter,
+                                    DBusError       *error,
+                                    int              first_arg_type,
+                                    va_list          var_args)
 {
   DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
   int spec_type, msg_type, i;
   dbus_bool_t retval;
 
-  _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
-  _dbus_return_val_if_error_is_set (error, FALSE);
+  _dbus_assert (_dbus_message_iter_check (real));
 
   retval = FALSE;
 
@@ -1743,13 +1788,13 @@ dbus_message_iter_get_args_valist (DBusMessageIter *iter,
           goto out;
        }
 
-      if (_dbus_type_is_basic (spec_type))
+      if (dbus_type_is_basic (spec_type))
         {
           DBusBasicValue *ptr;
 
           ptr = va_arg (var_args, DBusBasicValue*);
 
-          _dbus_return_val_if_fail (ptr != NULL, FALSE);
+          _dbus_assert (ptr != NULL);
 
           _dbus_type_reader_read_basic (&real->u.reader,
                                         ptr);
@@ -1777,13 +1822,13 @@ dbus_message_iter_get_args_valist (DBusMessageIter *iter,
               goto out;
             }
 
-          if (_dbus_type_is_fixed (spec_element_type))
+          if (dbus_type_is_fixed (spec_element_type))
             {
               ptr = va_arg (var_args, const DBusBasicValue**);
               n_elements_p = va_arg (var_args, int*);
 
-              _dbus_return_val_if_fail (ptr != NULL, FALSE);
-              _dbus_return_val_if_fail (n_elements_p != NULL, FALSE);
+              _dbus_assert (ptr != NULL);
+              _dbus_assert (n_elements_p != NULL);
 
               _dbus_type_reader_recurse (&real->u.reader, &array);
 
@@ -1795,26 +1840,26 @@ dbus_message_iter_get_args_valist (DBusMessageIter *iter,
                    spec_element_type == DBUS_TYPE_OBJECT_PATH)
             {
               char ***str_array_p;
-              int i;
+              int n_elements;
               char **str_array;
 
               str_array_p = va_arg (var_args, char***);
               n_elements_p = va_arg (var_args, int*);
 
-              _dbus_return_val_if_fail (str_array_p != NULL, FALSE);
-              _dbus_return_val_if_fail (n_elements_p != NULL, FALSE);
+              _dbus_assert (str_array_p != NULL);
+              _dbus_assert (n_elements_p != NULL);
 
               /* Count elements in the array */
               _dbus_type_reader_recurse (&real->u.reader, &array);
 
-              i = 0;
-              if (_dbus_type_reader_has_next (&array))
+              n_elements = 0;
+              while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
                 {
-                  while (_dbus_type_reader_next (&array))
-                    ++i;
+                  ++n_elements;
+                  _dbus_type_reader_next (&array);
                 }
 
-              str_array = dbus_new0 (char*, i + 1);
+              str_array = dbus_new0 (char*, n_elements + 1);
               if (str_array == NULL)
                 {
                   _DBUS_SET_OOM (error);
@@ -1825,29 +1870,32 @@ dbus_message_iter_get_args_valist (DBusMessageIter *iter,
               _dbus_type_reader_recurse (&real->u.reader, &array);
 
               i = 0;
-              if (_dbus_type_reader_has_next (&array))
+              while (i < n_elements)
                 {
-                  do
+                  const char *s;
+                  _dbus_type_reader_read_basic (&array,
+                                                &s);
+                  
+                  str_array[i] = _dbus_strdup (s);
+                  if (str_array[i] == NULL)
                     {
-                      const char *s;
-                      _dbus_type_reader_read_basic (&array,
-                                                    &s);
-
-                      str_array[i] = _dbus_strdup (s);
-                      if (str_array[i] == NULL)
-                        {
-                          dbus_free_string_array (str_array);
-                          _DBUS_SET_OOM (error);
-                          goto out;
-                        }
-
-                      ++i;
+                      dbus_free_string_array (str_array);
+                      _DBUS_SET_OOM (error);
+                      goto out;
                     }
-                  while (_dbus_type_reader_next (&array));
+                  
+                  ++i;
+                  
+                  if (!_dbus_type_reader_next (&array))
+                    _dbus_assert (i == n_elements);
                 }
 
+              _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
+              _dbus_assert (i == n_elements);
+              _dbus_assert (str_array[i] == NULL);
+
               *str_array_p = str_array;
-              *n_elements_p = i;
+              *n_elements_p = n_elements;
             }
 #ifndef DBUS_DISABLE_CHECKS
           else
@@ -2074,7 +2122,7 @@ dbus_message_iter_append_basic (DBusMessageIter *iter,
 
   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
-  _dbus_return_val_if_fail (_dbus_type_is_basic (type), FALSE);
+  _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
   _dbus_return_val_if_fail (value != NULL, FALSE);
 
   if (!_dbus_message_iter_open_signature (real))
@@ -2134,7 +2182,7 @@ dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
 
   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
-  _dbus_return_val_if_fail (_dbus_type_is_fixed (element_type), FALSE);
+  _dbus_return_val_if_fail (dbus_type_is_fixed (element_type), FALSE);
   _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
   _dbus_return_val_if_fail (value != NULL, FALSE);
   _dbus_return_val_if_fail (n_elements >= 0, FALSE);
@@ -2154,10 +2202,10 @@ dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
  * dbus_message_iter_close_container(). Container types are for
  * example struct, variant, and array. For variants, the
  * contained_signature should be the type of the single value inside
- * the variant. For structs, contained_signature should be #NULL; it
- * will be set to whatever types you write into the struct.  For
- * arrays, contained_signature should be the type of the array
- * elements.
+ * the variant. For structs and dict entries, contained_signature
+ * should be #NULL; it will be set to whatever types you write into
+ * the struct.  For arrays, contained_signature should be the type of
+ * the array elements.
  *
  * @todo If this fails due to lack of memory, the message is hosed and
  * you have to start over building the whole message.
@@ -2180,22 +2228,44 @@ dbus_message_iter_open_container (DBusMessageIter *iter,
 
   _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
   _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
-  _dbus_return_val_if_fail (_dbus_type_is_container (type), FALSE);
+  _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
   _dbus_return_val_if_fail (sub != NULL, FALSE);
   _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
                              contained_signature == NULL) ||
+                            (type == DBUS_TYPE_DICT_ENTRY &&
+                             contained_signature == NULL) ||
                             contained_signature != NULL, FALSE);
+  
+#if 0
+  /* FIXME this would fail if the contained_signature is a dict entry,
+   * since dict entries are invalid signatures standalone (they must be in
+   * an array)
+   */
+  _dbus_return_val_if_fail (contained_signature == NULL ||
+                            _dbus_check_is_valid_signature (contained_signature));
+#endif
 
   if (!_dbus_message_iter_open_signature (real))
     return FALSE;
 
-  _dbus_string_init_const (&contained_str, contained_signature);
-
   *real_sub = *real;
-  return _dbus_type_writer_recurse (&real->u.writer,
-                                    type,
-                                    &contained_str, 0,
-                                    &real_sub->u.writer);
+
+  if (contained_signature != NULL)
+    {
+      _dbus_string_init_const (&contained_str, contained_signature);
+
+      return _dbus_type_writer_recurse (&real->u.writer,
+                                        type,
+                                        &contained_str, 0,
+                                        &real_sub->u.writer);
+    }
+  else
+    {
+      return _dbus_type_writer_recurse (&real->u.writer,
+                                        type,
+                                        NULL, 0,
+                                        &real_sub->u.writer);
+    } 
 }
 
 
@@ -2274,41 +2344,41 @@ dbus_message_get_no_reply (DBusMessage *message)
 }
 
 /**
- * Sets a flag indicating that the addressed service will be
- * auto-activated before the message is delivered. When this flag is
- * set, the message is held until the service is succesfully activated
- * or fails to activate. In case of failure, the reply will be an
- * activation error. If this flag is not set (the default
+ * Sets a flag indicating that an owner for the destination name will
+ * be automatically started before the message is delivered. When this
+ * flag is set, the message is held until a name owner finishes
+ * starting up, or fails to start up. In case of failure, the reply
+ * will be an error.
  *
  * @param message the message
- * @param auto_activation #TRUE if auto-activation is desired
+ * @param auto_start #TRUE if auto-starting is desired
  */
 void
-dbus_message_set_auto_activation (DBusMessage *message,
-                                 dbus_bool_t  auto_activation)
+dbus_message_set_auto_start (DBusMessage *message,
+                             dbus_bool_t  auto_start)
 {
   _dbus_return_if_fail (message != NULL);
   _dbus_return_if_fail (!message->locked);
 
   _dbus_header_toggle_flag (&message->header,
-                            DBUS_HEADER_FLAG_AUTO_ACTIVATION,
-                            auto_activation);
+                            DBUS_HEADER_FLAG_NO_AUTO_START,
+                            !auto_start);
 }
 
 /**
- * Returns #TRUE if the message will cause the addressed service to be
- * auto-activated.
+ * Returns #TRUE if the message will cause an owner for
+ * destination name to be auto-started.
  *
  * @param message the message
- * @returns #TRUE if the message will use auto-activation
+ * @returns #TRUE if the message will use auto-start
  */
 dbus_bool_t
-dbus_message_get_auto_activation (DBusMessage *message)
+dbus_message_get_auto_start (DBusMessage *message)
 {
   _dbus_return_val_if_fail (message != NULL, FALSE);
 
-  return _dbus_header_get_flag (&message->header,
-                                DBUS_HEADER_FLAG_AUTO_ACTIVATION);
+  return !_dbus_header_get_flag (&message->header,
+                                 DBUS_HEADER_FLAG_NO_AUTO_START);
 }
 
 
@@ -2361,6 +2431,36 @@ dbus_message_get_path (DBusMessage   *message)
 }
 
 /**
+ * Checks if the message has a path
+ *
+ * @param message the message
+ * @returns #TRUE if there is a path field in the header
+ */
+dbus_bool_t
+dbus_message_has_path (DBusMessage   *message,
+                       const char    *path)
+{
+  const char *msg_path;
+  msg_path = dbus_message_get_path (message);
+  
+  if (msg_path == NULL)
+    {
+      if (path == NULL)
+        return TRUE;
+      else
+        return FALSE;
+    }
+
+  if (path == NULL)
+    return FALSE;
+   
+  if (strcmp (msg_path, path) == 0)
+    return TRUE;
+
+  return FALSE;
+}
+
+/**
  * Gets the object path this message is being sent to
  * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
  * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
@@ -2451,6 +2551,37 @@ dbus_message_get_interface (DBusMessage *message)
 }
 
 /**
+ * Checks if the message has an interface
+ *
+ * @param message the message
+ * @returns #TRUE if there is a interface field in the header
+ */
+dbus_bool_t
+dbus_message_has_interface (DBusMessage   *message,
+                            const char    *interface)
+{
+  const char *msg_interface;
+  msg_interface = dbus_message_get_interface (message);
+   
+  if (msg_interface == NULL)
+    {
+      if (interface == NULL)
+        return TRUE;
+      else
+        return FALSE;
+    }
+
+  if (interface == NULL)
+    return FALSE;
+     
+  if (strcmp (msg_interface, interface) == 0)
+    return TRUE;
+
+  return FALSE;
+
+}
+
+/**
  * Sets the interface member being invoked
  * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
  * (DBUS_MESSAGE_TYPE_SIGNAL).
@@ -2500,6 +2631,37 @@ dbus_message_get_member (DBusMessage *message)
 }
 
 /**
+ * Checks if the message has an interface member
+ *
+ * @param message the message
+ * @returns #TRUE if there is a member field in the header
+ */
+dbus_bool_t
+dbus_message_has_member (DBusMessage   *message,
+                         const char    *member)
+{
+  const char *msg_member;
+  msg_member = dbus_message_get_member (message);
+  if (msg_member == NULL)
+    {
+      if (member == NULL)
+        return TRUE;
+      else
+        return FALSE;
+    }
+
+  if (member == NULL)
+    return FALSE;
+    
+  if (strcmp (msg_member, member) == 0)
+    return TRUE;
+
+  return FALSE;
+
+}
+
+/**
  * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
  * The name is fully-qualified (namespaced).
  *
@@ -2546,10 +2708,13 @@ dbus_message_get_error_name (DBusMessage *message)
 }
 
 /**
- * Sets the message's destination service.
+ * Sets the message's destination. The destination is the name of
+ * another connection on the bus and may be either the unique name
+ * assigned by the bus to each connection, or a well-known name
+ * specified in advance.
  *
  * @param message the message
- * @param destination the destination service name or #NULL to unset
+ * @param destination the destination name or #NULL to unset
  * @returns #FALSE if not enough memory
  */
 dbus_bool_t
@@ -2559,7 +2724,7 @@ dbus_message_set_destination (DBusMessage  *message,
   _dbus_return_val_if_fail (message != NULL, FALSE);
   _dbus_return_val_if_fail (!message->locked, FALSE);
   _dbus_return_val_if_fail (destination == NULL ||
-                            _dbus_check_is_valid_service (destination),
+                            _dbus_check_is_valid_bus_name (destination),
                             FALSE);
 
   return set_or_delete_string_field (message,
@@ -2569,11 +2734,10 @@ dbus_message_set_destination (DBusMessage  *message,
 }
 
 /**
- * Gets the destination service of a message or #NULL if there is
- * none set.
+ * Gets the destination of a message or #NULL if there is none set.
  *
  * @param message the message
- * @returns the message destination service (should not be freed) or #NULL
+ * @returns the message destination (should not be freed) or #NULL
  */
 const char*
 dbus_message_get_destination (DBusMessage *message)
@@ -2604,7 +2768,7 @@ dbus_message_set_sender (DBusMessage  *message,
   _dbus_return_val_if_fail (message != NULL, FALSE);
   _dbus_return_val_if_fail (!message->locked, FALSE);
   _dbus_return_val_if_fail (sender == NULL ||
-                            _dbus_check_is_valid_service (sender),
+                            _dbus_check_is_valid_bus_name (sender),
                             FALSE);
 
   return set_or_delete_string_field (message,
@@ -2614,11 +2778,12 @@ dbus_message_set_sender (DBusMessage  *message,
 }
 
 /**
- * Gets the service which originated this message,
- * or #NULL if unknown or inapplicable.
+ * Gets the unique name of the connection which originated this
+ * message, or #NULL if unknown or inapplicable. The sender is filled
+ * in by the message bus.
  *
  * @param message the message
- * @returns the service name or #NULL
+ * @returns the unique name of the sender or #NULL
  */
 const char*
 dbus_message_get_sender (DBusMessage *message)
@@ -2790,67 +2955,65 @@ dbus_message_is_error (DBusMessage *message,
 }
 
 /**
- * Checks whether the message was sent to the given service.  If the
- * message has no service specified or has a different name, returns
- * #FALSE.
+ * Checks whether the message was sent to the given name.  If the
+ * message has no destination specified or has a different
+ * destination, returns #FALSE.
  *
  * @param message the message
- * @param service the service to check (must not be #NULL)
+ * @param name the name to check (must not be #NULL)
  *
- * @returns #TRUE if the message has the given destination service
+ * @returns #TRUE if the message has the given destination name
  */
 dbus_bool_t
 dbus_message_has_destination (DBusMessage  *message,
-                              const char   *service)
+                              const char   *name)
 {
   const char *s;
 
   _dbus_return_val_if_fail (message != NULL, FALSE);
-  _dbus_return_val_if_fail (service != NULL, FALSE);
-  /* don't check that service name is valid since it would be expensive,
-   * and not catch many common errors
+  _dbus_return_val_if_fail (name != NULL, FALSE);
+  /* don't check that name is valid since it would be expensive, and
+   * not catch many common errors
    */
 
   s = dbus_message_get_destination (message);
 
-  if (s && strcmp (s, service) == 0)
+  if (s && strcmp (s, name) == 0)
     return TRUE;
   else
     return FALSE;
 }
 
 /**
- * Checks whether the message has the given service as its sender.  If
- * the message has no sender specified or has a different sender,
- * returns #FALSE. Note that if a peer application owns multiple
- * services, its messages will have only one of those services as the
- * sender (usually the base service). So you can't use this
- * function to prove the sender didn't own service Foo, you can
- * only use it to prove that it did.
+ * Checks whether the message has the given unique name as its sender.
+ * If the message has no sender specified or has a different sender,
+ * returns #FALSE. Note that a peer application will always have the
+ * unique name of the connection as the sender. So you can't use this
+ * function to see whether a sender owned a well-known name.
  *
- * @todo this function is probably useless unless we make a hard guarantee
- * that the sender field in messages will always be the base service name
+ * Messages from the bus itself will have #DBUS_SERVICE_DBUS
+ * as the sender.
  *
  * @param message the message
- * @param service the service to check (must not be #NULL)
+ * @param name the name to check (must not be #NULL)
  *
- * @returns #TRUE if the message has the given origin service
+ * @returns #TRUE if the message has the given sender
  */
 dbus_bool_t
 dbus_message_has_sender (DBusMessage  *message,
-                         const char   *service)
+                         const char   *name)
 {
   const char *s;
 
   _dbus_return_val_if_fail (message != NULL, FALSE);
-  _dbus_return_val_if_fail (service != NULL, FALSE);
-  /* don't check that service name is valid since it would be expensive,
-   * and not catch many common errors
+  _dbus_return_val_if_fail (name != NULL, FALSE);
+  /* don't check that name is valid since it would be expensive, and
+   * not catch many common errors
    */
 
   s = dbus_message_get_sender (message);
 
-  if (s && strcmp (s, service) == 0)
+  if (s && strcmp (s, name) == 0)
     return TRUE;
   else
     return FALSE;
@@ -2937,53 +3100,18 @@ dbus_set_error_from_message (DBusError   *error,
  *
  * @{
  */
+
 /**
- * @typedef DBusMessageLoader
- *
- * The DBusMessageLoader object encapsulates the process of converting
- * a byte stream into a series of DBusMessage. It buffers the incoming
- * bytes as efficiently as possible, and generates a queue of
- * messages. DBusMessageLoader is typically used as part of a
- * DBusTransport implementation. The DBusTransport then hands off
- * the loaded messages to a DBusConnection, making the messages
- * visible to the application.
- *
- * @todo write tests for break-loader that a) randomly delete header
- * fields and b) set string fields to zero-length and other funky
- * values.
+ * The initial buffer size of the message loader.
  *
+ * @todo this should be based on min header size plus some average
+ * body size, or something. Or rather, the min header size only, if we
+ * want to try to read only the header, store that in a DBusMessage,
+ * then read only the body and store that, etc., depends on
+ * how we optimize _dbus_message_loader_get_buffer() and what
+ * the exact message format is.
  */
-
-/**
- * Implementation details of DBusMessageLoader.
- * All members are private.
- */
-struct DBusMessageLoader
-{
-  int refcount;        /**< Reference count. */
-
-  DBusString data;     /**< Buffered data */
-
-  DBusList *messages;  /**< Complete messages. */
-
-  long max_message_size; /**< Maximum size of a message */
-
-  unsigned int buffer_outstanding : 1; /**< Someone is using the buffer to read */
-
-  unsigned int corrupted : 1; /**< We got broken data, and are no longer working */
-};
-
-/**
- * The initial buffer size of the message loader.
- *
- * @todo this should be based on min header size plus some average
- * body size, or something. Or rather, the min header size only, if we
- * want to try to read only the header, store that in a DBusMessage,
- * then read only the body and store that, etc., depends on
- * how we optimize _dbus_message_loader_get_buffer() and what
- * the exact message format is.
- */
-#define INITIAL_LOADER_DATA_LEN 32
+#define INITIAL_LOADER_DATA_LEN 32
 
 /**
  * Creates a new message loader. Returns #NULL if memory can't
@@ -2999,13 +3127,14 @@ _dbus_message_loader_new (void)
   loader = dbus_new0 (DBusMessageLoader, 1);
   if (loader == NULL)
     return NULL;
-
+  
   loader->refcount = 1;
 
-  /* Try to cap message size at something that won't *totally* hose
-   * the system if we have a couple of them.
-   */
-  loader->max_message_size = _DBUS_ONE_MEGABYTE * 32;
+  loader->corrupted = FALSE;
+  loader->corruption_reason = DBUS_VALID;
+
+  /* this can be configured by the app, but defaults to the protocol max */
+  loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
 
   if (!_dbus_string_init (&loader->data))
     {
@@ -3085,13 +3214,6 @@ _dbus_message_loader_get_buffer (DBusMessageLoader  *loader,
 }
 
 /**
- * The smallest header size that can occur.  (It won't be valid due to
- * missing required header fields.) This is 4 bytes, two uint32, an
- * array length.
- */
-#define DBUS_MINIMUM_HEADER_SIZE 16
-
-/**
  * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
  * indicating to the loader how many bytes of the buffer were filled
  * in. This function must always be called, even if no bytes were
@@ -3136,7 +3258,7 @@ _dbus_message_loader_return_buffer (DBusMessageLoader  *loader,
  * loader->data and only delete it occasionally, instead of after
  * each message is loaded.
  *
- * load_message() returns FALSE if not enough memory
+ * load_message() returns FALSE if not enough memory OR the loader was corrupted
  */
 static dbus_bool_t
 load_message (DBusMessageLoader *loader,
@@ -3150,7 +3272,10 @@ load_message (DBusMessageLoader *loader,
   DBusValidity validity;
   const DBusString *type_str;
   int type_pos;
+  DBusValidationMode mode;
 
+  mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
+  
   oom = FALSE;
 
 #if 0
@@ -3161,18 +3286,29 @@ load_message (DBusMessageLoader *loader,
   _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
   _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
 
-  if (!_dbus_header_load_untrusted (&message->header,
-                                    &validity,
-                                    byte_order,
-                                    fields_array_len,
-                                    header_len,
-                                    body_len,
-                                    &loader->data, 0,
-                                    _dbus_string_get_length (&loader->data)))
+  if (!_dbus_header_load (&message->header,
+                          mode,
+                          &validity,
+                          byte_order,
+                          fields_array_len,
+                          header_len,
+                          body_len,
+                          &loader->data, 0,
+                          _dbus_string_get_length (&loader->data)))
     {
       _dbus_verbose ("Failed to load header for new message code %d\n", validity);
-      if (validity == DBUS_VALID)
+
+      /* assert here so we can catch any code that still uses DBUS_VALID to indicate
+         oom errors.  They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
+      _dbus_assert (validity != DBUS_VALID);
+
+      if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
         oom = TRUE;
+      else
+        {
+          loader->corrupted = TRUE;
+          loader->corruption_reason = validity;
+        }
       goto failed;
     }
 
@@ -3181,23 +3317,29 @@ load_message (DBusMessageLoader *loader,
   message->byte_order = byte_order;
 
   /* 2. VALIDATE BODY */
+  if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
+    {
+      get_const_signature (&message->header, &type_str, &type_pos);
+      
+      /* Because the bytes_remaining arg is NULL, this validates that the
+       * body is the right length
+       */
+      validity = _dbus_validate_body_with_reason (type_str,
+                                                  type_pos,
+                                                  byte_order,
+                                                  NULL,
+                                                  &loader->data,
+                                                  header_len,
+                                                  body_len);
+      if (validity != DBUS_VALID)
+        {
+          _dbus_verbose ("Failed to validate message body code %d\n", validity);
 
-  get_const_signature (&message->header, &type_str, &type_pos);
-
-  /* Because the bytes_remaining arg is NULL, this validates that the
-   * body is the right length
-   */
-  validity = _dbus_validate_body_with_reason (type_str,
-                                              type_pos,
-                                              byte_order,
-                                              NULL,
-                                              &loader->data,
-                                              header_len,
-                                              body_len);
-  if (validity != DBUS_VALID)
-    {
-      _dbus_verbose ("Failed to validate message body code %d\n", validity);
-      goto failed;
+          loader->corrupted = TRUE;
+          loader->corruption_reason = validity;
+          
+          goto failed;
+        }
     }
 
   /* 3. COPY OVER BODY AND QUEUE MESSAGE */
@@ -3229,6 +3371,8 @@ load_message (DBusMessageLoader *loader,
 
   _dbus_assert (!oom);
   _dbus_assert (!loader->corrupted);
+  _dbus_assert (loader->messages != NULL);
+  _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
 
   return TRUE;
 
@@ -3238,13 +3382,15 @@ load_message (DBusMessageLoader *loader,
 
   /* does nothing if the message isn't in the list */
   _dbus_list_remove_last (&loader->messages, message);
-
-  if (!oom)
-    loader->corrupted = TRUE;
+  
+  if (oom)
+    _dbus_assert (!loader->corrupted);
+  else
+    _dbus_assert (loader->corrupted);
 
   _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
 
-  return !oom;
+  return FALSE;
 }
 
 /**
@@ -3292,15 +3438,24 @@ _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
                              header_len, body_len))
             {
               dbus_message_unref (message);
-              return FALSE;
+              /* load_message() returns false if corrupted or OOM; if
+               * corrupted then return TRUE for not OOM
+               */
+              return loader->corrupted;
             }
+
+          _dbus_assert (loader->messages != NULL);
+          _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
        }
       else
         {
           _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
                          validity);
           if (validity != DBUS_VALID)
-            loader->corrupted = TRUE;
+            {
+              loader->corrupted = TRUE;
+              loader->corruption_reason = validity;
+            }
           return TRUE;
         }
     }
@@ -3377,6 +3532,8 @@ _dbus_message_loader_putback_message_link (DBusMessageLoader  *loader,
 dbus_bool_t
 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
 {
+  _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
+                (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
   return loader->corrupted;
 }
 
@@ -3579,1240 +3736,5 @@ dbus_message_type_to_string (int type)
 }
 
 /** @} */
-#ifdef DBUS_BUILD_TESTS
-#include "dbus-test.h"
-#include <stdio.h>
-#include <stdlib.h>
-
-static dbus_bool_t
-check_have_valid_message (DBusMessageLoader *loader)
-{
-  DBusMessage *message;
-  dbus_bool_t retval;
-
-  message = NULL;
-  retval = FALSE;
-
-  if (!_dbus_message_loader_queue_messages (loader))
-    _dbus_assert_not_reached ("no memory to queue messages");
-
-  if (_dbus_message_loader_get_is_corrupted (loader))
-    {
-      _dbus_warn ("loader corrupted on message that was expected to be valid\n");
-      goto failed;
-    }
-
-  message = _dbus_message_loader_pop_message (loader);
-  if (message == NULL)
-    {
-      _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
-      goto failed;
-    }
-
-  if (_dbus_string_get_length (&loader->data) > 0)
-    {
-      _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
-      goto failed;
-    }
-
-#if 0
-  /* FIXME */
-  /* Verify that we're able to properly deal with the message.
-   * For example, this would detect improper handling of messages
-   * in nonstandard byte order.
-   */
-  if (!check_message_handling (message))
-    goto failed;
-#endif
-
-  retval = TRUE;
-
- failed:
-  if (message)
-    dbus_message_unref (message);
-
-  return retval;
-}
-
-static dbus_bool_t
-check_invalid_message (DBusMessageLoader *loader)
-{
-  dbus_bool_t retval;
-
-  retval = FALSE;
-
-  if (!_dbus_message_loader_queue_messages (loader))
-    _dbus_assert_not_reached ("no memory to queue messages");
-
-  if (!_dbus_message_loader_get_is_corrupted (loader))
-    {
-      _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
-      goto failed;
-    }
-
-  retval = TRUE;
-
- failed:
-  return retval;
-}
-
-static dbus_bool_t
-check_incomplete_message (DBusMessageLoader *loader)
-{
-  DBusMessage *message;
-  dbus_bool_t retval;
-
-  message = NULL;
-  retval = FALSE;
-
-  if (!_dbus_message_loader_queue_messages (loader))
-    _dbus_assert_not_reached ("no memory to queue messages");
-
-  if (_dbus_message_loader_get_is_corrupted (loader))
-    {
-      _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete)\n");
-      goto failed;
-    }
-
-  message = _dbus_message_loader_pop_message (loader);
-  if (message != NULL)
-    {
-      _dbus_warn ("loaded message that was expected to be incomplete\n");
-      goto failed;
-    }
-
-  retval = TRUE;
-
- failed:
-  if (message)
-    dbus_message_unref (message);
-  return retval;
-}
-
-static dbus_bool_t
-check_loader_results (DBusMessageLoader      *loader,
-                      DBusMessageValidity     validity)
-{
-  if (!_dbus_message_loader_queue_messages (loader))
-    _dbus_assert_not_reached ("no memory to queue messages");
-
-  switch (validity)
-    {
-    case _DBUS_MESSAGE_VALID:
-      return check_have_valid_message (loader);
-    case _DBUS_MESSAGE_INVALID:
-      return check_invalid_message (loader);
-    case _DBUS_MESSAGE_INCOMPLETE:
-      return check_incomplete_message (loader);
-    case _DBUS_MESSAGE_UNKNOWN:
-      return TRUE;
-    }
-
-  _dbus_assert_not_reached ("bad DBusMessageValidity");
-  return FALSE;
-}
-
-
-/**
- * Loads the message in the given message file.
- *
- * @param filename filename to load
- * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
- * @param data string to load message into
- * @returns #TRUE if the message was loaded
- */
-dbus_bool_t
-dbus_internal_do_not_use_load_message_file (const DBusString    *filename,
-                                            dbus_bool_t          is_raw,
-                                            DBusString          *data)
-{
-  dbus_bool_t retval;
-
-  retval = FALSE;
-
-  if (is_raw)
-    {
-      DBusError error;
-
-      _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
-      dbus_error_init (&error);
-      if (!_dbus_file_get_contents (data, filename, &error))
-        {
-          _dbus_warn ("Could not load message file %s: %s\n",
-                      _dbus_string_get_const_data (filename),
-                      error.message);
-          dbus_error_free (&error);
-          goto failed;
-        }
-    }
-  else
-    {
-      if (FALSE) /* Message builder disabled, probably permanently,
-                  * I want to do it another way
-                  */
-        {
-          _dbus_warn ("Could not load message file %s\n",
-                      _dbus_string_get_const_data (filename));
-          goto failed;
-        }
-    }
-
-  retval = TRUE;
-
- failed:
-
-  return retval;
-}
-
-/**
- * Tries loading the message in the given message file
- * and verifies that DBusMessageLoader can handle it.
- *
- * @param filename filename to load
- * @param is_raw if #TRUE load as binary data, if #FALSE as message builder language
- * @param expected_validity what the message has to be like to return #TRUE
- * @returns #TRUE if the message has the expected validity
- */
-dbus_bool_t
-dbus_internal_do_not_use_try_message_file (const DBusString    *filename,
-                                           dbus_bool_t          is_raw,
-                                           DBusMessageValidity  expected_validity)
-{
-  DBusString data;
-  dbus_bool_t retval;
-
-  retval = FALSE;
-
-  if (!_dbus_string_init (&data))
-    _dbus_assert_not_reached ("could not allocate string\n");
-
-  if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
-                                                   &data))
-    goto failed;
-
-  retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
-
- failed:
-
-  if (!retval)
-    {
-      if (_dbus_string_get_length (&data) > 0)
-        _dbus_verbose_bytes_of_string (&data, 0,
-                                       _dbus_string_get_length (&data));
-
-      _dbus_warn ("Failed message loader test on %s\n",
-                  _dbus_string_get_const_data (filename));
-    }
-
-  _dbus_string_free (&data);
-
-  return retval;
-}
-
-/**
- * Tries loading the given message data.
- *
- *
- * @param data the message data
- * @param expected_validity what the message has to be like to return #TRUE
- * @returns #TRUE if the message has the expected validity
- */
-dbus_bool_t
-dbus_internal_do_not_use_try_message_data (const DBusString    *data,
-                                           DBusMessageValidity  expected_validity)
-{
-  DBusMessageLoader *loader;
-  dbus_bool_t retval;
-  int len;
-  int i;
-
-  loader = NULL;
-  retval = FALSE;
-
-  /* Write the data one byte at a time */
-
-  loader = _dbus_message_loader_new ();
-
-  /* check some trivial loader functions */
-  _dbus_message_loader_ref (loader);
-  _dbus_message_loader_unref (loader);
-  _dbus_message_loader_get_max_message_size (loader);
-
-  len = _dbus_string_get_length (data);
-  for (i = 0; i < len; i++)
-    {
-      DBusString *buffer;
-
-      _dbus_message_loader_get_buffer (loader, &buffer);
-      _dbus_string_append_byte (buffer,
-                                _dbus_string_get_byte (data, i));
-      _dbus_message_loader_return_buffer (loader, buffer, 1);
-    }
-
-  if (!check_loader_results (loader, expected_validity))
-    goto failed;
-
-  _dbus_message_loader_unref (loader);
-  loader = NULL;
-
-  /* Write the data all at once */
-
-  loader = _dbus_message_loader_new ();
-
-  {
-    DBusString *buffer;
-
-    _dbus_message_loader_get_buffer (loader, &buffer);
-    _dbus_string_copy (data, 0, buffer,
-                       _dbus_string_get_length (buffer));
-    _dbus_message_loader_return_buffer (loader, buffer, 1);
-  }
-
-  if (!check_loader_results (loader, expected_validity))
-    goto failed;
-
-  _dbus_message_loader_unref (loader);
-  loader = NULL;
-
-  /* Write the data 2 bytes at a time */
-
-  loader = _dbus_message_loader_new ();
-
-  len = _dbus_string_get_length (data);
-  for (i = 0; i < len; i += 2)
-    {
-      DBusString *buffer;
-
-      _dbus_message_loader_get_buffer (loader, &buffer);
-      _dbus_string_append_byte (buffer,
-                                _dbus_string_get_byte (data, i));
-      if ((i+1) < len)
-        _dbus_string_append_byte (buffer,
-                                  _dbus_string_get_byte (data, i+1));
-      _dbus_message_loader_return_buffer (loader, buffer, 1);
-    }
-
-  if (!check_loader_results (loader, expected_validity))
-    goto failed;
-
-  _dbus_message_loader_unref (loader);
-  loader = NULL;
-
-  retval = TRUE;
-
- failed:
-
-  if (loader)
-    _dbus_message_loader_unref (loader);
-
-  return retval;
-}
-
-static dbus_bool_t
-process_test_subdir (const DBusString          *test_base_dir,
-                     const char                *subdir,
-                     DBusMessageValidity        validity,
-                     DBusForeachMessageFileFunc function,
-                     void                      *user_data)
-{
-  DBusString test_directory;
-  DBusString filename;
-  DBusDirIter *dir;
-  dbus_bool_t retval;
-  DBusError error;
-
-  retval = FALSE;
-  dir = NULL;
-
-  if (!_dbus_string_init (&test_directory))
-    _dbus_assert_not_reached ("didn't allocate test_directory\n");
-
-  _dbus_string_init_const (&filename, subdir);
-
-  if (!_dbus_string_copy (test_base_dir, 0,
-                          &test_directory, 0))
-    _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
-
-  if (!_dbus_concat_dir_and_file (&test_directory, &filename))
-    _dbus_assert_not_reached ("couldn't allocate full path");
-
-  _dbus_string_free (&filename);
-  if (!_dbus_string_init (&filename))
-    _dbus_assert_not_reached ("didn't allocate filename string\n");
-
-  dbus_error_init (&error);
-  dir = _dbus_directory_open (&test_directory, &error);
-  if (dir == NULL)
-    {
-      _dbus_warn ("Could not open %s: %s\n",
-                  _dbus_string_get_const_data (&test_directory),
-                  error.message);
-      dbus_error_free (&error);
-      goto failed;
-    }
-
-  printf ("Testing %s:\n", subdir);
-
- next:
-  while (_dbus_directory_get_next_file (dir, &filename, &error))
-    {
-      DBusString full_path;
-      dbus_bool_t is_raw;
-
-      if (!_dbus_string_init (&full_path))
-        _dbus_assert_not_reached ("couldn't init string");
-
-      if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
-        _dbus_assert_not_reached ("couldn't copy dir to full_path");
-
-      if (!_dbus_concat_dir_and_file (&full_path, &filename))
-        _dbus_assert_not_reached ("couldn't concat file to dir");
-
-      if (_dbus_string_ends_with_c_str (&filename, ".message"))
-        is_raw = FALSE;
-      else if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
-        is_raw = TRUE;
-      else
-        {
-          _dbus_verbose ("Skipping non-.message file %s\n",
-                         _dbus_string_get_const_data (&filename));
-         _dbus_string_free (&full_path);
-          goto next;
-        }
-
-      printf ("    %s\n",
-              _dbus_string_get_const_data (&filename));
-
-      _dbus_verbose (" expecting %s for %s\n",
-                     validity == _DBUS_MESSAGE_VALID ? "valid" :
-                     (validity == _DBUS_MESSAGE_INVALID ? "invalid" :
-                      (validity == _DBUS_MESSAGE_INCOMPLETE ? "incomplete" : "unknown")),
-                     _dbus_string_get_const_data (&filename));
-
-      if (! (*function) (&full_path, is_raw, validity, user_data))
-        {
-          _dbus_string_free (&full_path);
-          goto failed;
-        }
-      else
-        _dbus_string_free (&full_path);
-    }
-
-  if (dbus_error_is_set (&error))
-    {
-      _dbus_warn ("Could not get next file in %s: %s\n",
-                  _dbus_string_get_const_data (&test_directory),
-                  error.message);
-      dbus_error_free (&error);
-      goto failed;
-    }
-
-  retval = TRUE;
-
- failed:
-
-  if (dir)
-    _dbus_directory_close (dir);
-  _dbus_string_free (&test_directory);
-  _dbus_string_free (&filename);
-
-  return retval;
-}
-
-/**
- * Runs the given function on every message file in the test suite.
- * The function should return #FALSE on test failure or fatal error.
- *
- * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
- * @param func the function to run
- * @param user_data data for function
- * @returns #FALSE if there's a failure
- */
-dbus_bool_t
-dbus_internal_do_not_use_foreach_message_file (const char                *test_data_dir,
-                                               DBusForeachMessageFileFunc func,
-                                               void                      *user_data)
-{
-  DBusString test_directory;
-  dbus_bool_t retval;
-
-  retval = FALSE;
-
-  _dbus_string_init_const (&test_directory, test_data_dir);
-
-  if (!process_test_subdir (&test_directory, "valid-messages",
-                            _DBUS_MESSAGE_VALID, func, user_data))
-    goto failed;
-
-  if (!process_test_subdir (&test_directory, "invalid-messages",
-                            _DBUS_MESSAGE_INVALID, func, user_data))
-    goto failed;
-
-  if (!process_test_subdir (&test_directory, "incomplete-messages",
-                            _DBUS_MESSAGE_INCOMPLETE, func, user_data))
-    goto failed;
-
-  retval = TRUE;
-
- failed:
-
-  _dbus_string_free (&test_directory);
-
-  return retval;
-}
-
-#define GET_AND_CHECK(iter, typename, literal)                                  \
-  do {                                                                          \
-    if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename)         \
-      _dbus_assert_not_reached ("got wrong argument type from message iter");   \
-    dbus_message_iter_get_basic (&iter, &v_##typename);                         \
-    if (v_##typename != literal)                                                \
-      _dbus_assert_not_reached ("got wrong value from message iter");           \
-  } while (0)
-
-#define GET_AND_CHECK_STRCMP(iter, typename, literal)                           \
-  do {                                                                          \
-    if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename)         \
-      _dbus_assert_not_reached ("got wrong argument type from message iter");   \
-    dbus_message_iter_get_basic (&iter, &v_##typename);                         \
-    if (strcmp (v_##typename, literal) != 0)                                    \
-      _dbus_assert_not_reached ("got wrong value from message iter");           \
-  } while (0)
-
-#define GET_AND_CHECK_AND_NEXT(iter, typename, literal)         \
-  do {                                                          \
-    GET_AND_CHECK(iter, typename, literal);                     \
-    if (!dbus_message_iter_next (&iter))                        \
-      _dbus_assert_not_reached ("failed to move iter to next"); \
-  } while (0)
-
-#define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal)  \
-  do {                                                          \
-    GET_AND_CHECK_STRCMP(iter, typename, literal);              \
-    if (!dbus_message_iter_next (&iter))                        \
-      _dbus_assert_not_reached ("failed to move iter to next"); \
-  } while (0)
-
-static void
-message_iter_test (DBusMessage *message)
-{
-  DBusMessageIter iter, array, array2;
-  const char *v_STRING;
-  double v_DOUBLE;
-  dbus_int32_t v_INT32;
-  dbus_uint32_t v_UINT32;
-#ifdef DBUS_HAVE_INT64
-  dbus_int64_t v_INT64;
-  dbus_uint64_t v_UINT64;
-#endif
-  unsigned char v_BYTE;
-  unsigned char v_BOOLEAN;
-
-  const dbus_int32_t *our_int_array;
-  int len;
-
-  dbus_message_iter_init (message, &iter);
-
-  GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
-  GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
-  GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
-  GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
-
-  if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
-    _dbus_assert_not_reached ("Argument type not an array");
-
-  if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
-    _dbus_assert_not_reached ("Array type not double");
-
-  dbus_message_iter_recurse (&iter, &array);
-
-  GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
-  GET_AND_CHECK (array, DOUBLE, 2.5);
-
-  if (dbus_message_iter_next (&array))
-    _dbus_assert_not_reached ("Didn't reach end of array");
-
-  if (!dbus_message_iter_next (&iter))
-    _dbus_assert_not_reached ("Reached end of arguments");
-
-  GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
-
-  if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
-    _dbus_assert_not_reached ("no array");
-
-  if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
-    _dbus_assert_not_reached ("Array type not int32");
-
-  /* Empty array */
-  dbus_message_iter_recurse (&iter, &array);
-
-  if (dbus_message_iter_next (&array))
-    _dbus_assert_not_reached ("Didn't reach end of array");
-
-  if (!dbus_message_iter_next (&iter))
-    _dbus_assert_not_reached ("Reached end of arguments");
-
-  GET_AND_CHECK (iter, BYTE, 0xF0);
-
-  if (dbus_message_iter_next (&iter))
-    _dbus_assert_not_reached ("Didn't reach end of arguments");
-}
-
-static void
-verify_test_message (DBusMessage *message)
-{
-  DBusMessageIter iter;
-  DBusError error;
-  dbus_int32_t our_int;
-  const char *our_str;
-  double our_double;
-  unsigned char our_bool;
-  unsigned char our_byte_1, our_byte_2;
-  dbus_uint32_t our_uint32;
-  const dbus_int32_t *our_uint32_array = (void*)0xdeadbeef;
-  int our_uint32_array_len;
-  dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
-  int our_int32_array_len;
-#ifdef DBUS_HAVE_INT64
-  dbus_int64_t our_int64;
-  dbus_uint64_t our_uint64;
-  dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
-  int our_uint64_array_len;
-  const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
-  int our_int64_array_len;
-#endif
-  const double *our_double_array = (void*)0xdeadbeef;
-  int our_double_array_len;
-  const unsigned char *our_byte_array = (void*)0xdeadbeef;
-  int our_byte_array_len;
-  const unsigned char *our_boolean_array = (void*)0xdeadbeef;
-  int our_boolean_array_len;
-
-  dbus_message_iter_init (message, &iter);
-
-  dbus_error_init (&error);
-  if (!dbus_message_iter_get_args (&iter, &error,
-                                  DBUS_TYPE_INT32, &our_int,
-#ifdef DBUS_HAVE_INT64
-                                   DBUS_TYPE_INT64, &our_int64,
-                                   DBUS_TYPE_UINT64, &our_uint64,
-#endif
-                                  DBUS_TYPE_STRING, &our_str,
-                                  DBUS_TYPE_DOUBLE, &our_double,
-                                  DBUS_TYPE_BOOLEAN, &our_bool,
-                                  DBUS_TYPE_BYTE, &our_byte_1,
-                                  DBUS_TYPE_BYTE, &our_byte_2,
-                                  DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
-                                   &our_uint32_array, &our_uint32_array_len,
-                                   DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
-                                   &our_int32_array, &our_int32_array_len,
-#ifdef DBUS_HAVE_INT64
-                                  DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
-                                   &our_uint64_array, &our_uint64_array_len,
-                                   DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
-                                   &our_int64_array, &our_int64_array_len,
-#endif
-                                   DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
-                                   &our_double_array, &our_double_array_len,
-                                   DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
-                                   &our_byte_array, &our_byte_array_len,
-                                   DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
-                                   &our_boolean_array, &our_boolean_array_len,
-                                  0))
-    {
-      _dbus_warn ("error: %s - %s\n", error.name,
-                  (error.message != NULL) ? error.message : "no message");
-      _dbus_assert_not_reached ("Could not get arguments");
-    }
-
-  if (our_int != -0x12345678)
-    _dbus_assert_not_reached ("integers differ!");
-
-#ifdef DBUS_HAVE_INT64
-  if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
-    _dbus_assert_not_reached ("64-bit integers differ!");
-  if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
-    _dbus_assert_not_reached ("64-bit unsigned integers differ!");
-#endif
-
-  if (our_double != 3.14159)
-    _dbus_assert_not_reached ("doubles differ!");
-
-  if (strcmp (our_str, "Test string") != 0)
-    _dbus_assert_not_reached ("strings differ!");
-
-  if (!our_bool)
-    _dbus_assert_not_reached ("booleans differ");
-
-  if (our_byte_1 != 42)
-    _dbus_assert_not_reached ("bytes differ!");
-
-  if (our_byte_2 != 24)
-    _dbus_assert_not_reached ("bytes differ!");
-
-  if (our_uint32_array_len != 4 ||
-      our_uint32_array[0] != 0x12345678 ||
-      our_uint32_array[1] != 0x23456781 ||
-      our_uint32_array[2] != 0x34567812 ||
-      our_uint32_array[3] != 0x45678123)
-    _dbus_assert_not_reached ("uint array differs");
-
-  if (our_int32_array_len != 4 ||
-      our_int32_array[0] != 0x12345678 ||
-      our_int32_array[1] != -0x23456781 ||
-      our_int32_array[2] != 0x34567812 ||
-      our_int32_array[3] != -0x45678123)
-    _dbus_assert_not_reached ("int array differs");
-
-#ifdef DBUS_HAVE_INT64
-  if (our_uint64_array_len != 4 ||
-      our_uint64_array[0] != 0x12345678 ||
-      our_uint64_array[1] != 0x23456781 ||
-      our_uint64_array[2] != 0x34567812 ||
-      our_uint64_array[3] != 0x45678123)
-    _dbus_assert_not_reached ("uint64 array differs");
-
-  if (our_int64_array_len != 4 ||
-      our_int64_array[0] != 0x12345678 ||
-      our_int64_array[1] != -0x23456781 ||
-      our_int64_array[2] != 0x34567812 ||
-      our_int64_array[3] != -0x45678123)
-    _dbus_assert_not_reached ("int64 array differs");
-#endif /* DBUS_HAVE_INT64 */
-
-  if (our_double_array_len != 3)
-    _dbus_assert_not_reached ("double array had wrong length");
-
-  /* On all IEEE machines (i.e. everything sane) exact equality
-   * should be preserved over the wire
-   */
-  if (our_double_array[0] != 0.1234 ||
-      our_double_array[1] != 9876.54321 ||
-      our_double_array[2] != -300.0)
-    _dbus_assert_not_reached ("double array had wrong values");
-
-  if (our_byte_array_len != 4)
-    _dbus_assert_not_reached ("byte array had wrong length");
-
-  if (our_byte_array[0] != 'a' ||
-      our_byte_array[1] != 'b' ||
-      our_byte_array[2] != 'c' ||
-      our_byte_array[3] != 234)
-    _dbus_assert_not_reached ("byte array had wrong values");
-
-  if (our_boolean_array_len != 5)
-    _dbus_assert_not_reached ("bool array had wrong length");
-
-  if (our_boolean_array[0] != TRUE ||
-      our_boolean_array[1] != FALSE ||
-      our_boolean_array[2] != TRUE ||
-      our_boolean_array[3] != TRUE ||
-      our_boolean_array[4] != FALSE)
-    _dbus_assert_not_reached ("bool array had wrong values");
-
-  if (dbus_message_iter_next (&iter))
-    _dbus_assert_not_reached ("Didn't reach end of arguments");
-}
-
-/**
- * @ingroup DBusMessageInternals
- * Unit test for DBusMessage.
- *
- * @returns #TRUE on success.
- */
-dbus_bool_t
-_dbus_message_test (const char *test_data_dir)
-{
-  DBusMessage *message;
-  DBusMessageLoader *loader;
-  DBusMessageIter iter, child_iter, child_iter2, child_iter3;
-  int i;
-  const char *data;
-  DBusMessage *copy;
-  const char *name1;
-  const char *name2;
-  const dbus_uint32_t our_uint32_array[] =
-    { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
-  const dbus_uint32_t our_int32_array[] =
-    { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
-  const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
-  const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
-#ifdef DBUS_HAVE_INT64
-  const dbus_uint64_t our_uint64_array[] =
-    { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
-  const dbus_uint64_t our_int64_array[] =
-    { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
-  const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
-  const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
-#endif
-  const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
-  const char **v_ARRAY_STRING = our_string_array;
-  const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
-  const double *v_ARRAY_DOUBLE = our_double_array;
-  const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
-  const unsigned char *v_ARRAY_BYTE = our_byte_array;
-  const unsigned char our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
-  const unsigned char *v_ARRAY_BOOLEAN = our_boolean_array;
-  char sig[64];
-  const char *s;
-  char *t;
-  DBusError error;
-  const char *v_STRING;
-  double v_DOUBLE;
-  dbus_int32_t v_INT32;
-  dbus_uint32_t v_UINT32;
-#ifdef DBUS_HAVE_INT64
-  dbus_int64_t v_INT64;
-  dbus_uint64_t v_UINT64;
-#endif
-  unsigned char v_BYTE;
-  unsigned char v2_BYTE;
-  unsigned char v_BOOLEAN;
-
-  _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
-
-  message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
-                                          "/org/freedesktop/TestPath",
-                                          "Foo.TestInterface",
-                                          "TestMethod");
-  _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
-  _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
-                                             "TestMethod"));
-  _dbus_assert (strcmp (dbus_message_get_path (message),
-                        "/org/freedesktop/TestPath") == 0);
-  _dbus_message_set_serial (message, 1234);
-
-  /* string length including nul byte not a multiple of 4 */
-  if (!dbus_message_set_sender (message, "org.foo.bar1"))
-    _dbus_assert_not_reached ("out of memory");
-
-  _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
-  dbus_message_set_reply_serial (message, 5678);
-
-  _dbus_verbose_bytes_of_string (&message->header.data, 0,
-                                 _dbus_string_get_length (&message->header.data));
-  _dbus_verbose_bytes_of_string (&message->body, 0,
-                                 _dbus_string_get_length (&message->body));
-
-  if (!dbus_message_set_sender (message, NULL))
-    _dbus_assert_not_reached ("out of memory");
-
-
-  _dbus_verbose_bytes_of_string (&message->header.data, 0,
-                                 _dbus_string_get_length (&message->header.data));
-  _dbus_verbose_bytes_of_string (&message->body, 0,
-                                 _dbus_string_get_length (&message->body));
-
-
-  _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
-  _dbus_assert (dbus_message_get_serial (message) == 1234);
-  _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
-  _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
-
-  _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
-  dbus_message_set_no_reply (message, TRUE);
-  _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
-  dbus_message_set_no_reply (message, FALSE);
-  _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
-
-  /* Set/get some header fields */
-
-  if (!dbus_message_set_path (message, "/foo"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_path (message),
-                        "/foo") == 0);
-
-  if (!dbus_message_set_interface (message, "org.Foo"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_interface (message),
-                        "org.Foo") == 0);
-
-  if (!dbus_message_set_member (message, "Bar"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_member (message),
-                        "Bar") == 0);
-
-  /* Set/get them with longer values */
-  if (!dbus_message_set_path (message, "/foo/bar"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_path (message),
-                        "/foo/bar") == 0);
-
-  if (!dbus_message_set_interface (message, "org.Foo.Bar"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_interface (message),
-                        "org.Foo.Bar") == 0);
-
-  if (!dbus_message_set_member (message, "BarFoo"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_member (message),
-                        "BarFoo") == 0);
-
-  /* Realloc shorter again */
-
-  if (!dbus_message_set_path (message, "/foo"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_path (message),
-                        "/foo") == 0);
-
-  if (!dbus_message_set_interface (message, "org.Foo"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_interface (message),
-                        "org.Foo") == 0);
-
-  if (!dbus_message_set_member (message, "Bar"))
-    _dbus_assert_not_reached ("out of memory");
-  _dbus_assert (strcmp (dbus_message_get_member (message),
-                        "Bar") == 0);
-
-  dbus_message_unref (message);
-
-  /* Test the vararg functions */
-  message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
-                                          "/org/freedesktop/TestPath",
-                                          "Foo.TestInterface",
-                                          "TestMethod");
-  _dbus_message_set_serial (message, 1);
-
-  v_INT32 = -0x12345678;
-#ifdef DBUS_HAVE_INT64
-  v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
-  v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
-#endif
-  v_STRING = "Test string";
-  v_DOUBLE = 3.14159;
-  v_BOOLEAN = TRUE;
-  v_BYTE = 42;
-  v2_BYTE = 24;
-
-  dbus_message_append_args (message,
-                           DBUS_TYPE_INT32, &v_INT32,
-#ifdef DBUS_HAVE_INT64
-                            DBUS_TYPE_INT64, &v_INT64,
-                            DBUS_TYPE_UINT64, &v_UINT64,
-#endif
-                           DBUS_TYPE_STRING, &v_STRING,
-                           DBUS_TYPE_DOUBLE, &v_DOUBLE,
-                           DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
-                           DBUS_TYPE_BYTE, &v_BYTE,
-                           DBUS_TYPE_BYTE, &v2_BYTE,
-                           DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
-                            _DBUS_N_ELEMENTS (our_uint32_array),
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
-                            _DBUS_N_ELEMENTS (our_int32_array),
-#ifdef DBUS_HAVE_INT64
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
-                            _DBUS_N_ELEMENTS (our_uint64_array),
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
-                            _DBUS_N_ELEMENTS (our_int64_array),
-#endif
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
-                            _DBUS_N_ELEMENTS (our_double_array),
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
-                            _DBUS_N_ELEMENTS (our_byte_array),
-                            DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
-                            _DBUS_N_ELEMENTS (our_boolean_array),
-                           DBUS_TYPE_INVALID);
-
-  i = 0;
-  sig[i++] = DBUS_TYPE_INT32;
-#ifdef DBUS_HAVE_INT64
-  sig[i++] = DBUS_TYPE_INT64;
-  sig[i++] = DBUS_TYPE_UINT64;
-#endif
-  sig[i++] = DBUS_TYPE_STRING;
-  sig[i++] = DBUS_TYPE_DOUBLE;
-  sig[i++] = DBUS_TYPE_BOOLEAN;
-  sig[i++] = DBUS_TYPE_BYTE;
-  sig[i++] = DBUS_TYPE_BYTE;
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_UINT32;
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_INT32;
-#ifdef DBUS_HAVE_INT64
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_UINT64;
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_INT64;
-#endif
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_DOUBLE;
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_BYTE;
-  sig[i++] = DBUS_TYPE_ARRAY;
-  sig[i++] = DBUS_TYPE_BOOLEAN;
-  sig[i++] = DBUS_TYPE_INVALID;
-
-  _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
-
-  _dbus_verbose ("HEADER\n");
-  _dbus_verbose_bytes_of_string (&message->header.data, 0,
-                                 _dbus_string_get_length (&message->header.data));
-  _dbus_verbose ("BODY\n");
-  _dbus_verbose_bytes_of_string (&message->body, 0,
-                                 _dbus_string_get_length (&message->body));
-
-  _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
-                 sig, dbus_message_get_signature (message));
-
-  s = dbus_message_get_signature (message);
-
-  _dbus_assert (dbus_message_has_signature (message, sig));
-  _dbus_assert (strcmp (s, sig) == 0);
-
-  verify_test_message (message);
-
-  copy = dbus_message_copy (message);
-
-  _dbus_assert (dbus_message_get_reply_serial (message) ==
-                dbus_message_get_reply_serial (copy));
-  _dbus_assert (message->header.padding == copy->header.padding);
-
-  _dbus_assert (_dbus_string_get_length (&message->header.data) ==
-                _dbus_string_get_length (&copy->header.data));
-
-  _dbus_assert (_dbus_string_get_length (&message->body) ==
-                _dbus_string_get_length (&copy->body));
-
-  verify_test_message (copy);
-
-  name1 = dbus_message_get_interface (message);
-  name2 = dbus_message_get_interface (copy);
-
-  _dbus_assert (strcmp (name1, name2) == 0);
-
-  name1 = dbus_message_get_member (message);
-  name2 = dbus_message_get_member (copy);
-
-  _dbus_assert (strcmp (name1, name2) == 0);
-
-  dbus_message_unref (message);
-  dbus_message_unref (copy);
-
-#if 0
-  /* FIXME */
-  message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
-                                          "/org/freedesktop/TestPath",
-                                          "Foo.TestInterface",
-                                          "TestMethod");
-
-  _dbus_message_set_serial (message, 1);
-  dbus_message_set_reply_serial (message, 0x12345678);
-
-  dbus_message_iter_init_append (message, &iter);
-  dbus_message_iter_append_string (&iter, "Test string");
-  dbus_message_iter_append_int32 (&iter, -0x12345678);
-  dbus_message_iter_append_uint32 (&iter, 0xedd1e);
-  dbus_message_iter_append_double (&iter, 3.14159);
-
-  dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_DOUBLE);
-  dbus_message_iter_append_double (&child_iter, 1.5);
-  dbus_message_iter_append_double (&child_iter, 2.5);
-
-  /* dict */
-  dbus_message_iter_append_dict (&iter, &child_iter);
-  dbus_message_iter_append_dict_key (&child_iter, "test");
-  dbus_message_iter_append_uint32 (&child_iter, 0xDEADBEEF);
-
-  /* dict (in dict) */
-  dbus_message_iter_append_dict_key (&child_iter, "testdict");
-  dbus_message_iter_append_dict (&child_iter, &child_iter2);
-
-  dbus_message_iter_append_dict_key (&child_iter2, "dictkey");
-  dbus_message_iter_append_string (&child_iter2, "dictvalue");
-
-  /* array of array of int32  (in dict) */
-  dbus_message_iter_append_dict_key (&child_iter, "array");
-  dbus_message_iter_append_array (&child_iter, &child_iter2, DBUS_TYPE_ARRAY);
-  dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
-  dbus_message_iter_append_int32 (&child_iter3, 0x12345678);
-  dbus_message_iter_append_int32 (&child_iter3, 0x23456781);
-  _dbus_warn ("next call expected to fail with wrong array type\n");
-  _dbus_assert (!dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_UINT32));
-  dbus_message_iter_append_array (&child_iter2, &child_iter3, DBUS_TYPE_INT32);
-  dbus_message_iter_append_int32 (&child_iter3, 0x34567812);
-  dbus_message_iter_append_int32 (&child_iter3, 0x45678123);
-  dbus_message_iter_append_int32 (&child_iter3, 0x56781234);
-
-  dbus_message_iter_append_byte (&iter, 0xF0);
-
-  dbus_message_iter_append_nil (&iter);
-
-  dbus_message_iter_append_custom (&iter, "MyTypeName",
-                                   "data", 5);
-
-  dbus_message_iter_append_byte (&iter, 0xF0);
-
-  dbus_message_iter_append_array (&iter, &child_iter, DBUS_TYPE_INT32);
-
-  dbus_message_iter_append_byte (&iter, 0xF0);
-
-  dbus_message_iter_append_dict (&iter, &child_iter);
-
-  dbus_message_iter_append_byte (&iter, 0xF0);
-
-  message_iter_test (message);
-
-  /* Message loader test */
-  _dbus_message_lock (message);
-  loader = _dbus_message_loader_new ();
-
-  /* check ref/unref */
-  _dbus_message_loader_ref (loader);
-  _dbus_message_loader_unref (loader);
-
-  /* Write the header data one byte at a time */
-  data = _dbus_string_get_const_data (&message->header);
-  for (i = 0; i < _dbus_string_get_length (&message->header); i++)
-    {
-      DBusString *buffer;
-
-      _dbus_message_loader_get_buffer (loader, &buffer);
-      _dbus_string_append_byte (buffer, data[i]);
-      _dbus_message_loader_return_buffer (loader, buffer, 1);
-    }
-
-  /* Write the body data one byte at a time */
-  data = _dbus_string_get_const_data (&message->body);
-  for (i = 0; i < _dbus_string_get_length (&message->body); i++)
-    {
-      DBusString *buffer;
-
-      _dbus_message_loader_get_buffer (loader, &buffer);
-      _dbus_string_append_byte (buffer, data[i]);
-      _dbus_message_loader_return_buffer (loader, buffer, 1);
-    }
-
-  copy = dbus_message_copy (message); /* save for tests below */
-  dbus_message_unref (message);
-
-  /* Now pop back the message */
-  if (!_dbus_message_loader_queue_messages (loader))
-    _dbus_assert_not_reached ("no memory to queue messages");
-
-  if (_dbus_message_loader_get_is_corrupted (loader))
-    _dbus_assert_not_reached ("message loader corrupted");
-
-  message = _dbus_message_loader_pop_message (loader);
-  if (!message)
-    _dbus_assert_not_reached ("received a NULL message");
-
-  if (dbus_message_get_reply_serial (message) != 0x12345678)
-    _dbus_assert_not_reached ("reply serial fields differ");
-
-  message_iter_test (message);
-
-  dbus_message_unref (message);
-  _dbus_message_loader_unref (loader);
-
-  message = dbus_message_new_method_return (copy);
-  if (message == NULL)
-    _dbus_assert_not_reached ("out of memory\n");
-  dbus_message_unref (copy);
-
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, "hello",
-                                 DBUS_TYPE_INVALID))
-    _dbus_assert_not_reached ("no memory");
-
-  if (!dbus_message_has_signature (message, "s"))
-    _dbus_assert_not_reached ("method return has wrong signature");
-
-  dbus_error_init (&error);
-  if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
-                              &t, DBUS_TYPE_INVALID))
-
-    {
-      _dbus_warn ("Failed to get expected string arg: %s\n", error.message);
-      exit (1);
-    }
-  dbus_free (t);
-
-  dbus_message_unref (message);
-
-  /* This ServiceAcquired message used to trigger a bug in
-   * setting header fields, adding to regression test.
-   */
-  message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_DBUS,
-                                     DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
-                                     "ServiceAcquired");
-
-  if (message == NULL)
-    _dbus_assert_not_reached ("out of memory");
-
-  _dbus_verbose ("Bytes after creation\n");
-  _dbus_verbose_bytes_of_string (&message->header, 0,
-                                 _dbus_string_get_length (&message->header));
-
-  if (!dbus_message_set_destination (message, ":1.0") ||
-      !dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, ":1.0",
-                                 DBUS_TYPE_INVALID))
-    _dbus_assert_not_reached ("out of memory");
-
-  _dbus_verbose ("Bytes after set_destination() and append_args()\n");
-  _dbus_verbose_bytes_of_string (&message->header, 0,
-                                 _dbus_string_get_length (&message->header));
-
-  if (!dbus_message_set_sender (message, "org.freedesktop.DBus"))
-    _dbus_assert_not_reached ("out of memory");
-
-  _dbus_verbose ("Bytes after set_sender()\n");
-  _dbus_verbose_bytes_of_string (&message->header, 0,
-                                 _dbus_string_get_length (&message->header));
-
-  /* When the bug happened the above set_destination() would
-   * corrupt the signature
-   */
-  if (!dbus_message_has_signature (message, "s"))
-    {
-      _dbus_warn ("Signature should be 's' but is '%s'\n",
-                  dbus_message_get_signature (message));
-      _dbus_assert_not_reached ("signal has wrong signature");
-    }
-
-  /* have to set destination again to reproduce the bug */
-  if (!dbus_message_set_destination (message, ":1.0"))
-    _dbus_assert_not_reached ("out of memory");
-
-  _dbus_verbose ("Bytes after set_destination()\n");
-  _dbus_verbose_bytes_of_string (&message->header, 0,
-                                 _dbus_string_get_length (&message->header));
-
-  /* When the bug happened the above set_destination() would
-   * corrupt the signature
-   */
-  if (!dbus_message_has_signature (message, "s"))
-    {
-      _dbus_warn ("Signature should be 's' but is '%s'\n",
-                  dbus_message_get_signature (message));
-      _dbus_assert_not_reached ("signal has wrong signature");
-    }
-
-  dbus_error_init (&error);
-  if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING,
-                              &t, DBUS_TYPE_INVALID))
-
-    {
-      _dbus_warn ("Failed to get expected string arg for signal: %s\n", error.message);
-      exit (1);
-    }
-  dbus_free (t);
-
-  dbus_message_unref (message);
-
-  /* Now load every message in test_data_dir if we have one */
-  if (test_data_dir == NULL)
-    return TRUE;
-
-  return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
-                                                        (DBusForeachMessageFileFunc)
-                                                        dbus_internal_do_not_use_try_message_file,
-                                                        NULL);
-
-#endif /* Commented out most tests for now */
-
-  return TRUE;
-}
 
-#endif /* DBUS_BUILD_TESTS */
+/* tests in dbus-message-util.c */