• 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 "eventfd_mock.h"
22 #include "filemgmt_libhilog.h"
23 #include "fs_file_watcher.h"
24 #include "inotify_mock.h"
25 #include "mock_watcher_callback.h"
26 #include "watcher_core.h"
27 
28 namespace OHOS {
29 namespace FileManagement {
30 namespace ModuleFileIO {
31 namespace Test {
32 
33 class WatcherCoreMockTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase(void)41 void WatcherCoreMockTest::SetUpTestCase(void)
42 {
43     GTEST_LOG_(INFO) << "SetUpTestCase";
44     EventfdMock::EnableMock();
45     InotifyMock::EnableMock();
46 }
47 
TearDownTestCase(void)48 void WatcherCoreMockTest::TearDownTestCase(void)
49 {
50     EventfdMock::DisableMock();
51     InotifyMock::DisableMock();
52     GTEST_LOG_(INFO) << "TearDownTestCase";
53 }
54 
SetUp(void)55 void WatcherCoreMockTest::SetUp(void)
56 {
57     GTEST_LOG_(INFO) << "SetUp";
58     errno = 0; // Reset errno
59 }
60 
TearDown(void)61 void WatcherCoreMockTest::TearDown(void)
62 {
63     FsFileWatcher &watcher = FsFileWatcher::GetInstance();
64     watcher.taskRunning_ = false;
65     watcher.run_ = false;
66     watcher.reading_ = false;
67     watcher.closed_ = false;
68     watcher.notifyFd_ = -1;
69     watcher.eventFd_ = -1;
70     watcher.dataCache_.ClearCache();
71     GTEST_LOG_(INFO) << "TearDown";
72 }
73 
74 /**
75  * @tc.name: WatcherCoreMockTest_DoCreateWatcher_001
76  * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for SUCCESS.
77  * @tc.size: MEDIUM
78  * @tc.type: FUNC
79  * @tc.level Level 0
80  */
81 HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_001, testing::ext::TestSize.Level0)
82 {
83     GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_001";
84     // Prepare test parameters
85     std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_001";
86     int32_t events = IN_CREATE;
87     std::shared_ptr<MockWatcherCallback> callback = std::make_shared<MockWatcherCallback>();
88     // Set mock behaviors
89     auto inotifyMock = InotifyMock::GetMock();
90     auto eventfdMock = EventfdMock::GetMock();
91     EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::Return(1));
92     EXPECT_CALL(*eventfdMock, eventfd(testing::_, testing::_)).Times(1).WillOnce(testing::Return(2));
93     // Do testing
94     auto result = WatcherCore::DoCreateWatcher(path, events, callback);
95     // Verify results
96     testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
97     testing::Mock::VerifyAndClearExpectations(eventfdMock.get());
98     EXPECT_TRUE(result.IsSuccess());
99     GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_001";
100 }
101 
102 /**
103  * @tc.name: WatcherCoreMockTest_DoCreateWatcher_002
104  * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when InitNotify fails.
105  * @tc.size: MEDIUM
106  * @tc.type: FUNC
107  * @tc.level Level 1
108  */
109 HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_002, testing::ext::TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_002";
112     // Prepare test parameters
113     std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_002";
114     int32_t events = IN_CREATE;
115     std::shared_ptr<MockWatcherCallback> callback = std::make_shared<MockWatcherCallback>();
116     // Set mock behaviors
117     auto inotifyMock = InotifyMock::GetMock();
118     EXPECT_CALL(*inotifyMock, inotify_init()).Times(1).WillOnce(testing::SetErrnoAndReturn(EIO, -1));
119     // Do testing
120     auto result = WatcherCore::DoCreateWatcher(path, events, callback);
121     // Verify results
122     testing::Mock::VerifyAndClearExpectations(inotifyMock.get());
123     EXPECT_FALSE(result.IsSuccess());
124     GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_002";
125 }
126 
127 /**
128  * @tc.name: WatcherCoreMockTest_DoCreateWatcher_003
129  * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when events are invalid.
130  * @tc.size: MEDIUM
131  * @tc.type: FUNC
132  * @tc.level Level 1
133  */
134 HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_003, testing::ext::TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_003";
137     // Prepare test parameters
138     std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_003";
139     int32_t events = -1;
140     std::shared_ptr<MockWatcherCallback> callback = std::make_shared<MockWatcherCallback>();
141     // Do testing
142     auto result = WatcherCore::DoCreateWatcher(path, events, callback);
143     // Verify results
144     EXPECT_FALSE(result.IsSuccess());
145     GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_003";
146 }
147 
148 /**
149  * @tc.name: WatcherCoreMockTest_DoCreateWatcher_004
150  * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when CheckEventValid false.
151  * @tc.size: MEDIUM
152  * @tc.type: FUNC
153  * @tc.level Level 1
154  */
155 HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_004, testing::ext::TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_004";
158     // Prepare test parameters
159     std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_004";
160     int32_t invalidEvents = ~IN_ALL_EVENTS;
161     std::shared_ptr<MockWatcherCallback> callback = std::make_shared<MockWatcherCallback>();
162     // Do testing
163     auto result = WatcherCore::DoCreateWatcher(path, invalidEvents, callback);
164     // Verify results
165     EXPECT_FALSE(result.IsSuccess());
166     GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_004";
167 }
168 
169 /**
170  * @tc.name: WatcherCoreMockTest_DoCreateWatcher_005
171  * @tc.desc: Test function of WatcherCore::DoCreateWatcher interface for FAILURE when callback is null.
172  * @tc.size: MEDIUM
173  * @tc.type: FUNC
174  * @tc.level Level 1
175  */
176 HWTEST_F(WatcherCoreMockTest, WatcherCoreMockTest_DoCreateWatcher_005, testing::ext::TestSize.Level1)
177 {
178     GTEST_LOG_(INFO) << "WatcherCoreMockTest-begin WatcherCoreMockTest_DoCreateWatcher_005";
179     // Prepare test parameters
180     std::string path = "/test/WatcherCoreMockTest_DoCreateWatcher_005";
181     int32_t events = IN_CREATE;
182     std::shared_ptr<MockWatcherCallback> callback = nullptr;
183     // Do testing
184     auto result = WatcherCore::DoCreateWatcher(path, events, callback);
185     // Verify results
186     EXPECT_FALSE(result.IsSuccess());
187     GTEST_LOG_(INFO) << "WatcherCoreMockTest-end WatcherCoreMockTest_DoCreateWatcher_005";
188 }
189 
190 } // namespace Test
191 } // namespace ModuleFileIO
192 } // namespace FileManagement
193 } // namespace OHOS