1 /*
2 * Copyright 2021 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 <base/bind.h>
18 #include <base/location.h>
19 #include <gtest/gtest.h>
20
21 #include <chrono>
22
23 #include "bta/dm/bta_dm_int.h"
24 #include "bta/hf_client/bta_hf_client_int.h"
25 #include "bta/include/bta_api.h"
26 #include "bta/include/bta_dm_api.h"
27 #include "bta/include/bta_hf_client_api.h"
28 #include "btif/include/stack_manager.h"
29 #include "common/message_loop_thread.h"
30 #include "osi/include/compat.h"
31 #include "stack/include/btm_status.h"
32 #include "test/common/main_handler.h"
33 #include "test/mock/mock_osi_alarm.h"
34 #include "test/mock/mock_osi_allocator.h"
35 #include "test/mock/mock_stack_acl.h"
36 #include "test/mock/mock_stack_btm_sec.h"
37
38 using namespace std::chrono_literals;
39
40 std::map<std::string, int> mock_function_count_map;
41
42 extern struct btm_client_interface_t btm_client_interface;
43
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)44 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
45
46 namespace base {
47 class MessageLoop;
48 } // namespace base
49
50 namespace {
51 constexpr uint8_t kUnusedTimer = BTA_ID_MAX;
52 const RawAddress kRawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
53 const RawAddress kRawAddress2({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
54 constexpr char kRemoteName[] = "TheRemoteName";
55
56 const char* test_flags[] = {
57 "INIT_logging_debug_enabled_for_all=true",
58 nullptr,
59 };
60
bta_dm_search_sm_execute(BT_HDR_RIGID * p_msg)61 bool bta_dm_search_sm_execute(BT_HDR_RIGID* p_msg) { return true; }
bta_dm_search_sm_disable()62 void bta_dm_search_sm_disable() { bta_sys_deregister(BTA_ID_DM_SEARCH); }
63
64 const tBTA_SYS_REG bta_dm_search_reg = {bta_dm_search_sm_execute,
65 bta_dm_search_sm_disable};
66
67 } // namespace
68
69 struct alarm_t {
alarm_talarm_t70 alarm_t(const char* name){};
71 int any_value;
72 };
73
74 class BtaDmTest : public testing::Test {
75 protected:
SetUp()76 void SetUp() override {
77 mock_function_count_map.clear();
78 bluetooth::common::InitFlags::Load(test_flags);
79 test::mock::osi_alarm::alarm_new.body = [](const char* name) -> alarm_t* {
80 return new alarm_t(name);
81 };
82 test::mock::osi_alarm::alarm_free.body = [](alarm_t* alarm) {
83 delete alarm;
84 };
85 test::mock::osi_allocator::osi_malloc.body = [](size_t size) {
86 return malloc(size);
87 };
88 test::mock::osi_allocator::osi_calloc.body = [](size_t size) {
89 return calloc(1UL, size);
90 };
91 test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
92 test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
93 free(*ptr);
94 *ptr = nullptr;
95 };
96
97 main_thread_start_up();
98 post_on_bt_main([]() { LOG_INFO("Main thread started up"); });
99
100 bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
101 bta_dm_init_cb();
102
103 for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
104 for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
105 bta_dm_cb.pm_timer[i].srvc_id[j] = kUnusedTimer;
106 }
107 }
108 }
TearDown()109 void TearDown() override {
110 bta_sys_deregister(BTA_ID_DM_SEARCH);
111 bta_dm_deinit_cb();
112 post_on_bt_main([]() { LOG_INFO("Main thread shutting down"); });
113 main_thread_shut_down();
114
115 test::mock::osi_alarm::alarm_new = {};
116 test::mock::osi_alarm::alarm_free = {};
117 test::mock::osi_allocator::osi_malloc = {};
118 test::mock::osi_allocator::osi_calloc = {};
119 test::mock::osi_allocator::osi_free = {};
120 test::mock::osi_allocator::osi_free_and_reset = {};
121 }
122 };
123
TEST_F(BtaDmTest,nop)124 TEST_F(BtaDmTest, nop) {
125 bool status = true;
126 ASSERT_EQ(true, status);
127 }
128
TEST_F(BtaDmTest,disable_no_acl_links)129 TEST_F(BtaDmTest, disable_no_acl_links) {
130 bta_dm_cb.disabling = true;
131
132 alarm_callback_t alarm_callback;
133 void* alarm_data{nullptr};
134 test::mock::osi_alarm::alarm_set_on_mloop.body =
135 [&alarm_callback, &alarm_data](alarm_t* alarm, uint64_t interval_ms,
136 alarm_callback_t cb, void* data) {
137 ASSERT_TRUE(alarm != nullptr);
138 alarm_callback = cb;
139 alarm_data = data;
140 };
141
142 bta_dm_disable(); // Waiting for all ACL connections to drain
143 ASSERT_EQ(0, mock_function_count_map["btm_remove_acl"]);
144 ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
145
146 // Execute timer callback
147 alarm_callback(alarm_data);
148 ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
149 ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
150 ASSERT_EQ(1, mock_function_count_map["future_ready"]);
151 ASSERT_TRUE(!bta_dm_cb.disabling);
152
153 test::mock::osi_alarm::alarm_set_on_mloop = {};
154 }
155
TEST_F(BtaDmTest,disable_first_pass_with_acl_links)156 TEST_F(BtaDmTest, disable_first_pass_with_acl_links) {
157 uint16_t links_up = 1;
158 test::mock::stack_acl::BTM_GetNumAclLinks.body = [&links_up]() {
159 return links_up;
160 };
161 bta_dm_cb.disabling = true;
162 // ACL link is open
163 bta_dm_cb.device_list.count = 1;
164
165 alarm_callback_t alarm_callback;
166 void* alarm_data{nullptr};
167 test::mock::osi_alarm::alarm_set_on_mloop.body =
168 [&alarm_callback, &alarm_data](alarm_t* alarm, uint64_t interval_ms,
169 alarm_callback_t cb, void* data) {
170 ASSERT_TRUE(alarm != nullptr);
171 alarm_callback = cb;
172 alarm_data = data;
173 };
174
175 bta_dm_disable(); // Waiting for all ACL connections to drain
176 ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
177 ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
178
179 links_up = 0;
180 // First disable pass
181 alarm_callback(alarm_data);
182 ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
183 ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
184 ASSERT_TRUE(!bta_dm_cb.disabling);
185
186 test::mock::stack_acl::BTM_GetNumAclLinks = {};
187 test::mock::osi_alarm::alarm_set_on_mloop = {};
188 }
189
TEST_F(BtaDmTest,disable_second_pass_with_acl_links)190 TEST_F(BtaDmTest, disable_second_pass_with_acl_links) {
191 uint16_t links_up = 1;
192 test::mock::stack_acl::BTM_GetNumAclLinks.body = [&links_up]() {
193 return links_up;
194 };
195 bta_dm_cb.disabling = true;
196 // ACL link is open
197 bta_dm_cb.device_list.count = 1;
198
199 alarm_callback_t alarm_callback;
200 void* alarm_data{nullptr};
201 test::mock::osi_alarm::alarm_set_on_mloop.body =
202 [&alarm_callback, &alarm_data](alarm_t* alarm, uint64_t interval_ms,
203 alarm_callback_t cb, void* data) {
204 ASSERT_TRUE(alarm != nullptr);
205 alarm_callback = cb;
206 alarm_data = data;
207 };
208
209 bta_dm_disable(); // Waiting for all ACL connections to drain
210 ASSERT_EQ(1, mock_function_count_map["alarm_set_on_mloop"]);
211 ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
212
213 // First disable pass
214 alarm_callback(alarm_data);
215 ASSERT_EQ(2, mock_function_count_map["alarm_set_on_mloop"]);
216 ASSERT_EQ(0, mock_function_count_map["BTIF_dm_disable"]);
217 ASSERT_EQ(1, mock_function_count_map["btm_remove_acl"]);
218
219 // Second disable pass
220 alarm_callback(alarm_data);
221 ASSERT_EQ(1, mock_function_count_map["BTIF_dm_disable"]);
222 ASSERT_TRUE(!bta_dm_cb.disabling);
223
224 test::mock::stack_acl::BTM_GetNumAclLinks = {};
225 test::mock::osi_alarm::alarm_set_on_mloop = {};
226 }
227
228 namespace {
229
230 struct BTA_DM_ENCRYPT_CBACK_parms {
231 const RawAddress bd_addr;
232 tBT_TRANSPORT transport;
233 tBTA_STATUS result;
234 };
235
236 std::queue<BTA_DM_ENCRYPT_CBACK_parms> BTA_DM_ENCRYPT_CBACK_queue;
237
BTA_DM_ENCRYPT_CBACK(const RawAddress & bd_addr,tBT_TRANSPORT transport,tBTA_STATUS result)238 void BTA_DM_ENCRYPT_CBACK(const RawAddress& bd_addr, tBT_TRANSPORT transport,
239 tBTA_STATUS result) {
240 BTA_DM_ENCRYPT_CBACK_queue.push({bd_addr, transport, result});
241 }
242
243 } // namespace
244
245 namespace bluetooth {
246 namespace legacy {
247 namespace testing {
248 tBTA_DM_PEER_DEVICE* allocate_device_for(const RawAddress& bd_addr,
249 tBT_TRANSPORT transport);
250
251 void bta_dm_remname_cback(void* p);
252
253 } // namespace testing
254 } // namespace legacy
255 } // namespace bluetooth
256
TEST_F(BtaDmTest,bta_dm_set_encryption)257 TEST_F(BtaDmTest, bta_dm_set_encryption) {
258 const RawAddress bd_addr{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}};
259 const tBT_TRANSPORT transport{BT_TRANSPORT_LE};
260 const tBTM_BLE_SEC_ACT sec_act{BTM_BLE_SEC_NONE};
261
262 // Callback not provided
263 bta_dm_set_encryption(bd_addr, transport, nullptr, sec_act);
264
265 // Device connection does not exist
266 bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
267
268 // Setup a connected device
269 tBTA_DM_PEER_DEVICE* device =
270 bluetooth::legacy::testing::allocate_device_for(bd_addr, transport);
271 ASSERT_TRUE(device != nullptr);
272 device->conn_state = BTA_DM_CONNECTED;
273 device->p_encrypt_cback = nullptr;
274
275 // Setup a device that is busy with another encryption
276 // Fake indication that the encryption is in progress with non-null callback
277 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
278 bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
279 ASSERT_EQ(0, mock_function_count_map["BTM_SetEncryption"]);
280 ASSERT_EQ(1UL, BTA_DM_ENCRYPT_CBACK_queue.size());
281 auto params = BTA_DM_ENCRYPT_CBACK_queue.front();
282 BTA_DM_ENCRYPT_CBACK_queue.pop();
283 ASSERT_EQ(BTA_BUSY, params.result);
284 device->p_encrypt_cback = nullptr;
285
286 // Setup a device that fails encryption
287 test::mock::stack_btm_sec::BTM_SetEncryption.body =
288 [](const RawAddress& bd_addr, tBT_TRANSPORT transport,
289 tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
290 tBTM_BLE_SEC_ACT sec_act) -> tBTM_STATUS {
291 return BTM_MODE_UNSUPPORTED;
292 };
293
294 bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
295 ASSERT_EQ(1, mock_function_count_map["BTM_SetEncryption"]);
296 ASSERT_EQ(0UL, BTA_DM_ENCRYPT_CBACK_queue.size());
297 device->p_encrypt_cback = nullptr;
298
299 // Setup a device that successfully starts encryption
300 test::mock::stack_btm_sec::BTM_SetEncryption.body =
301 [](const RawAddress& bd_addr, tBT_TRANSPORT transport,
302 tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
303 tBTM_BLE_SEC_ACT sec_act) -> tBTM_STATUS { return BTM_CMD_STARTED; };
304
305 bta_dm_set_encryption(bd_addr, transport, BTA_DM_ENCRYPT_CBACK, sec_act);
306 ASSERT_EQ(2, mock_function_count_map["BTM_SetEncryption"]);
307 ASSERT_EQ(0UL, BTA_DM_ENCRYPT_CBACK_queue.size());
308 ASSERT_NE(nullptr, device->p_encrypt_cback);
309
310 test::mock::stack_btm_sec::BTM_SetEncryption = {};
311 BTA_DM_ENCRYPT_CBACK_queue = {};
312 }
313
314 extern void bta_dm_encrypt_cback(const RawAddress* bd_addr,
315 tBT_TRANSPORT transport,
316 UNUSED_ATTR void* p_ref_data,
317 tBTM_STATUS result);
318
TEST_F(BtaDmTest,bta_dm_encrypt_cback)319 TEST_F(BtaDmTest, bta_dm_encrypt_cback) {
320 const RawAddress bd_addr{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}};
321 const tBT_TRANSPORT transport{BT_TRANSPORT_LE};
322
323 // Setup a connected device
324 tBTA_DM_PEER_DEVICE* device =
325 bluetooth::legacy::testing::allocate_device_for(bd_addr, transport);
326 ASSERT_TRUE(device != nullptr);
327 device->conn_state = BTA_DM_CONNECTED;
328
329 // Encryption with no callback set
330 device->p_encrypt_cback = nullptr;
331 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_SUCCESS);
332 ASSERT_EQ(0UL, BTA_DM_ENCRYPT_CBACK_queue.size());
333
334 // Encryption with callback
335 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
336 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_SUCCESS);
337 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
338 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_WRONG_MODE);
339 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
340 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_NO_RESOURCES);
341 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
342 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_BUSY);
343 device->p_encrypt_cback = BTA_DM_ENCRYPT_CBACK;
344 bta_dm_encrypt_cback(&bd_addr, transport, nullptr, BTM_ILLEGAL_VALUE);
345
346 ASSERT_EQ(5UL, BTA_DM_ENCRYPT_CBACK_queue.size());
347
348 auto params_BTM_SUCCESS = BTA_DM_ENCRYPT_CBACK_queue.front();
349 BTA_DM_ENCRYPT_CBACK_queue.pop();
350 ASSERT_EQ(BTA_SUCCESS, params_BTM_SUCCESS.result);
351 auto params_BTM_WRONG_MODE = BTA_DM_ENCRYPT_CBACK_queue.front();
352 BTA_DM_ENCRYPT_CBACK_queue.pop();
353 ASSERT_EQ(BTA_WRONG_MODE, params_BTM_WRONG_MODE.result);
354 auto params_BTM_NO_RESOURCES = BTA_DM_ENCRYPT_CBACK_queue.front();
355 BTA_DM_ENCRYPT_CBACK_queue.pop();
356 ASSERT_EQ(BTA_NO_RESOURCES, params_BTM_NO_RESOURCES.result);
357 auto params_BTM_BUSY = BTA_DM_ENCRYPT_CBACK_queue.front();
358 BTA_DM_ENCRYPT_CBACK_queue.pop();
359 ASSERT_EQ(BTA_BUSY, params_BTM_BUSY.result);
360 auto params_BTM_ILLEGAL_VALUE = BTA_DM_ENCRYPT_CBACK_queue.front();
361 BTA_DM_ENCRYPT_CBACK_queue.pop();
362 ASSERT_EQ(BTA_FAILURE, params_BTM_ILLEGAL_VALUE.result);
363 }
364
TEST_F(BtaDmTest,bta_dm_event_text)365 TEST_F(BtaDmTest, bta_dm_event_text) {
366 std::vector<std::pair<tBTA_DM_EVT, std::string>> events = {
367 std::make_pair(BTA_DM_API_SEARCH_EVT, "BTA_DM_API_SEARCH_EVT"),
368 std::make_pair(BTA_DM_API_DISCOVER_EVT, "BTA_DM_API_DISCOVER_EVT"),
369 std::make_pair(BTA_DM_INQUIRY_CMPL_EVT, "BTA_DM_INQUIRY_CMPL_EVT"),
370 std::make_pair(BTA_DM_REMT_NAME_EVT, "BTA_DM_REMT_NAME_EVT"),
371 std::make_pair(BTA_DM_SDP_RESULT_EVT, "BTA_DM_SDP_RESULT_EVT"),
372 std::make_pair(BTA_DM_SEARCH_CMPL_EVT, "BTA_DM_SEARCH_CMPL_EVT"),
373 std::make_pair(BTA_DM_DISCOVERY_RESULT_EVT,
374 "BTA_DM_DISCOVERY_RESULT_EVT"),
375 std::make_pair(BTA_DM_DISC_CLOSE_TOUT_EVT, "BTA_DM_DISC_CLOSE_TOUT_EVT"),
376 };
377 for (const auto& event : events) {
378 ASSERT_STREQ(event.second.c_str(), bta_dm_event_text(event.first).c_str());
379 }
380 ASSERT_STREQ(base::StringPrintf("UNKNOWN[0x%04x]",
381 std::numeric_limits<uint16_t>::max())
382 .c_str(),
383 bta_dm_event_text(static_cast<tBTA_DM_EVT>(
384 std::numeric_limits<uint16_t>::max()))
385 .c_str());
386 }
387
TEST_F(BtaDmTest,bta_dm_state_text)388 TEST_F(BtaDmTest, bta_dm_state_text) {
389 std::vector<std::pair<tBTA_DM_STATE, std::string>> states = {
390 std::make_pair(BTA_DM_SEARCH_IDLE, "BTA_DM_SEARCH_IDLE"),
391 std::make_pair(BTA_DM_SEARCH_ACTIVE, "BTA_DM_SEARCH_ACTIVE"),
392 std::make_pair(BTA_DM_SEARCH_CANCELLING, "BTA_DM_SEARCH_CANCELLING"),
393 std::make_pair(BTA_DM_DISCOVER_ACTIVE, "BTA_DM_DISCOVER_ACTIVE"),
394 };
395 for (const auto& state : states) {
396 ASSERT_STREQ(state.second.c_str(), bta_dm_state_text(state.first).c_str());
397 }
398 auto unknown =
399 base::StringPrintf("UNKNOWN[%d]", std::numeric_limits<int>::max());
400 ASSERT_STREQ(unknown.c_str(),
401 bta_dm_state_text(
402 static_cast<tBTA_DM_STATE>(std::numeric_limits<int>::max()))
403 .c_str());
404 }
405
TEST_F(BtaDmTest,bta_dm_remname_cback__typical)406 TEST_F(BtaDmTest, bta_dm_remname_cback__typical) {
407 bta_dm_search_cb = {
408 .name_discover_done = false,
409 .peer_bdaddr = kRawAddress,
410 };
411
412 tBTM_REMOTE_DEV_NAME name = {
413 .status = BTM_SUCCESS,
414 .bd_addr = kRawAddress,
415 .length = static_cast<uint16_t>(strlen(kRemoteName)),
416 .remote_bd_name = {},
417 .hci_status = HCI_SUCCESS,
418 };
419 strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
420 strlen(kRemoteName));
421
422 bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));
423
424 sync_main_handler();
425
426 ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
427 ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
428 }
429
TEST_F(BtaDmTest,bta_dm_remname_cback__wrong_address)430 TEST_F(BtaDmTest, bta_dm_remname_cback__wrong_address) {
431 bta_dm_search_cb = {
432 .name_discover_done = false,
433 .peer_bdaddr = kRawAddress,
434 };
435
436 tBTM_REMOTE_DEV_NAME name = {
437 .status = BTM_SUCCESS,
438 .bd_addr = kRawAddress2,
439 .length = static_cast<uint16_t>(strlen(kRemoteName)),
440 .remote_bd_name = {},
441 .hci_status = HCI_SUCCESS,
442 };
443 strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
444 strlen(kRemoteName));
445
446 bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));
447
448 sync_main_handler();
449
450 ASSERT_EQ(0, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
451 ASSERT_FALSE(bta_dm_search_cb.name_discover_done);
452 }
453
TEST_F(BtaDmTest,bta_dm_remname_cback__HCI_ERR_CONNECTION_EXISTS)454 TEST_F(BtaDmTest, bta_dm_remname_cback__HCI_ERR_CONNECTION_EXISTS) {
455 bta_dm_search_cb = {
456 .name_discover_done = false,
457 .peer_bdaddr = kRawAddress,
458 };
459
460 tBTM_REMOTE_DEV_NAME name = {
461 .status = BTM_SUCCESS,
462 .bd_addr = RawAddress::kEmpty,
463 .length = static_cast<uint16_t>(strlen(kRemoteName)),
464 .remote_bd_name = {},
465 .hci_status = HCI_ERR_CONNECTION_EXISTS,
466 };
467 strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
468 strlen(kRemoteName));
469
470 bluetooth::legacy::testing::bta_dm_remname_cback(static_cast<void*>(&name));
471
472 sync_main_handler();
473
474 ASSERT_EQ(1, mock_function_count_map["BTM_SecDeleteRmtNameNotifyCallback"]);
475 ASSERT_TRUE(bta_dm_search_cb.name_discover_done);
476 }
477