1 /*
2 *
3 * Copyright 2020 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <iomanip>
23 #include <iostream>
24 #include <map>
25 #include <sstream>
26 #include <vector>
27
28 #include "btif/include/btif_hh.h"
29 #include "common/init_flags.h"
30 #include "hci/include/hci_layer.h"
31 #include "hci/include/hci_packet_factory.h"
32 #include "internal_include/stack_config.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/osi.h"
35 #include "stack/btm/btm_dev.h"
36 #include "stack/btm/btm_int_types.h"
37 #include "stack/btm/btm_sco.h"
38 #include "stack/btm/btm_sec.h"
39 #include "stack/btm/security_device_record.h"
40 #include "stack/include/acl_api.h"
41 #include "stack/include/acl_hci_link_interface.h"
42 #include "stack/include/btm_client_interface.h"
43 #include "stack/include/hcidefs.h"
44 #include "stack/include/sec_hci_link_interface.h"
45 #include "stack/l2cap/l2c_int.h"
46 #include "test/mock/mock_osi_list.h"
47 #include "test/mock/mock_stack_hcic_hcicmds.h"
48 #include "types/raw_address.h"
49
50 using testing::Each;
51 using testing::Eq;
52
53 namespace mock = test::mock::stack_hcic_hcicmds;
54
55 extern tBTM_CB btm_cb;
56
57 uint8_t appl_trace_level = BT_TRACE_LEVEL_VERBOSE;
58 btif_hh_cb_t btif_hh_cb;
59 tL2C_CB l2cb;
60
hci_layer_get_interface()61 const hci_t* hci_layer_get_interface() { return nullptr; }
62
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)63 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {}
64
65 const std::string kSmpOptions("mock smp options");
66 const std::string kBroadcastAudioConfigOptions(
67 "mock broadcast audio config options");
68
get_trace_config_enabled(void)69 bool get_trace_config_enabled(void) { return false; }
get_pts_avrcp_test(void)70 bool get_pts_avrcp_test(void) { return false; }
get_pts_secure_only_mode(void)71 bool get_pts_secure_only_mode(void) { return false; }
get_pts_conn_updates_disabled(void)72 bool get_pts_conn_updates_disabled(void) { return false; }
get_pts_crosskey_sdp_disable(void)73 bool get_pts_crosskey_sdp_disable(void) { return false; }
get_pts_smp_options(void)74 const std::string* get_pts_smp_options(void) { return &kSmpOptions; }
get_pts_smp_failure_case(void)75 int get_pts_smp_failure_case(void) { return 123; }
get_pts_force_eatt_for_notifications(void)76 bool get_pts_force_eatt_for_notifications(void) { return false; }
get_pts_connect_eatt_unconditionally(void)77 bool get_pts_connect_eatt_unconditionally(void) { return false; }
get_pts_connect_eatt_before_encryption(void)78 bool get_pts_connect_eatt_before_encryption(void) { return false; }
get_pts_unencrypt_broadcast(void)79 bool get_pts_unencrypt_broadcast(void) { return false; }
get_pts_eatt_peripheral_collision_support(void)80 bool get_pts_eatt_peripheral_collision_support(void) { return false; }
get_pts_use_eatt_for_all_services(void)81 bool get_pts_use_eatt_for_all_services(void) { return false; }
get_pts_force_le_audio_multiple_contexts_metadata(void)82 bool get_pts_force_le_audio_multiple_contexts_metadata(void) { return false; }
get_pts_l2cap_ecoc_upper_tester(void)83 bool get_pts_l2cap_ecoc_upper_tester(void) { return false; }
get_pts_l2cap_ecoc_min_key_size(void)84 int get_pts_l2cap_ecoc_min_key_size(void) { return -1; }
get_pts_l2cap_ecoc_initial_chan_cnt(void)85 int get_pts_l2cap_ecoc_initial_chan_cnt(void) { return -1; }
get_pts_l2cap_ecoc_connect_remaining(void)86 bool get_pts_l2cap_ecoc_connect_remaining(void) { return false; }
get_pts_l2cap_ecoc_send_num_of_sdu(void)87 int get_pts_l2cap_ecoc_send_num_of_sdu(void) { return -1; }
get_pts_l2cap_ecoc_reconfigure(void)88 bool get_pts_l2cap_ecoc_reconfigure(void) { return false; }
get_pts_broadcast_audio_config_options(void)89 const std::string* get_pts_broadcast_audio_config_options(void) {
90 return &kBroadcastAudioConfigOptions;
91 }
get_pts_le_audio_disable_ases_before_stopping(void)92 bool get_pts_le_audio_disable_ases_before_stopping(void) { return false; }
get_all(void)93 config_t* get_all(void) { return nullptr; }
packet_fragmenter_get_interface()94 const packet_fragmenter_t* packet_fragmenter_get_interface() { return nullptr; }
95
96 stack_config_t mock_stack_config{
97 .get_trace_config_enabled = get_trace_config_enabled,
98 .get_pts_avrcp_test = get_pts_avrcp_test,
99 .get_pts_secure_only_mode = get_pts_secure_only_mode,
100 .get_pts_conn_updates_disabled = get_pts_conn_updates_disabled,
101 .get_pts_crosskey_sdp_disable = get_pts_crosskey_sdp_disable,
102 .get_pts_smp_options = get_pts_smp_options,
103 .get_pts_smp_failure_case = get_pts_smp_failure_case,
104 .get_pts_force_eatt_for_notifications =
105 get_pts_force_eatt_for_notifications,
106 .get_pts_connect_eatt_unconditionally =
107 get_pts_connect_eatt_unconditionally,
108 .get_pts_connect_eatt_before_encryption =
109 get_pts_connect_eatt_before_encryption,
110 .get_pts_unencrypt_broadcast = get_pts_unencrypt_broadcast,
111 .get_pts_eatt_peripheral_collision_support =
112 get_pts_eatt_peripheral_collision_support,
113 .get_pts_l2cap_ecoc_upper_tester = get_pts_l2cap_ecoc_upper_tester,
114 .get_pts_l2cap_ecoc_min_key_size = get_pts_l2cap_ecoc_min_key_size,
115 .get_pts_force_le_audio_multiple_contexts_metadata =
116 get_pts_force_le_audio_multiple_contexts_metadata,
117 .get_pts_l2cap_ecoc_initial_chan_cnt = get_pts_l2cap_ecoc_initial_chan_cnt,
118 .get_pts_l2cap_ecoc_connect_remaining =
119 get_pts_l2cap_ecoc_connect_remaining,
120 .get_pts_l2cap_ecoc_send_num_of_sdu = get_pts_l2cap_ecoc_send_num_of_sdu,
121 .get_pts_l2cap_ecoc_reconfigure = get_pts_l2cap_ecoc_reconfigure,
122 .get_pts_broadcast_audio_config_options =
123 get_pts_broadcast_audio_config_options,
124 .get_pts_le_audio_disable_ases_before_stopping =
125 get_pts_le_audio_disable_ases_before_stopping,
126 .get_all = get_all,
127 };
stack_config_get_interface(void)128 const stack_config_t* stack_config_get_interface(void) {
129 return &mock_stack_config;
130 }
131
132 namespace {
133
134 using testing::_;
135 using testing::DoAll;
136 using testing::NotNull;
137 using testing::Pointee;
138 using testing::Return;
139 using testing::SaveArg;
140 using testing::SaveArgPointee;
141 using testing::StrEq;
142 using testing::StrictMock;
143 using testing::Test;
144
Hex16(int n)145 std::string Hex16(int n) {
146 std::ostringstream oss;
147 oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << n;
148 return oss.str();
149 }
150
151 class StackBtmTest : public Test {
152 public:
153 protected:
SetUp()154 void SetUp() override { mock_function_count_map.clear(); }
TearDown()155 void TearDown() override {}
156 };
157
158 class StackBtmWithInitFreeTest : public StackBtmTest {
159 public:
160 protected:
SetUp()161 void SetUp() override { btm_cb.Init(BTM_SEC_MODE_SC); }
TearDown()162 void TearDown() override { btm_cb.Free(); }
163 };
164
TEST_F(StackBtmTest,GlobalLifecycle)165 TEST_F(StackBtmTest, GlobalLifecycle) {
166 get_btm_client_interface().lifecycle.btm_init();
167 get_btm_client_interface().lifecycle.btm_free();
168 }
169
TEST_F(StackBtmTest,DynamicLifecycle)170 TEST_F(StackBtmTest, DynamicLifecycle) {
171 auto* btm = new tBTM_CB();
172 delete btm;
173 }
174
TEST_F(StackBtmTest,InitFree)175 TEST_F(StackBtmTest, InitFree) {
176 btm_cb.Init(0x1);
177 btm_cb.Free();
178 }
179
TEST_F(StackBtmTest,tSCO_CB)180 TEST_F(StackBtmTest, tSCO_CB) {
181 bluetooth::common::InitFlags::SetAllForTesting();
182 tSCO_CB* p_sco = &btm_cb.sco_cb;
183 p_sco->Init();
184 p_sco->Free();
185 }
186
TEST_F(StackBtmTest,InformClientOnConnectionSuccess)187 TEST_F(StackBtmTest, InformClientOnConnectionSuccess) {
188 get_btm_client_interface().lifecycle.btm_init();
189
190 RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
191
192 btm_acl_connected(bda, 2, HCI_SUCCESS, false);
193 ASSERT_EQ(static_cast<size_t>(1),
194 mock_function_count_map.count("BTA_dm_acl_up"));
195
196 get_btm_client_interface().lifecycle.btm_free();
197 }
198
TEST_F(StackBtmTest,NoInformClientOnConnectionFail)199 TEST_F(StackBtmTest, NoInformClientOnConnectionFail) {
200 get_btm_client_interface().lifecycle.btm_init();
201
202 RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
203
204 btm_acl_connected(bda, 2, HCI_ERR_NO_CONNECTION, false);
205 ASSERT_EQ(static_cast<size_t>(0),
206 mock_function_count_map.count("BTA_dm_acl_up"));
207
208 get_btm_client_interface().lifecycle.btm_free();
209 }
210
TEST_F(StackBtmTest,default_packet_type)211 TEST_F(StackBtmTest, default_packet_type) {
212 get_btm_client_interface().lifecycle.btm_init();
213
214 btm_cb.acl_cb_.SetDefaultPacketTypeMask(0x4321);
215 ASSERT_EQ(0x4321, btm_cb.acl_cb_.DefaultPacketTypes());
216
217 get_btm_client_interface().lifecycle.btm_free();
218 }
219
TEST_F(StackBtmTest,change_packet_type)220 TEST_F(StackBtmTest, change_packet_type) {
221 int cnt = 0;
222 get_btm_client_interface().lifecycle.btm_init();
223
224 btm_cb.acl_cb_.SetDefaultPacketTypeMask(0xffff);
225 ASSERT_EQ(0xffff, btm_cb.acl_cb_.DefaultPacketTypes());
226
227 // Create connection
228 RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
229 btm_acl_created(bda, 0x123, HCI_ROLE_CENTRAL, BT_TRANSPORT_BR_EDR);
230
231 uint64_t features = 0xffffffffffffffff;
232 acl_process_supported_features(0x123, features);
233
234 uint16_t handle{0};
235 uint16_t packet_types{0};
236
237 mock::btsnd_hcic_change_conn_type.body = [&handle, &packet_types](
238 uint16_t h, uint16_t p) {
239 handle = h;
240 packet_types = p;
241 };
242 btm_set_packet_types_from_address(bda, 0x55aa);
243 ASSERT_EQ(++cnt, mock_function_count_map["btsnd_hcic_change_conn_type"]);
244 ASSERT_EQ(0x123, handle);
245 ASSERT_EQ(Hex16(0x4400 | HCI_PKT_TYPES_MASK_DM1), Hex16(packet_types));
246
247 btm_set_packet_types_from_address(bda, 0xffff);
248 ASSERT_EQ(++cnt, mock_function_count_map["btsnd_hcic_change_conn_type"]);
249 ASSERT_EQ(0x123, handle);
250 ASSERT_EQ(Hex16(0xcc00 | HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1),
251 Hex16(packet_types));
252
253 btm_set_packet_types_from_address(bda, 0x0);
254 ASSERT_EQ(0x123, handle);
255 ASSERT_EQ(Hex16(0xcc18), Hex16(packet_types));
256
257 mock::btsnd_hcic_change_conn_type = {};
258 get_btm_client_interface().lifecycle.btm_free();
259 }
260
TEST(ScoTest,make_sco_packet)261 TEST(ScoTest, make_sco_packet) {
262 std::vector<uint8_t> data = {10, 20, 30};
263 uint16_t handle = 0xab;
264 BT_HDR* p = btm_sco_make_packet(data, handle);
265 ASSERT_EQ(p->event, BT_EVT_TO_LM_HCI_SCO);
266 ASSERT_EQ(p->len, 3 + data.size());
267 ASSERT_EQ(p->data[0], 0xab);
268 ASSERT_EQ(p->data[1], 0);
269 ASSERT_EQ(p->data[2], 3);
270 ASSERT_EQ(p->data[3], 10);
271 ASSERT_EQ(p->data[4], 20);
272 ASSERT_EQ(p->data[5], 30);
273 osi_free(p);
274 }
275
TEST(BtmTest,BTM_EIR_MAX_SERVICES)276 TEST(BtmTest, BTM_EIR_MAX_SERVICES) { ASSERT_EQ(46, BTM_EIR_MAX_SERVICES); }
277
278 } // namespace
279
280 void btm_sec_rmt_name_request_complete(const RawAddress* p_bd_addr,
281 const uint8_t* p_bd_name,
282 tHCI_STATUS status);
283
284 struct {
285 RawAddress bd_addr;
286 DEV_CLASS dc;
287 tBTM_BD_NAME bd_name;
288 } btm_test;
289
TEST(SecTest,btm_sec_rmt_name_request_complete)290 TEST(SecTest, btm_sec_rmt_name_request_complete) {
291 bluetooth::common::InitFlags::SetAllForTesting();
292 btm_cb.Init(0);
293
294 ASSERT_TRUE(BTM_SecAddRmtNameNotifyCallback(
295 [](const RawAddress& bd_addr, DEV_CLASS dc, tBTM_BD_NAME bd_name) {
296 btm_test.bd_addr = bd_addr;
297 memcpy(btm_test.dc, dc, DEV_CLASS_LEN);
298 memcpy(btm_test.bd_name, bd_name, BTM_MAX_REM_BD_NAME_LEN);
299 }));
300
301 RawAddress bd_addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
302 const uint8_t* p_bd_name = (const uint8_t*)"MyTestName";
303
304 btm_test = {};
305 btm_sec_rmt_name_request_complete(&bd_addr, p_bd_name, HCI_SUCCESS);
306
307 ASSERT_THAT(btm_test.bd_name, Each(Eq(0)));
308 ASSERT_THAT(btm_test.dc, Each(Eq(0)));
309 ASSERT_EQ(bd_addr, btm_test.bd_addr);
310
311 btm_test = {};
312 ASSERT_TRUE(btm_find_or_alloc_dev(bd_addr) != nullptr);
313 btm_sec_rmt_name_request_complete(&bd_addr, p_bd_name, HCI_SUCCESS);
314
315 ASSERT_STREQ((const char*)p_bd_name, (const char*)btm_test.bd_name);
316 ASSERT_THAT(btm_test.dc, Each(Eq(0)));
317 ASSERT_EQ(bd_addr, btm_test.bd_addr);
318
319 btm_cb.Free();
320 }
321
TEST_F(StackBtmWithInitFreeTest,btm_sec_encrypt_change)322 TEST_F(StackBtmWithInitFreeTest, btm_sec_encrypt_change) {
323 bluetooth::common::InitFlags::SetAllForTesting();
324
325 RawAddress bd_addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
326 const uint16_t classic_handle = 0x1234;
327 const uint16_t ble_handle = 0x9876;
328
329 // Check the collision conditionals
330 btm_cb.collision_start_time = 0UL;
331 btm_sec_encrypt_change(classic_handle, HCI_ERR_LMP_ERR_TRANS_COLLISION, 0x01);
332 uint64_t collision_start_time = btm_cb.collision_start_time;
333 ASSERT_NE(0UL, collision_start_time);
334
335 btm_cb.collision_start_time = 0UL;
336 btm_sec_encrypt_change(classic_handle, HCI_ERR_DIFF_TRANSACTION_COLLISION,
337 0x01);
338 collision_start_time = btm_cb.collision_start_time;
339 ASSERT_NE(0UL, collision_start_time);
340
341 // No device
342 btm_cb.collision_start_time = 0;
343 btm_sec_encrypt_change(classic_handle, HCI_SUCCESS, 0x01);
344 ASSERT_EQ(0UL, btm_cb.collision_start_time);
345
346 // Setup device
347 tBTM_SEC_DEV_REC* device_record = btm_sec_allocate_dev_rec();
348 ASSERT_NE(nullptr, device_record);
349 ASSERT_EQ(BTM_SEC_IN_USE, device_record->sec_flags);
350 device_record->bd_addr = bd_addr;
351 device_record->hci_handle = classic_handle;
352 device_record->ble_hci_handle = ble_handle;
353
354 // With classic device encryption enable
355 btm_sec_encrypt_change(classic_handle, HCI_SUCCESS, 0x01);
356 ASSERT_EQ(BTM_SEC_IN_USE | BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED,
357 device_record->sec_flags);
358
359 // With classic device encryption disable
360 btm_sec_encrypt_change(classic_handle, HCI_SUCCESS, 0x00);
361 ASSERT_EQ(BTM_SEC_IN_USE | BTM_SEC_AUTHENTICATED, device_record->sec_flags);
362 device_record->sec_flags = BTM_SEC_IN_USE;
363
364 // With le device encryption enable
365 btm_sec_encrypt_change(ble_handle, HCI_SUCCESS, 0x01);
366 ASSERT_EQ(BTM_SEC_IN_USE | BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED,
367 device_record->sec_flags);
368
369 // With le device encryption disable
370 btm_sec_encrypt_change(ble_handle, HCI_SUCCESS, 0x00);
371 ASSERT_EQ(BTM_SEC_IN_USE | BTM_SEC_LE_AUTHENTICATED,
372 device_record->sec_flags);
373 device_record->sec_flags = BTM_SEC_IN_USE;
374
375 wipe_secrets_and_remove(device_record);
376 }
377
TEST_F(StackBtmWithInitFreeTest,BTM_SetEncryption)378 TEST_F(StackBtmWithInitFreeTest, BTM_SetEncryption) {
379 const RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
380 const tBT_TRANSPORT transport{BT_TRANSPORT_LE};
381 tBTM_SEC_CALLBACK* p_callback{nullptr};
382 tBTM_BLE_SEC_ACT sec_act{BTM_BLE_SEC_ENCRYPT};
383
384 // No device
385 ASSERT_EQ(BTM_WRONG_MODE, BTM_SetEncryption(bd_addr, transport, p_callback,
386 nullptr, sec_act));
387
388 // With device
389 tBTM_SEC_DEV_REC* device_record = btm_sec_allocate_dev_rec();
390 ASSERT_NE(nullptr, device_record);
391 device_record->bd_addr = bd_addr;
392 device_record->hci_handle = 0x1234;
393
394 ASSERT_EQ(BTM_WRONG_MODE, BTM_SetEncryption(bd_addr, transport, p_callback,
395 nullptr, sec_act));
396
397 wipe_secrets_and_remove(device_record);
398 }
399
TEST_F(StackBtmTest,sco_state_text)400 TEST_F(StackBtmTest, sco_state_text) {
401 std::vector<std::pair<tSCO_STATE, std::string>> states = {
402 std::make_pair(SCO_ST_UNUSED, "SCO_ST_UNUSED"),
403 std::make_pair(SCO_ST_LISTENING, "SCO_ST_LISTENING"),
404 std::make_pair(SCO_ST_W4_CONN_RSP, "SCO_ST_W4_CONN_RSP"),
405 std::make_pair(SCO_ST_CONNECTING, "SCO_ST_CONNECTING"),
406 std::make_pair(SCO_ST_CONNECTED, "SCO_ST_CONNECTED"),
407 std::make_pair(SCO_ST_DISCONNECTING, "SCO_ST_DISCONNECTING"),
408 std::make_pair(SCO_ST_PEND_UNPARK, "SCO_ST_PEND_UNPARK"),
409 std::make_pair(SCO_ST_PEND_ROLECHANGE, "SCO_ST_PEND_ROLECHANGE"),
410 std::make_pair(SCO_ST_PEND_MODECHANGE, "SCO_ST_PEND_MODECHANGE"),
411 };
412 for (const auto& state : states) {
413 ASSERT_STREQ(state.second.c_str(), sco_state_text(state.first).c_str());
414 }
415 std::ostringstream oss;
416 oss << "unknown_sco_state: " << std::numeric_limits<std::uint16_t>::max();
417 ASSERT_STREQ(oss.str().c_str(),
418 sco_state_text(static_cast<tSCO_STATE>(
419 std::numeric_limits<std::uint16_t>::max()))
420 .c_str());
421 }
422
TEST_F(StackBtmTest,btm_ble_sec_req_act_text)423 TEST_F(StackBtmTest, btm_ble_sec_req_act_text) {
424 ASSERT_EQ("BTM_BLE_SEC_REQ_ACT_NONE",
425 btm_ble_sec_req_act_text(BTM_BLE_SEC_REQ_ACT_NONE));
426 ASSERT_EQ("BTM_BLE_SEC_REQ_ACT_ENCRYPT",
427 btm_ble_sec_req_act_text(BTM_BLE_SEC_REQ_ACT_ENCRYPT));
428 ASSERT_EQ("BTM_BLE_SEC_REQ_ACT_PAIR",
429 btm_ble_sec_req_act_text(BTM_BLE_SEC_REQ_ACT_PAIR));
430 ASSERT_EQ("BTM_BLE_SEC_REQ_ACT_DISCARD",
431 btm_ble_sec_req_act_text(BTM_BLE_SEC_REQ_ACT_DISCARD));
432 }
433