• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = TestPacket::Make(
32       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 = TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset,
50                             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 = TestPacket::Make(test_avctp_data, test_avctp_data_payload_offset,
62                             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 =
71       TestPacket::Make(test_l2cap_data, 3, test_l2cap_data.size() - 2);
72 
73   // this will silent SIGABRT sent in ASSERT_DEATH below
74   ScopedSilentDeath _silentDeath;
75 
76   ASSERT_DEATH((*packet)[test_l2cap_data.size()], "");
77 }
78 
79 // Test that the iterator and array access operator return the same data
TEST(PacketTest,iteratorTest)80 TEST(PacketTest, iteratorTest) {
81   auto packet = TestPacket::Make(
82       test_avctp_data, test_avctp_data_payload_offset, test_avctp_data.size());
83 
84   // Check to see if the data matches
85   auto it = packet->begin();
86   for (size_t i = 0; i < packet->size(); i++) {
87     ASSERT_EQ((*packet)[i], *it++);
88   }
89 
90   // Check to see if the iterator points to the end of the data
91   ASSERT_EQ(it, packet->end());
92 }
93 
94 class ChildTestPacket : public TestPacket {
95  public:
96   using TestPacket::TestPacket;
97 
ToString() const98   std::string ToString() const override { return "ChildTestPacket"; };
99 };
100 
101 // Test specializing a packet to another packet type
TEST(PacketTest,specializeTest)102 TEST(PacketTest, specializeTest) {
103   auto packet = TestPacket::Make(test_l2cap_data);
104 
105   std::shared_ptr<ChildTestPacket> specialized_packet =
106       Packet::Specialize<ChildTestPacket>(packet);
107 
108   // Test that the new packet is an instance of ChildTestPacket
109   ASSERT_EQ(specialized_packet->ToString(), "ChildTestPacket");
110 
111   // Test that the underlying data is the same and no copy took place
112   ASSERT_EQ(&specialized_packet->GetData(), &packet->GetData());
113 }
114 
115 // Test that when the packet goes out of scope, that the underlying memory is
116 // freed
TEST(PacketTest,memoryFreeTest)117 TEST(PacketTest, memoryFreeTest) {
118   auto packet = TestPacket::Make(test_l2cap_data);
119   std::weak_ptr<std::vector<uint8_t>> data_ptr(packet->GetDataPointer());
120 
121   ASSERT_FALSE(data_ptr.expired());
122 
123   packet.reset();
124 
125   ASSERT_TRUE(data_ptr.expired());
126 }
127 
128 }  // namespace bluetooth