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 "avrcp_packet.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "avrcp_test_packets.h"
22 #include "packet_test_helper.h"
23 
24 namespace bluetooth {
25 
26 // A helper class that has public accessors to protected methods
27 class TestPacketBuilder : public PacketBuilder {
28 public:
MakeBuilder(std::vector<uint8_t> data)29   static std::unique_ptr<TestPacketBuilder> MakeBuilder(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::AddPayloadOctets1;
36   using PacketBuilder::AddPayloadOctets2;
37   using PacketBuilder::AddPayloadOctets3;
38   using PacketBuilder::AddPayloadOctets4;
39   using PacketBuilder::AddPayloadOctets6;
40   using PacketBuilder::AddPayloadOctets8;
41   using PacketBuilder::ReserveSpace;
42 
size() const43   size_t size() const override { return data_.size(); }
44 
Serialize(const std::shared_ptr<Packet> & pkt)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 
TestPacketBuilder(std::vector<uint8_t> data)55   TestPacketBuilder(std::vector<uint8_t> data) : data_(data) {}
56 
57   std::vector<uint8_t> data_;
58 };
59 
60 namespace avrcp {
61 
62 using TestAvrcpPacket = TestPacketType<Packet>;
63 
TEST(AvrcpPacketBuilderTest,buildPacketTest)64 TEST(AvrcpPacketBuilderTest, buildPacketTest) {
65   std::vector<uint8_t> get_capabilities_request_payload = {0x00, 0x19, 0x58, 0x10,
66                                                            0x00, 0x00, 0x01, 0x03};
67   auto cap_req_builder = TestPacketBuilder::MakeBuilder(get_capabilities_request_payload);
68 
69   auto builder = PacketBuilder::MakeBuilder(CType::STATUS, 0x09, 0x00, Opcode::VENDOR,
70                                             std::move(cap_req_builder));
71 
72   ASSERT_EQ(builder->size(), get_capabilities_request.size());
73 
74   auto test_packet = TestAvrcpPacket::Make();
75   builder->Serialize(test_packet);
76   ASSERT_EQ(test_packet->GetData(), get_capabilities_request);
77 }
78 
TEST(AvrcpPacketTest,getterTests)79 TEST(AvrcpPacketTest, getterTests) {
80   auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request);
81 
82   ASSERT_EQ(test_avrcp_packet->GetCType(), CType::STATUS);
83   ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0x09);
84   ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0x00);
85   ASSERT_EQ(test_avrcp_packet->GetOpcode(), Opcode::VENDOR);
86 }
87 
TEST(AvrcpPacketTest,getterMaskTests)88 TEST(AvrcpPacketTest, getterMaskTests) {
89   auto bad_get_cap_data = get_capabilities_request;
90   bad_get_cap_data[0] = 0xFF;  // CType
91   bad_get_cap_data[1] = 0xFF;  // Subunit Type & ID
92 
93   auto test_avrcp_packet = TestAvrcpPacket::Make(bad_get_cap_data);
94 
95   ASSERT_EQ(test_avrcp_packet->GetCType(), CType::INTERIM);
96   ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0b00011111);
97   ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0b00000111);
98 }
99 
TEST(AvrcpPacketTest,payloadBoundsTest)100 TEST(AvrcpPacketTest, payloadBoundsTest) {
101   auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request);
102 
103   std::vector<uint8_t> get_cap_payload_data = {0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x03};
104 
105   auto get_cap_payload_packet = TestAvrcpPacket::Make(test_avrcp_packet);
106 
107   // We are unable to do a direct vector compare here as one of the packets is
108   // a larger vector that only has a segment of data currently visible.
109   for (size_t i = 0; i < get_cap_payload_data.size(); i++) {
110     ASSERT_EQ(get_cap_payload_data[i], (*get_cap_payload_packet)[i]);
111   }
112 }
113 
114 }  // namespace avrcp
115 }  // namespace bluetooth
116