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 "packet.h"
18 
19 #include <android-base/silent_death_test.h>
20 #include <gtest/gtest.h>
21 
22 #include "packet_test_common.h"
23 #include "test_packets.h"
24 
25 namespace bluetooth {
26 
27 // Test making a packet from another packet. The new packet should have the
28 // same payload bounds as the old packet.
TEST(PacketTest,newPacketFromPacketTest)29 TEST(PacketTest, newPacketFromPacketTest) {
30   // Create a packet with payload bounds
31   auto packet =
32           TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset, test_avctp_data.size());
33 
34   // Create packet from bounded packet
35   auto new_packet = TestPacket::Make(packet);
36 
37   // Check to see if the new packet is bounded by the payload of the old packet
38   auto it = new_packet->begin();
39   for (size_t i = 0; i < test_avrcp_data.size(); i++) {
40     ASSERT_EQ(test_avrcp_data[i], *it++);
41   }
42 }
43 
44 // Test that the correct payload length is returned
TEST(PacketTest,sizeTest)45 TEST(PacketTest, sizeTest) {
46   auto packet = TestPacket::Make(test_avctp_data);
47   ASSERT_EQ(packet->size(), test_avctp_data.size());
48 
49   packet =
50           TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset, test_avctp_data.size());
51   ASSERT_EQ(packet->size(), test_avrcp_data.size());
52 }
53 
54 // Test the array access operator
TEST(PacketTest,arrayAccessTest)55 TEST(PacketTest, arrayAccessTest) {
56   auto packet = TestPacket::Make(test_l2cap_data);
57   for (size_t i = 0; i < test_l2cap_data.size(); i++) {
58     ASSERT_EQ(test_l2cap_data[i], (*packet)[i]);
59   }
60 
61   packet =
62           TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset, test_avctp_data.size());
63   for (size_t i = 0; i < test_avrcp_data.size(); i++) {
64     ASSERT_EQ(test_avrcp_data[i], (*packet)[i]);
65   }
66 }
67 
68 // Test that accessing past the end of the defined payload dies
TEST(PacketDeathTest,arrayAccessDeathTest)69 TEST(PacketDeathTest, arrayAccessDeathTest) {
70   auto packet = TestPacket::Make(test_l2cap_data, 3, test_l2cap_data.size() - 2);
71 
72   // this will silent SIGABRT sent in ASSERT_DEATH below
73   ScopedSilentDeath _silentDeath;
74 
75   ASSERT_DEATH((*packet)[test_l2cap_data.size()], "");
76 }
77 
78 // Test that the iterator and array access operator return the same data
TEST(PacketTest,iteratorTest)79 TEST(PacketTest, iteratorTest) {
80   auto packet =
81           TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset, test_avctp_data.size());
82 
83   // Check to see if the data matches
84   auto it = packet->begin();
85   for (size_t i = 0; i < packet->size(); i++) {
86     ASSERT_EQ((*packet)[i], *it++);
87   }
88 
89   // Check to see if the iterator points to the end of the data
90   ASSERT_EQ(it, packet->end());
91 }
92 
93 class ChildTestPacket : public TestPacket {
94 public:
95   using TestPacket::TestPacket;
96 
ToString() const97   std::string ToString() const override { return "ChildTestPacket"; }
98 };
99 
100 // Test specializing a packet to another packet type
TEST(PacketTest,specializeTest)101 TEST(PacketTest, specializeTest) {
102   auto packet = TestPacket::Make(test_l2cap_data);
103 
104   std::shared_ptr<ChildTestPacket> specialized_packet = Packet::Specialize<ChildTestPacket>(packet);
105 
106   // Test that the new packet is an instance of ChildTestPacket
107   ASSERT_EQ(specialized_packet->ToString(), "ChildTestPacket");
108 
109   // Test that the underlying data is the same and no copy took place
110   ASSERT_EQ(&specialized_packet->GetData(), &packet->GetData());
111 }
112 
113 // Test that when the packet goes out of scope, that the underlying memory is
114 // freed
TEST(PacketTest,memoryFreeTest)115 TEST(PacketTest, memoryFreeTest) {
116   auto packet = TestPacket::Make(test_l2cap_data);
117   std::weak_ptr<std::vector<uint8_t>> data_ptr(packet->GetDataPointer());
118 
119   ASSERT_FALSE(data_ptr.expired());
120 
121   packet.reset();
122 
123   ASSERT_TRUE(data_ptr.expired());
124 }
125 
126 }  // namespace bluetooth
127