1 /*
2 * Copyright (c) 2021 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 "directory_ex.h"
17 #include <gtest/gtest.h>
18 #include <fcntl.h>
19 #include <algorithm>
20 #include <iostream>
21 #include <fstream>
22
23 using namespace testing::ext;
24 using namespace std;
25
26 namespace OHOS {
27 namespace {
28 class UtilsDirectoryTest : public testing::Test {
29 public :
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void UtilsDirectoryTest::SetUpTestCase(void)
37 {
38 }
39
TearDownTestCase(void)40 void UtilsDirectoryTest::TearDownTestCase(void)
41 {
42 }
43
SetUp(void)44 void UtilsDirectoryTest::SetUp(void)
45 {
46 }
47
TearDown(void)48 void UtilsDirectoryTest::TearDown(void)
49 {
50 }
51
52 /*
53 * @tc.name: testGetCurrentProcFullFileName001
54 * @tc.desc: get the directory of directorytest
55 */
56 HWTEST_F(UtilsDirectoryTest, testGetCurrentProcFullFileName001, TestSize.Level0)
57 {
58 string strBaseName = "/data/test/UtilsDirectoryTest";
59 string strFilename = GetCurrentProcFullFileName();
60 EXPECT_EQ(strFilename, strBaseName);
61 }
62
63 /*
64 * @tc.name: testGetCurrentProcPath001
65 * @tc.desc: get the path of directorytest
66 */
67 HWTEST_F(UtilsDirectoryTest, testGetCurrentProcPath001, TestSize.Level0)
68 {
69 string strPathName = "/data/test/";
70 string strCurPathName = GetCurrentProcPath();
71 EXPECT_EQ(strCurPathName, strPathName);
72 }
73
74 /*
75 * @tc.name: testExtractFilePath001
76 * @tc.desc: get the filename of the path
77 */
78 HWTEST_F(UtilsDirectoryTest, testExtractFilePath001, TestSize.Level0)
79 {
80 string strFilePath = "/data/test/";
81 string strPath = ExtractFilePath(GetCurrentProcFullFileName());
82 EXPECT_EQ(strFilePath, strPath);
83 }
84
85 /*
86 * @tc.name: testExtractFileName001
87 * @tc.desc: get the filename of the path
88 */
89 HWTEST_F(UtilsDirectoryTest, testExtractFileName001, TestSize.Level0)
90 {
91 string strBaseName = "UtilsDirectoryTest";
92 string strName = ExtractFileName(GetCurrentProcFullFileName());
93 EXPECT_EQ(strBaseName, strName);
94 }
95
96 /*
97 * @tc.name: testExtractFileExt001
98 * @tc.desc: get the filename of the path
99 */
100 HWTEST_F(UtilsDirectoryTest, testExtractFileExt001, TestSize.Level0)
101 {
102 string strBaseName = "test/test.txt";
103 string strTypeName = ExtractFileExt(strBaseName);
104 EXPECT_EQ(strTypeName, "txt");
105 }
106
107 /*
108 * @tc.name: testExtractFileExt002
109 * @tc.desc: get the filename of the path and test whether the filename contains "."
110 */
111 HWTEST_F(UtilsDirectoryTest, testExtractFileExt002, TestSize.Level0)
112 {
113 string strBaseName = "test/test_txt";
114 string strTypeName = ExtractFileExt(strBaseName);
115 EXPECT_EQ(strTypeName, "");
116 }
117
118 /*
119 * @tc.name: testExcludeTrailingPathDelimiter001
120 * @tc.desc: directory unit test
121 */
122 HWTEST_F(UtilsDirectoryTest, testExcludeTrailingPathDelimiter001, TestSize.Level0)
123 {
124 string strResult = "data/test/UtilsDirectoryTest";
125 string strName = ExcludeTrailingPathDelimiter("data/test/UtilsDirectoryTest/");
126 EXPECT_EQ(strResult, strName);
127 }
128
129 /*
130 * @tc.name: testIncludeTrailingPathDelimiter001
131 * @tc.desc: directory unit test
132 */
133 HWTEST_F(UtilsDirectoryTest, testIncludeTrailingPathDelimiter001, TestSize.Level0)
134 {
135 string strResult = "data/test/UtilsDirectoryTest/";
136 string strName = IncludeTrailingPathDelimiter("data/test/UtilsDirectoryTest");
137 EXPECT_EQ(strResult, strName);
138 }
139
140 /*
141 * @tc.name: testGetDirFiles001
142 * @tc.desc: directory unit test
143 */
144 HWTEST_F(UtilsDirectoryTest, testGetDirFiles001, TestSize.Level0)
145 {
146 string resultfile[2] = { "/data/test/TestFile.txt", "/data/test/UtilsDirectoryTest" };
147 // prepare test data
148 ofstream file(resultfile[0], fstream::out);
149
150 string dirpath = "/data/";
151 vector<string> filenames;
152 GetDirFiles(dirpath, filenames);
153 auto pos = find(filenames.begin(), filenames.end(), resultfile[0]);
154 EXPECT_NE(pos, filenames.end());
155
156 pos = find(filenames.begin(), filenames.end(), resultfile[1]);
157 EXPECT_NE(pos, filenames.end());
158
159 // delete test data
160 RemoveFile(resultfile[0]);
161 }
162
163 /*
164 * @tc.name: testForceCreateDirectory001
165 * @tc.desc: directory unit test
166 */
167 HWTEST_F(UtilsDirectoryTest, testForceCreateDirectory001, TestSize.Level0)
168 {
169 string dirpath = "/data/test_dir/test2/test3";
170 bool ret = ForceCreateDirectory(dirpath);
171 EXPECT_EQ(ret, true);
172 ret = IsEmptyFolder(dirpath);
173 EXPECT_EQ(ret, true);
174 }
175
176 /*
177 * @tc.name: testForceRemoveDirectory001
178 * @tc.desc: directory unit test
179 */
180 HWTEST_F(UtilsDirectoryTest, testForceRemoveDirectory001, TestSize.Level0)
181 {
182 string dirpath = "/data/test_dir";
183 bool ret = ForceRemoveDirectory(dirpath);
184 EXPECT_EQ(ret, true);
185 }
186
187 /*
188 * @tc.name: testForceRemoveDirectory002
189 * @tc.desc: test whether the folder exists
190 */
191 HWTEST_F(UtilsDirectoryTest, testForceRemoveDirectory002, TestSize.Level0)
192 {
193 string dirpath = "/data/test/utils_directory_tmp/";
194 bool ret = ForceRemoveDirectory(dirpath);
195 EXPECT_EQ(ret, false);
196 }
197
198
199 /*
200 * @tc.name: testForceRemoveDirectory003
201 * @tc.desc: test whether it works when the full path is over than 255.
202 */
203 HWTEST_F(UtilsDirectoryTest, testForceRemoveDirectory003, TestSize.Level0)
204 {
205 string dirpath = "/data/test/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/"
206 "tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/"
207 "tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/"
208 "tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp/tmp";
209 bool ret = ForceCreateDirectory(dirpath);
210 EXPECT_EQ(ret, true);
211 string rootpath = "/data/test/tmp";
212 ret = ForceRemoveDirectory(rootpath);
213 EXPECT_EQ(ret, true);
214 }
215
216 /*
217 * @tc.name: testRemoveFile001
218 * @tc.desc: directory unit test
219 */
220 HWTEST_F(UtilsDirectoryTest, testRemoveFile001, TestSize.Level0)
221 {
222 string dirpath = "/data/test_dir";
223 bool ret = ForceCreateDirectory(dirpath);
224 EXPECT_EQ(ret, true);
225 string filename = dirpath + "/test.txt";
226 FILE *fp = fopen(filename.c_str(), "w");
227 if (NULL != fp) {
228 fclose(fp);
229 ret = RemoveFile(filename);
230 EXPECT_EQ(ret, true);
231 }
232 ret = ForceRemoveDirectory(dirpath);
233 EXPECT_EQ(ret, true);
234 }
235
236 /*
237 * @tc.name: testRemoveFile002
238 * @tc.desc: Remove soft link file.
239 */
240 HWTEST_F(UtilsDirectoryTest, testRemoveFile002, TestSize.Level0)
241 {
242 string dirpath = "/data/test_dir";
243 bool ret = ForceCreateDirectory(dirpath);
244 EXPECT_EQ(ret, true);
245
246 string targetname = "/data/test_target.txt";
247 FILE *fp = fopen(targetname.c_str(), "w");
248 if (NULL != fp) {
249 fclose(fp);
250 }
251
252 // symlink to a directory
253 string linkpath = "/data/test_symlink_dir";
254 int res = symlink(dirpath.c_str(), linkpath.c_str());
255 EXPECT_EQ(res, 0);
256
257 ret = ForceRemoveDirectory(linkpath);
258 EXPECT_EQ(ret, true);
259
260 // Target dir is not removed.
261 ret = faccessat(AT_FDCWD, dirpath.c_str(), F_OK, AT_SYMLINK_NOFOLLOW);
262 EXPECT_EQ(ret, 0);
263
264 // symlink to a file
265 string filename = dirpath + "/test.txt";
266 res = symlink(targetname.c_str(), filename.c_str());
267 EXPECT_EQ(res, 0);
268
269 ret = ForceRemoveDirectory(dirpath);
270 EXPECT_EQ(ret, true);
271
272 // Target file is not removed.
273 ret = faccessat(AT_FDCWD, targetname.c_str(), F_OK, AT_SYMLINK_NOFOLLOW);
274 EXPECT_EQ(ret, 0);
275
276 ret = RemoveFile(targetname);
277 EXPECT_EQ(ret, true);
278 }
279
280 /*
281 * @tc.name: testRemoveFile003
282 * @tc.desc: Remove dangling soft link file.
283 */
284 HWTEST_F(UtilsDirectoryTest, testRemoveFile003, TestSize.Level0)
285 {
286 string dirpath = "/data/test_dir";
287 bool ret = ForceCreateDirectory(dirpath);
288 EXPECT_EQ(ret, true);
289
290 // symlink to a file
291 string targetname = "/data/nonexisted.txt";
292 string filename = dirpath + "/test.txt";
293 int res = symlink(targetname.c_str(), filename.c_str());
294 EXPECT_EQ(res, 0);
295
296 ret = ForceRemoveDirectory(dirpath);
297 EXPECT_EQ(ret, true);
298
299 ret = RemoveFile(targetname);
300 EXPECT_EQ(ret, true);
301 }
302
303 /*
304 * @tc.name: testGetFolderSize001
305 * @tc.desc: directory unit test
306 */
307 HWTEST_F(UtilsDirectoryTest, testGetFolderSize001, TestSize.Level0)
308 {
309 string dirpath = "/data/test_folder/";
310 bool ret = ForceCreateDirectory(dirpath);
311 EXPECT_EQ(ret, true);
312 ofstream out(dirpath + "test.txt");
313 if (out.is_open()) {
314 out << "This is a line.\n";
315 out << "This is another line.\n";
316 out.close();
317 }
318 uint64_t resultsize = GetFolderSize(dirpath);
319 uint64_t resultcomp = 38;
320 EXPECT_EQ(resultsize, resultcomp);
321
322 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
323 ret = ChangeModeFile(dirpath + "test.txt", mode);
324 EXPECT_EQ(ret, true);
325
326 mode = S_IRUSR | S_IRGRP | S_IROTH;
327 ret = ChangeModeDirectory(dirpath, mode);
328 EXPECT_EQ(ret, true);
329
330 ret = ForceRemoveDirectory(dirpath);
331 EXPECT_EQ(ret, true);
332 }
333
334 /*
335 * @tc.name: testChangeModeFile001
336 * @tc.desc: test whether the folder exists
337 */
338 HWTEST_F(UtilsDirectoryTest, testChangeModeFile001, TestSize.Level0)
339 {
340 string dirpath = "/data/test/utils_directory_tmp/";
341 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
342 bool ret = ChangeModeFile(dirpath + "test.txt", mode);
343 EXPECT_EQ(ret, false);
344 }
345
346 /*
347 * @tc.name: testChangeModeDirectory001
348 * @tc.desc: test whether the folder is empty and get the size of the folder
349 */
350 HWTEST_F(UtilsDirectoryTest, testChangeModeDirectory001, TestSize.Level0)
351 {
352 string dirpath = "";
353 mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
354 bool ret = ChangeModeDirectory(dirpath, mode);
355 EXPECT_EQ(ret, false);
356
357 uint64_t resultsize = GetFolderSize(dirpath);
358 uint64_t resultcomp = 0;
359 EXPECT_EQ(resultsize, resultcomp);
360 }
361
362 /*
363 * @tc.name: testPathToRealPath001
364 * @tc.desc: directory unit test
365 */
366 HWTEST_F(UtilsDirectoryTest, testPathToRealPath001, TestSize.Level0)
367 {
368 string path = "/data/test";
369 string realpath;
370 bool ret = PathToRealPath(path, realpath);
371 EXPECT_EQ(ret, true);
372 EXPECT_EQ(path, realpath);
373 }
374
375 /*
376 * @tc.name: testPathToRealPath002
377 * @tc.desc: directory unit test
378 */
379 HWTEST_F(UtilsDirectoryTest, testPathToRealPath002, TestSize.Level0)
380 {
381 string path = "/data/../data/test";
382 string realpath;
383 bool ret = PathToRealPath(path, realpath);
384 EXPECT_EQ(ret, true);
385 EXPECT_EQ("/data/test", realpath);
386 }
387
388 /*
389 * @tc.name: testPathToRealPath003
390 * @tc.desc: directory unit test
391 */
392 HWTEST_F(UtilsDirectoryTest, testPathToRealPath003, TestSize.Level0)
393 {
394 string path = "./";
395 string realpath;
396 bool ret = PathToRealPath(path, realpath);
397 EXPECT_EQ(ret, true);
398 EXPECT_EQ("/data/test", realpath);
399 }
400
401 /*
402 * @tc.name: testPathToRealPath004
403 * @tc.desc: directory unit test
404 */
405 HWTEST_F(UtilsDirectoryTest, testPathToRealPath004, TestSize.Level0)
406 {
407 string path = "";
408 string realpath;
409 bool ret = PathToRealPath(path, realpath);
410 EXPECT_EQ(ret, false);
411 }
412
413 /*
414 * @tc.name: testPathToRealPath005
415 * @tc.desc: directory unit test
416 */
417 HWTEST_F(UtilsDirectoryTest, testPathToRealPath005, TestSize.Level0)
418 {
419 string path = "/data/test/data/test/data/test/data/test/data/test/data/ \
420 test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
421 test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
422 test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
423 test/data/test/data/test/data/test";
424 string realpath;
425 bool ret = PathToRealPath(path, realpath);
426 EXPECT_EQ(ret, false);
427 }
428
429 /*
430 * @tc.name: testPathToRealPath006
431 * @tc.desc: test whether the folder exists
432 */
433 HWTEST_F(UtilsDirectoryTest, testPathToRealPath006, TestSize.Level0)
434 {
435 string path(PATH_MAX, 'x');
436 string realpath;
437 bool ret = PathToRealPath(path, realpath);
438 EXPECT_EQ(ret, false);
439 }
440 } // namespace
441 } // namespace OHOS
442