aboutsummaryrefslogtreecommitdiff
path: root/gd/l2cap/le/internal/dynamic_channel_service_manager_test.cc
blob: d1f16561d3b5e03c086c420f684ccb9b05b754b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <future>

#include <gtest/gtest.h>

#include "common/bind.h"
#include "l2cap/cid.h"
#include "l2cap/le/dynamic_channel_manager.h"
#include "l2cap/le/dynamic_channel_service.h"
#include "l2cap/le/internal/dynamic_channel_service_manager_impl.h"
#include "os/handler.h"
#include "os/thread.h"

namespace bluetooth {
namespace l2cap {
namespace le {
namespace internal {

class L2capLeDynamicServiceManagerTest : public ::testing::Test {
 public:
  ~L2capLeDynamicServiceManagerTest() = default;

  void OnServiceRegistered(bool expect_success, DynamicChannelManager::RegistrationResult result,
                           std::unique_ptr<DynamicChannelService> user_service) {
    EXPECT_EQ(result == DynamicChannelManager::RegistrationResult::SUCCESS, expect_success);
    service_registered_ = expect_success;
  }

 protected:
  void SetUp() override {
    thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
    user_handler_ = new os::Handler(thread_);
    l2cap_handler_ = new os::Handler(thread_);
    manager_ = new DynamicChannelServiceManagerImpl{l2cap_handler_};
  }

  void TearDown() override {
    delete manager_;
    l2cap_handler_->Clear();
    delete l2cap_handler_;
    user_handler_->Clear();
    delete user_handler_;
    delete thread_;
  }

  void sync_user_handler() {
    std::promise<void> promise;
    auto future = promise.get_future();
    user_handler_->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
    future.wait_for(std::chrono::milliseconds(3));
  }

  DynamicChannelServiceManagerImpl* manager_ = nullptr;
  os::Thread* thread_ = nullptr;
  os::Handler* user_handler_ = nullptr;
  os::Handler* l2cap_handler_ = nullptr;

  bool service_registered_ = false;
};

TEST_F(L2capLeDynamicServiceManagerTest, register_and_unregister_le_dynamic_channel) {
  DynamicChannelServiceImpl::PendingRegistration pending_registration{
      .user_handler_ = user_handler_,
      .security_policy_ = SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
      .on_registration_complete_callback_ =
          common::BindOnce(&L2capLeDynamicServiceManagerTest::OnServiceRegistered, common::Unretained(this), true)};
  Psm psm = 0x41;
  EXPECT_FALSE(manager_->IsServiceRegistered(psm));
  manager_->Register(psm, std::move(pending_registration));
  EXPECT_TRUE(manager_->IsServiceRegistered(psm));
  sync_user_handler();
  EXPECT_TRUE(service_registered_);
  manager_->Unregister(psm, common::BindOnce([] {}), user_handler_);
  EXPECT_FALSE(manager_->IsServiceRegistered(psm));
}

TEST_F(L2capLeDynamicServiceManagerTest, register_le_dynamic_channel_even_number_psm) {
  DynamicChannelServiceImpl::PendingRegistration pending_registration{
      .user_handler_ = user_handler_,
      .security_policy_ = SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
      .on_registration_complete_callback_ =
          common::BindOnce(&L2capLeDynamicServiceManagerTest::OnServiceRegistered, common::Unretained(this), true)};
  Psm psm = 0x0100;
  EXPECT_FALSE(manager_->IsServiceRegistered(psm));
  manager_->Register(psm, std::move(pending_registration));
  EXPECT_TRUE(manager_->IsServiceRegistered(psm));
  sync_user_handler();
  EXPECT_TRUE(service_registered_);
}

}  // namespace internal
}  // namespace le
}  // namespace l2cap
}  // namespace bluetooth