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