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 <tuple>
18
19 #include <gtest/gtest.h>
20
21 #include "avrcp_test_packets.h"
22 #include "packet_test_helper.h"
23 #include "vendor_packet.h"
24
25 namespace bluetooth {
26
27 // A helper class that has public accessors to protected methods
28 class TestPacketBuilder : public PacketBuilder {
29 public:
MakeBuilder(std::vector<uint8_t> data)30 static std::unique_ptr<TestPacketBuilder> MakeBuilder(
31 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::ReserveSpace;
38 using PacketBuilder::AddPayloadOctets1;
39 using PacketBuilder::AddPayloadOctets2;
40 using PacketBuilder::AddPayloadOctets3;
41 using PacketBuilder::AddPayloadOctets4;
42 using PacketBuilder::AddPayloadOctets6;
43 using PacketBuilder::AddPayloadOctets8;
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(
72 CType::STATUS, CommandPdu::GET_CAPABILITIES, PacketType::SINGLE,
73 std::move(get_cap_payload));
74
75 ASSERT_EQ(builder->size(), get_capabilities_request.size());
76
77 auto test_packet = TestVendorPacket::Make();
78 builder->Serialize(test_packet);
79 ASSERT_EQ(test_packet->GetData(), get_capabilities_request);
80 }
81
82 using TestParam = std::tuple<std::vector<uint8_t>, CommandPdu>;
83 class VendorPacketTest : public ::testing::TestWithParam<TestParam> {
84 public:
GetPacketData()85 std::vector<uint8_t> GetPacketData() { return std::get<0>(GetParam()); }
GetCommandPdu()86 CommandPdu GetCommandPdu() { return std::get<1>(GetParam()); }
87 };
88
89 INSTANTIATE_TEST_CASE_P(
90 VendorPacketParameterTest, VendorPacketTest,
91 ::testing::Values(
92 std::make_tuple(get_capabilities_request, CommandPdu::GET_CAPABILITIES),
93 std::make_tuple(get_capabilities_response_company_id,
94 CommandPdu::GET_CAPABILITIES),
95 std::make_tuple(get_capabilities_response_events_supported,
96 CommandPdu::GET_CAPABILITIES),
97 std::make_tuple(get_element_attributes_request_partial,
98 CommandPdu::GET_ELEMENT_ATTRIBUTES),
99 std::make_tuple(get_element_attributes_request_full,
100 CommandPdu::GET_ELEMENT_ATTRIBUTES),
101 std::make_tuple(get_elements_attributes_response_full,
102 CommandPdu::GET_ELEMENT_ATTRIBUTES),
103 std::make_tuple(get_play_status_request, CommandPdu::GET_PLAY_STATUS),
104 std::make_tuple(get_play_status_response, CommandPdu::GET_PLAY_STATUS),
105 std::make_tuple(register_play_status_notification,
106 CommandPdu::REGISTER_NOTIFICATION),
107 std::make_tuple(interim_play_status_notification,
108 CommandPdu::REGISTER_NOTIFICATION),
109 std::make_tuple(interim_track_changed_notification,
110 CommandPdu::REGISTER_NOTIFICATION),
111 std::make_tuple(changed_play_pos_notification,
112 CommandPdu::REGISTER_NOTIFICATION)));
113
TEST_P(VendorPacketTest,getterTest)114 TEST_P(VendorPacketTest, getterTest) {
115 auto test_packet = TestVendorPacket::Make(GetPacketData());
116
117 ASSERT_EQ(test_packet->GetCompanyId(), BLUETOOTH_COMPANY_ID);
118 ASSERT_EQ(test_packet->GetCommandPdu(), GetCommandPdu());
119 ASSERT_EQ(test_packet->GetPacketType(), PacketType::SINGLE);
120 // ASSERT_EQ(test_packet->GetParameterLength(), 0x01);
121 }
122
TEST_P(VendorPacketTest,validTest)123 TEST_P(VendorPacketTest, validTest) {
124 auto test_packet = TestVendorPacket::Make(GetPacketData());
125 ASSERT_TRUE(test_packet->IsValid());
126 }
127
TEST_F(VendorPacketTest,invalidTest)128 TEST_F(VendorPacketTest, invalidTest) {
129 auto get_capabilities_request_copy = get_capabilities_request;
130 get_capabilities_request_copy.push_back(0x00);
131 auto test_packet = TestVendorPacket::Make(get_capabilities_request_copy);
132 ASSERT_FALSE(test_packet->IsValid());
133
134 std::vector<uint8_t> short_packet = {0x01, 0x02, 0x03, 0x04, 0x05};
135 test_packet = TestVendorPacket::Make(short_packet);
136 ASSERT_FALSE(test_packet->IsValid());
137 }
138
139 } // namespace avrcp
140 } // namespace bluetooth