• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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_core.h"
17 #include "uv_fs_mock.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace OHOS::FileManagement::ModuleFileIO::Test {
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace std;
25 
26 class CopyFileCoreMockTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32     static inline shared_ptr<UvfsMock>  uvfs = nullptr;
33 };
34 
SetUpTestCase(void)35 void CopyFileCoreMockTest::SetUpTestCase(void)
36 {
37     uvfs = std::make_shared<UvfsMock>();
38     Uvfs::ins = uvfs;
39     GTEST_LOG_(INFO) << "SetUpTestCase";
40 }
41 
TearDownTestCase(void)42 void CopyFileCoreMockTest::TearDownTestCase(void)
43 {
44     Uvfs::ins = nullptr;
45     uvfs = nullptr;
46     GTEST_LOG_(INFO) << "TearDownTestCase";
47 }
48 
SetUp(void)49 void CopyFileCoreMockTest::SetUp(void)
50 {
51     GTEST_LOG_(INFO) << "SetUp";
52 }
53 
TearDown(void)54 void CopyFileCoreMockTest::TearDown(void)
55 {
56     GTEST_LOG_(INFO) << "TearDown";
57 }
58 
59 /**
60  * @tc.name: CopyFileCoreMockTest_DoCopyFile_001
61  * @tc.desc: Test function of CopyFileCore::ValidMode interface for FALSE.
62  * @tc.size: MEDIUM
63  * @tc.type: FUNC
64  * @tc.level Level 1
65  */
66 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_001, testing::ext::TestSize.Level1)
67 {
68     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_001";
69 
70     FileInfo src;
71     FileInfo dest;
72     optional<int32_t> mode = std::make_optional(1);
73 
74     auto res = CopyFileCore::DoCopyFile(src, dest, mode);
75     EXPECT_EQ(res.IsSuccess(), false);
76 
77     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_001";
78 }
79 
80 /**
81  * @tc.name: CopyFileCoreMockTest_DoCopyFile_003
82  * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE.
83  * @tc.size: MEDIUM
84  * @tc.type: FUNC
85  * @tc.level Level 1
86  */
87 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_003, testing::ext::TestSize.Level1)
88 {
89     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_003";
90 
91     FileInfo src;
92     FileInfo dest;
93     src.isPath = true;
94     dest.isPath = false;
95 
96     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1));
97 
98     auto res = CopyFileCore::DoCopyFile(src, dest);
99     EXPECT_EQ(res.IsSuccess(), false);
100 
101     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_003";
102 }
103 
104 /**
105  * @tc.name: CopyFileCoreMockTest_DoCopyFile_005
106  * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for FALSE.
107  * @tc.size: MEDIUM
108  * @tc.type: FUNC
109  * @tc.level Level 1
110  */
111 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_005, testing::ext::TestSize.Level1)
112 {
113     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_005";
114 
115     FileInfo src;
116     FileInfo dest;
117     src.isPath = false;
118     dest.isPath = true;
119     int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
120     ASSERT_NE(fd, -1);
121     src.fdg = make_unique<DistributedFS::FDGuard>(fd);
122 
123     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(-1));
124 
125     auto res = CopyFileCore::DoCopyFile(src, dest);
126     EXPECT_EQ(res.IsSuccess(), false);
127     close(fd);
128 
129     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_005";
130 }
131 
132 /**
133  * @tc.name: CopyFileCoreMockTest_DoCopyFile_006
134  * @tc.desc: Test function of CopyFileCore::OpenFile.OpenCore interface for SUCCESS.
135  * @tc.size: MEDIUM
136  * @tc.type: FUNC
137  * @tc.level Level 1
138  */
139 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_006, testing::ext::TestSize.Level1)
140 {
141     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_006";
142 
143     FileInfo src;
144     FileInfo dest;
145     src.isPath = false;
146     dest.isPath = true;
147     int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
148     ASSERT_NE(fd, -1);
149     src.fdg = make_unique<DistributedFS::FDGuard>(fd);
150 
151     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1));
152 
153     auto res = CopyFileCore::DoCopyFile(src, dest);
154     EXPECT_EQ(res.IsSuccess(), true);
155     close(fd);
156 
157     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_006";
158 }
159 
160 /**
161  * @tc.name: CopyFileCoreMockTest_DoCopyFile_007
162  * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE.
163  * @tc.size: MEDIUM
164  * @tc.type: FUNC
165  * @tc.level Level 1
166  */
167 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_007, testing::ext::TestSize.Level1)
168 {
169     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_007";
170 
171     FileInfo src;
172     FileInfo dest;
173     src.isPath = false;
174     dest.isPath = false;
175     int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
176     ASSERT_NE(fd, -1);
177     src.fdg = make_unique<DistributedFS::FDGuard>(fd);
178     dest.fdg = make_unique<DistributedFS::FDGuard>(fd);
179 
180     EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(-1));
181 
182     auto res = CopyFileCore::DoCopyFile(src, dest);
183     EXPECT_EQ(res.IsSuccess(), false);
184     close(fd);
185 
186     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_007";
187 }
188 
189 /**
190  * @tc.name: CopyFileCoreMockTest_DoCopyFile_008
191  * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for FALSE.
192  * @tc.size: MEDIUM
193  * @tc.type: FUNC
194  * @tc.level Level 1
195  */
196 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_008, testing::ext::TestSize.Level1)
197 {
198     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_008";
199 
200     FileInfo src;
201     FileInfo dest;
202     src.isPath = false;
203     dest.isPath = false;
204     int fd = open("test.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
205     ASSERT_NE(fd, -1);
206     src.fdg = make_unique<DistributedFS::FDGuard>(fd);
207     dest.fdg = make_unique<DistributedFS::FDGuard>();
208 
209     EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1));
210 
211     auto res = CopyFileCore::DoCopyFile(src, dest);
212     EXPECT_EQ(res.IsSuccess(), false);
213     close(fd);
214 
215     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_008";
216 }
217 
218 /**
219  * @tc.name: CopyFileCoreMockTest_DoCopyFile_009
220  * @tc.desc: Test function of CopyFileCore::OpenFile.TruncateCore interface for SUCCESS.
221  * @tc.size: MEDIUM
222  * @tc.type: FUNC
223  * @tc.level Level 1
224  */
225 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_009, testing::ext::TestSize.Level1)
226 {
227     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_009";
228 
229     FileInfo src;
230     FileInfo dest;
231     src.isPath = false;
232     dest.isPath = false;
233 
234     int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
235     int destfd = open("dest.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
236     ASSERT_NE(srcfd, -1);
237     ASSERT_NE(destfd, -1);
238     src.fdg = make_unique<DistributedFS::FDGuard>(srcfd);
239     dest.fdg = make_unique<DistributedFS::FDGuard>(destfd);
240 
241     EXPECT_CALL(*uvfs, uv_fs_ftruncate(_, _, _, _, _)).Times(1).WillOnce(Return(1));
242 
243     auto res = CopyFileCore::DoCopyFile(src, dest);
244     EXPECT_EQ(res.IsSuccess(), true);
245     close(srcfd);
246     close(destfd);
247 
248     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_009";
249 }
250 
251 /**
252  * @tc.name: CopyFileCoreMockTest_DoCopyFile_0010
253  * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false.
254  * @tc.size: MEDIUM
255  * @tc.type: FUNC
256  * @tc.level Level 1
257  */
258 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0010, testing::ext::TestSize.Level1)
259 {
260     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_0010";
261 
262     FileInfo src;
263     FileInfo dest;
264     src.isPath = false;
265     dest.isPath = true;
266 
267     int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
268     ASSERT_NE(srcfd, -1);
269     const char* data = "Hello, World!";
270     ssize_t len = write(srcfd, data, strlen(data));
271     ASSERT_NE(len, -1);
272     src.fdg = make_unique<DistributedFS::FDGuard>(srcfd);
273 
274     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1));
275     EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(-1));
276 
277     auto res = CopyFileCore::DoCopyFile(src, dest);
278     EXPECT_EQ(res.IsSuccess(), false);
279     close(srcfd);
280 
281     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_0010";
282 }
283 
284 /**
285  * @tc.name: CopyFileCoreMockTest_DoCopyFile_0011
286  * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for false.
287  * @tc.size: MEDIUM
288  * @tc.type: FUNC
289  * @tc.level Level 1
290  */
291 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0011, testing::ext::TestSize.Level1)
292 {
293     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_0011";
294 
295     FileInfo src;
296     FileInfo dest;
297     src.isPath = false;
298     dest.isPath = true;
299 
300     int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
301     ASSERT_NE(srcfd, -1);
302     const char* data = "Hello, World!";
303     ssize_t len = write(srcfd, data, strlen(data));
304     ASSERT_NE(len, -1);
305     src.fdg = make_unique<DistributedFS::FDGuard>(srcfd);
306 
307     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1));
308     EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len + 1));
309 
310     auto res = CopyFileCore::DoCopyFile(src, dest);
311     EXPECT_EQ(res.IsSuccess(), false);
312     close(srcfd);
313 
314     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_0011";
315 }
316 
317 /**
318  * @tc.name: CopyFileCoreMockTest_DoCopyFile_0012
319  * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for SUCCESS.
320  * @tc.size: MEDIUM
321  * @tc.type: FUNC
322  * @tc.level Level 1
323  */
324 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0012, testing::ext::TestSize.Level1)
325 {
326     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_0012";
327 
328     FileInfo src;
329     FileInfo dest;
330     src.isPath = false;
331     dest.isPath = true;
332 
333     int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
334     ASSERT_NE(srcfd, -1);
335     const char* data = "Hello, World!";
336     ssize_t len = write(srcfd, data, strlen(data));
337     ASSERT_NE(len, -1);
338     src.fdg = make_unique<DistributedFS::FDGuard>(srcfd);
339 
340     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1));
341     EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(len));
342 
343     auto res = CopyFileCore::DoCopyFile(src, dest);
344     EXPECT_EQ(res.IsSuccess(), true);
345     close(srcfd);
346 
347     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_0012";
348 }
349 
350 /**
351  * @tc.name: CopyFileCoreMockTest_DoCopyFile_0013
352  * @tc.desc: Test function of CopyFileCore::OpenFile.SendFileCore interface for FALSE.
353  * @tc.size: MEDIUM
354  * @tc.type: FUNC
355  * @tc.level Level 1
356  */
357 HWTEST_F(CopyFileCoreMockTest, CopyFileCoreMockTest_DoCopyFile_0013, testing::ext::TestSize.Level1)
358 {
359     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-begin CopyFileCoreMockTest_DoCopyFile_0013";
360 
361     FileInfo src;
362     FileInfo dest;
363     src.isPath = false;
364     dest.isPath = true;
365 
366     int srcfd = open("src.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
367     ASSERT_NE(srcfd, -1);
368     const char* data = "Hello, World!";
369     ssize_t len = write(srcfd, data, strlen(data));
370     ASSERT_NE(len, -1);
371     src.fdg = make_unique<DistributedFS::FDGuard>(srcfd);
372 
373     EXPECT_CALL(*uvfs, uv_fs_open(_, _, _, _, _, _)).Times(1).WillOnce(Return(1));
374     EXPECT_CALL(*uvfs, uv_fs_sendfile(_, _, _, _, _, _, _)).Times(1).WillOnce(Return(0));
375 
376     auto res = CopyFileCore::DoCopyFile(src, dest);
377     EXPECT_EQ(res.IsSuccess(), false);
378     close(srcfd);
379 
380     GTEST_LOG_(INFO) << "CopyFileCoreMockTest-end CopyFileCoreMockTest_DoCopyFile_0013";
381 }
382 
383 } // namespace OHOS::FileManagement::ModuleFileIO::Test