• 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 "directory_ex.h"
20 #include "meta_file.h"
21 #include "dfs_error.h"
22 
23 namespace OHOS::FileManagement::CloudSync::Test {
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace std;
27 constexpr uint32_t TEST_USER_ID = 201;
28 constexpr uint64_t TEST_ISIZE = 1024;
29 
30 class CloudDiskDentryMetaFileTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 };
SetUpTestCase(void)37 void CloudDiskDentryMetaFileTest::SetUpTestCase(void)
38 {
39     GTEST_LOG_(INFO) << "SetUpTestCase";
40 }
41 
TearDownTestCase(void)42 void CloudDiskDentryMetaFileTest::TearDownTestCase(void)
43 {
44     GTEST_LOG_(INFO) << "TearDownTestCase";
45 }
46 
SetUp(void)47 void CloudDiskDentryMetaFileTest::SetUp(void)
48 {
49     GTEST_LOG_(INFO) << "SetUp";
50 }
51 
TearDown(void)52 void CloudDiskDentryMetaFileTest::TearDown(void)
53 {
54     GTEST_LOG_(INFO) << "TearDown";
55 }
56 
57 /**
58  * @tc.name: CloudDiskMetaFileHandleFileByFd001
59  * @tc.desc: Verify the MetaFile::HandleFileByFd function
60  * @tc.type: FUNC
61  * @tc.require: SR000HRKKA
62  */
63 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileHandleFileByFd001, TestSize.Level1)
64 {
65     MetaFile mFile(TEST_USER_ID, "/");
66     unsigned long endBlock = 0;
67     uint32_t level = 0;
68     int ret = mFile.HandleFileByFd(endBlock, level);
69     EXPECT_EQ(ret, EINVAL);
70 }
71 
72 /**
73  * @tc.name: CloudDiskMetaFileHandleFileByFd002
74  * @tc.desc: Verify the MetaFile::HandleFileByFd function
75  * @tc.type: FUNC
76  * @tc.require: SR000HRKKA
77  */
78 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileHandleFileByFd002, TestSize.Level1)
79 {
80     uint32_t userId = 100;
81     CloudDiskMetaFile mFile(userId, "/", "id1");
82     unsigned long endBlock = 0;
83     uint32_t level = 64;
84     int ret = mFile.HandleFileByFd(endBlock, level);
85     EXPECT_EQ(ret, 0);
86 }
87 
88 /**
89  * @tc.name: CloudDiskMetaFileHandleFileByFd003
90  * @tc.desc: Verify the MetaFile::HandleFileByFd function
91  * @tc.type: FUNC
92  * @tc.require: SR000HRKKA
93  */
94 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileHandleFileByFd003, TestSize.Level1)
95 {
96     uint32_t userId = 100;
97     CloudDiskMetaFile mFile(userId, "/", "id1");
98     unsigned long endBlock = 0;
99     uint32_t level = 0;
100     int ret = mFile.HandleFileByFd(endBlock, level);
101     EXPECT_EQ(ret, 0);
102 }
103 
104 /**
105  * @tc.name: CloudDiskMetaFileDoChildUpdateTest
106  * @tc.desc: Verify the CloudDiskMetaFile::DoChildUpdate function
107  * @tc.type: FUNC
108  * @tc.require: SR000HRKKC
109  */
110 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileDoChildUpdateTest, TestSize.Level1)
111 {
112     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
113     UniqueFd fd(-1);
114     mFile.fd_ = std::move(fd);
115     string name = "";
116     CloudDiskMetaFile::CloudDiskMetaFileCallBack callback;
117     int ret = mFile.DoChildUpdate(name, callback);
118     EXPECT_EQ(ret, EINVAL);
119 }
120 
121 /**
122  * @tc.name: DoLookupAndRemoveTest
123  * @tc.desc: Verify the CloudDiskMetaFile::DoLookupAndRemove function
124  * @tc.type: FUNC
125  * @tc.require: SR000HRKKA
126  */
127 HWTEST_F(CloudDiskDentryMetaFileTest, DoLookupAndRemoveTest, TestSize.Level1)
128 {
129     CloudDiskMetaFile mFile(100, "com.ohos.photos", "rootId");
130     UniqueFd fd(-1);
131     mFile.fd_ = std::move(fd);
132     MetaBase mBase1(".trash", "rootId");
133     int ret = mFile.DoLookupAndRemove(mBase1);
134     EXPECT_EQ(ret, 0);
135 }
136 
137 /**
138  * @tc.name: CloudDiskDentryFileCreateTest
139  * @tc.desc: Verify the CloudDiskMetaFile::DoCreate function
140  * @tc.type: FUNC
141  * @tc.require: SR000HRKKA
142  */
143 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskDentryFileCreateTest, TestSize.Level1)
144 {
145     std::string cacheDir =
146         "/data/service/el2/" + std::to_string(TEST_USER_ID) + "/hmdfs/cache/cloud_cache/dentry_cache/cloud/";
147     ForceRemoveDirectory(cacheDir);
148 
149     string CloudId = "";
150     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
151     MetaBase mBase1("file1", "id1");
152     mBase1.size = TEST_ISIZE;
153     int ret = mFile.DoCreate(mBase1);
154     EXPECT_EQ(ret, 0);
155     MetaBase mBase2("file2", "id2");
156     mBase2.size = TEST_ISIZE;
157     ret = mFile.DoCreate(mBase2);
158     EXPECT_EQ(ret, 0);
159     CloudDiskMetaFile mFile2(TEST_USER_ID, "/a/b", "id3");
160     MetaBase mBase3("file3", "id3");
161     ret = mFile2.DoCreate(mBase3);
162 }
163 
164 /**
165  * @tc.name: CloudDiskMetaFileLookupTest
166  * @tc.desc: Verify the CloudDiskMetaFile::DoLookup function
167  * @tc.type: FUNC
168  * @tc.require: SR000HRKKA
169  */
170 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileLookupTest, TestSize.Level1)
171 {
172     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
173     MetaBase mBase1("file1");
174     int ret = mFile.DoCreate(mBase1);
175     ret = mFile.DoLookup(mBase1);
176     EXPECT_EQ(ret, 0);
177     EXPECT_EQ(mBase1.size, TEST_ISIZE);
178 }
179 
180 /**
181  * @tc.name: CloudDiskMetaFileUpdateTest
182  * @tc.desc: Verify the CloudDiskMetaFile::DoUpdate function
183  * @tc.type: FUNC
184  * @tc.require: SR000HRKKC
185  */
186 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileUpdateTest, TestSize.Level1)
187 {
188     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
189     MetaBase mBase1("file1", "id1");
190     mBase1.size = 0;
191     int ret = mFile.DoCreate(mBase1);
192     ret = mFile.DoUpdate(mBase1);
193     EXPECT_EQ(ret, 0);
194     MetaBase mBase2("file1", "id1");
195     ret = mFile.DoLookup(mBase2);
196     EXPECT_EQ(ret, 0);
197     EXPECT_EQ(mBase2.size, 0);
198 }
199 
200 /**
201  * @tc.name: CloudDiskMetaFileRenameTest
202  * @tc.desc: Verify the CloudDiskMetaFile::DoRename function
203  * @tc.type: FUNC
204  * @tc.require: SR000HRKKC
205  */
206 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileRenameTest, TestSize.Level1)
207 {
208     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
209     MetaBase mBase1("file1", "id1");
210     std::shared_ptr<CloudDiskMetaFile> newMetaFile =
211         make_shared<CloudDiskMetaFile>(TEST_USER_ID, "/", "id1");
212     int ret = mFile.DoCreate(mBase1);
213     ret = mFile.DoRename(mBase1, "id2", newMetaFile);
214     EXPECT_EQ(ret, 0);
215     MetaBase mBase2("id2");
216     ret = mFile.DoLookup(mBase2);
217     EXPECT_EQ(ret, 0);
218 }
219 
220 /**
221  * @tc.name: CloudDiskMetaFileRemoveTest
222  * @tc.desc: Verify the CloudDiskMetaFile::DoRemove function
223  * @tc.type: FUNC
224  * @tc.require: SR000HRKJB
225  */
226 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileRemoveTest, TestSize.Level1)
227 {
228     CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
229     MetaBase mBase1("file1", "id1");
230     int ret = mFile.DoCreate(mBase1);
231     ret = mFile.DoRemove(mBase1);
232     EXPECT_EQ(ret, 0);
233     MetaBase mBase2("file1", "id1");
234     ret = mFile.DoLookup(mBase2);
235     EXPECT_EQ(ret, 0);
236 }
237 
238 /**
239  * @tc.name:LoadChildrenTest
240  * @tc.desc: Verify the CloudDiskMetaFile::LoadChildren function
241  * @tc.type: FUNC
242  * @tc.require: SR000HRKJB
243  */
244 HWTEST_F(CloudDiskDentryMetaFileTest, LoadChildrenTest, TestSize.Level1)
245 {
246     CloudDiskMetaFile mFile(100, "/", "id1");
247     MetaBase mBase1("file1", "id1");
248     std::vector<MetaBase> bases;
249     bases.push_back(mBase1);
250     int32_t ret = mFile.LoadChildren(bases);
251     EXPECT_EQ(ret, 0);
252 }
253 
254 /**
255  * @tc.name: CloudDiskMetaFileMgrTest001
256  * @tc.desc: Verify the MetaFileMgr
257  * @tc.type: FUNC
258  * @tc.require: SR000HRKJB
259  */
260 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileMgrTest001, TestSize.Level1)
261 {
262     GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 Start";
263     try {
264         auto m = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
265         MetaBase mBase1("file1", "file1");
266         EXPECT_EQ(m->DoCreate(mBase1), 0);
267         m = nullptr;
268         MetaFileMgr::GetInstance().CloudDiskClearAll();
269     } catch (...) {
270         EXPECT_FALSE(false);
271         GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 ERROR";
272     }
273     GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 End";
274 }
275 
276 /**
277  * @tc.name: CloudDiskMetaFileMgrTest002
278  * @tc.desc: Verify the CloudDiskMetaFileMgr
279  * @tc.type: FUNC
280  * @tc.require: issueI7SP3A
281  */
282 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileMgrTest002, TestSize.Level1)
283 {
284     GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 Start";
285     try {
286         auto m = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
287         MetaBase mBase1("testLongLongfileName", "testLongLongfileName");
288         EXPECT_EQ(m->DoCreate(mBase1), 0);
289         m = nullptr;
290         MetaFileMgr::GetInstance().CloudDiskClearAll();
291     } catch (...) {
292         EXPECT_FALSE(false);
293         GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 ERROR";
294     }
295     GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 End";
296 }
297 
298 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_GetDentryFilePath_001, TestSize.Level1)
299 {
300     GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 Start";
301     try {
302         CloudDiskMetaFile mFile(100, "/", "id1");
303         string cacheFilePath = mFile.GetDentryFilePath();
304         EXPECT_EQ((cacheFilePath != ""), true);
305     } catch (...) {
306         EXPECT_FALSE(false);
307         GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 ERROR";
308     }
309     GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 End";
310 }
311 
312 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoLookupAndUpdate_001, TestSize.Level1)
313 {
314     GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 Start";
315     try {
316         string fileName = "testLongLongfileName";
317         string cloudId = "testLongLongfileName";
318         unsigned long long size = 0x6b6b6b6b00000000;
319         MetaBase metaBase(fileName, cloudId);
320         metaBase.size = size;
__anon5582c3710102(MetaBase &m) 321         auto callback = [&metaBase] (MetaBase &m) {
322             m.size = metaBase.size;
323         };
324 
325         auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
326         int32_t ret = metaFile->DoLookupAndUpdate(fileName, callback);
327         EXPECT_EQ(ret, E_OK);
328     } catch (...) {
329         EXPECT_FALSE(false);
330         GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 ERROR";
331     }
332     GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 End";
333 }
334 
335 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoChildUpdate_001, TestSize.Level1)
336 {
337     GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 Start";
338     try {
339         string name = "testName";
340         CloudDiskMetaFile mFile(100, "/", "id1");
341         CloudDiskMetaFile::CloudDiskMetaFileCallBack updateFunc;
342         UniqueFd fd(-1);
343         mFile.fd_ = std::move(fd);
344 
345         int32_t ret = mFile.DoChildUpdate(name, updateFunc);
346         EXPECT_EQ(ret, EINVAL);
347     } catch (...) {
348         EXPECT_FALSE(false);
349         GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 ERROR";
350     }
351     GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 End";
352 }
353 
354 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoLookupAndRemove_001, TestSize.Level1)
355 {
356     GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 Start";
357     try {
358         CloudDiskMetaFile mFile(100, "com.ohos.photos", "rootId");
359         MetaBase mBase1(".trash", "rootId");
360         UniqueFd fd(-1);
361         mFile.fd_ = std::move(fd);
362 
363         int32_t ret = mFile.DoLookupAndRemove(mBase1);
364         EXPECT_EQ(ret, E_OK);
365     } catch (...) {
366         EXPECT_FALSE(false);
367         GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 ERROR";
368     }
369     GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 End";
370 }
371 } // namespace OHOS::FileManagement::CloudSync::Test