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

Commit cc366ee7 authored by Myles Watson's avatar Myles Watson Committed by Jakub Pawlowski
Browse files

PDL: Add big endian test packets

Bug: 138260498
Test: bluetooth_packet_parser_test
Change-Id: I2c6ef6b70afe38666052f6d70c19342dcf764d81
parent 8d6fc629
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -6,9 +6,11 @@ genrule {
    cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in)",
    srcs: [
        "test_packets.pdl",
        "big_endian_test_packets.pdl",
    ],
    out: [
        "packet/parser/test/test_packets.h",
        "packet/parser/test/big_endian_test_packets.h",
    ],
}

+266 −0
Original line number Diff line number Diff line
big_endian_packets

custom_field SixBytes : 48 "packet/parser/test/"
custom_field Variable "packet/parser/test/"

packet ParentBe {
  _fixed_ = 0x12 : 8,
  _size_(_payload_) : 8,
  _payload_,
  footer : 8,
}

packet ChildBe  : ParentBe {
  field_name : 16,
}

enum FourBitsBe : 4 {
  ONE = 1,
  TWO = 2,
  THREE = 3,
  FIVE = 5,
  TEN = 10,
  LAZY_ME = 15,
}

packet ParentTwoBe {
  _reserved_ : 4,
  four_bits : FourBitsBe,
  _payload_,
}

packet ChildTwoThreeBe  : ParentTwoBe (four_bits = THREE) {
  more_bits : FourBitsBe,
  _reserved_ : 4,
  sixteen_bits : 16
}

packet ChildTwoTwoBe  : ParentTwoBe (four_bits = TWO) {
  more_bits : FourBitsBe,
  _reserved_ : 4,
}

packet ChildTwoTwoThreeBe  :ChildTwoTwoBe (more_bits = THREE) {
}

enum TwoBitsBe : 2 {
  ZERO = 0,
  ONE = 1,
  TWO = 2,
  THREE = 3,
}

packet MiddleFourBitsBe {
  low_two : TwoBitsBe,
  next_four : FourBitsBe,
  straddle : FourBitsBe,
  four_more : FourBitsBe,
  high_two : TwoBitsBe,
}

packet ParentWithSixBytesBe {
  two_bytes : 16,
  six_bytes : SixBytes,
  _payload_,
}

packet ChildWithSixBytesBe  : ParentWithSixBytesBe (two_bytes = 0x1234) {
  child_six_bytes : SixBytes,
}

checksum SimpleSum : 16 "packet/parser/test/"

packet ParentWithSumBe {
  two_bytes : 16,
  _checksum_start_(example_checksum),
  sum_bytes : 16,
  _payload_,
  example_checksum : SimpleSum,
}

packet ChildWithSumBe  : ParentWithSumBe {
  more_bytes : 32,
  another_byte : 8,
}

packet ChildWithNestedSumBe  : ParentWithSumBe {
  _checksum_start_(nested_checksum),
  more_bytes : 32,
  nested_checksum : SimpleSum,
}

packet ParentSizeModifierBe {
  _size_(_payload_) : 8,
  _payload_ : [+2*8], // Include two_bytes in the size
  two_bytes : 16,
}

packet ChildSizeModifierBe  : ParentSizeModifierBe (two_bytes = 0x1211) {
  more_bytes : 32,
}

enum ForArraysBe : 16 {
  ONE = 0x0001,
  TWO = 0x0002,
  ONE_TWO = 0x0201,
  TWO_THREE = 0x0302,
  FFFF = 0xffff,
}

packet FixedArrayEnumBe {
  enum_array : ForArraysBe[5],
}

packet SizedArrayEnumBe {
  _size_(enum_array) : 16,
  enum_array : ForArraysBe[],
}

packet CountArrayEnumBe {
  _count_(enum_array) : 8,
  enum_array : ForArraysBe[],
}

