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 c151f991bcad8d60099fc3dcac8981bc76df2432..652273fbfb21e187fbd8ba745668f5061c82f325 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 dc97b33fd39f3f22c342f3ee7f8e8d2b70865e3c..d0fce4762e56833281fded705d3c0c5d60a895d3 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,59 @@ 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) +{ + if (networkId.empty() || deviceId.empty()) { + LOGE("RemoteFileShare::TransRemoteUriToLocal, invalid argument with %{public}d", EINVAL); + return EINVAL; + } + 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 2e338abfacf5d7d3829f0781a02296c5cd11dd38..5559074bae1035d63a2b7826adbf6a67ec37029f 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"; + } }