• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/link_manager.h"
18 
19 #include <future>
20 #include <thread>
21 
22 #include "common/bind.h"
23 #include "common/testing/bind_test_util.h"
24 #include "hci/acl_manager_mock.h"
25 #include "hci/address.h"
26 #include "l2cap/cid.h"
27 #include "l2cap/internal/parameter_provider_mock.h"
28 #include "l2cap/le/fixed_channel_manager.h"
29 #include "l2cap/le/internal/fixed_channel_service_impl_mock.h"
30 #include "l2cap/le/internal/fixed_channel_service_manager_impl_mock.h"
31 #include "os/handler.h"
32 #include "os/thread.h"
33 
34 #include <gmock/gmock.h>
35 #include <gtest/gtest.h>
36 
37 namespace bluetooth {
38 namespace l2cap {
39 namespace le {
40 namespace internal {
41 namespace {
42 
43 using hci::testing::MockAclManager;
44 using hci::testing::MockLeAclConnection;
45 using l2cap::internal::testing::MockParameterProvider;
46 using ::testing::_;  // Matcher to any value
47 using ::testing::ByMove;
48 using ::testing::DoAll;
49 using testing::MockFixedChannelServiceImpl;
50 using testing::MockFixedChannelServiceManagerImpl;
51 using ::testing::Return;
52 using ::testing::SaveArg;
53 
54 constexpr static auto kTestIdleDisconnectTimeoutLong = std::chrono::milliseconds(1000);
55 constexpr static auto kTestIdleDisconnectTimeoutShort = std::chrono::milliseconds(1000);
56 
57 class L2capLeLinkManagerTest : public ::testing::Test {
58  public:
SyncHandler(os::Handler * handler)59   static void SyncHandler(os::Handler* handler) {
60     std::promise<void> promise;
61     auto future = promise.get_future();
62     handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
63     future.wait_for(std::chrono::milliseconds(3));
64   }
65 
66  protected:
SetUp()67   void SetUp() override {
68     thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
69     l2cap_handler_ = new os::Handler(thread_);
70     user_handler_ = new os::Handler(thread_);
71     mock_parameter_provider_ = new MockParameterProvider;
72     EXPECT_CALL(*mock_parameter_provider_, GetLeLinkIdleDisconnectTimeout)
73         .WillRepeatedly(Return(kTestIdleDisconnectTimeoutLong));
74   }
75 
TearDown()76   void TearDown() override {
77     delete mock_parameter_provider_;
78     l2cap_handler_->Clear();
79     delete l2cap_handler_;
80     user_handler_->Clear();
81     delete user_handler_;
82     delete thread_;
83   }
84 
85   os::Thread* thread_ = nullptr;
86   os::Handler* l2cap_handler_ = nullptr;
87   os::Handler* user_handler_ = nullptr;
88   MockParameterProvider* mock_parameter_provider_ = nullptr;
89 };
90 
TEST_F(L2capLeLinkManagerTest,connect_fixed_channel_service_without_acl)91 TEST_F(L2capLeLinkManagerTest, connect_fixed_channel_service_without_acl) {
92   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
93   MockAclManager mock_acl_manager;
94   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
95                                          hci::AddressType::RANDOM_DEVICE_ADDRESS);
96 
97   // Step 1: Verify callback registration with HCI
98   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
99   os::Handler* hci_callback_handler = nullptr;
100   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
101       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
102   LinkManager le_link_manager(
103       l2cap_handler_,
104       &mock_acl_manager,
105       &mock_le_fixed_channel_service_manager,
106       nullptr,
107       mock_parameter_provider_);
108   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
109   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
110 
111   // Register fake services
112   MockFixedChannelServiceImpl mock_service_1, mock_service_2;
113   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
114   results.emplace_back(kSmpBrCid, &mock_service_1);
115   results.emplace_back(kConnectionlessCid, &mock_service_2);
116   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
117 
118   // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
119   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(1);
120   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
121       .handler_ = user_handler_,
122       .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
123   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
124 
125   // Step 3: ACL connection success event should trigger channel creation for all registered services
126   std::unique_ptr<MockLeAclConnection> acl_connection = std::make_unique<MockLeAclConnection>();
127   EXPECT_CALL(*acl_connection, GetRemoteAddress()).WillRepeatedly(Return(address_with_type));
128   hci::acl_manager::LeConnectionManagementCallbacks* connection_management_callbacks = nullptr;
129   os::Handler* connection_management_handler = nullptr;
130   EXPECT_CALL(*acl_connection, RegisterCallbacks(_, _))
131       .WillOnce(DoAll(SaveArg<0>(&connection_management_callbacks), SaveArg<1>(&connection_management_handler)));
132   std::unique_ptr<FixedChannel> channel_1, channel_2;
133   std::promise<void> promise_1, promise_2;
134   auto future_1 = promise_1.get_future();
135   auto future_2 = promise_2.get_future();
136   EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
137       .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
138         channel_1 = std::move(channel);
139         promise_1.set_value();
140       });
141   EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
142       .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
143         channel_2 = std::move(channel);
144         promise_2.set_value();
145       });
146   hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::LeConnectionCallbacks::OnLeConnectSuccess,
147                                               common::Unretained(hci_le_connection_callbacks), address_with_type,
148                                               std::move(acl_connection)));
149   SyncHandler(hci_callback_handler);
150   connection_management_handler->Post(common::BindOnce(
151       &hci::acl_manager::LeConnectionManagementCallbacks::OnReadRemoteVersionInformationComplete,
152       common::Unretained(connection_management_callbacks),
153       hci::ErrorCode::SUCCESS,
154       0,
155       0,
156       0));
157   auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
158   EXPECT_EQ(future_1_status, std::future_status::ready);
159   auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
160   EXPECT_EQ(future_2_status, std::future_status::ready);
161   ASSERT_NE(channel_1, nullptr);
162   ASSERT_NE(channel_2, nullptr);
163 
164   // Step 4: Calling ConnectServices() to the same device will not trigger another connection attempt
165   FixedChannelManager::ConnectionResult my_result;
166   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection_2{
167       .handler_ = user_handler_,
168       .on_fail_callback_ = common::testing::BindLambdaForTesting(
169           [&my_result](FixedChannelManager::ConnectionResult result) { my_result = result; })};
170   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection_2));
171   SyncHandler(user_handler_);
172   EXPECT_EQ(my_result.connection_result_code,
173             FixedChannelManager::ConnectionResultCode::FAIL_ALL_SERVICES_HAVE_CHANNEL);
174 
175   // Step 5: Register new service will cause new channels to be created during ConnectServices()
176   MockFixedChannelServiceImpl mock_service_3;
177   results.emplace_back(kSmpBrCid + 1, &mock_service_3);
178   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
179   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection_3{
180       .handler_ = user_handler_,
181       .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
182   std::unique_ptr<FixedChannel> channel_3;
183   std::promise<void> promise_3;
184   auto future_3 = promise_3.get_future();
185   EXPECT_CALL(mock_service_3, NotifyChannelCreation(_))
186       .WillOnce([&channel_3, &promise_3](std::unique_ptr<FixedChannel> channel) {
187         channel_3 = std::move(channel);
188         promise_3.set_value();
189       });
190   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection_3));
191   auto future_3_status = future_3.wait_for(kTestIdleDisconnectTimeoutShort);
192   EXPECT_EQ(future_3_status, std::future_status::ready);
193   EXPECT_NE(channel_3, nullptr);
194 
195   connection_management_handler->Post(common::BindOnce(
196       &hci::acl_manager::LeConnectionManagementCallbacks::OnDisconnection,
197       common::Unretained(connection_management_callbacks), hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION));
198   SyncHandler(connection_management_handler);
199 }
200 
TEST_F(L2capLeLinkManagerTest,connect_fixed_channel_service_without_acl_with_no_service)201 TEST_F(L2capLeLinkManagerTest, connect_fixed_channel_service_without_acl_with_no_service) {
202   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
203   MockAclManager mock_acl_manager;
204   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
205                                          hci::AddressType::PUBLIC_DEVICE_ADDRESS);
206 
207   // Step 1: Verify callback registration with HCI
208   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
209   os::Handler* hci_callback_handler = nullptr;
210   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
211       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
212   LinkManager le_link_manager(
213       l2cap_handler_,
214       &mock_acl_manager,
215       &mock_le_fixed_channel_service_manager,
216       nullptr,
217       mock_parameter_provider_);
218   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
219   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
220 
221   // Make sure no service is registered
222   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
223   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
224 
225   // Step 2: Connect to fixed channel without any service registered will result in failure
226   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(0);
227   FixedChannelManager::ConnectionResult my_result;
228   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
229       .handler_ = user_handler_,
230       .on_fail_callback_ = common::testing::BindLambdaForTesting(
231           [&my_result](FixedChannelManager::ConnectionResult result) { my_result = result; })};
232   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
233   SyncHandler(user_handler_);
234   EXPECT_EQ(my_result.connection_result_code, FixedChannelManager::ConnectionResultCode::FAIL_NO_SERVICE_REGISTERED);
235 
236 }
237 
TEST_F(L2capLeLinkManagerTest,connect_fixed_channel_service_without_acl_with_hci_failure)238 TEST_F(L2capLeLinkManagerTest, connect_fixed_channel_service_without_acl_with_hci_failure) {
239   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
240   MockAclManager mock_acl_manager;
241   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
242                                          hci::AddressType::RANDOM_DEVICE_ADDRESS);
243 
244   // Step 1: Verify callback registration with HCI
245   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
246   os::Handler* hci_callback_handler = nullptr;
247   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
248       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
249   LinkManager le_link_manager(
250       l2cap_handler_,
251       &mock_acl_manager,
252       &mock_le_fixed_channel_service_manager,
253       nullptr,
254       mock_parameter_provider_);
255   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
256   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
257 
258   // Register fake services
259   MockFixedChannelServiceImpl mock_service_1;
260   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
261   results.emplace_back(kSmpBrCid, &mock_service_1);
262   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
263 
264   // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
265   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(1);
266   FixedChannelManager::ConnectionResult my_result;
267   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
268       .handler_ = user_handler_,
269       .on_fail_callback_ = common::testing::BindLambdaForTesting(
270           [&my_result](FixedChannelManager::ConnectionResult result) { my_result = result; })};
271   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
272 
273   // Step 3: ACL connection failure event should trigger connection failure callback
274   EXPECT_CALL(mock_service_1, NotifyChannelCreation(_)).Times(0);
275   hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::LeConnectionCallbacks::OnLeConnectFail,
276                                               common::Unretained(hci_le_connection_callbacks), address_with_type,
277                                               hci::ErrorCode::PAGE_TIMEOUT));
278   SyncHandler(hci_callback_handler);
279   SyncHandler(user_handler_);
280   EXPECT_EQ(my_result.connection_result_code, FixedChannelManager::ConnectionResultCode::FAIL_HCI_ERROR);
281   EXPECT_EQ(my_result.hci_error, hci::ErrorCode::PAGE_TIMEOUT);
282 }
283 
TEST_F(L2capLeLinkManagerTest,not_acquiring_channels_should_disconnect_acl_after_timeout)284 TEST_F(L2capLeLinkManagerTest, not_acquiring_channels_should_disconnect_acl_after_timeout) {
285   EXPECT_CALL(*mock_parameter_provider_, GetLeLinkIdleDisconnectTimeout)
286       .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
287   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
288   MockAclManager mock_acl_manager;
289   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
290                                          hci::AddressType::RANDOM_DEVICE_ADDRESS);
291 
292   // Step 1: Verify callback registration with HCI
293   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
294   os::Handler* hci_callback_handler = nullptr;
295   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
296       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
297   LinkManager le_link_manager(
298       l2cap_handler_,
299       &mock_acl_manager,
300       &mock_le_fixed_channel_service_manager,
301       nullptr,
302       mock_parameter_provider_);
303   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
304   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
305 
306   // Register fake services
307   MockFixedChannelServiceImpl mock_service_1, mock_service_2;
308   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
309   results.emplace_back(kSmpBrCid, &mock_service_1);
310   results.emplace_back(kConnectionlessCid, &mock_service_2);
311   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
312 
313   // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
314   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(1);
315   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
316       .handler_ = user_handler_,
317       .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
318   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
319 
320   // Step 3: ACL connection success event should trigger channel creation for all registered services
321   auto* raw_acl_connection = new MockLeAclConnection();
322   std::unique_ptr<MockLeAclConnection> acl_connection(raw_acl_connection);
323   EXPECT_CALL(*acl_connection, GetRemoteAddress()).WillRepeatedly(Return(address_with_type));
324   hci::acl_manager::LeConnectionManagementCallbacks* connection_management_callbacks = nullptr;
325   os::Handler* connection_management_handler = nullptr;
326   EXPECT_CALL(*acl_connection, RegisterCallbacks(_, _))
327       .WillOnce(DoAll(SaveArg<0>(&connection_management_callbacks), SaveArg<1>(&connection_management_handler)));
328   std::unique_ptr<FixedChannel> channel_1, channel_2;
329   std::promise<void> promise_1, promise_2;
330   auto future_1 = promise_1.get_future();
331   auto future_2 = promise_2.get_future();
332   EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
333       .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
334         channel_1 = std::move(channel);
335         promise_1.set_value();
336       });
337   EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
338       .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
339         channel_2 = std::move(channel);
340         promise_2.set_value();
341       });
342   hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::LeConnectionCallbacks::OnLeConnectSuccess,
343                                               common::Unretained(hci_le_connection_callbacks), address_with_type,
344                                               std::move(acl_connection)));
345   SyncHandler(hci_callback_handler);
346   connection_management_handler->Post(common::BindOnce(
347       &hci::acl_manager::LeConnectionManagementCallbacks::OnReadRemoteVersionInformationComplete,
348       common::Unretained(connection_management_callbacks),
349       hci::ErrorCode::SUCCESS,
350       0,
351       0,
352       0));
353   auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
354   EXPECT_EQ(future_1_status, std::future_status::ready);
355   EXPECT_NE(channel_1, nullptr);
356   auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
357   EXPECT_EQ(future_2_status, std::future_status::ready);
358   EXPECT_NE(channel_2, nullptr);
359   hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
360   channel_1->RegisterOnCloseCallback(
361       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
362   hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
363   channel_2->RegisterOnCloseCallback(
364       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
365 
366   // Step 4: Leave channel IDLE long enough, they will disconnect
367   EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(1);
368   std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 1.2);
369   connection_management_handler->Post(common::BindOnce(
370       &hci::acl_manager::LeConnectionManagementCallbacks::OnDisconnection,
371       common::Unretained(connection_management_callbacks), hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST));
372   SyncHandler(connection_management_handler);
373 
374   // Step 5: Link disconnect will trigger all callbacks
375   SyncHandler(user_handler_);
376   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_1);
377   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_2);
378 }
379 
TEST_F(L2capLeLinkManagerTest,acquiring_channels_should_not_disconnect_acl_after_timeout)380 TEST_F(L2capLeLinkManagerTest, acquiring_channels_should_not_disconnect_acl_after_timeout) {
381   EXPECT_CALL(*mock_parameter_provider_, GetLeLinkIdleDisconnectTimeout)
382       .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
383   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
384   MockAclManager mock_acl_manager;
385   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
386                                          hci::AddressType::RANDOM_DEVICE_ADDRESS);
387 
388   // Step 1: Verify callback registration with HCI
389   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
390   os::Handler* hci_callback_handler = nullptr;
391   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
392       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
393   LinkManager le_link_manager(
394       l2cap_handler_,
395       &mock_acl_manager,
396       &mock_le_fixed_channel_service_manager,
397       nullptr,
398       mock_parameter_provider_);
399   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
400   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
401 
402   // Register fake services
403   MockFixedChannelServiceImpl mock_service_1, mock_service_2;
404   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
405   results.emplace_back(kSmpBrCid, &mock_service_1);
406   results.emplace_back(kConnectionlessCid, &mock_service_2);
407   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
408 
409   // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
410   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(1);
411   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
412       .handler_ = user_handler_,
413       .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
414   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
415 
416   // Step 3: ACL connection success event should trigger channel creation for all registered services
417   auto* raw_acl_connection = new MockLeAclConnection();
418   std::unique_ptr<MockLeAclConnection> acl_connection(raw_acl_connection);
419   EXPECT_CALL(*acl_connection, GetRemoteAddress()).WillRepeatedly(Return(address_with_type));
420   hci::acl_manager::LeConnectionManagementCallbacks* connection_management_callbacks = nullptr;
421   os::Handler* connection_management_handler = nullptr;
422   EXPECT_CALL(*acl_connection, RegisterCallbacks(_, _))
423       .WillOnce(DoAll(SaveArg<0>(&connection_management_callbacks), SaveArg<1>(&connection_management_handler)));
424   std::unique_ptr<FixedChannel> channel_1, channel_2;
425   std::promise<void> promise_1, promise_2;
426   auto future_1 = promise_1.get_future();
427   auto future_2 = promise_2.get_future();
428   EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
429       .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
430         channel_1 = std::move(channel);
431         promise_1.set_value();
432       });
433   EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
434       .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
435         channel_2 = std::move(channel);
436         promise_2.set_value();
437       });
438   hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::LeConnectionCallbacks::OnLeConnectSuccess,
439                                               common::Unretained(hci_le_connection_callbacks), address_with_type,
440                                               std::move(acl_connection)));
441   SyncHandler(hci_callback_handler);
442   connection_management_handler->Post(common::BindOnce(
443       &hci::acl_manager::LeConnectionManagementCallbacks::OnReadRemoteVersionInformationComplete,
444       common::Unretained(connection_management_callbacks),
445       hci::ErrorCode::SUCCESS,
446       0,
447       0,
448       0));
449   auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
450   EXPECT_EQ(future_1_status, std::future_status::ready);
451   EXPECT_NE(channel_1, nullptr);
452   auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
453   EXPECT_EQ(future_2_status, std::future_status::ready);
454   EXPECT_NE(channel_2, nullptr);
455   hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
456   channel_1->RegisterOnCloseCallback(
457       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
458   hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
459   channel_2->RegisterOnCloseCallback(
460       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
461 
462   channel_1->Acquire();
463 
464   // Step 4: ave channel IDLE, it won't disconnect to due acquired channel 1
465   EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(0);
466   std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 2);
467 
468   // Step 5: Link disconnect will trigger all callbacks
469   connection_management_handler->Post(common::BindOnce(
470       &hci::acl_manager::LeConnectionManagementCallbacks::OnDisconnection,
471       common::Unretained(connection_management_callbacks), hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST));
472   SyncHandler(connection_management_handler);
473   SyncHandler(user_handler_);
474   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_1);
475   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_2);
476 }
477 
TEST_F(L2capLeLinkManagerTest,acquiring_and_releasing_channels_should_eventually_disconnect_acl)478 TEST_F(L2capLeLinkManagerTest, acquiring_and_releasing_channels_should_eventually_disconnect_acl) {
479   EXPECT_CALL(*mock_parameter_provider_, GetLeLinkIdleDisconnectTimeout)
480       .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
481   MockFixedChannelServiceManagerImpl mock_le_fixed_channel_service_manager;
482   MockAclManager mock_acl_manager;
483   hci::AddressWithType address_with_type({{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
484                                          hci::AddressType::PUBLIC_IDENTITY_ADDRESS);
485 
486   // Step 1: Verify callback registration with HCI
487   hci::acl_manager::LeConnectionCallbacks* hci_le_connection_callbacks = nullptr;
488   os::Handler* hci_callback_handler = nullptr;
489   EXPECT_CALL(mock_acl_manager, RegisterLeCallbacks(_, _))
490       .WillOnce(DoAll(SaveArg<0>(&hci_le_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
491   LinkManager le_link_manager(
492       l2cap_handler_,
493       &mock_acl_manager,
494       &mock_le_fixed_channel_service_manager,
495       nullptr,
496       mock_parameter_provider_);
497   EXPECT_EQ(hci_le_connection_callbacks, &le_link_manager);
498   EXPECT_EQ(hci_callback_handler, l2cap_handler_);
499 
500   // Register fake services
501   MockFixedChannelServiceImpl mock_service_1, mock_service_2;
502   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
503   results.emplace_back(kSmpBrCid, &mock_service_1);
504   results.emplace_back(kConnectionlessCid, &mock_service_2);
505   EXPECT_CALL(mock_le_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
506 
507   // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
508   EXPECT_CALL(mock_acl_manager, CreateLeConnection(address_with_type, true)).Times(1);
509   LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
510       .handler_ = user_handler_,
511       .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
512   le_link_manager.ConnectFixedChannelServices(address_with_type, std::move(pending_fixed_channel_connection));
513 
514   // Step 3: ACL connection success event should trigger channel creation for all registered services
515   auto* raw_acl_connection = new MockLeAclConnection();
516   std::unique_ptr<MockLeAclConnection> acl_connection(raw_acl_connection);
517   EXPECT_CALL(*acl_connection, GetRemoteAddress()).WillRepeatedly(Return(address_with_type));
518   hci::acl_manager::LeConnectionManagementCallbacks* connection_management_callbacks = nullptr;
519   os::Handler* connection_management_handler = nullptr;
520   EXPECT_CALL(*acl_connection, RegisterCallbacks(_, _))
521       .WillOnce(DoAll(SaveArg<0>(&connection_management_callbacks), SaveArg<1>(&connection_management_handler)));
522   std::unique_ptr<FixedChannel> channel_1, channel_2;
523   std::promise<void> promise_1, promise_2;
524   auto future_1 = promise_1.get_future();
525   auto future_2 = promise_2.get_future();
526   EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
527       .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
528         channel_1 = std::move(channel);
529         promise_1.set_value();
530       });
531   EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
532       .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
533         channel_2 = std::move(channel);
534         promise_2.set_value();
535       });
536   hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::LeConnectionCallbacks::OnLeConnectSuccess,
537                                               common::Unretained(hci_le_connection_callbacks), address_with_type,
538                                               std::move(acl_connection)));
539   SyncHandler(hci_callback_handler);
540   connection_management_handler->Post(common::BindOnce(
541       &hci::acl_manager::LeConnectionManagementCallbacks::OnReadRemoteVersionInformationComplete,
542       common::Unretained(connection_management_callbacks),
543       hci::ErrorCode::SUCCESS,
544       0,
545       0,
546       0));
547   auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
548   EXPECT_EQ(future_1_status, std::future_status::ready);
549   EXPECT_NE(channel_1, nullptr);
550   auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
551   EXPECT_EQ(future_2_status, std::future_status::ready);
552   EXPECT_NE(channel_2, nullptr);
553   hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
554   channel_1->RegisterOnCloseCallback(
555       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
556   hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
557   channel_2->RegisterOnCloseCallback(
558       user_handler_, common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
559 
560   channel_1->Acquire();
561 
562   // Step 4: ave channel IDLE, it won't disconnect to due acquired channel 1
563   EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(0);
564   std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 2);
565 
566   // Step 5: ave channel IDLE long enough, they will disconnect
567   channel_1->Release();
568   EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(1);
569   std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 1.2);
570 
571   // Step 6: Link disconnect will trigger all callbacks
572   connection_management_handler->Post(common::BindOnce(
573       &hci::acl_manager::LeConnectionManagementCallbacks::OnDisconnection,
574       common::Unretained(connection_management_callbacks), hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST));
575   SyncHandler(connection_management_handler);
576   SyncHandler(user_handler_);
577   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_1);
578   EXPECT_EQ(hci::ErrorCode::CONNECTION_TERMINATED_BY_LOCAL_HOST, status_2);
579 }
580 
581 }  // namespace
582 }  // namespace internal
583 }  // namespace le
584 }  // namespace l2cap
585 }  // namespace bluetooth
586