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