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

Commit 5a492ec4 authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "PDL: Autogenerate packet tests" am: 4af6dd30

am: 0300d6a1

Change-Id: I33f0f82c9f704ea548505f3de19bd5b7a2587ac4
parents a324d800 0300d6a1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ filegroup {
        "device_database_test.cc",
        "dual_device_test.cc",
        "hci_layer_test.cc",
        "hci_packets_test.cc",
    ],
}

+7 −0
Original line number Diff line number Diff line
@@ -1570,6 +1570,13 @@ packet ReadLocalExtendedFeatures : CommandPacket (op_code = READ_LOCAL_EXTENDED_
  page_number : 8,
}

packet ReadLocalExtendedFeaturesComplete : CommandComplete (command_op_code = READ_LOCAL_EXTENDED_FEATURES) {
  status : ErrorCode,
  page_number : 8,
  maximum_page_number : 8,
  extended_lmp_features : 64,
}

packet ReadBufferSize : CommandPacket (op_code = READ_BUFFER_SIZE) {
}

+131 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define PACKET_TESTING
#include "hci/hci_packets.h"

#include <gtest/gtest.h>
#include <forward_list>
#include <memory>

#include "os/log.h"
#include "packet/bit_inserter.h"
#include "packet/raw_builder.h"

using bluetooth::packet::BitInserter;
using bluetooth::packet::RawBuilder;
using std::vector;

