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