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