packet SizedArrayCustomBe {
  _size_(six_bytes_array) : 8,
  an_extra_byte : 8,
  six_bytes_array : SixBytes[+1*8],
}

packet FixedArrayCustomBe {
  six_bytes_array : SixBytes[5],
}

packet CountArrayCustomBe {
  _count_(six_bytes_array) : 8,
  six_bytes_array : SixBytes[],
}

packet PacketWithFixedArraysOfBytesBe {
  fixed_256bit_in_bytes : 8[32],
  fixed_256bit_in_words : 32[8],
}

packet OneVariableBe {
  one : Variable,
}

packet SizedArrayVariableBe {
  _size_(variable_array) : 8,
  variable_array : Variable[],
}

packet FixedArrayVariableBe {
  variable_array : Variable[5],
}

packet CountArrayVariableBe {
  _count_(variable_array) : 8,
  variable_array : Variable[],
}

struct TwoRelatedNumbersBe {
  id : 8,
  count : 16,
}

packet OneStructBe {
  one : TwoRelatedNumbersBe,
}

packet TwoStructsBe {
  one : TwoRelatedNumbersBe,
  two : TwoRelatedNumbersBe,
}

packet ArrayOfStructBe {
  _count_(array) : 8,
  array : TwoRelatedNumbersBe[],
}

struct StructWithFixedTypesBe {
  four_bits : FourBitsBe,
  _reserved_ : 4,
  _checksum_start_(example_checksum),
  _fixed_ = 0xf3 : 8,
  id : 8,
  array : 8[3],
  example_checksum : SimpleSum,
  six_bytes : SixBytes,
}

packet OneFixedTypesStructBe {
  one : StructWithFixedTypesBe,
}

packet ArrayOfStructAndAnotherBe {
  _count_(array) : 8,
  array : TwoRelatedNumbersBe[],
  another : TwoRelatedNumbersBe,
}

group BitFieldGroupBe {
  seven_bits : 7,
  straddle : 4,
  five_bits : 5,
}

packet BitFieldGroupPacketBe {
  BitFieldGroupBe,
}

packet BitFieldGroupAfterPayloadPacketBe {
  _payload_,
  BitFieldGroupBe,
}

packet BitFieldGroupAfterUnsizedArrayPacketBe  : BitFieldGroupAfterPayloadPacketBe {
  array : 8[],
}

struct BitFieldBe {
  seven_bits : 7,
  straddle : 4,
  five_bits : 5,
}

packet BitFieldPacketBe {
  bit_field : BitFieldBe,
}

packet BitFieldAfterPayloadPacketBe {
  _payload_,
  bit_field : BitFieldBe,
}

packet BitFieldAfterUnsizedArrayPacketBe  : BitFieldAfterPayloadPacketBe {
  array : 8[],
}

packet BitFieldArrayPacketBe {
  _size_(array): 8,
  array : BitFieldBe[],
}

struct VersionlessStructBe {
  one_number : 8,
}

packet OneVersionlessStructPacketBe {
  versionless : VersionlessStructBe,
  _payload_,
}

packet OneVersionedStructPacketBe  : OneVersionlessStructPacketBe {
  version : 8,
  _payload_,
}

packet OneVersionOneStructPacketBe  : OneVersionedStructPacketBe (version = 0x01) {
  just_one_number : 8,
}

packet OneVersionTwoStructPacketBe  : OneVersionedStructPacketBe (version = 0x02) {
  one_number : 8,
  another_number : 8,
}
+106 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */

#define PACKET_TESTING
#include "packet/parser/test/big_endian_test_packets.h"
#include "packet/parser/test/test_packets.h"

#include <gtest/gtest.h>
@@ -1171,6 +1172,111 @@ DEFINE_AND_INSTANTIATE_OneVersionedStructPacketReflectionTest(one_versioned_stru
                                                              one_version_two_struct_packet);
DEFINE_AND_INSTANTIATE_OneVersionOneStructPacketReflectionTest(one_version_one_struct_packet);
DEFINE_AND_INSTANTIATE_OneVersionTwoStructPacketReflectionTest(one_version_two_struct_packet);

