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