From 03c3c0cfd2fe2ae10ce1edaffd0dd877879dd632 Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 26 Jul 2025 11:17:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?enviroment=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I437c5f1755721196a8a81a489870fcfd363ffe66 --- .../ani/bind_function_class.cpp | 73 +++ .../mod_environment/ani/environment_ani.cpp | 175 ++++++ .../src/mod_environment/ani/environment_ani.h | 42 ++ .../ani/ets/@ohos.file.environment.ets | 103 ++++ .../src/mod_environment/environment_core.cpp | 204 +++++++ .../js/src/mod_environment/environment_core.h | 36 ++ .../environment_core_mock_test.cpp | 521 ++++++++++++++++++ .../mock/accesstoken_kit_mock.cpp | 29 + .../mock/accesstoken_kit_mock.h | 44 ++ .../mock/ipc_skeleton_mock.cpp | 33 ++ .../mod_environment/mock/ipc_skeleton_mock.h | 46 ++ .../mod_environment/mock/parameter_mock.cpp | 27 + .../js/mod_environment/mock/parameter_mock.h | 41 ++ 13 files changed, 1374 insertions(+) create mode 100644 interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.h create mode 100644 interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.cpp create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.h create mode 100644 interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h create mode 100644 interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.h create mode 100644 interfaces/test/unittest/js/mod_environment/mock/parameter_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_environment/mock/parameter_mock.h diff --git a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp new file mode 100644 index 000000000..dfda60113 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 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 "ani_signature.h" +#include "bind_function.h" +#include "environment_ani.h" + +using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; + +static ani_status BindStaticMethods(ani_env *env) +{ + auto classDesc = Impl::EnvironmentImpl::classDesc.c_str(); + std::array methods = { + ani_native_function { + "getStorageDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetStorageDataDirSync) }, + ani_native_function { + "getUserDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDataDirSync) }, + ani_native_function { + "getUserDownloadDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDownloadDirSync) }, + ani_native_function { + "getUserDesktopDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDesktopDirSync) }, + ani_native_function { + "getUserDocumentDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDocumentDirSync) }, + ani_native_function { + "getExternalStorageDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetExternalStorageDirSync) }, + ani_native_function { + "getUserHomeDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserHomeDirSync) }, + }; + return BindClass(env, classDesc, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for environment!"); + return status; + }; + + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp new file mode 100644 index 000000000..da95c9a2a --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2025 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 "environment_ani.h" + +#include "environment_core.h" +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleEnvironment; + +ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetStorageDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get storage data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDownloadDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user download dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDesktopDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user desktop dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDocumentDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user document dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetExternalStorageDir(); + if (!ret.IsSuccess()) { + HILOGE("Get external storage dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserHomeDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user home dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.h b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h new file mode 100644 index 000000000..d14a78c69 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class EnvironmentAni final { +public: + static ani_string GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets new file mode 100644 index 000000000..71b8e1c2d --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 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 { BusinessError, AsyncCallback } from '@ohos.base'; + +namespace Environment { + export function getStorageDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); + }); + } + + export function getStorageDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); + }); + } + + export function getUserDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); + }); + } + export function getUserDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); + }); + } + + export function getUserDownloadDir(): string { + return EnvironmentImpl.getUserDownloadDirSync(); + } + + export function getUserDesktopDir(): string { + return EnvironmentImpl.getUserDesktopDirSync(); + } + + export function getUserDocumentDir(): string { + return EnvironmentImpl.getUserDocumentDirSync(); + } + + export function getExternalStorageDir(): string { + return EnvironmentImpl.getExternalStorageDirSync(); + } + + export function getUserHomeDir(): string { + return EnvironmentImpl.getUserHomeDirSync(); + } + +} + +export default Environment; + +class EnvironmentImpl { + + static { + loadLibrary("ani_file_environment"); + } + + static native getStorageDataDirSync(): string; + static native getUserDataDirSync(): string; + static native getUserDownloadDirSync(): string; + static native getUserDesktopDirSync(): string; + static native getUserDocumentDirSync(): string; + static native getExternalStorageDirSync(): string; + static native getUserHomeDirSync(): string; +} diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp new file mode 100644 index 000000000..5be54fe32 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2025 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 "environment_core.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "account_error_no.h" +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "os_account_manager.h" +#include "parameter.h" +#include "tokenid_kit.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +namespace { +const std::string STORAGE_DATA_PATH = "/data"; +const std::string PC_STORAGE_PATH = "/storage/Users/"; +const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; +const std::string USER_APP_DATA_PATH = "/appdata"; +const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string FILE_ACCESS_MANAGER_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; +const std::string DOWNLOAD_PATH = "/Download"; +const std::string DESKTOP_PATH = "/Desktop"; +const std::string DOCUMENTS_PATH = "/Documents"; +const std::string DEFAULT_USERNAME = "currentUser"; +const char *FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; +static bool IsSystemApp() +{ + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("ModuleUserDirectory::CheckCallingPermission have no fileAccess permission"); + return false; + } + return true; +} + +static std::string GetUserName() +{ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK || userName.empty()) { + HILOGE("Get userName Failed"); + } + userName = DEFAULT_USERNAME; + return userName; +} + +static std::string GetPublicPath(const std::string &directoryName) +{ + return PC_STORAGE_PATH + GetUserName() + directoryName; +} + +static bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + return true; + } + HILOGE("Not supporting all mounts"); + return false; +} + +static int CheckInvalidAccess(const std::string &permission) +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return E_DEVICENOTSUPPORT; + } + if (permission == FILE_ACCESS_MANAGER_PERMISSION) { + if (!IsSystemApp()) { + return E_PERMISSION_SYS; + } + } + if (!CheckCallingPermission(permission)) { + HILOGE("No Permission"); + return E_PERMISSION; + } + return ERRNO_NOERR; +} +} + +FsResult DoGetStorageDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + return FsResult::Success(std::move(STORAGE_DATA_PATH)); +} + +int GetUserId() +{ + return 0; +} + +FsResult DoGetUserDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + + auto userDataPath = std::make_shared(); + (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); + return FsResult::Success(std::move(*userDataPath)); +} + +FsResult DoGetUserDownloadDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string downloadPath = GetPublicPath(DOWNLOAD_PATH); + if (downloadPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(downloadPath)); +} + +FsResult DoGetUserDesktopDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string desktopPath = GetPublicPath(DESKTOP_PATH); + if (desktopPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(desktopPath)); +} + +FsResult DoGetUserDocumentDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string documentsPath = GetPublicPath(DOCUMENTS_PATH); + if (documentsPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(documentsPath)); +} + +FsResult DoGetExternalStorageDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); +} + +FsResult DoGetUserHomeDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + + static std::string userName = GetUserName(); + if (userName.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + std::string result = PC_STORAGE_PATH + userName; + return FsResult::Success(std::move(result)); +} +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_core.h b/interfaces/kits/js/src/mod_environment/environment_core.h new file mode 100644 index 000000000..0962c1f4a --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +using namespace ModuleFileIO; +FsResult DoGetStorageDataDir(); +FsResult DoGetUserDataDir(); +FsResult DoGetUserDownloadDir(); +FsResult DoGetUserDesktopDir(); +FsResult DoGetUserDocumentDir(); +FsResult DoGetExternalStorageDir(); +FsResult DoGetUserHomeDir(); +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp new file mode 100644 index 000000000..6b680cc5b --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp @@ -0,0 +1,521 @@ +/* + * Copyright (C) 2025 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 "accesstoken_kit_mock.h" +#include "environment_core.h" +#include "ipc_skeleton_mock.h" +#include "parameter_mock.h" +#include "securec.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class EnvironmentCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr accessToken = nullptr; + static inline std::shared_ptr paramMoc = nullptr; + static inline shared_ptr skeleton = nullptr; +}; + +void EnvironmentCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + accessToken = std::make_shared(); + Backup::BAccessTokenKit::token = accessToken; + paramMoc = std::make_shared(); + AppFileService::ParamMoc::paramMoc = paramMoc; + skeleton = make_shared(); + Backup::IPCSkeletonMock::skeleton = skeleton; +} + +void EnvironmentCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Backup::BAccessTokenKit::token = nullptr; + accessToken = nullptr; + AppFileService::IParamMoc::paramMoc = nullptr; + paramMoc = nullptr; + Backup::IPCSkeletonMock::skeleton = nullptr; + skeleton = nullptr; +} + +void EnvironmentCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void EnvironmentCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetStorageDataDir_001 + * @tc.desc: Test function of DoGetStorageDataDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetStorageDataDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetStorageDataDir_001"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetStorageDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetStorageDataDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetStorageDataDir_002 + * @tc.desc: Test function of DoGetStorageDataDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetStorageDataDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetStorageDataDir_002"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + + auto res = ModuleEnvironment::DoGetStorageDataDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetStorageDataDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDataDir_001 + * @tc.desc: Test function of DoGetUserDataDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDataDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDataDir_001"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetUserDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDataDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDataDir_002 + * @tc.desc: Test function of DoGetUserDataDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDataDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDataDir_002"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + + auto res = ModuleEnvironment::DoGetUserDataDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDataDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDownloadDir_001 + * @tc.desc: Test function of DoGetUserDownloadDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDownloadDir_001"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillRepeatedly(Invoke([&](const char *, const char *, char *value, uint32_t) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*skeleton, GetOsAccountShortName(_)).WillRepeatedly(Return(ERR_OK)); + + auto res = ModuleEnvironment::DoGetUserDownloadDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDownloadDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDownloadDir_002 + * @tc.desc: Test function of DoGetUserDownloadDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDownloadDir_002"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)).WillOnce(Return(-1)); + + auto res = ModuleEnvironment::DoGetUserDownloadDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDownloadDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDownloadDir_003 + * @tc.desc: Test function of DoGetUserDownloadDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDownloadDir_003"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillRepeatedly(Invoke([&](const char *, const char *, char *value, uint32_t) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*skeleton, GetOsAccountShortName(_)).WillRepeatedly(Return(1)); + + auto res = ModuleEnvironment::DoGetUserDownloadDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDownloadDir_003"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDesktopDir_001 + * @tc.desc: Test function of DoGetUserDesktopDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDesktopDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDesktopDir_001"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + + auto res = ModuleEnvironment::DoGetUserDesktopDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDesktopDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDesktopDir_002 + * @tc.desc: Test function of DoGetUserDesktopDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDesktopDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDesktopDir_002"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return -1; + })); + + auto res = ModuleEnvironment::DoGetUserDesktopDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDesktopDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDesktopDir_003 + * @tc.desc: Test function of DoGetUserDesktopDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDesktopDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDesktopDir_003"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *, const char *, char *value, uint32_t) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*skeleton, GetOsAccountShortName(_)).WillRepeatedly(Return(ERR_OK)); + + auto res = ModuleEnvironment::DoGetUserDesktopDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDesktopDir_003"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDocumentDir_001 + * @tc.desc: Test function of DoGetUserDocumentDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDocumentDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDocumentDir_001"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + + auto res = ModuleEnvironment::DoGetUserDocumentDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDocumentDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDocumentDir_002 + * @tc.desc: Test function of DoGetUserDocumentDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDocumentDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDocumentDir_002"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return -1; + })); + + auto res = ModuleEnvironment::DoGetUserDocumentDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDocumentDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDocumentDir_003 + * @tc.desc: Test function of DoGetUserDocumentDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDocumentDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDocumentDir_003"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *, const char *, char *value, uint32_t) { + strcpy_s(value, 5, "true"); + return 1; + })); + + auto res = ModuleEnvironment::DoGetUserDocumentDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDocumentDir_003"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_001 + * @tc.desc: Test function of DoGetExternalStorageDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_001"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_002 + * @tc.desc: Test function of DoGetExternalStorageDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_002"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); + EXPECT_CALL(*accessToken, VerifyAccessToken(_, _)).WillOnce(Return(1)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_003 + * @tc.desc: Test function of DoGetExternalStorageDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_003"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); + EXPECT_CALL(*accessToken, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_GRANTED)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_003"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_004 + * @tc.desc: Test function of DoGetExternalStorageDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_004"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_004"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserHomeDir_001 + * @tc.desc: Test function of DoGetUserHomeDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserHomeDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserHomeDir_001"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)).WillOnce(Return(1)); + + auto res = ModuleEnvironment::DoGetUserHomeDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserHomeDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserHomeDir_002 + * @tc.desc: Test function of DoGetUserHomeDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserHomeDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserHomeDir_002"; + + EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) + .WillOnce(Invoke([&](const char *key, const char *def, char *value, uint32_t len) { + strcpy_s(value, 5, "true"); + return 1; + })); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); + EXPECT_CALL(*accessToken, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_GRANTED)); + + auto res = ModuleEnvironment::DoGetUserHomeDir(); + + EXPECT_TRUE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserHomeDir_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.cpp b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.cpp new file mode 100644 index 000000000..021e591fe --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 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 "accesstoken_kit_mock.h" + +namespace OHOS::Security::AccessToken { + +bool TokenIdKit::IsSystemAppByFullTokenID(uint64_t tokenId) +{ + return OHOS::FileManagement::Backup::BAccessTokenKit::token->IsSystemAppByFullTokenID(tokenId); +} + +int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string &permissionName) +{ + return OHOS::FileManagement::Backup::BAccessTokenKit::token->VerifyAccessToken(tokenID, permissionName); +} +} // namespace OHOS::Security::AccessToken diff --git a/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h new file mode 100644 index 000000000..5f5c0b18b --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_MOCK_ACCESSTOKEN_KIT_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_MOCK_ACCESSTOKEN_KIT_MOCK_H + +#include + +#include "accesstoken_kit.h" +#include "tokenid_kit.h" + +namespace OHOS::FileManagement::Backup { +class BAccessTokenKit { +public: + virtual bool IsSystemAppByFullTokenID(uint64_t) = 0; + virtual int VerifyAccessToken(Security::AccessToken::AccessTokenID, const std::string &) = 0; + +public: + BAccessTokenKit() = default; + virtual ~BAccessTokenKit() = default; + +public: + static inline std::shared_ptr token = nullptr; +}; + +class AccessTokenKitMock : public BAccessTokenKit { +public: + MOCK_METHOD(bool, IsSystemAppByFullTokenID, (uint64_t)); + MOCK_METHOD(int, VerifyAccessToken, (Security::AccessToken::AccessTokenID, const std::string &)); +}; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_MOCK_ACCESSTOKEN_KIT_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.cpp b/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.cpp new file mode 100644 index 000000000..a231dfdee --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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 "ipc_skeleton_mock.h" + +namespace OHOS { +#ifdef CONFIG_IPC_SINGLE +using namespace IPC_SINGLE; +#endif + +uint32_t IPCSkeleton::GetCallingTokenID() +{ + return OHOS::FileManagement::Backup::BIPCSkeleton::skeleton->GetCallingTokenID(); +} + +ErrCode OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(std::string &shortName) +{ + return OHOS::FileManagement::Backup::BIPCSkeleton::skeleton->GetOsAccountShortName(shortName); +} + +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.h b/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.h new file mode 100644 index 000000000..89c5e9fc8 --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/ipc_skeleton_mock.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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 INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_IPC_SKELETON_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_IPC_SKELETON_MOCK_H + +#include + +#include "ipc_skeleton.h" +#include "os_account_manager.h" + +using ErrCode = int; + +namespace OHOS::FileManagement::Backup { +class BIPCSkeleton : public RefBase { +public: + virtual uint32_t GetCallingTokenID() = 0; + virtual ErrCode GetOsAccountShortName(std::string &shortName) = 0; + +public: + BIPCSkeleton() = default; + virtual ~BIPCSkeleton() = default; + +public: + static inline std::shared_ptr skeleton = nullptr; +}; + +class IPCSkeletonMock : public BIPCSkeleton { +public: + MOCK_METHOD(uint32_t, GetCallingTokenID, ()); + MOCK_METHOD(ErrCode, GetOsAccountShortName, (std::string & shortName)); +}; +} // namespace OHOS::FileManagement::Backup +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_IPC_SKELETON_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.cpp b/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.cpp new file mode 100644 index 000000000..2905890c1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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 "parameter_mock.h" + +#include "parameter.h" + +using namespace OHOS::AppFileService; +int GetParameter(const char *key, const char *def, char *value, uint32_t len) +{ + if (IParamMoc::paramMoc == nullptr) { + return -1; + } + return IParamMoc::paramMoc->GetParameter(key, def, value, len); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.h b/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.h new file mode 100644 index 000000000..41fc24fba --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/mock/parameter_mock.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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 INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_PARAMETER_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_PARAMETER_MOCK_H + +#include +#include + +namespace OHOS { +namespace AppFileService { +class IParamMoc { +public: + virtual ~IParamMoc() = default; + +public: + virtual int GetParameter(const char *key, const char *def, char *value, uint32_t len) = 0; + +public: + static inline std::shared_ptr paramMoc = nullptr; +}; + +class ParamMoc : public IParamMoc { +public: + MOCK_METHOD4(GetParameter, int(const char *key, const char *def, char *value, uint32_t len)); +}; +} // namespace AppFileService +} // namespace OHOS +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_ENVIRONMENT_PARAMETER_MOCK_H \ No newline at end of file -- Gitee From c61773d6a55c6fdeb355ecf339ff843aa17c3d67 Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 26 Jul 2025 16:00:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=20enviroment=E5=8A=9F=E8=83=BD=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I0a19cfe2cdfef551e3bfac53efce17efc594f9a8 --- .../kits/js/src/mod_environment/ani/bind_function_class.cpp | 1 + .../src/mod_environment/ani/ets/@ohos.file.environment.ets | 1 + interfaces/kits/js/src/mod_environment/environment_core.cpp | 4 +++- .../js/mod_environment/environment_core_mock_test.cpp | 5 +++-- .../unittest/js/mod_environment/mock/accesstoken_kit_mock.h | 2 -- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp index dfda60113..f33575660 100644 --- a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp @@ -14,6 +14,7 @@ */ #include + #include "ani_signature.h" #include "bind_function.h" #include "environment_ani.h" diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets index 71b8e1c2d..03cefd296 100644 --- a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -51,6 +51,7 @@ namespace Environment { }); }); } + export function getUserDataDir(callback: AsyncCallback): void { let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); promise.then((ret: NullishType): void => { diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index 5be54fe32..53aa8108a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -65,7 +65,7 @@ static std::string GetUserName() std::string userName; ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); if (errCode != ERR_OK || userName.empty()) { - HILOGE("Get userName Failed"); + HILOGE("Get userName failed"); } userName = DEFAULT_USERNAME; return userName; @@ -93,11 +93,13 @@ static int CheckInvalidAccess(const std::string &permission) HILOGE("Capability not supported"); return E_DEVICENOTSUPPORT; } + if (permission == FILE_ACCESS_MANAGER_PERMISSION) { if (!IsSystemApp()) { return E_PERMISSION_SYS; } } + if (!CheckCallingPermission(permission)) { HILOGE("No Permission"); return E_PERMISSION; diff --git a/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp index 6b680cc5b..90a95ab21 100644 --- a/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp @@ -72,7 +72,7 @@ void EnvironmentCoreTest::TearDown(void) /** * @tc.name: EnvironmentCoreTest_DoGetStorageDataDir_001 - * @tc.desc: Test function of DoGetStorageDataDir interface for FALSE. + * @tc.desc: Test function of DoGetStorageDataDir interface for FALSE tokenID error. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -164,7 +164,8 @@ HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_001, test EXPECT_CALL(*paramMoc, GetParameter(_, _, _, _)) .WillRepeatedly(Invoke([&](const char *, const char *, char *value, uint32_t) { strcpy_s(value, 5, "true"); - return 1; + uint32_t successValue = 1; + return successValue; })); EXPECT_CALL(*skeleton, GetOsAccountShortName(_)).WillRepeatedly(Return(ERR_OK)); diff --git a/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h index 5f5c0b18b..d47ac5887 100644 --- a/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h +++ b/interfaces/test/unittest/js/mod_environment/mock/accesstoken_kit_mock.h @@ -27,11 +27,9 @@ public: virtual bool IsSystemAppByFullTokenID(uint64_t) = 0; virtual int VerifyAccessToken(Security::AccessToken::AccessTokenID, const std::string &) = 0; -public: BAccessTokenKit() = default; virtual ~BAccessTokenKit() = default; -public: static inline std::shared_ptr token = nullptr; }; -- Gitee