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 <filesystem>
17 #include <fstream>
18 #include <gtest/gtest.h>
19
20 #include "copy_dir_core.h"
21
22 namespace OHOS::FileManagement::ModuleFileIO::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26
27 class CopyDirCoreTest : public testing::Test {
28 public:
29 static filesystem::path g_srcPath;
30 static filesystem::path g_destPath;
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35
CreateTestFile(const filesystem::path & path,const string & content="test")36 void CreateTestFile(const filesystem::path& path, const string& content = "test")
37 {
38 ofstream file(path);
39 file << content;
40 }
41 };
42
43 filesystem::path CopyDirCoreTest::g_srcPath;
44 filesystem::path CopyDirCoreTest::g_destPath;
45
SetUpTestCase(void)46 void CopyDirCoreTest::SetUpTestCase(void)
47 {
48 g_srcPath = filesystem::temp_directory_path() / "src/";
49 g_destPath = filesystem::temp_directory_path() / "dest/";
50 filesystem::create_directory(g_srcPath);
51 filesystem::create_directory(g_destPath);
52 GTEST_LOG_(INFO) << "SetUpTestCase";
53 }
54
TearDownTestCase(void)55 void CopyDirCoreTest::TearDownTestCase(void)
56 {
57 GTEST_LOG_(INFO) << "TearDownTestCase";
58 filesystem::remove_all(g_srcPath);
59 filesystem::remove_all(g_destPath);
60 }
61
SetUp(void)62 void CopyDirCoreTest::SetUp(void)
63 {
64 GTEST_LOG_(INFO) << "SetUp";
65 }
66
TearDown(void)67 void CopyDirCoreTest::TearDown(void)
68 {
69 GTEST_LOG_(INFO) << "TearDown";
70 }
71
72 /**
73 * @tc.name: CopyDirCoreTest_DoCopyDir_001
74 * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with empty directory.
75 * @tc.size: MEDIUM
76 * @tc.type: FUNC
77 * @tc.level Level 1
78 */
79 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_001, testing::ext::TestSize.Level1)
80 {
81 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_001";
82
83 string src = g_srcPath.string() + "/test01";
84 string dest = g_destPath.string();
85 filesystem::create_directories(src);
86
87 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
88
89 EXPECT_TRUE(result.fsResult.IsSuccess());
90 EXPECT_FALSE(result.errFiles.has_value());
91
92 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_001";
93 }
94
95 /**
96 * @tc.name: CopyDirCoreTest_DoCopyDir_002
97 * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid mode.
98 * @tc.size: MEDIUM
99 * @tc.type: FUNC
100 * @tc.level Level 1
101 */
102 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_002, testing::ext::TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_002";
105
106 string src = g_srcPath.string() + "/test02";
107 string dest = g_destPath.string();
108 filesystem::create_directories(src);
109
110 int invalidMode = COPYMODE_MAX + 1;
111 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>(invalidMode));
112
113 EXPECT_FALSE(result.fsResult.IsSuccess());
114 EXPECT_FALSE(result.errFiles.has_value());
115
116 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_002";
117 }
118
119 /**
120 * @tc.name: CopyDirCoreTest_DoCopyDir_003
121 * @tc.desc: Test function of DoCopyDir() interface for FAILED with non-existent source.
122 * @tc.size: MEDIUM
123 * @tc.type: FUNC
124 * @tc.level Level 1
125 */
126 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_003, testing::ext::TestSize.Level1)
127 {
128 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_003";
129
130 string src = g_srcPath.string() + "/non_existent";
131 string dest = g_destPath.string();
132
133 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
134
135 EXPECT_FALSE(result.fsResult.IsSuccess());
136 EXPECT_FALSE(result.errFiles.has_value());
137
138 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_003";
139 }
140
141 /**
142 * @tc.name: CopyDirCoreTest_DoCopyDir_004
143 * @tc.desc: Test function of DoCopyDir() interface for FAILED with invalid destination.
144 * @tc.size: MEDIUM
145 * @tc.type: FUNC
146 * @tc.level Level 1
147 */
148 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_004, testing::ext::TestSize.Level1)
149 {
150 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_004";
151
152 string src = g_srcPath.string();
153 string dest = g_destPath.string() + "/invalid_file.txt";
154 filesystem::path(dest).remove_filename();
155 filesystem::create_directories(filesystem::path(dest).parent_path());
156 ofstream(dest).close(); // 创建文件而非目录
157
158 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
159
160 EXPECT_FALSE(result.fsResult.IsSuccess());
161 EXPECT_FALSE(result.errFiles.has_value());
162
163 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_004";
164 }
165
166 /**
167 * @tc.name: CopyDirCoreTest_DoCopyDir_005
168 * @tc.desc: Test function of DoCopyDir() interface for FAILED with same source and destination.
169 * @tc.size: MEDIUM
170 * @tc.type: FUNC
171 * @tc.level Level 1
172 */
173 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_005, testing::ext::TestSize.Level1)
174 {
175 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_005";
176
177 string src = g_srcPath.string();
178 string dest = g_srcPath.string();
179
180 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
181
182 EXPECT_FALSE(result.fsResult.IsSuccess());
183 EXPECT_FALSE(result.errFiles.has_value());
184
185 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_005";
186 }
187
188 /**
189 * @tc.name: CopyDirCoreTest_DoCopyDir_006
190 * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with files.
191 * @tc.size: MEDIUM
192 * @tc.type: FUNC
193 * @tc.level Level 1
194 */
195 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_006, testing::ext::TestSize.Level1)
196 {
197 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_006";
198
199 string src = g_srcPath.string() + "/test06";
200 string dest = g_destPath.string();
201 filesystem::create_directories(src);
202 CreateTestFile(src + "/file1.txt", "content1");
203 CreateTestFile(src + "/file2.txt", "content2");
204
205 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
206
207 EXPECT_TRUE(result.fsResult.IsSuccess());
208 EXPECT_FALSE(result.errFiles.has_value());
209
210 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_006";
211 }
212
213 /**
214 * @tc.name: CopyDirCoreTest_DoCopyDir_007
215 * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with subdirectories.
216 * @tc.size: MEDIUM
217 * @tc.type: FUNC
218 * @tc.level Level 1
219 */
220 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_007, testing::ext::TestSize.Level1)
221 {
222 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_007";
223
224 string src = g_srcPath.string() + "/test07";
225 string dest = g_destPath.string();
226 filesystem::create_directories(src + "/subdir1");
227 filesystem::create_directories(src + "/subdir2");
228 CreateTestFile(src + "/subdir1/file1.txt", "sub1_content1");
229 CreateTestFile(src + "/subdir2/file2.txt", "sub2_content2");
230
231 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>());
232
233 EXPECT_TRUE(result.fsResult.IsSuccess());
234 EXPECT_FALSE(result.errFiles.has_value());
235
236 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_007";
237 }
238
239 /**
240 * @tc.name: CopyDirCoreTest_DoCopyDir_008
241 * @tc.desc: Test function of DoCopyDir() interface for FAILED with existing files (throw error mode).
242 * @tc.size: MEDIUM
243 * @tc.type: FUNC
244 * @tc.level Level 1
245 */
246 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_008, testing::ext::TestSize.Level1)
247 {
248 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_008";
249
250 string src = g_srcPath.string() + "/test08";
251 string dest = g_destPath.string();
252 filesystem::create_directories(src);
253 CreateTestFile(src + "/file1.txt", "content1");
254
255 string destDir = dest + "/" + filesystem::path(src).filename().string();
256 filesystem::create_directories(destDir);
257 CreateTestFile(destDir + "/file1.txt", "existing_content");
258
259 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>(DIRMODE_FILE_COPY_THROW_ERR));
260
261 EXPECT_FALSE(result.fsResult.IsSuccess());
262 EXPECT_TRUE(result.errFiles.has_value());
263
264 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_008";
265 }
266
267 /**
268 * @tc.name: CopyDirCoreTest_DoCopyDir_009
269 * @tc.desc: Test function of DoCopyDir() interface for SUCCESS with existing files (overwrite mode).
270 * @tc.size: MEDIUM
271 * @tc.type: FUNC
272 * @tc.level Level 1
273 */
274 HWTEST_F(CopyDirCoreTest, CopyDirCoreTest_DoCopyDir_009, testing::ext::TestSize.Level1)
275 {
276 GTEST_LOG_(INFO) << "CopyDirCoreTest-begin CopyDirCoreTest_DoCopyDir_009";
277
278 string src = g_srcPath.string() + "/test09";
279 string dest = g_destPath.string();
280 filesystem::create_directories(src);
281 CreateTestFile(src + "/file1.txt", "content1");
282
283 string destDir = dest + "/" + filesystem::path(src).filename().string();
284 filesystem::create_directories(destDir);
285 CreateTestFile(destDir + "/file1.txt", "existing_content");
286
287 auto result = CopyDirCore::DoCopyDir(src, dest, optional<int32_t>(DIRMODE_FILE_COPY_REPLACE));
288
289 EXPECT_TRUE(result.fsResult.IsSuccess());
290 EXPECT_FALSE(result.errFiles.has_value());
291
292 GTEST_LOG_(INFO) << "CopyDirCoreTest-end CopyDirCoreTest_DoCopyDir_009";
293 }
294
295 } // namespace OHOS::FileManagement::ModuleFileIO::Test