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 6649c2226b3c311d3192ee612123491f545941b2..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(); 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 8b05e2f70bc6c7ed5c244b456c8c98a94b78076a..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 { 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 6e495904fd67da8c29d1e3ad106b1df7482ad212..ba640a84cfca4ee72916e4dccc110a8ded4256dc 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -225,16 +225,18 @@ std::tuple TypeConverter::ToAniArrayBuffer(ani_env *env, return { false, nullptr }; } - static const char *className = "Lescompat/ArrayBuffer;"; + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); ani_status ret; ani_class cls; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); return { false, nullptr }; } + auto ctorDesc = BuiltInTypes::ArrayBuffer::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::ArrayBuffer::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { HILOGE("Not found ctor, err: %{private}d", ret); return { false, nullptr }; } diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 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/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/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index d8f84b8efffb8b031c236f084cd906fe850e2f5d..7e51b7d2010b3e22c80c8ef55c952bf8c69bc119 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -32,6 +32,7 @@ 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", 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