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