1 /*
2  * Copyright 2019 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 #include "l2cap/classic/internal/fixed_channel_impl.h"
17 
18 #include "common/testing/bind_test_util.h"
19 #include "l2cap/cid.h"
20 #include "l2cap/classic/internal/link_mock.h"
21 #include "l2cap/internal/parameter_provider_mock.h"
22 #include "os/handler.h"
23 
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 
27 namespace bluetooth {
28 namespace l2cap {
29 namespace classic {
30 namespace internal {
31 
32 using l2cap::internal::testing::MockParameterProvider;
33 using ::testing::_;
34 using testing::MockLink;
35 using ::testing::Return;
36 
37 class L2capClassicFixedChannelImplTest : public ::testing::Test {
38  public:
SyncHandler(os::Handler * handler)39   static void SyncHandler(os::Handler* handler) {
40     std::promise<void> promise;
41     auto future = promise.get_future();
42     handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
43     future.wait_for(std::chrono::seconds(1));
44   }
45 
46  protected:
SetUp()47   void SetUp() override {
48     thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
49     l2cap_handler_ = new os::Handler(thread_);
50   }
51 
TearDown()52   void TearDown() override {
53     l2cap_handler_->Clear();
54     delete l2cap_handler_;
55     delete thread_;
56   }
57 
58   os::Thread* thread_ = nullptr;
59   os::Handler* l2cap_handler_ = nullptr;
60 };
61 
TEST_F(L2capClassicFixedChannelImplTest,get_device)62 TEST_F(L2capClassicFixedChannelImplTest, get_device) {
63   MockParameterProvider mock_parameter_provider;
64   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
65       .WillRepeatedly(Return(std::chrono::seconds(5)));
66   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
67   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
68   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
69   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
70                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
71                              nullptr /* LinkManager */);
72   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
73                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
74   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
75   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
76   EXPECT_EQ(device.GetAddress(), fixed_channel_impl.GetDevice());
77 }
78 
TEST_F(L2capClassicFixedChannelImplTest,close_triggers_callback)79 TEST_F(L2capClassicFixedChannelImplTest, close_triggers_callback) {
80   MockParameterProvider mock_parameter_provider;
81   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
82       .WillRepeatedly(Return(std::chrono::seconds(5)));
83   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
84   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
85   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
86   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
87                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
88                              nullptr /* LinkManager */);
89   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
90                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
91   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
92   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
93 
94   // Register on close callback
95   auto user_handler = std::make_unique<os::Handler>(thread_);
96   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
97   fixed_channel_impl.RegisterOnCloseCallback(
98       user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
99 
100   // Channel closure should trigger such callback
101   fixed_channel_impl.OnClosed(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION);
102   SyncHandler(user_handler.get());
103   EXPECT_EQ(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, my_status);
104 
105   user_handler->Clear();
106 }
107 
TEST_F(L2capClassicFixedChannelImplTest,register_callback_after_close_should_call_immediately)108 TEST_F(L2capClassicFixedChannelImplTest, register_callback_after_close_should_call_immediately) {
109   MockParameterProvider mock_parameter_provider;
110   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
111       .WillRepeatedly(Return(std::chrono::seconds(5)));
112   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
113   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
114   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
115   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
116                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
117                              nullptr /* LinkManager */);
118   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
119                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
120   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
121   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
122 
123   // Channel closure should do nothing
124   fixed_channel_impl.OnClosed(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION);
125 
126   // Register on close callback should trigger callback immediately
127   auto user_handler = std::make_unique<os::Handler>(thread_);
128   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
129   fixed_channel_impl.RegisterOnCloseCallback(
130       user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
131   SyncHandler(user_handler.get());
132   EXPECT_EQ(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, my_status);
133 
134   user_handler->Clear();
135 }
136 
TEST_F(L2capClassicFixedChannelImplTest,close_twice_should_fail)137 TEST_F(L2capClassicFixedChannelImplTest, close_twice_should_fail) {
138   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
139   MockParameterProvider mock_parameter_provider;
140   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
141       .WillRepeatedly(Return(std::chrono::seconds(5)));
142   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
143   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
144   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
145   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
146                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
147                              nullptr /* LinkManager */);
148   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
149                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
150   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
151   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
152 
153   // Register on close callback
154   auto user_handler = std::make_unique<os::Handler>(thread_);
155   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
156   fixed_channel_impl.RegisterOnCloseCallback(
157       user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
158 
159   // Channel closure should trigger such callback
160   fixed_channel_impl.OnClosed(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION);
161   SyncHandler(user_handler.get());
162   EXPECT_EQ(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, my_status);
163 
164   // 2nd OnClose() callback should fail
165   EXPECT_DEATH(fixed_channel_impl.OnClosed(hci::ErrorCode::PAGE_TIMEOUT), ".*assertion \'!closed_\' failed.*");
166 
167   user_handler->Clear();
168 }
169 
TEST_F(L2capClassicFixedChannelImplTest,multiple_registration_should_fail)170 TEST_F(L2capClassicFixedChannelImplTest, multiple_registration_should_fail) {
171   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
172   MockParameterProvider mock_parameter_provider;
173   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
174       .WillRepeatedly(Return(std::chrono::seconds(5)));
175   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
176   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
177   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
178   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
179                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
180                              nullptr /* LinkManager */);
181   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
182                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
183   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
184   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
185 
186   // Register on close callback
187   auto user_handler = std::make_unique<os::Handler>(thread_);
188   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
189   fixed_channel_impl.RegisterOnCloseCallback(
190       user_handler.get(), common::testing::BindLambdaForTesting([&](hci::ErrorCode status) { my_status = status; }));
191 
192   EXPECT_DEATH(fixed_channel_impl.RegisterOnCloseCallback(user_handler.get(),
193                                                           common::BindOnce([](hci::ErrorCode status) { FAIL(); })),
194                ".*OnCloseCallback can only be registered once.*");
195 
196   user_handler->Clear();
197 }
198 
TEST_F(L2capClassicFixedChannelImplTest,call_acquire_before_registration_should_fail)199 TEST_F(L2capClassicFixedChannelImplTest, call_acquire_before_registration_should_fail) {
200   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
201   MockParameterProvider mock_parameter_provider;
202   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
203       .WillRepeatedly(Return(std::chrono::seconds(5)));
204   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
205   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
206   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
207   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
208                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
209                              nullptr /* LinkManager */);
210   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
211                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
212   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
213   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
214   EXPECT_DEATH(fixed_channel_impl.Acquire(), ".*Must register OnCloseCallback before calling any methods.*");
215 }
216 
TEST_F(L2capClassicFixedChannelImplTest,call_release_before_registration_should_fail)217 TEST_F(L2capClassicFixedChannelImplTest, call_release_before_registration_should_fail) {
218   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
219   MockParameterProvider mock_parameter_provider;
220   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
221       .WillRepeatedly(Return(std::chrono::seconds(5)));
222   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
223   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
224   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
225   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
226                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
227                              nullptr /* LinkManager */);
228   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
229                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
230   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
231   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
232   EXPECT_DEATH(fixed_channel_impl.Release(), ".*Must register OnCloseCallback before calling any methods.*");
233 }
234 
TEST_F(L2capClassicFixedChannelImplTest,test_acquire_release_channel)235 TEST_F(L2capClassicFixedChannelImplTest, test_acquire_release_channel) {
236   MockParameterProvider mock_parameter_provider;
237   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
238       .WillRepeatedly(Return(std::chrono::seconds(5)));
239   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
240       .WillRepeatedly(Return(std::chrono::seconds(5)));
241   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
242   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
243   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
244   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
245                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
246                              nullptr /* LinkManager */);
247   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
248                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
249   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
250   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
251 
252   // Register on close callback
253   auto user_handler = std::make_unique<os::Handler>(thread_);
254   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
255   fixed_channel_impl.RegisterOnCloseCallback(
256       user_handler.get(),
257       common::testing::BindLambdaForTesting([&my_status](hci::ErrorCode status) { my_status = status; }));
258 
259   // Default should be false
260   EXPECT_FALSE(fixed_channel_impl.IsAcquired());
261 
262   // Should be called 2 times after Acquire() and Release()
263   EXPECT_CALL(mock_classic_link, RefreshRefCount()).Times(2);
264 
265   fixed_channel_impl.Acquire();
266   EXPECT_TRUE(fixed_channel_impl.IsAcquired());
267 
268   fixed_channel_impl.Release();
269   EXPECT_FALSE(fixed_channel_impl.IsAcquired());
270 
271   user_handler->Clear();
272 }
273 
TEST_F(L2capClassicFixedChannelImplTest,test_acquire_after_close)274 TEST_F(L2capClassicFixedChannelImplTest, test_acquire_after_close) {
275   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
276   MockParameterProvider mock_parameter_provider;
277   EXPECT_CALL(mock_parameter_provider, GetClassicLinkIdleDisconnectTimeout())
278       .WillRepeatedly(Return(std::chrono::seconds(5)));
279   testing::MockClassicAclConnection* mock_acl_connection = new testing::MockClassicAclConnection();
280   EXPECT_CALL(*mock_acl_connection, GetAddress()).Times(1);
281   EXPECT_CALL(*mock_acl_connection, RegisterCallbacks(_, l2cap_handler_)).Times(1);
282   MockLink mock_classic_link(l2cap_handler_, &mock_parameter_provider,
283                              std::unique_ptr<testing::MockClassicAclConnection>(mock_acl_connection),
284                              nullptr /* LinkManager */);
285   hci::AddressWithType device{hci::Address{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}},
286                               hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
287   EXPECT_CALL(mock_classic_link, GetDevice()).WillRepeatedly(Return(device));
288   FixedChannelImpl fixed_channel_impl(kSmpBrCid, &mock_classic_link, l2cap_handler_);
289 
290   // Register on close callback
291   auto user_handler = std::make_unique<os::Handler>(thread_);
292   hci::ErrorCode my_status = hci::ErrorCode::SUCCESS;
293   std::promise<void> promise;
294   auto future = promise.get_future();
295   fixed_channel_impl.RegisterOnCloseCallback(user_handler.get(),
296                                              common::testing::BindLambdaForTesting([&](hci::ErrorCode status) {
297                                                my_status = status;
298                                                promise.set_value();
299                                              }));
300 
301   // Channel closure should trigger such callback
302   fixed_channel_impl.OnClosed(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION);
303   SyncHandler(user_handler.get());
304   auto future_status = future.wait_for(std::chrono::seconds(1));
305   EXPECT_EQ(future_status, std::future_status::ready);
306   EXPECT_EQ(hci::ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, my_status);
307 
308   // Release or Acquire after closing should crash
309   EXPECT_CALL(mock_classic_link, RefreshRefCount()).Times(0);
310   EXPECT_FALSE(fixed_channel_impl.IsAcquired());
311   EXPECT_DEATH(fixed_channel_impl.Acquire(), ".*Must register OnCloseCallback before calling any methods.*");
312 
313   user_handler->Clear();
314 }
315 
316 }  // namespace internal
317 }  // namespace classic
318 }  // namespace l2cap
319 }  // namespace bluetooth
320