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