diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn index 3445f1cade269ec1e0539610addcc98d3daa5340..cd278971f1f6d452ea4338082cd46cc6e4e40743 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2024 Huawei Device Co., Ltd. +# Copyright (c) 2021-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -26,6 +26,7 @@ config("module_private_config") { "${utils_path}/include/eventbus", "${services_path}/distributedhardwarefwkservice/include", "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/task", "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", @@ -60,6 +61,32 @@ ohos_unittest("CapabilityInfoTest") { ] } +ohos_unittest("DBAdapterTest") { + module_out_path = module_out_path + + sources = [ "db_adapter_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", + "kv_store:distributeddata_inner", + ] +} + ohos_unittest("LocalCapInfoMgrTest") { module_out_path = module_out_path @@ -205,6 +232,7 @@ group("resource_manager_test") { testonly = true deps = [ ":CapabilityInfoTest", + ":DBAdapterTest", ":LocalCapInfoMgrTest", ":MetaCapInfoTest", ":MetaInfoMgrTest", diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/db_adapter_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/db_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..304d31e935862aedea8d42bc55d1ad055686cad3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/db_adapter_test.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "db_adapter.h" + +#include + +#include "cJSON.h" + +#include "distributed_kv_data_manager.h" +#include "kvstore_observer.h" + +#include "constants.h" +#include "capability_info_manager.h" +#include "distributed_hardware_errno.h" +#include "dh_utils_tool.h" +#include "dh_context.h" +#include "meta_info_manager.h" +#include "version_info_manager.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +namespace { +const string DEV_ID_TEST = "123456789"; +const string DH_ID_TEST = "Camera_0"; +const string UUID_TEST = "111111"; +const string UDID_TEST = "222222"; +const string NETWORKID_TEST = "333333"; +} + +class DBAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DBAdapterTest::SetUp() +{ +} + +void DBAdapterTest::TearDown() {} + +void DBAdapterTest::SetUpTestCase() +{ + MetaInfoManager::GetInstance()->Init(); +} + +void DBAdapterTest::TearDownTestCase() +{ + MetaInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(DBAdapterTest, GetNetworkIdByKey_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string key = ""; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->GetNetworkIdByKey(key); + EXPECT_TRUE(ret.empty()); + + key = DEV_ID_TEST + RESOURCE_SEPARATOR + DH_ID_TEST; + ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->GetNetworkIdByKey(key); + EXPECT_TRUE(ret.empty()); +} + +HWTEST_F(DBAdapterTest, SyncByNotFound_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string key = ""; + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->dbAdapterPtr_->SyncByNotFound(key)); +} + +HWTEST_F(DBAdapterTest, SyncByNotFound_002, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, NETWORKID_TEST); + std::string devId = Sha256(UUID_TEST); + std::string key = devId + RESOURCE_SEPARATOR + DH_ID_TEST; + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->dbAdapterPtr_->SyncByNotFound(key)); + DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId(NETWORKID_TEST); +} + +HWTEST_F(DBAdapterTest, SyncDBForRecover_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->dbAdapterPtr_->SyncDBForRecover()); + + CapabilityInfoManager::GetInstance()->Init(); + ASSERT_TRUE(CapabilityInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + ASSERT_NO_FATAL_FAILURE(CapabilityInfoManager::GetInstance()->dbAdapterPtr_->SyncDBForRecover()); + CapabilityInfoManager::GetInstance()->UnInit(); + + VersionInfoManager::GetInstance()->Init(); + ASSERT_TRUE(VersionInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + ASSERT_NO_FATAL_FAILURE(VersionInfoManager::GetInstance()->dbAdapterPtr_->SyncDBForRecover()); + VersionInfoManager::GetInstance()->UnInit(); +} + +HWTEST_F(DBAdapterTest, RemoveDeviceData_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string deviceId = ""; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->RemoveDeviceData(deviceId); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(DBAdapterTest, RemoveDataByKey_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string key = ""; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->RemoveDataByKey(key); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); +} + +HWTEST_F(DBAdapterTest, GetEntriesByKeys_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::vector keys; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->GetEntriesByKeys(keys); + EXPECT_TRUE(ret.empty()); +} + +HWTEST_F(DBAdapterTest, GetEntriesByKeys_002, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::vector keys; + std::string key = "123456"; + keys.push_back(key); + MetaInfoManager::GetInstance()->dbAdapterPtr_->kvStoragePtr_ = nullptr; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->GetEntriesByKeys(keys); + EXPECT_TRUE(ret.empty()); +} + +HWTEST_F(DBAdapterTest, SyncDataByNetworkId_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string networkId = "123456789"; + MetaInfoManager::GetInstance()->dbAdapterPtr_->kvStoragePtr_ = nullptr; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->SyncDataByNetworkId(networkId); + EXPECT_EQ(false, ret); +} + +HWTEST_F(DBAdapterTest, ClearDataByPrefix_001, TestSize.Level0) +{ + ASSERT_TRUE(MetaInfoManager::GetInstance()->dbAdapterPtr_ != nullptr); + std::string prefix = "123###456"; + MetaInfoManager::GetInstance()->dbAdapterPtr_->kvStoragePtr_ = nullptr; + auto ret = MetaInfoManager::GetInstance()->dbAdapterPtr_->ClearDataByPrefix(prefix); + EXPECT_EQ(false, ret); +} +} +} \ 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 index 07d5d007cae08e16b2132ec51318843f1ae84bb5..b1c8b486ffbffa62f7fbd0ed30a45a02880a8c6f 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/meta_info_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,9 +19,12 @@ #include "constants.h" #include "dh_utils_tool.h" +#include "dh_context.h" #include "distributed_hardware_errno.h" #include "meta_capability_info.h" #include "meta_info_manager.h" +#include "task_board.h" +#include "impl_utils.h" using namespace testing::ext; using namespace std; @@ -30,6 +33,7 @@ namespace OHOS { namespace DistributedHardware { namespace { constexpr uint16_t DEV_TYPE_TEST = 14; + constexpr uint32_t MAX_DB_RECORD_LENGTH = 10005; } class MetaInfoMgrTest : public testing::Test { @@ -122,6 +126,19 @@ HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_001, TestSize.Level0) MetaInfoManager::GetInstance()->UnInit(); } +HWTEST_F(MetaInfoMgrTest, SyncRemoteMetaInfos_002, TestSize.Level0) +{ + std::string udid = "111111"; + std::string uuid = "222222"; + std::string networkId = "333333"; + DHContext::GetInstance().AddOnlineDevice(udid, uuid, networkId); + MetaInfoManager::GetInstance()->Init(); + auto ret = MetaInfoManager::GetInstance()->SyncRemoteMetaInfos(); + EXPECT_EQ(DH_FWK_SUCCESS, ret); + MetaInfoManager::GetInstance()->UnInit(); + DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId(networkId); +} + HWTEST_F(MetaInfoMgrTest, GetDataByKeyPrefix_001, TestSize.Level0) { std::string keyPrefix = ""; @@ -310,6 +327,251 @@ HWTEST_F(MetaInfoMgrTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0) EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); } +HWTEST_F(MetaInfoMgrTest, OnChange_001, TestSize.Level0) +{ + DistributedKv::Entry insert, update, del; + std::vector inserts, updates, deleteds; + inserts.push_back(insert); + updates.push_back(update); + deleteds.push_back(del); + DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn)); +} + +HWTEST_F(MetaInfoMgrTest, OnChange_002, TestSize.Level0) +{ + DistributedKv::Entry insert, update, del; + std::vector inserts, updates, deleteds; + std::string tempStr; + for (int32_t i = 1; i < MAX_DB_RECORD_LENGTH; i++) { + tempStr = std::to_string(i); + insert.key = tempStr.c_str(); + update.key = tempStr.c_str(); + del.key = tempStr.c_str(); + insert.value = tempStr.c_str(); + update.value = tempStr.c_str(); + del.value = tempStr.c_str(); + inserts.push_back(insert); + updates.push_back(update); + deleteds.push_back(del); + } + DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn)); +} + +HWTEST_F(MetaInfoMgrTest, OnChange_003, TestSize.Level0) +{ + DistributedKv::Entry insert, update, del; + insert.key = "insert_key"; + update.key = "update_key"; + del.key = "del_key"; + insert.value = "insert_value"; + update.value = "update_value"; + del.value = "del_value"; + std::vector inserts, updates, deleteds; + inserts.push_back(insert); + updates.push_back(update); + deleteds.push_back(del); + + DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), "", true); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(changeIn)); +} + +HWTEST_F(MetaInfoMgrTest, OnChange_004, TestSize.Level0) +{ + DistributedKv::DataOrigin origin; + origin.id = {}; + origin.store = ""; + MetaInfoManager::Keys keys; + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(origin, std::move(keys))); +} + +HWTEST_F(MetaInfoMgrTest, OnChange_005, TestSize.Level0) +{ + DistributedKv::DataOrigin origin; + origin.id = {}; + origin.store = ""; + MetaInfoManager::Keys keys; + keys[MetaInfoManager::OP_INSERT] = {"insert"}; + keys[MetaInfoManager::OP_UPDATE] = {"update"}; + keys[MetaInfoManager::OP_DELETE] = {"delete"}; + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->OnChange(origin, std::move(keys))); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_001, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + std::vector insertRecords; + DistributedKv::Entry entry; + entry.key = "insert"; + entry.value = jsonStr.c_str(); + insertRecords.push_back(entry); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords)); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_002, TestSize.Level0) +{ + std::string uuid = "123456789"; + std::string deviceId = Sha256(uuid); + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str()); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + DHContext::GetInstance().AddOnlineDevice("111111", uuid, ""); + std::vector insertRecords; + DistributedKv::Entry entry; + entry.key = "insert"; + entry.value = jsonStr.c_str(); + insertRecords.push_back(entry); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords)); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityAddChange_003, TestSize.Level0) +{ + std::string uuid = "123456789"; + std::string deviceId = Sha256(uuid); + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str()); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + DHContext::GetInstance().AddOnlineDevice("111111", uuid, "222222"); + std::vector insertRecords; + DistributedKv::Entry entry; + entry.key = "insert"; + entry.value = jsonStr.c_str(); + insertRecords.push_back(entry); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityAddChange(insertRecords)); + DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId("222222"); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_001, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + std::vector updateRecords; + DistributedKv::Entry update; + update.key = "update"; + update.value = jsonStr.c_str(); + updateRecords.push_back(update); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords)); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_002, TestSize.Level0) +{ + std::string uuid = "123456789"; + std::string deviceId = Sha256(uuid); + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str()); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + std::vector updateRecords; + DHContext::GetInstance().AddOnlineDevice("111111", uuid, ""); + DistributedKv::Entry update; + update.key = "update"; + update.value = jsonStr.c_str(); + updateRecords.push_back(update); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords)); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityUpdateChange_003, TestSize.Level0) +{ + std::string uuid = "123456789"; + std::string deviceId = Sha256(uuid); + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), deviceId.c_str()); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + std::vector updateRecords; + DHContext::GetInstance().AddOnlineDevice("111111", uuid, "222222"); + DistributedKv::Entry update; + update.key = "update"; + update.value = jsonStr.c_str(); + updateRecords.push_back(update); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords)); + + std::string enabledDeviceKey = deviceId + RESOURCE_SEPARATOR + "111111"; + TaskParam taskParam; + TaskBoard::GetInstance().SaveEnabledDevice(enabledDeviceKey, taskParam); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityUpdateChange(updateRecords)); + + DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId("222222"); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + +HWTEST_F(MetaInfoMgrTest, HandleMetaCapabilityDeleteChange_001, TestSize.Level0) +{ + cJSON *jsonObj = cJSON_CreateObject(); + ASSERT_TRUE(jsonObj != nullptr); + cJSON_AddStringToObject(jsonObj, DH_ID.c_str(), "111111"); + cJSON_AddStringToObject(jsonObj, DEV_ID.c_str(), "222222"); + char* cjson = cJSON_PrintUnformatted(jsonObj); + if (cjson == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr(cjson); + std::vector deleteRecords; + DistributedKv::Entry del; + del.key = "update"; + del.value = jsonStr.c_str(); + deleteRecords.push_back(del); + ASSERT_NO_FATAL_FAILURE(MetaInfoManager::GetInstance()->HandleMetaCapabilityDeleteChange(deleteRecords)); + cJSON_free(cjson); + cJSON_Delete(jsonObj); +} + HWTEST_F(MetaInfoMgrTest, GetEntriesByKeys_001, TestSize.Level0) { std::vector keys;