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