Skip to content

Commit

Permalink
Test splitting buffers in nested messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
BartHertog committed Jan 5, 2025
1 parent 3852816 commit 0a972ae
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/MessageInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ namespace EmbeddedProto
::EmbeddedProto::ReadBufferSection bufferSection(buffer, n_bytes_to_include_in_section);

// See how many bytes we will now process from the buffer and thus how many bytes are left for the next iteration.
n_bytes_to_include_in_section -= bufferSection.get_max_size();
n_bytes_to_include_in_section -= bufferSection.get_size();

return_value = deserialize(bufferSection);

n_bytes_to_include_in_section += bufferSection.get_size();

// In case we have bytes we still need to receive set the end of buffer return value.
// The return value of deserialize has priority.
if((::EmbeddedProto::Error::NO_ERRORS == return_value) && (0 < n_bytes_to_include_in_section))
{
return_value = ::EmbeddedProto::Error::END_OF_BUFFER;
}
}
}
return return_value;
Expand Down
145 changes: 145 additions & 0 deletions test/test_NestedMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,151 @@ TEST(NestedMessage, deserialize_one)
EXPECT_EQ(1, msg.get_v());
}

TEST(NestedMessage, deserialize_one_partial_clean_tag)
{
::demo::space::message_b<SIZE_MSG_A> msg;

static constexpr uint32_t SIZE1 = 10;
static constexpr uint32_t SIZE2 = 13;

// Test splitting the buffer after the tag of nested message a just before the size of the message.

::EmbeddedProto::ReadBufferFixedSize<SIZE1> buffer1({
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // u
0x12 // tag of nested a
});

// Split after the tag of nested message A

::EmbeddedProto::ReadBufferFixedSize<SIZE2> buffer2({
0x0A, // size of nested a
0x0A, 0x01, 0x01, // x
0x15, 0x00, 0x00, 0x80, 0x3F, // y of nested a
0x18, 0x02, // z
// And back to the parent message with field v.
0x18, 0x01 });

EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer1));
EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer2));

EXPECT_EQ(1.0F, msg.get_u());
EXPECT_EQ(1, msg.get_nested_a().get_x().get_length());
EXPECT_EQ(1, msg.get_nested_a().x(0));
EXPECT_EQ(1.0F, msg.get_nested_a().get_y());
EXPECT_EQ(1, msg.get_nested_a().get_z());
EXPECT_EQ(1, msg.get_v());
}

TEST(NestedMessage, deserialize_one_partial_clean_size)
{
::demo::space::message_b<SIZE_MSG_A> msg;

static constexpr uint32_t SIZE1 = 11;
static constexpr uint32_t SIZE2 = 12;

// Test splitting the buffer just after the size of nested message a.

::EmbeddedProto::ReadBufferFixedSize<SIZE1> buffer1({
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // u
0x12, 0x0A, // tag and size of nested a
});

// Split after the tag of nested message A

::EmbeddedProto::ReadBufferFixedSize<SIZE2> buffer2({
0x0A, 0x01, 0x01, // x of nested message a
0x15, 0x00, 0x00, 0x80, 0x3F, // y of nested a
0x18, 0x02, // z of nested message a
// And back to the parent message with field v.
0x18, 0x01 });

EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer1));
EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer2));

EXPECT_EQ(1.0F, msg.get_u());
EXPECT_EQ(1, msg.get_nested_a().get_x().get_length());
EXPECT_EQ(1, msg.get_nested_a().x(0));
EXPECT_EQ(1.0F, msg.get_nested_a().get_y());
EXPECT_EQ(1, msg.get_nested_a().get_z());
EXPECT_EQ(1, msg.get_v());
}

TEST(NestedMessage, deserialize_one_partial_size)
{
::demo::space::message_b<127> msg;

// Have a larger nested message a where the size consists of multiple bytes. Split in between
// those size bytes.

::EmbeddedProto::ReadBufferFixedSize<150> buffer({
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // u
0x18, 0x01, // v
0x12, 0x88, }); // Split within the size nested message A

EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer));

std::array<uint8_t, 10> temp_buffer({ 0x01, // Continuation of the size of nested message A.
0x15, 0x00, 0x00, 0x80, 0x3F, // y of nested a
0x18, 0x02, // z of nested message a
0x0A, 0x7F }); // Tag and size of x in nested message A.
for(const auto& tb: temp_buffer) {
buffer.push(tb);
}

