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(&hci::acl_manager::ConnectionCallbacks::OnConnectFail,
257 common::Unretained(hci_connection_callbacks), device,
258 hci::ErrorCode::PAGE_TIMEOUT));
259 SyncHandler(hci_callback_handler);
260 SyncHandler(user_handler.get());
261 EXPECT_EQ(my_result.connection_result_code, FixedChannelManager::ConnectionResultCode::FAIL_HCI_ERROR);
262 EXPECT_EQ(my_result.hci_error, hci::ErrorCode::PAGE_TIMEOUT);
263
264 user_handler->Clear();
265 }
266
TEST_F(L2capClassicLinkManagerTest,not_acquiring_channels_should_disconnect_acl_after_timeout)267 TEST_F(L2capClassicLinkManagerTest, not_acquiring_channels_should_disconnect_acl_after_timeout) {
268 EXPECT_CALL(*mock_parameter_provider_, GetClassicLinkIdleDisconnectTimeout)
269 .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
270 MockFixedChannelServiceManagerImpl mock_classic_fixed_channel_service_manager;
271 MockAclManager mock_acl_manager;
272 hci::Address device{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
273 auto user_handler = std::make_unique<os::Handler>(thread_);
274
275 // Step 1: Verify callback registration with HCI
276 hci::acl_manager::ConnectionCallbacks* hci_connection_callbacks = nullptr;
277 os::Handler* hci_callback_handler = nullptr;
278 EXPECT_CALL(mock_acl_manager, RegisterCallbacks(_, _))
279 .WillOnce(DoAll(SaveArg<0>(&hci_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
280 LinkManager classic_link_manager(l2cap_handler_, &mock_acl_manager, &mock_classic_fixed_channel_service_manager,
281 nullptr, mock_parameter_provider_);
282 EXPECT_EQ(hci_connection_callbacks, &classic_link_manager);
283 EXPECT_EQ(hci_callback_handler, l2cap_handler_);
284
285 // Register fake services
286 MockFixedChannelServiceImpl mock_service_1, mock_service_2;
287 std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
288 results.emplace_back(kSmpBrCid, &mock_service_1);
289 results.emplace_back(kConnectionlessCid, &mock_service_2);
290 EXPECT_CALL(mock_classic_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
291
292 // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
293 EXPECT_CALL(mock_acl_manager, CreateConnection(device)).Times(1);
294 LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
295 .handler_ = user_handler.get(),
296 .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
297 classic_link_manager.ConnectFixedChannelServices(device, std::move(pending_fixed_channel_connection));
298
299 // Step 3: ACL connection success event should trigger channel creation for all registered services
300 auto* raw_acl_connection = new MockClassicAclConnection();
301 std::unique_ptr<MockClassicAclConnection> acl_connection(raw_acl_connection);
302 EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
303 EXPECT_CALL(*acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
304 std::unique_ptr<FixedChannel> channel_1, channel_2;
305 std::promise<void> promise_1, promise_2;
306 auto future_1 = promise_1.get_future();
307 auto future_2 = promise_2.get_future();
308 EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
309 .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
310 channel_1 = std::move(channel);
311 promise_1.set_value();
312 });
313 EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
314 .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
315 channel_2 = std::move(channel);
316 promise_2.set_value();
317 });
318 hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::ConnectionCallbacks::OnConnectSuccess,
319 common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
320 SyncHandler(hci_callback_handler);
321 auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
322 EXPECT_EQ(future_1_status, std::future_status::ready);
323 EXPECT_NE(channel_1, nullptr);
324 auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
325 EXPECT_EQ(future_2_status, std::future_status::ready);
326 EXPECT_NE(channel_2, nullptr);
327 hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
328 channel_1->RegisterOnCloseCallback(
329 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
330 hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
331 channel_2->RegisterOnCloseCallback(
332 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
333
334 // Step 4: Leave channel IDLE long enough, they will disconnect
335 EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(1);
336 std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 1.2);
337
338 user_handler->Clear();
339 }
340
TEST_F(L2capClassicLinkManagerTest,acquiring_channels_should_not_disconnect_acl_after_timeout)341 TEST_F(L2capClassicLinkManagerTest, acquiring_channels_should_not_disconnect_acl_after_timeout) {
342 EXPECT_CALL(*mock_parameter_provider_, GetClassicLinkIdleDisconnectTimeout)
343 .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
344 MockFixedChannelServiceManagerImpl mock_classic_fixed_channel_service_manager;
345 MockAclManager mock_acl_manager;
346 hci::Address device{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
347 auto user_handler = std::make_unique<os::Handler>(thread_);
348
349 // Step 1: Verify callback registration with HCI
350 hci::acl_manager::ConnectionCallbacks* hci_connection_callbacks = nullptr;
351 os::Handler* hci_callback_handler = nullptr;
352 EXPECT_CALL(mock_acl_manager, RegisterCallbacks(_, _))
353 .WillOnce(DoAll(SaveArg<0>(&hci_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
354 LinkManager classic_link_manager(l2cap_handler_, &mock_acl_manager, &mock_classic_fixed_channel_service_manager,
355 nullptr, mock_parameter_provider_);
356 EXPECT_EQ(hci_connection_callbacks, &classic_link_manager);
357 EXPECT_EQ(hci_callback_handler, l2cap_handler_);
358
359 // Register fake services
360 MockFixedChannelServiceImpl mock_service_1, mock_service_2;
361 std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
362 results.emplace_back(kSmpBrCid, &mock_service_1);
363 results.emplace_back(kConnectionlessCid, &mock_service_2);
364 EXPECT_CALL(mock_classic_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
365
366 // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
367 EXPECT_CALL(mock_acl_manager, CreateConnection(device)).Times(1);
368 LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
369 .handler_ = user_handler.get(),
370 .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
371 classic_link_manager.ConnectFixedChannelServices(device, std::move(pending_fixed_channel_connection));
372
373 // Step 3: ACL connection success event should trigger channel creation for all registered services
374 auto* raw_acl_connection = new MockClassicAclConnection();
375 std::unique_ptr<MockClassicAclConnection> acl_connection(raw_acl_connection);
376 EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
377 EXPECT_CALL(*acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
378 std::unique_ptr<FixedChannel> channel_1, channel_2;
379 std::promise<void> promise_1, promise_2;
380 auto future_1 = promise_1.get_future();
381 auto future_2 = promise_2.get_future();
382 EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
383 .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
384 channel_1 = std::move(channel);
385 promise_1.set_value();
386 });
387 EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
388 .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
389 channel_2 = std::move(channel);
390 promise_2.set_value();
391 });
392 hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::ConnectionCallbacks::OnConnectSuccess,
393 common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
394 SyncHandler(hci_callback_handler);
395 auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
396 EXPECT_EQ(future_1_status, std::future_status::ready);
397 EXPECT_NE(channel_1, nullptr);
398 auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
399 EXPECT_EQ(future_2_status, std::future_status::ready);
400 EXPECT_NE(channel_2, nullptr);
401 hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
402 channel_1->RegisterOnCloseCallback(
403 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
404 hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
405 channel_2->RegisterOnCloseCallback(
406 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
407
408 channel_1->Acquire();
409
410 // Step 4: Leave channel IDLE, it won't disconnect to due acquired channel 1
411 EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(0);
412 std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 2);
413
414 user_handler->Clear();
415 }
416
TEST_F(L2capClassicLinkManagerTest,acquiring_and_releasing_channels_should_eventually_disconnect_acl)417 TEST_F(L2capClassicLinkManagerTest, acquiring_and_releasing_channels_should_eventually_disconnect_acl) {
418 EXPECT_CALL(*mock_parameter_provider_, GetClassicLinkIdleDisconnectTimeout)
419 .WillRepeatedly(Return(kTestIdleDisconnectTimeoutShort));
420 MockFixedChannelServiceManagerImpl mock_classic_fixed_channel_service_manager;
421 MockAclManager mock_acl_manager;
422 hci::Address device{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
423 auto user_handler = std::make_unique<os::Handler>(thread_);
424
425 // Step 1: Verify callback registration with HCI
426 hci::acl_manager::ConnectionCallbacks* hci_connection_callbacks = nullptr;
427 os::Handler* hci_callback_handler = nullptr;
428 EXPECT_CALL(mock_acl_manager, RegisterCallbacks(_, _))
429 .WillOnce(DoAll(SaveArg<0>(&hci_connection_callbacks), SaveArg<1>(&hci_callback_handler)));
430 LinkManager classic_link_manager(l2cap_handler_, &mock_acl_manager, &mock_classic_fixed_channel_service_manager,
431 nullptr, mock_parameter_provider_);
432 EXPECT_EQ(hci_connection_callbacks, &classic_link_manager);
433 EXPECT_EQ(hci_callback_handler, l2cap_handler_);
434
435 // Register fake services
436 MockFixedChannelServiceImpl mock_service_1, mock_service_2;
437 std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
438 results.emplace_back(kSmpBrCid, &mock_service_1);
439 results.emplace_back(kConnectionlessCid, &mock_service_2);
440 EXPECT_CALL(mock_classic_fixed_channel_service_manager, GetRegisteredServices()).WillRepeatedly(Return(results));
441
442 // Step 2: Connect to fixed channel without ACL connection should trigger ACL connection process
443 EXPECT_CALL(mock_acl_manager, CreateConnection(device)).Times(1);
444 LinkManager::PendingFixedChannelConnection pending_fixed_channel_connection{
445 .handler_ = user_handler.get(),
446 .on_fail_callback_ = common::BindOnce([](FixedChannelManager::ConnectionResult result) { FAIL(); })};
447 classic_link_manager.ConnectFixedChannelServices(device, std::move(pending_fixed_channel_connection));
448
449 // Step 3: ACL connection success event should trigger channel creation for all registered services
450 auto* raw_acl_connection = new MockClassicAclConnection();
451 std::unique_ptr<MockClassicAclConnection> acl_connection(raw_acl_connection);
452 EXPECT_CALL(*acl_connection, GetAddress()).WillRepeatedly(Return(device));
453 EXPECT_CALL(*acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
454 std::unique_ptr<FixedChannel> channel_1, channel_2;
455 std::promise<void> promise_1, promise_2;
456 auto future_1 = promise_1.get_future();
457 auto future_2 = promise_2.get_future();
458 EXPECT_CALL(mock_service_1, NotifyChannelCreation(_))
459 .WillOnce([&channel_1, &promise_1](std::unique_ptr<FixedChannel> channel) {
460 channel_1 = std::move(channel);
461 promise_1.set_value();
462 });
463 EXPECT_CALL(mock_service_2, NotifyChannelCreation(_))
464 .WillOnce([&channel_2, &promise_2](std::unique_ptr<FixedChannel> channel) {
465 channel_2 = std::move(channel);
466 promise_2.set_value();
467 });
468 hci_callback_handler->Post(common::BindOnce(&hci::acl_manager::ConnectionCallbacks::OnConnectSuccess,
469 common::Unretained(hci_connection_callbacks), std::move(acl_connection)));
470 SyncHandler(hci_callback_handler);
471 auto future_1_status = future_1.wait_for(kTestIdleDisconnectTimeoutShort);
472 EXPECT_EQ(future_1_status, std::future_status::ready);
473 EXPECT_NE(channel_1, nullptr);
474 auto future_2_status = future_2.wait_for(kTestIdleDisconnectTimeoutShort);
475 EXPECT_EQ(future_2_status, std::future_status::ready);
476 EXPECT_NE(channel_2, nullptr);
477 hci::ErrorCode status_1 = hci::ErrorCode::SUCCESS;
478 channel_1->RegisterOnCloseCallback(
479 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_1 = status; }));
480 hci::ErrorCode status_2 = hci::ErrorCode::SUCCESS;
481 channel_2->RegisterOnCloseCallback(
482 user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { status_2 = status; }));
483
484 channel_1->Acquire();
485
486 // Step 4: Leave channel IDLE, it won't disconnect to due acquired channel 1
487 EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(0);
488 std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 2);
489
490 // Step 5: Leave channel IDLE long enough, they will disconnect
491 channel_1->Release();
492 EXPECT_CALL(*raw_acl_connection, Disconnect(hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION)).Times(1);
493 std::this_thread::sleep_for(kTestIdleDisconnectTimeoutShort * 1.2);
494
495 user_handler->Clear();
496 }
497
498 } // namespace
499 } // namespace internal
500 } // namespace classic
501 } // namespace l2cap
502 } // namespace bluetooth
503