diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e190c68bb8796c04acf7a74c4eb2b72ab086460c..35a3028a72b6acc285ebda905ea488cd888803c9 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", @@ -1371,6 +1373,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,7 +1382,9 @@ ohos_unittest("UdmfPreProcessUtilsMockTest") { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "hitrace:libhitracechain", + "ipc:ipc_single", "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 0f70c3f394dab187077ba3d8e5d340e838c3201e..2d80d5263eece63ade5d2c96262040d4b43c5def 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 dd6e20e286f1ccbf7f07fdd5b2f991c341a9ea0e..110dde08e624007cf6b03e8ea0bbdf3167f5b36c 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 ddebf513d36b068f7297630f6707df2a13bc2a18..93ea1516b8451fc3df93eaf167de190a0c963390 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 GetSpecificBundleNameByTokenId +* @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.GetSpecificBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, false); +} + +/** +* @tc.name: GetAlterableBundleNameByTokenId002 +* @tc.desc: Normal test of GetSpecificBundleNameByTokenId 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.GetSpecificBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, true); +} + +/** +* @tc.name: GetAlterableBundleNameByTokenId003 +* @tc.desc: Normal test of GetSpecificBundleNameByTokenId 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.GetSpecificBundleNameByTokenId(tokenId, bundleName); + EXPECT_EQ(ret, true); +} }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp index 38a85bdfa64220ccbade2fe1e476733ac5625e11..8ffe52b9ed30348a0eea60fc8c09048a8cb42d5a 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 962adbcbfac4c5876b010604437a81118e42c084..4dbf15f489d6138470c81b6329fdd84b2d9487c2 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/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index d93a559053eff8627f4732a64cfbba2c83a59132..b81e089db65857dfd94524a2ec40e48524bfa929 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 a182ba3eb885d75711033f5225ea4d6bec06b2ac..8141886bae498abdc6e804acf86e3f80cfda7241 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -18,11 +18,14 @@ #include +#include "bundle_info.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" @@ -50,14 +53,17 @@ 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()) { return E_ERROR; } std::string bundleName; - GetHapBundleNameByToken(option.tokenId, bundleName); + if (!GetSpecificBundleNameByTokenId(option.tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId); + return E_ERROR; + } std::string intention = it->second; UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; @@ -487,5 +493,61 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) } return std::to_string(hapTokenInfo.apiVersion); } + +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +{ + Security::AccessToken::HapTokenInfo hapInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) + == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { + return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, bundleName); + } + if (UTILS::IsTokenNative()) { + ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); + std::string processName; + if (GetNativeProcessNameByToken(tokenId, processName)) { + bundleName = std::move(processName); + return true; + } + } + ZLOGE("Get bundle name faild, tokenid:%{public}u", tokenId); + return false; +} + +sptr PreProcessUtils::GetBundleMgr() +{ + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return nullptr; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + 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 &specificBundleName) +{ + auto bundleManager = GetBundleMgr(); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager, bundleName:%{public}s, appIndex:%{public}d", bundleName.c_str(), appIndex); + return false; + } + auto ret = bundleManager->GetDirByBundleNameAndAppIndex(bundleName, appIndex, specificBundleName); + if (ret != ERR_OK) { + ZLOGE("GetDirByBundleNameAndAppIndex failed, ret:%{public}d, bundleName:%{public}s, appIndex:%{public}d", + ret, bundleName.c_str(), appIndex); + 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 1532f4c49136116310619fbb39865319e80e42e6..5d602b6267d4e6b5b48e7d5c371de82bded66dc4 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -15,13 +15,14 @@ #ifndef UDMF_PREPROCESS_UTILS_H #define UDMF_PREPROCESS_UTILS_H +#include "bundlemgr/bundle_mgr_proxy.h" #include "unified_data.h" 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); @@ -42,11 +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 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 &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 835db3c9ea0409ba2f3957ab8b419ad648ecbe48..cd9940cb7a0559c549e51b8ce5781a2e9e283270 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; } @@ -395,18 +395,15 @@ int32_t UdmfServiceImpl::GetBatchData(const QueryOption &query, std::vectorGetSystemAbility(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; @@ -1046,7 +1033,10 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - PreProcessUtils::GetHapBundleNameByToken(tokenId, bundleName); + 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); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); @@ -1072,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; } @@ -1148,5 +1138,20 @@ int32_t UdmfServiceImpl::GetDataIfAvailable(const std::string &key, const DataLo dataLoadCallback_.Erase(key); return E_OK; } + +bool UdmfServiceImpl::IsValidInput(const QueryOption &query, UnifiedData &unifiedData, UnifiedKey &key) +{ + if (!unifiedData.IsValid() || !key.IsValid()) { + ZLOGE("Data or key is invalid, key = %{public}s", query.key.c_str()); + return false; + } + std::string intention = FindIntentionMap(query.intention); + 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; + } + return true; +} } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 09127a317b0703a0a92ec53d9097d0d66134ab3b..772ddaa2d160a37f2f21e521d9c0ff378d7e8f52 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -76,6 +76,7 @@ private: bool IsFileMangerIntention(const std::string &intention); std::string FindIntentionMap(const Intention &queryintention); bool IsValidOptionsNonDrag(UnifiedKey &key, const std::string &intention); + bool IsValidInput(const QueryOption &query, UnifiedData &unifiedData, UnifiedKey &key); class Factory { public: Factory();