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 <android-base/silent_death_test.h>
18 #include <gtest/gtest.h>
19
20 #include "avrcp_test_packets.h"
21 #include "capabilities_packet.h"
22 #include "packet_test_helper.h"
23
24 namespace bluetooth {
25 namespace avrcp {
26
27 using GetCapRequestTestPacket = TestPacketType<GetCapabilitiesRequest>;
28
29 // Test parsing a GetCapabilities Request
TEST(GetCapabilitiesRequestPacketTest,getterTest)30 TEST(GetCapabilitiesRequestPacketTest, getterTest) {
31 auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);
32
33 ASSERT_EQ(test_packet->GetCapabilityRequested(),
34 Capability::EVENTS_SUPPORTED);
35 }
36
TEST(GetCapabilitiesRequestPacketTest,validTest)37 TEST(GetCapabilitiesRequestPacketTest, validTest) {
38 auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);
39 ASSERT_TRUE(test_packet->IsValid());
40 }
41
TEST(GetCapabilitiesRequestPacketTest,invalidTest)42 TEST(GetCapabilitiesRequestPacketTest, invalidTest) {
43 std::vector<uint8_t> packet_copy = get_capabilities_request;
44 packet_copy.push_back(0x00);
45 auto test_packet = GetCapRequestTestPacket::Make(packet_copy);
46 ASSERT_FALSE(test_packet->IsValid());
47
48 std::vector<uint8_t> short_packet = {
49 0, 1, 2, 3, 4, 5, 6,
50 };
51 test_packet = GetCapRequestTestPacket::Make(short_packet);
52 ASSERT_FALSE(test_packet->IsValid());
53 }
54
55 // Test that the length returned by the builder grows correctlyas fields are
56 // added
TEST(GetCapabilityResponseBuilder,builderLengthTest)57 TEST(GetCapabilityResponseBuilder, builderLengthTest) {
58 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
59 ASSERT_EQ(builder->size(), 15u);
60 builder->AddCompanyId(0x000001);
61 ASSERT_EQ(builder->size(), 18u);
62 builder->AddCompanyId(0x000002);
63 ASSERT_EQ(builder->size(), 21u);
64
65 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
66 Event::PLAYBACK_STATUS_CHANGED);
67 ASSERT_EQ(builder->size(), 13u);
68 builder->AddEvent(Event::TRACK_CHANGED);
69 ASSERT_EQ(builder->size(), 14u);
70 builder->AddEvent(Event::PLAYBACK_POS_CHANGED);
71 ASSERT_EQ(builder->size(), 15u);
72 }
73
74 // Check to see that adding the same value multiple times does nothing
TEST(GetCapabilityResponseBuilder,duplicateAddTest)75 TEST(GetCapabilityResponseBuilder, duplicateAddTest) {
76 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
77 ASSERT_EQ(builder->size(), 15u);
78 builder->AddCompanyId(0x000000);
79 ASSERT_EQ(builder->size(), 15u);
80
81 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
82 Event::PLAYBACK_STATUS_CHANGED);
83 ASSERT_EQ(builder->size(), 13u);
84 builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED);
85 ASSERT_EQ(builder->size(), 13u);
86 }
87
88 // Test that trying to to add the wrong type of field to a builder causes death
TEST(GetCapabilityResponseBuilderDeathTest,mismatchAddDeathTest)89 TEST(GetCapabilityResponseBuilderDeathTest, mismatchAddDeathTest) {
90 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
91
92 {
93 // this will silent SIGABRT sent in ASSERT_DEATH below scop
94 ScopedSilentDeath _silentDeath;
95
96 ASSERT_DEATH(builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED),
97 "capability_ == Capability::EVENTS_SUPPORTED");
98 }
99
100 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
101 Event::PLAYBACK_STATUS_CHANGED);
102
103 // this will silent SIGABRT sent in ASSERT_DEATH below
104 ScopedSilentDeath _silentDeath;
105
106 ASSERT_DEATH(builder->AddCompanyId(0x000000),
107 "capability_ == Capability::COMPANY_ID");
108 }
109
110 // Test building a GetCapabilities Response to a Company ID request
TEST(GetCapabilityResponseBuilder,comanyIdBuilderTest)111 TEST(GetCapabilityResponseBuilder, comanyIdBuilderTest) {
112 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x002345);
113 builder->AddCompanyId(BLUETOOTH_COMPANY_ID);
114
115 auto test_packet = GetCapRequestTestPacket::Make();
116 builder->Serialize(test_packet);
117 ASSERT_EQ(test_packet->GetData(), get_capabilities_response_company_id);
118 }
119
120 // Test building a GetCapabilities Response to an Events Supported request
TEST(GetCapabilityResponseBuilder,eventsSupportedBuilderTest)121 TEST(GetCapabilityResponseBuilder, eventsSupportedBuilderTest) {
122 auto builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
123 Event::PLAYBACK_STATUS_CHANGED);
124 builder->AddEvent(Event::TRACK_CHANGED);
125 builder->AddEvent(Event::PLAYBACK_POS_CHANGED);
126
127 auto test_packet = GetCapRequestTestPacket::Make();
128 builder->Serialize(test_packet);
129 ASSERT_EQ(test_packet->GetData(), get_capabilities_response_events_supported);
130 }
131
132 } // namespace avrcp
133 } // namespace bluetooth