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 index c441ba91092d0f02833e7407498dedaf0a90c818..5d99495c8565a325f5287689b22df192fdf33fdd 100644 --- a/services/distributeddataservice/framework/test/app_id_mapping_config_manager_test.cpp +++ b/services/distributeddataservice/framework/test/app_id_mapping_config_manager_test.cpp @@ -14,6 +14,7 @@ */ #include "app_id_mapping/app_id_mapping_config_manager.h" +#include "access_check/app_access_check_config_manager.h" #include @@ -22,6 +23,8 @@ using namespace OHOS::DistributedData; namespace OHOS::Test { class AppIdMappingConfigManagerTest : public testing::Test {}; +class AppAccessCheckConfigManagerTest : public testing::Test {}; + /** * @tc.name: Convert * @tc.desc: Generate a pair based on the input appId and accountId. @@ -61,4 +64,19 @@ HWTEST_F(AppIdMappingConfigManagerTest, Convert02, TestSize.Level1) result = AppIdMappingConfigManager::GetInstance().Convert("src1"); EXPECT_EQ(result, "dst1"); } + +/** +* @tc.name: IsTrust +* @tc.desc: Test the IsTrust function. +* Cover the cases where the appid does not exist, exists, and exists with the same value as the saved one. +* @tc.type: FUNC +*/ +HWTEST_F(AppAccessCheckConfigManagerTest, IsTrust, TestSize.Level1) +{ + ASSERT_FALSE(AppAccessCheckConfigManager::GetInstance().IsTrust("123")); + AppAccessCheckConfigManager::GetInstance().appMapper_["123"] = "321"; + ASSERT_FALSE(AppAccessCheckConfigManager::GetInstance().IsTrust("123")); + AppAccessCheckConfigManager::GetInstance().appMapper_["321"] = "321"; + ASSERT_TRUE(AppAccessCheckConfigManager::GetInstance().IsTrust("321")); +} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 7c20c4aa2ad038db6804228edef4467b3de9a991..eaac9fbd7caf85db928fe11b546f8b5af1cf6808 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -53,6 +53,7 @@ config("module_private_config") { } ohos_unittest("CloudDataTest") { + branch_protector_ret = "pac_ret" sanitize = { cfi = true cfi_cross_dso = true diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 40d39394640128d486d1f61fc2476fa29ca4ab90..8b8ae80350a4e178abef9081f8a7b118f9e83b6e 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -55,6 +55,7 @@ #include "store/general_value.h" #include "store/store_info.h" #include "sync_manager.h" +#include "sync_strategies/network_sync_strategy.h" #include "token_setproc.h" using namespace testing::ext; @@ -841,7 +842,6 @@ HWTEST_F(CloudDataTest, UpdateStartSyncInfo, TestSize.Level0) MetaDataManager::GetInstance().LoadMeta(CloudLastSyncInfo::GetKey(user, TEST_CLOUD_BUNDLE, ""), lastSyncInfos, true); EXPECT_TRUE(!lastSyncInfos.empty()); - printf("code: %d", lastSyncInfos[0].code); EXPECT_TRUE(lastSyncInfos[0].code == -1); EXPECT_TRUE(lastSyncInfos[0].startTime != 0); EXPECT_TRUE(lastSyncInfos[0].finishTime != 0); @@ -3151,5 +3151,71 @@ HWTEST_F(CloudDataTest, GetValidGeneralCode, TestSize.Level0) ret = CloudData::SyncManager::ConvertValidGeneralCode(E_SYNC_TASK_MERGED); EXPECT_TRUE(ret == E_ERROR); } + +/** +* @tc.name: CheckSyncAction01 +* @tc.desc: Test the CheckSyncAction function, +* covering scenarios where the network is unavailable branches. +* @tc.type: FUNC +*/ +HWTEST_F(CloudDataTest, CheckSyncAction01, TestSize.Level1) +{ + delegate_.isNetworkAvailable_ = false; + CloudData::NetworkSyncStrategy strategy; + StoreInfo storeInfo; + auto result = strategy.CheckSyncAction(storeInfo); + EXPECT_EQ(result, E_NETWORK_ERROR); +} + +/** +* @tc.name: CheckSyncAction02 +* @tc.desc: Test the CheckSyncAction function, +* covering scenarios where user is 0 branches. +* @tc.type: FUNC +*/ +HWTEST_F(CloudDataTest, CheckSyncAction02, TestSize.Level1) +{ + CloudData::NetworkSyncStrategy strategy; + StoreInfo storeInfo; + storeInfo.user = 0; + delegate_.isNetworkAvailable_ = true; + auto result = strategy.CheckSyncAction(storeInfo); + EXPECT_EQ(result, E_OK); +} + +/** +* @tc.name: CheckSyncAction03 +* @tc.desc: Test the CheckSyncAction function, +* covering scenarios where both equal user branches. +* @tc.type: FUNC +*/ +HWTEST_F(CloudDataTest, CheckSyncAction03, TestSize.Level1) +{ + CloudData::NetworkSyncStrategy strategy; + StoreInfo storeInfo; + storeInfo.user = 0; + delegate_.isNetworkAvailable_ = true; + storeInfo.user = 100; + auto result = strategy.CheckSyncAction(storeInfo); + EXPECT_EQ(result, E_BLOCKED_BY_NETWORK_STRATEGY); +} + +/** +* @tc.name: CheckSyncAction04 +* @tc.desc: Test the CheckSyncAction function, +* covering scenarios where both unequal user branches. +* @tc.type: FUNC +*/ +HWTEST_F(CloudDataTest, CheckSyncAction04, TestSize.Level1) +{ + delegate_.isNetworkAvailable_ = false; + CloudData::NetworkSyncStrategy strategy; + StoreInfo storeInfo; + delegate_.isNetworkAvailable_ = true; + storeInfo.user = 100; + strategy.user_ = 100; + auto result = strategy.CheckSyncAction(storeInfo); + EXPECT_EQ(result, E_BLOCKED_BY_NETWORK_STRATEGY); +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/network_delegate_mock.cpp b/services/distributeddataservice/service/test/mock/network_delegate_mock.cpp index cfab2309bdb8df8927d2991961301889bb038938..d0b435bda83eeb70f2522d317700af787c754a2f 100644 --- a/services/distributeddataservice/service/test/mock/network_delegate_mock.cpp +++ b/services/distributeddataservice/service/test/mock/network_delegate_mock.cpp @@ -23,7 +23,7 @@ bool NetworkDelegateMock::IsNetworkAvailable() NetworkDelegate::NetworkType NetworkDelegateMock::GetNetworkType(bool retrieve) { - return NetworkDelegate::NONE; + return networkType; } void NetworkDelegateMock::RegOnNetworkChange() diff --git a/services/distributeddataservice/service/test/mock/network_delegate_mock.h b/services/distributeddataservice/service/test/mock/network_delegate_mock.h index 0fcea3d287c291d3f87929eff26337fdcac7fc63..24e894aaddc2054dc4d82f8b631a5f5937d964c5 100644 --- a/services/distributeddataservice/service/test/mock/network_delegate_mock.h +++ b/services/distributeddataservice/service/test/mock/network_delegate_mock.h @@ -26,6 +26,7 @@ public: void RegOnNetworkChange() override; void BindExecutor(std::shared_ptr executors) override; bool isNetworkAvailable_ = true; + NetworkType networkType = NetworkType::NONE; virtual ~NetworkDelegateMock() = default; }; } // namespace DistributedData