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 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
18 
19 #include <future>
20 
21 #include "common/bind.h"
22 #include "l2cap/cid.h"
23 #include "l2cap/le/fixed_channel_manager.h"
24 #include "l2cap/le/fixed_channel_service.h"
25 #include "os/handler.h"
26 #include "os/thread.h"
27 
28 #include <gtest/gtest.h>
29 
30 namespace bluetooth {
31 namespace l2cap {
32 namespace le {
33 namespace internal {
34 
35 class L2capLeServiceManagerTest : public ::testing::Test {
36  public:
37   ~L2capLeServiceManagerTest() = default;
38 
OnServiceRegistered(bool expect_success,FixedChannelManager::RegistrationResult result,std::unique_ptr<FixedChannelService> user_service)39   void OnServiceRegistered(bool expect_success, FixedChannelManager::RegistrationResult result,
40                            std::unique_ptr<FixedChannelService> user_service) {
41     EXPECT_EQ(result == FixedChannelManager::RegistrationResult::SUCCESS, expect_success);
42     service_registered_ = expect_success;
43   }
44 
45  protected:
SetUp()46   void SetUp() override {
47     manager_ = new FixedChannelServiceManagerImpl{nullptr};
48     thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
49     user_handler_ = new os::Handler(thread_);
50   }
51 
TearDown()52   void TearDown() override {
53     user_handler_->Clear();
54     delete user_handler_;
55     delete thread_;
56     delete manager_;
57   }
58 
sync_user_handler()59   void sync_user_handler() {
60     std::promise<void> promise;
61     auto future = promise.get_future();
62     user_handler_->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
63     future.wait_for(std::chrono::milliseconds(3));
64   }
65 
66   FixedChannelServiceManagerImpl* manager_ = nullptr;
67   os::Thread* thread_ = nullptr;
68   os::Handler* user_handler_ = nullptr;
69 
70   bool service_registered_ = false;
71 };
72 
TEST_F(L2capLeServiceManagerTest,register_and_unregister_le_fixed_channel)73 TEST_F(L2capLeServiceManagerTest, register_and_unregister_le_fixed_channel) {
74   FixedChannelServiceImpl::PendingRegistration pending_registration{
75       .user_handler_ = user_handler_,
76       .on_registration_complete_callback_ =
77           common::BindOnce(&L2capLeServiceManagerTest::OnServiceRegistered, common::Unretained(this), true)};
78   Cid cid = kSmpBrCid;
79   EXPECT_FALSE(manager_->IsServiceRegistered(cid));
80   manager_->Register(cid, std::move(pending_registration));
81   EXPECT_TRUE(manager_->IsServiceRegistered(cid));
82   sync_user_handler();
83   EXPECT_TRUE(service_registered_);
84   manager_->Unregister(cid, common::BindOnce([] {}), user_handler_);
85   EXPECT_FALSE(manager_->IsServiceRegistered(cid));
86 }
87 
TEST_F(L2capLeServiceManagerTest,register_le_fixed_channel_bad_cid)88 TEST_F(L2capLeServiceManagerTest, register_le_fixed_channel_bad_cid) {
89   FixedChannelServiceImpl::PendingRegistration pending_registration{
90       .user_handler_ = user_handler_,
91       .on_registration_complete_callback_ =
92           common::BindOnce(&L2capLeServiceManagerTest::OnServiceRegistered, common::Unretained(this), false)};
93   Cid cid = 0x1000;
94   EXPECT_FALSE(manager_->IsServiceRegistered(cid));
95   manager_->Register(cid, std::move(pending_registration));
96   EXPECT_FALSE(manager_->IsServiceRegistered(cid));
97   sync_user_handler();
98   EXPECT_FALSE(service_registered_);
99 }
100 
101 }  // namespace internal
102 }  // namespace le
103 }  // namespace l2cap
104 }  // namespace bluetooth
105