增加fileuri bundlename为空场景,以及fileuri testcase

Signed-off-by: lvyuanyuan <lvyuanyuan7@huawei.com>
Change-Id: Ia603efd15b076b4023057f4f0f64408fd4c16026
This commit is contained in:
lvyuanyuan 2023-07-18 08:11:15 +00:00
parent 4902ab5e59
commit b03f44f1d1
5 changed files with 257 additions and 10 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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",
]
}

View File

@ -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",
]
}

View File

@ -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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <fcntl.h>
#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<string> 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";
}
}