diff options
-rw-r--r-- | system/gd/hci/acl_manager_mock.h | 1 | ||||
-rw-r--r-- | system/main/shim/acl_api.cc | 17 | ||||
-rw-r--r-- | system/main/shim/acl_api.h | 3 | ||||
-rw-r--r-- | system/stack/Android.bp | 37 | ||||
-rw-r--r-- | system/stack/connection_manager/connection_manager.cc | 52 | ||||
-rw-r--r-- | system/stack/test/connection_manager_test.cc | 230 | ||||
-rw-r--r-- | system/test/mock/mock_main_shim_acl_api.cc | 9 | ||||
-rw-r--r-- | system/test/mock/mock_main_shim_acl_api.h | 32 | ||||
-rw-r--r-- | system/test/mock/mock_stack_btm_dev.cc | 4 |
9 files changed, 173 insertions, 212 deletions
diff --git a/system/gd/hci/acl_manager_mock.h b/system/gd/hci/acl_manager_mock.h index b02a50c382..330b82bdac 100644 --- a/system/gd/hci/acl_manager_mock.h +++ b/system/gd/hci/acl_manager_mock.h @@ -75,6 +75,7 @@ public: MOCK_METHOD(void, CreateLeConnection, (AddressWithType address_with_type, bool is_direct), (override)); MOCK_METHOD(void, CancelConnect, (Address address), (override)); + MOCK_METHOD(void, CancelLeConnect, (AddressWithType address_with_type), (override)); MOCK_METHOD(void, SetPrivacyPolicyForInitiatorAddress, (LeAddressManager::AddressPolicy address_policy, AddressWithType fixed_address, std::chrono::milliseconds minimum_rotation_time, diff --git a/system/main/shim/acl_api.cc b/system/main/shim/acl_api.cc index 1ad552e069..56e63e1977 100644 --- a/system/main/shim/acl_api.cc +++ b/system/main/shim/acl_api.cc @@ -60,19 +60,6 @@ void bluetooth::shim::ACL_CancelClassicConnection(const RawAddress& raw_address) Stack::GetInstance()->GetAcl()->CancelClassicConnection(address); } -void bluetooth::shim::ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type, - bool is_direct) { - BTM_LogHistory(kBtmLogTag, legacy_address_with_type, "Allow connection from", "Le"); - bluetooth::shim::GetAclManager()->CreateLeConnection( - ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct); -} - -void bluetooth::shim::ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type) { - BTM_LogHistory(kBtmLogTag, legacy_address_with_type, "Ignore connection from", "Le"); - bluetooth::shim::GetAclManager()->CancelLeConnect( - ToAddressWithTypeFromLegacy(legacy_address_with_type)); -} - void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) { std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE, @@ -128,10 +115,6 @@ void bluetooth::shim::ACL_Disconnect(uint16_t handle, bool is_classic, tHCI_STAT void bluetooth::shim::ACL_Shutdown() { Stack::GetInstance()->GetAcl()->Shutdown(); } -void bluetooth::shim::ACL_IgnoreAllLeConnections() { - return Stack::GetInstance()->GetAcl()->ClearFilterAcceptList(); -} - void bluetooth::shim::ACL_ReadConnectionAddress(uint16_t handle, RawAddress& conn_addr, tBLE_ADDR_TYPE* p_addr_type, bool ota_address) { std::promise<bluetooth::hci::AddressWithType> promise; diff --git a/system/main/shim/acl_api.h b/system/main/shim/acl_api.h index a07c42cbb8..6b28988ca6 100644 --- a/system/main/shim/acl_api.h +++ b/system/main/shim/acl_api.h @@ -29,15 +29,12 @@ namespace shim { void ACL_CreateClassicConnection(const RawAddress& raw_address); void ACL_CancelClassicConnection(const RawAddress& raw_address); -void ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct); -void ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type); void ACL_Disconnect(uint16_t handle, bool is_classic, tHCI_STATUS reason, std::string comment); void ACL_WriteData(uint16_t handle, BT_HDR* p_buf); void ACL_Flush(uint16_t handle); void ACL_ConfigureLePrivacy(bool is_le_privacy_enabled); void ACL_Shutdown(); -void ACL_IgnoreAllLeConnections(); void ACL_ReadConnectionAddress(uint16_t handle, RawAddress& conn_addr, tBLE_ADDR_TYPE* p_addr_type, bool ota_address); diff --git a/system/stack/Android.bp b/system/stack/Android.bp index b7ae03bb4b..939e90f3bf 100644 --- a/system/stack/Android.bp +++ b/system/stack/Android.bp @@ -1055,28 +1055,61 @@ cc_test { ], srcs: [ ":TestCommonMainHandler", - ":TestMockStackBtmInterface", + ":TestCommonMockFunctions", + ":TestMockMainShim", + ":TestMockMainShimEntry", + ":TestMockStackBtm", + ":TestMockStackMetrics", "connection_manager/connection_manager.cc", - "test/common/mock_btm_api_layer.cc", "test/connection_manager_test.cc", ], shared_libs: [ + "libaconfig_storage_read_api_cc", + "libbinder", + "libbinder_ndk", + "libcrypto", "libcutils", + "server_configurable_flags", ], static_libs: [ + "bluetooth_flags_c_lib_for_test", "libbase", "libbluetooth-types", + "libbluetooth_core_rs_bridge", + "libbluetooth_crypto_toolbox", "libbluetooth_gd", "libbluetooth_log", "libbt-common", + "libbt-platform-protos-lite", "libchrome", + "libcom.android.sysprop.bluetooth.wrapped", "libevent", "libgmock", "liblog", + "libosi", + "libstatslog_bt", ], sanitize: { cfi: false, }, + target: { + android: { + shared_libs: [ + "libcutils", + "libhidlbase", + "libstatssocket", + "libutils", + ], + static_libs: [ + "android.hardware.bluetooth@1.0", + "android.hardware.bluetooth@1.1", + "android.system.suspend-V1-ndk", + "android.system.suspend.control-V1-ndk", + "libperfetto_client_experimental", + "libstatslog_bt", + ], + }, + }, header_libs: ["libbluetooth_headers"], } diff --git a/system/stack/connection_manager/connection_manager.cc b/system/stack/connection_manager/connection_manager.cc index 801088ee18..1d76c65e20 100644 --- a/system/stack/connection_manager/connection_manager.cc +++ b/system/stack/connection_manager/connection_manager.cc @@ -27,9 +27,11 @@ #include <memory> #include <set> +#include "gd/hci/acl_manager.h" #include "gd/hci/controller_interface.h" #include "main/shim/acl_api.h" #include "main/shim/entry.h" +#include "main/shim/helpers.h" #include "main/shim/le_scanning_manager.h" #include "osi/include/alarm.h" #include "stack/btm/btm_dev.h" @@ -45,7 +47,8 @@ using namespace bluetooth; -constexpr char kBtmLogTag[] = "TA"; +constexpr char kBtmLogTagACL[] = "ACL"; +constexpr char kBtmLogTagTA[] = "TA"; struct closure_data { base::OnceClosure user_task; @@ -73,6 +76,21 @@ static void alarm_set_closure(const base::Location& posted_from, alarm_t* alarm, using unique_alarm_ptr = std::unique_ptr<alarm_t, decltype(&alarm_free)>; +namespace { +static void ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type, + bool is_direct) { + BTM_LogHistory(kBtmLogTagACL, legacy_address_with_type, "Allow connection from", "Le"); + bluetooth::shim::GetAclManager()->CreateLeConnection( + bluetooth::ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct); +} + +static void ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR& legacy_address_with_type) { + BTM_LogHistory(kBtmLogTagACL, legacy_address_with_type, "Ignore connection from", "Le"); + bluetooth::shim::GetAclManager()->CancelLeConnect( + bluetooth::ToAddressWithTypeFromLegacy(legacy_address_with_type)); +} +} // namespace + namespace connection_manager { struct tAPPS_CONNECTING { @@ -205,7 +223,7 @@ static void target_announcement_observe_results_cb(tBTM_INQ_RESULTS* p_inq, cons return; } - BTM_LogHistory(kBtmLogTag, addr, "Found TA from"); + BTM_LogHistory(kBtmLogTagTA, addr, "Found TA from"); /* Take fist app_id and use it for direct_connect */ auto app_id = *(it->second.doing_targeted_announcements_conn.begin()); @@ -216,7 +234,7 @@ static void target_announcement_observe_results_cb(tBTM_INQ_RESULTS* p_inq, cons static void target_announcements_filtering_set(bool enable) { log::debug("enable {}", enable); - BTM_LogHistory(kBtmLogTag, RawAddress::kEmpty, (enable ? "Start filtering" : "Stop filtering")); + BTM_LogHistory(kBtmLogTagTA, RawAddress::kEmpty, (enable ? "Start filtering" : "Stop filtering")); /* Safe to call as if there is no support for filtering, this call will be * ignored. */ @@ -258,13 +276,13 @@ bool background_connect_targeted_announcement_add(tAPP_ID app_id, const RawAddre } if (disable_accept_list) { - bluetooth::shim::ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); + ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); bgconn_dev[address].is_in_accept_list = false; } bgconn_dev[address].doing_targeted_announcements_conn.insert(app_id); if (bgconn_dev[address].doing_targeted_announcements_conn.size() == 1) { - BTM_LogHistory(kBtmLogTag, address, "Allow connection from"); + BTM_LogHistory(kBtmLogTagTA, address, "Allow connection from"); } if (num_of_targeted_announcements_users() == 1) { @@ -310,7 +328,8 @@ bool background_connect_add(uint8_t app_id, const RawAddress& address) { return false; } - bluetooth::shim::ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), false); + ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), false); + bgconn_dev[address].is_in_accept_list = true; } } @@ -330,7 +349,7 @@ bool remove_unconditional(const RawAddress& address) { log::info("address {} is not found", address); } - bluetooth::shim::ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); + ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); return count > 0; } @@ -358,7 +377,7 @@ bool background_connect_remove(uint8_t app_id, const RawAddress& address) { } if (removed_from_ta && it->second.doing_targeted_announcements_conn.size() == 0) { - BTM_LogHistory(kBtmLogTag, address, "Ignore connection from"); + BTM_LogHistory(kBtmLogTagTA, address, "Ignore connection from"); } if (is_anyone_connecting(it)) { @@ -371,7 +390,7 @@ bool background_connect_remove(uint8_t app_id, const RawAddress& address) { /* Keep using filtering */ log::debug("Keep using target announcement filtering"); } else if (!it->second.doing_bg_conn.empty()) { - bluetooth::shim::ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), false); + ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), false); bgconn_dev[address].is_in_accept_list = true; } } @@ -382,7 +401,7 @@ bool background_connect_remove(uint8_t app_id, const RawAddress& address) { // no more apps interested - remove from accept list and delete record if (accept_list_enabled) { - bluetooth::shim::ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); + ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); return true; } @@ -418,7 +437,7 @@ void on_app_deregistered(uint8_t app_id) { continue; } - bluetooth::shim::ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(it->first)); + ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(it->first)); it = bgconn_dev.erase(it); } } @@ -450,7 +469,7 @@ void reset(bool after_reset) { bgconn_dev.clear(); if (!after_reset) { target_announcements_filtering_set(false); - bluetooth::shim::ACL_IgnoreAllLeConnections(); + bluetooth::shim::GetAclManager()->ClearFilterAcceptList(); } } @@ -530,12 +549,12 @@ bool direct_connect_add(uint8_t app_id, const RawAddress& address, tBLE_ADDR_TYP return false; } - bluetooth::shim::ACL_AcceptLeConnectionFrom(address_with_type, true /* is_direct */); + ACL_AcceptLeConnectionFrom(address_with_type, true /* is_direct */); bgconn_dev[address].is_in_accept_list = true; } else { // if already in accept list, we should just bump parameters up for direct // connection. There is no API for that yet, so use API that's adding to accept list. - bluetooth::shim::ACL_AcceptLeConnectionFrom(address_with_type, true /* is_direct */); + ACL_AcceptLeConnectionFrom(address_with_type, true /* is_direct */); } // Setup a timer @@ -576,14 +595,13 @@ bool direct_connect_remove(uint8_t app_id, const RawAddress& address, bool conne /* In such case we need to add device back to allow list because, when connection timeout * out, the lower layer removes device from the allow list. */ - bluetooth::shim::ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), - false /* is_direct */); + ACL_AcceptLeConnectionFrom(BTM_Sec_GetAddressWithType(address), false /* is_direct */); } return true; } // no more apps interested - remove from acceptlist - bluetooth::shim::ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); + ACL_IgnoreLeConnectionFrom(BTM_Sec_GetAddressWithType(address)); if (!is_targeted_announcement_enabled) { bgconn_dev.erase(it); diff --git a/system/stack/test/connection_manager_test.cc b/system/stack/test/connection_manager_test.cc index c5cbcc059b..1a06158ca0 100644 --- a/system/stack/test/connection_manager_test.cc +++ b/system/stack/test/connection_manager_test.cc @@ -4,12 +4,14 @@ #include <base/functional/bind.h> #include <base/functional/callback.h> #include <base/location.h> +#include <bluetooth/log.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <memory> -#include "gd/hci/controller_interface_mock.h" +#include "gd/hci/acl_manager_mock.h" +#include "gd/hci/controller_mock.h" #include "main/shim/acl_api.h" #include "main/shim/entry.h" #include "main/shim/le_scanning_manager.h" @@ -22,35 +24,27 @@ #include "stack/include/btm_ble_api.h" #include "stack/include/btm_log_history.h" #include "stack/l2cap/internal/l2c_api.h" +#include "test/mock/mock_main_shim_entry.h" using testing::_; using testing::DoAll; +using testing::Eq; using testing::Mock; using testing::Return; using testing::SaveArg; -using connection_manager::tAPP_ID; +using bluetooth::hci::AddressWithType; -namespace { -// convenience mock, for verifying acceptlist operations on lower layer are -// actually scheduled -class AcceptlistMock { -public: - MOCK_METHOD2(AcceptlistAdd, bool(const RawAddress&, bool is_direct)); - MOCK_METHOD1(AcceptlistRemove, void(const RawAddress&)); - MOCK_METHOD0(AcceptlistClear, void()); - MOCK_METHOD2(OnConnectionTimedOut, void(uint8_t, const RawAddress&)); - - /* Not really accept list related, btui still BTM - just for testing put it - * here. */ - MOCK_METHOD2(EnableTargetedAnnouncements, void(bool, tBTM_INQ_RESULTS_CB*)); -}; +using connection_manager::tAPP_ID; +namespace test = bluetooth::hci::testing; -std::unique_ptr<AcceptlistMock> localAcceptlistMock; -} // namespace +const RawAddress address1{{0x01, 0x01, 0x01, 0x01, 0x01, 0x07}}; +const RawAddress address2{{0x22, 0x22, 0x02, 0x22, 0x33, 0x22}}; -RawAddress address1{{0x01, 0x01, 0x01, 0x01, 0x01, 0x01}}; -RawAddress address2{{0x22, 0x22, 0x02, 0x22, 0x33, 0x22}}; +const AddressWithType address1_hci{{0x07, 0x01, 0x01, 0x01, 0x01, 0x01}, + bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS}; +const AddressWithType address2_hci{{0x22, 0x33, 0x22, 0x02, 0x22, 0x22}, + bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS}; constexpr tAPP_ID CLIENT1 = 1; constexpr tAPP_ID CLIENT2 = 2; @@ -59,54 +53,28 @@ constexpr tAPP_ID CLIENT10 = 10; std::string get_client_name(uint8_t /* gatt_if */) { return ""; } -const tBLE_BD_ADDR BTM_Sec_GetAddressWithType(const RawAddress& bd_addr) { - return tBLE_BD_ADDR{.type = BLE_ADDR_PUBLIC, .bda = bd_addr}; -} - -tBTM_SEC_DEV_REC* btm_find_dev(const RawAddress& /* bd_addr */) { return nullptr; } - -namespace bluetooth { -namespace shim { - -void ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR& address, bool is_direct) { - localAcceptlistMock->AcceptlistAdd(address.bda, is_direct); -} -void ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR& address) { - return localAcceptlistMock->AcceptlistRemove(address.bda); -} - -void ACL_IgnoreAllLeConnections() { return localAcceptlistMock->AcceptlistClear(); } - -testing::NiceMock<bluetooth::hci::testing::MockControllerInterface> controller; - -hci::ControllerInterface* GetController() { - ON_CALL(controller, GetLeFilterAcceptListSize).WillByDefault(Return(128)); - return &controller; -} +class MockConnTimeout { +public: + MOCK_METHOD2(OnConnectionTimedOut, void(uint8_t, const RawAddress&)); +}; -} // namespace shim -} // namespace bluetooth +std::unique_ptr<MockConnTimeout> localConnTimeoutMock; -void BTM_BleTargetAnnouncementObserve(bool enable, tBTM_INQ_RESULTS_CB* p_results_cb) { - localAcceptlistMock->EnableTargetedAnnouncements(enable, p_results_cb); +namespace connection_manager { +void on_connection_timed_out(uint8_t app_id, const RawAddress& address) { + localConnTimeoutMock->OnConnectionTimedOut(app_id, address); } - -void BTM_LogHistory(const std::string& /*tag*/, const RawAddress& /*bd_addr*/, - const std::string& /*msg*/) {} - -namespace bluetooth { -namespace shim { -void set_target_announcements_filter(bool /*enable*/) {} -} // namespace shim -} // namespace bluetooth - -bool L2CA_ConnectFixedChnl(uint16_t /*fixed_cid*/, const RawAddress& /*bd_addr*/) { return false; } -uint16_t BTM_GetHCIConnHandle(RawAddress const&, tBT_TRANSPORT) { return 0xFFFF; } +} // namespace connection_manager namespace connection_manager { class BleConnectionManager : public testing::Test { void SetUp() override { - localAcceptlistMock = std::make_unique<AcceptlistMock>(); + localConnTimeoutMock = std::make_unique<MockConnTimeout>(); + /* extern */ test::mock_acl_manager_ = new bluetooth::hci::testing::MockAclManager(); + /* extern */ test::mock_controller_ = + new testing::NiceMock<bluetooth::hci::testing::MockControllerInterface>(); + ON_CALL(*test::mock_controller_, GetLeFilterAcceptListSize()).WillByDefault(Return(16)); + auto alarm_mock = AlarmMock::Get(); ON_CALL(*alarm_mock, AlarmNew(_)).WillByDefault(testing::Invoke([](const char* /*name*/) { // We must return something from alarm_new in tests, if we just return @@ -124,44 +92,43 @@ class BleConnectionManager : public testing::Test { void TearDown() override { connection_manager::reset(true); AlarmMock::Reset(); - localAcceptlistMock.reset(); + delete test::mock_controller_; + delete test::mock_acl_manager_; + localConnTimeoutMock.reset(); } }; -void on_connection_timed_out(uint8_t app_id, const RawAddress& address) { - localAcceptlistMock->OnConnectionTimedOut(app_id, address); -} - /** Verify that app can add a device to acceptlist, it is returned as interested * app, and then can remove the device later. */ TEST_F(BleConnectionManager, test_background_connection_add_remove) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).WillOnce(Return(true)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); std::set<tAPP_ID> apps = get_apps_connecting_to(address1); EXPECT_EQ(apps.size(), 1UL); EXPECT_EQ(apps.count(CLIENT1), 1UL); - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(_, _)).Times(0); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(_, _)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(address1_hci)).Times(1); EXPECT_TRUE(background_connect_remove(CLIENT1, address1)); EXPECT_EQ(get_apps_connecting_to(address1).size(), 0UL); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify that multiple clients adding same device multiple times, result in * device being added to whtie list only once, also, that device is removed only * after last client removes it. */ TEST_F(BleConnectionManager, test_background_connection_multiple_clients) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).WillOnce(Return(true)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); + EXPECT_TRUE(background_connect_add(CLIENT1, address1)); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); EXPECT_TRUE(background_connect_add(CLIENT2, address1)); @@ -169,9 +136,9 @@ TEST_F(BleConnectionManager, test_background_connection_multiple_clients) { EXPECT_EQ(get_apps_connecting_to(address1).size(), 3UL); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(_, _)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(_, _)).Times(0); // removing from nonexisting client, should fail EXPECT_FALSE(background_connect_remove(CLIENT10, address1)); @@ -181,20 +148,21 @@ TEST_F(BleConnectionManager, test_background_connection_multiple_clients) { EXPECT_FALSE(background_connect_remove(CLIENT1, address1)); EXPECT_TRUE(background_connect_remove(CLIENT2, address1)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(address1_hci)).Times(1); EXPECT_TRUE(background_connect_remove(CLIENT3, address1)); EXPECT_EQ(get_apps_connecting_to(address1).size(), 0UL); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify adding/removing device to direct connection. */ TEST_F(BleConnectionManager, test_direct_connection_client) { // Direct connect attempt: use faster scan parameters, add to acceptlist, // start 30 timeout - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, true)).WillOnce(Return(true)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, true)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1); EXPECT_TRUE(direct_connect_add(CLIENT1, address1)); @@ -205,9 +173,9 @@ TEST_F(BleConnectionManager, test_direct_connection_client) { // Client that don't do direct connection should fail attempt to stop it EXPECT_FALSE(direct_connect_remove(CLIENT2, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1); // Removal should lower the connection parameters, and free the alarm. @@ -215,13 +183,13 @@ TEST_F(BleConnectionManager, test_direct_connection_client) { // acceptlist is in use, i.e. next connection attempt EXPECT_TRUE(direct_connect_remove(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify direct connection timeout does remove device from acceptlist, and * lower the connection scan parameters */ TEST_F(BleConnectionManager, test_direct_connect_timeout) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, true)).WillOnce(Return(true)); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, true)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1); alarm_callback_t alarm_callback = nullptr; void* alarm_data = nullptr; @@ -233,34 +201,36 @@ TEST_F(BleConnectionManager, test_direct_connect_timeout) { // Start direct connect attempt... EXPECT_TRUE(direct_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(1); - EXPECT_CALL(*localAcceptlistMock, OnConnectionTimedOut(CLIENT1, address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(1); + EXPECT_CALL(*localConnTimeoutMock, OnConnectionTimedOut(CLIENT1, address1)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1); // simulate timeout seconds passed, alarm executing alarm_callback(alarm_data); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify that we properly handle successfull direct connection */ TEST_F(BleConnectionManager, test_direct_connection_success) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, true)).WillOnce(Return(true)); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, true)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1); // Start direct connect attempt... EXPECT_TRUE(direct_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(address1_hci)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1); // simulate event from lower layers - connections was established // successfully. on_connection_complete(address1); + + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify that we properly handle application unregistration */ @@ -272,35 +242,38 @@ TEST_F(BleConnectionManager, test_app_unregister) { * - unregistration of Client2 should trigger address2 removal */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, true)).WillOnce(Return(true)); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, true)).Times(1); EXPECT_TRUE(direct_connect_add(CLIENT1, address1)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address2, false)).WillOnce(Return(true)); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); + + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address2_hci, false)).Times(1); EXPECT_TRUE(background_connect_add(CLIENT1, address2)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address2, true)).WillOnce(Return(true)); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); + + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address2_hci, true)).Times(1); EXPECT_TRUE(direct_connect_add(CLIENT2, address2)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(address1_hci)).Times(1); on_app_deregistered(CLIENT1); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(address2)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(address2_hci)).Times(1); on_app_deregistered(CLIENT2); - - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } /** Verify adding device to both direct connection and background connection. */ TEST_F(BleConnectionManager, test_direct_and_background_connect) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, true)).WillOnce(Return(true)); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, true)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_CALL(*AlarmMock::Get(), AlarmNew(_)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)).Times(1); // add device as both direct and background connection EXPECT_TRUE(direct_connect_add(CLIENT1, address1)); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1); // not removing from acceptlist yet, as the background connection is still @@ -308,65 +281,65 @@ TEST_F(BleConnectionManager, test_direct_and_background_connect) { EXPECT_TRUE(direct_connect_remove(CLIENT1, address1)); // remove from acceptlist, because no more interest in device. - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(1); EXPECT_TRUE(background_connect_remove(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } TEST_F(BleConnectionManager, test_target_announement_connect) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_TRUE(background_connect_targeted_announcement_add(CLIENT1, address1)); EXPECT_TRUE(background_connect_targeted_announcement_add(CLIENT1, address1)); } TEST_F(BleConnectionManager, test_add_targeted_announement_when_allow_list_used) { /* Accept adding to allow list */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).WillOnce(Return(true)); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); /* This shall be called when registering announcements */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(1); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); EXPECT_TRUE(background_connect_targeted_announcement_add(CLIENT2, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } TEST_F(BleConnectionManager, test_add_background_connect_when_targeted_announcement_are_enabled) { /* Accept adding to allow list */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(0); /* This shall be called when registering announcements */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_TRUE(background_connect_targeted_announcement_add(CLIENT2, address1)); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } TEST_F(BleConnectionManager, test_re_add_background_connect_to_allow_list) { - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).Times(0); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_TRUE(background_connect_targeted_announcement_add(CLIENT2, address1)); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); /* Now remove app using targeted announcement and expect device * to be added to white list */ /* Accept adding to allow list */ - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).WillOnce(Return(true)); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); EXPECT_TRUE(background_connect_remove(CLIENT2, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(1); EXPECT_TRUE(background_connect_remove(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } TEST_F(BleConnectionManager, test_re_add_to_allow_list_after_timeout_with_multiple_clients) { @@ -374,15 +347,12 @@ TEST_F(BleConnectionManager, test_re_add_to_allow_list_after_timeout_with_multip alarm_callback_t alarm_callback = nullptr; void* alarm_data = nullptr; - /* Accept adding to allow list */ - ON_CALL(*localAcceptlistMock, AcceptlistAdd(address1, _)).WillByDefault(Return(true)); - - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).Times(1); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); EXPECT_TRUE(background_connect_add(CLIENT1, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); EXPECT_CALL(*AlarmMock::Get(), AlarmSetOnMloop(_, _, _, _)) .Times(1) @@ -390,16 +360,16 @@ TEST_F(BleConnectionManager, test_re_add_to_allow_list_after_timeout_with_multip // Start direct connect attempt... EXPECT_TRUE(direct_connect_add(CLIENT2, address1)); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); // simulate timeout seconds passed, alarm executing - EXPECT_CALL(*localAcceptlistMock, OnConnectionTimedOut(CLIENT2, address1)).Times(1); - EXPECT_CALL(*localAcceptlistMock, AcceptlistRemove(_)).Times(0); - EXPECT_CALL(*localAcceptlistMock, AcceptlistAdd(address1, false)).Times(1); + EXPECT_CALL(*localConnTimeoutMock, OnConnectionTimedOut(CLIENT2, address1)).Times(1); + EXPECT_CALL(*test::mock_acl_manager_, CancelLeConnect(_)).Times(0); + EXPECT_CALL(*test::mock_acl_manager_, CreateLeConnection(address1_hci, false)).Times(1); EXPECT_CALL(*AlarmMock::Get(), AlarmFree(_)).Times(1); alarm_callback(alarm_data); - Mock::VerifyAndClearExpectations(localAcceptlistMock.get()); + Mock::VerifyAndClearExpectations(test::mock_acl_manager_); } } // namespace connection_manager diff --git a/system/test/mock/mock_main_shim_acl_api.cc b/system/test/mock/mock_main_shim_acl_api.cc index f8d1f18fe2..3d257f1a5a 100644 --- a/system/test/mock/mock_main_shim_acl_api.cc +++ b/system/test/mock/mock_main_shim_acl_api.cc @@ -35,14 +35,6 @@ void bluetooth::shim::ACL_CreateClassicConnection(const RawAddress& /* raw_addre void bluetooth::shim::ACL_CancelClassicConnection(const RawAddress& /* raw_address */) { inc_func_call_count(__func__); } -void bluetooth::shim::ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR& /* legacy_address_with_type */, - bool /* is_direct */) { - inc_func_call_count(__func__); -} -void bluetooth::shim::ACL_IgnoreLeConnectionFrom( - const tBLE_BD_ADDR& /* legacy_address_with_type */) { - inc_func_call_count(__func__); -} void bluetooth::shim::ACL_ConfigureLePrivacy(bool /* is_le_privacy_enabled */) { inc_func_call_count(__func__); } @@ -54,7 +46,6 @@ void bluetooth::shim::ACL_Disconnect(uint16_t /* handle */, bool /* is_classic * tHCI_STATUS /* reason */, std::string /* comment */) { inc_func_call_count(__func__); } -void bluetooth::shim::ACL_IgnoreAllLeConnections() { inc_func_call_count(__func__); } void bluetooth::shim::ACL_ReadConnectionAddress(uint16_t /* handle */, RawAddress& /* conn_addr */, tBLE_ADDR_TYPE* /* p_addr_type */, bool /* ota_address */) { diff --git a/system/test/mock/mock_main_shim_acl_api.h b/system/test/mock/mock_main_shim_acl_api.h index 782bf4ae55..ab891673e3 100644 --- a/system/test/mock/mock_main_shim_acl_api.h +++ b/system/test/mock/mock_main_shim_acl_api.h @@ -42,19 +42,6 @@ namespace test { namespace mock { namespace main_shim_acl_api { -// Shared state between mocked functions and tests -// Name: ACL_AcceptLeConnectionFrom -// Params: const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct -// Return: bool -struct ACL_AcceptLeConnectionFrom { - std::function<void(const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct)> body{ - [](const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) {}}; - void operator()(const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) { - body(legacy_address_with_type, is_direct); - } -}; -extern struct ACL_AcceptLeConnectionFrom ACL_AcceptLeConnectionFrom; - // Name: ACL_AddToAddressResolution // Params: const tBLE_BD_ADDR& legacy_address_with_type, const Octet16& // peer_irk, const Octet16& local_irk Return: void @@ -157,25 +144,6 @@ struct ACL_Disconnect { }; extern struct ACL_Disconnect ACL_Disconnect; -// Name: ACL_IgnoreAllLeConnections -// Params: -// Return: void -struct ACL_IgnoreAllLeConnections { - std::function<void()> body{[]() {}}; - void operator()() { body(); } -}; -extern struct ACL_IgnoreAllLeConnections ACL_IgnoreAllLeConnections; - -// Name: ACL_IgnoreLeConnectionFrom -// Params: const tBLE_BD_ADDR& legacy_address_with_type -// Return: void -struct ACL_IgnoreLeConnectionFrom { - std::function<void(const tBLE_BD_ADDR& legacy_address_with_type)> body{ - [](const tBLE_BD_ADDR& legacy_address_with_type) {}}; - void operator()(const tBLE_BD_ADDR& legacy_address_with_type) { body(legacy_address_with_type); } -}; -extern struct ACL_IgnoreLeConnectionFrom ACL_IgnoreLeConnectionFrom; - // Name: ACL_ReadConnectionAddress // Params: uint16_t handle, RawAddress& conn_addr, tBLE_ADDR_TYPE*, bool // p_addr_type Return: void diff --git a/system/test/mock/mock_stack_btm_dev.cc b/system/test/mock/mock_stack_btm_dev.cc index 1dc2da8646..60f88b0d6e 100644 --- a/system/test/mock/mock_stack_btm_dev.cc +++ b/system/test/mock/mock_stack_btm_dev.cc @@ -111,9 +111,9 @@ bool maybe_resolve_address(RawAddress* bda, tBLE_ADDR_TYPE* bda_type) { inc_func_call_count(__func__); return test::mock::stack_btm_dev::maybe_resolve_address(bda, bda_type); } -const tBLE_BD_ADDR BTM_Sec_GetAddressWithType(const RawAddress& /* bd_addr */) { +const tBLE_BD_ADDR BTM_Sec_GetAddressWithType(const RawAddress& bd_addr) { inc_func_call_count(__func__); - return {}; + return tBLE_BD_ADDR{.type = BLE_ADDR_PUBLIC, .bda = bd_addr}; } void DumpsysRecord(int /* fd */) { inc_func_call_count(__func__); } |