// The reason why the message is large, a large array.
for(uint32_t i = 0; i < 127; ++i)
{
buffer.push(0x01);
}

// Deserialize the second part.
EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));

EXPECT_EQ(1.0F, msg.get_u());
EXPECT_EQ(127, msg.get_nested_a().get_x().get_length());
for(uint32_t i = 0; i < 127; ++i)
{
EXPECT_EQ(1, msg.get_nested_a().x(i));
}
EXPECT_EQ(1.0F, msg.get_nested_a().get_y());
EXPECT_EQ(1, msg.get_nested_a().get_z());
EXPECT_EQ(1, msg.get_v());
}

TEST(NestedMessage, deserialize_one_partial_clean_field)
{
::demo::space::message_b<SIZE_MSG_A> msg;

static constexpr uint32_t SIZE1 = 14;
static constexpr uint32_t SIZE2 = 9;

// Split after a random field of nested message a.

::EmbeddedProto::ReadBufferFixedSize<SIZE1> buffer1({
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // u
0x12, 0x0A, // tag and size of nested a
0x0A, 0x01, 0x01, // x
});

// Clean split at the end of a field in a nested message

::EmbeddedProto::ReadBufferFixedSize<SIZE2> buffer2({
0x15, 0x00, 0x00, 0x80, 0x3F, // y of nested a
0x18, 0x02, // z
// And back to the parent message with field v.
0x18, 0x01 });

EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer1));
EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer2));

EXPECT_EQ(1.0F, msg.get_u());
EXPECT_EQ(1, msg.get_nested_a().get_x().get_length());
EXPECT_EQ(1, msg.get_nested_a().x(0));
EXPECT_EQ(1.0F, msg.get_nested_a().get_y());
EXPECT_EQ(1, msg.get_nested_a().get_z());
EXPECT_EQ(1, msg.get_v());
}

TEST(NestedMessage, deserialize_nested_in_nested_max)
{
::demo::space::message_c<SIZE_MSG_A, SIZE_MSG_D> msg;
Expand Down
34 changes: 21 additions & 13 deletions test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,39 @@ def test_simple_types():


def test_nested_message():
#msg_b = nm.message_b()
msg_b = nm.message_b()

#msg_b.u = 1.0
#msg_b.v = 1
#msg_b.nested_a.x.append(1)
#msg_b.nested_a.y = 1.0
#msg_b.nested_a.z = 1

# Multi byte size of nested message a.
msg_b.u = 1.0
msg_b.v = 1
for i in range(0, 127):
msg_b.nested_a.x.append(2)
msg_b.nested_a.y = 1.0
msg_b.nested_a.z = 1

#msg_b.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
#msg_b.v = pow(2, 31) - 1 # Max int32
#msg_b.nested_a.x.append(pow(2, 31) - 1) # Max int32
#msg_b.nested_a.y = 3.40282347e+38 # Max float
#msg_b.nested_a.z = 9223372036854775807 # Max sint64

msg_c = nm.message_c()
msg_c.nested_b.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
msg_c.nested_b.v = pow(2, 31) - 1 # Max int32
msg_c.nested_b.nested_a.x.append(pow(2, 31) - 1) # Max int32
msg_c.nested_b.nested_a.y = 3.40282347e+38 # Max float
msg_c.nested_b.nested_a.z = 9223372036854775807 # Max sint64
msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
msg_c.nested_g.g = pow(2, 31) - 1 # Max int32
#msg_c = nm.message_c()
#msg_c.nested_b.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
#msg_c.nested_b.v = pow(2, 31) - 1 # Max int32
#msg_c.nested_b.nested_a.x.append(pow(2, 31) - 1) # Max int32
#msg_c.nested_b.nested_a.y = 3.40282347e+38 # Max float
#msg_c.nested_b.nested_a.z = 9223372036854775807 # Max sint64
#msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
#msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
#msg_c.nested_g.g = pow(2, 31) - 1 # Max int32

msg = msg_c
msg = msg_b

str = ""
msg_str = msg.SerializeToString()
Expand Down Expand Up @@ -367,7 +375,7 @@ def test_optional_empty():
#test_string()
#test_bytes()
#test_repeated_string_bytes()
#test_nested_message()
test_nested_message()
#test_oneof_fields()
#test_included_proto()
test_optional_empty()
#test_optional_empty()

0 comments on commit 0a972ae

Please sign in to comment.