vector<uint8_t> one_struct_be{
    0x01, 0x02, 0x03,  // id = 0x01, count = 0x0203
};

TEST(GeneratedPacketTest, testOneStructBe) {
  TwoRelatedNumbersBe trn;
  trn.id_ = 1;
  trn.count_ = 0x0203;

  auto packet = OneStructBeBuilder::Create(trn);
  ASSERT_EQ(one_struct_be.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);

  ASSERT_EQ(one_struct_be.size(), packet_bytes->size());
  for (size_t i = 0; i < one_struct_be.size(); i++) {
    ASSERT_EQ(one_struct_be[i], packet_bytes->at(i));
  }

  PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
  auto view = OneStructBeView::Create(packet_bytes_view);
  ASSERT_TRUE(view.IsValid());
  auto one = view.GetOne();
  ASSERT_EQ(one.id_, trn.id_);
  ASSERT_EQ(one.count_, trn.count_);
}

vector<uint8_t> two_structs_be{
    0x01, 0x01, 0x02,  // id, id * 0x0102
    0x02, 0x02, 0x04,
};

TEST(GeneratedPacketTest, testTwoStructsBe) {
  std::vector<TwoRelatedNumbersBe> count_array;
  for (uint8_t i = 1; i < 3; i++) {
    TwoRelatedNumbersBe trn;
    trn.id_ = i;
    trn.count_ = 0x0102 * i;
    count_array.push_back(trn);
  }

  auto packet = TwoStructsBeBuilder::Create(count_array[0], count_array[1]);
  ASSERT_EQ(two_structs_be.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);

  ASSERT_EQ(two_structs_be.size(), packet_bytes->size());
  for (size_t i = 0; i < two_structs_be.size(); i++) {
    ASSERT_EQ(two_structs_be[i], packet_bytes->at(i));
  }

  PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
  auto view = TwoStructsBeView::Create(packet_bytes_view);
  ASSERT_TRUE(view.IsValid());
  auto one = view.GetOne();
  ASSERT_EQ(one.id_, count_array[0].id_);
  ASSERT_EQ(one.count_, count_array[0].count_);
  auto two = view.GetTwo();
  ASSERT_EQ(two.id_, count_array[1].id_);
  ASSERT_EQ(two.count_, count_array[1].count_);
}

vector<uint8_t> array_of_struct_be{
    0x04,              // _count_
    0x01, 0x01, 0x02,  // id, id * 0x0102
    0x02, 0x02, 0x04, 0x03, 0x03, 0x06, 0x04, 0x04, 0x08,
};

TEST(GeneratedPacketTest, testArrayOfStructBe) {
  std::vector<TwoRelatedNumbersBe> count_array;
  for (uint8_t i = 1; i < 5; i++) {
    TwoRelatedNumbersBe trn;
    trn.id_ = i;
    trn.count_ = 0x0102 * i;
    count_array.push_back(trn);
  }

  auto packet = ArrayOfStructBeBuilder::Create(count_array);

  ASSERT_EQ(array_of_struct_be.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);

  ASSERT_EQ(array_of_struct_be.size(), packet_bytes->size());
  for (size_t i = 0; i < array_of_struct_be.size(); i++) {
    ASSERT_EQ(array_of_struct_be[i], packet_bytes->at(i));
  }

  PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
  auto view = ArrayOfStructBeView::Create(packet_bytes_view);
  ASSERT_TRUE(view.IsValid());
  auto array = view.GetArray();
  ASSERT_EQ(count_array.size(), array.size());
  for (size_t i = 0; i < count_array.size(); i++) {
    ASSERT_EQ(array[i].id_, count_array[i].id_);
    ASSERT_EQ(array[i].count_, count_array[i].count_);
  }
}
}  // namespace parser
}  // namespace packet
}  // namespace bluetooth