1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tuple>
#include <gtest/gtest.h>
#include "avrcp_test_packets.h"
#include "packet_test_helper.h"
#include "vendor_packet.h"
namespace bluetooth {
// A helper class that has public accessors to protected methods
class TestPacketBuilder : public PacketBuilder {
public:
static std::unique_ptr<TestPacketBuilder> MakeBuilder(
std::vector<uint8_t> data) {
std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
return builder;
};
// Make all the utility functions public
using PacketBuilder::ReserveSpace;
using PacketBuilder::AddPayloadOctets1;
using PacketBuilder::AddPayloadOctets2;
using PacketBuilder::AddPayloadOctets3;
using PacketBuilder::AddPayloadOctets4;
using PacketBuilder::AddPayloadOctets6;
using PacketBuilder::AddPayloadOctets8;
size_t size() const override { return data_.size(); };
bool Serialize(const std::shared_ptr<Packet>& pkt) override {
ReserveSpace(pkt, size());
for (uint8_t byte : data_) {
AddPayloadOctets1(pkt, byte);
}
return true;
}
TestPacketBuilder(std::vector<uint8_t> data) : data_(data){};
std::vector<uint8_t> data_;
};
namespace avrcp {
using TestVendorPacket = TestPacketType<VendorPacket>;
TEST(VendorPacketBuilderTest, builderTest) {
std::vector<uint8_t> get_cap_payload_data = {0x03};
auto get_cap_payload = TestPacketBuilder::MakeBuilder(get_cap_payload_data);
auto builder = VendorPacketBuilder::MakeBuilder(
CType::STATUS, CommandPdu::GET_CAPABILITIES, PacketType::SINGLE,
std::move(get_cap_payload));
ASSERT_EQ(builder->size(), get_capabilities_request.size());
auto test_packet = TestVendorPacket::Make();
builder->Serialize(test_packet);
ASSERT_EQ(test_packet->GetData(), get_capabilities_request);
}
using TestParam = std::tuple<std::vector<uint8_t>, CommandPdu>;
class VendorPacketTest : public ::testing::TestWithParam<TestParam> {
public:
std::vector<uint8_t> GetPacketData() { return std::get<0>(GetParam()); }
CommandPdu GetCommandPdu() { return std::get<1>(GetParam()); }
};
INSTANTIATE_TEST_CASE_P(
VendorPacketParameterTest, VendorPacketTest,
::testing::Values(
std::make_tuple(get_capabilities_request, CommandPdu::GET_CAPABILITIES),
std::make_tuple(get_capabilities_response_company_id,
CommandPdu::GET_CAPABILITIES),
std::make_tuple(get_capabilities_response_events_supported,
CommandPdu::GET_CAPABILITIES),
std::make_tuple(get_element_attributes_request_partial,
CommandPdu::GET_ELEMENT_ATTRIBUTES),
std::make_tuple(get_element_attributes_request_full,
CommandPdu::GET_ELEMENT_ATTRIBUTES),
std::make_tuple(get_elements_attributes_response_full,
CommandPdu::GET_ELEMENT_ATTRIBUTES),
std::make_tuple(get_play_status_request, CommandPdu::GET_PLAY_STATUS),
std::make_tuple(get_play_status_response, CommandPdu::GET_PLAY_STATUS),
std::make_tuple(register_play_status_notification,
CommandPdu::REGISTER_NOTIFICATION),
std::make_tuple(interim_play_status_notification,
CommandPdu::REGISTER_NOTIFICATION),
std::make_tuple(interim_track_changed_notification,
CommandPdu::REGISTER_NOTIFICATION),
std::make_tuple(changed_play_pos_notification,
CommandPdu::REGISTER_NOTIFICATION)));
TEST_P(VendorPacketTest, getterTest) {
auto test_packet = TestVendorPacket::Make(GetPacketData());
ASSERT_EQ(test_packet->GetCompanyId(), BLUETOOTH_COMPANY_ID);
ASSERT_EQ(test_packet->GetCommandPdu(), GetCommandPdu());
ASSERT_EQ(test_packet->GetPacketType(), PacketType::SINGLE);
// ASSERT_EQ(test_packet->GetParameterLength(), 0x01);
}
TEST_P(VendorPacketTest, validTest) {
auto test_packet = TestVendorPacket::Make(GetPacketData());
ASSERT_TRUE(test_packet->IsValid());
}
TEST_F(VendorPacketTest, invalidTest) {
auto get_capabilities_request_copy = get_capabilities_request;
get_capabilities_request_copy.push_back(0x00);
auto test_packet = TestVendorPacket::Make(get_capabilities_request_copy);
ASSERT_FALSE(test_packet->IsValid());
std::vector<uint8_t> short_packet = {0x01, 0x02, 0x03, 0x04, 0x05};
test_packet = TestVendorPacket::Make(short_packet);
ASSERT_FALSE(test_packet->IsValid());
}
} // namespace avrcp
} // namespace bluetooth
|