Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc1d9baa authored by Myles Watson's avatar Myles Watson
Browse files

packet: Enforce View inheritance and validation

Add was_validated_ flag to Views that can be constructed from bytes.
Check was_validated_ in every generated Get* function.

Test: bluetooth_packet_parser_test --gtest_filter=*Validate*Death*
Change-Id: I1de8d4e5e60bdd7b51562ffd258300e808db4adb
parent 458a0e1f
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -74,7 +74,8 @@ TEST(AclBuilderTest, buildAclCount) {
  count_packet->Serialize(it);

  PacketView<true> count_packet_bytes_view(count_packet_bytes);
  AclPacketView count_packet_view(count_packet_bytes_view);
  AclPacketView count_packet_view = AclPacketView::Create(count_packet_bytes_view);
  ASSERT_TRUE(count_packet_view.IsValid());

  ASSERT_EQ(handle, count_packet_view.GetHandle());
  ASSERT_EQ(packet_boundary_flag, count_packet_view.GetPacketBoundaryFlag());
@@ -105,7 +106,8 @@ TEST(AclBuilderTest, buildAclCountInverted) {
  BitInserter it(*counting_down_bytes_packet_bytes);
  counting_down_bytes_packet->Serialize(it);
  PacketView<true> counting_down_bytes_packet_bytes_view(counting_down_bytes_packet_bytes);
  AclPacketView counting_down_bytes_packet_view(counting_down_bytes_packet_bytes_view);
  AclPacketView counting_down_bytes_packet_view = AclPacketView::Create(counting_down_bytes_packet_bytes_view);
  ASSERT_TRUE(counting_down_bytes_packet_view.IsValid());

  ASSERT_EQ(handle, counting_down_bytes_packet_view.GetHandle());
  ASSERT_EQ(packet_boundary_flag, counting_down_bytes_packet_view.GetPacketBoundaryFlag());
@@ -137,7 +139,8 @@ TEST(AclBuilderTest, buildInformationRequest) {
  BitInserter it(*packet_bytes);
  packet->Serialize(it);
  PacketView<true> packet_bytes_view(packet_bytes);
  AclPacketView packet_view(packet_bytes_view);
  AclPacketView packet_view = AclPacketView::Create(packet_bytes_view);
  ASSERT_TRUE(packet_view.IsValid());

  ASSERT_EQ(packet_bytes->size(), information_request.size());
  for (size_t i = 0; i < packet_bytes->size(); i++) {
+2 −2
Original line number Diff line number Diff line
@@ -72,11 +72,11 @@ TEST(L2capPacketTest, extendedInformationStartFrameTest) {
  PacketView<true> packet_bytes_view(packet_bytes);
  ASSERT_EQ(extended_information_start_frame.size(), packet_bytes_view.size());

  BasicFrameView basic_frame_view(packet_bytes_view);
  BasicFrameView basic_frame_view = BasicFrameView::Create(packet_bytes_view);
  ASSERT_TRUE(basic_frame_view.IsValid());
  ASSERT_EQ(channel_id, basic_frame_view.GetChannelId());

  StandardFrameView standard_frame_view(packet_bytes_view);
  StandardFrameView standard_frame_view = StandardFrameView::Create(basic_frame_view);
  ASSERT_TRUE(standard_frame_view.IsValid());
  ASSERT_EQ(FrameType::I_FRAME, standard_frame_view.GetFrameType());
}
+1 −1
Original line number Diff line number Diff line
@@ -38,9 +38,9 @@ std::string EnumField::GetType() const {
}

void EnumField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  // Write the Getter Function Definition
  s << GetType();
  s << " Get" << GetName() << "() const {";
  s << "ASSERT(was_validated_);";

  // Write the Getter Function Body
  int num_leading_bits = 0;
+1 −1
Original line number Diff line number Diff line
@@ -48,9 +48,9 @@ std::string FixedField::GetType() const {
}

void FixedField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  // Write the Getter Function Definiton
  s << GetType();
  s << " Get" << GetName() << "() const {";
  s << "ASSERT(was_validated_);";

  // Write the Getter Function Body
  int num_leading_bits = 0;
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@ std::string PayloadField::GetType() const {
}

void PayloadField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  // Write the Getter Function Body
  if (start_offset.empty()) {
    ERROR(this) << "Can not have a payload that has an ambiguous start offset. "
                << "Is there a field with an unknown length before the "
@@ -76,6 +75,7 @@ void PayloadField::GenGetter(std::ostream& s, Size start_offset, Size end_offset
  }

  s << "PacketView<kLittleEndian> GetPayload() {";
  s << "ASSERT(was_validated_);";

  s << "size_t payload_begin = " << start_offset.bits() / 8 << " + (" << start_offset.dynamic_string() << ");";

Loading