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 #include <copy/file_copy_manager.h>
16 #include <gtest/gtest.h>
17 #include "dfs_error.h"
18
19 namespace OHOS::Storage::DistributedFile::Test {
20 using namespace OHOS::FileManagement;
21 using namespace testing;
22 using namespace testing::ext;
23 using namespace std;
24
25 class FileCopyManagerTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31
32 uint64_t process = 0;
33 uint64_t fileSize = 0;
34 using callBack = std::function<void(uint64_t processSize, uint64_t totalFileSize)>;
__anon936b79a20102(uint64_t processSize, uint64_t totalFileSize) 35 callBack listener = [&](uint64_t processSize, uint64_t totalFileSize) {
36 process = processSize;
37 fileSize = totalFileSize;
38 };
39 };
40
SetUpTestCase(void)41 void FileCopyManagerTest::SetUpTestCase(void)
42 {
43 GTEST_LOG_(INFO) << "SetUpTestCase";
44 }
45
TearDownTestCase(void)46 void FileCopyManagerTest::TearDownTestCase(void)
47 {
48 GTEST_LOG_(INFO) << "TearDownTestCase";
49 }
50
SetUp(void)51 void FileCopyManagerTest::SetUp(void)
52 {
53 GTEST_LOG_(INFO) << "SetUp";
54 }
55
TearDown(void)56 void FileCopyManagerTest::TearDown(void)
57 {
58 GTEST_LOG_(INFO) << "TearDown";
59 }
60
61 /**
62 * @tc.name: FileCopyManager_Copy_0001
63 * @tc.desc: The execution of the Copy failed.
64 * @tc.type: FUNC
65 * @tc.require: I7TDJK
66 */
67 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0001, TestSize.Level1)
68 {
69 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 Start";
70 string srcUri = "";
71 string destUri = "/data/test/test.txt";
72
73 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener);
74 EXPECT_EQ(ret, E_NOENT);
75 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 End";
76 }
77
78 /**
79 * @tc.name: FileCopyManager_Copy_0002
80 * @tc.desc: The execution of the Copy succeed.
81 * @tc.type: FUNC
82 * @tc.require: I7TDJK
83 */
84 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0002, TestSize.Level1)
85 {
86 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0002 Start";
87 string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
88 string destUri = "file://docs/storage/media/100/local/files/Docs/dest1.txt";
89 string srcPath = "/storage/media/100/local/files/Docs/1.txt";
90 int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
91 ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0002!" << errno;
92 close(fd);
93
94 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener);
95 EXPECT_EQ(ret, E_OK);
96 ASSERT_EQ(remove(srcPath.c_str()), 0);
97 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0002 End";
98 }
99
100 /**
101 * @tc.name: FileCopyManager_Copy_0003
102 * @tc.desc: The execution of the Copy failed.
103 * @tc.type: FUNC
104 * @tc.require: I7TDJK
105 */
106 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0003, TestSize.Level1)
107 {
108 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0003 Start";
109 string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
110 string destUri = "*";
111 string srcPath = "/storage/media/100/local/files/Docs/1.txt";
112 int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
113 ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0003!" << errno;
114 close(fd);
115
116 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Copy(srcUri, destUri, listener);
117 EXPECT_EQ(ret, E_NOENT);
118 ASSERT_EQ(remove(srcPath.c_str()), 0);
119 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0003 End";
120 }
121
122 /**
123 * @tc.name: FileCopyManager_Copy_0004
124 * @tc.desc: The execution of the cancel succeed.
125 * @tc.type: FUNC
126 * @tc.require: I7TDJK
127 */
128 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0004, TestSize.Level1)
129 {
130 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0004 Start";
131 string srcUri = "file://docs/storage/media/100/local/files/Docs/11.txt";
132 string destUri = "file://docs/storage/media/100/local/files/Docs/dest11.txt";
133 string srcpath = "/storage/media/100/local/files/Docs/11.txt";
134 int fd = open(srcpath.c_str(), O_RDWR | O_CREAT);
135 ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0004!" << errno;
136 close(fd);
137 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri, destUri);
138 EXPECT_EQ(ret, E_OK);
139 ASSERT_EQ(remove(srcpath.c_str()), 0);
140 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0004 End";
141 }
142
143 /**
144 * @tc.name: FileCopyManager_Copy_0005
145 * @tc.desc: The execution of the execlocal failed.
146 * @tc.type: FUNC
147 * @tc.require: I7TDJK
148 */
149 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0005, TestSize.Level1)
150 {
151 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0005 Start";
152 string srcuri = "file://docs/storage/media/100/local/files/Docs/11.txt";
153 string desturi = "file://docs/storage/media/100/local/files/Docs/dest11.txt";
154 string srcpath = "/storage/media/100/local/files/Docs/11.txt";
155 int fd = open(srcpath.c_str(), O_RDWR | O_CREAT);
156 ASSERT_TRUE(fd != -1) <<"Failed to open file in FileCopyManager_Copy_0005!" << errno;
157 close(fd);
158
159 auto infos = std::make_shared<FileInfos>();
160 infos->srcUri = srcuri;
161 infos->destUri = desturi;
162 infos->srcPath = srcpath;
163 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
164 EXPECT_EQ(ret, 2);
165 ASSERT_EQ(remove(srcpath.c_str()), 0);
166 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0005 End";
167 }
168
169 /**
170 * @tc.name: FileCopyManager_Copy_0006
171 * @tc.desc: The execution of the execlocal failed.
172 * @tc.type: FUNC
173 * @tc.require: I7TDJK
174 */
175 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0006, TestSize.Level1)
176 {
177 //无对应子文件夹,errno返回2
178 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 Start";
179 string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
180 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
181 string srcpath = "/storage/media/100/local/files/Docs/aa/";
182 string destpath = "/storage/media/100/local/files/Docs/aa1/";
183 std::error_code errCode;
184 if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) {
185 int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath);
186 if (res != E_OK) {
187 GTEST_LOG_(INFO) <<"Failed to mkdir";
188 }
189 } else if (errCode.value() != E_OK) {
190 GTEST_LOG_(INFO) <<"fs exists failed";
191 }
192
193 auto infos = std::make_shared<FileInfos>();
194 infos->srcUri = srcuri;
195 infos->destUri = desturi;
196 infos->srcPath = srcpath;
197 infos->destPath = destpath;
198 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
199 EXPECT_EQ(ret, 2);
200 ASSERT_EQ(remove(srcpath.c_str()), 0);
201 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 End";
202 }
203
204 /**
205 * @tc.name: FileCopyManager_Copy_0007
206 * @tc.desc: The execution of the execlocal failed.
207 * @tc.type: FUNC
208 * @tc.require: I7TDJK
209 */
210 HWTEST_F(FileCopyManagerTest, FileCopyManager_Copy_0007, TestSize.Level1)
211 {
212 //无destpath赋值,stat检查返回2
213 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0006 Start";
214 string srcuri = "file://docs/storage/media/100/local/files/Docs/aa/";
215 string desturi = "file://docs/storage/media/100/local/files/Docs/aa1/";
216 string srcpath = "/storage/media/100/local/files/Docs/aa/";
217 std::error_code errCode;
218 if (!std::filesystem::exists(srcpath, errCode) && errCode.value() == E_OK) {
219 int res = Storage::DistributedFile::FileCopyManager::GetInstance()->MakeDir(srcpath);
220 if (res != E_OK) {
221 GTEST_LOG_(INFO) <<"Failed to mkdir";
222 }
223 } else if (errCode.value() != E_OK) {
224 GTEST_LOG_(INFO) <<"fs exists failed";
225 }
226
227 auto infos = std::make_shared<FileInfos>();
228 infos->srcUri = srcuri;
229 infos->destUri = desturi;
230 infos->srcPath = srcpath;
231 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->ExecLocal(infos);
232 EXPECT_EQ(ret, 2);
233 ASSERT_EQ(remove(srcpath.c_str()), 0);
234 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0007 End";
235 }
236 }