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/remote_file_copy_manager.h"
17
18 #include <fstream>
19 #include <gtest/gtest.h>
20
21 #include "datashare_helper.h"
22 #include "dfs_error.h"
23 #include "directory_ex.h"
24 #include "distributed_file_fd_guard.h"
25 #include "file_copy_listener.h"
26 #include "file_copy_manager.h"
27 #include "file_size_utils.h"
28 #include "sandbox_helper.h"
29 #include "trans_listener.h"
30
31 std::string g_physicalPath = "/test/test";
32 int32_t g_getPhysicalPath = 0;
33
34 namespace OHOS::AppFileService {
GetPhysicalPath(const std::string & fileUri,const std::string & userId,std::string & physicalPath)35 int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath)
36 {
37 physicalPath = g_physicalPath;
38 return g_getPhysicalPath;
39 }
40 } // namespace OHOS::AppFileService
41
42 namespace OHOS::Storage::DistributedFile::Test {
43 using namespace OHOS::FileManagement;
44 using namespace testing;
45 using namespace testing::ext;
46 using namespace std;
47 class RemoteFileCopyManagerTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53
54 uint64_t process_ = 0;
55 uint64_t fileSize_ = 0;
56 using callBack = std::function<void(uint64_t processSize, uint64_t totalFileSize)>;
__anon6c403e4f0102(uint64_t processSize, uint64_t totalFileSize) 57 callBack listener_ = [&](uint64_t processSize, uint64_t totalFileSize) {
58 process_ = processSize;
59 fileSize_ = totalFileSize;
60 };
61 // 定义一个空的 ProcessCallback 用于测试
__anon6c403e4f0202(uint64_t processSize, uint64_t totalFileSize) 62 ProcessCallback emptyCallback_ = [](uint64_t processSize, uint64_t totalFileSize) {};
63 };
64
SetUpTestCase(void)65 void RemoteFileCopyManagerTest::SetUpTestCase(void)
66 {
67 GTEST_LOG_(INFO) << "SetUpTestCase";
68 }
69
TearDownTestCase(void)70 void RemoteFileCopyManagerTest::TearDownTestCase(void)
71 {
72 GTEST_LOG_(INFO) << "TearDownTestCase";
73 }
74
SetUp(void)75 void RemoteFileCopyManagerTest::SetUp(void)
76 {
77 GTEST_LOG_(INFO) << "SetUp";
78 }
79
TearDown(void)80 void RemoteFileCopyManagerTest::TearDown(void)
81 {
82 GTEST_LOG_(INFO) << "TearDown";
83 }
84
85 /**
86 * @tc.name: RemoteFileCopyManager_Copy_0001
87 * @tc.desc: The execution of the Copy failed.
88 * @tc.type: FUNC
89 * @tc.require: I7TDJK
90 */
91 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0001, TestSize.Level0)
92 {
93 GTEST_LOG_(INFO) << "FileCopyManager_Copy_0001 Start";
94 string localUri = "/data/test/test.txt";
95 string dstUri = "/data/test/test.txt";
96 int32_t userId = 100;
97 string copyPath = "/data/storage/el2/distributedfiles/123412345/test.txt";
98
99 sptr<IFileTransListener> listenerCallback;
100
101 auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy("", localUri, listenerCallback, userId, copyPath);
102 EXPECT_EQ(ret, EINVAL);
103
104 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(localUri, "", listenerCallback, userId, copyPath);
105 EXPECT_EQ(ret, EINVAL);
106
107 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy("", "", listenerCallback, userId, copyPath);
108 EXPECT_EQ(ret, EINVAL);
109
110 string remoteUri = "/data/test/Copy/?networkid=/";
111 if (!ForceCreateDirectory(remoteUri)) {
112 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0001 create dir err";
113 }
114 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(remoteUri, "", listenerCallback, userId, copyPath);
115 EXPECT_EQ(ret, EINVAL);
116 if (!ForceRemoveDirectory(remoteUri)) {
117 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0001 remove dir err";
118 }
119 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0001 End";
120 }
121
122 /**
123 * @tc.name: RemoteFileCopyManager_Copy_0002
124 * @tc.desc: The execution of the Copy succeed.
125 * @tc.type: FUNC
126 * @tc.require: I7TDJK
127 */
128 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_Copy_0002, TestSize.Level0)
129 {
130 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0002 Start";
131 string srcUri = "file://docs/storage/media/100/local/files/Docs/1.txt";
132 string destUri = "file://docs/storage/media/100/local/files/Docs/dest1.txt";
133 string srcPath = "/storage/media/100/local/files/Docs/1.txt";
134 int32_t userId = 100;
135 string copyPath = "/data/storage/el2/distributedfiles/123412345";
136
137 sptr<IFileTransListener> listenerCallback;
138 int fd = open(srcPath.c_str(), O_RDWR | O_CREAT);
139 ASSERT_TRUE(fd != -1) <<"Failed to open file in RemoteFileCopyManager_Copy_0002!" << errno;
140 close(fd);
141
142 auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCopy(srcUri, destUri, listenerCallback, userId, copyPath);
143 EXPECT_EQ(ret, ENOENT);
144 ASSERT_EQ(remove(srcPath.c_str()), 0);
145 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Copy_0002 End";
146 }
147
148 /**
149 * @tc.name: RemoteFileCopyManager_RemoteCancel_0001
150 * @tc.desc: The execution of the cancel succeed.
151 * @tc.type: FUNC
152 * @tc.require: I7TDJK
153 */
154 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoteCancel_0001, TestSize.Level0)
155 {
156 GTEST_LOG_(INFO) << "RemoteFileCopyManager_Cancel_0001 Start";
157 auto infos1 = std::make_shared<FileInfos>();
158 infos1->srcUri = "srcUri1";
159 infos1->destUri = "destUri1";
160 infos1->transListener = nullptr;
161 infos1->localListener = std::make_shared<FileCopyLocalListener>("",
__anon6c403e4f0302(uint64_t processSize, uint64_t totalSize) 162 true, [](uint64_t processSize, uint64_t totalSize) -> void {});
163 std::string srcUri = "srcUri1";
164 std::string destUri = "destUri3";
165
166 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.emplace_back(infos1);
167 auto ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri);
168 EXPECT_EQ(ret, E_OK);
169 srcUri = "srcUri2";
170 destUri = "destUri1";
171 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri);
172 EXPECT_EQ(ret, E_OK);
173
174 srcUri = "srcUri2";
175 destUri = "destUri2";
176 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri);
177 EXPECT_EQ(ret, E_OK);
178
179 srcUri = "srcUri1";
180 destUri = "destUri1";
181 ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoteCancel(srcUri, destUri);
182 EXPECT_EQ(ret, E_OK);
183 GTEST_LOG_(INFO) << "RemoteFileCopyManager_RemoteCancel_0001 End";
184 }
185
186 /**
187 * @tc.name: RemoteFileCopyManager_RemoveFileInfos_0001
188 * @tc.desc: RemoveFileInfos
189 * @tc.type: FUNC
190 * @tc.require: I7TDJK
191 */
192 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_RemoveFileInfos_0001, TestSize.Level0)
193 {
194 GTEST_LOG_(INFO) << "RemoteFileCopyManager_RemoveFileInfos_0001 Start";
195 auto infos1 = std::make_shared<FileInfos>();
196 auto infos2 = std::make_shared<FileInfos>();
197 auto infos3 = std::make_shared<FileInfos>();
198 infos1->srcUri = "srcUri1";
199 infos1->destUri = "destUri1";
200 infos2->srcUri = "srcUri2";
201 infos2->destUri = "destUri2";
202 infos3->srcUri = "srcUri1";
203 infos3->destUri = "destUri2";
204 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.clear();
205 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->AddFileInfos(infos1);
206 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->AddFileInfos(infos2);
207 auto len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size();
208 EXPECT_EQ(len, 2); // 2: vector size
209
210 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos2);
211 len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size();
212 EXPECT_EQ(len, 1); // 1: vector size
213
214 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos3);
215 len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size();
216 EXPECT_EQ(len, 1); // 1: vector size
217
218 Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->RemoveFileInfos(infos1);
219 len = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->FileInfosVec_.size();
220 EXPECT_EQ(len, 0); // 0: vector size
221 }
222
223 /**
224 * @tc.name: RemoteFileCopyManager_CreateFileInfos_0001
225 * @tc.desc: CreateFileInfos
226 * @tc.type: FUNC
227 * @tc.require: I7TDJK
228 */
229 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_CreateFileInfos_0001, TestSize.Level0)
230 {
231 GTEST_LOG_(INFO) << "RemoteFileCopyManager_CreateFileInfos_0001 Start";
232 auto infos = std::make_shared<FileInfos>();
233 infos->srcUri = "";
234 int32_t userId = 100;
235 string copyPath = "/data/storage/el2/distributedfiles/123412345/test.txt";
236 int32_t ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->CreateFileInfos("", "", infos, userId, copyPath);
237 EXPECT_EQ(ret, E_OK);
238 GTEST_LOG_(INFO) << "RemoteFileCopyManager_CreateFileInfos_0001 End";
239 }
240
241 /**
242 * @tc.name: FileCopyManager_Cancel_0001
243 * @tc.desc: The execution of the cancel succeed.
244 * @tc.type: FUNC
245 * @tc.require: I7TDJK
246 */
247 HWTEST_F(RemoteFileCopyManagerTest, RemoteFileCopyManager_IsMediaUri_0001, TestSize.Level0)
248 {
249 GTEST_LOG_(INFO) << "RemoteFileCopyManager_IsMediaUri_0001 Start";
250 string srcUri = "file://docs/storage/media/100/local/files/Docs/test.txt";
251 int32_t ret = Storage::DistributedFile::RemoteFileCopyManager::GetInstance()->IsMediaUri(srcUri);
252 EXPECT_EQ(ret, false);
253 GTEST_LOG_(INFO) << "RemoteFileCopyManager_IsMediaUri_0001 End";
254 }
255 }