From 5b423f9f70bbddc3c67e5f5d37f61d68ac5351b9 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Wed, 11 Jan 2023 16:54:22 +0800 Subject: [PATCH] Add C++ related interface. Signed-off-by: zhangkaixiang5@huawei.com --- BUILD.gn | 1 + bundle.json | 9 + interfaces/innerkits/file_share/BUILD.gn | 49 +++ .../innerkits/file_share/file_share.cpp | 342 ++++++++++++++++++ interfaces/innerkits/file_share/file_share.h | 72 ++++ .../innerkits/file_share/file_share_log.h | 36 ++ interfaces/kits/js/BUILD.gn | 11 +- .../kits/js/file_uri/get_uri_from_path.cpp | 46 ++- test/unittest/BUILD.gn | 5 +- test/unittest/file_share/BUILD.gn | 44 +++ test/unittest/file_share/file_share_test.cpp | 57 +++ 11 files changed, 666 insertions(+), 6 deletions(-) create mode 100644 interfaces/innerkits/file_share/BUILD.gn create mode 100644 interfaces/innerkits/file_share/file_share.cpp create mode 100644 interfaces/innerkits/file_share/file_share.h create mode 100644 interfaces/innerkits/file_share/file_share_log.h create mode 100644 test/unittest/file_share/BUILD.gn create mode 100644 test/unittest/file_share/file_share_test.cpp diff --git a/BUILD.gn b/BUILD.gn index 2c041cd26..1467872fb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,5 +17,6 @@ group("libremotefileshare") { deps = [ "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "//foundation/filemanagement/app_file_service/interfaces/kits/js:remotefileshare", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share:file_share_native", ] } diff --git a/bundle.json b/bundle.json index 8e6c15411..5e109ae84 100644 --- a/bundle.json +++ b/bundle.json @@ -40,6 +40,15 @@ ], "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" } + }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share:file_share_native", + "header": { + "header_files": [ + "file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share" + } } ], "test": [ diff --git a/interfaces/innerkits/file_share/BUILD.gn b/interfaces/innerkits/file_share/BUILD.gn new file mode 100644 index 000000000..e4b53a1fe --- /dev/null +++ b/interfaces/innerkits/file_share/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2022 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/ohos.gni") + +config("file_share_config") { + visibility = [ ":*" ] + include_dirs = [ + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//foundation/ability/ability_base/interfaces/kits/native/uri/include", + ] +} + +ohos_shared_library("file_share_native") { + sources = [ "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/file_share.cpp" ] + + public_configs = [ ":file_share_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", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + + ] + + relative_install_dir = "module" + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/innerkits/file_share/file_share.cpp b/interfaces/innerkits/file_share/file_share.cpp new file mode 100644 index 000000000..6404e36f5 --- /dev/null +++ b/interfaces/innerkits/file_share/file_share.cpp @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2022 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_share.h" +#include +#include +#include +#include +#include +#include "bundle_mgr_interface.h" +#include "bundlemgr/bundle_mgr_proxy.h" +#include "file_share_log.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" +#include "uri.h" + + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { +using namespace OHOS::AppExecFwk; + +static string GetBundleName(Security::AccessToken::AccessTokenID tokenID) +{ + Security::AccessToken::HapTokenInfo hapInfo; + int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenID, hapInfo); + if (result != 0) { + LOGI("FileShare::GetBundleName, failed to get hap token info %{public}d", result); + } + LOGI("FileShare::GetBundleName, bundle Name is %{public}s", hapInfo.bundleName.c_str()); + return hapInfo.bundleName.c_str(); +} + +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; + } + + LOGI("FileShare::GetBundleMgrProxy, get bundle manager proxy success."); + return iface_cast(remoteObject); +} + +static string GetBundleNameSelf() +{ + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("FileShare::GetBundleNameSelf, bundle mgr proxy is nullptr."); + return nullptr; + } + + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + LOGE("FileShare::GetBundleNameSelf, bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + LOGI("FileShare::GetBundleNameSelf, uid is %{public}d, bundleName is %{public}s", uid, bundleName.c_str()); + return bundleName; +} + +static string GetCurrentUid(Security::AccessToken::AccessTokenID tokenID) +{ + Security::AccessToken::HapTokenInfo hapInfo; + int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenID, hapInfo); + if (result != 0) { + LOGI("FileShare::GetCurrentUid, failed to get hap token info %{public}d", result); + } + return to_string(hapInfo.userID); +} + +static string GetRealPath(const string &uri, Security::AccessToken::AccessTokenID tokenID) +{ + Uri uriTemp(uri); + string scheme = uriTemp.GetScheme(); + string::size_type posFile = scheme.find(SCHEME); + if(posFile == string::npos){ + return ""; + } + + string path = uriTemp.GetPath(); + string::size_type posPath = path.find(PATH_SYMBOLS); + if (posPath == string::npos) { + return ""; + } + size_t num = sizeof(sandBoxPath) / sizeof(sandBoxPath[0]); + string fragmentPath; + string srcPathTemp; + for (size_t i = 0; i < num; i++) + { + if(path.length() >= sandBoxPath[i].length()){ + string sandBoxPathTemp = path.substr(0, sandBoxPath[i].length()); + if(sandBoxPathTemp == sandBoxPath[i]) { + srcPathTemp = srcPath[i]; + fragmentPath = path.substr(sandBoxPath[i].length()); + } + } + } + + if (srcPathTemp.find(CURRENTUSERID) != string::npos) { + srcPathTemp = srcPathTemp.replace(srcPathTemp.find(CURRENTUSERID), CURRENTUSERID.length(), + GetCurrentUid(tokenID)); + } + + if (srcPathTemp.find(PACKAGENAME) != string::npos) { + srcPathTemp = srcPathTemp.replace(srcPathTemp.find(PACKAGENAME), PACKAGENAME.length(), + GetBundleName(tokenID)); + } + LOGI("FileShare::GetRealPath, srcPathTemp1 is %{public}s", srcPathTemp.c_str()); + srcPathTemp += fragmentPath; + return srcPathTemp; +} + +static bool MakeDirectory(const string &path, int flag) +{ + string::size_type index = 0; + string subPath; + do { + index = path.find('/', index + 1); + if (index == string::npos) { + subPath = path; + } else { + subPath = path.substr(0, index); + } + + if (access(subPath.c_str(), 0) != 0) { + if (mkdir(subPath.c_str(), flag) != 0) { + return false; + } + } + } while (index != string::npos); + + LOGE("FileShare::MakeDirectory, make dir is %{public}s", subPath.c_str()); + return true; +} + +static bool IsExistDir(const std::string &path) +{ + if (path.empty()) { + return false; + } + + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + return false; + } + return S_ISDIR(buf.st_mode); +} + +static bool IsExistFile(const std::string &path) +{ + if (path.empty()) { + return false; + } + + struct stat buf = {}; + if (stat(path.c_str(), &buf) != 0) { + return false; + } + return S_ISREG(buf.st_mode); +} + +static string GetLinksPath(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag) +{ + string sharePath; + string realPath = GetRealPath(uri, tokenID); + string uid = GetCurrentUid(tokenID); + string bundleName = GetBundleName(tokenID); + string bundleNameSelf = GetBundleNameSelf(); + string flagStr = to_string(flag); + + if(IsExistFile(realPath)) + { + sharePath = REAL_DATA_PATH + "/" + uid + "/" + "share" + "/" + bundleNameSelf + "/" + + bundleName + "/" + flagStr + realPath; + } + if(IsExistDir(realPath)) + { + sharePath = REAL_DATA_PATH + "/" + uid + "/" + "share" + "/" + bundleNameSelf + "/" + + bundleName + "/" + flagStr + realPath; + + } + + LOGE("FileShare::GetLinksPath, share Path is %{public}s", sharePath.c_str()); + return sharePath; +} + +string IncludeTrailingPathDelimiter(const std::string& path) +{ + if (path.rfind("/") != path.size() - 1) { + return path + "/"; + } + + return path; +} + +bool RemoveFile(const string& fileName) +{ + if (access(fileName.c_str(), F_OK) == 0) { + return remove(fileName.c_str()) == 0; + } + + return true; +} + +static bool RemoveDirectory(const string& path) +{ + string subPath; + bool ret = true; + DIR *dir = opendir(path.c_str()); + if (dir == nullptr) { + return false; + } + + while (true) { + struct dirent *ptr = readdir(dir); + if (ptr == nullptr) { + break; + } + + if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { + continue; + } + subPath = IncludeTrailingPathDelimiter(path) + string(ptr->d_name); + + subPath = path + string(ptr->d_name); + if (ptr->d_type == DT_DIR) { + ret = RemoveDirectory(subPath); + } else { + if (access(subPath.c_str(), F_OK) == 0) { + if (remove(subPath.c_str()) != 0) { + closedir(dir); + return false; + } + } + } + } + closedir(dir); + + return ret; +} + +bool DeleteDir(const std::string &path) +{ + if (IsExistFile(path)) { + return RemoveFile(path); + } + if (IsExistDir(path)) { + return RemoveDirectory(path); + } + return true; +} + +int FileShare::CreateShareFile(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag) +{ + string dstPath; + string realPath = GetRealPath(uri, tokenID); + LOGE("FileShare::CreateShareFile, real Path is %{public}s", realPath.c_str()); + if(IsExistFile(realPath)) + { + LOGI("FileShare::CreateShareFile, file"); + if (access(realPath.c_str(), 0) == 0) { + dstPath = GetLinksPath(uri, tokenID, flag); + string fileName = dstPath.substr(dstPath.find_last_of("/") + 1); + string makePath = dstPath.erase(dstPath.find_last_of("/")); + if(!MakeDirectory(makePath.c_str(), flag)){ + LOGE("FileShare::CreateShareFile, make dir failed with %{public}d", errno); + return errno; + } + if(link(realPath.c_str(), (makePath + "/" + fileName).c_str()) != 0){ + LOGI("FileShare::CreateShareFile, symlink failed with %{public}d", errno); + return errno; + } + } + else{ + return errno; + LOGI("FileShare::CreateShareFile, dir Path not Exits"); + } + } + if(IsExistDir(realPath)) + { + LOGI("FileShare::CreateShareFile, dir"); + if (access(realPath.c_str(), 0) == 0) { + dstPath = GetLinksPath(uri, tokenID, flag); + if(!MakeDirectory(dstPath.c_str(), flag)){ + LOGE("FileShare::CreateShareFile, make dir failed with %{public}d", errno); + return errno; + } + if(link(realPath.c_str(), dstPath.c_str())!= 0){ + LOGI("FileShare::CreateShareFile, symlink failed with %{public}d", errno); + return errno; + } + } + else{ + LOGI("FileShare::CreateShareFile, symlink failed with %{public}d", errno); + return errno; + } + + } + LOGI("FileShare::CreateShareFile, Create Share File Success"); + return 0; +} + +int FileShare::DeleteShareFile(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag) +{ + string sharePath = REAL_DATA_PATH + "/" + "100" + "/" + "share"; + if(!DeleteDir(sharePath.c_str())) + { + LOGE("FileShare::DeleteShareFile, delete dir failed with %{public}d", errno); + return errno; + } + LOGI("FileShare::DeleteShareFile, delete Share File Success"); + return 0; +} +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS diff --git a/interfaces/innerkits/file_share/file_share.h b/interfaces/innerkits/file_share/file_share.h new file mode 100644 index 000000000..79ae82d82 --- /dev/null +++ b/interfaces/innerkits/file_share/file_share.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 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 FILE_SHARE_H +#define FILE_SHARE_H + +#include +#include +#include "accesstoken_kit.h" +#include "hap_token_info.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { +using namespace std; + +namespace { +const string SCHEME = "file"; +const string PATH_SYMBOLS = "/data/storage/"; +const string FRAGMENT_SYMBOLS = "/data"; +const string DOUBLE_SLASH_SYMBOLS = "//"; +const string PATH_SEPARATOR = "/"; +const string PACKAGENAME = ""; +const string CURRENTUSERID = ""; +const string REAL_DATA_PATH = "/data/app/el2"; + +string sandBoxPath[] = {"/data/storage/el1/bundle", + "/data/storage/el2/base", + "/data/storage/el1/database", + "/data/storage/el2/database", + "/data/storage/el1/base", + "/data/storage/el2/distributedfiles", + "/data/storage/el2/auth_groups", + "/data/storage/ark-cache", + "/data/storage/ark-profile" + }; + +std::string srcPath[] = {"/data/app/el1/bundle/public/", + "/data/app/el2//base/", + "/data/app/el1//database/", + "/data/app/el2//database/", + "/data/app/el1//base/", + "/mnt/hmdfs//account/merge_view/data/", + "/mnt/hmdfs//non_account/merge_view/data/", + "/data/local/ark-cache/", + "/data/local/ark-profile//" + }; +} +class FileShare { +public: + FileShare() {} + static int CreateShareFile(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag); + static int DeleteShareFile(const string &uri, Security::AccessToken::AccessTokenID tokenID, int flag); + ~FileShare() {} +}; +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/interfaces/innerkits/file_share/file_share_log.h b/interfaces/innerkits/file_share/file_share_log.h new file mode 100644 index 000000000..683e30355 --- /dev/null +++ b/interfaces/innerkits/file_share/file_share_log.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 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 LOG_H +#define LOG_H +#include "hilog/log.h" + +namespace OHOS { +namespace AppFileService { +const unsigned int APP_LOG_DOMAIN = 0xD004313; +const char APP_LOG_TAG[] = "AppFileService"; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOMAIN, APP_LOG_TAG}; + +#define PRINT_LOG(Level, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Level(OHOS::AppFileService::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ + __FUNCTION__, __LINE__, ##__VA_ARGS__) + +#define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) +#define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) +} // namespace AppFileService +} // namespace OHOS + +#endif // LOG_H \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index ddd3de47a..af6c7c25e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("fileshare") { deps = [ "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", + ] external_deps = [ @@ -84,11 +85,17 @@ ohos_shared_library("fileuri") { ] deps = [ - "//foundation/bundlemanager/bundle_framework/interfaces/kits/native/bundle:bundle_ndk", "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ability_runtime:abilitykit_native", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] relative_install_dir = "module/file" 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 abba57d32..9c152ab22 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -13,18 +13,58 @@ * limitations under the License. */ #include "get_uri_from_path.h" + +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" #include "log.h" -#include "native_interface_bundle.h" +#include "status_receiver_host.h" +#include "system_ability_definition.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; + } + + LOGI("GetBundleMgrProxy: get bundle manager proxy success."); + return iface_cast(remoteObject); +} static string GetBundleName() { - OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); - return nativeApplicationInfo.bundleName; + int uid = -1; + uid = IPCSkeleton::GetCallingUid(); + + sptr bundleMgrProxy = GetBundleMgrProxy(); + if (!bundleMgrProxy) { + LOGE("GetBundleName: bundle mgr proxy is nullptr."); + return nullptr; + } + + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + LOGI("GetBundleName: uid is %{public}d, bundleName is %{public}s", uid, bundleName.c_str()); + return bundleName; } napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index d81880b40..caa8b62e0 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -13,5 +13,8 @@ group("unittest") { testonly = true - deps = [ "remote_file_share:remote_file_share_test" ] + deps = [ + "remote_file_share:remote_file_share_test", + "file_share:file_share_test", + ] } diff --git a/test/unittest/file_share/BUILD.gn b/test/unittest/file_share/BUILD.gn new file mode 100644 index 000000000..fea3578f3 --- /dev/null +++ b/test/unittest/file_share/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (c) 2022 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_share_test") { + module_out_path = "filemanagement/app_file_service" + sources = [ + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share/file_share.cpp", + "file_share_test.cpp", + ] + include_dirs = [ + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + ] + + deps = [ + "//foundation/filemanagement/app_file_service/interfaces/innerkits/file_share:file_share_native", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] +} diff --git a/test/unittest/file_share/file_share_test.cpp b/test/unittest/file_share/file_share_test.cpp new file mode 100644 index 000000000..ecabf86a1 --- /dev/null +++ b/test/unittest/file_share/file_share_test.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 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 +#include +#include +#include +#include +#include +#include +#include "file_share.h" + +namespace { + using namespace std; + using namespace OHOS::AppFileService::ModuleFileShare; + + const int E_OK = 0; + + class FileShareTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: file_share_test_0000 + * @tc.desc: Test function of FileShare() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileShareTest, file_share_FileShare_0000, testing::ext::TestSize.Level1) + { + string uri = "file://com.example.demoa/data/storage/el2/base/files/1.txt"; + unsigned int tokenID = 100; + int flag = 1; + int ret = FileShare::CreateShareFile(uri, tokenID, flag); + EXPECT_EQ(ret, E_OK); + ret = FileShare::DeleteShareFile(uri, tokenID, flag); + EXPECT_EQ(ret, E_OK); + } +} \ No newline at end of file -- Gitee