• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include "file_utils.h"
22 #include "filemgmt_libhilog.h"
23 #include "fs_err_code.h"
24 #include "fs_file_watcher.h"
25 #include "fs_watcher.h"
26 #include "inotify_mock.h"
27 #include "unistd_mock.h"
28 
29 namespace OHOS {
30 namespace FileManagement {
31 namespace ModuleFileIO {
32 namespace Test {
33 
34 class FsWatcherMockTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37     static void TearDownTestCase(void);
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void FsWatcherMockTest::SetUpTestCase(void)
43 {
44     GTEST_LOG_(INFO) << "SetUpTestCase";
45     InotifyMock::EnableMock();
46     UnistdMock::EnableMock();
47 }
48 
TearDownTestCase(void)49 void FsWatcherMockTest::TearDownTestCase(void)
50 {
51     InotifyMock::DisableMock();
52     UnistdMock::DisableMock();
53     GTEST_LOG_(INFO) << "TearDownTestCase";
54 }
55 
SetUp(void)56 void FsWatcherMockTest::SetUp(void)
57 {
58     GTEST_LOG_(INFO) << "SetUp";
59     errno = 0; // Reset errno
60 }
61 
TearDown(void)62 void FsWatcherMockTest::TearDown(void)
63 {
64     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
65     watcher.taskRunning_ = false;
66     watcher.run_ = false;
67     watcher.reading_ = false;
68     watcher.closed_ = false;
69     watcher.notifyFd_ = -1;
70     watcher.eventFd_ = -1;
71     watcher.dataCache_.ClearCache();
72     GTEST_LOG_(INFO) << "TearDown";
73 }
74 
75 /**
76  * @tc.name: FsWatcherTest_Constructor_001
77  * @tc.desc: Test function of FsWatcher::Constructor interface for SUCCESS.
78  * @tc.size: MEDIUM
79  * @tc.type: FUNC
80  * @tc.level Level 0
81  */
82 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Constructor_001, testing::ext::TestSize.Level0)
83 {
84     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Constructor_001";
85     // Do testing
86     auto result = FsWatcher::Constructor();
87     // Verify results
88     EXPECT_TRUE(result.IsSuccess());
89     auto *watcher = result.GetData().value();
90     EXPECT_NE(watcher, nullptr);
91     if (watcher) {
92         auto *watcherEntity = watcher->GetWatchEntity();
93         EXPECT_NE(watcherEntity, nullptr);
94     }
95     delete watcher;
96     watcher = nullptr;
97     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Constructor_001";
98 }
99 
100 /**
101  * @tc.name: FsWatcherTest_Start_001
102  * @tc.desc: Test function of FsWatcher::Start interface for SUCCESS.
103  * @tc.size: MEDIUM
104  * @tc.type: FUNC
105  * @tc.level Level 0
106  */
107 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_001, testing::ext::TestSize.Level0)
108 {
109     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_001";
110     // Prepare test condition
111     int32_t expectedWd = 100;
112     auto watchEntity = CreateUniquePtr<FsWatchEntity>();
113     FsWatcher fsWatcher(std::move(watchEntity));
114     std::shared_ptr<WatcherInfo> info = std::make_shared<WatcherInfo>(nullptr);
115     fsWatcher.GetWatchEntity()->data_ = info;
116     // Prepare test condition for FsFileWatcher
117     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
118     watcher.notifyFd_ = 1;       // Valid notifyFd
119     watcher.taskRunning_ = true; // Avoid starting thread
120     // Set mock behaviors
121     auto inotifyMock = InotifyMock::GetMock();
122     EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_))
123         .Times(1)
124         .WillOnce(testing::Return(expectedWd));
125     // Do testing
126     auto result = fsWatcher.Start();
127     // Verify results
128     testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
129     EXPECT_TRUE(result.IsSuccess());
130     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_001";
131 }
132 
133 /**
134  * @tc.name: FsWatcherTest_Start_002
135  * @tc.desc: Test function of FsWatcher::Start interface for FAILURE when watchEntity is nullptr.
136  * @tc.size: MEDIUM
137  * @tc.type: FUNC
138  * @tc.level Level 1
139  */
140 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_002, testing::ext::TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_002";
143     // Prepare test condition
144     FsWatcher fsWatcher(nullptr);
145     // Do testing
146     auto result = fsWatcher.Start();
147     // Verify results
148     EXPECT_FALSE(result.IsSuccess());
149     auto errCode = result.GetError().GetErrNo();
150     EXPECT_EQ(errCode, E_INVAL_CODE);
151     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_002";
152 }
153 
154 /**
155  * @tc.name: FsWatcherTest_Start_003
156  * @tc.desc: Test function of FsWatcher::Start interface for FAILURE when StartNotify fails.
157  * @tc.size: MEDIUM
158  * @tc.type: FUNC
159  * @tc.level Level 1
160  */
161 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Start_003, testing::ext::TestSize.Level1)
162 {
163     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Start_003";
164     // Prepare test condition
165     auto watchEntity = CreateUniquePtr<FsWatchEntity>();
166     FsWatcher fsWatcher(std::move(watchEntity));
167     std::shared_ptr<WatcherInfo> info = std::make_shared<WatcherInfo>(nullptr);
168     fsWatcher.GetWatchEntity()->data_ = info;
169     // Prepare test condition for FsFileWatcher
170     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
171     watcher.notifyFd_ = -1; // Invalid notifyFd
172     // Do testing
173     auto result = fsWatcher.Start();
174     // Verify results
175     EXPECT_FALSE(result.IsSuccess());
176     auto errCode = result.GetError().GetErrNo();
177     EXPECT_EQ(errCode, E_IO_CODE);
178     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Start_003";
179 }
180 
181 /**
182  * @tc.name: FsWatcherTest_Stop_001
183  * @tc.desc: Test function of FsWatcher::Stop interface for SUCCESS.
184  * @tc.size: MEDIUM
185  * @tc.type: FUNC
186  * @tc.level Level 0
187  */
188 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_001, testing::ext::TestSize.Level0)
189 {
190     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_001";
191     // Prepare test condition
192     auto watchEntity = CreateUniquePtr<FsWatchEntity>();
193     FsWatcher fsWatcher(std::move(watchEntity));
194     std::shared_ptr<WatcherInfo> info = std::make_shared<WatcherInfo>(nullptr);
195     fsWatcher.GetWatchEntity()->data_ = info;
196     // Prepare test condition for FsFileWatcher
197     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
198     watcher.notifyFd_ = 1;
199     // Set mock behaviors
200     auto inotifyMock = InotifyMock::GetMock();
201     auto unistdMock = UnistdMock::GetMock();
202     EXPECT_CALL(*inotifyMock, inotify_rm_watch(testing::_, testing::_))
203         .Times(1)
204         .WillOnce(testing::SetErrnoAndReturn(0, 0));
205     EXPECT_CALL(*unistdMock, close(testing::_)).Times(2).WillRepeatedly(testing::Return(0));
206     // Do testing
207     auto result = fsWatcher.Stop();
208     // Verify results
209     testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
210     testing::Mock::VerifyAndClearExpectations(unistdMock.get());
211     EXPECT_TRUE(result.IsSuccess());
212     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_001";
213 }
214 
215 /**
216  * @tc.name: FsWatcherTest_Stop_002
217  * @tc.desc: Test function of FsWatcher::Stop interface for FAILURE when watchEntity is nullptr.
218  * @tc.size: MEDIUM
219  * @tc.type: FUNC
220  * @tc.level Level 1
221  */
222 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_002, testing::ext::TestSize.Level1)
223 {
224     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_002";
225     // Prepare test condition
226     FsWatcher fsWatcher(nullptr);
227     // Do testing
228     auto result = fsWatcher.Stop();
229     // Verify results
230     EXPECT_FALSE(result.IsSuccess());
231     auto errCode = result.GetError().GetErrNo();
232     EXPECT_EQ(errCode, E_INVAL_CODE);
233     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_002";
234 }
235 
236 /**
237  * @tc.name: FsWatcherTest_Stop_003
238  * @tc.desc: Test function of FsWatcher::Stop interface for FAILURE when StopNotify fails.
239  * @tc.size: MEDIUM
240  * @tc.type: FUNC
241  * @tc.level Level 1
242  */
243 HWTEST_F(FsWatcherMockTest, FsWatcherTest_Stop_003, testing::ext::TestSize.Level1)
244 {
245     GTEST_LOG_(INFO) << "FsWatcherMockTest-begin FsWatcherTest_Stop_003";
246     // Prepare test condition
247     auto watchEntity = CreateUniquePtr<FsWatchEntity>();
248     FsWatcher fsWatcher(std::move(watchEntity));
249     std::shared_ptr<WatcherInfo> info = std::make_shared<WatcherInfo>(nullptr);
250     fsWatcher.GetWatchEntity()->data_ = info;
251     // Prepare test condition for FsFileWatcher
252     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
253     watcher.notifyFd_ = -1; // Invalid notifyFd
254     // Do testing
255     auto result = fsWatcher.Stop();
256     // Verify results
257     EXPECT_FALSE(result.IsSuccess());
258     auto errCode = result.GetError().GetErrNo();
259     EXPECT_EQ(errCode, E_IO_CODE);
260     GTEST_LOG_(INFO) << "FsWatcherMockTest-end FsWatcherTest_Stop_003";
261 }
262 
263 } // namespace Test
264 } // namespace ModuleFileIO
265 } // namespace FileManagement
266 } // namespace OHOS