diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index cf488b7af1d2f6bb5f24260117dcdd83db700d80..f5a72492881fb703b878c10d00bd78589beaa206 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -381,6 +381,24 @@ ohos_unittest("ScreenManagerTest") { external_deps = [ "kv_store:datamgr_common" ] } +ohos_unittest("AppIdMappingConfigManagerTest") { + module_out_path = module_output_path + sources = [ "app_id_mapping_config_manager_test.cpp" ] + configs = [ ":module_private_config" ] + cflags = [ "-fno-access-control" ] + deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + external_deps = [ "kv_store:datamgr_common" ] +} + +ohos_unittest("ConnectManagerTest") { + module_out_path = module_output_path + sources = [ "connect_manager_test.cpp" ] + configs = [ ":module_private_config" ] + cflags = [ "-fno-access-control" ] + deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + external_deps = [ "kv_store:datamgr_common" ] +} + ############################################################################### group("unittest") { testonly = true @@ -388,11 +406,13 @@ group("unittest") { deps = [] deps += [ + ":AppIdMappingConfigManagerTest", ":AssetLoaderTest", ":BackupRuleManagerTest", ":BindEventTest", ":CheckerManagerTest", ":CloudInfoTest", + ":ConnectManagerTest", ":ConstantTest", ":CryptoTest", ":EventCenterTest", diff --git a/services/distributeddataservice/framework/test/app_id_mapping_config_manager_test.cpp b/services/distributeddataservice/framework/test/app_id_mapping_config_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c441ba91092d0f02833e7407498dedaf0a90c818 --- /dev/null +++ b/services/distributeddataservice/framework/test/app_id_mapping_config_manager_test.cpp @@ -0,0 +1,64 @@ +/* +* 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 "app_id_mapping/app_id_mapping_config_manager.h" + +#include + +using namespace testing::ext; +using namespace OHOS::DistributedData; +namespace OHOS::Test { +class AppIdMappingConfigManagerTest : public testing::Test {}; + +/** +* @tc.name: Convert +* @tc.desc: Generate a pair based on the input appId and accountId. +* @tc.type: FUNC +*/ +HWTEST_F(AppIdMappingConfigManagerTest, Convert01, TestSize.Level1) +{ + std::vector mapper = { + {"src1", "dst1"}, + {"src2", "dst2"}, + {"src3", "dst3"} + }; + AppIdMappingConfigManager::GetInstance().Initialize(mapper); + auto result = AppIdMappingConfigManager::GetInstance().Convert("123", "123456789"); + EXPECT_EQ(result.first, "123"); + EXPECT_EQ(result.second, "123456789"); + result = AppIdMappingConfigManager::GetInstance().Convert("src1", "987654321"); + EXPECT_EQ(result.first, "dst1"); + EXPECT_EQ(result.second, "default"); +} + +/** +* @tc.name: Convert +* @tc.desc: Convert the input appId to another id. +* @tc.type: FUNC +*/ +HWTEST_F(AppIdMappingConfigManagerTest, Convert02, TestSize.Level1) +{ + std::vector mapper = { + {"src1", "dst1"}, + {"src2", "dst2"}, + {"src3", "dst3"} + }; + AppIdMappingConfigManager::GetInstance().Initialize(mapper); + auto result = AppIdMappingConfigManager::GetInstance().Convert("321"); + EXPECT_EQ(result, "321");; + result = AppIdMappingConfigManager::GetInstance().Convert("src1"); + EXPECT_EQ(result, "dst1"); +} +} // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/framework/test/connect_manager_test.cpp b/services/distributeddataservice/framework/test/connect_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7965d9eb07b9d5de5923044375c65058de10a64d --- /dev/null +++ b/services/distributeddataservice/framework/test/connect_manager_test.cpp @@ -0,0 +1,70 @@ +/* +* 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 "communication/connect_manager.h" + +#include + +using namespace testing::ext; +using namespace OHOS::AppDistributedKv; +namespace OHOS::Test { +class ConnectManagerTest : public testing::Test {}; + +/** +* @tc.name: RegisterInstance +* @tc.desc: Register Instance. +* @tc.type: FUNC +*/ +HWTEST_F(ConnectManagerTest, RegisterInstance, TestSize.Level1) +{ + auto instance = ConnectManager::GetInstance(); + ASSERT_NE(instance, nullptr); + auto result = ConnectManager::RegisterInstance(std::make_shared()); + ASSERT_TRUE(result); + ASSERT_NE(ConnectManager::GetInstance(), nullptr); +} + +/** +* @tc.name: CloseSession +* @tc.desc: Close the session related to the networkid. +* @tc.type: FUNC +*/ +HWTEST_F(ConnectManagerTest, CloseSession, TestSize.Level1) +{ + auto ret = ConnectManager::GetInstance()->CloseSession(""); + ASSERT_FALSE(ret); + ConnectManager::GetInstance()->closeSessionTask_ = [](const std::string &networkId) { + return true; + }; + ret = ConnectManager::GetInstance()->CloseSession(""); + ASSERT_TRUE(ret); +} + +/** +* @tc.name: RegisterCloseSessionTask +* @tc.desc: Only one session closing task can be registered. +* @tc.type: FUNC +*/ +HWTEST_F(ConnectManagerTest, RegisterCloseSessionTask, TestSize.Level1) +{ + ConnectManager::GetInstance()->closeSessionTask_ = [](const std::string &networkId) { + return true; + }; + auto ret = ConnectManager::GetInstance()->RegisterCloseSessionTask([](const std::string &networkId) { + return true; + }); + ASSERT_FALSE(ret); +} +} // namespace OHOS::Test \ No newline at end of file