avrcp_packet_test.cc revision 8f7377353db29efe0a506123deec03d70935957a
1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include "avrcp_packet.h"
20#include "avrcp_test_packets.h"
21#include "packet_test_helper.h"
22
23namespace bluetooth {
24
25// A helper class that has public accessors to protected methods
26class TestPacketBuilder : public PacketBuilder {
27 public:
28  static std::unique_ptr<TestPacketBuilder> MakeBuilder(
29      std::vector<uint8_t> data) {
30    std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
31    return builder;
32  };
33
34  // Make all the utility functions public
35  using PacketBuilder::ReserveSpace;
36  using PacketBuilder::AddPayloadOctets1;
37  using PacketBuilder::AddPayloadOctets2;
38  using PacketBuilder::AddPayloadOctets3;
39  using PacketBuilder::AddPayloadOctets4;
40  using PacketBuilder::AddPayloadOctets6;
41  using PacketBuilder::AddPayloadOctets8;
42
43  size_t size() const override { return data_.size(); };
44
45  bool Serialize(const std::shared_ptr<Packet>& pkt) override {
46    ReserveSpace(pkt, size());
47
48    for (uint8_t byte : data_) {
49      AddPayloadOctets1(pkt, byte);
50    }
51
52    return true;
53  }
54
55  TestPacketBuilder(std::vector<uint8_t> data) : data_(data){};
56
57  std::vector<uint8_t> data_;
58};
59
60namespace avrcp {
61
62using TestAvrcpPacket = TestPacketType<Packet>;
63
64TEST(AvrcpPacketBuilderTest, buildPacketTest) {
65  std::vector<uint8_t> get_capabilities_request_payload = {
66      0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x03};
67  auto cap_req_builder =
68      TestPacketBuilder::MakeBuilder(get_capabilities_request_payload);
69
70  auto builder = PacketBuilder::MakeBuilder(
71      CType::STATUS, 0x09, 0x00, Opcode::VENDOR, std::move(cap_req_builder));
72
73  ASSERT_EQ(builder->size(), get_capabilities_request.size());
74
75  auto test_packet = TestAvrcpPacket::Make();
76  builder->Serialize(test_packet);
77  ASSERT_EQ(test_packet->GetData(), get_capabilities_request);
78}
79
80TEST(AvrcpPacketTest, getterTests) {
81  auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request);
82
83  ASSERT_EQ(test_avrcp_packet->GetCType(), CType::STATUS);
84  ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0x09);
85  ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0x00);
86  ASSERT_EQ(test_avrcp_packet->GetOpcode(), Opcode::VENDOR);
87}
88
89TEST(AvrcpPacketTest, getterMaskTests) {
90  auto bad_get_cap_data = get_capabilities_request;
91  bad_get_cap_data[0] = 0xFF;  // CType
92  bad_get_cap_data[1] = 0xFF;  // Subunit Type & ID
93
94  auto test_avrcp_packet = TestAvrcpPacket::Make(bad_get_cap_data);
95
96  ASSERT_EQ(test_avrcp_packet->GetCType(), CType::INTERIM);
97  ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0b00011111);
98  ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0b00000111);
99}
100
101TEST(AvrcpPacketTest, payloadBoundsTest) {
102  auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request);
103
104  std::vector<uint8_t> get_cap_payload_data = {0x00, 0x19, 0x58, 0x10,
105                                               0x00, 0x00, 0x01, 0x03};
106
107  auto get_cap_payload_packet = TestAvrcpPacket::Make(test_avrcp_packet);
108
109  // We are unable to do a direct vector compare here as one of the packets is
110  // a larger vector that only has a segment of data currently visible.
111  for (size_t i = 0; i < get_cap_payload_data.size(); i++) {
112    ASSERT_EQ(get_cap_payload_data[i], (*get_cap_payload_packet)[i]);
113  }
114}
115
116}  // namespace avrcp
117}  // namespace bluetooth