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() = 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 .security_policy_ = SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
80 .on_registration_complete_callback_ =
81 common::BindOnce(&L2capLeDynamicServiceManagerTest::OnServiceRegistered, common::Unretained(this), true)};
82 Psm psm = 0x41;
83 EXPECT_FALSE(manager_->IsServiceRegistered(psm));
84 manager_->Register(psm, std::move(pending_registration));
85 EXPECT_TRUE(manager_->IsServiceRegistered(psm));
86 sync_user_handler();
87 EXPECT_TRUE(service_registered_);
88 manager_->Unregister(psm, common::BindOnce([] {}), user_handler_);
89 EXPECT_FALSE(manager_->IsServiceRegistered(psm));
90 }
91
TEST_F(L2capLeDynamicServiceManagerTest,register_le_dynamic_channel_even_number_psm)92 TEST_F(L2capLeDynamicServiceManagerTest, register_le_dynamic_channel_even_number_psm) {
93 DynamicChannelServiceImpl::PendingRegistration pending_registration{
94 .user_handler_ = user_handler_,
95 .security_policy_ = SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
96 .on_registration_complete_callback_ =
97 common::BindOnce(&L2capLeDynamicServiceManagerTest::OnServiceRegistered, common::Unretained(this), true)};
98 Psm psm = 0x0100;
99 EXPECT_FALSE(manager_->IsServiceRegistered(psm));
100 manager_->Register(psm, std::move(pending_registration));
101 EXPECT_TRUE(manager_->IsServiceRegistered(psm));
102 sync_user_handler();
103 EXPECT_TRUE(service_registered_);
104 }
105
106 } // namespace internal
107 } // namespace le
108 } // namespace l2cap
109 } // namespace bluetooth
110