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 #pragma once 18 19 #include <memory> 20 21 #include "packet.h" 22 23 namespace bluetooth { 24 25 // A helper templated class to access the protected members of Packet to make 26 // testing easier 27 template <class PacketType> 28 class TestPacketType : public PacketType { 29 public: 30 using PacketType::PacketType; 31 Make()32 static std::shared_ptr<TestPacketType<PacketType>> Make() { 33 return std::shared_ptr<TestPacketType<PacketType>>( 34 new TestPacketType<PacketType>()); 35 } 36 Make(std::shared_ptr<Packet> packet)37 static std::shared_ptr<TestPacketType<PacketType>> Make( 38 std::shared_ptr<Packet> packet) { 39 return std::shared_ptr<TestPacketType<PacketType>>( 40 new TestPacketType<PacketType>(packet)); 41 } 42 Make(std::vector<uint8_t> payload)43 static std::shared_ptr<TestPacketType<PacketType>> Make( 44 std::vector<uint8_t> payload) { 45 size_t end = payload.size(); 46 return Make(std::move(payload), 0, end); 47 } 48 Make(std::vector<uint8_t> payload,size_t start,size_t end)49 static std::shared_ptr<TestPacketType<PacketType>> Make( 50 std::vector<uint8_t> payload, size_t start, size_t end) { 51 auto pkt = std::shared_ptr<TestPacketType<PacketType>>( 52 new TestPacketType<PacketType>()); 53 pkt->packet_start_index_ = start; 54 pkt->packet_end_index_ = end; 55 pkt->data_ = std::make_shared<std::vector<uint8_t>>(std::move(payload)); 56 return pkt; 57 } 58 GetData()59 const std::vector<uint8_t>& GetData() { return *PacketType::data_; } 60 GetDataPointer()61 std::shared_ptr<std::vector<uint8_t>> GetDataPointer() { 62 return PacketType::data_; 63 } 64 }; 65 66 } // namespace bluetooth