From c40d176267cd78889c9d9d3a1dbcf5263a2a7aeb Mon Sep 17 00:00:00 2001 From: zkx Date: Fri, 17 Mar 2023 10:17:48 +0800 Subject: [PATCH 01/13] Add Native Interface: GetDataShareUri and GetFileUri Signed-off-by: zkx --- BUILD.gn | 1 - bundle.json | 4 +- interfaces/innerkits/native/file_uri/BUILD.gn | 39 ++++++++ .../native/file_uri/include/file_uri.h | 47 +++++++++ .../native/file_uri/src/file_uri.cpp | 97 +++++++++++++++++++ 5 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 interfaces/innerkits/native/file_uri/BUILD.gn 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/BUILD.gn b/BUILD.gn index c51710244..7fc80e862 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,7 +15,6 @@ import("//build/ohos.gni") group("libremotefileshare") { deps = [ - "interfaces/innerkits/native/file_share:fileshare_native", "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "//foundation/filemanagement/app_file_service/interfaces/kits/js:remotefileshare", ] diff --git a/bundle.json b/bundle.json index fc02fdb76..2867be985 100644 --- a/bundle.json +++ b/bundle.json @@ -28,7 +28,9 @@ "fwk_group": [ "//foundation/filemanagement/app_file_service:libremotefileshare", "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri" + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:file_uri_native" ], "service_group": [ "//foundation/filemanagement/app_file_service:tgt_backup_extension", diff --git a/interfaces/innerkits/native/file_uri/BUILD.gn b/interfaces/innerkits/native/file_uri/BUILD.gn new file mode 100644 index 000000000..d977f3358 --- /dev/null +++ b/interfaces/innerkits/native/file_uri/BUILD.gn @@ -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. + +import("//build/ohos.gni") + +config("file_uri_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "../common", + ] +} + +ohos_shared_library("file_uri_native") { + sources = [ "src/file_uri.cpp" ] + + public_configs = [ ":file_uri_config" ] + + external_deps = [ + #"ability_base:base", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} 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..e2144cb4e --- /dev/null +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -0,0 +1,47 @@ +/* + * 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_SHARE +#define APP_FILE_SERVICE_FILE_SHARE + +#include + +namespace OHOS { +namespace AppFileService { +using namespace std; + +namespace { +const string FILE_SCHEME = "file"; +const string DATA_SHARE_SCHEME = "datashare"; +const string SCHEME_SEPARATOR = '://'; +const string PATH_SYMBOLS = "/"; +const string FRAGMENT_SYMBOLS = "#"; +const string DEVICE_ID_TAG = "deviceId"; +const string REMOTE_URI_TAG = "fdFromBinder"; +const string FD_TAG = "="; + +} + +class FileUri { +public: + FileUri() {} + string GetDataShareUri(const string fileUri); + string GetFileUri(const string dataShareUri); + ~FileUri() {} +}; +} // namespace AppFileService +} // namespace OHOS + +#endif \ 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..7b3adb04a --- /dev/null +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -0,0 +1,97 @@ +/* + * 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 +#include +#include + +#include "accesstoken_kit.h" +#include "hap_token_info.h" +#include "log.h" +#include "uri.h" + +namespace OHOS { +namespace AppFileService { + +static string GetBundleName(int32_t tokenId) +{ + Security::AccessToken::HapTokenInfo hapInfo; + int32_t result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo); + if (result != 0) { + LOGE("Failed to get hap token info %{public}d", result); + return nullptr; + } + return hapInfo.bundleName; +} + +// file://bundleName/media/image/12/[#deviceid=1xxx] datashare://xxx/media/image/12 +// file://bundleName/media/image/12 datashare:///media/image/12 +// file:////#fdFromBinder=12 datashare:////#fdFromBinder=10 +string GetDataShareUri(const string fileUri)//js侧接收后内部转换为datashare格式的uri,再透传 +{ + Uri uri(fileUri); + string dataShareUri = nullptr; + if (uri.GetScheme() != FILE_SCHEME) { + LOGE("Failed to get datashare uri, fileUri is %{public}s", fileUri.c_str()); + return dataShareUri; + } + string::size_type posFragment = fileUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); + if (posFragment > 0) { + string fragment = fileUri.substr(posFragment); + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + PATH_SYMBOLS + fragment; + } else { + string scheme = uri.GetScheme(); + string bundleName = uri.GetAuthority(); + string path = uri.GetPath(); + string deviceId = uri.GetFragment(); + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SEPARATOR + deviceId + PATH_SYMBOLS + path; + } + LOGI("Success to get datashare uri, dataShareUri is %{public}s", dataShareUri.c_str()); + return dataShareUri; +} + +// datashare://deviceid/media/image/12 file://bundleName/media/image/12[#deviceid=xxx] +// datashare:///media/image/12 file://bundlename/media/image/12 +// datashare:////#fdFromBinder=12 file:////#fdFromBinder=12 +string GetFileUri(const string dataShareUri, int32_t tokenId)//内部将datashare格式转换后抛出file格式的uri +{ + Uri uri(dataShareUri); + string scheme = uri.GetScheme(); + if (scheme != DATA_SHARE_SCHEME) { + LOGE("Failed to GetFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); + return nullptr; + } + string fileUri = nullptr; + string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); + if (posFragment > 0) { + string fragment = dataShareUri.substr(posFragment); + fileUri = FILE_SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + PATH_SYMBOLS + fragment; + } else { + string deviceId = uri.GetAuthority(); + string path = uri.GetPath(); + string bundleName = GetBundleName(tokenId); + string fragment = FRAGMENT_SYMBOLS + DEVICE_ID_TAG + FD_TAG + deviceId + fileUri = FILE_SCHEME + SCHEME_SEPARATOR + bundleName + PATH_SYMBOLS + path + PATH_SYMBOLS + fragment; + } + LOGI("Success to GetFileUri, fileUri is %{public}s", fileUri.c_str()); + return fileUri; +} +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file -- Gitee From ddf5b6980e876d8de9aba7e4e9acc65592beb64c Mon Sep 17 00:00:00 2001 From: zkx Date: Fri, 17 Mar 2023 17:55:55 +0800 Subject: [PATCH 02/13] Update GetDataShareUri and GetFileUri interface logic, RemoteUri class migration to FileUri class. Signed-off-by: zkx --- bundle.json | 13 +- interfaces/innerkits/native/file_uri/BUILD.gn | 7 +- .../native/file_uri/include/file_uri.h | 41 ++- .../native/file_uri/src/file_uri.cpp | 243 ++++++++++++++---- .../kits/js/file_uri/get_uri_from_path.cpp | 2 +- .../kits/js/file_uri/get_uri_from_path.h | 5 +- 6 files changed, 242 insertions(+), 69 deletions(-) diff --git a/bundle.json b/bundle.json index 2867be985..75018f8ce 100644 --- a/bundle.json +++ b/bundle.json @@ -28,9 +28,7 @@ "fwk_group": [ "//foundation/filemanagement/app_file_service:libremotefileshare", "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri", - "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", - "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:file_uri_native" + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri" ], "service_group": [ "//foundation/filemanagement/app_file_service:tgt_backup_extension", @@ -49,6 +47,15 @@ "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri: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/remote_file_share/native:remote_file_share_native", "header": { diff --git a/interfaces/innerkits/native/file_uri/BUILD.gn b/interfaces/innerkits/native/file_uri/BUILD.gn index d977f3358..9b085b969 100644 --- a/interfaces/innerkits/native/file_uri/BUILD.gn +++ b/interfaces/innerkits/native/file_uri/BUILD.gn @@ -21,17 +21,20 @@ config("file_uri_config") { ] } -ohos_shared_library("file_uri_native") { +ohos_shared_library("fileuri_native") { sources = [ "src/file_uri.cpp" ] public_configs = [ ":file_uri_config" ] external_deps = [ - #"ability_base:base", "ability_base:zuri", "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", ] part_name = "app_file_service" diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index e2144cb4e..9bd057f00 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -13,34 +13,47 @@ * limitations under the License. */ -#ifndef APP_FILE_SERVICE_FILE_SHARE -#define APP_FILE_SERVICE_FILE_SHARE +#ifndef APP_FILE_SERVICE_FILE_URI_H +#define APP_FILE_SERVICE_FILE_URI_H #include +#include +#include +#include "uri.h" namespace OHOS { namespace AppFileService { +namespace ModuleFileUri { using namespace std; - -namespace { const string FILE_SCHEME = "file"; const string DATA_SHARE_SCHEME = "datashare"; -const string SCHEME_SEPARATOR = '://'; -const string PATH_SYMBOLS = "/"; +const string SCHEME_SYMBOLS = '://'; +const string PATH_SYMBOL = "/"; const string FRAGMENT_SYMBOLS = "#"; -const string DEVICE_ID_TAG = "deviceId"; +const string NETWORK_ID_TAG = "networkid"; +const string EQUAL_SIGN = "="; const string REMOTE_URI_TAG = "fdFromBinder"; -const string FD_TAG = "="; - -} +const string MEDIA = "/media/"; +const string MEDIA_BUNDLE_NAME = "media"; +const int MAX_URI_SIZE = 128; -class FileUri { +class FileUri : public OHOS::Uri { + static setfdFromBinder; + static void RemoveFd(int fd); public: - FileUri() {} - string GetDataShareUri(const string fileUri); - string GetFileUri(const string dataShareUri); + explicit FileUri(const std::string &uri): Uri(uri) {} + static bool IsRemoteUri(const string &path, int &fd, const int& flags = O_RDONLY); + static int ConvertUri(const int &fd, string &remoteUri); + static int OpenRemoteUri(const string &remoteUri); + static bool IsMediaUri(const string &uri); + static bool IsFileUri(const string &uri); + static string GetDataShareUri(const string fileUri); + static string GetFileUri(const string dataShareUri); + static string GetFileUriFromPath(const string path); ~FileUri() {} }; + setFileUri::fdFromBinder; +} // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 7b3adb04a..6aad87ea0 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -15,83 +15,234 @@ #include "file_uri.h" #include -#include #include #include #include #include #include +#include +#include "bundle_mgr_proxy.h" +#include "bundle_info.h" +#include "ipc_skeleton.h" #include "accesstoken_kit.h" #include "hap_token_info.h" #include "log.h" -#include "uri.h" namespace OHOS { namespace AppFileService { +namespace ModuleFileUri { -static string GetBundleName(int32_t tokenId) +bool FileUri::IsMediaUri(const string &uri) { - Security::AccessToken::HapTokenInfo hapInfo; - int32_t result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo); - if (result != 0) { - LOGE("Failed to get hap token info %{public}d", result); - return nullptr; + FileUri fileUri = FileUri(uri); + string scheme = fileUri.GetScheme(); + string path = fileUri.GetPath(); + std::size_t len = MEDIA.length(); + if (path.length() > len) { + string media = path.substr(0, len); + return scheme == DATA_SHARE_SCHEME && media == MEDIA; } - return hapInfo.bundleName; + return false; } -// file://bundleName/media/image/12/[#deviceid=1xxx] datashare://xxx/media/image/12 -// file://bundleName/media/image/12 datashare:///media/image/12 -// file:////#fdFromBinder=12 datashare:////#fdFromBinder=10 -string GetDataShareUri(const string fileUri)//js侧接收后内部转换为datashare格式的uri,再透传 +bool FileUri::IsFileUri(const string &uri) { - Uri uri(fileUri); - string dataShareUri = nullptr; - if (uri.GetScheme() != FILE_SCHEME) { - LOGE("Failed to get datashare uri, fileUri is %{public}s", fileUri.c_str()); - return dataShareUri; + return FileUri(uri).GetScheme() == FILE_SCHEME; +} + +static bool IsAllDigits(string fdStr) +{ + for (size_t i = 0; i < fdStr.size(); i++) { + if (!isdigit(fdStr[i])) { + return false; + } } - string::size_type posFragment = fileUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - if (posFragment > 0) { - string fragment = fileUri.substr(posFragment); - dataShareUri = DATA_SHARE_SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + PATH_SYMBOLS + fragment; - } else { - string scheme = uri.GetScheme(); - string bundleName = uri.GetAuthority(); - string path = uri.GetPath(); - string deviceId = uri.GetFragment(); - dataShareUri = DATA_SHARE_SCHEME + SCHEME_SEPARATOR + deviceId + PATH_SYMBOLS + path; + return true; +} + +static string GetCallingPkgName() +{ + uint32_t pid = IPCSkeleton::GetCallingTokenID(); + Security::AccessToken::HapTokenInfo tokenInfo = Security::AccessToken::HapTokenInfo(); + Security::AccessToken::AccessTokenKit::GetHapTokenInfo(pid, tokenInfo); + return tokenInfo.bundleName; +} + +void FileUri::RemoveFd(int fd) +{ + auto iter = fdFromBinder.find(fd); + if (iter != fdFromBinder.end()) { + fdFromBinder.erase(iter); } - LOGI("Success to get datashare uri, dataShareUri is %{public}s", dataShareUri.c_str()); - return dataShareUri; } -// datashare://deviceid/media/image/12 file://bundleName/media/image/12[#deviceid=xxx] -// datashare:///media/image/12 file://bundlename/media/image/12 -// datashare:////#fdFromBinder=12 file:////#fdFromBinder=12 -string GetFileUri(const string dataShareUri, int32_t tokenId)//内部将datashare格式转换后抛出file格式的uri +bool FileUri::IsRemoteUri(const string& path, int &fd, const int& flags) { - Uri uri(dataShareUri); - string scheme = uri.GetScheme(); + string::size_type posDatashare = path.find(SCHEME_SYMBOLS); + string::size_type posFragment = path.find(FRAGMENT_SYMBOLS); + string::size_type posFd = path.find(EQUAL_SIGN); + if (posDatashare == string::npos || posFragment == string::npos || + posFd == string::npos) { + return false; + } + + string scheme = path.substr(0, posDatashare); if (scheme != DATA_SHARE_SCHEME) { - LOGE("Failed to GetFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); + return false; + } + + string fragment = path.substr(posFragment + 1, REMOTE_URI_TAG.size()); + if (fragment == REMOTE_URI_TAG) { + string fdStr = path.substr(posFd + 1); + if (IsAllDigits(fdStr)) { + fd = stoi(fdStr.c_str()); + if (fd < 0 || flags != O_RDONLY) { + fd = -1; + } + RemoveFd(fd); + return true; + } + fd = -1; + return true; + } + return false; +} + +int FileUri::ConvertUri(const int &fd, string &remoteUri) +{ + if (fd < 0) { + return -EINVAL; + } + + if (fdFromBinder.size() == MAX_URI_SIZE) { + close(*fdFromBinder.begin()); + fdFromBinder.erase(fdFromBinder.begin()); + } + fdFromBinder.emplace(fd); + + string pkgName = GetCallingPkgName(); + remoteUri = DATA_SHARE_SCHEME + ":///" + pkgName + "/" + FRAGMENT_SYMBOLS + + REMOTE_URI_TAG + EQUAL_SIGN + to_string(fd); + return 0; +} + +int FileUri::OpenRemoteUri(const string &remoteUri) +{ + int fd = -1; + (void)IsRemoteUri(remoteUri, fd); + + return fd; +} + +//datashare://deviceid/autuority/path +//datashare://deviceid/media/image/12 + +// file://media/image/12#networkid=deviceid +// file://media/image/12 +// file:////#fdFromBinder=12 +string FileUri::GetFileUri(const string dataShareUri) +{ + FileUri fileUri(dataShareUri); + string scheme = fileUri.GetScheme(); + if (scheme != DATA_SHARE_SCHEME) { + LOGE("Failed to GetFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); return nullptr; } - string fileUri = nullptr; + + string uri = nullptr; + string fragment = nullptr; string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); if (posFragment > 0) { - string fragment = dataShareUri.substr(posFragment); - fileUri = FILE_SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + PATH_SYMBOLS + fragment; + fragment = dataShareUri.substr(posFragment); + uri = FILE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; + } else if (FileUri::IsMediaUri(dataShareUri)) { + string deviceId = fileUri.GetAuthority(); + if (deviceId != nullptr) { + fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId + } + string path = fileUri.GetPath(); + uri = FILE_SCHEME + SCHEME_SYMBOLS + path + PATH_SYMBOL + fragment; } else { - string deviceId = uri.GetAuthority(); - string path = uri.GetPath(); - string bundleName = GetBundleName(tokenId); - string fragment = FRAGMENT_SYMBOLS + DEVICE_ID_TAG + FD_TAG + deviceId - fileUri = FILE_SCHEME + SCHEME_SEPARATOR + bundleName + PATH_SYMBOLS + path + PATH_SYMBOLS + fragment; + LOGE("Failed to GetFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); + return nullptr; } LOGI("Success to GetFileUri, fileUri is %{public}s", fileUri.c_str()); - return fileUri; + return uri; +} + +// file://media/image/12#networkid=deviceid +// file://media/image/12 +// file:////#fdFromBinder=12 + +// datashare://deviceid/media/image/12 +// datashare:///media/image/12 +// datashare:////#fdFromBinder=12 +string FileUri::GetDataShareUri(const string uri) +{ + string dataShareUri = nullptr; + if (!FileUri::IsFileUri(uri)) { + LOGE("Failed to GetDataShareUri, parameter is invalid! parameter = %{public}s", uri.c_str()); + return nullptr; + } + string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); + if (posFragment > 0) { + string fragment = uri.substr(posFragment); + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; + } else { + FileUri fileUri(uri); + string bundleName = fileUri.GetAuthority(); + string path = fileUri.GetPath(); + string deviceId = fileUri.GetFragment(); + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + deviceId + PATH_SYMBOL + bundleName + PATH_SYMBOL + path; + } + LOGI("Success to GetDataShareUri, dataShareUri is %{public}s", dataShareUri.c_str()); + return dataShareUri; +} + +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; + } + + string bundleName; + if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); + return nullptr; + } + + return bundleName; +} + +string FileUri::GetFileUriFromPath(const string path) +{ + string bundleName = GetBundleName(); + string uri = FILE_SCHEME + SCHEME_SYMBOLS + bundleName + path; } +} // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS \ No newline at end of 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 30c77385a..ef25da906 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.cpp +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -80,7 +80,7 @@ napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) return nullptr; } string packageName = GetBundleName(); - string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + path.get(); + string uri = FILE_SCHEME + SCHEME_SYMBOLS + packageName + 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 7dbea01cd..b335c096c 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -24,9 +24,8 @@ namespace AppFileService { namespace ModuleFileUri { using namespace std; -const string SCHEME = "file"; -const char SCHEME_SEPARATOR = ':'; -const string PATH_SYMBOLS = "//"; +const string FILE_SCHEME = "file"; +const char SCHEME_SYMBOLS = '://'; const string FRAGMENT_SYMBOLS = "#"; class GetUriFromPath final { -- Gitee From bcdae8ceaa5c2631f199de94ae445b52646b02b1 Mon Sep 17 00:00:00 2001 From: zkx Date: Mon, 20 Mar 2023 14:50:46 +0800 Subject: [PATCH 03/13] update interface name. Signed-off-by: zkx --- .../native/file_uri/include/file_uri.h | 6 +++--- .../innerkits/native/file_uri/src/file_uri.cpp | 18 +++--------------- .../kits/js/file_uri/get_uri_from_path.h | 2 +- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 9bd057f00..a83d83fc1 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -27,7 +27,7 @@ namespace ModuleFileUri { using namespace std; const string FILE_SCHEME = "file"; const string DATA_SHARE_SCHEME = "datashare"; -const string SCHEME_SYMBOLS = '://'; +const string SCHEME_SYMBOLS = "://"; const string PATH_SYMBOL = "/"; const string FRAGMENT_SYMBOLS = "#"; const string NETWORK_ID_TAG = "networkid"; @@ -47,8 +47,8 @@ public: static int OpenRemoteUri(const string &remoteUri); static bool IsMediaUri(const string &uri); static bool IsFileUri(const string &uri); - static string GetDataShareUri(const string fileUri); - static string GetFileUri(const string dataShareUri); + static string TransToDataShareUri(const string fileUri); + static string TransToFileUri(const string dataShareUri); static string GetFileUriFromPath(const string path); ~FileUri() {} }; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 6aad87ea0..7e1a21f14 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -135,13 +135,7 @@ int FileUri::OpenRemoteUri(const string &remoteUri) return fd; } -//datashare://deviceid/autuority/path -//datashare://deviceid/media/image/12 - -// file://media/image/12#networkid=deviceid -// file://media/image/12 -// file:////#fdFromBinder=12 -string FileUri::GetFileUri(const string dataShareUri) +string FileUri::TransToFileUri(const string dataShareUri) { FileUri fileUri(dataShareUri); string scheme = fileUri.GetScheme(); @@ -171,14 +165,7 @@ string FileUri::GetFileUri(const string dataShareUri) return uri; } -// file://media/image/12#networkid=deviceid -// file://media/image/12 -// file:////#fdFromBinder=12 - -// datashare://deviceid/media/image/12 -// datashare:///media/image/12 -// datashare:////#fdFromBinder=12 -string FileUri::GetDataShareUri(const string uri) +string FileUri::TransToDataShareUri(const string uri) { string dataShareUri = nullptr; if (!FileUri::IsFileUri(uri)) { @@ -242,6 +229,7 @@ string FileUri::GetFileUriFromPath(const string path) { string bundleName = GetBundleName(); string uri = FILE_SCHEME + SCHEME_SYMBOLS + bundleName + path; + return uri; } } // namespace ModuleFileUri } // namespace AppFileService 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 b335c096c..674ee5dc6 100644 --- a/interfaces/kits/js/file_uri/get_uri_from_path.h +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -25,7 +25,7 @@ namespace ModuleFileUri { using namespace std; const string FILE_SCHEME = "file"; -const char SCHEME_SYMBOLS = '://'; +const string SCHEME_SYMBOLS = "://"; const string FRAGMENT_SYMBOLS = "#"; class GetUriFromPath final { -- Gitee From 90b1740cc47f5f7fcb015b984d421004d0283d5f Mon Sep 17 00:00:00 2001 From: zkx Date: Mon, 20 Mar 2023 16:44:35 +0800 Subject: [PATCH 04/13] =?UTF-8?q?fileuri=5Fnative.z.so=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zkx --- BUILD.gn | 2 ++ .../innerkits/native/file_uri/src/file_uri.cpp | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 7fc80e862..d03eee0e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,6 +17,8 @@ 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/native/file_share:fileshare_native", + "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:fileuri_native", ] } diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 7e1a21f14..a2b9c09f8 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -28,10 +28,14 @@ #include "accesstoken_kit.h" #include "hap_token_info.h" #include "log.h" +#include "iservice_registry.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { +using namespace OHOS::AppExecFwk; bool FileUri::IsMediaUri(const string &uri) { @@ -140,7 +144,7 @@ string FileUri::TransToFileUri(const string dataShareUri) FileUri fileUri(dataShareUri); string scheme = fileUri.GetScheme(); if (scheme != DATA_SHARE_SCHEME) { - LOGE("Failed to GetFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); + LOGE("Failed to TransToFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); return nullptr; } @@ -152,16 +156,16 @@ string FileUri::TransToFileUri(const string dataShareUri) uri = FILE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; } else if (FileUri::IsMediaUri(dataShareUri)) { string deviceId = fileUri.GetAuthority(); - if (deviceId != nullptr) { - fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId + if (!deviceId.empty()) { + fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId; } string path = fileUri.GetPath(); uri = FILE_SCHEME + SCHEME_SYMBOLS + path + PATH_SYMBOL + fragment; } else { - LOGE("Failed to GetFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); + LOGE("Failed to TransToFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); return nullptr; } - LOGI("Success to GetFileUri, fileUri is %{public}s", fileUri.c_str()); + LOGI("Success to TransToFileUri, fileUri is %{public}s", uri.c_str()); return uri; } @@ -169,7 +173,7 @@ string FileUri::TransToDataShareUri(const string uri) { string dataShareUri = nullptr; if (!FileUri::IsFileUri(uri)) { - LOGE("Failed to GetDataShareUri, parameter is invalid! parameter = %{public}s", uri.c_str()); + LOGE("Failed to TransToDataShareUri, parameter is invalid! parameter = %{public}s", uri.c_str()); return nullptr; } string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); @@ -183,7 +187,7 @@ string FileUri::TransToDataShareUri(const string uri) string deviceId = fileUri.GetFragment(); dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + deviceId + PATH_SYMBOL + bundleName + PATH_SYMBOL + path; } - LOGI("Success to GetDataShareUri, dataShareUri is %{public}s", dataShareUri.c_str()); + LOGI("Success to TransToDataShareUri, dataShareUri is %{public}s", dataShareUri.c_str()); return dataShareUri; } -- Gitee From 05c9bdded17e19e2818d90c6cd2d11b7253ec9db Mon Sep 17 00:00:00 2001 From: zkx Date: Tue, 21 Mar 2023 10:12:43 +0800 Subject: [PATCH 05/13] add file uri native interface tdd test. Signed-off-by: zkx --- bundle.json | 34 +++++----- test/unittest/BUILD.gn | 1 + test/unittest/file_uri_native/BUILD.gn | 25 ++++++++ .../file_uri_native/file_uri_test.cpp | 63 +++++++++++++++++++ 4 files changed, 106 insertions(+), 17 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/bundle.json b/bundle.json index 75018f8ce..5a5644f9c 100644 --- a/bundle.json +++ b/bundle.json @@ -41,28 +41,19 @@ { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share:fileshare_native", "header": { - "header_files": [ - "file_share.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" - } - }, - { - "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:fileuri_native", - "header": { - "header_files": [ - "file_uri.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" + "header_files": [ + "file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_share/include" } }, { "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native:remote_file_share_native", "header": { - "header_files": [ - "remote_file_share.h" - ], - "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" + "header_files": [ + "remote_file_share.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/remote_file_share/native" } }, { @@ -79,6 +70,15 @@ "impl/i_service_reverse.h" ] } + }, + { + "name": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:fileuri_native", + "header": { + "header_files": [ + "file_uri.h" + ], + "header_base": "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri/include" + } } ], "test": [ 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..fb297cd22 --- /dev/null +++ b/test/unittest/file_uri_native/BUILD.gn @@ -0,0 +1,25 @@ +# 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 = [ + "app_file_service:fileuri_native", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] +} 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..a86e17369 --- /dev/null +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -0,0 +1,63 @@ +/* + * 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 +#include +#include +#include +#include + +#include "file_uri.h" + +namespace { + using namespace std; + using namespace OHOS::AppFileService; + + const int E_OK = 0; + const int E_INVALID_ARGUMENT = 12100002; + + 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 TransToFileUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0000"; + + string dataShareUri = "datashare://1002/media/image/12"; + string resultUri = "file://media/image/12#networkid=1002"; + string uri = FileUri::TransToFileUri(uri, tokenId, flag); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000 uri = " << uri; + // int32_t ret = -1; + // if (uri == resultUri) { + // ret = E_OK; + // } + // EXPECT_EQ(ret, E_OK); + EXPECT_EQ(uri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000"; + } +} \ No newline at end of file -- Gitee From 0a63186753b553f4c511c4e763d1590d713f9bd5 Mon Sep 17 00:00:00 2001 From: zkx Date: Tue, 21 Mar 2023 12:58:40 +0800 Subject: [PATCH 06/13] update build.gn Signed-off-by: zkx --- BUILD.gn | 17 ++++++++++++----- bundle.json | 5 ++--- test/unittest/file_uri_native/BUILD.gn | 6 ++++-- test/unittest/file_uri_native/file_uri_test.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index d03eee0e5..a40287b7c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -13,12 +13,19 @@ import("//build/ohos.gni") -group("libremotefileshare") { +group("file_share_js_sdk") { 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/native/file_share:fileshare_native", - "//foundation/filemanagement/app_file_service/interfaces/innerkits/native/file_uri:fileuri_native", + "interfaces/kits/js:remotefileshare", + "interfaces/kits/js:fileshare", + "interfaces/kits/js:fileuri", + ] +} + +group("file_share_native_sdk") { + deps = [ + "interfaces/innerkits/remote_file_share/native:remote_file_share_native", + "interfaces/innerkits/native/file_share:fileshare_native", + "interfaces/innerkits/native/file_uri:fileuri_native", ] } diff --git a/bundle.json b/bundle.json index 5a5644f9c..85e9af7f9 100644 --- a/bundle.json +++ b/bundle.json @@ -26,9 +26,8 @@ "group_type": { "base_group": [], "fwk_group": [ - "//foundation/filemanagement/app_file_service:libremotefileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri" + "//foundation/filemanagement/app_file_service:file_share_js_sdk", + "//foundation/filemanagement/app_file_service:file_share_native_sdk" ], "service_group": [ "//foundation/filemanagement/app_file_service:tgt_backup_extension", diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn index fb297cd22..c0799c767 100644 --- a/test/unittest/file_uri_native/BUILD.gn +++ b/test/unittest/file_uri_native/BUILD.gn @@ -16,9 +16,11 @@ import("//build/test.gni") ohos_unittest("file_uri_test") { module_out_path = "filemanagement/app_file_service" sources = [ "file_uri_test.cpp" ] - + deps = [ + "../../../interfaces/innerkits/native/file_uri:fileuri_native", + "//third_party/googletest:gtest_main", + ] external_deps = [ - "app_file_service:fileuri_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", ] diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index a86e17369..47cb3d75d 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -23,7 +23,7 @@ namespace { using namespace std; - using namespace OHOS::AppFileService; + using namespace OHOS::AppFileService::ModuleFileUri; const int E_OK = 0; const int E_INVALID_ARGUMENT = 12100002; @@ -50,14 +50,14 @@ namespace { string dataShareUri = "datashare://1002/media/image/12"; string resultUri = "file://media/image/12#networkid=1002"; - string uri = FileUri::TransToFileUri(uri, tokenId, flag); - GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000 uri = " << uri; + string fileuri = FileUri::TransToFileUri(dataShareUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000 fileuri = " << fileuri; // int32_t ret = -1; // if (uri == resultUri) { // ret = E_OK; // } // EXPECT_EQ(ret, E_OK); - EXPECT_EQ(uri, resultUri); + EXPECT_EQ(fileuri, resultUri); GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000"; } } \ No newline at end of file -- Gitee From e928e37aa87b2cce7b78634eb8d1f26041d6059e Mon Sep 17 00:00:00 2001 From: zkx Date: Tue, 21 Mar 2023 14:32:25 +0800 Subject: [PATCH 07/13] format gn file. Signed-off-by: zkx --- BUILD.gn | 4 ++-- test/unittest/BUILD.gn | 2 +- test/unittest/file_uri_native/BUILD.gn | 3 ++- test/unittest/file_uri_native/file_uri_test.cpp | 3 --- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index a40287b7c..0ea2c036b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,17 +15,17 @@ import("//build/ohos.gni") group("file_share_js_sdk") { deps = [ - "interfaces/kits/js:remotefileshare", "interfaces/kits/js:fileshare", "interfaces/kits/js:fileuri", + "interfaces/kits/js:remotefileshare", ] } group("file_share_native_sdk") { deps = [ - "interfaces/innerkits/remote_file_share/native:remote_file_share_native", "interfaces/innerkits/native/file_share:fileshare_native", "interfaces/innerkits/native/file_uri:fileuri_native", + "interfaces/innerkits/remote_file_share/native:remote_file_share_native", ] } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 20930d644..53d8db6d1 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -15,7 +15,7 @@ group("unittest") { testonly = true deps = [ "file_share_native:file_share_test", - "remote_file_share:remote_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 index c0799c767..3dc6e1c66 100644 --- a/test/unittest/file_uri_native/BUILD.gn +++ b/test/unittest/file_uri_native/BUILD.gn @@ -17,10 +17,11 @@ ohos_unittest("file_uri_test") { module_out_path = "filemanagement/app_file_service" sources = [ "file_uri_test.cpp" ] deps = [ - "../../../interfaces/innerkits/native/file_uri:fileuri_native", + # "../../../interfaces/innerkits/native/file_uri:fileuri_native", "//third_party/googletest:gtest_main", ] external_deps = [ + "app_file_service:fileuri_native", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", ] diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index 47cb3d75d..4196af2fe 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -25,9 +25,6 @@ namespace { using namespace std; using namespace OHOS::AppFileService::ModuleFileUri; - const int E_OK = 0; - const int E_INVALID_ARGUMENT = 12100002; - class FileUriTest : public testing::Test { public: static void SetUpTestCase(void) {}; -- Gitee From 0388996702197bb63a4e25a412b39904cb3b6606 Mon Sep 17 00:00:00 2001 From: zkx Date: Tue, 21 Mar 2023 16:09:17 +0800 Subject: [PATCH 08/13] add log. Signed-off-by: zkx --- interfaces/innerkits/native/file_uri/src/file_uri.cpp | 7 ++++++- test/unittest/file_uri_native/file_uri_test.cpp | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index a2b9c09f8..0b217433d 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -143,23 +143,28 @@ string FileUri::TransToFileUri(const string dataShareUri) { FileUri fileUri(dataShareUri); string scheme = fileUri.GetScheme(); + LOGI("1------------scheme is %{public}s", scheme.c_str()); if (scheme != DATA_SHARE_SCHEME) { LOGE("Failed to TransToFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); return nullptr; } - string uri = nullptr; string fragment = nullptr; string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); + LOGI("2------------posFragment is %{public}d", posFragment); if (posFragment > 0) { fragment = dataShareUri.substr(posFragment); + LOGI("3------------fragment is %{public}s", fragment.c_str()); uri = FILE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; } else if (FileUri::IsMediaUri(dataShareUri)) { string deviceId = fileUri.GetAuthority(); + LOGI("4------------deviceId is %{public}s", deviceId.c_str()); if (!deviceId.empty()) { fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId; } + LOGI("5------------fragment is %{public}s", fragment.c_str()); string path = fileUri.GetPath(); + LOGI("6------------path is %{public}s", path.c_str()); uri = FILE_SCHEME + SCHEME_SYMBOLS + path + PATH_SYMBOL + fragment; } else { LOGE("Failed to TransToFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index 4196af2fe..a81907138 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -48,13 +48,14 @@ namespace { string dataShareUri = "datashare://1002/media/image/12"; string resultUri = "file://media/image/12#networkid=1002"; string fileuri = FileUri::TransToFileUri(dataShareUri); - GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000 fileuri = " << fileuri; + ASSERT_TRUE(!fileuri.empty()) << "fileuri is nullptr!"; + EXPECT_EQ(fileuri, resultUri); // int32_t ret = -1; // if (uri == resultUri) { // ret = E_OK; // } // EXPECT_EQ(ret, E_OK); - EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000"; } } \ No newline at end of file -- Gitee From e8c53df219b597c36fef6e97961599ed9b515a4e Mon Sep 17 00:00:00 2001 From: zkx Date: Tue, 21 Mar 2023 17:47:42 +0800 Subject: [PATCH 09/13] bug fix Signed-off-by: zkx --- interfaces/innerkits/native/file_uri/src/file_uri.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 0b217433d..bd2d0217c 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -152,7 +152,7 @@ string FileUri::TransToFileUri(const string dataShareUri) string fragment = nullptr; string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); LOGI("2------------posFragment is %{public}d", posFragment); - if (posFragment > 0) { + if (posFragment != string::npos) { fragment = dataShareUri.substr(posFragment); LOGI("3------------fragment is %{public}s", fragment.c_str()); uri = FILE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; @@ -182,7 +182,7 @@ string FileUri::TransToDataShareUri(const string uri) return nullptr; } string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - if (posFragment > 0) { + if (posFragment != string::npos) { string fragment = uri.substr(posFragment); dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; } else { -- Gitee From 687f5a2cc5bea76f555a8551d190e43c4759dd0f Mon Sep 17 00:00:00 2001 From: zkx Date: Wed, 22 Mar 2023 18:53:53 +0800 Subject: [PATCH 10/13] update tdd test moudle. Signed-off-by: zkx --- .../native/file_uri/include/file_uri.h | 2 +- .../native/file_uri/src/file_uri.cpp | 58 ++++++---- test/unittest/file_uri_native/BUILD.gn | 1 - .../file_uri_native/file_uri_test.cpp | 106 ++++++++++++++++-- 4 files changed, 133 insertions(+), 34 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index a83d83fc1..2fec1651c 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -27,7 +27,7 @@ namespace ModuleFileUri { using namespace std; const string FILE_SCHEME = "file"; const string DATA_SHARE_SCHEME = "datashare"; -const string SCHEME_SYMBOLS = "://"; +const string SCHEME_SYMBOL = ":"; const string PATH_SYMBOL = "/"; const string FRAGMENT_SYMBOLS = "#"; const string NETWORK_ID_TAG = "networkid"; diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index bd2d0217c..bd3a20944 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -19,16 +19,16 @@ #include #include #include -#include #include +#include -#include "bundle_mgr_proxy.h" -#include "bundle_info.h" -#include "ipc_skeleton.h" #include "accesstoken_kit.h" +#include "bundle_info.h" +#include "bundle_mgr_proxy.h" #include "hap_token_info.h" -#include "log.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" +#include "log.h" #include "status_receiver_host.h" #include "system_ability_definition.h" @@ -83,7 +83,7 @@ void FileUri::RemoveFd(int fd) bool FileUri::IsRemoteUri(const string& path, int &fd, const int& flags) { - string::size_type posDatashare = path.find(SCHEME_SYMBOLS); + string::size_type posDatashare = path.find(SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL); string::size_type posFragment = path.find(FRAGMENT_SYMBOLS); string::size_type posFd = path.find(EQUAL_SIGN); if (posDatashare == string::npos || posFragment == string::npos || @@ -146,16 +146,16 @@ string FileUri::TransToFileUri(const string dataShareUri) LOGI("1------------scheme is %{public}s", scheme.c_str()); if (scheme != DATA_SHARE_SCHEME) { LOGE("Failed to TransToFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); - return nullptr; + return ""; } - string uri = nullptr; - string fragment = nullptr; + string uri; + string fragment; string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); LOGI("2------------posFragment is %{public}d", posFragment); if (posFragment != string::npos) { fragment = dataShareUri.substr(posFragment); LOGI("3------------fragment is %{public}s", fragment.c_str()); - uri = FILE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; + uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; } else if (FileUri::IsMediaUri(dataShareUri)) { string deviceId = fileUri.GetAuthority(); LOGI("4------------deviceId is %{public}s", deviceId.c_str()); @@ -165,10 +165,10 @@ string FileUri::TransToFileUri(const string dataShareUri) LOGI("5------------fragment is %{public}s", fragment.c_str()); string path = fileUri.GetPath(); LOGI("6------------path is %{public}s", path.c_str()); - uri = FILE_SCHEME + SCHEME_SYMBOLS + path + PATH_SYMBOL + fragment; + uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + path + fragment; } else { LOGE("Failed to TransToFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); - return nullptr; + return ""; } LOGI("Success to TransToFileUri, fileUri is %{public}s", uri.c_str()); return uri; @@ -176,21 +176,32 @@ string FileUri::TransToFileUri(const string dataShareUri) string FileUri::TransToDataShareUri(const string uri) { - string dataShareUri = nullptr; if (!FileUri::IsFileUri(uri)) { LOGE("Failed to TransToDataShareUri, parameter is invalid! parameter = %{public}s", uri.c_str()); - return nullptr; + return ""; } + string dataShareUri; string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); + LOGI("7------------posFragment is %{public}d", posFragment); if (posFragment != string::npos) { string fragment = uri.substr(posFragment); - dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + PATH_SYMBOL + PATH_SYMBOL + fragment; + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; } else { FileUri fileUri(uri); string bundleName = fileUri.GetAuthority(); string path = fileUri.GetPath(); - string deviceId = fileUri.GetFragment(); - dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOLS + deviceId + PATH_SYMBOL + bundleName + PATH_SYMBOL + path; + string fragment = fileUri.GetFragment(); + LOGI("8------------bundleName is %{public}s", bundleName.c_str()); + LOGI("9------------path is %{public}s", path.c_str()); + LOGI("10------------fragment is %{public}s", fragment.c_str()); + string deviceId; + if (!fragment.empty()) { + posFragment = fragment.find(EQUAL_SIGN); + LOGI("11------------posFragment is %{public}d", posFragment); + deviceId = fragment.substr(posFragment + 1); + LOGI("12------------deviceId is %{public}s", deviceId.c_str()); + } + dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + deviceId + PATH_SYMBOL + bundleName + path; } LOGI("Success to TransToDataShareUri, dataShareUri is %{public}s", dataShareUri.c_str()); return dataShareUri; @@ -222,22 +233,23 @@ static string GetBundleName() sptr bundleMgrProxy = GetBundleMgrProxy(); if (!bundleMgrProxy) { LOGE("GetBundleName: bundle mgr proxy is nullptr."); - return nullptr; + return ""; } - string bundleName; - if (!bundleMgrProxy->GetBundleNameForUid(uid, bundleName)) { + BundleInfo bundleInfo; + auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo); + if (ret != 0) { LOGE("GetBundleName: bundleName get fail. uid is %{public}d", uid); - return nullptr; + return ""; } - return bundleName; + return bundleInfo.name; } string FileUri::GetFileUriFromPath(const string path) { string bundleName = GetBundleName(); - string uri = FILE_SCHEME + SCHEME_SYMBOLS + bundleName + path; + string uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + bundleName + path; return uri; } } // namespace ModuleFileUri diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn index 3dc6e1c66..a3da843e3 100644 --- a/test/unittest/file_uri_native/BUILD.gn +++ b/test/unittest/file_uri_native/BUILD.gn @@ -17,7 +17,6 @@ ohos_unittest("file_uri_test") { module_out_path = "filemanagement/app_file_service" sources = [ "file_uri_test.cpp" ] deps = [ - # "../../../interfaces/innerkits/native/file_uri:fileuri_native", "//third_party/googletest:gtest_main", ] external_deps = [ diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index a81907138..dd6a847b9 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -34,7 +34,7 @@ namespace { }; /** - * @tc.name: File_Uri_test_0000 + * @tc.name: File_Uri_TransToFileUri_0000 * @tc.desc: Test function of TransToFileUri() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC @@ -44,18 +44,106 @@ namespace { HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0000, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0000"; - string dataShareUri = "datashare://1002/media/image/12"; string resultUri = "file://media/image/12#networkid=1002"; string fileuri = FileUri::TransToFileUri(dataShareUri); - ASSERT_TRUE(!fileuri.empty()) << "fileuri is nullptr!"; + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; EXPECT_EQ(fileuri, resultUri); - // int32_t ret = -1; - // if (uri == resultUri) { - // ret = E_OK; - // } - // EXPECT_EQ(ret, E_OK); - GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000"; } + + /** + * @tc.name: File_Uri_TransToFileUri_0001 + * @tc.desc: Test function of TransToFileUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0001"; + string dataShareUri = "datashare:///media/image/12"; + string resultUri = "file://media/image/12"; + string fileuri = FileUri::TransToFileUri(dataShareUri); + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; + EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0001"; + } + + /** + * @tc.name: File_Uri_TransToFileUri_0002 + * @tc.desc: Test function of TransToFileUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0002"; + string dataShareUri = "datashare:////#fdFromBinder=12"; + string resultUri = "file:////#fdFromBinder=12"; + string fileuri = FileUri::TransToFileUri(dataShareUri); + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; + EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0002"; + } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0003 + * @tc.desc: Test function of TransToDataShareUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0003"; + string fileuri = "file://media/image/12#networkid=1002"; + string resultUri = "datashare://1002/media/image/12"; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + ASSERT_TRUE(!dataShareUri.empty()) << "dataShareUri is empty!"; + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0003"; + } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0004 + * @tc.desc: Test function of TransToDataShareUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0004"; + string fileuri = "file://media/image/12"; + string resultUri = "datashare:///media/image/12"; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + ASSERT_TRUE(!dataShareUri.empty()) << "dataShareUri is empty!"; + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0004"; + } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0005 + * @tc.desc: Test function of TransToDataShareUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0005"; + string fileuri = "file:////#fdFromBinder=12"; + string resultUri = "datashare:////#fdFromBinder=12"; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + ASSERT_TRUE(!dataShareUri.empty()) << "dataShareUri is empty!"; + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0005"; + } } \ No newline at end of file -- Gitee From 657279d7df39d360483f6218794e24ef375c5678 Mon Sep 17 00:00:00 2001 From: zkx Date: Wed, 22 Mar 2023 19:05:12 +0800 Subject: [PATCH 11/13] delete log. Signed-off-by: zkx --- .../innerkits/native/file_uri/src/file_uri.cpp | 12 ------------ test/unittest/file_uri_native/file_uri_test.cpp | 6 +++--- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index bd3a20944..7ee9a3614 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -143,7 +143,6 @@ string FileUri::TransToFileUri(const string dataShareUri) { FileUri fileUri(dataShareUri); string scheme = fileUri.GetScheme(); - LOGI("1------------scheme is %{public}s", scheme.c_str()); if (scheme != DATA_SHARE_SCHEME) { LOGE("Failed to TransToFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); return ""; @@ -151,20 +150,15 @@ string FileUri::TransToFileUri(const string dataShareUri) string uri; string fragment; string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - LOGI("2------------posFragment is %{public}d", posFragment); if (posFragment != string::npos) { fragment = dataShareUri.substr(posFragment); - LOGI("3------------fragment is %{public}s", fragment.c_str()); uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; } else if (FileUri::IsMediaUri(dataShareUri)) { string deviceId = fileUri.GetAuthority(); - LOGI("4------------deviceId is %{public}s", deviceId.c_str()); if (!deviceId.empty()) { fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId; } - LOGI("5------------fragment is %{public}s", fragment.c_str()); string path = fileUri.GetPath(); - LOGI("6------------path is %{public}s", path.c_str()); uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + path + fragment; } else { LOGE("Failed to TransToFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); @@ -182,7 +176,6 @@ string FileUri::TransToDataShareUri(const string uri) } string dataShareUri; string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - LOGI("7------------posFragment is %{public}d", posFragment); if (posFragment != string::npos) { string fragment = uri.substr(posFragment); dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; @@ -191,15 +184,10 @@ string FileUri::TransToDataShareUri(const string uri) string bundleName = fileUri.GetAuthority(); string path = fileUri.GetPath(); string fragment = fileUri.GetFragment(); - LOGI("8------------bundleName is %{public}s", bundleName.c_str()); - LOGI("9------------path is %{public}s", path.c_str()); - LOGI("10------------fragment is %{public}s", fragment.c_str()); string deviceId; if (!fragment.empty()) { posFragment = fragment.find(EQUAL_SIGN); - LOGI("11------------posFragment is %{public}d", posFragment); deviceId = fragment.substr(posFragment + 1); - LOGI("12------------deviceId is %{public}s", deviceId.c_str()); } dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + deviceId + PATH_SYMBOL + bundleName + path; } diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index dd6a847b9..3378be93e 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -47,7 +47,7 @@ namespace { string dataShareUri = "datashare://1002/media/image/12"; string resultUri = "file://media/image/12#networkid=1002"; string fileuri = FileUri::TransToFileUri(dataShareUri); - ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty!"; EXPECT_EQ(fileuri, resultUri); GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000"; } @@ -66,7 +66,7 @@ namespace { string dataShareUri = "datashare:///media/image/12"; string resultUri = "file://media/image/12"; string fileuri = FileUri::TransToFileUri(dataShareUri); - ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty!"; EXPECT_EQ(fileuri, resultUri); GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0001"; } @@ -85,7 +85,7 @@ namespace { string dataShareUri = "datashare:////#fdFromBinder=12"; string resultUri = "file:////#fdFromBinder=12"; string fileuri = FileUri::TransToFileUri(dataShareUri); - ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty !"; + ASSERT_TRUE(!fileuri.empty()) << "fileuri is empty!"; EXPECT_EQ(fileuri, resultUri); GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0002"; } -- Gitee From ce9f1de8a37991b103f7150158065623c6917a23 Mon Sep 17 00:00:00 2001 From: zkx Date: Wed, 22 Mar 2023 19:20:18 +0800 Subject: [PATCH 12/13] gn format Signed-off-by: zkx --- test/unittest/file_uri_native/BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/unittest/file_uri_native/BUILD.gn b/test/unittest/file_uri_native/BUILD.gn index a3da843e3..0114f6eb2 100644 --- a/test/unittest/file_uri_native/BUILD.gn +++ b/test/unittest/file_uri_native/BUILD.gn @@ -16,9 +16,7 @@ import("//build/test.gni") ohos_unittest("file_uri_test") { module_out_path = "filemanagement/app_file_service" sources = [ "file_uri_test.cpp" ] - deps = [ - "//third_party/googletest:gtest_main", - ] + deps = [ "//third_party/googletest:gtest_main" ] external_deps = [ "app_file_service:fileuri_native", "c_utils:utils", -- Gitee From 5126c5786e377ead111800a2be8a97707a92d523 Mon Sep 17 00:00:00 2001 From: zkx Date: Thu, 23 Mar 2023 15:47:17 +0800 Subject: [PATCH 13/13] add tdd test module, update interfaces logic. Signed-off-by: zkx --- .../native/file_uri/include/file_uri.h | 2 +- .../native/file_uri/src/file_uri.cpp | 79 +++++++------ .../file_uri_native/file_uri_test.cpp | 108 ++++++++++++++++++ 3 files changed, 155 insertions(+), 34 deletions(-) diff --git a/interfaces/innerkits/native/file_uri/include/file_uri.h b/interfaces/innerkits/native/file_uri/include/file_uri.h index 2fec1651c..9ade1879a 100644 --- a/interfaces/innerkits/native/file_uri/include/file_uri.h +++ b/interfaces/innerkits/native/file_uri/include/file_uri.h @@ -42,6 +42,7 @@ class FileUri : public OHOS::Uri { static void RemoveFd(int fd); public: explicit FileUri(const std::string &uri): Uri(uri) {} + static bool IsRemoteUri(const string &uri); static bool IsRemoteUri(const string &path, int &fd, const int& flags = O_RDONLY); static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); @@ -52,7 +53,6 @@ public: static string GetFileUriFromPath(const string path); ~FileUri() {} }; - setFileUri::fdFromBinder; } // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/innerkits/native/file_uri/src/file_uri.cpp b/interfaces/innerkits/native/file_uri/src/file_uri.cpp index 7ee9a3614..1a3b73b98 100644 --- a/interfaces/innerkits/native/file_uri/src/file_uri.cpp +++ b/interfaces/innerkits/native/file_uri/src/file_uri.cpp @@ -37,22 +37,44 @@ namespace AppFileService { namespace ModuleFileUri { using namespace OHOS::AppExecFwk; +setFileUri::fdFromBinder; + +static bool IsUri(const string &uri) +{ + string::size_type posSymbol = uri.find(SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL); + return posSymbol != string::npos; +} + +bool FileUri::IsRemoteUri(const string& uri) +{ + if (IsUri(uri)) { + string scheme = FileUri(uri).GetScheme(); + if (scheme == DATA_SHARE_SCHEME || scheme == FILE_SCHEME) { + string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG + EQUAL_SIGN); + return posFragment != string::npos; + } + } + return false; +} + bool FileUri::IsMediaUri(const string &uri) { - FileUri fileUri = FileUri(uri); - string scheme = fileUri.GetScheme(); - string path = fileUri.GetPath(); - std::size_t len = MEDIA.length(); - if (path.length() > len) { - string media = path.substr(0, len); - return scheme == DATA_SHARE_SCHEME && media == MEDIA; + if (IsUri(uri)) { + FileUri fileUri = FileUri(uri); + string scheme = fileUri.GetScheme(); + string path = fileUri.GetPath(); + std::size_t len = MEDIA.length(); + if (path.length() > len) { + string media = path.substr(0, len); + return scheme == DATA_SHARE_SCHEME && media == MEDIA; + } } return false; } bool FileUri::IsFileUri(const string &uri) { - return FileUri(uri).GetScheme() == FILE_SCHEME; + return IsUri(uri) && FileUri(uri).GetScheme() == FILE_SCHEME; } static bool IsAllDigits(string fdStr) @@ -83,19 +105,11 @@ void FileUri::RemoveFd(int fd) bool FileUri::IsRemoteUri(const string& path, int &fd, const int& flags) { - string::size_type posDatashare = path.find(SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL); - string::size_type posFragment = path.find(FRAGMENT_SYMBOLS); - string::size_type posFd = path.find(EQUAL_SIGN); - if (posDatashare == string::npos || posFragment == string::npos || - posFd == string::npos) { - return false; - } - - string scheme = path.substr(0, posDatashare); - if (scheme != DATA_SHARE_SCHEME) { + if (!IsRemoteUri(path)) { return false; } - + string::size_type posFd = path.find(EQUAL_SIGN); + string::size_type posFragment = path.find(FRAGMENT_SYMBOLS); string fragment = path.substr(posFragment + 1, REMOTE_URI_TAG.size()); if (fragment == REMOTE_URI_TAG) { string fdStr = path.substr(posFd + 1); @@ -142,24 +156,19 @@ int FileUri::OpenRemoteUri(const string &remoteUri) string FileUri::TransToFileUri(const string dataShareUri) { FileUri fileUri(dataShareUri); - string scheme = fileUri.GetScheme(); - if (scheme != DATA_SHARE_SCHEME) { - LOGE("Failed to TransToFileUri, parameter is invalid! parameter = %{public}s", dataShareUri.c_str()); - return ""; - } string uri; string fragment; - string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - if (posFragment != string::npos) { - fragment = dataShareUri.substr(posFragment); - uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; - } else if (FileUri::IsMediaUri(dataShareUri)) { + if (IsMediaUri(dataShareUri)) { string deviceId = fileUri.GetAuthority(); if (!deviceId.empty()) { fragment = FRAGMENT_SYMBOLS + NETWORK_ID_TAG + EQUAL_SIGN + deviceId; } string path = fileUri.GetPath(); uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + path + fragment; + } else if (IsRemoteUri(dataShareUri)) { + string::size_type posFragment = dataShareUri.find(FRAGMENT_SYMBOLS); + fragment = dataShareUri.substr(posFragment); + uri = FILE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; } else { LOGE("Failed to TransToFileUri, dataShareUri is %{public}s", dataShareUri.c_str()); return ""; @@ -175,19 +184,23 @@ string FileUri::TransToDataShareUri(const string uri) return ""; } string dataShareUri; - string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS + REMOTE_URI_TAG); - if (posFragment != string::npos) { + if (IsRemoteUri(uri)) { + string::size_type posFragment = uri.find(FRAGMENT_SYMBOLS); string fragment = uri.substr(posFragment); dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + fragment; } else { FileUri fileUri(uri); string bundleName = fileUri.GetAuthority(); string path = fileUri.GetPath(); + if (bundleName.empty() || path.empty()) { + LOGE("Failed to TransToDataShareUri, parameter is invalid! parameter = %{public}s", uri.c_str()); + return ""; + } string fragment = fileUri.GetFragment(); string deviceId; if (!fragment.empty()) { - posFragment = fragment.find(EQUAL_SIGN); - deviceId = fragment.substr(posFragment + 1); + string::size_type posId = fragment.find(EQUAL_SIGN); + deviceId = fragment.substr(posId + 1); } dataShareUri = DATA_SHARE_SCHEME + SCHEME_SYMBOL + PATH_SYMBOL + PATH_SYMBOL + deviceId + PATH_SYMBOL + bundleName + path; } diff --git a/test/unittest/file_uri_native/file_uri_test.cpp b/test/unittest/file_uri_native/file_uri_test.cpp index 3378be93e..4099e59ba 100644 --- a/test/unittest/file_uri_native/file_uri_test.cpp +++ b/test/unittest/file_uri_native/file_uri_test.cpp @@ -146,4 +146,112 @@ namespace { EXPECT_EQ(dataShareUri, resultUri); GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0005"; } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0006 + * @tc.desc: Test function of TransToDataShareUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0006"; + string fileuri = "file://"; + string resultUri = ""; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0006"; + } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0007 + * @tc.desc: Test function of TransToDataShareUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0007, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0007"; + string fileuri = "file:/media/image/12"; + string resultUri = ""; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0007"; + } + + /** + * @tc.name: File_Uri_TransToDataShareUri_0008 + * @tc.desc: Test function of TransToDataShareUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0008, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0008"; + string fileuri = "fil://media/image/12"; + string resultUri = ""; + string dataShareUri = FileUri::TransToDataShareUri(fileuri); + EXPECT_EQ(dataShareUri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0008"; + } + + /** + * @tc.name: File_Uri_TransToFileUri_0009 + * @tc.desc: Test function of TransToFileUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0009, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0009"; + string dataShareUri = "datashare:////"; + string resultUri = ""; + string fileuri = FileUri::TransToFileUri(dataShareUri); + EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0009"; + } + + /** + * @tc.name: File_Uri_TransToFileUri_0010 + * @tc.desc: Test function of TransToFileUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0010, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0010"; + string dataShareUri = "datashare:/media/image/12"; + string resultUri = ""; + string fileuri = FileUri::TransToFileUri(dataShareUri); + EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0010"; + } + + /** + * @tc.name: File_Uri_TransToFileUri_0011 + * @tc.desc: Test function of TransToFileUri() interface for fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H63TL + */ + HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0011, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0011"; + string dataShareUri = "datashar:///media/image/12"; + string resultUri = ""; + string fileuri = FileUri::TransToFileUri(dataShareUri); + EXPECT_EQ(fileuri, resultUri); + GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0011"; + } } \ No newline at end of file -- Gitee