From cbf39cec3df11ef67cac7321223ecd10a294f81f Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Wed, 28 May 2025 14:14:17 +0800 Subject: [PATCH 1/3] add Signed-off-by: yangbiao59 --- interfaces/test/unittest/js/BUILD.gn | 4 +- .../js/mod_fs/properties/access_core_test.cpp | 20 + .../properties/copy_file_core_mock_test.cpp | 409 ++++++++++++++++++ .../mod_fs/properties/copy_file_core_test.cpp | 74 ++++ .../js/mod_fs/properties/dup_core_test.cpp | 23 + .../js/mod_fs/properties/read_core_test.cpp | 6 +- .../js/mod_fs/properties/rmdir_core_test.cpp | 98 +++++ .../js/mod_fs/properties/write_core_test.cpp | 60 ++- 8 files changed, 689 insertions(+), 5 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 7d9d8155..fb5be17b 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -82,9 +82,10 @@ ohos_unittest("ani_file_fs_test") { "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_core_test.cpp", "mod_fs/properties/create_randomaccessfile_core_test.cpp", "mod_fs/properties/create_stream_core_test.cpp", + "mod_fs/properties/copy_core_test.cpp", + "mod_fs/properties/copy_file_core_test.cpp", "mod_fs/properties/dup_core_test.cpp", "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", @@ -152,6 +153,7 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/copy_dir_core_mock_test.cpp", "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/copy_file_core_mock_test.cpp", "mod_fs/properties/dup_core_mock_test.cpp", "mod_fs/properties/fdatasync_core_mock_test.cpp", "mod_fs/properties/fsync_core_mock_test.cpp", 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 d3339db6..8359ed25 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 @@ -93,4 +93,24 @@ HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_002, testing::ext::TestSize.Lev GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_002"; } +/** + * @tc.name: AccessCoreTest_DoAccess_003 + * @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"; + + std::string path = "test"; + std::optional mode = std::make_optional(AccessModeType::ERROR); + + auto res = AccessCore::DoAccess(path, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "NClassTest-end AccessCoreTest_DoAccess_003"; +} + } // OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp new file mode 100644 index 00000000..4a12f7ea --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_mock_test.cpp @@ -0,0 +1,409 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "copy_file_core.h" +#include "mock/uv_fs_mock.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyFileCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void CopyFileCoreTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyFileCoreTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyFileCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyFileCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_001 + * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_001"; + + FileInfo src; + FileInfo dest; + optional mode = std::make_optional(1); + + auto res = CopyFileCore::DoCopyFile(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_001"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_003 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_003"; + + FileInfo src; + FileInfo dest; + src.isPath = true; + dest.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_003"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_004 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_004"; + + FileInfo src; + FileInfo dest; + src.isPath = true; + dest.isPath = false; + src.fdg = make_unique(1); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_004"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_005 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_005"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_005"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_006 + * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_006"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_006"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_007 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_007"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + dest.fdg = make_unique(fd); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_007"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_008 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_008"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(fd, -1); + src.fdg = make_unique(fd); + dest.fdg = make_unique(); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(fd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_008"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_009 + * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_009"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = false; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + int destfd = open("dest.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + ASSERT_NE(destfd, -1); + src.fdg = make_unique(srcfd); + dest.fdg = make_unique(destfd); + + EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(srcfd); + close(destfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_009"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0010 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0010"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(-1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0010"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0011 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0011"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len + 1)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0011"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0012 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0012"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), true); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0012"; +} + +/** + * @tc.name: CopyFileCoreTest_DoCopyFile_0013 + * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreTest, CopyFileCoreTest_DoCopyFile_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreTest-begin CopyFileCoreTest_DoCopyFile_0013"; + + FileInfo src; + FileInfo dest; + src.isPath = false; + dest.isPath = true; + + int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ASSERT_NE(srcfd, -1); + const char* data = "Hello, World!"; + ssize_t len = write(srcfd, data, strlen(data)); + ASSERT_NE(len, -1); + src.fdg = make_unique(srcfd); + + EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1)); + EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(0)); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + close(srcfd); + + GTEST_LOG_(INFO) << "CopyFileCoreTest-end CopyFileCoreTest_DoCopyFile_0013"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp new file mode 100644 index 00000000..b26d26e8 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "copy_file_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyFileCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void CopyFileCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CopyFileCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CopyFileCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyFileCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyFileCoreMockTest_DoCopyFile_001 + * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_001"; + + FileInfo src; + FileInfo dest; + optional mode = std::make_optional(1); + + auto res = CopyFileCore::DoCopyFile(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/dup_core_test.cpp index 42db95d0..e622a27f 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 @@ -15,6 +15,8 @@ #include "dup_core.h" +#include +#include #include namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -68,4 +70,25 @@ HWTEST_F(DupCoreTest, DupCoreTest_DoDup_001, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "NClassTest-end DupCoreTest_DoDup_001"; } +/** + * @tc.name: DupCoreTest_DoDup_002 + * @tc.desc: Test function of DupCore::DoDup interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(DupCoreTest, DupCoreTest_DoDup_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin DupCoreTest_DoDup_002"; + 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/read_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/read_core_test.cpp index 9c0b4151..2ae3fe89 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 @@ -85,7 +85,7 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_002"; - int32_t fd = -1; + int32_t fd = 1; void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0xffffffff + 1); @@ -106,11 +106,11 @@ HWTEST_F(ReadCoreTest, ReadCoreTest_DoRead_003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "ReadCoreTest-begin ReadCoreTest_DoRead_003"; - int32_t fd = -1; + int32_t fd = 1; void *buf = nullptr; ArrayBuffer arrayBuffer(buf, 0); optional options = std::make_optional(); - options->length = std::make_optional(-1); + options->offset = std::make_optional(-1); auto res = ReadCore::DoRead(fd, arrayBuffer); EXPECT_EQ(res.IsSuccess(), false); 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 e99ed4dc..8f23023f 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 @@ -15,6 +15,8 @@ #include "rmdir_core.h" +#include +#include #include @@ -69,4 +71,100 @@ HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_001, testing::ext::TestSize.Lev GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_001"; } +/** + * @tc.name: RmdirCoreTest_DoRmdirent_002 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_002"; + std::string fpath = "invalid?path"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_002"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_003 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_003"; + std::string fpath = "/dir"; + auto res = RmdirentCore::DoRmdirent(fpath); + + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_003"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_004 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for Failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_004"; + + std::filesystem::create_directories("test_dir"); + std::ofstream("test_dir/test_file.txt") << "test"; + + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_write | std::filesystem::perms::owner_exec, + std::filesystem::perm_options::replace); + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_004"; +} + +/** + * @tc.name: RmdirCoreTest_DoRmdirent_005 + * @tc.desc: Test function of RmdirCore::DoRmdirent interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RmdirCoreTest-begin RmdirCoreTest_DoRmdirent_005"; + + // 创建测试目录 + std::filesystem::create_directories("test_dir"); + // 在目录下创建一个文件 + std::ofstream("test_dir/test_file.txt") << "test"; + + auto res = RmdirentCore::DoRmdirent("test_dir"); + EXPECT_EQ(res.IsSuccess(), true); + + try { + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); + } catch (...) {} + std::filesystem::remove_all("test_dir"); + + GTEST_LOG_(INFO) << "RmdirCoreTest-end RmdirCoreTest_DoRmdirent_005"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/write_core_test.cpp index db078298..1741f168 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 @@ -17,7 +17,6 @@ #include - namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; using namespace testing::ext; @@ -87,4 +86,63 @@ HWTEST_F(WriteCoreTest, WriteCoreTest_DoWrite2_001, testing::ext::TestSize.Level GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite2_001"; } +#if defined(_WIN64) || defined(__X86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @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_002, testing::ext::TestSize.Level1) +{ + 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(), false); + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_002"; +} +#else +#endif + +/** + * @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_DoWrite1_003, testing::ext::TestSize.Level1) +{ + 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, options); + EXPECT_EQ(res.IsSuccess(), false); + GTEST_LOG_(INFO) << "WriteCoreTest-end WriteCoreTest_DoWrite1_003"; +} + +/** + * @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_DoWrite1_004, testing::ext::TestSize.Level1) +{ + 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_DoWrite1_004"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From 0e9e4e0752796cdad97cfc6e8476f27ab0e74672 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Wed, 28 May 2025 17:49:25 +0800 Subject: [PATCH 2/3] new add Signed-off-by: yangbiao59 --- interfaces/test/unittest/js/BUILD.gn | 3 + .../js/mod_fs/properties/lseek_core_test.cpp | 7 - .../js/mod_fs/properties/rmdir_core_test.cpp | 8 +- .../mod_fs/properties/stat_core_mock_test.cpp | 179 ++++++++++++++++++ .../js/mod_fs/properties/stat_core_test.cpp | 102 +--------- 5 files changed, 186 insertions(+), 113 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index fb5be17b..aec84eee 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -89,12 +89,14 @@ ohos_unittest("ani_file_fs_test") { "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/lseek_core_test.cpp", "mod_fs/properties/movedir_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_core_test.cpp", "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", "mod_fs/properties/rmdir_core_test.cpp", + "mod_fs/properties/stat_core_test.cpp", "mod_fs/properties/truncate_core_test.cpp", "mod_fs/properties/utimes_core_test.cpp", "mod_fs/properties/write_core_test.cpp", @@ -166,6 +168,7 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/read_core_mock_test.cpp", "mod_fs/properties/read_lines_core_mock_test.cpp", "mod_fs/properties/rename_core_mock_test.cpp", + "mod_fs/properties/stat_core_mock_test.cpp", "mod_fs/properties/symlink_core_mock_test.cpp", "mod_fs/properties/truncate_core_mock_test.cpp", "mod_fs/properties/unlink_core_mock_test.cpp", diff --git a/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp index 93c9f22c..f5bd78bb 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp @@ -14,10 +14,8 @@ */ #include "lseek_core.h" -#include "mock/uv_fs_mock.h" #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,20 +28,15 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void LseekCoreTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; } void LseekCoreTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; GTEST_LOG_(INFO) << "TearDownTestCase"; } 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 8f23023f..82bf8b12 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 @@ -149,18 +149,16 @@ HWTEST_F(RmdirCoreTest, RmdirCoreTest_DoRmdirent_005, testing::ext::TestSize.Lev { 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); + std::filesystem::permissions("test_dir", + std::filesystem::perms::owner_all, + std::filesystem::perm_options::replace); } catch (...) {} std::filesystem::remove_all("test_dir"); diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp new file mode 100644 index 00000000..b308980d --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_mock_test.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "stat_core.h" +#include "mock/uv_fs_mock.h" + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class StatCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void StatCoreMockTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void StatCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void StatCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void StatCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: StatCoreMockTest_DoStat_001 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_001"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = true; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_001"; +} + +/** + * @tc.name: StatCoreMockTest_DoStat_002 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_002"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = true; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_002"; +} + +/** + * @tc.name: StatCoreMockTest_DoStat_003 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_003"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_003"; +} + +/** + * @tc.name: StatCoreMockTest_DoStat_004 + * @tc.desc: Test function of FsyncCore::DoStat interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_004"; + + FileInfo fileinfo; + fileinfo.path = std::make_unique(1); + fileinfo.fdg = std::make_unique(1); + fileinfo.isPath = false; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_004"; +} + +/** + * @tc.name: StatCoreMockTest_DoStat_005 + * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(StatCoreMockTest, StatCoreMockTest_DoStat_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StatCoreMockTest-begin StatCoreMockTest_DoStat_005"; + + FileInfo fileinfo; + string buffer = "Hello, World!"; + fileinfo.path = std::make_unique(buffer.size() + 1); + std::strcpy(fileinfo.path.get(), buffer.c_str()); + fileinfo.fdg = std::make_unique(-1); + fileinfo.isPath = true; + + EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = StatCore::DoStat(fileinfo); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "StatCoreMockTest-end StatCoreMockTest_DoStat_005"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp index 4c5c7b4b..5ad93fbf 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/stat_core_test.cpp @@ -14,10 +14,8 @@ */ #include "stat_core.h" -#include "mock/uv_fs_mock.h" #include -#include namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -30,20 +28,15 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr uvfs = nullptr; }; void StatCoreTest::SetUpTestCase(void) { - uvfs = std::make_shared(); - Uvfs::ins = uvfs; GTEST_LOG_(INFO) << "SetUpTestCase"; } void StatCoreTest::TearDownTestCase(void) { - Uvfs::ins = nullptr; - uvfs = nullptr; GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -87,75 +80,6 @@ HWTEST_F(StatCoreTest, StatCoreTest_DoStat_002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_002"; - FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.isPath = true; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); - - auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_002"; -} - -/** - * @tc.name: StatCoreTest_DoStat_003 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(StatCoreTest, StatCoreTest_DoStat_003, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_003"; - - FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.isPath = true; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); - - auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_003"; -} - -/** - * @tc.name: StatCoreTest_DoStat_004 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(StatCoreTest, StatCoreTest_DoStat_004, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_004"; - - FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.isPath = false; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); - - auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), false); - - GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_004"; -} - -/** - * @tc.name: StatCoreTest_DoStat_005 - * @tc.desc: Test function of FsyncCore::DoStat interface for FALSE. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(StatCoreTest, StatCoreTest_DoStat_005, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_005"; - FileInfo fileinfo; fileinfo.path = std::make_unique(1); fileinfo.fdg = std::make_unique(-1); @@ -164,31 +88,7 @@ HWTEST_F(StatCoreTest, StatCoreTest_DoStat_005, testing::ext::TestSize.Level1) auto res = StatCore::DoStat(fileinfo); EXPECT_EQ(res.IsSuccess(), false); - GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_005"; -} - -/** - * @tc.name: StatCoreTest_DoStat_006 - * @tc.desc: Test function of FsyncCore::DoStat interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - */ -HWTEST_F(StatCoreTest, StatCoreTest_DoStat_006, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "StatCoreTest-begin StatCoreTest_DoStat_006"; - - FileInfo fileinfo; - fileinfo.path = std::make_unique(1); - fileinfo.fdg = std::make_unique(1); - fileinfo.isPath = false; - - EXPECT_CALL(*uvfs, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); - - auto res = StatCore::DoStat(fileinfo); - EXPECT_EQ(res.IsSuccess(), true); - - GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_006"; + GTEST_LOG_(INFO) << "StatCoreTest-end StatCoreTest_DoStat_002"; } } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From f1b2b0ce8f9849138b65a471594e35d42fb27a74 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Thu, 29 May 2025 17:39:57 +0800 Subject: [PATCH 3/3] add move,copy_file Signed-off-by: yangbiao59 --- interfaces/test/unittest/js/BUILD.gn | 1 + .../mod_fs/properties/copy_file_core_test.cpp | 24 +++ .../js/mod_fs/properties/mock/uv_fs_mock.cpp | 5 + .../js/mod_fs/properties/mock/uv_fs_mock.h | 2 + .../mod_fs/properties/move_core_mock_test.cpp | 183 ++++++++++++++++++ .../js/mod_fs/properties/move_core_test.cpp | 51 +++++ 6 files changed, 266 insertions(+) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index aec84eee..728b762f 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -91,6 +91,7 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/listfile_core_test.cpp", "mod_fs/properties/lseek_core_test.cpp", "mod_fs/properties/movedir_core_test.cpp", + "mod_fs/properties/move_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_core_test.cpp", "mod_fs/properties/read_lines_core_test.cpp", diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp index b26d26e8..8d14cda0 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_file_core_test.cpp @@ -71,4 +71,28 @@ HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_001, testing::ext GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_001"; } +/** + * @tc.name: CopyFileCoreMockTest_DoCopyFile_002 + * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_002"; + + FileInfo src; + FileInfo dest; + src.isPath = true; + dest.isPath = true; + src.path = std::make_unique(1); + dest.path = std::make_unique(1); + + auto res = CopyFileCore::DoCopyFile(src, dest); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_002"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp b/interfaces/test/unittest/js/mod_fs/properties/mock/uv_fs_mock.cpp index de6d4803..4902c0eb 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 @@ -131,3 +131,8 @@ int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, { return Uvfs::ins->uv_fs_sendfile(loop, req, outFd, inFd, off, len, cb); } + +const char* uv_err_name(int err) +{ + return Uvfs::ins->uv_err_name(err); +} 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 1f9d5402..cadadf88 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 @@ -57,6 +57,7 @@ public: virtual int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) = 0; virtual int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb) = 0; + virtual const char* uv_err_name(int err) = 0; }; class UvfsMock : public Uvfs { @@ -90,6 +91,7 @@ public: MOCK_METHOD4(uv_fs_fsync, int(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb)); MOCK_METHOD7(uv_fs_sendfile, int(uv_loop_t* loop, uv_fs_t* req, uv_file outFd, uv_file inFd, int64_t off, size_t len, uv_fs_cb cb)); + MOCK_METHOD1(uv_err_name, const char*(int err)); }; } // OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp index 517178fd..1853ef2d 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/move_core_mock_test.cpp @@ -265,5 +265,188 @@ HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_008, testing::ext::TestSize.L GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_008"; } +/** + * @tc.name: MoveCoreMockTest_DoMove_009 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_009"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_009"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_0010 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0010"; + + std::string src; + std::string dest = "file.txt"; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + int fd = open(dest.c_str(), O_RDWR | O_CREAT, 0666); + ASSERT_NE(fd, -1); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + ASSERT_EQ(unlink(dest.c_str()), 0); + close(fd); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0010"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_0011 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0011"; + + std::string src; + std::string dest; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0011"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_0012 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0012"; + + std::string src = "file.txt"; + std::string dest = "dest.txt"; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); + ASSERT_NE(fd, -1); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)).WillOnce(Return(-1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + ASSERT_EQ(unlink(src.c_str()), 0); + close(fd); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0012"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_0013 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0013"; + + std::string src = "file.txt"; + std::string dest = "dest.txt"; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); + ASSERT_NE(fd, -1); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + ASSERT_EQ(unlink(src.c_str()), 0); + close(fd); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0013"; +} + +/** + * @tc.name: MoveCoreMockTest_DoMove_0014 + * @tc.desc: Test function of MoveCore::DoMove interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreMockTest, MoveCoreMockTest_DoMove_0014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreMockTest-begin MoveCoreMockTest_DoMove_0013"; + + std::string src = "file.txt"; + std::string dest = "dest.txt"; + optional mode = std::make_optional(MODE_FORCE_MOVE); + + int fd = open(src.c_str(), O_RDWR | O_CREAT, 0666); + ASSERT_NE(fd, -1); + + EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_rename(_, _, _, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*uvMock, uv_err_name(_)).WillRepeatedly(Return("EXDEV")); + EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), true); + + ASSERT_EQ(unlink(src.c_str()), 0); + close(fd); + + GTEST_LOG_(INFO) << "MoveCoreMockTest-end MoveCoreMockTest_DoMove_0013"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp index b4a494e8..e17eeaf3 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/move_core_test.cpp @@ -15,6 +15,7 @@ #include "move_core.h" +#include #include @@ -112,4 +113,54 @@ HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_003, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_003"; } +/** + * @tc.name: MoveCoreTest_DoMove_004 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreTest-begin MoveCoreTest_DoMove_004"; + + std::string src = "dir"; + std::string dest = "/dest.txt"; + int mode = 3; + + std::filesystem::path dirpath = "dir"; + ASSERT_TRUE(std::filesystem::create_directories(dirpath)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + std::filesystem::remove(dirpath); + GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_004"; +} + +/** + * @tc.name: MoveCoreTest_DoMove_005 + * @tc.desc: Test function of MoveCore::DoMove interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(MoveCoreTest, MoveCoreTest_DoMove_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "MoveCoreTest-begin MoveCoreTest_DoMove_005"; + + std::string src = "/src.txt"; + std::string dest = "dir"; + int mode = 3; + + std::filesystem::path dirpath = "dir"; + ASSERT_TRUE(std::filesystem::create_directories(dirpath)); + + auto res = MoveCore::DoMove(src, dest, mode); + EXPECT_EQ(res.IsSuccess(), false); + + std::filesystem::remove(dirpath); + GTEST_LOG_(INFO) << "MoveCoreTest-end MoveCoreTest_DoMove_005"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee