diff options
| author | 2024-06-20 17:11:26 -0700 | |
|---|---|---|
| committer | 2024-09-10 01:13:41 +0000 | |
| commit | 5a2a1ce5225865bc57bb8ed076617b79c1e9b7df (patch) | |
| tree | e84e4558649874672ee75649af0f74774977046c | |
| parent | 5623e6ccd9a7688b3ed2044db5c6f75b0ccaa206 (diff) | |
[22/25] stack::sdp Use stack::l2cap::get_interface()
Bug: 343808590
Test: m .
Flag: EXEMPT, Mechanical Refactor
Change-Id: I24d81356f5145aad0166c3e45ca4262bc642d54b
| -rw-r--r-- | system/stack/fuzzers/sdp_fuzzer.cc | 2 | ||||
| -rw-r--r-- | system/stack/sdp/sdp_discovery.cc | 9 | ||||
| -rw-r--r-- | system/stack/sdp/sdp_main.cc | 15 | ||||
| -rw-r--r-- | system/stack/sdp/sdp_server.cc | 9 | ||||
| -rw-r--r-- | system/stack/sdp/sdp_utils.cc | 19 | ||||
| -rw-r--r-- | system/stack/sdp/sdpint.h | 2 | ||||
| -rw-r--r-- | system/stack/test/sdp/stack_sdp_api_test.cc | 60 | ||||
| -rw-r--r-- | system/stack/test/sdp/stack_sdp_parse_test.cc | 54 | ||||
| -rw-r--r-- | system/stack/test/sdp/stack_sdp_test.cc | 94 | ||||
| -rw-r--r-- | system/stack/test/sdp/stack_sdp_utils_test.cc | 43 |
10 files changed, 189 insertions, 118 deletions
diff --git a/system/stack/fuzzers/sdp_fuzzer.cc b/system/stack/fuzzers/sdp_fuzzer.cc index c20eefbc44..de994d415e 100644 --- a/system/stack/fuzzers/sdp_fuzzer.cc +++ b/system/stack/fuzzers/sdp_fuzzer.cc @@ -75,7 +75,7 @@ public: [](uint16_t psm, const RawAddress& raw_address) { return kDummyCID; }; test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body = [](uint16_t psm, const RawAddress& p_bd_addr, uint16_t sec_level) { - return L2CA_ConnectReq(psm, p_bd_addr); + return bluetooth::stack::l2cap::get_interface().L2CA_ConnectReq(psm, p_bd_addr); }; test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t cid, BT_HDR* p_data) -> tL2CAP_DW_RESULT { diff --git a/system/stack/sdp/sdp_discovery.cc b/system/stack/sdp/sdp_discovery.cc index 25b65b90b0..73f15d1acd 100644 --- a/system/stack/sdp/sdp_discovery.cc +++ b/system/stack/sdp/sdp_discovery.cc @@ -176,7 +176,8 @@ static void sdp_snd_service_search_req(tCONN_CB* p_ccb, uint8_t cont_len, uint8_ /* Set the length of the SDP data in the buffer */ p_cmd->len = (uint16_t)(p - p_start); - if (L2CA_DataWrite(p_ccb->connection_id, p_cmd) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_cmd) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_cmd->len); } @@ -693,7 +694,8 @@ static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply, /* Set the length of the SDP data in the buffer */ p_msg->len = p - p_start; - if (L2CA_DataWrite(p_ccb->connection_id, p_msg) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_msg) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_msg->len); } @@ -861,7 +863,8 @@ static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply, uint8_t* /* Set the length of the SDP data in the buffer */ p_msg->len = (uint16_t)(p - p_start); - if (L2CA_DataWrite(p_ccb->connection_id, p_msg) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_msg) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_msg->len); } diff --git a/system/stack/sdp/sdp_main.cc b/system/stack/sdp/sdp_main.cc index fd9704ee72..f166bb601e 100644 --- a/system/stack/sdp/sdp_main.cc +++ b/system/stack/sdp/sdp_main.cc @@ -31,7 +31,6 @@ #include "stack/include/bt_hdr.h" #include "stack/include/bt_psm_types.h" #include "stack/include/btm_sec_api_types.h" -#include "stack/include/l2c_api.h" #include "stack/include/l2cdefs.h" #include "stack/include/sdp_status.h" #include "stack/sdp/sdpint.h" @@ -286,7 +285,8 @@ tCONN_CB* sdp_conn_originate(const RawAddress& bd_addr) { /* Transition to the next appropriate state, waiting for connection confirm */ if (cid == 0) { p_ccb->con_state = tSDP_STATE::CONN_SETUP; - cid = L2CA_ConnectReqWithSecurity(BT_PSM_SDP, bd_addr, BTM_SEC_NONE); + cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity(BT_PSM_SDP, bd_addr, + BTM_SEC_NONE); } else { p_ccb->con_state = tSDP_STATE::CONN_PEND; log::warn("SDP already active for peer {}. cid={:#0x}", bd_addr, cid); @@ -323,7 +323,7 @@ void sdp_disconnect(tCONN_CB* p_ccb, tSDP_REASON reason) { sdpu_release_ccb(ccb); return; } else { - if (!L2CA_DisconnectReq(ccb.connection_id)) { + if (!stack::l2cap::get_interface().L2CA_DisconnectReq(ccb.connection_id)) { log::warn("Unable to disconnect L2CAP peer:{} cid:{}", ccb.device_address, ccb.connection_id); } @@ -381,7 +381,7 @@ void sdp_conn_timer_timeout(void* data) { log::verbose("SDP - CCB timeout in state: {} CID: 0x{:x}", sdp_state_text(ccb.con_state), ccb.connection_id); - if (!L2CA_DisconnectReq(ccb.connection_id)) { + if (!stack::l2cap::get_interface().L2CA_DisconnectReq(ccb.connection_id)) { log::warn("Unable to disconnect L2CAP peer:{} cid:{}", ccb.device_address, ccb.connection_id); } @@ -424,14 +424,15 @@ void sdp_init(void) { sdp_cb.reg_info.pL2CA_Error_Cb = sdp_on_l2cap_error; /* Now, register with L2CAP */ - if (!L2CA_RegisterWithSecurity(BT_PSM_SDP, sdp_cb.reg_info, true /* enable_snoop */, nullptr, - SDP_MTU_SIZE, 0, BTM_SEC_NONE)) { + if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(BT_PSM_SDP, sdp_cb.reg_info, + true /* enable_snoop */, nullptr, + SDP_MTU_SIZE, 0, BTM_SEC_NONE)) { log::error("SDP Registration failed"); } } void sdp_free(void) { - L2CA_Deregister(BT_PSM_SDP); + stack::l2cap::get_interface().L2CA_Deregister(BT_PSM_SDP); for (int i = 0; i < SDP_MAX_CONNECTIONS; i++) { alarm_free(sdp_cb.ccb[i].sdp_conn_timer); sdp_cb.ccb[i].sdp_conn_timer = NULL; diff --git a/system/stack/sdp/sdp_server.cc b/system/stack/sdp/sdp_server.cc index 8f95a79424..7061cc5885 100644 --- a/system/stack/sdp/sdp_server.cc +++ b/system/stack/sdp/sdp_server.cc @@ -297,7 +297,8 @@ static void process_service_search(tCONN_CB* p_ccb, uint16_t trans_num, uint16_t p_buf->len = p_rsp - p_rsp_start; /* Send the buffer through L2CAP */ - if (L2CA_DataWrite(p_ccb->connection_id, p_buf) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_buf) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_buf->len); } @@ -560,7 +561,8 @@ static void process_service_attr_req(tCONN_CB* p_ccb, uint16_t trans_num, uint16 p_buf->len = p_rsp - p_rsp_start; /* Send the buffer through L2CAP */ - if (L2CA_DataWrite(p_ccb->connection_id, p_buf) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_buf) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_buf->len); } @@ -916,7 +918,8 @@ static void process_service_search_attr_req(tCONN_CB* p_ccb, uint16_t trans_num, p_buf->len = p_rsp - p_rsp_start; /* Send the buffer through L2CAP */ - if (L2CA_DataWrite(p_ccb->connection_id, p_buf) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_buf) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}", p_ccb->device_address, p_ccb->connection_id, p_buf->len); } diff --git a/system/stack/sdp/sdp_utils.cc b/system/stack/sdp/sdp_utils.cc index 9ab89e9fe9..2fb4f4622b 100644 --- a/system/stack/sdp/sdp_utils.cc +++ b/system/stack/sdp/sdp_utils.cc @@ -48,6 +48,7 @@ #include "stack/include/bt_types.h" #include "stack/include/bt_uuid16.h" #include "stack/include/btm_sec_api_types.h" +#include "stack/include/l2cap_interface.h" #include "stack/include/sdpdefs.h" #include "stack/include/stack_metrics_logging.h" #include "stack/sdp/internal/sdp_api.h" @@ -522,7 +523,8 @@ bool sdpu_process_pend_ccb_new_cid(const tCONN_CB& ccb) { if (!new_conn) { // Only change state of the first ccb p_ccb->con_state = tSDP_STATE::CONN_SETUP; - new_cid = L2CA_ConnectReqWithSecurity(BT_PSM_SDP, p_ccb->device_address, BTM_SEC_NONE); + new_cid = stack::l2cap::get_interface().L2CA_ConnectReqWithSecurity( + BT_PSM_SDP, p_ccb->device_address, BTM_SEC_NONE); new_conn = true; } // Check if L2CAP started the connection process @@ -732,7 +734,8 @@ void sdpu_build_n_send_error(tCONN_CB* p_ccb, uint16_t trans_num, tSDP_STATUS er p_buf->len = p_rsp - p_rsp_start; /* Send the buffer through L2CAP */ - if (L2CA_DataWrite(p_ccb->connection_id, p_buf) != tL2CAP_DW_RESULT::SUCCESS) { + if (stack::l2cap::get_interface().L2CA_DataWrite(p_ccb->connection_id, p_buf) != + tL2CAP_DW_RESULT::SUCCESS) { log::warn("Unable to write L2CAP data cid:{}", p_ccb->connection_id); } } @@ -1536,8 +1539,10 @@ void sdpu_set_avrc_target_version(const tSDP_ATTRIBUTE* p_attr, const RawAddress } if (iop_version != 0) { - log::info("device={} is in IOP database. Reply AVRC Target version {:x} instead of {:x}.", - *bdaddr, iop_version, avrcp_version); + log::info( + "device={} is in IOP database. Reply AVRC Target version {:x} instead " + "of {:x}.", + *bdaddr, iop_version, avrcp_version); uint8_t* p_version = p_attr->value_ptr + 6; UINT16_TO_BE_FIELD(p_version, iop_version); return; @@ -1638,8 +1643,10 @@ void sdpu_set_avrc_target_features(const tSDP_ATTRIBUTE* p_attr, const RawAddres bool browsing_supported = ((AVRCP_FEAT_BRW_BIT & avrcp_peer_features) == AVRCP_FEAT_BRW_BIT); bool coverart_supported = ((AVRCP_FEAT_CA_BIT & avrcp_peer_features) == AVRCP_FEAT_CA_BIT); - log::info("SDP AVRCP DB Version 0x{:x}, browse supported {}, cover art supported {}", - avrcp_peer_features, browsing_supported, coverart_supported); + log::info( + "SDP AVRCP DB Version 0x{:x}, browse supported {}, cover art supported " + "{}", + avrcp_peer_features, browsing_supported, coverart_supported); if (avrcp_version < AVRC_REV_1_4 || !browsing_supported) { log::info("Reset Browsing Feature"); p_attr->value_ptr[AVRCP_SUPPORTED_FEATURES_POSITION] &= ~AVRCP_BROWSE_SUPPORT_BITMASK; diff --git a/system/stack/sdp/sdpint.h b/system/stack/sdp/sdpint.h index 5d298146bd..fcbdb93abd 100644 --- a/system/stack/sdp/sdpint.h +++ b/system/stack/sdp/sdpint.h @@ -34,7 +34,7 @@ #include "internal_include/bt_target.h" #include "osi/include/alarm.h" #include "stack/include/bt_hdr.h" -#include "stack/include/l2c_api.h" +#include "stack/include/l2cap_interface.h" #include "stack/include/sdp_callback.h" #include "stack/sdp/sdp_discovery_db.h" #include "types/bluetooth/uuid.h" diff --git a/system/stack/test/sdp/stack_sdp_api_test.cc b/system/stack/test/sdp/stack_sdp_api_test.cc index a37c9a52f9..f45712ef27 100644 --- a/system/stack/test/sdp/stack_sdp_api_test.cc +++ b/system/stack/test/sdp/stack_sdp_api_test.cc @@ -14,6 +14,7 @@ * limitations under the License. */ +#include <gmock/gmock.h> #include <gtest/gtest.h> #include <stdlib.h> @@ -27,49 +28,43 @@ #include "stack/sdp/sdpint.h" #include "test/fake/fake_osi.h" #include "test/mock/mock_osi_allocator.h" -#include "test/mock/mock_stack_l2cap_api.h" +#include "test/mock/mock_stack_l2cap_interface.h" #ifndef BT_DEFAULT_BUFFER_SIZE #define BT_DEFAULT_BUFFER_SIZE (4096 + 16) #endif +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnArg; +using ::testing::SaveArg; + namespace { constexpr uint8_t kSDP_MAX_CONNECTIONS = static_cast<uint8_t>(SDP_MAX_CONNECTIONS); const RawAddress kRawAddress = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6}); int L2CA_ConnectReqWithSecurity_cid = 0x42; -tSDP_DISCOVERY_DB* sdp_db = nullptr; class StackSdpWithMocksTest : public ::testing::Test { protected: void SetUp() override { fake_osi_ = std::make_unique<::test::fake::FakeOsi>(); + bluetooth::testing::stack::l2cap::set_interface(&mock_stack_l2cap_interface_); - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body = - [](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, uint16_t /* sec_level */) { - return ++L2CA_ConnectReqWithSecurity_cid; - }; - test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t /* cid */, - BT_HDR* p_data) -> tL2CAP_DW_RESULT { - osi_free_and_reset(reinterpret_cast<void**>(&p_data)); - return tL2CAP_DW_RESULT::FAILED; - }; - test::mock::stack_l2cap_api::L2CA_DisconnectReq.body = [](uint16_t /* cid */) { return true; }; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body = - [](uint16_t psm, const tL2CAP_APPL_INFO& /* p_cb_info */, bool /* enable_snoop */, - tL2CAP_ERTM_INFO* /* p_ertm_info */, uint16_t /* my_mtu */, - uint16_t /* required_remote_mtu */, uint16_t /* sec_level */) { return psm; }; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _)) + .WillOnce(DoAll(SaveArg<1>(&l2cap_callbacks_), ::testing::ReturnArg<0>())); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_Deregister(_)); } void TearDown() override { - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_DataWrite = {}; - test::mock::stack_l2cap_api::L2CA_DisconnectReq = {}; - + bluetooth::testing::stack::l2cap::reset_interface(); fake_osi_.reset(); } + tL2CAP_APPL_INFO l2cap_callbacks_{}; + bluetooth::testing::stack::l2cap::Mock mock_stack_l2cap_interface_; std::unique_ptr<test::fake::FakeOsi> fake_osi_; }; @@ -78,11 +73,9 @@ protected: void SetUp() override { StackSdpWithMocksTest::SetUp(); sdp_init(); - sdp_db = static_cast<tSDP_DISCOVERY_DB*>(osi_malloc(BT_DEFAULT_BUFFER_SIZE)); } void TearDown() override { - osi_free(sdp_db); sdp_free(); StackSdpWithMocksTest::TearDown(); } @@ -93,11 +86,16 @@ protected: TEST_F(StackSdpApiTest, nop) {} TEST_F(StackSdpApiTest, SDP_ServiceSearchRequest) { - tSDP_DISCOVERY_DB db; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) { RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i}); ASSERT_NE(nullptr, sdp_conn_originate(bd_addr)); } + tSDP_DISCOVERY_DB db; ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api() ->service.SDP_ServiceSearchRequest( kRawAddress, &db, @@ -105,11 +103,16 @@ TEST_F(StackSdpApiTest, SDP_ServiceSearchRequest) { } TEST_F(StackSdpApiTest, SDP_ServiceSearchAttributeRequest) { - tSDP_DISCOVERY_DB db; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) { RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i}); ASSERT_NE(nullptr, sdp_conn_originate(bd_addr)); } + tSDP_DISCOVERY_DB db; ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api() ->service.SDP_ServiceSearchAttributeRequest( kRawAddress, &db, @@ -117,11 +120,16 @@ TEST_F(StackSdpApiTest, SDP_ServiceSearchAttributeRequest) { } TEST_F(StackSdpApiTest, SDP_ServiceSearchAttributeRequest2) { - tSDP_DISCOVERY_DB db; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) { RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i}); ASSERT_NE(nullptr, sdp_conn_originate(bd_addr)); } + tSDP_DISCOVERY_DB db; ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api() ->service.SDP_ServiceSearchAttributeRequest2( kRawAddress, &db, diff --git a/system/stack/test/sdp/stack_sdp_parse_test.cc b/system/stack/test/sdp/stack_sdp_parse_test.cc index 41ff1653e3..024543b4d0 100644 --- a/system/stack/test/sdp/stack_sdp_parse_test.cc +++ b/system/stack/test/sdp/stack_sdp_parse_test.cc @@ -23,7 +23,6 @@ #include <cstdint> #include "gd/os/rand.h" -#include "os/log.h" #include "osi/include/allocator.h" #include "stack/include/bt_hdr.h" #include "stack/include/bt_uuid16.h" @@ -34,7 +33,7 @@ #include "test/common/mock_functions.h" #include "test/fake/fake_osi.h" #include "test/mock/mock_osi_allocator.h" -#include "test/mock/mock_stack_l2cap_api.h" +#include "test/mock/mock_stack_l2cap_interface.h" constexpr uint32_t kBtDefaultBufferSize = static_cast<uint32_t>(BT_DEFAULT_BUFFER_SIZE); @@ -42,41 +41,32 @@ constexpr uint32_t kBtDefaultBufferSize = static_cast<uint32_t>(BT_DEFAULT_BUFFE using bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api; +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnArg; +using ::testing::SaveArg; + namespace { const RawAddress kRawAddress = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}); constexpr size_t kSdpDbSize = BTA_DM_SDP_DB_SIZE * 16; constexpr size_t kSdpPacketStartOffset = 9; -int L2CA_ConnectReqWithSecurity_cid = 0x42; +int L2CA_ConnectReqWithSecurity_cid = 42; class StackSdpParserWithMocksTest : public ::testing::Test { protected: void SetUp() override { reset_mock_function_count_map(); fake_osi_ = std::make_unique<test::fake::FakeOsi>(); - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body = - [](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, uint16_t /* sec_level */) { - return ++L2CA_ConnectReqWithSecurity_cid; - }; - test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t /* cid */, BT_HDR* p_data) { - osi_free_and_reset((void**)&p_data); - return tL2CAP_DW_RESULT::FAILED; - }; - test::mock::stack_l2cap_api::L2CA_DisconnectReq.body = [](uint16_t /* cid */) { return true; }; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body = - [](uint16_t psm, const tL2CAP_APPL_INFO& /* p_cb_info */, bool /* enable_snoop */, - tL2CAP_ERTM_INFO* /* p_ertm_info */, uint16_t /* my_mtu */, - uint16_t /* required_remote_mtu */, uint16_t /* sec_level */) { return psm; }; + bluetooth::testing::stack::l2cap::set_interface(&mock_stack_l2cap_interface_); } - void TearDown() override { - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_DisconnectReq = {}; - test::mock::stack_l2cap_api::L2CA_DataWrite = {}; - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity = {}; - fake_osi_.reset(); - } + void TearDown() override { fake_osi_.reset(); } + tL2CAP_APPL_INFO l2cap_callbacks_{}; + bluetooth::testing::stack::l2cap::Mock mock_stack_l2cap_interface_; std::unique_ptr<test::fake::FakeOsi> fake_osi_; }; @@ -84,6 +74,10 @@ class StackSdpParserInitTest : public StackSdpParserWithMocksTest { protected: void SetUp() override { StackSdpParserWithMocksTest::SetUp(); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _)) + .WillOnce(DoAll(SaveArg<1>(&l2cap_callbacks_), ::testing::ReturnArg<0>())); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_Deregister(_)); + sdp_init(); p_db_ = (tSDP_DISCOVERY_DB*)osi_malloc(kSdpDbSize); } @@ -101,6 +95,12 @@ protected: } // namespace TEST_F(StackSdpParserInitTest, SDP_InitDiscoveryDb) { + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); + bluetooth::Uuid uuid; const bool success = get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb( p_db_, kBtDefaultBufferSize, 1, &uuid, 0, nullptr); @@ -114,6 +114,12 @@ class StackSdpAsClientParseTest : public StackSdpParserInitTest { protected: void SetUp() override { StackSdpParserInitTest::SetUp(); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); + ASSERT_TRUE(get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb(p_db_, kSdpDbSize, 1, p_uuid_list, 0, nullptr)); ASSERT_TRUE(get_legacy_stack_sdp_api()->service.SDP_ServiceSearchRequest( @@ -165,6 +171,8 @@ TEST_F(StackSdpAsClientParseTest, nop) {} TEST_F_WITH_FLAGS(StackSdpAsClientParseTest, sdp_disc_server_rsp_packets00, REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(TEST_BT, stack_sdp_detect_nil_property_type))) { + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DisconnectReq(_)).Times(1); + parse_sdp_responses(bluetooth::testing::stack::sdp::packets00::rx_pkts, bluetooth::testing::stack::sdp::packets00::kNumRxPkts); diff --git a/system/stack/test/sdp/stack_sdp_test.cc b/system/stack/test/sdp/stack_sdp_test.cc index 5a86aa565f..8c1bd4e253 100644 --- a/system/stack/test/sdp/stack_sdp_test.cc +++ b/system/stack/test/sdp/stack_sdp_test.cc @@ -30,49 +30,50 @@ #include "stack/sdp/sdpint.h" #include "test/fake/fake_osi.h" #include "test/mock/mock_osi_allocator.h" -#include "test/mock/mock_stack_l2cap_api.h" +#include "test/mock/mock_stack_l2cap_interface.h" #ifndef BT_DEFAULT_BUFFER_SIZE #define BT_DEFAULT_BUFFER_SIZE (4096 + 16) #endif +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::NotNull; +using ::testing::Pointee; +using ::testing::Return; +using ::testing::ReturnArg; +using ::testing::SaveArg; +using ::testing::SaveArgPointee; +using ::testing::StrEq; +using ::testing::StrictMock; +using ::testing::Test; + namespace { constexpr uint8_t kSDP_MAX_CONNECTIONS = static_cast<uint8_t>(SDP_MAX_CONNECTIONS); RawAddress addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6}); -int L2CA_ConnectReqWithSecurity_cid = 0x42; +int L2CA_ConnectReqWithSecurity_cid = 42; tSDP_DISCOVERY_DB* sdp_db = nullptr; class StackSdpWithMocksTest : public ::testing::Test { protected: void SetUp() override { fake_osi_ = std::make_unique<test::fake::FakeOsi>(); + bluetooth::testing::stack::l2cap::set_interface(&mock_stack_l2cap_interface_); - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body = - [](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, uint16_t /* sec_level */) { - return ++L2CA_ConnectReqWithSecurity_cid; - }; - test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t /* cid */, - BT_HDR* p_data) -> tL2CAP_DW_RESULT { - osi_free_and_reset((void**)&p_data); - return tL2CAP_DW_RESULT::FAILED; - }; - test::mock::stack_l2cap_api::L2CA_DisconnectReq.body = [](uint16_t /* cid */) { return true; }; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body = - [](uint16_t psm, const tL2CAP_APPL_INFO& /* p_cb_info */, bool /* enable_snoop */, - tL2CAP_ERTM_INFO* /* p_ertm_info */, uint16_t /* my_mtu */, - uint16_t /* required_remote_mtu */, uint16_t /* sec_level */) { return psm; }; + tL2CAP_APPL_INFO l2cap_callbacks{}; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _)) + .WillOnce(DoAll(SaveArg<1>(&l2cap_callbacks), ::testing::ReturnArg<0>())); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_Deregister(_)); } void TearDown() override { - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_DataWrite = {}; - test::mock::stack_l2cap_api::L2CA_DisconnectReq = {}; - + bluetooth::testing::stack::l2cap::reset_interface(); fake_osi_.reset(); } + bluetooth::testing::stack::l2cap::Mock mock_stack_l2cap_interface_; std::unique_ptr<test::fake::FakeOsi> fake_osi_; }; @@ -96,6 +97,19 @@ protected: TEST_F(StackSdpInitTest, nop) {} TEST_F(StackSdpInitTest, sdp_service_search_request) { + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillOnce(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DisconnectReq(_)).WillOnce(Return(true)); + + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DataWrite(_, _)) + .WillOnce(Invoke([](uint16_t /* cid */, BT_HDR* p_data) -> tL2CAP_DW_RESULT { + osi_free_and_reset((void**)&p_data); + return tL2CAP_DW_RESULT::SUCCESS; + })); + ASSERT_TRUE(SDP_ServiceSearchRequest(addr, sdp_db, nullptr)); int cid = L2CA_ConnectReqWithSecurity_cid; tCONN_CB* p_ccb = sdpu_find_ccb_by_cid(cid); @@ -127,6 +141,19 @@ tCONN_CB* find_ccb(uint16_t cid, tSDP_STATE state) { } TEST_F(StackSdpInitTest, sdp_service_search_request_queuing) { + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillOnce(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { + return L2CA_ConnectReqWithSecurity_cid; + })); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DataWrite(_, _)) + .WillRepeatedly(Invoke([](uint16_t /* cid */, BT_HDR* data) -> tL2CAP_DW_RESULT { + osi_free(data); + return tL2CAP_DW_RESULT::SUCCESS; + })); + + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DisconnectReq(_)).WillOnce(Return(true)); + ASSERT_TRUE(SDP_ServiceSearchRequest(addr, sdp_db, nullptr)); const int cid = L2CA_ConnectReqWithSecurity_cid; tCONN_CB* p_ccb1 = find_ccb(cid, tSDP_STATE::CONN_SETUP); @@ -165,9 +192,21 @@ void sdp_callback(const RawAddress& /* bd_addr */, tSDP_RESULT result) { } TEST_F(StackSdpInitTest, sdp_service_search_request_queuing_race_condition) { + uint16_t cid = L2CA_ConnectReqWithSecurity_cid; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([&cid](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { return cid++; })); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DisconnectReq(_)).WillRepeatedly(Return(true)); + + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_DataWrite(_, _)) + .WillOnce(Invoke([](uint16_t /* cid */, BT_HDR* p_data) -> tL2CAP_DW_RESULT { + osi_free_and_reset((void**)&p_data); + return tL2CAP_DW_RESULT::SUCCESS; + })); + // start first request ASSERT_TRUE(SDP_ServiceSearchRequest(addr, sdp_db, sdp_callback)); - const int cid1 = L2CA_ConnectReqWithSecurity_cid; + const uint16_t cid1 = L2CA_ConnectReqWithSecurity_cid; tCONN_CB* p_ccb1 = find_ccb(cid1, tSDP_STATE::CONN_SETUP); ASSERT_NE(p_ccb1, nullptr); ASSERT_EQ(p_ccb1->con_state, tSDP_STATE::CONN_SETUP); @@ -180,7 +219,7 @@ TEST_F(StackSdpInitTest, sdp_service_search_request_queuing_race_condition) { sdp_disconnect(p_ccb1, tSDP_STATUS::SDP_SUCCESS); sdp_cb.reg_info.pL2CA_DisconnectCfm_Cb(p_ccb1->connection_id, 0); - const int cid2 = L2CA_ConnectReqWithSecurity_cid; + const uint16_t cid2 = L2CA_ConnectReqWithSecurity_cid + 1; ASSERT_NE(cid1, cid2); // The callback a queued a new request tCONN_CB* p_ccb2 = find_ccb(cid2, tSDP_STATE::CONN_SETUP); ASSERT_NE(p_ccb2, nullptr); @@ -410,6 +449,10 @@ TEST_F(SDP_GetDiRecord_Tests, SDP_GetDiRecord_Regression_test0) { } TEST_F(StackSdpInitTest, sdpu_dump_all_ccb) { + uint16_t cid = L2CA_ConnectReqWithSecurity_cid; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([&cid](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { return cid++; })); sdpu_dump_all_ccb(); for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) { @@ -425,6 +468,11 @@ TEST_F(StackSdpInitTest, sdpu_dump_all_ccb) { TEST_F(StackSdpInitTest, SDP_Dumpsys) { SDP_Dumpsys(1); } TEST_F(StackSdpInitTest, SDP_Dumpsys_ccb) { + uint16_t cid = L2CA_ConnectReqWithSecurity_cid; + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _)) + .WillRepeatedly(Invoke([&cid](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, + uint16_t /* sec_level */) -> uint16_t { return cid++; })); + for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) { RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i}); ASSERT_NE(nullptr, sdp_conn_originate(bd_addr)); diff --git a/system/stack/test/sdp/stack_sdp_utils_test.cc b/system/stack/test/sdp/stack_sdp_utils_test.cc index 1b5abe2431..764df1affc 100644 --- a/system/stack/test/sdp/stack_sdp_utils_test.cc +++ b/system/stack/test/sdp/stack_sdp_utils_test.cc @@ -34,7 +34,7 @@ #include "test/mock/mock_btif_config.h" #include "test/mock/mock_osi_allocator.h" #include "test/mock/mock_osi_properties.h" -#include "test/mock/mock_stack_l2cap_api.h" +#include "test/mock/mock_stack_l2cap_interface.h" #ifndef BT_DEFAULT_BUFFER_SIZE #define BT_DEFAULT_BUFFER_SIZE (4096 + 16) @@ -53,10 +53,13 @@ static int L2CA_ConnectReqWithSecurity_cid = 0x42; static RawAddress addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6}); static tSDP_DISCOVERY_DB* sdp_db = nullptr; -using testing::_; -using testing::DoAll; -using testing::Return; -using testing::SetArrayArgument; +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnArg; +using ::testing::SaveArg; +using ::testing::SetArrayArgument; bool sdp_dynamic_change_hfp_version(const tSDP_ATTRIBUTE* p_attr, const RawAddress& remote_address); void hfp_fallback(bool& is_hfp_fallback, const tSDP_ATTRIBUTE* p_attr); @@ -210,30 +213,19 @@ class StackSdpMockAndFakeTest : public ::testing::Test { protected: void SetUp() override { fake_osi_ = std::make_unique<test::fake::FakeOsi>(); - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body = - [](uint16_t /* psm */, const RawAddress& /* p_bd_addr */, uint16_t /* sec_level */) { - return ++L2CA_ConnectReqWithSecurity_cid; - }; - test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t /* cid */, - BT_HDR* p_data) -> tL2CAP_DW_RESULT { - osi_free_and_reset((void**)&p_data); - return tL2CAP_DW_RESULT::FAILED; - }; - test::mock::stack_l2cap_api::L2CA_DisconnectReq.body = [](uint16_t /* cid */) { return true; }; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body = - [](uint16_t /* psm */, const tL2CAP_APPL_INFO& /* p_cb_info */, bool /* enable_snoop */, - tL2CAP_ERTM_INFO* /* p_ertm_info */, uint16_t /* my_mtu */, - uint16_t /* required_remote_mtu */, uint16_t /* sec_level */) { - return 42; // return non zero - }; + bluetooth::testing::stack::l2cap::set_interface(&mock_stack_l2cap_interface_); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _)) + .WillOnce(DoAll(SaveArg<1>(&l2cap_callbacks_), ::testing::ReturnArg<0>())); + EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_Deregister(_)); } void TearDown() override { - test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity = {}; - test::mock::stack_l2cap_api::L2CA_DataWrite = {}; - test::mock::stack_l2cap_api::L2CA_DisconnectReq = {}; + bluetooth::testing::stack::l2cap::reset_interface(); + fake_osi_.reset(); } + + tL2CAP_APPL_INFO l2cap_callbacks_{}; + bluetooth::testing::stack::l2cap::Mock mock_stack_l2cap_interface_; std::unique_ptr<test::fake::FakeOsi> fake_osi_; }; @@ -247,6 +239,7 @@ protected: void TearDown() override { osi_free(sdp_db); + sdp_free(); StackSdpMockAndFakeTest::TearDown(); } }; |