/gdbus/message-serialize-invalid test: be compatible with D-Bus 1.4.8
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Thu, 31 Mar 2011 12:58:55 +0000 (13:58 +0100)
committerDavid Zeuthen <davidz@redhat.com>
Fri, 15 Apr 2011 10:00:16 +0000 (06:00 -0400)
Older versions of libdbus would let you construct an invalid
DBusMessage, but that's a bug, which will be fixed in 1.4.8/1.5.0.
Instead, construct a valid message of the same length, then replace
substrings in the serialized blob with their invalid counterparts.

Bug: https://bugzilla.gnome.org/show_bug.cgi?id=646326

Signed-off-by: David Zeuthen <davidz@redhat.com>
gio/tests/gdbus-serialization.c

index 38227c3..25dada2 100644 (file)
@@ -736,15 +736,30 @@ message_serialize_complex (void)
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
+replace (char       *blob,
+         gsize       len,
+        const char *before,
+        const char *after)
+{
+  gsize i;
+  gsize slen = strlen (before) + 1;
+
+  g_assert_cmpuint (strlen (before), ==, strlen (after));
+  g_assert_cmpuint (len, >=, slen);
+
+  for (i = 0; i < (len - slen + 1); i++)
+    {
+      if (memcmp (blob + i, before, slen) == 0)
+        memcpy (blob + i, after, slen);
+    }
+}
+
+static void
 message_serialize_invalid (void)
 {
   guint n;
 
-  /* Here we're relying on libdbus-1's DBusMessage type not checking
-   * anything. If that were to change, we'd need to do our own
-   * thing.
-   *
-   * Other things we could check (note that GDBus _does_ check for all
+  /* Other things we could check (note that GDBus _does_ check for all
    * these things - we just don't have test-suit coverage for it)
    *
    *  - array exceeding 64 MiB (2^26 bytes) - unfortunately libdbus-1 checks
@@ -769,9 +784,14 @@ message_serialize_invalid (void)
       DBusMessage *dbus_message;
       char *blob;
       int blob_len;
+      int i;
+      /* these are in pairs with matching length */
+      const gchar *valid_utf8_str = "this is valid...";
       const gchar *invalid_utf8_str = "this is invalid\xff";
-      const gchar *invalid_object_path = "/this/is/not a valid object path";
+      const gchar *valid_signature = "a{sv}a{sv}a{sv}aiai";
       const gchar *invalid_signature = "not valid signature";
+      const gchar *valid_object_path = "/this/is/a/valid/dbus/object/path";
+      const gchar *invalid_object_path = "/this/is/not a valid object path!";
 
       dbus_message = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
       dbus_message_set_serial (dbus_message, 0x41);
@@ -782,21 +802,21 @@ message_serialize_invalid (void)
         case 0:
           /* invalid UTF-8 */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_STRING, &invalid_utf8_str,
+                                    DBUS_TYPE_STRING, &valid_utf8_str,
                                     DBUS_TYPE_INVALID);
           break;
 
         case 1:
           /* invalid object path */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_OBJECT_PATH, &invalid_object_path,
+                                    DBUS_TYPE_OBJECT_PATH, &valid_object_path,
                                     DBUS_TYPE_INVALID);
           break;
 
         case 2:
           /* invalid signature */
           dbus_message_append_args (dbus_message,
-                                    DBUS_TYPE_SIGNATURE, &invalid_signature,
+                                    DBUS_TYPE_SIGNATURE, &valid_signature,
                                     DBUS_TYPE_INVALID);
           break;
 
@@ -805,6 +825,11 @@ message_serialize_invalid (void)
           break;
         }
       dbus_message_marshal (dbus_message, &blob, &blob_len);
+      /* hack up the message to be invalid by replacing each valid string
+       * with its invalid counterpart */
+      replace (blob, blob_len, valid_utf8_str, invalid_utf8_str);
+      replace (blob, blob_len, valid_object_path, invalid_object_path);
+      replace (blob, blob_len, valid_signature, invalid_signature);
 
       error = NULL;
       message = g_dbus_message_new_from_blob ((guchar *) blob,