• 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "../../../utils/dentry/src/meta_file_clouddisk.cpp"
20 #include "assistant.h"
21 
22 namespace OHOS::FileManagement {
23 using namespace testing;
24 using namespace testing::ext;
25 
26 constexpr uint32_t TEST_USER_ID = 201;
27 constexpr uint64_t TEST_ISIZE = 1024;
28 
29 class MetaFileCloudDiskTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     static inline std::shared_ptr<CloudDisk::AssistantMock> insMock = nullptr;
36 };
37 
SetUpTestCase(void)38 void MetaFileCloudDiskTest::SetUpTestCase(void)
39 {
40     GTEST_LOG_(INFO) << "SetUpTestCase";
41 }
42 
TearDownTestCase(void)43 void MetaFileCloudDiskTest::TearDownTestCase(void)
44 {
45     GTEST_LOG_(INFO) << "TearDownTestCase";
46 }
47 
SetUp(void)48 void MetaFileCloudDiskTest::SetUp(void)
49 {
50     GTEST_LOG_(INFO) << "SetUp";
51     insMock = std::make_shared<CloudDisk::AssistantMock>();
52     CloudDisk::Assistant::ins = insMock;
53 }
54 
TearDown(void)55 void MetaFileCloudDiskTest::TearDown(void)
56 {
57     GTEST_LOG_(INFO) << "TearDown";
58     CloudDisk::Assistant::ins = nullptr;
59     insMock = nullptr;
60 }
61 
62 /**
63  * @tc.name: SetFileType001
64  * @tc.desc: Verify the SetFileType function
65  * @tc.type: FUNC
66  */
67 HWTEST_F(MetaFileCloudDiskTest, SetFileType001, TestSize.Level1)
68 {
69     GTEST_LOG_(INFO) << "SetFileType001 Start";
70     try {
71         HmdfsDentry de;
72         MetaHelper::SetFileType(&de, FILE_TYPE_CONTENT);
73     } catch (...) {
74         EXPECT_TRUE(false);
75         GTEST_LOG_(INFO) << "SetFileType001 ERROR";
76     }
77     GTEST_LOG_(INFO) << "SetFileType001 End";
78 }
79 
80 /**
81  * @tc.name: FindDentryPage001
82  * @tc.desc: Verify the FindDentryPage function
83  * @tc.type: FUNC
84  */
85 HWTEST_F(MetaFileCloudDiskTest, FindDentryPage001, TestSize.Level1)
86 {
87     GTEST_LOG_(INFO) << "FindDentryPage001 Start";
88     try {
89         // init InitDcacheLookupCtx
90         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
91         MetaBase mBase("file1", "id1");
92         mBase.size = TEST_ISIZE;
93         DcacheLookupCtx ctx;
94         InitDcacheLookupCtx(&ctx, mBase, -1);
95 
96         // FindDentryPage -> nullptr || FilePosLock -> 1
97         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1));
98         auto dentryBlk = FindDentryPage(0, &ctx);
99         EXPECT_EQ(dentryBlk, nullptr);
100     } catch (...) {
101         EXPECT_TRUE(false);
102         GTEST_LOG_(INFO) << "FindDentryPage001 ERROR";
103     }
104     GTEST_LOG_(INFO) << "FindDentryPage001 End";
105 }
106 
107 /**
108  * @tc.name: FindDentryPage002
109  * @tc.desc: Verify the FindDentryPage function
110  * @tc.type: FUNC
111  */
112 HWTEST_F(MetaFileCloudDiskTest, FindDentryPage002, TestSize.Level1)
113 {
114     GTEST_LOG_(INFO) << "FindDentryPage002 Start";
115     try {
116         // init InitDcacheLookupCtx
117         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
118         MetaBase mBase("file1", "id1");
119         mBase.size = TEST_ISIZE;
120         DcacheLookupCtx ctx;
121         InitDcacheLookupCtx(&ctx, mBase, -1);
122 
123         // FindDentryPage -> nullptr || FilePosLock -> 0&& ReadFile ->0
124         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0));
125         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(0));
126         auto dentryBlk = FindDentryPage(0, &ctx);
127         EXPECT_EQ(dentryBlk, nullptr);
128     } catch (...) {
129         EXPECT_TRUE(false);
130         GTEST_LOG_(INFO) << "FindDentryPage002 ERROR";
131     }
132     GTEST_LOG_(INFO) << "FindDentryPage002 End";
133 }
134 
135 /**
136  * @tc.name: FindDentryPage003
137  * @tc.desc: Verify the FindDentryPage function
138  * @tc.type: FUNC
139  */
140 HWTEST_F(MetaFileCloudDiskTest, FindDentryPage003, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "FindDentryPage003 Start";
143     try {
144         // init InitDcacheLookupCtx
145         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
146         MetaBase mBase("file1", "id1");
147         mBase.size = TEST_ISIZE;
148         DcacheLookupCtx ctx;
149         InitDcacheLookupCtx(&ctx, mBase, -1);
150 
151         // FindDentryPage -> not nullptr || FilePosLock -> 0&& ReadFile -> DENTRYGROUP_SIZE
152         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0));
153         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE));
154         auto dentryBlk = FindDentryPage(0, &ctx);
155         EXPECT_NE(dentryBlk, nullptr);
156     } catch (...) {
157         EXPECT_TRUE(false);
158         GTEST_LOG_(INFO) << "FindDentryPage003 ERROR";
159     }
160     GTEST_LOG_(INFO) << "FindDentryPage003 End";
161 }
162 
163 /**
164  * @tc.name: InLevel001
165  * @tc.desc: Verify the InLeve function
166  * @tc.type: FUNC
167  */
168 HWTEST_F(MetaFileCloudDiskTest, InLevel001, TestSize.Level1)
169 {
170     GTEST_LOG_(INFO) << "InLevel001 Start";
171     try {
172         // init InitDcacheLookupCtx
173         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
174         MetaBase mBase("file1", "id1");
175         mBase.size = TEST_ISIZE;
176         DcacheLookupCtx ctx;
177         InitDcacheLookupCtx(&ctx, mBase, -1);
178 
179         // FindDentryPage -> not nullptr || FilePosLock -> 0&& ReadFile -> DENTRYGROUP_SIZE
180         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
181         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
182 
183         HmdfsDentry *de = InLevel(2, &ctx);
184         EXPECT_EQ(de, nullptr);
185     } catch (...) {
186         EXPECT_TRUE(false);
187         GTEST_LOG_(INFO) << "InLevel001 ERROR";
188     }
189     GTEST_LOG_(INFO) << "InLevel001 End";
190 }
191 
192 /**
193  * @tc.name: GetCreateInfo001
194  * @tc.desc: Verify the GetCreateInfo function
195  * @tc.type: FUNC
196  */
197 HWTEST_F(MetaFileCloudDiskTest, GetCreateInfo001, TestSize.Level1)
198 {
199     GTEST_LOG_(INFO) << "GetCreateInfo001 Start";
200     try {
201         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
202         MetaBase mBase("file1", "id1");
203         uint32_t bitPos = 0;
204         unsigned long bidx = 0;
205         HmdfsDentryGroup dentryBlk = {0};
206         uint32_t namehash = 0;
207 
208         // HAndleFileFd -> success
209         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(0));
210         EXPECT_CALL(*insMock, ftruncate(_, _)).WillRepeatedly(Return(0));
211 
212         // FilePosLock -> 2
213         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(2));
214 
215         int ret = mFile.GetCreateInfo(mBase, bitPos, namehash, bidx, dentryBlk);
216         EXPECT_EQ(ret, 2);
217     } catch (...) {
218         EXPECT_TRUE(false);
219         GTEST_LOG_(INFO) << "GetCreateInfo001 ERROR";
220     }
221     GTEST_LOG_(INFO) << "GetCreateInfo001 End";
222 }
223 
224 /**
225  * @tc.name: GetCreateInfo002
226  * @tc.desc: Verify the GetCreateInfo function
227  * @tc.type: FUNC
228  */
229 HWTEST_F(MetaFileCloudDiskTest, GetCreateInfo002, TestSize.Level1)
230 {
231     GTEST_LOG_(INFO) << "GetCreateInfo002 Start";
232     try {
233         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
234         MetaBase mBase("file1", "id1");
235         uint32_t bitPos = 0;
236         unsigned long bidx = 0;
237         HmdfsDentryGroup dentryBlk = {0};
238         uint32_t namehash = 0;
239 
240         // HAndleFileFd -> success
241         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(0));
242         EXPECT_CALL(*insMock, ftruncate(_, _)).WillRepeatedly(Return(0));
243 
244         // FilePosLock -> 0 && ReadFile -> 0 ENOENT
245         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
246         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0));
247 
248         int ret = mFile.GetCreateInfo(mBase, bitPos, namehash, bidx, dentryBlk);
249         EXPECT_EQ(ret, 2);
250     } catch (...) {
251         EXPECT_TRUE(false);
252         GTEST_LOG_(INFO) << "GetCreateInfo002 ERROR";
253     }
254     GTEST_LOG_(INFO) << "GetCreateInfo002 End";
255 }
256 
257 /**
258  * @tc.name: GetCreateInfo003
259  * @tc.desc: Verify the GetCreateInfo function
260  * @tc.type: FUNC
261  */
262 HWTEST_F(MetaFileCloudDiskTest, GetCreateInfo003, TestSize.Level1)
263 {
264     GTEST_LOG_(INFO) << "GetCreateInfo003 Start";
265     try {
266         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
267         MetaBase mBase("file1", "id1");
268         uint32_t bitPos = 0;
269         unsigned long bidx = 0;
270         HmdfsDentryGroup dentryBlk = {0};
271         uint32_t namehash = 0;
272 
273         // HAndleFileFd -> success
274         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(0));
275         EXPECT_CALL(*insMock, ftruncate(_, _)).WillRepeatedly(Return(0));
276 
277         // FilePosLock -> 0 && ReadFile -> DENTRYGROUP_SIZE
278         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
279         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
280 
281         int ret = mFile.GetCreateInfo(mBase, bitPos, namehash, bidx, dentryBlk);
282         EXPECT_EQ(ret, 0);
283     } catch (...) {
284         EXPECT_TRUE(false);
285         GTEST_LOG_(INFO) << "GetCreateInfo003 ERROR";
286     }
287     GTEST_LOG_(INFO) << "GetCreateInfo003 End";
288 }
289 
290 /**
291  * @tc.name: DoCreate001
292  * @tc.desc: Verify the DoCreate function
293  * @tc.type: FUNC
294  */
295 HWTEST_F(MetaFileCloudDiskTest, DoCreate001, TestSize.Level1)
296 {
297     GTEST_LOG_(INFO) << "DoCreate001 Start";
298     try {
299         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
300         mFile.fd_ = UniqueFd(1);
301         MetaBase mBase("file1", "id1");
302 
303         // InLevel -> nullPtr
304         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
305         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
306 
307         // GetCreateInfo -> 2
308         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(-1));
309 
310         int ret = mFile.DoCreate(mBase);
311         EXPECT_NE(ret, 0);
312     } catch (...) {
313         EXPECT_TRUE(false);
314         GTEST_LOG_(INFO) << "DoCreate001 ERROR";
315     }
316     GTEST_LOG_(INFO) << "DoCreate001 End";
317 }
318 
319 /**
320  * @tc.name: DoCreate002
321  * @tc.desc: Verify the DoCreate function
322  * @tc.type: FUNC
323  */
324 HWTEST_F(MetaFileCloudDiskTest, DoCreate002, TestSize.Level1)
325 {
326     GTEST_LOG_(INFO) << "DoCreate002 Start";
327     try {
328         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
329         mFile.fd_ = UniqueFd(1);
330         MetaBase mBase("file1", "id1");
331 
332         // GetCreateInfo -> success
333         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(0));
334         EXPECT_CALL(*insMock, ftruncate(_, _)).WillRepeatedly(Return(0));
335         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
336         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
337         // WriteFile->0
338         EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillRepeatedly(Return(0));
339 
340         int ret = mFile.DoCreate(mBase);
341         EXPECT_NE(ret, 0);
342     } catch (...) {
343         EXPECT_TRUE(false);
344         GTEST_LOG_(INFO) << "DoCreate002 ERROR";
345     }
346     GTEST_LOG_(INFO) << "DoCreate002 End";
347 }
348 
349 /**
350  * @tc.name: DoCreate003
351  * @tc.desc: Verify the DoCreate function
352  * @tc.type: FUNC
353  */
354 HWTEST_F(MetaFileCloudDiskTest, DoCreate003, TestSize.Level1)
355 {
356     GTEST_LOG_(INFO) << "DoCreate003 Start";
357     try {
358         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
359         mFile.fd_ = UniqueFd(1);
360         MetaBase mBase("file1", "id1");
361 
362         // GetCreateInfo -> success
363         EXPECT_CALL(*insMock, fstat(_, _)).WillRepeatedly(Return(0));
364         EXPECT_CALL(*insMock, ftruncate(_, _)).WillRepeatedly(Return(0));
365         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
366         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
367         // WriteFile->DENTRYGROUP_SIZE
368         EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
369 
370         int ret = mFile.DoCreate(mBase);
371         EXPECT_EQ(ret, 0);
372     } catch (...) {
373         EXPECT_TRUE(false);
374         GTEST_LOG_(INFO) << "DoCreate003 ERROR";
375     }
376     GTEST_LOG_(INFO) << "DoCreate003 End";
377 }
378 
379 /**
380  * @tc.name: LoadChildren001
381  * @tc.desc: Verify the LoadChildren function
382  * @tc.type: FUNC
383  */
384 HWTEST_F(MetaFileCloudDiskTest, LoadChildren001, TestSize.Level1)
385 {
386     GTEST_LOG_(INFO) << "LoadChildren001 Start";
387     try {
388         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
389         mFile.fd_ = UniqueFd(1);
390         std::vector<MetaBase> bases;
391         struct stat mockStat = {.st_size = 4 * 4096};
392 
393         EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0)));
394         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(2));
395 
396         int ret = mFile.LoadChildren(bases);
397         EXPECT_EQ(ret, 2);
398     } catch (...) {
399         EXPECT_TRUE(false);
400         GTEST_LOG_(INFO) << "LoadChildren001 ERROR";
401     }
402     GTEST_LOG_(INFO) << "LoadChildren001 End";
403 }
404 
405 /**
406  * @tc.name: LoadChildren002
407  * @tc.desc: Verify the LoadChildren function
408  * @tc.type: FUNC
409  */
410 HWTEST_F(MetaFileCloudDiskTest, LoadChildren002, TestSize.Level1)
411 {
412     GTEST_LOG_(INFO) << "LoadChildren002 Start";
413     try {
414         CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
415         mFile.fd_ = UniqueFd(1);
416         std::vector<MetaBase> bases;
417         struct stat mockStat = {.st_size = 4 * 4096};
418 
419         EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0)));
420         EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0));
421         EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE));
422 
423         int ret = mFile.LoadChildren(bases);
424         EXPECT_EQ(ret, 0);
425     } catch (...) {
426         EXPECT_TRUE(false);
427         GTEST_LOG_(INFO) << "LoadChildren002 ERROR";
428     }
429     GTEST_LOG_(INFO) << "LoadChildren002 End";
430 }
431 } // namespace OHOS::FileManagement