1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <filesystem>
17 #include <fstream>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "unlink_core.h"
23 #include "uv_fs_mock.h"
24
25 namespace OHOS::FileManagement::ModuleFileIO::Test {
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace std;
29
30 class UnlinkCoreMockTest : public testing::Test {
31 public:
32 static filesystem::path tempFilePath;
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 static inline shared_ptr<UvfsMock> uvMock = nullptr;
38 };
39
40 filesystem::path UnlinkCoreMockTest::tempFilePath;
41
SetUpTestCase(void)42 void UnlinkCoreMockTest::SetUpTestCase(void)
43 {
44 GTEST_LOG_(INFO) << "SetUpTestCase";
45 tempFilePath = filesystem::temp_directory_path() / "unlink_test_file.txt";
46 ofstream(tempFilePath) << "unlink_test_file";
47 uvMock = std::make_shared<UvfsMock>();
48 Uvfs::ins = uvMock;
49 }
50
TearDownTestCase(void)51 void UnlinkCoreMockTest::TearDownTestCase(void)
52 {
53 GTEST_LOG_(INFO) << "TearDownTestCase";
54 filesystem::remove(tempFilePath);
55 Uvfs::ins = nullptr;
56 uvMock = nullptr;
57 }
58
SetUp(void)59 void UnlinkCoreMockTest::SetUp(void)
60 {
61 GTEST_LOG_(INFO) << "SetUp";
62 }
63
TearDown(void)64 void UnlinkCoreMockTest::TearDown(void)
65 {
66 GTEST_LOG_(INFO) << "TearDown";
67 }
68
69 /**
70 * @tc.name: UnlinkCoreMockTest_DoUnlink_001
71 * @tc.desc: Test function of UnlinkCore::DoUnlink interface for SUCCESS.
72 * @tc.size: MEDIUM
73 * @tc.type: FUNC
74 * @tc.level Level 1
75 */
76 HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_001, testing::ext::TestSize.Level1)
77 {
78 GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_001";
79
80 EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(1));
81
82 string path = tempFilePath.string();
83 auto res = UnlinkCore::DoUnlink(path);
84 EXPECT_EQ(res.IsSuccess(), true);
85
86 GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_001";
87 }
88
89 /**
90 * @tc.name: UnlinkCoreMockTest_DoUnlink_002
91 * @tc.desc: Test function of UnlinkCore::DoUnlink interface for FAILED.
92 * @tc.size: MEDIUM
93 * @tc.type: FUNC
94 * @tc.level Level 1
95 */
96 HWTEST_F(UnlinkCoreMockTest, UnlinkCoreMockTest_DoUnlink_002, testing::ext::TestSize.Level1)
97 {
98 GTEST_LOG_(INFO) << "UnlinkCoreMockTest-begin UnlinkCoreMockTest_DoUnlink_002";
99
100 EXPECT_CALL(*uvMock, uv_fs_unlink(_, _, _, _)).WillOnce(Return(-1));
101
102 string path = tempFilePath.string();
103 auto res = UnlinkCore::DoUnlink(path);
104 EXPECT_EQ(res.IsSuccess(), false);
105
106 GTEST_LOG_(INFO) << "UnlinkCoreMockTest-end UnlinkCoreMockTest_DoUnlink_002";
107 }
108
109 } // namespace OHOS::FileManagement::ModuleFileIO::Test