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/fixed_channel_allocator.h"
18 #include "l2cap/classic/internal/fixed_channel_impl_mock.h"
19 #include "l2cap/classic/internal/link_mock.h"
20 #include "l2cap/internal/parameter_provider_mock.h"
21
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24
25 namespace bluetooth {
26 namespace l2cap {
27 namespace internal {
28
29 using l2cap::classic::internal::testing::MockFixedChannelImpl;
30 using l2cap::classic::internal::testing::MockLink;
31 using testing::MockParameterProvider;
32 using ::testing::Return;
33
34 const hci::AddressWithType device{{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}}, hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
35
36 class L2capFixedChannelAllocatorTest : public ::testing::Test {
37 protected:
SetUp()38 void SetUp() override {
39 thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
40 handler_ = new os::Handler(thread_);
41 mock_parameter_provider_ = new MockParameterProvider();
42 mock_classic_link_ = new MockLink(handler_, mock_parameter_provider_);
43 EXPECT_CALL(*mock_classic_link_, GetDevice()).WillRepeatedly(Return(device));
44 // Use classic as a place holder
45 channel_allocator_ =
46 std::make_unique<FixedChannelAllocator<MockFixedChannelImpl, MockLink>>(mock_classic_link_, handler_);
47 }
48
TearDown()49 void TearDown() override {
50 channel_allocator_.reset();
51 delete mock_classic_link_;
52 delete mock_parameter_provider_;
53 handler_->Clear();
54 delete handler_;
55 delete thread_;
56 }
57
58 os::Thread* thread_{nullptr};
59 os::Handler* handler_{nullptr};
60 MockParameterProvider* mock_parameter_provider_{nullptr};
61 MockLink* mock_classic_link_{nullptr};
62 std::unique_ptr<FixedChannelAllocator<MockFixedChannelImpl, MockLink>> channel_allocator_;
63 };
64
TEST_F(L2capFixedChannelAllocatorTest,precondition)65 TEST_F(L2capFixedChannelAllocatorTest, precondition) {
66 Cid cid = kFirstFixedChannel;
67 EXPECT_FALSE(channel_allocator_->IsChannelAllocated(cid));
68 }
69
TEST_F(L2capFixedChannelAllocatorTest,allocate_and_free_channel)70 TEST_F(L2capFixedChannelAllocatorTest, allocate_and_free_channel) {
71 Cid cid = kFirstFixedChannel;
72 auto channel = channel_allocator_->AllocateChannel(cid);
73 EXPECT_TRUE(channel_allocator_->IsChannelAllocated(cid));
74 EXPECT_EQ(channel, channel_allocator_->FindChannel(cid));
75 ASSERT_NO_FATAL_FAILURE(channel_allocator_->FreeChannel(cid));
76 EXPECT_FALSE(channel_allocator_->IsChannelAllocated(cid));
77 }
78
79 } // namespace internal
80 } // namespace l2cap
81 } // namespace bluetooth
82