diff options
author | 2025-03-07 04:35:26 +0100 | |
---|---|---|
committer | 2025-03-12 12:27:39 +0100 | |
commit | 36706f4baf3f36477884be8bedf17ca023250e41 (patch) | |
tree | 7700df63f06e4d007f834e96c94b1c76f462a053 | |
parent | eb06188da9843a034ee8915beffd092581cc7279 (diff) |
Test: make mock_hci_layer_ into unique_ptr
Getting rid of memory leaks, dangling pointers in tests.
Bug: None
Test: updated
Flag: exempt, test only change
Change-Id: I66f13abdb21b5d6322d6e9a3ab0b15386b0cffc0
-rw-r--r-- | system/btif/test/btif_core_test.cc | 18 | ||||
-rw-r--r-- | system/stack/test/btm/stack_btm_sec_test.cc | 5 | ||||
-rw-r--r-- | system/stack/test/btm/stack_btm_test.cc | 33 | ||||
-rw-r--r-- | system/test/mock/mock_main_shim_entry.cc | 4 | ||||
-rw-r--r-- | system/test/mock/mock_main_shim_entry.h | 4 |
5 files changed, 38 insertions, 26 deletions
diff --git a/system/btif/test/btif_core_test.cc b/system/btif/test/btif_core_test.cc index 21723e3674..3073bd194c 100644 --- a/system/btif/test/btif_core_test.cc +++ b/system/btif/test/btif_core_test.cc @@ -762,12 +762,13 @@ class BtifCoreWithVendorSupportTest : public BtifCoreWithControllerTest { protected: void SetUp() override { BtifCoreWithControllerTest::SetUp(); - bluetooth::hci::testing::mock_hci_layer_ = &hci_; + bluetooth::hci::testing::mock_hci_layer_ = + std::make_unique<bluetooth::hci::testing::MockHciLayer>(); test::mock::osi_properties::osi_property_get.body = get_properties; std::promise<void> configuration_promise; auto configuration_done = configuration_promise.get_future(); - EXPECT_CALL(hci_, + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, EnqueueCommand(_, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_))) .WillOnce( // Replace with real PDL for 0xfc17 @@ -785,7 +786,7 @@ protected: configuration_promise.set_value(); }) .RetiresOnSaturation(); - EXPECT_CALL(hci_, + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, EnqueueCommand(_, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_))) .WillOnce([](std::unique_ptr<CommandBuilder> cmd, ContextualOnceCallback<void(CommandCompleteView)> callback) { @@ -799,7 +800,8 @@ protected: callback(response); }) .RetiresOnSaturation(); - EXPECT_CALL(hci_, RegisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT, _)) + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, + RegisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT, _)) .WillOnce(SaveArg<1>(&this->vse_callback_)); do_in_main_thread(BindOnce([]() { bluetooth::bqr::EnableBtQualityReport(get_main()); })); ASSERT_EQ(std::future_status::ready, configuration_done.wait_for(std::chrono::seconds(1))); @@ -809,18 +811,18 @@ protected: std::promise<void> disable_promise; auto disable_future = disable_promise.get_future(); auto set_promise = [&disable_promise]() { disable_promise.set_value(); }; - EXPECT_CALL(hci_, UnregisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT)); - EXPECT_CALL(hci_, + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, + UnregisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT)); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, EnqueueCommand(_, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_))) .WillOnce(Invoke(set_promise)) .RetiresOnSaturation(); do_in_main_thread(BindOnce([]() { bluetooth::bqr::DisableBtQualityReport(); })); ASSERT_EQ(std::future_status::ready, disable_future.wait_for(std::chrono::seconds(1))); - bluetooth::hci::testing::mock_hci_layer_ = nullptr; + bluetooth::hci::testing::mock_hci_layer_.reset(); BtifCoreWithControllerTest::TearDown(); } - bluetooth::hci::testing::MockHciLayer hci_; ContextualCallback<void(VendorSpecificEventView)> vse_callback_; }; diff --git a/system/stack/test/btm/stack_btm_sec_test.cc b/system/stack/test/btm/stack_btm_sec_test.cc index 58317afc29..62226e2750 100644 --- a/system/stack/test/btm/stack_btm_sec_test.cc +++ b/system/stack/test/btm/stack_btm_sec_test.cc @@ -69,7 +69,8 @@ protected: down_thread_ = new bluetooth::os::Thread("down_thread", bluetooth::os::Thread::Priority::NORMAL); down_handler_ = new bluetooth::os::Handler(down_thread_); - bluetooth::hci::testing::mock_hci_layer_ = &mock_hci_; + bluetooth::hci::testing::mock_hci_layer_ = + std::make_unique<bluetooth::hci::testing::MockHciLayer>(); bluetooth::hci::testing::mock_gd_shim_handler_ = up_handler_; } void TearDown() override { @@ -79,10 +80,10 @@ protected: down_handler_->Clear(); delete down_handler_; delete down_thread_; + bluetooth::hci::testing::mock_hci_layer_.reset(); StackBtmSecTest::TearDown(); } bluetooth::common::BidiQueue<bluetooth::hci::ScoView, bluetooth::hci::ScoBuilder> sco_queue_{10}; - bluetooth::hci::testing::MockHciLayer mock_hci_; bluetooth::os::Thread* up_thread_; bluetooth::os::Handler* up_handler_; bluetooth::os::Thread* down_thread_; diff --git a/system/stack/test/btm/stack_btm_test.cc b/system/stack/test/btm/stack_btm_test.cc index b053049e82..2341b21677 100644 --- a/system/stack/test/btm/stack_btm_test.cc +++ b/system/stack/test/btm/stack_btm_test.cc @@ -82,11 +82,12 @@ protected: down_thread_ = new bluetooth::os::Thread("down_thread", bluetooth::os::Thread::Priority::NORMAL); down_handler_ = new bluetooth::os::Handler(down_thread_); - bluetooth::hci::testing::mock_hci_layer_ = &mock_hci_; + bluetooth::hci::testing::mock_hci_layer_ = + std::make_unique<bluetooth::hci::testing::MockHciLayer>(); bluetooth::hci::testing::mock_gd_shim_handler_ = up_handler_; bluetooth::legacy::hci::testing::SetMock(legacy_hci_mock_); - EXPECT_CALL(mock_hci_, RegisterForScoConnectionRequests(_)); - EXPECT_CALL(mock_hci_, RegisterForDisconnects(_)); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, RegisterForScoConnectionRequests(_)); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, RegisterForDisconnects(_)); } void TearDown() override { up_handler_->Clear(); @@ -95,10 +96,10 @@ protected: down_handler_->Clear(); delete down_handler_; delete down_thread_; + bluetooth::hci::testing::mock_hci_layer_.release(); StackBtmTest::TearDown(); } bluetooth::common::BidiQueue<bluetooth::hci::ScoView, bluetooth::hci::ScoBuilder> sco_queue_{10}; - bluetooth::hci::testing::MockHciLayer mock_hci_; bluetooth::legacy::hci::testing::MockInterface legacy_hci_mock_; bluetooth::os::Thread* up_thread_; bluetooth::os::Handler* up_handler_; @@ -111,7 +112,8 @@ public: protected: void SetUp() override { StackBtmWithQueuesTest::SetUp(); - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); btm_cb.Init(); btm_sec_cb.Init(BTM_SEC_MODE_SC); } @@ -123,7 +125,8 @@ protected: }; TEST_F(StackBtmWithQueuesTest, GlobalLifecycle) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); get_btm_client_interface().lifecycle.btm_init(); get_btm_client_interface().lifecycle.btm_free(); } @@ -134,20 +137,23 @@ TEST_F(StackBtmTest, DynamicLifecycle) { } TEST_F(StackBtmWithQueuesTest, InitFree) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); btm_cb.Init(); btm_cb.Free(); } TEST_F(StackBtmWithQueuesTest, tSCO_CB) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); tSCO_CB* p_sco = &btm_cb.sco_cb; p_sco->Init(); p_sco->Free(); } TEST_F(StackBtmWithQueuesTest, InformClientOnConnectionSuccess) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); get_btm_client_interface().lifecycle.btm_init(); RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}); @@ -159,7 +165,8 @@ TEST_F(StackBtmWithQueuesTest, InformClientOnConnectionSuccess) { } TEST_F(StackBtmWithQueuesTest, NoInformClientOnConnectionFail) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); get_btm_client_interface().lifecycle.btm_init(); RawAddress bda({0x11, 0x22, 0x33, 0x44, 0x55, 0x66}); @@ -171,7 +178,8 @@ TEST_F(StackBtmWithQueuesTest, NoInformClientOnConnectionFail) { } TEST_F(StackBtmWithQueuesTest, default_packet_type) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); get_btm_client_interface().lifecycle.btm_init(); btm_cb.acl_cb_.SetDefaultPacketTypeMask(0x4321); @@ -181,7 +189,8 @@ TEST_F(StackBtmWithQueuesTest, default_packet_type) { } TEST_F(StackBtmWithQueuesTest, change_packet_type) { - EXPECT_CALL(mock_hci_, GetScoQueueEnd()).WillOnce(Return(sco_queue_.GetUpEnd())); + EXPECT_CALL(*bluetooth::hci::testing::mock_hci_layer_, GetScoQueueEnd()) + .WillOnce(Return(sco_queue_.GetUpEnd())); get_btm_client_interface().lifecycle.btm_init(); uint16_t handle = 0x123; diff --git a/system/test/mock/mock_main_shim_entry.cc b/system/test/mock/mock_main_shim_entry.cc index cb599b02a1..3cdc714eaf 100644 --- a/system/test/mock/mock_main_shim_entry.cc +++ b/system/test/mock/mock_main_shim_entry.cc @@ -40,7 +40,7 @@ namespace testing { std::unique_ptr<MockAclManager> mock_acl_manager_; std::unique_ptr<MockControllerInterface> mock_controller_; -HciInterface* mock_hci_layer_{nullptr}; +std::unique_ptr<MockHciLayer> mock_hci_layer_; os::Handler* mock_gd_shim_handler_{nullptr}; MockLeAdvertisingManager* mock_le_advertising_manager_{nullptr}; MockLeScanningManager* mock_le_scanning_manager_{nullptr}; @@ -60,7 +60,7 @@ namespace shim { hci::AclManager* GetAclManager() { return hci::testing::mock_acl_manager_.get(); } hci::ControllerInterface* GetController() { return hci::testing::mock_controller_.get(); } -hci::HciInterface* GetHciLayer() { return hci::testing::mock_hci_layer_; } +hci::HciInterface* GetHciLayer() { return hci::testing::mock_hci_layer_.get(); } hci::LeAdvertisingManager* GetAdvertising() { return hci::testing::mock_le_advertising_manager_; } hci::LeScanningManager* GetScanning() { return hci::testing::mock_le_scanning_manager_; } hci::DistanceMeasurementManager* GetDistanceMeasurementManager() { diff --git a/system/test/mock/mock_main_shim_entry.h b/system/test/mock/mock_main_shim_entry.h index 8666648d69..5d427fd963 100644 --- a/system/test/mock/mock_main_shim_entry.h +++ b/system/test/mock/mock_main_shim_entry.h @@ -20,7 +20,7 @@ #include "hci/acl_manager_mock.h" #include "hci/controller_interface_mock.h" #include "hci/distance_measurement_manager_mock.h" -#include "hci/hci_interface.h" +#include "hci/hci_layer_mock.h" #include "hci/le_advertising_manager_mock.h" #include "hci/le_scanning_manager_mock.h" #include "storage/storage_module.h" @@ -31,7 +31,7 @@ namespace testing { extern std::unique_ptr<MockAclManager> mock_acl_manager_; extern std::unique_ptr<MockControllerInterface> mock_controller_; -extern HciInterface* mock_hci_layer_; +extern std::unique_ptr<MockHciLayer> mock_hci_layer_; extern os::Handler* mock_gd_shim_handler_; extern MockLeAdvertisingManager* mock_le_advertising_manager_; extern MockLeScanningManager* mock_le_scanning_manager_; |