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