• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "fuse_operations.h"
20 #include "cloud_disk_inode.h"
21 #include "cloud_file_utils.h"
22 #include "file_operations_helper.h"
23 #include "file_operations_local.h"
24 #include "file_operations_base.h"
25 #include "fuse_ioctl.h"
26 #include "parameters.h"
27 #include "utils_log.h"
28 #include "assistant.h"
29 
30 namespace OHOS::FileManagement::CloudDisk::Test {
31 using namespace testing;
32 using namespace testing::ext;
33 using namespace std;
34 using namespace CloudFile;
35 
36 class FileOperationsLocalTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     static inline FileOperationsLocal* fileoperationslocal_ = new FileOperationsLocal();
43     static inline shared_ptr<AssistantMock> insMock = nullptr;
44 };
45 
SetUpTestCase(void)46 void FileOperationsLocalTest::SetUpTestCase(void)
47 {
48     GTEST_LOG_(INFO) << "SetUpTestCase";
49     insMock = make_shared<AssistantMock>();
50     Assistant::ins = insMock;
51 }
52 
TearDownTestCase(void)53 void FileOperationsLocalTest::TearDownTestCase(void)
54 {
55     GTEST_LOG_(INFO) << "TearDownTestCase";
56     fileoperationslocal_ = nullptr;
57     Assistant::ins = nullptr;
58     insMock = nullptr;
59 }
60 
SetUp(void)61 void FileOperationsLocalTest::SetUp(void)
62 {
63     GTEST_LOG_(INFO) << "SetUp";
64 }
65 
TearDown(void)66 void FileOperationsLocalTest::TearDown(void)
67 {
68     GTEST_LOG_(INFO) << "TearDown";
69 }
70 
71 /**
72  * @tc.name: LookUpTest001
73  * @tc.desc: Verify the LookUp function
74  * @tc.type: FUNC
75  * @tc.require: issuesI92WQP
76  */
77 HWTEST_F(FileOperationsLocalTest, LookUpTest001, TestSize.Level1)
78 {
79     GTEST_LOG_(INFO) << "LookUpTest001 Start";
80     try {
81         CloudDiskFuseData data;
82         data.userId = 0;
83         fuse_req_t req = nullptr;
84         fuse_ino_t parent = 1;
85         const char *name = "mock";
86         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
87         EXPECT_CALL(*insMock, fuse_reply_err(_, _)).WillOnce(Return(0));
88 
89         fileoperationslocal_->Lookup(req, parent, name);
90         EXPECT_TRUE(true);
91     } catch (...) {
92         EXPECT_TRUE(false);
93         GTEST_LOG_(INFO) << "LookUpTest001 ERROR";
94     }
95     GTEST_LOG_(INFO) << "LookUpTest001 End";
96 }
97 
98 /**
99  * @tc.name: LookUpTest002
100  * @tc.desc: Verify the LookUp function
101  * @tc.type: FUNC
102  * @tc.require: issuesI92WQP
103  */
104 HWTEST_F(FileOperationsLocalTest, LookUpTest002, TestSize.Level1)
105 {
106     GTEST_LOG_(INFO) << "LookUpTest001 Start";
107     try {
108         CloudDiskFuseData data;
109         data.userId = 100;
110         fuse_req_t req = nullptr;
111         fuse_ino_t parent = 0;
112         const char *name = "mock";
113         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
114         EXPECT_CALL(*insMock, fuse_reply_entry(_, _)).WillOnce(Return(0));
115 
116         fileoperationslocal_->Lookup(req, parent, name);
117         EXPECT_TRUE(true);
118     } catch (...) {
119         EXPECT_TRUE(false);
120         GTEST_LOG_(INFO) << "LookUpTest001 ERROR";
121     }
122     GTEST_LOG_(INFO) << "LookUpTest001 End";
123 }
124 
125 /**
126  * @tc.name: LookUpTest003
127  * @tc.desc: Verify the LookUp function
128  * @tc.type: FUNC
129  * @tc.require: issuesI92WQP
130  */
131 HWTEST_F(FileOperationsLocalTest, LookUpTest003, TestSize.Level1)
132 {
133     GTEST_LOG_(INFO) << "LookUpTest003 Start";
134     try {
135         CloudDiskFuseData data;
136         data.userId = 100;
137         fuse_req_t req = nullptr;
138         fuse_ino_t parent = 1;
139         const char *name = "mock";
140         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
141         EXPECT_CALL(*insMock, fuse_reply_entry(_, _)).WillOnce(Return(0));
142 
143         fileoperationslocal_->Lookup(req, parent, name);
144         EXPECT_TRUE(true);
145     } catch (...) {
146         EXPECT_TRUE(false);
147         GTEST_LOG_(INFO) << "LookUpTest003 ERROR";
148     }
149     GTEST_LOG_(INFO) << "LookUpTest003 End";
150 }
151 
152 /**
153  * @tc.name: LookUpTest004
154  * @tc.desc: Verify the LookUp function
155  * @tc.type: FUNC
156  * @tc.require: issuesI92WQP
157  */
158 HWTEST_F(FileOperationsLocalTest, LookUpTest004, TestSize.Level1)
159 {
160     GTEST_LOG_(INFO) << "LookUpTest004 Start";
161     try {
162         CloudDiskFuseData data;
163         data.userId = 100;
164         fuse_req_t req = nullptr;
165         fuse_ino_t parent = 100;
166         const char *name = "mock";
167         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
168         EXPECT_CALL(*insMock, fuse_reply_entry(_, _)).WillOnce(Return(0));
169 
170         fileoperationslocal_->Lookup(req, parent, name);
171         EXPECT_TRUE(true);
172     } catch (...) {
173         EXPECT_TRUE(false);
174         GTEST_LOG_(INFO) << "LookUpTest004 ERROR";
175     }
176     GTEST_LOG_(INFO) << "LookUpTest004 End";
177 }
178 
179 /**
180  * @tc.name: GetAttrTest001
181  * @tc.desc: Verify the GetAttr function
182  * @tc.type: FUNC
183  * @tc.require: issuesI92WQP
184  */
185 HWTEST_F(FileOperationsLocalTest, GetAttrTest001, TestSize.Level1)
186 {
187     GTEST_LOG_(INFO) << "GetAttrTest001 Start";
188     try {
189         CloudDiskFuseData data;
190         data.userId = 0;
191         fuse_req_t req = nullptr;
192         fuse_ino_t ino = FUSE_ROOT_ID;
193         struct fuse_file_info fi;
194         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
195         EXPECT_CALL(*insMock, fuse_reply_err(_, _)).WillOnce(Return(0));
196 
197         fileoperationslocal_->GetAttr(req, ino, &fi);
198         EXPECT_TRUE(true);
199     } catch (...) {
200         EXPECT_TRUE(false);
201         GTEST_LOG_(INFO) << "GetAttrTest001 ERROR";
202     }
203     GTEST_LOG_(INFO) << "GetAttrTest001 End";
204 }
205 
206 /**
207  * @tc.name: GetAttrTest002
208  * @tc.desc: Verify the GetAttr function
209  * @tc.type: FUNC
210  * @tc.require: issuesI92WQP
211  */
212 HWTEST_F(FileOperationsLocalTest, GetAttrTest002, TestSize.Level1)
213 {
214     GTEST_LOG_(INFO) << "GetAttrTest002 Start";
215     try {
216         CloudDiskFuseData data;
217         data.userId = 100;
218         fuse_req_t req = nullptr;
219         fuse_ino_t ino = FUSE_ROOT_ID;
220         struct fuse_file_info fi;
221         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
222         EXPECT_CALL(*insMock, fuse_reply_attr(_, _, _)).WillOnce(Return(0));
223 
224         fileoperationslocal_->GetAttr(req, ino, &fi);
225         EXPECT_TRUE(true);
226     } catch (...) {
227         EXPECT_TRUE(false);
228         GTEST_LOG_(INFO) << "GetAttrTest002 ERROR";
229     }
230     GTEST_LOG_(INFO) << "GetAttrTest002 End";
231 }
232 
233 /**
234  * @tc.name: GetAttrTest003
235  * @tc.desc: Verify the GetAttr function
236  * @tc.type: FUNC
237  * @tc.require: issuesI92WQP
238  */
239 HWTEST_F(FileOperationsLocalTest, GetAttrTest003, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "GetAttrTest003 Start";
242     try {
243         CloudDiskFuseData data;
244         data.userId = 100;
245         fuse_req_t req = nullptr;
246         fuse_ino_t ino = -1;
247         struct fuse_file_info fi;
248         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
249         EXPECT_CALL(*insMock, fuse_reply_err(_, _)).WillOnce(Return(0));
250 
251         fileoperationslocal_->GetAttr(req, ino, &fi);
252         EXPECT_TRUE(true);
253     } catch (...) {
254         EXPECT_TRUE(false);
255         GTEST_LOG_(INFO) << "GetAttrTest003 ERROR";
256     }
257     GTEST_LOG_(INFO) << "GetAttrTest003 End";
258 }
259 
260 /**
261  * @tc.name: GetAttrTest004
262  * @tc.desc: Verify the GetAttr function
263  * @tc.type: FUNC
264  * @tc.require: issuesI92WQP
265  */
266 HWTEST_F(FileOperationsLocalTest, GetAttrTest004, TestSize.Level1)
267 {
268     GTEST_LOG_(INFO) << "GetAttrTest004 Start";
269     try {
270         CloudDiskFuseData data;
271         data.userId = 100;
272         fuse_req_t req = nullptr;
273         fuse_ino_t ino = 0 ;
274         struct fuse_file_info fi;
275         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
276         EXPECT_CALL(*insMock, fuse_reply_attr(_, _, _)).WillOnce(Return(0));
277 
278         fileoperationslocal_->GetAttr(req, ino, &fi);
279         EXPECT_TRUE(true);
280     } catch (...) {
281         EXPECT_TRUE(false);
282         GTEST_LOG_(INFO) << "GetAttrTest004 ERROR";
283     }
284     GTEST_LOG_(INFO) << "GetAttrTest004 End";
285 }
286 
287 /**
288  * @tc.name: ReadDirTest001
289  * @tc.desc: Verify the ReadDir function
290  * @tc.type: FUNC
291  * @tc.require: issuesI92WQP
292  */
293 HWTEST_F(FileOperationsLocalTest, ReadDirTest001, TestSize.Level1)
294 {
295     GTEST_LOG_(INFO) << "ReadDirTest001 Start";
296     try {
297         CloudDiskFuseData data;
298         data.userId = 100;
299         fuse_req_t req = nullptr;
300         fuse_ino_t ino = FUSE_ROOT_ID;
301         size_t size = 0;
302         off_t off = 0;
303         struct fuse_file_info fi;
304         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
305         EXPECT_CALL(*insMock, fuse_reply_buf(_, _, _)).WillOnce(Return(0));
306 
307         fileoperationslocal_->ReadDir(req, ino, size, off, &fi);
308         EXPECT_TRUE(true);
309     } catch (...) {
310         EXPECT_TRUE(false);
311         GTEST_LOG_(INFO) << "ReadDirTest001 ERROR";
312     }
313     GTEST_LOG_(INFO) << "ReadDirTest001 End";
314 }
315 
316 /**
317  * @tc.name: ReadDirTest002
318  * @tc.desc: Verify the ReadDir function
319  * @tc.type: FUNC
320  * @tc.require: issuesI92WQP
321  */
322 HWTEST_F(FileOperationsLocalTest, ReadDirTest002, TestSize.Level1)
323 {
324     GTEST_LOG_(INFO) << "ReadDirTest002 Start";
325     try {
326         CloudDiskFuseData data;
327         data.userId = 0;
328         fuse_req_t req = nullptr;
329         fuse_ino_t ino = FUSE_ROOT_ID;
330         size_t size = 0;
331         off_t off = 0;
332         struct fuse_file_info fi;
333         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
334 
335         fileoperationslocal_->ReadDir(req, ino, size, off, &fi);
336         EXPECT_TRUE(true);
337     } catch (...) {
338         EXPECT_TRUE(false);
339         GTEST_LOG_(INFO) << "ReadDirTest002 ERROR";
340     }
341     GTEST_LOG_(INFO) << "ReadDirTest002 End";
342 }
343 
344 /**
345  * @tc.name: ReadDirTest003
346  * @tc.desc: Verify the ReadDir function
347  * @tc.type: FUNC
348  * @tc.require: issuesI92WQP
349  */
350 HWTEST_F(FileOperationsLocalTest, ReadDirTest003, TestSize.Level1)
351 {
352     GTEST_LOG_(INFO) << "ReadDirTest003 Start";
353     try {
354         CloudDiskFuseData data;
355         data.userId = 100;
356         fuse_req_t req = nullptr;
357         fuse_ino_t ino = -1;
358         size_t size = 0;
359         off_t off = 0;
360         struct fuse_file_info fi;
361         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
362         EXPECT_CALL(*insMock, fuse_reply_err(_, _)).WillOnce(Return(0));
363 
364         fileoperationslocal_->ReadDir(req, ino, size, off, &fi);
365         EXPECT_TRUE(true);
366     } catch (...) {
367         EXPECT_TRUE(false);
368         GTEST_LOG_(INFO) << "ReadDirTest003 ERROR";
369     }
370     GTEST_LOG_(INFO) << "ReadDirTest003 End";
371 }
372 
373 /**
374  * @tc.name: ReadDirTest004
375  * @tc.desc: Verify the ReadDir function
376  * @tc.type: FUNC
377  * @tc.require: issuesI92WQP
378  */
379 HWTEST_F(FileOperationsLocalTest, ReadDirTest004, TestSize.Level1)
380 {
381     GTEST_LOG_(INFO) << "ReadDirTest004 Start";
382     try {
383         CloudDiskFuseData data;
384         data.userId = 100;
385         fuse_req_t req = nullptr;
386         fuse_ino_t ino = 0;
387         size_t size = 0;
388         off_t off = 0;
389         struct fuse_file_info fi;
390         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
391 
392         fileoperationslocal_->ReadDir(req, ino, size, off, &fi);
393         EXPECT_TRUE(true);
394     } catch (...) {
395         EXPECT_TRUE(false);
396         GTEST_LOG_(INFO) << "ReadDirTest004 ERROR";
397     }
398     GTEST_LOG_(INFO) << "ReadDirTest004 End";
399 }
400 
401 /**
402  * @tc.name: ReadDirTest005
403  * @tc.desc: Verify the ReadDir function
404  * @tc.type: FUNC
405  * @tc.require: issuesI92WQP
406  */
407 HWTEST_F(FileOperationsLocalTest, ReadDirTest005, TestSize.Level1)
408 {
409     GTEST_LOG_(INFO) << "ReadDirTest005 Start";
410     try {
411         CloudDiskFuseData data;
412         data.userId = 1;
413         fuse_req_t req = nullptr;
414         fuse_ino_t ino = FUSE_ROOT_ID;
415         size_t size = 0;
416         off_t off = 0;
417         struct fuse_file_info fi;
418         EXPECT_CALL(*insMock, fuse_req_userdata(_)).WillOnce(Return(reinterpret_cast<void*>(&data)));
419         EXPECT_CALL(*insMock, fuse_reply_buf(_, _, _)).WillOnce(Return(0));
420 
421         fileoperationslocal_->ReadDir(req, ino, size, off, &fi);
422         EXPECT_TRUE(true);
423     } catch (...) {
424         EXPECT_TRUE(false);
425         GTEST_LOG_(INFO) << "ReadDirTest005 ERROR";
426     }
427     GTEST_LOG_(INFO) << "ReadDirTest005 End";
428 }
429 
430 /**
431  * @tc.name: IoctlTest001
432  * @tc.desc: Verify the Ioctl function
433  * @tc.type: FUNC
434  * @tc.require: nullptr
435  */
436 HWTEST_F(FileOperationsLocalTest, IoctlTest001, TestSize.Level1)
437 {
438     GTEST_LOG_(INFO) << "IoctlTest001 Start";
439     try {
440         CloudDiskFuseData data;
441         data.userId = 1;
442         fuse_req_t req = nullptr;
443         fuse_ino_t ino = FUSE_ROOT_ID;
444         int cmd = 0;
445         void *arg = nullptr;
446         struct fuse_file_info fi;
447         unsigned flags = 0;
448         void *inBuf = nullptr;
449         size_t inBufsz = 0;
450         size_t outBufsz = 0;
451         EXPECT_CALL(*insMock, fuse_reply_err(_, _)).WillOnce(Return(0));
452 
453         fileoperationslocal_->Ioctl(req, ino, cmd, arg, &fi, flags, inBuf, inBufsz, outBufsz);
454         EXPECT_TRUE(true);
455     } catch (...) {
456         EXPECT_TRUE(false);
457         GTEST_LOG_(INFO) << "IoctlTest001 ERROR";
458     }
459     GTEST_LOG_(INFO) << "IoctlTest001 End";
460 }
461 
462 /**
463  * @tc.name: IoctlTest002
464  * @tc.desc: Verify the Ioctl function
465  * @tc.type: FUNC
466  * @tc.require: nullptr
467  */
468 HWTEST_F(FileOperationsLocalTest, IoctlTest002, TestSize.Level1)
469 {
470     GTEST_LOG_(INFO) << "IoctlTest002 Start";
471     try {
472         CloudDiskFuseData data;
473         data.userId = 1;
474         fuse_req_t req = nullptr;
475         fuse_ino_t ino = FUSE_ROOT_ID;
476         int cmd = HMDFS_IOC_CLEAN_CACHE_DAEMON;
477         void *arg = nullptr;
478         struct fuse_file_info fi;
479         unsigned flags = 0;
480         void *inBuf = nullptr;
481         size_t inBufsz = 0;
482         size_t outBufsz = 0;
483         EXPECT_CALL(*insMock, fuse_reply_ioctl(_, _, _, _)).WillOnce(Return(0));
484 
485         fileoperationslocal_->Ioctl(req, ino, cmd, arg, &fi, flags, inBuf, inBufsz, outBufsz);
486         EXPECT_TRUE(true);
487     } catch (...) {
488         EXPECT_TRUE(false);
489         GTEST_LOG_(INFO) << "IoctlTest002 ERROR";
490     }
491     GTEST_LOG_(INFO) << "IoctlTest002 End";
492 }
493 } // namespace OHOS::FileManagement::CloudDisk::Test