diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 928421ec07e6f786d2a9ff6dad3b3b5add7f0fcd..3c4ddabd989b02f0a78d67f916b3c4cf72541662 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -851,6 +851,8 @@ ohos_shared_library("ani_file_hash") { include_dirs = [ "src/mod_hash", "src/mod_hash/ani", + "src/mod_hash/class_hashstream", + "src/mod_hash/class_hashstream/ani", ] sources = [ "src/common/ani_helper/ani_signature.cpp", @@ -861,6 +863,8 @@ ohos_shared_library("ani_file_hash") { "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", "src/mod_hash/ani/hash_ani.cpp", + "src/mod_hash/class_hashstream/ani/hashstream_ani.cpp", + "src/mod_hash/class_hashstream/hs_hashstream.cpp", "src/mod_hash/hash_core.cpp", ] diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 4a683a78389923e50500cae3dfe396b8b81857c8..f8a2a92debbfdf077663d6197e7c20acad3f5c09 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -60,6 +60,7 @@ const string BuiltInTypes::Array::objectSetterSig = // BuiltInTypes::ArrayBuffer const Type BuiltInTypes::ArrayBuffer::classType = Builder::BuildClass("escompat.ArrayBuffer"); const string BuiltInTypes::ArrayBuffer::classDesc = BuiltInTypes::ArrayBuffer::classType.Descriptor(); +const string BuiltInTypes::ArrayBuffer::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::intType }); // BuiltInTypes::BigInt const Type BuiltInTypes::BigInt::classType = Builder::BuildClass("escompat.BigInt"); const string BuiltInTypes::BigInt::classDesc = BuiltInTypes::BigInt::classType.Descriptor(); @@ -115,6 +116,18 @@ const Type FS::WatchEventInner::classType = Builder::BuildClass("@ohos.file.fs.W const string FS::WatchEventInner::classDesc = FS::WatchEventInner::classType.Descriptor(); const string FS::WatchEventInner::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::doubleType, BasicTypes::doubleType }); +// FS::ReadStream +const Type FS::ReadStream::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ReadStream"); +const string FS::ReadStream::classDesc = FS::ReadStream::classType.Descriptor(); +const string FS::ReadStream::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +// FS::WriteStream +const Type FS::WriteStream::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WriteStream"); +const string FS::WriteStream::classDesc = FS::WriteStream::classType.Descriptor(); +const string FS::WriteStream::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +// FS::AtomicFile +const Type FS::AtomicFile::classType = Builder::BuildClass("@ohos.file.fs.fileIo.AtomicFile"); +const string FS::AtomicFile::classDesc = FS::AtomicFile::classType.Descriptor(); +const string FS::AtomicFile::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); // FS::LocationType const Type FS::LocationType::classType = Builder::BuildClass("@ohos.file.fs.fileIo.LocationType"); const string FS::LocationType::classDesc = FS::LocationType::classType.Descriptor(); @@ -133,5 +146,9 @@ const string Impl::SecurityLabelImpl::classDesc = Impl::SecurityLabelImpl::class // Impl::StatvfsImpl const Type Impl::StatvfsImpl::classType = Builder::BuildClass("@ohos.file.statvfs.StatvfsImpl"); const string Impl::StatvfsImpl::classDesc = Impl::StatvfsImpl::classType.Descriptor(); +// HASH::HashStreamImpl +const Type HASH::HashStreamImpl::classType = Builder::BuildClass("@ohos.file.hash.HashStreamImpl"); +const string HASH::HashStreamImpl::classDesc = HASH::HashStreamImpl::classType.Descriptor(); +const string HASH::HashStreamImpl::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature \ No newline at end of file diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index 8b7e4328169b56ae56340086e9ef251da41e2c4c..3938f3fb85e10d82ecb19701ace14276ad983376 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -93,6 +93,7 @@ struct Array : public BaseType { struct ArrayBuffer : public BaseType { static const Type classType; static const string classDesc; + static const string ctorSig; }; struct BigInt : public BaseType { @@ -181,6 +182,24 @@ struct LocationType : public BaseType { static const string classDesc; }; +struct ReadStream : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct WriteStream : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct AtomicFile : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + } // namespace FS namespace Impl { @@ -212,6 +231,16 @@ struct StatvfsImpl : public BaseType { } // namespace Impl +namespace HASH { + +struct HashStreamImpl : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +} // namespace HASH + } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature #endif // INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 564d7bc56e1baf1dd65cdcaa42f1521936887de3..ba640a84cfca4ee72916e4dccc110a8ded4256dc 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -146,10 +146,10 @@ static std::tuple ParseFd(ani_env *env, const ani_object &pathOrF { ani_boolean isFd = false; - auto intClassDesc = BoxedTypes::Int::classDesc.c_str(); - ani_class intClass; - env->FindClass(intClassDesc, &intClass); - env->Object_InstanceOf(pathOrFd, intClass, &isFd); + auto classDesc = BoxedTypes::Double::classDesc.c_str(); + ani_class cls; + env->FindClass(classDesc, &cls); + env->Object_InstanceOf(pathOrFd, cls, &isFd); if (isFd) { ani_int fd; if (ANI_OK != env->Object_CallMethodByName_Int(pathOrFd, "toInt", nullptr, &fd)) { @@ -225,16 +225,18 @@ std::tuple TypeConverter::ToAniArrayBuffer(ani_env *env, return { false, nullptr }; } - static const char *className = "Lescompat/ArrayBuffer;"; + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); ani_status ret; ani_class cls; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); return { false, nullptr }; } + auto ctorDesc = BuiltInTypes::ArrayBuffer::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::ArrayBuffer::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { HILOGE("Not found ctor, err: %{private}d", ret); return { false, nullptr }; } diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets index 9ccc27495eee2897f9289fcad6b5e1222704da59..71b8e1c2dac3e5e36a47f44b92cdb0f47dd4591a 100644 --- a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -22,8 +22,8 @@ namespace Environment { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -35,8 +35,8 @@ namespace Environment { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -46,8 +46,8 @@ namespace Environment { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -58,8 +58,8 @@ namespace Environment { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -100,4 +100,4 @@ class EnvironmentImpl { static native getUserDocumentDirSync(): string; static native getExternalStorageDirSync(): string; static native getUserHomeDirSync(): string; -} \ No newline at end of file +} diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index ca851366c6ea19d88c329bf10e09d4a3495c6286..cec91441f4d040c8fcdfe3c8efd4c4af5eeb1ccf 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -166,7 +166,9 @@ static ani_status BindTaskSignalClassMethods(ani_env *env) static ani_status BindAtomicFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/AtomicFile;"; + auto classDesc = FS::AtomicFile::classDesc.c_str(); + auto ctorDesc = FS::AtomicFile::ctorDesc.c_str(); + auto ctorSig = FS::AtomicFile::ctorSig.c_str(); std::array methods = { ani_native_function { "getPath", nullptr, reinterpret_cast(AtomicFileAni::GetPath) }, @@ -176,11 +178,11 @@ static ani_status BindAtomicFileMethods(ani_env *env) ani_native_function { "nativeFinishWrite", nullptr, reinterpret_cast(AtomicFileAni::FinishWrite) }, ani_native_function { "nativeFailWrite", nullptr, reinterpret_cast(AtomicFileAni::FailWrite) }, ani_native_function { "delete", nullptr, reinterpret_cast(AtomicFileAni::Delete) }, - ani_native_function { "", "Lstd/core/String;:V", reinterpret_cast(AtomicFileAni::Constructor) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(AtomicFileAni::Constructor) }, ani_native_function { "openRead", nullptr, reinterpret_cast(AtomicFileAni::OpenRead) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); const static string mkdirCtorSig1 = diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index df9bfb23f058a74a382161bbace8dad9551b51b1..03f56f6b0a0f15181bf810d2ff8ea17ee9af6adf 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -47,8 +47,8 @@ function access(path: string, mode?: AccessModeType): Promise { promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -62,8 +62,8 @@ function access(path: string, callback: AsyncCallback): void { e.code = 0; let result = ret as boolean; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, false); + }).catch((e: Error): void => { + callback(e as BusinessError, false); }); } @@ -75,8 +75,8 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -94,8 +94,8 @@ function close(file: number | File): Promise { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -106,8 +106,8 @@ function close(file: number | File, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -121,8 +121,8 @@ function connectDfs(networkId: string, listeners: DfsListeners): Promise { FileIoImpl.connectDfs(networkId, listeners), networkId, listeners); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -132,8 +132,8 @@ function disconnectDfs(networkId: string): Promise { let promise = taskpool.execute((networkId: string): void => FileIoImpl.disConnectDfs(networkId), networkId); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -150,8 +150,8 @@ function getxattr(path: string, key: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -171,8 +171,8 @@ function copyDir(src: string, dest: string, mode?: number): Promise { FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }); } @@ -185,8 +185,8 @@ function copyDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -198,8 +198,8 @@ function copyDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -237,7 +237,7 @@ function fdatasync(fd: number): Promise { let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -249,8 +249,8 @@ function fdatasync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -273,7 +273,7 @@ function mkdir(path: string): Promise { let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -285,8 +285,8 @@ function mkdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -309,8 +309,8 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -326,8 +326,8 @@ function moveDir(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }) } @@ -341,8 +341,8 @@ function moveDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -355,8 +355,8 @@ function moveDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -393,8 +393,8 @@ function mkdtemp(prefix: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -408,8 +408,8 @@ function mkdtemp(prefix: string, callback: AsyncCallback): void { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -424,8 +424,8 @@ function moveFile(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -438,8 +438,8 @@ function moveFile(src: string, dest: string, mode: number, callback: AsyncCallba let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -451,8 +451,8 @@ function moveFile(src: string, dest: string, callback: AsyncCallback): voi let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -468,8 +468,8 @@ function open(path: String, mode?: number): Promise { promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -483,9 +483,9 @@ function open(path: String, mode: number, callback: AsyncCallback): e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -498,9 +498,9 @@ function open(path: String, callback: AsyncCallback): void { e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -516,8 +516,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions) promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -532,8 +532,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -546,8 +546,8 @@ function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -563,8 +563,8 @@ function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -578,8 +578,8 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -592,8 +592,8 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -610,8 +610,8 @@ function readLines(filePath: string, options?: Options): Promise promise.then((ret: NullishType): void => { let it = ret as ReaderIterator; resolve(it); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -625,9 +625,9 @@ function readLines(filePath: string, callback: AsyncCallback): v e.code = 0; let it = ret as ReaderIterator; callback(e, it); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -640,9 +640,9 @@ function readLines(filePath: string, options: Options, callback: AsyncCallback { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -656,8 +656,8 @@ function rmdir(path: string): Promise { let promise = taskpool.execute((path: string): void => FileIoImpl.rmdirSync(path), path); promise.then((ret: NullishType) => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -668,8 +668,8 @@ function rmdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -684,8 +684,8 @@ function truncate(file: string | number, len?: number): Promise { }, file, len); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -698,8 +698,8 @@ function truncate(file: string | number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -711,8 +711,8 @@ function truncate(file: string | number, len: number, callback: AsyncCallback(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -726,8 +726,8 @@ function unlink(path: string): Promise { let promise = taskpool.execute((path: string): undefined => unlinkSync(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -738,8 +738,8 @@ function unlink(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -751,9 +751,9 @@ function readText(filePath: string, options?: ReadTextOptions): Promise promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); }); } @@ -766,8 +766,8 @@ function readText(filePath: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -780,8 +780,8 @@ function readText(filePath: string, options: ReadTextOptions, callback: AsyncCal e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -797,8 +797,8 @@ function listFile(path: string, options?: ListFileOptions): Promise { promise.then((ret: NullishType): void => { let r = ret as string[]; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -812,8 +812,8 @@ function listFile(path: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -826,8 +826,8 @@ function listFile(path: string, options: ListFileOptions, callback: AsyncCallbac e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -851,8 +851,8 @@ function stat(file: string | number): Promise { promise.then((ret: NullishType): void => { let r = ret as Stat; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -866,8 +866,8 @@ function stat(file: string | number, callback: AsyncCallback): void e.code = 0; let r = ret as Stat; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } @@ -882,8 +882,8 @@ function fsync(fd: number): Promise { }, fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -896,8 +896,8 @@ function fsync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -913,8 +913,8 @@ function symlink(target: string, srcPath: string): Promise { }, target, srcPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -929,8 +929,8 @@ function rename(oldPath: string, newPath: string): Promise { }, oldPath, newPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -943,8 +943,8 @@ function rename(oldPath: string, newPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -964,8 +964,8 @@ function createRandomAccessFile(file: string | File, mode?: number, promise.then((ret: NullishType): void => { let raffile = ret as RandomAccessFileInner; resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -978,8 +978,8 @@ function fdopenStream(fd: number, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -994,7 +994,7 @@ function setxattr(path: string, key: string, value: string): Promise { FileIoImpl.setxattrSync(path, key, value), path, key, value); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -1009,9 +1009,9 @@ function createRandomAccessFile(file: string | File, callback: AsyncCallback { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1025,9 +1025,9 @@ function createRandomAccessFile(file: string | File, mode: number, e.code = 0; let raffile = ret as RandomAccessFile; callback(e, raffile); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1040,9 +1040,9 @@ function fdopenStream(fd: number, mode: string, callback: AsyncCallback) e.code = 0; let stream = ret as Stream; callback(e, stream); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1058,8 +1058,8 @@ function createStream(path: string, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1073,9 +1073,9 @@ function createStream(path: string, mode: string, callback: AsyncCallback { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1103,8 +1103,8 @@ function symlink(target: string, srcPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1130,8 +1130,8 @@ function lstat(path: string): Promise { let r = ret as Stat; resolve(r); } - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1151,8 +1151,8 @@ function lstat(path: string, callback: AsyncCallback): void { let r = ret as Stat; callback(e, r); } - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } function copyFile(src: string | number, dest: string | number, mode?: number): Promise { @@ -1161,8 +1161,8 @@ function copyFile(src: string | number, dest: string | number, mode?: number): P FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1173,8 +1173,8 @@ function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1186,8 +1186,8 @@ function copyFile(src: string | number, dest: string | number, mode: number, cal let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1198,8 +1198,8 @@ function copy(srcUri: string, destUri: string, options: CopyOptions, callback: A let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1210,8 +1210,8 @@ function copyFile(src: string | number, dest: string | number, callback: AsyncCa let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1222,8 +1222,8 @@ function copy(srcUri: string, destUri: string, callback: AsyncCallback): v let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1331,7 +1331,7 @@ export class RandomAccessFileInner implements RandomAccessFile { } native setFilePointer0(filePointer: number): void; - + native close(): void; writeSync(buffer: ArrayBuffer | string, options?: WriteOptions): number { @@ -1350,8 +1350,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1365,8 +1365,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1379,8 +1379,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1400,8 +1400,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1415,8 +1415,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1429,8 +1429,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1474,8 +1474,8 @@ export class FileInner implements File { }, exclusive); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1488,8 +1488,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1501,8 +1501,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1590,7 +1590,7 @@ export class StatInner implements Stat { this.nativeStat = stat; } } - + native isBlockDevice(): boolean; native isCharacterDevice(): boolean; native isDirectory(): boolean; @@ -1632,8 +1632,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.closeSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1644,8 +1644,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1656,8 +1656,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.flushSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1668,8 +1668,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1683,8 +1683,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1698,8 +1698,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1712,8 +1712,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1727,8 +1727,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1742,8 +1742,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1756,11 +1756,11 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + native readSync(buffer: ArrayBuffer, options?: ReadOptions): number; native seek(offset: number, whence?: number): number; } @@ -2119,15 +2119,15 @@ export class FileIoImpl { static native createStreamSync(path: string, mode: string): fileIo.Stream; static native createWatcherSync(path: string, events: number, listener: WatchEventListener): fileIo.Watcher; - + static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; - + static native dup(fd: number): fileIo.File; static native listFileSync(path: string, options?: ListFileOptions): string[]; static native lstatSync(path: string): fileIo.Stat; - + static native lseekSync(fd: number, offset: number, whence?: fileIo.WhenceType): number; static native mkdirSync(path: string): void; @@ -2143,7 +2143,7 @@ export class FileIoImpl { static native openSync(path: String, mode?: number): fileIo.File; static native readlinesSync(filePath: string, options?: Options): fileIo.ReaderIterator; - + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; static native readTextSync(filePath: string, options?: ReadTextOptions): string; diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index 5dc3e3bac009eae40dc57679682581e36c285d16..71666f6ead96ebef7a5f3a0c3023665fd6cbc1c3 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -17,6 +17,7 @@ #include +#include "ani_signature.h" #include "error_handler.h" #include "file_wrapper.h" #include "filemgmt_libhilog.h" @@ -30,6 +31,7 @@ namespace ANI { namespace fs = std::filesystem; using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; const std::string READ_STREAM_CLASS = "ReadStream"; const std::string WRITE_STREAM_CLASS = "WriteStream"; @@ -52,7 +54,8 @@ void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj } if (ANI_OK != - env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + env->Object_SetFieldByName_Long(obj, "nativePtr", + static_cast(reinterpret_cast(ret.GetData().value())))) { HILOGE("Failed to wrap entity for obj AtomicFile"); ErrorHandler::Throw(env, EIO); return; @@ -120,20 +123,24 @@ ani_object AtomicFileAni::GetBaseFile(ani_env *env, [[maybe_unused]] ani_object static ani_object CreateReadStream(ani_env *env, ani_string filePath) { - static const char *className = "L@ohos/file/fs/fileIo/ReadStream;"; + auto classDesc = FS::ReadStream::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::ReadStream::ctorDesc.c_str(); + auto ctorSig = FS::ReadStream::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { - HILOGE("New %s obj Failed", className); + HILOGE("New %s obj Failed", classDesc); return nullptr; } @@ -142,20 +149,24 @@ static ani_object CreateReadStream(ani_env *env, ani_string filePath) static ani_object CreateWriteStream(ani_env *env, ani_string filePath) { - static const char *className = "L@ohos/file/fs/fileIo/WriteStream;"; + auto classDesc = FS::WriteStream::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::WriteStream::ctorDesc.c_str(); + auto ctorSig = FS::WriteStream::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath)) { - HILOGE("New %s obj Failed", className); + HILOGE("New %s obj Failed", classDesc); return nullptr; } @@ -163,9 +174,9 @@ static ani_object CreateWriteStream(ani_env *env, ani_string filePath) } static ani_object CreateStream( - ani_env *env, [[maybe_unused]] ani_object object, const std::string &streamName, const std::string &fineName) + ani_env *env, [[maybe_unused]] ani_object object, const std::string &streamName, const std::string &fileName) { - auto [succ, filePath] = TypeConverter::ToAniString(env, fineName); + auto [succ, filePath] = TypeConverter::ToAniString(env, fileName); if (!succ) { HILOGE("Failed to ani_string"); ErrorHandler::Throw(env, UNKNOWN_ERR); diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp index 9218629ae14331a023ceadada777aadedbd1eafa..5dbc3b4ace78e2defa5c8eb7bf438acec1fb96a7 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp @@ -153,7 +153,7 @@ FsResult FsFile::TryLock(bool exclusive) const } int ret = 0; - auto mode = exclusive ? LOCK_EX : LOCK_SH; + auto mode = static_cast(exclusive ? LOCK_EX : LOCK_SH); ret = flock(fileEntity->fd_.get()->GetFD(), mode | LOCK_NB); if (ret < 0) { HILOGE("Failed to try to lock file"); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp index 1ef94cd44cc449dc0c5f4fcfab661739735504ef..d7d99779dee65254f58d7e6ef08339d37fcd5e86 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -30,10 +30,6 @@ using namespace std; static tuple ParseStringToFileInfo(const string &path) { - if (strlen(path.c_str()) < 0) { - HILOGE("The first argument requires filepath/file"); - return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; - } OHOS::DistributedFS::FDGuard sfd; auto fdg = CreateUniquePtr(sfd, false); if (fdg == nullptr) { diff --git a/interfaces/kits/js/src/mod_fs/properties/open_core.cpp b/interfaces/kits/js/src/mod_fs/properties/open_core.cpp index f688e8b5e208a25f3f2b4d1ec3074b8c6417325a..8ead99468d9e2761f1932c1410595fe6900b82aa 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open_core.cpp @@ -75,7 +75,7 @@ static tuple ValidAndConvertFlags(const optional &mode) } flags = static_cast(modeValue); uint32_t invalidMode = (O_WRONLY | O_RDWR); - if ((modeValue & invalidMode) == invalidMode) { + if ((flags & invalidMode) == invalidMode) { HILOGE("Invalid mode"); return { false, flags }; } diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 9edb9abefa382ab207f90b9c032d56c684df9034..9f8f7b08f551f2fea4dadf773329500dede24795 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -17,6 +17,7 @@ #include "ani_signature.h" #include "bind_function.h" #include "hash_ani.h" +#include "hashstream_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; @@ -30,6 +31,19 @@ static ani_status BindStaticMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindHashStreamMethods(ani_env *env) +{ + auto classDesc = HASH::HashStreamImpl::classDesc.c_str(); + auto ctorDesc = HASH::HashStreamImpl::ctorDesc.c_str(); + auto ctorSig = HASH::HashStreamImpl::ctorSig.c_str(); + std::array methods = { + ani_native_function { "digest", nullptr, reinterpret_cast(HashStreamAni::Digest) }, + ani_native_function { "update", nullptr, reinterpret_cast(HashStreamAni::Update) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(HashStreamAni::Constructor) }, + }; + return BindClass(env, classDesc, methods); +} + ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { if (vm == nullptr) { @@ -55,6 +69,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; + status = BindHashStreamMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for hashstream!"); + return status; + }; + *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index dfa16ab00530db92e6dc328c889cb035d50080d4..c9b4d5840c1d53fd46ad87299c377187d33f7612 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -23,8 +23,8 @@ export default namespace hash { promise.then((ret: NullishType): void => { let res = ret as string; resolve(res); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -36,8 +36,8 @@ export default namespace hash { e.code = 0; let res = ret as string; callback(e, res); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -46,12 +46,12 @@ export default namespace hash { } export class HashStream extends stream.Transform { - hs: hash.HashStream; + hs: HashStreamImpl; hashBuf?: ArrayBuffer; constructor(algorithm: string) { super(); - this.hs = new hash.HashStream(algorithm); + this.hs = new HashStreamImpl(algorithm); } digest(): string { @@ -83,6 +83,18 @@ export default namespace hash { } } +export class HashStreamImpl extends stream.Transform { + + static { + loadLibrary("ani_file_hash"); + } + private nativePtr: long = 0; + + native constructor(alg: string); + native digest(): string; + native update(data: ArrayBuffer): void; +} + class HashImpl { static { diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49079f114e175112a30ad6e7340cb250df0ffb5a --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hashstream_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "hs_hashstream.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +namespace fs = std::filesystem; +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +HsHashStream *Unwrap(ani_env *env, ani_object object) +{ + ani_long nativePtr; + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr); + if (ret != ANI_OK) { + HILOGE("Unwrap hashstream err: %{private}d", ret); + return nullptr; + } + uintptr_t ptrValue = static_cast(nativePtr); + HsHashStream *hashStream = reinterpret_cast(ptrValue); + return hashStream; +} + +void HashStreamAni::Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto [succ, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer); + if (!succ) { + HILOGE("illegal array buffer"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = hashStream->Update(arrayBuffer); + if (!ret.IsSuccess()) { + HILOGE("Cannot Update!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} + +ani_string HashStreamAni::Digest(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto hashStream = Unwrap(env, object); + if (hashStream == nullptr) { + HILOGE("Cannot unwrap hashStream!"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = hashStream->Digest(); + if (!ret.IsSuccess()) { + HILOGE("Cannot Digest!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const auto &res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Convert result to ani string failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) +{ + auto [succ, algorithm] = TypeConverter::ToUTF8String(env, alg); + if (!succ) { + HILOGE("Invalid alg"); + ErrorHandler::Throw(env, EINVAL); + return; + } + + auto ret = HsHashStream::Constructor(algorithm); + if (!ret.IsSuccess()) { + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } + + if (ANI_OK != + env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + HILOGE("Failed to wrap entity for obj HashStream"); + ErrorHandler::Throw(env, EIO); + return; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..c5f98974ea2d5b59ee5eb107fb631857c7e411ee --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class HashStreamAni final { +public: + static void Constructor(ani_env *env, ani_object obj, ani_string alg); + static ani_string Digest(ani_env *env, [[maybe_unused]] ani_object object); + static void Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_HASHSTREAM_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b03eb2b15e9f1ed4e8c34f50095039b56808e11e --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hs_hashstream.h" + +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg) +{ + return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED; +} + +static string HashFinal(const unique_ptr &hashBuf, size_t hashLen) +{ + stringstream ss; + for (size_t i = 0; i < hashLen; ++i) { + const int hexPerByte = 2; + ss << std::uppercase << std::setfill('0') << std::setw(hexPerByte) << std::hex + << static_cast(hashBuf[i]); + } + + return ss.str(); +} + +tuple HsHashStream::GetHsEntity() +{ + if (!entity) { + return { false, nullptr }; + } + return { true, entity.get() }; +} + +FsResult HsHashStream::Update(ArrayBuffer &buffer) +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: + MD5_Update(&hsEntity->md5Ctx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA1: + SHA1_Update(&hsEntity->shaCtx, buffer.buf, buffer.length); + break; + case HASH_ALGORITHM_TYPE_SHA256: + SHA256_Update(&hsEntity->sha256Ctx, buffer.buf, buffer.length); + break; + default: + break; + } + + return FsResult::Success(); +} + +FsResult HsHashStream::Digest() +{ + auto [succ, hsEntity] = GetHsEntity(); + if (!succ) { + HILOGE("Failed to get entity of HashStream"); + return FsResult::Error(EIO); + } + + string digestStr; + switch (hsEntity->algType) { + case HASH_ALGORITHM_TYPE_MD5: { + auto res = make_unique(MD5_DIGEST_LENGTH); + MD5_Final(res.get(), &hsEntity->md5Ctx); + digestStr = HashFinal(res, MD5_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + auto res = make_unique(SHA_DIGEST_LENGTH); + SHA1_Final(res.get(), &hsEntity->shaCtx); + digestStr = HashFinal(res, SHA_DIGEST_LENGTH); + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + auto res = make_unique(SHA256_DIGEST_LENGTH); + SHA256_Final(res.get(), &hsEntity->sha256Ctx); + digestStr = HashFinal(res, SHA256_DIGEST_LENGTH); + break; + } + default: + break; + } + return FsResult::Success(digestStr); +} + +FsResult HsHashStream::Constructor(string alg) +{ + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("algType is not found."); + return FsResult::Error(EINVAL); + } + + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + if (rawPtr == nullptr) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + std::unique_ptr hsEntity(rawPtr); + hsEntity->algType = algType; + + switch (algType) { + case HASH_ALGORITHM_TYPE_MD5: { + MD5_CTX ctx; + MD5_Init(&ctx); + hsEntity->md5Ctx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA1: { + SHA_CTX ctx; + SHA1_Init(&ctx); + hsEntity->shaCtx = ctx; + break; + } + case HASH_ALGORITHM_TYPE_SHA256: { + SHA256_CTX ctx; + SHA256_Init(&ctx); + hsEntity->sha256Ctx = ctx; + break; + } + default: + break; + } + + HsHashStream *hsStreamPtr = new HsHashStream(move(hsEntity)); + if (hsStreamPtr == nullptr) { + HILOGE("Failed to create HsHashStream object on heap."); + return FsResult::Error(ENOMEM); + } + + return FsResult::Success(move(hsStreamPtr)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h new file mode 100644 index 0000000000000000000000000000000000000000..b39b86fb2998afc82c15d7fd94acfd66b1d61299 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H + +#include +#include "filemgmt_libfs.h" +#include "fs_utils.h" +#include "hs_hashstream_entity.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class HsHashStream { +public: + HsHashStream(const HsHashStream &) = delete; + HsHashStream &operator=(const HsHashStream &) = delete; + + tuple GetHsEntity(); + FsResult Update(ArrayBuffer &buffer); + FsResult Digest(); + + static FsResult Constructor(string alg); + ~HsHashStream() = default; + +private: + unique_ptr entity; + explicit HsHashStream(unique_ptr entity) : entity(move(entity)) {} +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h new file mode 100644 index 0000000000000000000000000000000000000000..de1700cadb3121bc80cd58cd57b1aa1451affab3 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/hs_hashstream_entity.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H + +#include +#include +#include + +#include "hash_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct HsHashStreamEntity { + MD5_CTX md5Ctx; + SHA_CTX shaCtx; + SHA256_CTX sha256Ctx; + HASH_ALGORITHM_TYPE algType = HASH_ALGORITHM_TYPE_UNSUPPORTED; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_CLASS_HASHSTREAM_HS_HASHSTREAM_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 06e659dc275ae1890e009c02fc830bd8e766dbea..4f81cef3d8fc0f2c702d436507c7493149fb2cfb 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -23,8 +23,8 @@ namespace securityLabel { let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -35,8 +35,8 @@ namespace securityLabel { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -50,8 +50,8 @@ namespace securityLabel { promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -63,8 +63,8 @@ namespace securityLabel { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } diff --git a/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets index 831bc046d4a4836e2a9652147f047c8e00a7f76d..f3c569c80543f18810ca4e4c0e31a8ff0fe81b7a 100644 --- a/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets +++ b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets @@ -26,12 +26,12 @@ namespace statfs { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } - + export function getFreeSize(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(StatvfsImpl.getFreeSizeSync, path); promise.then((ret: NullishType): void => { @@ -39,27 +39,27 @@ namespace statfs { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + export function getTotalSizeSync(path: string): number { return StatvfsImpl.getTotalSizeSync(path); } - + export function getTotalSize(path: string): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void): void => { let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } - + export function getTotalSize(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); promise.then((ret: NullishType): void => { @@ -67,8 +67,8 @@ namespace statfs { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } } @@ -83,4 +83,4 @@ class StatvfsImpl { static native getFreeSizeSync(path: string): number; static native getTotalSizeSync(path: string): number; -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb263f86b72ed9418d085f89ade25f70f08..a801e70f7cdc1e7531b6fbfe926c6c571b952a2d 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,6 +17,10 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", + "js:ani_file_statvfs_test", + "js:ani_file_hash_test", + "js:ani_file_securitylabel_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", ] diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index a22cb0a2b7c27d01c14e9d238930fb1c01014702..f8830d71598d99cf0b073026ec1adbf3ba5f81d8 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -20,8 +20,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -34,12 +32,27 @@ ohos_unittest("ani_file_fs_test") { ] sources = [ + "mod_fs/class_atomicfile/fs_atomicfile_test.cpp", + "mod_fs/class_file/fs_file_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/class_stat/fs_stat_test.cpp", + "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/properties/access_core_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/copy_dir_core_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", + "mod_fs/properties/dup_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", + "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/lstat_core_test.cpp", + "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", - "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/properties/rmdir_core_test.cpp", + "mod_fs/properties/truncate_core_test.cpp", + "mod_fs/properties/utimes_core_test.cpp", + "mod_fs/properties/write_core_test.cpp", ] deps = [ @@ -64,4 +77,153 @@ ohos_unittest("ani_file_fs_test") { defines = [ "private=public", ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/class_stat/fs_stat_mock_test.cpp", + "mod_fs/class_stream/fs_stream_mock_test.cpp", + "mod_fs/class_stream/mock/c_mock.cpp", + "mod_fs/properties/access_core_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/truncate_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", + "mod_fs/properties/lstat_core_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/open_core_mock_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_hash_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_hash/hash_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_hash", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${file_api_path}/interfaces/kits/js:ani_file_hash", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "libuv:uv", + "googletest:gmock_main", + "googletest:gtest_main", + ] +} + +ohos_unittest("ani_file_securitylabel_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_securitylabel/securitylabel_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + ] + + 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_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..f506c732f2a9a1959c92d90f6ec371fc1c1da48b --- /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 g_filePath = "/data/test/FsAtomicfileTest.txt"; +string g_deleteFile = "/data/test/FsAtomicfileDelTest.txt"; + +class FsAtomicfileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void FsAtomicfileTest::SetUpTestCase(void) +{ + ofstream tempfile(g_filePath); + tempfile << "hello world"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsAtomicfileTest::TearDownTestCase(void) +{ + filesystem::remove(g_filePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsAtomicfileTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsAtomicfileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsAtomicfileTest_GetPath_001 + * @tc.desc: Test function of FsAtomicFile::GetPath interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + string path = stream->GetPath(); + EXPECT_EQ(path, g_filePath); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_001 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_001"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_002 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for path > PATH_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_002"; + + size_t largeLength = static_cast(PATH_MAX) + 1; + string largeString(largeLength, 'a'); + + auto ret = FsAtomicFile::Constructor(largeString); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_002"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_003 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed realpath. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_003"; + + string path = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_003"; +} + +/** + * @tc.name: FsAtomicfileTest_GetBaseFile_004 + * @tc.desc: Test function of FsAtomicFile::GetBaseFile interface for failed open. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_004"; + + string path = "/data/test/aaaaaaaaaa.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->GetBaseFile(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetBaseFile_004"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_001 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_TRUE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_002 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no parent dir. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_002"; + + string path = "/data/local/tmp/test/test/test/test.txt"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_002"; +} + +/** + * @tc.name: FsAtomicfileTest_StartWrite_003 + * @tc.desc: Test function of FsAtomicFile::StartWrite interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_003"; + + string path = "/sys/kernel/address_bits"; + + auto ret = FsAtomicFile::Constructor(path); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + EXPECT_FALSE(retFl.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_StartWrite_003"; +} + +/** + * @tc.name: FsAtomicfileTest_FinishWrite_001 + * @tc.desc: Test function of FsAtomicFile::FinishWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FinishWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FinishWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_FailWrite_001 + * @tc.desc: Test function of FsAtomicFile::FailWrite interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->FailWrite(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_FailWrite_001"; +} + +/** + * @tc.name: FsAtomicfileTest_Delete_001 + * @tc.desc: Test function of FsAtomicFile::Delete interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; + + auto ret = FsAtomicFile::Constructor(g_deleteFile); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto retFl = stream->StartWrite(); + ASSERT_TRUE(retFl.IsSuccess()); + string newPath = retFl.GetData().value(); + ofstream tempfile(newPath); + tempfile << "hello world"; + tempfile.close(); + + auto retFW = stream->Delete(); + EXPECT_TRUE(retFW.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_Delete_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_001 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for succ. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; + + auto ret = FsAtomicFile::Constructor(g_filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_TRUE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_002 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for valied path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_002"; + + auto ret = FsAtomicFile::Constructor("aaaaaaaaaaaaaaaa"); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_FALSE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file 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..90cc1f29fd106e2b2727a9d0ab28e7bc0534308e --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_mock_test.cpp @@ -0,0 +1,386 @@ +/* + * 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 "file_entity.h" +#include "fs_file.h" +#include "system_mock.h" +#include "uv_fs_mock.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +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; + 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/test/file_test.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"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/data/test/file_test.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + 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 ERROR. + * @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"; + + 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_002"; +} + +/** + * @tc.name: FsFileMockTest_GetName_003 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_003"; + + fsFile->fileEntity->path_ = "/invalid/path"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_003"; +} + +/** + * @tc.name: FsFileMockTest_GetName_004 + * @tc.desc: Test function of GetName() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004"; + + fsFile->fileEntity->path_ = "file_test.txt"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_005 + * @tc.desc: Test function of GetParent() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_005"; + + fsFile->fileEntity->path_ = "/invalid/path"; + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1)); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_005"; +} + +/** + * @tc.name: FsFileMockTest_GetName_006 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/file_test.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), true); + + 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"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("/test/dir_test"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007"; +} + +/** + * @tc.name: FsFileMockTest_GetName_008 + * @tc.desc: Test function of GetName() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_008"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("file_test.txt"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_008"; +} + +/** + * @tc.name: FsFileMockTest_GetParent_009 + * @tc.desc: Test function of GetParent() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_009"; + + uv_fs_t mock_req; + mock_req.ptr = const_cast("dir_test"); + + EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)) + .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) { + *req = mock_req; + return 0; + })); + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_009"; +} + +/** + * @tc.name: FsFileMockTest_Lock_010 + * @tc.desc: Test function of Lock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_010"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->Lock(true); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_010"; +} + +/** + * @tc.name: FsFileMockTest_Lock_011 + * @tc.desc: Test function of Lock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_011"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->Lock(false); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_011"; +} + +/** + * @tc.name: FsFileMockTest_TryLock_012 + * @tc.desc: Test function of TryLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_012"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->TryLock(true); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_012"; +} + +/** + * @tc.name: FsFileMockTest_TryLock_013 + * @tc.desc: Test function of TryLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_013"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->TryLock(false); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_013"; +} + +/** + * @tc.name: FsFileMockTest_UnLock_014 + * @tc.desc: Test function of UnLock() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_014"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1)); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_014"; +} + +/** + * @tc.name: FsFileMockTest_UnLock_015 + * @tc.desc: Test function of UnLock() interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_015"; + + EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1)); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_015"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e432f66f6109e8f96b668a2911b0842e34435beb --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_file/fs_file_test.cpp @@ -0,0 +1,278 @@ +/* + * 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 "file_entity.h" +#include "fs_file.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(); + unique_ptr fileEntity; + 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 = make_unique(); + const int fdValue = 3; + const bool isClosed = false; + fileEntity->fd_ = make_unique(fdValue, isClosed); + fileEntity->path_ = "/data/test/file_test.txt"; + fileEntity->uri_ = ""; + fsFile = make_unique(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 = 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 = 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 = 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 = 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 = 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 = 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 = make_unique(nullptr); + auto result = fsFile->UnLock(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_UnLock_009"; +} + +/** + * @tc.name: FsFileTest_GetName_010 + * @tc.desc: Test function of GetName() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetName_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetName_010"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetName(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetName_010"; +} + +/** + * @tc.name: FsFileTest_GetParent_011 + * @tc.desc: Test function of GetParent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetParent_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetParent_011"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetParent(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetParent_011"; +} + +/** + * @tc.name: FsFileTest_GetPath_012 + * @tc.desc: Test function of GetPath() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsFileTest, FsFileTest_GetPath_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsFileTest-begin FsFileTest_GetPath_012"; + + fsFile->fileEntity->uri_ = "file://storage/file_test.txt"; + auto result = fsFile->GetPath(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsFileTest-end FsFileTest_GetPath_012"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/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..321d4f59e3ff3a0ed1281313fd574040bf8bba11 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp @@ -0,0 +1,250 @@ +/* + * 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 "file_entity.h" +#include "fs_randomaccessfile.h" +#include "uv_fs_mock.h" + + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsRandomAccessFileMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +protected: + std::unique_ptr rafEntity; + std::unique_ptr raf; +}; + +void FsRandomAccessFileMockTest::SetUpTestCase(void) +{ + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsRandomAccessFileMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsRandomAccessFileMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + rafEntity = std::make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = std::make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = std::make_unique(std::move(rafEntity)); +} + +void FsRandomAccessFileMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_001 + * @tc.desc: Test function of ReadSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_001"; + + ArrayBuffer buffer(malloc(100), 100); + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->ReadSync(buffer, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_001"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_002 + * @tc.desc: Test function of ReadSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_002"; + + ArrayBuffer buffer(malloc(100), 100); + ReadOptions options; + options.offset = 10; + options.length = 10; + raf->rafEntity->filePointer = 20; + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->ReadSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_002"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_003 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_003"; + + std::string data = "test data"; + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(data, std::nullopt); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_003"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_004 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_004"; + + std::string data = "test data"; + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(data, options); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_004"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_005 + * @tc.desc: Test function of WriteSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_005"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), false); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_005"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_WriteSync_006 + * @tc.desc: Test function of WriteSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_006"; + + ArrayBuffer buffer(malloc(100), 100); + WriteOptions options; + options.length = 4; + options.offset = 0; + + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0)); + auto result = raf->WriteSync(buffer, options); + EXPECT_EQ(result.IsSuccess(), true); + free(buffer.buf); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_006"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_007 + * @tc.desc: Test function of CloseSync() interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_007"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(-1)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_007"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_CloseSync_008 + * @tc.desc: Test function of CloseSync() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_008"; + + EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(0)); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_008"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5930ef0144a0fbae3c967ca87ee40049746f4ec9 --- /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 + +#include "file_entity.h" +#include "fs_randomaccessfile.h" + +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_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..2de8331e085893f1b1c0b6c862446bbcaa369cda --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_mock_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 + +#include "fs_stat_entity.h" +#include "fs_stat.h" +#include "securec.h" +#include "system_mock.h" + +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 = 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"; + + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); + statEntity->fileInfo_->isPath = true; + int length = 100; + string testPath = "/test/stat_path"; + statEntity->fileInfo_->path = make_unique(length); + strncpy_s(statEntity->fileInfo_->path.get(), length, testPath.c_str(), testPath.size()); + statEntity->fileInfo_->path.get()[99] = '\0'; + fsStat = make_unique(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"; + + unique_ptr statEntity; + unique_ptr fsStat; + statEntity = make_unique(); + statEntity->fileInfo_ = make_unique(); + statEntity->fileInfo_->isPath = false; + const int fdValue = 3; //模拟fd为3 + const bool isClosed = false; + statEntity->fileInfo_->fdg = make_unique(fdValue, isClosed); + fsStat = make_unique(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..3e74ed320796418ad541d4df7170e2524f839433 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stat/fs_stat_test.cpp @@ -0,0 +1,471 @@ +/* + * 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_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e5c30f653bc63963254ed9520688e5b4963cc5a --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_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 +#include "c_mock.h" +#include "create_stream_core.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; + +class FsStreamMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + mock_ = std::make_shared(); + ICMock::ins = mock_; + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + ICMock::ins = nullptr; + mock_ = nullptr; + rmdir(g_streamFilePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; + + static inline std::shared_ptr mock_ = nullptr; +}; + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for fail ftell. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + auto retWr = result->Write("FsStreamWriteTest_0001", opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + string buf = "FsStreamWriteTest_0002"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), buf.length()), opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + ReadOptions opt; + opt.offset = 5; + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + + free(buffer); + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ba1451edcdc1a4338b74302188bab7585141eff --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -0,0 +1,30 @@ +/* +* Copyright (C) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "c_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { +int fseek(FILE *stream, long len, int offset) +{ + return ICMock::ins->fseek(stream, len, offset); +} + +long ftell(FILE *stream) +{ + return ICMock::ins->ftell(stream); +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..622d50fc9bf751d62dc6c4b480764c8d97b63530 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -0,0 +1,42 @@ +/* +* Copyright (C) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class ICMock { +public: + static inline std::shared_ptr ins = nullptr; +public: + virtual ~ICMock() = default; + virtual int fseek(FILE *, long, int) = 0; + virtual long ftell(FILE *) = 0; +}; + +class CMock : public ICMock { +public: + MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); + MOCK_METHOD(long, ftell, (FILE *), (override)); +}; + + +} // OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_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..b054b2a079458d6273498ffd057812b4299cca96 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/access_core_mock_test.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "access_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class AccessCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr uvMock = nullptr; + const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; +}; + +void AccessCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void AccessCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void AccessCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void AccessCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_001 + * @tc.desc: Test function of AccessCore::ValidAccessArgs interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-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) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_004"; +} + +/** + * @tc.name: AccessCoreMockTest_DoAccess_005 + * @tc.desc: Test function of AccessCore::DoAccess interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreMockTest, AccessCoreMockTest_DoAccess_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreMockTest-begin AccessCoreMockTest_DoAccess_005"; + + std::string path = DISTRIBUTED_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0)); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreMockTest-end AccessCoreMockTest_DoAccess_005"; +} + +} // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/access_core_test.cpp index 913df19e9f30cf248cb1775e6d0ec0a18f930101..6460d9a657d69bc56bb344028401b8c1b68c5904 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,12 +17,14 @@ #include "access_core.h" #include -#include +#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; using namespace std; +const mode_t DIR_PERMISSIONS = 0755; class AccessCoreTest : public testing::Test { public: @@ -30,6 +32,11 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + const string CLOUDDISK_FILE_PREFIX = "/data/storage/el2/cloud"; + const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles"; + const string CLOUD_FILE_LOCATION = "user.cloud.location"; + const string POSITION_LOCAL = "1"; + const string POSITION_BOTH = "2"; }; void AccessCoreTest::SetUpTestCase(void) @@ -52,16 +59,47 @@ void AccessCoreTest::TearDown(void) GTEST_LOG_(INFO) << "TearDown"; } +// 递归创建多级目录的辅助函数 +bool CreateDirectoryRecursive(const std::string& path) { + if (path.empty()) { + return false; + } + + size_t pos = 0; + std::string dir; + if (path[0] == '/') { + dir += '/'; + pos++; + } + + while ((pos = path.find('/', pos)) != std::string::npos) { + dir = path.substr(0, pos++); + if (dir.empty()) { + continue; + } + if (mkdir(dir.c_str(), DIR_PERMISSIONS) == -1) { + if (errno != EEXIST) { + return false; + } + } + } + + if (mkdir(path.c_str(), DIR_PERMISSIONS) == -1 && errno != EEXIST) { + return false; + } + return true; +} + /** * @tc.name: AccessCoreTest_DoAccess_001 - * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_001"; std::string path; std::optional mode; @@ -69,48 +107,48 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Lev auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_001"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_001"; } /** * @tc.name: AccessCoreTest_DoAccess_002 - * @tc.desc: Test function of AccessCore::ValidAccessArgs interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_002"; - std::string path = "TEST"; - std::optional mode; + std::string path = ""; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = DEFAULT_FLAG; - auto res = AccessCore::DoAccess(path, mode); - EXPECT_EQ(res.IsSuccess(), true); + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_002"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_002"; } /** * @tc.name: AccessCoreTest_DoAccess_003 - * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_003"; - std::string path = ""; - AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = DEFAULT_FLAG; + std::string path = "test"; + std::optional mode = std::make_optional(AccessModeType::ERROR); - auto res = AccessCore::DoAccess(path, mode, flag); + auto res = AccessCore::DoAccess(path, mode); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_003"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_003"; } /** @@ -122,17 +160,87 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Lev */ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_004, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin AccessCoreTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_004"; - std::string path = "TEST"; + std::string path = CLOUDDISK_FILE_PREFIX; AccessModeType mode = AccessModeType::EXIST; - AccessFlag flag = DEFAULT_FLAG; + AccessFlag flag = LOCAL_FLAG; auto res = AccessCore::DoAccess(path, mode, flag); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_004"; + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_004"; } +/** + * @tc.name: AccessCoreTest_DoAccess_005 + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_005"; + + std::string path = CLOUDDISK_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + ASSERT_TRUE(CreateDirectoryRecursive(path)); + auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_LOCAL.c_str(), POSITION_LOCAL.size(), 0); + ASSERT_NE(re, -1); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_005"; +} + +/** + * @tc.name: AccessCoreTest_DoAccess_006 + * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_006"; + + std::string path = "AccessCoreTest"; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_006"; +} + +/** + * @tc.name: AccessCoreTest_DoAccess_007 + * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_007"; + + std::string path = CLOUDDISK_FILE_PREFIX; + AccessModeType mode = AccessModeType::EXIST; + AccessFlag flag = LOCAL_FLAG; + + ASSERT_TRUE(CreateDirectoryRecursive(path)); + auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_BOTH.c_str(), POSITION_BOTH.size(), 0); + ASSERT_NE(re, -1); + + auto res = AccessCore::DoAccess(path, mode, flag); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_007"; +} } // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad9b034bbe2f62190612326f63bccdd83cae8cb6 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_dir_core_test.cpp @@ -0,0 +1,295 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "copy_dir_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyDirCoreTest : public testing::Test { +public: + static filesystem::path g_srcPath; + static filesystem::path g_destPath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + void CreateTestFile(const filesystem::path& path, const string& content = "test") + { + ofstream file(path); + file << content; + } +}; + +filesystem::path CopyDirCoreTest::g_srcPath; +filesystem::path CopyDirCoreTest::g_destPath; + +void CopyDirCoreTest::SetUpTestCase(void) +{ + g_srcPath = filesystem::temp_directory_path() / "src/"; + g_destPath = filesystem::temp_directory_path() / "dest/"; + filesystem::create_directory(g_srcPath); + filesystem::create_directory(g_destPath); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyDirCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove_all(g_srcPath); + filesystem::remove_all(g_destPath); +} + +void CopyDirCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyDirCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_001 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with empty directory. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_001"; + + string src = g_srcPath.string() + "/test01"; + string dest = g_destPath.string(); + filesystem::create_directories(src); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_001"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_002 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid mode. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_002"; + + string src = g_srcPath.string() + "/test02"; + string dest = g_destPath.string(); + filesystem::create_directories(src); + + int invalidMode = COPYMODE_MAX + 1; + auto result = CopyDirCore::DoCopyDir(src, dest, optional(invalidMode)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_002"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_003 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with non-existent source. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_003"; + + string src = g_srcPath.string() + "/non_existent"; + string dest = g_destPath.string(); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_003"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_004 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid destination. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_004"; + + string src = g_srcPath.string(); + string dest = g_destPath.string() + "/invalid_file.txt"; + filesystem::path(dest).remove_filename(); + filesystem::create_directories(filesystem::path(dest).parent_path()); + ofstream(dest).close(); // 创建文件而非目录 + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_004"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_005 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with same source and destination. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_005"; + + string src = g_srcPath.string(); + string dest = g_srcPath.string(); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_005"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_006 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with files. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_006"; + + string src = g_srcPath.string() + "/test06"; + string dest = g_destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + CreateTestFile(src + "/file2.txt", "content2"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_006"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_007 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with subdirectories. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_007"; + + string src = g_srcPath.string() + "/test07"; + string dest = g_destPath.string(); + filesystem::create_directories(src + "/subdir1"); + filesystem::create_directories(src + "/subdir2"); + CreateTestFile(src + "/subdir1/file1.txt", "sub1_content1"); + CreateTestFile(src + "/subdir2/file2.txt", "sub2_content2"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional()); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_007"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_008 + * @tc.desc: Test function of DoCopyDir() interface for FAILED with existing files (throw error mode). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_008"; + + string src = g_srcPath.string() + "/test08"; + string dest = g_destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + + string destDir = dest + "/" + filesystem::path(src).filename().string(); + filesystem::create_directories(destDir); + CreateTestFile(destDir + "/file1.txt", "existing_content"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional(DIRMODE_FILE_COPY_THROW_ERR)); + + EXPECT_FALSE(result.fsResult.IsSuccess()); + EXPECT_TRUE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_008"; +} + +/** + * @tc.name: CopyDirCoreTest_DoCopyDir_009 + * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with existing files (overwrite mode). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_009"; + + string src = g_srcPath.string() + "/test09"; + string dest = g_destPath.string(); + filesystem::create_directories(src); + CreateTestFile(src + "/file1.txt", "content1"); + + string destDir = dest + "/" + filesystem::path(src).filename().string(); + filesystem::create_directories(destDir); + CreateTestFile(destDir + "/file1.txt", "existing_content"); + + auto result = CopyDirCore::DoCopyDir(src, dest, optional(DIRMODE_FILE_COPY_REPLACE)); + + EXPECT_TRUE(result.fsResult.IsSuccess()); + EXPECT_FALSE(result.errFiles.has_value()); + + GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_009"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/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..2dc12961ce1d952a291f369853c8b23bc59061a4 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "create_randomaccessfile_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CreateRandomAccessFileCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void CreateRandomAccessFileCoreMockTest::SetUpTestCase(void) +{ + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CreateRandomAccessFileCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CreateRandomAccessFileCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CreateRandomAccessFileCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = 0; + optional options = nullopt; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreMockTest, CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 0; + opts.end = 100; + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(0)); + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreMockTest_DoCreateRandomAccessFile_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..848d976725b378dcd71c4026ead21630ec8967bb --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_test.cpp @@ -0,0 +1,189 @@ +/* + * 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_randomaccessfile_core.h" + +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 to verify an invalid mode. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; + + string path = "/test/path.txt"; + int32_t mode = -5; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = -1; + opts.end = 100; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; + + string path = "/test/path.txt"; + int32_t mode = 0; + RandomAccessFileOptions opts; + opts.start = 10; + opts.end = -1; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; + + int fd = -1; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), false); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004"; +} + +/**' + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify the path is invalid. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; + + string path = ""; + int32_t mode = 0; + optional options = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005"; +} + +/** + * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006 + * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; + + int fd = 3; + optional opts = nullopt; + + auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..826208e3190e33ca446b0af2785d8f443a5465df --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/dup_core_mock_test.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dup_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class DupCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void DupCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void DupCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvMock = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void DupCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void DupCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: DupCoreMockTest_DoDup_001 + * @tc.desc: Test function of DupCore::DoDup interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(DupCoreMockTest, DupCoreMockTest_DoDup_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DupCoreMockTest-begin DupCoreMockTest_DoDup_001"; + + int32_t fd = 1; + + EXPECT_CALL(*uvMock, uv_fs_readlink(_, _, _, _)).WillOnce(Return(-1)); + auto res = DupCore::DoDup(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "DupCoreMockTest-end DupCoreMockTest_DoDup_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp index 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/lstat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..691f2b3deefaf877c841422a22a0b2c2a4c1eb8b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_mock_test.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "lstat_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LstatCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void LstatCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = make_shared(); + Uvfs::ins = uvMock; +} + +void LstatCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void LstatCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LstatCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LstatCoreMockTest_DoLstat_001 + * @tc.desc: Test function of LstatCore::DoLstat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreMockTest, LstatCoreMockTest_DoLstat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreMockTest-begin LstatCoreMockTest_DoLstat_001"; + + EXPECT_CALL(*uvMock, uv_fs_lstat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = LstatCore::DoLstat("/data/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LstatCoreMockTest-end LstatCoreMockTest_DoLstat_001"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c40e99f2f7b26816ef2635c08645f330a84b464c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lstat_core_test.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "lstat_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LstatCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LstatCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open("/data/test/lstat.txt", CREATE | O_RDWR, 0644); + if (fd <= 0) { + ASSERT_TRUE(false); + } + close(fd); +} + +void LstatCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + rmdir("/data/test/lstat.txt"); +} + +void LstatCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LstatCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LstatCoreTest_DoLstat_001 + * @tc.desc: Test function of LstatCore::DoLstat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreTest, LstatCoreTest_DoLstat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreTest-begin LstatCoreTest_DoLstat_001"; + + auto res = LstatCore::DoLstat("/invalid/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), false); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "LstatCoreTest-end LstatCoreTest_DoLstat_001"; +} + +/** + * @tc.name: LstatCoreTest_DoLstat_002 + * @tc.desc: Test function of LstatCore::DoLstat interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LstatCoreTest, LstatCoreTest_DoLstat_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LstatCoreTest-begin LstatCoreTest_DoLstat_002"; + + auto res = LstatCore::DoLstat("/data/test/lstat.txt"); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "LstatCoreTest-end LstatCoreTest_DoLstat_002"; +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5878a93046576b034577b1d1431decb895cdb2dc --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "system_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { +int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) +{ + return System::ins->setxattr(path, name, value, size, flags); +} + +int getxattr(const char *path, const char *name, void *value, size_t size) +{ + return System::ins->getxattr(path, name, value, size); +} + +int fgetxattr(int filedes, const char *name, void *value, size_t size) +{ + return System::ins->fgetxattr(filedes, name, value, size); +} + +int flock(int fd, int operation) +{ + return System::ins->flock(fd, operation); +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..3ff74be78027d70a93550e1fbb0a4d59a0331885 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/system_mock.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class System { +public: + static inline std::shared_ptr ins = nullptr; + +public: + virtual ~System() = default; + virtual int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) = 0; + virtual int getxattr(const char *path, const char *name, void *value, size_t size) = 0; + virtual int fgetxattr(int filedes, const char *name, void *value, size_t size) = 0; + virtual int flock(int fd, int operation) = 0; +}; + +class SystemMock : public System { +public: + MOCK_METHOD5(setxattr, int(const char *path, const char *name, const void *value, size_t size, int flags)); + MOCK_METHOD4(getxattr, int(const char *path, const char *name, void *value, size_t size)); + MOCK_METHOD4(fgetxattr, int(int filedes, const char *name, void *value, size_t size)); + MOCK_METHOD2(flock, int(int fd, int operation)); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_SYSTEM_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index 13ceb7b5405e3cca678d7b6a7e93f5a8b23c6cb1..b656a1ad103d4e47cc0346e49b7d1fc1e074035c 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp @@ -13,12 +13,14 @@ * limitations under the License. */ + #include "uv_fs_mock.h" + using namespace OHOS::FileManagement::ModuleFileIO; -int uv_fs_read( - uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) +int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_read(loop, req, file, bufs, nbufs, off, cb); } @@ -53,9 +55,9 @@ int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_rmdir(loop, req, path, cb); } -int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) +int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, uv_fs_cb cb) { - return Uvfs::ins->uv_fs_symlink(loop, req, path, newPath, flags, cb); + return Uvfs::ins->uv_fs_symlink(loop, req, path, new_path, flags, cb); } int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) @@ -68,12 +70,22 @@ int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, u return Uvfs::ins->uv_fs_ftruncate(loop, req, fd, offset, cb); } -int uv_fs_write( - uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) +int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, + uv_fs_cb cb) { return Uvfs::ins->uv_fs_write(loop, req, fd, bufs, nbufs, offset, cb); } +int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_realpath(loop, req, path, cb); +} + +int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_close(loop, req, file, cb); +} + int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) { return Uvfs::ins->uv_fs_fdatasync(loop, req, file, cb); @@ -99,4 +111,27 @@ int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) return Uvfs::ins->uv_fs_unlink(loop, req, path, cb); } -void uv_fs_req_cleanup(uv_fs_t *req) {} +void uv_fs_req_cleanup(uv_fs_t *req) +{ + return; +} + +int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_rename(loop, req, path, newPath, cb); +} + +int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_fsync(loop, req, file, cb); +} + +int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); +} + +int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) +{ + return Uvfs::ins->uv_fs_lstat(loop, req, path, cb); +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h index 6b0f3c5fdebacac71bd14803b4b7cc9691584e1a..ed0a9e9c4158b51629e026ac66290a5c643e1991 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h +++ b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.h @@ -13,10 +13,10 @@ * limitations under the License. */ -#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H -#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H +#ifndef UV_FS_MOCK_H +#define UV_FS_MOCK_H -#include "read_core.h" +#include "uv.h" #include @@ -25,54 +25,67 @@ namespace OHOS::FileManagement::ModuleFileIO { class Uvfs { public: static inline std::shared_ptr ins = nullptr; - public: virtual ~Uvfs() = default; virtual int uv_fs_read(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t off, uv_fs_cb cb) = 0; virtual int uv_fs_readlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; virtual int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_utime( - uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb) = 0; + virtual int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, + double mtime, uv_fs_cb cb) = 0; virtual int uv_fs_scandir(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_scandir_next(uv_fs_t *req, uv_dirent_t *ent) = 0; - virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; - virtual int uv_fs_symlink( - uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb) = 0; + virtual int uv_fs_rmdir(uv_loop_t *loop, uv_fs_t *req, const char* path, uv_fs_cb cb) = 0; + virtual int uv_fs_symlink(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + uv_fs_cb cb) = 0; virtual int uv_fs_open(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_ftruncate(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb) = 0; virtual int uv_fs_write(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, uv_fs_cb cb) = 0; + virtual int uv_fs_realpath(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_close(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_fdatasync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_mkdir(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb) = 0; virtual int uv_fs_access(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb) = 0; virtual int uv_fs_mkdtemp(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb) = 0; virtual int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_rename(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb) = 0; + virtual int uv_fs_lstat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb) = 0; + virtual int uv_fs_fsync(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb) = 0; + virtual int uv_fs_sendfile(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, size_t len, + uv_fs_cb cb) = 0; }; class UvfsMock : public Uvfs { public: MOCK_METHOD7(uv_fs_read, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, const uv_buf_t bufs[], unsigned int nbufs, - int64_t off, uv_fs_cb cb)); + int64_t off, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_readlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_stat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6( - uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_utime, int(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, + uv_fs_cb cb)); MOCK_METHOD5(uv_fs_scandir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD2(uv_fs_scandir_next, int(uv_fs_t *req, uv_dirent_t *ent)); MOCK_METHOD4(uv_fs_rmdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); - MOCK_METHOD6(uv_fs_symlink, - int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, int flags, uv_fs_cb cb)); + MOCK_METHOD6(uv_fs_symlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *new_path, int flags, + uv_fs_cb cb)); MOCK_METHOD6(uv_fs_open, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_ftruncate, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, int64_t offset, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_write, int(uv_loop_t *loop, uv_fs_t *req, uv_file fd, const uv_buf_t bufs[], unsigned int nbufs, - int64_t offset, uv_fs_cb cb)); + int64_t offset, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_realpath, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_close, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_fdatasync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_mkdir, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int mode, uv_fs_cb cb)); MOCK_METHOD5(uv_fs_access, int(uv_loop_t *loop, uv_fs_t *req, const char *path, int flags, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_mkdtemp, int(uv_loop_t *loop, uv_fs_t *req, const char *tpl, uv_fs_cb cb)); MOCK_METHOD4(uv_fs_unlink, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD5(uv_fs_rename, int(uv_loop_t *loop, uv_fs_t *req, const char *path, const char *newPath, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_lstat, int(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)); + MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t *loop, uv_fs_t *req, uv_file file, uv_fs_cb cb)); + MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t *loop, uv_fs_t *req, uv_file outFd, uv_file inFd, int64_t off, + size_t len, uv_fs_cb cb)); }; -} // namespace OHOS::FileManagement::ModuleFileIO -#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_PROPERTIES_MOCK_UV_FS_MOCK_H \ No newline at end of file +} // OHOS::FileManagement::ModuleFileIO +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/open_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33f25f3cdd2782d35a5bd416a935aa96cc11aea2 --- /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 "mock/uv_fs_mock.h" +#include "open_core.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/open_test.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/open_test.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/open_test.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/open_test.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..a6cad138a535dfdc7f4779fc5991572ea60e2f8f --- /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/open_test.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/open_test.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/open_test.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/open_test.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/open_test.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/open_test.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/dir_test"; + 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..b6aba53160df95a361589c93ae19302ec7f8ddbc --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/read_core_mock_test.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "read_core.h" +#include "uv_fs_mock.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class ReadCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr uvMock = nullptr; +}; + +void ReadCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void ReadCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void ReadCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ReadCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadCoreMockTest_DoRead_001 + * @tc.desc: Test function of ReadCore::DoRead interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_001"; + + int32_t fd = 1; + void *buf = nullptr; + ArrayBuffer arrayBuffer(buf, 0); + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + + auto res = ReadCore::DoRead(fd, arrayBuffer); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_001"; +} + +/** + * @tc.name: ReadCoreMockTest_DoRead_002 + * @tc.desc: Test function of ReadCore::DoRead interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_002"; + + int32_t fd = 1; + void *buf = nullptr; + ArrayBuffer arrayBuffer(buf, 0); + + EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1)); + + auto res = ReadCore::DoRead(fd, arrayBuffer); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp index c4dfda62e57d9df66484565a9b9c19f96adeec8c..8ecb653c52f1095f48bc6035421420dcc5ffad99 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp @@ -14,10 +14,11 @@ */ #include "read_core.h" -#include "mock/uv_fs_mock.h" +#include +#include #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -61,7 +62,7 @@ void ReadCoreTest::TearDown(void) */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_001"; + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_001"; int32_t fd = -1; void *buf = nullptr; @@ -70,31 +71,28 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_001, testing::ext::TestSize.Level1) auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_001"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_001"; } /** * @tc.name: ReadCoreTest_DoRead_002 - * @tc.desc: Test function of ReadCore::DoRead interface for FALSE. + * @tc.desc: Test function of ReadCore::DoRead interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_002"; - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_002"; int32_t fd = 1; void *buf = nullptr; - ArrayBuffer arrayBuffer(buf, 0); + ArrayBuffer arrayBuffer(buf, 0xffffffff + 1); + auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_002"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_002"; } /** @@ -106,19 +104,18 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_002, testing::ext::TestSize.Level1) */ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_003, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "NClassTest-begin ReadCoreTest_DoRead_005"; - - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1)); + GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_003"; int32_t fd = 1; void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0); - auto res = ReadCore::DoRead(fd, arrayBuffer); - EXPECT_EQ(res.IsSuccess(), true); + optional options = std::make_optional(); + options->offset = std::make_optional(-1); + + auto res = ReadCore::DoRead(fd, arrayBuffer, options); + EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "NClassTest-end ReadCoreTest_DoRead_003"; + GTEST_LOG_(INFO) << "ReadCoreTest-end ReadCoreTest_DoRead_003"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp index e6d30f2dc4720174e215d65797ff5866cdee77c0..0cbdf7ebd35e4e003d3c69cbd68631cc53a3e2e0 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/rmdir_core_test.cpp @@ -14,10 +14,11 @@ */ #include "rmdir_core.h" -#include "mock/uv_fs_mock.h" +#include +#include #include -#include + namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -70,4 +71,98 @@ HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_001, testing::ext::TestSize.Lev GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_001"; } +/** + * @tc.name: RmdirCoreTest_DoRmdirent_002 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_002"; + std::string fpath = "invalid?path"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_002"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_003 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_003"; + std::string fpath = "/dir"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_003"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_004 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_004"; + + std::filesystem::create_directories("test_dir"); + std::ofstream("test_dir/test_file.txt") << "test"; + + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_write | std::filesystem::perms::owner_exec, + std::filesystem::perm_options::replace); + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_004"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_005 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_005"; + + std::filesystem::create_directories("test_dir"); + std::ofstream("test_dir/test_file.txt") << "test"; + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_005"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp similarity index 57% rename from interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp rename to interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp index 2910fcb9e1eb3ae036fdc3776247901a3aa2e64f..dd6e4b43b79db6a533e5d3c026777373580cc4cd 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/symlink_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/symlink_core_mock_test.cpp @@ -15,90 +15,90 @@ #include "symlink_core.h" -#include "mock/uv_fs_mock.h" +#include "uv_fs_mock.h" #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; using namespace std; -class SymlinkCoreTest : public testing::Test { +class SymlinkCoreMockTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); + static inline shared_ptr uvMock = nullptr; }; -void SymlinkCoreTest::SetUpTestCase(void) +void SymlinkCoreMockTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; } -void SymlinkCoreTest::TearDownTestCase(void) +void SymlinkCoreMockTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; } -void SymlinkCoreTest::SetUp(void) +void SymlinkCoreMockTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; } -void SymlinkCoreTest::TearDown(void) +void SymlinkCoreMockTest::TearDown(void) { GTEST_LOG_(INFO) << "TearDown"; } /** - * @tc.name: SymlinkCoreTest_DoSymlink_001 + * @tc.name: SymlinkCoreMockTest_DoSymlink_001 * @tc.desc: Test function of SymlinkCore::DoSymlink interface for FALSE. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(SymlinkCoreTest, SymlinkCoreTest_DoSymlink_001, testing::ext::TestSize.Level1) +HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreTest_DoSymlink_001"; + GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreMockTest_DoSymlink_001"; string target; string srcPath; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(-1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreTest_DoSymlink_001"; + GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_001"; } /** - * @tc.name: SymlinkCoreTest_DoSymlink_002 + * @tc.name: SymlinkCoreMockTest_DoSymlink_002 * @tc.desc: Test function of SymlinkCore::DoSymlink interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(SymlinkCoreTest, SymlinkCoreTest_DoSymlink_002, testing::ext::TestSize.Level1) +HWTEST_F(SymlinkCoreMockTest, SymlinkCoreMockTest_DoSymlink_002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreTest_DoSymlink_002"; + GTEST_LOG_(INFO) << "SymlinkCore-begin SymlinkCoreMockTest_DoSymlink_002"; string target; string srcPath; - std::shared_ptr uv = std::make_shared(); - Uvfs::ins = uv; - EXPECT_CALL(*uv, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_symlink(_, _, _, _, _, _)).WillOnce(Return(1)); auto res = SymlinkCore::DoSymlink(target, srcPath); EXPECT_EQ(res.IsSuccess(), true); - GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreTest_DoSymlink_002"; + GTEST_LOG_(INFO) << "SymlinkCore-end SymlinkCoreMockTest_DoSymlink_002"; } } // OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8e459014b266b582a592deb6285028e2067833c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_mock_test.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "truncate_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class TruncateCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void TruncateCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void TruncateCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void TruncateCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TruncateCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_001 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_001"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_001"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_002 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_002"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_002"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_003 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_003"; + + FileInfo fileInfo; + fileInfo.isPath = true; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_003"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_004 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_004"; + + FileInfo fileInfo; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_004"; +} + +/** + * @tc.name: TruncateCoreMockTest_DoTruncate_005 + * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_005"; + + FileInfo fileInfo; + fileInfo.fdg = std::make_unique(1); + + EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1)); + auto res = TruncateCore::DoTruncate(fileInfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_005"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/truncate_core_test.cpp index 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/utimes_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7d7e60c22b290332f8cbeac98b4b4bd3dce42df --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_mock_test.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utimes_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class UtimesCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void UtimesCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void UtimesCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void UtimesCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void UtimesCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_001 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_001"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_001"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_002 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_002"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(-1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_002"; +} + +/** + * @tc.name: UtimesCoreMockTest_DoUtimes_003 + * @tc.desc: Test function of UtimesCore::DoUtimes interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_003"; + + string path; + double mtime = 1; + + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(1)); + auto res = UtimesCore::DoUtimes(path, mtime); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/utimes_core_test.cpp index 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_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_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..069a3230e9d756b9f4a145bbd6e918432ddfa557 --- /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" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static const string g_filePath = "/data/test/HashCoreTest.txt"; +class HashCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(g_filePath.c_str()); + }; + 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(g_filePath, 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(g_filePath, "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(g_filePath, "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(g_filePath, "sha256"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004"; +} + +/** + * @tc.name: DoHashTest_0005 + * @tc.desc: Test function of DoHash() interface for no such file or directory. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0005"; + auto ret = HashCore::DoHash("/data/local/tmp/azuxyicayhyskjeh", "sha256"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0005"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f320af0371ac30c73381a072a2a449b71ebe3a99 --- /dev/null +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -0,0 +1,144 @@ +/* + * 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" + +namespace OHOS { +namespace FileManagement { +namespace ModuleSecurityLabel { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +static const string g_filePath = "/data/test/SecurityLabelCoreTest.txt"; +static const string g_validFilePath = "/data/test/validFilePath"; + +class SecurityLabelCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(g_filePath.c_str()); + }; + 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(g_filePath, "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 DoSetSecurityLabel_0002"; + auto ret = DoSetSecurityLabel(g_validFilePath, "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(g_filePath, "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(g_validFilePath); + 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(g_filePath); + EXPECT_TRUE(ret.IsSuccess()); + + const string level = ret.GetData().value(); + EXPECT_EQ(level, "s2"); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0002"; +} + +} // namespace ModuleSecurityLabel +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file 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..82316d884a2a557cb956332ee3eba59575bc70d0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_statvfs/statvfs_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 "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); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + 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); + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + 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); + auto err = result.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_004"; +} + +} \ No newline at end of file