From caca843355d8055279568fae49ab4283541fe837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 12 May 2025 14:42:32 +0800 Subject: [PATCH 01/39] =?UTF-8?q?hashstream=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/BUILD.gn | 4 + .../src/mod_hash/ani/bind_function_class.cpp | 17 ++ .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 16 +- .../class_hashstream/ani/hashstream_ani.cpp | 123 +++++++++++++ .../class_hashstream/ani/hashstream_ani.h | 37 ++++ .../class_hashstream/hs_hashstream.cpp | 161 ++++++++++++++++++ .../mod_hash/class_hashstream/hs_hashstream.h | 46 +++++ .../class_hashstream/hs_hashstream_entity.h | 37 ++++ 8 files changed, 439 insertions(+), 2 deletions(-) create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h create mode 100644 interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 532923e60..9304a3b10 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -856,6 +856,8 @@ ohos_shared_library("ani_file_hash") { include_dirs = [ "src/mod_hash", "src/mod_hash/ani", + "src/mod_hash/class_hashstream", + "src/mod_hash/class_hashstream/ani", ] sources = [ "src/common/ani_helper/error_handler.cpp", @@ -865,6 +867,8 @@ ohos_shared_library("ani_file_hash") { "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", "src/mod_hash/ani/hash_ani.cpp", + "src/mod_hash/class_hashstream/ani/hashstream_ani.cpp", + "src/mod_hash/class_hashstream/hs_hashstream.cpp", "src/mod_hash/hash_core.cpp", ] diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index d93492ac7..6bf91b45d 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -29,6 +29,17 @@ static ani_status BindStaticMethods(ani_env *env) return BindClass(env, className, methods); } +static ani_status BindHashStreamMethods(ani_env *env) +{ + static const char *className = "L@ohos/file/hash/HashStreamImpl;"; + std::array methods = { + ani_native_function { "digest", nullptr, reinterpret_cast(HashStreamAni::Digest) }, + ani_native_function { "update", nullptr, reinterpret_cast(HashStreamAni::Update) }, + ani_native_function { "", "Lstd/core/String;:V", reinterpret_cast(HashStreamAni::Constructor) }, + }; + return BindClass(env, className, methods); +} + ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { if (vm == nullptr) { @@ -54,6 +65,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; + status = BindHashStreamMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for hashstream!"); + return status; + }; + *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index dfa16ab00..cf35acc06 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -46,12 +46,12 @@ export default namespace hash { } export class HashStream extends stream.Transform { - hs: hash.HashStream; + hs: HashStreamImpl; hashBuf?: ArrayBuffer; constructor(algorithm: string) { super(); - this.hs = new hash.HashStream(algorithm); + this.hs = new HashStreamImpl(algorithm); } digest(): string { @@ -83,6 +83,18 @@ export default namespace hash { } } +export class HashStreamImpl extends stream.Transform { + + static { + loadLibrary("ani_file_hash"); + } + private nativePtr: long = 0; + + native constructor(alg: string); + native digest(): string; + native update(data: ArrayBuffer): void; +} + class HashImpl { static { diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp new file mode 100644 index 000000000..49079f114 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp @@ -0,0 +1,123 @@ +/* + * 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 "hashstream_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "hs_hashstream.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +namespace fs = std::filesystem; +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +HsHashStream *Unwrap(ani_env *env, ani_object object) +{ + ani_long nativePtr; + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr); + if (ret != ANI_OK) { + HILOGE("Unwrap hashstream err: %{private}d", ret); + return nullptr; + } + uintptr_t ptrValue = static_cast(nativePtr); + HsHashStream *hashStream = reinterpret_cast(ptrValue); + return hashStream; +} + +void HashStreamAni::Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto [succ, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer); + if (!succ) { + HILOGE("illegal array buffer"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = hashStream->Update(arrayBuffer); + if (!ret.IsSuccess()) { + HILOGE("Cannot Update!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} + +ani_string HashStreamAni::Digest(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = hashStream->Digest(); + if (!ret.IsSuccess()) { + HILOGE("Cannot Digest!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const auto &res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Convert result to ani string failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) +{ + auto [succ, algorithm] = TypeConverter::ToUTF8String(env, alg); + if (!succ) { + HILOGE("Invalid alg"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = HsHashStream::Constructor(algorithm); + if (!ret.IsSuccess()) { + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } + + if (ANI_OK != + env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + HILOGE("Failed to wrap entity for obj HashStream"); + ErrorHandler::Throw(env, EIO); + return; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h new file mode 100644 index 000000000..c5f98974e --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h @@ -0,0 +1,37 @@ +/* + * 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_HASH_HASHSTREAM_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class HashStreamAni final { +public: + static void Constructor(ani_env *env, ani_object obj, ani_string alg); + static ani_string Digest(ani_env *env, [[maybe_unused]] ani_object object); + static void Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp new file mode 100644 index 000000000..b03eb2b15 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp @@ -0,0 +1,161 @@ +/* + * 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 "hs_hashstream.h" + +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg) +{ + return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED; +} + +static string HashFinal(const unique_ptr &hashBuf, size_t hashLen) +{ + stringstream ss; + for (size_t i = 0; i < hashLen; ++i) { + const int hexPerByte = 2; + ss << std::uppercase << std::setfill('0') << std::setw(hexPerByte) << std::hex + << static_cast(hashBuf[i]); + } + + return ss.str(); +} + +tuple HsHashStream::GetHsEntity() +{ + if (!entity) { + return { false, nullptr }; + } + return { true, entity.get() }; +} + +FsResult HsHashStream::Update(ArrayBuffer &buffer) +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: + MD5_Update(&hsEntity->md5Ctx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA1: + SHA1_Update(&hsEntity->shaCtx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA256: + SHA256_Update(&hsEntity->sha256Ctx, buffer.buf, buffer.length); + break; + default: + break; + } + + return FsResult::Success(); +} + +FsResult HsHashStream::Digest() +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + string digestStr; + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: { + auto res = make_unique(MD5_DIGEST_LENGTH); + MD5_Final(res.get(), &hsEntity->md5Ctx); + digestStr = HashFinal(res, MD5_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + auto res = make_unique(SHA_DIGEST_LENGTH); + SHA1_Final(res.get(), &hsEntity->shaCtx); + digestStr = HashFinal(res, SHA_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + auto res = make_unique(SHA256_DIGEST_LENGTH); + SHA256_Final(res.get(), &hsEntity->sha256Ctx); + digestStr = HashFinal(res, SHA256_DIGEST_LENGTH); + break; + } + default: + break; + } + return FsResult::Success(digestStr); +} + +FsResult HsHashStream::Constructor(string alg) +{ + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("algType is not found."); + return FsResult::Error(EINVAL); + } + + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + if (rawPtr == nullptr) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + std::unique_ptr hsEntity(rawPtr); + hsEntity->algType = algType; + + switch (algType) { + case HASH_ALGORITHM_TYPE_MD5: { + MD5_CTX ctx; + MD5_Init(&ctx); + hsEntity->md5Ctx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + SHA_CTX ctx; + SHA1_Init(&ctx); + hsEntity->shaCtx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + SHA256_CTX ctx; + SHA256_Init(&ctx); + hsEntity->sha256Ctx = ctx; + break; + } + default: + break; + } + + HsHashStream *hsStreamPtr = new HsHashStream(move(hsEntity)); + if (hsStreamPtr == nullptr) { + HILOGE("Failed to create HsHashStream object on heap."); + return FsResult::Error(ENOMEM); + } + + return FsResult::Success(move(hsStreamPtr)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h new file mode 100644 index 000000000..b39b86fb2 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h @@ -0,0 +1,46 @@ +/* + * 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_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H + +#include +#include "filemgmt_libfs.h" +#include "fs_utils.h" +#include "hs_hashstream_entity.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class HsHashStream { +public: + HsHashStream(const HsHashStream &) = delete; + HsHashStream &operator=(const HsHashStream &) = delete; + + tuple GetHsEntity(); + FsResult Update(ArrayBuffer &buffer); + FsResult Digest(); + + static FsResult Constructor(string alg); + ~HsHashStream() = default; + +private: + unique_ptr entity; + explicit HsHashStream(unique_ptr entity) : entity(move(entity)) {} +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h new file mode 100644 index 000000000..de1700cad --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h @@ -0,0 +1,37 @@ +/* + * 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_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H + +#include +#include +#include + +#include "hash_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct HsHashStreamEntity { + MD5_CTX md5Ctx; + SHA_CTX shaCtx; + SHA256_CTX sha256Ctx; + HASH_ALGORITHM_TYPE algType = HASH_ALGORITHM_TYPE_UNSUPPORTED; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H \ No newline at end of file -- Gitee From 66b16a32a87f135369a645216668c41da135787e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 13 May 2025 10:01:11 +0800 Subject: [PATCH 02/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=B4=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 6bf91b45d..4468b1012 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -15,6 +15,7 @@ #include #include "bind_function.h" +#include "hashstream_ani.h" #include "hash_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; -- Gitee From 04ee705cba9ab559c8903d53923ebdb1d6dea886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 28 May 2025 15:20:24 +0800 Subject: [PATCH 03/39] hsh&securitylabel tdd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/test/unittest/BUILD.gn | 3 + interfaces/test/unittest/js/BUILD.gn | 108 ++++++ .../class_atomicfile/fs_atomicfile_test.cpp | 366 ++++++++++++++++++ .../class_stream/fs_stream_mock_test.cpp | 191 +++++++++ .../js/mod_fs/class_stream/mock/c_mock.cpp | 32 ++ .../js/mod_fs/class_stream/mock/c_mock.h | 42 ++ .../unittest/js/mod_hash/hash_core_test.cpp | 136 +++++++ .../securitylabel_core_test.cpp | 142 +++++++ 8 files changed, 1020 insertions(+) create mode 100644 interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h create mode 100644 interfaces/test/unittest/js/mod_hash/hash_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..1c1ca95da 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,7 +17,10 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", + "js:ani_file_hash_test", + "js:ani_file_securitylabel_test", ] } diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index e4e6a9d52..49f400ac2 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -74,4 +74,112 @@ ohos_unittest("ani_file_fs_test") { ] cflags_cc = [ "-DENABLE_NAPI_MOCK" ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_stream/fs_stream_mock_test.cpp", + "mod_fs/class_stream/mock/c_mock.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_hash_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_hash/hash_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_hash", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${file_api_path}/interfaces/kits/js:ani_file_hash", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "libuv:uv", + "googletest:gmock_main", + "googletest:gtest_main", + ] +} + +ohos_unittest("ani_file_securitylabel_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_securitylabel/securitylabel_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "libuv:uv", + "googletest:gmock_main", + "googletest:gtest_main", + ] } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp new file mode 100644 index 000000000..081de4750 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp @@ -0,0 +1,366 @@ +/* + * 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 +#include "fs_atomicfile.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace std; +namespace fs = std::filesystem; + +string filePath = "/data/test/FsAtomicfileTest.txt"; +string deleteFile = "/data/test/FsAtomicfileDelTest.txt"; + +class FsAtomicfileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsAtomicfileTest::SetUpTestCase(void) +{ + ofstream tempfile(filePath); + tempfile << "hello world"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsAtomicfileTest::TearDownTestCase(void) +{ + filesystem::remove(filePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsAtomicfileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsAtomicfileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsAtomicfileTest_GetPath_001 + * @tc.desc: Test function of FsAtomicFile::GetPath interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + string path = stream->GetPath(); + EXPECT_EQ(path, filePath); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_001 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_002 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for path > PATH_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_002"; + + size_t largeLength = static_cast(PATH_MAX) + 1; + string largeString(largeLength, 'a'); + + auto ret = FsAtomicFile::Constructor(largeString); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_002"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_003 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed realpath. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_003"; + + string path = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_003"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_004 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed open. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_004"; + + string path = "/data/test/aaaaaaaaaa.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_004"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_001 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_002 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no parent dir. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_002"; + + string path = "/data/local/tmp/test/test/test/test.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_002"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_003 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_003"; + + string path = "/sys/kernel/address_bits"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_003"; +} + +/** + * @tc.name: FsAtomicfileTest_FinishWrite_001 + * @tc.desc: Test function of FsAtomicFile::FinishWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FinishWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FinishWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_FailWrite_001 + * @tc.desc: Test function of FsAtomicFile::FailWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FailWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FailWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_Delete_001 + * @tc.desc: Test function of FsAtomicFile::Delete interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; + + auto ret = FsAtomicFile::Constructor(deleteFile); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->Delete(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_Delete_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_001 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_TRUE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_002 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for valied path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_002"; + + auto ret = FsAtomicFile::Constructor("aaaaaaaaaaaaaaaa"); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_FALSE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp new file mode 100644 index 000000000..f13339006 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -0,0 +1,191 @@ +/* + * 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 "c_mock.h" +#include "create_stream_core.h" +#include "fs_utils.h" + +#define STREAM_FILE_PATH "/data/test/FsStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class FsStreamMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + mock_ = std::make_shared(); + ICMock::ins = mock_; + int32_t fd = open(STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + ICMock::ins = nullptr; + mock_ = nullptr; + rmdir(STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; + + static inline std::shared_ptr mock_ = nullptr; +}; + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for fail ftell. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + auto retWr = result->Write("FsStreamWriteTest_0001", opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + string buf = "FsStreamWriteTest_0002"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + ReadOptions opt; + opt.offset = 5; + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0001"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp new file mode 100644 index 000000000..a64f834fa --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -0,0 +1,32 @@ +/* +* 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 "c_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { + +int fseek(FILE *stream, long len, int offset) +{ + return ICMock::ins->fseek(stream, len, offset); +} + +long ftell(FILE *stream) +{ + return ICMock::ins->ftell(stream); +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h new file mode 100644 index 000000000..2fbdcee16 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.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_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class ICMock { +public: + static inline std::shared_ptr ins = nullptr; +public: + virtual ~ICMock() = default; + virtual int fseek(FILE *, long, int) = 0; + virtual long ftell(FILE *) = 0; +}; + +class CMock : public ICMock { +public: + MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); + MOCK_METHOD(long, ftell, (FILE *), (override)); +}; + + +} // OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp new file mode 100644 index 000000000..6c13b0e6e --- /dev/null +++ b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp @@ -0,0 +1,136 @@ +/* + * 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 +#include "hash_core.h" + +#define FILE_PATH "/data/test/HashCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class HashCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: DoHashTest_0001 + * @tc.desc: Test function of DoHash() interface for invalid alg. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0001"; + string alg = "sha128"; + auto ret = HashCore::DoHash(FILE_PATH, alg); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0001"; +} + +/** + * @tc.name: DoHashTest_0002 + * @tc.desc: Test function of DoHash() interface for md5 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0002"; + auto ret = HashCore::DoHash(FILE_PATH, "md5"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0002"; +} + +/** + * @tc.name: DoHashTest_0003 + * @tc.desc: Test function of DoHash() interface for sha1 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0003"; + auto ret = HashCore::DoHash(FILE_PATH, "sha1"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0003"; +} + +/** + * @tc.name: DoHashTest_0004 + * @tc.desc: Test function of DoHash() interface for sha256 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0004"; + auto ret = HashCore::DoHash(FILE_PATH, "sha256"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004"; +} + +/** + * @tc.name: DoHashTest_0005 + * @tc.desc: Test function of DoHash() interface for no such file or directory. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0005"; + auto ret = HashCore::DoHash("/data/local/tmp/azuxyicayhyskjeh", "sha256"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0005"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp new file mode 100644 index 000000000..ae28da157 --- /dev/null +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -0,0 +1,142 @@ +/* + * 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 +#include "securitylabel_core.h" + +#define FILE_PATH "/data/test/SecurityLabelCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleSecurityLabel { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +class SecurityLabelCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: DoSetSecurityLabel_0001 + * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid level. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0001"; + auto ret = DoSetSecurityLabel(FILE_PATH, "abc"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0001"; +} + +/** + * @tc.name: DoSetSecurityLabel_0002 + * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetDoSetSecurityLabel_0002SecurityLabel_0001"; + auto ret = DoSetSecurityLabel("FILE_PATH", "s1"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0002"; +} + +/** + * @tc.name: DoSetSecurityLabel_0003 + * @tc.desc: Test function of DoSetSecurityLabel() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0003"; + auto ret = DoSetSecurityLabel(FILE_PATH, "s2"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0003"; +} + +/** + * @tc.name: DoGetSecurityLabel_0001 + * @tc.desc: Test function of DoGetSecurityLabel() interface for invalid path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0001"; + auto ret = DoGetSecurityLabel("FILE_PATH"); + EXPECT_TRUE(ret.IsSuccess()); + + const string level = ret.GetData().value(); + EXPECT_EQ(level, "s3"); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0001"; +} + +/** + * @tc.name: DoGetSecurityLabel_0002 + * @tc.desc: Test function of DoGetSecurityLabel() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0002"; + auto ret = DoGetSecurityLabel(FILE_PATH); + EXPECT_TRUE(ret.IsSuccess()); + + const string level = ret.GetData().value(); + EXPECT_EQ(level, "s2"); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0002"; +} + +} // namespace ModuleSecurityLabel +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file -- Gitee From 04b83f030c8efa3c15627f6ed7006fe95e7f4d40 Mon Sep 17 00:00:00 2001 From: tianp Date: Wed, 28 May 2025 15:39:06 +0800 Subject: [PATCH 04/39] =?UTF-8?q?randomaccessfile=E6=8E=A5=E5=8F=A3TDD?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/BUILD.gn | 1 + interfaces/test/unittest/js/BUILD.gn | 73 ++- .../fs_randomaccessfile_mock_test.cpp | 249 +++++++++++ .../fs_randomaccessfile_test.cpp | 417 ++++++++++++++++++ ...create_randomaccessfile_core_mock_test.cpp | 108 +++++ .../create_randomaccessfile_core_test.cpp | 187 ++++++++ .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 92 +++- .../js/mod_fs/properties/mock/uv_fs_mock.h | 102 +++-- 8 files changed, 1135 insertions(+), 94 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..e7ec1360f 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,6 +17,7 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", ] diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index e4e6a9d52..3be27d0d0 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -19,22 +19,61 @@ ohos_unittest("ani_file_fs_test") { testonly = true module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + include_dirs = [ - "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", - "mock/uv_fs_mock.h", ] sources = [ + "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", - "mod_fs/properties/access_core_test.cpp", - "mod_fs/properties/dup_core_test.cpp", - "mod_fs/properties/read_core_test.cpp", - "mod_fs/properties/rmdir_core_test.cpp", - "mod_fs/properties/symlink_core_test.cpp", - "mod_fs/properties/truncate_core_test.cpp", - "mod_fs/properties/utimes_core_test.cpp", - "mod_fs/properties/write_core_test.cpp", ] deps = [ @@ -43,35 +82,21 @@ ohos_unittest("ani_file_fs_test") { "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${utils_path}/filemgmt_libn:filemgmt_libn", "${file_api_path}/interfaces/kits/js:ani_file_fs", ] external_deps = [ "ability_runtime:ability_manager", "app_file_service:fileuri_native", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "dfs_service:distributed_file_daemon_kit_inner", - "dfs_service:libdistributedfileutils", - "eventhandler:libeventhandler", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "hisysevent:libhisysevent", "ipc:ipc_core", "libuv:uv", - "runtime_core:ani", - "runtime_core:libarkruntime", - "samgr:samgr_proxy", ] defines = [ "private=public", ] - - cflags_cc = [ "-DENABLE_NAPI_MOCK" ] } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp new file mode 100644 index 000000000..443a7e3c3 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp @@ -0,0 +1,249 @@ +/* + * 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 "fs_randomaccessfile.h" +#include "../properties/mock/uv_fs_mock.h" +#include "file_entity.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsRandomAccessFileMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +protected: + std::unique_ptr rafEntity; + std::unique_ptr raf; +}; + +void FsRandomAccessFileMockTest::SetUpTestCase(void) +{ + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsRandomAccessFileMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsRandomAccessFileMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + rafEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = std::make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = std::make_unique(std::move(rafEntity)); +} + +void FsRandomAccessFileMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_001 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_001"; + + ArrayBuffer buffer(malloc(100), 100); + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->ReadSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_001"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_002 + * @tc.desc: Test function of ReadSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_002"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.offset = 10; + options.length = 10; + raf->rafEntity->filePointer = 20; + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_002"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_003 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_003"; + + std::string data = "test data"; + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(data, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_003"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_004 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_004"; + + std::string data = "test data"; + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_004"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_005 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_005"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_005"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_006 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_006"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_006"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_007 + * @tc.desc: Test function of CloseSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_007"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(-1)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_007"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_008 + * @tc.desc: Test function of CloseSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_008"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(0)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_008"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp new file mode 100644 index 000000000..a863fee9c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp @@ -0,0 +1,417 @@ +/* + * 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 "fs_randomaccessfile.h" +#include "file_entity.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsRandomAccessFileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +protected: + std::unique_ptr rafEntity; + std::unique_ptr raf; +}; + +void FsRandomAccessFileTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsRandomAccessFileTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsRandomAccessFileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + rafEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = std::make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = std::make_unique(std::move(rafEntity)); +} + +void FsRandomAccessFileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +// 测试Constructor +/** + * @tc.name: FsRandomAccessFileTest_Constructor_001 + * @tc.desc: Test function of Constructor() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_Constructor_001"; + + auto result = FsRandomAccessFile::Constructor(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_Constructor_001"; +} + +// 测试GetFD +/** + * @tc.name: FsRandomAccessFileTest_GetFD_002 + * @tc.desc: Test function of GetFD() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFD_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFD_002"; + + auto result = raf->GetFD(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFD_002"; +} + +/** + * @tc.name: FsRandomAccessFileTest_GetFD_003 + * @tc.desc: Test function of GetFD() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFD_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFD_003"; + + raf = std::make_unique(nullptr); + auto result = raf->GetFD(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFD_003"; +} + +// GetFPointer +/** + * @tc.name: FsRandomAccessFileTest_GetFPointer_004 + * @tc.desc: Test function of GetFPointer() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFPointer_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFPointer_004"; + + raf->rafEntity->filePointer = 100; + auto result = raf->GetFPointer(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFPointer_004"; +} + +/** + * @tc.name: FsRandomAccessFileTest_GetFPointer_005 + * @tc.desc: Test function of GetFPointer() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_GetFPointer_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_GetFPointer_005"; + + raf = std::make_unique(nullptr); + auto result = raf->GetFPointer(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFPointer_005"; +} + +// SetFilePointerSync +/** + * @tc.name: FsRandomAccessFileTest_SetFilePointerSync_006 + * @tc.desc: Test function of SetFilePointerSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_SetFilePointerSync_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_SetFilePointerSync_006"; + + auto result = raf->SetFilePointerSync(50); + EXPECT_EQ(result.IsSuccess(), true); + EXPECT_EQ(raf->rafEntity->filePointer, 50); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_SetFilePointerSync_006"; +} + +/** + * @tc.name: FsRandomAccessFileTest_SetFilePointerSync_007 + * @tc.desc: Test function of SetFilePointerSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_SetFilePointerSync_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_SetFilePointerSync_007"; + + raf = std::make_unique(nullptr); + auto result = raf->SetFilePointerSync(50); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_SetFilePointerSync_007"; +} + +// ReadSync +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_008 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_008"; + + raf = std::make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + + auto result = raf->ReadSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_008"; +} + +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_009 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_009"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.offset = -5; + + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_009"; +} + +/** + * @tc.name: FsRandomAccessFileTest_ReadSync_010 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_ReadSync_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_ReadSync_010"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.length = -1; + + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_ReadSync_010"; +} + +// WriteSync +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_011 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_011"; + + raf = std::make_unique(nullptr); + std::string data = "test data"; + auto result = raf->WriteSync(data, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_011"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_012 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_012"; + + raf = std::make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + auto result = raf->WriteSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_012"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_013 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_013"; + + std::string data = "test data"; + WriteOptions options; + options.offset = -5; + + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_013"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_014 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_014"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.offset = -5; + + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_014"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_015 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_015"; + + std::string data = "test data"; + WriteOptions options; + options.length = -5; + + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_015"; +} + +/** + * @tc.name: FsRandomAccessFileTest_WriteSync_016 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_WriteSync_016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_WriteSync_016"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = -5; + + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_WriteSync_016"; +} + +// CloseSync +/** + * @tc.name: FsRandomAccessFileTest_CloseSync_017 + * @tc.desc: Test function of CloseSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + + */ +HWTEST_F(FsRandomAccessFileTest, FsRandomAccessFileTest_CloseSync_017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileTest_CloseSync_017"; + + raf = std::make_unique(nullptr); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_CloseSync_017"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp new file mode 100644 index 000000000..a3628289f --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp @@ -0,0 +1,108 @@ +/* + * 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 "create_randomaccessfile_core.h" +#include "mock/uv_fs_mock.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CreateRandomAccessFileCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void CreateRandomAccessFileCoreMockTest::SetUpTestCase(void) +{ + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CreateRandomAccessFileCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CreateRandomAccessFileCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CreateRandomAccessFileCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = 0; + optional options = nullopt; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 0; + opts.end = 100; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp new file mode 100644 index 000000000..1a3cf17be --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -0,0 +1,187 @@ +/* + * 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 "create_randomaccessfile_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CreateRandomAccessFileCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CreateRandomAccessFileCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CreateRandomAccessFileCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CreateRandomAccessFileCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CreateRandomAccessFileCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = -5; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = -1; + opts.end = 100; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 10; + opts.end = -1; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; + + int fd = -1; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; + + string path = ""; + int32_t mode = 0; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; + + int fd = 3; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index c4f112268..30bdcb135 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -13,74 +13,118 @@ * limitations under the License. */ - #include "uv_fs_mock.h" - using namespace OHOS::FileManagement::ModuleFileIO; -int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t off, - uv_fs_cb cb) +int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); } -int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) +int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) { return Uvfs::ins->uv_fs_readlink(loop, req, path, cb); } -int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) +int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) { return Uvfs::ins->uv_fs_stat(loop, req, path, cb); } -int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime, - double mtime, uv_fs_cb cb) +int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) { return Uvfs::ins->uv_fs_utime(loop, req, path, atime, mtime, cb); } -int uv_fs_scandir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - uv_fs_cb cb) +int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) { return Uvfs::ins->uv_fs_scandir(loop, req, path, flags, cb); } -int uv_fs_scandir_next(uv_fs_t* req, uv_dirent_t* ent) +int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) { return Uvfs::ins->uv_fs_scandir_next(req, ent); } -int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) +int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) { return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path, - const char* new_path, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) { return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); } -int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - int mode, uv_fs_cb cb) +int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) { return Uvfs::ins->uv_fs_open(loop, req, path, flags, mode, cb); } -int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file fd, - int64_t offset, uv_fs_cb cb) +int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) { return Uvfs::ins->uv_fs_ftruncate(loop, req, fd, offset, cb); } -int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file fd, const uv_buf_t bufs[], - unsigned int nbufs, int64_t offset, uv_fs_cb cb) +int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); } + +int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_realpath(loop, req, path, cb); +} + +int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_close(loop, req, file, cb); +} + +int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_fdatasync(loop, req, file, cb); +} + +int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_mkdir(loop, req, path, mode, cb); +} + +int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_access(loop, req, path, flags, cb); +} + +int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_mkdtemp(loop, req, tpl, cb); +} + +int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_unlink(loop, req, path, cb); +} + +void uv_fs_req_cleanup(uv_fs_t *req) +{ + return Uvfs::ins->uv_fs_req_cleanup(req); +} + +int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_rename(loop, req, path, newPath, cb); +} + +int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_fsync(loop, req, file, cb); +} + +int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index ad0522f9e..294e120a8 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -13,10 +13,10 @@ * limitations under the License. */ -#ifndef UV_FS_READ_MOCK_H -#define UV_FS_READ_MOCK_H +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H -#include "read_core.h" +#include "uv.h" #include @@ -27,57 +27,67 @@ public: static inline std::shared_ptr ins = nullptr; public: virtual ~Uvfs() = default; - virtual int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t off, - uv_fs_cb cb) = 0; - virtual int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, - uv_fs_cb cb) = 0; - virtual int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime, + virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, + int64_t off, uv_fs_cb cb) = 0; + virtual int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) = 0; - virtual int uv_fs_scandir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, + virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; + virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; + virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) = 0; - virtual int uv_fs_scandir_next(uv_fs_t* req, uv_dirent_t* ent) = 0; - virtual int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path, - const char* new_path, int flags, uv_fs_cb cb) = 0; - virtual int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - int mode, uv_fs_cb cb) = 0; - virtual int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file fd, + virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; + virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, + int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; - virtual int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file fd, const uv_buf_t bufs[], - unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) = 0; + virtual int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; + virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; + virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; + virtual void uv_fs_req_cleanup(uv_fs_t *req) = 0; + virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, + uv_fs_cb cb) = 0; }; class UvfsMock : public Uvfs { public: - MOCK_METHOD7(uv_fs_read, int(uv_loop_t* loop, uv_fs_t* req, - uv_file file, - const uv_buf_t bufs[], - unsigned int nbufs, - int64_t off, - uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_stat, int(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_utime, int(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime, - double mtime, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, + MOCK_METHOD7(uv_fs_read, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, + int64_t off, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_stat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)); - MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t* req, uv_dirent_t* ent)); - MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t* loop, uv_fs_t* req, const char* path, - const char* new_path, int flags, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_open, int(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, - int mode, uv_fs_cb cb)); - MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t* loop, uv_fs_t* req, uv_file fd, + MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); + MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); + MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_write, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb)); - MOCK_METHOD7(uv_fs_write, int(uv_loop_t* loop, uv_fs_t* req, uv_file fd, const uv_buf_t bufs[], - unsigned int nbufs, int64_t offset, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_realpath, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_close, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_fdatasync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_mkdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_access, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); + MOCK_METHOD1(uv_fs_req_cleanup, void(uv_fs_t *req)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, + size_t len, uv_fs_cb cb)); }; - -} // OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file -- Gitee From 9773cd9ff11fd6c49c61ddd988dadf3cf766d5df Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 01:15:50 +0000 Subject: [PATCH 05/39] update interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h. Signed-off-by: tianp --- .../test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 38c3b3a7c..42760eaa3 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -37,12 +37,10 @@ public: virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; - virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb) = 0; virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; @@ -70,7 +68,7 @@ public: MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); -- Gitee From 4e9646312aae19ad25e945ba3a7f97904f7f9b10 Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 01:16:39 +0000 Subject: [PATCH 06/39] update interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp. Signed-off-by: tianp --- .../test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index e4ff6703d..6f7172b8a 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -53,7 +53,7 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) { return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); } -- Gitee From 55d2b7170aa48c4d1596b54dc8b7110194e45602 Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 09:31:33 +0800 Subject: [PATCH 07/39] =?UTF-8?q?open&fileTDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/BUILD.gn | 1 + interfaces/test/unittest/js/BUILD.gn | 52 ++ .../mod_fs/class_file/fs_file_mock_test.cpp | 443 ++++++++++++++++++ .../js/mod_fs/class_file/fs_file_test.cpp | 224 +++++++++ .../js/mod_fs/properties/mock/system_mock.cpp | 40 ++ .../js/mod_fs/properties/mock/system_mock.h | 46 ++ .../mod_fs/properties/open_core_mock_test.cpp | 146 ++++++ .../js/mod_fs/properties/open_core_test.cpp | 193 ++++++++ 8 files changed, 1145 insertions(+) create mode 100644 interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..e7ec1360f 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,6 +17,7 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", ] diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b..779f4a869 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ + "mod_fs/class_file/fs_file_test.cpp", "mod_fs/properties/close_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", @@ -61,6 +62,57 @@ ohos_unittest("ani_file_fs_test") { "libuv:uv", ] + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/open_core_mock_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + defines = [ "private=public", ] diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp new file mode 100644 index 000000000..ffe9a7e67 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -0,0 +1,443 @@ +/* + * 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 "fs_file.h" +#include "../properties/mock/uv_fs_mock.h" +#include "../properties/mock/system_mock.h" +#include "file_entity.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsFileMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; + static inline shared_ptr sys = nullptr; +private: + std::unique_ptr fileEntity; + std::unique_ptr fsFile; +}; + +void FsFileMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + sys = std::make_shared(); + System::ins = sys; +} + +void FsFileMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; + System::ins = nullptr; + sys = nullptr; +} + +void FsFileMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + + fileEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + fileEntity->fd_ = std::make_unique(fdValue, isClosed); + fileEntity->path_ = "/data/testdir/testfile.txt"; + fileEntity->uri_ = ""; + fsFile = std::make_unique(std::move(fileEntity)); +} + +void FsFileMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsFileMockTest_GetPath_001 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_001"; + + fsFile->fileEntity->uri_ = "file:///storage/test.txt"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_001"; +} + +/** + * @tc.name: FsFileMockTest_GetPath_002 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/data/testdir/testfile.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_002"; +} + +/** + * @tc.name: FsFileMockTest_GetPath_003 + * @tc.desc: Test function of GetPath() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_003"; + + fsFile->fileEntity->path_ = "/invalid/path"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_003"; +} + +/** + * @tc.name: FsFileMockTest_GetName_004 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; + + fsFile->fileEntity->uri_ = "file:///storage/test.txt"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004"; +} + +/** + * @tc.name: FsFileMockTest_GetName_005 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_005"; + + fsFile->fileEntity->path_ = "/invalid/path"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_005"; +} + +/** + * @tc.name: FsFileMockTest_GetName_006 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006"; + + fsFile->fileEntity->path_ = "file.txt"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_006"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_007 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_007"; + + fsFile->fileEntity->uri_ = "file:///storage/test.txt"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_008 + * @tc.desc: Test function of GetParent() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_008"; + + fsFile->fileEntity->path_ = "/invalid/path"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_008"; +} + +/** + * @tc.name: FsFileMockTest_GetName_009 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_009"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/testfile.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_009"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_010 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_010"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/data/testdir"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_010"; +} + +/** + * @tc.name: FsFileMockTest_GetName_011 + * @tc.desc: Test function of GetName() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_011"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("testfile.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_011"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_012 + * @tc.desc: Test function of GetParent() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_012"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("testdir"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_012"; +} + +/** + * @tc.name: FsFileMockTest_Lock_013 + * @tc.desc: Test function of Lock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_013"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->Lock(true); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_013"; +} + +/** + * @tc.name: FsFileMockTest_Lock_014 + * @tc.desc: Test function of Lock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_014"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->Lock(false); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_014"; +} + +/** + * @tc.name: FsFileMockTest_TryLock_015 + * @tc.desc: Test function of TryLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_015"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->TryLock(true); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_015"; +} + +/** + * @tc.name: FsFileMockTest_TryLock_016 + * @tc.desc: Test function of TryLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_016"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->TryLock(false); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_016"; +} + +/** + * @tc.name: FsFileMockTest_UnLock_017 + * @tc.desc: Test function of UnLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_017"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_017"; +} + +/** + * @tc.name: FsFileMockTest_UnLock_018 + * @tc.desc: Test function of UnLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_018, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_018"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_018"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp new file mode 100644 index 000000000..f73a0c185 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -0,0 +1,224 @@ +/* + * 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 "fs_file.h" +#include "file_entity.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsFileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +private: + std::unique_ptr fileEntity; + std::unique_ptr fsFile; +}; + +void FsFileTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsFileTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsFileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + + fileEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + fileEntity->fd_ = std::make_unique(fdValue, isClosed); + fileEntity->path_ = "/data/testdir/testfile.txt"; + fileEntity->uri_ = ""; + fsFile = std::make_unique(std::move(fileEntity)); +} + +void FsFileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsFileTest_Constructor_001 + * @tc.desc: Test function of FsFile::Constructor() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_Constructor_001"; + + auto result = FsFile::Constructor(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_Constructor_001"; +} + +/** + * @tc.name: FsFileTest_GetFD_002 + * @tc.desc: Test function of GetFD() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetFD_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetFD_002"; + + auto result = fsFile->GetFD(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetFD_002"; +} + +/** + * @tc.name: FsFileTest_GetFD_003 + * @tc.desc: Test function of GetFD() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetFD_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetFD_003"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->GetFD(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetFD_003"; +} + +/** + * @tc.name: FsFileTest_GetPath_004 + * @tc.desc: Test function of GetPath() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetPath_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_004"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetPath_004"; +} + +/** + * @tc.name: FsFileTest_GetName_005 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetName_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_005"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetName_005"; +} + +/** + * @tc.name: FsFileTest_GetParent_006 + * @tc.desc: Test function of GetParent() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetParent_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_006"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetParent_006"; +} + +/** + * @tc.name: FsFileTest_Lock_007 + * @tc.desc: Test function of Lock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_Lock_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_Lock_007"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->Lock(true); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_Lock_007"; +} + +/** + * @tc.name: FsFileTest_TryLock_008 + * @tc.desc: Test function of TryLock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_TryLock_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_TryLock_008"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->TryLock(true); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_TryLock_008"; +} + +/** + * @tc.name: FsFileTest_UnLock_009 + * @tc.desc: Test function of UnLock() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_UnLock_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_UnLock_009"; + + fsFile = std::make_unique(nullptr); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_UnLock_009"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp new file mode 100644 index 000000000..5878a9304 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp @@ -0,0 +1,40 @@ +/* + * 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 "system_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { +int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) +{ + return System::ins->setxattr(path, name, value, size, flags); +} + +int getxattr(const char *path, const char *name, void *value, size_t size) +{ + return System::ins->getxattr(path, name, value, size); +} + +int fgetxattr(int filedes, const char *name, void *value, size_t size) +{ + return System::ins->fgetxattr(filedes, name, value, size); +} + +int flock(int fd, int operation) +{ + return System::ins->flock(fd, operation); +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h new file mode 100644 index 000000000..3ff74be78 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h @@ -0,0 +1,46 @@ +/* + * 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_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class System { +public: + static inline std::shared_ptr ins = nullptr; + +public: + virtual ~System() = default; + virtual int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) = 0; + virtual int getxattr(const char *path, const char *name, void *value, size_t size) = 0; + virtual int fgetxattr(int filedes, const char *name, void *value, size_t size) = 0; + virtual int flock(int fd, int operation) = 0; +}; + +class SystemMock : public System { +public: + MOCK_METHOD5(setxattr, int(const char *path, const char *name, const void *value, size_t size, int flags)); + MOCK_METHOD4(getxattr, int(const char *path, const char *name, void *value, size_t size)); + MOCK_METHOD4(fgetxattr, int(int filedes, const char *name, void *value, size_t size)); + MOCK_METHOD2(flock, int(int fd, int operation)); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp new file mode 100644 index 000000000..3a277ed75 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -0,0 +1,146 @@ +/* + * 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 "open_core.h" +#include "mock/uv_fs_mock.h" + +#include +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class OpenCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void OpenCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void OpenCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void OpenCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void OpenCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: OpenCoreMockTest_DoOpen_001 + * @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_001"; + + string path = "/test/path.txt"; + int32_t mode = 0; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_001"; +} + +/** + * @tc.name: OpenCoreMockTest_DoOpen_002 + * @tc.desc: Test function of OpenCore::DoOpen interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_002"; + + string path = "file://test/path.txt"; + int32_t mode = 0; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_002"; +} + +/** + * @tc.name: OpenCoreMockTest_DoOpen_003 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_003"; + + string path = "file://test/path.txt"; + int32_t mode = 0; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_003"; +} + +/** + * @tc.name: OpenCoreMockTest_DoOpen_004 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_004"; + + string path = "/test/path.txt"; + int32_t mode = 0; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreMockTest-end OpenCoreMockTest_DoOpen_004"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp new file mode 100644 index 000000000..a95aa20bc --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -0,0 +1,193 @@ +/* + * 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 "open_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class OpenCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void OpenCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void OpenCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void OpenCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void OpenCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_001 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_001"; + + string path = "/test/path.txt"; + int32_t mode = -1; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_001"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_002 + * @tc.desc: Test function of OpenCore::DoOpen interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_002"; + + string path = "/test/path.txt"; + int32_t mode = 3; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_002"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_003 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_003"; + + string path = "file://media/image.jpg"; + int32_t mode = 0; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_003"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_004 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_004"; + + string path = "file://docs/non_existent.pdf"; + int32_t mode = 0; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_004"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_005 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_005"; + + string path = "content://com.example.provider/file.txt"; + int32_t mode = 0; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_005"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_006 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_006"; + + string path = "datashare://media/image.jpg"; + int32_t mode = 0; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_006"; +} + +/** + * @tc.name: OpenCoreTest_DoOpen_007 + * @tc.desc: Test function of OpenCore::DoOpen interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_007"; + + string path = "invalid://path/to/file"; + int32_t mode = 0; + + auto res = OpenCore::DoOpen(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "OpenCoreTest-end OpenCoreTest_DoOpen_007"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From bf1e22f4d6e147de8eea2d8a34eaffab8a9563a6 Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 09:44:57 +0800 Subject: [PATCH 08/39] =?UTF-8?q?build.gn=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../js/src/mod_fs/properties/create_randomaccessfile_core.cpp | 2 +- interfaces/test/unittest/js/BUILD.gn | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp index 1ef94cd44..f0c8692a2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -30,7 +30,7 @@ using namespace std; static tuple ParseStringToFileInfo(const string &path) { - if (strlen(path.c_str()) < 0) { + if (path.empty()) { HILOGE("The first argument requires filepath/file"); return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; } diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 7ce0e4084..c8b5596d6 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -77,8 +77,12 @@ ohos_unittest("ani_file_fs_mock_test") { resource_config_file = "../resource/ohos_test.xml" include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", ] -- Gitee From 594e08ddae7cd859c40deaaa3514850478880373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 10:03:38 +0800 Subject: [PATCH 09/39] =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_atomicfile/fs_atomicfile_test.cpp | 24 +++++++++---------- .../js/mod_fs/class_stream/mock/c_mock.cpp | 1 - .../js/mod_fs/class_stream/mock/c_mock.h | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp index 081de4750..f506c732f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp @@ -23,8 +23,8 @@ namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace std; namespace fs = std::filesystem; -string filePath = "/data/test/FsAtomicfileTest.txt"; -string deleteFile = "/data/test/FsAtomicfileDelTest.txt"; +string g_filePath = "/data/test/FsAtomicfileTest.txt"; +string g_deleteFile = "/data/test/FsAtomicfileDelTest.txt"; class FsAtomicfileTest : public testing::Test { public: @@ -36,7 +36,7 @@ public: void FsAtomicfileTest::SetUpTestCase(void) { - ofstream tempfile(filePath); + ofstream tempfile(g_filePath); tempfile << "hello world"; tempfile.close(); GTEST_LOG_(INFO) << "SetUpTestCase"; @@ -44,7 +44,7 @@ void FsAtomicfileTest::SetUpTestCase(void) void FsAtomicfileTest::TearDownTestCase(void) { - filesystem::remove(filePath); + filesystem::remove(g_filePath); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -69,12 +69,12 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); string path = stream->GetPath(); - EXPECT_EQ(path, filePath); + EXPECT_EQ(path, g_filePath); GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; } @@ -90,7 +90,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -181,7 +181,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSi { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -248,7 +248,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -276,7 +276,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -304,7 +304,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; - auto ret = FsAtomicFile::Constructor(deleteFile); + auto ret = FsAtomicFile::Constructor(g_deleteFile); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -332,7 +332,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp index a64f834fa..f9567b0d5 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -28,5 +28,4 @@ long ftell(FILE *stream) { return ICMock::ins->ftell(stream); } - } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h index 2fbdcee16..8f2bbe88e 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -16,7 +16,7 @@ #ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H -#include +#include #include #include -- Gitee From 54014e72aed5f3246da37be61cb2d5c370a54281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 10:37:16 +0800 Subject: [PATCH 10/39] del atomic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_atomicfile/fs_atomicfile_test.cpp | 366 ------------------ 1 file changed, 366 deletions(-) delete mode 100644 interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp deleted file mode 100644 index f506c732f..000000000 --- a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/* - * 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 -#include "fs_atomicfile.h" - -namespace OHOS::FileManagement::ModuleFileIO::Test { -using namespace std; -namespace fs = std::filesystem; - -string g_filePath = "/data/test/FsAtomicfileTest.txt"; -string g_deleteFile = "/data/test/FsAtomicfileDelTest.txt"; - -class FsAtomicfileTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void FsAtomicfileTest::SetUpTestCase(void) -{ - ofstream tempfile(g_filePath); - tempfile << "hello world"; - tempfile.close(); - GTEST_LOG_(INFO) << "SetUpTestCase"; -} - -void FsAtomicfileTest::TearDownTestCase(void) -{ - filesystem::remove(g_filePath); - GTEST_LOG_(INFO) << "TearDownTestCase"; -} - -void FsAtomicfileTest::SetUp(void) -{ - GTEST_LOG_(INFO) << "SetUp"; -} - -void FsAtomicfileTest::TearDown(void) -{ - GTEST_LOG_(INFO) << "TearDown"; -} - -/** - * @tc.name: FsAtomicfileTest_GetPath_001 - * @tc.desc: Test function of FsAtomicFile::GetPath interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - string path = stream->GetPath(); - EXPECT_EQ(path, g_filePath); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; -} - -/** - * @tc.name: FsAtomicfileTest_GetBaseFile_001 - * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->GetBaseFile(); - EXPECT_TRUE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_001"; -} - -/** - * @tc.name: FsAtomicfileTest_GetBaseFile_002 - * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for path > PATH_MAX. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_002"; - - size_t largeLength = static_cast(PATH_MAX) + 1; - string largeString(largeLength, 'a'); - - auto ret = FsAtomicFile::Constructor(largeString); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->GetBaseFile(); - EXPECT_FALSE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_002"; -} - -/** - * @tc.name: FsAtomicfileTest_GetBaseFile_003 - * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed realpath. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_003, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_003"; - - string path = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - - auto ret = FsAtomicFile::Constructor(path); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->GetBaseFile(); - EXPECT_FALSE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_003"; -} - -/** - * @tc.name: FsAtomicfileTest_GetBaseFile_004 - * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed open. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_004"; - - string path = "/data/test/aaaaaaaaaa.txt"; - - auto ret = FsAtomicFile::Constructor(path); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->GetBaseFile(); - EXPECT_FALSE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_004"; -} - -/** - * @tc.name: FsAtomicfileTest_StartWrite_001 - * @tc.desc: Test function of FsAtomicFile::StartWrite interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - EXPECT_TRUE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_001"; -} - -/** - * @tc.name: FsAtomicfileTest_StartWrite_002 - * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no parent dir. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_002"; - - string path = "/data/local/tmp/test/test/test/test.txt"; - - auto ret = FsAtomicFile::Constructor(path); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - EXPECT_FALSE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_002"; -} - -/** - * @tc.name: FsAtomicfileTest_StartWrite_003 - * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no permission. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_003, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_003"; - - string path = "/sys/kernel/address_bits"; - - auto ret = FsAtomicFile::Constructor(path); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - EXPECT_FALSE(retFl.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_003"; -} - -/** - * @tc.name: FsAtomicfileTest_FinishWrite_001 - * @tc.desc: Test function of FsAtomicFile::FinishWrite interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - ASSERT_TRUE(retFl.IsSuccess()); - string newPath = retFl.GetData().value(); - ofstream tempfile(newPath); - tempfile << "hello world"; - tempfile.close(); - - auto retFW = stream->FinishWrite(); - EXPECT_TRUE(retFW.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FinishWrite_001"; -} - -/** - * @tc.name: FsAtomicfileTest_FailWrite_001 - * @tc.desc: Test function of FsAtomicFile::FailWrite interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - ASSERT_TRUE(retFl.IsSuccess()); - string newPath = retFl.GetData().value(); - ofstream tempfile(newPath); - tempfile << "hello world"; - tempfile.close(); - - auto retFW = stream->FailWrite(); - EXPECT_TRUE(retFW.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FailWrite_001"; -} - -/** - * @tc.name: FsAtomicfileTest_Delete_001 - * @tc.desc: Test function of FsAtomicFile::Delete interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; - - auto ret = FsAtomicFile::Constructor(g_deleteFile); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto retFl = stream->StartWrite(); - ASSERT_TRUE(retFl.IsSuccess()); - string newPath = retFl.GetData().value(); - ofstream tempfile(newPath); - tempfile << "hello world"; - tempfile.close(); - - auto retFW = stream->Delete(); - EXPECT_TRUE(retFW.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_Delete_001"; -} - -/** - * @tc.name: FsAtomicfileTest_ReadFully_001 - * @tc.desc: Test function of FsAtomicFile::ReadFully interface for succ. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; - - auto ret = FsAtomicFile::Constructor(g_filePath); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto result = stream->ReadFully(); - ASSERT_TRUE(result.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_001"; -} - -/** - * @tc.name: FsAtomicfileTest_ReadFully_002 - * @tc.desc: Test function of FsAtomicFile::ReadFully interface for valied path. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_002"; - - auto ret = FsAtomicFile::Constructor("aaaaaaaaaaaaaaaa"); - ASSERT_TRUE(ret.IsSuccess()); - - shared_ptr stream(move(ret.GetData().value())); - auto result = stream->ReadFully(); - ASSERT_FALSE(result.IsSuccess()); - - GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_002"; -} - -} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From 5989ae0985d8d43f838dbb29776b70a3138d278d Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 17:04:22 +0800 Subject: [PATCH 11/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0uv=5Ffs=5Fmock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 38 ++++++++++++++++--- .../js/mod_fs/properties/mock/uv_fs_mock.h | 38 +++++++++++++------ 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 13ceb7b54..616e607e6 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -17,8 +17,8 @@ using namespace OHOS::FileManagement::ModuleFileIO; -int uv_fs_read( - uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) +int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); } @@ -68,12 +68,22 @@ int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, u return Uvfs::ins->uv_fs_ftruncate(loop, req, fd, offset, cb); } -int uv_fs_write( - uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) +int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); } +int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_realpath(loop, req, path, cb); +} + +int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_close(loop, req, file, cb); +} + int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) { return Uvfs::ins->uv_fs_fdatasync(loop, req, file, cb); @@ -99,4 +109,22 @@ int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_unlink(loop, req, path, cb); } -void uv_fs_req_cleanup(uv_fs_t *req) {} +void uv_fs_req_cleanup(uv_fs_t *req) +{ + return Uvfs::ins->uv_fs_req_cleanup(req); +} + +int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_rename(loop, req, path, newPath, cb); +} + +int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_fsync(loop, req, file, cb); +} + +int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 6b0f3c5fd..42760eaa3 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -16,7 +16,7 @@ #ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H -#include "read_core.h" +#include "uv.h" #include @@ -32,46 +32,60 @@ public: int64_t off, uv_fs_cb cb) = 0; virtual int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_utime( - uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) = 0; + virtual int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, + double mtime, uv_fs_cb cb) = 0; virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; - virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink( - uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) = 0; + virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, + uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; + virtual void uv_fs_req_cleanup(uv_fs_t *req) = 0; + virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, + uv_fs_cb cb) = 0; }; class UvfsMock : public Uvfs { public: MOCK_METHOD7(uv_fs_read, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, - int64_t off, uv_fs_cb cb)); + int64_t off, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_stat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6( - uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, + uv_fs_cb cb)); MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, - int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, + uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_write, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb)); + int64_t offset, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_realpath, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_close, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_fdatasync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_mkdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_access, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); + MOCK_METHOD1(uv_fs_req_cleanup, void(uv_fs_t *req)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, + size_t len, uv_fs_cb cb)); }; } // namespace OHOS::FileManagement::ModuleFileIO -- Gitee From d2ac9e03e51fc8a092e7d0aa09ac7cfc8bd3a31d Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 17:19:17 +0800 Subject: [PATCH 12/39] =?UTF-8?q?class=5Fstat\statvfsTDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/BUILD.gn | 2 + interfaces/test/unittest/js/BUILD.gn | 80 +++ .../mod_fs/class_stat/fs_stat_mock_test.cpp | 114 +++++ .../js/mod_fs/class_stat/fs_stat_test.cpp | 472 ++++++++++++++++++ .../js/mod_fs/properties/mock/system_mock.cpp | 40 ++ .../js/mod_fs/properties/mock/system_mock.h | 46 ++ .../js/mod_statvfs/statvfs_core_test.cpp | 134 +++++ 7 files changed, 888 insertions(+) create mode 100644 interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h create mode 100644 interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..b1756e7f4 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,6 +17,8 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", + "js:ani_file_statvfs_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", ] diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b..c8ba826f7 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ + "mod_fs/class_stat/fs_stat_test.cpp", "mod_fs/properties/close_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", @@ -64,4 +65,83 @@ ohos_unittest("ani_file_fs_test") { defines = [ "private=public", ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_stat/fs_stat_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_statvfs_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_statvfs/statvfs_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_statvfs", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_statvfs", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + ] } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp new file mode 100644 index 000000000..b6e874658 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -0,0 +1,114 @@ +/* + * 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 "fs_stat.h" +#include "../properties/mock/system_mock.h" +#include "fs_stat_entity.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsStatMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr sys = nullptr; +}; + +void FsStatMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + sys = std::make_shared(); + System::ins = sys; +} + +void FsStatMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + System::ins = nullptr; + sys = nullptr; +} + +void FsStatMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsStatMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsStatMockTest_GetLocation_001 + * @tc.desc: Test function of GetLocation() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_001"; + + std::unique_ptr statEntity; + std::unique_ptr fsStat; + statEntity = std::make_unique(); + statEntity->fileInfo_ = std::make_unique(); + statEntity->fileInfo_->isPath = true; + statEntity->fileInfo_->path = std::make_unique(100); + strcpy(statEntity->fileInfo_->path.get(), "/test/path"); + fsStat = std::make_unique(std::move(statEntity)); + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)); + EXPECT_EQ(fsStat->GetLocation(), 1); + + GTEST_LOG_(INFO) << "FsStatMockTes-end FsStatMockTest_GetLocation_001"; +} + +/** + * @tc.name: FsStatMockTest_GetLocation_002 + * @tc.desc: Test function of GetLocation() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_002"; + + std::unique_ptr statEntity; + std::unique_ptr fsStat; + statEntity = std::make_unique(); + statEntity->fileInfo_ = std::make_unique(); + statEntity->fileInfo_->isPath = false; + const int fdValue = 3; + const bool isClosed = false; + statEntity->fileInfo_->fdg = std::make_unique(fdValue, isClosed); + fsStat = std::make_unique(std::move(statEntity)); + + EXPECT_CALL(*sys, fgetxattr(_, _, _, _)).WillOnce(Return(1)); + EXPECT_EQ(fsStat->GetLocation(), 1); + + GTEST_LOG_(INFO) << "FsStatMockTes-end FsStatMockTest_GetLocation_002"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp new file mode 100644 index 000000000..15a8640f2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp @@ -0,0 +1,472 @@ +/* + * 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 "fs_stat.h" + +#include + + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsStatTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsStatTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsStatTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsStatTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsStatTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsStatTest_Constructor_001 + * @tc.desc: Test FsStat::Constructor for success case + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_Constructor_001"; + + auto stat = FsStat::Constructor(); + EXPECT_NE(stat, nullptr); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_Constructor_001"; +} + +/** + * @tc.name: FsStatTest_IsBlockDevice_001 + * @tc.desc: Test FsStat::IsBlockDevice for directory + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsBlockDevice_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsBlockDevice_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFBLK; + EXPECT_TRUE(stat->IsBlockDevice()); + EXPECT_FALSE(stat->IsFile()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsBlockDevice_001"; +} + +/** + * @tc.name: FsStatTest_IsCharacterDevice_001 + * @tc.desc: Test FsStat::IsCharacterDevice for directory + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsCharacterDevice_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsCharacterDevice_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFCHR; + EXPECT_TRUE(stat->IsCharacterDevice()); + EXPECT_FALSE(stat->IsFile()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsCharacterDevice_001"; +} + +/** + * @tc.name: FsStatTest_IsDirectory_001 + * @tc.desc: Test FsStat::IsDirectory for directory + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsDirectory_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsDirectory_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFDIR | 0755; + EXPECT_TRUE(stat->IsDirectory()); + EXPECT_FALSE(stat->IsFile()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsDirectory_001"; +} + +/** + * @tc.name: FsStatTest_IsFIFO_001 + * @tc.desc: Test FsStat::IsFIFO for directory + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsFIFO_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsFIFO_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFIFO; + EXPECT_TRUE(stat->IsFIFO()); + EXPECT_FALSE(stat->IsFile()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsFIFO_001"; +} + +/** + * @tc.name: FsStatTest_IsFile_001 + * @tc.desc: Test FsStat::IsFile for regular file + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsFile_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFREG | 0644; + EXPECT_TRUE(stat->IsFile()); + EXPECT_FALSE(stat->IsDirectory()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsFile_001"; +} + +/** + * @tc.name: FsStatTest_IsSocket_001 + * @tc.desc: Test FsStat::IsSocket for symbolic link + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsSocket_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsSocket_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFSOCK; + EXPECT_TRUE(stat->IsSocket()); + EXPECT_FALSE(stat->IsDirectory()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsSocket_001"; +} + +/** + * @tc.name: FsStatTest_IsSymbolicLink_001 + * @tc.desc: Test FsStat::IsSymbolicLink for symbolic link + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_IsSymbolicLink_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_IsSymbolicLink_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFLNK | 0777; + EXPECT_TRUE(stat->IsSymbolicLink()); + EXPECT_FALSE(stat->IsDirectory()); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_IsSymbolicLink_001"; +} + +/** + * @tc.name: FsStatTest_GetIno_001 + * @tc.desc: Test FsStat::GetIno for valid inode number + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetIno_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetIno_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_ino = 123456789; + EXPECT_EQ(stat->GetIno(), 123456789); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetIno_001"; +} + +/** + * @tc.name: FsStatTest_GetMode_001 + * @tc.desc: Test FsStat::GetMode for permission bits + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetMode_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMode_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mode = S_IFREG | 0755; + EXPECT_EQ(stat->GetMode(), 0755); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMode_001"; +} + +/** + * @tc.name: FsStatTest_GetUid_001 + * @tc.desc: Test FsStat::GetUid for user ID + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetUid_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetUid_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_uid = 1000; + EXPECT_EQ(stat->GetUid(), 1000); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetUid_001"; +} + +/** + * @tc.name: FsStatTest_GetGid_001 + * @tc.desc: Test FsStat::GetGid for group ID + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetGid_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetGid_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_gid = 1000; + EXPECT_EQ(stat->GetGid(), 1000); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetGid_001"; +} + +/** + * @tc.name: FsStatTest_GetSize_001 + * @tc.desc: Test FsStat::GetSize for file size + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetSize_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_size = 123456789; + EXPECT_EQ(stat->GetSize(), 123456789); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetSize_001"; +} + +/** + * @tc.name: FsStatTest_GetAtime_001 + * @tc.desc: Test FsStat::GetAtime for access time in seconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetAtime_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetAtime_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_atim.tv_sec = 1630473600; + stat->entity->stat_.st_atim.tv_nsec = 500000000; + + EXPECT_EQ(stat->GetAtime(), 1630473600); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetAtime_001"; +} + +/** + * @tc.name: FsStatTest_GetMtime_001 + * @tc.desc: Test FsStat::GetMtime for modification time in seconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetMtime_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMtime_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mtim.tv_sec = 1630473601; + stat->entity->stat_.st_mtim.tv_nsec = 500000000; + + EXPECT_EQ(stat->GetMtime(), 1630473601); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMtime_001"; +} + +/** + * @tc.name: FsStatTest_GetCtime_001 + * @tc.desc: Test FsStat::GetCtime for change time in seconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetCtime_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetCtime_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_ctim.tv_sec = 1630473602; + stat->entity->stat_.st_ctim.tv_nsec = 500000000; + + EXPECT_EQ(stat->GetCtime(), 1630473602); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetCtime_001"; +} + +/** + * @tc.name: FsStatTest_GetAtimeNs_001 + * @tc.desc: Test FsStat::GetAtimeNs for access time in nanoseconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetAtimeNs_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetAtimeNs_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_atim.tv_sec = 1630473600; + stat->entity->stat_.st_atim.tv_nsec = 500000000; + + int64_t expected = 1630473600LL * 1000000000 + 500000000; + EXPECT_EQ(stat->GetAtimeNs(), expected); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetAtimeNs_001"; +} + +/** + * @tc.name: FsStatTest_GetMtimeNs_001 + * @tc.desc: Test FsStat::GetMtimeNs for modification time in nanoseconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetMtimeNs_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetMtimeNs_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_mtim.tv_sec = 1630473601; + stat->entity->stat_.st_mtim.tv_nsec = 500000000; + + int64_t expected = 1630473601LL * 1000000000 + 500000000; + EXPECT_EQ(stat->GetMtimeNs(), expected); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetMtimeNs_001"; +} + +/** + * @tc.name: FsStatTest_GetCtimeNs_001 + * @tc.desc: Test FsStat::GetCtimeNs for change time in nanoseconds + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStatTest, FsStatTest_GetCtimeNs_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStatTest-begin FsStatTest_GetCtimeNs_001"; + + auto stat = FsStat::Constructor(); + ASSERT_NE(stat, nullptr); + + stat->entity->stat_.st_ctim.tv_sec = 1630473602; + stat->entity->stat_.st_ctim.tv_nsec = 500000000; + + int64_t expected = 1630473602LL * 1000000000 + 500000000; + EXPECT_EQ(stat->GetCtimeNs(), expected); + delete stat; + + GTEST_LOG_(INFO) << "FsStatTest-end FsStatTest_GetCtimeNs_001"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp new file mode 100644 index 000000000..5878a9304 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp @@ -0,0 +1,40 @@ +/* + * 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 "system_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { +int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) +{ + return System::ins->setxattr(path, name, value, size, flags); +} + +int getxattr(const char *path, const char *name, void *value, size_t size) +{ + return System::ins->getxattr(path, name, value, size); +} + +int fgetxattr(int filedes, const char *name, void *value, size_t size) +{ + return System::ins->fgetxattr(filedes, name, value, size); +} + +int flock(int fd, int operation) +{ + return System::ins->flock(fd, operation); +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h new file mode 100644 index 000000000..3ff74be78 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h @@ -0,0 +1,46 @@ +/* + * 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_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class System { +public: + static inline std::shared_ptr ins = nullptr; + +public: + virtual ~System() = default; + virtual int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) = 0; + virtual int getxattr(const char *path, const char *name, void *value, size_t size) = 0; + virtual int fgetxattr(int filedes, const char *name, void *value, size_t size) = 0; + virtual int flock(int fd, int operation) = 0; +}; + +class SystemMock : public System { +public: + MOCK_METHOD5(setxattr, int(const char *path, const char *name, const void *value, size_t size, int flags)); + MOCK_METHOD4(getxattr, int(const char *path, const char *name, void *value, size_t size)); + MOCK_METHOD4(fgetxattr, int(int filedes, const char *name, void *value, size_t size)); + MOCK_METHOD2(flock, int(int fd, int operation)); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp b/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp new file mode 100644 index 000000000..31ea3914b --- /dev/null +++ b/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp @@ -0,0 +1,134 @@ +/* + * 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 "statvfs_core.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class StatvFsCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void StatvFsCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open("/data/test/statvfs.txt", O_CREAT | O_RDWR, 0644); + close(fd); +} + +void StatvFsCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + rmdir("/data/test/statvfs.txt"); +} + +void StatvFsCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void StatvFsCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: StatvFsCoreTest_DoGetFreeSize_001 + * @tc.desc: Test function of DoGetFreeSize interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetFreeSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetFreeSize_001"; + + struct statvfs diskInfo; + diskInfo.f_bsize = 2; + diskInfo.f_bfree = 1; + + auto result = ModuleStatvfs::StatvfsCore::DoGetFreeSize("/data/test/statvfs.txt"); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetFreeSize_001"; +} + +/** + * @tc.name: StatvFsCoreTest_DoGetFreeSize_002 + * @tc.desc: Test function of DoGetFreeSize interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetFreeSize_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetFreeSize_002"; + + auto result = ModuleStatvfs::StatvfsCore::DoGetFreeSize("/test/path"); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetFreeSize_002"; +} + +/** + * @tc.name: StatvFsCoreTest_DoGetTotalSize_003 + * @tc.desc: Test function of DoGetTotalSize interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetTotalSize_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetTotalSize_003"; + + struct statvfs diskInfo; + diskInfo.f_bsize = 2; + diskInfo.f_blocks = 1; + + auto result = ModuleStatvfs::StatvfsCore::DoGetTotalSize("/data/test/statvfs.txt"); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_003"; +} + +/** + * @tc.name: StatvFsCoreTest_DoGetTotalSize_004 + * @tc.desc: Test function of DoGetTotalSize interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetTotalSize_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetTotalSize_004"; + + auto result = ModuleStatvfs::StatvfsCore::DoGetTotalSize("/test/path"); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_004"; +} + +} \ No newline at end of file -- Gitee From c9989cba7f2f6dc52c2f598905f01288164a9e62 Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 20:22:54 +0800 Subject: [PATCH 13/39] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../mod_fs/properties/create_randomaccessfile_core.cpp | 4 ---- .../fs_randomaccessfile_mock_test.cpp | 9 +++++---- .../class_randomaccessfile/fs_randomaccessfile_test.cpp | 6 +++--- .../create_randomaccessfile_core_mock_test.cpp | 8 ++++---- .../properties/create_randomaccessfile_core_test.cpp | 4 ++-- .../unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp | 2 +- .../test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h | 2 -- 7 files changed, 15 insertions(+), 20 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp index f0c8692a2..d7d99779d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -30,10 +30,6 @@ using namespace std; static tuple ParseStringToFileInfo(const string &path) { - if (path.empty()) { - HILOGE("The first argument requires filepath/file"); - return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; - } OHOS::DistributedFS::FDGuard sfd; auto fdg = CreateUniquePtr(sfd, false); if (fdg == nullptr) { diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp index 443a7e3c3..321d4f59e 100644 --- a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp @@ -13,12 +13,13 @@ * limitations under the License. */ -#include "fs_randomaccessfile.h" -#include "../properties/mock/uv_fs_mock.h" +#include +#include + #include "file_entity.h" +#include "fs_randomaccessfile.h" +#include "uv_fs_mock.h" -#include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp index a863fee9c..5930ef014 100644 --- a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp @@ -13,11 +13,11 @@ * limitations under the License. */ -#include "fs_randomaccessfile.h" -#include "file_entity.h" - #include +#include "file_entity.h" +#include "fs_randomaccessfile.h" + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp index a3628289f..2dc12961c 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp @@ -13,11 +13,11 @@ * limitations under the License. */ -#include "create_randomaccessfile_core.h" -#include "mock/uv_fs_mock.h" - -#include #include +#include + +#include "create_randomaccessfile_core.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp index 1a3cf17be..5519721f9 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ -#include "create_randomaccessfile_core.h" - #include +#include "create_randomaccessfile_core.h" + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 6f7172b8a..aa3534347 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -111,7 +111,7 @@ int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) void uv_fs_req_cleanup(uv_fs_t *req) { - return Uvfs::ins->uv_fs_req_cleanup(req); + return; } int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 42760eaa3..736eebb6e 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -51,7 +51,6 @@ public: virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; - virtual void uv_fs_req_cleanup(uv_fs_t *req) = 0; virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) = 0; @@ -82,7 +81,6 @@ public: MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); - MOCK_METHOD1(uv_fs_req_cleanup, void(uv_fs_t *req)); MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb)); -- Gitee From cd5c5d117b9c35c889aa94aaa28e3114a21c94a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 20:07:22 +0800 Subject: [PATCH 14/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: 周鑫 --- .../class_stream/fs_stream_mock_test.cpp | 42 ++++++++++--------- .../unittest/js/mod_hash/hash_core_test.cpp | 16 +++---- .../securitylabel_core_test.cpp | 22 +++++----- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp index f13339006..7e5c30f65 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -18,26 +18,27 @@ #include "create_stream_core.h" #include "fs_utils.h" -#define STREAM_FILE_PATH "/data/test/FsStreamCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; + +static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; + class FsStreamMockTest : public testing::Test { public: static void SetUpTestCase(void) { mock_ = std::make_shared(); ICMock::ins = mock_; - int32_t fd = open(STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { ICMock::ins = nullptr; mock_ = nullptr; - rmdir(STREAM_FILE_PATH); + rmdir(g_streamFilePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -55,8 +56,8 @@ public: */ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -68,7 +69,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; } /** @@ -81,8 +82,8 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) */ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0002"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -95,7 +96,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0002"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; } /** @@ -108,8 +109,8 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) */ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -123,7 +124,7 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1 auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; } /** @@ -136,8 +137,8 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1 */ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0002"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -146,13 +147,13 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1 WriteOptions opt; opt.offset = 5; string buf = "FsStreamWriteTest_0002"; - auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), buf.length()), opt); EXPECT_FALSE(retWr.IsSuccess()); auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0002"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; } /** @@ -165,8 +166,8 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1 */ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -180,10 +181,11 @@ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) auto retRd = result->Read(arrayBuffer, opt); EXPECT_FALSE(retRd.IsSuccess()); + free(buffer); auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; } } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp index 6c13b0e6e..069a3230e 100644 --- a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp +++ b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp @@ -18,22 +18,22 @@ #include #include "hash_core.h" -#define FILE_PATH "/data/test/HashCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; + +static const string g_filePath = "/data/test/HashCoreTest.txt"; class HashCoreTest : public testing::Test { public: static void SetUpTestCase(void) { - int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { - rmdir(FILE_PATH); + rmdir(g_filePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -51,7 +51,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0001"; string alg = "sha128"; - auto ret = HashCore::DoHash(FILE_PATH, alg); + auto ret = HashCore::DoHash(g_filePath, alg); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -71,7 +71,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0002"; - auto ret = HashCore::DoHash(FILE_PATH, "md5"); + auto ret = HashCore::DoHash(g_filePath, "md5"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0002"; @@ -88,7 +88,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0003"; - auto ret = HashCore::DoHash(FILE_PATH, "sha1"); + auto ret = HashCore::DoHash(g_filePath, "sha1"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0003"; @@ -105,7 +105,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0004"; - auto ret = HashCore::DoHash(FILE_PATH, "sha256"); + auto ret = HashCore::DoHash(g_filePath, "sha256"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004"; diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp index ae28da157..f320af037 100644 --- a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -18,23 +18,25 @@ #include #include "securitylabel_core.h" -#define FILE_PATH "/data/test/SecurityLabelCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleSecurityLabel { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; + +static const string g_filePath = "/data/test/SecurityLabelCoreTest.txt"; +static const string g_validFilePath = "/data/test/validFilePath"; + class SecurityLabelCoreTest : public testing::Test { public: static void SetUpTestCase(void) { - int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { - rmdir(FILE_PATH); + rmdir(g_filePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -51,7 +53,7 @@ public: HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0001"; - auto ret = DoSetSecurityLabel(FILE_PATH, "abc"); + auto ret = DoSetSecurityLabel(g_filePath, "abc"); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -70,8 +72,8 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize. */ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetDoSetSecurityLabel_0002SecurityLabel_0001"; - auto ret = DoSetSecurityLabel("FILE_PATH", "s1"); + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0002"; + auto ret = DoSetSecurityLabel(g_validFilePath, "s1"); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -91,7 +93,7 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0003"; - auto ret = DoSetSecurityLabel(FILE_PATH, "s2"); + auto ret = DoSetSecurityLabel(g_filePath, "s2"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0003"; @@ -108,7 +110,7 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0001"; - auto ret = DoGetSecurityLabel("FILE_PATH"); + auto ret = DoGetSecurityLabel(g_validFilePath); EXPECT_TRUE(ret.IsSuccess()); const string level = ret.GetData().value(); @@ -128,7 +130,7 @@ HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0002"; - auto ret = DoGetSecurityLabel(FILE_PATH); + auto ret = DoGetSecurityLabel(g_filePath); EXPECT_TRUE(ret.IsSuccess()); const string level = ret.GetData().value(); -- Gitee From 2a1a882d93c70e9e9d198f3e37b0744bec08ca15 Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 22:51:17 +0800 Subject: [PATCH 15/39] =?UTF-8?q?=E6=84=8F=E8=A7=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/js/BUILD.gn | 8 ++------ .../properties/create_randomaccessfile_core_test.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index c8b5596d6..d3fc839a0 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -20,8 +20,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -35,13 +33,13 @@ ohos_unittest("ani_file_fs_test") { sources = [ "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", - "mod_fs/properties/create_randomaccessfile_core_test.cpp", + "mod_fs/class_stream/fs_stream_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", - "mod_fs/class_stream/fs_stream_test.cpp", ] deps = [ @@ -74,8 +72,6 @@ ohos_unittest("ani_file_fs_mock_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp index 5519721f9..848d97672 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -52,7 +52,7 @@ void CreateRandomAccessFileCoreTest::TearDown(void) /** * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001 - * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify an invalid mode. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 @@ -137,13 +137,15 @@ HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreate auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); EXPECT_EQ(res.IsSuccess(), false); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; } /**' * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005 - * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify the path is invalid. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -- Gitee From 3bc67d881a78a2a59716bfb3f60d76e70096947d Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 29 May 2025 23:17:53 +0800 Subject: [PATCH 16/39] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../js/mod_fs/class_file/fs_file_mock_test.cpp | 11 ++++++----- .../unittest/js/mod_fs/class_file/fs_file_test.cpp | 1 + .../unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp | 2 +- .../unittest/js/mod_fs/properties/mock/uv_fs_mock.h | 2 -- .../unittest/js/mod_fs/properties/open_core_test.cpp | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index ffe9a7e67..d9bfebf4b 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -12,13 +12,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "fs_file.h" -#include "../properties/mock/uv_fs_mock.h" -#include "../properties/mock/system_mock.h" -#include "file_entity.h" -#include #include +#include + +#include "file_entity.h" +#include "fs_file.h" +#include "system_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index f73a0c185..4728b8e7b 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "fs_file.h" #include "file_entity.h" diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 616e607e6..09f91974c 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -111,7 +111,7 @@ int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) void uv_fs_req_cleanup(uv_fs_t *req) { - return Uvfs::ins->uv_fs_req_cleanup(req); + return; } int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 42760eaa3..736eebb6e 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -51,7 +51,6 @@ public: virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; - virtual void uv_fs_req_cleanup(uv_fs_t *req) = 0; virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) = 0; @@ -82,7 +81,6 @@ public: MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); - MOCK_METHOD1(uv_fs_req_cleanup, void(uv_fs_t *req)); MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb)); diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp index a95aa20bc..b505270fc 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ -#include "open_core.h" - #include +#include "open_core.h" + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; -- Gitee From acb97f086fd22e3dfad606ba4625dfb99cec9e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Fri, 30 May 2025 15:15:02 +0800 Subject: [PATCH 17/39] atomicfile ani signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../src/common/ani_helper/ani_signature.cpp | 13 + .../js/src/common/ani_helper/ani_signature.h | 19 + .../src/common/ani_helper/type_converter.cpp | 10 +- .../js/src/mod_fs/ani/bind_function_class.cpp | 8 +- .../class_atomicfile/ani/atomicfile_ani.cpp | 41 +- interfaces/test/unittest/js/BUILD.gn | 1 + .../class_atomicfile/fs_atomicfile_test.cpp | 366 ++++++++++++++++++ 7 files changed, 436 insertions(+), 22 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 4a683a783..fb85eb18d 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -60,6 +60,7 @@ const string BuiltInTypes::Array::objectSetterSig = // BuiltInTypes::ArrayBuffer const Type BuiltInTypes::ArrayBuffer::classType = Builder::BuildClass("escompat.ArrayBuffer"); const string BuiltInTypes::ArrayBuffer::classDesc = BuiltInTypes::ArrayBuffer::classType.Descriptor(); +const string BuiltInTypes::ArrayBuffer::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::intType }); // BuiltInTypes::BigInt const Type BuiltInTypes::BigInt::classType = Builder::BuildClass("escompat.BigInt"); const string BuiltInTypes::BigInt::classDesc = BuiltInTypes::BigInt::classType.Descriptor(); @@ -115,6 +116,18 @@ const Type FS::WatchEventInner::classType = Builder::BuildClass("@ohos.file.fs.W const string FS::WatchEventInner::classDesc = FS::WatchEventInner::classType.Descriptor(); const string FS::WatchEventInner::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::doubleType, BasicTypes::doubleType }); +// FS::ReadStream +const Type FS::ReadStream::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ReadStream"); +const string FS::ReadStream::classDesc = FS::ReadStream::classType.Descriptor(); +const string FS::ReadStream::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +// FS::WriteStream +const Type FS::WriteStream::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WriteStream"); +const string FS::WriteStream::classDesc = FS::WriteStream::classType.Descriptor(); +const string FS::WriteStream::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +// FS::AtomicFile +const Type FS::AtomicFile::classType = Builder::BuildClass("@ohos.file.fs.fileIo.AtomicFile"); +const string FS::AtomicFile::classDesc = FS::AtomicFile::classType.Descriptor(); +const string FS::AtomicFile::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); // FS::LocationType const Type FS::LocationType::classType = Builder::BuildClass("@ohos.file.fs.fileIo.LocationType"); const string FS::LocationType::classDesc = FS::LocationType::classType.Descriptor(); diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index 8b7e43281..128f1104a 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -93,6 +93,7 @@ struct Array : public BaseType { struct ArrayBuffer : public BaseType { static const Type classType; static const string classDesc; + static const string ctorSig; }; struct BigInt : public BaseType { @@ -181,6 +182,24 @@ struct LocationType : public BaseType { static const string classDesc; }; +struct ReadStream : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct WriteStream : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct AtomicFile : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + } // namespace FS namespace Impl { diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 564d7bc56..59d97e909 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -225,16 +225,18 @@ std::tuple TypeConverter::ToAniArrayBuffer(ani_env *env, return { false, nullptr }; } - static const char *className = "Lescompat/ArrayBuffer;"; + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); ani_status ret; ani_class cls; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); return { false, nullptr }; } + auto ctorDesc = BuiltInTypes::ArrayBuffer::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::ArrayBuffer::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { HILOGE("Not found ctor, err: %{private}d", ret); return { false, nullptr }; } diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index ca851366c..cec91441f 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -166,7 +166,9 @@ static ani_status BindTaskSignalClassMethods(ani_env *env) static ani_status BindAtomicFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/AtomicFile;"; + auto classDesc = FS::AtomicFile::classDesc.c_str(); + auto ctorDesc = FS::AtomicFile::ctorDesc.c_str(); + auto ctorSig = FS::AtomicFile::ctorSig.c_str(); std::array methods = { ani_native_function { "getPath", nullptr, reinterpret_cast(AtomicFileAni::GetPath) }, @@ -176,11 +178,11 @@ static ani_status BindAtomicFileMethods(ani_env *env) ani_native_function { "nativeFinishWrite", nullptr, reinterpret_cast(AtomicFileAni::FinishWrite) }, ani_native_function { "nativeFailWrite", nullptr, reinterpret_cast(AtomicFileAni::FailWrite) }, ani_native_function { "delete", nullptr, reinterpret_cast(AtomicFileAni::Delete) }, - ani_native_function { "", "Lstd/core/String;:V", reinterpret_cast(AtomicFileAni::Constructor) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(AtomicFileAni::Constructor) }, ani_native_function { "openRead", nullptr, reinterpret_cast(AtomicFileAni::OpenRead) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); const static string mkdirCtorSig1 = diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index 5dc3e3bac..71666f6ea 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -17,6 +17,7 @@ #include +#include "ani_signature.h" #include "error_handler.h" #include "file_wrapper.h" #include "filemgmt_libhilog.h" @@ -30,6 +31,7 @@ namespace ANI { namespace fs = std::filesystem; using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; const std::string READ_STREAM_CLASS = "ReadStream"; const std::string WRITE_STREAM_CLASS = "WriteStream"; @@ -52,7 +54,8 @@ void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj } if (ANI_OK != - env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + env->Object_SetFieldByName_Long(obj, "nativePtr", + static_cast(reinterpret_cast(ret.GetData().value())))) { HILOGE("Failed to wrap entity for obj AtomicFile"); ErrorHandler::Throw(env, EIO); return; @@ -120,20 +123,24 @@ ani_object AtomicFileAni::GetBaseFile(ani_env *env, [[maybe_unused]] ani_object static ani_object CreateReadStream(ani_env *env, ani_string filePath) { - static const char *className = "L@ohos/file/fs/fileIo/ReadStream;"; + auto classDesc = FS::ReadStream::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::ReadStream::ctorDesc.c_str(); + auto ctorSig = FS::ReadStream::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { - HILOGE("New %s obj Failed", className); + HILOGE("New %s obj Failed", classDesc); return nullptr; } @@ -142,20 +149,24 @@ static ani_object CreateReadStream(ani_env *env, ani_string filePath) static ani_object CreateWriteStream(ani_env *env, ani_string filePath) { - static const char *className = "L@ohos/file/fs/fileIo/WriteStream;"; + auto classDesc = FS::WriteStream::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::WriteStream::ctorDesc.c_str(); + auto ctorSig = FS::WriteStream::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { - HILOGE("New %s obj Failed", className); + HILOGE("New %s obj Failed", classDesc); return nullptr; } @@ -163,9 +174,9 @@ static ani_object CreateWriteStream(ani_env *env, ani_string filePath) } static ani_object CreateStream( - ani_env *env, [[maybe_unused]] ani_object object, const std::string &streamName, const std::string &fineName) + ani_env *env, [[maybe_unused]] ani_object object, const std::string &streamName, const std::string &fileName) { - auto [succ, filePath] = TypeConverter::ToAniString(env, fineName); + auto [succ, filePath] = TypeConverter::ToAniString(env, fileName); if (!succ) { HILOGE("Failed to ani_string"); ErrorHandler::Throw(env, UNKNOWN_ERR); diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b..106f1d223 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ + "mod_fs/class_atomicfile/fs_atomicfile_test.cpp", "mod_fs/properties/close_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp new file mode 100644 index 000000000..081de4750 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp @@ -0,0 +1,366 @@ +/* + * 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 +#include "fs_atomicfile.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace std; +namespace fs = std::filesystem; + +string filePath = "/data/test/FsAtomicfileTest.txt"; +string deleteFile = "/data/test/FsAtomicfileDelTest.txt"; + +class FsAtomicfileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsAtomicfileTest::SetUpTestCase(void) +{ + ofstream tempfile(filePath); + tempfile << "hello world"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsAtomicfileTest::TearDownTestCase(void) +{ + filesystem::remove(filePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsAtomicfileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsAtomicfileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsAtomicfileTest_GetPath_001 + * @tc.desc: Test function of FsAtomicFile::GetPath interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + string path = stream->GetPath(); + EXPECT_EQ(path, filePath); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_001 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_002 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for path > PATH_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_002"; + + size_t largeLength = static_cast(PATH_MAX) + 1; + string largeString(largeLength, 'a'); + + auto ret = FsAtomicFile::Constructor(largeString); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_002"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_003 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed realpath. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_003"; + + string path = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_003"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_004 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed open. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_004"; + + string path = "/data/test/aaaaaaaaaa.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_004"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_001 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_002 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no parent dir. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_002"; + + string path = "/data/local/tmp/test/test/test/test.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_002"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_003 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_003"; + + string path = "/sys/kernel/address_bits"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_003"; +} + +/** + * @tc.name: FsAtomicfileTest_FinishWrite_001 + * @tc.desc: Test function of FsAtomicFile::FinishWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FinishWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FinishWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_FailWrite_001 + * @tc.desc: Test function of FsAtomicFile::FailWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FailWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FailWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_Delete_001 + * @tc.desc: Test function of FsAtomicFile::Delete interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; + + auto ret = FsAtomicFile::Constructor(deleteFile); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->Delete(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_Delete_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_001 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; + + auto ret = FsAtomicFile::Constructor(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_TRUE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_002 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for valied path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_002"; + + auto ret = FsAtomicFile::Constructor("aaaaaaaaaaaaaaaa"); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_FALSE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From 853ffd531251d7f7b5f2f8532c7defec266ec178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Fri, 30 May 2025 16:22:59 +0800 Subject: [PATCH 18/39] ani signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 Change-Id: I0257332826a5aa4a5e12cc346a061349bda23098 --- .../kits/js/src/common/ani_helper/ani_signature.cpp | 4 ++++ .../kits/js/src/common/ani_helper/ani_signature.h | 10 ++++++++++ .../kits/js/src/mod_hash/ani/bind_function_class.cpp | 10 ++++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 4a683a783..6649c2226 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -133,5 +133,9 @@ const string Impl::SecurityLabelImpl::classDesc = Impl::SecurityLabelImpl::class // Impl::StatvfsImpl const Type Impl::StatvfsImpl::classType = Builder::BuildClass("@ohos.file.statvfs.StatvfsImpl"); const string Impl::StatvfsImpl::classDesc = Impl::StatvfsImpl::classType.Descriptor(); +// HASH::HashStreamImpl +const Type HASH::HashStreamImpl::classType = Builder::BuildClass("@ohos.file.hash.HashStreamImpl"); +const string HASH::HashStreamImpl::classDesc = HASH::HashStreamImpl::classType.Descriptor(); +const string HASH::HashStreamImpl::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature \ No newline at end of file diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index 8b7e43281..8b05e2f70 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -212,6 +212,16 @@ struct StatvfsImpl : public BaseType { } // namespace Impl +namespace HASH { + +struct HashStreamImpl : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +} // namespace HASH + } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature #endif // INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 3237bad1b..9f8f7b08f 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -16,8 +16,8 @@ #include #include "ani_signature.h" #include "bind_function.h" -#include "hashstream_ani.h" #include "hash_ani.h" +#include "hashstream_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; @@ -33,13 +33,15 @@ static ani_status BindStaticMethods(ani_env *env) static ani_status BindHashStreamMethods(ani_env *env) { - static const char *className = "L@ohos/file/hash/HashStreamImpl;"; + auto classDesc = HASH::HashStreamImpl::classDesc.c_str(); + auto ctorDesc = HASH::HashStreamImpl::ctorDesc.c_str(); + auto ctorSig = HASH::HashStreamImpl::ctorSig.c_str(); std::array methods = { ani_native_function { "digest", nullptr, reinterpret_cast(HashStreamAni::Digest) }, ani_native_function { "update", nullptr, reinterpret_cast(HashStreamAni::Update) }, - ani_native_function { "", "Lstd/core/String;:V", reinterpret_cast(HashStreamAni::Constructor) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(HashStreamAni::Constructor) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) -- Gitee From bbe29ec1bf79f6781ad749f07e41dea9a24d5a2f Mon Sep 17 00:00:00 2001 From: tianp Date: Fri, 30 May 2025 17:58:40 +0800 Subject: [PATCH 19/39] =?UTF-8?q?lstat\copy=5FdirTDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/js/BUILD.gn | 51 ++- .../mod_fs/properties/copy_dir_core_test.cpp | 312 ++++++++++++++++++ .../properties/lstat_core_mock_test.cpp | 79 +++++ .../js/mod_fs/properties/lstat_core_test.cpp | 89 +++++ .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 47 ++- .../js/mod_fs/properties/mock/uv_fs_mock.h | 39 ++- 6 files changed, 595 insertions(+), 22 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b..263ed1178 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -20,8 +20,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -40,6 +38,54 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/listfile_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/properties/lstat_core_test.cpp", + "mod_fs/properties/copy_dir_core_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/lstat_core_mock_test.cpp", ] deps = [ @@ -55,6 +101,7 @@ ohos_unittest("ani_file_fs_test") { "ability_runtime:ability_manager", "app_file_service:fileuri_native", "c_utils:utils", + "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp new file mode 100644 index 000000000..f04a346c1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -0,0 +1,312 @@ +/* + * 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 + +#include "copy_dir_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyDirCoreTest : public testing::Test { +public: + static filesystem::path srcPath; + static filesystem::path destPath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + void CreateTestFile(const filesystem::path& path, const string& content = "test") { + ofstream file(path); + file << content; + } + + void CreateTestDirStructure() { + filesystem::create_directories(srcPath / "subdir1"); + filesystem::create_directories(srcPath / "subdir2"); + CreateTestFile(srcPath / "file1.txt"); + CreateTestFile(srcPath / "subdir1" / "file2.txt"); + CreateTestFile(srcPath / "subdir2" / "file3.txt"); + } +}; + +filesystem::path CopyDirCoreTest::srcPath; +filesystem::path CopyDirCoreTest::destPath; + +void CopyDirCoreTest::SetUpTestCase(void) +{ + srcPath = filesystem::temp_directory_path() / "src/"; + destPath = filesystem::temp_directory_path() / "dest/"; + std::filesystem::create_directory(srcPath); + std::filesystem::create_directory(destPath); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyDirCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(srcPath); + filesystem::remove_all(destPath); +} + +void CopyDirCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyDirCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_001 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with empty directory. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_001"; + + string src = srcPath.string() + "/test01"; + string dest = destPath.string(); + filesystem::create_directories(src); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_001"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_002 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid mode. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_002"; + + string src = srcPath.string() + "/test02"; + string dest = destPath.string(); + filesystem::create_directories(src); + + int invalidMode = COPYMODE_MAX + 1; + auto result = CopyDirCore::DoCopyDir(src, dest, optional(invalidMode)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_002"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_003 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with non-existent source. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_003"; + + string src = srcPath.string() + "/non_existent"; + string dest = destPath.string(); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_003"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_004 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid destination. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_004"; + + string src = srcPath.string(); + string dest = destPath.string() + "/invalid_file.txt"; + filesystem::path(dest).remove_filename(); + filesystem::create_directories(filesystem::path(dest).parent_path()); + std::ofstream(dest).close(); // 创建文件而非目录 + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_004"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_005 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with same source and destination. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_005"; + + string src = srcPath.string(); + string dest = srcPath.string(); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_005"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_006 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with files. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_006"; + + string src = srcPath.string() + "/test06"; + string dest = destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + CreateTestFile(src + "/file2.txt", "content2"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_006"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_007 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with subdirectories. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_007"; + + string src = srcPath.string() + "/test07"; + string dest = destPath.string(); + filesystem::create_directories(src + "/subdir1"); + filesystem::create_directories(src + "/subdir2"); + CreateTestFile(src + "/subdir1/file1.txt", "sub1_content1"); + CreateTestFile(src + "/subdir2/file2.txt", "sub2_content2"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_007"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_008 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with existing files (throw error mode). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_008"; + + string src = srcPath.string() + "/test08"; + string dest = destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + + string destDir = dest + "/" + filesystem::path(src).filename().string(); + filesystem::create_directories(destDir); + CreateTestFile(destDir + "/file1.txt", "existing_content"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional(DIRMODE_FILE_COPY_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_008"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_009 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with existing files (overwrite mode). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_009"; + + string src = srcPath.string() + "/test09"; + string dest = destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + + string destDir = dest + "/" + filesystem::path(src).filename().string(); + filesystem::create_directories(destDir); + CreateTestFile(destDir + "/file1.txt", "existing_content"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional(DIRMODE_FILE_COPY_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_009"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp new file mode 100644 index 000000000..f03ea7bdf --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp @@ -0,0 +1,79 @@ +/* + * 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 "lstat_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LstatCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void LstatCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void LstatCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void LstatCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LstatCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LstatCoreMockTest_DoLstat_001 + * @tc.desc: Test function of LstatCore::DoLstat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreMockTest, LstatCoreMockTest_DoLstat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreMockTest-begin LstatCoreMockTest_DoLstat_001"; + + EXPECT_CALL(*uvMock, uv_fs_lstat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = LstatCore::DoLstat("/data/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LstatCoreMockTest-end LstatCoreMockTest_DoLstat_001"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp new file mode 100644 index 000000000..8996b3b48 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp @@ -0,0 +1,89 @@ +/* + * 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 "lstat_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LstatCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LstatCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open("/data/test/lstat.txt", CREATE | O_RDWR, 0644); + close(fd); +} + +void LstatCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + rmdir("/data/test/lstat.txt"); +} + +void LstatCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LstatCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LstatCoreTest_DoLstat_001 + * @tc.desc: Test function of LstatCore::DoLstat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreTest, LstatCoreTest_DoLstat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreTest-begin LstatCoreTest_DoLstat_001"; + + auto res = LstatCore::DoLstat("/invalid/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LstatCoreTest-end LstatCoreTest_DoLstat_001"; +} + +/** + * @tc.name: LstatCoreTest_DoLstat_002 + * @tc.desc: Test function of LstatCore::DoLstat interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreTest, LstatCoreTest_DoLstat_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreTest-begin LstatCoreTest_DoLstat_002"; + + auto res = LstatCore::DoLstat("/data/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "LstatCoreTest-end LstatCoreTest_DoLstat_002"; +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 13ceb7b54..60c4f3b0e 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -17,8 +17,8 @@ using namespace OHOS::FileManagement::ModuleFileIO; -int uv_fs_read( - uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) +int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); } @@ -53,9 +53,9 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); } int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) @@ -68,12 +68,22 @@ int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, u return Uvfs::ins->uv_fs_ftruncate(loop, req, fd, offset, cb); } -int uv_fs_write( - uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) +int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); } +int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_realpath(loop, req, path, cb); +} + +int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_close(loop, req, file, cb); +} + int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) { return Uvfs::ins->uv_fs_fdatasync(loop, req, file, cb); @@ -99,4 +109,27 @@ int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_unlink(loop, req, path, cb); } -void uv_fs_req_cleanup(uv_fs_t *req) {} +void uv_fs_req_cleanup(uv_fs_t *req) +{ + return; +} + +int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_rename(loop, req, path, newPath, cb); +} + +int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_fsync(loop, req, file, cb); +} + +int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); +} + +int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_lstat(loop, req, path, cb); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 6b0f3c5fd..b6a1df928 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -16,7 +16,7 @@ #ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H -#include "read_core.h" +#include "uv.h" #include @@ -25,53 +25,66 @@ namespace OHOS::FileManagement::ModuleFileIO { class Uvfs { public: static inline std::shared_ptr ins = nullptr; - public: virtual ~Uvfs() = default; virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) = 0; virtual int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_utime( - uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) = 0; + virtual int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, + double mtime, uv_fs_cb cb) = 0; virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; - virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink( - uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) = 0; + virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; + virtual int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, + int64_t off, size_t len, uv_fs_cb cb) = 0; + virtual int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; }; class UvfsMock : public Uvfs { public: MOCK_METHOD7(uv_fs_read, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, - int64_t off, uv_fs_cb cb)); + int64_t off, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_stat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6( - uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, + uv_fs_cb cb)); MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, - int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_write, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb)); + int64_t offset, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_realpath, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_close, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_fdatasync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_mkdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_access, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, + int64_t off, size_t len, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_lstat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); }; } // namespace OHOS::FileManagement::ModuleFileIO -- Gitee From ca1d9682a4c0c31ba3e89bb856edf95ce294aa00 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 30 May 2025 12:44:51 +0200 Subject: [PATCH 20/39] Fix invlid code after primitive type refactor Change-Id: I0615ceabceb8503b09b036dbbb595fe63eda1bc7 Signed-off-by: Martin Sajti --- .../ani/ets/@ohos.file.environment.ets | 18 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 378 +++++++++--------- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 8 +- .../ani/ets/@ohos.file.securityLabel.ets | 16 +- .../ani/ets/@ohos.file.statvfs.ets | 26 +- 5 files changed, 223 insertions(+), 223 deletions(-) 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 9ccc27495..71b8e1c2d 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 @@ -22,8 +22,8 @@ namespace Environment { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -35,8 +35,8 @@ namespace Environment { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -46,8 +46,8 @@ namespace Environment { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -58,8 +58,8 @@ namespace Environment { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -100,4 +100,4 @@ class EnvironmentImpl { static native getUserDocumentDirSync(): string; static native getExternalStorageDirSync(): string; static native getUserHomeDirSync(): string; -} \ No newline at end of file +} diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index df9bfb23f..03f56f6b0 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -47,8 +47,8 @@ function access(path: string, mode?: AccessModeType): Promise { promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -62,8 +62,8 @@ function access(path: string, callback: AsyncCallback): void { e.code = 0; let result = ret as boolean; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, false); + }).catch((e: Error): void => { + callback(e as BusinessError, false); }); } @@ -75,8 +75,8 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -94,8 +94,8 @@ function close(file: number | File): Promise { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -106,8 +106,8 @@ function close(file: number | File, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -121,8 +121,8 @@ function connectDfs(networkId: string, listeners: DfsListeners): Promise { FileIoImpl.connectDfs(networkId, listeners), networkId, listeners); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -132,8 +132,8 @@ function disconnectDfs(networkId: string): Promise { let promise = taskpool.execute((networkId: string): void => FileIoImpl.disConnectDfs(networkId), networkId); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -150,8 +150,8 @@ function getxattr(path: string, key: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -171,8 +171,8 @@ function copyDir(src: string, dest: string, mode?: number): Promise { FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }); } @@ -185,8 +185,8 @@ function copyDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -198,8 +198,8 @@ function copyDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -237,7 +237,7 @@ function fdatasync(fd: number): Promise { let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -249,8 +249,8 @@ function fdatasync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -273,7 +273,7 @@ function mkdir(path: string): Promise { let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -285,8 +285,8 @@ function mkdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -309,8 +309,8 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -326,8 +326,8 @@ function moveDir(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }) } @@ -341,8 +341,8 @@ function moveDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -355,8 +355,8 @@ function moveDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -393,8 +393,8 @@ function mkdtemp(prefix: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -408,8 +408,8 @@ function mkdtemp(prefix: string, callback: AsyncCallback): void { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -424,8 +424,8 @@ function moveFile(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -438,8 +438,8 @@ function moveFile(src: string, dest: string, mode: number, callback: AsyncCallba let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -451,8 +451,8 @@ function moveFile(src: string, dest: string, callback: AsyncCallback): voi let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -468,8 +468,8 @@ function open(path: String, mode?: number): Promise { promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -483,9 +483,9 @@ function open(path: String, mode: number, callback: AsyncCallback): e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -498,9 +498,9 @@ function open(path: String, callback: AsyncCallback): void { e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -516,8 +516,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions) promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -532,8 +532,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -546,8 +546,8 @@ function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -563,8 +563,8 @@ function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -578,8 +578,8 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -592,8 +592,8 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -610,8 +610,8 @@ function readLines(filePath: string, options?: Options): Promise promise.then((ret: NullishType): void => { let it = ret as ReaderIterator; resolve(it); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -625,9 +625,9 @@ function readLines(filePath: string, callback: AsyncCallback): v e.code = 0; let it = ret as ReaderIterator; callback(e, it); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -640,9 +640,9 @@ function readLines(filePath: string, options: Options, callback: AsyncCallback { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -656,8 +656,8 @@ function rmdir(path: string): Promise { let promise = taskpool.execute((path: string): void => FileIoImpl.rmdirSync(path), path); promise.then((ret: NullishType) => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -668,8 +668,8 @@ function rmdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -684,8 +684,8 @@ function truncate(file: string | number, len?: number): Promise { }, file, len); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -698,8 +698,8 @@ function truncate(file: string | number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -711,8 +711,8 @@ function truncate(file: string | number, len: number, callback: AsyncCallback(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -726,8 +726,8 @@ function unlink(path: string): Promise { let promise = taskpool.execute((path: string): undefined => unlinkSync(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -738,8 +738,8 @@ function unlink(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -751,9 +751,9 @@ function readText(filePath: string, options?: ReadTextOptions): Promise promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); }); } @@ -766,8 +766,8 @@ function readText(filePath: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -780,8 +780,8 @@ function readText(filePath: string, options: ReadTextOptions, callback: AsyncCal e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -797,8 +797,8 @@ function listFile(path: string, options?: ListFileOptions): Promise { promise.then((ret: NullishType): void => { let r = ret as string[]; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -812,8 +812,8 @@ function listFile(path: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -826,8 +826,8 @@ function listFile(path: string, options: ListFileOptions, callback: AsyncCallbac e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -851,8 +851,8 @@ function stat(file: string | number): Promise { promise.then((ret: NullishType): void => { let r = ret as Stat; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -866,8 +866,8 @@ function stat(file: string | number, callback: AsyncCallback): void e.code = 0; let r = ret as Stat; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } @@ -882,8 +882,8 @@ function fsync(fd: number): Promise { }, fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -896,8 +896,8 @@ function fsync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -913,8 +913,8 @@ function symlink(target: string, srcPath: string): Promise { }, target, srcPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -929,8 +929,8 @@ function rename(oldPath: string, newPath: string): Promise { }, oldPath, newPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -943,8 +943,8 @@ function rename(oldPath: string, newPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -964,8 +964,8 @@ function createRandomAccessFile(file: string | File, mode?: number, promise.then((ret: NullishType): void => { let raffile = ret as RandomAccessFileInner; resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -978,8 +978,8 @@ function fdopenStream(fd: number, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -994,7 +994,7 @@ function setxattr(path: string, key: string, value: string): Promise { FileIoImpl.setxattrSync(path, key, value), path, key, value); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -1009,9 +1009,9 @@ function createRandomAccessFile(file: string | File, callback: AsyncCallback { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1025,9 +1025,9 @@ function createRandomAccessFile(file: string | File, mode: number, e.code = 0; let raffile = ret as RandomAccessFile; callback(e, raffile); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1040,9 +1040,9 @@ function fdopenStream(fd: number, mode: string, callback: AsyncCallback) e.code = 0; let stream = ret as Stream; callback(e, stream); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1058,8 +1058,8 @@ function createStream(path: string, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1073,9 +1073,9 @@ function createStream(path: string, mode: string, callback: AsyncCallback { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1103,8 +1103,8 @@ function symlink(target: string, srcPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1130,8 +1130,8 @@ function lstat(path: string): Promise { let r = ret as Stat; resolve(r); } - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1151,8 +1151,8 @@ function lstat(path: string, callback: AsyncCallback): void { let r = ret as Stat; callback(e, r); } - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } function copyFile(src: string | number, dest: string | number, mode?: number): Promise { @@ -1161,8 +1161,8 @@ function copyFile(src: string | number, dest: string | number, mode?: number): P FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1173,8 +1173,8 @@ function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1186,8 +1186,8 @@ function copyFile(src: string | number, dest: string | number, mode: number, cal let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1198,8 +1198,8 @@ function copy(srcUri: string, destUri: string, options: CopyOptions, callback: A let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1210,8 +1210,8 @@ function copyFile(src: string | number, dest: string | number, callback: AsyncCa let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1222,8 +1222,8 @@ function copy(srcUri: string, destUri: string, callback: AsyncCallback): v let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1331,7 +1331,7 @@ export class RandomAccessFileInner implements RandomAccessFile { } native setFilePointer0(filePointer: number): void; - + native close(): void; writeSync(buffer: ArrayBuffer | string, options?: WriteOptions): number { @@ -1350,8 +1350,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1365,8 +1365,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1379,8 +1379,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1400,8 +1400,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1415,8 +1415,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1429,8 +1429,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1474,8 +1474,8 @@ export class FileInner implements File { }, exclusive); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1488,8 +1488,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1501,8 +1501,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1590,7 +1590,7 @@ export class StatInner implements Stat { this.nativeStat = stat; } } - + native isBlockDevice(): boolean; native isCharacterDevice(): boolean; native isDirectory(): boolean; @@ -1632,8 +1632,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.closeSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1644,8 +1644,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1656,8 +1656,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.flushSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1668,8 +1668,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1683,8 +1683,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1698,8 +1698,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1712,8 +1712,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1727,8 +1727,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1742,8 +1742,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1756,11 +1756,11 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + native readSync(buffer: ArrayBuffer, options?: ReadOptions): number; native seek(offset: number, whence?: number): number; } @@ -2119,15 +2119,15 @@ export class FileIoImpl { static native createStreamSync(path: string, mode: string): fileIo.Stream; static native createWatcherSync(path: string, events: number, listener: WatchEventListener): fileIo.Watcher; - + static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; - + static native dup(fd: number): fileIo.File; static native listFileSync(path: string, options?: ListFileOptions): string[]; static native lstatSync(path: string): fileIo.Stat; - + static native lseekSync(fd: number, offset: number, whence?: fileIo.WhenceType): number; static native mkdirSync(path: string): void; @@ -2143,7 +2143,7 @@ export class FileIoImpl { static native openSync(path: String, mode?: number): fileIo.File; static native readlinesSync(filePath: string, options?: Options): fileIo.ReaderIterator; - + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; static native readTextSync(filePath: string, options?: ReadTextOptions): string; diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index dfa16ab00..eea6d2cd0 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -23,8 +23,8 @@ export default namespace hash { promise.then((ret: NullishType): void => { let res = ret as string; resolve(res); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -36,8 +36,8 @@ export default namespace hash { e.code = 0; let res = ret as string; callback(e, res); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 06e659dc2..4f81cef3d 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -23,8 +23,8 @@ namespace securityLabel { let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -35,8 +35,8 @@ namespace securityLabel { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -50,8 +50,8 @@ namespace securityLabel { promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -63,8 +63,8 @@ namespace securityLabel { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } diff --git a/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets index 831bc046d..f3c569c80 100644 --- a/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets +++ b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets @@ -26,12 +26,12 @@ namespace statfs { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } - + export function getFreeSize(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(StatvfsImpl.getFreeSizeSync, path); promise.then((ret: NullishType): void => { @@ -39,27 +39,27 @@ namespace statfs { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + export function getTotalSizeSync(path: string): number { return StatvfsImpl.getTotalSizeSync(path); } - + export function getTotalSize(path: string): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void): void => { let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } - + export function getTotalSize(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); promise.then((ret: NullishType): void => { @@ -67,8 +67,8 @@ namespace statfs { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } } @@ -83,4 +83,4 @@ class StatvfsImpl { static native getFreeSizeSync(path: string): number; static native getTotalSizeSync(path: string): number; -} \ No newline at end of file +} -- Gitee From 38136a3b8bf8c4b44d3a36d68bb4f17ff7033aac Mon Sep 17 00:00:00 2001 From: tianp Date: Wed, 4 Jun 2025 14:21:12 +0800 Subject: [PATCH 21/39] =?UTF-8?q?strncpy=E5=87=BD=E6=95=B0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- interfaces/test/unittest/js/BUILD.gn | 6 ------ .../unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp | 3 ++- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index c8ba826f7..e57ca09a7 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -20,8 +20,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -73,8 +71,6 @@ ohos_unittest("ani_file_fs_mock_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -120,8 +116,6 @@ ohos_unittest("ani_file_fs_mock_test") { ohos_unittest("ani_file_statvfs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - sources = [ "mod_statvfs/statvfs_core_test.cpp", ] diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp index b6e874658..bde39d014 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -75,7 +75,8 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize. statEntity->fileInfo_ = std::make_unique(); statEntity->fileInfo_->isPath = true; statEntity->fileInfo_->path = std::make_unique(100); - strcpy(statEntity->fileInfo_->path.get(), "/test/path"); + strncpy(stat->fileInfo_->path.get(), "/test/path", 99); + statEntity->fileInfo_->path.get()[99] = '\0'; fsStat = std::make_unique(std::move(statEntity)); EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)); -- Gitee From b0797e8ae94880650489c7c38852459dc4ec9643 Mon Sep 17 00:00:00 2001 From: tianp Date: Wed, 4 Jun 2025 14:23:58 +0800 Subject: [PATCH 22/39] =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../unittest/js/mod_fs/properties/copy_dir_core_test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp index f04a346c1..c71b521b5 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -34,12 +34,14 @@ public: void SetUp(); void TearDown(); - void CreateTestFile(const filesystem::path& path, const string& content = "test") { + void CreateTestFile(const filesystem::path& path, const string& content = "test") + { ofstream file(path); file << content; } - void CreateTestDirStructure() { + void CreateTestDirStructure() + { filesystem::create_directories(srcPath / "subdir1"); filesystem::create_directories(srcPath / "subdir2"); CreateTestFile(srcPath / "file1.txt"); -- Gitee From de75eebf8d5019c5a6b26547e7a8017f4b8f05cc Mon Sep 17 00:00:00 2001 From: tianp Date: Wed, 4 Jun 2025 18:08:52 +0800 Subject: [PATCH 23/39] =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../mod_fs/class_file/fs_file_mock_test.cpp | 192 ++++++------------ .../js/mod_fs/class_file/fs_file_test.cpp | 59 +++++- .../mod_fs/properties/open_core_mock_test.cpp | 8 +- .../js/mod_fs/properties/open_core_test.cpp | 14 +- 4 files changed, 134 insertions(+), 139 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index d9bfebf4b..90cc1f29f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -13,14 +13,14 @@ * limitations under the License. */ -#include -#include - #include "file_entity.h" #include "fs_file.h" #include "system_mock.h" #include "uv_fs_mock.h" +#include +#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -34,7 +34,6 @@ public: void TearDown(); static inline shared_ptr uvMock = nullptr; static inline shared_ptr sys = nullptr; -private: std::unique_ptr fileEntity; std::unique_ptr fsFile; }; @@ -65,7 +64,7 @@ void FsFileMockTest::SetUp(void) const int fdValue = 3; const bool isClosed = false; fileEntity->fd_ = std::make_unique(fdValue, isClosed); - fileEntity->path_ = "/data/testdir/testfile.txt"; + fileEntity->path_ = "/data/test/file_test.txt"; fileEntity->uri_ = ""; fsFile = std::make_unique(std::move(fileEntity)); } @@ -86,27 +85,8 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Leve { GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_001"; - fsFile->fileEntity->uri_ = "file:///storage/test.txt"; - EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); - auto result = fsFile->GetPath(); - EXPECT_EQ(result.IsSuccess(), true); - - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_001"; -} - -/** - * @tc.name: FsFileMockTest_GetPath_002 - * @tc.desc: Test function of GetPath() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002"; - uv_fs_t mock_req; - mock_req.ptr = const_cast("/data/testdir/testfile.txt"); + mock_req.ptr = const_cast("/data/test/file_test.txt"); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { @@ -116,136 +96,98 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Leve auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_002"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_001"; } /** - * @tc.name: FsFileMockTest_GetPath_003 + * @tc.name: FsFileMockTest_GetPath_002 * @tc.desc: Test function of GetPath() interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_003, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_003"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002"; fsFile->fileEntity->path_ = "/invalid/path"; EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_003"; -} - -/** - * @tc.name: FsFileMockTest_GetName_004 - * @tc.desc: Test function of GetName() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; - - fsFile->fileEntity->uri_ = "file:///storage/test.txt"; - EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); - auto result = fsFile->GetName(); - EXPECT_EQ(result.IsSuccess(), true); - - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_002"; } /** - * @tc.name: FsFileMockTest_GetName_005 + * @tc.name: FsFileMockTest_GetName_003 * @tc.desc: Test function of GetName() interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_005, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_005"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_003"; fsFile->fileEntity->path_ = "/invalid/path"; EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_005"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_003"; } /** - * @tc.name: FsFileMockTest_GetName_006 + * @tc.name: FsFileMockTest_GetName_004 * @tc.desc: Test function of GetName() interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; - fsFile->fileEntity->path_ = "file.txt"; + fsFile->fileEntity->path_ = "file_test.txt"; EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_006"; -} - -/** - * @tc.name: FsFileMockTest_GetParent_007 - * @tc.desc: Test function of GetParent() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_007"; - - fsFile->fileEntity->uri_ = "file:///storage/test.txt"; - EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(0)); - auto result = fsFile->GetParent(); - EXPECT_EQ(result.IsSuccess(), true); - - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004"; } /** - * @tc.name: FsFileMockTest_GetParent_008 + * @tc.name: FsFileMockTest_GetParent_005 * @tc.desc: Test function of GetParent() interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_008, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_008"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_005"; fsFile->fileEntity->path_ = "/invalid/path"; EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_008"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_005"; } /** - * @tc.name: FsFileMockTest_GetName_009 + * @tc.name: FsFileMockTest_GetName_006 * @tc.desc: Test function of GetName() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_009, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_009"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006"; uv_fs_t mock_req; - mock_req.ptr = const_cast("/testfile.txt"); + mock_req.ptr = const_cast("/file_test.txt"); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { @@ -255,22 +197,22 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_009, testing::ext::TestSize.Leve auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_009"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_006"; } /** - * @tc.name: FsFileMockTest_GetParent_010 + * @tc.name: FsFileMockTest_GetParent_007 * @tc.desc: Test function of GetParent() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_010, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_010"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_007"; uv_fs_t mock_req; - mock_req.ptr = const_cast("/data/testdir"); + mock_req.ptr = const_cast("/test/dir_test"); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { @@ -280,22 +222,22 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_010, testing::ext::TestSize.Le auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_010"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007"; } /** - * @tc.name: FsFileMockTest_GetName_011 + * @tc.name: FsFileMockTest_GetName_008 * @tc.desc: Test function of GetName() interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_011, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_011"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_008"; uv_fs_t mock_req; - mock_req.ptr = const_cast("testfile.txt"); + mock_req.ptr = const_cast("file_test.txt"); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { @@ -305,22 +247,22 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_011, testing::ext::TestSize.Leve auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_011"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_008"; } /** - * @tc.name: FsFileMockTest_GetParent_012 + * @tc.name: FsFileMockTest_GetParent_009 * @tc.desc: Test function of GetParent() interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_012, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_012"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_009"; uv_fs_t mock_req; - mock_req.ptr = const_cast("testdir"); + mock_req.ptr = const_cast("dir_test"); EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { @@ -330,115 +272,115 @@ HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_012, testing::ext::TestSize.Le auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_012"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_009"; } /** - * @tc.name: FsFileMockTest_Lock_013 + * @tc.name: FsFileMockTest_Lock_010 * @tc.desc: Test function of Lock() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_013, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_010, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_013"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_010"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); auto result = fsFile->Lock(true); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_013"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_010"; } /** - * @tc.name: FsFileMockTest_Lock_014 + * @tc.name: FsFileMockTest_Lock_011 * @tc.desc: Test function of Lock() interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_014, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_011, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_014"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_011"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); auto result = fsFile->Lock(false); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_014"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_011"; } /** - * @tc.name: FsFileMockTest_TryLock_015 + * @tc.name: FsFileMockTest_TryLock_012 * @tc.desc: Test function of TryLock() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_015, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_012, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_015"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_012"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); auto result = fsFile->TryLock(true); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_015"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_012"; } /** - * @tc.name: FsFileMockTest_TryLock_016 + * @tc.name: FsFileMockTest_TryLock_013 * @tc.desc: Test function of TryLock() interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_016, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_013, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_016"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_013"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); auto result = fsFile->TryLock(false); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_016"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_013"; } /** - * @tc.name: FsFileMockTest_UnLock_017 + * @tc.name: FsFileMockTest_UnLock_014 * @tc.desc: Test function of UnLock() interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_017, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_014, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_017"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_014"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); auto result = fsFile->UnLock(); EXPECT_EQ(result.IsSuccess(), true); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_017"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_014"; } /** - * @tc.name: FsFileMockTest_UnLock_018 + * @tc.name: FsFileMockTest_UnLock_015 * @tc.desc: Test function of UnLock() interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_018, testing::ext::TestSize.Level1) +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_015, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_018"; + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_015"; EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); auto result = fsFile->UnLock(); EXPECT_EQ(result.IsSuccess(), false); - GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_018"; + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_015"; } } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index 4728b8e7b..9a72cf86f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "fs_file.h" #include "file_entity.h" +#include "fs_file.h" #include @@ -29,7 +29,6 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); -private: std::unique_ptr fileEntity; std::unique_ptr fsFile; }; @@ -52,7 +51,7 @@ void FsFileTest::SetUp(void) const int fdValue = 3; const bool isClosed = false; fileEntity->fd_ = std::make_unique(fdValue, isClosed); - fileEntity->path_ = "/data/testdir/testfile.txt"; + fileEntity->path_ = "/data/test/file_test.txt"; fileEntity->uri_ = ""; fsFile = std::make_unique(std::move(fileEntity)); } @@ -222,4 +221,58 @@ HWTEST_F(FsFileTest, FsFileTest_UnLock_009, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_UnLock_009"; } +/** + * @tc.name: FsFileTest_GetName_010 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetName_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_010"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetName_010"; +} + +/** + * @tc.name: FsFileTest_GetParent_011 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_011"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetParent_011"; +} + +/** + * @tc.name: FsFileTest_GetPath_012 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetPath_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_012"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetPath_012"; +} + } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp index 3a277ed75..d3b3752b2 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -70,7 +70,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_001, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_001"; - string path = "/test/path.txt"; + string path = "/test/open_test.txt"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); @@ -91,7 +91,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_002, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_002"; - string path = "file://test/path.txt"; + string path = "file://test/open_test.txt"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); @@ -112,7 +112,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_003, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_003"; - string path = "file://test/path.txt"; + string path = "file://test/open_test.txt"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); @@ -133,7 +133,7 @@ HWTEST_F(OpenCoreMockTest, OpenCoreMockTest_DoOpen_004, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "OpenCoreMockTest-begin OpenCoreMockTest_DoOpen_004"; - string path = "/test/path.txt"; + string path = "/test/open_test.txt"; int32_t mode = 0; EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp index b505270fc..9b40e7ef6 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_001"; - string path = "/test/path.txt"; + string path = "/test/open_test.txt"; int32_t mode = -1; auto res = OpenCore::DoOpen(path, mode); @@ -81,7 +81,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_002"; - string path = "/test/path.txt"; + string path = "/test/open_test.txt"; int32_t mode = 3; auto res = OpenCore::DoOpen(path, mode); @@ -101,7 +101,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_003"; - string path = "file://media/image.jpg"; + string path = "file://media/open_test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -121,7 +121,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_004"; - string path = "file://docs/non_existent.pdf"; + string path = "file://docs/open_test.pdf"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -141,7 +141,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_005"; - string path = "content://com.example.provider/file.txt"; + string path = "content://com.example.provider/open_test.txt"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -161,7 +161,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_006"; - string path = "datashare://media/image.jpg"; + string path = "datashare://media/open_test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -181,7 +181,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_007"; - string path = "invalid://path/to/file"; + string path = "invalid://path/dir_test"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); -- Gitee From 660a512c270581b81e7ba754608238c2a0c81e6e Mon Sep 17 00:00:00 2001 From: tianp Date: Thu, 5 Jun 2025 19:43:51 +0800 Subject: [PATCH 24/39] =?UTF-8?q?classDesc=E5=B1=9E=E6=80=A7=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../kits/js/src/common/ani_helper/type_converter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 564d7bc56..6e495904f 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -146,10 +146,10 @@ static std::tuple ParseFd(ani_env *env, const ani_object &pathOrF { ani_boolean isFd = false; - auto intClassDesc = BoxedTypes::Int::classDesc.c_str(); - ani_class intClass; - env->FindClass(intClassDesc, &intClass); - env->Object_InstanceOf(pathOrFd, intClass, &isFd); + auto classDesc = BoxedTypes::Double::classDesc.c_str(); + ani_class cls; + env->FindClass(classDesc, &cls); + env->Object_InstanceOf(pathOrFd, cls, &isFd); if (isFd) { ani_int fd; if (ANI_OK != env->Object_CallMethodByName_Int(pathOrFd, "toInt", nullptr, &fd)) { -- Gitee From 6378f602fd90ca9577b95622ad2068ca9b62f4f1 Mon Sep 17 00:00:00 2001 From: tianp Date: Tue, 17 Jun 2025 16:24:11 +0800 Subject: [PATCH 25/39] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp --- .../js/mod_fs/class_stat/fs_stat_mock_test.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp index bde39d014..98093ee64 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -13,12 +13,13 @@ * limitations under the License. */ -#include "fs_stat.h" -#include "../properties/mock/system_mock.h" -#include "fs_stat_entity.h" - -#include #include +#include + +#include "fs_stat_entity.h" +#include "fs_stat.h" +#include "securec.h" +#include "system_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -74,8 +75,10 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize. statEntity = std::make_unique(); statEntity->fileInfo_ = std::make_unique(); statEntity->fileInfo_->isPath = true; - statEntity->fileInfo_->path = std::make_unique(100); - strncpy(stat->fileInfo_->path.get(), "/test/path", 99); + int length = 100; + string testPath = "/test/path"; + statEntity->fileInfo_->path = std::make_unique(length); + strncpy_s(statEntity->fileInfo_->path.get(), length, testPath.c_str(), testPath.size()); statEntity->fileInfo_->path.get()[99] = '\0'; fsStat = std::make_unique(std::move(statEntity)); -- Gitee From 6e496e713de65da555262fd4f03c8d27c4a21220 Mon Sep 17 00:00:00 2001 From: tianp Date: Tue, 17 Jun 2025 19:16:49 +0800 Subject: [PATCH 26/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp --- .../js/mod_fs/properties/copy_dir_core_test.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp index c71b521b5..0c2431388 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -15,7 +15,6 @@ #include #include - #include #include "copy_dir_core.h" @@ -39,15 +38,6 @@ public: ofstream file(path); file << content; } - - void CreateTestDirStructure() - { - filesystem::create_directories(srcPath / "subdir1"); - filesystem::create_directories(srcPath / "subdir2"); - CreateTestFile(srcPath / "file1.txt"); - CreateTestFile(srcPath / "subdir1" / "file2.txt"); - CreateTestFile(srcPath / "subdir2" / "file3.txt"); - } }; filesystem::path CopyDirCoreTest::srcPath; -- Gitee From 4c459d492e9625e60c9008b0709296cad7a7a397 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Thu, 19 Jun 2025 10:39:47 +0800 Subject: [PATCH 27/39] add tdd Signed-off-by: yangbiao59 --- interfaces/test/unittest/js/BUILD.gn | 13 ++ .../properties/access_core_mock_test.cpp | 172 ++++++++++++++++++ .../js/mod_fs/properties/access_core_test.cpp | 149 ++++++++++++--- .../mod_fs/properties/dup_core_mock_test.cpp | 79 ++++++++ .../js/mod_fs/properties/dup_core_test.cpp | 13 +- .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 11 +- .../js/mod_fs/properties/mock/uv_fs_mock.h | 27 +-- .../mod_fs/properties/read_core_mock_test.cpp | 107 +++++++++++ .../js/mod_fs/properties/read_core_test.cpp | 37 ++-- .../js/mod_fs/properties/rmdir_core_test.cpp | 99 +++++++++- ...re_test.cpp => symlink_core_mock_test.cpp} | 42 ++--- .../properties/truncate_core_mock_test.cpp | 169 +++++++++++++++++ .../mod_fs/properties/truncate_core_test.cpp | 125 +------------ .../properties/utimes_core_mock_test.cpp | 124 +++++++++++++ .../js/mod_fs/properties/utimes_core_test.cpp | 76 +------- .../js/mod_fs/properties/write_core_test.cpp | 78 ++++---- 16 files changed, 992 insertions(+), 329 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp rename interfaces/test/unittest/js/mod_fs/properties/{symlink_core_test.cpp => symlink_core_mock_test.cpp} (57%) create mode 100644 interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index d3fc839a0..cf3a548da 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -34,12 +34,19 @@ ohos_unittest("ani_file_fs_test") { sources = [ "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/properties/access_core_test.cpp", "mod_fs/properties/close_core_test.cpp", "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", + "mod_fs/properties/dup_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", + "mod_fs/properties/read_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", + "mod_fs/properties/rmdir_core_test.cpp", + "mod_fs/properties/truncate_core_test.cpp", + "mod_fs/properties/utimes_core_test.cpp", + "mod_fs/properties/write_core_test.cpp", ] deps = [ @@ -85,7 +92,13 @@ ohos_unittest("ani_file_fs_mock_test") { sources = [ "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/truncate_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", ] diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp new file mode 100644 index 000000000..a44f8bb38 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -0,0 +1,172 @@ +/* + * 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 "access_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class AccessCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr uvMock = nullptr; + const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; +}; + +void AccessCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void AccessCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void AccessCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void AccessCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_001 + * @tc.desc: Test function of AccessCore::ValidAccessArgs interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_001"; + + std::string path = "TEST"; + std::optional mode; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = AccessCore::DoAccess(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_001"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_002 + * @tc.desc: Test function of AccessCore::ValidAccessArgs interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_002"; + + std::string path = "TEST"; + std::optional mode; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + auto res = AccessCore::DoAccess(path, mode); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_002"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_003 + * @tc.desc: Test function of AccessCore::DoAccess interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_003"; + + std::string path = "TEST"; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = DEFAULT_FLAG; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_003"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_004 + * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_004"; + + std::string path = "TEST"; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = DEFAULT_FLAG; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_004"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_005 + * @tc.desc: Test function of AccessCore::DoAccess interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_005"; + + std::string path = DISTRIBUTED_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_005"; +} + +} // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp index 913df19e9..805e99915 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp @@ -17,7 +17,8 @@ #include "access_core.h" #include -#include +#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,6 +31,11 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + const string CLOUDDISK_FILE_PREFIX = "/data/storage/el2/cloud"; + const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; + const string CLOUD_FILE_LOCATION = "user.cloud.location"; + const string POSITION_LOCAL = "1"; + const string POSITION_BOTH = "2"; }; void AccessCoreTest::SetUpTestCase(void) @@ -52,16 +58,45 @@ void AccessCoreTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } +// 递归创建多级目录的辅助函数 +bool CreateDirectoryRecursive(const std::string& path) { + if (path.empty()) { + return false; + } + + size_t pos = 0; + std::string dir; + if (path[0] == '/') { + dir += '/'; + pos++; + } + + while ((pos = path.find('/', pos)) != std::string::npos) { + dir = path.substr(0, pos++); + if (dir.empty()) continue; + if (mkdir(dir.c_str(), 0755) == -1) { + if (errno != EEXIST) { + return false; + } + } + } + + if (mkdir(path.c_str(), 0755) == -1 && errno != EEXIST) { + return false; + } + return true; +} + /** * @tc.name: AccessCoreTest_DoAccess_001 - * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_001"; std::string path; std::optional mode; @@ -69,48 +104,48 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Lev auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_001"; } /** * @tc.name: AccessCoreTest_DoAccess_002 - * @tc.desc: Test function of AccessCore::ValidAccessArgs interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_002"; - std::string path = "TEST"; - std::optional mode; + std::string path = ""; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = DEFAULT_FLAG; - auto res = AccessCore::DoAccess(path, mode); - EXPECT_EQ(res.IsSuccess(), true); + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_002"; } /** * @tc.name: AccessCoreTest_DoAccess_003 - * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_003"; - std::string path = ""; - AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = DEFAULT_FLAG; + std::string path = "test"; + std::optional mode = std::make_optional(AccessModeType::ERROR); - auto res = AccessCore::DoAccess(path, mode, flag); + auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_003"; } /** @@ -122,17 +157,87 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Lev */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_004"; - std::string path = "TEST"; + std::string path = CLOUDDISK_FILE_PREFIX; AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = DEFAULT_FLAG; + AccessFlag flag = LOCAL_FLAG; auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_004"; } +/** + * @tc.name: AccessCoreTest_DoAccess_005 + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_005"; + + std::string path = CLOUDDISK_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + ASSERT_TRUE(CreateDirectoryRecursive(path)); + auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_LOCAL.c_str(), POSITION_LOCAL.size(), 0); + ASSERT_NE(re, -1); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_005"; +} + +/** + * @tc.name: AccessCoreTest_DoAccess_006 + * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_006"; + + std::string path = "test"; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_006"; +} + +/** + * @tc.name: AccessCoreTest_DoAccess_007 + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_007"; + + std::string path = CLOUDDISK_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + ASSERT_TRUE(CreateDirectoryRecursive(path)); + auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_BOTH.c_str(), POSITION_BOTH.size(), 0); + ASSERT_NE(re, -1); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_007"; +} } // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp new file mode 100644 index 000000000..826208e31 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp @@ -0,0 +1,79 @@ +/* + * 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 "dup_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class DupCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void DupCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void DupCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void DupCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void DupCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: DupCoreMockTest_DoDup_001 + * @tc.desc: Test function of DupCore::DoDup interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(DupCoreMockTest, DupCoreMockTest_DoDup_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DupCoreMockTest-begin DupCoreMockTest_DoDup_001"; + + int32_t fd = 1; + + EXPECT_CALL(*uvMock, uv_fs_readlink(_, _, _, _)).WillOnce(Return(-1)); + auto res = DupCore::DoDup(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "DupCoreMockTest-end DupCoreMockTest_DoDup_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp index 6d92836d6..e622a27f6 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp @@ -14,10 +14,10 @@ */ #include "dup_core.h" -#include "mock/uv_fs_mock.h" +#include +#include #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -80,16 +80,15 @@ HWTEST_F(DupCoreTest, DupCoreTest_DoDup_001, testing::ext::TestSize.Level1) HWTEST_F(DupCoreTest, DupCoreTest_DoDup_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "NClassTest-begin DupCoreTest_DoDup_002"; - int32_t fd = 1; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_readlink(_, _, _, _)).WillOnce(Return(-1)); + int32_t fd = open("temp_file.txt", O_CREAT | O_RDWR, 0666); + ASSERT_NE(fd, -1); + close(fd); auto res = DupCore::DoDup(fd); + EXPECT_EQ(res.IsSuccess(), false); GTEST_LOG_(INFO) << "NClassTest-end DupCoreTest_DoDup_002"; } - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index aa3534347..b656a1ad1 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -13,8 +13,10 @@ * limitations under the License. */ + #include "uv_fs_mock.h" + using namespace OHOS::FileManagement::ModuleFileIO; int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, @@ -53,9 +55,9 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); } int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) @@ -128,3 +130,8 @@ int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, i { return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); } + +int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_lstat(loop, req, path, cb); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 736eebb6e..b1e9c4b72 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H -#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H +#ifndef UV_FS_MOCK_H +#define UV_FS_MOCK_H #include "uv.h" @@ -25,7 +25,6 @@ namespace OHOS::FileManagement::ModuleFileIO { class Uvfs { public: static inline std::shared_ptr ins = nullptr; - public: virtual ~Uvfs() = default; virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, @@ -37,7 +36,7 @@ public: virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; @@ -51,9 +50,10 @@ public: virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; - virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; - virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, - uv_fs_cb cb) = 0; + virtual int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, + int64_t off, size_t len, uv_fs_cb cb) = 0; + virtual int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; }; class UvfsMock : public Uvfs { @@ -67,7 +67,7 @@ public: MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); @@ -81,10 +81,11 @@ public: MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); - MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); - MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, - size_t len, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, + int64_t off, size_t len, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_lstat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); }; -} // namespace OHOS::FileManagement::ModuleFileIO -#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file +} // OHOS::FileManagement::ModuleFileIO +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp new file mode 100644 index 000000000..b6aba5316 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp @@ -0,0 +1,107 @@ +/* + * 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 "read_core.h" +#include "uv_fs_mock.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr uvMock = nullptr; +}; + +void ReadCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void ReadCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void ReadCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadCoreMockTest_DoRead_001 + * @tc.desc: Test function of ReadCore::DoRead interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_001"; + + int32_t fd = 1; + void *buf = nullptr; + ArrayBuffer arrayBuffer(buf, 0); + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + + auto res = ReadCore::DoRead(fd, arrayBuffer); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_001"; +} + +/** + * @tc.name: ReadCoreMockTest_DoRead_002 + * @tc.desc: Test function of ReadCore::DoRead interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_002"; + + int32_t fd = 1; + void *buf = nullptr; + ArrayBuffer arrayBuffer(buf, 0); + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1)); + + auto res = ReadCore::DoRead(fd, arrayBuffer); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp index c4dfda62e..8ecb653c5 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp @@ -14,10 +14,11 @@ */ #include "read_core.h" -#include "mock/uv_fs_mock.h" +#include +#include #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -61,7 +62,7 @@ void ReadCoreTest::TearDown(void) */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_001"; + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_001"; int32_t fd = -1; void *buf = nullptr; @@ -70,31 +71,28 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_001, testing::ext::TestSize.Level1) auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_001"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_001"; } /** * @tc.name: ReadCoreTest_DoRead_002 - * @tc.desc: Test function of ReadCore::DoRead interface for FALSE. + * @tc.desc: Test function of ReadCore::DoRead interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_002"; - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_002"; int32_t fd = 1; void *buf = nullptr; - ArrayBuffer arrayBuffer(buf, 0); + ArrayBuffer arrayBuffer(buf, 0xffffffff + 1); + auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_002"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_002"; } /** @@ -106,19 +104,18 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_002, testing::ext::TestSize.Level1) */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_005"; - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1)); + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_003"; int32_t fd = 1; void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0); - auto res = ReadCore::DoRead(fd, arrayBuffer); - EXPECT_EQ(res.IsSuccess(), true); + optional options = std::make_optional(); + options->offset = std::make_optional(-1); + + auto res = ReadCore::DoRead(fd, arrayBuffer, options); + EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_003"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_003"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp index e6d30f2dc..82bf8b12b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp @@ -14,10 +14,11 @@ */ #include "rmdir_core.h" -#include "mock/uv_fs_mock.h" +#include +#include #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -70,4 +71,98 @@ HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_001, testing::ext::TestSize.Lev GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_001"; } +/** + * @tc.name: RmdirCoreTest_DoRmdirent_002 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_002"; + std::string fpath = "invalid?path"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_002"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_003 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_003"; + std::string fpath = "/dir"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_003"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_004 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_004"; + + std::filesystem::create_directories("test_dir"); + std::ofstream("test_dir/test_file.txt") << "test"; + + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_write | std::filesystem::perms::owner_exec, + std::filesystem::perm_options::replace); + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_004"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_005 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_005"; + + std::filesystem::create_directories("test_dir"); + std::ofstream("test_dir/test_file.txt") << "test"; + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_005"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp similarity index 57% rename from interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp rename to interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp index 2910fcb9e..dd6e4b43b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp @@ -15,90 +15,90 @@ #include "symlink_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; using namespace std; -class SymlinkCoreTest : public testing::Test { +class SymlinkCoreMockTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); + static inline shared_ptr uvMock = nullptr; }; -void SymlinkCoreTest::SetUpTestCase(void) +void SymlinkCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; } -void SymlinkCoreTest::TearDownTestCase(void) +void SymlinkCoreMockTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; } -void SymlinkCoreTest::SetUp(void) +void SymlinkCoreMockTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; } -void SymlinkCoreTest::TearDown(void) +void SymlinkCoreMockTest::TearDown(void) { GTEST_LOG_(INFO) << "TearDown"; } /** - * @tc.name: SymlinkCoreTest_DoSymlink_001 + * @tc.name: SymlinkCoreMockTest_DoSymlink_001 * @tc.desc: Test function of SymlinkCore::DoSymlink interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(SymlinkCoreTest, SymlinkCoreTest_DoSymlink_001, testing::ext::TestSize.Level1) +HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreTest_DoSymlink_001"; + GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreMockTest_DoSymlink_001"; string target; string srcPath; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreTest_DoSymlink_001"; + GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_001"; } /** - * @tc.name: SymlinkCoreTest_DoSymlink_002 + * @tc.name: SymlinkCoreMockTest_DoSymlink_002 * @tc.desc: Test function of SymlinkCore::DoSymlink interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(SymlinkCoreTest, SymlinkCoreTest_DoSymlink_002, testing::ext::TestSize.Level1) +HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreTest_DoSymlink_002"; + GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreMockTest_DoSymlink_002"; string target; string srcPath; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreTest_DoSymlink_002"; + GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_002"; } } // OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp new file mode 100644 index 000000000..a8e459014 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp @@ -0,0 +1,169 @@ +/* + * 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 "truncate_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class TruncateCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void TruncateCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void TruncateCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void TruncateCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TruncateCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_001 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_001"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_001"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_002 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_002"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_002"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_003 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_003"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_003"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_004 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_004"; + + FileInfo fileInfo; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_004"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_005 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_005"; + + FileInfo fileInfo; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_005"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp index cb1ef1e16..b9a98e51f 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp @@ -14,10 +14,9 @@ */ #include "truncate_core.h" -#include "mock/uv_fs_mock.h" #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -91,126 +90,4 @@ HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_002, testing::ext::TestSi GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_002"; } -/** - * @tc.name: TruncateCoreTest_DoTruncate_003 - * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_003, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "TruncateCoreTest-begin TruncateCoreTest_DoTruncate_003"; - FileInfo fileInfo; - fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); - - auto res = TruncateCore::DoTruncate(fileInfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_003"; -} - -/** - * @tc.name: TruncateCoreTest_DoTruncate_004 - * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "TruncateCoreTest-begin TruncateCoreTest_DoTruncate_004"; - FileInfo fileInfo; - fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); - EXPECT_CALL(*uv, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); - - auto res = TruncateCore::DoTruncate(fileInfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_004"; -} - -/** - * @tc.name: TruncateCoreTest_DoTruncate_005 - * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_005, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "TruncateCoreTest-begin TruncateCoreTest_DoTruncate_005"; - FileInfo fileInfo; - fileInfo.isPath = true; - fileInfo.fdg = std::make_unique(1); - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); - EXPECT_CALL(*uv, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); - - auto res = TruncateCore::DoTruncate(fileInfo); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_005"; -} - -/** - * @tc.name: TruncateCoreTest_DoTruncate_006 - * @tc.desc: Test function of RmdirCore::DoTruncate interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_006, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "TruncateCoreTest-begin TruncateCoreTest_DoTruncate_006"; - FileInfo fileInfo; - fileInfo.fdg = std::make_unique(1); - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); - - auto res = TruncateCore::DoTruncate(fileInfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_006"; -} - -/** - * @tc.name: TruncateCoreTest_DoTruncate_007 - * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(TruncateCoreTest, TruncateCoreTest_DoTruncate_007, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "TruncateCoreTest-begin TruncateCoreTest_DoTruncate_007"; - FileInfo fileInfo; - fileInfo.fdg = std::make_unique(1); - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); - - auto res = TruncateCore::DoTruncate(fileInfo); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "TruncateCoreTest-end TruncateCoreTest_DoTruncate_007"; -} - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp new file mode 100644 index 000000000..c7d7e60c2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp @@ -0,0 +1,124 @@ +/* + * 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 "utimes_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class UtimesCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void UtimesCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void UtimesCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void UtimesCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void UtimesCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_001 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_001"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_001"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_002 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_002"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_002"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_003 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_003"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp index b81af547a..54ee576cc 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp @@ -14,10 +14,9 @@ */ #include "utimes_core.h" -#include "mock/uv_fs_mock.h" #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -62,84 +61,13 @@ void UtimesCoreTest::TearDown(void) HWTEST_F(UtimesCoreTest, UtimesCoreTest_DoUtimes_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "UtimesCoreTest-begin UtimesCoreTest_DoUtimes_001"; + string path; double mtime = -1; auto res = UtimesCore::DoUtimes(path, mtime); - EXPECT_EQ(res.IsSuccess(), false); GTEST_LOG_(INFO) << "UtimesCoreTest-end UtimesCoreTest_DoUtimes_001"; } -/** - * @tc.name: UtimesCoreTest_DoUtimes_002 - * @tc.desc: Test function of UtimesCore::DoUtimes interface for Failed. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(UtimesCoreTest, UtimesCoreTest_DoUtimes_002, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "UtimesCoreTest-begin UtimesCoreTest_DoUtimes_002"; - string path; - double mtime = 1; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); - - auto res = UtimesCore::DoUtimes(path, mtime); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "UtimesCoreTest-end UtimesCoreTest_DoUtimes_002"; -} - -/** - * @tc.name: UtimesCoreTest_DoUtimes_003 - * @tc.desc: Test function of UtimesCore::DoUtimes interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(UtimesCoreTest, UtimesCoreTest_DoUtimes_003, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "UtimesCoreTest-begin UtimesCoreTest_DoUtimes_003"; - string path; - double mtime = 1; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); - EXPECT_CALL(*uv, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(-1)); - - auto res = UtimesCore::DoUtimes(path, mtime); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "UtimesCoreTest-end UtimesCoreTest_DoUtimes_003"; -} - -/** - * @tc.name: UtimesCoreTest_DoUtimes_004 - * @tc.desc: Test function of UtimesCore::DoUtimes interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(UtimesCoreTest, UtimesCoreTest_DoUtimes_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "UtimesCoreTest-begin UtimesCoreTest_DoUtimes_004"; - string path; - double mtime = 1; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); - EXPECT_CALL(*uv, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(1)); - - auto res = UtimesCore::DoUtimes(path, mtime); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "UtimesCoreTest-end UtimesCoreTest_DoUtimes_004"; -} - } // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp index c6ee32ef6..1741f168f 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp @@ -14,10 +14,8 @@ */ #include "write_core.h" -#include "mock/uv_fs_mock.h" #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -71,88 +69,80 @@ HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_001, testing::ext::TestSize.Level } /** - * @tc.name: WriteCoreTest_DoWrite1_002 - * @tc.desc: Test function of WriteCore::DoWrite3 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite2_001 + * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_002, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite2_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_002"; - int32_t fd = 1; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite2_001"; + int32_t fd = -1; string buffer; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); auto res = WriteCore::DoWrite(fd, buffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_002"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite2_001"; } +#if defined(_WIN64) || defined(__X86_64__) || defined(__ppc64__) || defined(__LP64__) /** - * @tc.name: WriteCoreTest_DoWrite1_003 - * @tc.desc: Test function of WriteCore::DoWrite3 interface for SUCCESS. + * @tc.name: WriteCoreTest_DoWrite1_002 + * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_003, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_003"; - int32_t fd = 1; - string buffer; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(1)); + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_002"; + int32_t fd = -1; + ArrayBuffer buffer(nullptr, 0); auto res = WriteCore::DoWrite(fd, buffer); - EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_003"; + EXPECT_EQ(res.IsSuccess(), false); + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_002"; } +#else +#endif /** - * @tc.name: WriteCoreTest_DoWrite2_001 - * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite1_003 + * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite2_001, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite2_001"; - int32_t fd = -1; - string buffer; + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_003"; + int32_t fd = 1; + ArrayBuffer buffer(nullptr, 1); + std::optional options = std::make_optional(WriteOptions()); + options->offset = std::make_optional(-1); - auto res = WriteCore::DoWrite(fd, buffer); + auto res = WriteCore::DoWrite(fd, buffer, options); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite2_001"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_003"; } /** - * @tc.name: WriteCoreTest_DoWrite2_002 - * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. + * @tc.name: WriteCoreTest_DoWrite1_004 + * @tc.desc: Test function of WriteCore::DoWrite1 interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite2_002, testing::ext::TestSize.Level1) +HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite1_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite2_002"; - int32_t fd = -1; - string buffer; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - - EXPECT_CALL(*uv, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + GTEST_LOG_(INFO) << "WriteCoreTest-begin WriteCoreTest_DoWrite1_004"; + int32_t fd = 1; + ArrayBuffer buffer(nullptr, 1); auto res = WriteCore::DoWrite(fd, buffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite2_002"; + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_004"; } - } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From 21f1d28cce18a2785c9fb67e1c1624d8f59f0129 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Thu, 19 Jun 2025 06:12:46 +0000 Subject: [PATCH 28/39] modify Signed-off-by: yangbiao59 --- .../unittest/js/mod_fs/properties/access_core_test.cpp | 9 ++++++--- .../unittest/js/mod_fs/properties/rmdir_core_test.cpp | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp index 805e99915..bb1d24854 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp @@ -24,6 +24,7 @@ namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; using namespace std; +const mode_t DIR_PERMISSIONS = 0755; class AccessCoreTest : public testing::Test { public: @@ -73,15 +74,17 @@ bool CreateDirectoryRecursive(const std::string& path) { while ((pos = path.find('/', pos)) != std::string::npos) { dir = path.substr(0, pos++); - if (dir.empty()) continue; - if (mkdir(dir.c_str(), 0755) == -1) { + if (dir.empty()) { + continue; + } + if (mkdir(dir.c_str(), DIR_PERMISSIONS) == -1) { if (errno != EEXIST) { return false; } } } - if (mkdir(path.c_str(), 0755) == -1 && errno != EEXIST) { + if (mkdir(path.c_str(), DIR_PERMISSIONS) == -1 && errno != EEXIST) { return false; } return true; diff --git a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp index 82bf8b12b..0cbdf7ebd 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp @@ -121,7 +121,7 @@ HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_004, testing::ext::TestSize.Lev std::filesystem::create_directories("test_dir"); std::ofstream("test_dir/test_file.txt") << "test"; - std::filesystem::permissions("test_dir", + std::filesystem::permissions("test_dir", std::filesystem::perms::owner_write | std::filesystem::perms::owner_exec, std::filesystem::perm_options::replace); -- Gitee From c22aa93f6432245b286c3ceff34b352396c3195b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Fri, 20 Jun 2025 01:19:02 +0000 Subject: [PATCH 29/39] =?UTF-8?q?=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=A9=BA=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp index f9567b0d5..8ba1451ed 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -18,7 +18,6 @@ using namespace OHOS::FileManagement::ModuleFileIO; extern "C" { - int fseek(FILE *stream, long len, int offset) { return ICMock::ins->fseek(stream, len, offset); -- Gitee From 9d29f1dc23a35d6e4539a92cbf0d5b3e0ed03b8e Mon Sep 17 00:00:00 2001 From: tianp Date: Fri, 20 Jun 2025 10:07:59 +0800 Subject: [PATCH 30/39] =?UTF-8?q?=E6=97=A0=E7=AC=A6=E5=8F=B7=E6=95=B0?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp Change-Id: I05f777149aa5cfd57c9780f5a0242599a35c69af --- interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/open_core.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp index 9218629ae..5dbc3b4ac 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp @@ -153,7 +153,7 @@ FsResult FsFile::TryLock(bool exclusive) const } int ret = 0; - auto mode = exclusive ? LOCK_EX : LOCK_SH; + auto mode = static_cast(exclusive ? LOCK_EX : LOCK_SH); ret = flock(fileEntity->fd_.get()->GetFD(), mode | LOCK_NB); if (ret < 0) { HILOGE("Failed to try to lock file"); diff --git a/interfaces/kits/js/src/mod_fs/properties/open_core.cpp b/interfaces/kits/js/src/mod_fs/properties/open_core.cpp index f688e8b5e..8ead99468 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open_core.cpp @@ -75,7 +75,7 @@ static tuple ValidAndConvertFlags(const optional &mode) } flags = static_cast(modeValue); uint32_t invalidMode = (O_WRONLY | O_RDWR); - if ((modeValue & invalidMode) == invalidMode) { + if ((flags & invalidMode) == invalidMode) { HILOGE("Invalid mode"); return { false, flags }; } -- Gitee From 7a1e4012741a5a4f8d3a9db93f75beb2c2805db7 Mon Sep 17 00:00:00 2001 From: tianp Date: Fri, 20 Jun 2025 12:13:44 +0800 Subject: [PATCH 31/39] =?UTF-8?q?open,fileTDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: tianp Change-Id: Ib89a14404e229cc3c60cb2729ee420afb4cbdece --- .../js/mod_fs/class_file/fs_file_mock_test.cpp | 4 ++-- .../js/mod_fs/class_file/fs_file_test.cpp | 2 ++ .../js/mod_fs/properties/open_core_test.cpp | 16 ++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index 90cc1f29f..4f7694aec 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "file_entity.h" #include "fs_file.h" +#include "file_entity.h" #include "system_mock.h" #include "uv_fs_mock.h" @@ -64,7 +64,7 @@ void FsFileMockTest::SetUp(void) const int fdValue = 3; const bool isClosed = false; fileEntity->fd_ = std::make_unique(fdValue, isClosed); - fileEntity->path_ = "/data/test/file_test.txt"; + fileEntity->path_ = "/data/test/test.txt"; fileEntity->uri_ = ""; fsFile = std::make_unique(std::move(fileEntity)); } diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index 9a72cf86f..95d11cfd9 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -18,6 +18,7 @@ #include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -257,6 +258,7 @@ HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetParent_011"; } + /** * @tc.name: FsFileTest_GetPath_012 * @tc.desc: Test function of GetPath() interface for SUCCESS. diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp index 9b40e7ef6..657b27abf 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ -#include - #include "open_core.h" +#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -61,7 +61,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_001"; - string path = "/test/open_test.txt"; + string path = "/test/test.txt"; int32_t mode = -1; auto res = OpenCore::DoOpen(path, mode); @@ -81,7 +81,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_002"; - string path = "/test/open_test.txt"; + string path = "/test/test.txt"; int32_t mode = 3; auto res = OpenCore::DoOpen(path, mode); @@ -101,7 +101,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_003"; - string path = "file://media/open_test.jpg"; + string path = "file://media/test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -121,7 +121,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_004"; - string path = "file://docs/open_test.pdf"; + string path = "file://docs/test.pdf"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -141,7 +141,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_005"; - string path = "content://com.example.provider/open_test.txt"; + string path = "content://com.example.provider/test.txt"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -161,7 +161,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_006"; - string path = "datashare://media/open_test.jpg"; + string path = "datashare://media/test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); -- Gitee From 5cbced5a327b8b6f6eb8b356e9cb482eb6d92441 Mon Sep 17 00:00:00 2001 From: tianp Date: Fri, 20 Jun 2025 14:33:03 +0800 Subject: [PATCH 32/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp Change-Id: I622749fe27b48b0cc4a6710368b387f81d2100ff --- .../js/mod_fs/class_file/fs_file_mock_test.cpp | 4 ++-- .../unittest/js/mod_fs/class_file/fs_file_test.cpp | 2 -- .../js/mod_fs/properties/open_core_mock_test.cpp | 2 +- .../unittest/js/mod_fs/properties/open_core_test.cpp | 12 ++++++------ 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp index 4f7694aec..90cc1f29f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "fs_file.h" #include "file_entity.h" +#include "fs_file.h" #include "system_mock.h" #include "uv_fs_mock.h" @@ -64,7 +64,7 @@ void FsFileMockTest::SetUp(void) const int fdValue = 3; const bool isClosed = false; fileEntity->fd_ = std::make_unique(fdValue, isClosed); - fileEntity->path_ = "/data/test/test.txt"; + fileEntity->path_ = "/data/test/file_test.txt"; fileEntity->uri_ = ""; fsFile = std::make_unique(std::move(fileEntity)); } diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index 95d11cfd9..9a72cf86f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -18,7 +18,6 @@ #include - namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -258,7 +257,6 @@ HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetParent_011"; } - /** * @tc.name: FsFileTest_GetPath_012 * @tc.desc: Test function of GetPath() interface for SUCCESS. diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp index d3b3752b2..33f25f3cd 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "open_core.h" #include "mock/uv_fs_mock.h" +#include "open_core.h" #include #include diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp index 657b27abf..a6cad138a 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/open_core_test.cpp @@ -61,7 +61,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_001"; - string path = "/test/test.txt"; + string path = "/test/open_test.txt"; int32_t mode = -1; auto res = OpenCore::DoOpen(path, mode); @@ -81,7 +81,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_002"; - string path = "/test/test.txt"; + string path = "/test/open_test.txt"; int32_t mode = 3; auto res = OpenCore::DoOpen(path, mode); @@ -101,7 +101,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_003"; - string path = "file://media/test.jpg"; + string path = "file://media/open_test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -121,7 +121,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_004"; - string path = "file://docs/test.pdf"; + string path = "file://docs/open_test.pdf"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -141,7 +141,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_005"; - string path = "content://com.example.provider/test.txt"; + string path = "content://com.example.provider/open_test.txt"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); @@ -161,7 +161,7 @@ HWTEST_F(OpenCoreTest, OpenCoreTest_DoOpen_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "OpenCoreTest-begin OpenCoreTest_DoOpen_006"; - string path = "datashare://media/test.jpg"; + string path = "datashare://media/open_test.jpg"; int32_t mode = 0; auto res = OpenCore::DoOpen(path, mode); -- Gitee From 9f8a6ce7288e547650ec0e2e69bf63f17d692c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Fri, 20 Jun 2025 14:34:06 +0800 Subject: [PATCH 33/39] =?UTF-8?q?=E6=84=8F=E8=A7=81=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/test/unittest/BUILD.gn | 4 ++-- interfaces/test/unittest/js/BUILD.gn | 2 +- interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 1c1ca95da..96bb9d37b 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -18,9 +18,9 @@ group("file_api_unittest") { "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", "js:ani_file_fs_mock_test", - "remote_uri:remote_uri_test", - "task_signal:task_signal_test", "js:ani_file_hash_test", "js:ani_file_securitylabel_test", + "remote_uri:remote_uri_test", + "task_signal:task_signal_test", ] } diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 54faf83e1..ffbc92ef3 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -161,9 +161,9 @@ ohos_unittest("ani_file_securitylabel_test") { ] deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libfs:filemgmt_libfs", - "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", ] external_deps = [ diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h index 8f2bbe88e..622d50fc9 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -17,8 +17,8 @@ #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H #include -#include #include +#include namespace OHOS::FileManagement::ModuleFileIO { -- Gitee From 5f3efcfb653d3ab6d6e19b98474c66162413ade9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Fri, 20 Jun 2025 08:12:59 +0000 Subject: [PATCH 34/39] =?UTF-8?q?=E5=91=BD=E5=90=8Dxiugai?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_atomicfile/fs_atomicfile_test.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp index 081de4750..f506c732f 100644 --- a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp @@ -23,8 +23,8 @@ namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace std; namespace fs = std::filesystem; -string filePath = "/data/test/FsAtomicfileTest.txt"; -string deleteFile = "/data/test/FsAtomicfileDelTest.txt"; +string g_filePath = "/data/test/FsAtomicfileTest.txt"; +string g_deleteFile = "/data/test/FsAtomicfileDelTest.txt"; class FsAtomicfileTest : public testing::Test { public: @@ -36,7 +36,7 @@ public: void FsAtomicfileTest::SetUpTestCase(void) { - ofstream tempfile(filePath); + ofstream tempfile(g_filePath); tempfile << "hello world"; tempfile.close(); GTEST_LOG_(INFO) << "SetUpTestCase"; @@ -44,7 +44,7 @@ void FsAtomicfileTest::SetUpTestCase(void) void FsAtomicfileTest::TearDownTestCase(void) { - filesystem::remove(filePath); + filesystem::remove(g_filePath); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -69,12 +69,12 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); string path = stream->GetPath(); - EXPECT_EQ(path, filePath); + EXPECT_EQ(path, g_filePath); GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; } @@ -90,7 +90,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -181,7 +181,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSi { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -248,7 +248,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -276,7 +276,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -304,7 +304,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; - auto ret = FsAtomicFile::Constructor(deleteFile); + auto ret = FsAtomicFile::Constructor(g_deleteFile); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -332,7 +332,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); -- Gitee From 45f2502494a23c499cbdf1f08d0b0c7f02f03433 Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 21 Jun 2025 11:40:00 +0800 Subject: [PATCH 35/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp Change-Id: Idabdd798d06501c64f5ab4cbfd7fc638c393a3b2 --- .../js/mod_fs/class_file/fs_file_test.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp index 9a72cf86f..e432f66f6 100644 --- a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -29,8 +29,8 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - std::unique_ptr fileEntity; - std::unique_ptr fsFile; + unique_ptr fileEntity; + unique_ptr fsFile; }; void FsFileTest::SetUpTestCase(void) @@ -47,13 +47,13 @@ void FsFileTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; - fileEntity = std::make_unique(); + fileEntity = make_unique(); const int fdValue = 3; const bool isClosed = false; - fileEntity->fd_ = std::make_unique(fdValue, isClosed); + fileEntity->fd_ = make_unique(fdValue, isClosed); fileEntity->path_ = "/data/test/file_test.txt"; fileEntity->uri_ = ""; - fsFile = std::make_unique(std::move(fileEntity)); + fsFile = make_unique(move(fileEntity)); } void FsFileTest::TearDown(void) @@ -106,7 +106,7 @@ HWTEST_F(FsFileTest, FsFileTest_GetFD_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetFD_003"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetFD(); EXPECT_EQ(result.IsSuccess(), false); @@ -124,7 +124,7 @@ HWTEST_F(FsFileTest, FsFileTest_GetPath_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_004"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetPath(); EXPECT_EQ(result.IsSuccess(), false); @@ -142,7 +142,7 @@ HWTEST_F(FsFileTest, FsFileTest_GetName_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_005"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetName(); EXPECT_EQ(result.IsSuccess(), false); @@ -160,7 +160,7 @@ HWTEST_F(FsFileTest, FsFileTest_GetParent_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_006"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->GetParent(); EXPECT_EQ(result.IsSuccess(), false); @@ -178,7 +178,7 @@ HWTEST_F(FsFileTest, FsFileTest_Lock_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_Lock_007"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->Lock(true); EXPECT_EQ(result.IsSuccess(), false); @@ -196,7 +196,7 @@ HWTEST_F(FsFileTest, FsFileTest_TryLock_008, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_TryLock_008"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->TryLock(true); EXPECT_EQ(result.IsSuccess(), false); @@ -214,7 +214,7 @@ HWTEST_F(FsFileTest, FsFileTest_UnLock_009, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_UnLock_009"; - fsFile = std::make_unique(nullptr); + fsFile = make_unique(nullptr); auto result = fsFile->UnLock(); EXPECT_EQ(result.IsSuccess(), false); -- Gitee From 0c6d11139d0ab3eb179cec29938e8f392b57e886 Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 21 Jun 2025 17:00:40 +0800 Subject: [PATCH 36/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp Change-Id: Ibab09d027866fd38ce214f5730c2b41b924240b2 --- interfaces/test/unittest/js/BUILD.gn | 29 ++++++++++++++++++ .../mod_fs/class_stat/fs_stat_mock_test.cpp | 30 +++++++++---------- .../js/mod_fs/class_stat/fs_stat_test.cpp | 1 - .../js/mod_statvfs/statvfs_core_test.cpp | 8 +++++ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 36bffa890..aed530837 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -181,4 +181,33 @@ ohos_unittest("ani_file_securitylabel_test") { "googletest:gmock_main", "googletest:gtest_main", ] +} + +ohos_unittest("ani_file_statvfs_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_statvfs/statvfs_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_statvfs", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_statvfs", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + ] } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp index 98093ee64..2de8331e0 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_test.cpp @@ -38,7 +38,7 @@ public: void FsStatMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - sys = std::make_shared(); + sys = make_shared(); System::ins = sys; } @@ -70,17 +70,17 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_001, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_001"; - std::unique_ptr statEntity; - std::unique_ptr fsStat; - statEntity = std::make_unique(); - statEntity->fileInfo_ = std::make_unique(); + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); statEntity->fileInfo_->isPath = true; int length = 100; - string testPath = "/test/path"; - statEntity->fileInfo_->path = std::make_unique(length); + string testPath = "/test/stat_path"; + statEntity->fileInfo_->path = make_unique(length); strncpy_s(statEntity->fileInfo_->path.get(), length, testPath.c_str(), testPath.size()); statEntity->fileInfo_->path.get()[99] = '\0'; - fsStat = std::make_unique(std::move(statEntity)); + fsStat = make_unique(move(statEntity)); EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)); EXPECT_EQ(fsStat->GetLocation(), 1); @@ -99,15 +99,15 @@ HWTEST_F(FsStatMockTest, FsStatMockTest_GetLocation_002, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsStatMockTes-begin FsStatMockTest_GetLocation_002"; - std::unique_ptr statEntity; - std::unique_ptr fsStat; - statEntity = std::make_unique(); - statEntity->fileInfo_ = std::make_unique(); + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); statEntity->fileInfo_->isPath = false; - const int fdValue = 3; + const int fdValue = 3; //模拟fd为3 const bool isClosed = false; - statEntity->fileInfo_->fdg = std::make_unique(fdValue, isClosed); - fsStat = std::make_unique(std::move(statEntity)); + statEntity->fileInfo_->fdg = make_unique(fdValue, isClosed); + fsStat = make_unique(move(statEntity)); EXPECT_CALL(*sys, fgetxattr(_, _, _, _)).WillOnce(Return(1)); EXPECT_EQ(fsStat->GetLocation(), 1); diff --git a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp index 15a8640f2..3e74ed320 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp @@ -17,7 +17,6 @@ #include - namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; diff --git a/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp b/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp index 31ea3914b..82316d884 100644 --- a/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp +++ b/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp @@ -36,6 +36,10 @@ void StatvFsCoreTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; int32_t fd = open("/data/test/statvfs.txt", O_CREAT | O_RDWR, 0644); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } close(fd); } @@ -89,6 +93,8 @@ HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetFreeSize_002, testing::ext::TestS auto result = ModuleStatvfs::StatvfsCore::DoGetFreeSize("/test/path"); EXPECT_EQ(result.IsSuccess(), false); + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetFreeSize_002"; } @@ -127,6 +133,8 @@ HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetTotalSize_004, testing::ext::Test auto result = ModuleStatvfs::StatvfsCore::DoGetTotalSize("/test/path"); EXPECT_EQ(result.IsSuccess(), false); + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_004"; } -- Gitee From 054307c8b064c53547c203c19fa73bdd5be3aa07 Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 21 Jun 2025 18:40:50 +0800 Subject: [PATCH 37/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp Change-Id: Idde19393e78404bcb95e9e03bab6bec38b1537c2 --- interfaces/test/unittest/js/BUILD.gn | 15 +++++++-------- .../js/mod_fs/properties/copy_dir_core_test.cpp | 6 +++--- .../js/mod_fs/properties/lstat_core_mock_test.cpp | 2 +- .../js/mod_fs/properties/lstat_core_test.cpp | 2 ++ .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 0cc49253f..7e51b7d20 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -32,21 +32,20 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ - "mod_fs/class_stat/fs_stat_test.cpp", - "mod_fs/class_file/fs_file_test.cpp", "mod_fs/class_atomicfile/fs_atomicfile_test.cpp", + "mod_fs/class_file/fs_file_test.cpp", "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/class_stat/fs_stat_test.cpp", "mod_fs/class_stream/fs_stream_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/copy_dir_core_test.cpp", "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", + "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", - "mod_fs/class_stream/fs_stream_test.cpp", - "mod_fs/properties/lstat_core_test.cpp", - "mod_fs/properties/copy_dir_core_test.cpp", ] deps = [ @@ -92,13 +91,13 @@ ohos_unittest("ani_file_fs_mock_test") { ] sources = [ - "mod_fs/properties/lstat_core_mock_test.cpp", - "mod_fs/class_stat/fs_stat_mock_test.cpp", "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/class_stat/fs_stat_mock_test.cpp", "mod_fs/class_stream/fs_stream_mock_test.cpp", "mod_fs/class_stream/mock/c_mock.cpp", - "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/lstat_core_mock_test.cpp", "mod_fs/properties/mock/system_mock.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", "mod_fs/properties/open_core_mock_test.cpp", diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp index 0c2431388..300697131 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -47,8 +47,8 @@ void CopyDirCoreTest::SetUpTestCase(void) { srcPath = filesystem::temp_directory_path() / "src/"; destPath = filesystem::temp_directory_path() / "dest/"; - std::filesystem::create_directory(srcPath); - std::filesystem::create_directory(destPath); + filesystem::create_directory(srcPath); + filesystem::create_directory(destPath); GTEST_LOG_(INFO) << "SetUpTestCase"; } @@ -157,7 +157,7 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize. string dest = destPath.string() + "/invalid_file.txt"; filesystem::path(dest).remove_filename(); filesystem::create_directories(filesystem::path(dest).parent_path()); - std::ofstream(dest).close(); // 创建文件而非目录 + ofstream(dest).close(); // 创建文件而非目录 auto result = CopyDirCore::DoCopyDir(src, dest, optional()); diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp index f03ea7bdf..691f2b3de 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp @@ -36,7 +36,7 @@ public: void LstatCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - uvMock = std::make_shared(); + uvMock = make_shared(); Uvfs::ins = uvMock; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp index 8996b3b48..97ae4dd65 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp @@ -66,6 +66,8 @@ HWTEST_F(LstatCoreTest, LstatCoreTest_DoLstat_001, testing::ext::TestSize.Level1 auto res = LstatCore::DoLstat("/invalid/test/lstat.txt"); EXPECT_EQ(res.IsSuccess(), false); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); GTEST_LOG_(INFO) << "LstatCoreTest-end LstatCoreTest_DoLstat_001"; } diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 60c4f3b0e..2cb8bd80e 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -53,9 +53,9 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); } int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) -- Gitee From cb347544f74b45c328d1c1c1e92c70952943b2b2 Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 21 Jun 2025 20:26:01 +0800 Subject: [PATCH 38/39] =?UTF-8?q?=E6=84=8F=E8=A7=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: tianp Change-Id: I89cf7105ab94774d434524867f2f056a894fe0a6 --- .../mod_fs/properties/copy_dir_core_test.cpp | 65 ++++++++----------- .../js/mod_fs/properties/lstat_core_test.cpp | 3 + 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp index 300697131..ad9b034bb 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -26,8 +26,8 @@ using namespace std; class CopyDirCoreTest : public testing::Test { public: - static filesystem::path srcPath; - static filesystem::path destPath; + static filesystem::path g_srcPath; + static filesystem::path g_destPath; static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); @@ -40,23 +40,23 @@ public: } }; -filesystem::path CopyDirCoreTest::srcPath; -filesystem::path CopyDirCoreTest::destPath; +filesystem::path CopyDirCoreTest::g_srcPath; +filesystem::path CopyDirCoreTest::g_destPath; void CopyDirCoreTest::SetUpTestCase(void) { - srcPath = filesystem::temp_directory_path() / "src/"; - destPath = filesystem::temp_directory_path() / "dest/"; - filesystem::create_directory(srcPath); - filesystem::create_directory(destPath); + g_srcPath = filesystem::temp_directory_path() / "src/"; + g_destPath = filesystem::temp_directory_path() / "dest/"; + filesystem::create_directory(g_srcPath); + filesystem::create_directory(g_destPath); GTEST_LOG_(INFO) << "SetUpTestCase"; } void CopyDirCoreTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; - filesystem::remove_all(srcPath); - filesystem::remove_all(destPath); + filesystem::remove_all(g_srcPath); + filesystem::remove_all(g_destPath); } void CopyDirCoreTest::SetUp(void) @@ -75,14 +75,13 @@ void CopyDirCoreTest::TearDown(void) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_001"; - string src = srcPath.string() + "/test01"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test01"; + string dest = g_destPath.string(); filesystem::create_directories(src); auto result = CopyDirCore::DoCopyDir(src, dest, optional()); @@ -99,14 +98,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_001, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_002"; - string src = srcPath.string() + "/test02"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test02"; + string dest = g_destPath.string(); filesystem::create_directories(src); int invalidMode = COPYMODE_MAX + 1; @@ -124,14 +122,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_002, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_003"; - string src = srcPath.string() + "/non_existent"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/non_existent"; + string dest = g_destPath.string(); auto result = CopyDirCore::DoCopyDir(src, dest, optional()); @@ -147,14 +144,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_003, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_004"; - string src = srcPath.string(); - string dest = destPath.string() + "/invalid_file.txt"; + string src = g_srcPath.string(); + string dest = g_destPath.string() + "/invalid_file.txt"; filesystem::path(dest).remove_filename(); filesystem::create_directories(filesystem::path(dest).parent_path()); ofstream(dest).close(); // 创建文件而非目录 @@ -173,14 +169,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_005, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_005"; - string src = srcPath.string(); - string dest = srcPath.string(); + string src = g_srcPath.string(); + string dest = g_srcPath.string(); auto result = CopyDirCore::DoCopyDir(src, dest, optional()); @@ -196,14 +191,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_005, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_006, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_006"; - string src = srcPath.string() + "/test06"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test06"; + string dest = g_destPath.string(); filesystem::create_directories(src); CreateTestFile(src + "/file1.txt", "content1"); CreateTestFile(src + "/file2.txt", "content2"); @@ -222,14 +216,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_006, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_007, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_007"; - string src = srcPath.string() + "/test07"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test07"; + string dest = g_destPath.string(); filesystem::create_directories(src + "/subdir1"); filesystem::create_directories(src + "/subdir2"); CreateTestFile(src + "/subdir1/file1.txt", "sub1_content1"); @@ -249,14 +242,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_007, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_008, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_008"; - string src = srcPath.string() + "/test08"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test08"; + string dest = g_destPath.string(); filesystem::create_directories(src); CreateTestFile(src + "/file1.txt", "content1"); @@ -278,14 +270,13 @@ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_008, testing::ext::TestSize. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: AR000IGDNF */ HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_009, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_009"; - string src = srcPath.string() + "/test09"; - string dest = destPath.string(); + string src = g_srcPath.string() + "/test09"; + string dest = g_destPath.string(); filesystem::create_directories(src); CreateTestFile(src + "/file1.txt", "content1"); diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp index 97ae4dd65..c40e99f2f 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp @@ -34,6 +34,9 @@ void LstatCoreTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; int32_t fd = open("/data/test/lstat.txt", CREATE | O_RDWR, 0644); + if (fd <= 0) { + ASSERT_TRUE(false); + } close(fd); } -- Gitee From 78915ee293cde06381cc1c95d3bfa4692a3a2103 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Mon, 23 Jun 2025 09:16:57 +0000 Subject: [PATCH 39/39] modify Signed-off-by: yangbiao59 --- .../mod_fs/properties/access_core_mock_test.cpp | 16 ++++++++-------- .../js/mod_fs/properties/access_core_test.cpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp index a44f8bb38..b054b2a07 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -67,7 +67,7 @@ void AccessCoreMockTest::TearDown(void) */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_001"; std::string path = "TEST"; std::optional mode; @@ -76,7 +76,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::Test auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_001"; } /** @@ -88,7 +88,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_002"; std::string path = "TEST"; std::optional mode; @@ -98,7 +98,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::Test auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_002"; } /** @@ -110,7 +110,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_002, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_003"; std::string path = "TEST"; AccessModeType mode = AccessModeType::EXIST; @@ -120,7 +120,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::Test auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_003"; } /** @@ -132,7 +132,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_003, testing::ext::Test */ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreMockTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_004"; std::string path = "TEST"; AccessModeType mode = AccessModeType::EXIST; @@ -143,7 +143,7 @@ HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_004, testing::ext::Test auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreMockTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_004"; } /** diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp index bb1d24854..6460d9a65 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp @@ -208,7 +208,7 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_006, testing::ext::TestSize.Lev { GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_006"; - std::string path = "test"; + std::string path = "AccessCoreTest"; AccessModeType mode = AccessModeType::EXIST; AccessFlag flag = LOCAL_FLAG; -- Gitee