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