From 00c9ba5cbbfc0614aa1b8c57a8320ac7cd35d387 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Fri, 11 Jul 2025 09:31:59 +0800 Subject: [PATCH] =?UTF-8?q?fwk=E4=BB=A3=E7=A0=81=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../ipc/get_dh_descriptors_callback_stub.cpp | 8 +- .../hardware_status_listener_stub_test.h | 46 +++-- .../hardware_status_listener_stub_test.cpp | 62 +++++++ .../include/resourcemanager/version_info.h | 1 + .../src/componentloader/component_loader.cpp | 8 +- .../componentmanager/component_manager.cpp | 6 +- .../src/distributed_hardware_service.cpp | 20 +- .../ipc/get_dh_descriptors_callback_proxy.cpp | 10 +- .../local_hardware_manager.cpp | 8 +- .../capability_info_manager.cpp | 4 + .../src/resourcemanager/version_info.cpp | 35 ++-- .../src/task/offline_task.cpp | 10 +- .../src/task/online_task.cpp | 14 +- .../src/task/task_board.cpp | 33 ++-- .../src/task/task_executor.cpp | 5 +- .../src/transport/dh_comm_tool.cpp | 3 + .../src/transport/dh_transport.cpp | 12 +- .../src/component_loader_test.cpp | 36 ++++ .../src/component_manager_test_ext.cpp | 45 +++++ .../distributedhardwareservice/BUILD.gn | 8 + .../distributed_hardware_service_test.h | 15 +- .../src/distributed_hardware_service_test.cpp | 114 +++++++++++- .../hardware_status_listener_proxy/BUILD.gn | 1 + .../hardware_status_listener_proxy_test.h | 16 +- .../hardware_status_listener_proxy_test.cpp | 52 ++++++ .../publisher/src/publisher_item_test.cpp | 21 ++- .../unittest/common/resourcemanager/BUILD.gn | 2 + .../resourcemanager/resource_manager_test.cpp | 39 ++++ .../resourcemanager/version_info_test.cpp | 99 ++++++++++ .../test/unittest/common/task/BUILD.gn | 43 +++++ .../common/task/src/online_task_test.cpp | 172 ++++++++++++++++++ utils/include/dh_utils_tool.h | 2 +- utils/src/dh_utils_tool.cpp | 2 +- 33 files changed, 857 insertions(+), 95 deletions(-) create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/task/src/online_task_test.cpp diff --git a/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp b/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp index 81790ffb..13aa9f0f 100644 --- a/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp +++ b/interfaces/inner_kits/src/ipc/get_dh_descriptors_callback_stub.cpp @@ -60,12 +60,12 @@ int32_t GetDhDescriptorsCallbackStub::OnRemoteRequest( int32_t GetDhDescriptorsCallbackStub::ReadDescriptors(MessageParcel &data, std::vector &descriptors) { - int32_t size = data.ReadInt32(); - if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { - DHLOGE("The array descriptors are too large, size: %{public}d!", size); + uint32_t size = data.ReadUint32(); + if (size > MAX_DH_DESCRIPTOR_ARRAY_SIZE) { + DHLOGE("The array descriptors size is invalid, size: %{public}u!", size); return ERR_DH_FWK_PARA_INVALID; } - for (int32_t i = 0; i < size; ++i) { + for (uint32_t i = 0; i < size; ++i) { DHDescriptor descriptor; descriptor.dhType = static_cast(data.ReadUint32()); descriptor.id = data.ReadString(); diff --git a/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/include/hardware_status_listener_stub_test.h b/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/include/hardware_status_listener_stub_test.h index 6ce58793..45a5c127 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/include/hardware_status_listener_stub_test.h +++ b/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/include/hardware_status_listener_stub_test.h @@ -16,16 +16,14 @@ #ifndef OHOS_HARDWARE_STATUS_LISTENER_STUB_TEST_H #define OHOS_HARDWARE_STATUS_LISTENER_STUB_TEST_H -#include -#include -#include - #include -#include "distributed_hardware_errno.h" #include "iremote_stub.h" -#include "idistributed_hardware.h" + +#include "distributed_hardware_errno.h" +#include "get_dh_descriptors_callback_stub.h" #include "hardware_status_listener_stub.h" +#include "idistributed_hardware.h" namespace OHOS { namespace DistributedHardware { @@ -38,6 +36,7 @@ public: std::shared_ptr sinkListenerStub_ = nullptr; std::shared_ptr sourceListenerStub_ = nullptr; + std::shared_ptr getDHCallbackStub_ = nullptr; }; class MockHDSinkStatusListenerStub : public HDSinkStatusListenerStub { @@ -54,16 +53,31 @@ public: class MockHDSourceStatusListenerStub : public HDSourceStatusListenerStub { public: -void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) -{ - (void)networkId; - (void)dhDescriptor; -} -void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) -{ - (void)networkId; - (void)dhDescriptor; -} + void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) + { + (void)networkId; + (void)dhDescriptor; + } + void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) + { + (void)networkId; + (void)dhDescriptor; + } +}; + +class TestGetDhDescriptorsCallbackStub : public GetDhDescriptorsCallbackStub { +public: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) + { + (void)networkId; + (void)descriptors; + (void)enableStep; + } + void OnError(const std::string &networkId, int32_t error) + { + (void)networkId; + (void)error; + } }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/src/hardware_status_listener_stub_test.cpp b/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/src/hardware_status_listener_stub_test.cpp index 4360c73d..2cf40846 100644 --- a/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/src/hardware_status_listener_stub_test.cpp +++ b/interfaces/inner_kits/test/unittest/common/ipc/hardware_status_listener_stub/src/hardware_status_listener_stub_test.cpp @@ -19,6 +19,9 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { +constexpr uint32_t MAX_DH_DESCRIPTOR_ARRAY_SIZE = 4100; +constexpr uint32_t INVALID_CODE = 100; + void HardwareStatusListenerStubTest::SetUpTestCase() { } @@ -31,12 +34,14 @@ void HardwareStatusListenerStubTest::SetUp() { sinkListenerStub_ = std::make_shared(); sourceListenerStub_ = std::make_shared(); + getDHCallbackStub_ = std::make_shared(); } void HardwareStatusListenerStubTest::TearDown() { sinkListenerStub_ = nullptr; sourceListenerStub_ = nullptr; + getDHCallbackStub_ = nullptr; } /** @@ -152,5 +157,62 @@ HWTEST_F(HardwareStatusListenerStubTest, OnRemoteRequest_103, TestSize.Level0) data.WriteString("id_test"); EXPECT_EQ(DH_FWK_SUCCESS, sourceListenerStub_->OnRemoteRequest(code, data, reply, option)); } + +HWTEST_F(HardwareStatusListenerStubTest, OnRemoteRequest_104, TestSize.Level0) +{ + ASSERT_TRUE(getDHCallbackStub_ != nullptr); + std::vector dhDescriptors; + uint32_t code = static_cast(IGetDhDescriptorsCallback::Message::ON_SUCCESS); + MessageParcel data; + MessageParcel reply; + MessageOption option; + data.WriteInterfaceToken(getDHCallbackStub_->GetDescriptor()); + data.WriteString("netWorkId_test"); + data.WriteUint32(MAX_DH_DESCRIPTOR_ARRAY_SIZE); + auto ret = getDHCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(HardwareStatusListenerStubTest, OnRemoteRequest_105, TestSize.Level0) +{ + ASSERT_TRUE(getDHCallbackStub_ != nullptr); + uint32_t code = static_cast(IGetDhDescriptorsCallback::Message::ON_SUCCESS); + MessageParcel data; + MessageParcel reply; + MessageOption option; + data.WriteInterfaceToken(getDHCallbackStub_->GetDescriptor()); + data.WriteString("netWorkId_test"); + data.WriteUint32(1); + data.WriteUint32(static_cast(DHType::CAMERA)); + data.WriteString("camera_1"); + auto ret = getDHCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(HardwareStatusListenerStubTest, OnRemoteRequest_106, TestSize.Level0) +{ + ASSERT_TRUE(getDHCallbackStub_ != nullptr); + uint32_t code = static_cast(IGetDhDescriptorsCallback::Message::ON_ERROR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + data.WriteInterfaceToken(getDHCallbackStub_->GetDescriptor()); + data.WriteString("netWorkId_test"); + data.WriteInt32(1); + auto ret = getDHCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(HardwareStatusListenerStubTest, OnRemoteRequest_107, TestSize.Level0) +{ + ASSERT_TRUE(getDHCallbackStub_ != nullptr); + uint32_t code = INVALID_CODE; + MessageParcel data; + MessageParcel reply; + MessageOption option; + data.WriteInterfaceToken(getDHCallbackStub_->GetDescriptor()); + auto ret = getDHCallbackStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_NE(DH_FWK_SUCCESS, ret); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h index eb97edc6..ee25de29 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h @@ -34,6 +34,7 @@ struct VersionInfo { void ToJson(cJSON *jsonObject, const CompVersion &compVer); void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo); void FromJson(const cJSON *jsonObject, CompVersion &compVer); +void FromJsoncompVerContinue(const cJSON *jsonObject, CompVersion &compVer); void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo); } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 785822ef..8566f51b 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -248,7 +248,9 @@ void ComponentLoader::ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilt cJSON *filter = nullptr; config.sourceFeatureFilters.clear(); cJSON_ArrayForEach(filter, sourceFeatureFilters) { - config.sourceFeatureFilters.push_back(filter->valuestring); + if (filter != nullptr && filter->type == cJSON_String) { + config.sourceFeatureFilters.push_back(std::string(filter->valuestring)); + } } } @@ -257,7 +259,9 @@ void ComponentLoader::ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFea cJSON *feature = nullptr; config.sinkSupportedFeatures.clear(); cJSON_ArrayForEach(feature, sinkSupportedFeatures) { - config.sinkSupportedFeatures.push_back(feature->valuestring); + if (feature != nullptr && feature->type == cJSON_String) { + config.sinkSupportedFeatures.push_back(std::string(feature->valuestring)); + } } } diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 5eaf2b6b..76814600 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -1376,7 +1376,7 @@ int32_t ComponentManager::ForceDisableSinkInternal( auto itEnableInfo = status.enableInfos.find(dhDescriptor.id); if (itEnableInfo == status.enableInfos.end()) { DHLOGE("Repeat call ForceDisableSink, dhType = %{public}u, id = %{public}s.", - dhDescriptor.dhType, dhDescriptor.id.c_str()); + dhDescriptor.dhType, GetAnonyString(dhDescriptor.id).c_str()); return ERR_DH_FWK_COMPONENT_REPEAT_CALL; } auto &enableInfo = itEnableInfo->second; @@ -1425,7 +1425,7 @@ int32_t ComponentManager::ForceDisableSourceInternal(const std::string &networkI auto itEnableInfo = status.enableInfos.find(enableInfoKey); if (itEnableInfo == status.enableInfos.end()) { DHLOGE("Repeat call ForceDisableSource, networkId = %{public}s, dhType = %{public}u, id = %{public}s.", - GetAnonyString(networkId).c_str(), dhDescriptor.dhType, dhDescriptor.id.c_str()); + GetAnonyString(networkId).c_str(), dhDescriptor.dhType, GetAnonyString(dhDescriptor.id).c_str()); return ERR_DH_FWK_COMPONENT_REPEAT_CALL; } auto &enableInfo = itEnableInfo->second; @@ -1613,7 +1613,7 @@ void ComponentManager::RecoverAutoEnableSink(DHType dhType) std::vector> metaCapInfos; MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(localDeviceInfo.udidHash, metaCapInfos); std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr localMetaInfo) { - if (localMetaInfo->GetDHType() == dhType) { + if (localMetaInfo != nullptr && localMetaInfo->GetDHType() == dhType) { localMetaInfos.push_back({localMetaInfo->GetDHId(), localMetaInfo->GetDHType()}); } }); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index bba3c278..9209f70d 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -28,6 +28,7 @@ #include "system_ability_definition.h" #include "access_manager.h" +#include "anonymous_string.h" #include "av_trans_control_center.h" #include "capability_info_manager.h" #include "meta_info_manager.h" @@ -321,6 +322,10 @@ int32_t DistributedHardwareService::PauseDistributedHardware(DHType dhType, cons DHLOGE("PauseDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); return ERR_DH_FWK_PARA_INVALID; } + if (sinkMap[dhType] == nullptr) { + DHLOGE("Sinkhandler ptr is null"); + return ERR_DH_FWK_PARA_INVALID; + } int32_t ret = sinkMap[dhType]->PauseDistributedHardware(networkId); if (ret != 0) { DHLOGE("PauseDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret); @@ -339,6 +344,10 @@ int32_t DistributedHardwareService::ResumeDistributedHardware(DHType dhType, con DHLOGE("ResumeDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); return ERR_DH_FWK_PARA_INVALID; } + if (sinkMap[dhType] == nullptr) { + DHLOGE("Sinkhandler ptr is null"); + return ERR_DH_FWK_PARA_INVALID; + } int32_t ret = sinkMap[dhType]->ResumeDistributedHardware(networkId); if (ret != 0) { DHLOGE("ResumeDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret); @@ -357,6 +366,10 @@ int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const DHLOGE("StopDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); return ERR_DH_FWK_PARA_INVALID; } + if (sinkMap[dhType] == nullptr) { + DHLOGE("Sinkhandler ptr is null"); + return ERR_DH_FWK_PARA_INVALID; + } int32_t ret = sinkMap[dhType]->StopDistributedHardware(networkId); if (ret != 0) { DHLOGE("StopDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret); @@ -368,7 +381,8 @@ int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const int32_t DistributedHardwareService::GetDistributedHardware(const std::string &networkId, EnableStep enableStep, const sptr callback) { - if (!IsIdLengthValid(networkId)) { + if (!IsIdLengthValid(networkId) || callback == nullptr) { + DHLOGE("networkId size is invalid or callback ptr is null"); return ERR_DH_FWK_PARA_INVALID; } std::string deviceId; @@ -392,7 +406,7 @@ int32_t DistributedHardwareService::GetDistributedHardware(const std::string &ne descriptor.dhType = metaCapInfo->GetDHType(); descriptors.push_back(descriptor); } - DHLOGI("Get MetacapInfo Success, networkId: %{public}s.", realNetworkId.c_str()); + DHLOGI("Get MetacapInfo Success, networkId: %{public}s.", GetAnonyString(realNetworkId).c_str()); callback->OnSuccess(realNetworkId, descriptors, enableStep); return DH_FWK_SUCCESS; } @@ -406,7 +420,7 @@ int32_t DistributedHardwareService::GetDistributedHardware(const std::string &ne descriptor.dhType = capabilitie->GetDHType(); descriptors.push_back(descriptor); } - DHLOGI("Get CapabilitieInfo Success, deviceId: %{public}s.", deviceId.c_str()); + DHLOGI("Get CapabilitieInfo Success, deviceId: %{public}s.", GetAnonyString(deviceId).c_str()); callback->OnSuccess(realNetworkId, descriptors, enableStep); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp index 637706dc..7237a4b4 100644 --- a/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp +++ b/services/distributedhardwarefwkservice/src/ipc/get_dh_descriptors_callback_proxy.cpp @@ -102,16 +102,16 @@ void GetDhDescriptorsCallbackProxy::OnError(const std::string &networkId, int32_ int32_t GetDhDescriptorsCallbackProxy::WriteDescriptors(MessageParcel &data, const std::vector &descriptors) { - int32_t size = (int32_t)descriptors.size(); - if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) { - DHLOGE("The array descriptors are too large, size: %{public}d!", size); + uint32_t size = static_cast(descriptors.size()); + if (size > MAX_DH_DESCRIPTOR_ARRAY_SIZE) { + DHLOGE("The array descriptors are too large, size: %{public}u!", size); return ERR_DH_FWK_PARA_INVALID; } - if (!data.WriteInt32(size)) { + if (!data.WriteUint32(size)) { DHLOGE("Write descriptors size failed!"); return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; } - for (int32_t i = 0; i < size; ++i) { + for (uint32_t i = 0; i < size; ++i) { const DHDescriptor &descriptor = descriptors.at(i); DHType type = static_cast(descriptor.dhType); if (!data.WriteUint32(static_cast(type))) { diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp index 18d5801b..c4bb76a0 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp @@ -51,9 +51,9 @@ void LocalHardwareManager::Init() std::lock_guard lock(localHardwareMgrMutex_); std::vector allCompTypes; ComponentLoader::GetInstance().GetAllCompTypes(allCompTypes); - int64_t allQueryStartTime = GetCurrentTime(); + uint64_t allQueryStartTime = GetCurrentTime(); for (auto dhType : allCompTypes) { - int64_t singleQueryStartTime = GetCurrentTime(); + uint64_t singleQueryStartTime = GetCurrentTime(); IHardwareHandler *hardwareHandler = nullptr; int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandler); if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) { @@ -78,11 +78,11 @@ void LocalHardwareManager::Init() pluginListenerMap_[dhType] = listener; hardwareHandler->RegisterPluginListener(listener); } - int64_t singleQueryEndTime = GetCurrentTime(); + uint64_t singleQueryEndTime = GetCurrentTime(); DHLOGI("query %{public}#X hardware cost time: %{public}" PRIu64 " ms", dhType, singleQueryEndTime - singleQueryStartTime); } - int64_t allQueryEndTime = GetCurrentTime(); + uint64_t allQueryEndTime = GetCurrentTime(); DHLOGI("query all local hardware cost time: %{public}" PRIu64 " ms", allQueryEndTime - allQueryStartTime); std::vector> capabilityInfos; std::vector> metaCapInfos; diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index 3960f25f..9af32875 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -604,6 +604,10 @@ void CapabilityInfoManager::AsyncGetDistributedHardware(const std::string &netwo const sptr callback) { DHLOGI("AsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); + if (callback == nullptr) { + DHLOGE("callback ptr is null"); + return; + } int32_t waitTimeMill = SYNC_DATA_TIMEOUT_MS; while (waitTimeMill > 0) { auto beginTime = std::chrono::steady_clock::now(); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index e31af4bb..0a227dcf 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -151,26 +151,35 @@ void FromJson(const cJSON *jsonObject, CompVersion &compVer) if (IsString(sinkVerJson)) { compVer.sinkVersion = sinkVerJson->valuestring; } + FromJsoncompVerContinue(jsonObject, compVer); +} + +void FromJsoncompVerContinue(const cJSON *jsonObject, CompVersion &compVer) +{ cJSON *sourceFeatureFilters = cJSON_GetObjectItem(jsonObject, SOURCE_FEATURE_FILTER); cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(jsonObject, SINK_SUPPORTED_FEATURE); - if (sourceFeatureFilters || sinkSupportedFeatures) { - compVer.haveFeature = true; - if (sourceFeatureFilters) { - cJSON *filterObj = nullptr; - compVer.sourceFeatureFilters.clear(); - cJSON_ArrayForEach(filterObj, sourceFeatureFilters) { + if (!IsArray(sourceFeatureFilters) && !IsArray(sinkSupportedFeatures)) { + compVer.haveFeature = false; + return; + } + compVer.haveFeature = true; + if (IsArray(sourceFeatureFilters)) { + cJSON *filterObj = nullptr; + compVer.sourceFeatureFilters.clear(); + cJSON_ArrayForEach(filterObj, sourceFeatureFilters) { + if (filterObj != nullptr && filterObj->type == cJSON_String) { compVer.sourceFeatureFilters.push_back(filterObj->valuestring); } } - if (sinkSupportedFeatures) { - cJSON *featureObj = nullptr; - compVer.sinkSupportedFeatures.clear(); - cJSON_ArrayForEach(featureObj, sinkSupportedFeatures) { + } + if (IsArray(sinkSupportedFeatures)) { + cJSON *featureObj = nullptr; + compVer.sinkSupportedFeatures.clear(); + cJSON_ArrayForEach(featureObj, sinkSupportedFeatures) { + if (featureObj != nullptr && featureObj->type == cJSON_String) { compVer.sinkSupportedFeatures.push_back(featureObj->valuestring); } } - } else { - compVer.haveFeature = false; } } @@ -191,7 +200,7 @@ void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo) } const cJSON *compVer = cJSON_GetObjectItem(jsonObject, COMP_VER); - if (compVer != NULL) { + if (IsArray(compVer)) { cJSON *compVerObj = nullptr; cJSON_ArrayForEach(compVerObj, compVer) { CompVersion compVerValue; diff --git a/services/distributedhardwarefwkservice/src/task/offline_task.cpp b/services/distributedhardwarefwkservice/src/task/offline_task.cpp index 32e99581..e94f73c7 100644 --- a/services/distributedhardwarefwkservice/src/task/offline_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/offline_task.cpp @@ -119,7 +119,9 @@ void OffLineTask::CreateDisableTask() std::vector> capabilityInfos; CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos); std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr cap) { - devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + if (cap != nullptr) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + } }); if (devDhInfos.empty()) { @@ -128,7 +130,9 @@ void OffLineTask::CreateDisableTask() std::string udidHash = DHContext::GetInstance().GetUdidHashIdByUUID(GetUUID()); MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr cap) { - devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + if (cap != nullptr) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + } }); } @@ -158,7 +162,9 @@ void OffLineTask::CreateDisableSinkTask() std::vector> metaCapInfos; MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(localDeviceInfo.udidHash, metaCapInfos); std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr localMetaInfo) { + if (localMetaInfo != nullptr) { localMetaInfos.push_back({localMetaInfo->GetDHId(), localMetaInfo->GetDHType()}); + } }); if (localMetaInfos.empty()) { DHLOGE("Can not get localMetainfo."); diff --git a/services/distributedhardwarefwkservice/src/task/online_task.cpp b/services/distributedhardwarefwkservice/src/task/online_task.cpp index 3bf08b54..f47c5944 100644 --- a/services/distributedhardwarefwkservice/src/task/online_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/online_task.cpp @@ -117,14 +117,18 @@ void OnLineTask::CreateEnableTask() std::vector> capabilityInfos; CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos); std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr cap) { - devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + if (cap != nullptr) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + } }); if (devDhInfos.empty()) { DHLOGW("Can not get cap info from CapabilityInfo, try use local Capability info"); LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos); std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr cap) { - devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + if (cap != nullptr) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + } }); } @@ -134,7 +138,9 @@ void OnLineTask::CreateEnableTask() std::vector> metaCapInfos; MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr cap) { - devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + if (cap != nullptr) { + devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()}); + } }); } @@ -165,7 +171,9 @@ void OnLineTask::CreateEnableSinkTask() std::vector> metaCapInfos; MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(localDeviceInfo.udidHash, metaCapInfos); std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr localMetaInfo) { + if (localMetaInfo != nullptr) { localMetaInfos.push_back({localMetaInfo->GetDHId(), localMetaInfo->GetDHType()}); + } }); if (localMetaInfos.empty()) { DHLOGE("Can not get localMetainfo."); diff --git a/services/distributedhardwarefwkservice/src/task/task_board.cpp b/services/distributedhardwarefwkservice/src/task/task_board.cpp index 00e6cf10..47345e5a 100644 --- a/services/distributedhardwarefwkservice/src/task/task_board.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_board.cpp @@ -70,8 +70,11 @@ bool TaskBoard::IsAllDisableTaskFinish() std::lock_guard lock(tasksMtx_); int32_t disableCount = 0; for (auto iter = tasks_.begin(); iter != tasks_.end(); iter++) { - if (iter->second->GetTaskType() == TaskType::DISABLE || iter->second->GetTaskType() == TaskType::META_DISABLE) { - disableCount++; + if (iter->second != nullptr) { + if (iter->second->GetTaskType() == TaskType::DISABLE || + iter->second->GetTaskType() == TaskType::META_DISABLE) { + disableCount++; + } } } DHLOGI("DisableTask count: %{public}d", disableCount); @@ -105,18 +108,20 @@ void TaskBoard::DumpAllTasks(std::vector &taskInfos) { std::lock_guard lock(tasksMtx_); for (auto t : tasks_) { - TaskDump taskInfo = { - .id = t.second->GetId(), - .taskType = t.second->GetTaskType(), - .taskParm = { - .networkId = t.second->GetNetworkId(), - .uuid = t.second->GetUUID(), - .dhId = t.second->GetDhId(), - .dhType = t.second->GetDhType(), - }, - .taskSteps = t.second->GetTaskSteps() - }; - taskInfos.emplace_back(taskInfo); + if (t.second != nullptr) { + TaskDump taskInfo = { + .id = t.second->GetId(), + .taskType = t.second->GetTaskType(), + .taskParm = { + .networkId = t.second->GetNetworkId(), + .uuid = t.second->GetUUID(), + .dhId = t.second->GetDhId(), + .dhType = t.second->GetDhType(), + }, + .taskSteps = t.second->GetTaskSteps() + }; + taskInfos.emplace_back(taskInfo); + } } } diff --git a/services/distributedhardwarefwkservice/src/task/task_executor.cpp b/services/distributedhardwarefwkservice/src/task/task_executor.cpp index 94f7103c..5496a6e5 100644 --- a/services/distributedhardwarefwkservice/src/task/task_executor.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_executor.cpp @@ -75,7 +75,6 @@ std::shared_ptr TaskExecutor::PopTask() if (!taskQueue_.empty()) { task = taskQueue_.front(); taskQueue_.pop(); - DHLOGI("Pop task: %{public}s", task->GetId().c_str()); } return task; @@ -99,6 +98,10 @@ void TaskExecutor::TriggerTask() }; DHLOGI("Post task to EventBus: %{public}s", task->GetId().c_str()); + if (DHContext::GetInstance().GetEventHandler() == nullptr) { + DHLOGE("eventHandler is nullptr"); + continue; + } DHContext::GetInstance().GetEventHandler()->PostTask(taskFunc, task->GetId()); } } diff --git a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp index 61cc6fe3..07463e64 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp @@ -291,6 +291,9 @@ void DHCommTool::DHCommToolEventHandler::ProcessFullCapsRsp(const FullCapsRsp &c } for (auto const &cap : capsRsp.caps) { + if (cap == nullptr) { + continue; + } BusinessState curState = ComponentManager::GetInstance().QueryBusinessState(capsRsp.networkId, cap->GetDHId()); DHLOGI("DH state: %{public}" PRIu32 ", networkId: %{public}s, dhId: %{public}s", (uint32_t)curState, GetAnonyString(capsRsp.networkId).c_str(), GetAnonyString(cap->GetDHId()).c_str()); diff --git a/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp b/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp index 5ac8aa55..651400d7 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp @@ -104,6 +104,7 @@ void DHTransport::OnBytesReceived(int32_t socketId, const void *data, uint32_t d if (memcpy_s(buf, dataLen + 1, reinterpret_cast(data), dataLen) != EOK) { DHLOGE("OnBytesReceived: memcpy memory failed"); free(buf); + buf = nullptr; return; } @@ -111,6 +112,7 @@ void DHTransport::OnBytesReceived(int32_t socketId, const void *data, uint32_t d DHLOGI("Receive message size: %{public}" PRIu32, dataLen); HandleReceiveMessage(message); free(buf); + buf = nullptr; return; } @@ -151,11 +153,11 @@ bool DHTransport::CheckCalleeAclRight(const std::shared_ptr commMsg) .tokenId = commMsg->tokenId, }; DmAccessCallee dmDstCallee = { - .networkId = localNetworkId, .accountId = accountId, + .networkId = localNetworkId, + .pkgName = DH_FWK_PKG_NAME, .userId = userId, .tokenId = localTokenId, - .pkgName = DH_FWK_PKG_NAME, }; DHLOGI("CheckAclRight remotenetworkId: %{public}s, accountId: %{public}s, localNetworkId: %{public}s", GetAnonyString(commMsg->msg).c_str(), GetAnonyString(accountId).c_str(), @@ -510,16 +512,20 @@ int32_t DHTransport::Send(const std::string &remoteNetworkId, const std::string compressedPayLoadSize) != EOK) { DHLOGE("Send: memcpy memory failed"); free(buf); + buf = nullptr; return ERR_DH_FWK_COMPONENT_TRANSPORT_OPT_FAILED; } int32_t ret = SendBytes(socketId, buf, compressedPayLoadSize); - free(buf); if (ret != DH_FWK_SUCCESS) { DHLOGE("dsoftbus send error, ret: %{public}d", ret); + free(buf); + buf = nullptr; return ERR_DH_FWK_COMPONENT_TRANSPORT_OPT_FAILED; } DHLOGI("Send payload success"); + free(buf); + buf = nullptr; return DH_FWK_SUCCESS; } } // DistributedHardware diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp index 8c89ca08..d87fb816 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp @@ -584,5 +584,41 @@ HWTEST_F(ComponentLoaderTest, IsDHTypeSupport_001, TestSize.Level1) auto ret = ComponentLoader::GetInstance().IsDHTypeSupport(DHType::AUDIO); EXPECT_EQ(ret, false); } + +HWTEST_F(ComponentLoaderTest, ParseSourceFeatureFiltersFromJson_001, TestSize.Level1) +{ + CompConfig config; + cJSON *srcFilters = cJSON_CreateArray(); + ASSERT_TRUE(srcFilters != nullptr); + ComponentLoader::GetInstance().ParseSourceFeatureFiltersFromJson(srcFilters, config); + EXPECT_TRUE(config.sourceFeatureFilters.empty()); + cJSON_Delete(srcFilters); + + cJSON *srcFilters1 = cJSON_CreateArray(); + ASSERT_TRUE(srcFilters1 != nullptr); + cJSON_AddItemToArray(srcFilters1, cJSON_CreateString("dcamera_1")); + cJSON_AddItemToArray(srcFilters1, cJSON_CreateNumber(1)); + ComponentLoader::GetInstance().ParseSourceFeatureFiltersFromJson(srcFilters1, config); + EXPECT_FALSE(config.sourceFeatureFilters.empty()); + cJSON_Delete(srcFilters1); +} + +HWTEST_F(ComponentLoaderTest, ParseSinkSupportedFeaturesFromJson_001, TestSize.Level1) +{ + CompConfig config; + cJSON *sinkFilters = cJSON_CreateArray(); + ASSERT_TRUE(sinkFilters != nullptr); + ComponentLoader::GetInstance().ParseSinkSupportedFeaturesFromJson(sinkFilters, config); + EXPECT_TRUE(config.sinkSupportedFeatures.empty()); + cJSON_Delete(sinkFilters); + + cJSON *sinkFilters1 = cJSON_CreateArray(); + ASSERT_TRUE(sinkFilters1 != nullptr); + cJSON_AddItemToArray(sinkFilters1, cJSON_CreateString("dcamera_1")); + cJSON_AddItemToArray(sinkFilters1, cJSON_CreateNumber(1)); + ComponentLoader::GetInstance().ParseSinkSupportedFeaturesFromJson(sinkFilters1, config); + EXPECT_FALSE(config.sinkSupportedFeatures.empty()); + cJSON_Delete(sinkFilters1); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp index 95a7cc59..614645cb 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test_ext.cpp @@ -39,6 +39,7 @@ constexpr int32_t CAMERA_UID = CURRENT_DEVICE_UID; constexpr int32_t CAMERA_PID = 4083; constexpr int32_t AUDIO_UID = CURRENT_DEVICE_UID; constexpr int32_t AUDIO_PID = 4085; +constexpr uint16_t DEV_TYPE_TEST = 14; const CompVersion VERSION = { .sinkVersion = "1.0", .sourceVersion = "1.0" }; const DHDescriptor CAMERA_DESCRIPTOR = { .id = "camera_1", .dhType = DHType::CAMERA }; const DHDescriptor AUDIO_DESCRIPTOR = { .id = "audio_1", .dhType = DHType::AUDIO }; @@ -915,5 +916,49 @@ HWTEST_F(ComponentManagerTestExt, WaitForResult_001, testing::ext::TestSize.Leve auto ret = ComponentManager::GetInstance().WaitForResult(ComponentManager::Action::START_SOURCE, actionsResult); EXPECT_EQ(ret, true); } + +HWTEST_F(ComponentManagerTestExt, RecoverAutoEnableSink_001, testing::ext::TestSize.Level0) +{ + DHType dhType = DHType::CAMERA; + std::string udidHash = "udidHash-1"; + std::string dhId = "dhId_1"; + std::string deviceId = "deviceId-1"; + CompVersion compVersion{ .sinkVersion = "1.0" }; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, dhType, "attrs_test", "subtype", udidHash, compVersion); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); + EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverAutoEnableSink(dhType)); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(ComponentManagerTestExt, RecoverAutoEnableSink_002, testing::ext::TestSize.Level0) +{ + DHType dhType = DHType::CAMERA; + std::string udidHash = "udidHash-1"; + std::string dhId = "dhId_1"; + std::string deviceId = "deviceId-1"; + CompVersion compVersion{ .sinkVersion = "1.0" }; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs_test", "subtype", udidHash, compVersion); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); + EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverAutoEnableSink(dhType)); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(ComponentManagerTestExt, RecoverAutoEnableSink_003, testing::ext::TestSize.Level0) +{ + DHType dhType = DHType::CAMERA; + std::string udidHash = "udidHash-1"; + std::string dhId = "dhId_test"; + std::shared_ptr dhMetaCapInfo = nullptr; + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(VALUABLE_DEVICE_INFO)); + EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RecoverAutoEnableSink(dhType)); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn index 47348140..aafb14cd 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn @@ -23,13 +23,18 @@ config("module_private_config") { "${av_trans_path}/common/include", "include", "${innerkits_path}/include", + "${innerkits_path}/include/ipc", "${utils_path}/include", "${common_path}/log/include", "${common_path}/utils/include", "${services_path}/distributedhardwarefwkservice/include", "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/componentmanager", + "${services_path}/distributedhardwarefwkservice/include/transport", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", "${services_path}/distributedhardwarefwkservice/include/task", "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/lowlatency", ] } @@ -41,6 +46,7 @@ ohos_unittest("DistributedHardwareServiceTest") { configs = [ ":module_private_config" ] deps = [ + "${innerkits_path}:libdhfwk_sdk", "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", ] @@ -61,10 +67,12 @@ ohos_unittest("DistributedHardwareServiceTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "dsoftbus:softbus_client", "eventhandler:libeventhandler", "hilog:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", + "samgr:samgr_proxy", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h index 44ebcd93..3ccf2c2f 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,6 +18,8 @@ #include +#include "get_dh_descriptors_callback_stub.h" + namespace OHOS { namespace DistributedHardware { class DistributedHardwareServiceTest : public testing::Test { @@ -26,6 +28,17 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + +public: + class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override; + void OnError(const std::string &networkId, int32_t error) override; + }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp index 8aa627b4..63fcf1e0 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp @@ -22,6 +22,7 @@ #include "constants.h" #include "cJSON.h" #include "component_loader.h" +#include "component_manager.h" #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -64,11 +65,11 @@ void SetUpComponentLoaderConfig() CompHandler handler; handler.compConfig.name = "distributed_audio"; handler.compConfig.type = DHType::AUDIO; - handler.compConfig.compHandlerLoc = "libdistributed_camera_handler.z.so"; + handler.compConfig.compHandlerLoc = "libdistributed_audio_handler.z.so"; handler.compConfig.compHandlerVersion = "1.0"; - handler.compConfig.compSourceLoc = "libdistributed_camera_source_sdk.z.so"; + handler.compConfig.compSourceLoc = "libdistributed_audio_source_sdk.z.so"; handler.compConfig.compSourceVersion = "1.0"; - handler.compConfig.compSinkLoc = "libdistributed_camera_sink_sdk.z.so"; + handler.compConfig.compSinkLoc = "libdistributed_audio_sink_sdk.z.so"; handler.compConfig.compSinkVersion = "2.0"; handler.compConfig.compSinkSaId = TEST_COMP_SINK_SA_ID; handler.compConfig.haveFeature = false; @@ -93,6 +94,21 @@ void SetDownComponentLoaderConfig() } } +void DistributedHardwareServiceTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +void DistributedHardwareServiceTest::TestGetDistributedHardwareCallback::OnError(const std::string &networkId, + int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: register_publisher_listener_001 * @tc.desc: Verify the RegisterPublisherListener function @@ -294,19 +310,22 @@ HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_001, TestSize.L HWTEST_F(DistributedHardwareServiceTest, GetDistributedHardware_001, TestSize.Level1) { DistributedHardwareService service(ASID, true); - - std::string networkId = "111"; + std::string networkId = ""; EnableStep enableStep = EnableStep::ENABLE_SOURCE; auto ret = service.GetDistributedHardware(networkId, enableStep, nullptr); - EXPECT_EQ(ret, DH_FWK_SUCCESS); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); - networkId = "local"; + networkId = "111"; ret = service.GetDistributedHardware(networkId, enableStep, nullptr); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); + + sptr callback(new TestGetDistributedHardwareCallback()); + ret = service.GetDistributedHardware(networkId, enableStep, callback); EXPECT_EQ(ret, DH_FWK_SUCCESS); - networkId = ""; - ret = service.GetDistributedHardware(networkId, enableStep, nullptr); - EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); + networkId = "local"; + ret = service.GetDistributedHardware(networkId, enableStep, callback); + EXPECT_EQ(ret, DH_FWK_SUCCESS); } /** @@ -465,6 +484,31 @@ HWTEST_F(DistributedHardwareServiceTest, DisableSource_001, TestSize.Level1) EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } +HWTEST_F(DistributedHardwareServiceTest, PauseDistributedHardware_003, TestSize.Level1) +{ + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + IDistributedHardwareSink *sinkPtr = nullptr; + ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); + auto ret = service.PauseDistributedHardware(dhType, networkId); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); + ComponentManager::GetInstance().compSink_.clear(); +} + +HWTEST_F(DistributedHardwareServiceTest, PauseDistributedHardware_004, TestSize.Level1) +{ + SetUpComponentLoaderConfig(); + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + ComponentManager::GetInstance().InitCompSink(dhType); + auto ret = service.PauseDistributedHardware(dhType, networkId); + EXPECT_NE(ret, DH_FWK_SUCCESS); + SetDownComponentLoaderConfig(); + ComponentManager::GetInstance().compSink_.clear(); +} + HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_002, TestSize.Level1) { DistributedHardwareService service(ASID, true); @@ -473,6 +517,31 @@ HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_002, TestSize EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } +HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_003, TestSize.Level1) +{ + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + IDistributedHardwareSink *sinkPtr = nullptr; + ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); + auto ret = service.ResumeDistributedHardware(dhType, networkId); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); + ComponentManager::GetInstance().compSink_.clear(); +} + +HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_004, TestSize.Level1) +{ + SetUpComponentLoaderConfig(); + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + ComponentManager::GetInstance().InitCompSink(dhType); + auto ret = service.ResumeDistributedHardware(dhType, networkId); + EXPECT_NE(ret, DH_FWK_SUCCESS); + SetDownComponentLoaderConfig(); + ComponentManager::GetInstance().compSink_.clear(); +} + HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_002, TestSize.Level1) { DistributedHardwareService service(ASID, true); @@ -481,6 +550,31 @@ HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_002, TestSize.L EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); } +HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_003, TestSize.Level1) +{ + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + IDistributedHardwareSink *sinkPtr = nullptr; + ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); + auto ret = service.StopDistributedHardware(dhType, networkId); + EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID); + ComponentManager::GetInstance().compSink_.clear(); +} + +HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_004, TestSize.Level1) +{ + SetUpComponentLoaderConfig(); + DistributedHardwareService service(ASID, true); + std::string networkId = "111"; + DHType dhType = DHType::AUDIO; + ComponentManager::GetInstance().InitCompSink(dhType); + auto ret = service.StopDistributedHardware(dhType, networkId); + EXPECT_NE(ret, DH_FWK_SUCCESS); + SetDownComponentLoaderConfig(); + ComponentManager::GetInstance().compSink_.clear(); +} + HWTEST_F(DistributedHardwareServiceTest, DoBusinessInit_001, TestSize.Level1) { DistributedHardwareService service(ASID, true); diff --git a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/BUILD.gn index c56df971..e2ae6f39 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/BUILD.gn @@ -21,6 +21,7 @@ config("module_private_config") { include_dirs = [ "include", "${common_path}/utils/include", + "${innerkits_path}/include/ipc", "${services_path}/distributedhardwarefwkservice/include/ipc", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/include/hardware_status_listener_proxy_test.h b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/include/hardware_status_listener_proxy_test.h index 8fddb6cd..f194dcfa 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/include/hardware_status_listener_proxy_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/include/hardware_status_listener_proxy_test.h @@ -21,6 +21,8 @@ #include #include "distributed_hardware_errno.h" +#include "get_dh_descriptors_callback_proxy.h" +#include "get_dh_descriptors_callback_stub.h" #include "hardware_status_listener_proxy.h" #include "hardware_status_listener_stub.h" @@ -36,7 +38,7 @@ public: class TestHDSinkStatusListenerStub : public OHOS::DistributedHardware::HDSinkStatusListenerStub { public: - TestHDSinkStatusListenerStub() = default; + TestHDSinkStatusListenerStub() = default; virtual ~TestHDSinkStatusListenerStub() = default; void OnEnable(const DHDescriptor &dhDescriptor) override; void OnDisable(const DHDescriptor &dhDescriptor) override; @@ -45,11 +47,21 @@ public: class TestHDSourceStatusListenerStub : public OHOS::DistributedHardware::HDSourceStatusListenerStub { public: - TestHDSourceStatusListenerStub() = default; + TestHDSourceStatusListenerStub() = default; virtual ~TestHDSourceStatusListenerStub() = default; void OnEnable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; void OnDisable(const std::string &networkId, const DHDescriptor &dhDescriptor) override; }; + + class TestGetDhDescriptorsCallbackStub : public + OHOS::DistributedHardware::GetDhDescriptorsCallbackStub { + public: + TestGetDhDescriptorsCallbackStub() = default; + virtual ~TestGetDhDescriptorsCallbackStub() = default; + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override; + void OnError(const std::string &networkId, int32_t error) override; + }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/src/hardware_status_listener_proxy_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/src/hardware_status_listener_proxy_test.cpp index b362915f..80d042a4 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/src/hardware_status_listener_proxy_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/ipc/hardware_status_listener_proxy/src/hardware_status_listener_proxy_test.cpp @@ -19,6 +19,8 @@ using namespace testing::ext; namespace OHOS { namespace DistributedHardware { +constexpr uint32_t MAX_DH_DESCRIPTOR_ARRAY_SIZE = 4100; + void HardwareStatusListenerProxyTest::SetUpTestCase() { } @@ -59,6 +61,21 @@ void HardwareStatusListenerProxyTest::TestHDSourceStatusListenerStub::OnDisable( (void)dhDescriptor.dhType; } +void HardwareStatusListenerProxyTest::TestGetDhDescriptorsCallbackStub::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +void HardwareStatusListenerProxyTest::TestGetDhDescriptorsCallbackStub::OnError( + const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: OnMessage_001 * @tc.desc: Verify the OnMessage function @@ -130,5 +147,40 @@ HWTEST_F(HardwareStatusListenerProxyTest, OnMessage_102, TestSize.Level1) HDSourceStatusListenerProxy listenerProxy(listenerStub); EXPECT_NO_FATAL_FAILURE(listenerProxy.OnDisable("netWorkId_test", dhDescriptor)); } + +HWTEST_F(HardwareStatusListenerProxyTest, WriteDescriptors_001, TestSize.Level1) +{ + std::vector dhDescriptors; + for (uint32_t i = 1; i < MAX_DH_DESCRIPTOR_ARRAY_SIZE; i++) { + std::string dhId = "camera" + std::to_string(i); + DHDescriptor dhDescriptor { + .id = dhId, + .dhType = DHType::CAMERA + }; + dhDescriptors.push_back(dhDescriptor); + } + MessageParcel data; + sptr callbackStub(new (std::nothrow) TestGetDhDescriptorsCallbackStub()); + ASSERT_TRUE(callbackStub != nullptr); + GetDhDescriptorsCallbackProxy callbackProxy(callbackStub); + auto ret = callbackProxy.WriteDescriptors(data, dhDescriptors); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(HardwareStatusListenerProxyTest, WriteDescriptors_002, TestSize.Level1) +{ + std::vector dhDescriptors; + DHDescriptor dhDescriptor { + .id = "camera_1", + .dhType = DHType::CAMERA + }; + dhDescriptors.push_back(dhDescriptor); + MessageParcel data; + sptr callbackStub(new (std::nothrow) TestGetDhDescriptorsCallbackStub()); + ASSERT_TRUE(callbackStub != nullptr); + GetDhDescriptorsCallbackProxy callbackProxy(callbackStub); + auto ret = callbackProxy.WriteDescriptors(data, dhDescriptors); + EXPECT_EQ(NO_ERROR, ret); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/publisher/src/publisher_item_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/publisher/src/publisher_item_test.cpp index c88de323..7ebecc36 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/publisher/src/publisher_item_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/publisher/src/publisher_item_test.cpp @@ -56,7 +56,8 @@ HWTEST_F(PublisherItemTest, AddListener_001, TestSize.Level1) HWTEST_F(PublisherItemTest, AddListener_002, TestSize.Level1) { PublisherItem item(DHTopic::TOPIC_MIN); - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); item.AddListener(listener); EXPECT_EQ(false, item.listeners_.empty()); } @@ -74,7 +75,8 @@ HWTEST_F(PublisherItemTest, RemoveListener_001, TestSize.Level1) item.RemoveListener(listener); EXPECT_EQ(true, item.listeners_.empty()); - sptr listener1(new MockIPublisherListener()); + sptr listener1(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener1 != nullptr); item.AddListener(listener1); item.RemoveListener(listener1); EXPECT_EQ(true, item.listeners_.empty()); @@ -108,7 +110,8 @@ HWTEST_F(PublisherItemTest, PublishMessage_002, TestSize.Level1) { PublisherItem item(DHTopic::TOPIC_MIN); std::string message = "message"; - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); item.AddListener(listener); item.PublishMessage(message); EXPECT_EQ(false, item.listeners_.empty()); @@ -117,28 +120,32 @@ HWTEST_F(PublisherItemTest, PublishMessage_002, TestSize.Level1) HWTEST_F(PublisherItemTest, RegisterListener_001, TestSize.Level1) { DHTopic topic = DHTopic::TOPIC_MIN; - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); EXPECT_NO_FATAL_FAILURE(Publisher::GetInstance().RegisterListener(topic, listener)); } HWTEST_F(PublisherItemTest, RegisterListener_002, TestSize.Level1) { DHTopic topic = DHTopic::TOPIC_LOW_LATENCY; - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); EXPECT_NO_FATAL_FAILURE(Publisher::GetInstance().RegisterListener(topic, listener)); } HWTEST_F(PublisherItemTest, UnregisterListener_001, TestSize.Level1) { DHTopic topic = DHTopic::TOPIC_MIN; - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); EXPECT_NO_FATAL_FAILURE(Publisher::GetInstance().UnregisterListener(topic, listener)); } HWTEST_F(PublisherItemTest, UnregisterListener_002, TestSize.Level1) { DHTopic topic = DHTopic::TOPIC_LOW_LATENCY; - sptr listener(new MockIPublisherListener()); + sptr listener(new (std::nothrow) MockIPublisherListener()); + ASSERT_TRUE(listener != nullptr); EXPECT_NO_FATAL_FAILURE(Publisher::GetInstance().UnregisterListener(topic, listener)); } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn index 71f2f3b3..c0038af1 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn @@ -22,6 +22,7 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${innerkits_path}/include/ipc", "${services_path}/distributedhardwarefwkservice/include", "${services_path}/distributedhardwarefwkservice/include/resourcemanager", "${services_path}/distributedhardwarefwkservice/include/task", @@ -190,6 +191,7 @@ ohos_unittest("ResourceManagerTest") { ] deps = [ + "${innerkits_path}:libdhfwk_sdk", "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", "${utils_path}:distributedhardwareutils", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp index f5dc7c25..2c541ab6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp @@ -28,6 +28,7 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "dh_utils_tool.h" +#include "get_dh_descriptors_callback_stub.h" #include "task_board.h" #include "impl_utils.h" @@ -102,6 +103,17 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + +public: + class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override; + void OnError(const std::string &networkId, int32_t error) override; + }; }; void ResourceManagerTest::SetUpTestCase(void) @@ -141,6 +153,20 @@ void ResourceManagerTest::TearDown() CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(CAP_INFO_9->GetKey()); } +void ResourceManagerTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +void ResourceManagerTest::TestGetDistributedHardwareCallback::OnError(const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: resource_manager_test_001 * @tc.desc: Verify the CapabilityInfoManager UnInit function. @@ -899,5 +925,18 @@ HWTEST_F(ResourceManagerTest, GetDataByDHType_001, TestSize.Level1) EXPECT_EQ(DH_FWK_SUCCESS, ret); CapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); } + +HWTEST_F(ResourceManagerTest, AsyncGetDistributedHardware_001, TestSize.Level1) +{ + std::string networkId = "networkId_test"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback = nullptr; + ASSERT_NO_FATAL_FAILURE(CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, + enableStep, callback)); + + sptr callback1(new (std::nothrow) TestGetDistributedHardwareCallback()); + ASSERT_NO_FATAL_FAILURE(CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, + enableStep, callback1)); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp index 33e772a0..086f25ef 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp @@ -160,5 +160,104 @@ HWTEST_F(VersionInfoTest, FromJson_VersionInfo_003, TestSize.Level1) cJSON_Delete(jsonObj); EXPECT_TRUE(verInfo.deviceId.empty()); } + +HWTEST_F(VersionInfoTest, FromJsoncompVerContinue_001, TestSize.Level1) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddItemToObject(jsonObj, "test", cJSON_CreateArray()); + CompVersion compVer; + FromJsoncompVerContinue(jsonObj, compVer); + EXPECT_EQ(false, compVer.haveFeature); + cJSON_Delete(jsonObj); +} + +HWTEST_F(VersionInfoTest, FromJsoncompVerContinue_002, TestSize.Level1) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON *srcJsonArr = cJSON_CreateArray(); + if (srcJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToArray(srcJsonArr, cJSON_CreateString("src_feature")); + cJSON_AddItemToArray(srcJsonArr, cJSON_CreateNumber(1)); + cJSON_AddItemToObject(jsonObj, SOURCE_FEATURE_FILTER, srcJsonArr); + cJSON_AddStringToObject(jsonObj, SINK_SUPPORTED_FEATURE, "sink_filter"); + CompVersion compVer; + FromJsoncompVerContinue(jsonObj, compVer); + EXPECT_EQ(true, compVer.haveFeature); + cJSON_Delete(jsonObj); +} + +HWTEST_F(VersionInfoTest, FromJsoncompVerContinue_003, TestSize.Level1) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON *sinkJsonArr = cJSON_CreateArray(); + if (sinkJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToArray(sinkJsonArr, cJSON_CreateString("sink_feature")); + cJSON_AddItemToArray(sinkJsonArr, cJSON_CreateNumber(1)); + cJSON_AddItemToObject(jsonObj, SOURCE_FEATURE_FILTER, sinkJsonArr); + cJSON_AddStringToObject(jsonObj, SINK_SUPPORTED_FEATURE, "src_filter"); + CompVersion compVer; + FromJsoncompVerContinue(jsonObj, compVer); + EXPECT_EQ(true, compVer.haveFeature); + cJSON_Delete(jsonObj); +} + +HWTEST_F(VersionInfoTest, FromJsoncompVerContinue_004, TestSize.Level1) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON *srcJsonArr = cJSON_CreateArray(); + if (srcJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToArray(srcJsonArr, cJSON_CreateString("src_feature")); + cJSON_AddItemToArray(srcJsonArr, cJSON_CreateNumber(1)); + cJSON_AddItemToObject(jsonObj, SOURCE_FEATURE_FILTER, srcJsonArr); + + cJSON *sinkJsonArr = cJSON_CreateArray(); + if (sinkJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToArray(sinkJsonArr, cJSON_CreateString("sink_feature")); + cJSON_AddItemToArray(sinkJsonArr, cJSON_CreateNumber(1)); + cJSON_AddItemToObject(jsonObj, SINK_SUPPORTED_FEATURE, sinkJsonArr); + CompVersion compVer; + FromJsoncompVerContinue(jsonObj, compVer); + EXPECT_EQ(true, compVer.haveFeature); + cJSON_Delete(jsonObj); +} + +HWTEST_F(VersionInfoTest, FromJsoncompVerContinue_005, TestSize.Level1) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON *srcJsonArr = cJSON_CreateArray(); + if (srcJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToObject(jsonObj, SOURCE_FEATURE_FILTER, srcJsonArr); + + cJSON *sinkJsonArr = cJSON_CreateArray(); + if (sinkJsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToObject(jsonObj, SINK_SUPPORTED_FEATURE, sinkJsonArr); + CompVersion compVer; + FromJsoncompVerContinue(jsonObj, compVer); + EXPECT_EQ(true, compVer.haveFeature); + cJSON_Delete(jsonObj); +} } } \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn index 26ab12cf..dce15408 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn @@ -32,6 +32,7 @@ config("module_private_config") { "${services_path}/distributedhardwarefwkservice/include/utils", "${services_path}/distributedhardwarefwkservice/include/task", "${services_path}/distributedhardwarefwkservice/include/transport", + "${services_path}/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include", "${common_path}/utils/include", "${common_path}/log/include", ] @@ -127,6 +128,47 @@ ohos_unittest("DHEnableTaskTest") { ] } +ohos_unittest("OnlineTaskTest") { + module_out_path = module_out_path + + sources = [ + "src/online_task_test.cpp", + "${services_path}/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/mock_dh_utils_tool.cpp", + ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "device_manager:devicemanagersdk", + "eventhandler:libeventhandler", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"OnlineTaskTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + ## UnitTest dh_task_test ohos_unittest("DHTaskTest") { module_out_path = module_out_path @@ -174,6 +216,7 @@ group("dh_task_test") { deps = [ ":DHDisableTaskTest", ":DHEnableTaskTest", + ":OnlineTaskTest", ":DHTaskTest", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/task/src/online_task_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/online_task_test.cpp new file mode 100644 index 00000000..21f9e5cd --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/online_task_test.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_ONLINE_TASK_TEST_H +#define OHOS_DISTRIBUTED_ONLINE_TASK_TEST_H + +#include + +#include "device_manager.h" +#include "dm_device_info.h" + +#include "capability_info_manager.h" +#include "capability_info.h" +#include "dh_utils_tool.h" +#include "dh_context.h" +#include "local_capability_info_manager.h" +#include "meta_info_manager.h" +#include "meta_capability_info.h" +#include "mock_dh_utils_tool.h" +#include "online_task.h" +#include "offline_task.h" +#include "task.h" +#include "task_board.h" + +using namespace testing::ext; +using namespace std; +using namespace testing; + +namespace OHOS { +namespace DistributedHardware { +namespace { + const std::string NETWORKID_TEST = "111111"; + const std::string UUID_TEST = "222222"; + const std::string UDID_TEST = "333333"; + const std::string DHID_TEST = "audio_1"; + const std::string DEV_NAME_TEST = "phone"; + const DHType DH_TYPE_TEST = DHType::AUDIO; + const uint16_t DEV_TYPE_TEST = 14; +} + +class OnlineTaskTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + +private: + std::shared_ptr utilTool_; +}; + +void OnlineTaskTest::SetUpTestCase() +{ +} + +void OnlineTaskTest::TearDownTestCase() +{ +} + +void OnlineTaskTest::SetUp() +{ + auto utilTool = IDHUtilTool::GetOrCtreateInstance(); + utilTool_ = std::static_pointer_cast(utilTool); +} + +void OnlineTaskTest::TearDown() +{ + IDHUtilTool::ReleaseInstance(); + utilTool_ = nullptr; +} + +HWTEST_F(OnlineTaskTest, CreateEnableTask_001, TestSize.Level1) +{ + OnLineTask onlineTask(NETWORKID_TEST, UUID_TEST, UDID_TEST, DHID_TEST, DH_TYPE_TEST); + std::string deviceId = Sha256(UUID_TEST); + std::string udidHash = Sha256(UDID_TEST); + std::string capinfoKey = deviceId + RESOURCE_SEPARATOR + DHID_TEST; + std::string metaInfoKey = udidHash + RESOURCE_SEPARATOR + DHID_TEST; + std::shared_ptr capPtr = nullptr; + std::shared_ptr metaCapPtr = nullptr; + CapabilityInfoManager::GetInstance()->globalCapInfoMap_[capinfoKey] = capPtr; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[capinfoKey] = capPtr; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[metaInfoKey] = metaCapPtr; + ASSERT_NO_FATAL_FAILURE(onlineTask.CreateEnableTask()); +} + +HWTEST_F(OnlineTaskTest, CreateEnableSinkTask_001, TestSize.Level1) +{ + OnLineTask onlineTask(NETWORKID_TEST, UUID_TEST, UDID_TEST, DHID_TEST, DH_TYPE_TEST); + std::string deviceId = Sha256(UUID_TEST); + std::string udidHash = Sha256(UDID_TEST); + std::string metaInfoKey = udidHash + RESOURCE_SEPARATOR + DHID_TEST; + std::shared_ptr metaCapPtr = nullptr; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[metaInfoKey] = metaCapPtr; + DeviceInfo deviceInfo(NETWORKID_TEST, UUID_TEST, deviceId, UDID_TEST, udidHash, DEV_NAME_TEST, DEV_TYPE_TEST); + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(deviceInfo)); + ASSERT_NO_FATAL_FAILURE(onlineTask.CreateEnableSinkTask()); +} + +HWTEST_F(OnlineTaskTest, CreateDisableTask_001, TestSize.Level1) +{ + OffLineTask offlineTask(NETWORKID_TEST, UUID_TEST, UDID_TEST, DHID_TEST, DH_TYPE_TEST); + DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, NETWORKID_TEST); + std::string deviceId = Sha256(UUID_TEST); + std::string udidHash = Sha256(UDID_TEST); + std::string capinfoKey = deviceId + RESOURCE_SEPARATOR + DHID_TEST; + std::string metaInfoKey = udidHash + RESOURCE_SEPARATOR + DHID_TEST; + std::shared_ptr capPtr = nullptr; + std::shared_ptr metaCapPtr = nullptr; + CapabilityInfoManager::GetInstance()->globalCapInfoMap_[capinfoKey] = capPtr; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[metaInfoKey] = metaCapPtr; + ASSERT_NO_FATAL_FAILURE(offlineTask.CreateDisableTask()); +} + +HWTEST_F(OnlineTaskTest, CreateDisableSinkTask_001, TestSize.Level1) +{ + OffLineTask offlineTask(NETWORKID_TEST, UUID_TEST, UDID_TEST, DHID_TEST, DH_TYPE_TEST); + std::string deviceId = Sha256(UUID_TEST); + std::string udidHash = Sha256(UDID_TEST); + std::string metaInfoKey = udidHash + RESOURCE_SEPARATOR + DHID_TEST; + std::shared_ptr metaCapPtr = nullptr; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[metaInfoKey] = metaCapPtr; + DeviceInfo deviceInfo(NETWORKID_TEST, UUID_TEST, deviceId, UDID_TEST, udidHash, DEV_NAME_TEST, DEV_TYPE_TEST); + EXPECT_CALL(*utilTool_, GetLocalDeviceInfo()).WillRepeatedly(Return(deviceInfo)); + ASSERT_NO_FATAL_FAILURE(offlineTask.CreateDisableSinkTask()); +} + +HWTEST_F(OnlineTaskTest, DumpAllTasks_001, TestSize.Level1) +{ + std::vector taskInfos; + std::string taskId = "task_001"; + std::shared_ptr task = nullptr; + TaskBoard::GetInstance().tasks_.emplace(taskId, task); + TaskBoard::GetInstance().DumpAllTasks(taskInfos); + ASSERT_TRUE(taskInfos.empty()); + TaskBoard::GetInstance().tasks_.clear(); +} + +HWTEST_F(OnlineTaskTest, IsAllDisableTaskFinish_001, TestSize.Level1) +{ + std::string taskId = "task_001"; + std::shared_ptr task = nullptr; + TaskBoard::GetInstance().tasks_.emplace(taskId, task); + auto ret = TaskBoard::GetInstance().IsAllDisableTaskFinish(); + EXPECT_EQ(ret, true); + TaskBoard::GetInstance().tasks_.clear(); +} + +HWTEST_F(OnlineTaskTest, IsAllDisableTaskFinish_002, TestSize.Level1) +{ + std::string taskId = "task_002"; + std::shared_ptr onlineTask = std::make_shared("networkId", "uuid", "udid", "dhId", DHType::AUDIO); + TaskBoard::GetInstance().tasks_.emplace(taskId, onlineTask); + auto ret = TaskBoard::GetInstance().IsAllDisableTaskFinish(); + EXPECT_EQ(ret, true); + TaskBoard::GetInstance().tasks_.clear(); +} +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 7c7f2852..50366137 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -27,7 +27,7 @@ namespace DistributedHardware { /** * return current time in millisecond. */ -int64_t GetCurrentTime(); +uint64_t GetCurrentTime(); /** * return a random string id. diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index b13ba102..fb0d9bfe 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -55,7 +55,7 @@ namespace { constexpr const char *PARAM_KEY_OS_TYPE = "OS_TYPE"; } -int64_t GetCurrentTime() +uint64_t GetCurrentTime() { struct timeval tv { 0 -- Gitee