namespace bluetooth {
namespace hci {

std::vector<uint8_t> reset = {0x03, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_ResetReflectionTest(reset);

std::vector<uint8_t> reset_complete = {0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_ResetCompleteReflectionTest(reset_complete);

std::vector<uint8_t> read_buffer_size = {0x05, 0x10, 0x00};
DEFINE_AND_INSTANTIATE_ReadBufferSizeReflectionTest(read_buffer_size);

std::vector<uint8_t> read_buffer_size_complete = {0x0e, 0x0b, 0x01, 0x05, 0x10, 0x00, 0x00,
                                                  0x04, 0x3c, 0x07, 0x00, 0x08, 0x00};
DEFINE_AND_INSTANTIATE_ReadBufferSizeCompleteReflectionTest(read_buffer_size_complete);

std::vector<uint8_t> host_buffer_size = {0x33, 0x0c, 0x07, 0x9b, 0x06, 0xff, 0x14, 0x00, 0x0a, 0x00};
DEFINE_AND_INSTANTIATE_HostBufferSizeReflectionTest(host_buffer_size);

std::vector<uint8_t> host_buffer_size_complete = {0x0e, 0x04, 0x01, 0x33, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_HostBufferSizeCompleteReflectionTest(host_buffer_size_complete);

std::vector<uint8_t> read_local_version_information = {0x01, 0x10, 0x00};
DEFINE_AND_INSTANTIATE_ReadLocalVersionInformationReflectionTest(read_local_version_information);

std::vector<uint8_t> read_local_version_information_complete = {0x0e, 0x0c, 0x01, 0x01, 0x10, 0x00, 0x09,
                                                                0x00, 0x00, 0x09, 0x1d, 0x00, 0xbe, 0x02};
DEFINE_AND_INSTANTIATE_ReadLocalVersionInformationCompleteReflectionTest(read_local_version_information_complete);

std::vector<uint8_t> read_bd_addr = {0x09, 0x10, 0x00};
DEFINE_AND_INSTANTIATE_ReadBdAddrReflectionTest(read_bd_addr);

std::vector<uint8_t> read_bd_addr_complete = {0x0e, 0x0a, 0x01, 0x09, 0x10, 0x00, 0x14, 0x8e, 0x61, 0x5f, 0x36, 0x88};
DEFINE_AND_INSTANTIATE_ReadBdAddrCompleteReflectionTest(read_bd_addr_complete);

std::vector<uint8_t> read_local_supported_commands = {0x02, 0x10, 0x00};
DEFINE_AND_INSTANTIATE_ReadLocalSupportedCommandsReflectionTest(read_local_supported_commands);

std::vector<uint8_t> read_local_supported_commands_complete = {
    0x0e, 0x44, 0x01, 0x02, 0x10, 0x00, /* Supported commands start here (total 64 bytes) */
    0xff, 0xff, 0xff, 0x03, 0xce, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xf2, 0x0f, 0xe8, 0xfe,
    0x3f, 0xf7, 0x83, 0xff, 0x1c, 0x00, 0x00, 0x00, 0x61, 0xff, 0xff, 0xff, 0x7f, 0xbe, 0x20, 0xf5,
    0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
DEFINE_AND_INSTANTIATE_ReadLocalSupportedCommandsCompleteReflectionTest(read_local_supported_commands_complete);

std::vector<uint8_t> read_local_extended_features_0 = {0x04, 0x10, 0x01, 0x00};

std::vector<uint8_t> read_local_extended_features_complete_0 = {0x0e, 0x0e, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02,
                                                                0xff, 0xfe, 0x8f, 0xfe, 0xd8, 0x3f, 0x5b, 0x87};

std::vector<uint8_t> write_simple_paring_mode = {0x56, 0x0c, 0x01, 0x01};
DEFINE_AND_INSTANTIATE_WriteSimplePairingModeReflectionTest(write_simple_paring_mode);

std::vector<uint8_t> write_simple_paring_mode_complete = {0x0e, 0x04, 0x01, 0x56, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_WriteSimplePairingModeCompleteReflectionTest(write_simple_paring_mode_complete);

std::vector<uint8_t> write_le_host_supported = {0x6d, 0x0c, 0x02, 0x01, 0x01};
DEFINE_AND_INSTANTIATE_WriteLeHostSupportReflectionTest(write_le_host_supported);

std::vector<uint8_t> write_le_host_supported_complete = {0x0e, 0x04, 0x01, 0x6d, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_WriteLeHostSupportCompleteReflectionTest(write_le_host_supported_complete);

std::vector<uint8_t> read_local_extended_features_1 = {0x04, 0x10, 0x01, 0x01};

std::vector<uint8_t> read_local_extended_features_complete_1 = {0x0e, 0x0e, 0x01, 0x04, 0x10, 0x00, 0x01, 0x02,
                                                                0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

std::vector<uint8_t> read_local_extended_features_2 = {0x04, 0x10, 0x01, 0x02};
DEFINE_AND_INSTANTIATE_ReadLocalExtendedFeaturesReflectionTest(read_local_extended_features_0,
                                                               read_local_extended_features_1,
                                                               read_local_extended_features_2);

std::vector<uint8_t> read_local_extended_features_complete_2 = {0x0e, 0x0e, 0x01, 0x04, 0x10, 0x00, 0x02, 0x02,
                                                                0x45, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
DEFINE_AND_INSTANTIATE_ReadLocalExtendedFeaturesCompleteReflectionTest(read_local_extended_features_complete_0,
                                                                       read_local_extended_features_complete_1,
                                                                       read_local_extended_features_complete_2);

std::vector<uint8_t> write_secure_connections_host_support = {0x7a, 0x0c, 0x01, 0x01};
DEFINE_AND_INSTANTIATE_WriteSecureConnectionsHostSupportReflectionTest(write_secure_connections_host_support);

std::vector<uint8_t> write_secure_connections_host_support_complete = {0x0e, 0x04, 0x01, 0x7a, 0x0c, 0x00};
DEFINE_AND_INSTANTIATE_WriteSecureConnectionsHostSupportCompleteReflectionTest(
    write_secure_connections_host_support_complete);

std::vector<uint8_t> le_read_white_list_size = {0x0f, 0x20, 0x00};
DEFINE_AND_INSTANTIATE_LeReadWhiteListSizeReflectionTest(le_read_white_list_size);

std::vector<uint8_t> le_read_white_list_size_complete = {0x0e, 0x05, 0x01, 0x0f, 0x20, 0x00, 0x80};
DEFINE_AND_INSTANTIATE_LeReadWhiteListSizeCompleteReflectionTest(le_read_white_list_size_complete);

std::vector<uint8_t> le_read_buffer_size = {0x02, 0x20, 0x00};
DEFINE_AND_INSTANTIATE_LeReadBufferSizeReflectionTest(le_read_buffer_size);

std::vector<uint8_t> le_read_buffer_size_complete = {0x0e, 0x07, 0x01, 0x02, 0x20, 0x00, 0xfb, 0x00, 0x10};
DEFINE_AND_INSTANTIATE_LeReadBufferSizeCompleteReflectionTest(le_read_buffer_size_complete);

}  // namespace hci
}  // namespace bluetooth
+20 −127
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 */

#define PACKET_TESTING
#include "l2cap/l2cap_packets.h"

#include <gtest/gtest.h>
@@ -28,139 +29,31 @@ using bluetooth::packet::BitInserter;
using bluetooth::packet::RawBuilder;
using std::vector;

namespace {
vector<uint8_t> extended_information_start_frame = {
    0x0B /* First size byte */,
    0x00 /* Second size byte */,
    0xc1 /* First ChannelId byte */,
    0xc2,
    0x4A /* 0x12 ReqSeq, Final, IFrame */,
    0xD0 /* 0x13 ReqSeq */,
    0x89 /* 0x21 TxSeq sar = START */,
    0x8C /* 0x23 TxSeq  */,
    0x10 /* first length byte */,
    0x11,
    0x01 /* first payload byte */,
    0x02,
    0x03,
    0x04,
    0x05,
};
}  // namespace

namespace bluetooth {
namespace l2cap {

TEST(L2capPacketTest, extendedInformationStartFrameTest) {
  uint16_t channel_id = 0xc2c1;
  uint16_t l2cap_sdu_length = 0x1110;
  Final f = Final::POLL_RESPONSE;
  uint16_t req_seq = 0x1312;
  uint16_t tx_seq = 0x2321;

  std::unique_ptr<RawBuilder> payload = std::make_unique<RawBuilder>();
  payload->AddOctets4(0x04030201);
  payload->AddOctets1(0x05);

  auto packet = ExtendedInformationStartFrameBuilder::Create(channel_id, f, req_seq, tx_seq, l2cap_sdu_length,
                                                             std::move(payload));

  ASSERT_EQ(extended_information_start_frame.size(), packet->size());
  std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
  BitInserter it(*packet_bytes);
  packet->Serialize(it);
  PacketView<true> packet_bytes_view(packet_bytes);
  ASSERT_EQ(extended_information_start_frame.size(), packet_bytes_view.size());

  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 = StandardFrameView::Create(basic_frame_view);
  ASSERT_TRUE(standard_frame_view.IsValid());
  ASSERT_EQ(FrameType::I_FRAME, standard_frame_view.GetFrameType());
}

vector<uint8_t> i_frame_with_fcs = {
    0x0E, 0x00, 0x40, 0x00, 0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x38, 0x61,
};
TEST(L2capPacketTest, iFrameWithFcsTest) {
  uint16_t channel_id = 0x0040;
  SegmentationAndReassembly sar = SegmentationAndReassembly::UNSEGMENTED;  // 0
  uint16_t req_seq = 0;
  uint16_t tx_seq = 1;
  RetransmissionDisable r = RetransmissionDisable::NORMAL;  // 0

  std::unique_ptr<RawBuilder> payload = std::make_unique<RawBuilder>();
  vector<uint8_t> payload_bytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
  payload->AddOctets(payload_bytes);

  auto packet = StandardInformationFrameWithFcsBuilder::Create(channel_id, tx_seq, r, req_seq, sar, std::move(payload));

  ASSERT_EQ(i_frame_with_fcs.size(), packet->size());
  std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
  BitInserter it(*packet_bytes);
  packet->Serialize(it);
  PacketView<true> packet_bytes_view(packet_bytes);
  ASSERT_EQ(i_frame_with_fcs.size(), packet_bytes_view.size());

  for (size_t i = 0; i < i_frame_with_fcs.size(); i++) {
    ASSERT_EQ(i_frame_with_fcs[i], packet_bytes_view[i]);
  }

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

  StandardFrameWithFcsView standard_frame_view = StandardFrameWithFcsView::Create(basic_frame_view);
  ASSERT_TRUE(standard_frame_view.IsValid());
  ASSERT_EQ(FrameType::I_FRAME, standard_frame_view.GetFrameType());

  StandardInformationFrameWithFcsView information_frame_view =
      StandardInformationFrameWithFcsView::Create(standard_frame_view);
  ASSERT_TRUE(information_frame_view.IsValid());
  ASSERT_EQ(sar, information_frame_view.GetSar());
  ASSERT_EQ(req_seq, information_frame_view.GetReqSeq());
  ASSERT_EQ(tx_seq, information_frame_view.GetTxSeq());
  ASSERT_EQ(r, information_frame_view.GetR());
}

vector<uint8_t> rr_frame_with_fcs = {
    0x04, 0x00, 0x40, 0x00, 0x01, 0x01, 0xD4, 0x14,
std::vector<uint8_t> extended_information_start_frame = {
    0x0B, /* First size byte */
    0x00, /* Second size byte */
    0xc1, /* First ChannelId byte */
    0xc2, /**/
    0x4A, /* 0x12 ReqSeq, Final, IFrame */
    0xD0, /* 0x13 ReqSeq */
    0x89, /* 0x21 TxSeq sar = START */
    0x8C, /* 0x23 TxSeq  */
    0x10, /* first length byte */
    0x11, /**/
    0x01, /* first payload byte */
    0x02, 0x03, 0x04, 0x05,
};
TEST(L2capPacketTest, rrFrameWithFcsTest) {
  uint16_t channel_id = 0x0040;
  SupervisoryFunction s = SupervisoryFunction::RECEIVER_READY;  // 0
  RetransmissionDisable r = RetransmissionDisable::NORMAL;      // 0
  uint16_t req_seq = 1;

  auto packet = StandardSupervisoryFrameWithFcsBuilder::Create(channel_id, s, r, req_seq);

  ASSERT_EQ(rr_frame_with_fcs.size(), packet->size());
  std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
  BitInserter it(*packet_bytes);
  packet->Serialize(it);
  PacketView<true> packet_bytes_view(packet_bytes);
  ASSERT_EQ(rr_frame_with_fcs.size(), packet_bytes_view.size());

  for (size_t i = 0; i < rr_frame_with_fcs.size(); i++) {
    ASSERT_EQ(rr_frame_with_fcs[i], packet_bytes_view[i]);
  }

  BasicFrameWithFcsView basic_frame_view = BasicFrameWithFcsView::Create(packet_bytes_view);
  ASSERT_TRUE(basic_frame_view.IsValid());
  ASSERT_EQ(channel_id, basic_frame_view.GetChannelId());
DEFINE_AND_INSTANTIATE_ExtendedInformationStartFrameReflectionTest(extended_information_start_frame);

  StandardFrameWithFcsView standard_frame_view = StandardFrameWithFcsView::Create(basic_frame_view);
  ASSERT_TRUE(standard_frame_view.IsValid());
  ASSERT_EQ(FrameType::S_FRAME, standard_frame_view.GetFrameType());
std::vector<uint8_t> i_frame_with_fcs = {0x0E, 0x00, 0x40, 0x00, 0x02, 0x00, 0x00, 0x01, 0x02,
                                         0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x38, 0x61};
DEFINE_AND_INSTANTIATE_StandardInformationFrameWithFcsReflectionTest(i_frame_with_fcs);

  StandardSupervisoryFrameWithFcsView supervisory_frame_view =
      StandardSupervisoryFrameWithFcsView::Create(standard_frame_view);
  ASSERT_TRUE(supervisory_frame_view.IsValid());
  ASSERT_EQ(s, supervisory_frame_view.GetS());
  ASSERT_EQ(r, supervisory_frame_view.GetR());
  ASSERT_EQ(req_seq, supervisory_frame_view.GetReqSeq());
}
std::vector<uint8_t> rr_frame_with_fcs = {0x04, 0x00, 0x40, 0x00, 0x01, 0x01, 0xD4, 0x14};
DEFINE_AND_INSTANTIATE_StandardSupervisoryFrameWithFcsReflectionTest(rr_frame_with_fcs);
}  // namespace l2cap
}  // namespace bluetooth
+6 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "fields/packet_field.h"

#include "util.h"

PacketField::PacketField(std::string name, ParseLocation loc) : loc_(loc), name_(name) {}

std::string PacketField::GetDebugName() const {
@@ -66,3 +68,7 @@ void PacketField::GenBounds(std::ostream& s, Size start_offset, Size end_offset,
bool PacketField::GenBuilderMember(std::ostream& s) const {
  return GenBuilderParameter(s);
}

void PacketField::GenBuilderParameterFromView(std::ostream& s) const {
  s << "view.Get" << util::UnderscoreToCamelCase(GetName()) << "()";
}
Loading