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 "vendor_packet.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <tuple>
22 
23 #include "avrcp_test_packets.h"
24 #include "packet_test_helper.h"
25 
26 namespace bluetooth {
27 
28 // A helper class that has public accessors to protected methods
29 class TestPacketBuilder : public PacketBuilder {
30 public:
MakeBuilder(std::vector<uint8_t> data)31   static std::unique_ptr<TestPacketBuilder> MakeBuilder(std::vector<uint8_t> data) {
32     std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
33     return builder;
34   }
35 
36   // Make all the utility functions public
37   using PacketBuilder::AddPayloadOctets1;
38   using PacketBuilder::AddPayloadOctets2;
39   using PacketBuilder::AddPayloadOctets3;
40   using PacketBuilder::AddPayloadOctets4;
41   using PacketBuilder::AddPayloadOctets6;
42   using PacketBuilder::AddPayloadOctets8;
43   using PacketBuilder::ReserveSpace;
44 
size() const45   size_t size() const override { return data_.size(); }
46 
Serialize(const std::shared_ptr<Packet> & pkt)47   bool Serialize(const std::shared_ptr<Packet>& pkt) override {
48     ReserveSpace(pkt, size());
49 
50     for (uint8_t byte : data_) {
51       AddPayloadOctets1(pkt, byte);
52     }
53 
54     return true;
55   }
56 
TestPacketBuilder(std::vector<uint8_t> data)57   TestPacketBuilder(std::vector<uint8_t> data) : data_(data) {}
58 
59   std::vector<uint8_t> data_;
60 };
61 
62 namespace avrcp {
63 
64 using TestVendorPacket = TestPacketType<VendorPacket>;
65 
TEST(VendorPacketBuilderTest,builderTest)66 TEST(VendorPacketBuilderTest, builderTest) {
67   std::vector<uint8_t> get_cap_payload_data = {0x03};
68 
69   auto get_cap_payload = TestPacketBuilder::MakeBuilder(get_cap_payload_data);
70 
71   auto builder = VendorPacketBuilder::MakeBuilder(CType::STATUS, CommandPdu::GET_CAPABILITIES,
72                                                   PacketType::SINGLE, std::move(get_cap_payload));
73 
74   ASSERT_EQ(builder->size(), get_capabilities_request.size());
75 
76   auto test_packet = TestVendorPacket::Make();
77   builder->Serialize(test_packet);
78   ASSERT_EQ(test_packet->GetData(), get_capabilities_request);
79 }
80 
81 using TestParam = std::tuple<std::vector<uint8_t>, CommandPdu>;
82 class VendorPacketTest : public ::testing::TestWithParam<TestParam> {
83 public:
GetPacketData()84   std::vector<uint8_t> GetPacketData() { return std::get<0>(GetParam()); }
GetCommandPdu()85   CommandPdu GetCommandPdu() { return std::get<1>(GetParam()); }
86 };
87 
88 INSTANTIATE_TEST_CASE_P(
89         VendorPacketParameterTest, VendorPacketTest,
90         ::testing::Values(
91                 std::make_tuple(get_capabilities_request, CommandPdu::GET_CAPABILITIES),
92                 std::make_tuple(get_capabilities_response_company_id, CommandPdu::GET_CAPABILITIES),
93                 std::make_tuple(get_capabilities_response_events_supported,
94                                 CommandPdu::GET_CAPABILITIES),
95                 std::make_tuple(get_element_attributes_request_partial,
96                                 CommandPdu::GET_ELEMENT_ATTRIBUTES),
97                 std::make_tuple(get_element_attributes_request_full,
98                                 CommandPdu::GET_ELEMENT_ATTRIBUTES),
99                 std::make_tuple(get_elements_attributes_response_full,
100                                 CommandPdu::GET_ELEMENT_ATTRIBUTES),
101                 std::make_tuple(get_play_status_request, CommandPdu::GET_PLAY_STATUS),
102                 std::make_tuple(get_play_status_response, CommandPdu::GET_PLAY_STATUS),
103                 std::make_tuple(register_play_status_notification,
104                                 CommandPdu::REGISTER_NOTIFICATION),
105                 std::make_tuple(interim_play_status_notification,
106                                 CommandPdu::REGISTER_NOTIFICATION),
107                 std::make_tuple(interim_track_changed_notification,
108                                 CommandPdu::REGISTER_NOTIFICATION),
109                 std::make_tuple(changed_play_pos_notification, CommandPdu::REGISTER_NOTIFICATION)));
110 
TEST_P(VendorPacketTest,getterTest)111 TEST_P(VendorPacketTest, getterTest) {
112   auto test_packet = TestVendorPacket::Make(GetPacketData());
113 
114   ASSERT_EQ(test_packet->GetCompanyId(), BLUETOOTH_COMPANY_ID);
115   ASSERT_EQ(test_packet->GetCommandPdu(), GetCommandPdu());
116   ASSERT_EQ(test_packet->GetPacketType(), PacketType::SINGLE);
117   // ASSERT_EQ(test_packet->GetParameterLength(), 0x01);
118 }
119 
TEST_P(VendorPacketTest,validTest)120 TEST_P(VendorPacketTest, validTest) {
121   auto test_packet = TestVendorPacket::Make(GetPacketData());
122   ASSERT_TRUE(test_packet->IsValid());
123 }
124 
TEST_F(VendorPacketTest,invalidTest)125 TEST_F(VendorPacketTest, invalidTest) {
126   auto get_capabilities_request_copy = get_capabilities_request;
127   get_capabilities_request_copy.push_back(0x00);
128   auto test_packet = TestVendorPacket::Make(get_capabilities_request_copy);
129   ASSERT_FALSE(test_packet->IsValid());
130 
131   std::vector<uint8_t> short_packet = {0x01, 0x02, 0x03, 0x04, 0x05};
132   test_packet = TestVendorPacket::Make(short_packet);
133   ASSERT_FALSE(test_packet->IsValid());
134 }
135 
136 }  // namespace avrcp
137 }  // namespace bluetooth
138