From b03f44f1d1003d279c8367973ae650a8d2a6a5e7 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 18 Jul 2023 08:11:15 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0fileuri=20bundlename=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E5=9C=BA=E6=99=AF=EF=BC=8C=E4=BB=A5=E5=8F=8Afileuri?= =?UTF-8?q?=20testcase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ia603efd15b076b4023057f4f0f64408fd4c16026 --- interfaces/common/src/common_func.cpp | 13 +- .../native/file_uri/src/file_uri.cpp | 3 +- test/unittest/BUILD.gn | 1 + test/unittest/file_uri_native/BUILD.gn | 36 +++ .../file_uri_native/file_uri_test.cpp | 214 ++++++++++++++++++ 5 files changed, 257 insertions(+), 10 deletions(-) create mode 100644 test/unittest/file_uri_native/BUILD.gn create mode 100644 test/unittest/file_uri_native/file_uri_test.cpp diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 575328f76..53bf7968e 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -184,26 +184,21 @@ bool CommonFunc::CheckPublicDirPath(const std::string &sandboxPath) return false; } -static bool NormalizePath(string &path) +static void NormalizePath(string &path) { - if (path.size() <= 0) { - return false; + if (path.size() == 0) { + return; } if (path[0] != BACKFLASH) { path.insert(0, 1, BACKFLASH); } - - return true; } string CommonFunc::GetUriFromPath(const string &path) { string realPath = path; - if (!realPath.empty() && !NormalizePath(realPath)) { - LOGE("GetUriFromPath::NormalizePath failed!"); - return ""; - } + NormalizePath(realPath); string packageName = GetSelfBundleName(); realPath = FILE_SCHEME_PREFIX + packageName + realPath; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 43bc3e990..d30bc0a55 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -51,7 +51,8 @@ string FileUri::GetPath() string realPath = sandboxPath; string providerBundleName = uri_.GetAuthority(); string targetBundleName = CommonFunc::GetSelfBundleName(); - if (CommonFunc::CheckPublicDirPath(realPath) || targetBundleName != providerBundleName) { + if (CommonFunc::CheckPublicDirPath(realPath) || + ((targetBundleName != providerBundleName) && (providerBundleName != ""))) { realPath = PATH_SHARE + MODE_RW + providerBundleName + sandboxPath; if (access(realPath.c_str(), F_OK) != 0) { realPath = PATH_SHARE + MODE_R + providerBundleName + sandboxPath; diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index bce4a610a..53d8db6d1 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -15,6 +15,7 @@ group("unittest") { testonly = true deps = [ "file_share_native:file_share_test", + "file_uri_native:file_uri_test", "remote_file_share:remote_file_share_test", ] } diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn new file mode 100644 index 000000000..be6bb9caf --- /dev/null +++ b/test/unittest/file_uri_native/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 2023 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. + +import("//build/test.gni") + +ohos_unittest("file_uri_test") { + module_out_path = "filemanagement/app_file_service" + sources = [ "file_uri_test.cpp" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "app_file_service:fileshare_native", + "app_file_service:fileuri_native", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] + + deps = [ + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] +} diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp new file mode 100644 index 000000000..6da553a98 --- /dev/null +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2023 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 "file_uri.h" + +#include +#include +#include + +#include "accesstoken_kit.h" +#include "ipc_skeleton.h" +#include "uri.h" + +#include "common_func.h" +#include "file_share.h" +#include "log.h" + +using namespace std; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::AppFileService; + +const string bundleA = "com.ohos.systemui"; +string CommonFunc::GetSelfBundleName() +{ + return bundleA; +} + +namespace OHOS::AppFileService::ModuleFileUri { + const string PATH_SHARE = "/data/storage/el2/share"; + const string MODE_RW = "/rw/"; + const string MODE_R = "/r/"; + const int E_OK = 0; + + class FileUriTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: file_uri_test_0000 + * @tc.desc: Test function of ToString() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000"; + + string fileStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + bundleA + fileStr; + FileUri fileUri(fileStr); + EXPECT_EQ(fileUri.ToString(), uri); + + FileUri fileUri2(uri); + EXPECT_EQ(fileUri2.ToString(), uri); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000"; + } + + /** + * @tc.name: file_uri_test_0001 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000"; + + string fileStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + bundleA + fileStr; + FileUri fileUri(fileStr); + string name = fileUri.GetName(); + EXPECT_EQ(name, "test.txt"); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000"; + } + + /** + * @tc.name: file_uri_test_0002 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000"; + string fileStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + bundleA + fileStr; + FileUri fileUri(uri); + string path = fileUri.GetPath(); + EXPECT_EQ(path, fileStr); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000"; + } + + /** + * @tc.name: file_uri_test_0003 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001"; + string fileStr = "/Documents/test.txt"; + string uri = "file://" + bundleA + fileStr; + string rltStr = PATH_SHARE + MODE_R + bundleA + fileStr; + FileUri fileUri(uri); + string path = fileUri.GetPath(); + EXPECT_EQ(path, rltStr); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; + } + + /** + * @tc.name: file_uri_test_0004 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002"; + string fileStr = "/data/storage/el2/base/files/test.txt"; + string bundleB = "com.demo.b"; + string uri = "file://" + bundleB + fileStr; + string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr; + FileUri fileUri(uri); + string path = fileUri.GetPath(); + EXPECT_EQ(path, rltStr); + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002"; + } + + /** + * @tc.name: file_uri_test_0005 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003"; + int32_t uid = -1; + uid = OHOS::IPCSkeleton::GetCallingUid(); + string bundleB = "com.ohos.settingsdata"; + string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt"; + int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!"; + + string actStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + bundleB + actStr; + uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, bundleA, 0); + + int32_t flag = 3; + int32_t ret = CreateShareFile(uri, tokenId, flag); + EXPECT_EQ(ret, E_OK); + + string rltStr = PATH_SHARE + MODE_R + bundleB + actStr; + FileUri fileUri(uri); + string path = fileUri.GetPath(); + EXPECT_EQ(path, rltStr); + + vector sharePathList; + sharePathList.push_back(uri); + ret = DeleteShareFile(tokenId, sharePathList); + EXPECT_EQ(ret, E_OK); + close(fd); + 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 + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I7LW57 + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; + string fileStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + fileStr; + FileUri fileUri(uri); + EXPECT_EQ(fileUri.ToString(), uri); + EXPECT_EQ(fileUri.GetName(), "test.txt"); + EXPECT_EQ(fileUri.GetPath(), fileStr); + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004"; + } +} \ No newline at end of file -- Gitee