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