diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn old mode 100755 new mode 100644 index 52cb469b2ccbe8cbe276837f4e2f992058d77db3..bf081dca670533624c054addec62e5da0238fc54 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -922,6 +922,8 @@ ohos_unittest("UdmfRunTimeStoreTest") { "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "dataclassification:data_transit_mgr", "dsoftbus:softbus_client", @@ -930,6 +932,7 @@ ohos_unittest("UdmfRunTimeStoreTest") { "image_framework:image", "ipc:ipc_core", "kv_store:distributeddata_inner", + "samgr:samgr_proxy", "udmf:udmf_client", ] @@ -1191,6 +1194,7 @@ ohos_unittest("UdmfServiceImplTest") { "ability_runtime:uri_permission_mgr", "access_token:libaccesstoken_sdk", "app_file_service:remote_file_share_native", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "device_manager:devicemanagersdk", @@ -1200,6 +1204,7 @@ ohos_unittest("UdmfServiceImplTest") { "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", + "samgr:samgr_proxy", "udmf:udmf_client", ] } @@ -1304,17 +1309,98 @@ ohos_unittest("UdmfServiceStubMockTest") { "access_token:libtoken_setproc", "access_token:libtokenid_sdk", "app_file_service:remote_file_share_native", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "device_manager:devicemanagersdk", "googletest:gtest_main", "hilog:libhilog", + "hisysevent:libhisysevent", "hitrace:hitrace_meter", "hitrace:libhitracechain", "ipc:ipc_core", "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", + "samgr:samgr_proxy", + "udmf:udmf_client", + ] +} + +ohos_unittest("UdmfPreProcessUtilsTest") { + module_out_path = module_output_path + sources = [ "udmf_preprocess_utils_test.cpp" ] + + include_dirs = [ + "${data_service_path}/service/udmf/permission", + "${data_service_path}/service/udmf/preprocess", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + deps = [ + "${data_service_path}/service:distributeddatasvc", + "${data_service_path}/service/udmf:udmf_server", + ] + + external_deps = [ + "ability_runtime:uri_permission_mgr", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "device_manager:devicemanagersdk", + "googletest:gtest_main", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "kv_store:distributeddata_inner", + "kv_store:distributeddb", + "relational_store:native_rdb", + "udmf:udmf_client", + ] +} + +ohos_unittest("UdmfPreProcessUtilsMockTest") { + module_out_path = module_output_path + sources = [ + "${data_service_path}/service/test/mock/access_token_mock.cpp", + "${data_service_path}/service/udmf/preprocess/preprocess_utils.cpp", + "udmf_preprocess_utils_mock_test.cpp", + ] + + include_dirs = [ + "${data_service_path}/service/udmf/preprocess", + "${data_service_path}/service/test/mock", + "${data_service_path}/service/udmf/store", + "${data_service_path}/framework/include/account", + "${data_service_path}/adapter/include/communicator", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + deps = [ + "${data_service_path}/service:distributeddatasvc", + "mock:distributeddata_mock_static", + ] + + external_deps = [ + "ability_runtime:uri_permission_mgr", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "app_file_service:remote_file_share_native", + "c_utils:utils", + "device_manager:devicemanagersdk", + "googletest:gmock_main", + "googletest:gtest_main", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", "udmf:udmf_client", ] } diff --git a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn index e3e58f256fb914e20ecf39a18b653ed03062d357..6b0a7d2bd30f4f3506a2bbc3562cf1ebd26c62aa 100644 --- a/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/udmfservice_fuzzer/BUILD.gn @@ -57,6 +57,7 @@ ohos_fuzztest("UdmfServiceFuzzTest") { "ability_runtime:uri_permission_mgr", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", @@ -64,6 +65,7 @@ ohos_fuzztest("UdmfServiceFuzzTest") { "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", + "samgr:samgr_proxy", "udmf:udmf_client", ] } diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72ac33d88a74659b7eedcdee2d39e33201ec271c --- /dev/null +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_mock_test.cpp @@ -0,0 +1,78 @@ +/* +* 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. +*/ + +#include "preprocess_utils.h" +#include "gtest/gtest.h" +#include "access_token_mock.h" + +namespace OHOS::UDMF { +using namespace testing; +using namespace std; +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; +class UdmfPreProcessUtilsMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() {} + void TearDown() {} + static inline shared_ptr accessTokenKitMock = nullptr; +}; + +void UdmfPreProcessUtilsMockTest::SetUpTestCase(void) +{ + accessTokenKitMock = make_shared(); + BAccessTokenKit::accessTokenkit = accessTokenKitMock; +} + +void UdmfPreProcessUtilsMockTest::TearDownTestCase(void) +{ + BAccessTokenKit::accessTokenkit = nullptr; + accessTokenKitMock = nullptr; +} + +/** +* @tc.name: GetHapUidByToken001 +* @tc.desc: Abnormal test of GetHapUidByToken, AccessTokenKit GetHapTokenInfo failed +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsMockTest, GetHapUidByToken001, TestSize.Level1) +{ + uint32_t tokenId = 0; + int userId = 0; + PreProcessUtils preProcessUtils; + EXPECT_CALL(*accessTokenKitMock, GetHapTokenInfo(_, _)).WillOnce(Return(RET_SUCCESS)); + int32_t ret = preProcessUtils.GetHapUidByToken(tokenId, userId); + EXPECT_EQ(ret, E_OK); +} + +/** +* @tc.name: GetInstIndex001 +* @tc.desc: Abnormal test of GetInstIndex, AccessTokenKit GetInstIndex failed +* @tc.type: FUNC +* @tc.require: +*/ +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)); + bool ret = preProcessUtils.GetInstIndex(tokenId, instIndex); + EXPECT_EQ(ret, true); +} +}; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2201229a6845e8a0fdf50967b33c1a73835f90a7 --- /dev/null +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp @@ -0,0 +1,182 @@ +/* +* 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. +*/ + +#include "preprocess_utils.h" +#include "gtest/gtest.h" +#include "text.h" + +namespace OHOS::UDMF { +using namespace testing::ext; +class UdmfPreProcessUtilsTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + void SetUp() {} + void TearDown() {} +}; + +/** +* @tc.name: RuntimeDataImputation001 +* @tc.desc: Abnormal test of RuntimeDataImputation, option is invalid +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, RuntimeDataImputation001, TestSize.Level1) +{ + UnifiedData data; + CustomOption option; + PreProcessUtils preProcessUtils; + int32_t ret = preProcessUtils.RuntimeDataImputation(data, option); + EXPECT_EQ(ret, E_ERROR); +} + +/** +* @tc.name: GetHapUidByToken001 +* @tc.desc: Abnormal test of GetHapUidByToken, tokenId is invalid +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, GetHapUidByToken001, TestSize.Level1) +{ + uint32_t tokenId = 0; + int userId = 0; + PreProcessUtils preProcessUtils; + int32_t ret = preProcessUtils.GetHapUidByToken(tokenId, userId); + EXPECT_EQ(ret, E_ERROR); +} + +/** +* @tc.name: SetRemoteData001 +* @tc.desc: Abnormal test of SetRemoteData, data is null +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, SetRemoteData001, TestSize.Level1) +{ + UnifiedData data; + PreProcessUtils preProcessUtils; + EXPECT_NO_FATAL_FAILURE(preProcessUtils.SetRemoteData(data)); +} + +/** +* @tc.name: SetRemoteData002 +* @tc.desc: Normal test of SetRemoteData +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, SetRemoteData002, TestSize.Level1) +{ + UnifiedData data; + std::vector> inputRecords; + for (int32_t i = 0; i < 512; ++i) { + inputRecords.emplace_back(std::make_shared()); + } + data.SetRecords(inputRecords); + data.runtime_ = std::make_shared(); + PreProcessUtils preProcessUtils; + EXPECT_NO_FATAL_FAILURE(preProcessUtils.SetRemoteData(data)); +} + +/** +* @tc.name: IsFileType001 +* @tc.desc: Abnormal test of IsFileType, record is nullptr +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, IsFileType001, TestSize.Level1) +{ + std::shared_ptr record = nullptr; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.IsFileType(record); + EXPECT_EQ(ret, false); +} + +/** +* @tc.name: GetDfsUrisFromLocal001 +* @tc.desc: Abnormal test of GetDfsUrisFromLocal, uris is null +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, GetDfsUrisFromLocal001, TestSize.Level1) +{ + const std::vector uris; + int32_t userId = 0; + UnifiedData data; + PreProcessUtils preProcessUtils; + int32_t ret = preProcessUtils.GetDfsUrisFromLocal(uris, userId, data); + EXPECT_EQ(ret, E_FS_ERROR); +} + +/** +* @tc.name: CheckUriAuthorization001 +* @tc.desc: Abnormal test of CheckUriAuthorization, uris is invalid +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, CheckUriAuthorization001, TestSize.Level1) +{ + const std::vector uris = {"test"}; + uint32_t tokenId = 0; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.CheckUriAuthorization(uris, tokenId); + EXPECT_EQ(ret, false); +} + +/** +* @tc.name: GetInstIndex001 +* @tc.desc: Normal test of GetInstIndex +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, GetInstIndex001, TestSize.Level1) +{ + uint32_t tokenId = 0; + int32_t instIndex = 0; + PreProcessUtils preProcessUtils; + bool ret = preProcessUtils.GetInstIndex(tokenId, instIndex); + EXPECT_EQ(instIndex, 0); + EXPECT_EQ(ret, true); +} + +/** +* @tc.name: ProcessFileType001 +* @tc.desc: Abnormal test of ProcessFileType, records is nullptr +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, ProcessFileType001, TestSize.Level1) +{ + std::vector> records = { nullptr }; + std::function)> callback; + PreProcessUtils preProcessUtils; + EXPECT_NO_FATAL_FAILURE(preProcessUtils.ProcessFileType(records, callback)); +} + +/** +* @tc.name: GetHtmlFileUris001 +* @tc.desc: Abnormal test of GetHtmlFileUris, uris is invalid +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, GetHtmlFileUris001, TestSize.Level1) +{ + uint32_t tokenId = 0; + UnifiedData data; + bool isLocal = false; + std::vector uris = {"test"}; + PreProcessUtils preProcessUtils; + EXPECT_NO_FATAL_FAILURE(preProcessUtils.GetHtmlFileUris(tokenId, data, isLocal, uris)); +} +}; // 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 55830d53ba9ea71edec2abbdc73535b0dc679c15..d93a559053eff8627f4732a64cfbba2c83a59132 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -216,4 +216,28 @@ HWTEST_F(UdmfServiceImplTest, OnUserChangeTest001, TestSize.Level1) ASSERT_EQ(sizeAfter, 0); } +/** +* @tc.name: TransferToEntriesIfNeedTest001 +* @tc.desc: TransferToEntriesIfNeed test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfServiceImplTest, TransferToEntriesIfNeedTest001, TestSize.Level1) +{ + UnifiedData data; + QueryOption query; + auto record1 = std::make_shared(); + auto record2 = std::make_shared(); + data.AddRecord(record1); + data.AddRecord(record2); + auto properties = std::make_shared(); + properties->tag = "records_to_entries_data_format"; + data.SetProperties(properties); + query.tokenId = 1; + UdmfServiceImpl udmfServiceImpl; + udmfServiceImpl.TransferToEntriesIfNeed(query, data); + EXPECT_TRUE(data.IsNeedTransferToEntries()); + int recordSize = 2; + EXPECT_EQ(data.GetRecords().size(), recordSize); +} }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index 5fd2313034164e68516e8b7c9d8190ecb7ca918a..854d247138c183232020b2349ef4ce4eb195ee58 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -72,19 +72,28 @@ ohos_source_set("udmf_server") { "-Oz", ] - deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + deps = [ + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service/bootstrap:distributeddata_bootstrap", + ] external_deps = [ "ability_base:zuri", "ability_runtime:uri_permission_mgr", "access_token:libtokenid_sdk", "app_file_service:remote_file_share_native", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", "device_manager:devicemanagersdk", "hilog:libhilog", + "hisysevent:libhisysevent", "hitrace:hitrace_meter", "hitrace:libhitracechain", "ipc:ipc_core", "kv_store:distributeddb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", "udmf:udmf_client", "udmf:utd_client", ] diff --git a/services/distributeddataservice/service/udmf/lifecycle/lifecycle_policy.cpp b/services/distributeddataservice/service/udmf/lifecycle/lifecycle_policy.cpp index cc3d88659c07bafb8a2561ee2afcf9dea85e2ae7..30a3241a893f541b42c62ec8443c9a6cd8653761 100644 --- a/services/distributeddataservice/service/udmf/lifecycle/lifecycle_policy.cpp +++ b/services/distributeddataservice/service/udmf/lifecycle/lifecycle_policy.cpp @@ -30,7 +30,7 @@ Status LifeCyclePolicy::OnGot(const UnifiedKey &key) ZLOGE("Get store failed:%{public}s", key.intention.c_str()); return E_DB_ERROR; } - if (store->Delete(key.key) != E_OK) { + if (store->Delete(UnifiedKey(key.key).GetPropertyKey()) != E_OK) { ZLOGE("Remove data failed:%{public}s", key.intention.c_str()); return E_DB_ERROR; } @@ -92,7 +92,7 @@ Status LifeCyclePolicy::GetTimeoutKeys( } if (curTime > data.GetRuntime()->createTime + duration_cast(interval).count() || curTime < data.GetRuntime()->createTime) { - timeoutKeys.push_back(data.GetRuntime()->key.key); + timeoutKeys.push_back(UnifiedKey(data.GetRuntime()->key.key).GetPropertyKey()); } } return E_OK; diff --git a/services/distributeddataservice/service/udmf/preprocess/data_handler.cpp b/services/distributeddataservice/service/udmf/preprocess/data_handler.cpp index 94d6ad7f15bed33d5727bdd5c8548d85a272f0ac..d772d99983642dba33ddc6d12455c9b193260f7b 100644 --- a/services/distributeddataservice/service/udmf/preprocess/data_handler.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/data_handler.cpp @@ -21,6 +21,7 @@ namespace OHOS::UDMF { constexpr const char *UD_KEY_SEPARATOR = "/"; constexpr const char *UD_KEY_ENTRY_SEPARATOR = "#"; +constexpr const char *UD_KEY_PROPERTIES_SEPARATOR = "#properties"; Status DataHandler::MarshalToEntries(const UnifiedData &unifiedData, std::vector &entries) { @@ -34,6 +35,16 @@ Status DataHandler::MarshalToEntries(const UnifiedData &unifiedData, std::vector std::vector udKeyBytes = { unifiedKey.begin(), unifiedKey.end() }; Entry entry = { udKeyBytes, runtimeBytes }; entries.emplace_back(entry); + std::string propsKey = unifiedData.GetRuntime()->key.GetPropertyKey() + UD_KEY_PROPERTIES_SEPARATOR; + std::vector propsBytes; + auto propsTlv = TLVObject(propsBytes); + if (!TLVUtil::Writing(*unifiedData.GetProperties(), propsTlv, TAG::TAG_PROPERTIES)) { + ZLOGE("Properties info marshalling failed:%{public}s", propsKey.c_str()); + return E_WRITE_PARCEL_ERROR; + } + std::vector propsKeyBytes = { propsKey.begin(), propsKey.end() }; + Entry propsEntry = { propsKeyBytes, propsBytes }; + entries.emplace_back(std::move(propsEntry)); return BuildEntries(unifiedData.GetRecords(), unifiedKey, entries); } @@ -75,7 +86,14 @@ Status DataHandler::UnmarshalEntryItem(UnifiedData &unifiedData, const std::vect continue; } auto isStartWithKey = keyStr.find(key) == 0; - if (!isStartWithKey) { + std::string propsKey = UnifiedKey(key).GetPropertyKey() + UD_KEY_PROPERTIES_SEPARATOR; + if (!isStartWithKey && (keyStr == propsKey)) { + std::shared_ptr properties; + if (!TLVUtil::ReadTlv(properties, data, TAG::TAG_PROPERTIES)) { + ZLOGE("Unmarshall unified properties failed."); + return E_READ_PARCEL_ERROR; + } + unifiedData.SetProperties(std::move(properties)); continue; } auto isEntryItem = keyStr.rfind(UD_KEY_ENTRY_SEPARATOR) != std::string::npos; diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.cpp b/services/distributeddataservice/service/udmf/store/runtime_store.cpp index 28ccfb432ee3ac4ca2ac56ac7e451bbacf70d225..7bffd601591dab878cd45a0c3632dbdca947859b 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.cpp +++ b/services/distributeddataservice/service/udmf/store/runtime_store.cpp @@ -36,7 +36,6 @@ using namespace RadarReporter; using namespace DistributedDB; using Anonymous = OHOS::DistributedData::Anonymous; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; - constexpr const char *SUMMARY_SUFIX = "#summary"; RuntimeStore::RuntimeStore(const std::string &storeId) : storeId_(storeId) @@ -114,7 +113,7 @@ Status RuntimeStore::Get(const std::string &key, UnifiedData &unifiedData) { UpdateTime(); std::vector entries; - if (GetEntries(key, entries) != E_OK) { + if (GetEntries(UnifiedKey(key).GetPropertyKey(), entries) != E_OK) { ZLOGE("GetEntries failed, dataPrefix: %{public}s.", key.c_str()); return E_DB_ERROR; } @@ -218,7 +217,7 @@ Status RuntimeStore::GetRuntime(const std::string &key, Runtime &runtime) Status RuntimeStore::Update(const UnifiedData &unifiedData) { std::string key = unifiedData.GetRuntime()->key.key; - if (Delete(key) != E_OK) { + if (Delete(UnifiedKey(key).GetPropertyKey()) != E_OK) { UpdateTime(); ZLOGE("Delete unified data failed, dataPrefix: %{public}s.", key.c_str()); return E_DB_ERROR; @@ -376,7 +375,8 @@ Status RuntimeStore::GetBatchData(const std::string &dataPrefix, std::vector keySet; for (const auto &entry : entries) { std::string keyStr = {entry.key.begin(), entry.key.end()}; - if (std::count(keyStr.begin(), keyStr.end(), '/') == SLASH_COUNT_IN_KEY) { + if (std::count(keyStr.begin(), keyStr.end(), '/') == SLASH_COUNT_IN_KEY && + std::count(keyStr.begin(), keyStr.end(), '#') == 0) { keySet.emplace_back(keyStr); } } diff --git a/services/distributeddataservice/service/udmf/store/runtime_store.h b/services/distributeddataservice/service/udmf/store/runtime_store.h index 9c6338eb53b8e9015d5a2fc8c0115e837c317d3c..eb83a3503cefebf22a1a7ce15f9f801508ff7228 100644 --- a/services/distributeddataservice/service/udmf/store/runtime_store.h +++ b/services/distributeddataservice/service/udmf/store/runtime_store.h @@ -59,6 +59,8 @@ private: Status GetEntries(const std::string &dataPrefix, std::vector &entries); Status PutEntries(const std::vector &entries); Status DeleteEntries(const std::vector &keys); + Status UnmarshalEntries( + const std::string &key, std::vector &entries, UnifiedData &unifiedData); bool BuildMetaDataParam(DistributedData::StoreMetaData &metaData); void NotifySyncProcss(const DevSyncProcessMap &processMap, ProcessCallback callback, const DevNameMap &deviceNameMap); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index ade32eb626020e0e59e841c8d09ae04f2f04d422..a8e59d48ee39e4c9512fed02df2ed79ab6595374 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -21,24 +21,29 @@ #include "tokenid_kit.h" #include "accesstoken_kit.h" +#include "bootstrap.h" +#include "bundle_info.h" +#include "bundlemgr/bundle_mgr_proxy.h" #include "checker_manager.h" +#include "device_manager_adapter.h" +#include "iservice_registry.h" #include "lifecycle/lifecycle_manager.h" #include "log_print.h" +#include "metadata/store_meta_data.h" +#include "metadata/meta_data_manager.h" #include "preprocess_utils.h" #include "reporter.h" +#include "store_account_observer.h" +#include "system_ability_definition.h" #include "uri_permission_manager.h" #include "udmf_radar_reporter.h" -#include "device_manager_adapter.h" -#include "store_account_observer.h" -#include "utils/anonymous.h" -#include "bootstrap.h" -#include "metadata/store_meta_data.h" -#include "metadata/meta_data_manager.h" #include "unified_data_helper.h" +#include "utils/anonymous.h" namespace OHOS { namespace UDMF { using namespace Security::AccessToken; +using namespace OHOS::DistributedHardware; using FeatureSystem = DistributedData::FeatureSystem; using UdmfBehaviourMsg = OHOS::DistributedDataDfx::UdmfBehaviourMsg; using Reporter = OHOS::DistributedDataDfx::Reporter; @@ -51,6 +56,9 @@ constexpr const char *DATA_PREFIX = "udmf://"; constexpr const char *FILE_SCHEME = "file"; constexpr const char *PRIVILEGE_READ_AND_KEEP = "readAndKeep"; constexpr const char *MANAGE_UDMF_APP_SHARE_OPTION = "ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION"; +constexpr const char *DEVICE_2IN1_TAG = "2in1"; +constexpr const char *DEVICE_PHONE_TAG = "phone"; +constexpr const char *DEVICE_DEFAULT_TAG = "default"; constexpr const char *HAP_LIST[] = {"com.ohos.pasteboarddialog"}; __attribute__((used)) UdmfServiceImpl::Factory UdmfServiceImpl::factory_; UdmfServiceImpl::Factory::Factory() @@ -168,6 +176,7 @@ int32_t UdmfServiceImpl::GetData(const QueryOption &query, UnifiedData &unifiedD msg.appId = bundleName; res = RetrieveData(query, unifiedData); } + TransferToEntriesIfNeed(query, unifiedData); auto errFind = ERROR_MAP.find(res); msg.result = errFind == ERROR_MAP.end() ? "E_ERROR" : errFind->second; for (const auto &record : unifiedData.GetRecords()) { @@ -181,11 +190,23 @@ int32_t UdmfServiceImpl::GetData(const QueryOption &query, UnifiedData &unifiedD return res; } -int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &unifiedData) +bool UdmfServiceImpl::CheckDragParams(UnifiedKey &key, const QueryOption &query) { - UnifiedKey key(query.key); if (!key.IsValid()) { ZLOGE("Unified key: %{public}s is invalid.", query.key.c_str()); + return false; + } + if (key.intention != UD_INTENTION_MAP.at(UD_INTENTION_DRAG)) { + ZLOGE("Invalid intention:%{public}s", key.intention.c_str()); + return false; + } + return true; +} + +int32_t UdmfServiceImpl::RetrieveData(const QueryOption &query, UnifiedData &unifiedData) +{ + UnifiedKey key(query.key); + if (!CheckDragParams(key, query)) { return E_INVALID_PARAMETERS; } auto store = StoreCache::GetInstance().GetStore(key.intention); @@ -428,7 +449,7 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vectortokenId == query.tokenId) { unifiedDataSet.push_back(data); - deleteKeys.push_back(runtime->key.key); + deleteKeys.push_back(UnifiedKey(runtime->key.key).GetPropertyKey()); } } if (deleteKeys.empty()) { @@ -688,15 +709,16 @@ int32_t UdmfServiceImpl::QueryDataCommon( { auto find = UD_INTENTION_MAP.find(query.intention); std::string intention = find == UD_INTENTION_MAP.end() ? intention : find->second; - if (!UnifiedDataUtils::IsValidOptions(query.key, intention)) { + UnifiedKey key(query.key); + if (!UnifiedDataUtils::IsValidOptions(key, intention, UD_INTENTION_MAP.at(UD_INTENTION_DATA_HUB))) { ZLOGE("Unified key: %{public}s and intention: %{public}s is invalid.", query.key.c_str(), intention.c_str()); return E_INVALID_PARAMETERS; } - std::string dataPrefix = DATA_PREFIX + intention; - UnifiedKey key(query.key); - key.IsValid(); - if (intention.empty()) { - dataPrefix = key.key; + std::string dataPrefix; + if (key.key.empty()) { + dataPrefix = DATA_PREFIX + intention; + } else { + dataPrefix = UnifiedKey(key.key).GetPropertyKey(); intention = key.intention; } ZLOGD("dataPrefix = %{public}s, intention: %{public}s.", dataPrefix.c_str(), intention.c_str()); @@ -827,5 +849,54 @@ int32_t UdmfServiceImpl::OnUserChange(uint32_t code, const std::string &user, co } return Feature::OnUserChange(code, user, account); } + +void UdmfServiceImpl::TransferToEntriesIfNeed(const QueryOption &query, UnifiedData &unifiedData) +{ + if (unifiedData.IsNeedTransferToEntries() && IsNeedTransferDeviceType(query)) { + unifiedData.ConvertRecordsToEntries(); + } +} + +bool UdmfServiceImpl::IsNeedTransferDeviceType(const QueryOption &query) +{ + auto deviceInfo = DmAdapter::GetInstance().GetLocalDevice(); + if (deviceInfo.deviceType != DEVICE_TYPE_PC && deviceInfo.deviceType != DEVICE_TYPE_PAD + && deviceInfo.deviceType != DEVICE_TYPE_2IN1) { + return false; + } + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return false; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA."); + return false; + } + auto bundleManager = iface_cast(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager"); + return false; + } + std::string bundleName; + PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); + int32_t userId = DistributedData::AccountDelegate::GetInstance()->GetUserByToken( + IPCSkeleton::GetCallingFullTokenID()); + AppExecFwk::BundleInfo bundleInfo; + bundleManager->GetBundleInfoV9(bundleName, static_cast( + AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo, userId); + for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) { + if (std::find(hapModuleInfo.deviceTypes.begin(), hapModuleInfo.deviceTypes.end(), + DEVICE_PHONE_TAG) == hapModuleInfo.deviceTypes.end() + && std::find(hapModuleInfo.deviceTypes.begin(), hapModuleInfo.deviceTypes.end(), + DEVICE_DEFAULT_TAG) == hapModuleInfo.deviceTypes.end() + && std::find(hapModuleInfo.deviceTypes.begin(), hapModuleInfo.deviceTypes.end(), + DEVICE_2IN1_TAG) != hapModuleInfo.deviceTypes.end()) { + return true; + } + } + return false; +} } // namespace UDMF } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 93082d45a19df6e4ad212444e817a7762708ae76..da95d6eb4dc8abacaed2fefe39abd2c7395f8d70 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -58,6 +58,9 @@ private: bool VerifyPermission(const std::string &permission, uint32_t callerTokenId); bool HasDatahubPriviledge(const std::string &bundleName); void RegisterAsyncProcessInfo(const std::string &businessUdKey); + void TransferToEntriesIfNeed(const QueryOption &query, UnifiedData &unifiedData); + bool IsNeedTransferDeviceType(const QueryOption &query); + bool CheckDragParams(UnifiedKey &key, const QueryOption &query); class Factory { public: diff --git a/services/distributeddataservice/service/udmf/udmf_service_stub.h b/services/distributeddataservice/service/udmf/udmf_service_stub.h index 793a13d997e4677012e6d3166da5b2760c2cbca5..9afabddceb7111add6d8d9d5f3584c1257516ad4 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); using Handler = int32_t (UdmfServiceStub::*)(MessageParcel &data, MessageParcel &reply); static constexpr Handler HANDLERS[static_cast(UdmfServiceInterfaceCode::CODE_BUTT)] = {