diff --git a/bundle.json b/bundle.json index 857d7149db444ad19f992af160f204cdd3f6f4a5..652bc657a83cbc51e9e020006ce8cb5d542219a6 100644 --- a/bundle.json +++ b/bundle.json @@ -63,7 +63,6 @@ "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/test/unittest:kit_test", "//foundation/distributedhardware/distributed_hardware_fwk/utils/test/fuzztest:fuzztest", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest:fuzztest", - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest:fuzztest", "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/test/fuzztest:fuzztest" ] } diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a0c0009eed2237a250d1f6d827e7180c1934d187..224e6be5f04acc5bf6b4515eb4cdc07e46861ec5 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -60,6 +60,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY = -10410; constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; constexpr int32_t ERR_DH_FWK_RESOURCE_DEVICE_ID_NOT_EXIST = -10412; + constexpr int32_t ERR_DH_FWK_RESOURCE_UUID_NOT_FOUND = -10413; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn index 96513b9cc101f376236fd37a11e681604fc785bd..332e4afa7271757f0845b454924b4e0103057581 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/BUILD.gn @@ -36,10 +36,7 @@ config("module_private_config") { ohos_unittest("DistributedHardwareFwkKitTest") { module_out_path = module_out_path - sources = [ - "src/distributed_hardware_fwk_kit_test.cpp", - "src/start_dhfwk_tool.cpp", - ] + sources = [ "src/distributed_hardware_fwk_kit_test.cpp" ] configs = [ ":module_private_config" ] diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h index c0565933fa3bec4316016b9918b940b090d8988e..2ab039f22e2f935fe1b124a1c13fd391ff0403c5 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/distributed_hardware_fwk_kit_test.h @@ -27,8 +27,6 @@ #include "publisher_listener_stub.h" #include "system_ability_load_callback_stub.h" -#include "start_dhfwk_tool.h" - using OHOS::DistributedHardware::DHTopic; namespace OHOS { @@ -54,7 +52,6 @@ public: public: std::shared_ptr dhfwkPtr_ = nullptr; - std::shared_ptr startDHFWKToolPtr_ = nullptr; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp index e3fb029c0decf23643f67434f5b670d73e204da1..ba667792bd226da12443a2d511674edfa43bf8a5 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp +++ b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/distributed_hardware_fwk_kit_test.cpp @@ -26,7 +26,6 @@ #include "distributed_hardware_fwk_kit.h" #include "distributed_hardware_log.h" #include "distributed_hardware_errno.h" -#include "start_dhfwk_tool.h" using OHOS::DistributedHardware::DHTopic; @@ -39,14 +38,11 @@ namespace { { DHTopic::TOPIC_STOP_DSCREEN, "Stop DScreen" }, { DHTopic::TOPIC_DEV_OFFLINE, "Dev Offline" } }; - const int32_t TOPIC_MSG_CNT = 100; const std::string DHARDWARE_PROC_NAME = "dhardware"; - const int32_t SA_EXIT_SLEEP_TIME = 1000; } void DistributedHardwareFwkKitTest::SetUp() { dhfwkPtr_ = std::make_shared(); - startDHFWKToolPtr_ = std::make_shared(); } void DistributedHardwareFwkKitTest::TearDown() @@ -55,11 +51,6 @@ void DistributedHardwareFwkKitTest::TearDown() dhfwkPtr_.reset(); dhfwkPtr_ = nullptr; } - - if (startDHFWKToolPtr_ != nullptr) { - startDHFWKToolPtr_.reset(); - startDHFWKToolPtr_ = nullptr; - } } void DistributedHardwareFwkKitTest::SetUpTestCase() @@ -112,90 +103,5 @@ HWTEST_F(DistributedHardwareFwkKitTest, RegisterListener01, testing::ext::TestSi ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener4); EXPECT_EQ(DH_FWK_SUCCESS, ret); } - -HWTEST_F(DistributedHardwareFwkKitTest, PublishMessage01, testing::ext::TestSize.Level0) -{ - EXPECT_EQ(true, startDHFWKToolPtr_->StartDHFWK()); - - sptr listener1 = new TestPublisherListener(); - int32_t ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener1); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - sptr listener2 = new TestPublisherListener(); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener2); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - sptr listener3 = new TestPublisherListener(); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener3); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - sptr listener4 = new TestPublisherListener(); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener4); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - int32_t cnt = 0; - while (cnt < TOPIC_MSG_CNT) { - for (const auto &tpMsg : TOPIC_MSGS) { - ret = dhfwkPtr_->PublishMessage(tpMsg.first, tpMsg.second); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - } - cnt++; - } - - EXPECT_EQ(TOPIC_MSG_CNT, listener1->GetTopicMsgCnt(DHTopic::TOPIC_START_DSCREEN)); - EXPECT_EQ(TOPIC_MSG_CNT, listener2->GetTopicMsgCnt(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO)); - EXPECT_EQ(TOPIC_MSG_CNT, listener3->GetTopicMsgCnt(DHTopic::TOPIC_STOP_DSCREEN)); - EXPECT_EQ(TOPIC_MSG_CNT, listener4->GetTopicMsgCnt(DHTopic::TOPIC_DEV_OFFLINE)); - - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener1); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener2); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener3); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener4); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - startDHFWKToolPtr_->KillProcess(DHARDWARE_PROC_NAME.c_str()); - std::this_thread::sleep_for(std::chrono::milliseconds(SA_EXIT_SLEEP_TIME)); -} - -HWTEST_F(DistributedHardwareFwkKitTest, PublishMessage02, testing::ext::TestSize.Level0) -{ - EXPECT_EQ(true, startDHFWKToolPtr_->StartDHFWK()); - - sptr listener = new TestPublisherListener(); - int32_t ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - int32_t cnt = 0; - while (cnt < TOPIC_MSG_CNT) { - for (const auto &tpMsg : TOPIC_MSGS) { - ret = dhfwkPtr_->PublishMessage(tpMsg.first, tpMsg.second); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - } - cnt++; - } - - EXPECT_EQ(TOPIC_MSG_CNT, listener->GetTopicMsgCnt(DHTopic::TOPIC_START_DSCREEN)); - EXPECT_EQ(TOPIC_MSG_CNT, listener->GetTopicMsgCnt(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO)); - EXPECT_EQ(TOPIC_MSG_CNT, listener->GetTopicMsgCnt(DHTopic::TOPIC_STOP_DSCREEN)); - EXPECT_EQ(TOPIC_MSG_CNT, listener->GetTopicMsgCnt(DHTopic::TOPIC_DEV_OFFLINE)); - - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = dhfwkPtr_->UnregisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - startDHFWKToolPtr_->KillProcess(DHARDWARE_PROC_NAME.c_str()); - std::this_thread::sleep_for(std::chrono::milliseconds(SA_EXIT_SLEEP_TIME)); -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/start_dhfwk_tool.cpp b/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/start_dhfwk_tool.cpp deleted file mode 100644 index a7a2ddaee88e74be29891154bba5a8f505614e3d..0000000000000000000000000000000000000000 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/src/start_dhfwk_tool.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (c) 2022 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. - */ - -#include "start_dhfwk_tool.h" - -#include -#include -#include -#include -#include -#include - -#include "iservice_registry.h" -#include "system_ability_definition.h" - -#include "distributed_hardware_log.h" - -#undef DH_LOG_TAG -#define DH_LOG_TAG "DHFWKKitTest" - -namespace OHOS { -namespace DistributedHardware { -namespace { - const int32_t LOAD_SA_TIMEOUT_MS = 1000; -} -bool StartDHFWKTool::StartDHFWK() -{ - DHLOGI("Try Start DHFWK sa"); - sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!samgr) { - DHLOGE("Get SystemAbilityManager failed"); - return false; - } - - sptr loadCallback = new LoadDHFWKSACallback(this); - int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, loadCallback); - if (ret != ERR_OK) { - DHLOGE("Failed Load DHFWK system ability, ret: %" PRId32, ret); - return false; - } - - std::unique_lock lock(mutex_); - DHLOGI("Wait DHFWK sa to start"); - auto waitStatus = loadConVar_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), - [this]() { return this->isDHFWKStart_.load(); }); - if (!waitStatus) { - DHLOGE("DHFWK load sa timeout."); - return false; - } - - DHLOGI("Start DHFWK sa success"); - return true; -} - -void StartDHFWKTool::KillProcess(const char *processname) -{ - if (processname == nullptr) { - DHLOGE("process name invalid"); - return; - } - - char cmd[128] = ""; - int32_t ret = sprintf_s(cmd, sizeof(cmd), "kill -9 $(pidof %s)", processname); - if (ret < 0) { - DHLOGE("Kill Process error, cmd: %s, ret: " PRId32, cmd, ret); - return; - } - - system(cmd); -} - -StartDHFWKTool::LoadDHFWKSACallback::LoadDHFWKSACallback(StartDHFWKTool *toolPtr) -{ - this->toolPtr_ = toolPtr; -} - -void StartDHFWKTool::LoadDHFWKSACallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, - const sptr &remoteObject) -{ - if (toolPtr_ == nullptr) { - return; - } - - std::unique_lock lock(toolPtr_->mutex_); - DHLOGI("DHFWK SA Started."); - toolPtr_->isDHFWKStart_ = true; -} - -void StartDHFWKTool::LoadDHFWKSACallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) -{ - if (toolPtr_ == nullptr) { - return; - } - - std::unique_lock lock(toolPtr_->mutex_); - DHLOGI("DHFWK SA Stopped."); - toolPtr_->isDHFWKStart_ = false; -} -} // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 302c11fdd8807fdca269f26f276a8c1eb09b7667..dc1165409c4400ef848a9044fb0d8694f6dd9fe8 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -75,7 +75,6 @@ private: int32_t GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType, std::string &sinkVersion); void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo); - void UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions); sptr GetRemoteDHMS(const std::string &networkId) const; private: diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h index 3871740d12bbe6b555b34e0f294d7720dfce1160..d31dc2b547590aafaa54ea9a9123554d01cfe789 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h @@ -52,6 +52,7 @@ public: int32_t AddVersion(const VersionInfo &versionInfo); int32_t GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo); + int32_t RemoveVersionInfoByDeviceId(const std::string &deviceId); int32_t SyncVersionInfoFromDB(const std::string &deviceId); int32_t SyncRemoteVersionInfos(); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 6dde137dfb96d8f4c76063507fe529b06a74fa1b..70453a31158b55ea6b8fa526ef5c668a929469e9 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -416,20 +416,6 @@ void ComponentManager::UpdateVersionCache(const std::string &uuid, const Version VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } -void ComponentManager::UpdateVersionCache(const std::string &uuid, - const std::unordered_map &versions) -{ - DHVersion dhVersion; - dhVersion.uuid = uuid; - for (const auto &versionPair : versions) { - CompVersion compVersion; - compVersion.dhType = versionPair.first; - compVersion.sinkVersion = versionPair.second; - dhVersion.compVersions.insert(std::pair(compVersion.dhType, compVersion)); - } - VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); -} - sptr ComponentManager::GetRemoteDHMS(const std::string &networkId) const { DHLOGI("start, networkId = %s", GetAnonyString(networkId).c_str()); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp index 28907dc453039f16ec024887236e991e69377d9e..d9e3d109e878e311e531fcb20050d1ce3460c35c 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -134,6 +134,31 @@ void VersionInfoManager::UpdateVersionCache(const VersionInfo &versionInfo) VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } +int32_t VersionInfoManager::RemoveVersionInfoByDeviceId(const std::string &deviceId) +{ + DHLOGI("Remove version device info, key: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + if (dbAdapterPtr_->RemoveDataByKey(deviceId) != DH_FWK_SUCCESS) { + DHLOGE("Remove version info failed, key: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_UUID_NOT_FOUND; + } + DHLOGI("Delete version ,uuid: %s", GetAnonyString(uuid).c_str()); + VersionManager::GetInstance().RemoveDHVersion(uuid); + + return DH_FWK_SUCCESS; +} + int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) { DHLOGI("Sync versionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); @@ -293,4 +318,4 @@ void VersionInfoManager::OnEvent(VersionInfoEvent &ev) } } } // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn index 8a29f0b3847a94867b2d667da3a4ff3749e22b16..8de46bca31df81236d969bf394a93d242a3d96ba 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -16,7 +16,10 @@ group("fuzztest") { deps = [ "accessmanager_fuzzer:fuzztest", + "componentloader_fuzzer:fuzztest", "componentmanager_fuzzer:fuzztest", "resourcemanager_fuzzer:fuzztest", + "task_fuzzer:fuzztest", + "versioninfomanager_fuzzer:fuzztest", ] } diff --git a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/BUILD.gn index 1190743c1dd35e28c5e8df380533b59da77593db..06ac5f649618e60e91bf4737dad8699dd37f707b 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp index d91c89b152ae5f6a695bf60db74ef65cea665ce9..68c7338674b79de73e307cdb94815e1c85c899b8 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.h index dbbf2403121dccb375425db8c5eafb7ffa87f29a..6792be5bcd55e4994d7cbb53553eb0e77f16f0f2 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.h +++ b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/project.xml index 85e7ef2c1cc6471e288306f6e3dcea5287a78b0e..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/project.xml +++ b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/project.xml @@ -1,5 +1,5 @@ - + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/BUILD.gn index d7e6d1d7709cf67198a5b8eb8ff65a6c3e6d9993..da1d72f0d3f815a1005e764f6e215578778278f1 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.cpp index 31ca68e45e8a9380e04449b8f2973caec30917f7..c05bf82249b775610e6f40a0185c771ee64ef651 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.h index c69a7db9769f3a9e8a2e7967a0fd54e4fce2a041..bc78043591d8f67de8eee6799dd6d5fc9c9e572d 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.h +++ b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/componentmanager_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/project.xml index 85e7ef2c1cc6471e288306f6e3dcea5287a78b0e..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/project.xml +++ b/services/distributedhardwarefwkservice/test/fuzztest/componentmanager_fuzzer/project.xml @@ -1,5 +1,5 @@ - + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea875f80bf48473c4a106f1f20368e56ef87fe35 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "task_fuzzer.h" + +#include +#include + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "task_board.h" +#include "task_executor.h" +#include "task_factory.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const uint32_t DH_TYPE_SIZE = 10; + const DHType dhTypeFuzz[DH_TYPE_SIZE] = { + DHType::CAMERA, DHType::AUDIO, DHType::DISPLAY, DHType::VIRMODEM_MIC, + DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER + }; + + const uint32_t TASK_TYPE_SIZE = 5; + const TaskType taskTypeFuzz[TASK_TYPE_SIZE] = { + TaskType::UNKNOWN, TaskType::ENABLE, TaskType::DISABLE, TaskType::ON_LINE, TaskType::OFF_LINE + }; +} + +void TaskFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string networkId(reinterpret_cast(data), size); + std::string uuid(reinterpret_cast(data), size); + std::string dhId(reinterpret_cast(data), size); + DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE]; + + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .dhId = dhId, + .dhType = dhType + }; + TaskType taskType = taskTypeFuzz[data[0] % TASK_TYPE_SIZE]; + + auto task = TaskFactory::GetInstance().CreateTask(taskType, taskParam, nullptr); + if (task != nullptr) { + TaskBoard::GetInstance().RemoveTask(task->GetId()); + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::TaskFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..bc78043591d8f67de8eee6799dd6d5fc9c9e572d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 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 TEST_COMPONENTMANAGER_FUZZER_H +#define TEST_COMPONENTMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "componentmanager_fuzzer" + +#endif diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7a1c5a306293bd6e5e49ae7ab471558e0fdfadff --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2022 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("VersioninfoManagerFuzzTest") { + module_out_path = "distributed_hardware_fwk/versioninfomanager" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "versioninfomanager_fuzzer.cpp" ] + + deps = [ + "${utils_path}:distributedhardwareutils", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk:system_ability_fwk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"VersioninfoManagerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":VersioninfoManagerFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..340d9e0b4ef16cc80e133b3d1012d0b5289ee1fc --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "versioninfomanager_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "version_info_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_manager_factory.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; + const uint32_t DH_TYPE_SIZE = 10; + const DHType dhTypeFuzz[DH_TYPE_SIZE] = { + DHType::CAMERA, DHType::AUDIO, DHType::DISPLAY, DHType::VIRMODEM_MIC, + DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER + }; +} + +void VersioninfoManagerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + VersionInfo versionInfo; + versionInfo.deviceId = std::string(reinterpret_cast(data), size); + versionInfo.dhVersion = std::string(reinterpret_cast(data), size); + + CompVersion compVer; + compVer.dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE]; + compVer.name = std::string(reinterpret_cast(data), size); + compVer.handlerVersion = std::string(reinterpret_cast(data), size); + compVer.sourceVersion = std::string(reinterpret_cast(data), size); + compVer.sinkVersion = std::string(reinterpret_cast(data), size); + versionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); + + VersionInfo info; + VersionInfoManager::GetInstance()->AddVersion(versionInfo); + VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(versionInfo.deviceId, info); + VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(versionInfo.deviceId); + VersionInfoManager::GetInstance()->RemoveVersionInfoByDeviceId(versionInfo.deviceId); + + usleep(SLEEP_TIME_US); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::VersioninfoManagerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..241f550d51ed0b58f29965ea58ea4b88609ee5f2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 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 TEST_ACCESSMANAGER_FUZZER_H +#define TEST_ACCESSMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "versioninfomanager_fuzzer" + +#endif + diff --git a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn index 6f86118882a9a6549fee5d57d72c6bfc8b4f9358..7204caa3fa8bf6e96fc5d61cbdd6cccd57ab2365 100644 --- a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -19,8 +19,11 @@ group("test") { "common/componentloader:component_loader_test", "common/componentmanager:component_manager_test", "common/distributedhardwaremanager:distributed_hardware_manager_test", + "common/distributedhardwareservice:distributed_hardware_service_test", + "common/localhardwaremanager:local_hardware_manager_test", "common/resourcemanager:resource_manager_test", "common/task:dh_task_test", + "common/versioninfomanager:versioninfo_manager_test", "common/versionmanager:version_manager_test", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp index a42e67448454fc74fdc693868ff09798aeb6c4cc..8f732ff7645afa61f01227a26c268159cebdc3fb 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp @@ -225,5 +225,27 @@ HWTEST_F(AccessManagerTest, SendOffLineEvent_003, TestSize.Level0) for_each(threadVec.begin(), threadVec.end(), [](std::thread &t) { t.join(); }); } + +/** + * @tc.name: AccessManagerInit + * @tc.desc: Verify the Init function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(AccessManagerTest, AccessManagerInit, TestSize.Level0) +{ + EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init()); +} + +/** + * @tc.name: AccessManagerUnInit + * @tc.desc: Verify the Init UnInit function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(AccessManagerTest, AccessManagerUnInit, TestSize.Level0) +{ + EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->UnInit()); +} } // namespace DistributedHardware } // namespace OHOS 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 6e7c858910da915d3b6766544a661100ac7e5cdb..7838556d0c6294349c6a13ea90677f705f28f816 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 @@ -73,7 +73,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_002, TestSize.Level0) } /** - * @tc.name: component_loader_test_004 + * @tc.name: component_loader_test_003 * @tc.desc: Verify the GetSource function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -89,7 +89,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_003, TestSize.Level0) } /** - * @tc.name: component_loader_test_005 + * @tc.name: component_loader_test_004 * @tc.desc: Verify the GetSink function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -105,7 +105,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_004, TestSize.Level0) } /** - * @tc.name: component_loader_test_006 + * @tc.name: component_loader_test_005 * @tc.desc: Verify the ReleaseHardwareHandler function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -120,7 +120,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_005, TestSize.Level0) } /** - * @tc.name: component_loader_test_007 + * @tc.name: component_loader_test_006 * @tc.desc: Verify the ReleaseSource function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -135,7 +135,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_006, TestSize.Level0) } /** - * @tc.name: component_loader_test_008 + * @tc.name: component_loader_test_007 * @tc.desc: Verify the ReleaseSink function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -148,5 +148,17 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_007, TestSize.Level0) EXPECT_TRUE(ComponentLoader::GetInstance().compHandlerMap_[iter.first].sinkHandler == nullptr); } } + +/** + * @tc.name: component_loader_test_008 + * @tc.desc: Verify the GetAllCompTypes function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(ComponentLoaderTest, component_loader_test_008, TestSize.Level0) +{ + auto vec = ComponentLoader::GetInstance().GetAllCompTypes(); + EXPECT_EQ(vec.size(), ComponentLoader::GetInstance().compHandlerMap_.size()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn index 41ef7950e4c61b28ead2b1fb2dc3ce49153a8f8e..0fe794d654933c65cde9cc829eb5081b07da113c 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn @@ -23,9 +23,12 @@ config("module_private_config") { "include", "${utils_path}/include", "${utils_path}/include/log", + "${utils_path}/include/eventbus", "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/componentloader", "${services_path}/distributedhardwarefwkservice/include/componentmanager", "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", @@ -55,8 +58,10 @@ ohos_unittest("ComponentManagerTest") { ] external_deps = [ + "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", "ipc:ipc_core", + "kv_store:distributeddata_inner", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp index 6d0d96ce597a4842a0b32808eb171434ad92762c..591b9a7fe14d810c359253b663ffc22cb466ba56 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -16,6 +16,9 @@ #include "component_manager_test.h" #include +#include +#include +#include #include #include @@ -23,14 +26,21 @@ #include "component_disable.h" #include "component_enable.h" +#include "component_loader.h" +#include "capability_info.h" +#include "capability_info_manager.h" #define private public #include "component_manager.h" #undef private #include "constants.h" +#include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "mock_idistributed_hardware_sink.h" #include "mock_idistributed_hardware_source.h" +#include "version_info_manager.h" +#include "version_manager.h" using namespace testing::ext; @@ -39,13 +49,39 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "ComponentManagerTest" +namespace { constexpr int32_t EXECUTE_TIME_TEST = 1000; +constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; +const std::string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const std::string NAME_CAMERA = "distributed_camera"; +const std::string VERSION_1 = "1.0"; const std::string DEV_ID_TEST = "123456"; const std::string DH_ID_TEST = "Camera_0"; +const std::string NETWORK_TEST = "nt36a637105409e904d4da83790a4a8"; +const std::string UUID_TEST = "bb536a637105409e904d4da78290ab1"; +const std::string DH_ATTR_1 = "attr1"; +const std::string DEV_NAME = "Dev1"; +const std::string DH_ID_1 = "Camera_1"; +const std::shared_ptr CAP_INFO_1 = + std::make_shared(DH_ID_1, GetDeviceIdByUUID(UUID_TEST), DEV_NAME, + TEST_DEV_TYPE_PAD, DHType::CAMERA, DH_ATTR_1); +} -void ComponentManagerTest::SetUpTestCase(void) {} +void ComponentManagerTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} -void ComponentManagerTest::TearDownTestCase(void) {} +void ComponentManagerTest::TearDownTestCase(void) +{ + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} void ComponentManagerTest::SetUp() { @@ -339,5 +375,94 @@ HWTEST_F(ComponentManagerTest, disable_test_004, TestSize.Level0) thread5.join(); thread6.join(); } + +/** + * @tc.name: init_compSource_test_001 + * @tc.desc: Verify the InitCompSource + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, init_compSource_test_001, TestSize.Level0) +{ + ComponentLoader::GetInstance().Init(); + ComponentManager::GetInstance().compSource_.clear(); + auto ret = ComponentManager::GetInstance().InitCompSource(); + EXPECT_NE(ret, ComponentManager::GetInstance().compSource_.empty()); +} + +/** + * @tc.name: init_compSink_test_001 + * @tc.desc: Verify the InitCompSource + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, init_compSink_test_001, TestSize.Level0) +{ + ComponentLoader::GetInstance().Init(); + ComponentManager::GetInstance().compSink_.clear(); + auto ret = ComponentManager::GetInstance().InitCompSink(); + EXPECT_NE(ret, ComponentManager::GetInstance().compSink_.empty()); +} + +/** + * @tc.name: get_enableparam_test_001 + * @tc.desc: Verify the GetEnableParam + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, get_enableparam_test_001, TestSize.Level0) +{ + DHContext::GetInstance().AddOnlineDevice(NETWORK_TEST, UUID_TEST); + + CapabilityInfoManager::GetInstance()->Init(); + std::vector> resInfos { CAP_INFO_1 }; + CapabilityInfoManager::GetInstance()->AddCapability(resInfos); + + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + VersionInfo verInfo1; + verInfo1.deviceId = GetDeviceIdByUUID(UUID_TEST); + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionInfoManager::GetInstance()->Init(); + VersionInfoManager::GetInstance()->AddVersion(verInfo1); + + EnableParam param; + auto ret = ComponentManager::GetInstance().GetEnableParam(NETWORK_TEST, UUID_TEST, + DH_ID_1, DHType::CAMERA, param); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: get_sinkversionfromvermgr_test_001 + * @tc.desc: Verify the GetSinkVersionFromVerMgr + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, get_sinkversion_fromvermgr_test_001, TestSize.Level0) +{ + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + DHVersion dhVersion; + dhVersion.uuid = UUID_TEST; + dhVersion.dhVersion = VERSION_1; + dhVersion.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionManager::GetInstance().AddDHVersion(UUID_TEST, dhVersion); + std::string sinkVersion; + auto ret = ComponentManager::GetInstance().GetSinkVersionFromVerMgr(UUID_TEST, DHType::CAMERA, sinkVersion); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..543fd5cc9b0573a288383eb831774a6f7ca67f0a --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn @@ -0,0 +1,76 @@ +# Copyright (c) 2022 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/distributed_hardware_service_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${common_path}/log/include", + "${common_path}/utils/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/utils", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + ] +} + +ohos_unittest("DistributedHardwareServiceTest") { + module_out_path = module_out_path + + sources = [ "src/distributed_hardware_service_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DistributedHardwareServiceTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("distributed_hardware_service_test") { + testonly = true + deps = [ ":DistributedHardwareServiceTest" ] +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..44ebcd936c51cb59dbe15293ff711e2f66df8b24 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 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_HARDWARE_SERVICE_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_SERVICE_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DistributedHardwareServiceTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/start_dhfwk_tool.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h similarity index 46% rename from interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/start_dhfwk_tool.h rename to services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h index b3645df3262cc3dc0d3465b97d2da39643ad6d95..985317c0e3e7ca3422ae72a6f77102cbdfa5183f 100644 --- a/interfaces/inner_kits/test/unittest/common/distributedhardwarefwkkit/include/start_dhfwk_tool.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h @@ -13,36 +13,28 @@ * limitations under the License. */ -#ifndef OHOS_START_DHFWK_TOOL_H -#define OHOS_START_DHFWK_TOOL_H +#ifndef OHOS_DISTRIBUTED_MOCK_PUBLISHER_LISTENER_H +#define OHOS_DISTRIBUTED_MOCK_PUBLISHER_LISTENER_H -#include -#include -#include - -#include "system_ability_load_callback_stub.h" +#include "ipublisher_listener.h" namespace OHOS { namespace DistributedHardware { -class StartDHFWKTool { -public: - bool StartDHFWK(); - void KillProcess(const char *processname); +class MockPublisherListener : public IPublisherListener { public: - class LoadDHFWKSACallback : public SystemAbilityLoadCallbackStub { - public: - explicit LoadDHFWKSACallback(StartDHFWKTool *toolPtr); - void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; - void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; - private: - StartDHFWKTool *toolPtr_ = nullptr; - }; + MockPublisherListener() = default; + ~MockPublisherListener() = default; + void OnMessage(const DHTopic topic, const std::string& message) + { + (void) topic; + (void) message; + } -private: - std::atomic isDHFWKStart_ = false; - std::condition_variable loadConVar_; - std::mutex mutex_; + sptr AsObject() + { + return nullptr; + } }; } // namespace DistributedHardware } // namespace OHOS -#endif // OHOS_START_DHFWK_TOOL_H \ No newline at end of file +#endif \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..2e6e5fe95a7b5b57ef20d6d45faef12d91485147 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "distributed_hardware_service_test.h" + +#include +#include +#include + +#include "constants.h" +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#define protected public +#define private public +#include "distributed_hardware_service.h" +#include "distributed_hardware_manager.h" +#include "task_board.h" +#undef private +#undef protected +#include "mock_publisher_listener.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { + const int32_t ASID = 4801; + const DHTopic TOPIC = DHTopic::TOPIC_START_DSCREEN; + sptr g_listener = sptr(new MockPublisherListener()); + + const std::u16string ARGS_H = u"-h"; + const std::u16string ARGS_L = u"-l"; + const std::u16string ARGS_E = u"-e"; + const std::u16string ARGS_T = u"-t"; + const std::u16string ARGS_C = u"-c"; +} +void DistributedHardwareServiceTest::SetUpTestCase(void) {} + +void DistributedHardwareServiceTest::TearDownTestCase(void) {} + +void DistributedHardwareServiceTest::SetUp() {} + +void DistributedHardwareServiceTest::TearDown() {} + +/** + * @tc.name: register_publisher_listener_001 + * @tc.desc: Verify the RegisterPublisherListener function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + auto ret = service.RegisterPublisherListener(TOPIC, g_listener); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: unregister_publisher_listener_001 + * @tc.desc: Verify the UnregisterPublisherListener function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, unregister_publisher_listener_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + service.RegisterPublisherListener(TOPIC, g_listener); + auto ret = service.UnregisterPublisherListener(TOPIC, g_listener); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: publish_message_001 + * @tc.desc: Verify the PublishMessage function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, publish_message_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + std::string msg; + service.RegisterPublisherListener(TOPIC, g_listener); + auto ret = service.PublishMessage(TOPIC, msg); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: onStop_test_002 + * @tc.desc: Verify the OnStop function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, onStop_test_002, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + service.OnStop(); + + EXPECT_EQ(service.state_, ServiceRunningState::STATE_NOT_START); +} + +/** + * @tc.name: dump_test_001 + * @tc.desc: Verify the Dump function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, dump_test_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + int32_t fd = 1; + + auto ret = service.Dump(fd, std::vector { ARGS_H }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_L }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_E }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_T }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_C }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..37505648a53f9db71e152b7e54bd7a42a66fdba9 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2022 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/local_hardware_manager_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/eventbus", + "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/localhardwaremanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//third_party/json/include", + ] +} + +ohos_unittest("LocalHardwareManagerTest") { + module_out_path = module_out_path + + sources = [ + "src/local_hardware_manager_test.cpp", + "src/mock_hardware_handler.cpp", + "src/plugin_listener_impl_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "kv_store:distributeddata_inner", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"LocalHardwareManagerTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +group("local_hardware_manager_test") { + testonly = true + deps = [ ":LocalHardwareManagerTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h new file mode 100644 index 0000000000000000000000000000000000000000..57de623904e83813760faf251a43c3b582bc2da0 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 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_HARDWARE_COMPONENT_LOADER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_LOADER_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class LocalHardwareManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..7b59c15c127ded1c3f0194937b798aad0dfd92d3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 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_HARDWARE_MCOK_HARDWARE_HANDLER_H +#define OHOS_DISTRIBUTED_HARDWARE_MCOK_HARDWARE_HANDLER_H + +#include + +#include "ihardware_handler.h" + +namespace OHOS { +namespace DistributedHardware { +class MockHardwareHandler : public IHardwareHandler { +public: + MockHardwareHandler() = default; + virtual ~MockHardwareHandler() = default; + + int32_t Initialize() override; + std::vector Query() override; + std::map QueryExtraInfo() override; + bool IsSupportPlugin() override; + void RegisterPluginListener(std::shared_ptr listener) override; + void UnRegisterPluginListener() override; + + int32_t PluginHardware(const std::string &dhId, const std::string &attr); + int32_t UnPluginHardware(const std::string &dhId); + + std::shared_ptr listener_ = nullptr; + std::vector> listenerVec; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h new file mode 100644 index 0000000000000000000000000000000000000000..a6d34db69cb1cee862b64045674b93ac02e98147 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 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_PLUGIN_LISTENER_IMPL_TEST_H +#define OHOS_DISTRIBUTED_PLUGIN_LISTENER_IMPL_TEST_H + +#include + +#define private public +#include "plugin_listener_impl.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class PluginListenerImplTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deccf44c60c46e87ce0a841ff4da4b8e7ab33673 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "local_hardware_manager_test.h" + +#define private public +#include "capability_info_manager.h" +#include "component_loader.h" +#include "local_hardware_manager.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void LocalHardwareManagerTest::SetUpTestCase(void) +{ + ComponentLoader::GetInstance().Init(); +} + +void LocalHardwareManagerTest::TearDownTestCase(void) +{ + ComponentLoader::GetInstance().UnInit(); +} + +void LocalHardwareManagerTest::SetUp() {} + +void LocalHardwareManagerTest::TearDown() {} + +/** + * @tc.name: local_hardware_manager_test_001 + * @tc.desc: Verify the Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(LocalHardwareManagerTest, local_hardware_manager_test_001, TestSize.Level0) +{ + LocalHardwareManager::GetInstance().Init(); +} + +/** + * @tc.name: local_hardware_manager_test_002 + * @tc.desc: Verify the UnInit function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(LocalHardwareManagerTest, local_hardware_manager_test_002, TestSize.Level0) +{ + LocalHardwareManager::GetInstance().UnInit(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b94efa681455cb3a645ae91da77211ecc5635ca5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "mock_hardware_handler.h" + +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t MockHardwareHandler::Initialize() +{ + return DH_FWK_SUCCESS; +} + +std::vector MockHardwareHandler::Query() +{ + DHItem item; + return std::vector { item }; +} + +std::map MockHardwareHandler::QueryExtraInfo() +{ + std::map extraInfo; + return extraInfo; +} + +bool MockHardwareHandler::IsSupportPlugin() +{ + return true; +} + +void MockHardwareHandler::RegisterPluginListener(std::shared_ptr listener) +{ + listener_ = listener; +} + +void MockHardwareHandler::UnRegisterPluginListener() +{ + listener_ = nullptr; +} + +int32_t MockHardwareHandler::PluginHardware(const std::string &dhId, const std::string &attr) +{ + if (listener_ != nullptr) { + listener_->PluginHardware(dhId, attr); + } + return DH_FWK_SUCCESS; +} + +int32_t MockHardwareHandler::UnPluginHardware(const std::string &dhId) +{ + if (listener_ != nullptr) { + listener_->UnPluginHardware(dhId); + } + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a26a0413af6bb2b44d18eca98b3ce792105262e --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "plugin_listener_impl_test.h" + +#include "distributed_hardware_errno.h" +#include "mock_hardware_handler.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { +const std::string dhId = "00000000000000000"; +const std::string attrs = "11111111111111111"; +std::shared_ptr g_listener; +std::shared_ptr g_mockHardwareHandler; +} + +void PluginListenerImplTest::SetUpTestCase(void) +{ + DHType dhType = DHType::CAMERA; + g_listener = std::make_shared(dhType); + g_mockHardwareHandler = std::make_shared(); +} + +void PluginListenerImplTest::TearDownTestCase(void) {} + +void PluginListenerImplTest::SetUp() {} + +void PluginListenerImplTest::TearDown() {} + +/** + * @tc.name: plugin_listener_impl_test_001 + * @tc.desc: Verify the PluginHardware function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(PluginListenerImplTest, plugin_listener_impl_test_001, TestSize.Level0) +{ + g_mockHardwareHandler->RegisterPluginListener(g_listener); + EXPECT_EQ(g_mockHardwareHandler->PluginHardware(dhId, attrs), DH_FWK_SUCCESS); +} + +/** + * @tc.name: plugin_listener_impl_test_002 + * @tc.desc: Verify the UnPluginHardware function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(PluginListenerImplTest, plugin_listener_impl_test_002, TestSize.Level0) +{ + EXPECT_EQ(g_mockHardwareHandler->UnPluginHardware(dhId), DH_FWK_SUCCESS); + g_mockHardwareHandler->UnRegisterPluginListener(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4abe5dec5d0101892c9e7c4016fd4cd5e3829916 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn @@ -0,0 +1,70 @@ +# Copyright (c) 2022 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/versioninfo_manager_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${common_path}/utils/include", + "${common_path}/log/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + ] +} + +## UnitTest versioninfo_manager_test +ohos_unittest("VersioninfoManagerTest") { + module_out_path = module_out_path + + sources = [ + "src/version_info_manager_test.cpp", + "src/version_info_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "kv_store:distributeddata_inner", + ] +} + +group("versioninfo_manager_test") { + testonly = true + deps = [ ":VersioninfoManagerTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h new file mode 100644 index 0000000000000000000000000000000000000000..b2c20ca5534f73e3b2c0a73183aab3239e513161 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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_HARDWARE_VERSION_INFO_MANAGER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H + +#include +#include + +#include "version_info.h" + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h new file mode 100644 index 0000000000000000000000000000000000000000..3e49b244555f9e198a282c98f999cd87b92af30d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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_HARDWARE_VERSION_INFO_MANAGER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5475fb074493a822171db50ab109cc22b00cbdd3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "version_info_manager_test.h" + +#include +#include +#include + +#define private public +#include "version_info_manager.h" +#include "version_manager.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoManagerTest" + +namespace { +const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a7"; +const string DEV_ID_2 = "bb536a637105409e904d4da83790a4a8"; +const string DEV_ID_3 = "bb536a637105409e904d4da83790a4a9"; +const string NAME_SCREEN = "distributed_screen"; +const string NAME_CAMERA = "distributed_camera"; +const string NAME_INPUT = "distributed_input"; +const string VERSION_1 = "1.0"; +const string VERSION_2 = "2.0"; +const string VERSION_3 = "3.0"; +std::vector g_versionInfos; +} + +std::vector CreateVersionInfos() +{ + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + + CompVersion compVersions2 = { + .name = NAME_SCREEN, + .dhType = DHType::DISPLAY, + .handlerVersion = VERSION_2, + .sourceVersion = VERSION_2, + .sinkVersion = VERSION_2 + }; + + CompVersion compVersions3 = { + .name = NAME_INPUT, + .dhType = DHType::INPUT, + .handlerVersion = VERSION_3, + .sourceVersion = VERSION_3, + .sinkVersion = VERSION_3 + }; + + VersionInfo verInfo1; + verInfo1.deviceId = DEV_ID_1; + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionInfo verInfo2; + verInfo2.deviceId = DEV_ID_2; + verInfo1.dhVersion = VERSION_2; + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + + VersionInfo verInfo3; + verInfo3.deviceId = DEV_ID_3; + verInfo1.dhVersion = VERSION_3; + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + + return std::vector { verInfo1, verInfo2, verInfo3 }; +} + +void VersionInfoManagerTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } + + g_versionInfos = CreateVersionInfos(); +} + +void VersionInfoManagerTest::TearDownTestCase(void) +{ + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} + +void VersionInfoManagerTest::SetUp() +{ +} + +void VersionInfoManagerTest::TearDown() +{ +} + +/** + * @tc.name:version_info_manager_test_001 + * @tc.desc: Verify the VersionInfoManager Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_001, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->Init(), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_002 + * @tc.desc: Verify the VersionInfoManager SyncRemoteVersionInfos function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_002, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->SyncRemoteVersionInfos(), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_003 + * @tc.desc: Verify the VersionInfoManager AddVersion function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_004, TestSize.Level0) +{ + for (const auto& verInfo : g_versionInfos) { + EXPECT_EQ(VersionInfoManager::GetInstance()->AddVersion(verInfo), DH_FWK_SUCCESS); + } +} + +/** + * @tc.name:version_info_manager_test_005 + * @tc.desc: Verify the VersionInfoManager GetVersionInfoByDeviceId function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_005, TestSize.Level0) +{ + VersionInfo versionInfo; + for (const auto& verInfo : g_versionInfos) { + EXPECT_EQ(VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(verInfo.deviceId, versionInfo), + DH_FWK_SUCCESS); + } +} + +/** + * @tc.name:version_info_manager_test_006 + * @tc.desc: Verify the VersionInfoManager SyncVersionInfoFromDB function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_006, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(DEV_ID_1), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_007 + * @tc.desc: Verify the VersionInfoManager UnInit function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_007, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->UnInit(), DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0de57f655dd31ace598ac24294870725f6127caf --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "version_info_test.h" + +#include +#include +#include + +#define private public +#include "version_info_manager.h" +#include "version_manager.h" +#include "version_info.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +using namespace testing::ext; +using namespace std; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoTest" + +namespace { +const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a7"; +const string NAME_SCREEN = "distributed_screen"; +const string VERSION_1 = "1.0"; +} + +void VersionInfoTest::SetUpTestCase(void) +{ +} + +void VersionInfoTest::TearDownTestCase(void) +{ +} + +void VersionInfoTest::SetUp() +{ +} + +void VersionInfoTest::TearDown() +{ +} + +/** + * @tc.name:version_info_test_001 + * @tc.desc: Verify the VersionInfoTest ToJson function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoTest, version_info_test_001, TestSize.Level0) +{ + CompVersion compVersions1 = { + .name = NAME_SCREEN, + .dhType = DHType::DISPLAY, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + + VersionInfo verInfo1; + verInfo1.deviceId = DEV_ID_1; + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + std::string jsonStr = verInfo1.ToJsonString(); + EXPECT_NE(jsonStr.empty(), true); + + VersionInfo verInfo2; + verInfo2.FromJsonString(jsonStr); + + EXPECT_EQ(verInfo2.ToJsonString(), jsonStr); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn index b62aae599f414daad0f650ec85daf2c460022855..9435ce6ad356231d40e5dc59d35fbab396424a2d 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h index ac3e715a9eb82f785e152ef7ca3ad55c352d30e2..652467dee4cc702166daa486d02c55c1f13f04d9 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp index 13cfe7b0c2caecc0694c72b5f14fcd579c97150b..865d4367beb269bceb939cb3f1b9e2477d815056 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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