GDBusMessage: Add a way to get/set byte order of a message

Also use this in the test cases to check that serialization to and
from both big and little endian works.

Signed-off-by: David Zeuthen <davidz@redhat.com>
This commit is contained in:
David Zeuthen 2010-08-04 13:32:19 -04:00
parent 6e723e8b3e
commit 6f070be65b
6 changed files with 180 additions and 77 deletions

View File

@ -2295,6 +2295,7 @@ GDBusMessage
GDBusMessageType
GDBusMessageFlags
GDBusMessageHeaderField
GDBusMessageByteOrder
g_dbus_message_new
g_dbus_message_new_signal
g_dbus_message_new_method_call
@ -2303,6 +2304,8 @@ g_dbus_message_new_method_error
g_dbus_message_new_method_error_valist
g_dbus_message_new_method_error_literal
g_dbus_message_print
g_dbus_message_get_byte_order
g_dbus_message_set_byte_order
g_dbus_message_get_message_type
g_dbus_message_set_message_type
g_dbus_message_get_serial

View File

@ -99,6 +99,7 @@ struct _GDBusMessage
#ifdef G_OS_UNIX
GUnixFDList *fd_list;
#endif
GDBusMessageByteOrder byte_order;
};
G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT);
@ -134,6 +135,18 @@ g_dbus_message_class_init (GDBusMessageClass *klass)
static void
g_dbus_message_init (GDBusMessage *message)
{
/* Any D-Bus implementation is supposed to handle both Big and
* Little Endian encodings and the Endianness is part of the D-Bus
* message - we prefer to use Big Endian (since it's Network Byte
* Order and just easier to read for humans) but if the machine is
* Little Endian we use that for performance reasons.
*/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
#else
/* this could also be G_PDP_ENDIAN */
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
#endif
message->headers = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
@ -255,6 +268,8 @@ g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
message = g_dbus_message_new ();
message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
/* reply with same endianness */
message->byte_order = method_call_message->byte_order;
g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
sender = g_dbus_message_get_sender (method_call_message);
@ -327,6 +342,8 @@ g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
message = g_dbus_message_new ();
message->type = G_DBUS_MESSAGE_TYPE_ERROR;
message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
/* reply with same endianness */
message->byte_order = method_call_message->byte_order;
g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
g_dbus_message_set_error_name (message, error_name);
@ -371,6 +388,38 @@ g_dbus_message_new_method_error_valist (GDBusMessage *method_call_me
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_dbus_message_get_byte_order:
* @message: A #GDBusMessage.
*
* Gets the byte order of @message.
*
* Returns: The byte order.
*/
GDBusMessageByteOrder
g_dbus_message_get_byte_order (GDBusMessage *message)
{
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
return message->byte_order;
}
/**
* g_dbus_message_set_byte_order:
* @message: A #GDBusMessage.
* @byte_order: The byte order.
*
* Sets the byte order of @message.
*/
void
g_dbus_message_set_byte_order (GDBusMessage *message,
GDBusMessageByteOrder byte_order)
{
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
message->byte_order = byte_order;
}
/* ---------------------------------------------------------------------------------------------------- */
/* TODO: need GI annotations to specify that any guchar value goes for the type */
/**
@ -1385,7 +1434,9 @@ g_dbus_message_bytes_needed (guchar *blob,
* @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
* @error: Return location for error or %NULL.
*
* Creates a new #GDBusMessage from the data stored at @blob.
* Creates a new #GDBusMessage from the data stored at @blob. The byte
* order that the message was in can be retrieved using
* g_dbus_message_get_byte_order().
*
* Returns: A new #GDBusMessage or %NULL if @error is set. Free with
* g_object_unref().
@ -1429,16 +1480,18 @@ g_dbus_message_new_from_blob (guchar *blob,
{
case 'l':
byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
break;
case 'B':
byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
break;
default:
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Invalid endianness value. Expected 'l' or 'B' but found '%c' (%d)"),
endianness, endianness);
_("Invalid endianness value. Expected 0x6c ('l') or 0x42 ('B') but found value 0x%02x"),
endianness);
goto out;
}
g_data_input_stream_set_byte_order (dis, byte_order);
@ -1945,7 +1998,8 @@ append_body_to_blob (GVariant *value,
* @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
* @error: Return location for error.
*
* Serializes @message to a blob.
* Serializes @message to a blob. The byte order returned by
* g_dbus_message_get_byte_order() will be used.
*
* Returns: A pointer to a valid binary D-Bus message of @out_size bytes
* generated by @message or %NULL if @error is set. Free with g_free().
@ -1987,22 +2041,19 @@ g_dbus_message_to_blob (GDBusMessage *message,
mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
/* Any D-Bus implementation is supposed to handle both Big and
* Little Endian encodings and the Endianness is part of the D-Bus
* message - we prefer to use Big Endian (since it's Network Byte
* Order and just easier to read for humans) but if the machine is
* Little Endian we use that for performance reasons.
*/
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
#else
/* this could also be G_PDP_ENDIAN */
byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
#endif
switch (message->byte_order)
{
case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
break;
case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
break;
}
g_data_output_stream_set_byte_order (dos, byte_order);
/* Core header */
g_data_output_stream_put_byte (dos, byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN ? 'l' : 'B', NULL, NULL);
g_data_output_stream_put_byte (dos, (guchar) message->byte_order, NULL, NULL);
g_data_output_stream_put_byte (dos, message->type, NULL, NULL);
g_data_output_stream_put_byte (dos, message->flags, NULL, NULL);
g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */

View File

@ -59,6 +59,10 @@ GDBusMessage *g_dbus_message_new_method_error_literal (GDBusMessage
gchar *g_dbus_message_print (GDBusMessage *message,
guint indent);
GDBusMessageByteOrder g_dbus_message_get_byte_order (GDBusMessage *message);
void g_dbus_message_set_byte_order (GDBusMessage *message,
GDBusMessageByteOrder byte_order);
GDBusMessageType g_dbus_message_get_message_type (GDBusMessage *message);
void g_dbus_message_set_message_type (GDBusMessage *message,
GDBusMessageType type);

View File

@ -1039,6 +1039,7 @@ g_dbus_server_flags_get_type G_GNUC_CONST
g_dbus_signal_flags_get_type G_GNUC_CONST
g_dbus_send_message_flags_get_type G_GNUC_CONST
g_credentials_type_get_type G_GNUC_CONST
g_dbus_message_byte_order_get_type G_GNUC_CONST
#endif
#endif

View File

@ -1184,6 +1184,21 @@ typedef enum
G_CREDENTIALS_TYPE_LINUX_UCRED
} GCredentialsType;
/**
* GDBusMessageByteOrder:
* @G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN: The byte order is big endian.
* @G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN: The byte order is little endian.
*
* Enumeration used to describe the byte order of a D-Bus message.
*
* Since: 2.26
*/
typedef enum
{
G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN = 'B',
G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN = 'l'
} GDBusMessageByteOrder;
G_END_DECLS
#endif /* __GIO_ENUMS_H__ */

View File

@ -525,6 +525,7 @@ check_serialization (GVariant *value,
DBusError dbus_error;
gchar *s;
gchar *s1;
guint n;
message = g_dbus_message_new ();
g_dbus_message_set_body (message, value);
@ -536,73 +537,101 @@ check_serialization (GVariant *value,
g_dbus_message_set_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, g_variant_new_signature (s));
g_free (s);
/* First check that the serialization to the D-Bus wire format is correct */
error = NULL;
blob = g_dbus_message_to_blob (message,
&blob_size,
G_DBUS_CAPABILITY_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert (blob != NULL);
dbus_error_init (&dbus_error);
dbus_1_message = dbus_message_demarshal ((char *) blob, blob_size, &dbus_error);
if (dbus_error_is_set (&dbus_error))
/* First check that the serialization to the D-Bus wire format is correct - do this for both byte orders */
for (n = 0; n < 2; n++)
{
g_printerr ("Error calling dbus_message_demarshal() on this blob: %s: %s\n",
dbus_error.name,
dbus_error.message);
hexdump (blob, blob_size);
dbus_error_free (&dbus_error);
s = g_variant_print (value, TRUE);
g_printerr ("\nThe blob was generated from the following GVariant value:\n%s\n\n", s);
g_free (s);
g_printerr ("If the blob was encoded using DBusMessageIter, the payload would have been:\n");
print_gv_dbus_message (value);
g_assert_not_reached ();
}
s = dbus_1_message_print (dbus_1_message);
dbus_message_unref (dbus_1_message);
g_assert_cmpstr (s, ==, expected_dbus_1_output);
g_free (s);
/* Then serialize back and check that the body is identical */
error = NULL;
recovered_message = g_dbus_message_new_from_blob (blob,
blob_size,
G_DBUS_CAPABILITY_FLAGS_NONE,
&error);
g_assert (recovered_message != NULL);
g_assert_no_error (error);
if (value == NULL)
{
g_assert (g_dbus_message_get_body (recovered_message) == NULL);
}
else
{
g_assert (g_dbus_message_get_body (recovered_message) != NULL);
if (!g_variant_equal (g_dbus_message_get_body (recovered_message), value))
GDBusMessageByteOrder byte_order;
switch (n)
{
s = g_variant_print (g_dbus_message_get_body (recovered_message), TRUE);
s1 = g_variant_print (value, TRUE);
g_printerr ("Recovered value:\n%s\ndoes not match given value\n%s\n",
s,
s1);
case 0:
byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
break;
case 1:
byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
break;
case 2:
g_assert_not_reached ();
break;
}
g_dbus_message_set_byte_order (message, byte_order);
error = NULL;
blob = g_dbus_message_to_blob (message,
&blob_size,
G_DBUS_CAPABILITY_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert (blob != NULL);
switch (byte_order)
{
case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
g_assert_cmpint (blob[0], ==, 'B');
break;
case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
g_assert_cmpint (blob[0], ==, 'l');
break;
}
dbus_error_init (&dbus_error);
dbus_1_message = dbus_message_demarshal ((char *) blob, blob_size, &dbus_error);
if (dbus_error_is_set (&dbus_error))
{
g_printerr ("Error calling dbus_message_demarshal() on this blob: %s: %s\n",
dbus_error.name,
dbus_error.message);
hexdump (blob, blob_size);
dbus_error_free (&dbus_error);
s = g_variant_print (value, TRUE);
g_printerr ("\nThe blob was generated from the following GVariant value:\n%s\n\n", s);
g_free (s);
g_free (s1);
g_printerr ("If the blob was encoded using DBusMessageIter, the payload would have been:\n");
print_gv_dbus_message (value);
g_assert_not_reached ();
}
s = dbus_1_message_print (dbus_1_message);
dbus_message_unref (dbus_1_message);
g_assert_cmpstr (s, ==, expected_dbus_1_output);
g_free (s);
/* Then serialize back and check that the body is identical */
error = NULL;
recovered_message = g_dbus_message_new_from_blob (blob,
blob_size,
G_DBUS_CAPABILITY_FLAGS_NONE,
&error);
g_assert (recovered_message != NULL);
g_assert_no_error (error);
if (value == NULL)
{
g_assert (g_dbus_message_get_body (recovered_message) == NULL);
}
else
{
g_assert (g_dbus_message_get_body (recovered_message) != NULL);
if (!g_variant_equal (g_dbus_message_get_body (recovered_message), value))
{
s = g_variant_print (g_dbus_message_get_body (recovered_message), TRUE);
s1 = g_variant_print (value, TRUE);
g_printerr ("Recovered value:\n%s\ndoes not match given value\n%s\n",
s,
s1);
g_free (s);
g_free (s1);
g_assert_not_reached ();
}
}
g_object_unref (recovered_message);
}
g_object_unref (message);
g_object_unref (recovered_message);
}
static void