diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp index 37238e41c4b1bcea9cc11f95e6abbdda68cfde60..948c89dc3d7d20c7a45a383dc87d3d75b8dd2206 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.cpp @@ -104,7 +104,7 @@ void PreProcessUtils::SetRemoteData(UnifiedData &data) return; } -int32_t PreProcessUtils::SetRemoteUri(uint32_t tokenId, UnifiedData &data) +int32_t PreProcessUtils::HandleFileUris(uint32_t tokenId, UnifiedData &data) { return E_OK; } diff --git a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h index d71695fdbcad14b19e7f5d164e078c442949f84b..ad5b6a864be3a9be5e31b473b92eaa74af658733 100644 --- a/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h +++ b/services/distributeddataservice/service/test/mock/preprocess_utils_mock.h @@ -30,7 +30,7 @@ public: 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); + static int32_t HandleFileUris(uint32_t tokenId, UnifiedData &data); static bool GetInstIndex(uint32_t tokenId, int32_t &instIndex); static bool IsNetworkingEnabled(); static void ProcessFileType(std::vector> records, diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index 541089c743ace704355e480b1607062a807c2b8d..5584c0b242f14a2170386e0ba12ac3439c0bf02e 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -42,6 +42,7 @@ using UnifiedData = OHOS::UDMF::UnifiedData; using Summary = OHOS::UDMF::Summary; namespace OHOS::Test { namespace DistributedDataTest { +static constexpr const char *HAP_BUNDLE_NAME = "ohos.mytest.demo"; static void GrantPermissionNative() { @@ -80,16 +81,64 @@ public: DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors); DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter(); DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener(); + AllocTestHapToken(); + } + static void TearDownTestCase(void) + { + DeleteTestHapToken(); } - static void TearDownTestCase(void){}; void SetUp(){}; void TearDown(){}; + static void AllocTestHapToken(); + static void DeleteTestHapToken(); static constexpr const char *STORE_ID = "drag"; static constexpr uint32_t TOKEN_ID = 5; static constexpr const char *APP_ID = "appId"; }; +void UdmfServiceImplTest::AllocTestHapToken() +{ + HapInfoParams info = { + .userID = 100, + .bundleName = HAP_BUNDLE_NAME, + .instIndex = 0, + .appIDDesc = "ohos.mytest.demo_09AEF01D" + }; + HapPolicyParams policy = { + .apl = APL_NORMAL, + .domain = "test.domain", + .permList = { + { + .permissionName = "ohos.permission.test", + .bundleName = HAP_BUNDLE_NAME, + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "open the door", + .descriptionId = 1 + } + }, + .permStateList = { + { + .permissionName = "ohos.permission.test", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } + } + } + }; + AccessTokenKit::AllocHapToken(info, policy); +} + +void UdmfServiceImplTest::DeleteTestHapToken() +{ + auto tokenId = AccessTokenKit::GetHapTokenID(100, HAP_BUNDLE_NAME, 0); + AccessTokenKit::DeleteToken(tokenId); +} + /** * @tc.name: SaveData001 * @tc.desc: Abnormal test of SaveData, unifiedData is invalid @@ -986,5 +1035,57 @@ HWTEST_F(UdmfServiceImplTest, PushDelayData004, TestSize.Level1) auto status = service.PushDelayData(query.key, insertedData); EXPECT_EQ(status, UDMF::E_OK); } + +/** + * @tc.name: SaveData005 + * @tc.desc: test no permission + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, SaveData005, TestSize.Level1) +{ + CustomOption option; + option.intention = UD_INTENTION_DRAG; + option.tokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(100, HAP_BUNDLE_NAME, 0); + + std::string key = ""; + UnifiedData unifiedData; + std::shared_ptr obj = std::make_shared(); + obj->value_[UNIFORM_DATA_TYPE] = "general.file-uri"; + obj->value_[FILE_URI_PARAM] = "file://error_bundle_name/a.jpeg"; + obj->value_[FILE_TYPE] = "general.image"; + auto record = std::make_shared(UDType::FILE_URI, obj); + unifiedData.AddRecord(record); + + UdmfServiceImpl impl; + auto status = impl.SaveData(option, unifiedData, key); + EXPECT_EQ(status, E_NO_PERMISSION); +} + +/** + * @tc.name: PushDelayData007 + * @tc.desc: test no permission + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, PushDelayData007, TestSize.Level1) +{ + QueryOption query; + query.key = "k1"; + + UdmfServiceImpl service; + DelayGetDataInfo delayGetDataInfo; + service.delayDataCallback_.Insert(query.key, delayGetDataInfo); + + UnifiedData insertedData; + std::shared_ptr obj = std::make_shared(); + obj->value_[UNIFORM_DATA_TYPE] = "general.file-uri"; + obj->value_[FILE_URI_PARAM] = "file://error_bundle_name/a.jpeg"; + obj->value_[FILE_TYPE] = "general.image"; + auto record = std::make_shared(UDType::FILE_URI, obj); + insertedData.AddRecord(record); + + auto status = service.PushDelayData(query.key, insertedData); + EXPECT_EQ(status, E_NO_PERMISSION); + service.delayDataCallback_.Clear(); +} }; // namespace DistributedDataTest }; // namespace OHOS::Test diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 9e7c45d1563629e1f42b2ae3522e045a5bc218af..6ad139152e51fec762f3f019831822f3d269f12c 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -190,7 +190,7 @@ void PreProcessUtils::SetRemoteData(UnifiedData &data) }); } -int32_t PreProcessUtils::SetRemoteUri(uint32_t tokenId, UnifiedData &data) +int32_t PreProcessUtils::HandleFileUris(uint32_t tokenId, UnifiedData &data) { std::vector uris; ProcessFileType(data.GetRecords(), [&uris](std::shared_ptr obj) { diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 1ebe94d0ba5fc618572df0b3aecd894e1b81960d..4b5013a8cba968d4793bdda0c6452ec13648f0a1 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -30,7 +30,7 @@ public: 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); + static int32_t HandleFileUris(uint32_t tokenId, UnifiedData &data); static bool GetInstIndex(uint32_t tokenId, int32_t &instIndex); static bool IsNetworkingEnabled(); static void ProcessFileType(std::vector> records, diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 4db92da9345e0d2be2dbf31620094a2a2e74ed73..35a14a8e06d16a9b77c10e42e6f2bf3104585100 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -140,10 +140,11 @@ int32_t UdmfServiceImpl::SaveData(CustomOption &option, UnifiedData &unifiedData } std::string intention = unifiedData.GetRuntime()->key.intention; if (intention == UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) { - int32_t ret = PreProcessUtils::SetRemoteUri(option.tokenId, unifiedData); + int32_t ret = PreProcessUtils::HandleFileUris(option.tokenId, unifiedData); if (ret != E_OK) { - ZLOGW("SetRemoteUri failed, ret: %{public}d, bundleName:%{public}s.", ret, + ZLOGE("HandleFileUris failed, ret: %{public}d, bundleName:%{public}s.", ret, unifiedData.GetRuntime()->createPackage.c_str()); + return ret; } } PreProcessUtils::SetRecordUid(unifiedData); @@ -269,6 +270,10 @@ int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &uni if (res != E_OK) { return res; } + if (!IsReadAndKeep(runtime->privileges, query) && LifeCycleManager::GetInstance().OnGot(key) != E_OK) { + ZLOGE("Remove data failed:%{public}s", key.intention.c_str()); + return E_DB_ERROR; + } if (key.intention == UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) { int32_t ret = ProcessUri(query, unifiedData); @@ -279,10 +284,6 @@ int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &uni return E_NO_PERMISSION; } } - if (!IsReadAndKeep(runtime->privileges, query) && LifeCycleManager::GetInstance().OnGot(key) != E_OK) { - ZLOGE("Remove data failed:%{public}s", key.intention.c_str()); - return E_DB_ERROR; - } { std::lock_guard lock(cacheMutex_); @@ -1245,9 +1246,10 @@ int32_t UdmfServiceImpl::PushDelayData(const std::string &key, UnifiedData &unif ZLOGE("Imputation failed"); return E_ERROR; } - int32_t ret = PreProcessUtils::SetRemoteUri(option.tokenId, unifiedData); + int32_t ret = PreProcessUtils::HandleFileUris(option.tokenId, unifiedData); if (ret != E_OK) { - ZLOGW("SetRemoteUri failed, ret:%{public}d, key:%{public}s.", ret, key.c_str()); + ZLOGE("HandleFileUris failed, ret:%{public}d, key:%{public}s.", ret, key.c_str()); + return ret; } QueryOption query;