/gdbus/message-serialize-invalid test: be compatible with D-Bus 1.4.8

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>
This commit is contained in:
Simon McVittie 2011-03-31 13:58:55 +01:00 committed by David Zeuthen
parent fc80ec9d0c
commit eb70e54370

View File

@ -735,16 +735,31 @@ 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 static void
message_serialize_invalid (void) message_serialize_invalid (void)
{ {
guint n; guint n;
/* Here we're relying on libdbus-1's DBusMessage type not checking /* Other things we could check (note that GDBus _does_ check for all
* 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
* these things - we just don't have test-suit coverage for it) * these things - we just don't have test-suit coverage for it)
* *
* - array exceeding 64 MiB (2^26 bytes) - unfortunately libdbus-1 checks * - array exceeding 64 MiB (2^26 bytes) - unfortunately libdbus-1 checks
@ -769,9 +784,14 @@ message_serialize_invalid (void)
DBusMessage *dbus_message; DBusMessage *dbus_message;
char *blob; char *blob;
int blob_len; 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_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 *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 = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);
dbus_message_set_serial (dbus_message, 0x41); dbus_message_set_serial (dbus_message, 0x41);
@ -782,21 +802,21 @@ message_serialize_invalid (void)
case 0: case 0:
/* invalid UTF-8 */ /* invalid UTF-8 */
dbus_message_append_args (dbus_message, dbus_message_append_args (dbus_message,
DBUS_TYPE_STRING, &invalid_utf8_str, DBUS_TYPE_STRING, &valid_utf8_str,
DBUS_TYPE_INVALID); DBUS_TYPE_INVALID);
break; break;
case 1: case 1:
/* invalid object path */ /* invalid object path */
dbus_message_append_args (dbus_message, dbus_message_append_args (dbus_message,
DBUS_TYPE_OBJECT_PATH, &invalid_object_path, DBUS_TYPE_OBJECT_PATH, &valid_object_path,
DBUS_TYPE_INVALID); DBUS_TYPE_INVALID);
break; break;
case 2: case 2:
/* invalid signature */ /* invalid signature */
dbus_message_append_args (dbus_message, dbus_message_append_args (dbus_message,
DBUS_TYPE_SIGNATURE, &invalid_signature, DBUS_TYPE_SIGNATURE, &valid_signature,
DBUS_TYPE_INVALID); DBUS_TYPE_INVALID);
break; break;
@ -805,6 +825,11 @@ message_serialize_invalid (void)
break; break;
} }
dbus_message_marshal (dbus_message, &blob, &blob_len); 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; error = NULL;
message = g_dbus_message_new_from_blob ((guchar *) blob, message = g_dbus_message_new_from_blob ((guchar *) blob,