From da142fba487266fbfdb57286f217faceba76a2b9 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 09:58:04 +0800 Subject: [PATCH 1/7] uri transfer Signed-off-by: zhangyaomaggie --- .../include/remote_file_share.h | 4 + .../src/remote_file_share.cpp | 52 +++++++++ .../remote_file_share_test.cpp | 107 ++++++++++++++++++ 3 files changed, 163 insertions(+) diff --git a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h index c151f991b..0743e6000 100644 --- a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h +++ b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h @@ -41,6 +41,10 @@ public: static int32_t GetDfsUrisFromLocal(const std::vector &uriList, const int32_t &userId, std::unordered_map &uriToDfsUriMaps); + static int32_t TransRemoteUriToLocal(const std::vector &uriList, + const std::string &networkId, + const std::string &deviceId, + std::vector &resultList) ~RemoteFileShare() {} }; } // namespace ModuleRemoteFileShare diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index dc97b33fd..7deeca2f9 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -58,6 +58,8 @@ namespace { const std::string PACKAGE_NAME = "get_dfs_uri_from_local"; const std::string NETWORK_PARA = "?networkid="; const std::string MEDIA_BUNDLE_NAME = "com.ohos.medialibrary.medialibrarydata"; + const std::string FILE_MANAGER_URI_HEAD = "/storage/"; + const std::string REMOTE_SHARE_PATH_MID = "hmdfs/"; } #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) @@ -456,6 +458,56 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri LOGD("GetDfsUriFromLocal successfully"); return 0; } + +int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &uriList, + const std::string &networkId, + const std::string &deviceId, + std::vector &resultList) +{ + LOGI("TransRemoteUriToLocal begin"); + constexpr int splitThree = 3; + bool allValid = true; + std::vector tmpResultList; + for (auto &uriStr : uriList) { + Uri uri(uriStr); + std::string bundleName = uri.GetAuthority(); + std::string sandboxPath = SandboxHelper::Decode(uri.GetPath()); + if (!SandboxHelper::IsValidPath(sandboxPath) || uri.GetScheme() != FILE_SCHEME) { + LOGE("Sandbox path from uri is error"); + allValid = false; + break; + } + if ((bundleName != FILE_MANAGER_AUTHORITY) || (sandboxPath.find(FILE_MANAGER_URI_HEAD) != 0)) { + LOGE("Sandbox path doesn't begin with docs/storage"); + allValid = false; + break; + } + int cnt = 0; + size_t pos = 0; + std::string part; + while (cnt < splitThree && pos != std::string::npos) { + pos = sandboxPath.find('/', pos + 1); + cnt++; + } + if (pos != std::string::npos) { + part = sandboxPath.substr(pos + 1); + } + if (part.empty()) { + allValid = false; + break; + } + std::string localUri = FILE_SCHEME + "://" + bundleName + FILE_MANAGER_URI_HEAD + + REMOTE_SHARE_PATH_MID + deviceId + "/" + part; + tmpResultList.push_back(localUri); + } + if (!allValid) { + LOGW("Failed to update uriList"); + resultList = uriList; + return -EINVAL; + } + resultList = tmpResultList; + return 0; +} } // namespace ModuleRemoteFileShare } // namespace AppFileService } // namespace OHOS diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp index 2e338abfa..6680b8c12 100644 --- a/test/unittest/remote_file_share/remote_file_share_test.cpp +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -395,4 +395,111 @@ namespace { GTEST_LOG_(INFO) << "RemoteFileShareTest file size is " << hui.fileSize; GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDfsUriFromLocal_0011"; } + + /** + * @tc.name: remote_file_share_test_0012 + * @tc.desc: Test function of TransRemoteUriToLocal() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7KDF7 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0012, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0012"; + const vector uriList = {"file://docs/storage/Users/currentUser/Document/1.txt", + "file://docs/storage/Users/currentUser/Download/Subject/2.jpg", + "file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt", + "file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"}; + const vector expectedList = {"file://docs/storage/hmdfs/001/Document/1.txt", + "file://docs/storage/hmdfs/001/Download/Subject/2.jpg", + "file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt", + "file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt"}; + const string networkId = "100"; + const string deviceId = "001"; + vector resultList; + int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList); + EXPECT_EQ(ret, E_OK); + EXPECT_EQ(resultList, expectedList); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0012"; + } + + /** + * @tc.name: remote_file_share_test_0013 + * @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE. + * the sandboxPath of uri does not equal to "storage" + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7KDF7 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0013, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0013"; + const vector uriList = {"file://docs/storage/Users/currentUser/Document/1.txt", + "file://docs/hmdfs/Users/currentUser/Download/Subject/2.jpg", + "file://docs/tmp/Users/currentUser/Document/Subject1/Subject2/1.txt", + "file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"}; + const string networkId = "100"; + const string deviceId = "001"; + vector resultList; + int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList); + EXPECT_NE(ret, E_OK); + EXPECT_EQ(resultList, uriList); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0013"; + } + + /** + * @tc.name: remote_file_share_test_0014 + * @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE. + * the bundlename of uri does not equal to "docs" + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7KDF7 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0014, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0014"; + const vector uriList = {"file://docs/storage/Users/currentUser/Document/1.txt", + "file://doc/storage/Users/currentUser/Download/Subject/2.jpg", + "file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt", + "file://doc/storage/100/account/Document/Subject1/Subject2/1.txt"}; + const string networkId = "100"; + const string deviceId = "001"; + vector resultList; + int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList); + EXPECT_NE(ret, E_OK); + EXPECT_EQ(resultList, uriList); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0014"; + } + + /** + * @tc.name: remote_file_share_test_0015 + * @tc.desc: Test function of TransRemoteUriToLocal() interface for FAILURE. + * the scheme of uri does not equal to "file" + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7KDF7 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_TransRemoteUriToLocal_0015, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_TransRemoteUriToLocal_0015"; + const vector uriList = {"FILE://docs/storage/Users/currentUser/Document/1.txt", + "file://docs/storage/Users/currentUser/Download/Subject/2.jpg", + "file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt", + "file://docs/storage/100/account/Document/Subject1/Subject2/1.txt"}; + const string networkId = "100"; + const string deviceId = "001"; + vector resultList; + int ret = RemoteFileShare::TransRemoteUriToLocal(uriList, networkId, deviceId, resultList); + EXPECT_NE(ret, E_OK); + EXPECT_EQ(resultList, uriList); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_TransRemoteUriToLocal_0015"; + } } -- Gitee From 54f8b675ba29a3de017dc20f6bcaffb43af75924 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 10:13:40 +0800 Subject: [PATCH 2/7] uri transfer Signed-off-by: zhangyaomaggie --- .../native/remote_file_share/include/remote_file_share.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h index 0743e6000..652273fbf 100644 --- a/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h +++ b/interfaces/innerkits/native/remote_file_share/include/remote_file_share.h @@ -44,7 +44,7 @@ public: static int32_t TransRemoteUriToLocal(const std::vector &uriList, const std::string &networkId, const std::string &deviceId, - std::vector &resultList) + std::vector &resultList); ~RemoteFileShare() {} }; } // namespace ModuleRemoteFileShare -- Gitee From e4e585cb7a9c9217b3f06fae134b4feaae632cd1 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 10:15:02 +0800 Subject: [PATCH 3/7] uri transfer Signed-off-by: zhangyaomaggie --- test/unittest/remote_file_share/remote_file_share_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/remote_file_share/remote_file_share_test.cpp b/test/unittest/remote_file_share/remote_file_share_test.cpp index 6680b8c12..5559074ba 100644 --- a/test/unittest/remote_file_share/remote_file_share_test.cpp +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -414,7 +414,7 @@ namespace { const vector expectedList = {"file://docs/storage/hmdfs/001/Document/1.txt", "file://docs/storage/hmdfs/001/Download/Subject/2.jpg", "file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt", - "file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt"}; + "file://docs/storage/hmdfs/001/Document/Subject1/Subject2/1.txt"}; const string networkId = "100"; const string deviceId = "001"; vector resultList; -- Gitee From 31a0d77c96826ae4128c6ef50db683121ed63fd6 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 11:36:31 +0800 Subject: [PATCH 4/7] uri transfer Signed-off-by: zhangyaomaggie --- .../native/remote_file_share/src/remote_file_share.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 7deeca2f9..6cf071d58 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -507,7 +507,7 @@ int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &u } resultList = tmpResultList; return 0; -} +} } // namespace ModuleRemoteFileShare } // namespace AppFileService } // namespace OHOS -- Gitee From c1a19185049824779ac11d6b942352274e77d33b Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 12:06:52 +0800 Subject: [PATCH 5/7] uri transfer for parameter check Signed-off-by: zhangyaomaggie --- .../native/remote_file_share/src/remote_file_share.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 6cf071d58..7d05b364e 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -464,7 +464,10 @@ int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &u const std::string &deviceId, std::vector &resultList) { - LOGI("TransRemoteUriToLocal begin"); + if (networkId.empty() || deviceId.empty() || deviceId.size() != HMDFS_CID_SIZE) { + LOGE("RemoteFileShare::CreateSharePath, invalid argument with %{public}d", EINVAL); + return EINVAL; + } constexpr int splitThree = 3; bool allValid = true; std::vector tmpResultList; -- Gitee From 6413e8d9cb21fbba73208a60d1d4ad963fd79cdf Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 12:08:11 +0800 Subject: [PATCH 6/7] uri transfer for parameter check Signed-off-by: zhangyaomaggie --- .../native/remote_file_share/src/remote_file_share.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 7d05b364e..f93b42d94 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -465,7 +465,7 @@ int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &u std::vector &resultList) { if (networkId.empty() || deviceId.empty() || deviceId.size() != HMDFS_CID_SIZE) { - LOGE("RemoteFileShare::CreateSharePath, invalid argument with %{public}d", EINVAL); + LOGE("RemoteFileShare::TransRemoteUriToLocal, invalid argument with %{public}d", EINVAL); return EINVAL; } constexpr int splitThree = 3; -- Gitee From f8811821ed433496d5a46d706b18a75693f5e3ea Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Wed, 10 Jul 2024 15:22:56 +0800 Subject: [PATCH 7/7] uri transfer for parameter check Signed-off-by: zhangyaomaggie --- .../native/remote_file_share/src/remote_file_share.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index f93b42d94..d0fce4762 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -464,7 +464,7 @@ int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &u const std::string &deviceId, std::vector &resultList) { - if (networkId.empty() || deviceId.empty() || deviceId.size() != HMDFS_CID_SIZE) { + if (networkId.empty() || deviceId.empty()) { LOGE("RemoteFileShare::TransRemoteUriToLocal, invalid argument with %{public}d", EINVAL); return EINVAL; } -- Gitee