From 265a3b3668d0c9ccd11196d37e1ad817e9b860d6 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Thu, 31 Oct 2024 19:42:13 +0800 Subject: [PATCH] =?UTF-8?q?add=20resourcemanager=E6=96=87=E4=BB=B6UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../test/unittest/BUILD.gn | 3 +- .../src/distributed_hardware_manager_test.cpp | 14 +- .../src/local_hardware_manager_test.cpp | 7 + .../unittest/common/resourcemanager/BUILD.gn | 184 ++++++++- .../resourcemanager/capability_info_test.cpp | 238 +++++++++++ .../include/resource_manager_test.h | 32 -- .../local_capability_info_manager_test.cpp | 244 +++++++++++ .../resourcemanager/meta_cap_info_test.cpp | 329 +++++++++++++++ .../meta_info_manager_test.cpp | 329 +++++++++++++++ .../{src => }/resource_manager_test.cpp | 388 +----------------- .../resourcemanager/version_info_test.cpp | 140 +++++++ .../{versioninfomanager => }/BUILD.gn | 0 .../include/version_info_manager_test.h | 0 .../src/version_info_manager_test.cpp | 29 +- .../versioninfomanager/versioninfo/BUILD.gn | 72 ---- .../versioninfo/include/version_info_test.h | 33 -- .../versioninfo/src/version_info_test.cpp | 161 -------- 17 files changed, 1520 insertions(+), 683 deletions(-) create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/capability_info_test.cpp delete mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_cap_info_test.cpp create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp rename services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/{src => }/resource_manager_test.cpp (69%) create mode 100644 services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp rename services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/{versioninfomanager => }/BUILD.gn (100%) rename services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/{versioninfomanager => }/include/version_info_manager_test.h (100%) rename services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/{versioninfomanager => }/src/version_info_manager_test.cpp (93%) delete mode 100644 services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn delete mode 100644 services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/include/version_info_test.h delete mode 100644 services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/src/version_info_test.cpp diff --git a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn index 5d8580e6..35a30988 100644 --- a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn @@ -36,8 +36,7 @@ group("test") { "common/task:dh_task_test", "common/transport:transport_test", "common/utils:dh_context_test", - "common/versioninfomanager/versioninfo:version_info_test", - "common/versioninfomanager/versioninfomanager:versioninfo_manager_test", + "common/versioninfomanager:versioninfo_manager_test", "common/versionmanager:version_manager_test", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp index fbfad029..bc330561 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -77,5 +77,17 @@ HWTEST_F(DistributedHardwareManagerTest, GetComponentVersion_001, TestSize.Level ret = DistributedHardwareManager::GetInstance().GetComponentVersion(versionMap); EXPECT_EQ(DH_FWK_SUCCESS, ret); } + +HWTEST_F(DistributedHardwareManagerTest, GetComponentVersion_002, TestSize.Level0) +{ + ComponentLoader::GetInstance().isLocalVersionInit_.store(false); + std::unordered_map versionMap; + int32_t ret = DistributedHardwareManager::GetInstance().GetComponentVersion(versionMap); + EXPECT_EQ(ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL, ret); + + ComponentLoader::GetInstance().isLocalVersionInit_.store(true); + ret = DistributedHardwareManager::GetInstance().GetComponentVersion(versionMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/src/local_hardware_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/src/local_hardware_manager_test.cpp index f9f35707..8fea3417 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/src/local_hardware_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/src/local_hardware_manager_test.cpp @@ -108,5 +108,12 @@ HWTEST_F(LocalHardwareManagerTest, QueryLocalHardware_001, TestSize.Level0) EXPECT_EQ(true, LocalHardwareManager::GetInstance().pluginListenerMap_.empty()); } +HWTEST_F(LocalHardwareManagerTest, QueryLocalHardware_002, TestSize.Level0) +{ + DHType dhType = DHType::INPUT; + IHardwareHandler *hardwareHandler = nullptr; + LocalHardwareManager::GetInstance().QueryLocalHardware(dhType, hardwareHandler); + EXPECT_EQ(true, LocalHardwareManager::GetInstance().pluginListenerMap_.empty()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn index 2809608b..6ba47ea3 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn @@ -20,7 +20,6 @@ module_out_path = "distributed_hardware_fwk/resource_manager_test" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "include", "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", @@ -33,10 +32,146 @@ config("module_private_config") { } ## UnitTest resource_manager_test +ohos_unittest("CapabilityInfoTest") { + module_out_path = module_out_path + + sources = [ "capability_info_test.cpp" ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "hilog:libhilog", + "kv_store:distributeddata_inner", + "power_manager:powermgr_client", + ] + + if (powermgr_power_manager_fwk) { + external_deps += [ "power_manager:powermgr_client" ] + defines = [ "POWER_MANAGER_ENABLE" ] + } +} + +ohos_unittest("LocalCapInfoMgrTest") { + module_out_path = module_out_path + + sources = [ "local_capability_info_manager_test.cpp" ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "kv_store:distributeddata_inner", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + ] + + if (powermgr_power_manager_fwk) { + external_deps += [ "power_manager:powermgr_client" ] + defines = [ "POWER_MANAGER_ENABLE" ] + } +} + +ohos_unittest("MetaCapInfoTest") { + module_out_path = module_out_path + + sources = [ "meta_cap_info_test.cpp" ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "hilog:libhilog", + "kv_store:distributeddata_inner", + "power_manager:powermgr_client", + ] + + if (powermgr_power_manager_fwk) { + external_deps += [ "power_manager:powermgr_client" ] + defines = [ "POWER_MANAGER_ENABLE" ] + } +} + +ohos_unittest("MetaInfoMgrTest") { + module_out_path = module_out_path + + sources = [ "meta_info_manager_test.cpp" ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "kv_store:distributeddata_inner", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + ] + + if (powermgr_power_manager_fwk) { + external_deps += [ "power_manager:powermgr_client" ] + defines = [ "POWER_MANAGER_ENABLE" ] + } +} + ohos_unittest("ResourceManagerTest") { module_out_path = module_out_path - sources = [ "src/resource_manager_test.cpp" ] + sources = [ "resource_manager_test.cpp" ] configs = [ ":module_private_config" ] @@ -58,6 +193,42 @@ ohos_unittest("ResourceManagerTest") { "eventhandler:libeventhandler", "hilog:libhilog", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + ] + + if (powermgr_power_manager_fwk) { + external_deps += [ "power_manager:powermgr_client" ] + defines = [ "POWER_MANAGER_ENABLE" ] + } +} + +ohos_unittest("VersionInfoTest") { + module_out_path = module_out_path + + sources = [ "version_info_test.cpp" ] + + configs = [ ":module_private_config" ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] @@ -69,5 +240,12 @@ ohos_unittest("ResourceManagerTest") { group("resource_manager_test") { testonly = true - deps = [ ":ResourceManagerTest" ] + deps = [ + ":CapabilityInfoTest", + ":LocalCapInfoMgrTest", + ":MetaCapInfoTest", + ":MetaInfoMgrTest", + ":ResourceManagerTest", + ":VersionInfoTest", + ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/capability_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/capability_info_test.cpp new file mode 100644 index 00000000..eea461bd --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/capability_info_test.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2024 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 + +#include "cJSON.h" + +#include "constants.h" +#include "meta_capability_info.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +class CapabilityInfoTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CapabilityInfoTest::SetUp() {} + +void CapabilityInfoTest::TearDown() {} + +void CapabilityInfoTest::SetUpTestCase() {} + +void CapabilityInfoTest::TearDownTestCase() {} + +HWTEST_F(CapabilityInfoTest, ToJson_001, TestSize.Level0) +{ + cJSON *jsonObject = nullptr; + CapabilityInfo capability; + ToJson(jsonObject, capability); + EXPECT_TRUE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_001, TestSize.Level0) +{ + cJSON *jsonObject = nullptr; + CapabilityInfo capability; + FromJson(jsonObject, capability); + EXPECT_TRUE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_002, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddNumberToObject(jsonObj, DH_ID.c_str(), 1); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_TRUE(capability.GetDeviceId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_003, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddNumberToObject(jsonObj, DEV_ID.c_str(), 1); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_004, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddNumberToObject(jsonObj, DEV_NAME.c_str(), 1); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_005, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), "devname_test"); + cJSON_AddStringToObject(jsonObj, DEV_TYPE.c_str(), "devtype_test"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_006, TestSize.Level0) +{ + const uint16_t devType = 1; + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), "devname_test"); + cJSON_AddNumberToObject(jsonObj, DEV_TYPE.c_str(), devType); + cJSON_AddStringToObject(jsonObj, DH_TYPE.c_str(), "dhtype_test"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_007, TestSize.Level0) +{ + const uint16_t devType = 1; + const uint32_t dhType = 1; + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), "devname_test"); + cJSON_AddNumberToObject(jsonObj, DEV_TYPE.c_str(), devType); + cJSON_AddNumberToObject(jsonObj, DH_TYPE.c_str(), dhType); + cJSON_AddNumberToObject(jsonObj, DH_ATTRS.c_str(), 1); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_008, TestSize.Level0) +{ + const uint16_t devType = 1; + const uint32_t dhType = 1; + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), "devname_test"); + cJSON_AddNumberToObject(jsonObj, DEV_TYPE.c_str(), devType); + cJSON_AddNumberToObject(jsonObj, DH_TYPE.c_str(), dhType); + cJSON_AddStringToObject(jsonObj, DH_ATTRS.c_str(), "dhattrs_test"); + cJSON_AddNumberToObject(jsonObj, DH_SUBTYPE.c_str(), 1); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} + +HWTEST_F(CapabilityInfoTest, FromJson_009, TestSize.Level0) +{ + const uint16_t devType = 1; + const uint32_t dhType = 1; + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "dhid_test"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_test"); + cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), "devname_test"); + cJSON_AddNumberToObject(jsonObj, DEV_TYPE.c_str(), devType); + cJSON_AddNumberToObject(jsonObj, DH_TYPE.c_str(), dhType); + cJSON_AddStringToObject(jsonObj, DH_ATTRS.c_str(), "dhattrs_test"); + cJSON_AddStringToObject(jsonObj, DH_SUBTYPE.c_str(), "dhsubtype_test"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + CapabilityInfo capability; + FromJson(jsonObj, capability); + cJSON_free(cjson); + cJSON_Delete(jsonObj); + EXPECT_FALSE(capability.GetDHId().empty()); +} +} +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h deleted file mode 100644 index 7bcef973..00000000 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2021 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_RESOURCE_MANAGER_TEST_H -#define OHOS_DISTRIBUTED_HARDWARE_RESOURCE_MANAGER_TEST_H - -#include - -namespace OHOS { -namespace DistributedHardware { -class ResourceManagerTest : 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/resourcemanager/local_capability_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp new file mode 100644 index 00000000..16cfd8a4 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2024 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 + +#include "cJSON.h" + +#include "constants.h" +#include "capability_info.h" +#include "distributed_hardware_errno.h" +#include "local_capability_info_manager.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint16_t DEV_TYPE_TEST = 14; +} +class LocalCapInfoMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LocalCapInfoMgrTest::SetUp() {} + +void LocalCapInfoMgrTest::TearDown() {} + +void LocalCapInfoMgrTest::SetUpTestCase() {} + +void LocalCapInfoMgrTest::TearDownTestCase() {} + +HWTEST_F(LocalCapInfoMgrTest, Init_001, TestSize.Level0) +{ + auto ret = LocalCapabilityInfoManager::GetInstance()->Init(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = LocalCapabilityInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, UnInit_001, TestSize.Level0) +{ + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, SyncDeviceInfoFromDB_001, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + LocalCapabilityInfoManager::GetInstance()->Init(); + ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, AddCapability_001, TestSize.Level0) +{ + std::vector> resInfos; + auto ret = LocalCapabilityInfoManager::GetInstance()->AddCapability(resInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, AddCapability_002, TestSize.Level0) +{ + std::shared_ptr capInfo = std::make_shared("", "", "", 0, DHType::UNKNOWN, "", ""); + std::vector> resInfos; + resInfos.push_back(capInfo); + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->AddCapability(resInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, AddCapability_003, TestSize.Level0) +{ + std::shared_ptr capInfo = std::make_shared("", "", "", 0, DHType::UNKNOWN, "", ""); + std::vector> resInfos; + resInfos.push_back(capInfo); + LocalCapabilityInfoManager::GetInstance()->Init(); + auto ret = LocalCapabilityInfoManager::GetInstance()->AddCapability(resInfos); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, RemoveCapabilityInfoByKey_001, TestSize.Level0) +{ + std::string key = ""; + auto ret = LocalCapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, RemoveCapabilityInfoByKey_002, TestSize.Level0) +{ + std::string key = "deviceId_test"; + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + LocalCapabilityInfoManager::GetInstance()->Init(); + ret = LocalCapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetCapabilitiesByDeviceId_001, TestSize.Level0) +{ + std::string deviceId = ""; + std::vector> resInfos; + LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, resInfos); + + deviceId = "123456789"; + std::string dhid = "111111"; + std::shared_ptr capInfo = std::make_shared( + "dhid", deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", "subtype"); + std::string key = deviceId + "###" + dhid; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo; + LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, resInfos); + EXPECT_TRUE(resInfos.empty()); +} + +HWTEST_F(LocalCapInfoMgrTest, GetCapability_001, TestSize.Level0) +{ + std::string deviceId = ""; + std::string dhId = ""; + std::shared_ptr capPtr; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + deviceId = "deviceId_test"; + ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + dhId = "dhId_test"; + deviceId = ""; + ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetCapability_002, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string dhId = "dhId_test"; + std::shared_ptr capPtr; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret); + + std::shared_ptr capbilityInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", "subtype"); + std::string key = deviceId + "###" + dhId; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capbilityInfo; + ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDataByKey_001, TestSize.Level0) +{ + std::string key = ""; + std::shared_ptr capPtr; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKey(key, capPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDataByKey_002, TestSize.Level0) +{ + std::string key = "key_test"; + std::shared_ptr capPtr; + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKey(key, capPtr); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + LocalCapabilityInfoManager::GetInstance()->Init(); + ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKey(key, capPtr); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDataByDHType_001, TestSize.Level0) +{ + CapabilityInfoMap capabilityMap; + std::string deviceId = "deviceId_test"; + std::shared_ptr capbilityInfo = std::make_shared( + "dhId_test", deviceId, "devName_test", 14, DHType::AUDIO, "attrs", "subtype"); + std::string key = deviceId + "###" + "dhId_test"; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capbilityInfo; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByDHType(DHType::CAMERA, capabilityMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = LocalCapabilityInfoManager::GetInstance()->GetDataByDHType(DHType::AUDIO, capabilityMap); + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDataByKeyPrefix_001, TestSize.Level0) +{ + std::string keyPrefix = ""; + CapabilityInfoMap capabilityMap; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDataByKeyPrefix_002, TestSize.Level0) +{ + std::string keyPrefix = "keyPrefix_test"; + CapabilityInfoMap capabilityMap; + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + LocalCapabilityInfoManager::GetInstance()->Init(); + ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); +} + +HWTEST_F(LocalCapInfoMgrTest, ClearRemoteDeviceLocalInfoData_001, TestSize.Level0) +{ + std::string peeruuid = ""; + LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = LocalCapabilityInfoManager::GetInstance()->ClearRemoteDeviceLocalInfoData(peeruuid); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + LocalCapabilityInfoManager::GetInstance()->Init(); + ret = LocalCapabilityInfoManager::GetInstance()->ClearRemoteDeviceLocalInfoData(peeruuid); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} +} +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_cap_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_cap_info_test.cpp new file mode 100644 index 00000000..2a45acb2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_cap_info_test.cpp @@ -0,0 +1,329 @@ +/* + * Copyright (c) 2024 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 + +#include "cJSON.h" + +#include "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "meta_capability_info.h" +#include "meta_info_manager.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint16_t DEV_TYPE_TEST = 14; +} + +class MetaInfoMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void MetaInfoMgrTest::SetUp() {} + +void MetaInfoMgrTest::TearDown() {} + +void MetaInfoMgrTest::SetUpTestCase() {} + +void MetaInfoMgrTest::TearDownTestCase() {} + +HWTEST_F(MetaInfoMgrTest, Init_001, TestSize.Level0) +{ + auto ret = MetaInfoManager::GetInstance()->Init(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + ret = MetaInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(MetaInfoMgrTest, UnInit_001, TestSize.Level0) +{ + MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = MetaInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret); +} + +HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_001, TestSize.Level0) +{ + std::vector> metaCapInfos; + auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_002, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::vector> metaCapInfos; + metaCapInfos.push_back(metaCapInfo); + auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_001, TestSize.Level0) +{ + std::string udidHash = ""; + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_002, TestSize.Level0) +{ + std::string udidHash = "132456798"; + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_003, TestSize.Level0) +{ + std::string udidHash = "132456798"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_001, TestSize.Level0) +{ + auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_001, TestSize.Level0) +{ + std::string keyPrefix = ""; + MetaCapInfoMap metaCapMap; + auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + keyPrefix = "keyPrefix_test"; + ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_002, TestSize.Level0) +{ + std::string keyPrefix = "keyPrefix_test"; + MetaCapInfoMap metaCapMap; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_001, TestSize.Level0) +{ + std::string key = ""; + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + key = "key_test"; + ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_002, TestSize.Level0) +{ + std::string key = "key_test"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_001, TestSize.Level0) +{ + std::string udidHash = ""; + std::string dhId = ""; + std::shared_ptr metaCapPtr; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + udidHash = "123456789"; + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + udidHash = ""; + dhId = "123456789"; + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_002, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapPtr; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret); + + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfosByUdidHash_001, TestSize.Level0) +{ + std::string udidHash = ""; + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + + udidHash = "udidHash_test"; + std::string deviceId = "deviceId_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapInfo; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(deviceId, metaCapInfos); + EXPECT_TRUE(metaCapInfos.empty()); + + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + EXPECT_FALSE(metaCapInfos.empty()); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapByValue_001, TestSize.Level0) +{ + std::string value = ""; + std::shared_ptr metaCapPtr = nullptr; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + value = "invalid JSON string"; + ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); + + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + metaCapPtr = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaDataByDHType_001, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + MetaCapInfoMap metaInfoMap; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + auto ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::AUDIO, metaInfoMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::CAMERA, metaInfoMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_001, TestSize.Level0) +{ + std::string networkId = ""; + auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + networkId = "networkId_test"; + ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_002, TestSize.Level0) +{ + std::string networkId = "networkId_test"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoInMemByUdid_001, TestSize.Level0) +{ + std::string peerudid = "123456789"; + std::string peerudIdHash = Sha256(peerudid); + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapPtr = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = peerudIdHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapPtr; + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(deviceId); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(peerudid); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(MetaInfoMgrTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0) +{ + MetaInfoManager::GetInstance()->Init(); + std::string peerudid = "peerudid_test"; + std::string peeruuid = "peeruuid_test"; + auto ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + + MetaInfoManager::GetInstance()->UnInit(); + ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetEntriesByKeys_001, TestSize.Level0) +{ + std::vector keys; + auto ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + + keys.push_back("key_test"); + ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + MetaInfoManager::GetInstance()->UnInit(); +} +} +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp new file mode 100644 index 00000000..2a45acb2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp @@ -0,0 +1,329 @@ +/* + * Copyright (c) 2024 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 + +#include "cJSON.h" + +#include "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "meta_capability_info.h" +#include "meta_info_manager.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint16_t DEV_TYPE_TEST = 14; +} + +class MetaInfoMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void MetaInfoMgrTest::SetUp() {} + +void MetaInfoMgrTest::TearDown() {} + +void MetaInfoMgrTest::SetUpTestCase() {} + +void MetaInfoMgrTest::TearDownTestCase() {} + +HWTEST_F(MetaInfoMgrTest, Init_001, TestSize.Level0) +{ + auto ret = MetaInfoManager::GetInstance()->Init(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + ret = MetaInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(MetaInfoMgrTest, UnInit_001, TestSize.Level0) +{ + MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + auto ret = MetaInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret); +} + +HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_001, TestSize.Level0) +{ + std::vector> metaCapInfos; + auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, AddMetaCapInfos_002, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::vector> metaCapInfos; + metaCapInfos.push_back(metaCapInfo); + auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_001, TestSize.Level0) +{ + std::string udidHash = ""; + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_002, TestSize.Level0) +{ + std::string udidHash = "132456798"; + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncMetaInfoFromDB_003, TestSize.Level0) +{ + std::string udidHash = "132456798"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_001, TestSize.Level0) +{ + auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_001, TestSize.Level0) +{ + std::string keyPrefix = ""; + MetaCapInfoMap metaCapMap; + auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + keyPrefix = "keyPrefix_test"; + ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_002, TestSize.Level0) +{ + std::string keyPrefix = "keyPrefix_test"; + MetaCapInfoMap metaCapMap; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_001, TestSize.Level0) +{ + std::string key = ""; + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + key = "key_test"; + ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoByKey_002, TestSize.Level0) +{ + std::string key = "key_test"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_001, TestSize.Level0) +{ + std::string udidHash = ""; + std::string dhId = ""; + std::shared_ptr metaCapPtr; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + udidHash = "123456789"; + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + udidHash = ""; + dhId = "123456789"; + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfo_002, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapPtr; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret); + + ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapInfosByUdidHash_001, TestSize.Level0) +{ + std::string udidHash = ""; + std::vector> metaCapInfos; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + + udidHash = "udidHash_test"; + std::string deviceId = "deviceId_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapInfo; + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(deviceId, metaCapInfos); + EXPECT_TRUE(metaCapInfos.empty()); + + MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); + EXPECT_FALSE(metaCapInfos.empty()); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaCapByValue_001, TestSize.Level0) +{ + std::string value = ""; + std::shared_ptr metaCapPtr = nullptr; + auto ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + value = "invalid JSON string"; + ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); + + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + metaCapPtr = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetMetaDataByDHType_001, TestSize.Level0) +{ + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + MetaCapInfoMap metaInfoMap; + std::shared_ptr dhMetaCapInfo = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = udidHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; + auto ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::AUDIO, metaInfoMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::CAMERA, metaInfoMap); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); +} + +HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_001, TestSize.Level0) +{ + std::string networkId = ""; + auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + networkId = "networkId_test"; + ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, SyncDataByNetworkId_002, TestSize.Level0) +{ + std::string networkId = "networkId_test"; + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(MetaInfoMgrTest, RemoveMetaInfoInMemByUdid_001, TestSize.Level0) +{ + std::string peerudid = "123456789"; + std::string peerudIdHash = Sha256(peerudid); + std::string deviceId = "deviceId_test"; + std::string udidHash = "udidHash_test"; + std::string dhId = "dhId_test"; + std::shared_ptr metaCapPtr = std::make_shared( + dhId, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); + std::string key = peerudIdHash + "###" + dhId; + MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapPtr; + auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(deviceId); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + + ret = MetaInfoManager::GetInstance()->RemoveMetaInfoInMemByUdid(peerudid); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + +HWTEST_F(MetaInfoMgrTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0) +{ + MetaInfoManager::GetInstance()->Init(); + std::string peerudid = "peerudid_test"; + std::string peeruuid = "peeruuid_test"; + auto ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + + MetaInfoManager::GetInstance()->UnInit(); + ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); +} + +HWTEST_F(MetaInfoMgrTest, GetEntriesByKeys_001, TestSize.Level0) +{ + std::vector keys; + auto ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + + keys.push_back("key_test"); + ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + + MetaInfoManager::GetInstance()->Init(); + ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); + EXPECT_EQ(0, ret.size()); + MetaInfoManager::GetInstance()->UnInit(); +} +} +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp similarity index 69% rename from services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp index 211d48b4..589f8116 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp @@ -13,23 +13,20 @@ * limitations under the License. */ -#include "resource_manager_test.h" - +#include #include #include #include #include +#include "cJSON.h" + #include "constants.h" #include "capability_info.h" #include "capability_info_manager.h" -#include "local_capability_info_manager.h" -#include "meta_capability_info.h" -#include "meta_info_manager.h" #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" -#include "cJSON.h" using namespace testing::ext; using namespace std; @@ -101,6 +98,14 @@ const shared_ptr CAP_INFO_9 = DH_SUBTYPE_4); } +class ResourceManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + void ResourceManagerTest::SetUpTestCase(void) { auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); @@ -849,54 +854,6 @@ HWTEST_F(ResourceManagerTest, DumpCapabilityInfos_001, TestSize.Level0) EXPECT_EQ(nullptr, CapabilityInfoManager::GetInstance()->dbAdapterPtr_); } -/** - * @tc.name: FromJson_001 - * @tc.desc: Verify the FromJson function. - * @tc.type: FUNC - * @tc.require: AR000GHSJE - */ -HWTEST_F(ResourceManagerTest, FromJson_001, TestSize.Level0) -{ - std::string dhId; - std::string devId; - std::string devName; - uint16_t devType = 0; - DHType dhType = DHType::GPS; - std::string dhAttrs; - std::string dhSubtype; - CapabilityInfo info(dhId, devId, devName, devType, dhType, dhAttrs, dhSubtype); - - cJSON* json = cJSON_CreateObject(); - if (json == nullptr) { - return; - } - const char* DH_ID = "dh_id"; - const char* DEV_ID = "dev_id"; - const char* DEV_NAME = "dev_name"; - const char* DEV_TYPE = "dev_type"; - const char* DH_TYPE = "dh_type"; - const char* DH_ATTRS = "dh_attrs"; - const char* DH_SUBTYPE = "dh_subtype"; - - cJSON_AddStringToObject(json, DH_ID, "dh_id"); - cJSON_AddStringToObject(json, DEV_ID, "dev_id"); - cJSON_AddStringToObject(json, DEV_NAME, "dev_name"); - cJSON_AddNumberToObject(json, DEV_TYPE, devType); - cJSON_AddStringToObject(json, DH_TYPE, "dh_type"); - cJSON_AddStringToObject(json, DH_ATTRS, "dh_attrs"); - cJSON_AddStringToObject(json, DH_SUBTYPE, "dh_subtype"); - - char* cjson = cJSON_PrintUnformatted(json); - if (cjson == nullptr) { - cJSON_Delete(json); - return; - } - std::string jsonStr(cjson); - cJSON_free(cjson); - cJSON_Delete(json); - EXPECT_EQ(DH_FWK_SUCCESS, info.FromJsonString(jsonStr)); -} - /** * @tc.name: GetEntriesByKeys_001 * @tc.desc: Verify the GetEntriesByKeys function. @@ -909,328 +866,5 @@ HWTEST_F(ResourceManagerTest, GetEntriesByKeys_001, TestSize.Level0) auto entries = CapabilityInfoManager::GetInstance()->GetEntriesByKeys(keys); EXPECT_EQ(0, entries.size()); } - -HWTEST_F(ResourceManagerTest, Init_001, TestSize.Level0) -{ - auto ret = LocalCapabilityInfoManager::GetInstance()->Init(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, UnInit_001, TestSize.Level0) -{ - LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = LocalCapabilityInfoManager::GetInstance()->UnInit(); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret); -} - -HWTEST_F(ResourceManagerTest, UnInit_002, TestSize.Level0) -{ - LocalCapabilityInfoManager::GetInstance()->Init(); - auto ret = LocalCapabilityInfoManager::GetInstance()->UnInit(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, SyncDeviceInfoFromDB_001, TestSize.Level0) -{ - std::string deviceId = "deviceId_test"; - LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - LocalCapabilityInfoManager::GetInstance()->Init(); - ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); -} - -HWTEST_F(ResourceManagerTest, RemoveCapabilityInfoByKey_001, TestSize.Level0) -{ - std::string key = "deviceId_test"; - LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = LocalCapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - LocalCapabilityInfoManager::GetInstance()->Init(); - ret = LocalCapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetCapability_001, TestSize.Level0) -{ - std::string deviceId = "deviceId_test"; - std::string dhId = "dhId_test"; - std::shared_ptr capPtr; - LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); - auto ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret); - - std::shared_ptr capbilityInfo = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::AUDIO, "attrs", "subtype"); - std::string key = deviceId + "###" + dhId; - LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capbilityInfo; - ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capPtr); - LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetDataByKey_002, TestSize.Level0) -{ - std::string key = "key_test"; - std::shared_ptr capPtr; - LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKey(key, capPtr); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - LocalCapabilityInfoManager::GetInstance()->Init(); - ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKey(key, capPtr); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); -} - -HWTEST_F(ResourceManagerTest, GetDataByDHType_001, TestSize.Level0) -{ - CapabilityInfoMap capabilityMap; - std::string deviceId = "deviceId_test"; - std::shared_ptr capbilityInfo = std::make_shared( - "dhId_test", deviceId, "devName_test", 14, DHType::AUDIO, "attrs", "subtype"); - std::string key = deviceId + "###" + "dhId_test"; - LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capbilityInfo; - auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByDHType(DHType::CAMERA, capabilityMap); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - ret = LocalCapabilityInfoManager::GetInstance()->GetDataByDHType(DHType::AUDIO, capabilityMap); - LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetDataByKeyPrefix_002, TestSize.Level0) -{ - std::string keyPrefix = "keyPrefix_test"; - CapabilityInfoMap capabilityMap; - LocalCapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - LocalCapabilityInfoManager::GetInstance()->Init(); - ret = LocalCapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); -} - -HWTEST_F(ResourceManagerTest, UnInit_003, TestSize.Level0) -{ - MetaInfoManager::GetInstance()->Init(); - auto ret = MetaInfoManager::GetInstance()->UnInit(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, AddMetaCapInfos_001, TestSize.Level0) -{ - std::string deviceId = "deviceId_test"; - std::string udidHash = "udidHash_test"; - std::string dhId = "dhId_test"; - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - std::shared_ptr dhMetaCapInfo = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); - std::vector> metaCapInfos; - metaCapInfos.push_back(dhMetaCapInfo); - auto ret = MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); -} - -HWTEST_F(ResourceManagerTest, SyncMetaInfoFromDB_001, TestSize.Level0) -{ - std::string deviceId = "deviceId_test"; - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(deviceId); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - MetaInfoManager::GetInstance()->Init(); - ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(deviceId); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); -} - -HWTEST_F(ResourceManagerTest, SyncRemoteMetaInfos_001, TestSize.Level0) -{ - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - MetaInfoManager::GetInstance()->Init(); - ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetDataByKeyPrefix_003, TestSize.Level0) -{ - std::string keyPrefix = "keyPrefix_test"; - MetaCapInfoMap metaCapMap; - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - MetaInfoManager::GetInstance()->Init(); - ret = MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); -} - -HWTEST_F(ResourceManagerTest, RemoveMetaInfoByKey_001, TestSize.Level0) -{ - std::string key = "key_test"; - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - auto ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); - - MetaInfoManager::GetInstance()->Init(); - ret = MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetMetaCapInfo_001, TestSize.Level0) -{ - std::string deviceId = "deviceId_test"; - std::string udidHash = "udidHash_test"; - std::string dhId = "dhId_test"; - std::shared_ptr metaCapPtr; - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); - auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND, ret); - - std::shared_ptr dhMetaCapInfo = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); - std::string key = udidHash + "###" + dhId; - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; - ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(udidHash, dhId, metaCapPtr); - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; - std::vector> metaCapInfos; - MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); - - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; - MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos); - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); -} - -HWTEST_F(ResourceManagerTest, GetMetaCapByValue_001, TestSize.Level0) -{ - std::string value = ""; - std::shared_ptr metaCapPtr = nullptr; - auto ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); - EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); - - std::string deviceId = "deviceId_test"; - std::string udidHash = "udidHash_test"; - std::string dhId = "dhId_test"; - metaCapPtr = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); - ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); - EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); - - value = "invalid JSON string"; - ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); - EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); -} - -HWTEST_F(ResourceManagerTest, GetEntriesByKeys_002, TestSize.Level0) -{ - std::vector keys; - auto ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); - EXPECT_EQ(0, ret.size()); - - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - keys.push_back("key_test"); - ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); - EXPECT_EQ(0, ret.size()); - - MetaInfoManager::GetInstance()->Init(); - ret = MetaInfoManager::GetInstance()->GetEntriesByKeys(keys); - EXPECT_EQ(0, ret.size()); -} - -HWTEST_F(ResourceManagerTest, FromJson_002, TestSize.Level0) -{ - MetaCapabilityInfo metaCapInfo; - std::string dhId = "dhId_test"; - std::string dveId = "devId_test"; - std::string devName = "devName_test"; - uint16_t devType = 14; - DHType dhType = DHType::AUDIO; - std::string dhAttrs = "dhAttrs_test"; - std::string dhSubtype = "dhSubtype_test"; - std::string sinkVersion = "sinkVersion_test"; - cJSON *jsonObj = cJSON_CreateObject(); - if (jsonObj == nullptr) { - return; - } - cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), dhId.c_str()); - cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), dveId.c_str()); - cJSON_AddStringToObject(jsonObj, DEV_NAME.c_str(), devName.c_str()); - cJSON_AddNumberToObject(jsonObj, DEV_TYPE.c_str(), (double)devType); - cJSON_AddNumberToObject(jsonObj, DH_TYPE.c_str(), (double)dhType); - cJSON_AddStringToObject(jsonObj, DH_ATTRS.c_str(), dhAttrs.c_str()); - cJSON_AddStringToObject(jsonObj, DH_SUBTYPE.c_str(), sinkVersion.c_str()); - cJSON_AddStringToObject(jsonObj, SINK_VER.c_str(), sinkVersion.c_str()); - FromJson(jsonObj, metaCapInfo); - EXPECT_EQ(0, MetaInfoManager::GetInstance()->globalMetaInfoMap_.size()); -} - -HWTEST_F(ResourceManagerTest, GetMetaDataByDHType_001, TestSize.Level0) -{ - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); - std::string deviceId = "deviceId_test"; - std::string udidHash = "udidHash_test"; - std::string dhId = "dhId_test"; - MetaCapInfoMap metaInfoMap; - std::shared_ptr dhMetaCapInfo = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); - std::string key = udidHash + "###" + dhId; - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; - auto ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::AUDIO, metaInfoMap); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, GetMetaDataByDHType_002, TestSize.Level0) -{ - MetaInfoManager::GetInstance()->globalMetaInfoMap_.clear(); - std::string deviceId = "deviceId_test"; - std::string udidHash = "udidHash_test"; - std::string dhId = "dhId_test"; - MetaCapInfoMap metaInfoMap; - std::shared_ptr dhMetaCapInfo = std::make_shared( - dhId, deviceId, "devName_test", 14, DHType::AUDIO, "attrs_test", "subtype", udidHash, "1.0"); - std::string key = udidHash + "###" + dhId; - MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = dhMetaCapInfo; - auto ret = MetaInfoManager::GetInstance()->GetMetaDataByDHType(DHType::AUDIO, metaInfoMap); - EXPECT_EQ(DH_FWK_SUCCESS, ret); -} - -HWTEST_F(ResourceManagerTest, SyncDataByNetworkId_001, TestSize.Level0) -{ - std::string networkId = ""; - auto ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); - EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); - - networkId = "networkId_test"; - ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - ret = MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); -} - -HWTEST_F(ResourceManagerTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0) -{ - MetaInfoManager::GetInstance()->Init(); - std::string peerudid = "peerudid_test"; - std::string peeruuid = "peeruuid_test"; - auto ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); - - MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; - ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); -} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp new file mode 100644 index 00000000..8482690e --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/version_info_test.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2024 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 + +#include "cJSON.h" + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "version_info.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void VersionInfoTest::SetUp() {} + +void VersionInfoTest::TearDown() {} + +void VersionInfoTest::SetUpTestCase() {} + +void VersionInfoTest::TearDownTestCase() {} + +HWTEST_F(VersionInfoTest, FromJsonString_001, TestSize.Level0) +{ + std::shared_ptr versionInfoPtr = std::make_shared(); + std::string jsonStr = ""; + auto ret = versionInfoPtr->FromJsonString(jsonStr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(VersionInfoTest, ToJson_001, TestSize.Level0) +{ + cJSON *jsonObj = nullptr; + VersionInfo verInfo; + ToJson(jsonObj, verInfo); + EXPECT_TRUE(verInfo.deviceId.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_CompVersion_001, TestSize.Level0) +{ + cJSON *jsonObj = nullptr; + CompVersion compVer; + FromJson(jsonObj, compVer); + EXPECT_TRUE(compVer.name.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_CompVersion_002, TestSize.Level0) +{ + const uint32_t dhType = 1; + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, NAME.c_str(), "name_test"); + cJSON_AddNumberToObject(jsonObj, TYPE.c_str(), dhType); + cJSON_AddStringToObject(jsonObj, HANDLER.c_str(), "handler_ver_1.0"); + cJSON_AddStringToObject(jsonObj, SOURCE_VER.c_str(), "source_ver_1.0"); + cJSON_AddStringToObject(jsonObj, SINK_VER.c_str(), "sink_ver_1.0"); + CompVersion compVer; + FromJson(jsonObj, compVer); + cJSON_Delete(jsonObj); + EXPECT_FALSE(compVer.name.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_CompVersion_003, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddNumberToObject(jsonObj, NAME.c_str(), 1); + cJSON_AddStringToObject(jsonObj, TYPE.c_str(), "dhType_audio"); + cJSON_AddNumberToObject(jsonObj, HANDLER.c_str(), 1); + cJSON_AddNumberToObject(jsonObj, SOURCE_VER.c_str(), 1); + cJSON_AddNumberToObject(jsonObj, SINK_VER.c_str(), 1); + CompVersion compVer; + FromJson(jsonObj, compVer); + cJSON_Delete(jsonObj); + EXPECT_TRUE(compVer.name.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_VersionInfo_001, TestSize.Level0) +{ + cJSON *jsonObj = nullptr; + VersionInfo verInfo; + FromJson(jsonObj, verInfo); + EXPECT_TRUE(verInfo.deviceId.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_VersionInfo_002, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "devid_123"); + cJSON_AddStringToObject(jsonObj, DH_VER.c_str(), "dhver_1.0"); + + cJSON * jsonArr = cJSON_CreateArray(); + if (jsonArr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + cJSON_AddItemToArray(jsonArr, cJSON_CreateString("com_ver_test")); + cJSON_AddItemToObject(jsonObj, COMP_VER.c_str(), jsonArr); + VersionInfo verInfo; + FromJson(jsonObj, verInfo); + cJSON_Delete(jsonObj); + EXPECT_FALSE(verInfo.deviceId.empty()); +} + +HWTEST_F(VersionInfoTest, FromJson_VersionInfo_003, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddNumberToObject(jsonObj, DEV_ID.c_str(), 1); + cJSON_AddNumberToObject(jsonObj, DH_VER.c_str(), 1); + VersionInfo verInfo; + FromJson(jsonObj, verInfo); + cJSON_Delete(jsonObj); + EXPECT_TRUE(verInfo.deviceId.empty()); +} +} +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn similarity index 100% rename from services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/include/version_info_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h similarity index 100% rename from services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/include/version_info_manager_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp similarity index 93% rename from services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp index 09d1bd44..0833a706 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp @@ -23,6 +23,7 @@ #include "version_manager.h" #include "dh_context.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" using namespace testing::ext; using namespace std; @@ -183,6 +184,13 @@ HWTEST_F(VersionInfoManagerTest, version_info_manager_test_005, TestSize.Level0) EXPECT_EQ(VersionInfoManager::GetInstance()->UnInit(), DH_FWK_SUCCESS); } +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_006, TestSize.Level0) +{ + VersionInfoManager::GetInstance()->dbAdapterPtr_= nullptr; + auto ret = VersionInfoManager::GetInstance()->UnInit(); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED, ret); +} + /** * @tc.name: UpdateVersionCache_001 * @tc.desc: Verify the UpdateVersionCache function @@ -206,9 +214,17 @@ HWTEST_F(VersionInfoManagerTest, UpdateVersionCache_001, TestSize.Level0) */ HWTEST_F(VersionInfoManagerTest, UpdateVersionCache_002, TestSize.Level0) { - VersionInfo versionInfo; + std::string testUdid = "111111"; + std::string testUuid = "222222"; + std::string testNetworkId = "333333"; + std::string testDeviceId = Sha256(testUuid); + VersionInfo versionInfo = { + .deviceId = testDeviceId, + .dhVersion = "1.0" + }; + DHContext::GetInstance().AddOnlineDevice(testUdid, testUuid, testNetworkId); VersionInfoManager::GetInstance()->UpdateVersionCache(versionInfo); - EXPECT_EQ(DH_FWK_SUCCESS, VersionInfoManager::GetInstance()->Init()); + EXPECT_FALSE(versionInfo.dhVersion.empty()); } /** @@ -426,5 +442,14 @@ HWTEST_F(VersionInfoManagerTest, GetVersionInfoByDeviceId_001, TestSize.Level0) EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } +HWTEST_F(VersionInfoManagerTest, GetVersionInfoByDeviceId_002, TestSize.Level0) +{ + std::string deviceId = DEV_ID_1; + VersionInfo versionInfo; + VersionInfoManager::GetInstance()->Init(); + int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + EXPECT_EQ(DH_FWK_SUCCESS, ret); +} + } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn deleted file mode 100644 index 12ed28dc..00000000 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (c) 2022-2024 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/version_info_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", - ] -} - -ohos_unittest("VersionInfoTest") { - module_out_path = module_out_path - - sources = [ "src/version_info_test.cpp" ] - - configs = [ ":module_private_config" ] - - deps = [ - "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", - "${utils_path}:distributedhardwareutils", - ] - - external_deps = [ - "cJSON:cjson", - "c_utils:utils", - "eventhandler:libeventhandler", - "kv_store:distributeddata_inner", - "safwk:system_ability_fwk", - ] - - if (powermgr_power_manager_fwk) { - external_deps += [ "power_manager:powermgr_client" ] - defines = [ "POWER_MANAGER_ENABLE" ] - } - - cflags = [ - "-Wall", - "-Werror", - "-g3", - "-Dprivate=public", - ] -} - -group("version_info_test") { - testonly = true - deps = [ ":VersionInfoTest" ] -} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/include/version_info_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/include/version_info_test.h deleted file mode 100644 index 5d08f4bf..00000000 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/include/version_info_test.h +++ /dev/null @@ -1,33 +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. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_TEST_H -#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_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/versioninfo/src/version_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/src/version_info_test.cpp deleted file mode 100644 index a6f196d5..00000000 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/src/version_info_test.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (c) 2022-2024 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 - -#include "version_info_manager.h" -#include "version_manager.h" -#include "version_info.h" -#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::SCREEN, - .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); -} - -/** - * @tc.name: FromJson_001 - * @tc.desc: Verify the FromJson ToJson function. - * @tc.type: FUNC - * @tc.require: AR000GHSCV - */ -HWTEST_F(VersionInfoTest, FromJson_001, TestSize.Level0) -{ - VersionInfo verInfo; - const char* DH_ID = "dh_id"; - const char* DEV_ID = "dev_id"; - const char* DEV_NAME = "dev_name"; - const char* DEV_TYPE = "dev_type"; - const char* DH_TYPE = "dh_type"; - const char* DH_ATTRS = "dh_attrs"; - - cJSON* json = cJSON_CreateObject(); - if (json == nullptr) { - return; - } - cJSON_AddStringToObject(json, DH_ID, "dh_id"); - cJSON_AddStringToObject(json, DEV_ID, "dev_id"); - cJSON_AddStringToObject(json, DEV_NAME, "dev_name"); - cJSON_AddStringToObject(json, DEV_TYPE, "dev_type"); - cJSON_AddStringToObject(json, DH_TYPE, "dh_type"); - cJSON_AddStringToObject(json, DH_ATTRS, "dh_attrs"); - char* cjson = cJSON_PrintUnformatted(json); - if (cjson == nullptr) { - cJSON_Delete(json); - return; - } - std::string jsonStr(cjson); - cJSON_free(cjson); - cJSON_Delete(json); - EXPECT_EQ(DH_FWK_SUCCESS, verInfo.FromJsonString(jsonStr)); -} - -/** - * @tc.name: FromJson_002 - * @tc.desc: Verify the FromJson ToJson function. - * @tc.type: FUNC - * @tc.require: AR000GHSCV - */ -HWTEST_F(VersionInfoTest, FromJson_002, TestSize.Level0) -{ - VersionInfo verInfo; - const char* DH_ID = "dh_id"; - const char* DEV_ID = "dev_id"; - const char* DEV_NAME = "dev_name"; - const char* DEV_TYPE = "dev_type"; - const char* DH_TYPE = "dh_type"; - const char* DH_ATTRS = "dh_attrs"; - cJSON* json = cJSON_CreateObject(); - if (json == nullptr) { - return; - } - cJSON_AddStringToObject(json, DH_ID, "dh_id"); - cJSON_AddStringToObject(json, DEV_ID, "dev_id"); - cJSON_AddStringToObject(json, DEV_NAME, "dev_name"); - cJSON_AddStringToObject(json, DEV_TYPE, "dev_type"); - cJSON_AddStringToObject(json, DH_TYPE, "dh_type"); - cJSON_AddStringToObject(json, DH_ATTRS, "dh_attrs"); - char* cjson = cJSON_PrintUnformatted(json); - if (cjson == nullptr) { - cJSON_Delete(json); - return; - } - std::string jsonStr(cjson); - cJSON_free(cjson); - cJSON_Delete(json); - EXPECT_EQ(DH_FWK_SUCCESS, verInfo.FromJsonString(jsonStr)); -} -} // namespace DistributedHardware -} // namespace OHOS -- Gitee