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 <gtest/gtest.h>
18 
19 #include "avrcp_browse_packet.h"
20 #include "avrcp_test_packets.h"
21 #include "packet_test_helper.h"
22 
23 namespace bluetooth {
24 
25 // A helper class that has public accessors to protected methods
26 class TestPacketBuilder : public PacketBuilder {
27  public:
MakeBuilder(std::vector<uint8_t> data)28   static std::unique_ptr<TestPacketBuilder> MakeBuilder(
29       std::vector<uint8_t> data) {
30     std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data));
31     return builder;
32   };
33 
34   // Make all the utility functions public
35   using PacketBuilder::ReserveSpace;
36   using PacketBuilder::AddPayloadOctets1;
37   using PacketBuilder::AddPayloadOctets2;
38   using PacketBuilder::AddPayloadOctets3;
39   using PacketBuilder::AddPayloadOctets4;
40   using PacketBuilder::AddPayloadOctets6;
41   using PacketBuilder::AddPayloadOctets8;
42 
size() const43   size_t size() const override { return data_.size(); };
44 
Serialize(const std::shared_ptr<Packet> & pkt)45   bool Serialize(const std::shared_ptr<Packet>& pkt) override {
46     ReserveSpace(pkt, size());
47 
48     for (uint8_t byte : data_) {
49       AddPayloadOctets1(pkt, byte);
50     }
51 
52     return true;
53   }
54 
TestPacketBuilder(std::vector<uint8_t> data)55   TestPacketBuilder(std::vector<uint8_t> data) : data_(data){};
56 
57   std::vector<uint8_t> data_;
58 };
59 
60 namespace avrcp {
61 
62 using TestBrowsePacket = TestPacketType<BrowsePacket>;
63 
TEST(AvrcpBrowsePacketBuilderTest,buildPacketTest)64 TEST(AvrcpBrowsePacketBuilderTest, buildPacketTest) {
65   std::vector<uint8_t> get_folder_items_request_payload = {
66       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00};
67   auto payload_builder =
68       TestPacketBuilder::MakeBuilder(get_folder_items_request_payload);
69 
70   auto builder = BrowsePacketBuilder::MakeBuilder(BrowsePdu::GET_FOLDER_ITEMS,
71                                                   std::move(payload_builder));
72 
73   ASSERT_EQ(builder->size(), get_folder_items_request.size());
74 
75   auto test_packet = TestBrowsePacket::Make();
76   builder->Serialize(test_packet);
77   ASSERT_EQ(test_packet->GetData(), get_folder_items_request);
78 }
79 
TEST(AvrcpBrowsePacketTest,gettersTest)80 TEST(AvrcpBrowsePacketTest, gettersTest) {
81   auto test_browse_packet = TestBrowsePacket::Make(get_folder_items_request);
82 
83   ASSERT_EQ(test_browse_packet->GetPdu(), BrowsePdu::GET_FOLDER_ITEMS);
84   ASSERT_EQ(test_browse_packet->GetLength(), 10u);
85 }
86 
TEST(AvrcpBrowsePacketTest,payloadBoundsTest)87 TEST(AvrcpBrowsePacketTest, payloadBoundsTest) {
88   auto test_browse_packet = TestBrowsePacket::Make(get_folder_items_request);
89   std::vector<uint8_t> get_folder_items_request_payload = {
90       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00};
91 
92   // Make a folder items payload packet out of the payload of the browse packet
93   auto get_folder_items_payload = TestBrowsePacket::Make(test_browse_packet);
94 
95   for (size_t i = 0; i < get_folder_items_request_payload.size(); i++) {
96     ASSERT_EQ((*get_folder_items_payload)[i],
97               get_folder_items_request_payload[i]);
98   }
99 }
100 
TEST(AvrcpBrowsePacketTest,validTest)101 TEST(AvrcpBrowsePacketTest, validTest) {
102   auto test_browse_packet = TestBrowsePacket::Make(get_folder_items_request);
103 
104   ASSERT_TRUE(test_browse_packet->IsValid());
105 }
106 
TEST(AvrcpBrowsePacketTest,invalidTest)107 TEST(AvrcpBrowsePacketTest, invalidTest) {
108   auto packet_copy = get_folder_items_request;
109   packet_copy.push_back(0x00);
110   auto test_browse_packet = TestBrowsePacket::Make(packet_copy);
111   ASSERT_FALSE(test_browse_packet->IsValid());
112 
113   std::vector<uint8_t> short_packet = {0x00, 0x01};
114   test_browse_packet = TestBrowsePacket::Make(short_packet);
115   ASSERT_FALSE(test_browse_packet->IsValid());
116 }
117 
118 }  // namespace avrcp
119 }  // namespace bluetooth