diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index bbf0c5d38c3a4b7b637dfc8bb091178251911648..928421ec07e6f786d2a9ff6dad3b3b5add7f0fcd 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -662,6 +662,8 @@ ohos_shared_library("ani_file_fs") { include_dirs = [ "include/ipc", "src/mod_fs/ani", + "src/mod_fs/class_atomicfile", + "src/mod_fs/class_atomicfile/ani", "src/mod_fs/class_file", "src/mod_fs/class_file/ani", "src/mod_fs/class_randomaccessfile", @@ -686,6 +688,8 @@ ohos_shared_library("ani_file_fs") { "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", + "src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp", + "src/mod_fs/class_atomicfile/fs_atomicfile.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", "src/mod_fs/class_file/ani/file_wrapper.cpp", "src/mod_fs/class_file/file_instantiator.cpp", 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 f0a10fb222d5f5ea0e54be348062636752d64330..564d7bc56e1baf1dd65cdcaa42f1521936887de3 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -219,6 +219,52 @@ std::tuple TypeConverter::ToArrayBuffer(ani_env *env, ani_arr return { true, ArrayBuffer { std::move(buf), length } }; } +std::tuple TypeConverter::ToAniArrayBuffer(ani_env *env, void *buffer, size_t length) +{ + if (env == nullptr) { + return { false, nullptr }; + } + + static const char *className = "Lescompat/ArrayBuffer;"; + ani_status ret; + ani_class cls; + if ((ret = env->FindClass(className, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", className, ret); + return { false, nullptr }; + } + + ani_method ctor; + if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { + HILOGE("Not found ctor, err: %{private}d", ret); + return { false, nullptr }; + } + + ani_object obj; + if ((ret = env->Object_New(cls, ctor, &obj, length)) != ANI_OK) { + HILOGE("New Uint8Array err: %{private}d", ret); + return { false, nullptr }; + } + + if (!buffer || !length) { + return { true, static_cast(obj) }; + } + + void *buf = nullptr; + ani_size len = 0; + + if ((ANI_OK != env->ArrayBuffer_GetInfo(static_cast(obj), &buf, &len)) && (!buf)) { + return { false, nullptr }; + } + + int res = memcpy_s(buf, length, buffer, length); + if (res != 0) { + return { false, nullptr }; + } + len = length; + + return { true, static_cast(obj) }; +} + std::tuple TypeConverter::ToAniStringList( ani_env *env, const std::string strList[], const uint32_t length) { diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.h b/interfaces/kits/js/src/common/ani_helper/type_converter.h index a6d06265992b98d16aedb01d1a7acf99367e817d..bac25493fda5311c32340a5ce0371dd981a9af1d 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.h +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.h @@ -32,6 +32,7 @@ public: static std::tuple ToUTF8String(ani_env *env, const ani_string &path); static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &value); static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &value); + static std::tuple ToAniArrayBuffer(ani_env *env, void *buffer, size_t length); static std::tuple ToAniString(ani_env *env, std::string str); static std::tuple ToAniString(ani_env *env, std::string str, size_t size); static std::tuple ToAniString(ani_env *env, const char *str); 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 10e86d4ba2a557e12d3ea4ee639121eadafe26ba..4be8bb09bc2ac048f545c1e12d6ad27ed432a4fc 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 @@ -18,6 +18,7 @@ #include #include "access_ani.h" +#include "atomicfile_ani.h" #include "ani_signature.h" #include "bind_function.h" #include "close_ani.h" @@ -163,6 +164,24 @@ static ani_status BindTaskSignalClassMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindAtomicFileMethods(ani_env *env) +{ + static const char *className = "L@ohos/file/fs/fileIo/AtomicFile;"; + + std::array methods = { + ani_native_function { "getPath", nullptr, reinterpret_cast(AtomicFileAni::GetPath) }, + ani_native_function { "getBaseFile", nullptr, reinterpret_cast(AtomicFileAni::GetBaseFile) }, + ani_native_function { "readFully", nullptr, reinterpret_cast(AtomicFileAni::ReadFully) }, + ani_native_function { "nativeStartWrite", nullptr, reinterpret_cast(AtomicFileAni::StartWrite) }, + 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 { "openRead", nullptr, reinterpret_cast(AtomicFileAni::OpenRead) }, + }; + + return BindClass(env, className, methods); +} const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); const static string mkdirCtorSig1 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::booleanType }); @@ -215,25 +234,9 @@ static ani_status BindStaticMethods(ani_env *env) return BindClass(env, classDesc, methods); } -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +static ani_status DoBindMethods(ani_env *env) { - if (vm == nullptr) { - HILOGE("Invalid parameter vm"); - return ANI_INVALID_ARGS; - } - - if (result == nullptr) { - HILOGE("Invalid parameter result"); - return ANI_INVALID_ARGS; - } - - ani_env *env; - ani_status status = vm->GetEnv(ANI_VERSION_1, &env); - if (status != ANI_OK) { - HILOGE("Invalid ani version!"); - return ANI_INVALID_VERSION; - } - + ani_status status; if ((status = BindStaticMethods(env)) != ANI_OK) { HILOGE("Cannot bind native static methods for BindStaticMethods!"); return status; @@ -274,6 +277,38 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; + if ((status = BindAtomicFileMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for AtomicFile Class!"); + return status; + }; + + return ANI_OK; +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = DoBindMethods(env); + if (status != ANI_OK) { + return status; + } + *result = ANI_VERSION_1; return ANI_OK; } 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 7a7c409778757930988b6f8f8c5b9a9bb3ed9fed..df9bfb23f058a74a382161bbace8dad9551b51b1 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 @@ -15,6 +15,16 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; import stream from '@ohos.util.stream'; +const UNKNOWN_ERR: number = 13900042 +const UNKNOWN_MSG: string = "Unknown error" + +function createBusinessError(code: number, msg: string): BusinessError { + let err = new BusinessError(); + err.code = code; + err.message = msg; + return err; +} + namespace fileIo { export namespace OpenMode { export const READ_ONLY = 0o0; @@ -1756,8 +1766,8 @@ export class StreamInner implements Stream { } export class ReadStream extends stream.Readable { - private pathInner: string; - private bytesReadInner: number; + path: string; + bytesRead: number; private offset: number; private start?: number; private end?: number; @@ -1765,22 +1775,14 @@ export class ReadStream extends stream.Readable { constructor(path: string, options?: ReadStreamOptions) { super(); - this.pathInner = path; - this.bytesReadInner = 0; + this.path = path; + this.bytesRead = 0; this.start = options?.start; this.end = options?.end; - this.stream = createStreamSync(this.pathInner, 'r'); + this.stream = createStreamSync(this.path, 'r'); this.offset = this.start ?? 0; } - get path(): string { - return this.pathInner; - } - - get bytesRead(): number { - return this.bytesReadInner; - } - seek(offset: number, whence?: WhenceType): number { if (whence === undefined) { let off = this.stream?.seek(offset); @@ -1822,7 +1824,7 @@ export class ReadStream extends stream.Readable { this.stream?.read(buffer, { offset: off, length: readSize }) .then((readOut: number) => { if (readOut > 0) { - this.bytesReadInner += readOut; + this.bytesRead += readOut; this.push(new Uint8Array(buffer.slice(0, readOut))); } if (readOut !== readSize || readOut < size) { @@ -1834,8 +1836,8 @@ export class ReadStream extends stream.Readable { } export class WriteStream extends stream.Writable { - private pathInner: string; - private bytesWrittenInner: number; + path: string; + bytesWritten: number; private offset: number; private mode: string; private start?: number; @@ -1843,22 +1845,14 @@ export class WriteStream extends stream.Writable { constructor(path: string, options?: WriteStreamOptions) { super(); - this.pathInner = path; - this.bytesWrittenInner = 0; + this.path = path; + this.bytesWritten = 0; this.start = options?.start; this.mode = this.convertOpenMode(options?.mode); - this.stream = createStreamSync(this.pathInner, this.mode); + this.stream = createStreamSync(this.path, this.mode); this.offset = this.start ?? 0; } - get path(): string { - return this.pathInner; - } - - get bytesWritten(): number { - return this.bytesWrittenInner; - } - seek(offset: number, whence?: WhenceType): number { if (whence === undefined) { let off = this.stream?.seek(offset); @@ -1878,6 +1872,10 @@ export class WriteStream extends stream.Writable { this.stream?.close(); } + closeSync(): void { + this.stream?.closeSync(); + } + doInitialize(callback: () => void): void { callback(); } @@ -1886,7 +1884,7 @@ export class WriteStream extends stream.Writable { this.stream?.write(chunk, { offset: this.offset }) .then((writeIn: number) => { this.offset += writeIn; - this.bytesWrittenInner += writeIn; + this.bytesWritten += writeIn; callback(); }) .finally(() => { @@ -1915,6 +1913,53 @@ export class WriteStream extends stream.Writable { } } +export class AtomicFile { + static { + loadLibrary("ani_file_fs"); + } + + private nativePtr: long = 0; + private native getPath(): string; + private writeStream: WriteStream | null = null; + + native constructor(path: string); + + native getBaseFile(): File; + + native openRead(): ReadStream; + + native readFully(): ArrayBuffer; + + native nativeStartWrite(): WriteStream; + startWrite(): WriteStream { + let ws = this.nativeStartWrite(); + this.writeStream = ws; + return ws; + } + + native nativeFinishWrite(): void; + finishWrite(): void { + if (!this.writeStream) { + throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); + } + this.writeStream?.close(); + this.nativeFinishWrite(); + this.writeStream = null; + }; + + native nativeFailWrite(): void; + failWrite(): void { + if (!this.writeStream) { + throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); + } + this.writeStream?.close(); + this.nativeFailWrite(); + this.writeStream = null; + }; + + native delete(): void; +} + export enum WhenceType { SEEK_SET = 0, SEEK_CUR = 1, 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 new file mode 100644 index 0000000000000000000000000000000000000000..5dc3e3bac009eae40dc57679682581e36c285d16 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -0,0 +1,356 @@ +/* + * 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 "atomicfile_ani.h" + +#include + +#include "error_handler.h" +#include "file_wrapper.h" +#include "filemgmt_libhilog.h" +#include "fs_atomicfile.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; + +const std::string READ_STREAM_CLASS = "ReadStream"; +const std::string WRITE_STREAM_CLASS = "WriteStream"; +const std::string TEMP_FILE_SUFFIX = "_XXXXXX"; + +void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj) +{ + auto [succ, filePath] = TypeConverter::ToUTF8String(env, pathObj); + if (!succ) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, E_PARAMS); + return; + } + + auto ret = FsAtomicFile::Constructor(filePath); + 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 AtomicFile"); + ErrorHandler::Throw(env, EIO); + return; + } +} + +static FsAtomicFile *Unwrap(ani_env *env, ani_object object) +{ + ani_long file; + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &file); + if (ret != ANI_OK) { + HILOGE("Unwrap file err: %{private}d", ret); + return nullptr; + } + + return reinterpret_cast(file); +} + +ani_string AtomicFileAni::GetPath(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto file = Unwrap(env, object); + if (file == nullptr) { + ErrorHandler::Throw(env, E_PARAMS); + return nullptr; + } + + string path = file->GetPath(); + auto [succ, result] = TypeConverter::ToAniString(env, path); + if (!succ) { + HILOGE("ToAniString failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_object AtomicFileAni::GetBaseFile(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto ret = atomicFile->GetBaseFile(); + if (!ret.IsSuccess()) { + HILOGE("Failed to GetBaseFile"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const FsFile *fsFile = ret.GetData().value(); + auto result = FileWrapper::Wrap(env, move(fsFile)); + if (result == nullptr) { + HILOGE("Failed to wrap"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +static ani_object CreateReadStream(ani_env *env, ani_string filePath) +{ + static const char *className = "L@ohos/file/fs/fileIo/ReadStream;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + return move(obj); +} + +static ani_object CreateWriteStream(ani_env *env, ani_string filePath) +{ + static const char *className = "L@ohos/file/fs/fileIo/WriteStream;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + return move(obj); +} + +static ani_object CreateStream( + ani_env *env, [[maybe_unused]] ani_object object, const std::string &streamName, const std::string &fineName) +{ + auto [succ, filePath] = TypeConverter::ToAniString(env, fineName); + if (!succ) { + HILOGE("Failed to ani_string"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + if (streamName == READ_STREAM_CLASS) { + auto stream = CreateReadStream(env, filePath); + if (stream == nullptr) { + HILOGE("Failed to create read stream"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return move(stream); + } + if (streamName == WRITE_STREAM_CLASS) { + auto stream = CreateWriteStream(env, filePath); + if (stream == nullptr) { + HILOGE("Failed to create write stream"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return move(stream); + } + + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; +} + +ani_object AtomicFileAni::OpenRead(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto entity = atomicFile->GetEntity(); + if (entity == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return CreateStream(env, object, READ_STREAM_CLASS, entity->baseFileName); +} + +ani_arraybuffer AtomicFileAni::ReadFully(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto ret = atomicFile->ReadFully(); + if (!ret.IsSuccess()) { + HILOGE("Failed to read fully"); + ErrorHandler::Throw(env, ret.GetError()); + return nullptr; + } + + auto &bufferData = ret.GetData().value(); + uint8_t *buffer = bufferData->buffer; + size_t length = bufferData->length; + auto [succ, obj] = TypeConverter::ToAniArrayBuffer(env, buffer, length); + if (!succ) { + HILOGE("Failed to ani_arrayBuffer"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return obj; +} + +ani_object AtomicFileAni::StartWrite(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto entity = atomicFile->GetEntity(); + if (entity == nullptr) { + HILOGE("Failed to get atomicFile entity"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + fs::path filePath = entity->newFileName; + fs::path parentPath = filePath.parent_path(); + if (access(parentPath.c_str(), F_OK) != 0) { + HILOGE("Parent directory does not exist, err:%{public}d", errno); + ErrorHandler::Throw(env, ENOENT); + return nullptr; + } + + char *tmpfile = const_cast(entity->newFileName.c_str()); + if (mkstemp(tmpfile) == -1) { + HILOGE("Fail to create tmp file err:%{public}d!", errno); + ErrorHandler::Throw(env, ENOENT); + return nullptr; + } + + ani_object writeStream = CreateStream(env, object, WRITE_STREAM_CLASS, entity->newFileName); + if (writeStream == nullptr) { + HILOGE("Failed to create write stream"); + return nullptr; + } + + return writeStream; +} + +void AtomicFileAni::FinishWrite(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return; + } + + auto entity = atomicFile->GetEntity(); + if (entity == nullptr) { + HILOGE("Failed to get atomicFile entity"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return; + } + + auto ret = atomicFile->FinishWrite(); + if (!ret.IsSuccess()) { + HILOGE("Failed to finish write"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + } + return; +} + +void AtomicFileAni::FailWrite(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return; + } + + auto entity = atomicFile->GetEntity(); + if (entity == nullptr) { + HILOGE("Failed to get atomicFile entity"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return; + } + + auto ret = atomicFile->FailWrite(); + if (!ret.IsSuccess()) { + HILOGE("Failed to fail write"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + } + return; +} + +void AtomicFileAni::Delete(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto atomicFile = Unwrap(env, object); + if (atomicFile == nullptr) { + HILOGE("Failed to get atomicFile"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return; + } + + auto ret = atomicFile->Delete(); + if (!ret.IsSuccess()) { + HILOGE("Failed to delete"); + ErrorHandler::Throw(env, ret.GetError()); + return; + } + + return; +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.h b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..08898f125e60a2f3ae79f39ae7340428392a1c87 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.h @@ -0,0 +1,43 @@ +/* + * 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_FS_ATOMICFILE_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_ATOMICFILE_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class AtomicFileAni final { +public: + static void Constructor(ani_env *env, ani_object obj, ani_string pathObj); + static ani_string GetPath(ani_env *env, [[maybe_unused]] ani_object object); + static ani_object GetBaseFile(ani_env *env, [[maybe_unused]] ani_object object); + static ani_object OpenRead(ani_env *env, [[maybe_unused]] ani_object object); + static ani_arraybuffer ReadFully(ani_env *env, [[maybe_unused]] ani_object object); + static ani_object StartWrite(ani_env *env, [[maybe_unused]] ani_object object); + static void FinishWrite(ani_env *env, [[maybe_unused]] ani_object object); + static void FailWrite(ani_env *env, [[maybe_unused]] ani_object object); + static void Delete(ani_env *env, [[maybe_unused]] ani_object object); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_ATOMICFILE_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2caa33889b4355e772b956cb41798fe94bbb0e53 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.cpp @@ -0,0 +1,235 @@ +/* + * 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_atomicfile.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "fs_atomicfile_entity.h" +#include "filemgmt_libfs.h" +#include "file_instantiator.h" +#include "file_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace fs = std::filesystem; + +const std::string READ_STREAM_CLASS = "ReadStream"; +const std::string WRITE_STREAM_CLASS = "WriteStream"; +const std::string TEMP_FILE_SUFFIX = "_XXXXXX"; + +FsAtomicFileEntity *FsAtomicFile::GetEntity() +{ + if (!entity) { + return nullptr; + } + return entity.get(); +} + +void FsAtomicFile::FinalizeCallback(void *finalizeData, [[maybe_unused]] void *finalizeHint) +{ + BufferData *bufferData = static_cast(finalizeData); + delete bufferData; +} + +string FsAtomicFile::GetPath() +{ + return entity->baseFileName; +} + +FsResult FsAtomicFile::GetBaseFile() +{ + if (entity == nullptr) { + HILOGE("Failed to get atomicFileEntity"); + return FsResult::Error(UNKNOWN_ERR); + } + + if (entity->baseFileName.size() >= PATH_MAX) { + HILOGE("Base file name is too long"); + return FsResult::Error(UNKNOWN_ERR); + } + + auto absolutePath = std::make_unique(PATH_MAX); + char *result = realpath(entity->baseFileName.c_str(), absolutePath.get()); + if (result == nullptr) { + HILOGE("Failed to resolve real path, err:%{public}d", errno); + return FsResult::Error(errno); + } + + int fd = open(result, O_RDONLY); + if (fd < 0) { + HILOGE("Failed to open file, err:%{public}d", errno); + return FsResult::Error(errno); + } + + return FileInstantiator::InstantiateFile(fd, entity->baseFileName, false); +} + +static std::tuple, int32_t> ReadFileToBuffer(FILE *fp) +{ + int fd = fileno(fp); + if (fd < 0) { + HILOGE("Failed to get file descriptor, err:%{public}d", errno); + return { nullptr, UNKNOWN_ERR }; + } + + struct stat fileStat {}; + if (fstat(fd, &fileStat) < 0) { + HILOGE("Failed to get file stats, err:%{public}d", errno); + return { nullptr, errno }; + } + + long fileSize = fileStat.st_size; + if (fileSize <= 0) { + HILOGE("Invalid file size"); + return { nullptr, EIO }; + } + + auto bufferData = std::make_unique(); + bufferData->buffer = new (std::nothrow) uint8_t[fileSize]; + if (bufferData->buffer == nullptr) { + HILOGE("Failed to allocate memory"); + return { nullptr, ENOMEM }; + } + bufferData->length = fread(bufferData->buffer, sizeof(uint8_t), fileSize, fp); + if ((bufferData->length != static_cast(fileSize) && !feof(fp)) || ferror(fp)) { + HILOGE("Failed to read file, actual length is:%zu, fileSize:%ld", bufferData->length, fileSize); + delete[] bufferData->buffer; + bufferData->buffer = nullptr; + bufferData->length = 0; + return { nullptr, EIO }; + } + return { std::move(bufferData), ERRNO_NOERR }; +} + +FsResult> FsAtomicFile::ReadFully() +{ + if (entity == nullptr) { + HILOGE("Failed to get atomicFileEntity"); + return FsResult>::Error(UNKNOWN_ERR); + } + + auto absolutePath = std::make_unique(PATH_MAX); + char *result = realpath(entity->baseFileName.c_str(), absolutePath.get()); + if (result == nullptr) { + HILOGE("Failed to resolve file real path, err:%{public}d", errno); + return FsResult>::Error(errno); + } + + auto file = std::unique_ptr(std::fopen(result, "rb"), &std::fclose); + if (!file) { + HILOGE("Failed to open file, err:%{public}d", errno); + return FsResult>::Error(errno); + } + + auto [bufferData, errCode] = ReadFileToBuffer(file.get()); + if (errCode != ERRNO_NOERR) { + return FsResult>::Error(errCode); + } + return FsResult>::Success(move(bufferData)); +} + +FsResult FsAtomicFile::StartWrite() +{ + fs::path filePath = entity->newFileName; + fs::path parentPath = filePath.parent_path(); + if (access(parentPath.c_str(), F_OK) != 0) { + HILOGE("Parent directory does not exist, err:%{public}d", errno); + return FsResult::Error(ENOENT); + } + + char *tmpfile = const_cast(entity->newFileName.c_str()); + if (mkstemp(tmpfile) == -1) { + HILOGE("Fail to create tmp file err:%{public}d!", errno); + return FsResult::Error(ENOENT); + } + + return FsResult::Success(entity->newFileName); +} + +FsResult FsAtomicFile::FinishWrite() +{ + if (std::rename(entity->newFileName.c_str(), entity->baseFileName.c_str()) != 0) { + HILOGE("rename failed"); + return FsResult::Error(errno); + } + std::string tmpNewFileName = entity->baseFileName; + entity->newFileName = tmpNewFileName.append(TEMP_FILE_SUFFIX); + + return FsResult::Success(); +} + +FsResult FsAtomicFile::FailWrite() +{ + if (!fs::remove(entity->newFileName)) { + HILOGW("Failed to remove file"); + return FsResult::Error(errno); + } + std::string tmpNewFileName = entity->baseFileName; + entity->newFileName = tmpNewFileName.append(TEMP_FILE_SUFFIX); + + return FsResult::Success(); +} + +FsResult FsAtomicFile::Delete() +{ + auto rafentity = GetEntity(); + if (rafentity == nullptr) { + HILOGE("Failed to get atomicFileEntity"); + return FsResult::Error(UNKNOWN_ERR); + } + + bool errFlag = false; + std::error_code fsErrcode; + if (fs::exists(rafentity->newFileName, fsErrcode) && !fs::remove(rafentity->newFileName, fsErrcode)) { + errFlag = true; + } + if (fs::exists(rafentity->baseFileName, fsErrcode) && !fs::remove(rafentity->baseFileName, fsErrcode)) { + errFlag = true; + } + if (errFlag) { + HILOGE("Failed to remove file, err:%{public}s", fsErrcode.message().c_str()); + return FsResult::Error(fsErrcode.value()); + } + + rafentity->newFileName.clear(); + rafentity->baseFileName.clear(); + return FsResult::Success(); +} + +FsResult FsAtomicFile::Constructor(string path) +{ + auto atomicFileEntity = CreateUniquePtr(); + if (atomicFileEntity == nullptr) { + HILOGE("Failed to request heap memory"); + return FsResult::Error(ENOMEM); + } + atomicFileEntity->baseFileName = path; + atomicFileEntity->newFileName = path.append(TEMP_FILE_SUFFIX); + + auto file = new FsAtomicFile(move(atomicFileEntity)); + + return FsResult::Success(file); +} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h new file mode 100644 index 0000000000000000000000000000000000000000..80996b1c76844d90248768f25499a424dbf07352 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h @@ -0,0 +1,57 @@ +/* + * 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_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_H + +#include "fs_atomicfile_entity.h" +#include "fs_file.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +struct BufferData { + uint8_t *buffer = nullptr; + size_t length = 0; + + ~BufferData() + { + delete[] buffer; + } +}; + +class FsAtomicFile final { +public: + FsAtomicFileEntity *GetEntity(); + static FsResult Constructor(string path); + string GetPath(); + FsResult GetBaseFile(); + FsResult> ReadFully(); + FsResult StartWrite(); + FsResult FinishWrite(); + FsResult FailWrite(); + FsResult Delete(); + static void FinalizeCallback(void *finalizeData, [[maybe_unused]] void *finalizeHint); + +private: + unique_ptr entity; + explicit FsAtomicFile(unique_ptr entity) : entity(move(entity)) {} +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile_entity.h b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile_entity.h new file mode 100644 index 0000000000000000000000000000000000000000..71bcb9ee18e96c6f3b174dcc380aeae781d7a2b3 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile_entity.h @@ -0,0 +1,31 @@ +/* + * 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_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_ENTITY_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct FsAtomicFileEntity { + std::string baseFileName = ""; + std::string newFileName = ""; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_ATOMICFILE_FS_ATOMICFILE_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/access_core.cpp b/interfaces/kits/js/src/mod_fs/properties/access_core.cpp index d97f4aa80452f316e0cc85ba660509004c9e544e..d890961924c3ab71f326d91605ca1569bc184936 100644 --- a/interfaces/kits/js/src/mod_fs/properties/access_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/access_core.cpp @@ -140,7 +140,7 @@ static int HandleLocalCheck(const string &path, int mode) } #endif -static int Access(const string &path, int mode, int flag = DEFAULT_FLAG) +int AccessCore::Access(const string &path, int mode, int flag) { #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (flag == LOCAL_FLAG && IsCloudOrDistributedFilePath(path)) { diff --git a/interfaces/kits/js/src/mod_fs/properties/access_core.h b/interfaces/kits/js/src/mod_fs/properties/access_core.h index 0a6f9e1b2f7776e5b6af5db1c12b773e0d9113bb..e2a93e6ff468ade22f6256b6022331ea422dc4e8 100644 --- a/interfaces/kits/js/src/mod_fs/properties/access_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/access_core.h @@ -37,10 +37,9 @@ public: static FsResult DoAccess(const string &path, const optional &mode = nullopt); static FsResult DoAccess(const string &path, const AccessModeType &mode, const AccessFlag &flag); + static int Access(const string &path, int mode, int flag = DEFAULT_FLAG); }; -constexpr int DIR_DEFAULT_PERM = 0770; -const string PROCEDURE_ACCESS_NAME = "FileIOAccess"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS 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 1ef94cd44cc449dc0c5f4fcfab661739735504ef..f0c8692a2f52e67c139da36cb8ecf53afbc460f5 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/kits/js/src/mod_fs/properties/mkdir_core.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp index 203ead742fda8d818fec8031e45a0118a1436bd9..1af4b747ed5d792d995c33a1561e65f79467ab72 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.cpp @@ -27,6 +27,7 @@ #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) #include +#include "access_core.h" #include "bundle_mgr_proxy.h" #include "if_system_ability_manager.h" #include "ipc_skeleton.h" @@ -45,126 +46,6 @@ namespace ModuleFileIO { using namespace std; using namespace AppExecFwk; -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -const string CLOUDDISK_FILE_PREFIX = "/data/storage/el2/cloud"; -const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; -const string PACKAGE_NAME_FLAG = ""; -const string USER_ID_FLAG = ""; -const string PHYSICAL_PATH_PREFIX = "/mnt/hmdfs//account/device_view/local/data/"; -const string CLOUD_FILE_LOCATION = "user.cloud.location"; -const char POSITION_LOCAL = '1'; -const char POSITION_BOTH = '3'; -const int BASE_USER_RANGE = 200000; -#endif - -enum AccessFlag : int32_t { - DEFAULT_FLAG = -1, - LOCAL_FLAG, -}; - -struct AccessArgs { - string path; - int mode = -1; - int flag = DEFAULT_FLAG; -}; - -static int UvAccess(const string &path, int mode) -{ - std::unique_ptr access_req = { new uv_fs_t, FsUtils::FsReqCleanup }; - if (!access_req) { - HILOGE("Failed to request heap memory."); - return ENOMEM; - } - return uv_fs_access(nullptr, access_req.get(), path.c_str(), mode, nullptr); -} - -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -static bool IsCloudOrDistributedFilePath(const string &path) -{ - return path.find(CLOUDDISK_FILE_PREFIX) == 0 || path.find(DISTRIBUTED_FILE_PREFIX) == 0; -} - -static int GetCurrentUserId() -{ - int uid = IPCSkeleton::GetCallingUid(); - int userId = uid / BASE_USER_RANGE; - return userId; -} - -static sptr GetBundleMgrProxy() -{ - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - HILOGE("fail to get system ability mgr"); - return nullptr; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - HILOGE("fail to get bundle manager proxy"); - return nullptr; - } - - return iface_cast(remoteObject); -} - -static string GetSelfBundleName() -{ - sptr bundleMgrProxy = GetBundleMgrProxy(); - if (!bundleMgrProxy) { - HILOGE("bundleMgrProxy is nullptr"); - return ""; - } - BundleInfo bundleInfo; - auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo); - if (ret != 0) { - HILOGE("bundleName get fail"); - return ""; - } - return bundleInfo.name; -} - -static int HandleLocalCheck(const string &path, int mode) -{ - // check if the file of /data/storage/el2/cloud is on the local - if (path.find(CLOUDDISK_FILE_PREFIX) == 0) { - char val[2] = { '\0' }; - if (getxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), val, sizeof(val)) < 0) { - HILOGI("get cloud file location fail, err: %{public}d", errno); - return errno; - } - if (val[0] == POSITION_LOCAL || val[0] == POSITION_BOTH) { - return 0; - } - return ENOENT; - } - // check if the distributed file of /data/storage/el2/distributedfiles is on the local, - // convert into physical path(/mnt/hmdfs//account/device_view/local/data/) and check - if (path.find(DISTRIBUTED_FILE_PREFIX) == 0) { - int userId = GetCurrentUserId(); - string bundleName = GetSelfBundleName(); - string relativePath = path.substr(DISTRIBUTED_FILE_PREFIX.length()); - string physicalPath = PHYSICAL_PATH_PREFIX + relativePath; - physicalPath.replace(physicalPath.find(USER_ID_FLAG), USER_ID_FLAG.length(), to_string(userId)); - physicalPath.replace(physicalPath.find(PACKAGE_NAME_FLAG), PACKAGE_NAME_FLAG.length(), bundleName); - - return UvAccess(physicalPath, mode); - } - - return ENOENT; -} -#endif - -static int AccessCore(const string &path, int mode, int flag = DEFAULT_FLAG) -{ -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - if (flag == LOCAL_FLAG && IsCloudOrDistributedFilePath(path)) { - return HandleLocalCheck(path, mode); - } -#endif - return UvAccess(path, mode); -} - static int MkdirCore(const string &path) { std::unique_ptr mkdir_req = { new uv_fs_t, FsUtils::FsReqCleanup }; @@ -179,7 +60,7 @@ static int32_t MkdirExec(const string &path, bool recursion, bool hasOption) { #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (hasOption) { - int ret = AccessCore(path, 0); + int ret = AccessCore::Access(path, 0); if (ret == ERRNO_NOERR) { HILOGD("The path already exists"); return EEXIST; @@ -192,7 +73,7 @@ static int32_t MkdirExec(const string &path, bool recursion, bool hasOption) HILOGD("Failed to create directories, error: %{public}d", errno); return errno; } - ret = AccessCore(path, 0); + ret = AccessCore::Access(path, 0); if (ret) { HILOGE("Failed to verify the result of Mkdirs function"); return ret; diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h index cc6793693dca3c355419aae57de9ccaaf7749d9a..7b92bbde0e12e6a2538311e04f44018e78cecfa7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/mkdir_core.h @@ -28,7 +28,6 @@ public: static FsResult DoMkdir(const std::string& path, std::optional recursion = std::nullopt); }; constexpr int DIR_DEFAULT_PERM = 0770; -const std::string PROCEDURE_READTEXT_NAME = "FileIOMkdir"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp index 819215c30e9be4c365a6315fc8e078ade722960e..1f986915199031de14d6eedf10ebb6115003e9c6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp @@ -25,9 +25,9 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; -static int CheckOptionArg(optional option) +static int CheckOptionArg(Options option) { - auto encoding = option->encoding; + auto encoding = option.encoding; if (encoding != "utf-8") { return EINVAL; } @@ -79,7 +79,7 @@ static FsResult InstantiateReaderIterator(void *iterator, in FsResult ReadLinesCore::DoReadLines(const string &path, optional option) { if (option.has_value()) { - int ret = CheckOptionArg(option); + int ret = CheckOptionArg(option.value()); if (ret) { HILOGE("Invalid option.encoding parameter"); return FsResult::Error(ret); @@ -98,11 +98,7 @@ FsResult ReadLinesCore::DoReadLines(const string &path, opti HILOGE("Failed to get size of the file"); return FsResult::Error(ret); } - auto readeriterator = InstantiateReaderIterator(iterator, offset); - if (!readeriterator.IsSuccess()) { - return FsResult::Error(ENOMEM); - } - return FsResult::Success(readeriterator.GetData().value()); + return InstantiateReaderIterator(iterator, offset); } } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb263f86b72ed9418d085f89ade25f70f08..17315cb07e23f5428635e2d494ed42db2017163a 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -16,8 +16,13 @@ group("file_api_unittest") { deps = [ "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", + "js:ani_file_environment", "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", + "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 e4e6a9d52bb0398a1fa2cc095b3c3d8f1c9e0f3d..ab199b047bffded78b1ed6616af56c25417508cf 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -14,27 +14,164 @@ import("//build/test.gni") import("//foundation/filemanagement/file_api/file_api.gni") +ohos_unittest("ani_file_environment") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + include_dirs = [ + "${src_path}/mod_environment", + "${src_path}/mod_environment/ani", + ] + + sources = [ + "mod_environment/environment_core_mock_test.cpp", + "mod_environment/assistant.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_environment", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + "ipc:ipc_core", + "init:libbegetutil", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:libarkruntime", + ] + + defines = [ + "private=public", + ] +} + ohos_unittest("ani_file_fs_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/class_tasksignal", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", - "mock/uv_fs_mock.h", ] sources = [ - "mod_fs/properties/mock/uv_fs_mock.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_readeriterator/fs_reader_iterator_test.cpp", + "mod_fs/class_stat/fs_stat_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/copy_core_test.cpp", + "mod_fs/properties/copy_file_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/movedir_core_test.cpp", + "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/read_lines_core_test.cpp", + "mod_fs/properties/read_text_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", + "mod_fs/properties/xattr_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_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_atomicfile/fs_atomicfile_mock_test.cpp", + "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp", + "mod_fs/class_stat/fs_stat_mock_test.cpp", + "mod_fs/properties/access_core_mock_test.cpp", + "mod_fs/properties/copy_dir_core_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/copy_file_core_mock_test.cpp", + "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/fdatasync_core_mock_test.cpp", + "mod_fs/properties/fsync_core_mock_test.cpp", + "mod_fs/properties/mkdir_core_mock_test.cpp", + "mod_fs/properties/mkdtemp_core_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/move_core_mock_test.cpp", + "mod_fs/properties/open_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/read_lines_core_mock_test.cpp", + "mod_fs/properties/rename_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/truncate_core_mock_test.cpp", + "mod_fs/properties/unlink_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", + "mod_fs/properties/write_core_mock_test.cpp", + "mod_fs/properties/xattr_core_mock_test.cpp", ] deps = [ @@ -43,35 +180,108 @@ 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", ] +} + +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", + ] - cflags_cc = [ "-DENABLE_NAPI_MOCK" ] + 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", + ] +} + +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_environment/assistant.cpp b/interfaces/test/unittest/js/mod_environment/assistant.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9537f1df6cdcee4e1ec1127a7d5a765c7124179d --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/assistant.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 "assistant.h" + + +bool IsSystemAppByFullTokenID(uint64_t tokenId) +{ + return OHOS::FileManagement::Backup::BAccessTokenKit::token->IsSystemAppByFullTokenID(tokenId); +} + +int GetParameter(const char *key, const char *def, char *value, uint32_t len) +{ + return OHOS::FileManagement::Backup::BAccessTokenKit::token->GetParameter(key, def, value, len); +} + +int VerifyAccessToken(unsigned int tokenID, const std::string& permissionName) +{ + return OHOS::FileManagement::Backup::BAccessTokenKit::token->VerifyAccessToken(tokenID, permissionName); +} diff --git a/interfaces/test/unittest/js/mod_environment/assistant.h b/interfaces/test/unittest/js/mod_environment/assistant.h new file mode 100644 index 0000000000000000000000000000000000000000..3abb78928f1cfff1b43da06c50ffb0ae5644e799 --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/assistant.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_ASSISTANT_H +#define OHOS_FILEMGMT_BACKUP_ASSISTANT_H + +#include + +#include "accesstoken_kit.h" +#include "tokenid_kit.h" + +namespace OHOS::FileManagement::Backup { +class BAccessTokenKit { +public: + virtual bool IsSystemAppByFullTokenID(uint64_t) = 0; + virtual int GetParameter(const char *key, const char *def, char *value, uint32_t len) = 0; + virtual int VerifyAccessToken(unsigned int tokenID, const std::string& permissionName) = 0; +public: + BAccessTokenKit() = default; + virtual ~BAccessTokenKit() = default; +public: + static inline std::shared_ptr token = nullptr; +}; + +class AccessTokenKitMock : public BAccessTokenKit { +public: + MOCK_METHOD(bool, IsSystemAppByFullTokenID, (uint64_t)); + MOCK_METHOD(int, GetParameter, (const char *key, const char *def, char *value, uint32_t len)); + MOCK_METHOD(int, VerifyAccessToken, (unsigned int tokenID, const std::string& permissionName)); +}; +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_BACKUP_ASSISTANT_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a29f187960d2375b1b2ab3e5eec3caaa32f51bf --- /dev/null +++ b/interfaces/test/unittest/js/mod_environment/environment_core_mock_test.cpp @@ -0,0 +1,354 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "environment_core.h" +#include "assistant.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class EnvironmentCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr accessToken = nullptr; +}; + +void EnvironmentCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + accessToken = std::make_shared(); + Backup::BAccessTokenKit::token = accessToken; +} + +void EnvironmentCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Backup::BAccessTokenKit::token = nullptr; + accessToken = nullptr; +} + +void EnvironmentCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void EnvironmentCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetStorageDataDir_001 + * @tc.desc: Test function of DoGetStorageDataDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetStorageDataDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetStorageDataDir_001"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)) + .WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetStorageDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetStorageDataDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetStorageDataDir_002 + * @tc.desc: Test function of DoGetStorageDataDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetStorageDataDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetStorageDataDir_002"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)) + .WillOnce(Return(true)); + + auto res = ModuleEnvironment::DoGetStorageDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetStorageDataDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDataDir_001 + * @tc.desc: Test function of DoGetUserDataDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDataDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDataDir_001"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetUserDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDataDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDataDir_002 + * @tc.desc: Test function of DoGetUserDataDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDataDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDataDir_002"; + + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)) + .WillOnce(Return(true)); + + auto res = ModuleEnvironment::DoGetUserDataDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDataDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDownloadDir_001 + * @tc.desc: Test function of DoGetUserDownloadDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDownloadDir_001"; + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + + auto res = ModuleEnvironment::DoGetUserDownloadDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDownloadDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDownloadDir_002 + * @tc.desc: Test function of DoGetUserDownloadDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDownloadDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDownloadDir_002"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)) + .WillOnce(Return(-1)); + + auto res = ModuleEnvironment::DoGetUserDownloadDir(); + + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDownloadDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDocumentDir_001 + * @tc.desc: Test function of DoGetUserDocumentDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDocumentDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDocumentDir_001"; + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + + auto res = ModuleEnvironment::DoGetUserDocumentDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDocumentDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserDocumentDir_002 + * @tc.desc: Test function of DoGetUserDocumentDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserDocumentDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserDocumentDir_002"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)) + .WillOnce(Return(-1)); + + auto res = ModuleEnvironment::DoGetUserDocumentDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserDocumentDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_001 + * @tc.desc: Test function of DoGetExternalStorageDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_001"; + + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(Return(-1)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_002 + * @tc.desc: Test function of DoGetExternalStorageDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_002"; + + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(false)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetExternalStorageDir_003 + * @tc.desc: Test function of DoGetExternalStorageDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetExternalStorageDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetExternalStorageDir_003"; + + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetExternalStorageDir_003"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserHomeDir_001 + * @tc.desc: Test function of DoGetUserHomeDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserHomeDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserHomeDir_001"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(Return(1)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserHomeDir_001"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserHomeDir_002 + * @tc.desc: Test function of DoGetUserHomeDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserHomeDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserHomeDir_002"; + + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + EXPECT_CALL(*accessToken, VerifyAccessToken(_, _)).WillOnce(Return(1)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserHomeDir_002"; +} + +/** + * @tc.name: EnvironmentCoreTest_DoGetUserHomeDir_003 + * @tc.desc: Test function of DoGetUserHomeDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(EnvironmentCoreTest, EnvironmentCoreTest_DoGetUserHomeDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "EnvironmentCoreTest-begin EnvironmentCoreTest_DoGetUserHomeDir_003"; + + char key[] = "persist.sys.allow_download"; + + EXPECT_CALL(*accessToken, GetParameter(_, _, _, _)).WillOnce(DoAll(SetArgPointee<2>(*key), Return(1))); + EXPECT_CALL(*accessToken, IsSystemAppByFullTokenID(_)).WillOnce(Return(true)); + EXPECT_CALL(*accessToken, VerifyAccessToken(_, _)).WillOnce(Return(0)); + + auto res = ModuleEnvironment::DoGetExternalStorageDir(); + EXPECT_FALSE(res.IsSuccess()); + + GTEST_LOG_(INFO) << "EnvironmentCoreTest-end EnvironmentCoreTest_DoGetUserHomeDir_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..c8305436192499a974e1491ba64065b872f97653 --- /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_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 0000000000000000000000000000000000000000..ffe9a7e674156b28ffb1cdaa444557a1d38d933d --- /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 0000000000000000000000000000000000000000..f73a0c1857051cdf86f38fc94b94c08be8be69c0 --- /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/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 0000000000000000000000000000000000000000..f691f3a42ed3fd8ee4bd56f0ce6eb1418dcb669b --- /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) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void FsRandomAccessFileMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +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 0000000000000000000000000000000000000000..a863fee9c520fb3e7b0e36451043511264374de2 --- /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/class_readeriterator/fs_reader_iterator_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e97190c90fc8d022fd433ef2bbaf028ac83d416 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_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_reader_iterator.h" +#include "read_lines_core.h" +#include "../properties/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 FsReaderIteratorMockTest : public testing::Test { +public: + static std::filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path FsReaderIteratorMockTest::tempFilePath; + +void FsReaderIteratorMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = std::filesystem::temp_directory_path() / "test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456\n"; + tempfile.close(); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void FsReaderIteratorMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void FsReaderIteratorMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsReaderIteratorMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsReaderIteratorMockTest_Next_001 + * @tc.desc: Test FsReaderIterator::Next for success case + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsReaderIteratorMockTest, FsReaderIteratorMockTest_Next_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-begin FsReaderIteratorMockTest_Next_001"; + + std::string path = tempFilePath.string(); + std::optional option = std::nullopt; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + + auto result = ReadLinesCore::DoReadLines(path, option); + EXPECT_TRUE(result.IsSuccess()); + + auto iterator = result.GetData().value(); + ASSERT_NE(iterator, nullptr); + + auto nextResult = iterator->Next(); + EXPECT_TRUE(nextResult.IsSuccess()); + EXPECT_TRUE(nextResult.GetData().value().done); + EXPECT_EQ(nextResult.GetData().value().value, "Test content\n"); + + nextResult = iterator->Next(); + EXPECT_TRUE(nextResult.IsSuccess()); + EXPECT_FALSE(nextResult.GetData().value().done); + EXPECT_EQ(nextResult.GetData().value().value, "123\n"); + + nextResult = iterator->Next(); + EXPECT_TRUE(nextResult.IsSuccess()); + EXPECT_FALSE(nextResult.GetData().value().done); + EXPECT_EQ(nextResult.GetData().value().value, "456\n"); + + nextResult = iterator->Next(); + EXPECT_FALSE(nextResult.IsSuccess()); + + GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-end FsReaderIteratorMockTest_Next_001"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_test.cpp b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54fe8d44d5733f5d47c95302a59901378ced8b89 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_readeriterator/fs_reader_iterator_test.cpp @@ -0,0 +1,80 @@ +/* + * 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_reader_iterator.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsReaderIteratorTest : public testing::Test { +public: + static std::filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path FsReaderIteratorTest::tempFilePath; + +void FsReaderIteratorTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = std::filesystem::temp_directory_path() / "test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456"; + tempfile.close(); +} + +void FsReaderIteratorTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); +} + +void FsReaderIteratorTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsReaderIteratorTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsReaderIteratorTest_Constructor_001 + * @tc.desc: Test FsReaderIterator::Constructor for success case + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsReaderIteratorTest, FsReaderIteratorTest_Constructor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsReaderIteratorTest-begin FsReaderIteratorTest_Constructor_001"; + + auto result = FsReaderIterator::Constructor(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsReaderIteratorTest-end FsReaderIteratorTest_Constructor_001"; +} + +} \ 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 0000000000000000000000000000000000000000..b6e8746581503d4b39410e812572bb1d1b36f2e0 --- /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 0000000000000000000000000000000000000000..15a8640f2751309fcf229bf2077e668dbc550b74 --- /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/class_stream/fs_stream_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38be45ba53f36590a5c03ba17dd387ae09e065f2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp @@ -0,0 +1,818 @@ +/* + * 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 "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 FsStreamTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: FsStreamCloseTest_0001 + * @tc.desc: Test function of Close() interface for close success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamCloseTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamCloseTest_0001"; +} + +/** + * @tc.name: FsStreamCloseTest_0002 + * @tc.desc: Test function of Close() interface for close fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamCloseTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + retCs = result->Close(); + EXPECT_FALSE(retCs.IsSuccess()); + auto err = retCs.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamCloseTest_0002"; +} + +/** + * @tc.name: FsStreamFlushTest_0001 + * @tc.desc: Test function of Flush() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamFlushTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retFl = result->Flush(); + EXPECT_FALSE(retFl.IsSuccess()); + auto err = retFl.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamFlushTest_0001"; +} + +/** + * @tc.name: FsStreamFlushTest_0002 + * @tc.desc: Test function of Flush() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamFlushTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retFl = result->Flush(); + ASSERT_TRUE(retFl.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamFlushTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, 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(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retSk = result->Seek(0); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for error whence < SEEK_SET (0). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, 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(); + + optional opt = -1; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0003 + * @tc.desc: Test function of Seek() interface for error whence > SEEK_END (2). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + optional opt = 3; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0003"; +} + +/** + * @tc.name: FsStreamSeekTest_0004 + * @tc.desc: Test function of Seek() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retSk = result->Seek(1); + ASSERT_TRUE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, 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(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retWr = result->Write("FsStreamWriteTest_0001"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, 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(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + string buf = "FsStreamWriteTest_0001"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0003 + * @tc.desc: Test function of Write() interface for string error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + auto retWr = result->Write("FsStreamWriteTest_0003", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0003"; +} + +/** + * @tc.name: FsStreamWriteTest_0004 + * @tc.desc: Test function of Write() interface for ArrayBuffer error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + string buf = "FsStreamWriteTest_0004"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0005 + * @tc.desc: Test function of Write() interface for string error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + auto retWr = result->Write("FsStreamWriteTest_0005", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0006 + * @tc.desc: Test function of Write() interface for ArrayBuffer error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + string buf = "FsStreamWriteTest_0006"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0004"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamWriteTest_0007 + * @tc.desc: Test function of Write() interface for string > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0007"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(largeString); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0008 + * @tc.desc: Test function of Write() interface for ArrayBuffer > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0008"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(ArrayBuffer(static_cast(largeString.data()), largeLength)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0008"; +} +#endif + +/** + * @tc.name: FsStreamWriteTest_0009 + * @tc.desc: Test function of Write() interface for string length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0009"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + auto retWr = result->Write("FsStreamWriteTest_0009", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0009"; +} + +/** + * @tc.name: FsStreamWriteTest_0010 + * @tc.desc: Test function of Write() interface for ArrayBuffer length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0010"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + string buf = "FsStreamWriteTest_0010"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0010"; +} + +/** + * @tc.name: FsStreamWriteTest_0013 + * @tc.desc: Test function of Write() interface for string no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0013"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retWr = result->Write("FsStreamWriteTest_0013"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0013"; +} + +/** + * @tc.name: FsStreamWriteTest_0014 + * @tc.desc: Test function of Write() interface for ArrayBuffer no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0014"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + string buf = "FsStreamWriteTest_0014"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0014"; +} + +/** + * @tc.name: FsStreamWriteTest_0015 + * @tc.desc: Test function of Write() interface for string success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0015"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + auto retWr = result->Write("FsStreamWriteTest_0015", opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0015"; +} + +/** + * @tc.name: FsStreamWriteTest_0016 + * @tc.desc: Test function of Write() interface for ArrayBuffer success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0016"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + string buf = "FsStreamWriteTest_0016"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0016"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for single argument null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, 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(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0001"; +} + +/** + * @tc.name: FsStreamReadTest_0002 + * @tc.desc: Test function of Read() interface for error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0002"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamReadTest_0003 + * @tc.desc: Test function of Read() interface for bufLen > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + + void *buffer = std::malloc(largeLength); + ArrayBuffer arrayBuffer(buffer, largeLength); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0003"; +} +#endif + +/** + * @tc.name: FsStreamReadTest_0004 + * @tc.desc: Test function of Read() interface for error length. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.length = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0004"; +} + +/** + * @tc.name: FsStreamReadTest_0005 + * @tc.desc: Test function of Read() interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0005"; +} + +/** + * @tc.name: FsStreamReadTest_0006 + * @tc.desc: Test function of Read() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = 0; + opt.length = 5; + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + auto retRd = result->Read(arrayBuffer, opt); + ASSERT_TRUE(retRd.IsSuccess()); + + size_t retLen = retRd.GetData().value(); + EXPECT_EQ(retLen, 0); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0006"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file 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 0000000000000000000000000000000000000000..288cf690424dc9bd22de83687ba01dbb0b2defd7 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -0,0 +1,150 @@ +/* + * 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 "mock/uv_fs_mock.h" + +#include +#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; +}; + +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"; +} + + +} // 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 913df19e9f30cf248cb1775e6d0ec0a18f930101..8359ed25b3efc0fe54a938e3000032c819810644 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,7 @@ #include "access_core.h" #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -54,7 +54,7 @@ void AccessCoreTest::TearDown(void) /** * @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 @@ -74,7 +74,7 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Lev /** * @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 @@ -83,18 +83,19 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_002, testing::ext::TestSize.Lev { GTEST_LOG_(INFO) << "NClassTest-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"; } /** * @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 @@ -103,36 +104,13 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Lev { GTEST_LOG_(INFO) << "NClassTest-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"; } -/** - * @tc.name: AccessCoreTest_DoAccess_004 - * @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_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_004"; - - std::string path = "TEST"; - AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = DEFAULT_FLAG; - - auto res = AccessCore::DoAccess(path, mode, flag); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_004"; -} - - } // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/close_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/close_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9af77027e33a739192c29ee2515cd097c34b7e68 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/close_core_test.cpp @@ -0,0 +1,171 @@ +/* +* 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 "close_core.h" +#include "open_core.h" + +#define FILE_PATH "/data/test/CloseCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class CloseCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) { + int32_t fd = open(FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** +* @tc.name: DoCloseTestFd_0001 +* @tc.desc: Test function of DoClose() interface for invalid arguments. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(CloseCoreTest, DoCloseTestFd_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CloseCoreTest-begin DoCloseTestFd_0001"; + auto ret = CloseCore::DoClose(-1); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + + GTEST_LOG_(INFO) << "CloseCoreTest-end DoCloseTestFd_0001"; +} + +// /** +// * @tc.name: DoCloseTestFd_0002 +// * @tc.desc: Test function of DoClose() interface for sucess. +// * @tc.size: MEDIUM +// * @tc.type: FUNC +// * @tc.level Level 1 +// * @tc.require: AR000IGDNF +// */ +// HWTEST_F(CloseCoreTest, DoCloseTestFd_0002, testing::ext::TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "CloseCoreTest-begin DoCloseTestFd_0002"; +// int32_t fd = open(FILE_PATH, O_RDWR); +// if (fd <= 0) { +// close(fd); +// ASSERT_TRUE(false); +// } + +// auto ret = CloseCore::DoClose(fd); +// EXPECT_TRUE(ret.IsSuccess()); + +// int32_t fdEnd = open(FILE_PATH, O_RDWR); +// if (fdEnd <= 0) { +// close(fdEnd); +// ASSERT_TRUE(false); +// } +// EXPECT_EQ(fdEnd, fd); + +// ret = CloseCore::DoClose(fd); +// EXPECT_TRUE(ret.IsSuccess()); + +// GTEST_LOG_(INFO) << "CloseCoreTest-end DoCloseTestFd_0002"; +// } + +// /** +// * @tc.name: DoCloseTestFd_0003 +// * @tc.desc: Test function of DoClose() interface for failed. +// * @tc.size: MEDIUM +// * @tc.type: FUNC +// * @tc.level Level 1 +// * @tc.require: AR000IGDNF +// */ +// HWTEST_F(CloseCoreTest, DoCloseTestFd_0003, testing::ext::TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "CloseCoreTest-begin DoCloseTestFd_0003"; +// int32_t fd = open(FILE_PATH, O_RDWR); +// if (fd <= 0) { +// close(fd); +// ASSERT_TRUE(false); +// } +// auto ret = CloseCore::DoClose(fd); +// EXPECT_TRUE(ret.IsSuccess()); + +// ret = CloseCore::DoClose(fd); +// EXPECT_FALSE(ret.IsSuccess()); +// auto err = ret.GetError(); +// EXPECT_EQ(err.GetErrNo(), 13900008); + +// GTEST_LOG_(INFO) << "CloseCoreTest-end DoCloseTestFd_0003"; +// } + +/** +* @tc.name: DoCloseTestFile_0001 +* @tc.desc: Test function of DoClose() interface for success. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(CloseCoreTest, DoCloseTestFile_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CloseCoreTest-begin DoCloseTestFile_0001"; + auto fileRes = OpenCore::DoOpen(FILE_PATH); + if (!fileRes.IsSuccess()) { + ASSERT_TRUE(false); + } + FsFile *file = fileRes.GetData().value(); + auto ret = CloseCore::DoClose(file); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "CloseCoreTest-end DoCloseTestFile_0001"; +} + +/** +* @tc.name: DoCloseTestFile_0002 +* @tc.desc: Test function of DoClose() interface for failed get fd. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(CloseCoreTest, DoCloseTestFile_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CloseCoreTest-begin DoCloseTestFile_0002"; + auto fileRes = OpenCore::DoOpen(FILE_PATH); + if (!fileRes.IsSuccess()) { + ASSERT_TRUE(false); + } + FsFile *file = fileRes.GetData().value(); + auto ret = CloseCore::DoClose(file); + EXPECT_TRUE(ret.IsSuccess()); + + ret = CloseCore::DoClose(file); + EXPECT_FALSE(ret.IsSuccess()); + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "CloseCoreTest-end DoCloseTestFile_0002"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26884e8dd6a84fb680d403379e0e0207642bc81b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp @@ -0,0 +1,673 @@ +/* + * 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 "copy_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CopyCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +void Func() +{ + sleep(1); +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_001 + * @tc.desc: Test function of CopyCoreTest::UnregisterListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_001"; + + std::shared_ptr fileInfos = nullptr; + + CopyCore::UnregisterListener(fileInfos); + EXPECT_EQ(fileInfos, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_001"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_002 + * @tc.desc: Test function of CopyCoreTest::UnregisterListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_002"; + + std::shared_ptr fileInfos = std::make_shared(); + + CopyCore::UnregisterListener(fileInfos); + EXPECT_EQ(CopyCore::jsCbMap_.find(*fileInfos), CopyCore::jsCbMap_.end()); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_002"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_003 + * @tc.desc: Test function of CopyCoreTest::UnregisterListener interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_003"; + + std::shared_ptr fileInfos = std::make_shared(); + CopyCore::jsCbMap_[*fileInfos] = nullptr; + + CopyCore::UnregisterListener(fileInfos); + EXPECT_EQ(CopyCore::jsCbMap_.find(*fileInfos), CopyCore::jsCbMap_.end()); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_003"; +} + +/** + * @tc.name: CopyCoreTest_ValidOperand_001 + * @tc.desc: Test function of CopyCoreTest::ValidOperand interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidOperand_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidOperand_001"; + + std::string uriStr = "file://"; + + bool res = CopyCore::ValidOperand(uriStr); + EXPECT_TRUE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidOperand_001"; +} + +/** + * @tc.name: CopyCoreTest_ValidOperand_002 + * @tc.desc: Test function of CopyCoreTest::ValidOperand interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidOperand_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidOperand_002"; + + std::string uriStr = "TEST"; + + bool res = CopyCore::ValidOperand(uriStr); + EXPECT_FALSE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidOperand_002"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_001 + * @tc.desc: Test function of CopyCoreTest::CheckOrCreatePath interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_001"; + + std::string destPath = "file"; + + bool res = CopyCore::CheckOrCreatePath(destPath); + EXPECT_EQ(res, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_001"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_002 + * @tc.desc: Test function of CopyCoreTest::CheckOrCreatePath interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_002"; + + std::string destPath = "file"; + auto file = open(destPath.c_str(), O_RDWR); + if (file >= 0) { + bool res = CopyCore::CheckOrCreatePath(destPath); + EXPECT_EQ(res, 0); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_002"; +} + +/** + * @tc.name: CopyCoreTest_ValidParam_001 + * @tc.desc: Test function of CopyCoreTest::ValidParam interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParam_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParam_001"; + + string src = "test"; + string dest; + std::optional options = std::nullopt; + std::shared_ptr fileInfos = nullptr; + + int res = CopyCore::ValidParam(src, dest, options, fileInfos); + EXPECT_EQ(res, CommonErrCode::E_PARAMS); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParam_001"; +} + +/** + * @tc.name: CopyCoreTest_ValidParam_002 + * @tc.desc: Test function of CopyCoreTest::ValidParam interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParam_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParam_002"; + + string src = "file://"; + string dest = "file://"; + auto fuc = [] (uint64_t progressSize, uint64_t totalSize) {}; + CopyOptions copyOptions; + copyOptions.listenerCb = fuc; + copyOptions.taskSignalEntityCore = std::make_shared(); + std::optional options = + std::make_optional(copyOptions); + std::shared_ptr fileInfos = nullptr; + + auto res = CopyCore::ValidParam(src, dest, options, fileInfos); + EXPECT_EQ(res, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParam_002"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_001 + * @tc.desc: Test function of CopyCoreTest::ExecLocal interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_001"; + + std::shared_ptr infos = std::make_shared(); + infos->isFile = true; + infos->srcPath = "test"; + infos->destPath = "test"; + std::shared_ptr callback; + + int res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, EINVAL); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_001"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_002 + * @tc.desc: Test function of CopyCoreTest::ExecLocal interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_002"; + + std::shared_ptr infos = std::make_shared(); + infos->isFile = true; + infos->srcPath = "srcPath"; + infos->destPath = "destPath"; + infos->hasListener = false; + std::shared_ptr callback; + + int res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, 2); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_002"; +} + +/** + * @tc.name: CopyCoreTest_GetUVEntry_001 + * @tc.desc: Test function of CopyCoreTest::GetUVEntry interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetUVEntry_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetUVEntry_001"; + + std::shared_ptr infos = std::make_shared(); + CopyCore::jsCbMap_[*infos] = std::make_shared(nullptr); + + auto res = CopyCore::GetUVEntry(infos); + EXPECT_NE(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetUVEntry_001"; +} + +/** + * @tc.name: CopyCoreTest_GetUVEntry_002 + * @tc.desc: Test function of CopyCoreTest::GetUVEntry interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetUVEntry_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetUVEntry_002"; + + std::shared_ptr infos = std::make_shared(); + + auto res = CopyCore::GetUVEntry(infos); + EXPECT_NE(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetUVEntry_002"; +} + +/** + * @tc.name: CopyCoreTest_ReceiveComplete_001 + * @tc.desc: Test function of CopyCoreTest::ReceiveComplete interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ReceiveComplete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ReceiveComplete_001"; + + std::shared_ptr entry = nullptr; + + CopyCore::ReceiveComplete(entry); + EXPECT_EQ(entry, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ReceiveComplete_001"; +} + +/** + * @tc.name: CopyCoreTest_ReceiveComplete_002 + * @tc.desc: Test function of CopyCoreTest::ReceiveComplete interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ReceiveComplete_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ReceiveComplete_002"; + + auto fuc = [] (uint64_t progressSize, uint64_t totalSize) {}; + auto ptr = std::make_shared(fuc); + ptr->maxProgressSize = 1; + std::shared_ptr entry = std::make_shared(ptr, nullptr); + + CopyCore::ReceiveComplete(entry); + EXPECT_NE(entry, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ReceiveComplete_002"; +} + +/** + * @tc.name: CopyCoreTest_ReceiveComplete_003 + * @tc.desc: Test function of CopyCoreTest::ReceiveComplete interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ReceiveComplete_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ReceiveComplete_003"; + + auto fuc = [] (uint64_t progressSize, uint64_t totalSize) {}; + auto ptr = std::make_shared(fuc); + std::shared_ptr entry = std::make_shared(ptr, nullptr); + + CopyCore::ReceiveComplete(entry); + EXPECT_NE(entry, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ReceiveComplete_003"; +} + +/** + * @tc.name: CopyCoreTest_WaitNotifyFinished_001 + * @tc.desc: Test function of CopyCoreTest::WaitNotifyFinished interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_WaitNotifyFinished_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_WaitNotifyFinished_001"; + + std::shared_ptr callback = std::make_shared(nullptr); + callback->notifyHandler = std::thread(Func); + + CopyCore::WaitNotifyFinished(callback); + EXPECT_NE(callback, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_WaitNotifyFinished_001"; +} + +/** + * @tc.name: CopyCoreTest_GetReceivedInfo_001 + * @tc.desc: Test function of CopyCoreTest::GetReceivedInfo interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetReceivedInfo_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetReceivedInfo_001"; + + int wd = 0; + std::shared_ptr callback = std::make_shared(nullptr); + + CopyCore::GetReceivedInfo(wd, callback); + EXPECT_NE(callback, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetReceivedInfo_001"; +} + +/** + * @tc.name: CopyCoreTest_GetReceivedInfo_002 + * @tc.desc: Test function of CopyCoreTest::GetReceivedInfo interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetReceivedInfo_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetReceivedInfo_002"; + + int wd = 1; + std::shared_ptr callback = std::make_shared(nullptr); + callback->wds.push_back(std::make_pair(wd, std::make_shared())); + + CopyCore::GetReceivedInfo(wd, callback); + EXPECT_NE(callback, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetReceivedInfo_002"; +} + +/** + * @tc.name: CopyCoreTest_GetRealPath_001 + * @tc.desc: Test function of CopyCoreTest::GetRealPath interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRealPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRealPath_001"; + + std::string path = "./test1/../test2"; + + auto res = CopyCore::GetRealPath(path); + EXPECT_EQ(res, "test2"); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRealPath_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRegisteredListener_001 + * @tc.desc: Test function of CopyCoreTest::GetRegisteredListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRegisteredListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRegisteredListener_001"; + + std::shared_ptr infos = std::make_shared(); + + auto res = CopyCore::GetRegisteredListener(infos); + EXPECT_NE(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRegisteredListener_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRegisteredListener_002 + * @tc.desc: Test function of CopyCoreTest::GetRegisteredListener interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRegisteredListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRegisteredListener_002"; + + std::shared_ptr infos = std::make_shared(); + CopyCore::jsCbMap_[*infos] = std::make_shared(nullptr); + + auto res = CopyCore::GetRegisteredListener(infos); + EXPECT_NE(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRegisteredListener_002"; +} + +/** + * @tc.name: CopyCoreTest_CloseNotifyFdLocked_001 + * @tc.desc: Test function of CopyCoreTest::CloseNotifyFdLocked interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CloseNotifyFdLocked_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CloseNotifyFdLocked_001"; + + std::shared_ptr infos = std::make_shared(); + std::shared_ptr callback = std::make_shared(nullptr); + callback->reading = false; + + CopyCore::CloseNotifyFdLocked(infos, callback); + EXPECT_FALSE(callback->reading); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CloseNotifyFdLocked_001"; +} + +/** + * @tc.name: CopyCoreTest_CloseNotifyFdLocked_002 + * @tc.desc: Test function of CopyCoreTest::CloseNotifyFdLocked interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CloseNotifyFdLocked_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CloseNotifyFdLocked_002"; + + std::shared_ptr infos; + std::shared_ptr callback = std::make_shared(nullptr); + callback->reading = true; + + CopyCore::CloseNotifyFdLocked(infos, callback); + EXPECT_TRUE(callback->reading); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CloseNotifyFdLocked_002"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_001 + * @tc.desc: Test function of CopyCoreTest::MakeDir interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_001"; + + std::filesystem::path testDir = std::filesystem::temp_directory_path() / "copy_core_test"; + std::filesystem::remove_all(testDir); + std::filesystem::create_directory(testDir); + + string path = testDir.string(); + + auto res = CopyCore::MakeDir(path); + EXPECT_EQ(res, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_001"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_002 + * @tc.desc: Test function of CopyCoreTest::MakeDir interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_002"; + + string path = "/test"; + + auto res = CopyCore::MakeDir(path); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_002"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_001 + * @tc.desc: Test function of CopyCoreTest::IsDirectory interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_001"; + + string path = "non_existent_path"; + + auto res = CopyCore::IsDirectory(path); + EXPECT_FALSE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_001"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_002 + * @tc.desc: Test function of CopyCoreTest::IsDirectory interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_002"; + + string path = "test_dir"; + mkdir("test_dir", 0755); + + auto res = CopyCore::IsDirectory(path); + EXPECT_TRUE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_002"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_001 + * @tc.desc: Test function of CopyCoreTest::IsFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_001"; + + string path = "test_file"; + creat("test_file", 0644); + + auto res = CopyCore::IsFile(path); + EXPECT_TRUE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_001"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_002 + * @tc.desc: Test function of CopyCoreTest::IsFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_002"; + + string path = "test_file"; + + auto res = CopyCore::IsFile(path); + EXPECT_TRUE(res); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_002"; +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bb3a7f6e515fe16ae705b9ab91d06ae6bd59780 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_mock_test.cpp @@ -0,0 +1,308 @@ +/* + * 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 "copy_dir_core.h" +#include "mock/system_mock.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyDirCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr sys = nullptr; +}; + +void CopyDirCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + sys = std::make_shared(); + System::ins = sys; +} + +void CopyDirCoreMockTest::TearDownTestCase(void) +{ + System::ins = nullptr; + sys = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyDirCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyDirCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_001 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_001"; + + string src = "/src/dir"; + string dest = "/dest"; + int32_t mode = 0; + error_code errCode; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, ParentPath(src)).WillOnce(Return(filesystem::path("/src"))); + EXPECT_CALL(*sys, Exists(dest + "/dir", errCode)).WillOnce(Return(false)); + EXPECT_CALL(*sys, CreateDirectory(filesystem::path(dest + "/dir"), errCode)).WillOnce(Return(true)); + EXPECT_CALL(*sys, Scandir(src.c_str(), _, _, _)).WillOnce(Return(0)); + + auto result = CopyDirCore::DoCopyDir(src, dest, mode); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_001"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_002 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_002"; + + string src = "/src/file"; + string dest = "/dest/file"; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(false)); + auto result = CopyDirCore::DoCopyDir(src, dest); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_002"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_003 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_003"; + + string src = "/src/file"; + string dest = "/dest/file"; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(false)); + auto result = CopyDirCore::DoCopyDir(src, dest); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_003"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_004 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_004"; + + string src = "/src/file"; + string dest = "/dest/file"; + int32_t mode = -1; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + auto result = CopyDirCore::DoCopyDir(src, dest, mode); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_004"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_005 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_005"; + + string src = "/src/file"; + string dest = "/dest/file"; + int32_t mode = 3; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + auto result = CopyDirCore::DoCopyDir(src, dest, mode); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_005"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_006 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_006"; + + string src = "/same"; + string dest = "/same"; + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + + auto result = CopyDirCore::DoCopyDir(src, dest); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_006"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_007 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_007"; + + string src = "/src/dir"; + string dest = "/dest"; + error_code errCode(EACCES, std::generic_category()); + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, Exists(dest + "/dir", _)).WillOnce(Return(false)); + EXPECT_CALL(*sys, CreateDirectory(_, errCode)).WillOnce(Return(false)); + + auto result = CopyDirCore::DoCopyDir(src, dest); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_007"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_008 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_008"; + + string src = "/dest/dir"; + string dest = "/dest"; + int32_t mode = 0; + error_code errCode; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, ParentPath(src)).WillOnce(Return(filesystem::path("/dest"))); + + auto result = CopyDirCore::DoCopyDir(src, dest, mode); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_008"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_009 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_009"; + + string src = "/src/dir"; + string dest = "/dest"; + int32_t mode = 0; + error_code errCode; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, ParentPath(src)).WillOnce(Return(filesystem::path("/src"))); + EXPECT_CALL(*sys, Exists(dest + "/dir", errCode)).WillOnce(Return(false)); + EXPECT_CALL(*sys, CreateDirectory(filesystem::path(dest + "/dir"), errCode)).WillOnce(Return(true)); + EXPECT_CALL(*sys, Scandir(src.c_str(), _, _, _)).WillOnce(Return(-1)); + + auto result = CopyDirCore::DoCopyDir(src, dest, mode); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_009"; +} + +/** + * @tc.name: CopyDirCoreMockTest_DoCopyDir_010 + * @tc.desc: Test function of CopyDirCore::DoCopyDir interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreMockTest, CopyDirCoreMockTest_DoCopyDir_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-begin CopyDirCoreMockTest_DoCopyDir_010"; + + string src = "/src/dir/file.txt"; + string dest = "/dest/dir/file.txt"; + + EXPECT_CALL(*sys, IsDirectory(src, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, IsDirectory(dest, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, CreateDirectory(_, _)).WillOnce(Return(true)); + EXPECT_CALL(*sys, Scandir(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*sys, CopyFile(src, dest, _, _)).WillOnce(Return(false)); + + auto result = CopyDirCore::DoCopyDir("/src/dir", "/dest"); + EXPECT_FALSE(result.fsResult.IsSuccess()); + + GTEST_LOG_(INFO) << "CopyDirCoreMockTest-end CopyDirCoreMockTest_DoCopyDir_010"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a12f7ea141515e901ea96b9263837b219e5271e --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp @@ -0,0 +1,409 @@ +/* + * 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 "copy_file_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 CopyFileCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void CopyFileCoreTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyFileCoreTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyFileCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyFileCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_001 + * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_001"; + + FileInfo src; + FileInfo dest; + optional mode = std::make_optional(1); + + auto res = CopyFileCore::DoCopyFile(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_001"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_003 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_003"; + + FileInfo src; + FileInfo dest; + src.isPath = true; + dest.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_003"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_004 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_004"; + + FileInfo src; + FileInfo dest; + src.isPath = true; + dest.isPath = false; + src.fdg = make_unique(1); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_004"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_005 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_005"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_005"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_006 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_006"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_006"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_007 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_007"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + dest.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_007"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_008 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_008"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + dest.fdg = make_unique(); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_008"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_009 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_009"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + int destfd = open("dest.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + ASSERT_NE(destfd, -1); + src.fdg = make_unique(srcfd); + dest.fdg = make_unique(destfd); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(srcfd); + close(destfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_009"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0010 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0010"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0010"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0011 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0011"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len + 1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0011"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0012 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0012"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0012"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0013 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0013"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(0)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0013"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b26d26e8c2be983188cca5e5834bc5eaf2caaa8d --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp @@ -0,0 +1,74 @@ +/* + * 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 "copy_file_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyFileCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CopyFileCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyFileCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyFileCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyFileCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyFileCoreMockTest_DoCopyFile_001 + * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_001"; + + FileInfo src; + FileInfo dest; + optional mode = std::make_optional(1); + + auto res = CopyFileCore::DoCopyFile(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ 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 0000000000000000000000000000000000000000..54c44ba5cd40d2cd4c3cd002606a1f0e9cb37b53 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_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 "create_randomaccessfile_core.h" +#include "mock/uv_fs_mock.h" +#include "mock/system_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; + static inline shared_ptr sys = nullptr; +}; + +void CreateRandomAccessFileCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + sys = std::make_shared(); + System::ins = sys; +} + +void CreateRandomAccessFileCoreMockTest::TearDownTestCase(void) +{ + System::ins = nullptr; + sys = nullptr; + 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) << "CreateRandomAccessFileCoreMockTest-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) << "CreateRandomAccessFileCoreMockTest-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) << "CreateRandomAccessFileCoreMockTest-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) << "CreateRandomAccessFileCoreMockTest-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 0000000000000000000000000000000000000000..58c1805df311fe24dc677721f57c5fbbd05118db --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -0,0 +1,188 @@ +/* + * 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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; + + int fd = -1; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-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) << "CreateRandomAccessFileCoreTest-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; + + int fd = 3; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "CreateRandomAccessFileCoreTest-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/create_stream_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_stream_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..904908c536e54bac0af7b06754b8e0d4bb4e5829 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_stream_core_test.cpp @@ -0,0 +1,81 @@ +/* +* 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 "create_stream_core.h" + +#define CREATE_STREAM_FILE_PATH "/data/test/CreateStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class CreateStreamCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) { + int32_t fd = open(CREATE_STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() { + rmdir(CREATE_STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; +/** +* @tc.name: DoCreateStreamTest_0001 +* @tc.desc: Test function of DoCreateStream() interface for success. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(CreateStreamCoreTest, DoCreateStreamTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CreateStreamCoreTest-begin DoCreateStreamTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(CREATE_STREAM_FILE_PATH, "r"); + ASSERT_TRUE(ret.IsSuccess()); + + auto stream = ret.GetData().value(); + ASSERT_NE(stream, nullptr); + auto retClose = stream->Close(); + EXPECT_TRUE(retClose.IsSuccess()); + + GTEST_LOG_(INFO) << "CreateStreamCoreTest-end DoCreateStreamTest_0001"; +} + +/** +* @tc.name: DoCreateStreamTest_0002 +* @tc.desc: Test function of DoCreateStream() interface for fail. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(CreateStreamCoreTest, DoCreateStreamTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CreateStreamCoreTest-begin DoCreateStreamTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(CREATE_STREAM_FILE_PATH, "ssss"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "CreateStreamCoreTest-end DoCreateStreamTest_0002"; +} + +} +} +} \ 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 0000000000000000000000000000000000000000..455933c2fea24a47d498c9608bc72f34e9e4e7ba --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp @@ -0,0 +1,100 @@ +/* + * 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 "mock/uv_fs_mock.h" + +#include +#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) << "NClassTest-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) << "NClassTest-end DupCoreMockTest_DoDup_001"; +} + +// /** +// * @tc.name: DupCoreMockTest_DoDup_002 +// * @tc.desc: Test function of DupCore::DoDup interface for SUCCESS. +// * @tc.size: MEDIUM +// * @tc.type: FUNC +// * @tc.level Level 1 +// */ +// HWTEST_F(DupCoreMockTest, DupCoreMockTest_DoDup_002, testing::ext::TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "NClassTest-begin DupCoreMockTest_DoDup_002"; + +// int32_t fd = 1; + +// EXPECT_CALL(*uvMock, uv_fs_readlink(_, _, _, _)).WillOnce(Return(0)); +// auto res = DupCore::DoDup(fd); +// EXPECT_EQ(res.IsSuccess(), true); + +// GTEST_LOG_(INFO) << "NClassTest-end DupCoreMockTest_DoDup_002"; +// } + +} // 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 6d92836d637c1b567f3df12fa74b5cb47fec6cd5..e622a27f6fc6c20428eb25b9ec53de7323c859e7 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/fdatasync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2228f425132edd9d25e5542a02bc42e4b8b388a0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp @@ -0,0 +1,102 @@ +/* + * 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 + +#include "fdatasync_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FDataSyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void FDataSyncCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void FDataSyncCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FDataSyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FDataSyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_001 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin FDataSyncCoreMockTest_DoFDataSync_001"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end FDataSyncCoreMockTest_DoFDataSync_001"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_002 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin FDataSyncCoreMockTest_DoFDataSync_002"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(-1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end FDataSyncCoreMockTest_DoFDataSync_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4c3493feb8bf5dcd20dbc39a27e10840260de86 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp @@ -0,0 +1,118 @@ +/* +* 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 "fdopen_stream_core.h" + +#define FDOPEN_STREAM_FILE_PATH "/data/test/FdopenStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class FdopenStreamCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) { + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() { + rmdir(FDOPEN_STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +// /** +// * @tc.name: DoFdopenStreamTest_0001 +// * @tc.desc: Test function of DoFdopenStream() interface for success. +// * @tc.size: MEDIUM +// * @tc.type: FUNC +// * @tc.level Level 1 +// * @tc.require: AR000IGDNF +// */ +// HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0001, testing::ext::TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0001"; +// int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); +// if (fd <= 0) { +// close(fd); +// ASSERT_TRUE(false); +// } + +// auto ret = FdopenStreamCore::DoFdopenStream(fd, "r"); +// ASSERT_TRUE(ret.IsSuccess()); + +// auto stream = ret.GetData().value(); +// ASSERT_NE(stream, nullptr); +// auto retClose = stream->Close(); +// EXPECT_TRUE(retClose.IsSuccess()); + +// close(fd); + +// GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0001"; +// } + +/** +* @tc.name: DoFdopenStreamTest_0002 +* @tc.desc: Test function of DoFdopenStream() interface for fd < 0. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0002"; + auto ret = FdopenStreamCore::DoFdopenStream(-1, "r"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0002"; +} + +/** +* @tc.name: DoFdopenStreamTest_0003 +* @tc.desc: Test function of DoFdopenStream() interface for fail. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0003, testing::ext::TestSize.Level1) +{ + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0003"; + auto ret = FdopenStreamCore::DoFdopenStream(fd, "sssssss"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + close(fd); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0003"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8afb42852e31362517b6dcbaec1b81297afdb8b2 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp @@ -0,0 +1,99 @@ +/* + * 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 "fsync_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 FsyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void FsyncCoreMockTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsyncCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_001 + * @tc.desc: Test function of RenameCore::DoFsync interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_001"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_001"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_002 + * @tc.desc: Test function of RenameCore::DoFsync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_002"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_002"; +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..063ce90a9a7122c2286b936a95602eedb4a4977b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp @@ -0,0 +1,524 @@ +/* +* 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 "listfile_core.h" + +#define DIR_PATH "/data/test/ListFileCoreTest" +#define EMPTY_PATH "/data/test/Empty" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +namespace fs = std::filesystem; + +static void create_file(const fs::path& path, const std::vector& data, bool binary = true) +{ + fs::create_directories(path.parent_path()); // 确保目录存在 + std::ofstream file(path, binary ? std::ios::binary : std::ios::out); + if (!file) { + std::cerr << "创建文件失败: " << path << std::endl; + return; + } + file.write(reinterpret_cast(data.data()), data.size()); + std::cout << "已创建文件: " << path << std::endl; +} + +static void writeBuffer(const string filename) +{ + const int target_size = 1024; + std::vector buffer(target_size, 0); + std::ofstream file(filename, std::ios::binary); + file.write(buffer.data(), buffer.size()); +} + +class ListFileCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) { + fs::create_directory(EMPTY_PATH); + const std::vector directories = { + "/data/test/ListFileCoreTest", // 单级目录 + "/data/test/ListFileCoreTest/level1", // 二级目录 + "/data/test/ListFileCoreTest/level1/level2" // 三级目录 + }; + + // 为每个文件类型创建3个文件 + for (int i = 0; i < 3; ++i) { + create_file(directories[i] / ("text_" + std::to_string(i+1) + ".txt"), + {'F','i','l','e',' ',char('0'+i+1)}, false); + + create_file(directories[i] / ("image_" + std::to_string(i+1) + ".png"), + {0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A}); + + create_file(directories[i] / ("photo_" + std::to_string(i+1) + ".jpg"), + {0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46}); + + create_file(directories[i] / ("data_" + std::to_string(i+1) + ".data"), + {0xAB,0xCD,0xEF, char(i), char(i+1), char(i+2)}); + } + + writeBuffer("/data/test/ListFileCoreTest/text_1.txt"); + }; + static void TearDownTestCase() { + rmdir(DIR_PATH); + rmdir(EMPTY_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** +* @tc.name: DoListFileCoreTest_0001 +* @tc.desc: Test function of DoListFileCore() interface for error suffix. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0001"; + FsListFileOptions opt; + FsFileFilter filter; + // filter.SetSuffix({"txt"}); // 无效后缀(缺少`.`) + std::vector suffixVector = {"txt"}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0001"; +} + +/** +* @tc.name: DoListFileCoreTest_0002 +* @tc.desc: Test function of DoListFileCore() interface for error suffix. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0002"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector suffixVector = {".tx@t"}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0002"; +} + +/** +* @tc.name: DoListFileCoreTest_0003 +* @tc.desc: Test function of DoListFileCore() interface for error listNum. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0003"; + FsListFileOptions opt; + opt.listNum = -5; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0003"; +} + +/** +* @tc.name: DoListFileCoreTest_0004 +* @tc.desc: Test function of DoListFileCore() interface for current suffix. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0004"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector suffixVector = {".txt"}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 2); + EXPECT_EQ(files[0], "text_1.txt"); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0004"; +} + +/** +* @tc.name: DoListFileCoreTest_0005 +* @tc.desc: Test function of DoListFileCore() interface for no maching suffix . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0005"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector suffixVector = {".abc"}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_FALSE(files[0].empty()); + EXPECT_EQ(files[0], "level1"); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0005"; +} + +/** +* @tc.name: DoListFileCoreTest_0006 +* @tc.desc: Test function of DoListFileCore() interface for current display name . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0006"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector displayNameVector = {"text*.txt"}; + std::optional> optionalDisplayName = displayNameVector; + filter.SetDisplayName(optionalDisplayName); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 1); + EXPECT_EQ(files[0], "text_1.txt"); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0006"; +} + +/** +* @tc.name: DoListFileCoreTest_0007 +* @tc.desc: Test function of DoListFileCore() interface for current fileSizeOver . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0007"; + FsListFileOptions opt; + FsFileFilter filter; + filter.SetFileSizeOver(500); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 2); + EXPECT_EQ(files[0], "text_1.txt"); + EXPECT_EQ(files[1], "level1"); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0007"; +} + +/** +* @tc.name: DoListFileCoreTest_0008 +* @tc.desc: Test function of DoListFileCore() interface for displayMame = 0 . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0008"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector displayNameVector = {}; + std::optional> optionalDisplayName = displayNameVector; + filter.SetDisplayName(optionalDisplayName); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0008"; +} + +/** +* @tc.name: DoListFileCoreTest_0009 +* @tc.desc: Test function of DoListFileCore() interface for error fileSizeOver . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0009"; + FsListFileOptions opt; + FsFileFilter filter; + filter.SetFileSizeOver(-1); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0009"; +} + +/** +* @tc.name: DoListFileCoreTest_0010 +* @tc.desc: Test function of DoListFileCore() interface for error lstModitiedAfter . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0010"; + FsListFileOptions opt; + FsFileFilter filter; + filter.SetLastModifiedAfter(-1); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0010"; +} + +/** +* @tc.name: DoListFileCoreTest_0011 +* @tc.desc: Test function of DoListFileCore() interface for true recursion . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0011"; + FsListFileOptions opt; + opt.recursion = true; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 12); + EXPECT_EQ(files[0], "/text_1.txt"); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0011"; +} + +/** +* @tc.name: DoListFileCoreTest_0012 +* @tc.desc: Test function of DoListFileCore() interface for empty directory . +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0012"; + auto result = ListFileCore::DoListFile(EMPTY_PATH); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_TRUE(files.empty()); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0012"; +} + +/** +* @tc.name: DoListFileCoreTest_0013 +* @tc.desc: Test function of DoListFileCore() interface for current listNum. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0013"; + FsListFileOptions opt; + opt.listNum = 3; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 3); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0013"; +} + +/** +* @tc.name: DoListFileCoreTest_0014 +* @tc.desc: Test function of DoListFileCore() interface for no such file or directory. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0014"; + auto result = ListFileCore::DoListFile("/data/local/tmp/ascecedssw"); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0014"; +} + +/** +* @tc.name: DoListFileCoreTest_0015 +* @tc.desc: Test function of DoListFileCore() interface for >256 length suffix. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0015"; + FsListFileOptions opt; + FsFileFilter filter; + + const size_t TARGET_LENGTH = 257; + std::string str = "."; + for (size_t i = 1; i < TARGET_LENGTH; ++i) { + str += std::to_string(i % 10); + } + + std::vector suffixVector = {str}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0015"; +} + +/** +* @tc.name: DoListFileCoreTest_0016 +* @tc.desc: Test function of DoListFileCore() interface for empty suffix. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0016"; + FsListFileOptions opt; + FsFileFilter filter; + std::vector suffixVector = {}; + std::optional> optionalSuffix = suffixVector; + filter.SetSuffix(optionalSuffix); + opt.filter = filter; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + EXPECT_FALSE(result.IsSuccess()); + + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0016"; +} + +/** +* @tc.name: DoListFileCoreTest_0017 +* @tc.desc: Test function of DoListFileCore() interface for 0 listNum. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ListFileCoreTest-begin DoListFileCoreTest_0017"; + FsListFileOptions opt; + opt.listNum = 0; + + auto result = ListFileCore::DoListFile(DIR_PATH, opt); + ASSERT_TRUE(result.IsSuccess()); + + auto files = result.GetData().value(); + EXPECT_EQ(files.size(), 5); + + GTEST_LOG_(INFO) << "ListFileCoreTest-end DoListFileCoreTest_0017"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93c9f22ce24cad6f87837b5e1430ed4e441cd9fa --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp @@ -0,0 +1,122 @@ +/* + * 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 "lseek_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 LseekCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void LseekCoreTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void LseekCoreTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void LseekCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LseekCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_001 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_001"; + + int32_t fd = -1; + int64_t offset = 0; + + auto res = LseekCore::DoLseek(fd, offset); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_001"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_002 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_002"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(static_cast(-1)); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_002"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_003 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_003"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(SeekPos::CURRENT); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34c0d40b50f042e93ad92e7d49a34670ed9e9ff8 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdir_core_mock_test.cpp @@ -0,0 +1,183 @@ +/* + * 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 + +#include "mkdir_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MkdirCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path MkdirCoreMockTest::tempFilePath; + +void MkdirCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "test"; + std::filesystem::create_directory(tempFilePath); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void MkdirCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void MkdirCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MkdirCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0001 + * @tc.desc: Test function of DoMkdir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0001"; + + EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(0)); + + string path = tempFilePath.string() + "/test01"; + auto ret = MkdirCore::DoMkdir(path); + EXPECT_EQ(ret.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0001"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0002 + * @tc.desc: Test function of DoMkdir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0002"; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-2)).WillOnce(Return(0)); + + string path = tempFilePath.string() + "/test02/testDir"; + auto ret = MkdirCore::DoMkdir(path, true); + EXPECT_EQ(ret.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0002"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0003 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0003"; + + EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(1)); + + string path = tempFilePath.string() + "/test03"; + auto ret = MkdirCore::DoMkdir(path); + EXPECT_EQ(ret.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0003"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0004 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0004"; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + string path = "/"; + auto ret = MkdirCore::DoMkdir(path, true); + EXPECT_EQ(ret.IsSuccess(), false); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900015); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "File exists"); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0004"; +} + +/** + * @tc.name: MkdirCoreMockTest_DoMkdir_0005 + * @tc.desc: Test function of DoMkdir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0005"; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(2)); + + string path = ""; + auto ret = MkdirCore::DoMkdir(path, true); + EXPECT_EQ(ret.IsSuccess(), false); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900002); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "No such file or directory"); + + GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0005"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d86d3d920aae590f3c339619df95348acd8d603 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mkdtemp_core_mock_test.cpp @@ -0,0 +1,116 @@ +/* + * 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 + +#include "mkdtemp_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MkdtempCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path MkdtempCoreMockTest::tempFilePath; + +void MkdtempCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "test"; + std::filesystem::create_directory(tempFilePath); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void MkdtempCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void MkdtempCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MkdtempCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0001 + * @tc.desc: Test function of DoMkdtemp() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0001"; + + uv_fs_t mock_req; + mock_req.path = const_cast("/data/local/tmp/test/XXXXXX"); + + EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char* , uv_fs_cb) { + *req = mock_req; + return 0; + })); + + auto ret = MkdtempCore::DoMkdtemp("/data/local/tmp/test/XXXXXX"); + EXPECT_EQ(ret.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0001"; +} + +/** + * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0002 + * @tc.desc: Test function of DoMkdtemp() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0002"; + + string path = tempFilePath.string() + "/XXXXXX"; + + EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)).WillOnce(Return(-1)); + auto ret = MkdtempCore::DoMkdtemp(path); + EXPECT_EQ(ret.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ 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 0000000000000000000000000000000000000000..716dee535524932626c454032b8d89db3d4afb33 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp @@ -0,0 +1,81 @@ +/* + * 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); +} +} + +bool IsDirectory(const std::string& path, std::error_code& errCode) +{ + return System::ins->IsDirectory(path, errCode); +} + +bool Exists(const std::string& path, std::error_code& errCode) +{ + return System::ins->Exists(path, errCode); +} + +bool CreateDirectory(const std::filesystem::path& path, std::error_code& errCode) +{ + return System::ins->CreateDirectory(path, errCode); +} + +bool RemoveFile(const std::string& path, std::error_code& errCode) +{ + return System::ins->RemoveFile(path, errCode); +} +bool CopyFile(const std::string& src, const std::string& dest, std::filesystem::copy_options options, + std::error_code& errCode) +{ + return System::ins->CopyFile(src, dest, options, errCode); +} + +int Scandir(const char* path, struct dirent*** namelist, int (*filter)(const struct dirent*), + int (*compar)(const struct dirent**, const struct dirent**)) +{ + return System::ins->Scandir(path, namelist, filter, compar); +} + +int ScandirNext(struct dirent** namelist, struct dirent* entry) +{ + return System::ins->ScandirNext(namelist, entry); +} + +std::filesystem::path ParentPath(const std::string& path) +{ + return System::ins->ParentPath(path); +} \ 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 0000000000000000000000000000000000000000..f18ee390913d251269563358f42d667c0c628cd8 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h @@ -0,0 +1,67 @@ +/* + * 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 +#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; + virtual bool IsDirectory(const std::string& path, std::error_code& errCode) = 0; + virtual bool Exists(const std::string& path, std::error_code& errCode) = 0; + virtual bool CreateDirectory(const std::filesystem::path& path, std::error_code& errCode) = 0; + virtual bool RemoveFile(const std::string& path, std::error_code& errCode) = 0; + virtual bool CopyFile(const std::string& src, const std::string& dest, + std::filesystem::copy_options options, std::error_code& errCode) = 0; + virtual int Scandir(const char* path, struct dirent*** namelist, int (*filter)(const struct dirent*), + int (*compar)(const struct dirent**, const struct dirent**)) = 0; + virtual int ScandirNext(struct dirent** namelist, struct dirent* entry) = 0; + virtual std::filesystem::path ParentPath(const std::string& path) = 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)); + MOCK_METHOD2(IsDirectory, bool(const std::string& path, std::error_code& errCode)); + MOCK_METHOD2(Exists, bool(const std::string& path, std::error_code& errCode)); + MOCK_METHOD2(CreateDirectory, bool(const std::filesystem::path& path, std::error_code& errCode)); + MOCK_METHOD2(RemoveFile, bool(const std::string& path, std::error_code& errCode)); + MOCK_METHOD4(CopyFile, bool(const std::string& src, const std::string& dest, + std::filesystem::copy_options options, std::error_code& errCode)); + MOCK_METHOD4(Scandir, int(const char* path, struct dirent*** namelist, int (*filter)(const struct dirent*), + int (*compar)(const struct dirent**, const struct dirent**))); + MOCK_METHOD2(ScandirNext, int(struct dirent** namelist, struct dirent* entry)); + MOCK_METHOD1(ParentPath, std::filesystem::path(const std::string& path)); +}; + +} // 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/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index c4f112268ebfe3ef27f7127b1024a5196348b1ec..de6d4803d70b96156380db5be59a41c62e661dec 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 @@ -19,12 +19,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); } @@ -39,14 +35,12 @@ 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); } @@ -61,26 +55,79 @@ 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); +} 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 ad0522f9ee28ca3ecdb1e5d3e8e66fbb8892200a..1f9d5402b3df9b225b2d1ce7746c863da811c026 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 UV_FS_READ_MOCK_H #define UV_FS_READ_MOCK_H -#include "read_core.h" +#include "uv.h" #include @@ -27,14 +27,9 @@ 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_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; @@ -50,22 +45,29 @@ public: 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_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, - 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, @@ -76,8 +78,19 @@ public: 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 diff --git a/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..517178fd08faa710cc1f98af7bbe7a88e607eef4 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp @@ -0,0 +1,269 @@ +/* + * 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 "move_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 MoveCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr uvMock = nullptr; +}; + +void MoveCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void MoveCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void MoveCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MoveCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_000 + * @tc.desc: Test function of MoveCore::DoMove interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_000"; + + std::string src; + std::string dest; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(0)); + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_000"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_001 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_001"; + + std::string src; + std::string dest; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_001"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_002 + * @tc.desc: Test function of MoveCore::DoMove interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_002"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_002"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_003 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_003"; + + std::string src; + std::string dest; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_003"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_004 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_004"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_THROW_ERR); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)).WillOnce(Return(0)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_004"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_005 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_005"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_THROW_ERR); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_005"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_006 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_006"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_006"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_007 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_007"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_007"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_008 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_008"; + + std::string src; + std::string dest; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_008"; +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4a494e81d9474d3c6136043d01faf89192ee05a --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp @@ -0,0 +1,115 @@ +/* + * 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 "move_core.h" + +#include + + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MoveCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void MoveCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void MoveCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void MoveCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MoveCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MoveCoreTest_DoMove_001 + * @tc.desc: Test function of MoveCore::DoMove interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreTest-begin MoveCoreTest_DoMove_001"; + + std::string src = ""; + std::string dest = ""; + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_001"; +} + +/** + * @tc.name: MoveCoreTest_DoMove_002 + * @tc.desc: Test function of MoveCore::DoMove interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreTest-begin MoveCoreTest_DoMove_002"; + + std::string src = "/src.txt"; + std::string dest = ""; + + auto res = MoveCore::DoMove(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_002"; +} + +/** + * @tc.name: MoveCoreTest_DoMove_003 + * @tc.desc: Test function of MoveCore::DoMove interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreTest-begin MoveCoreTest_DoMove_003"; + + std::string src = "/src.txt"; + std::string dest = "/dest.txt"; + int mode = 3; + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17c95f4bf6d33b377406edd11c83c361da81ed5f --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/movedir_core_test.cpp @@ -0,0 +1,343 @@ +/* + * 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 "movedir_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class MoveDirCoreTest : public testing::Test { +public: + static filesystem::path srcPath; + static filesystem::path destPath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path MoveDirCoreTest::srcPath; +filesystem::path MoveDirCoreTest::destPath; + +void MoveDirCoreTest::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 MoveDirCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(srcPath); + filesystem::remove_all(destPath); +} + +void MoveDirCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void MoveDirCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0001 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0001"; + + string src = srcPath.string() + "/test01"; + string dest = destPath.string(); + filesystem::create_directories(src); + + auto result = MoveDirCore::DoMoveDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0001"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0002 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string() + "/test02"; + string dest = destPath.string(); + filesystem::create_directories(src); + + int invalidMode = DIRMODE_MAX + 1; + auto result = MoveDirCore::DoMoveDir(src, dest, optional(invalidMode)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0002"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0003 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string() + "/test03"; + string dest = destPath.string(); + + auto result = MoveDirCore::DoMoveDir(src, dest, optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0003"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0004 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0002"; + + string src = srcPath.string(); + string dest = destPath.string() + "/test04"; + + auto result = MoveDirCore::DoMoveDir(src, dest, optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0004"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0005 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0005"; + + string src = "/data/local/test05/src/src/src/test05"; + string dest = destPath.string() + "/src"; + filesystem::create_directories(src); + filesystem::create_directories(dest); + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test05/", destPath.string(), optional(DIRMODE_DIRECTORY_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900032); + EXPECT_FALSE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test05"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0005"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0006 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0006"; + + string src = "/data/local/test06/src/src/src/test06"; + string dest = destPath.string() + "/src"; + filesystem::create_directories(src); + filesystem::create_directories(dest); + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test06/src", destPath.string(), optional(DIRMODE_DIRECTORY_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + + filesystem::remove_all("/data/local/test06"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0006"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0007 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0007"; + + filesystem::create_directories("/data/local/test07/src"); + filesystem::create_directories("/data/local/test07/dest"); + filesystem::path srcFile = "/data/local/test07/src/test_file.txt"; + ofstream(srcFile) << "Test content\n123\n456"; + filesystem::path destFile = "/data/local/test07/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test07/src/", "/data/local/test07/dest/", optional(DIRMODE_FILE_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + auto err = result.fsResult.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900015); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test07"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0007"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0008 + * @tc.desc: Test function of DoMoveDir() interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0008"; + + filesystem::create_directories("/data/local/test08/src"); + filesystem::create_directories("/data/local/test08/dest/test_file/test_file"); + filesystem::path srcFile = "/data/local/test08/src/test_file"; + ofstream(srcFile) << "Test content\n123\n456"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test08/src/", "/data/local/test08/dest/test_file/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test08"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0008"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0009 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0009"; + + filesystem::create_directories("/data/local/test09/src"); + filesystem::create_directories("/data/local/test09/dest"); + filesystem::path srcFile = "/data/local/test09/src/test_file.txt"; + ofstream(srcFile) << "Test content\n123\n456"; + filesystem::path destFile = "/data/local/test09/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test09/src/", "/data/local/test09/dest/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test09"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0009"; +} + +/** + * @tc.name: MoveDirCoreTest_DoMoveDir_0010 + * @tc.desc: Test function of DoMoveDir() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(MoveDirCoreTest, MoveDirCoreTest_DoMoveDir_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveDirCoreTest-begin MoveDirCoreTest_DoMoveDir_0010"; + + filesystem::create_directories("/data/local/test09/src/test_file.txt"); + filesystem::create_directories("/data/local/test09/dest"); + filesystem::path destFile = "/data/local/test09/dest/test_file.txt"; + ofstream(destFile) << "Test content\ndest"; + + auto result = MoveDirCore::DoMoveDir( + "/data/local/test09/src/", "/data/local/test09/dest/", optional(DIRMODE_FILE_REPLACE)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + filesystem::remove_all("/data/local/test09"); + + GTEST_LOG_(INFO) << "MoveDirCoreTest-end MoveDirCoreTest_DoMoveDir_0010"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ 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 0000000000000000000000000000000000000000..3a277ed75738a38910b5db70dc5cff7ffebb2cbb --- /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 0000000000000000000000000000000000000000..a95aa20bc776418aea8b6517f998b5c72b9a998f --- /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 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 0000000000000000000000000000000000000000..af3948eebd2589039a8306f82089fde49f925777 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_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 "read_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 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 c4dfda62e57d9df66484565a9b9c19f96adeec8c..2ae3fe892e807c7bba5ae1c68c18aaefa85fc481 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); + optional options = std::make_optional(); + options->offset = std::make_optional(-1); + auto res = ReadCore::DoRead(fd, arrayBuffer); - EXPECT_EQ(res.IsSuccess(), true); + 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/read_lines_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df5d48226d1d8fb99c42b31967fdb29b370ddfee --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_mock_test.cpp @@ -0,0 +1,135 @@ +/* + * 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 + +#include "mock/uv_fs_mock.h" +#include "read_lines_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadLinesCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path ReadLinesCoreMockTest::tempFilePath; + +void ReadLinesCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt"; + ofstream(tempFilePath) << "Test content\n123\n456"; + ofstream(tempFilePath).close(); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void ReadLinesCoreMockTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void ReadLinesCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadLinesCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_001 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin ReadLinesCoreMockTest_DoReadLines_001"; + + string path = tempFilePath.string(); + Options option; + option.encoding = "utf-8"; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end ReadLinesCoreMockTest_DoReadLines_001"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_002 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin ReadLinesCoreMockTest_DoReadLines_002"; + + string path = tempFilePath.string(); + Options option; + option.encoding = "utf-8"; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + auto res = ReadLinesCore::DoReadLines(path); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end ReadLinesCoreMockTest_DoReadLines_002"; +} + +/** + * @tc.name: ReadLinesCoreMockTest_DoReadLines_003 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin ReadLinesCoreMockTest_DoReadLines_003"; + + string path = tempFilePath.string(); + Options option; + option.encoding = "utf-8"; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end ReadLinesCoreMockTest_DoReadLines_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..693247094fb59b806e36dd75c855a4c866c675a0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_lines_core_test.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + + +#include "read_lines_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadLinesCoreTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path ReadLinesCoreTest::tempFilePath; + +void ReadLinesCoreTest::SetUpTestCase(void) +{ + tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt"; + ofstream(tempFilePath) << "Test content\n123\n456"; + ofstream(tempFilePath).close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void ReadLinesCoreTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void ReadLinesCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadLinesCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadLinesCoreTest_DoReadLines_001 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin ReadLinesCoreTest_DoReadLines_001"; + + string path = "ReadLinesCoreTest_DoReadLines_001"; + Options option; + option.encoding = "utf-8"; + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end ReadLinesCoreTest_DoReadLines_001"; +} + +/** + * @tc.name: ReadLinesCoreTest_DoReadLines_002 + * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin ReadLinesCoreTest_DoReadLines_002"; + + string path = "ReadLinesCoreTest_DoReadLines_002"; + Options option; + option.encoding = "utf-16"; + auto res = ReadLinesCore::DoReadLines(path, option); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end ReadLinesCoreTest_DoReadLines_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_text_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_text_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd900e1ee8bc2d79bb17aed930d1d1c4123cfea1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_text_core_test.cpp @@ -0,0 +1,246 @@ +/* +* 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 + +#define FILE_INFO "hello world" +#define FILE_PATH "/data/test/ReadTextCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +const string content = "hello world"; + +class ReadTextCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) { + int32_t fd = open(FILE_PATH, CREATE | O_RDWR, 0644); + write(fd, content.c_str(), content.length()); + close(fd); + }; + static void TearDownTestCase() { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; +/** +* @tc.name: DoReadTextTest_0001 +* @tc.desc: Test function of DoReadText() interface for single argument. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0001"; + auto ret = ReadTextCore::DoReadText(FILE_PATH); + EXPECT_TRUE(ret.IsSuccess()); + + const auto &resText = ret.GetData().value(); + string res = std::get<0>(resText); + EXPECT_EQ(res, FILE_INFO); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0001"; +} + +/** +* @tc.name: DoReadTextTest_0002 +* @tc.desc: Test function of DoReadText() interface for invalid offset. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0002"; + ReadTextOptions options; + options.offset = -1; + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0002"; +} + +/** +* @tc.name: DoReadTextTest_0003 +* @tc.desc: Test function of DoReadText() interface for invalid length < 0. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0003"; + ReadTextOptions options; + options.length = -5; + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0003"; +} + +/** +* @tc.name: DoReadTextTest_0004 +* @tc.desc: Test function of DoReadText() interface for invalid length > UINT_MAX. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0004"; + ReadTextOptions options; + options.length = static_cast(UINT_MAX) + 1; + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0004"; +} + +/** +* @tc.name: DoReadTextTest_0005 +* @tc.desc: Test function of DoReadText() interface for invalid encoding. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0005"; + ReadTextOptions options; + options.encoding = "gbk"; + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0005"; +} + +/** +* @tc.name: DoReadTextTest_0006 +* @tc.desc: Test function of DoReadText() interface for no such file or directory. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0006"; + auto ret = ReadTextCore::DoReadText("ReadTextCoreTest-begin-DoReadTextTest_0006.txt"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0006"; +} + +/** +* @tc.name: DoReadTextTest_0007 +* @tc.desc: Test function of DoReadText() interface for huge offset. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0007"; + ReadTextOptions options; + options.offset = 1000000; // 假设文件较小 + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0007"; +} + +/** +* @tc.name: DoReadTextTest_0008 +* @tc.desc: Test function of DoReadText() interface for no such file or directory. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0008"; + ReadTextOptions options; + options.length = 1000000; // 超过实际大小 + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + ASSERT_TRUE(ret.IsSuccess()); + + const auto &resText = ret.GetData().value(); + string res = std::get<0>(resText); + EXPECT_EQ(res, FILE_INFO); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0008"; +} + +/** +* @tc.name: DoReadTextTest_0009 +* @tc.desc: Test function of DoReadText() interface for success. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(ReadTextCoreTest, DoReadTextTest_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadTextCoreTest-begin DoReadTextTest_0009"; + ReadTextOptions options; + options.offset = 2; + options.length = 5; + auto ret = ReadTextCore::DoReadText(FILE_PATH, options); + ASSERT_TRUE(ret.IsSuccess()); + + const auto &resText = ret.GetData().value(); + string res = std::get<0>(resText); + std::string extracted = std::string(FILE_INFO).substr( + options.offset.value(), options.length.value()); + EXPECT_EQ(res, extracted); + + GTEST_LOG_(INFO) << "ReadTextCoreTest-end DoReadTextTest_0009"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ee7698e3b3bc881b6fa5561fc00b9a4bba4d9c4 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/rename_core_mock_test.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "rename_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 RenameCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void RenameCoreMockTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void RenameCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void RenameCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void RenameCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: RenameCoreMockTest_DoRename_001 + * @tc.desc: Test function of RenameCore::DoRename interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RenameCoreMockTest, RenameCoreMockTest_DoRename_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RenameCoreMockTest-begin RenameCoreMockTest_DoRename_001"; + string src; + string dest; + + EXPECT_CALL(*uvfs, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + + auto res = RenameCore::DoRename(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RenameCoreMockTest-end RenameCoreMockTest_DoRename_001"; +} + +/** + * @tc.name: RenameCoreMockTest_DoRename_002 + * @tc.desc: Test function of RenameCore::DoRename interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RenameCoreMockTest, RenameCoreMockTest_DoRename_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RenameCoreMockTest-begin RenameCoreMockTest_DoRename_002"; + string src; + string dest; + + EXPECT_CALL(*uvfs, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(1)); + + auto res = RenameCore::DoRename(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "RenameCoreMockTest-end RenameCoreMockTest_DoRename_002"; +} + + +} // 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 e6d30f2dc4720174e215d65797ff5866cdee77c0..8f23023fbc1f46e7ad58f56c3bd77b88bbdb04c4 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,100 @@ 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/stat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c5c7b4b1b2ef6f12b8d72533ec89f146de3ace5 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp @@ -0,0 +1,194 @@ +/* + * 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 "stat_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 StatCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void StatCoreTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void StatCoreTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void StatCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void StatCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: StatCoreTest_DoStat_001 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_001"; + + FileInfo fileinfo; + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_001"; +} + +/** + * @tc.name: StatCoreTest_DoStat_002 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_002"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = true; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_002"; +} + +/** + * @tc.name: StatCoreTest_DoStat_003 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_003"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = true; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_003"; +} + +/** + * @tc.name: StatCoreTest_DoStat_004 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_004"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_004"; +} + +/** + * @tc.name: StatCoreTest_DoStat_005 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_005"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.fdg = std::make_unique(-1); + fileinfo.isPath = false; + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_005"; +} + +/** + * @tc.name: StatCoreTest_DoStat_006 + * @tc.desc: Test function of FsyncCore::DoStat interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreTest, StatCoreTest_DoStat_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_006"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.fdg = std::make_unique(1); + fileinfo.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_006"; +} + +} // 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 58% 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 2910fcb9e1eb3ae036fdc3776247901a3aa2e64f..1d36e44f051468c909bdd932f2c4d01c60ba0bf1 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 @@ -25,80 +25,81 @@ 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 0000000000000000000000000000000000000000..24d2769abf65d43ca55937d139a74b56a5c6e6fe --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp @@ -0,0 +1,170 @@ +/* + * 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 "mock/uv_fs_mock.h" + +#include +#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 cb1ef1e16de011bd4cd9a0fa33d7b587dbe0f62a..b9a98e51fe0f98d7f0926da279c754f8eabce463 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/unlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1adefab1f86904dd78b9c12e9bcc30830e47e160 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/unlink_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 +#include +#include +#include + +#include "mock/uv_fs_mock.h" +#include "unlink_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class UnlinkCoreTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path UnlinkCoreTest::tempFilePath; + +void UnlinkCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "unlink_test_file.txt"; + ofstream(tempFilePath) << "Test content\n123\n456"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void UnlinkCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void UnlinkCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void UnlinkCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: UnlinkCoreTest_DoUnlink_001 + * @tc.desc: Test function of UnlinkCore::DoUnlink interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UnlinkCoreTest, UnlinkCoreTest_DoUnlink_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin UnlinkCoreTest_DoUnlink_001"; + + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1)); + + string path = tempFilePath.string(); + auto res = UnlinkCore::DoUnlink(path); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "NClassTest-end UnlinkCoreTest_DoUnlink_001"; +} + +/** + * @tc.name: UnlinkCoreTest_DoUnlink_002 + * @tc.desc: Test function of UnlinkCore::DoUnlink interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UnlinkCoreTest, UnlinkCoreTest_DoUnlink_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin UnlinkCoreTest_DoUnlink_002"; + + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)); + + string path = tempFilePath.string(); + auto res = UnlinkCore::DoUnlink(path); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end UnlinkCoreTest_DoUnlink_002"; +} + +} // 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 0000000000000000000000000000000000000000..c2aba0a67d4e02c22e729f2e6aa9f0a0a7322641 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp @@ -0,0 +1,125 @@ +/* + * 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 "mock/uv_fs_mock.h" + +#include +#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 b81af547a425f6e2e047c505acd7e18b14e27ee3..54ee576ccdf5b0bdb5e025aa8b1f2a3f89cfe7c6 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_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c88af5150b7afa72240b3f48bf70d1cdb43dcc23 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/write_core_mock_test.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 "write_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 WriteCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void WriteCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void WriteCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void WriteCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void WriteCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: WriteCoreMockTest_DoWrite1_001 + * @tc.desc: Test function of WriteCore::DoWrite3 interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite1_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite1_001"; + + int32_t fd = 1; + string buffer; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = WriteCore::DoWrite(fd, buffer); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite1_001"; +} + +/** + * @tc.name: WriteCoreMockTest_DoWrite1_002 + * @tc.desc: Test function of WriteCore::DoWrite3 interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite1_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite1_002"; + + int32_t fd = 1; + string buffer; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(1)); + auto res = WriteCore::DoWrite(fd, buffer); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite1_002"; +} + +/** + * @tc.name: WriteCoreMockTest_DoWrite2_003 + * @tc.desc: Test function of WriteCore::DoWrite2 interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WriteCoreMockTest, WriteCoreMockTest_DoWrite2_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteCoreMockTest-begin WriteCoreMockTest_DoWrite2_003"; + + int32_t fd = -1; + string buffer; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = WriteCore::DoWrite(fd, buffer); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "WriteCoreMockTest-end WriteCoreMockTest_DoWrite2_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file 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 c6ee32ef6cd57b7d4343c24bd8293467fa69a2fc..1741f168f8781985786a0559a8b297b628df4402 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 diff --git a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4be02cbffdf56d9937ca735608ff5f9bbd863dbd --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp @@ -0,0 +1,178 @@ +/* + * 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 +#include + +#include "mock/system_mock.h" +#include "xattr_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class XattrCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr sys = nullptr; +}; + +filesystem::path XattrCoreMockTest::tempFilePath; + +void XattrCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = "/data/local/tmp/xattr_test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456"; + tempfile.close(); + sys = std::make_shared(); + System::ins = sys; +} + +void XattrCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); + System::ins = nullptr; + sys = nullptr; +} + +void XattrCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void XattrCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: XattrCoreMockTest_DoSetXattr_001 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreMockTest_DoSetXattr_001"; + + string path = tempFilePath.string(); + string key = "test_key"; + string value = "test_value"; + + EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(-1)); + auto ret = XattrCore::DoSetXattr(path, key, value); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreMockTest_DoSetXattr_001"; +} + +/** + * @tc.name: XattrCoreMockTest_DoSetXattr_002 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreMockTest_DoSetXattr_002"; + + string path = tempFilePath.string(); + string key = "test_key"; + string value = "test_value"; + + EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(0)); + auto ret = XattrCore::DoSetXattr(path, key, value); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreMockTest_DoSetXattr_002"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_001 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreMockTest_DoGetXattr_001"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(-1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreMockTest_DoGetXattr_001"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_002 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreMockTest_DoGetXattr_002"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(Return(-1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreMockTest_DoGetXattr_002"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_003 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreMockTest_DoGetXattr_003"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(Return(1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreMockTest_DoGetXattr_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75fd856dfa02c911bbc50acf120794a7c7abd7c1 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp @@ -0,0 +1,141 @@ +/* + * 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 + +#include "xattr_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class XattrCoreTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path XattrCoreTest::tempFilePath; + +void XattrCoreTest::SetUpTestCase(void) +{ + tempFilePath = "/data/local/tmp/xattr_test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void XattrCoreTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void XattrCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void XattrCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: XattrCoreTest_DoSetXattr_001 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreTest_DoSetXattr_001"; + + string path = tempFilePath.string(); + std::string key = "test_key"; + std::string value(4097, 'a'); + + auto ret = XattrCore::DoSetXattr(path, key, value); + + EXPECT_FALSE(ret.IsSuccess()); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900020); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "Invalid argument"); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreTest_DoSetXattr_001"; +} + +/** + * @tc.name: XattrCoreTest_DoSetXattr_002 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreTest_DoSetXattr_002"; + + string path = tempFilePath.string(); + std::string key(4097, 'a'); + std::string value = "test_value"; + + auto ret = XattrCore::DoSetXattr(path, key, value); + + EXPECT_FALSE(ret.IsSuccess()); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900020); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "Invalid argument"); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreTest_DoSetXattr_002"; +} + +/** + * @tc.name: XattrCoreTest_DoGetXattr_001 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoGetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin XattrCoreTest_DoGetXattr_001"; + + std::string path = "/data/local/tmp/nonexistent_file"; + string key = "test_key"; + + auto ret = XattrCore::DoGetXattr(path, key); + + EXPECT_TRUE(ret.IsSuccess()); + EXPECT_EQ(ret.GetData().value(), ""); + + GTEST_LOG_(INFO) << "NClassTest-end XattrCoreTest_DoGetXattr_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ 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 0000000000000000000000000000000000000000..f7bddd4e583c81d9f5188a827a27908091531e19 --- /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"; +} + +} +} +} \ 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 0000000000000000000000000000000000000000..99f65b679a2fc6d7c8550b80aeba8c6e57482aea --- /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"; +} + +} +} +} \ 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 0000000000000000000000000000000000000000..31ea3914bcf5297d0bb04f03eaf8b1cf1ad077f8 --- /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