From 05b7d22578c924d8b076d6f78db20b05c5ccb1a2 Mon Sep 17 00:00:00 2001 From: zph Date: Sat, 7 Jun 2025 15:50:55 +0800 Subject: [PATCH 1/7] update Signed-off-by: zph --- .../udmf/preprocess/preprocess_utils.cpp | 53 ++++++++++++++++++- .../udmf/preprocess/preprocess_utils.h | 2 + .../service/udmf/udmf_service_impl.cpp | 10 +++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index a182ba3eb..2e39d382a 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -18,11 +18,15 @@ #include +#include "bundle_info.h" +#include "bundlemgr/bundle_mgr_proxy.h" #include "dds_trace.h" #include "udmf_radar_reporter.h" #include "accesstoken_kit.h" #include "device_manager_adapter.h" +#include "iservice_registry.h" #include "log_print.h" +#include "system_ability_definition.h" #include "udmf_radar_reporter.h" #include "udmf_utils.h" #include "remote_file_share.h" @@ -57,7 +61,10 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & return E_ERROR; } std::string bundleName; - GetHapBundleNameByToken(option.tokenId, bundleName); + if (GetAlterableBundleNameByTokenId(option.tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; @@ -487,5 +494,49 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) } return std::to_string(hapTokenInfo.apiVersion); } + +bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +{ + Security::AccessToken::HapTokenInfo hapInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) + == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { + return GetDirByBundleNameAndAppIndex(hapInfo.bundleName, hapInfo.instIndex, bundleName); + } + if (UTILS::IsTokenNative()) { + ZLOGD("TypeATokenTypeEnum is TOKEN_HAP"); + std::string processName; + if (GetNativeProcessNameByToken(tokenId, processName)) { + bundleName = processName; + return true; + } + } + ZLOGE("Get bundle name faild."); + return false; +} + +bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName) +{ + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return false; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + return false; + } + auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); + return false; + } + auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); + if (ret != ERR_OK) { + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", ret); + return false; + } + return true; +} } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 1532f4c49..36bc7a0ee 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -42,11 +42,13 @@ public: static void SetRecordUid(UnifiedData &data); static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); + static bool GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); private: static bool CheckUriAuthorization(const std::vector& uris, uint32_t tokenId); static int32_t GetDfsUrisFromLocal(const std::vector &uris, int32_t userId, UnifiedData &data); static bool IsFileType(std::shared_ptr record); static std::string GetSdkVersionByToken(uint32_t tokenId); + static bool GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName); }; } // namespace UDMF } // namespace OHOS diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 835db3c9e..824a6fb1a 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -406,7 +406,10 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); + if (PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str()); return E_INVALID_PARAMETERS; @@ -1046,7 +1049,10 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - PreProcessUtils::GetHapBundleNameByToken(tokenId, bundleName); + if (PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName)) { + ZLOGE("GetAlterableBundleNameByTokenId failed."); + return E_ERROR; + } UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); -- Gitee From c7c2c349298737a7679a0866ef6cdec820ef4dc6 Mon Sep 17 00:00:00 2001 From: zph Date: Sat, 7 Jun 2025 17:11:46 +0800 Subject: [PATCH 2/7] update Signed-off-by: zph --- .../udmf/preprocess/preprocess_utils.cpp | 3 ++- .../service/udmf/udmf_service_impl.cpp | 26 +++++++++++++------ .../service/udmf/udmf_service_impl.h | 1 + 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 2e39d382a..8877b67b1 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -514,7 +514,8 @@ bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::str return false; } -bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName) +bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, + std::string &dirName) { auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 824a6fb1a..00e5a8be8 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -395,14 +395,8 @@ int32_t UdmfServiceImpl::GetBatchData(const QueryOption &query, std::vector Date: Sat, 7 Jun 2025 18:33:47 +0800 Subject: [PATCH 3/7] update Signed-off-by: zph --- services/distributeddataservice/service/test/BUILD.gn | 3 +++ .../service/udmf/preprocess/preprocess_utils.cpp | 4 ++-- .../distributeddataservice/service/udmf/udmf_service_impl.cpp | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e190c68bb..95753846b 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1371,6 +1371,8 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { "access_token:libtoken_setproc", "access_token:libtokenid_sdk", "app_file_service:remote_file_share_native", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "device_manager:devicemanagersdk", "googletest:gmock_main", @@ -1378,6 +1380,7 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "hitrace:libhitracechain", + "ipc:ipc_core", "kv_store:datamgr_common", "udmf:udmf_client", ] diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 8877b67b1..43c8b040a 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -61,7 +61,7 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & return E_ERROR; } std::string bundleName; - if (GetAlterableBundleNameByTokenId(option.tokenId, bundleName)) { + if (!GetAlterableBundleNameByTokenId(option.tokenId, bundleName)) { ZLOGE("GetAlterableBundleNameByTokenId failed."); return E_ERROR; } @@ -503,7 +503,7 @@ bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::str return GetDirByBundleNameAndAppIndex(hapInfo.bundleName, hapInfo.instIndex, bundleName); } if (UTILS::IsTokenNative()) { - ZLOGD("TypeATokenTypeEnum is TOKEN_HAP"); + ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); std::string processName; if (GetNativeProcessNameByToken(tokenId, processName)) { bundleName = processName; diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 00e5a8be8..0ab5689b1 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -400,7 +400,7 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - if (PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName)) { + if (!PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName)) { ZLOGE("GetAlterableBundleNameByTokenId failed."); return E_ERROR; } @@ -1043,7 +1043,7 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - if (PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName)) { + if (!PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName)) { ZLOGE("GetAlterableBundleNameByTokenId failed."); return E_ERROR; } -- Gitee From a40b377f8d83c940d28c855ec6ee953f53938b91 Mon Sep 17 00:00:00 2001 From: zph Date: Sat, 7 Jun 2025 23:16:44 +0800 Subject: [PATCH 4/7] update Signed-off-by: zph --- .../service/test/BUILD.gn | 3 + .../service/test/mock/access_token_mock.cpp | 5 ++ .../service/test/mock/access_token_mock.h | 2 + .../test/udmf_preprocess_utils_mock_test.cpp | 51 ++++++++++++ .../service/test/udmf_service_impl_test.cpp | 77 +++++++++++++++++++ .../udmf/preprocess/preprocess_utils.cpp | 25 ++++-- .../udmf/preprocess/preprocess_utils.h | 4 +- .../service/udmf/udmf_service_impl.cpp | 15 +--- 8 files changed, 160 insertions(+), 22 deletions(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 95753846b..8bb7e4891 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1326,6 +1326,8 @@ ohos_unittest("UdmfPreProcessUtilsTest") { external_deps = [ "ability_runtime:uri_permission_mgr", "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "device_manager:devicemanagersdk", "googletest:gtest_main", @@ -1382,6 +1384,7 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { "hitrace:libhitracechain", "ipc:ipc_core", "kv_store:datamgr_common", + "samgr:samgr_proxy", "udmf:udmf_client", ] } diff --git a/services/distributeddataservice/service/test/mock/access_token_mock.cpp b/services/distributeddataservice/service/test/mock/access_token_mock.cpp index 0f70c3f39..2d80d5263 100644 --- a/services/distributeddataservice/service/test/mock/access_token_mock.cpp +++ b/services/distributeddataservice/service/test/mock/access_token_mock.cpp @@ -27,6 +27,11 @@ int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapToke return BAccessTokenKit::accessTokenkit->GetHapTokenInfo(tokenID, hapTokenInfoRes); } +int AccessTokenKit::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfo) +{ + return BAccessTokenKit::accessTokenkit->GetNativeTokenInfo(tokenID, nativeTokenInfo); +} + int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) { return BAccessTokenKit::accessTokenkit->VerifyAccessToken(tokenID, permissionName); diff --git a/services/distributeddataservice/service/test/mock/access_token_mock.h b/services/distributeddataservice/service/test/mock/access_token_mock.h index dd6e20e28..110dde08e 100644 --- a/services/distributeddataservice/service/test/mock/access_token_mock.h +++ b/services/distributeddataservice/service/test/mock/access_token_mock.h @@ -26,6 +26,7 @@ class BAccessTokenKit { public: virtual ATokenTypeEnum GetTokenTypeFlag(AccessTokenID) = 0; virtual int GetHapTokenInfo(AccessTokenID, HapTokenInfo&) = 0; + virtual int GetNativeTokenInfo(AccessTokenID, NativeTokenInfo&) = 0; virtual int VerifyAccessToken(AccessTokenID, const std::string&) = 0; BAccessTokenKit() = default; virtual ~BAccessTokenKit() = default; @@ -37,6 +38,7 @@ class AccessTokenKitMock : public BAccessTokenKit { public: MOCK_METHOD(ATokenTypeEnum, GetTokenTypeFlag, (AccessTokenID)); MOCK_METHOD(int, GetHapTokenInfo, (AccessTokenID, HapTokenInfo&)); + MOCK_METHOD(int, GetNativeTokenInfo, (AccessTokenID, NativeTokenInfo&)); MOCK_METHOD(int, VerifyAccessToken, (AccessTokenID, const std::string&)); }; } diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp index ddebf513d..c874b77c2 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -75,4 +75,55 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetInstIndex001, TestSize.Level1) bool ret = preProcessUtils.GetInstIndex(tokenId, instIndex); EXPECT_EQ(ret, true); } + +/** +* @tc.name: GetAlterableBundleNameByTokenId001 +* @tc.desc: Abnormal test of GetAlterableBundleNameByTokenId +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId001, TestSize.Level1) +{ + uint32_t tokenId = 0; + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_SHELL)); + std::string bundleName = ""; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, false); +} + +/** +* @tc.name: GetAlterableBundleNameByTokenId002 +* @tc.desc: Normal test of GetAlterableBundleNameByTokenId for native +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId002, TestSize.Level1) +{ + uint32_t tokenId = 999; + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_NATIVE)); + EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + std::string bundleName = ""; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, true); +} + +/** +* @tc.name: GetAlterableBundleNameByTokenId003 +* @tc.desc: Normal test of GetAlterableBundleNameByTokenId for hap +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId003, TestSize.Level1) +{ + uint32_t tokenId = 9999; + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + std::string bundleName = ""; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, true); +} }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index d93a55905..b81e089db 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -240,4 +240,81 @@ HWTEST_F(UdmfServiceImplTest, TransferToEntriesIfNeedTest001, TestSize.Level1) int recordSize = 2; EXPECT_EQ(data.GetRecords().size(), recordSize); } + +/** + * @tc.name: IsValidInput001 + * @tc.desc: invalid unifiedData + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, IsValidInput001, TestSize.Level1) +{ + QueryOption query; + UnifiedData unifiedData; + UnifiedKey key; + + UdmfServiceImpl impl; + bool result = impl.IsValidInput(query, unifiedData, key); + EXPECT_FALSE(result); +} + +/** + * @tc.name: IsValidInput002 + * @tc.desc: invalid key + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, IsValidInput002, TestSize.Level1) +{ + QueryOption query; + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + UnifiedKey key; + + UdmfServiceImpl impl; + bool result = impl.IsValidInput(query, unifiedData, key); + EXPECT_FALSE(result); +} + +/** + * @tc.name: IsValidInput003 + * @tc.desc: valid input + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, IsValidInput003, TestSize.Level1) +{ + QueryOption query; + query.intention = Intention::UD_INTENTION_DATA_HUB; + query.key = "udmf://DataHub/aaa/N]2fIEMbrJj@wp:jMuPa7"; + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + UnifiedKey key("udmf://DataHub/aaa/N]2fIEMbrJj@wp:jMuPa7"); + EXPECT_TRUE(key.IsValid()); + + UdmfServiceImpl impl; + bool result = impl.IsValidInput(query, unifiedData, key); + EXPECT_TRUE(result); +} + +/** + * @tc.name: IsValidInput004 + * @tc.desc: invalid intention + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, IsValidInput004, TestSize.Level1) +{ + QueryOption query; + query.intention = Intention::UD_INTENTION_DRAG; + UnifiedData unifiedData; + UnifiedKey key("udmf://drag/aaa/N]2fIEMbrJj@wp:jMuPa7"); + + UdmfServiceImpl impl; + bool result = impl.IsValidInput(query, unifiedData, key); + EXPECT_FALSE(result); +} + }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 43c8b040a..bb44a79fd 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -19,7 +19,6 @@ #include #include "bundle_info.h" -#include "bundlemgr/bundle_mgr_proxy.h" #include "dds_trace.h" #include "udmf_radar_reporter.h" #include "accesstoken_kit.h" @@ -500,7 +499,7 @@ bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::str Security::AccessToken::HapTokenInfo hapInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { - return GetDirByBundleNameAndAppIndex(hapInfo.bundleName, hapInfo.instIndex, bundleName); + return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, bundleName); } if (UTILS::IsTokenNative()) { ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); @@ -514,27 +513,37 @@ bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::str return false; } -bool PreProcessUtils::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, - std::string &dirName) +sptr PreProcessUtils::GetBundleMgr() { auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { ZLOGE("Failed to get system ability mgr."); - return false; + return nullptr; } auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); if (bundleMgrProxy == nullptr) { ZLOGE("Failed to Get BMS SA."); - return false; + return nullptr; } auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); + return nullptr; + } + return bundleManager; +} + +bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, + std::string &dirName) +{ + auto bundleManager = GetBundleMgr(); if (bundleManager == nullptr) { ZLOGE("Failed to get bundle manager"); return false; } - auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); + auto ret = bundleManager->GetSpecificBundleName(bundleName, appIndex, dirName); if (ret != ERR_OK) { - ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", ret); + ZLOGE("GetSpecificBundleName failed, ret:%{public}d", ret); return false; } return true; diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 36bc7a0ee..a969ab1e0 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -15,6 +15,7 @@ #ifndef UDMF_PREPROCESS_UTILS_H #define UDMF_PREPROCESS_UTILS_H +#include "bundlemgr/bundle_mgr_proxy.h" #include "unified_data.h" namespace OHOS { @@ -43,12 +44,13 @@ public: static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); static bool GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); + static sptr GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector& uris, uint32_t tokenId); static int32_t GetDfsUrisFromLocal(const std::vector &uris, int32_t userId, UnifiedData &data); static bool IsFileType(std::shared_ptr record); static std::string GetSdkVersionByToken(uint32_t tokenId); - static bool GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::string &dirName); + static bool GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, std::string &dirName); }; } // namespace UDMF } // namespace OHOS diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 0ab5689b1..c6480a290 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -899,17 +899,7 @@ bool UdmfServiceImpl::IsNeedTransferDeviceType(const QueryOption &query) && deviceInfo.deviceType != DEVICE_TYPE_2IN1) { return false; } - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - ZLOGE("Failed to get system ability mgr."); - return false; - } - auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundleMgrProxy == nullptr) { - ZLOGE("Failed to Get BMS SA."); - return false; - } - auto bundleManager = iface_cast(bundleMgrProxy); + auto bundleManager = PreProcessUtils::GetBundleMgr(); if (bundleManager == nullptr) { ZLOGE("Failed to get bundle manager"); return false; @@ -1156,8 +1146,7 @@ bool UdmfServiceImpl::IsValidInput(const QueryOption &query, UnifiedData &unifie return false; } std::string intention = FindIntentionMap(query.intention); - if (!IsValidOptionsNonDrag(key, intention) || - key.intention != UD_INTENTION_MAP.at(UD_INTENTION_DATA_HUB)) { + if (!IsValidOptionsNonDrag(key, intention) || key.intention != UD_INTENTION_MAP.at(UD_INTENTION_DATA_HUB)) { ZLOGE("Invalid params: key.intention = %{public}s, intention = %{public}s", key.intention.c_str(), intention.c_str()); return false; -- Gitee From 7bf3dc81ebf1ba8c024f2390e58dbe4d3c6b6d4c Mon Sep 17 00:00:00 2001 From: zph Date: Sun, 8 Jun 2025 00:03:11 +0800 Subject: [PATCH 5/7] update Signed-off-by: zph --- .../service/udmf/preprocess/preprocess_utils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index bb44a79fd..18943ddd2 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -541,9 +541,9 @@ bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32 ZLOGE("Failed to get bundle manager"); return false; } - auto ret = bundleManager->GetSpecificBundleName(bundleName, appIndex, dirName); + auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); if (ret != ERR_OK) { - ZLOGE("GetSpecificBundleName failed, ret:%{public}d", ret); + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", ret); return false; } return true; -- Gitee From fbe1fe734a41e36733a64bb96d113badd0231c0f Mon Sep 17 00:00:00 2001 From: zph Date: Sun, 8 Jun 2025 16:30:42 +0800 Subject: [PATCH 6/7] update Signed-off-by: zph --- .../test/udmf_preprocess_utils_mock_test.cpp | 12 ++++++------ .../udmf/preprocess/preprocess_utils.cpp | 19 ++++++++++--------- .../udmf/preprocess/preprocess_utils.h | 4 ++-- .../service/udmf/udmf_service_impl.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp index c874b77c2..93ea1516b 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -78,7 +78,7 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetInstIndex001, TestSize.Level1) /** * @tc.name: GetAlterableBundleNameByTokenId001 -* @tc.desc: Abnormal test of GetAlterableBundleNameByTokenId +* @tc.desc: Abnormal test of GetSpecificBundleNameByTokenId * @tc.type: FUNC * @tc.require: */ @@ -89,13 +89,13 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId001, TestSi EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_SHELL)); std::string bundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); EXPECT_EQ(ret, false); } /** * @tc.name: GetAlterableBundleNameByTokenId002 -* @tc.desc: Normal test of GetAlterableBundleNameByTokenId for native +* @tc.desc: Normal test of GetSpecificBundleNameByTokenId for native * @tc.type: FUNC * @tc.require: */ @@ -107,13 +107,13 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId002, TestSi EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); EXPECT_EQ(ret, true); } /** * @tc.name: GetAlterableBundleNameByTokenId003 -* @tc.desc: Normal test of GetAlterableBundleNameByTokenId for hap +* @tc.desc: Normal test of GetSpecificBundleNameByTokenId for hap * @tc.type: FUNC * @tc.require: */ @@ -123,7 +123,7 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId003, TestSi EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetAlterableBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); EXPECT_EQ(ret, true); } }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 18943ddd2..5400c660c 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -60,8 +60,8 @@ int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption & return E_ERROR; } std::string bundleName; - if (!GetAlterableBundleNameByTokenId(option.tokenId, bundleName)) { - ZLOGE("GetAlterableBundleNameByTokenId failed."); + if (!GetSpecificBundleNameByTokenId(option.tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId); return E_ERROR; } std::string intention = it->second; @@ -494,7 +494,7 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) return std::to_string(hapTokenInfo.apiVersion); } -bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) { Security::AccessToken::HapTokenInfo hapInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) @@ -505,11 +505,11 @@ bool PreProcessUtils::GetAlterableBundleNameByTokenId(uint32_t tokenId, std::str ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); std::string processName; if (GetNativeProcessNameByToken(tokenId, processName)) { - bundleName = processName; + bundleName = std::move(processName); return true; } } - ZLOGE("Get bundle name faild."); + ZLOGE("Get bundle name faild, tokenid:%{public}u", tokenId); return false; } @@ -534,16 +534,17 @@ sptr PreProcessUtils::GetBundleMgr() } bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, - std::string &dirName) + std::string &specificBundleName) { auto bundleManager = GetBundleMgr(); if (bundleManager == nullptr) { - ZLOGE("Failed to get bundle manager"); + ZLOGE("Failed to get bundle manager, bundleName:%{public}s, appIndex:%{public}d", bundleName.c_str(), appIndex); return false; } - auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dirName); + auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, specificBundleName); if (ret != ERR_OK) { - ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d", ret); + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d, bundleName:%{public}s, appIndex:%{public}d", + ret, bundleName.c_str(), appIndex); return false; } return true; diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index a969ab1e0..ac7764a94 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -43,14 +43,14 @@ public: static void SetRecordUid(UnifiedData &data); static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); - static bool GetAlterableBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); + static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); static sptr GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector& uris, uint32_t tokenId); static int32_t GetDfsUrisFromLocal(const std::vector &uris, int32_t userId, UnifiedData &data); static bool IsFileType(std::shared_ptr record); static std::string GetSdkVersionByToken(uint32_t tokenId); - static bool GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, std::string &dirName); + static bool GetSpecificBundleName(const std::string &bundleName, int32_t appIndex, std::string &specificBundleName); }; } // namespace UDMF } // namespace OHOS diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index c6480a290..a869aa104 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -400,8 +400,8 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - if (!PreProcessUtils::GetAlterableBundleNameByTokenId(query.tokenId, bundleName)) { - ZLOGE("GetAlterableBundleNameByTokenId failed."); + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return E_ERROR; } if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { @@ -1033,8 +1033,8 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - if (!PreProcessUtils::GetAlterableBundleNameByTokenId(tokenId, bundleName)) { - ZLOGE("GetAlterableBundleNameByTokenId failed."); + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", tokenId); return E_ERROR; } UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); -- Gitee From 7678288a31067829381a63f87122dfb6a02fbe20 Mon Sep 17 00:00:00 2001 From: zph Date: Sun, 8 Jun 2025 16:57:59 +0800 Subject: [PATCH 7/7] update Signed-off-by: zph --- services/distributeddataservice/service/test/BUILD.gn | 2 +- .../service/test/udmf_preprocess_utils_test.cpp | 4 ++-- .../service/test/udmf_run_time_store_test.cpp | 4 ++-- .../service/udmf/preprocess/preprocess_utils.cpp | 2 +- .../service/udmf/preprocess/preprocess_utils.h | 2 +- .../distributeddataservice/service/udmf/udmf_service_impl.cpp | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 8bb7e4891..35a3028a7 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1382,7 +1382,7 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "hitrace:libhitracechain", - "ipc:ipc_core", + "ipc:ipc_single", "kv_store:datamgr_common", "samgr:samgr_proxy", "udmf:udmf_client", diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp index 38a85bdfa..8ffe52b9e 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp @@ -29,7 +29,7 @@ public: /** * @tc.name: RuntimeDataImputation001 -* @tc.desc: Abnormal test of RuntimeDataImputation, option is invalid +* @tc.desc: Abnormal test of FillRuntimeInfo, option is invalid * @tc.type: FUNC * @tc.require: */ @@ -38,7 +38,7 @@ HWTEST_F(UdmfPreProcessUtilsTest, RuntimeDataImputation001, TestSize.Level1) UnifiedData data; CustomOption option; PreProcessUtils preProcessUtils; - int32_t ret = preProcessUtils.RuntimeDataImputation(data, option); + int32_t ret = preProcessUtils.FillRuntimeInfo(data, option); EXPECT_EQ(ret, E_ERROR); } diff --git a/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp b/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp index 962adbcbf..4dbf15f48 100644 --- a/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp +++ b/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp @@ -681,7 +681,7 @@ HWTEST_F(UdmfRunTimeStoreTest, GetRuntime001, TestSize.Level1) { UnifiedData inputData; CustomOption option = {.intention = Intention::UD_INTENTION_DRAG}; - auto status = PreProcessUtils::RuntimeDataImputation(inputData, option); + auto status = PreProcessUtils::FillRuntimeInfo(inputData, option); EXPECT_EQ(status, E_OK); auto key = inputData.GetRuntime()->key.GetUnifiedKey(); @@ -707,7 +707,7 @@ HWTEST_F(UdmfRunTimeStoreTest, GetRuntime002, TestSize.Level1) { UnifiedData inputData; CustomOption option = {.intention = Intention::UD_INTENTION_DRAG}; - auto status = PreProcessUtils::RuntimeDataImputation(inputData, option); + auto status = PreProcessUtils::FillRuntimeInfo(inputData, option); EXPECT_EQ(status, E_OK); auto key = inputData.GetRuntime()->key.GetUnifiedKey(); diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 5400c660c..8141886ba 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -53,7 +53,7 @@ using namespace Security::AccessToken; using namespace OHOS::AppFileService::ModuleRemoteFileShare; using namespace RadarReporter; -int32_t PreProcessUtils::RuntimeDataImputation(UnifiedData &data, CustomOption &option) +int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option) { auto it = UD_INTENTION_MAP.find(option.intention); if (it == UD_INTENTION_MAP.end()) { diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index ac7764a94..5d602b626 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -22,7 +22,7 @@ namespace OHOS { namespace UDMF { class PreProcessUtils { public: - static int32_t RuntimeDataImputation(UnifiedData &data, CustomOption &option); + static int32_t FillRuntimeInfo(UnifiedData &data, CustomOption &option); static std::string GenerateId(); static time_t GetTimestamp(); static int32_t GetHapUidByToken(uint32_t tokenId, int &userId); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index a869aa104..cd9940cb7 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -132,7 +132,7 @@ int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData } // imput runtime info before put it into store and save one privilege - if (PreProcessUtils::RuntimeDataImputation(unifiedData, option) != E_OK) { + if (PreProcessUtils::FillRuntimeInfo(unifiedData, option) != E_OK) { ZLOGE("Imputation failed"); return E_ERROR; } @@ -1062,7 +1062,7 @@ int32_t UdmfServiceImpl::PushDelayData(const std::string &key, UnifiedData &unif .intention = UD_INTENTION_DRAG, .tokenId = static_cast(IPCSkeleton::GetCallingTokenID()), }; - if (PreProcessUtils::RuntimeDataImputation(unifiedData, option) != E_OK) { + if (PreProcessUtils::FillRuntimeInfo(unifiedData, option) != E_OK) { ZLOGE("Imputation failed"); return E_ERROR; } -- Gitee