• 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 "file_entity.h"
17 #include "fs_file.h"
18 #include "system_mock.h"
19 #include "uv_fs_mock.h"
20 
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23 
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28 
29 class FsFileMockTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     static inline shared_ptr<UvfsMock> uvMock = nullptr;
36     static inline shared_ptr<SystemMock> sys = nullptr;
37     std::unique_ptr<FileEntity> fileEntity;
38     std::unique_ptr<FsFile> fsFile;
39 };
40 
SetUpTestCase(void)41 void FsFileMockTest::SetUpTestCase(void)
42 {
43     GTEST_LOG_(INFO) << "SetUpTestCase";
44     uvMock = std::make_shared<UvfsMock>();
45     Uvfs::ins = uvMock;
46     sys = std::make_shared<SystemMock>();
47     System::ins = sys;
48 }
49 
TearDownTestCase(void)50 void FsFileMockTest::TearDownTestCase(void)
51 {
52     GTEST_LOG_(INFO) << "TearDownTestCase";
53     Uvfs::ins = nullptr;
54     uvMock = nullptr;
55     System::ins = nullptr;
56     sys = nullptr;
57 }
58 
SetUp(void)59 void FsFileMockTest::SetUp(void)
60 {
61     GTEST_LOG_(INFO) << "SetUp";
62 
63     fileEntity = std::make_unique<FileEntity>();
64     const int fdValue = 3;
65     const bool isClosed = false;
66     fileEntity->fd_ = std::make_unique<DistributedFS::FDGuard>(fdValue, isClosed);
67     fileEntity->path_ = "/data/test/file_test.txt";
68     fileEntity->uri_ = "";
69     fsFile = std::make_unique<FsFile>(std::move(fileEntity));
70 }
71 
TearDown(void)72 void FsFileMockTest::TearDown(void)
73 {
74     GTEST_LOG_(INFO) << "TearDown";
75 }
76 
77 /**
78  * @tc.name: FsFileMockTest_GetPath_001
79  * @tc.desc: Test function of GetPath() interface for SUCCESS.
80  * @tc.size: MEDIUM
81  * @tc.type: FUNC
82  * @tc.level Level 1
83  */
84 HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_001, testing::ext::TestSize.Level1)
85 {
86     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_001";
87 
88     uv_fs_t mock_req;
89     mock_req.ptr = const_cast<char*>("/data/test/file_test.txt");
90 
91     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _))
__anonca3f149e0102(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 92         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
93             *req = mock_req;
94             return 0;
95         }));
96     auto result = fsFile->GetPath();
97     EXPECT_EQ(result.IsSuccess(), true);
98 
99     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_001";
100 }
101 
102 /**
103  * @tc.name: FsFileMockTest_GetPath_002
104  * @tc.desc: Test function of GetPath() interface for ERROR.
105  * @tc.size: MEDIUM
106  * @tc.type: FUNC
107  * @tc.level Level 1
108  */
109 HWTEST_F(FsFileMockTest, FsFileMockTest_GetPath_002, testing::ext::TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetPath_002";
112 
113     fsFile->fileEntity->path_ = "/invalid/path";
114     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1));
115     auto result = fsFile->GetPath();
116     EXPECT_EQ(result.IsSuccess(), false);
117 
118     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetPath_002";
119 }
120 
121 /**
122  * @tc.name: FsFileMockTest_GetName_003
123  * @tc.desc: Test function of GetName() interface for ERROR.
124  * @tc.size: MEDIUM
125  * @tc.type: FUNC
126  * @tc.level Level 1
127  */
128 HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_003, testing::ext::TestSize.Level1)
129 {
130     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_003";
131 
132     fsFile->fileEntity->path_ = "/invalid/path";
133     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1));
134     auto result = fsFile->GetName();
135     EXPECT_EQ(result.IsSuccess(), false);
136 
137     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_003";
138 }
139 
140 /**
141  * @tc.name: FsFileMockTest_GetName_004
142  * @tc.desc: Test function of GetName() interface for ERROR.
143  * @tc.size: MEDIUM
144  * @tc.type: FUNC
145  * @tc.level Level 1
146  */
147 HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_004, testing::ext::TestSize.Level1)
148 {
149     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_004";
150 
151     fsFile->fileEntity->path_ = "file_test.txt";
152     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1));
153     auto result = fsFile->GetName();
154     EXPECT_EQ(result.IsSuccess(), false);
155 
156     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_004";
157 }
158 
159 /**
160  * @tc.name: FsFileMockTest_GetParent_005
161  * @tc.desc: Test function of GetParent() interface for ERROR.
162  * @tc.size: MEDIUM
163  * @tc.type: FUNC
164  * @tc.level Level 1
165  */
166 HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_005, testing::ext::TestSize.Level1)
167 {
168     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_005";
169 
170     fsFile->fileEntity->path_ = "/invalid/path";
171     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _)).WillOnce(Return(-1));
172     auto result = fsFile->GetParent();
173     EXPECT_EQ(result.IsSuccess(), false);
174 
175     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_005";
176 }
177 
178 /**
179  * @tc.name: FsFileMockTest_GetName_006
180  * @tc.desc: Test function of GetName() interface for SUCCESS.
181  * @tc.size: MEDIUM
182  * @tc.type: FUNC
183  * @tc.level Level 1
184  */
185 HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_006, testing::ext::TestSize.Level1)
186 {
187     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_006";
188 
189     uv_fs_t mock_req;
190     mock_req.ptr = const_cast<char*>("/file_test.txt");
191 
192     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _))
__anonca3f149e0202(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 193         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
194             *req = mock_req;
195             return 0;
196         }));
197     auto result = fsFile->GetName();
198     EXPECT_EQ(result.IsSuccess(), true);
199 
200     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_006";
201 }
202 
203 /**
204  * @tc.name: FsFileMockTest_GetParent_007
205  * @tc.desc: Test function of GetParent() interface for SUCCESS.
206  * @tc.size: MEDIUM
207  * @tc.type: FUNC
208  * @tc.level Level 1
209  */
210 HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_007, testing::ext::TestSize.Level1)
211 {
212     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_007";
213 
214     uv_fs_t mock_req;
215     mock_req.ptr = const_cast<char*>("/test/dir_test");
216 
217     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _))
__anonca3f149e0302(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 218         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
219             *req = mock_req;
220             return 0;
221         }));
222     auto result = fsFile->GetParent();
223     EXPECT_EQ(result.IsSuccess(), true);
224 
225     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_007";
226 }
227 
228 /**
229  * @tc.name: FsFileMockTest_GetName_008
230  * @tc.desc: Test function of GetName() interface for FALSE.
231  * @tc.size: MEDIUM
232  * @tc.type: FUNC
233  * @tc.level Level 1
234  */
235 HWTEST_F(FsFileMockTest, FsFileMockTest_GetName_008, testing::ext::TestSize.Level1)
236 {
237     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetName_008";
238 
239     uv_fs_t mock_req;
240     mock_req.ptr = const_cast<char*>("file_test.txt");
241 
242     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _))
__anonca3f149e0402(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 243         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
244             *req = mock_req;
245             return 0;
246         }));
247     auto result = fsFile->GetName();
248     EXPECT_EQ(result.IsSuccess(), false);
249 
250     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetName_008";
251 }
252 
253 /**
254  * @tc.name: FsFileMockTest_GetParent_009
255  * @tc.desc: Test function of GetParent() interface for FALSE.
256  * @tc.size: MEDIUM
257  * @tc.type: FUNC
258  * @tc.level Level 1
259  */
260 HWTEST_F(FsFileMockTest, FsFileMockTest_GetParent_009, testing::ext::TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_GetParent_009";
263 
264     uv_fs_t mock_req;
265     mock_req.ptr = const_cast<char*>("dir_test");
266 
267     EXPECT_CALL(*uvMock, uv_fs_realpath(_, _, _, _))
__anonca3f149e0502(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 268         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
269             *req = mock_req;
270             return 0;
271         }));
272     auto result = fsFile->GetParent();
273     EXPECT_EQ(result.IsSuccess(), false);
274 
275     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_GetParent_009";
276 }
277 
278 /**
279  * @tc.name: FsFileMockTest_Lock_010
280  * @tc.desc: Test function of Lock() interface for SUCCESS.
281  * @tc.size: MEDIUM
282  * @tc.type: FUNC
283  * @tc.level Level 1
284  */
285 HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_010, testing::ext::TestSize.Level1)
286 {
287     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_010";
288 
289     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1));
290     auto result = fsFile->Lock(true);
291     EXPECT_EQ(result.IsSuccess(), true);
292 
293     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_010";
294 }
295 
296 /**
297  * @tc.name: FsFileMockTest_Lock_011
298  * @tc.desc: Test function of Lock() interface for FALSE.
299  * @tc.size: MEDIUM
300  * @tc.type: FUNC
301  * @tc.level Level 1
302  */
303 HWTEST_F(FsFileMockTest, FsFileMockTest_Lock_011, testing::ext::TestSize.Level1)
304 {
305     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_Lock_011";
306 
307     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1));
308     auto result = fsFile->Lock(false);
309     EXPECT_EQ(result.IsSuccess(), false);
310 
311     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_Lock_011";
312 }
313 
314 /**
315  * @tc.name: FsFileMockTest_TryLock_012
316  * @tc.desc: Test function of TryLock() interface for SUCCESS.
317  * @tc.size: MEDIUM
318  * @tc.type: FUNC
319  * @tc.level Level 1
320  */
321 HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_012, testing::ext::TestSize.Level1)
322 {
323     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_012";
324 
325     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1));
326     auto result = fsFile->TryLock(true);
327     EXPECT_EQ(result.IsSuccess(), true);
328 
329     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_012";
330 }
331 
332 /**
333  * @tc.name: FsFileMockTest_TryLock_013
334  * @tc.desc: Test function of TryLock() interface for FALSE.
335  * @tc.size: MEDIUM
336  * @tc.type: FUNC
337  * @tc.level Level 1
338  */
339 HWTEST_F(FsFileMockTest, FsFileMockTest_TryLock_013, testing::ext::TestSize.Level1)
340 {
341     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_TryLock_013";
342 
343     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1));
344     auto result = fsFile->TryLock(false);
345     EXPECT_EQ(result.IsSuccess(), false);
346 
347     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_TryLock_013";
348 }
349 
350 /**
351  * @tc.name: FsFileMockTest_UnLock_014
352  * @tc.desc: Test function of UnLock() interface for SUCCESS.
353  * @tc.size: MEDIUM
354  * @tc.type: FUNC
355  * @tc.level Level 1
356  */
357 HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_014, testing::ext::TestSize.Level1)
358 {
359     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_014";
360 
361     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(1));
362     auto result = fsFile->UnLock();
363     EXPECT_EQ(result.IsSuccess(), true);
364 
365     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_014";
366 }
367 
368 /**
369  * @tc.name: FsFileMockTest_UnLock_015
370  * @tc.desc: Test function of UnLock() interface for FALSE.
371  * @tc.size: MEDIUM
372  * @tc.type: FUNC
373  * @tc.level Level 1
374  */
375 HWTEST_F(FsFileMockTest, FsFileMockTest_UnLock_015, testing::ext::TestSize.Level1)
376 {
377     GTEST_LOG_(INFO) << "FsFileMockTest-begin FsFileMockTest_UnLock_015";
378 
379     EXPECT_CALL(*sys, flock(_, _)).WillOnce(Return(-1));
380     auto result = fsFile->UnLock();
381     EXPECT_EQ(result.IsSuccess(), false);
382 
383     GTEST_LOG_(INFO) << "FsFileMockTest-end FsFileMockTest_UnLock_015";
384 }
385 
386 }