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 string name = "";
114 CloudDiskMetaFile::CloudDiskMetaFileCallBack callback;
115 int ret = mFile.DoChildUpdate(name, callback);
116 EXPECT_EQ(ret, 2);
117 }
118
119 /**
120 * @tc.name: DoLookupAndRemoveTest
121 * @tc.desc: Verify the CloudDiskMetaFile::DoLookupAndRemove function
122 * @tc.type: FUNC
123 * @tc.require: SR000HRKKA
124 */
125 HWTEST_F(CloudDiskDentryMetaFileTest, DoLookupAndRemoveTest, TestSize.Level1)
126 {
127 CloudDiskMetaFile mFile(100, "com.ohos.photos", "rootId");
128 MetaBase mBase1(".trash", "rootId");
129 int ret = mFile.DoLookupAndRemove(mBase1);
130 EXPECT_EQ(ret, 0);
131 }
132
133 /**
134 * @tc.name: CloudDiskDentryFileCreateTest
135 * @tc.desc: Verify the CloudDiskMetaFile::DoCreate function
136 * @tc.type: FUNC
137 * @tc.require: SR000HRKKA
138 */
139 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskDentryFileCreateTest, TestSize.Level1)
140 {
141 std::string cacheDir =
142 "/data/service/el2/" + std::to_string(TEST_USER_ID) + "/hmdfs/cache/cloud_cache/dentry_cache/cloud/";
143 ForceRemoveDirectory(cacheDir);
144
145 string CloudId = "";
146 CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
147 MetaBase mBase1("file1", "id1");
148 mBase1.size = TEST_ISIZE;
149 int ret = mFile.DoCreate(mBase1);
150 EXPECT_EQ(ret, 0);
151 MetaBase mBase2("file2", "id2");
152 mBase2.size = TEST_ISIZE;
153 ret = mFile.DoCreate(mBase2);
154 EXPECT_EQ(ret, 0);
155 CloudDiskMetaFile mFile2(TEST_USER_ID, "/a/b", "id3");
156 MetaBase mBase3("file3", "id3");
157 ret = mFile2.DoCreate(mBase3);
158 }
159
160 /**
161 * @tc.name: CloudDiskMetaFileLookupTest
162 * @tc.desc: Verify the CloudDiskMetaFile::DoLookup function
163 * @tc.type: FUNC
164 * @tc.require: SR000HRKKA
165 */
166 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileLookupTest, TestSize.Level1)
167 {
168 CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
169 MetaBase mBase1("file1");
170 int ret = mFile.DoCreate(mBase1);
171 ret = mFile.DoLookup(mBase1);
172 EXPECT_EQ(ret, 0);
173 EXPECT_EQ(mBase1.size, TEST_ISIZE);
174 }
175
176 /**
177 * @tc.name: CloudDiskMetaFileUpdateTest
178 * @tc.desc: Verify the CloudDiskMetaFile::DoUpdate function
179 * @tc.type: FUNC
180 * @tc.require: SR000HRKKC
181 */
182 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileUpdateTest, TestSize.Level1)
183 {
184 CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
185 MetaBase mBase1("file1", "id1");
186 mBase1.size = 0;
187 int ret = mFile.DoCreate(mBase1);
188 ret = mFile.DoUpdate(mBase1);
189 EXPECT_EQ(ret, 0);
190 MetaBase mBase2("file1", "id1");
191 ret = mFile.DoLookup(mBase2);
192 EXPECT_EQ(ret, 0);
193 EXPECT_EQ(mBase2.size, 0);
194 }
195
196 /**
197 * @tc.name: CloudDiskMetaFileRenameTest
198 * @tc.desc: Verify the CloudDiskMetaFile::DoRename function
199 * @tc.type: FUNC
200 * @tc.require: SR000HRKKC
201 */
202 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileRenameTest, TestSize.Level1)
203 {
204 CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
205 MetaBase mBase1("file1", "id1");
206 std::shared_ptr<CloudDiskMetaFile> newMetaFile =
207 make_shared<CloudDiskMetaFile>(TEST_USER_ID, "/", "id1");
208 int ret = mFile.DoCreate(mBase1);
209 ret = mFile.DoRename(mBase1, "id2", newMetaFile);
210 EXPECT_EQ(ret, 0);
211 MetaBase mBase2("id2");
212 ret = mFile.DoLookup(mBase2);
213 EXPECT_EQ(ret, 0);
214 }
215
216 /**
217 * @tc.name: CloudDiskMetaFileRemoveTest
218 * @tc.desc: Verify the CloudDiskMetaFile::DoRemove function
219 * @tc.type: FUNC
220 * @tc.require: SR000HRKJB
221 */
222 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileRemoveTest, TestSize.Level1)
223 {
224 CloudDiskMetaFile mFile(TEST_USER_ID, "/", "id1");
225 MetaBase mBase1("file1", "id1");
226 int ret = mFile.DoCreate(mBase1);
227 ret = mFile.DoRemove(mBase1);
228 EXPECT_EQ(ret, 0);
229 MetaBase mBase2("file1", "id1");
230 ret = mFile.DoLookup(mBase2);
231 EXPECT_EQ(ret, 0);
232 }
233
234 /**
235 * @tc.name:LoadChildrenTest
236 * @tc.desc: Verify the CloudDiskMetaFile::LoadChildren function
237 * @tc.type: FUNC
238 * @tc.require: SR000HRKJB
239 */
240 HWTEST_F(CloudDiskDentryMetaFileTest, LoadChildrenTest, TestSize.Level1)
241 {
242 CloudDiskMetaFile mFile(100, "/", "id1");
243 MetaBase mBase1("file1", "id1");
244 std::vector<MetaBase> bases;
245 bases.push_back(mBase1);
246 int32_t ret = mFile.LoadChildren(bases);
247 EXPECT_EQ(ret, 0);
248 }
249
250 /**
251 * @tc.name: CloudDiskMetaFileMgrTest001
252 * @tc.desc: Verify the MetaFileMgr
253 * @tc.type: FUNC
254 * @tc.require: SR000HRKJB
255 */
256 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileMgrTest001, TestSize.Level1)
257 {
258 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 Start";
259 try {
260 auto m = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
261 MetaBase mBase1("file1", "file1");
262 EXPECT_EQ(m->DoCreate(mBase1), 0);
263 m = nullptr;
264 MetaFileMgr::GetInstance().CloudDiskClearAll();
265 } catch (...) {
266 EXPECT_FALSE(false);
267 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 ERROR";
268 }
269 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest001 End";
270 }
271
272 /**
273 * @tc.name: CloudDiskMetaFileMgrTest002
274 * @tc.desc: Verify the CloudDiskMetaFileMgr
275 * @tc.type: FUNC
276 * @tc.require: issueI7SP3A
277 */
278 HWTEST_F(CloudDiskDentryMetaFileTest, CloudDiskMetaFileMgrTest002, TestSize.Level1)
279 {
280 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 Start";
281 try {
282 auto m = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
283 MetaBase mBase1("testLongLongfileName", "testLongLongfileName");
284 EXPECT_EQ(m->DoCreate(mBase1), 0);
285 m = nullptr;
286 MetaFileMgr::GetInstance().CloudDiskClearAll();
287 } catch (...) {
288 EXPECT_FALSE(false);
289 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 ERROR";
290 }
291 GTEST_LOG_(INFO) << "CloudDiskMetaFileMgrTest002 End";
292 }
293
294 /**
295 * @tc.name: CreateRecycleDentryTest001
296 * @tc.desc: Verify the CreateRecycleDentry
297 * @tc.type: FUNC
298 * @tc.require: SR000HRKJB
299 */
300 HWTEST_F(CloudDiskDentryMetaFileTest, CreateRecycleDentryTest001, TestSize.Level1)
301 {
302 GTEST_LOG_(INFO) << "CreateRecycleDentryTest001 Start";
303 try {
304 uint32_t userId = 100;
305 string bundleName = "com.ohos.photos";
306 int32_t ret = MetaFileMgr::GetInstance().CreateRecycleDentry(userId, bundleName);
307 EXPECT_EQ(ret, 0);
308 MetaFileMgr::GetInstance().CloudDiskClearAll();
309 } catch (...) {
310 EXPECT_FALSE(false);
311 GTEST_LOG_(INFO) << "CreateRecycleDentryTest001 ERROR";
312 }
313 GTEST_LOG_(INFO) << "CreateRecycleDentryTest001 End";
314 }
315
316 /**
317 * @tc.name: MoveIntoRecycleDentryfileTest001
318 * @tc.desc: Verify the MoveIntoRecycleDentryfile
319 * @tc.type: FUNC
320 * @tc.require: SR000HRKJB
321 */
322 HWTEST_F(CloudDiskDentryMetaFileTest, MoveIntoRecycleDentryfileTest001, TestSize.Level1)
323 {
324 GTEST_LOG_(INFO) << "MoveIntoRecycleDentryfileTest001 Start";
325 try {
326 uint32_t userId = 100;
327 string bundleName = "com.ohos.photos";
328 string name = ".trash";
329 string parentCloudId = "rootId";
330 int64_t rowId = 0;
331 int32_t ret = MetaFileMgr::GetInstance().MoveIntoRecycleDentryfile(userId, bundleName,
332 name, parentCloudId, rowId);
333 EXPECT_EQ(ret, 0);
334 MetaFileMgr::GetInstance().CloudDiskClearAll();
335 } catch (...) {
336 EXPECT_FALSE(false);
337 GTEST_LOG_(INFO) << "MoveIntoRecycleDentryfileTest001 ERROR";
338 }
339 GTEST_LOG_(INFO) << "MoveIntoRecycleDentryfileTest001 End";
340 }
341
342 /**
343 * @tc.name: RemoveFromRecycleDentryfileTest001
344 * @tc.desc: Verify the RemoveFromRecycleDentryfile
345 * @tc.type: FUNC
346 * @tc.require: SR000HRKJB
347 */
348 HWTEST_F(CloudDiskDentryMetaFileTest, RemoveFromRecycleDentryfileTest001, TestSize.Level1)
349 {
350 GTEST_LOG_(INFO) << "RemoveFromRecycleDentryfileTest001 Start";
351 try {
352 uint32_t userId = 100;
353 string bundleName = "com.ohos.photos";
354 string name = ".trash";
355 string parentCloudId = "rootId";
356 int64_t rowId = 0;
357 int32_t ret = MetaFileMgr::GetInstance().RemoveFromRecycleDentryfile(userId, bundleName,
358 name, parentCloudId, rowId);
359 EXPECT_EQ(ret, 0);
360 MetaFileMgr::GetInstance().CloudDiskClearAll();
361 } catch (...) {
362 EXPECT_FALSE(false);
363 GTEST_LOG_(INFO) << "RemoveFromRecycleDentryfileTest001 ERROR";
364 }
365 GTEST_LOG_(INFO) << "RemoveFromRecycleDentryfileTest001 End";
366 }
367
368
369 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_GetDentryFilePath_001, TestSize.Level1)
370 {
371 GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 Start";
372 try {
373 CloudDiskMetaFile mFile(100, "/", "id1");
374 string cacheFilePath = mFile.GetDentryFilePath();
375 EXPECT_EQ((cacheFilePath != ""), true);
376 } catch (...) {
377 EXPECT_FALSE(false);
378 GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 ERROR";
379 }
380 GTEST_LOG_(INFO) << "DfsService_GetDentryFilePath_001 End";
381 }
382
383 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoLookupAndUpdate_001, TestSize.Level1)
384 {
385 GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 Start";
386 try {
387 string fileName = "testLongLongfileName";
388 string cloudId = "testLongLongfileName";
389 unsigned long long size = 0x6b6b6b6b00000000;
390 MetaBase metaBase(fileName, cloudId);
391 metaBase.size = size;
__anoncf6c962f0102(MetaBase &m) 392 auto callback = [&metaBase] (MetaBase &m) {
393 m.size = metaBase.size;
394 };
395
396 auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskMetaFile(TEST_USER_ID, "/o/p/q/r/s/t", "id1");
397 int32_t ret = metaFile->DoLookupAndUpdate(fileName, callback);
398 EXPECT_EQ(ret, E_OK);
399 } catch (...) {
400 EXPECT_FALSE(false);
401 GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 ERROR";
402 }
403 GTEST_LOG_(INFO) << "DfsService_DoLookupAndUpdate_001 End";
404 }
405
406 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoChildUpdate_001, TestSize.Level1)
407 {
408 GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 Start";
409 try {
410 string name = "testName";
411 CloudDiskMetaFile mFile(100, "/", "id1");
412 CloudDiskMetaFile::CloudDiskMetaFileCallBack updateFunc;
413
414 int32_t ret = mFile.DoChildUpdate(name, updateFunc);
415 EXPECT_EQ(ret, E_INVAL_ARG);
416 } catch (...) {
417 EXPECT_FALSE(false);
418 GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 ERROR";
419 }
420 GTEST_LOG_(INFO) << "DfsService_DoChildUpdate_001 End";
421 }
422
423 HWTEST_F(CloudDiskDentryMetaFileTest, DfsService_DoLookupAndRemove_001, TestSize.Level1)
424 {
425 GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 Start";
426 try {
427 CloudDiskMetaFile mFile(100, "com.ohos.photos", "rootId");
428 MetaBase mBase1(".trash", "rootId");
429
430 int32_t ret = mFile.DoLookupAndRemove(mBase1);
431 EXPECT_EQ(ret, E_OK);
432 } catch (...) {
433 EXPECT_FALSE(false);
434 GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 ERROR";
435 }
436 GTEST_LOG_(INFO) << "DfsService_DoLookupAndRemove_001 End";
437 }
438 } // namespace OHOS::FileManagement::CloudSync::Test