From 04ee705cba9ab559c8903d53923ebdb1d6dea886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 28 May 2025 15:20:24 +0800 Subject: [PATCH 1/4] hsh&securitylabel tdd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/test/unittest/BUILD.gn | 3 + interfaces/test/unittest/js/BUILD.gn | 108 ++++++ .../class_atomicfile/fs_atomicfile_test.cpp | 366 ++++++++++++++++++ .../class_stream/fs_stream_mock_test.cpp | 191 +++++++++ .../js/mod_fs/class_stream/mock/c_mock.cpp | 32 ++ .../js/mod_fs/class_stream/mock/c_mock.h | 42 ++ .../unittest/js/mod_hash/hash_core_test.cpp | 136 +++++++ .../securitylabel_core_test.cpp | 142 +++++++ 8 files changed, 1020 insertions(+) create mode 100644 interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h create mode 100644 interfaces/test/unittest/js/mod_hash/hash_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 3a6c6eb26..1c1ca95da 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,7 +17,10 @@ group("file_api_unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "js:ani_file_fs_test", + "js:ani_file_fs_mock_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", + "js:ani_file_hash_test", + "js:ani_file_securitylabel_test", ] } diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index e4e6a9d52..49f400ac2 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -74,4 +74,112 @@ ohos_unittest("ani_file_fs_test") { ] cflags_cc = [ "-DENABLE_NAPI_MOCK" ] +} + +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_stream/fs_stream_mock_test.cpp", + "mod_fs/class_stream/mock/c_mock.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${file_api_path}/interfaces/kits/js:ani_file_fs", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + defines = [ + "private=public", + ] +} + +ohos_unittest("ani_file_hash_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_hash/hash_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_hash", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${file_api_path}/interfaces/kits/js:ani_file_hash", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "libuv:uv", + "googletest:gmock_main", + "googletest:gtest_main", + ] +} + +ohos_unittest("ani_file_securitylabel_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ + "mod_securitylabel/securitylabel_core_test.cpp", + ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + ] + + deps = [ + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "libuv:uv", + "googletest:gmock_main", + "googletest:gtest_main", + ] } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp b/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp new file mode 100644 index 000000000..081de4750 --- /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 filePath = "/data/test/FsAtomicfileTest.txt"; +string 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(filePath); + tempfile << "hello world"; + tempfile.close(); + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsAtomicfileTest::TearDownTestCase(void) +{ + filesystem::remove(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(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + string path = stream->GetPath(); + EXPECT_EQ(path, 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(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(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(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(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(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(filePath); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_TRUE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_001"; +} + +/** + * @tc.name: FsAtomicfileTest_ReadFully_002 + * @tc.desc: Test function of FsAtomicFile::ReadFully interface for valied path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_002"; + + auto ret = FsAtomicFile::Constructor("aaaaaaaaaaaaaaaa"); + ASSERT_TRUE(ret.IsSuccess()); + + shared_ptr stream(move(ret.GetData().value())); + auto result = stream->ReadFully(); + ASSERT_FALSE(result.IsSuccess()); + + GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_ReadFully_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp new file mode 100644 index 000000000..f13339006 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "c_mock.h" +#include "create_stream_core.h" +#include "fs_utils.h" + +#define STREAM_FILE_PATH "/data/test/FsStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class FsStreamMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + mock_ = std::make_shared(); + ICMock::ins = mock_; + int32_t fd = open(STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + ICMock::ins = nullptr; + mock_ = nullptr; + rmdir(STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; + + static inline std::shared_ptr mock_ = nullptr; +}; + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for fail ftell. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + auto retWr = result->Write("FsStreamWriteTest_0001", opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + string buf = "FsStreamWriteTest_0002"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + ReadOptions opt; + opt.offset = 5; + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0001"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp new file mode 100644 index 000000000..a64f834fa --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -0,0 +1,32 @@ +/* +* Copyright (C) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "c_mock.h" + +using namespace OHOS::FileManagement::ModuleFileIO; + +extern "C" { + +int fseek(FILE *stream, long len, int offset) +{ + return ICMock::ins->fseek(stream, len, offset); +} + +long ftell(FILE *stream) +{ + return ICMock::ins->ftell(stream); +} + +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h new file mode 100644 index 000000000..2fbdcee16 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -0,0 +1,42 @@ +/* +* Copyright (C) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO { + +class ICMock { +public: + static inline std::shared_ptr ins = nullptr; +public: + virtual ~ICMock() = default; + virtual int fseek(FILE *, long, int) = 0; + virtual long ftell(FILE *) = 0; +}; + +class CMock : public ICMock { +public: + MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); + MOCK_METHOD(long, ftell, (FILE *), (override)); +}; + + +} // OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp new file mode 100644 index 000000000..6c13b0e6e --- /dev/null +++ b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "hash_core.h" + +#define FILE_PATH "/data/test/HashCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class HashCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: DoHashTest_0001 + * @tc.desc: Test function of DoHash() interface for invalid alg. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0001"; + string alg = "sha128"; + auto ret = HashCore::DoHash(FILE_PATH, alg); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0001"; +} + +/** + * @tc.name: DoHashTest_0002 + * @tc.desc: Test function of DoHash() interface for md5 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0002"; + auto ret = HashCore::DoHash(FILE_PATH, "md5"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0002"; +} + +/** + * @tc.name: DoHashTest_0003 + * @tc.desc: Test function of DoHash() interface for sha1 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0003"; + auto ret = HashCore::DoHash(FILE_PATH, "sha1"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0003"; +} + +/** + * @tc.name: DoHashTest_0004 + * @tc.desc: Test function of DoHash() interface for sha256 success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0004"; + auto ret = HashCore::DoHash(FILE_PATH, "sha256"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004"; +} + +/** + * @tc.name: DoHashTest_0005 + * @tc.desc: Test function of DoHash() interface for no such file or directory. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(HashCoreTest, DoHashTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0005"; + auto ret = HashCore::DoHash("/data/local/tmp/azuxyicayhyskjeh", "sha256"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0005"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp new file mode 100644 index 000000000..ae28da157 --- /dev/null +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "securitylabel_core.h" + +#define FILE_PATH "/data/test/SecurityLabelCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleSecurityLabel { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +class SecurityLabelCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: DoSetSecurityLabel_0001 + * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid level. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0001"; + auto ret = DoSetSecurityLabel(FILE_PATH, "abc"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0001"; +} + +/** + * @tc.name: DoSetSecurityLabel_0002 + * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetDoSetSecurityLabel_0002SecurityLabel_0001"; + auto ret = DoSetSecurityLabel("FILE_PATH", "s1"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0002"; +} + +/** + * @tc.name: DoSetSecurityLabel_0003 + * @tc.desc: Test function of DoSetSecurityLabel() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0003"; + auto ret = DoSetSecurityLabel(FILE_PATH, "s2"); + ASSERT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0003"; +} + +/** + * @tc.name: DoGetSecurityLabel_0001 + * @tc.desc: Test function of DoGetSecurityLabel() interface for invalid path. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0001"; + auto ret = DoGetSecurityLabel("FILE_PATH"); + EXPECT_TRUE(ret.IsSuccess()); + + const string level = ret.GetData().value(); + EXPECT_EQ(level, "s3"); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0001"; +} + +/** + * @tc.name: DoGetSecurityLabel_0002 + * @tc.desc: Test function of DoGetSecurityLabel() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGDNF + */ +HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0002"; + auto ret = DoGetSecurityLabel(FILE_PATH); + EXPECT_TRUE(ret.IsSuccess()); + + const string level = ret.GetData().value(); + EXPECT_EQ(level, "s2"); + + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0002"; +} + +} // namespace ModuleSecurityLabel +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file -- Gitee From 594e08ddae7cd859c40deaaa3514850478880373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 10:03:38 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_atomicfile/fs_atomicfile_test.cpp | 24 +++++++++---------- .../js/mod_fs/class_stream/mock/c_mock.cpp | 1 - .../js/mod_fs/class_stream/mock/c_mock.h | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) 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 index 081de4750..f506c732f 100644 --- 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 @@ -23,8 +23,8 @@ namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace std; namespace fs = std::filesystem; -string filePath = "/data/test/FsAtomicfileTest.txt"; -string deleteFile = "/data/test/FsAtomicfileDelTest.txt"; +string g_filePath = "/data/test/FsAtomicfileTest.txt"; +string g_deleteFile = "/data/test/FsAtomicfileDelTest.txt"; class FsAtomicfileTest : public testing::Test { public: @@ -36,7 +36,7 @@ public: void FsAtomicfileTest::SetUpTestCase(void) { - ofstream tempfile(filePath); + ofstream tempfile(g_filePath); tempfile << "hello world"; tempfile.close(); GTEST_LOG_(INFO) << "SetUpTestCase"; @@ -44,7 +44,7 @@ void FsAtomicfileTest::SetUpTestCase(void) void FsAtomicfileTest::TearDownTestCase(void) { - filesystem::remove(filePath); + filesystem::remove(g_filePath); GTEST_LOG_(INFO) << "TearDownTestCase"; } @@ -69,12 +69,12 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetPath_001, testing::ext::TestSize. { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetPath_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); string path = stream->GetPath(); - EXPECT_EQ(path, filePath); + EXPECT_EQ(path, g_filePath); GTEST_LOG_(INFO) << "FsAtomicfileTest-end FsAtomicfileTest_GetPath_001"; } @@ -90,7 +90,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_GetBaseFile_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_GetBaseFile_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -181,7 +181,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_StartWrite_001, testing::ext::TestSi { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_StartWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -248,7 +248,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FinishWrite_001, testing::ext::TestS { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FinishWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -276,7 +276,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_FailWrite_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_FailWrite_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -304,7 +304,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_Delete_001, testing::ext::TestSize.L { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_Delete_001"; - auto ret = FsAtomicFile::Constructor(deleteFile); + auto ret = FsAtomicFile::Constructor(g_deleteFile); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); @@ -332,7 +332,7 @@ HWTEST_F(FsAtomicfileTest, FsAtomicfileTest_ReadFully_001, testing::ext::TestSiz { GTEST_LOG_(INFO) << "FsAtomicfileTest-begin FsAtomicfileTest_ReadFully_001"; - auto ret = FsAtomicFile::Constructor(filePath); + auto ret = FsAtomicFile::Constructor(g_filePath); ASSERT_TRUE(ret.IsSuccess()); shared_ptr stream(move(ret.GetData().value())); diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp index a64f834fa..f9567b0d5 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -28,5 +28,4 @@ long ftell(FILE *stream) { return ICMock::ins->ftell(stream); } - } \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h index 2fbdcee16..8f2bbe88e 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -16,7 +16,7 @@ #ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H #define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H -#include +#include #include #include -- Gitee From 54014e72aed5f3246da37be61cb2d5c370a54281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 10:37:16 +0800 Subject: [PATCH 3/4] del atomic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_atomicfile/fs_atomicfile_test.cpp | 366 ------------------ 1 file changed, 366 deletions(-) delete mode 100644 interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_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 deleted file mode 100644 index f506c732f..000000000 --- a/interfaces/test/unittest/js/mod_fs/class_atomicfile/fs_atomicfile_test.cpp +++ /dev/null @@ -1,366 +0,0 @@ -/* - * 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 -- Gitee From 8e3f2a84bcc7f2fb4e23c50f144d116d43f21964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 29 May 2025 20:07:22 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_stream/fs_stream_mock_test.cpp | 42 ++++++++++--------- .../unittest/js/mod_hash/hash_core_test.cpp | 16 +++---- .../securitylabel_core_test.cpp | 22 +++++----- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp index f13339006..7e04b342a 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -18,26 +18,27 @@ #include "create_stream_core.h" #include "fs_utils.h" -#define STREAM_FILE_PATH "/data/test/FsStreamCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; + +static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; + class FsStreamMockTest : public testing::Test { public: static void SetUpTestCase(void) { mock_ = std::make_shared(); ICMock::ins = mock_; - int32_t fd = open(STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { ICMock::ins = nullptr; mock_ = nullptr; - rmdir(STREAM_FILE_PATH); + rmdir(g_streamFilePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -55,8 +56,8 @@ public: */ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -68,7 +69,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; } /** @@ -81,8 +82,8 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) */ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamSeekTest_0002"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -95,7 +96,7 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamSeekTest_0002"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; } /** @@ -108,8 +109,8 @@ HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) */ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -123,7 +124,7 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1 auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; } /** @@ -136,8 +137,8 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1 */ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamWriteTest_0002"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "w+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -146,13 +147,13 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1 WriteOptions opt; opt.offset = 5; string buf = "FsStreamWriteTest_0002"; - auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), buf.length()), opt); EXPECT_FALSE(retWr.IsSuccess()); auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamWriteTest_0002"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; } /** @@ -165,8 +166,8 @@ HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1 */ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin FsStreamReadTest_0001"; - auto ret = CreateStreamCore::DoCreateStream(STREAM_FILE_PATH, "r+"); + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); ASSERT_TRUE(ret.IsSuccess()); auto result = ret.GetData().value(); @@ -180,10 +181,11 @@ HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) auto retRd = result->Read(arrayBuffer, opt); EXPECT_FALSE(retRd.IsSuccess()); + delete buffer; auto retCs = result->Close(); ASSERT_TRUE(retCs.IsSuccess()); - GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end FsStreamReadTest_0001"; + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; } } // namespace ModuleFileIO diff --git a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp index 6c13b0e6e..069a3230e 100644 --- a/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp +++ b/interfaces/test/unittest/js/mod_hash/hash_core_test.cpp @@ -18,22 +18,22 @@ #include #include "hash_core.h" -#define FILE_PATH "/data/test/HashCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; + +static const string g_filePath = "/data/test/HashCoreTest.txt"; class HashCoreTest : public testing::Test { public: static void SetUpTestCase(void) { - int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { - rmdir(FILE_PATH); + rmdir(g_filePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -51,7 +51,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0001"; string alg = "sha128"; - auto ret = HashCore::DoHash(FILE_PATH, alg); + auto ret = HashCore::DoHash(g_filePath, alg); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -71,7 +71,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0002"; - auto ret = HashCore::DoHash(FILE_PATH, "md5"); + auto ret = HashCore::DoHash(g_filePath, "md5"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0002"; @@ -88,7 +88,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0003"; - auto ret = HashCore::DoHash(FILE_PATH, "sha1"); + auto ret = HashCore::DoHash(g_filePath, "sha1"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0003"; @@ -105,7 +105,7 @@ HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1) HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0004"; - auto ret = HashCore::DoHash(FILE_PATH, "sha256"); + auto ret = HashCore::DoHash(g_filePath, "sha256"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004"; diff --git a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp index ae28da157..f320af037 100644 --- a/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp +++ b/interfaces/test/unittest/js/mod_securitylabel/securitylabel_core_test.cpp @@ -18,23 +18,25 @@ #include #include "securitylabel_core.h" -#define FILE_PATH "/data/test/SecurityLabelCoreTest.txt" - namespace OHOS { namespace FileManagement { namespace ModuleSecurityLabel { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; + +static const string g_filePath = "/data/test/SecurityLabelCoreTest.txt"; +static const string g_validFilePath = "/data/test/validFilePath"; + class SecurityLabelCoreTest : public testing::Test { public: static void SetUpTestCase(void) { - int32_t fd = open(FILE_PATH, O_CREAT | O_RDWR, 0644); + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); close(fd); }; static void TearDownTestCase() { - rmdir(FILE_PATH); + rmdir(g_filePath.c_str()); }; void SetUp() {}; void TearDown() {}; @@ -51,7 +53,7 @@ public: HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0001"; - auto ret = DoSetSecurityLabel(FILE_PATH, "abc"); + auto ret = DoSetSecurityLabel(g_filePath, "abc"); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -70,8 +72,8 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize. */ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetDoSetSecurityLabel_0002SecurityLabel_0001"; - auto ret = DoSetSecurityLabel("FILE_PATH", "s1"); + GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0002"; + auto ret = DoSetSecurityLabel(g_validFilePath, "s1"); EXPECT_FALSE(ret.IsSuccess()); auto err = ret.GetError(); @@ -91,7 +93,7 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0003"; - auto ret = DoSetSecurityLabel(FILE_PATH, "s2"); + auto ret = DoSetSecurityLabel(g_filePath, "s2"); ASSERT_TRUE(ret.IsSuccess()); GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0003"; @@ -108,7 +110,7 @@ HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0001"; - auto ret = DoGetSecurityLabel("FILE_PATH"); + auto ret = DoGetSecurityLabel(g_validFilePath); EXPECT_TRUE(ret.IsSuccess()); const string level = ret.GetData().value(); @@ -128,7 +130,7 @@ HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize. HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0002"; - auto ret = DoGetSecurityLabel(FILE_PATH); + auto ret = DoGetSecurityLabel(g_filePath); EXPECT_TRUE(ret.IsSuccess()); const string level = ret.GetData().value(); -- Gitee