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