diff --git a/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn b/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..93bb21bc3ce2157e61f30ba3750eebb27ea6028d --- /dev/null +++ b/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn @@ -0,0 +1,45 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") + +ohos_source_set("distributedata_bundlemgr") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + boundary_sanitize = true + ubsan = true + } + sources = [ "bundlemgr_adapter.cpp" ] + + cflags_cc = [ "-fvisibility=hidden" ] + + if (build_public_version) { + cflags_cc += [ "-DCONFIG_PUBLIC_VERSION" ] + } + + include_dirs = [ "${data_service_path}/adapter/include/bundle_mgr" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "hilog:libhilog", + "kv_store:datamgr_common", + ] + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] +} diff --git a/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp b/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd17f542764623eb737e1411ee49372e5760e30c --- /dev/null +++ b/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp @@ -0,0 +1,96 @@ +/* +* 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. +*/ +#define LOG_TAG "BundleMgrAdapter" + +#include "bundlemgr_adapter.h" +#include +#include "accesstoken_kit.h" +#include "bundlemgr/bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "log_print.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedData { +BundleMgrAdapter::BundleMgrAdapter() +{ +} +BundleMgrAdapter::~BundleMgrAdapter() +{ +} +BundleMgrAdapter& BundleMgrAdapter::GetInstance() +{ + static BundleMgrAdapter instance; + return instance; +} + +std::string BundleMgrAdapter::GetKey(const std::string &bundleName, int32_t userId) +{ + return bundleName + "###" + std::to_string(userId); +} + +std::string BundleMgrAdapter::GetAppidFromCache(const std::string &bundleName, int32_t userId) +{ + std::string appId; + std::string key = GetKey(bundleName, userId); + appIds_.Get(key, appId); + return appId; +} + +std::string BundleMgrAdapter::GetBundleAppId(const std::string &bundleName, int32_t userId) +{ + std::string appId = GetAppidFromCache(bundleName, userId); + if (!appId.empty()) { + return appId; + } + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return ""; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + return ""; + } + auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); + return ""; + } + appId = bundleManager->GetAppIdByBundleName(bundleName, userId); + if (appId.empty()) { + ZLOGE("GetAppIdByBundleName failed appId:%{public}s, bundleName:%{public}s, uid:%{public}d", + appId.c_str(), bundleName.c_str(), userId); + } else { + appIds_.Set(GetKey(bundleName, userId), appId); + } + return appId; +} + +void BundleMgrAdapter::DeleteCache(const std::string &bundleName, int32_t user) +{ + std::string key = GetKey(bundleName, user); + appIds_.Delete(key); +} + +void BundleMgrAdapter::ClearCache() +{ + appIds_.ResetCapacity(0); + appIds_.ResetCapacity(CACHE_SIZE); +} +} // namespace DistributedData +} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h b/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..5519c9b105b12093cbf1ee67e69b56470e3d047d --- /dev/null +++ b/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h @@ -0,0 +1,39 @@ +/* +* 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. +*/ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H +#include "lru_bucket.h" +#include "visibility.h" +namespace OHOS { +namespace DistributedData { +class BundleMgrAdapter { +public: + BundleMgrAdapter(); + ~BundleMgrAdapter(); + API_EXPORT static BundleMgrAdapter& GetInstance(); + API_EXPORT std::string GetBundleAppId(const std::string &bundleName, int32_t userId); + API_EXPORT void DeleteCache(const std::string &bundleName, int32_t user); + API_EXPORT void ClearCache(); + +private: + std::string GetAppidFromCache(const std::string &bundleName, int32_t userId); + std::string GetKey(const std::string &bundleName, int32_t userId); + static constexpr int32_t CACHE_SIZE = 32; + LRUBucket appIds_ {CACHE_SIZE}; +}; +} // namespace DistributedData +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_BUNDLEMGR_ADAPTER_H \ No newline at end of file diff --git a/services/distributeddataservice/app/src/checker/BUILD.gn b/services/distributeddataservice/app/src/checker/BUILD.gn index f0fa8dd258432a3254d58b3173dd84958c6fa2d7..0ad405bb94ee9c00e84585aa25c2561e1d55fb58 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -32,7 +32,10 @@ ohos_source_set("distributeddata_checker") { "-Oz", ] - include_dirs = [ "${data_service_path}/framework/include" ] + include_dirs = [ + "${data_service_path}/adapter/include", + "${data_service_path}/framework/include", + ] if (build_public_version) { cflags_cc += [ "-DCONFIG_PUBLIC_VERSION" ] diff --git a/services/distributeddataservice/app/src/checker/bundle_checker.cpp b/services/distributeddataservice/app/src/checker/bundle_checker.cpp index b726ab915be6d6ba3efe6693eb0825b2b93122bb..1bfc0fd2187febc30e4630d7bf1ba727ba7071e5 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.cpp +++ b/services/distributeddataservice/app/src/checker/bundle_checker.cpp @@ -18,6 +18,7 @@ #include #include "accesstoken_kit.h" #include "bundlemgr/bundle_mgr_proxy.h" +#include "bundle_mgr/bundlemgr_adapter.h" #include "hap_token_info.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -61,63 +62,13 @@ bool BundleChecker::SetSwitchesInfo(const CheckerManager::Switches &switches) return true; } -std::string BundleChecker::GetKey(const std::string &bundleName, int32_t userId) -{ - return bundleName + "###" + std::to_string(userId); -} - -std::string BundleChecker::GetAppidFromCache(const std::string &bundleName, int32_t userId) -{ - std::string appId; - std::string key = GetKey(bundleName, userId); - appIds_.Get(key, appId); - return appId; -} - std::string BundleChecker::GetBundleAppId(const CheckerManager::StoreInfo &info) { int32_t userId = info.uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = GetAppidFromCache(info.bundleName, userId); - if (!appId.empty()) { - return appId; - } - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - ZLOGE("Failed to get system ability mgr."); - return ""; - } - auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundleMgrProxy == nullptr) { - ZLOGE("Failed to Get BMS SA."); - return ""; - } - auto bundleManager = iface_cast(bundleMgrProxy); - if (bundleManager == nullptr) { - ZLOGE("Failed to get bundle manager"); - return ""; - } - appId = bundleManager->GetAppIdByBundleName(info.bundleName, userId); - if (appId.empty()) { - ZLOGE("GetAppIdByBundleName failed appId:%{public}s, bundleName:%{public}s, uid:%{public}d", - appId.c_str(), info.bundleName.c_str(), userId); - } else { - appIds_.Set(GetKey(info.bundleName, userId), appId); - } + std::string appId = BundleMgrAdapter::GetInstance().GetBundleAppId(info.bundleName, userId); return appId; } -void BundleChecker::DeleteCache(const std::string &bundleName, int32_t user, int32_t index) -{ - std::string key = GetKey(bundleName, user); - appIds_.Delete(key); -} - -void BundleChecker::ClearCache() -{ - appIds_.ResetCapacity(0); - appIds_.ResetCapacity(CACHE_SIZE); -} - std::string BundleChecker::GetAppId(const CheckerManager::StoreInfo &info) { if (AccessTokenKit::GetTokenTypeFlag(info.tokenId) != TOKEN_HAP) { diff --git a/services/distributeddataservice/app/src/checker/bundle_checker.h b/services/distributeddataservice/app/src/checker/bundle_checker.h index a7b07159c1b228bad10df261654e782e9f7545f1..b0c63431a207cd374abb37605c3bebaf3e1227ed 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.h +++ b/services/distributeddataservice/app/src/checker/bundle_checker.h @@ -16,7 +16,6 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_BUNDLE_CHECKER_H #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CHECKER_BUNDLE_CHECKER_H #include "checker/checker_manager.h" -#include "lru_bucket.h" namespace OHOS { namespace DistributedData { class BundleChecker : public CheckerManager::Checker { @@ -37,12 +36,7 @@ public: std::vector GetStaticStores() override; bool IsDynamic(const CheckerManager::StoreInfo &info) override; bool IsStatic(const CheckerManager::StoreInfo &info) override; - void DeleteCache(const std::string &bundleName, int32_t user, int32_t index) override; - void ClearCache() override; private: - static constexpr int32_t CACHE_SIZE = 32; - std::string GetAppidFromCache(const std::string &bundleName, int32_t userId); - std::string GetKey(const std::string &bundleName, int32_t userId); static BundleChecker instance_; std::map trusts_; std::map distrusts_; @@ -50,7 +44,6 @@ private: std::vector dynamicStores_; std::vector staticStores_; std::string GetBundleAppId(const CheckerManager::StoreInfo &info); - LRUBucket appIds_ {CACHE_SIZE}; }; } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 51af0c61e09fed7a55bceae2b34aa11a97ed40ce..9d505889925ad35805c945fb680a6bbaf6fba55d 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -29,6 +29,7 @@ #include "auth_delegate.h" #include "auto_launch_export.h" #include "bootstrap.h" +#include "bundle_mgr/bundlemgr_adapter.h" #include "checker/checker_manager.h" #include "communication_provider.h" #include "communicator_context.h" @@ -930,7 +931,7 @@ void KvStoreDataService::AccountEventChanged(const AccountEventInfo &eventInfo) MetaDataManager::GetInstance().DelMeta(StoreMetaMapping(meta).GetKey(), true); PermitDelegate::GetInstance().DelCache(meta.GetKeyWithoutPath()); } - CheckerManager::GetInstance().ClearCache(); + BundleMgrAdapter::GetInstance().ClearCache(); g_kvStoreAccountEventStatus = 0; break; } @@ -1050,7 +1051,7 @@ void KvStoreDataService::OnSessionReady(const AppDistributedKv::DeviceInfo &info int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t user, int32_t index) { - CheckerManager::GetInstance().DeleteCache(bundleName, user, index); + BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUninstall(bundleName, user, index); @@ -1061,7 +1062,7 @@ int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t u int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user, int32_t index) { - CheckerManager::GetInstance().DeleteCache(bundleName, user, index); + BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUpdate(bundleName, user, index); @@ -1072,7 +1073,7 @@ int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user int32_t KvStoreDataService::OnInstall(const std::string &bundleName, int32_t user, int32_t index) { - CheckerManager::GetInstance().DeleteCache(bundleName, user, index); + BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppInstall(bundleName, user, index); @@ -1093,7 +1094,7 @@ int32_t KvStoreDataService::OnScreenUnlocked(int32_t user) int32_t KvStoreDataService::ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) { - CheckerManager::GetInstance().DeleteCache(bundleName, userId, appIndex); + BundleMgrAdapter::GetInstance().DeleteCache(bundleName, userId); auto callerToken = IPCSkeleton::GetCallingTokenID(); NativeTokenInfo nativeTokenInfo; if (AccessTokenKit::GetNativeTokenInfo(callerToken, nativeTokenInfo) != RET_SUCCESS || diff --git a/services/distributeddataservice/framework/checker/checker_manager.cpp b/services/distributeddataservice/framework/checker/checker_manager.cpp index 739ff5ce6fec69087428a17f5ccc03e48ccf1093..e02f50c64a219711e8456453445ac5c0bb8d33d3 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -47,26 +47,6 @@ void CheckerManager::RegisterPlugin(const std::string &checker, std::functionDeleteCache(bundleName, user, index); - } -} - -void CheckerManager::ClearCache() -{ - for (auto &[name, checker] : checkers_) { - if (checker == nullptr) { - continue; - } - checker->ClearCache(); - } -} - std::string CheckerManager::GetAppId(const StoreInfo &info) { for (auto &[name, checker] : checkers_) { diff --git a/services/distributeddataservice/framework/include/checker/checker_manager.h b/services/distributeddataservice/framework/include/checker/checker_manager.h index f42f43c616b4a4f1b5626e6a8aa79f0ccf038270..b0ad878e150a71fd97d486cdf014da85be637f33 100644 --- a/services/distributeddataservice/framework/include/checker/checker_manager.h +++ b/services/distributeddataservice/framework/include/checker/checker_manager.h @@ -53,8 +53,6 @@ public: virtual std::vector GetStaticStores() = 0; virtual bool IsDynamic(const StoreInfo &info) = 0; virtual bool IsStatic(const StoreInfo &info) = 0; - virtual void DeleteCache(const std::string &bundleName, int32_t user, int32_t index){}; - virtual void ClearCache(){}; protected: API_EXPORT ~Checker() = default; }; @@ -70,8 +68,6 @@ public: API_EXPORT bool IsSwitches(const StoreInfo &info); API_EXPORT void LoadCheckers(std::vector &checkers); API_EXPORT Checker *GetChecker(const std::string &checker); - API_EXPORT void DeleteCache(const std::string &bundleName, int32_t user, int32_t index); - API_EXPORT void ClearCache(); private: std::map checkers_; diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 37a5bafddd07269758be7cb49b3601c027d7f709..2f754c7c10efd11e33e9ec191100cb747fafa073 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -366,6 +366,7 @@ ohos_unittest("ServiceMetaDataTest") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", + "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/utils:distributeddata_utils", "${data_service_path}/app/src/checker:distributeddata_checker", diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 799699962afa099106f9e7689d8452ef1fc65e57..b0cfab3778c3c9456474daa915167f2216b42daf 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -71,6 +71,7 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", + "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/dfx:distributeddata_dfx", "${data_service_path}/adapter/network:distributeddata_network", diff --git a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn index 398162fcc317b76037c3d3ce7bb6fe90e603c982..e40e04ef32966d50d40d7750f22de44ebe97e7e0 100644 --- a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn @@ -44,6 +44,7 @@ ohos_fuzztest("UdmfServiceFuzzTest") { deps = [ "${data_service_path}/adapter/account:distributeddata_account", + "${data_service_path}/adapter/bundle_mgr:distributedata_bundlemgr", "${data_service_path}/adapter/communicator:distributeddata_communicator", "${data_service_path}/adapter/dfx:distributeddata_dfx", "${data_service_path}/framework:distributeddatasvcfwk", diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index 5e93729438346600d59f9ad56ee6536d259a59a4..7b7e648f109939bf02c65bcc6b71d6192f45bb4a 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -94,11 +94,6 @@ bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &proc return true; } -std::string PreProcessUtils::GetAppId(const std::string &bundleName) -{ - return "com.demo.test"; -} - std::string PreProcessUtils::GetLocalDeviceId() { return "123"; diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp index c42f223a7aa6252a8f179c7f67ef1a64e2fb7421..8ffe52b9ed30348a0eea60fc8c09048a8cb42d5a 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp @@ -179,18 +179,4 @@ HWTEST_F(UdmfPreProcessUtilsTest, GetHtmlFileUris001, TestSize.Level1) PreProcessUtils preProcessUtils; EXPECT_NO_FATAL_FAILURE(preProcessUtils.GetHtmlFileUris(tokenId, data, isLocal, uris)); } - -/** -* @tc.name: GetAppId001 -* @tc.desc: Abnormal test of GetAppId, samgrProxy is nullptr -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(UdmfPreProcessUtilsTest, GetAppId001, TestSize.Level1) -{ - std::string bundleName = "test"; - PreProcessUtils preProcessUtils; - std::string appId = preProcessUtils.GetAppId(bundleName); - EXPECT_EQ(appId, ""); -} }; // 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 735df7c174105d019f4881c175e79f2eb6b1e448..c43180b4a78ad4769626aea38dbc5a58283908aa 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -85,7 +85,9 @@ public: void SetUp(){}; void TearDown(){}; - const std::string STORE_ID = "drag"; + static constexpr const char *STORE_ID = "drag"; + static constexpr uint32_t TOKEN_ID = 5; + static constexpr const char *APP_ID = "appId"; }; /** @@ -465,163 +467,68 @@ HWTEST_F(UdmfServiceImplTest, CheckAppId002, TestSize.Level1) } /** - * @tc.name: ValidateAndProcessRuntimeData001 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData001, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission001 +* @tc.desc: runtime is null +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission001, TestSize.Level1) { - UnifiedData data; - std::shared_ptr record = std::make_shared(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData001"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; + std::string appId; std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; QueryOption query; - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_DB_ERROR); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData002 - * @tc.desc: Normal test - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData002, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission002 +* @tc.desc: query.tokenId is invalid +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission002, TestSize.Level1) { - UnifiedData data; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->tokenId = 1; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData002"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->tokenId = TOKEN_ID; QueryOption query; - query.tokenId = 1; - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData003 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData003, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission003 +* @tc.desc: Normal test +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission003, TestSize.Level1) { - UnifiedData data; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->tokenId = 1; - data.runtime_ = std::make_shared<Runtime>(); - data.runtime_->appId = "appId"; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData003"; - properties->tag = tag; - data.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = {data}; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->tokenId = TOKEN_ID; QueryOption query; - query.tokenId = 2; - + query.tokenId = TOKEN_ID; UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_TRUE(ret); } /** - * @tc.name: ValidateAndProcessRuntimeData004 - * @tc.desc: invalid appId - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData004, TestSize.Level1) +* @tc.name: CheckDeleteDataPermission004 +* @tc.desc: bundleName is empty +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, CheckDeleteDataPermission004, TestSize.Level1) { - UnifiedData data1; - data1.runtime_ = std::make_shared<Runtime>(); - data1.runtime_->tokenId = 1; - data1.runtime_ = std::make_shared<Runtime>(); - data1.runtime_->appId = "appId"; - std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); - data1.AddRecord(record); - std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); - std::string tag = "this is a tag of test ValidateAndProcessRuntimeData004"; - properties->tag = tag; - data1.SetProperties(std::move(properties)); - - UnifiedData data2; - data2.runtime_ = std::make_shared<Runtime>(); - data2.runtime_->tokenId = 1; - data2.runtime_ = std::make_shared<Runtime>(); - data2.runtime_->appId = "appId"; - data2.AddRecord(record); - data2.SetProperties(std::move(properties)); - - std::vector<UnifiedData> dataSet = { data1, data2 }; - std::shared_ptr<Runtime> runtime; - std::vector<UnifiedData> unifiedDataSet; - std::vector<std::string> deleteKeys; + std::string appId; + std::shared_ptr<Runtime> runtime = std::make_shared<Runtime>(); + runtime->appId = APP_ID; QueryOption query; - query.tokenId = 2; - - UdmfServiceImpl impl; - int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - EXPECT_EQ(result, UDMF::E_OK); -} - -/** - * @tc.name: CloseStoreWhenCorrupted001 - * @tc.desc: Normal test of CloseStoreWhenCorrupted - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, CloseStoreWhenCorrupted001, TestSize.Level1) -{ - std::string intention = "drag"; - StoreCache::GetInstance().CloseStores(); - auto store = StoreCache::GetInstance().GetStore(intention); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); - int32_t status = UDMF::E_OK; - UdmfServiceImpl impl; - impl.HandleDbError(intention, status); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); -} - -/** - * @tc.name: CloseStoreWhenCorrupted002 - * @tc.desc: Abnormal test of CloseStoreWhenCorrupted - * @tc.type: FUNC - */ -HWTEST_F(UdmfServiceImplTest, CloseStoreWhenCorrupted002, TestSize.Level1) -{ - std::string intention = "drag"; - StoreCache::GetInstance().CloseStores(); - auto store = StoreCache::GetInstance().GetStore(intention); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 1); - int32_t status = UDMF::E_DB_CORRUPTED; + query.tokenId = TOKEN_ID; UdmfServiceImpl impl; - impl.HandleDbError(intention, status); - EXPECT_EQ(StoreCache::GetInstance().stores_.Size(), 0); - EXPECT_EQ(status, UDMF::E_DB_ERROR); + bool ret = impl.CheckDeleteDataPermission(appId, runtime, query); + EXPECT_FALSE(ret); } }; // namespace DistributedDataTest }; // namespace OHOS::Test diff --git a/services/distributeddataservice/service/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index c908469dfbf2d4c2fa31496e34f0b78a6f6b69a4..313702368a3e1e27eb143a05bb5c1a131dd21072 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -17,6 +17,7 @@ config("module_public_config") { visibility = [ ":*" ] include_dirs = [ + "${data_service_path}/adapter/include", "${data_service_path}/adapter/include/communicator", "${data_service_path}/service/matrix/include", "${data_service_path}/service/permission/include", diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 361358841df901527884fb0b52f47132b890af59..d7d527f8834f19518cd43bbaa9f497e80a6209b5 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -33,7 +33,7 @@ #include "utils/crypto.h" #include "uri_permission_manager_client.h" #include "ipc_skeleton.h" -#include "bundle_mgr_interface.h" +#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -71,7 +71,8 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; - std::string appId = GetAppId(bundleName); + int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; + std::string appId = DistributedData::BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); @@ -151,32 +152,6 @@ bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &proc return true; } -std::string PreProcessUtils::GetAppId(const std::string &bundleName) -{ - auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgrProxy == nullptr) { - ZLOGE("Failed to get system ability mgr, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundleMgrProxy == nullptr) { - ZLOGE("Failed to Get BMS SA, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - auto bundleManager = iface_cast<AppExecFwk::IBundleMgr>(bundleMgrProxy); - if (bundleManager == nullptr) { - ZLOGE("Failed to get bundle manager, bundleName:%{public}s", bundleName.c_str()); - return ""; - } - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = bundleManager->GetAppIdByBundleName(bundleName, userId); - if (appId.empty()) { - ZLOGE("GetAppIdByBundleName failed, bundleName:%{public}s, uid:%{public}d", bundleName.c_str(), uid); - } - return appId; -} - std::string PreProcessUtils::GetLocalDeviceId() { auto info = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice(); diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 366dc17c60e1f34450724393d1db11061dbb1e84..5d602b6267d4e6b5b48e7d5c371de82bded66dc4 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -44,7 +44,6 @@ public: 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 std::string GetAppId(const std::string &bundleName); static sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector<std::string>& uris, uint32_t tokenId); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 5a916467442912f2c266b435717f05b57723949c..5ffbd747fe7f8b37a8c77dc58bbfd75e99025ada 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -42,6 +42,7 @@ #include "udmf_utils.h" #include "unified_data_helper.h" #include "utils/anonymous.h" +#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { @@ -451,7 +452,8 @@ int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr<Runtime> runtime, const std: ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); return E_INVALID_PARAMETERS; } - std::string appId = PreProcessUtils::GetAppId(bundleName); + int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; + std::string appId = BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); if (appId.empty() || appId != runtime->appId) { ZLOGE("Update failed: runtime->appId %{public}s and bundleName appId %{public}s mismatch", runtime->appId.c_str(), appId.c_str()); @@ -485,11 +487,15 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie return E_OK; } std::shared_ptr<Runtime> runtime; + std::string appId; std::vector<std::string> deleteKeys; - status = ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); - if (status != E_OK) { - ZLOGE("ValidateAndProcessRuntimeData failed."); - return status; + for (const auto &data : dataSet) { + auto runtime = data.GetRuntime(); + if (!CheckDeleteDataPermission(appId, runtime, query)) { + continue; + } + unifiedDataSet.push_back(data); + deleteKeys.emplace_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); } if (deleteKeys.empty()) { ZLOGE("No data to delete for this application"); @@ -505,36 +511,26 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie return E_OK; } -int32_t UdmfServiceImpl::ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, - std::shared_ptr<Runtime> runtime, std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, - std::vector<std::string> &deleteKeys) +bool UdmfServiceImpl::CheckDeleteDataPermission(std::string &appId, const std::shared_ptr<Runtime> &runtime, + const QueryOption &query) { - std::string appId; - bool isFirstInvoke = false; - for (const auto &data : dataSet) { - runtime = data.GetRuntime(); - if (runtime == nullptr) { - ZLOGE("Invalid runtime."); - return E_DB_ERROR; - } - if (runtime->tokenId != query.tokenId) { - if (runtime->appId.empty()) { - continue; - } - if (!isFirstInvoke) { - std::string bundleName; - PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); - appId = PreProcessUtils::GetAppId(bundleName); - isFirstInvoke = true; - } - if (appId.empty() || appId != runtime->appId) { - continue; - } - } - unifiedDataSet.push_back(data); - deleteKeys.emplace_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); + if (runtime == nullptr) { + ZLOGE("Invalid runtime."); + return false; } - return E_OK; + if (runtime->tokenId == query.tokenId) { + return true; + } + std::string bundleName; + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); + return false; + } + if (CheckAppId(runtime, bundleName) != E_OK) { + ZLOGE("Delete failed: tokenId or appId mismatch, bundleName: %{public}s", bundleName.c_str()); + return false; + } + return true; } int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 87d0af224a9c35ae3fd47a2fcba3f410e8ffc4a5..584eee68ffc7716abefdf283513ebd94153ddd67 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -80,8 +80,8 @@ private: std::string FindIntentionMap(const Intention &queryintention); bool IsValidOptionsNonDrag(UnifiedKey &key, const std::string &intention); bool IsValidInput(const QueryOption &query, UnifiedData &unifiedData, UnifiedKey &key); - int32_t ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, std::shared_ptr<Runtime> runtime, - std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, std::vector<std::string> &deleteKeys); + bool CheckDeleteDataPermission(std::string &appId, const std::shared_ptr<Runtime> &runtime, + const QueryOption &query); int32_t CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &bundleName); void CloseStoreWhenCorrupted(const std::string &intention, int32_t status); void HandleDbError(const std::string &intention, int32_t &status);