• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @tc.name: testRemoveFile001
200  * @tc.desc: directory unit test
201  */
202 HWTEST_F(UtilsDirectoryTest, testRemoveFile001, TestSize.Level0)
203 {
204     string dirpath = "/data/test_dir";
205     bool ret = ForceCreateDirectory(dirpath);
206     EXPECT_EQ(ret, true);
207     string filename = dirpath + "/test.txt";
208     FILE *fp = fopen(filename.c_str(), "w");
209     if (NULL != fp) {
210         fclose(fp);
211         ret = RemoveFile(filename);
212         EXPECT_EQ(ret, true);
213     }
214     ret = ForceRemoveDirectory(dirpath);
215     EXPECT_EQ(ret, true);
216 }
217 
218 /*
219  * @tc.name: testRemoveFile002
220  * @tc.desc: Remove soft link file.
221  */
222 HWTEST_F(UtilsDirectoryTest, testRemoveFile002, TestSize.Level0)
223 {
224     string dirpath = "/data/test_dir";
225     bool ret = ForceCreateDirectory(dirpath);
226     EXPECT_EQ(ret, true);
227 
228     string targetname = "/data/test_target.txt";
229     FILE *fp = fopen(targetname.c_str(), "w");
230     if (NULL != fp) {
231         fclose(fp);
232     }
233 
234     // symlink to a directory
235     string linkpath = "/data/test_symlink_dir";
236     int res = symlink(dirpath.c_str(), linkpath.c_str());
237     EXPECT_EQ(res, 0);
238 
239     ret = ForceRemoveDirectory(linkpath);
240     EXPECT_EQ(ret, true);
241 
242     // Target dir is not removed.
243     ret = faccessat(AT_FDCWD, dirpath.c_str(), F_OK, AT_SYMLINK_NOFOLLOW);
244     EXPECT_EQ(ret, 0);
245 
246     // symlink to a file
247     string filename = dirpath + "/test.txt";
248     res = symlink(targetname.c_str(), filename.c_str());
249     EXPECT_EQ(res, 0);
250 
251     ret = ForceRemoveDirectory(dirpath);
252     EXPECT_EQ(ret, true);
253 
254     // Target file is not removed.
255     ret = faccessat(AT_FDCWD, targetname.c_str(), F_OK, AT_SYMLINK_NOFOLLOW);
256     EXPECT_EQ(ret, 0);
257 
258     ret = RemoveFile(targetname);
259     EXPECT_EQ(ret, true);
260 }
261 
262 /*
263  * @tc.name: testRemoveFile003
264  * @tc.desc: Remove dangling soft link file.
265  */
266 HWTEST_F(UtilsDirectoryTest, testRemoveFile003, TestSize.Level0)
267 {
268     string dirpath = "/data/test_dir";
269     bool ret = ForceCreateDirectory(dirpath);
270     EXPECT_EQ(ret, true);
271 
272     // symlink to a file
273     string targetname = "/data/nonexisted.txt";
274     string filename = dirpath + "/test.txt";
275     int res = symlink(targetname.c_str(), filename.c_str());
276     EXPECT_EQ(res, 0);
277 
278     ret = ForceRemoveDirectory(dirpath);
279     EXPECT_EQ(ret, true);
280 
281     ret = RemoveFile(targetname);
282     EXPECT_EQ(ret, true);
283 }
284 
285 /*
286  * @tc.name: testGetFolderSize001
287  * @tc.desc: directory unit test
288  */
289 HWTEST_F(UtilsDirectoryTest, testGetFolderSize001, TestSize.Level0)
290 {
291     string dirpath = "/data/test_folder/";
292     bool ret = ForceCreateDirectory(dirpath);
293     EXPECT_EQ(ret, true);
294     ofstream out(dirpath + "test.txt");
295     if (out.is_open()) {
296         out << "This is a line.\n";
297         out << "This is another line.\n";
298         out.close();
299     }
300     uint64_t resultsize = GetFolderSize(dirpath);
301     uint64_t resultcomp = 38;
302     EXPECT_EQ(resultsize, resultcomp);
303 
304     mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
305     ret = ChangeModeFile(dirpath + "test.txt", mode);
306     EXPECT_EQ(ret, true);
307 
308     mode = S_IRUSR  | S_IRGRP | S_IROTH;
309     ret = ChangeModeDirectory(dirpath, mode);
310     EXPECT_EQ(ret, true);
311 
312     ret = ForceRemoveDirectory(dirpath);
313     EXPECT_EQ(ret, true);
314 }
315 
316 /*
317  * @tc.name: testChangeModeFile001
318  * @tc.desc: test whether the folder exists
319  */
320 HWTEST_F(UtilsDirectoryTest, testChangeModeFile001, TestSize.Level0)
321 {
322     string dirpath = "/data/test/utils_directory_tmp/";
323     mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
324     bool ret = ChangeModeFile(dirpath + "test.txt", mode);
325     EXPECT_EQ(ret, false);
326 }
327 
328 /*
329  * @tc.name: testChangeModeDirectory001
330  * @tc.desc: test whether the folder is empty and get the size of the folder
331  */
332 HWTEST_F(UtilsDirectoryTest, testChangeModeDirectory001, TestSize.Level0)
333 {
334     string dirpath = "";
335     mode_t mode = S_IRUSR  | S_IRGRP | S_IROTH;
336     bool ret = ChangeModeDirectory(dirpath, mode);
337     EXPECT_EQ(ret, false);
338 
339     uint64_t resultsize = GetFolderSize(dirpath);
340     uint64_t resultcomp = 0;
341     EXPECT_EQ(resultsize, resultcomp);
342 }
343 
344 /*
345  * @tc.name: testPathToRealPath001
346  * @tc.desc: directory unit test
347  */
348 HWTEST_F(UtilsDirectoryTest, testPathToRealPath001, TestSize.Level0)
349 {
350     string path = "/data/test";
351     string realpath;
352     bool ret = PathToRealPath(path, realpath);
353     EXPECT_EQ(ret, true);
354     EXPECT_EQ(path, realpath);
355 }
356 
357 /*
358  * @tc.name: testPathToRealPath002
359  * @tc.desc: directory unit test
360  */
361 HWTEST_F(UtilsDirectoryTest, testPathToRealPath002, TestSize.Level0)
362 {
363     string path = "/data/../data/test";
364     string realpath;
365     bool ret = PathToRealPath(path, realpath);
366     EXPECT_EQ(ret, true);
367     EXPECT_EQ("/data/test", realpath);
368 }
369 
370 /*
371  * @tc.name: testPathToRealPath003
372  * @tc.desc: directory unit test
373  */
374 HWTEST_F(UtilsDirectoryTest, testPathToRealPath003, TestSize.Level0)
375 {
376     string path = "./";
377     string realpath;
378     bool ret = PathToRealPath(path, realpath);
379     EXPECT_EQ(ret, true);
380     EXPECT_EQ("/data/test", realpath);
381 }
382 
383 /*
384  * @tc.name: testPathToRealPath004
385  * @tc.desc: directory unit test
386  */
387 HWTEST_F(UtilsDirectoryTest, testPathToRealPath004, TestSize.Level0)
388 {
389     string path = "";
390     string realpath;
391     bool ret = PathToRealPath(path, realpath);
392     EXPECT_EQ(ret, false);
393 }
394 
395 /*
396  * @tc.name: testPathToRealPath005
397  * @tc.desc: directory unit test
398  */
399 HWTEST_F(UtilsDirectoryTest, testPathToRealPath005, TestSize.Level0)
400 {
401     string path = "/data/test/data/test/data/test/data/test/data/test/data/ \
402         test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
403         test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
404         test/data/test/data/test/data/test/data/test/data/test/data/test/data/ \
405         test/data/test/data/test/data/test";
406     string realpath;
407     bool ret = PathToRealPath(path, realpath);
408     EXPECT_EQ(ret, false);
409 }
410 
411 /*
412  * @tc.name: testPathToRealPath006
413  * @tc.desc: test whether the folder exists
414  */
415 HWTEST_F(UtilsDirectoryTest, testPathToRealPath006, TestSize.Level0)
416 {
417     string path(PATH_MAX, 'x');
418     string realpath;
419     bool ret = PathToRealPath(path, realpath);
420     EXPECT_EQ(ret, false);
421 }
422 }  // namespace
423 }  // namespace OHOS