1 /*
2  * Copyright 2019 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 "l2cap/internal/dynamic_channel_allocator.h"
18 #include "l2cap/classic/internal/link_mock.h"
19 #include "l2cap/internal/parameter_provider_mock.h"
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 namespace bluetooth {
25 namespace l2cap {
26 namespace internal {
27 
28 using classic::internal::testing::MockLink;
29 using l2cap::internal::testing::MockParameterProvider;
30 using ::testing::Return;
31 
32 const hci::AddressWithType device{{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}}, hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
33 
34 class L2capClassicDynamicChannelAllocatorTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {
37     thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
38     handler_ = new os::Handler(thread_);
39     mock_parameter_provider_ = new MockParameterProvider();
40     mock_classic_link_ = new MockLink(handler_, mock_parameter_provider_);
41     EXPECT_CALL(*mock_classic_link_, GetDevice()).WillRepeatedly(Return(device));
42     channel_allocator_ = std::make_unique<DynamicChannelAllocator>(mock_classic_link_, handler_);
43   }
44 
TearDown()45   void TearDown() override {
46     channel_allocator_.reset();
47     delete mock_classic_link_;
48     delete mock_parameter_provider_;
49     handler_->Clear();
50     delete handler_;
51     delete thread_;
52   }
53 
54   os::Thread* thread_{nullptr};
55   os::Handler* handler_{nullptr};
56   MockParameterProvider* mock_parameter_provider_{nullptr};
57   MockLink* mock_classic_link_{nullptr};
58   std::unique_ptr<DynamicChannelAllocator> channel_allocator_;
59 };
60 
TEST_F(L2capClassicDynamicChannelAllocatorTest,precondition)61 TEST_F(L2capClassicDynamicChannelAllocatorTest, precondition) {
62   Psm psm = 0x03;
63   EXPECT_FALSE(channel_allocator_->IsPsmUsed(psm));
64 }
65 
TEST_F(L2capClassicDynamicChannelAllocatorTest,allocate_and_free_channel)66 TEST_F(L2capClassicDynamicChannelAllocatorTest, allocate_and_free_channel) {
67   Psm psm = 0x03;
68   Cid remote_cid = kFirstDynamicChannel;
69   auto channel = channel_allocator_->AllocateChannel(psm, remote_cid);
70   Cid local_cid = channel->GetCid();
71   EXPECT_TRUE(channel_allocator_->IsPsmUsed(psm));
72   EXPECT_EQ(channel, channel_allocator_->FindChannelByCid(local_cid));
73   ASSERT_NO_FATAL_FAILURE(channel_allocator_->FreeChannel(local_cid));
74   EXPECT_FALSE(channel_allocator_->IsPsmUsed(psm));
75 }
76 
TEST_F(L2capClassicDynamicChannelAllocatorTest,reserve_channel)77 TEST_F(L2capClassicDynamicChannelAllocatorTest, reserve_channel) {
78   Psm psm = 0x03;
79   Cid remote_cid = kFirstDynamicChannel;
80   Cid reserved = channel_allocator_->ReserveChannel();
81   auto channel = channel_allocator_->AllocateReservedChannel(reserved, psm, remote_cid);
82   Cid local_cid = channel->GetCid();
83   EXPECT_EQ(local_cid, reserved);
84   EXPECT_TRUE(channel_allocator_->IsPsmUsed(psm));
85   EXPECT_EQ(channel, channel_allocator_->FindChannelByCid(local_cid));
86   ASSERT_NO_FATAL_FAILURE(channel_allocator_->FreeChannel(local_cid));
87   EXPECT_FALSE(channel_allocator_->IsPsmUsed(psm));
88 }
89 
90 }  // namespace internal
91 }  // namespace l2cap
92 }  // namespace bluetooth
93