diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 3c4ddabd989b02f0a78d67f916b3c4cf72541662..97cbc5d2fde65bdce1a3bcc9231e2695f0d0c701 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -1077,7 +1077,6 @@ ohos_shared_library("ani_file_statvfs") { ] use_exceptions = true cflags = [ - "-fvisibility=hidden", "-fdata-sections", "-ffunction-sections", "-Oz", diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 6240c5e86c5cb8ff447b355349bb0e3bf0282dab..f3acdf930a13c888d0cf0697489a24bff570b8a7 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -90,6 +90,7 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", @@ -97,6 +98,7 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/truncate_core_test.cpp", "mod_fs/properties/utimes_core_test.cpp", "mod_fs/properties/write_core_test.cpp", + "mod_fs/properties/xattr_core_test.cpp", ] deps = [ @@ -150,7 +152,9 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/fdatasync_core_mock_test.cpp", "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/read_lines_core_mock_test.cpp", "mod_fs/properties/symlink_core_mock_test.cpp", "mod_fs/properties/truncate_core_mock_test.cpp", "mod_fs/properties/utimes_core_mock_test.cpp", @@ -158,6 +162,8 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/mock/system_mock.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", "mod_fs/properties/open_core_mock_test.cpp", + "mod_fs/properties/unlink_core_mock_test.cpp", + "mod_fs/properties/xattr_core_mock_test.cpp", ] deps = [ diff --git a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a602df5c8b4b9b959a6d7df6cd55d062c3155770 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include "unlink_core.h" +#include "uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class UnlinkCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +filesystem::path UnlinkCoreMockTest::tempFilePath; + +void UnlinkCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = filesystem::temp_directory_path() / "unlink_test_file.txt"; + ofstream(tempFilePath) << "Test content\n123\n456"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void UnlinkCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void UnlinkCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void UnlinkCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: UnlinkCoreMockTest_DoUnlink_001 + * @tc.desc: Test function of UnlinkCore::DoUnlink interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_001"; + + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1)); + + string path = tempFilePath.string(); + auto res = UnlinkCore::DoUnlink(path); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_001"; +} + +/** + * @tc.name: UnlinkCoreMockTest_DoUnlink_002 + * @tc.desc: Test function of UnlinkCore::DoUnlink interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_002"; + + EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1)); + + string path = tempFilePath.string(); + auto res = UnlinkCore::DoUnlink(path); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ff953b3e11346578fd5129a6cccd5307fabc88e --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/xattr_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 +#include + +#include +#include +#include + +#include "system_mock.h" +#include "xattr_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class XattrCoreMockTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr sys = nullptr; +}; + +filesystem::path XattrCoreMockTest::tempFilePath; + +void XattrCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + tempFilePath = "/data/local/tmp/xattr_test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456"; + tempfile.close(); + sys = make_shared(); + System::ins = sys; +} + +void XattrCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + filesystem::remove(tempFilePath); + System::ins = nullptr; + sys = nullptr; +} + +void XattrCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void XattrCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: XattrCoreMockTest_DoSetXattr_001 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoSetXattr_001"; + + string path = tempFilePath.string(); + string key = "test_key"; + string value = "test_value"; + + EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(-1)); + auto ret = XattrCore::DoSetXattr(path, key, value); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoSetXattr_001"; +} + +/** + * @tc.name: XattrCoreMockTest_DoSetXattr_002 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoSetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoSetXattr_002"; + + string path = tempFilePath.string(); + string key = "test_key"; + string value = "test_value"; + + EXPECT_CALL(*sys, setxattr(_, _, _, _, _)).WillOnce(Return(0)); + auto ret = XattrCore::DoSetXattr(path, key, value); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoSetXattr_002"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_001 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_001"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillRepeatedly(Return(-1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_001"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_002 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_002"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(Return(-1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_002"; +} + +/** + * @tc.name: XattrCoreMockTest_DoGetXattr_003 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreMockTest, XattrCoreMockTest_DoGetXattr_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreMockTest-begin XattrCoreMockTest_DoGetXattr_003"; + + string path = tempFilePath.string(); + string key = "test_key"; + + EXPECT_CALL(*sys, getxattr(_, _, _, _)).WillOnce(Return(1)).WillOnce(Return(1)); + auto ret = XattrCore::DoGetXattr(path, key); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "XattrCoreMockTest-end XattrCoreMockTest_DoGetXattr_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..febfd1c1e759677898417e140b8f18ca93cd8bd0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/xattr_core_test.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include "xattr_core.h" + +#define MAX_LEN 4096 + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class XattrCoreTest : public testing::Test { +public: + static filesystem::path tempFilePath; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +filesystem::path XattrCoreTest::tempFilePath; + +void XattrCoreTest::SetUpTestCase(void) +{ + tempFilePath = "/data/local/tmp/xattr_test_file.txt"; + ofstream tempfile(tempFilePath); + tempfile << "Test content\n123\n456"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void XattrCoreTest::TearDownTestCase(void) +{ + filesystem::remove(tempFilePath); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void XattrCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void XattrCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: XattrCoreTest_DoSetXattr_001 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoSetXattr_001"; + + string path = tempFilePath.string(); + std::string key = "test_key"; + std::string value(MAX_LEN + 1, 'a'); + + auto ret = XattrCore::DoSetXattr(path, key, value); + + EXPECT_FALSE(ret.IsSuccess()); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900020); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "Invalid argument"); + + GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoSetXattr_001"; +} + +/** + * @tc.name: XattrCoreTest_DoSetXattr_002 + * @tc.desc: Test function of XattrCore::DoSetXattr interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoSetXattr_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoSetXattr_002"; + + string path = tempFilePath.string(); + std::string key(MAX_LEN + 1, 'a'); + std::string value = "test_value"; + + auto ret = XattrCore::DoSetXattr(path, key, value); + + EXPECT_FALSE(ret.IsSuccess()); + auto err = ret.GetError(); + int errCode = err.GetErrNo(); + EXPECT_EQ(errCode, 13900020); + auto msg = err.GetErrMsg(); + EXPECT_EQ(msg, "Invalid argument"); + + GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoSetXattr_002"; +} + +/** + * @tc.name: XattrCoreTest_DoGetXattr_001 + * @tc.desc: Test function of XattrCore::DoGetXattr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(XattrCoreTest, XattrCoreTest_DoGetXattr_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "XattrCoreTest-begin XattrCoreTest_DoGetXattr_001"; + + std::string path = "/data/local/tmp/nonexistent_file"; + string key = "test_key"; + + auto ret = XattrCore::DoGetXattr(path, key); + + ASSERT_TRUE(ret.IsSuccess()); + EXPECT_EQ(ret.GetData().value(), ""); + + GTEST_LOG_(INFO) << "XattrCoreTest-end XattrCoreTest_DoGetXattr_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file