diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..d1a29d8ec514fddcc0b2745b0c4509f38f1fa51c --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h @@ -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. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_ANI_RANDOMACCESSFILE_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_ANI_RANDOMACCESSFILE_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class RandomAccessFileAni final { +public: + static void SetFilePointer(ani_env *env, [[maybe_unused]] ani_object object, ani_double fp); + static void Close(ani_env *env, [[maybe_unused]] ani_object object); + static ani_double WriteSync(ani_env *env, [[maybe_unused]] ani_object object, ani_object buf, ani_object options); + static ani_double ReadSync( + ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buf, ani_object options); + static ani_object GetReadStream(ani_env *env, [[maybe_unused]] ani_object object); + static ani_object GetWriteStream(ani_env *env, [[maybe_unused]] ani_object object); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_ANI_RANDOMACCESSFILE_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d8b149f62ab2ec10d5ee9ee42d29d0823f99fd7 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp @@ -0,0 +1,287 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "fs_randomaccessfile.h" + +#include + +#include "file_uri.h" +#include "file_utils.h" +#include "filemgmt_libfs.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static int DoReadRAF(void* buf, size_t len, int fd, int64_t offset) +{ + unique_ptr readReq = { + new (nothrow) uv_fs_t, FsUtils::FsReqCleanup }; + if (readReq == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + uv_buf_t iov = uv_buf_init(static_cast(buf), len); + int ret = uv_fs_read(nullptr, readReq.get(), fd, &iov, 1, offset, nullptr); + return ret; +} + +static int DoWriteRAF(void* buf, size_t len, int fd, int64_t offset) +{ + unique_ptr writeReq = { + new (nothrow) uv_fs_t, FsUtils::FsReqCleanup }; + if (writeReq == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + uv_buf_t iov = uv_buf_init(static_cast(buf), len); + int ret = uv_fs_write(nullptr, writeReq.get(), fd, &iov, 1, offset, nullptr); + return ret; +} + +FsResult FsRandomAccessFile::GetFD() const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + return FsResult::Success(rafEntity->fd.get()->GetFD()); +} + +FsResult FsRandomAccessFile::GetFPointer() const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + return FsResult::Success(rafEntity->filePointer); +} + +FsResult FsRandomAccessFile::SetFilePointerSync(const int64_t &fp) const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + rafEntity->filePointer = fp; + return FsResult::Success(); +} + +static int64_t CalculateOffset(int64_t offset, int64_t fPointer) +{ + if (offset < 0) { + HILOGD("No specified offset provided"); + offset = fPointer; + } else { + offset += fPointer; + } + return offset; +} + +tuple ValidReadArg(ArrayBuffer &buffer, const optional &options) +{ + size_t retLen = 0; + int64_t offset = -1; + bool succ = false; + void *buf = buffer.buf; + size_t bufLen = buffer.length; + + if (bufLen > UINT_MAX) { + HILOGE("Invalid arraybuffer"); + return { false, nullptr, retLen, offset }; + } + optional lengthOp = nullopt; + optional offsetOp = nullopt; + if (options.has_value()) { + ReadOptions op = options.value(); + lengthOp = op.length; + offsetOp = op.offset; + } + tie(succ, retLen) = FsUtils::GetActualLen(bufLen, 0, lengthOp); + if (!succ) { + HILOGE("Failed to get actual length"); + return { false, nullptr, retLen, offset }; + } + if (offsetOp.has_value()) { + offset = offsetOp.value(); + if (offset < 0) { + HILOGE("option.offset shall be positive number"); + return { false, nullptr, retLen, offset }; + } + } + return { true, buf, retLen, offset }; +} + +FsResult FsRandomAccessFile::ReadSync(ArrayBuffer &buffer, const optional &options) const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + auto [succ, buf, len, offset] = ValidReadArg(buffer, options); + if (!succ) { + HILOGE("Invalid buffer/options"); + return FsResult::Error(EINVAL); + } + offset = CalculateOffset(offset, rafEntity->filePointer); + int actLen = DoReadRAF(buf, len, rafEntity->fd.get()->GetFD(), offset); + if (actLen < 0) { + HILOGE("Failed to read file for %{private}d", actLen); + return FsResult::Error(actLen); + } + rafEntity->filePointer = offset + actLen; + return FsResult::Success(static_cast(actLen)); +} + +tuple ValidWriteArg( + void *buffer, const size_t bufLen, const optional &options) +{ + size_t retLen = 0; + int64_t offset = -1; + bool succ = false; + + if (bufLen > UINT_MAX) { + HILOGE("The Size of buffer is too large"); + return { false, nullptr, 0, offset }; + } + + optional lengthOp = nullopt; + optional offsetOp = nullopt; + if (options.has_value()) { + WriteOptions op = options.value(); + lengthOp = op.length; + offsetOp = op.offset; + } + + tie(succ, retLen) = FsUtils::GetActualLen(bufLen, 0, lengthOp); + if (!succ) { + HILOGE("Failed to get actual length"); + return { false, nullptr, 0, offset }; + } + + if (offsetOp.has_value()) { + offset = offsetOp.value(); + if (offset < 0) { + HILOGE("option.offset shall be positive number"); + return { false, nullptr, 0, offset }; + } + } + return { true, buffer, retLen, offset }; +} + +FsResult FsRandomAccessFile::WriteSync(const string &buffer, const optional &options) const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + + bool succ = false; + size_t len = 0; + int64_t offset = -1; + void *buf = nullptr; + size_t bufLen = static_cast(buffer.length()); + + tie(succ, buf, len, offset) = ValidWriteArg(const_cast(static_cast(buffer.c_str())), + bufLen, options); + if (!succ) { + HILOGE("Invalid buffer/options"); + return FsResult::Error(EINVAL); + } + offset = CalculateOffset(offset, rafEntity->filePointer); + int writeLen = DoWriteRAF(buf, len, rafEntity->fd.get()->GetFD(), offset); + if (writeLen < 0) { + return FsResult::Error(writeLen); + } + rafEntity->filePointer = offset + writeLen; + return FsResult::Success(static_cast(writeLen)); +} + +FsResult FsRandomAccessFile::WriteSync(const ArrayBuffer &buffer, const optional &options) const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + + bool succ = false; + size_t len = 0; + int64_t offset = -1; + void *buf = nullptr; + + tie(succ, buf, len, offset) = ValidWriteArg(buffer.buf, buffer.length, options); + if (!succ) { + HILOGE("Invalid buffer/options"); + return FsResult::Error(EINVAL); + } + offset = CalculateOffset(offset, rafEntity->filePointer); + int writeLen = DoWriteRAF(buf, len, rafEntity->fd.get()->GetFD(), offset); + if (writeLen < 0) { + return FsResult::Error(writeLen); + } + rafEntity->filePointer = offset + writeLen; + return FsResult::Success(static_cast(writeLen)); +} + +static int CloseFd(int fd) +{ + unique_ptr closeReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!closeReq) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int ret = uv_fs_close(nullptr, closeReq.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to close file with ret: %{private}d", ret); + return ret; + } + return ERRNO_NOERR; +} + +FsResult FsRandomAccessFile::CloseSync() const +{ + if (!rafEntity) { + HILOGE("Failed to get entity of RandomAccessFile"); + return FsResult::Error(EIO); + } + auto err = CloseFd(rafEntity->fd.get()->GetFD()); + if (err) { + return FsResult::Error(err); + } + return FsResult::Success(); +} + +FsResult FsRandomAccessFile::Constructor() +{ + auto rafEntity = CreateUniquePtr(); + if (rafEntity == nullptr) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + FsRandomAccessFile *randomAccessFilePtr = new FsRandomAccessFile(move(rafEntity)); + if (randomAccessFilePtr == nullptr) { + HILOGE("INNER BUG. Failed to wrap entity for obj RandomAccessFile"); + return FsResult::Error(EIO); + } + return FsResult::Success(move(randomAccessFilePtr)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h new file mode 100644 index 0000000000000000000000000000000000000000..024cf25b7db7d2bfdfaae1b591abf1a6c4bd8664 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h @@ -0,0 +1,84 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H + +#include "randomaccessfile_entity.h" + +#include "filemgmt_libfs.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +struct WriteOptions { + optional length = nullopt; + optional offset = nullopt; + optional encoding = nullopt; +}; + +struct ReadOptions { + optional offset = nullopt; + optional length = nullopt; +}; + +class FsRandomAccessFile { +public: + inline static const string className_ = "RandomAccessFile"; + + RandomAccessFileEntity *GetRAFEntity() const + { + return rafEntity.get(); + } + + FsRandomAccessFile(const FsRandomAccessFile &other) = delete; + FsRandomAccessFile &operator=(const FsRandomAccessFile &other) = delete; + + FsRandomAccessFile(FsRandomAccessFile &&other) noexcept : rafEntity(move(other.rafEntity)) + { + other.rafEntity = nullptr; + } + + FsRandomAccessFile &operator=(FsRandomAccessFile &&other) noexcept + { + if (this != &other) { + rafEntity = move(other.rafEntity); + other.rafEntity = nullptr; + } + return *this; + } + + ~FsRandomAccessFile() = default; + + FsResult SetFilePointerSync(const int64_t &fp) const; + FsResult WriteSync(const string &buffer, const optional &options = nullopt) const; + FsResult WriteSync(const ArrayBuffer &buffer, const optional &options = nullopt) const; + FsResult ReadSync(ArrayBuffer &buffer, const optional &options = nullopt) const; + FsResult CloseSync() const; + FsResult GetFD() const; + FsResult GetFPointer() const; + + static FsResult Constructor(); + +private: + unique_ptr rafEntity; + explicit FsRandomAccessFile(unique_ptr entity) : rafEntity(move(entity)) {} +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif //INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h index 3f5b12a7f268a3781cbc5031adb9c3c11591d066..6d80bebec09effa4bda3e0ab6f2178df139e9b89 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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 @@ -16,17 +16,21 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H +#include +#include #include #include "fd_guard.h" -#include "n_val.h" +#include "filemgmt_libhilog.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { +using namespace std; + const int64_t INVALID_POS = -1; struct RandomAccessFileEntity { - std::unique_ptr fd = {nullptr}; + unique_ptr fd = {nullptr}; int64_t filePointer = 0; int64_t start = INVALID_POS; int64_t end = INVALID_POS; @@ -34,4 +38,4 @@ struct RandomAccessFileEntity { } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -#endif \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17fe296a3fc95112d2b9d1c8441e8cb45ea05f7b --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "create_randomaccessfile_ani.h" + +#include "ani_helper.h" +#include "ani_signature.h" +#include "create_randomaccessfile_core.h" +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; + +static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) +{ + if (rafFile == nullptr) { + HILOGE("FsRandomAccessFile pointer is null!"); + return nullptr; + } + + auto classDesc = FS::RandomAccessFileInner::classDesc.c_str(); + ani_class cls; + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); + return nullptr; + } + + auto ctorDesc = FS::RandomAccessFileInner::ctorDesc.c_str(); + auto ctorSig = FS::RandomAccessFileInner::ctorSig.c_str(); + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); + return nullptr; + } + + ani_long ptr = static_cast(reinterpret_cast(rafFile)); + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { + HILOGE("New %s obj Failed!", classDesc); + return nullptr; + } + + const auto &fdRet = rafFile->GetFD(); + if (!fdRet.IsSuccess()) { + HILOGE("GetFD Failed!"); + return nullptr; + } + + const auto &fd = fdRet.GetData().value(); + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast(fd))) { + HILOGE("Set fd field value failed!"); + return nullptr; + } + + const auto &fpRet = rafFile->GetFPointer(); + if (!fpRet.IsSuccess()) { + HILOGE("GetFPointer Failed!"); + return nullptr; + } + + const auto &fp = fpRet.GetData().value(); + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "filePointer", static_cast(fp))) { + HILOGE("Set fp field value failed!"); + return nullptr; + } + return obj; +} + +static tuple JudgeFile(ani_env *env, ani_object obj) +{ + auto stringTypeDesc = BuiltInTypes::String::classDesc.c_str(); + ani_class stringClass; + env->FindClass(stringTypeDesc, &stringClass); + ani_boolean isString = false; + env->Object_InstanceOf(obj, stringClass, &isString); + if (isString) { + return { true, true }; + } + + auto fileClassDesc = FS::FileInner::classDesc.c_str(); + ani_class fileClass; + env->FindClass(fileClassDesc, &fileClass); + ani_boolean isFile = false; + env->Object_InstanceOf(obj, fileClass, &isFile); + if (isFile) { + return { true, false }; + } + HILOGE("Invalid file type"); + return { false, false }; +} + +static tuple> ToRafOptions(ani_env *env, ani_object obj) +{ + RandomAccessFileOptions options; + ani_boolean isUndefined; + env->Reference_IsUndefined(obj, &isUndefined); + if (isUndefined) { + return { true, nullopt }; + } + + auto [succStart, start] = AniHelper::ParseInt64Option(env, obj, "start"); + if (!succStart) { + HILOGE("Illegal option.start parameter"); + return { false, nullopt }; + } + options.start = start; + + auto [succEnd, end] = AniHelper::ParseInt64Option(env, obj, "end"); + if (!succEnd) { + HILOGE("Illegal option.end parameter"); + return { false, nullopt }; + } + options.end = end; + + return { true, make_optional(move(options)) }; +} + +static ani_object CreateRandomAccessFileByString( + ani_env *env, ani_object file, ani_object mode, optional op) +{ + auto [succPath, path] = TypeConverter::ToUTF8String(env, static_cast(file)); + if (!succPath) { + HILOGE("Parse file path failed"); + return nullptr; + } + auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); + if (!succMode) { + HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + FsResult ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, modeOp, op); + if (!ret.IsSuccess()) { + HILOGE("CreateRandomAccessFile failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + const FsRandomAccessFile *refFile = ret.GetData().value(); + auto result = Wrap(env, move(refFile)); + if (result == nullptr) { + delete refFile; + refFile = nullptr; + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_object file, ani_object mode, ani_object options) +{ + auto [succOp, op] = ToRafOptions(env, options); + if (!succOp) { + HILOGE("Failed to resolve options!"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto [succ, isPath] = JudgeFile(env, file); + if (!succ) { + HILOGE("Judge file argument failed"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + if (isPath) { + return CreateRandomAccessFileByString(env, file, mode, op); + } else { + ani_double fdOp; + if (ANI_OK != env->Object_GetPropertyByName_Double(file, "fd", &fdOp)) { + HILOGE("Get fd in class file failed"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + int32_t fd = static_cast(fdOp); + FsResult ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, op); + if (!ret.IsSuccess()) { + HILOGE("CreateRandomAccessFile failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + const FsRandomAccessFile *refFile = ret.GetData().value(); + auto result = Wrap(env, move(refFile)); + if (result == nullptr) { + delete refFile; + refFile = nullptr; + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..eb794059659181dfc0edb82d7373ba42b2257f54 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_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_FS_PROPERTIES_ANI_CREATE_RANDOMACCESSFILE_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_CREATE_RANDOMACCESSFILE_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class CreateRandomAccessFileAni final { +public: + static ani_object CreateRandomAccessFileSync(ani_env *env, [[maybe_unused]] ani_class clazz, + ani_object file, ani_object mode, ani_object options); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_CREATE_RANDOMACCESSFILE_ANI_H \ No newline at end of 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 new file mode 100644 index 0000000000000000000000000000000000000000..e4a1969f3e493328008e81f49cd5fafe169c3a13 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "create_randomaccessfile_core.h" + +#include + +#include "file_entity.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_randomaccessfile.h" +#include "fs_utils.h" +#include "randomaccessfile_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static tuple ParseStringToFileInfo(const string &path) +{ + OHOS::DistributedFS::FDGuard sfd; + auto fdg = CreateUniquePtr(sfd, false); + if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); + close(sfd); + return { false, FileInfo { false, nullptr, nullptr }, ENOMEM}; + } + size_t length = path.length() + 1; + auto chars = std::make_unique(length); + auto ret = strncpy_s(chars.get(), length, path.c_str(), length - 1); + if (ret != EOK) { + HILOGE("Copy file path failed!"); + return { false, FileInfo { false, nullptr, nullptr }, ENOMEM}; + } + return { true, FileInfo { true, move(chars), move(fdg) }, ERRNO_NOERR}; +} + +static tuple ParseFdToFileInfo(const int32_t &fd) +{ + if (fd < 0) { + HILOGE("Invalid fd"); + return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; + } + auto dupFd = dup(fd); + if (dupFd < 0) { + HILOGE("Failed to get valid fd, fail reason: %{public}s, fd: %{public}d", strerror(errno), fd); + return { false, FileInfo { false, nullptr, nullptr }, EINVAL}; + } + auto fdg = CreateUniquePtr(dupFd, false); + if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); + close(dupFd); + return { false, FileInfo { false, nullptr, nullptr }, ENOMEM}; + } + return { true, FileInfo { false, nullptr, move(fdg) }, ERRNO_NOERR}; +} + +static tuple ValidRafOptions(const optional &options) +{ + RandomAccessFileOptions op = options.value(); + int64_t opStart = INVALID_POS; + int64_t opEnd = INVALID_POS; + + optional startOp = op.start; + optional endOp = op.end; + + if (startOp.has_value()) { + int64_t start = 0; + start = startOp.value(); + if (start < 0) { + HILOGE("Invalid option.start, positive integer is desired"); + return {false, opStart, opEnd}; + } + opStart = start; + } + if (endOp.has_value()) { + int64_t end = 0; + end = endOp.value(); + if (end < 0) { + HILOGE("Invalid option.end, positive integer is desired"); + return {false, opStart, opEnd}; + } + opEnd = end; + } + return {true, opStart, opEnd}; +} + +static tuple ValidAndConvertFlags(const optional &mode, + const optional &options, FileInfo &fileInfo) +{ + uint32_t flags = O_RDONLY; + int64_t start = INVALID_POS; + int64_t end = INVALID_POS; + if (fileInfo.isPath && mode.has_value()) { + auto modeValue = mode.value(); + if (modeValue < 0) { + HILOGE("Invalid flags"); + return {false, flags, start, end}; + } + flags = FsUtils::ConvertFlags(static_cast(modeValue)); + } + + if (options.has_value()) { + auto [succOpt, start, end] = ValidRafOptions(options); + if (!succOpt) { + HILOGE("Invalid RandomAccessFile options"); + return {false, flags, start, end}; + } + } + + return {true, flags, start, end}; +} + +static FsResult InstantiateRandomAccessFile(unique_ptr fdg, + int64_t fp, + int64_t start = INVALID_POS, + int64_t end = INVALID_POS) +{ + FsResult result = FsRandomAccessFile::Constructor(); + if (!result.IsSuccess()) { + HILOGE("Failed to instantiate class"); + return FsResult::Error(EIO); + } + + const FsRandomAccessFile *objRAF = result.GetData().value(); + if (!objRAF) { + HILOGE("Cannot instantiate randomaccessfile"); + return FsResult::Error(EIO); + } + + auto *rafEntity = objRAF->GetRAFEntity(); + if (!rafEntity) { + HILOGE("Cannot instantiate randomaccessfile because of void entity"); + return FsResult::Error(EIO); + } + rafEntity->fd.swap(fdg); + rafEntity->filePointer = fp; + rafEntity->start = start; + rafEntity->end = end; + return result; +} + +FsResult CreateRandomAccessFileCore::DoCreateRandomAccessFile( + const string &path, const optional &mode, const optional &options) +{ + auto [succ, fileInfo, err] = ParseStringToFileInfo(path); + if (!succ) { + return FsResult::Error(err); + } + + auto [succFlags, flags, ignoreStart, ignoreEnd] = ValidAndConvertFlags(mode, options, fileInfo); + if (!succFlags) { + return FsResult::Error(EINVAL); + } + + unique_ptr openReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!openReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + int ret = uv_fs_open(nullptr, openReq.get(), fileInfo.path.get(), flags, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP, NULL); + if (ret < 0) { + return FsResult::Error(ret); + } + + fileInfo.fdg->SetFD(openReq.get()->result, false); + + if (options.has_value()) { + auto [succ, start, end] = ValidRafOptions(options); + if (succ) { + return InstantiateRandomAccessFile(move(fileInfo.fdg), 0, start, end); + } + } + return InstantiateRandomAccessFile(move(fileInfo.fdg), 0); +} + +FsResult CreateRandomAccessFileCore::DoCreateRandomAccessFile( + const int32_t &fd, const optional &options) +{ + auto [succ, fileInfo, err] = ParseFdToFileInfo(fd); + if (!succ) { + return FsResult::Error(err); + } + if (options.has_value()) { + auto [succ, start, end] = ValidRafOptions(options); + if (succ) { + return InstantiateRandomAccessFile(move(fileInfo.fdg), 0, start, end); + } + } + return InstantiateRandomAccessFile(move(fileInfo.fdg), 0); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h new file mode 100644 index 0000000000000000000000000000000000000000..0900891bae980358ec50348db1ee807f88c6149f --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_RANDOMACCESSFILE_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_RANDOMACCESSFILE_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_file.h" +#include "fs_randomaccessfile.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +struct RandomAccessFileOptions { + optional start = nullopt; + optional end = nullopt; +}; + +class CreateRandomAccessFileCore final { +public: + static FsResult DoCreateRandomAccessFile( + const string &path, const optional &mode = nullopt, + const optional &options = nullopt); + static FsResult DoCreateRandomAccessFile( + const int32_t &fd, const optional &options = nullopt); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_RANDOMACCESSFILE_CORE_H \ 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..46044f5d4bec9e4d06a46e71077f00098fd9e4c1 --- /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: + unique_ptr rafEntity; + unique_ptr raf; +}; + +void FsRandomAccessFileMockTest::SetUpTestCase(void) +{ + uvMock = 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 = make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = make_unique(move(rafEntity)); +} + +void FsRandomAccessFileMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsRandomAccessFileMockTest_ReadSync_001 + * @tc.desc: Test function of ReadSync() interface for is failed for options is nullopt. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, 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, 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, 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 is failed for options is nullopt. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_003"; + + string data = "test data"; + EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + auto result = raf->WriteSync(data, 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, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_004"; + + 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 is failed for uv_fs_write() return -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, 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, 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 is failed for uv_fs_write() return -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + +*/ +HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, 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, 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..217af8aaa2f320c879cc046c69fb45c2f7aefd05 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp @@ -0,0 +1,414 @@ +/* + * 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: + unique_ptr rafEntity; + 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 = make_unique(); + const int fdValue = 3; + const bool isClosed = false; + rafEntity->fd = make_unique(fdValue, isClosed); + rafEntity->filePointer = 0; + raf = make_unique(move(rafEntity)); +} + +void FsRandomAccessFileTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @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) << "FsRandomAccessFileTest-begin FsRandomAccessFileTest_Constructor_001"; + + auto result = FsRandomAccessFile::Constructor(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileTest-end FsRandomAccessFileTest_Constructor_001"; +} + +/** + * @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) << "FsRandomAccessFileTest-begin FsRandomAccessFileTest_GetFD_002"; + + auto result = raf->GetFD(); + EXPECT_EQ(result.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsRandomAccessFileTest-end FsRandomAccessFileTest_GetFD_002"; +} + +/** + * @tc.name: FsRandomAccessFileTest_GetFD_003 + * @tc.desc: Test function of GetFD() interface for is failed for FsRandomAccessFile is nullopt. + * @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 = 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 = make_unique(nullptr); + auto result = raf->GetFPointer(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_GetFPointer_005"; +} + +/** + * @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 = 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 = make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + + auto result = raf->ReadSync(buffer, 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 = make_unique(nullptr); + string data = "test data"; + auto result = raf->WriteSync(data, 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 = make_unique(nullptr); + ArrayBuffer buffer(malloc(100), 100); + auto result = raf->WriteSync(buffer, 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"; + + 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"; + + 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 = make_unique(nullptr); + auto result = raf->CloseSync(); + EXPECT_EQ(result.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileTest_CloseSync_017"; +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/create_randomaccessfile_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acbd8d42270e81e08c554f54dcd0c8e80f94552f --- /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/CreateRandomAccessFileCoreMockTest_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/CreateRandomAccessFileCoreMockTest_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