1 /* 2 * Copyright 2020 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 <base/logging.h> 18 #include <gtest/gtest.h> 19 #include <stdio.h> 20 #include <cstdint> 21 22 #include "stack/include/ldacBT_bco_for_fluoride.h" 23 24 #include "osi/test/AllocationTestHarness.h" 25 #undef LOG_TAG 26 #include "stack/a2dp/a2dp_vendor_ldac_decoder.cc" 27 28 extern void allocation_tracker_uninit(void); 29 namespace { 30 Data(BT_HDR * packet)31uint8_t* Data(BT_HDR* packet) { return packet->data + packet->offset; } 32 33 } // namespace 34 35 /** 36 * Test class to test selected functionality in stack/a2dp 37 */ 38 class A2dpStackTest : public AllocationTestHarness { 39 protected: SetUp()40 void SetUp() override { 41 AllocationTestHarness::SetUp(); 42 // Disable our allocation tracker to allow ASAN full range 43 allocation_tracker_uninit(); 44 } 45 TearDown()46 void TearDown() override { AllocationTestHarness::TearDown(); } 47 AllocateL2capPacket(const std::vector<uint8_t> data) const48 BT_HDR* AllocateL2capPacket(const std::vector<uint8_t> data) const { 49 auto packet = AllocatePacket(data.size()); 50 std::copy(data.cbegin(), data.cend(), Data(packet)); 51 return packet; 52 } 53 54 private: AllocatePacket(size_t packet_length) const55 BT_HDR* AllocatePacket(size_t packet_length) const { 56 BT_HDR* packet = 57 static_cast<BT_HDR*>(osi_calloc(sizeof(BT_HDR) + packet_length)); 58 packet->len = packet_length; 59 return packet; 60 } 61 }; 62 TEST_F(A2dpStackTest,DecodePacket_ZeroLength)63TEST_F(A2dpStackTest, DecodePacket_ZeroLength) { 64 const std::vector<uint8_t> data; 65 BT_HDR* p_buf = AllocateL2capPacket(data); 66 CHECK(!a2dp_vendor_ldac_decoder_decode_packet(p_buf)); 67 osi_free(p_buf); 68 } 69