diff --git a/services/distributedhardwarefwkservice/src/hdfoperate/hdf_operate.cpp b/services/distributedhardwarefwkservice/src/hdfoperate/hdf_operate.cpp index e465fee177a96b55efefe95f43dd962b7149f3fe..dd660855ff341141ea82b232d6addc34e6ebfb14 100644 --- a/services/distributedhardwarefwkservice/src/hdfoperate/hdf_operate.cpp +++ b/services/distributedhardwarefwkservice/src/hdfoperate/hdf_operate.cpp @@ -156,6 +156,10 @@ int32_t HdfOperateManager::UnLoadDistributedHDF(DHType dhType) int32_t HdfOperateManager::AddDeathRecipient(DHType dhType, sptr &remote) { + if (remote == nullptr) { + DHLOGE("remote ptr is null."); + return ERR_DH_FWK_POINTER_IS_NULL; + } DHLOGI("Add death recipient begin, dhType = %{public}#X!", dhType); bool ret = false; switch (dhType) { @@ -179,6 +183,10 @@ int32_t HdfOperateManager::AddDeathRecipient(DHType dhType, sptr int32_t HdfOperateManager::RemoveDeathRecipient(DHType dhType, sptr &remote) { + if (remote == nullptr) { + DHLOGE("remote ptr is null."); + return ERR_DH_FWK_POINTER_IS_NULL; + } DHLOGI("Remove death recipient begin, dhType = %{public}#X!", dhType); bool ret = false; switch (dhType) { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/BUILD.gn index de452ff4ce5a6debfcad57c111a4d6806b034704..0faba3495bc3d2d97373eb528193dda882d35076 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/BUILD.gn @@ -21,6 +21,7 @@ config("module_private_config") { include_dirs = [ "${av_trans_path}/common/include", "${innerkits_path}/include", + "${innerkits_path}/include/ipc", "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", @@ -40,6 +41,7 @@ ohos_unittest("HdfOperateTest") { configs = [ ":module_private_config" ] deps = [ + "${innerkits_path}:libdhfwk_sdk", "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/hdf_operate_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/hdf_operate_test.cpp index 1a2459c42b51bb4a7c2c73e78975f290ae3b5e30..7cb3eb48f53db796362cee7da2aeb4ad1b3d0365 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/hdf_operate_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/hdfoperate/hdf_operate_test.cpp @@ -13,12 +13,15 @@ * limitations under the License. */ +#include "hdf_operate.h" + #include +#include "iremote_stub.h" #include "component_loader.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" -#include "hdf_operate.h" +#include "publisher_listener_stub.h" using namespace testing; using namespace testing::ext; @@ -31,6 +34,17 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + + class TestPublisherListenerStub : public OHOS::DistributedHardware::PublisherListenerStub { + public: + TestPublisherListenerStub() = default; + virtual ~TestPublisherListenerStub() = default; + void OnMessage(const DHTopic topic, const std::string& message) + { + (void)topic; + (void)message; + } + }; }; constexpr int32_t TEST_COMP_SINK_SA_ID = 4804; @@ -230,5 +244,33 @@ HWTEST_F(HdfOperateTest, RigidReleaseSourcePtr_001, TestSize.Level1) HdfOperateManager::GetInstance().sourceHandlerDataMap_.erase(DHType::AUDIO); EXPECT_EQ(ERR_DH_FWK_LOADER_DLCLOSE_FAIL, ret); } + +HWTEST_F(HdfOperateTest, AddDeathRecipient_001, TestSize.Level1) +{ + sptr remote = nullptr; + auto ret = HdfOperateManager::GetInstance().AddDeathRecipient(DHType::UNKNOWN, remote); + EXPECT_EQ(ERR_DH_FWK_POINTER_IS_NULL, ret); +} + +HWTEST_F(HdfOperateTest, AddDeathRecipient_002, TestSize.Level1) +{ + sptr remote(new TestPublisherListenerStub()); + auto ret = HdfOperateManager::GetInstance().AddDeathRecipient(DHType::UNKNOWN, remote); + EXPECT_EQ(ERR_DH_FWK_NO_HDF_SUPPORT, ret); +} + +HWTEST_F(HdfOperateTest, RemoveDeathRecipient_001, TestSize.Level1) +{ + sptr remote = nullptr; + auto ret = HdfOperateManager::GetInstance().RemoveDeathRecipient(DHType::UNKNOWN, remote); + EXPECT_EQ(ERR_DH_FWK_POINTER_IS_NULL, ret); +} + +HWTEST_F(HdfOperateTest, RemoveDeathRecipient_002, TestSize.Level1) +{ + sptr remote(new TestPublisherListenerStub()); + auto ret = HdfOperateManager::GetInstance().RemoveDeathRecipient(DHType::UNKNOWN, remote); + EXPECT_EQ(ERR_DH_FWK_NO_HDF_SUPPORT, ret); +} } // namespace DistributedHardware } // namespace OHOS