From 9058d4c6d489f2859caf553df4a23edb489d626b Mon Sep 17 00:00:00 2001 From: wan-xiaoqing321 Date: Tue, 19 Aug 2025 14:58:34 +0800 Subject: [PATCH 01/11] =?UTF-8?q?fuzz=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wan-xiaoqing321 --- .../udmfservice_fuzzer/udmfservice_fuzzer.cpp | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/udmfservice_fuzzer.cpp b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/udmfservice_fuzzer.cpp index 59d2290c2..827a48d7c 100644 --- a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/udmfservice_fuzzer.cpp +++ b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/udmfservice_fuzzer.cpp @@ -387,7 +387,10 @@ void OnGetAppShareOptionFuzz(FuzzedDataProvider &provider) { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); MessageParcel requestUpdate; + std::vector remainingData = provider.ConsumeRemainingBytes(); requestUpdate.WriteInterfaceToken(INTERFACE_TOKEN); + requestUpdate.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + requestUpdate.RewindRead(0); CustomOption option = {.intention = Intention::UD_INTENTION_DRAG}; std::string intention = UD_INTENTION_MAP.at(option.intention); ITypesUtil::Marshal(requestUpdate, intention); @@ -658,16 +661,49 @@ void StoreSyncFuzz(FuzzedDataProvider &provider) void SetAppShareOptionFuzz(FuzzedDataProvider &provider) { std::shared_ptr udmfServiceImpl = std::make_shared(); - std::string intention = provider.ConsumeRandomLengthString(); - int32_t shareOption = provider.ConsumeIntegral(); - udmfServiceImpl->SetAppShareOption(intention, shareOption); + std::shared_ptr executor = std::make_shared(NUM_MAX, NUM_MIN); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); + + MessageParcel requestUpdate; + std::vector remainingData = provider.ConsumeRemainingBytes(); + requestUpdate.WriteInterfaceToken(INTERFACE_TOKEN); + requestUpdate.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + requestUpdate.RewindRead(0); + CustomOption option = {.intention = Intention::UD_INTENTION_DRAG}; + std::string intention = UD_INTENTION_MAP.at(option.intention); + ITypesUtil::Marshal(requestUpdate, intention); + + MessageParcel replyUpdate; + udmfServiceImpl->OnRemoteRequest(static_cast(UdmfServiceInterfaceCode::SET_APP_SHARE_OPTION), + requestUpdate, replyUpdate); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), nullptr }); + executor = nullptr; } void RemoveAppShareOptionFuzz(FuzzedDataProvider &provider) { std::shared_ptr udmfServiceImpl = std::make_shared(); - std::string intention = provider.ConsumeRandomLengthString(); - udmfServiceImpl->RemoveAppShareOption(intention); + std::shared_ptr executor = std::make_shared(NUM_MAX, NUM_MIN); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); + + MessageParcel requestUpdate; + std::vector remainingData = provider.ConsumeRemainingBytes(); + requestUpdate.WriteInterfaceToken(INTERFACE_TOKEN); + requestUpdate.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + requestUpdate.RewindRead(0); + CustomOption option = {.intention = Intention::UD_INTENTION_DRAG}; + std::string intention = UD_INTENTION_MAP.at(option.intention); + ITypesUtil::Marshal(requestUpdate, intention); + + MessageParcel replyUpdate; + udmfServiceImpl->OnRemoteRequest(static_cast(UdmfServiceInterfaceCode::REMOVE_APP_SHARE_OPTION), + requestUpdate, replyUpdate); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), nullptr }); + executor = nullptr; } void QueryDataCommonFuzz(FuzzedDataProvider &provider) @@ -698,8 +734,24 @@ void QueryDataCommonFuzz(FuzzedDataProvider &provider) void ClearAsynProcessByKeyFuzz(FuzzedDataProvider &provider) { std::shared_ptr udmfServiceImpl = std::make_shared(); + std::shared_ptr executor = std::make_shared(NUM_MAX, NUM_MIN); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); + + MessageParcel requestUpdate; + std::vector remainingData = provider.ConsumeRemainingBytes(); + requestUpdate.WriteInterfaceToken(INTERFACE_TOKEN); + requestUpdate.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + requestUpdate.RewindRead(0); std::string businessUdKey = provider.ConsumeRandomLengthString(); - udmfServiceImpl->ClearAsynProcessByKey(businessUdKey); + ITypesUtil::Marshal(requestUpdate, businessUdKey); + + MessageParcel replyUpdate; + udmfServiceImpl->OnRemoteRequest(static_cast(UdmfServiceInterfaceCode::CLEAR_ASYN_PROCESS_BY_KEY), + requestUpdate, replyUpdate); + udmfServiceImpl->OnBind( + { "UdmfServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), nullptr }); + executor = nullptr; } void TransferToEntriesIfNeedFuzz(FuzzedDataProvider &provider) -- Gitee From d5c5343209faa7df657335deede1b6844b9a0b03 Mon Sep 17 00:00:00 2001 From: wanxiaoqing Date: Mon, 25 Aug 2025 16:06:59 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanxiaoqing --- .../test/udmf_service_impl_mock_test.cpp | 28 +-- .../service/test/udmf_service_impl_test.cpp | 163 +++++++++++++++++- .../udmf/preprocess/preprocess_utils.cpp | 1 - .../service/udmf/store/store.h | 4 + .../service/udmf/udmf_service_impl.cpp | 3 +- .../service/udmf/udmf_service_stub.h | 1 - 6 files changed, 181 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/service/test/udmf_service_impl_mock_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_mock_test.cpp index 9daa166f4..ec2539109 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_mock_test.cpp @@ -73,25 +73,27 @@ HWTEST_F(UdmfServiceImplMockTest, IsNeedMetaSyncTest001, TestSize.Level0) StoreMetaData meta = StoreMetaData("100", "distributeddata", "drag"); std::vector devices = {"remote_device"}; - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); auto isNeedSync = udmfServiceImpl.IsNeedMetaSync(meta, devices); EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(true)); isNeedSync = udmfServiceImpl.IsNeedMetaSync(meta, devices); EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(Return(true)) - .WillOnce(Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillOnce(testing::Return(false)); isNeedSync = udmfServiceImpl.IsNeedMetaSync(meta, devices); EXPECT_EQ(isNeedSync, true); - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(Return(true)) - .WillOnce(Return(true)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(true)) + .WillOnce(testing::Return(true)); isNeedSync = udmfServiceImpl.IsNeedMetaSync(meta, devices); EXPECT_EQ(isNeedSync, true); } @@ -111,9 +113,9 @@ HWTEST_F(UdmfServiceImplMockTest, IsNeedMetaSyncTest002, TestSize.Level0) StoreMetaData meta = StoreMetaData("100", "distributeddata", "drag"); std::vector devices = {"remote_device"}; - EXPECT_CALL(*metaDataManagerMock, LoadMeta(_, _, _)) - .WillOnce(Return(false)) - .WillOnce(Return(false)); + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillOnce(testing::Return(false)); auto isNeedSync = udmfServiceImpl.IsNeedMetaSync(meta, devices); EXPECT_EQ(isNeedSync, true); // mock mask diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index 9a32e1587..e58b2006d 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -32,13 +32,12 @@ #include "token_setproc.h" using namespace testing::ext; -using namespace OHOS::DistributedData; using namespace OHOS::Security::AccessToken; using namespace OHOS::UDMF; -using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Entry = DistributedDB::Entry; using Key = DistributedDB::Key; using Value = DistributedDB::Value; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using UnifiedData = OHOS::UDMF::UnifiedData; using Summary = OHOS::UDMF::Summary; namespace OHOS::Test { @@ -411,7 +410,7 @@ HWTEST_F(UdmfServiceImplTest, SyncTest001, TestSize.Level0) query.tokenId = 1; query.intention = UD_INTENTION_DRAG; UdmfServiceImpl udmfServiceImpl; - StoreMetaData meta = StoreMetaData("100", "distributeddata", "drag"); + DistributedData::StoreMetaData meta = DistributedData::StoreMetaData("100", "distributeddata", "drag"); std::vector devices = {"remote_device"}; auto ret = udmfServiceImpl.Sync(query, devices); @@ -541,6 +540,164 @@ HWTEST_F(UdmfServiceImplTest, IsValidInput004, TestSize.Level1) EXPECT_FALSE(result); } +/** + * @tc.name: UpdateData002 + * @tc.desc: invalid parameter + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, UpdateData002, TestSize.Level1) +{ + QueryOption query; + query.intention = Intention::UD_INTENTION_DATA_HUB; + query.key = "udmf://DataHub/aaa/N]2fIEMbrJj@wp:jMuPa7"; + query.tokenId = 99999; + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + + UdmfServiceImpl impl; + EXPECT_NE(impl.UpdateData(query, unifiedData), E_OK); +} + +/** + * @tc.name: UpdateData003 + * @tc.desc: invalid parameter + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, UpdateData003, TestSize.Level1) +{ + QueryOption query; + query.intention = Intention::UD_INTENTION_DATA_HUB; + query.key = "udmf://DataHub/aaa/N]2fIEMbrJj@wp:jMuPa7"; + query.tokenId = static_cast(IPCSkeleton::GetCallingTokenID()); + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + + UdmfServiceImpl impl; + EXPECT_NE(impl.UpdateData(query, unifiedData), E_OK); +} + +/** + * @tc.name: UpdateData004 + * @tc.desc: invalid parameter + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, UpdateData004, TestSize.Level1) +{ + QueryOption query; + UnifiedData unifiedData; + + query.key = "invalid_key"; + UdmfServiceImpl impl; + EXPECT_EQ(impl.UpdateData(query, unifiedData), E_INVALID_PARAMETERS); +} + +/** +* @tc.name: SaveData002 +* @tc.desc: invalid parameter +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, SaveData002, TestSize.Level1) +{ + CustomOption option; + QueryOption query; + UnifiedData unifiedData; + std::string key = ""; + + UdmfServiceImpl impl; + EXPECT_EQ(impl.SaveData(option, unifiedData, key), E_INVALID_PARAMETERS); +} + +/** +* @tc.name: SaveData003 +* @tc.desc: invalid parameter +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, SaveData003, TestSize.Level1) +{ + CustomOption option; + QueryOption query; + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + std::string key = ""; + option.intention = Intention::UD_INTENTION_BASE; + + UdmfServiceImpl impl; + EXPECT_EQ(impl.SaveData(option, unifiedData, key), E_INVALID_PARAMETERS); +} + +/** +* @tc.name: SaveData004 +* @tc.desc: invalid parameter +* @tc.type: FUNC +*/ +HWTEST_F(UdmfServiceImplTest, SaveData004, TestSize.Level1) +{ + CustomOption option; + QueryOption query; + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + std::string key = ""; + option.intention = Intention::UD_INTENTION_DATA_HUB; + option.tokenId = 99999; + + UdmfServiceImpl impl; + EXPECT_NE(impl.SaveData(option, unifiedData, key), E_OK); +} + +/** + * @tc.name: IsValidInput005 + * @tc.desc: valid input + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, IsValidInput005, TestSize.Level1) +{ + QueryOption query; + query.intention = Intention::UD_INTENTION_DRAG; + query.key = "udmf://drag/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://drag/aaa/N]2fIEMbrJj@wp:jMuPa7"); + EXPECT_TRUE(key.IsValid()); + + UdmfServiceImpl impl; + bool result = impl.IsValidInput(query, unifiedData, key); + EXPECT_FALSE(result); +} + +/** + * @tc.name: PushDelayData001 + * @tc.desc: valid input + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, PushDelayData001, TestSize.Level1) +{ + UnifiedData unifiedData; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + unifiedData.AddRecord(record1); + unifiedData.AddRecord(record2); + std::string key = "invalid key"; + + UdmfServiceImpl impl; + auto result = impl.PushDelayData(key, unifiedData); + EXPECT_NE(result, E_OK); +} + /** * @tc.name: CheckAppId001 * @tc.desc: invalid bundleName diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index f50c0f1c1..77eebed84 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -16,7 +16,6 @@ #include "preprocess_utils.h" -#include #include #include "bundle_info.h" diff --git a/services/distributeddataservice/service/udmf/store/store.h b/services/distributeddataservice/service/udmf/store/store.h index 1b215e250..a21f24b56 100644 --- a/services/distributeddataservice/service/udmf/store/store.h +++ b/services/distributeddataservice/service/udmf/store/store.h @@ -16,6 +16,10 @@ #ifndef UDMF_STORE_H #define UDMF_STORE_H +#include +#include +#include + #include "unified_data.h" namespace OHOS { diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 770a10c31..00f16b007 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -1153,7 +1153,8 @@ bool UdmfServiceImpl::IsValidOptionsNonDrag(UnifiedKey &key, const std::string & return false; } -int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr iUdmfNotifier, std::string &key) +int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr iUdmfNotifier, + std::string &key) { std::string bundleName; auto tokenId = static_cast(IPCSkeleton::GetCallingTokenID()); diff --git a/services/distributeddataservice/service/udmf/udmf_service_stub.h b/services/distributeddataservice/service/udmf/udmf_service_stub.h index c5f617a9f..3ca1214b0 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_stub.h +++ b/services/distributeddataservice/service/udmf/udmf_service_stub.h @@ -45,7 +45,6 @@ private: int32_t OnRemoveAppShareOption(MessageParcel &data, MessageParcel &reply); int32_t OnObtainAsynProcess(MessageParcel &data, MessageParcel &reply); int32_t OnClearAsynProcessByKey(MessageParcel &data, MessageParcel &reply); - int32_t OnInvokeHap(MessageParcel &data, MessageParcel &reply); int32_t OnPushDelayData(MessageParcel &data, MessageParcel &reply); int32_t OnSetDelayInfo(MessageParcel &data, MessageParcel &reply); int32_t OnGetDataIfAvailable(MessageParcel &data, MessageParcel &reply); -- Gitee From 87a458e4a99fcaa8864b74f3855a91380a4ae5b7 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Thu, 14 Aug 2025 16:38:23 +0800 Subject: [PATCH 03/11] fix appid Signed-off-by: wenjinchao Change-Id: I82edea6f91370270b4af5c88fb45d3b0e8d86d26 Signed-off-by: wanxiaoqing --- .../adapter/bundle_mgr/BUILD.gn | 45 --------- .../adapter/bundle_mgr/bundlemgr_adapter.cpp | 96 ------------------- .../include/bundle_mgr/bundlemgr_adapter.h | 39 -------- .../app/src/checker/BUILD.gn | 5 +- .../app/src/checker/bundle_checker.cpp | 53 +++++++++- .../app/src/checker/bundle_checker.h | 7 ++ .../app/src/kvstore_data_service.cpp | 11 +-- .../framework/checker/checker_manager.cpp | 20 ++++ .../include/checker/checker_manager.h | 4 + .../framework/test/BUILD.gn | 1 - .../distributeddataservice/service/BUILD.gn | 1 - .../test/fuzztest/udmfservice_fuzzer/BUILD.gn | 1 - .../service/udmf/BUILD.gn | 1 - .../udmf/preprocess/preprocess_utils.cpp | 5 +- .../service/udmf/udmf_service_impl.cpp | 5 +- 15 files changed, 92 insertions(+), 202 deletions(-) delete mode 100644 services/distributeddataservice/adapter/bundle_mgr/BUILD.gn delete mode 100644 services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp delete mode 100644 services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h diff --git a/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn b/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn deleted file mode 100644 index 93bb21bc3..000000000 --- a/services/distributeddataservice/adapter/bundle_mgr/BUILD.gn +++ /dev/null @@ -1,45 +0,0 @@ -# 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 deleted file mode 100644 index fd17f5427..000000000 --- a/services/distributeddataservice/adapter/bundle_mgr/bundlemgr_adapter.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* -* 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 deleted file mode 100644 index 5519c9b10..000000000 --- a/services/distributeddataservice/adapter/include/bundle_mgr/bundlemgr_adapter.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -* 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 0ad405bb9..f0fa8dd25 100644 --- a/services/distributeddataservice/app/src/checker/BUILD.gn +++ b/services/distributeddataservice/app/src/checker/BUILD.gn @@ -32,10 +32,7 @@ ohos_source_set("distributeddata_checker") { "-Oz", ] - include_dirs = [ - "${data_service_path}/adapter/include", - "${data_service_path}/framework/include", - ] + include_dirs = [ "${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 1bfc0fd21..b726ab915 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.cpp +++ b/services/distributeddataservice/app/src/checker/bundle_checker.cpp @@ -18,7 +18,6 @@ #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" @@ -62,13 +61,63 @@ 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 = BundleMgrAdapter::GetInstance().GetBundleAppId(info.bundleName, userId); + 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); + } 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 b0c63431a..a7b07159c 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.h +++ b/services/distributeddataservice/app/src/checker/bundle_checker.h @@ -16,6 +16,7 @@ #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 { @@ -36,7 +37,12 @@ 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_; @@ -44,6 +50,7 @@ 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 9d5058899..51af0c61e 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -29,7 +29,6 @@ #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" @@ -931,7 +930,7 @@ void KvStoreDataService::AccountEventChanged(const AccountEventInfo &eventInfo) MetaDataManager::GetInstance().DelMeta(StoreMetaMapping(meta).GetKey(), true); PermitDelegate::GetInstance().DelCache(meta.GetKeyWithoutPath()); } - BundleMgrAdapter::GetInstance().ClearCache(); + CheckerManager::GetInstance().ClearCache(); g_kvStoreAccountEventStatus = 0; break; } @@ -1051,7 +1050,7 @@ void KvStoreDataService::OnSessionReady(const AppDistributedKv::DeviceInfo &info int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t user, int32_t index) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUninstall(bundleName, user, index); @@ -1062,7 +1061,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) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppUpdate(bundleName, user, index); @@ -1073,7 +1072,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) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, user); + CheckerManager::GetInstance().DeleteCache(bundleName, user, index); auto staticActs = FeatureSystem::GetInstance().GetStaticActs(); staticActs.ForEachCopies([bundleName, user, index](const auto &, const std::shared_ptr& acts) { acts->OnAppInstall(bundleName, user, index); @@ -1094,7 +1093,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) { - BundleMgrAdapter::GetInstance().DeleteCache(bundleName, userId); + CheckerManager::GetInstance().DeleteCache(bundleName, userId, appIndex); 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 e02f50c64..739ff5ce6 100644 --- a/services/distributeddataservice/framework/checker/checker_manager.cpp +++ b/services/distributeddataservice/framework/checker/checker_manager.cpp @@ -47,6 +47,26 @@ 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 b0ad878e1..f42f43c61 100644 --- a/services/distributeddataservice/framework/include/checker/checker_manager.h +++ b/services/distributeddataservice/framework/include/checker/checker_manager.h @@ -53,6 +53,8 @@ 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; }; @@ -68,6 +70,8 @@ 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 2f754c7c1..37a5bafdd 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -366,7 +366,6 @@ 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 b0cfab377..799699962 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -71,7 +71,6 @@ 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 e40e04ef3..398162fcc 100644 --- a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn @@ -44,7 +44,6 @@ 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/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index d1d7d3889..6ac317304 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -17,7 +17,6 @@ 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 77eebed84..a05173952 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -22,6 +22,7 @@ #include "dds_trace.h" #include "udmf_radar_reporter.h" #include "accesstoken_kit.h" +#include "checker/checker_manager.h" #include "device_manager_adapter.h" #include "file_mount_manager.h" #include "iservice_registry.h" @@ -32,7 +33,6 @@ #include "utils/crypto.h" #include "uri_permission_manager_client.h" #include "ipc_skeleton.h" -#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -70,8 +70,7 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; - int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = DistributedData::BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId({IPCSkeleton::GetCallingUid(), option.tokenId, bundleName}); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 00f16b007..bf1786eef 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -25,6 +25,7 @@ #include "bootstrap.h" #include "bundle_info.h" #include "bundlemgr/bundle_mgr_proxy.h" +#include "checker/checker_manager.h" #include "checker_manager.h" #include "device_manager_adapter.h" #include "device_matrix.h" @@ -43,7 +44,6 @@ #include "unified_data_helper.h" #include "utils/anonymous.h" #include "permission_validator.h" -#include "bundle_mgr/bundlemgr_adapter.h" namespace OHOS { namespace UDMF { @@ -468,8 +468,7 @@ int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr runtime, const std: ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); return E_INVALID_PARAMETERS; } - int32_t userId = IPCSkeleton::GetCallingUid() / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; - std::string appId = BundleMgrAdapter::GetInstance().GetBundleAppId(bundleName, userId); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId({IPCSkeleton::GetCallingUid(), runtime->tokenId, bundleName}); 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()); -- Gitee From d18468ed7c4401f988a2b376b485591ef22af8fb Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Fri, 15 Aug 2025 15:08:34 +0800 Subject: [PATCH 04/11] fix appid Signed-off-by: wenjinchao Change-Id: Ifc575af784c4afa7a6bd6e748c500431cfeaaac1 Signed-off-by: wanxiaoqing --- .../service/udmf/preprocess/preprocess_utils.cpp | 3 ++- .../distributeddataservice/service/udmf/udmf_service_impl.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index a05173952..f31698f79 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -70,7 +70,8 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; - std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId({IPCSkeleton::GetCallingUid(), option.tokenId, bundleName}); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId( + { IPCSkeleton::GetCallingUid(), option.tokenId, bundleName }); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index bf1786eef..20f36a665 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -468,7 +468,8 @@ int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr runtime, const std: ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); return E_INVALID_PARAMETERS; } - std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId({IPCSkeleton::GetCallingUid(), runtime->tokenId, bundleName}); + std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId( + { IPCSkeleton::GetCallingUid(), runtime->tokenId, bundleName }); 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()); -- Gitee From dc46b648cbff382cea359f0b9528dddd85acc70e Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 16 Aug 2025 18:31:09 +0800 Subject: [PATCH 05/11] fix appid Signed-off-by: wenjinchao Change-Id: I17801c60e3a98f3c73222c4da5a491ee88607f6e --- .../distributeddataservice/app/test/BUILD.gn | 25 ++++ .../app/test/unittest/bundle_checker_test.cpp | 120 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 80725c641..d79c567b8 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -560,6 +560,30 @@ ohos_unittest("RouteHeadHandlerImplTest") { part_name = "datamgr_service" } +ohos_unittest("BundleCheckerTest") { + module_out_path = module_output_path + sources = [ "unittest/bundle_checker_test.cpp" ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "openssl:libcrypto_shared", + ] + + deps = [ + "${data_service_path}/app/src/checker:distributeddata_checker", + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] +} + ############################################################################### group("unittest") { @@ -567,6 +591,7 @@ group("unittest") { deps = [] deps += [ + ":BundleCheckerTest", ":FeatureStubImplTest", ":KvStoreDataServiceClearTest", ":KvStoreDataServiceTest", diff --git a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp new file mode 100644 index 000000000..965967806 --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp @@ -0,0 +1,120 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "accesstoken_kit.h" +#include "checker/bundle_checker.h" +#include "bootstrap.h" +#include "hap_token_info.h" +#include "nativetoken_kit.h" +#include "utils/crypto.h" +#include "utils/crypto.h" +using namespace testing::ext; +using namespace OHOS::DistributedData; +using namespace OHOS::Security::AccessToken; +class BundleCheckerTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + void SetUp(); + void TearDown(); + NativeTokenInfoParams infoInstance{0}; +}; + +void BundleCheckerTest::SetUp(void) +{ + infoInstance.dcapsNum = 0; + infoInstance.permsNum = 0; + infoInstance.aclsNum = 0; + infoInstance.dcaps = nullptr; + infoInstance.perms = nullptr; + infoInstance.acls = nullptr; + infoInstance.processName = "BundleCheckerTest"; + infoInstance.aplStr = "system_core"; + + HapInfoParams info = { + .userID = 100, + .bundleName = "com.ohos.dlpmanager", + .instIndex = 0, + .appIDDesc = "com.ohos.dlpmanager" + }; + PermissionDef infoManagerTestPermDef = { + .permissionName = "ohos.permission.test", + .bundleName = "ohos.test.demo", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "open the door", + .descriptionId = 1 + }; + PermissionStateFull infoManagerTestState = { + .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = {"local"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1} + }; + HapPolicyParams policy = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = {infoManagerTestPermDef}, + .permStateList = {infoManagerTestState} + }; + AccessTokenKit::AllocHapToken(info, policy); + + Bootstrap::GetInstance().LoadComponents(); + Bootstrap::GetInstance().LoadDirectory(); + Bootstrap::GetInstance().LoadCheckers(); + Bootstrap::GetInstance().LoadNetworks(); +} + +void BundleCheckerTest::TearDown() +{ + auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); + AccessTokenKit::DeleteToken(tokenId); +} + +/** +* @tc.name: SystemChecker bms +* @tc.desc: checker the bundle name of the system abilities. +* @tc.type: FUNC +* @tc.require: +* @tc.author: Sven Wang +*/ +HWTEST_F(BundleCheckerTest, SystemCheckerBMS, TestSize.Level0) +{ + CheckerManager::StoreInfo info; + auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); + info.uid = 100 * 200000; + info.tokenId = tokenId; + info.bundleName = "com.ohos.dlpmanager"; + BundleChecker checker; + std::string appIdInCache; + ASSERT_TRUE(!checker.GetAppId(info).empty()); + checker.appIds_.Get("com.ohos.dlpmanager###100", appIdInCache); + ASSERT_TRUE(!appIdInCache.empty()); + ASSERT_EQ(Crypto::Sha256(appIdInCache), checker.GetAppId(info)); + + checker.DeleteCache(info.bundleName, 100, 0); + std::string appIdInCache1; + checker.appIds_.Get("com.ohos.dlpmanager###100", appIdInCache1); + ASSERT_TRUE(appIdInCache1.empty()); + + checker.GetAppId(info); + ASSERT_GE(checker.appIds_.Size(), 1); + checker.ClearCache(); + ASSERT_EQ(checker.appIds_.Size(), 0); +} \ No newline at end of file -- Gitee From 47bbb2ab67b1cd16f04a2e4e5d90133be290c99f Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 16 Aug 2025 18:32:58 +0800 Subject: [PATCH 06/11] fix appid Signed-off-by: wenjinchao Change-Id: I916a760ba0edc66f03c96c4cdde7cb64b9b5733b --- .../app/test/unittest/bundle_checker_test.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp index 965967806..8770b0fce 100644 --- a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp +++ b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Huawei Device Co., Ltd. +* 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 @@ -20,7 +20,6 @@ #include "hap_token_info.h" #include "nativetoken_kit.h" #include "utils/crypto.h" -#include "utils/crypto.h" using namespace testing::ext; using namespace OHOS::DistributedData; using namespace OHOS::Security::AccessToken; @@ -88,13 +87,12 @@ void BundleCheckerTest::TearDown() } /** -* @tc.name: SystemChecker bms -* @tc.desc: checker the bundle name of the system abilities. +* @tc.name: GetAppIdTest001 +* @tc.desc: get appId from cache. * @tc.type: FUNC * @tc.require: -* @tc.author: Sven Wang */ -HWTEST_F(BundleCheckerTest, SystemCheckerBMS, TestSize.Level0) +HWTEST_F(BundleCheckerTest, GetAppIdTest001, TestSize.Level0) { CheckerManager::StoreInfo info; auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); -- Gitee From 0791acc0f4869e0247fc573d2901c964c0c01869 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Sat, 16 Aug 2025 19:07:53 +0800 Subject: [PATCH 07/11] fix appid Signed-off-by: wenjinchao Change-Id: I6bc74a09d428c4fc7e54333e7cd052bbf47d94e7 Signed-off-by: wanxiaoqing --- .../service/test/mock/preprocess_utils_mock.cpp | 4 +++- .../service/test/mock/preprocess_utils_mock.h | 3 ++- .../test/udmf_preprocess_utils_mock_test.cpp | 9 ++++++--- .../service/udmf/preprocess/preprocess_utils.cpp | 10 ++++++---- .../service/udmf/preprocess/preprocess_utils.h | 3 ++- .../service/udmf/udmf_service_impl.cpp | 13 ++++++++----- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index 7b7e648f1..c1d117dda 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -188,8 +188,10 @@ bool PreProcessUtils::GetSpecificBundleName(const std::string &bundleName, int32 return true; } -bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName) { + specificBundleName = "specificBundleName"; bundleName = "bundleName"; return true; } diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h index e79e2bb6c..92af6e73e 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h @@ -43,7 +43,8 @@ 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 bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName); static std::string GetAppId(const std::string &bundleName); static sptr GetBundleMgr(); }; 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 93ea1516b..b09a55fff 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -88,8 +88,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId001, TestSi EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_FAILED)); EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_SHELL)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, false); } @@ -106,8 +107,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId002, TestSi EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_NATIVE)); EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, true); } @@ -122,8 +124,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId003, TestSi uint32_t tokenId = 9999; EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); std::string bundleName = ""; + std::string specificBundleName = ""; PreProcessUtils preProcessUtils; - bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, bundleName); + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, 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 f31698f79..48c5871d8 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -62,12 +62,13 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option return E_ERROR; } std::string bundleName; - if (!GetSpecificBundleNameByTokenId(option.tokenId, bundleName)) { + std::string specificBundleName; + if (!GetSpecificBundleNameByTokenId(option.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", option.tokenId); return E_ERROR; } std::string intention = it->second; - UnifiedKey key(intention, bundleName, GenerateId()); + UnifiedKey key(intention, specificBundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; std::string appId = DistributedData::CheckerManager::GetInstance().GetAppId( @@ -499,12 +500,13 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) return std::to_string(hapTokenInfo.apiVersion); } -bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, 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); + bundleName = hapInfo.bundleName; + return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, specificBundleName); } if (UTILS::IsTokenNative()) { ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 8cd126f55..8c3867e0f 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -43,7 +43,8 @@ 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 bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string & bundleName); static sptr GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector& 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 20f36a665..8abb6bfa0 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -417,11 +417,12 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_INVALID_PARAMETERS; } std::string bundleName; - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + std::string specificBundleName; + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return E_ERROR; } - if (key.bundleName != bundleName && !HasDatahubPriviledge(bundleName)) { + if (key.bundleName != specificBundleName && !HasDatahubPriviledge(bundleName)) { ZLOGE("update data failed by %{public}s, key: %{public}s.", bundleName.c_str(), query.key.c_str()); return E_INVALID_PARAMETERS; } @@ -538,7 +539,8 @@ bool UdmfServiceImpl::CheckDeleteDataPermission(std::string &appId, const std::s return true; } std::string bundleName; - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, bundleName)) { + std::string specificBundleName; + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(query.tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", query.tokenId); return false; } @@ -1157,12 +1159,13 @@ int32_t UdmfServiceImpl::SetDelayInfo(const DataLoadInfo &dataLoadInfo, sptr(IPCSkeleton::GetCallingTokenID()); - if (!PreProcessUtils::GetSpecificBundleNameByTokenId(tokenId, bundleName)) { + if (!PreProcessUtils::GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName)) { ZLOGE("GetSpecificBundleNameByTokenId failed, tokenid:%{public}u", tokenId); return E_ERROR; } - UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), bundleName, dataLoadInfo.sequenceKey); + UnifiedKey udkey(UD_INTENTION_MAP.at(UD_INTENTION_DRAG), specificBundleName, dataLoadInfo.sequenceKey); key = udkey.GetUnifiedKey(); dataLoadCallback_.Insert(key, iface_cast(iUdmfNotifier)); -- Gitee From e7c2eacc2a86843c424d795c3f94dff5e8c073f5 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Mon, 18 Aug 2025 10:37:53 +0800 Subject: [PATCH 08/11] fix appid Signed-off-by: wenjinchao Change-Id: I9fef6562314ff5b5fd6db18978a6a918ef84733e --- .../udmf/preprocess/preprocess_utils.cpp | 3 +- .../udmf/preprocess/preprocess_utils.h | 2 +- .../service/udmf/udmf_service_impl.cpp | 29 +++++++++++++------ .../service/udmf/udmf_service_impl.h | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 48c5871d8..fcacb5372 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -500,7 +500,8 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) return std::to_string(hapTokenInfo.apiVersion); } -bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, std::string &bundleName) +bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, + std::string &bundleName) { Security::AccessToken::HapTokenInfo hapInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 8c3867e0f..e378592c8 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -44,7 +44,7 @@ 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 &specificBundleName, - std::string & bundleName); + std::string &bundleName); static sptr GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector& 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 8abb6bfa0..d668981b9 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -438,8 +438,27 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi HandleDbError(key.intention, res); return res; } + auto verifyRes = VerifyUpdatePermission(query, data, bundleName); + if (verifyRes != E_OK) { + ZLOGE("VerifyUpdatePermission failed:%{public}d, key: %{public}s.", verifyRes, query.key.c_str()); + return verifyRes; + } + std::shared_ptr runtime = data.GetRuntime(); + runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); + unifiedData.SetRuntime(*runtime); + PreProcessUtils::SetRecordUid(unifiedData); + if ((res = store->Update(unifiedData)) != E_OK) { + ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); + HandleDbError(key.intention, res); + return E_DB_ERROR; + } + return E_OK; +} + +int32_t UdmfServiceImpl::VerifyUpdatePermission(const QueryOption &query, UnifiedData &data, std::string &bundleName) +{ if (data.IsEmpty()) { - ZLOGE("Invalid parameter, unified data has no record; intention: %{public}s.", key.intention.c_str()); + ZLOGE("Invalid parameter, unified data has no record"); return E_INVALID_PARAMETERS; } std::shared_ptr runtime = data.GetRuntime(); @@ -452,14 +471,6 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi ZLOGE("Update failed: tokenId or appId mismatch, bundleName: %{public}s", bundleName.c_str()); return E_INVALID_PARAMETERS; } - runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); - unifiedData.SetRuntime(*runtime); - PreProcessUtils::SetRecordUid(unifiedData); - if ((res = store->Update(unifiedData)) != E_OK) { - ZLOGE("Unified data update failed:%{public}s", key.intention.c_str()); - HandleDbError(key.intention, res); - return E_DB_ERROR; - } return E_OK; } diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 8b03412f1..0260d75fd 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -91,6 +91,7 @@ private: const UnifiedData &unifiedData); std::vector ProcessResult(const std::map &results); DistributedData::StoreMetaData BuildMeta(const std::string &storeId, int userId); + int32_t VerifyUpdatePermission(const QueryOption &query, UnifiedData &unifiedData, std::string &bundleName); class Factory { public: -- Gitee From a91130376491eb5479990be09293a94576d682c9 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Mon, 18 Aug 2025 14:19:33 +0800 Subject: [PATCH 09/11] fix appid Signed-off-by: wenjinchao Change-Id: Ie53fcf09a4e40adff8859c93e624178fd67305fa Signed-off-by: wanxiaoqing --- .../service/udmf/preprocess/preprocess_utils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index fcacb5372..5a8f2e146 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -514,6 +514,7 @@ bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::stri std::string processName; if (GetNativeProcessNameByToken(tokenId, processName)) { bundleName = std::move(processName); + specificBundleName = bundleName; return true; } } -- Gitee From 9daae38822df39b28cb3777c79d9302d30b4bcc2 Mon Sep 17 00:00:00 2001 From: wenjinchao Date: Mon, 18 Aug 2025 16:47:22 +0800 Subject: [PATCH 10/11] fix appid Signed-off-by: wenjinchao Change-Id: Ib85cea627b7782044d77807b0de8ab4a1c72fe14 Signed-off-by: wanxiaoqing --- .../distributeddataservice/app/src/checker/bundle_checker.h | 2 +- .../app/test/unittest/bundle_checker_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/app/src/checker/bundle_checker.h b/services/distributeddataservice/app/src/checker/bundle_checker.h index a7b07159c..d5c655bb9 100644 --- a/services/distributeddataservice/app/src/checker/bundle_checker.h +++ b/services/distributeddataservice/app/src/checker/bundle_checker.h @@ -50,7 +50,7 @@ private: std::vector dynamicStores_; std::vector staticStores_; std::string GetBundleAppId(const CheckerManager::StoreInfo &info); - LRUBucket appIds_ {CACHE_SIZE}; + LRUBucket appIds_ { CACHE_SIZE }; }; } // namespace DistributedData } // namespace OHOS diff --git a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp index 8770b0fce..deb5c7def 100644 --- a/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp +++ b/services/distributeddataservice/app/test/unittest/bundle_checker_test.cpp @@ -103,7 +103,7 @@ HWTEST_F(BundleCheckerTest, GetAppIdTest001, TestSize.Level0) std::string appIdInCache; ASSERT_TRUE(!checker.GetAppId(info).empty()); checker.appIds_.Get("com.ohos.dlpmanager###100", appIdInCache); - ASSERT_TRUE(!appIdInCache.empty()); + ASSERT_FALSE(appIdInCache.empty()); ASSERT_EQ(Crypto::Sha256(appIdInCache), checker.GetAppId(info)); checker.DeleteCache(info.bundleName, 100, 0); -- Gitee From 5d6872be4273a7bd9e96a1507594ab0f90f466c5 Mon Sep 17 00:00:00 2001 From: wan-xiaoqing321 Date: Fri, 22 Aug 2025 10:12:38 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E6=96=B0=E5=A2=9Etokenid=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wan-xiaoqing321 Signed-off-by: wanxiaoqing --- .../test/mock/preprocess_utils_mock.cpp | 4 +- .../service/test/mock/preprocess_utils_mock.h | 4 +- .../test/udmf_preprocess_utils_mock_test.cpp | 59 ++++++++++++++++--- .../udmf/preprocess/preprocess_utils.cpp | 32 +++++----- .../udmf/preprocess/preprocess_utils.h | 4 +- 5 files changed, 74 insertions(+), 29 deletions(-) diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index c1d117dda..37238e41c 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -82,13 +82,13 @@ int32_t PreProcessUtils::GetHapUidByToken(uint32_t tokenId, int &userId) return E_OK; } -bool PreProcessUtils::GetHapBundleNameByToken(int tokenId, std::string &bundleName) +bool PreProcessUtils::GetHapBundleNameByToken(uint32_t tokenId, std::string &bundleName) { bundleName = "processName"; return true; } -bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &processName) +bool PreProcessUtils::GetNativeProcessNameByToken(uint32_t tokenId, std::string &processName) { processName = "processName"; return true; diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h index 92af6e73e..d71695fdb 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h @@ -26,8 +26,8 @@ public: static std::string GenerateId(); static time_t GetTimestamp(); static int32_t GetHapUidByToken(uint32_t tokenId, int &userId); - static bool GetHapBundleNameByToken(int tokenId, std::string &bundleName); - static bool GetNativeProcessNameByToken(int tokenId, std::string &processName); + static bool GetHapBundleNameByToken(uint32_t tokenId, std::string &bundleName); + static bool GetNativeProcessNameByToken(uint32_t tokenId, std::string &processName); static std::string GetLocalDeviceId(); static void SetRemoteData(UnifiedData &data); static int32_t SetRemoteUri(uint32_t tokenId, UnifiedData &data); 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 b09a55fff..0ca8e3b2d 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -16,6 +16,7 @@ #include "preprocess_utils.h" #include "gtest/gtest.h" #include "access_token_mock.h" +#include "ipc_skeleton.h" namespace OHOS::UDMF { using namespace testing; @@ -54,7 +55,7 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetHapUidByToken001, TestSize.Level1) uint32_t tokenId = 0; int userId = 0; PreProcessUtils preProcessUtils; - EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_SUCCESS)); int32_t ret = preProcessUtils.GetHapUidByToken(tokenId, userId); EXPECT_EQ(ret, E_OK); } @@ -70,8 +71,8 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetInstIndex001, TestSize.Level1) uint32_t tokenId = 0; int32_t instIndex = 0; PreProcessUtils preProcessUtils; - EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_HAP)); - EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_HAP)); + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_SUCCESS)); bool ret = preProcessUtils.GetInstIndex(tokenId, instIndex); EXPECT_EQ(ret, true); } @@ -85,8 +86,8 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetInstIndex001, TestSize.Level1) 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)); + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_SHELL)); std::string bundleName = ""; std::string specificBundleName = ""; PreProcessUtils preProcessUtils; @@ -103,9 +104,9 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId001, TestSi 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)); + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_NATIVE)); + EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillRepeatedly(Return(RET_SUCCESS)); std::string bundleName = ""; std::string specificBundleName = ""; PreProcessUtils preProcessUtils; @@ -122,11 +123,51 @@ HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId002, TestSi HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId003, TestSize.Level1) { uint32_t tokenId = 9999; - EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_SUCCESS)); std::string bundleName = ""; std::string specificBundleName = ""; PreProcessUtils preProcessUtils; bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); EXPECT_EQ(ret, true); } + +/** +* @tc.name: GetAlterableBundleNameByTokenId004 +* @tc.desc: Abnormal test of GetSpecificBundleNameByTokenId for native +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, GetAlterableBundleNameByTokenId004, TestSize.Level1) +{ + uint32_t tokenId = 999; + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_NATIVE)); + EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillRepeatedly(Return(RET_FAILED)); + std::string bundleName = ""; + std::string specificBundleName = ""; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.GetSpecificBundleNameByTokenId(tokenId, specificBundleName, bundleName); + EXPECT_EQ(ret, false); +} + +/** +* @tc.name: FillRuntimeInfo001 +* @tc.desc: Normal test of FillRuntimeInfo +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, FillRuntimeInfo001, TestSize.Level1) +{ + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillRepeatedly(Return(RET_FAILED)); + EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_NATIVE)); + EXPECT_CALL(*accessTokenKitMock, GetNativeTokenInfo(_, _)).WillRepeatedly(Return(RET_SUCCESS)); + + UnifiedData data; + CustomOption option; + option.intention = UD_INTENTION_DATA_HUB; + option.tokenId = static_cast(IPCSkeleton::GetCallingTokenID()); + PreProcessUtils preProcessUtils; + auto ret = preProcessUtils.FillRuntimeInfo(data, option); + EXPECT_EQ(ret, E_OK); +} }; // 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 5a8f2e146..9e7c45d15 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -111,6 +111,10 @@ time_t PreProcessUtils::GetTimestamp() int32_t PreProcessUtils::GetHapUidByToken(uint32_t tokenId, int &userId) { + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + ZLOGE("TokenType is not TOKEN_HAP"); + return E_ERROR; + } Security::AccessToken::HapTokenInfo tokenInfo; auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { @@ -121,14 +125,8 @@ int32_t PreProcessUtils::GetHapUidByToken(uint32_t tokenId, int &userId) return E_OK; } -bool PreProcessUtils::GetHapBundleNameByToken(int tokenId, std::string &bundleName) +bool PreProcessUtils::GetHapBundleNameByToken(uint32_t tokenId, std::string &bundleName) { - Security::AccessToken::HapTokenInfo hapInfo; - if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) - == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { - bundleName = hapInfo.bundleName; - return true; - } if (UTILS::IsTokenNative()) { ZLOGD("TypeATokenTypeEnum is TOKEN_HAP"); std::string processName; @@ -137,11 +135,17 @@ bool PreProcessUtils::GetHapBundleNameByToken(int tokenId, std::string &bundleNa return true; } } + Security::AccessToken::HapTokenInfo hapInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) + == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { + bundleName = hapInfo.bundleName; + return true; + } ZLOGE("Get bundle name faild"); return false; } -bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &processName) +bool PreProcessUtils::GetNativeProcessNameByToken(uint32_t tokenId, std::string &processName) { Security::AccessToken::NativeTokenInfo nativeInfo; if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, nativeInfo) @@ -503,12 +507,6 @@ std::string PreProcessUtils::GetSdkVersionByToken(uint32_t tokenId) bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &specificBundleName, std::string &bundleName) { - Security::AccessToken::HapTokenInfo hapInfo; - if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) - == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { - bundleName = hapInfo.bundleName; - return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, specificBundleName); - } if (UTILS::IsTokenNative()) { ZLOGI("TypeATokenTypeEnum is TOKEN_NATIVE"); std::string processName; @@ -518,6 +516,12 @@ bool PreProcessUtils::GetSpecificBundleNameByTokenId(uint32_t tokenId, std::stri return true; } } + Security::AccessToken::HapTokenInfo hapInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) + == Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { + bundleName = hapInfo.bundleName; + return GetSpecificBundleName(hapInfo.bundleName, hapInfo.instIndex, specificBundleName); + } ZLOGE("Get bundle name faild, tokenid:%{public}u", tokenId); return false; } diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index e378592c8..1ebe94d0b 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -26,8 +26,8 @@ public: static std::string GenerateId(); static time_t GetTimestamp(); static int32_t GetHapUidByToken(uint32_t tokenId, int &userId); - static bool GetHapBundleNameByToken(int tokenId, std::string &bundleName); - static bool GetNativeProcessNameByToken(int tokenId, std::string &processName); + static bool GetHapBundleNameByToken(uint32_t tokenId, std::string &bundleName); + static bool GetNativeProcessNameByToken(uint32_t tokenId, std::string &processName); static std::string GetLocalDeviceId(); static void SetRemoteData(UnifiedData &data); static int32_t SetRemoteUri(uint32_t tokenId, UnifiedData &data); -- Gitee