Commit 72c433f8 authored by Havoc Pennington's avatar Havoc Pennington

2005-01-24 Havoc Pennington <hp@redhat.com>

	* dbus/dbus-message-factory.c: more testing of message validation

	* dbus/dbus-protocol.h (DBUS_MINIMUM_HEADER_SIZE): move to this
	header
parent 9d21554d
2005-01-24 Havoc Pennington <hp@redhat.com>
* dbus/dbus-message-factory.c: more testing of message validation
* dbus/dbus-protocol.h (DBUS_MINIMUM_HEADER_SIZE): move to this
header
2005-01-23 Havoc Pennington <hp@redhat.com>
* dbus/dbus-message-factory.c, dbus/dbus-message-util.c:
......
......@@ -1424,6 +1424,87 @@ value_generator (int *ip)
return node;
}
static void
build_body (TestTypeNode **nodes,
int n_nodes,
int byte_order,
DBusString *signature,
DBusString *body)
{
int i;
DataBlock block;
DBusTypeReader reader;
DBusTypeWriter writer;
i = 0;
while (i < n_nodes)
{
if (! node_build_signature (nodes[i], signature))
_dbus_assert_not_reached ("no memory");
++i;
}
if (!data_block_init (&block, byte_order, 0))
_dbus_assert_not_reached ("no memory");
data_block_init_reader_writer (&block,
&reader, &writer);
/* DBusTypeWriter assumes it's writing into an existing signature,
* so doesn't add nul on its own. We have to do that.
*/
if (!_dbus_string_insert_byte (&block.signature,
0, '\0'))
_dbus_assert_not_reached ("no memory");
i = 0;
while (i < n_nodes)
{
if (!node_write_value (nodes[i], &block, &writer, i))
_dbus_assert_not_reached ("no memory");
++i;
}
if (!_dbus_string_copy_len (&block.body, 0,
_dbus_string_get_length (&block.body) - N_FENCE_BYTES,
body, 0))
_dbus_assert_not_reached ("oom");
data_block_free (&block);
}
dbus_bool_t
dbus_internal_do_not_use_generate_bodies (int sequence,
int byte_order,
DBusString *signature,
DBusString *body)
{
TestTypeNode *nodes[1];
int i;
int n_nodes;
nodes[0] = value_generator (&sequence);
if (nodes[0] == NULL)
return FALSE;
n_nodes = 1;
build_body (nodes, n_nodes, byte_order, signature, body);
i = 0;
while (i < n_nodes)
{
node_destroy (nodes[i]);
++i;
}
return TRUE;
}
static void
make_and_run_values_inside_container (const TestTypeNodeClass *container_klass,
int n_nested)
......@@ -2305,18 +2386,26 @@ object_path_from_seed (char *buf,
v = (unsigned char) ('A' + seed);
i = 0;
while (i + 1 < len)
if (len < 2)
{
if (v < 'A' || v > 'z')
v = 'A';
buf[i] = '/';
++i;
buf[i] = v;
++i;
buf[0] = '/';
i = 1;
}
else
{
i = 0;
while (i + 1 < len)
{
if (v < 'A' || v > 'z')
v = 'A';
v += 1;
buf[i] = '/';
++i;
buf[i] = v;
++i;
v += 1;
}
}
buf[i] = '\0';
......
......@@ -923,7 +923,7 @@ _dbus_type_reader_read_fixed_multi (const DBusTypeReader *reader,
_dbus_assert (reader->klass == &array_reader_class);
element_type = _dbus_first_type_in_signature (reader->type_str,
reader->type_pos);
reader->type_pos);
_dbus_assert (element_type != DBUS_TYPE_INVALID); /* why we don't use get_current_type() */
_dbus_assert (_dbus_type_is_fixed (element_type));
......
......@@ -193,7 +193,7 @@ validate_body_helper (DBusTypeReader *reader,
dbus_uint32_t claimed_len;
a = _DBUS_ALIGN_ADDRESS (p, 4);
if (a + 4 >= end)
if (a + 4 > end)
return DBUS_INVALID_NOT_ENOUGH_DATA;
while (p != a)
{
......@@ -205,6 +205,9 @@ validate_body_helper (DBusTypeReader *reader,
claimed_len = _dbus_unpack_uint32 (byte_order, p);
p += 4;
/* p may now be == end */
_dbus_assert (p <= end);
if (current_type == DBUS_TYPE_ARRAY)
{
int array_elem_type = _dbus_type_reader_get_element_type (reader);
......@@ -1322,6 +1325,70 @@ _dbus_marshal_validate_test (void)
_dbus_string_free (&str);
/* Body validation; test basic validation of valid bodies for both endian */
{
int sequence;
DBusString signature;
DBusString body;
if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
_dbus_assert_not_reached ("oom");
sequence = 0;
while (dbus_internal_do_not_use_generate_bodies (sequence,
DBUS_LITTLE_ENDIAN,
&signature, &body))
{
DBusValidity validity;
validity = _dbus_validate_body_with_reason (&signature, 0,
DBUS_LITTLE_ENDIAN,
NULL, &body, 0,
_dbus_string_get_length (&body));
if (validity != DBUS_VALID)
{
_dbus_warn ("invalid code %d expected valid on sequence %d little endian\n",
validity, sequence);
_dbus_verbose_bytes_of_string (&signature, 0, _dbus_string_get_length (&signature));
_dbus_verbose_bytes_of_string (&body, 0, _dbus_string_get_length (&body));
_dbus_assert_not_reached ("test failed");
}
_dbus_string_set_length (&signature, 0);
_dbus_string_set_length (&body, 0);
++sequence;
}
sequence = 0;
while (dbus_internal_do_not_use_generate_bodies (sequence,
DBUS_BIG_ENDIAN,
&signature, &body))
{
DBusValidity validity;
validity = _dbus_validate_body_with_reason (&signature, 0,
DBUS_BIG_ENDIAN,
NULL, &body, 0,
_dbus_string_get_length (&body));
if (validity != DBUS_VALID)
{
_dbus_warn ("invalid code %d expected valid on sequence %d big endian\n",
validity, sequence);
_dbus_verbose_bytes_of_string (&signature, 0, _dbus_string_get_length (&signature));
_dbus_verbose_bytes_of_string (&body, 0, _dbus_string_get_length (&body));
_dbus_assert_not_reached ("test failed");
}
_dbus_string_set_length (&signature, 0);
_dbus_string_set_length (&body, 0);
++sequence;
}
_dbus_string_free (&signature);
_dbus_string_free (&body);
}
return TRUE;
}
......
This diff is collapsed.
......@@ -40,10 +40,11 @@ typedef struct
} DBusMessageData;
#define _DBUS_MESSAGE_DATA_MAX_NESTING 10
typedef struct
{
int generator;
int sequence;
int sequence_nos[_DBUS_MESSAGE_DATA_MAX_NESTING];
int depth;
} DBusMessageDataIter;
void _dbus_message_data_free (DBusMessageData *data);
......
......@@ -1148,7 +1148,9 @@ _dbus_message_test (const char *test_data_dir)
{
DBusMessageDataIter diter;
DBusMessageData mdata;
int count;
count = 0;
_dbus_message_data_iter_init (&diter);
while (_dbus_message_data_iter_get_and_next (&diter,
......@@ -1157,14 +1159,17 @@ _dbus_message_test (const char *test_data_dir)
if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
mdata.expected_validity))
{
_dbus_warn ("expected validity %d and did not get it; generator %d sequence %d\n",
mdata.expected_validity,
diter.generator, diter.sequence);
_dbus_warn ("expected validity %d and did not get it\n",
mdata.expected_validity);
_dbus_assert_not_reached ("message data failed");
}
_dbus_message_data_free (&mdata);
count += 1;
}
printf ("%d sample messages tested\n", count);
}
check_memleaks ();
......
......@@ -2969,13 +2969,6 @@ _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
loader->buffer_outstanding = TRUE;
}
/**
* 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
......
......@@ -184,6 +184,14 @@ extern "C" {
DBUS_STRUCT_END_CHAR_AS_STRING
/**
* 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. This isn't any kind of resource limit, just the
* necessary/logical outcome of the header signature.
*/
#define DBUS_MINIMUM_HEADER_SIZE 16
/* Services */
#define DBUS_SERVICE_ORG_FREEDESKTOP_DBUS "org.freedesktop.DBus"
......
......@@ -69,8 +69,10 @@ typedef dbus_bool_t (* DBusForeachMessageFileFunc) (const DBusString *filename
dbus_bool_t dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
DBusForeachMessageFileFunc func,
void *user_data);
dbus_bool_t dbus_internal_do_not_use_generate_bodies (int sequence,
int byte_order,
DBusString *signature,
DBusString *body);
#endif /* DBUS_TEST_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment