From 14c90b84df913a95164299b206259ede558d254b Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 28 Nov 2023 14:06:39 +0800 Subject: [PATCH] add fileuri tdd and remove path print Signed-off-by: lvyuanyuan Change-Id: Ieca003cc3034a5ca611845afe7feab588844727c --- interfaces/common/src/sandbox_helper.cpp | 2 +- .../native/file_share/src/file_share.cpp | 8 ++++---- .../innerkits/native/file_uri/src/file_uri.cpp | 2 -- .../remote_file_share/src/remote_file_share.cpp | 16 +++++++--------- .../kits/js/file_share/grant_uri_permission.cpp | 4 +--- test/unittest/file_uri_native/file_uri_test.cpp | 15 ++++++++++++++- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index f923a25b1..fb70c5dec 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -136,7 +136,7 @@ bool SandboxHelper::GetSandboxPathMap() nlohmann::json jsonObj; int ret = JsonUtils::GetJsonObjFromPath(jsonObj, SANDBOX_JSON_FILE_PATH); if (ret != 0) { - LOGE("Get json object failed from %{public}s with %{public}d", SANDBOX_JSON_FILE_PATH.c_str(), ret); + LOGE("Get json object failed with %{public}d", ret); return false; } diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 1b77656c7..34b25793d 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -101,7 +101,7 @@ static bool IsExistFile(const string &path) { struct stat buf = {}; if (stat(path.c_str(), &buf) != 0) { - LOGE("Get path stat failed, path: %s err %{public}d", path.c_str(), errno); + LOGE("Get path stat failed, err %{public}d", errno); return false; } return S_ISREG(buf.st_mode); @@ -192,7 +192,7 @@ static void DeleteExistShareFile(const string &path) static void DelSharePath(const string &delPath) { if (!SandboxHelper::CheckValidPath(delPath)) { - LOGE("DelSharePath, umount path is invalid, path = %{private}s", delPath.c_str()); + LOGE("DelSharePath, umount path is invalid"); return; } @@ -259,7 +259,7 @@ static bool NotRequiredBindMount(const FileShareInfo &info, uint32_t flag, const static int32_t CreateSingleShareFile(const string &uri, uint32_t tokenId, uint32_t flag, FileShareInfo &info) { - LOGD("CreateShareFile begin with uri %{private}s", uri.c_str()); + LOGD("CreateShareFile begin"); if (NotRequiredBindMount(info, flag, uri)) { LOGD("Not required to bind mount"); return 0; @@ -282,7 +282,7 @@ static int32_t CreateSingleShareFile(const string &uri, uint32_t tokenId, uint32 } for (size_t i = 0; i < info.sharePath_.size(); i++) { - if ((ret = open(info.sharePath_[i].c_str(), O_RDONLY | O_CREAT)) < 0) { + if ((ret = open(info.sharePath_[i].c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) < 0) { LOGE("Create file failed with %{public}d", errno); return -errno; } diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index f027c842c..fe9591efb 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -68,7 +68,6 @@ string FileUri::GetRealPath() string sandboxPath = SandboxHelper::Decode(uri_.GetPath()); string realPath = sandboxPath; string bundleName = uri_.GetAuthority(); - LOGD("GetRealPath decode path is %{private}s", sandboxPath.c_str()); if (bundleName == FILE_MANAGER_AUTHORITY && uri_.ToString().find(NETWORK_PARA) == string::npos && access(realPath.c_str(), F_OK) == 0) { @@ -81,7 +80,6 @@ string FileUri::GetRealPath() realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath; } } - LOGD("GetRealPath real path is %{private}s", realPath.c_str()); return realPath; } 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 a8abd93b8..7911c4620 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 @@ -112,7 +112,7 @@ static std::string GetFileName(const int &fd) std::string fileName = filePath; std::size_t firstSlash = fileName.rfind("/"); if (firstSlash == fileName.npos) { - LOGE("RemoteFileShare::GetFileName, get error path with %{public}s", fileName.c_str()); + LOGE("RemoteFileShare::GetFileName, get error path"); return ""; } fileName = fileName.substr(firstSlash + 1, fileName.size() - firstSlash); @@ -150,7 +150,7 @@ static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &S LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; } else { - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", SHARE_PATH.c_str()); + LOGI("RemoteFileShare::DeleteShareDir, delete path successfully"); } } if (access(PACKAGE_PATH.c_str(), F_OK) == 0) { @@ -159,7 +159,7 @@ static bool DeleteShareDir(const std::string &PACKAGE_PATH, const std::string &S LOGE("RemoteFileShare::DeleteShareDir, delete dir failed with %{public}d", errno); result = false; } else { - LOGI("RemoteFileShare::DeleteShareDir, delete %{public}s path successfully", PACKAGE_PATH.c_str()); + LOGI("RemoteFileShare::DeleteShareDir, delete path successfully"); } } return result; @@ -253,7 +253,7 @@ int RemoteFileShare::CreateSharePath(const int &fd, std::string &sharePath, DeleteShareDir(PACKAGE_PATH, LOWER_SHARE_PATH); return errno; } - LOGI("RemoteFileShare::CreateSharePath, create %{public}s successfully", sharePath.c_str()); + LOGI("RemoteFileShare::CreateSharePath, create successfully"); return 0; } @@ -348,7 +348,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int { Uri uri(uriStr); std::string bundleName = uri.GetAuthority(); - LOGD("GetDfsUriFromLocal begin with uri:%{private}s", uriStr.c_str()); + LOGD("GetDfsUriFromLocal begin"); std::string physicalPath = GetPhysicalPath(uri, std::to_string(userId)); if (physicalPath == "") { LOGE("Failed to get physical path"); @@ -357,7 +357,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int if (bundleName == MEDIA_AUTHORITY || bundleName == FILE_MANAGER_AUTHORITY) { (void)SetPublicDirHmdfsInfo(physicalPath, uriStr, hui); - LOGD("GetDfsUriFromLocal successfully with %{private}s", hui.uriStr.c_str()); + LOGD("GetDfsUriFromLocal successfully"); return 0; } @@ -369,8 +369,6 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int } struct HmdfsDstInfo hdi; - LOGD("PhysicalPath: %{private}s DistributedPath: %{private}s BundleName: %{private}s", - physicalPath.c_str(), distributedPath.c_str(), bundleName.c_str()); InitHmdfsInfo(hdi, physicalPath, distributedPath, bundleName); std::string ioctlDir = SHAER_PATH_HEAD + std::to_string(userId) + SHAER_PATH_MID; @@ -389,7 +387,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int close(dirFd); SetHmdfsUriInfo(hui, uri, hdi.size); - LOGD("GetDfsUriFromLocal successfully with %{private}s", hui.uriStr.c_str()); + LOGD("GetDfsUriFromLocal successfully"); return 0; } } // namespace ModuleRemoteFileShare diff --git a/interfaces/kits/js/file_share/grant_uri_permission.cpp b/interfaces/kits/js/file_share/grant_uri_permission.cpp index 69f9d60ce..34fc2b20f 100644 --- a/interfaces/kits/js/file_share/grant_uri_permission.cpp +++ b/interfaces/kits/js/file_share/grant_uri_permission.cpp @@ -210,13 +210,11 @@ namespace ModuleFileShare { uriPermInfo.uri = string(uri.get()); if (!CheckValidPublicUri(uriPermInfo.uri)) { - LOGE("FileShare::GetJSArgs uri = %{private}s parameter format error!", uriPermInfo.uri.c_str()); + LOGE("FileShare::GetJSArgs uri parameter format error!"); NError(EINVAL).ThrowErr(env); return false; } - LOGD("FileShare::GetJSArgs uri = %{private}s", uriPermInfo.uri.c_str()); - auto [succBundleName, bundleName, lenBundleName] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); if (!succBundleName) { LOGE("FileShare::GetJSArgs get bundleName parameter failed!"); diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index 2b03d516a..e9efac2a1 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -112,6 +112,11 @@ namespace OHOS::AppFileService::ModuleFileUri { FileUri fileUri(uri); string path = fileUri.GetPath(); EXPECT_EQ(path, fileStr); + + string uri2 = "file://media/Photo/12/IMG_12345_999999/test.jpg"; + FileUri fileUri2(uri2); + path = fileUri2.GetPath(); + EXPECT_EQ(path, "/Photo/12/IMG_12345_999999"); GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000"; } @@ -131,6 +136,13 @@ namespace OHOS::AppFileService::ModuleFileUri { FileUri fileUri(uri); string path = fileUri.GetRealPath(); EXPECT_EQ(path, fileStr); + + string fileStr2 = "docs/storage/Users/currentUser/Documents/1.txt"; + string uri2 = "file://" + fileStr2; + FileUri fileUri2(uri2); + path.clear(); + path = fileUri2.GetRealPath(); + EXPECT_EQ(path, PATH_SHARE + MODE_R + fileStr2); GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; } @@ -195,7 +207,7 @@ namespace OHOS::AppFileService::ModuleFileUri { GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003"; } - /** + /** * @tc.name: file_uri_test_0006 * @tc.desc: Test function of GetPath() interface for SUCCESS. * @tc.size: MEDIUM @@ -212,6 +224,7 @@ namespace OHOS::AppFileService::ModuleFileUri { EXPECT_EQ(fileUri.ToString(), uri); EXPECT_EQ(fileUri.GetName(), "test.txt"); EXPECT_EQ(fileUri.GetPath(), fileStr); + EXPECT_EQ(fileUri.GetRealPath(), fileStr); GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; } -- Gitee