• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024-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 "copy/file_copy_manager.h"
17 
18 #include <fstream>
19 #include <gtest/gtest.h>
20 
21 #include "accesstoken_kit.h"
22 #include "datashare_helper.h"
23 #include "dfs_error.h"
24 #include "directory_ex.h"
25 #include "ipc_skeleton.h"
26 
27 namespace OHOS::Storage::DistributedFile::Test {
28 using namespace OHOS::FileManagement;
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace std;
32 constexpr int32_t FILE_NOT_FOUND = 2;
33 class FileCopyManagerTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 
40     uint64_t process_ = 0;
41     uint64_t fileSize_ = 0;
42     using callBack = std::function<void(uint64_t processSize, uint64_t totalFileSize)>;
__anona324e1440102(uint64_t processSize, uint64_t totalFileSize) 43     callBack listener_ = [&](uint64_t processSize, uint64_t totalFileSize) {
44         process_ = processSize;
45         fileSize_ = totalFileSize;
46     };
47     // 定义一个空的 ProcessCallback 用于测试
48     ProcessCallback emptyCallback_;
49 };
50 
SetUpTestCase(void)51 void FileCopyManagerTest::SetUpTestCase(void)
52 {
53     GTEST_LOG_(INFO) << "SetUpTestCase";
54 }
55 
TearDownTestCase(void)56 void FileCopyManagerTest::TearDownTestCase(void)
57 {
58     GTEST_LOG_(INFO) << "TearDownTestCase";
59 }
60 
SetUp(void)61 void FileCopyManagerTest::SetUp(void)
62 {
63     GTEST_LOG_(INFO) << "SetUp";
64 }
65 
TearDown(void)66 void FileCopyManagerTest::TearDown(void)
67 {
68     GTEST_LOG_(INFO) << "TearDown";
69 }
70 
71 /**
72 * @tc.name: FileCopyManager_Copy_0001
73 * @tc.desc: The execution of the Copy failed.
74 * @tc.type: FUNC
75 * @tc.require: I7TDJK
76  */
77 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0001, TestSize.Level0)
78 {
79     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 Start";
80     string localUri = "/data/test/test.txt";
81     string dstUri = "/data/test/test.txt";
82 
83     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", localUri, listener_);
84     EXPECT_EQ(ret, EINVAL);
85 
86     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, "", listener_);
87     EXPECT_EQ(ret, EINVAL);
88 
89     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", "", listener_);
90     EXPECT_EQ(ret, EINVAL);
91 
92     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, listener_);
93     EXPECT_EQ(ret, EINVAL);
94 
95     string remoteUri = "/data/test/Copy/?networkid=/";
96     if (!ForceCreateDirectory(remoteUri)) {
97         GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 create dir err";
98     }
99     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(remoteUri, "", listener_);
100     EXPECT_EQ(ret, EINVAL);
101     if (!ForceRemoveDirectory(remoteUri)) {
102         GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 remove dir err";
103     }
104     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 End";
105 }
106 
107 /**
108 * @tc.name: FileCopyManager_Copy_0002
109 * @tc.desc: The execution of the Copy succeed.
110 * @tc.type: FUNC
111 * @tc.require: I7TDJK
112  */
113 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0002, TestSize.Level0)
114 {
115     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0002 Start";
116     string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
117     string destUri = "file://docs/storage/media/100/local/files/Docs/dest1.txt";
118     string srcPath = "/storage/media/100/local/files/Docs/1.txt";
119     string dstPath = "/storage/media/100/local/files/Docs/dest1.txt";
120     int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
121     ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0002!" << errno;
122     close(fd);
123 
124     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_);
125     EXPECT_EQ(ret, E_OK);
126     ASSERT_EQ(remove(srcPath.c_str()), 0);
127     ASSERT_EQ(remove(dstPath.c_str()), 0);
128     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0002 End";
129 }
130 
131 /**
132 * @tc.name: FileCopyManager_Copy_0003
133 * @tc.desc: The execution of the Copy succeed.
134 * @tc.type: FUNC
135 * @tc.require: I7TDJK
136  */
137 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0003, TestSize.Level0)
138 {
139     //same uri
140     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0003 Start";
141     string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
142     string destUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
143     string srcPath = "/storage/media/100/local/files/Docs/1.txt";
144     int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
145     ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0003!" << errno;
146     close(fd);
147 
148     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_);
149     EXPECT_EQ(ret, EINVAL);
150     ASSERT_EQ(remove(srcPath.c_str()), 0);
151     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0003 End";
152 }
153 
154 /**
155 * @tc.name: FileCopyManager_Copy_0004
156 * @tc.desc: The execution of the Copy succeed.
157 * @tc.type: FUNC
158 * @tc.require: I7TDJK
159  */
160 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0004, TestSize.Level0)
161 {
162     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0004 Start";
163     string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
164     string destUri = "file://docs/storage/media/100/local/files/Docs/a1.txt";
165     string srcPath = "/storage/media/100/local/files/Docs/1.txt";
166     string destPath = "/storage/media/100/local/files/Docs/a1.txt";
167     int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
168     ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0004!" << errno;
169     close(fd);
170 
171     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_);
172     EXPECT_EQ(ret, E_OK);
173     ASSERT_EQ(remove(srcPath.c_str()), 0);
174     ASSERT_EQ(remove(destPath.c_str()), 0);
175     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0004 End";
176 }
177 
178 /**
179 * @tc.name: FileCopyManager_Copy_0005
180 * @tc.desc: The execution of the Copy failed.
181 * @tc.type: FUNC
182 * @tc.require: I7TDJK
183  */
184 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0005, TestSize.Level0)
185 {
186     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0005 Start";
187     string localUri = "/data/test/test.txt";
188     string dstUri = "/data/test/test.txt";
189 
190     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy("", localUri, emptyCallback_);
191     EXPECT_EQ(ret, EINVAL);
192 
193     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, "", listener_);
194     EXPECT_EQ(ret, EINVAL);
195 
196     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, listener_);
197     EXPECT_EQ(ret, EINVAL);
198 
199     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(localUri, dstUri, emptyCallback_);
200     EXPECT_EQ(ret, EINVAL);
201 
202     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0005 End";
203 }
204 
205 /**
206  * @tc.name: FileCopyManager_Copy_0006
207  * @tc.desc: The execution of the Copy failed.
208  * @tc.type: FUNC
209  * @tc.require: I7TDJK
210  */
211 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0006, TestSize.Level0)
212 {
213     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 Start";
214     string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
215     string destUri = "file://docs/storage/media/100/local/files/Docs/a1.txt";
216     string srcPath = "/storage/media/100/local/files/Docs/1.txt";
217 
218     EXPECT_TRUE(OHOS::RemoveFile(srcPath));
219     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, emptyCallback_);
220     EXPECT_EQ(ret, EINVAL);
221 
222     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener_);
223     EXPECT_EQ(ret, ENOENT);
224 
225     GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 End";
226 }
227 
228 /**
229 * @tc.name: FileCopyManager_ExecLocal_0001
230 * @tc.desc: The execution of the execlocal failed.
231 * @tc.type: FUNC
232 * @tc.require: I7TDJK
233  */
234 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0001, TestSize.Level0)
235 {
236     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0001 Start";
237     string srcuri = "file://docs/storage/media/100/local/files/Docs/11.txt";
238     string desturi = "file://docs/storage/media/100/local/files/Docs/dest11.txt";
239     string srcpath = "/storage/media/100/local/files/Docs/11.txt";
240     string destpath = "/storage/media/100/local/files/Docs/dest11.txt";
241     // infos is nullptr
242     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(nullptr);
243     EXPECT_EQ(ret, EINVAL);
244 
245     auto infos = std::make_shared<FileInfos>();
246     infos->srcUri = srcuri;
247     infos->destUri = desturi;
248     // infos localListener is nullptr
249     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
250     EXPECT_EQ(ret, EINVAL);
251 
252     infos->srcUriIsFile = true;
253     infos->srcPath = srcpath;
254     infos->destPath = srcpath;
255     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
256         infos->srcUriIsFile, listener_);
257     // srcPath and destPath is same
258     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
259     EXPECT_EQ(ret, EINVAL);
260 
261     infos->destPath = destpath;
262     // src file not exist
263     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
264     EXPECT_EQ(ret, ENOENT);
265 
266     int fd = open(srcpath.c_str(), O_RDWR | O_CREAT);
267     ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_ExecLocal_0001" << errno;
268     close(fd);
269     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
270     EXPECT_EQ(ret, E_OK);
271     EXPECT_TRUE(std::filesystem::exists("/storage/media/100/local/files/Docs/dest11.txt"));
272     ASSERT_EQ(remove(srcpath.c_str()), 0);
273     ASSERT_EQ(remove(destpath.c_str()), 0);
274 
275     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
276         infos->srcUriIsFile, emptyCallback_);
277     // src file not exist, callback is nullptr
278     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
279     EXPECT_EQ(ret, ENOENT);
280     ASSERT_EQ(remove(destpath.c_str()), 0);
281     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0001 End";
282 }
283 
284 /**
285 * @tc.name: FileCopyManager_ExecLocal_0002
286 * @tc.desc: The execution of the execlocal failed.
287 * @tc.type: FUNC
288 * @tc.require: I7TDJK
289  */
290 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0002, TestSize.Level0)
291 {
292     //无对应子文件夹,errno返回2
293     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0002 Start";
294     string srcuri = "file://docs/storage/media/100/local/files/Docs/srcaa11/";
295     string desturi = "file://docs/storage/media/100/local/files/Docs/dstaa11/";
296     string srcpath = "/storage/media/100/local/files/Docs/srcaa11/";
297     string destpath = "/storage/media/100/local/files/Docs/dstaa11/";
298     std::error_code errCode;
299     if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) {
300         int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath);
301         if (res != E_OK) {
302             GTEST_LOG_(INFO) <<"Failed to mkdir";
303         }
304     } else if (errCode.value() != E_OK) {
305         GTEST_LOG_(INFO) <<"fs exists failed";
306     }
307 
308     auto infos = std::make_shared<FileInfos>();
309     infos->srcUri = srcuri;
310     infos->destUri = desturi;
311     infos->srcPath = srcpath;
312     infos->destPath = destpath;
__anona324e1440202(uint64_t processSize, uint64_t totalSize) 313     ProcessCallback processCallback = [](uint64_t processSize, uint64_t totalSize) -> void {};
314     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
315         infos->srcUriIsFile, processCallback);
316     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
317     // destpath not exist
318     EXPECT_EQ(ret, ENOENT);
319 
320     if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) {
321         int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath);
322         EXPECT_EQ(res, E_OK);
323     }
324     // dir copy to dir
325     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
326     EXPECT_EQ(ret, 0);
327 
328     ASSERT_EQ(remove(srcpath.c_str()), 0);
329     ASSERT_EQ(ForceRemoveDirectory(destpath.c_str()), true);
330     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0002 End";
331 }
332 
333 /**
334 * @tc.name: FileCopyManager_ExecLocal_0003
335 * @tc.desc: The execution of the execlocal failed.
336 * @tc.type: FUNC
337 * @tc.require: I7TDJK
338  */
339 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0003, TestSize.Level0)
340 {
341     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0003 Start";
342     string srcuri = "file://docs/storage/media/100/local/files/Docs/srcaa12/";
343     string desturi = "file://docs/storage/media/100/local/files/Docs/dstaa12/";
344     string srcpath = "/storage/media/100/local/files/Docs/srcaa12/";
345     string destpath = "/storage/media/100/local/files/Docs/dstaa12/";
346     std::error_code errCode;
347     if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) {
348         int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath);
349         if (res != E_OK) {
350             GTEST_LOG_(INFO) <<"Failed to mkdir";
351         }
352     } else if (errCode.value() != E_OK) {
353         GTEST_LOG_(INFO) <<"fs exists failed";
354     }
355 
356     auto infos = std::make_shared<FileInfos>();
357     infos->srcUri = srcuri;
358     infos->destUri = desturi;
359     infos->srcPath = srcpath;
360     infos->destPath = destpath;
361     infos->srcUriIsFile = true;
__anona324e1440302(uint64_t processSize, uint64_t totalSize) 362     ProcessCallback processCallback = [](uint64_t processSize, uint64_t totalSize) -> void {};
363     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
364         infos->srcUriIsFile, processCallback);
365     // srcUriIsFile is true, destpath not exist
366     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
367     EXPECT_NE(ret, E_OK);
368     ASSERT_EQ(ForceRemoveDirectory(srcpath), true);
369     if (!ForceRemoveDirectory(destpath)) {
370         GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0003 remove dir err";
371     }
372     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0003 End";
373 }
374 
375 /**
376 * @tc.name: FileCopyManager_ExecLocal_0004
377 * @tc.desc: The execution of the execlocal failed.
378 * @tc.type: FUNC
379 * @tc.require: I7TDJK
380  */
381 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0004, TestSize.Level0)
382 {
383     //无对应子文件夹,errno返回2
384     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0004 Start";
385     string srcuri = "file://docs/storage/media/100/local/files/Docs/srcaa13/";
386     string desturi = "file://docs/storage/media/100/local/files/Docs/dstaa13/";
387     string srcpath = "/storage/media/100/local/files/Docs/srcaa13/";
388     string destpath = "/storage/media/100/local/files/Docs/dstaa13/";
389     std::error_code errCode;
390     auto infos = std::make_shared<FileInfos>();
391     infos->srcUri = srcuri;
392     infos->destUri = desturi;
393     infos->srcPath = srcpath;
394     infos->destPath = destpath;
__anona324e1440402(uint64_t processSize, uint64_t totalSize) 395     ProcessCallback processCallback = [](uint64_t processSize, uint64_t totalSize) -> void {};
396     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
397         infos->srcUriIsFile, processCallback);
398 
399     if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) {
400         int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath);
401         EXPECT_EQ(res, E_OK);
402     }
403     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
404     // src path not exist
405     EXPECT_EQ(ret, E_OK);
406     ASSERT_EQ(ForceRemoveDirectory(destpath.c_str()), true);
407     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0004 End";
408 }
409 
410 /**
411 * @tc.name: FileCopyManager_ExecLocal_0005
412 * @tc.desc: ExecLocal
413 * @tc.type: FUNC
414 * @tc.require: I7TDJK
415  */
416 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0005, TestSize.Level0)
417 {
418     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0005 Start";
419     string srcuri = "file://docs/storage/media/100/local/files/Docs/11.txt";
420     string desturi = "file://docs/storage/media/100/local/files/Docs/aa/";
421     string srcpath = "/storage/media/100/local/files/Docs/11.txt";
422     string destpath = "/storage/media/100/local/files/Docs/aa/";
423     int fd = open(srcpath.c_str(), O_RDWR | O_CREAT);
424     ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_ExecLocal_0005!" << errno;
425     close(fd);
426 
427     std::error_code errCode;
428     if (!std::filesystem::exists(destpath, errCode) && errCode.value() == E_OK) {
429         int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(destpath);
430         ASSERT_EQ(res, E_OK);
431     }
432 
433     auto infos = std::make_shared<FileInfos>();
434     infos->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440502(uint64_t processSize, uint64_t totalSize) 435         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
436     infos->srcUri = srcuri;
437     infos->destUri = desturi;
438     infos->srcPath = srcpath;
439     infos->destPath = destpath;
440     infos->srcUriIsFile = true;
441     // file to dir
442     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
443     EXPECT_EQ(ret, EINVAL);
444     ASSERT_EQ(remove(srcpath.c_str()), 0);
445     EXPECT_TRUE(ForceRemoveDirectory(destpath));
446     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0005 End";
447 }
448 
449 /**
450 * @tc.name: FileCopyManager_ExecLocal_0006
451 * @tc.desc: ExecLocal
452 * @tc.type: FUNC
453 * @tc.require: I7TDJK
454  */
455 HWTEST_F(FileCopyManagerTest, FileCopyManager_ExecLocal_0006, TestSize.Level0)
456 {
457     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0006 Start";
458     string srcuri = "file://docs/storage/media/100/local/files/Docs/11.txt";
459     string desturi = "file://docs/storage/media/100/local/files/Docs/dest11.txt";
460     string srcpath = "/storage/media/100/local/files/Docs/11.txt";
461     string destpath = "/aa/dest11.txt";
462 
463     auto infos = std::make_shared<FileInfos>();
464     infos->srcUriIsFile = true;
465     infos->srcPath = srcpath;
466     infos->destPath = destpath;
467     infos->localListener = FileCopyLocalListener::GetLocalListener(infos->srcPath,
468         infos->srcUriIsFile, listener_);
469 
470     // destpath is invalid
471     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
472     EXPECT_EQ(ret, ENOENT);
473     GTEST_LOG_(INFO) << "FileCopyManager_ExecLocal_0006 End";
474 }
475 
476 /**
477 * @tc.name: FileCopyManager_DeleteResFile_0001
478 * @tc.desc: Verify the DeleteResFile function when transListener is not null and destPath exists.
479 * @tc.type: FUNC
480 * @tc.require: I7TDJK
481  */
482 HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0001, TestSize.Level0)
483 {
484     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0001 Start";
485 
486     auto infos = std::make_shared<FileInfos>();
487     infos->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_));
488     infos->destPath = "/data/test/test.txt";
489 
490     // 创建测试文件
491     std::ofstream(infos->destPath).close();
492     EXPECT_TRUE(std::filesystem::exists(infos->destPath));
493 
494     // 调用 DeleteResFile
495     Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos);
496 
497     // 验证文件是否被删除
498     EXPECT_FALSE(std::filesystem::exists(infos->destPath));
499     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0001 End";
500 }
501 
502 /**
503 * @tc.name: FileCopyManager_DeleteResFile_0002
504 * @tc.desc: Verify the DeleteResFile function when transListener is not null and destPath does not exist.
505 * @tc.type: FUNC
506 * @tc.require: I7TDJK
507  */
508 HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0002, TestSize.Level0)
509 {
510     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0002 Start";
511 
512     auto infos = std::make_shared<FileInfos>();
513     infos->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_));
514     infos->destPath = "/data/test/nonexistent.txt";
515 
516     // 确保文件不存在
517     EXPECT_FALSE(std::filesystem::exists(infos->destPath));
518 
519     // 调用 DeleteResFile
520     Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos);
521 
522     // 验证文件仍然不存在
523     EXPECT_FALSE(std::filesystem::exists(infos->destPath));
524     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0002 End";
525 }
526 
527 /**
528 * @tc.name: FileCopyManager_DeleteResFile_0003
529 * @tc.desc: Verify the DeleteResFile function when transListener is null and local files/dirs exist.
530 * @tc.type: FUNC
531 * @tc.require: I7TDJK
532  */
533 HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0003, TestSize.Level0)
534 {
535     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0003 Start";
536 
537     auto infos = std::make_shared<FileInfos>();
538     infos->transListener = nullptr; // 模拟 transListener 为 nullptr
539     infos->subDirs = {"/data/test/subdir"};
540 
541     // 创建测试文件和目录
542     std::filesystem::create_directories("/data/test/subdir");
543     std::ofstream("/data/test/file1.txt").close();
544     std::ofstream("/data/test/file2.txt").close();
545     std::ofstream("/data/test/subdir/file3.txt").close();
546 
547     // 模拟 localListener 返回文件路径
548     infos->localListener = std::make_shared<FileCopyLocalListener>("/data/test/test.txt", true, emptyCallback_);
549     infos->localListener->filePaths_ = {
550         "/data/test/file0.txt",
551         "/data/test/file1.txt",
552         "/data/test/file2.txt",
553         "/data/test/subdir/file3.txt"
554     };
555 
556     // 调用 DeleteResFile
557     Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos);
558 
559     // 验证文件和目录是否被删除
560     EXPECT_FALSE(std::filesystem::exists("/data/test/file1.txt"));
561     EXPECT_FALSE(std::filesystem::exists("/data/test/file2.txt"));
562     EXPECT_FALSE(std::filesystem::exists("/data/test/subdir"));
563 
564     // 调用 DeleteResFile
565     infos->localListener = nullptr;
566     Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos);
567     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0003 End";
568 }
569 
570 /**
571 * @tc.name: FileCopyManager_DeleteResFile_0004
572 * @tc.desc: Verify the DeleteResFile function when transListener is null and local files/dirs do not exist.
573 * @tc.type: FUNC
574 * @tc.require: I7TDJK
575  */
576 HWTEST_F(FileCopyManagerTest, FileCopyManager_DeleteResFile_0004, TestSize.Level0)
577 {
578     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0004 Start";
579 
580     auto infos = std::make_shared<FileInfos>();
581     infos->transListener = nullptr; // 模拟 transListener 为 nullptr
582     infos->subDirs = {"/data/test/nonexistent_subdir"};
583 
584     // 模拟 localListener 返回文件路径
585     infos->localListener = std::make_shared<FileCopyLocalListener>("/data/test/test.txt", true, emptyCallback_);
586     infos->localListener->filePaths_ = {
587         "/data/test/nonexistent_file.txt",
588         "/data/test/nonexistent_file.txt"
589     };
590 
591     // 调用 DeleteResFile
592     Storage::DistributedFile::FileCopyManager::GetInstance()->DeleteResFile(infos);
593 
594     // 验证文件和目录仍然不存在
595     EXPECT_FALSE(std::filesystem::exists("/data/test/nonexistent_file.txt"));
596     EXPECT_FALSE(std::filesystem::exists("/data/test/nonexistent_subdir"));
597     GTEST_LOG_(INFO) << "FileCopyManager_DeleteResFile_0004 End";
598 }
599 
600 /**
601 * @tc.name: FileCopyManager_OpenSrcFile_0001
602 * @tc.desc: Verify the OpenSrcFile function when srcUri is a media URI.
603 * @tc.type: FUNC
604 * @tc.require: I7TDJK
605 */
606 HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0001, TestSize.Level0)
607 {
608     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0001 Start";
609 
610     // Prepare mock data for the test
611     auto infos = std::make_shared<FileInfos>();
612     infos->srcUri = "file://media/image/12";
613     infos->srcPath = "/media/image/12";
614 
615     // Mock remote connection and dataShareHelper behavior (use a mock framework if necessary)
616     // Here we assume dataShareHelper->OpenFile() will return a valid file descriptor (for simplicity).
617     int32_t srcFd = -1;
618 
619     // Call the OpenSrcFile function
620     int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd);
621 
622     // Validate the file descriptor is opened successfully
623     EXPECT_EQ(ret, 1);
624     EXPECT_GE(srcFd, -1); // Expect a valid file descriptor
625 
626     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0001 End";
627 }
628 
629 /**
630 * @tc.name: FileCopyManager_OpenSrcFile_0002
631 * @tc.desc: Verify the OpenSrcFile function when srcUri is a media URI.
632 * @tc.type: FUNC
633 * @tc.require: I7TDJK
634 */
635 HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0002, TestSize.Level0)
636 {
637     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0002 Start";
638 
639     // Prepare mock data for the test
640     auto infos = std::make_shared<FileInfos>();
641     infos->srcUri = "file://media/image/12";
642     infos->srcPath = "/data/test/existentfile.txt";
643 
644     // Mock remote connection and dataShareHelper behavior (use a mock framework if necessary)
645     // Here we assume dataShareHelper->OpenFile() will return a valid file descriptor (for simplicity).
646     int32_t srcFd = -1;
647     std::ofstream(infos->srcPath).close();
648 
649     // Call the OpenSrcFile function
650     int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd);
651 
652     // Validate the file descriptor is opened successfully
653     EXPECT_EQ(ret, 1);
654     EXPECT_GE(srcFd, -1); // Expect a valid file descriptor
655 
656     // Clean up the mock file
657     std::remove(infos->srcPath.c_str());
658 
659     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0002 End";
660 }
661 
662 /**
663 * @tc.name: FileCopyManager_OpenSrcFile_0003
664 * @tc.desc: Verify the OpenSrcFile function when srcUri is a local file URI and file does not exist.
665 * @tc.type: FUNC
666 * @tc.require: I7TDJK
667 */
668 HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0003, TestSize.Level0)
669 {
670     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0003 Start";
671 
672     // Prepare mock data for the test
673     auto infos = std::make_shared<FileInfos>();
674     infos->srcUri = "file:///data/test/nonexistentfile.txt";
675     infos->srcPath = "/data/test/nonexistentfile.txt";
676 
677     // Call the OpenSrcFile function
678     int32_t srcFd = -1;
679     int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd);
680 
681     // Validate that the function fails when file does not exist
682     EXPECT_NE(ret, 0);  // Expect error code
683     EXPECT_EQ(srcFd, -1); // Expect invalid file descriptor
684 
685     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0003 End";
686 }
687 
688 /**
689 * @tc.name: FileCopyManager_OpenSrcFile_0004
690 * @tc.desc: Verify the OpenSrcFile function when srcUri is a local file URI and file exists.
691 * @tc.type: FUNC
692 * @tc.require: I7TDJK
693 */
694 HWTEST_F(FileCopyManagerTest, FileCopyManager_OpenSrcFile_0004, TestSize.Level0)
695 {
696     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0004 Start";
697 
698     // Prepare mock data for the test
699     auto infos = std::make_shared<FileInfos>();
700     infos->srcUri = "file:///data/test/existingfile.txt";
701     infos->srcPath = "/data/test/existingfile.txt";
702 
703     // Create a mock file for the test
704     std::ofstream outfile(infos->srcPath);
705     outfile << "Test content";
706     outfile.close();
707 
708     // Call the OpenSrcFile function
709     int32_t srcFd = -1;
710     int ret = Storage::DistributedFile::FileCopyManager::GetInstance()->OpenSrcFile(infos->srcPath, infos, srcFd);
711 
712     // Validate the file descriptor is opened successfully
713     EXPECT_EQ(ret, 0);
714     EXPECT_GE(srcFd, 0);  // Expect a valid file descriptor
715 
716     // Clean up the mock file
717     std::remove(infos->srcPath.c_str());
718 
719     GTEST_LOG_(INFO) << "FileCopyManager_OpenSrcFile_0004 End";
720 }
721 
722 /**
723 * @tc.name: FileCopyManager_CheckOrCreateLPath_0001
724 * @tc.desc: CheckOrCreateLPath
725 * @tc.type: FUNC
726 * @tc.require: I7TDJK
727 */
728 HWTEST_F(FileCopyManagerTest, FileCopyManager_CheckOrCreateLPath_0001, TestSize.Level1)
729 {
730     GTEST_LOG_(INFO) << "FileCopyManager_CheckOrCreateLPath_0001 Start";
731 
732     std::string destPath = "";
733     int32_t ret = Storage::DistributedFile::FileCopyManager::GetInstance()->CheckOrCreatePath(destPath);
734     EXPECT_EQ(ret, FILE_NOT_FOUND);
735 
736     GTEST_LOG_(INFO) << "FileCopyManager_CheckOrCreateLPath_0001 End";
737 }
738 
739 /**
740 * @tc.name: FileCopyManager_RemoveFileInfos_0001
741 * @tc.desc: RemoveFileInfos
742 * @tc.type: FUNC
743 * @tc.require: I7TDJK
744 */
745 HWTEST_F(FileCopyManagerTest, FileCopyManager_RemoveFileInfos_0001, TestSize.Level0)
746 {
747     GTEST_LOG_(INFO) << "FileCopyManager_RemoveFileInfos_0001 Start";
748     auto infos1 = std::make_shared<FileInfos>();
749     auto infos2 = std::make_shared<FileInfos>();
750     auto infos3 = std::make_shared<FileInfos>();
751     infos1->srcUri = "srcUri1";
752     infos1->destUri = "destUri1";
753     infos2->srcUri = "srcUri2";
754     infos2->destUri = "destUri2";
755     infos3->srcUri = "srcUri1";
756     infos3->destUri = "destUri2";
757     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.clear();
758     Storage::DistributedFile::FileCopyManager::GetInstance()->AddFileInfos(infos1);
759     Storage::DistributedFile::FileCopyManager::GetInstance()->AddFileInfos(infos2);
760     auto len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size();
761     EXPECT_EQ(len, 2); // 2: vector size
762 
763     Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos2);
764     len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size();
765     EXPECT_EQ(len, 1); // 1: vector size
766 
767     Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos3);
768     len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size();
769     EXPECT_EQ(len, 1); // 1: vector size
770 
771     Storage::DistributedFile::FileCopyManager::GetInstance()->RemoveFileInfos(infos1);
772     len = Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.size();
773     EXPECT_EQ(len, 0); // 0: vector size
774 }
775 
776 /**
777 * @tc.name: FileCopyManager_RecurCopyDir_0001
778 * @tc.desc: RecurCopyDir
779 * @tc.type: FUNC
780 * @tc.require: I7TDJK
781 */
782 HWTEST_F(FileCopyManagerTest, FileCopyManager_RecurCopyDir_0001, TestSize.Level0)
783 {
784     GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 Start";
785     auto infos = std::make_shared<FileInfos>();
786     std::string srcPath = "";
787     std::string dstPath = "";
788     auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos);
789     EXPECT_EQ(res, E_OK);
790 
791     srcPath = "/data/test/RecurCopyDir/src/";
792     dstPath = "/data/test/RecurCopyDir/dst/";
793 
794     std::string subSrcDir = srcPath + "dir/";
795     std::string testFile = srcPath + "1.txt"; // 1: file name
796     if (!ForceCreateDirectory(subSrcDir)) {
797         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 create dir err" << subSrcDir;
798     }
799     if (!ForceCreateDirectory(dstPath)) {
800         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 create dir err" << dstPath;
801     }
802     int fd = open(testFile.c_str(), O_RDWR | O_CREAT);
803     if (fd < 0) {
804         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 create file err" << testFile;
805     } else {
806         close(fd);
807     }
808     std::string testLink = srcPath + "2";
809     if (symlink(testFile.c_str(), testLink.c_str())) {
810         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 create linkfile err" << testLink;
811     }
812     infos->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440602(uint64_t processSize, uint64_t totalSize) 813         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
814     res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos);
815     EXPECT_EQ(res, E_OK);
816 
817     dstPath = dstPath + "dir/dir1/dir2";
818     res = Storage::DistributedFile::FileCopyManager::GetInstance()->RecurCopyDir(srcPath, dstPath, infos);
819     EXPECT_EQ(res, ENOENT);
820 
821     std::string rootPath = "/data/test/RecurCopyDir";
822     if (!ForceRemoveDirectory(rootPath)) {
823         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 remove dir err" << subSrcDir;
824     }
825     GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 End";
826 }
827 
828 /**
829 * @tc.name: FileCopyManager_CopySubDir_0001
830 * @tc.desc: CopySubDir
831 * @tc.type: FUNC
832 * @tc.require: I7TDJK
833 */
834 HWTEST_F(FileCopyManagerTest, FileCopyManager_CopySubDir_0001, TestSize.Level0)
835 {
836     GTEST_LOG_(INFO) << "FileCopyManager_CopySubDir_0001 Start";
837     auto infos = std::make_shared<FileInfos>();
838     std::string srcPath = "/data/test/CopySubDir/src";
839     std::string dstPath = "/data/test/CopySubDir/dst";
840     infos->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440702(uint64_t processSize, uint64_t totalSize) 841         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
842     auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, dstPath, infos);
843     EXPECT_EQ(res, ENOENT);
844 
845     if (!ForceCreateDirectory(dstPath)) {
846         GTEST_LOG_(INFO) << "FileCopyManager_CopySubDir_0001 create dir err" << dstPath;
847     }
848     res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, dstPath, infos);
849     EXPECT_EQ(res, E_OK);
850 
851     std::string tmpDstDir = dstPath + "/dir";
852     res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopySubDir(srcPath, tmpDstDir, infos);
853     EXPECT_EQ(res, E_OK);
854     std::string rootPath = "/data/test/CopySubDir";
855     if (!ForceRemoveDirectory(rootPath)) {
856         GTEST_LOG_(INFO) << "FileCopyManager_CopySubDir_0001 create dir err" << rootPath;
857     }
858     GTEST_LOG_(INFO) << "FileCopyManager_CopySubDir_0001 End";
859 }
860 
861 /**
862 * @tc.name: FileCopyManager_CopyDirFunc_0001
863 * @tc.desc: CopyDirFunc
864 * @tc.type: FUNC
865 * @tc.require: I7TDJK
866 */
867 HWTEST_F(FileCopyManagerTest, FileCopyManager_CopyDirFunc_0001, TestSize.Level0)
868 {
869     GTEST_LOG_(INFO) << "FileCopyManager_CopyDirFunc_0001 Start";
870     auto infos = std::make_shared<FileInfos>();
871     std::string srcPath = "/data/test/CopyDirFunc/";
872     std::string dstPath = "/data/test/CopyDirFunc/dst";
873     infos->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440802(uint64_t processSize, uint64_t totalSize) 874         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
875     auto res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(srcPath, dstPath, infos);
876     EXPECT_EQ(res, EINVAL);
877 
878     std::string tmpSrcPath = "/test/CopyDirFunc/";
879     res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(tmpSrcPath, dstPath, infos);
880     EXPECT_EQ(res, ENOENT);
881 
882     srcPath += "src";
883     if (!ForceCreateDirectory(dstPath)) {
884         GTEST_LOG_(INFO) << "FileCopyManager_CopyDirFunc_0001 create dir err" << dstPath;
885     }
886     res = Storage::DistributedFile::FileCopyManager::GetInstance()->CopyDirFunc(srcPath, dstPath, infos);
887     EXPECT_EQ(res, E_OK);
888     std::string rootPath = "/data/test/CopyDirFunc";
889     if (!ForceRemoveDirectory(rootPath)) {
890         GTEST_LOG_(INFO) << "FileCopyManager_RecurCopyDir_0001 remove dir err" << rootPath;
891     }
892     GTEST_LOG_(INFO) << "FileCopyManager_CopyDirFunc_0001 End";
893 }
894 
895 /**
896 * @tc.name: FileCopyManager_Cancel_0001
897 * @tc.desc: The execution of the cancel succeed.
898 * @tc.type: FUNC
899 * @tc.require: I7TDJK
900  */
901 HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0001, TestSize.Level0)
902 {
903     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0001 Start";
904     auto infos1 = std::make_shared<FileInfos>();
905     auto infos2 = std::make_shared<FileInfos>();
906     infos1->srcUri = "srcUri1";
907     infos1->destUri = "destUri1";
908     infos1->transListener = nullptr;
909     infos1->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440902(uint64_t processSize, uint64_t totalSize) 910         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
911     infos2->srcUri = "srcUri2";
912     infos2->destUri = "destUri2";
913     infos2->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_));
914     infos2->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440a02(uint64_t processSize, uint64_t totalSize) 915         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
916     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1);
917     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos2);
918     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel();
919     EXPECT_EQ(ret, E_OK);
920     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0001 End";
921 }
922 
923 /**
924 * @tc.name: FileCopyManager_Cancel_0002
925 * @tc.desc: The execution of the cancel succeed.
926 * @tc.type: FUNC
927 * @tc.require: I7TDJK
928  */
929 HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0002, TestSize.Level0)
930 {
931     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0002 Start";
932     auto infos1 = std::make_shared<FileInfos>();
933     infos1->srcUri = "srcUri1";
934     infos1->destUri = "destUri1";
935     infos1->transListener = nullptr;
936     infos1->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440b02(uint64_t processSize, uint64_t totalSize) 937         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
938     std::string srcUri = "srcUri1";
939     std::string destUri = "destUri1";
940 
941     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1);
942     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri, true);
943     EXPECT_EQ(ret, E_OK);
944     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0002 End";
945 }
946 
947 /**
948 * @tc.name: FileCopyManager_Cancel_0003
949 * @tc.desc: The execution of the cancel succeed.
950 * @tc.type: FUNC
951 * @tc.require: I7TDJK
952  */
953 HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0003, TestSize.Level0)
954 {
955     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0003 Start";
956     auto infos1 = std::make_shared<FileInfos>();
957     auto infos2 = std::make_shared<FileInfos>();
958     infos1->srcUri = "srcUri1";
959     infos1->destUri = "destUri1";
960     infos1->transListener = nullptr;
961     infos1->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440c02(uint64_t processSize, uint64_t totalSize) 962         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
963     infos2->srcUri = "srcUri2";
964     infos2->destUri = "destUri2";
965     infos2->transListener = sptr(new (std::nothrow) TransListener("/data/test/test.txt", emptyCallback_));
966     infos2->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440d02(uint64_t processSize, uint64_t totalSize) 967         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
968     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1);
969     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos2);
970     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(true);
971     EXPECT_EQ(ret, E_OK);
972     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0003 End";
973 }
974 
975 /**
976 * @tc.name: FileCopyManager_Cancel_0004
977 * @tc.desc: The execution of the cancel succeed.
978 * @tc.type: FUNC
979 * @tc.require: I7TDJK
980  */
981 HWTEST_F(FileCopyManagerTest, FileCopyManager_Cancel_0004, TestSize.Level0)
982 {
983     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0004 Start";
984     auto infos1 = std::make_shared<FileInfos>();
985     infos1->srcUri = "srcUri1";
986     infos1->destUri = "destUri1";
987     infos1->transListener = nullptr;
988     infos1->localListener = std::make_shared<FileCopyLocalListener>("",
__anona324e1440e02(uint64_t processSize, uint64_t totalSize) 989         true, [](uint64_t processSize, uint64_t totalSize) -> void {});
990     std::string srcUri = "srcUri1";
991     std::string destUri = "destUri3";
992 
993     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1);
994     auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
995     EXPECT_EQ(ret, E_OK);
996     srcUri = "srcUri2";
997     destUri = "destUri1";
998     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
999     EXPECT_EQ(ret, E_OK);
1000 
1001     srcUri = "srcUri2";
1002     destUri = "destUri2";
1003     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
1004     EXPECT_EQ(ret, E_OK);
1005 
1006     srcUri = "srcUri1";
1007     destUri = "destUri1";
1008     ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
1009     EXPECT_EQ(ret, E_OK);
1010     Storage::DistributedFile::FileCopyManager::GetInstance()->FileInfosVec_.clear();
1011     GTEST_LOG_(INFO) << "FileCopyManager_Cancel_0004 End";
1012 }
1013 
1014 /**
1015  * @tc.name: FileCopyManager_MakeDir_0001
1016  * @tc.desc: test MakeDir function.
1017  * @tc.type: FUNC
1018  * @tc.require: I7TDJK
1019  */
1020 HWTEST_F(FileCopyManagerTest, FileCopyManager_MakeDir_0001, TestSize.Level1)
1021 {
1022     GTEST_LOG_(INFO) << "FileCopyManager_MakeDir_0001";
1023     string srcpath = "/storage/media/100/local/files/Docs/bb/";
1024     std::error_code errCode;
1025     int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath);
1026     EXPECT_EQ(res, E_OK);
1027     ASSERT_EQ(ForceRemoveDirectory(srcpath.c_str()), true);
1028     GTEST_LOG_(INFO) << "FileCopyManager_MakeDir_0001 End";
1029 }
1030 }