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