From a2de31e648c8c134db492276c1744eaff9fd8753 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Thu, 13 Jul 2023 10:24:27 +0000 Subject: [PATCH 1/2] =?UTF-8?q?file=5Furi=E6=8E=A5=E5=8F=A3=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: If73cbd0d626c7e244907cc67564da1266cd491d4 --- bundle.json | 9 +++ interfaces/common/include/common_func.h | 2 + interfaces/common/src/common_func.cpp | 75 ++++++++++++++++++- interfaces/innerkits/native/BUILD.gn | 47 ++++++++++++ .../native/file_uri/include/file_uri.h | 39 ++++++++++ .../native/file_uri/src/file_uri.cpp | 73 ++++++++++++++++++ interfaces/kits/js/BUILD.gn | 1 + .../kits/js/file_uri/get_uri_from_path.cpp | 72 +----------------- .../kits/js/file_uri/get_uri_from_path.h | 8 -- 9 files changed, 249 insertions(+), 77 deletions(-) create mode 100644 interfaces/innerkits/native/file_uri/include/file_uri.h create mode 100644 interfaces/innerkits/native/file_uri/src/file_uri.cpp diff --git a/bundle.json b/bundle.json index 5186429d4..878e81cbf 100644 --- a/bundle.json +++ b/bundle.json @@ -74,6 +74,15 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:fileuri_native", + "header": { + "header_files": [ + "file_uri.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" + } + }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native:remote_file_share_native", "header": { diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index cbeb03d3b..428213ece 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -27,6 +27,8 @@ public: static bool CheckValidPath(const std::string &filePath); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); + static std::string GetSelfBundleName(); + static std::string GetUriFromPath(const std::string &path); }; } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index fae72c9d9..a2621b3e0 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -17,14 +17,21 @@ #include +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "uri.h" +#include "system_ability_definition.h" + #include "log.h" #include "json_utils.h" -#include "uri.h" using namespace std; namespace OHOS { namespace AppFileService { +using namespace OHOS::AppExecFwk; namespace { const string PACKAGE_NAME_FLAG = ""; const string CURRENT_USER_ID_FLAG = ""; @@ -32,6 +39,8 @@ namespace { const string SANDBOX_PATH_KEY = "sandbox-path"; const string MOUNT_PATH_MAP_KEY = "mount-path-map"; const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; + const std::string FILE_SCHEME_PREFIX = "file://"; + const char SCHEME_PATH_BEGIN = '/'; } std::unordered_map CommonFunc::sandboxPathMap_; @@ -122,6 +131,70 @@ bool CommonFunc::CheckValidPath(const std::string &filePath) return false; } } + +static sptr GetBundleMgrProxy() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + LOGE("fail to get system ability mgr."); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + LOGE("fail to get bundle manager proxy."); + return nullptr; + } + + return iface_cast(remoteObject); +} + +string CommonFunc::GetSelfBundleName() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetSelfBundleName: bundle mgr proxy is nullptr."); + return ""; + } + + BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != ERR_OK) { + LOGE("GetSelfBundleName: bundleName get fail. uid is %{public}d", uid); + return ""; + } + + return bundleInfo.name; +} + +static bool NormalizePath(string &path) +{ + if (path.size() <= 0) { + return false; + } + + if (path[0] != SCHEME_PATH_BEGIN) { + path.insert(0, 1, SCHEME_PATH_BEGIN); + } + + return true; +} + +string CommonFunc::GetUriFromPath(const string &path) +{ + string realPath = path; + if (!NormalizePath(realPath)) { + return ""; + } + + string packageName = GetSelfBundleName(); + realPath = FILE_SCHEME_PREFIX + packageName + realPath; + return realPath; +} } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 833d8305d..6e53db4b5 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -23,6 +23,15 @@ config("file_share_config") { ] } +config("file_uri_config") { + visibility = [ ":*" ] + include_dirs = [ + "file_uri/include", + "//third_party/json/include", + "../../common/include", + ] +} + ohos_shared_library("fileshare_native") { sources = [ "../../common/src/common_func.cpp", @@ -36,9 +45,41 @@ ohos_shared_library("fileshare_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", + "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} + +ohos_shared_library("fileuri_native") { + sources = [ + "../../common/src/common_func.cpp", + "../../common/src/json_utils.cpp", + "file_uri/src/file_uri.cpp", + ] + + public_configs = [ ":file_uri_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", ] part_name = "app_file_service" @@ -78,8 +119,13 @@ ohos_shared_library("remote_file_share_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", + "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", ] innerapi_tags = [ "platformsdk_indirect" ] @@ -90,6 +136,7 @@ ohos_shared_library("remote_file_share_native") { group("app_file_service_native") { deps = [ ":fileshare_native", + ":fileuri_native", ":remote_file_share_native", ] } diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h new file mode 100644 index 000000000..eb76514c3 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef APP_FILE_SERVICE_FILE_URI_FILE_URI_H +#define APP_FILE_SERVICE_FILE_URI_FILE_URI_H + +#include + +#include "uri.h" +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +class FileUri { +public: + std::string GetName(); + std::string GetPath(); + std::string ToString(); + + explicit FileUri(const std::string &uriOrPath); + ~FileUri() = default; +private: + Uri uri_; +}; +} // ModuleFileUri +} // namespace AppFileService +} // namespace OHOS +#endif // APP_FILE_SERVICE_FILE_URI_FILE_URI_H \ No newline at end of file diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp new file mode 100644 index 000000000..f06313c90 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -0,0 +1,73 @@ +/* + * 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 "uri.h" + +#include "common_func.h" +#include "log.h" + +using namespace std; +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +const std::string PATH_SHARE = "/data/storage/el2/share"; +const std::string MODE_RW = "/rw/"; +const std::string MODE_R = "/r/"; +const std::string FILE_SCHEME_PREFIX = "file://"; +string FileUri::GetName() +{ + string sandboxPath = uri_.GetPath(); + size_t posLast = sandboxPath.find_last_of("/"); + if (posLast == string::npos) { + return sandboxPath; + } + + if (posLast == sandboxPath.size()) { + return ""; + } + + return sandboxPath.substr(posLast + 1); +} + +string FileUri::GetPath() +{ + string path = uri_.GetPath(); + string bundleName = uri_.GetAuthority(); + if (bundleName != CommonFunc::GetSelfBundleName()) { + path = PATH_SHARE + MODE_RW + bundleName + path; + if (access(path.c_str(), F_OK) != 0) { + path = PATH_SHARE + MODE_R + bundleName + path; + } + } + + return path; +} + +string FileUri::ToString() +{ + return uri_.ToString(); +} + +FileUri::FileUri(const string &uriOrPath): uri_( + (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath) +) +{} +} +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b93c1e085..58b987fc6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("fileuri") { ] sources = [ + "../../common/src/common_func.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", ] diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp index ea23b1118..48e085d01 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -14,71 +14,15 @@ */ #include "get_uri_from_path.h" -#include "bundle_info.h" -#include "bundle_mgr_proxy.h" -#include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "log.h" #include "status_receiver_host.h" -#include "system_ability_definition.h" + +#include "common_func.h" +#include "log.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::FileManagement::LibN; -using namespace OHOS::AppExecFwk; - -static sptr GetBundleMgrProxy() -{ - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - LOGE("fail to get system ability mgr."); - return nullptr; - } - - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - LOGE("fail to get bundle manager proxy."); - return nullptr; - } - - return iface_cast(remoteObject); -} - -static string GetBundleName() -{ - int uid = -1; - uid = IPCSkeleton::GetCallingUid(); - - sptr bundleMgrProxy = GetBundleMgrProxy(); - if (!bundleMgrProxy) { - LOGE("GetBundleName: bundle mgr proxy is nullptr."); - return nullptr; - } - - BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); - if (ret != ERR_OK) { - LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); - return nullptr; - } - - return bundleInfo.name; -} - -static bool NormalizePath(string &path) -{ - if (path.size() <= 0) { - return false; - } - - if (path[0] != SCHEME_PATH_BEGIN) { - path.insert(0, 1, SCHEME_PATH_BEGIN); - } - - return true; -} napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) { @@ -95,15 +39,7 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } - string realPath = path.get(); - if (!NormalizePath(realPath)) { - LOGE("GetUriFromPath::NormalizePath failed!"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - string packageName = GetBundleName(); - string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + realPath; + std::string uri = CommonFunc::GetUriFromPath(path.get()); return NVal::CreateUTF8String(env, uri).val_; } diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h index 46ee4fc43..8adb88fb9 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -22,14 +22,6 @@ namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -using namespace std; - -const string SCHEME = "file"; -const char SCHEME_SEPARATOR = ':'; -const string PATH_SYMBOLS = "//"; -const string FRAGMENT_SYMBOLS = "#"; -const char SCHEME_PATH_BEGIN = '/'; - class GetUriFromPath final { public: static napi_value Sync(napi_env env, napi_callback_info info); -- Gitee From bf209bba13f1b623352c3b3d66416fa3d6bfabc1 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Mon, 17 Jul 2023 01:11:12 +0000 Subject: [PATCH 2/2] =?UTF-8?q?file=20uri=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I7fa0141de51dadf1eb477b49d12c9ca8ed507d66 --- test/unittest/BUILD.gn | 1 + test/unittest/file_uri_native/BUILD.gn | 36 ++++ .../file_uri_native/file_uri_test.cpp | 168 ++++++++++++++++++ test/unittest/remote_file_share/BUILD.gn | 1 + 4 files changed, 206 insertions(+) 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/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index bce4a610a..20930d644 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -16,5 +16,6 @@ group("unittest") { deps = [ "file_share_native:file_share_test", "remote_file_share:remote_file_share_test", + "file_uri_native:file_uri_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..d67fd7cfa --- /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:fileuri_native", + "app_file_service:remote_file_share_native", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] + + deps = [ + "//third_party/googletest:gtest_main", + "//third_party/googletest:gmock_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..a2ab3f4a2 --- /dev/null +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -0,0 +1,168 @@ +/* + * 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 + +#include "uri.h" + +#include "common_func.h" +#include "log.h" +#include "remote_file_share.h" + +using namespace std; +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri{ + const string bundleA = "com.demo.a"; + const string PATH_SHARE = "/data/storage/el2/share"; + const string MODE_RW = "/rw/"; + const string MODE_R = "/r/"; + class MockCommonFunc : public CommonFunc { + public: + MOCK_METHOD0(GetSelfBundleName, string()); + } mockCommonFunc; + + class FileUriTest : public testing::Test { + public: + static void SetUpTestCase(void) + { + EXPECT_CALL(mockCommonFunc, GetSelfBundleName()).WillOnce(testing::Return(bundleA)); + }; + 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: SR000H63TL + */ + 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: SR000H63TL + */ + 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: SR000H63TL + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000"; + //MockObject mockCommonFunc2 {}; + //MOCK_METHOD(mockCommonFunc2, GetBundleName).defaults().will(returnValue(bundleA));; + EXPECT_CALL(mockCommonFunc, GetSelfBundleName()).WillOnce(testing::Return(bundleA)); + 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: SR000H63TL + */ + HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001"; + EXPECT_CALL(mockCommonFunc, GetSelfBundleName()).WillOnce(testing::Return(bundleA)); + const string dirPath = "/data/app/el2/100/base/com.demo.a/"; + const string fileStr = "/data/app/el2/100/base/com.demo.a/remote_file_share_test.txt"; + int ret = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); + ASSERT_TRUE((ret != -1) || (ret == -1 && errno == EEXIST)) << "RemoteFileShareTest mkdir failed! " << errno; + int fd = open(fileStr.c_str(), O_RDWR); + ASSERT_TRUE(fd != -1) << "RemoteFileShareTest Create File Failed!"; + const int userId = 100; + const string deviceId = "0"; + string sharePath = ""; + ret = ModuleRemoteFileShare::RemoteFileShare::CreateSharePath(fd, sharePath, userId, deviceId); + close(fd); + + const string uriStr = "/data/storage/el2/base/files/test.txt"; + FileUri fileUri(uriStr); + string path = fileUri.GetPath(); + EXPECT_EQ(path, fileStr); + /*EXPECT_CALL(mockCommonFunc, GetSelfBundleName()).WillOnce(testing::Return(bundleA)); + string bundleNameB = "com.ohos.b"; + const string dirPath = "/data/service/el2/100/share/" + MODE_RW + bundleNameB + "/"; + int ret = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); + ASSERT_TRUE((ret != -1) || (ret == -1 && errno == EEXIST)) << "RemoteFileShareTest mkdir failed! " << errno; + const string fileStr = "com.demo.a/data/storage/el2/base/remote_file_share_test.txt"; + const int userId = 100; + + int ret = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); + ASSERT_TRUE((ret != -1) || (ret == -1 && errno == EEXIST)) << "RemoteFileShareTest mkdir failed! " << errno; + + int fd = open(fileStr.c_str(), O_RDWR | O_CREAT); + ASSERT_TRUE(fd != -1) << "RemoteFileShareTest open file failed! " << errno; + string bundleNameB = "com.ohos.b"; + string fileStr = "/data/storage/el2/base/files/test.txt"; + string uri = "file://" + bundleNameB + fileStr; + FileUri fileUri(fileStr); + string path = fileUri.GetPath(); + EXPECT_EQ(path, PATH_SHARE + MODE_R + bundleA + path);*/ + GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001"; + } +} +} +} \ 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 f3e9d436c..c705b8120 100644 --- a/test/unittest/remote_file_share/BUILD.gn +++ b/test/unittest/remote_file_share/BUILD.gn @@ -32,5 +32,6 @@ ohos_unittest("remote_file_share_test") { deps = [ "../../../interfaces/innerkits/native:remote_file_share_native", "//third_party/googletest:gtest_main", + "//third_party/googletest:gmock_main", ] } -- Gitee