diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index 64671d029774ff60a04bc9309a7bdcf537ea2417..cf2d1c4af8580bb4edb421591eef6ac67ff924b5 100644 --- a/interfaces/common/include/sandbox_helper.h +++ b/interfaces/common/include/sandbox_helper.h @@ -37,10 +37,13 @@ public: static int32_t GetMediaSharePath(const std::vector &fileUris, std::vector &physicalPaths); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); + static int32_t GetPhysicalDir(const std::string &fileUri, const std::string &userId, std::string &physicalDir); static int32_t GetBackupPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); static bool IsValidPath(const std::string &path); static void GetNetworkIdFromUri(const std::string &fileUri, std::string &networkId); + static std::string GetLowerDir(std::string &lowerPathHead, const std::string &userId, const std::string &bundleName, + const std::string &networkId); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index 854ddcab08fc8641a05062c06367407563bad465..b4da9470a2a895899484226657e5f8a4819beeaf 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -135,6 +135,26 @@ static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, return lowerPathHead + lowerPathTail; } +string SandboxHelper::GetLowerDir(string &lowerPathHead, const string &userId, const string &bundleName, + const string &networkId) +{ + if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), + CURRENT_USER_ID_FLAG.length(), userId); + } + + if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), + PACKAGE_NAME_FLAG.length(), bundleName); + } + + if (lowerPathHead.find(NETWORK_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(NETWORK_ID_FLAG), + NETWORK_ID_FLAG.length(), networkId); + } + return lowerPathHead; +} + bool SandboxHelper::GetSandboxPathMap() { lock_guard lock(mapMutex_); @@ -372,6 +392,47 @@ static void DoGetPhysicalPath(string &lowerPathTail, string &lowerPathHead, cons } } +int32_t SandboxHelper::GetPhysicalDir(const std::string &fileUri, const std::string &userId, + std::string &physicalDir) +{ + if (!IsValidPath(fileUri)) { + LOGE("fileUri is ValidUri, The fileUri contains '/./' or '../'characters"); + return -EINVAL; + } + Uri uri(fileUri); + string bundleName = uri.GetAuthority(); + if (bundleName == MEDIA) { + LOGE("Media not support."); + return -EINVAL; + } + + string sandboxPath = SandboxHelper::Decode(uri.GetPath()); + if ((sandboxPath.find(FILE_MANAGER_URI_HEAD) == 0 && bundleName != FILE_MANAGER_AUTHORITY) || + (sandboxPath.find(FUSE_URI_HEAD) == 0 && bundleName != DLP_MANAGER_BUNDLE_NAME)) { + return -EINVAL; + } + + if (!GetSandboxPathMap()) { + LOGE("GetSandboxPathMap failed"); + return -EINVAL; + } + + string lowerPathTail = ""; + string lowerPathHead = ""; + DoGetPhysicalPath(lowerPathTail, lowerPathHead, sandboxPath, sandboxPathMap_); + + if (lowerPathHead == "") { + LOGE("lowerPathHead is invalid"); + return -EINVAL; + } + + string networkId = LOCAL; + GetNetworkIdFromUri(fileUri, networkId); + + physicalDir = GetLowerDir(lowerPathHead, userId, bundleName, networkId); + return 0; +} + int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath) { diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 08a8d8cb39a01936346b560da3816dfcab3576f2..1af139dcbb43aa02ed28c36fb27f68cb932631f1 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -164,6 +164,9 @@ ohos_shared_library("remote_file_share_native") { "hilog:libhilog", "json:nlohmann_json_static", "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "storage_service:storage_manager_sa_proxy", + "ipc:ipc_core", ] if (defined(global_parts_info) && diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 36491aafb0d985b09c61268fdbbd5f5ea46f9458..d9b3fd12dcb0e3ae4e3b89b146c2059b11b26623 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -53,6 +53,12 @@ const string EXTERNAL_PATH = "file://docs/storage/External"; const string NETWORK_PARA = "networkid="; const int32_t DLP_COMMON = 0; const std::string BACKSLASH = "/"; +const string DOCS_TYPE = "docs"; +const string FILE_DEFAULT_PATH = "/storage/Users/currentUser/"; +const string FILE_PATH_TAIL = "/files/Docs"; +const string FILE_PATH_HEAD = "/mnt/hmdfs/"; +const string FILE_PATH_MID = "/account/device_view/"; +const string FILE_MANAGER_BUNDLE_NAME = "hmos.filemanager"; } struct FileShareInfo { @@ -95,6 +101,9 @@ static void GetProviderInfo(string uriStr, FileShareInfo &info) Uri uri(uriStr); info.providerBundleName_ = uri.GetAuthority(); info.providerSandboxPath_ = SandboxHelper::Decode(uri.GetPath()); + if (info.providerBundleName_ == DOCS_TYPE && info.targetBundleName_.find(FILE_MANAGER_BUNDLE_NAME) == 0) { + info.providerSandboxPath_ = FILE_DEFAULT_PATH; + } } static bool IsExistDir(const string &path, FileShareInfo &info) @@ -208,6 +217,32 @@ static int32_t GetShareFileType(FileShareInfo &info) return -ENOENT; } +static int32_t GetDocsDir(const string &uri, FileShareInfo &info) +{ + string networkId = ""; + SandboxHelper::GetNetworkIdFromUri(uri, networkId); + if (networkId == "") { + LOGE("get network id failed."); + return -EINVAL; + } + + const string filePathPrefix = FILE_PATH_HEAD + info.currentUid_ + FILE_PATH_MID + networkId + FILE_PATH_TAIL; + string tmpPath = info.providerLowerPath_; + string::size_type prefixMatchLen = filePathPrefix.length(); + if (tmpPath.length() < prefixMatchLen) { + LOGE("get docs dir failed"); + return -EINVAL; + } + + string filePathTemp = tmpPath.substr(0, prefixMatchLen); + if (filePathTemp == filePathPrefix) { + info.providerLowerPath_ = filePathTemp; + LOGI("docs dir: %{private}s", info.providerLowerPath_.c_str()); + return 0; + } + return -EINVAL; +} + static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t flag, FileShareInfo &info) { int32_t ret = 0; @@ -219,6 +254,14 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl return ret; } + if (info.providerBundleName_ == DOCS_TYPE && info.targetBundleName_.find(FILE_MANAGER_BUNDLE_NAME) == 0) { + ret = GetDocsDir(uri, info); + if (ret != 0) { + LOGE("Failed to get docs dir, errno: %{public}d", ret); + return ret; + } + } + ret = GetShareFileType(info); if (ret != 0) { LOGE("Failed to get share file type %{public}d", ret); 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 652273fbfb21e187fbd8ba745668f5061c82f325..e934ed6aa3968fc4b1191282c05291fc1a23d83f 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,8 @@ public: static int32_t GetDfsUrisFromLocal(const std::vector &uriList, const int32_t &userId, std::unordered_map &uriToDfsUriMaps); + static int32_t GetDfsUrisDirFromLocal(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, 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 28dfc1a78ba4be6018730d38452dafac47bf3c54..14095f08aca26ad4c087bc533169eff3e1c30daf 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 @@ -28,8 +28,11 @@ #include "device_manager.h" #include "device_manager_callback.h" #endif +#include "iservice_registry.h" +#include "istorage_manager.h" #include "sandbox_helper.h" #include "securec.h" +#include "system_ability_definition.h" #include "unique_fd.h" #include "uri.h" @@ -49,8 +52,8 @@ const std::string BACKSLASH = "/"; const std::string DISTRIBUTED_DIR_PATH = "/data/storage/el2/distributedfiles"; const std::string DST_PATH_HEAD = "/data/service/el2/"; const std::string DST_PATH_MID = "/hmdfs/account/data/"; -const std::string SHAER_PATH_HEAD = "/mnt/hmdfs/"; -const std::string SHAER_PATH_MID = "/account/merge_view/services/"; +const std::string SHARE_PATH_HEAD = "/mnt/hmdfs/"; +const std::string SHARE_PATH_MID = "/account/merge_view/services/"; const std::string LOWER_SHARE_PATH_HEAD = "/mnt/hmdfs/"; const std::string LOWER_SHARE_PATH_MID = "/account/device_view/local/services/"; const std::string SHARE_PATH_DIR = "/.share"; @@ -62,6 +65,13 @@ 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/"; +const std::string SANDBOX_BASE_DIR = "/data/storage/el2/base"; +const std::string SANDBOX_CLOUD_DIR = "/data/storage/el2/cloud"; +const std::string MEDIA_PHOTO_PATH = "/Photo"; +const std::string MEDIA_PATH_PREFIX_ONE = "/account/cloud_merge_view/files"; +const std::string MEDIA_PATH_PREFIX_TWO = "/account/merge_view/files"; +constexpr int32_t GET_CLIENT_RETRY_TIMES = 5; +constexpr int32_t SLEEP_TIME = 1; } //namespace #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct HmdfsShareControl) @@ -145,7 +155,7 @@ static int CreateShareDir(const std::string &path) static std::string GetSharePath(const int &userId, const std::string &packageName) { - return SHAER_PATH_HEAD + std::to_string(userId) + SHAER_PATH_MID + packageName; + return SHARE_PATH_HEAD + std::to_string(userId) + SHARE_PATH_MID + packageName; } static std::string GetLowerSharePath(const int &userId, const std::string &packageName) @@ -283,6 +293,76 @@ static int GetDistributedPath(Uri &uri, return E_OK; } +static int GetMediaDistributedDir(const int &userId, std::string &distributedDir, const std::string &networkId) +{ + distributedDir = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + MEDIA_BUNDLE_NAME + + REMOTE_SHARE_PATH_DIR + BACKSLASH + networkId + MEDIA_PHOTO_PATH; + if (distributedDir.size() >= PATH_MAX) { + LOGE("Path is too long with %{public}zu", distributedDir.size()); + return -EINVAL; + } + LOGI("media distributedDir: %{private}s", distributedDir.c_str()); + return E_OK; +} + +static int GetDistributedDir(const int &userId, std::string &distributedDir, const std::string &bundleName, + const std::string &networkId, Uri uri) +{ + std::string sandboxPath = SandboxHelper::Decode(uri.GetPath()); + std::string sandboxDir = ""; + if (sandboxPath.rfind(DISTRIBUTED_DIR_PATH, 0) == 0) { + sandboxDir = DISTRIBUTED_DIR_PATH; + } else if (sandboxPath.rfind(SANDBOX_BASE_DIR, 0) == 0) { + sandboxDir = SANDBOX_BASE_DIR; + } else if (sandboxPath.rfind(SANDBOX_CLOUD_DIR, 0) == 0) { + sandboxDir = SANDBOX_CLOUD_DIR; + } else { + LOGE("Get distributed dir failed."); + return -EINVAL; + } + + distributedDir = DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + bundleName + REMOTE_SHARE_PATH_DIR + + BACKSLASH + networkId + sandboxDir; + if (distributedDir.size() >= PATH_MAX) { + LOGE("Path is too long with %{public}zu", distributedDir.size()); + return -EINVAL; + } + LOGI("bundleName: %{public}s, distributedDir: %{private}s", bundleName.c_str(), distributedDir.c_str()); + return E_OK; +} + +static int32_t GetMediaPhysicalDir(const int32_t &userId, std::string &physicalDir, const std::string &bundleName) +{ + if (bundleName != MEDIA_BUNDLE_NAME) { + LOGE("bundleName is not media type."); + return -EINVAL; + } + physicalDir = SHARE_PATH_HEAD + std::to_string(userId) + MEDIA_PATH_PREFIX_TWO + MEDIA_PHOTO_PATH; + if (physicalDir.size() >= PATH_MAX) { + LOGE("Path is too long with %{public}zu", physicalDir.size()); + return -EINVAL; + } + LOGI("bundleName: %{public}s, physicalDir: %{private}s", bundleName.c_str(), physicalDir.c_str()); + return E_OK; +} + +static std::string GetPhysicalDir(Uri &uri, const int32_t &userId) +{ + std::string sandboxPath = SandboxHelper::Decode(uri.GetPath()); + if (!SandboxHelper::IsValidPath(sandboxPath) || uri.GetScheme() != FILE_SCHEME) { + LOGE("Sandbox path from uri is error"); + return ""; + } + + std::string physicalDir = ""; + int ret = SandboxHelper::GetPhysicalDir(uri.ToString(), std::to_string(userId), physicalDir); + if (ret != E_OK) { + LOGE("Get physical path failed with %{public}d", ret); + return ""; + } + return physicalDir; +} + static std::string GetPhysicalPath(Uri &uri, const std::string &userId) { std::string sandboxPath = SandboxHelper::Decode(uri.GetPath()); @@ -368,7 +448,7 @@ static int32_t SetPublicDirHmdfsInfo(const std::string &physicalPath, const std: static int32_t GetMergePathFd(HmdfsDstInfo &hdi, UniqueFd &dirFd, const int32_t &userId) { LOGI("Open merge path start"); - std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + SHAER_PATH_MID; + std::string ioctlDir = SHARE_PATH_HEAD + std::to_string(userId) + SHARE_PATH_MID; UniqueFd dirMergeFd(open(ioctlDir.c_str(), O_RDONLY)); if (dirFd < 0) { LOGE("Open merge path failed with %{public}d", errno); @@ -413,7 +493,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int struct HmdfsDstInfo hdi; InitHmdfsInfo(hdi, physicalPath, distributedPath, bundleName); LOGI("open ioctlDir Create ioctl start"); - std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; + std::string ioctlDir = SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; UniqueFd dirFd(open(ioctlDir.c_str(), O_RDONLY)); if (dirFd < 0) { LOGE("Open share path failed with %{public}d", errno); @@ -455,7 +535,7 @@ static int32_t GetMediaDfsUrisFromLocal(const std::vector &uriList, std::unordered_map &uriToDfsUriMaps) { LOGI("GetMediaDfsUrisFromLocal start"); - std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; + std::string ioctlDir = SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; UniqueFd dirFd(open(ioctlDir.c_str(), O_RDONLY)); if (dirFd < 0) { LOGE("Open share path failed with %{public}d", errno); @@ -503,7 +583,7 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri if (ret != E_OK) { return ret; } - std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; + std::string ioctlDir = SHARE_PATH_HEAD + std::to_string(userId) + LOWER_SHARE_PATH_MID; UniqueFd dirFd(open(ioctlDir.c_str(), O_RDONLY)); if (dirFd < 0) { LOGE("Open share path failed with %{public}d", errno); @@ -545,6 +625,235 @@ int32_t RemoteFileShare::GetDfsUrisFromLocal(const std::vector &uri return E_OK; } +static sptr GetStorageManager() +{ + auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + LOGI("Get samgr failed."); + return nullptr; + } + + int32_t count = 0; + sptr storageManager = nullptr; + while (storageManager == nullptr && count++ < GET_CLIENT_RETRY_TIMES) { + auto storageObj = saMgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID); + if (storageObj == nullptr) { + LOGE("Get starage manger failed."); + sleep(SLEEP_TIME); + continue; + } + + storageManager = iface_cast(storageObj); + if (storageManager == nullptr) { + LOGE("Iface_cast failed."); + sleep(SLEEP_TIME); + continue; + } + } + return storageManager; +} + +static int32_t MountDisFileShare(const int32_t &userId, const std::string &distributedDir, + const std::string &physicalDir) +{ + if (distributedDir == "" || physicalDir == "") { + LOGE("Invalid distributedDir or physicalDir."); + return -EINVAL; + } + auto storageMgr = GetStorageManager(); + if (storageMgr == nullptr) { + LOGE("Get storage manager failed."); + return -EINVAL; + } + + std::map shareFiles = { + {distributedDir, physicalDir} + }; + int32_t ret = storageMgr->MountDisShareFile(userId, shareFiles); + if (ret != E_OK) { + LOGE("MountDisShareFile failed."); + return ret; + } + return E_OK; +} + +static std::string GetMediaSandboxPath(const std::string &physicalPath, const std::string &usrId) +{ + const std::vector prefixList = { + SHARE_PATH_HEAD + usrId + MEDIA_PATH_PREFIX_ONE, + SHARE_PATH_HEAD + usrId + MEDIA_PATH_PREFIX_TWO + }; + std::string mediaSandboxPath = ""; + for (size_t i = 0; i < prefixList.size(); i++) { + std::string sandboxPathPrefix = prefixList[i]; + std::string::size_type prefixMatchLen = sandboxPathPrefix.length(); + if (physicalPath.length() >= prefixMatchLen) { + std::string sandboxPathTemp = physicalPath.substr(0, prefixMatchLen); + if (sandboxPathTemp == sandboxPathPrefix) { + mediaSandboxPath = physicalPath.substr(prefixMatchLen); + LOGI("mediaSandboxPath: %{private}s", mediaSandboxPath.c_str()); + return mediaSandboxPath; + } + } + } + return mediaSandboxPath; +} + +static int32_t SetHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const std::string &physicalPath, + const std::string &networkId, const std::string &bundleName) +{ + hui.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + BACKSLASH + + networkId + uri.GetPath() + NETWORK_PARA + networkId; + struct stat buf = {}; + if (stat(physicalPath.c_str(), &buf) != E_OK) { + int32_t ret = -errno; + LOGE("Failed to get physical path stat with %{public}d", ret); + return ret; + } + hui.fileSize = static_cast(buf.st_size); + return E_OK; +} + +static int32_t SetMediaHmdfsUriDirInfo(struct HmdfsUriInfo &hui, Uri &uri, const std::string &physicalPath, + const std::string &networkId, const std::string &usrId) +{ + std::string authority = uri.GetAuthority(); + if (authority != MEDIA_AUTHORITY) { + LOGE("not meida authority."); + return -EINVAL; + } + std::string mediaSandboxPath = GetMediaSandboxPath(physicalPath, usrId); + if (mediaSandboxPath == "") { + LOGE("mediaSandboxPath is null."); + return -EINVAL; + } + hui.uriStr = FILE_SCHEME + "://" + MEDIA_BUNDLE_NAME + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + BACKSLASH + + networkId + mediaSandboxPath + NETWORK_PARA + networkId; + + struct stat buf = {}; + if (stat(physicalPath.c_str(), &buf) != E_OK) { + LOGE("Failed to get physical path stat with %{public}d", -errno); + return -errno; + } + hui.fileSize = static_cast(buf.st_size); + return E_OK; +} + + +static int32_t GetMediaDfsUrisDirFromLocal(const std::vector &uriList, const int32_t &userId, + std::unordered_map &uriToDfsUriMaps) +{ + if (uriList.size() <= 0) { + LOGE("Failed to uri GetMediaDfsDirFromLocal, list is null or empty"); + return -EINVAL; + } + + std::string networkId = GetLocalNetworkId(); + std::string distributedDir; + int ret = GetMediaDistributedDir(userId, distributedDir, networkId); + if (ret != E_OK) { + LOGE("Failed to uri get distribute dir."); + return ret; + } + + std::string physicalDir = ""; + ret = GetMediaPhysicalDir(userId, physicalDir, MEDIA_BUNDLE_NAME); + if (ret != E_OK) { + LOGE("Failed to uri get physical dir."); + return ret; + } + + ret = MountDisFileShare(userId, distributedDir, physicalDir); + if (ret != E_OK) { + LOGE("Failed to mount."); + return ret; + } + + std::vector physicalPaths; + int getPhysicalPathRet = SandboxHelper::GetMediaSharePath(uriList, physicalPaths); + if (getPhysicalPathRet != E_OK) { + LOGE("Failed to get physical path."); + return -EINVAL; + } + for (size_t i = 0; i < uriList.size(); i++) { + Uri uri(uriList[i]); + HmdfsUriInfo dfsUriInfo; + SetMediaHmdfsUriDirInfo(dfsUriInfo, uri, physicalPaths[i], networkId, std::to_string(userId)); + uriToDfsUriMaps.insert({uriList[i], dfsUriInfo}); + LOGI("dfsUri: %{private}s", dfsUriInfo.uriStr.c_str()); + } + return E_OK; +} + +static int32_t CheckIfNeedMount(const int &userId, const std::string &bundleName, const std::string &networkId, Uri uri) +{ + std::string distributedDir; + int32_t ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + if (ret != E_OK) { + LOGE("Failed to get distributed dir."); + return ret; + } + + std::string physicalDir = GetPhysicalDir(uri, userId); + if (physicalDir == "") { + LOGE("Failed to get physicalDir dir."); + return -EINVAL; + } + + ret = MountDisFileShare(userId, distributedDir, physicalDir); + if (ret != E_OK) { + LOGE("Failed to mount, errno: %{public}d.", ret); + return ret; + } + return E_OK; +} + +int32_t RemoteFileShare::GetDfsUrisDirFromLocal(const std::vector &uriList, const int32_t &userId, + std::unordered_map &uriToDfsUriMaps) +{ + std::vector otherUriList; + std::vector mediaUriList; + int ret = UriCategoryByType(uriList, mediaUriList, otherUriList); + if (ret == E_OK && mediaUriList.size() != 0) { + ret = GetMediaDfsUrisDirFromLocal(mediaUriList, userId, uriToDfsUriMaps); + } + if (ret != E_OK) { + LOGE("uri category failed or process media uri failed."); + return ret; + } + std::string networkId = GetLocalNetworkId(); + for (const auto &uriStr : otherUriList) { + Uri uri(uriStr); + std::string physicalPath = GetPhysicalPath(uri, std::to_string(userId)); + if (physicalPath == "") { + LOGE("Failed to get physical path"); + return -EINVAL; + } + + std::string bundleName = uri.GetAuthority(); + if (bundleName == FILE_MANAGER_AUTHORITY) { + HmdfsUriInfo dfsUriInfo; + (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, dfsUriInfo, networkId); + uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); + LOGI("localUri: %{private}s, dfsUri: %{private}s", uriStr.c_str(), dfsUriInfo.uriStr.c_str()); + continue; + } + + ret = CheckIfNeedMount(userId, bundleName, networkId, uri); + if (ret != E_OK) { + LOGE("Failed to mount, %{public}d", ret); + return ret; + } + + HmdfsUriInfo dfsUriInfo; + (void)SetHmdfsUriDirInfo(dfsUriInfo, uri, physicalPath, networkId, bundleName); + uriToDfsUriMaps.insert({uriStr, dfsUriInfo}); + LOGI("bundleName: %{public}s, dfsUri: %{private}s", bundleName.c_str(), dfsUriInfo.uriStr.c_str()); + } + LOGI("GetDfsUrisDirFromLocal successfully"); + return E_OK; +} + int32_t RemoteFileShare::TransRemoteUriToLocal(const std::vector &uriList, const std::string &networkId, const std::string &deviceId, diff --git a/test/unittest/file_share_native/BUILD.gn b/test/unittest/file_share_native/BUILD.gn index 74c2f5f554e5368f2502c9df606234a5da693aa3..c7911bdf37a391e0570250d292105dd7030de2be 100644 --- a/test/unittest/file_share_native/BUILD.gn +++ b/test/unittest/file_share_native/BUILD.gn @@ -28,6 +28,14 @@ ohos_unittest("file_share_test") { "${app_file_service_path}/test/unittest/resource/ohos_test.xml" sources = [ "file_share_test.cpp" ] + include_dirs = [ + "${app_file_service_path}/interfaces/innerkits/native/file_share/include", + "${app_file_service_path}/interfaces/innerkits/native/file_share/src", + ] + + deps = + [ "${app_file_service_path}/interfaces/innerkits/native:fileuri_native" ] + external_deps = [ "ability_base:base", "ability_base:want", diff --git a/test/unittest/file_share_native/file_share_test.cpp b/test/unittest/file_share_native/file_share_test.cpp index 2b26573d008a67d1b17dd2ff1a2e8cfd9d3381a9..8b05e4b545de21a893c573963beb57854dae60ba 100644 --- a/test/unittest/file_share_native/file_share_test.cpp +++ b/test/unittest/file_share_native/file_share_test.cpp @@ -25,6 +25,7 @@ #include "access_token_error.h" #include "accesstoken_kit.h" +#include "file_share.cpp" #include "file_share.h" #include "ipc_skeleton.h" #include "log.h" @@ -831,4 +832,136 @@ HWTEST_F(FileShareTest, File_share_GetNetworkIdFromUri_005, testing::ext::TestSi GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetNetworkIdFromUri_005"; } +/** + * @tc.name: File_share_GetDocsDir_001 + * @tc.desc: Test function of GetDocsDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetDocsDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetDocsDir_001"; + std::string uri = "file://docs/storage/Users/currentUser/test.jpg?networkid=123456"; + FileShareInfo info; + info.providerBundleName_ = "docs"; + info.targetBundleName_ = "com.demo.a"; + info.providerLowerPath_ = "/mnt/hmdfs/100/account/device_view/123456/files/docs/test.jpg"; + info.currentUid_ = "100"; + + int32_t ret = GetDocsDir(uri, info); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetDocsDir_001"; +} + +/** + * @tc.name: File_share_GetDocsDir_002 + * @tc.desc: Test function of GetDocsDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetDocsDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetDocsDir_002"; + std::string uri = "file://docs/storage/Users/currentUser/test.jpg"; + FileShareInfo info; + info.providerBundleName_ = "docs"; + info.targetBundleName_ = "com.demo.a"; + info.providerLowerPath_ = "/mnt/hmdfs/100/account/device_view/123456/files/docs/test.jpg"; + info.currentUid_ = "100"; + + int32_t ret = GetDocsDir(uri, info); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetDocsDir_002"; +} + +/** + * @tc.name: File_share_GetDocsDir_003 + * @tc.desc: Test function of GetDocsDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetDocsDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetDocsDir_003"; + std::string uri = "/mnt/hmdfs/100/account/device_view"; + FileShareInfo info; + info.providerBundleName_ = "docs"; + info.targetBundleName_ = "com.demo.a"; + info.providerLowerPath_ = "/mnt/hmdfs/100/account/device_view/123456/files/docs/test.jpg"; + info.currentUid_ = "100"; + + int32_t ret = GetDocsDir(uri, info); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetDocsDir_003"; +} + +/** + * @tc.name: File_share_GetLowerDir_001 + * @tc.desc: Test function of GetLowerDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetLowerDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetLowerDir_001"; + std::string lowerPathHead = "/data/app/el5//database//"; + std::string usrId = "100"; + std::string bundleName = "com.demo.a"; + std::string networkId = "123456"; + + std::string ret = SandboxHelper::GetLowerDir(lowerPathHead, usrId, bundleName, networkId); + EXPECT_EQ(ret, "/data/app/el5/100/database/com.demo.a/123456"); + + lowerPathHead = "/data/app/el5"; + ret = SandboxHelper::GetLowerDir(lowerPathHead, usrId, bundleName, networkId); + EXPECT_EQ(ret, lowerPathHead); + + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetLowerDir_001"; +} + +/** + * @tc.name: File_share_GetPhysicalDir_001 + * @tc.desc: Test function of GetPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7PDZL + */ +HWTEST_F(FileShareTest, File_share_GetPhysicalDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileShareTest-begin File_share_GetPhysicalDir_001"; + std::string uri = "file://com.demo.a/data/storage/el2/distributedfiles/remote_share.txt"; + std::string usrId = "100"; + std::string physicalDir; + + int32_t ret = SandboxHelper::GetPhysicalDir(uri, usrId, physicalDir); + EXPECT_EQ(ret, E_OK); + EXPECT_EQ(physicalDir, "/mnt/hmdfs/100/account/device_view/local/data/com.demo.a/"); + + uri = "file://com.demo.a/data/storage/el2/distributedfiles/../remote_share.txt"; + ret = SandboxHelper::GetPhysicalDir(uri, usrId, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + uri = "file://media/data/storage/el2/distributedfiles/remote_share.txt"; + ret = SandboxHelper::GetPhysicalDir(uri, usrId, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + uri = "file://com.demo.a/data/storage/el12/distributedfiles/remote_share.txt"; + ret = SandboxHelper::GetPhysicalDir(uri, usrId, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "FileShareTest-end File_share_GetPhysicalDir_001"; +} + } // namespace \ No newline at end of file diff --git a/test/unittest/remote_file_share/BUILD.gn b/test/unittest/remote_file_share/BUILD.gn index 2f7d8578f34bbada48bd81254a95c119f17a41cd..0350b895a1c4227595f770d6c674d0c819ad232d 100644 --- a/test/unittest/remote_file_share/BUILD.gn +++ b/test/unittest/remote_file_share/BUILD.gn @@ -49,5 +49,7 @@ ohos_unittest("remote_file_share_test") { "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", + "safwk:system_ability_fwk", + "storage_service:storage_manager_sa_proxy", ] } 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 5df49b3223a62c48e99469c81c842bd1e68c29ee..4d59e804bf3897ab2db44cd1312b68c67c1f4ed6 100644 --- a/test/unittest/remote_file_share/remote_file_share_test.cpp +++ b/test/unittest/remote_file_share/remote_file_share_test.cpp @@ -25,6 +25,7 @@ #include "remote_file_share.h" #include "remote_file_share.cpp" +#include "uri.h" namespace { using namespace std; @@ -33,6 +34,7 @@ namespace { const int E_INVALID_ARGUMENT = 22; const int E_OK = 0; const int USER_ID = 100; + const int32_t TEST_CHAR = 95; class RemoteFileShareTest : public testing::Test { public: @@ -776,5 +778,482 @@ namespace { string sharePath = "/data/filetest"; EXPECT_EQ(true, DeleteShareDir(packagePath, sharePath)); GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_DeleteShareDir_0000"; -} + } + + /** + * @tc.name: Remote_file_share_GetMediaDistributedDir_0001 + * @tc.desc: Test function of GetMediaDistributedDir() for ok + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaDistributedDir_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaDistributedDir_0001"; + int32_t userId = USER_ID; + string distributedDir; + string networkId = "network123"; + + int32_t ret = GetMediaDistributedDir(userId, distributedDir, networkId); + EXPECT_EQ(ret, E_OK); + EXPECT_EQ(distributedDir, DST_PATH_HEAD + std::to_string(userId) + DST_PATH_MID + MEDIA_BUNDLE_NAME + + REMOTE_SHARE_PATH_DIR + BACKSLASH + networkId + MEDIA_PHOTO_PATH); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaDistributedDir_0001"; + } + + /** + * @tc.name: Remote_file_share_GetMediaDistributedDir_0002 + * @tc.desc: Test function of GetMediaDistributedDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaDistributedDir_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaDistributedDir_0002"; + int32_t userId = USER_ID; + string distributedDir; + char networkId[PATH_MAX]; + int32_t ret = memset_s(networkId, sizeof(networkId), TEST_CHAR, sizeof(networkId)); + ASSERT_TRUE(ret == E_OK); + + string networkIdStr = networkId; + ret = GetMediaDistributedDir(userId, distributedDir, networkIdStr); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaDistributedDir_0002"; + } + + /** + * @tc.name: Remote_file_share_GetDistributedDir_0001 + * @tc.desc: Test function of GetDistributedDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetDistributedDir_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetDistributedDir_0001"; + int32_t userId = USER_ID; + string distributedDir; + string networkId = "network123"; + string bundleName = "com.demo.a"; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + + int32_t ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + EXPECT_EQ(ret, E_OK); + + char networkIdArray[PATH_MAX]; + ret = memset_s(networkIdArray, sizeof(networkIdArray), TEST_CHAR, sizeof(networkIdArray)); + ASSERT_TRUE(ret == E_OK); + + networkId = networkIdArray; + ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDistributedDir_0001"; + } + + /** + * @tc.name: Remote_file_share_GetDistributedDir_0002 + * @tc.desc: Test function of GetDistributedDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetDistributedDir_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetDistributedDir_0002"; + int32_t userId = USER_ID; + string distributedDir; + string networkId = "network123"; + string bundleName = "com.demo.a"; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/distributedfiles/remote_share.txt"); + + int32_t ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + EXPECT_EQ(ret, E_OK); + + char networkIdArray[PATH_MAX]; + ret = memset_s(networkIdArray, sizeof(networkIdArray), TEST_CHAR, sizeof(networkIdArray)); + ASSERT_TRUE(ret == E_OK); + + networkId = networkIdArray; + ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDistributedDir_0002"; + } + + /** + * @tc.name: Remote_file_share_GetDistributedDir_0003 + * @tc.desc: Test function of GetDistributedDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetDistributedDir_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetDistributedDir_0003"; + int32_t userId = USER_ID; + string distributedDir; + string networkId = "network123"; + string bundleName = "com.ohos.medialibrary.medialibrarydata"; + OHOS::Uri uri("file://media/Photo/16/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + + int32_t ret = GetDistributedDir(userId, distributedDir, bundleName, networkId, uri); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDistributedDir_0003"; + } + + /** + * @tc.name: Remote_file_share_GetMediaPhysicalDir_0001 + * @tc.desc: Test function of GetMediaPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaPhysicalDir_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaPhysicalDir_0001"; + int32_t userId = USER_ID; + string physicalDir; + string bundleName = "com.ohos.medialibrary.medialibrarydata"; + + int32_t ret = GetMediaPhysicalDir(userId, physicalDir, bundleName); + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaPhysicalDir_0001"; + } + + /** + * @tc.name: Remote_file_share_GetMediaPhysicalDir_0002 + * @tc.desc: Test function of GetMediaPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaPhysicalDir_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaPhysicalDir_0002"; + int32_t userId = USER_ID; + string physicalDir; + string bundleName = "com.demo.a"; + + int32_t ret = GetMediaPhysicalDir(userId, physicalDir, bundleName); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaPhysicalDir_0002"; + } + + /** + * @tc.name: Remote_file_share_GetPhysicalDir_0001 + * @tc.desc: Test function of GetPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetPhysicalDir_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetPhysicalDir_0001"; + OHOS::Uri uri("file://media/Photo/16/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + int32_t userId = USER_ID; + + string physicalDir = GetPhysicalDir(uri, userId); + int32_t ret = (physicalDir == "") ? -EINVAL : E_OK; + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetPhysicalDir_0001"; + } + + /** + * @tc.name: Remote_file_share_GetPhysicalDir_0002 + * @tc.desc: Test function of GetPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetPhysicalDir_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetPhysicalDir_0002"; + + const vector uriList = {"FILE://docs/storage/Users/currentUser/Document/1.txt", + "files://docs/storage/Users/currentUser/Download/Subject/2.jpg", + "file://com.demo.a/data/storage/el2/distributedfiles/..", + "file://docs/storage/100/account/Document/Subject1/../1.txt"}; + + int32_t userId = USER_ID; + for (size_t i = 0; i < uriList.size(); i++) { + OHOS::Uri uri(uriList[i]); + string physicalDir = GetPhysicalDir(uri, userId); + int32_t ret = (physicalDir == "") ? -EINVAL : E_OK; + EXPECT_EQ(ret, -EINVAL); + } + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetPhysicalDir_0002"; + } + + /** + * @tc.name: Remote_file_share_GetPhysicalDir_0003 + * @tc.desc: Test function of GetPhysicalDir() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetPhysicalDir_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetPhysicalDir_0003"; + + const vector uriList = { + "file://com.demo.a/data/storage/el2/base/remote_share.txt", + "file://com.demo.a/data/storage/el2/distributedfiles/remote_share.txt"}; + + int32_t userId = USER_ID; + for (size_t i = 0; i < uriList.size(); i++) { + OHOS::Uri uri(uriList[i]); + string physicalDir = GetPhysicalDir(uri, userId); + int32_t ret = (physicalDir == "") ? -EINVAL : E_OK; + EXPECT_EQ(ret, E_OK); + } + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetPhysicalDir_0003"; + } + + /** + * @tc.name: Remote_file_share_ModuleDisFileShare_0001 + * @tc.desc: Test function of ModuleDisFileShare() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_ModuleDisFileShare_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_ModuleDisFileShare_0001"; + + string distributedDir = ""; + string physicalDir = ""; + int32_t userId = USER_ID; + + int32_t ret = MountDisFileShare(userId, distributedDir, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + distributedDir = "/data/service/el2/100/hmdfs/account/data/com.demo.a/.remote_share/123456789"; + ret = MountDisFileShare(userId, distributedDir, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + physicalDir = "data/app/el2/100/base/com.demo.a"; + ret = MountDisFileShare(userId, distributedDir, physicalDir); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_ModuleDisFileShare_0001"; + } + + /** + * @tc.name: Remote_file_share_GetMediaSandboxPath_0001 + * @tc.desc: Test function of GetMediaSandboxPath() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaSandboxPath_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaSandboxPath_0001"; + + string physicalPath = "/mnt/hmdfs/100/account/cloud_merge_view/files/Photo/16/IMG_1747924738_000.jpg"; + string mediaSandboxPath = GetMediaSandboxPath(physicalPath, to_string(USER_ID)); + int32_t ret = (mediaSandboxPath == "/Photo/16/IMG_1747924738_000.jpg") ? E_OK : -EINVAL; + EXPECT_EQ(ret, E_OK); + + physicalPath = "/mnt/hmdfs/100/account/merge_view/files/Photo/16/IMG_1747924738_000.jpg"; + mediaSandboxPath = GetMediaSandboxPath(physicalPath, to_string(USER_ID)); + ret = (mediaSandboxPath == "/Photo/16/IMG_1747924738_000.jpg") ? E_OK : -EINVAL; + EXPECT_EQ(ret, E_OK); + + physicalPath = "/mnt/hmdfs/100/account"; + mediaSandboxPath = GetMediaSandboxPath(physicalPath, to_string(USER_ID)); + ret = (mediaSandboxPath == "") ? E_OK : -EINVAL; + EXPECT_EQ(ret, E_OK); + + physicalPath = "/mnt/hmdfs/100/account/device_view/files/Photo/16/IMG_1747924738_000.jpg"; + mediaSandboxPath = GetMediaSandboxPath(physicalPath, to_string(USER_ID)); + ret = (mediaSandboxPath == "") ? E_OK : -EINVAL; + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaSandboxPath_0001"; + } + + /** + * @tc.name: Remote_file_share_SetMediaHmdfsUriDirInfo_0001 + * @tc.desc: Test function of SetMediaHmdfsUriDirInfo() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_SetMediaHmdfsUriDirInfo_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_SetMediaHmdfsUriDirInfo_0001"; + + HmdfsUriInfo hui; + OHOS::Uri uri("file://media/Photo/16/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + string physicalPath = "/mnt/hmdfs/100/account/cloud_merge_view/files/Photo/16/IMG_1747924738_000.jpg"; + string networkId = "network123"; + string usrId = to_string(USER_ID); + + int32_t ret = SetMediaHmdfsUriDirInfo(hui, uri, physicalPath, networkId, usrId); + ASSERT_TRUE(ret != E_OK); + ret = (hui.uriStr == "") ? -EINVAL : E_OK; + EXPECT_EQ(ret, E_OK); + + physicalPath = "/mnt/hmdfs/100/account"; + ret = SetMediaHmdfsUriDirInfo(hui, uri, physicalPath, networkId, usrId); + EXPECT_EQ(ret, -EINVAL); + + physicalPath = "com.demo.a"; + OHOS::Uri uri1("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + ret = SetMediaHmdfsUriDirInfo(hui, uri1, physicalPath, networkId, usrId); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_SetMediaHmdfsUriDirInfo_0001"; + } + + /** + * @tc.name: Remote_file_share_SetHmdfsUriDirInfo_0001 + * @tc.desc: Test function of SetHmdfsUriDirInfo() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_SetHmdfsUriDirInfo_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_SetHmdfsUriDirInfo_0001"; + + HmdfsUriInfo hui; + OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + string physicalPath = "/data/app/el2/100/base/com.demo.a/remote_share.txt"; + string networkId = "network123"; + string bundleName = "com.demo.a"; + + int32_t ret = SetHmdfsUriDirInfo(hui, uri, physicalPath, networkId, bundleName); + ASSERT_TRUE(ret != E_OK); + ret = (hui.uriStr == "") ? -EINVAL : E_OK; + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_SetHmdfsUriDirInfo_0001"; + } + + /** + * @tc.name: Remote_file_share_GetMediaDfsUrisDirFromLocal_0001 + * @tc.desc: Test function of GetMediaDfsUrisDirFromLocal() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetMediaDfsUrisDirFromLocal_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetMediaDfsUrisDirFromLocal_0001"; + + vector uriList = {}; + int32_t usrId = USER_ID; + unordered_map uriToDfsUriMaps; + int32_t ret = GetMediaDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + uriList.push_back("file://media/Photo/16/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + uriList.push_back("file://media/Photo/1/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + ret = GetMediaDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetMediaDfsUrisDirFromLocal_0001"; + } + + /** + * @tc.name: Remote_file_share_GetDfsUrisDirFromLocal_0001 + * @tc.desc: Test function of GetDfsUrisDirFromLocal() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetDfsUrisDirFromLocal_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetDfsUrisDirFromLocal_0001"; + + vector uriList = {}; + int32_t usrId = USER_ID; + unordered_map uriToDfsUriMaps; + int32_t ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + uriList.push_back("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + uriList.push_back("file://media/Photo/16/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + uriList.push_back("file://media/Photo/1/IMG_202506061600_000/screenshot_20250522_223718.jpg"); + ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDfsUrisDirFromLocal_0001"; + } + + /** + * @tc.name: Remote_file_share_GetDfsUrisDirFromLocal_0002 + * @tc.desc: Test function of GetDfsUrisDirFromLocal() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_GetDfsUrisDirFromLocal_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_GetDfsUrisDirFromLocal_0002"; + + vector uriList = {}; + int32_t usrId = USER_ID; + unordered_map uriToDfsUriMaps; + + uriList.push_back("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + int32_t ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + uriList.push_back("file://com.demo.a/data/storage/el2/../remote_share.txt"); + ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, -EINVAL); + + uriList.clear(); + uriList.push_back("file://docs/storage/Users/currentUser/Document/Subject1/Subject2/1.txt"); + ret = RemoteFileShare::GetDfsUrisDirFromLocal(uriList, usrId, uriToDfsUriMaps); + EXPECT_EQ(ret, E_OK); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_GetDfsUrisDirFromLocal_0002"; + } + + /** + * @tc.name: Remote_file_share_CheckIfNeedMount_0001 + * @tc.desc: Test function of CheckIfNeedMount() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ + HWTEST_F(RemoteFileShareTest, Remote_file_share_CheckIfNeedMount_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteFileShareTest-begin Remote_file_share_CheckIfNeedMount_0001"; + + OHOS::Uri uri("file://com.demo.a/data/storage/el2/base/remote_share.txt"); + string bundleName = "com.demo.a"; + string networkId = "network123"; + int32_t usrId = USER_ID; + + int32_t ret = CheckIfNeedMount(usrId, bundleName, networkId, uri); + EXPECT_EQ(ret, -EINVAL); + + OHOS::Uri uri1("file://com.demo.a/data/storage/el22/base/remote_share.txt"); + ret = CheckIfNeedMount(usrId, bundleName, networkId, uri1); + EXPECT_EQ(ret, -EINVAL); + + OHOS::Uri uri2("file://meida/data/storage/el2/base/remote_share.txt"); + ret = CheckIfNeedMount(usrId, bundleName, networkId, uri2); + EXPECT_EQ(ret, -EINVAL); + + GTEST_LOG_(INFO) << "RemoteFileShareTest-end Remote_file_share_CheckIfNeedMount_0001"; + } }