• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "utils_directory.h"
17 
18 #include <fcntl.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <sys/types.h>
22 #include <system_error>
23 #include <unistd.h>
24 
25 #include "directory_ex.h"
26 
27 namespace OHOS {
28 namespace Storage {
29 namespace DistributedFile {
30 namespace Utils {
31 using namespace testing;
32 using namespace testing::ext;
33 using namespace std;
34 
35 class UtilsDirectoryTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void UtilsDirectoryTest::SetUpTestCase(void)
44 {
45     GTEST_LOG_(INFO) << "SetUpTestCase";
46 }
47 
TearDownTestCase(void)48 void UtilsDirectoryTest::TearDownTestCase(void)
49 {
50     GTEST_LOG_(INFO) << "TearDownTestCase";
51 }
52 
SetUp(void)53 void UtilsDirectoryTest::SetUp(void)
54 {
55     GTEST_LOG_(INFO) << "SetUp";
56 }
57 
TearDown(void)58 void UtilsDirectoryTest::TearDown(void)
59 {
60     GTEST_LOG_(INFO) << "TearDown";
61 }
62 
63 /**
64  * @tc.name: ForceCreateDirectoryTest001
65  * @tc.desc: Verify the ForceCreateDirectory function
66  * @tc.type: FUNC
67  * @tc.require: I6H5MH
68  */
69 HWTEST_F(UtilsDirectoryTest, ForceCreateDirectoryTest001, TestSize.Level1)
70 {
71     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest001 Start";
72     try {
73         string path = "/data/tdd";
74         ForceCreateDirectory(path);
75     } catch (...) {
76         EXPECT_FALSE(false);
77         GTEST_LOG_(INFO) << " ForceCreateDirectoryTest001 ERROR";
78     }
79     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest001 End";
80 }
81 
82 /**
83  * @tc.name: ForceCreateDirectoryTest002
84  * @tc.desc: Verify the ForceCreateDirectoryTest002 function
85  * @tc.type: FUNC
86  * @tc.require: I6H5MH
87  */
88 HWTEST_F(UtilsDirectoryTest, ForceCreateDirectoryTest002, TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest002 Start";
91     try {
92         string path = "/data/tdd";
93         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO); // 00777
94         ForceCreateDirectory(path, mode);
95     } catch (...) {
96         EXPECT_TRUE(false);
97         GTEST_LOG_(INFO) << " ForceCreateDirectoryTest002 ERROR";
98     }
99     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest002 End";
100 }
101 
102 /**
103  * @tc.name: ForceCreateDirectoryTest003
104  * @tc.desc: Verify the ForceCreateDirectoryTest003 function
105  * @tc.type: FUNC
106  * @tc.require: I6H5MH
107  */
108 HWTEST_F(UtilsDirectoryTest, ForceCreateDirectoryTest003, TestSize.Level1)
109 {
110     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest003 Start";
111     try {
112         string path = "/data/tdd";
113         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO);
114         uid_t uid = UID_ROOT;
115         gid_t gid = 0; // root
116         ForceCreateDirectory(path, mode, uid, gid);
117     } catch (...) {
118         EXPECT_TRUE(false);
119         GTEST_LOG_(INFO) << " ForceCreateDirectoryTest003 ERROR";
120     }
121     GTEST_LOG_(INFO) << "ForceCreateDirectoryTest003 End";
122 }
123 
124 /**
125  * @tc.name: ForceRemoveDirectoryTest001
126  * @tc.desc: Verify the ForceRemoveDirectory function
127  * @tc.type: FUNC
128  * @tc.require: I6H5MH
129  */
130 HWTEST_F(UtilsDirectoryTest, ForceRemoveDirectoryTest001, TestSize.Level1)
131 {
132     GTEST_LOG_(INFO) << "ForceRemoveDirectory001 Start";
133     try {
134         string path = "/data/tdd";
135         ForceRemoveDirectory(path);
136     } catch (...) {
137         EXPECT_TRUE(false);
138         GTEST_LOG_(INFO) << " ForceRemoveDirectoryTest001 ERROR";
139     }
140     GTEST_LOG_(INFO) << "ForceRemoveDirectoryTest001 End";
141 }
142 
143 /**
144  * @tc.name: ForceRemoveDirectoryDeepFirstTest001Test001
145  * @tc.desc: Verify the ForceRemoveDirectoryDeepFirst function
146  * @tc.type: FUNC
147  * @tc.require: I6H5MH
148  */
149 HWTEST_F(UtilsDirectoryTest, ForceRemoveDirectoryDeepFirstTest001Test001, TestSize.Level1)
150 {
151     GTEST_LOG_(INFO) << "ForceRemoveDirectoryDeepFirstTest001Test001 Start";
152     try {
153         string path = "/data/tdd";
154         ForceRemoveDirectory(path);
155         EXPECT_FALSE(ForceRemoveDirectoryDeepFirst(path));
156 
157         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO);
158         uid_t uid = UID_ROOT;
159         gid_t gid = 0; // root
160         ForceCreateDirectory(path, mode, uid, gid);
161 
162         string subPath = path + "/sub";
163         ForceCreateDirectory(path, mode, uid, gid);
164 
165         string fileStr = path + "/test.txt";
166         int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
167         ASSERT_TRUE(fd != -1) << "ForceIsFolderTest001 Create File Failed!";
168         close(fd);
169 
170         EXPECT_TRUE(ForceRemoveDirectoryDeepFirst(path));
171         EXPECT_NE(access(fileStr.c_str(), F_OK), 0);
172         EXPECT_NE(access(subPath.c_str(), F_OK), 0);
173     } catch (...) {
174         EXPECT_TRUE(false);
175         GTEST_LOG_(INFO) << " ForceRemoveDirectoryDeepFirstTest001Test001 ERROR";
176     }
177     GTEST_LOG_(INFO) << "ForceRemoveDirectoryDeepFirstTest001Test001 End";
178 }
179 
180 /**
181  * @tc.name: IsFolderTest001
182  * @tc.desc: Verify the IsFolder & IsFile function
183  * @tc.type: FUNC
184  * @tc.require: I6H5MH
185  */
186 HWTEST_F(UtilsDirectoryTest, IsFolderTest001, TestSize.Level1)
187 {
188     GTEST_LOG_(INFO) << "IsFolderTest001 Start";
189     try {
190         string path;
191         EXPECT_FALSE(IsFolder(path));
192 
193         path = "/data/tdd";
194         ForceRemoveDirectory(path);
195         EXPECT_FALSE(IsFolder(path));
196 
197         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO);
198         uid_t uid = UID_ROOT;
199         gid_t gid = 0; // root
200         ForceCreateDirectory(path, mode, uid, gid);
201         EXPECT_TRUE(IsFolder(path));
202 
203         string fileStr = path + "/test.txt";
204         int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
205         ASSERT_TRUE(fd != -1) << "IsFolderTest001 Create File Failed!";
206         close(fd);
207         EXPECT_TRUE(IsFile(fileStr));
208 
209         auto ret = unlink(fileStr.c_str());
210         ASSERT_TRUE(ret != -1) << "Failed to delete file in IsFolderTest001! " << errno;
211         ForceRemoveDirectory(path);
212     } catch (...) {
213         EXPECT_TRUE(false);
214         GTEST_LOG_(INFO) << " IsFolderTest001 ERROR";
215     }
216     GTEST_LOG_(INFO) << "IsFolderTest001 End";
217 }
218 
219 /**
220  * @tc.name: GetFilePathTest001
221  * @tc.desc: Verify the GetFilePath function
222  * @tc.type: FUNC
223  * @tc.require: I6H5MH
224  */
225 HWTEST_F(UtilsDirectoryTest, GetFilePathTest001, TestSize.Level1)
226 {
227     GTEST_LOG_(INFO) << "GetFilePathTest001 Start";
228     try {
229         string path = "/data/tdd";
230         ForceRemoveDirectory(path);
231 
232         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO);
233         uid_t uid = UID_ROOT;
234         gid_t gid = 0; // root
235         ForceCreateDirectory(path, mode, uid, gid);
236 
237         string subPath = path + "/sub";
238         ForceCreateDirectory(subPath, mode, uid, gid);
239 
240         string fileStr1 = path + "/test.txt";
241         string fileStr2 = subPath + "/1.txt";
242         string fileStr3 = subPath + "/2.txt";
243         int32_t fd = open(fileStr1.c_str(), O_RDWR | O_CREAT);
244         ASSERT_TRUE(fd != -1) << "GetFilePathTest001 Create fileStr1 Failed!";
245         close(fd);
246         fd = open(fileStr2.c_str(), O_RDWR | O_CREAT);
247         ASSERT_TRUE(fd != -1) << "GetFilePathTest001 Create fileStr2 Failed!";
248         close(fd);
249         fd = open(fileStr3.c_str(), O_RDWR | O_CREAT);
250         ASSERT_TRUE(fd != -1) << "GetFilePathTest001 Create fileStr3 Failed!";
251         close(fd);
252 
253         auto rlt = GetFilePath(fileStr1);
254         std::vector<std::string> ept;
255         ept.emplace_back(fileStr1);
256         EXPECT_EQ(rlt, ept);
257 
258         rlt.clear();
259         rlt = GetFilePath(path);
260         EXPECT_EQ(rlt.size(), 3);
261 
262         EXPECT_NE(find(rlt.begin(), rlt.end(), fileStr1), rlt.end());
263         EXPECT_NE(find(rlt.begin(), rlt.end(), fileStr2), rlt.end());
264         EXPECT_NE(find(rlt.begin(), rlt.end(), fileStr3), rlt.end());
265         EXPECT_TRUE(ForceRemoveDirectoryDeepFirst(path));
266     } catch (...) {
267         EXPECT_TRUE(false);
268         GTEST_LOG_(INFO) << " GetFilePathTest001 ERROR";
269     }
270     GTEST_LOG_(INFO) << "GetFilePathTest001 End";
271 }
272 
273 /**
274  * @tc.name: ChangeOwnerRecursiveTest001
275  * @tc.desc: Verify the ChangeOwnerRecursive function
276  * @tc.type: FUNC
277  * @tc.require: I6H5MH
278  */
279 HWTEST_F(UtilsDirectoryTest, ChangeOwnerRecursiveTest001, TestSize.Level1)
280 {
281     GTEST_LOG_(INFO) << "ChangeOwnerRecursiveTest001 Start";
282     try {
283         string path = "/data/tdd";
284         ForceRemoveDirectory(path);
285 
286         mode_t mode = (S_IRWXU | S_IRWXG | S_IRWXO);
287         uid_t uid = UID_ROOT;
288         gid_t gid = 0; // root
289         ForceCreateDirectory(path, mode, uid, gid);
290 
291         string subPath = path + "/sub";
292         ForceCreateDirectory(subPath, mode, uid, gid);
293 
294         string fileStr = path + "/test.txt";
295         int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
296         ASSERT_TRUE(fd != -1) << "ChangeOwnerRecursiveTest001 Create fileStr Failed!";
297         close(fd);
298 
299         EXPECT_EQ(ChangeOwnerRecursive(fileStr, UID_SYSTEM, 1), -1);
300         EXPECT_EQ(ChangeOwnerRecursive(path, UID_SYSTEM, 1), 0);
301         EXPECT_TRUE(ForceRemoveDirectoryDeepFirst(path));
302     } catch (...) {
303         EXPECT_TRUE(false);
304         GTEST_LOG_(INFO) << " ChangeOwnerRecursiveTest001 ERROR";
305     }
306     GTEST_LOG_(INFO) << "ChangeOwnerRecursiveTest001 End";
307 }
308 
309 /**
310  * @tc.name: IsInt32Test001
311  * @tc.desc: Verify the IsInt32 function
312  * @tc.type: FUNC
313  * @tc.require: I6H5MH
314  */
315 HWTEST_F(UtilsDirectoryTest, IsInt32Test001, TestSize.Level1)
316 {
317     GTEST_LOG_(INFO) << "IsInt32Test001 Start";
318     try {
319         nlohmann::json jsonObj;
320         string key = "test";
321         EXPECT_EQ(IsInt32(jsonObj, key), false);
322 
323         jsonObj["test"] = 20;
324         EXPECT_EQ(IsInt32(jsonObj, key), true);
325     } catch (...) {
326         EXPECT_TRUE(false);
327         GTEST_LOG_(INFO) << " IsInt32Test001 ERROR";
328     }
329     GTEST_LOG_(INFO) << "IsInt32Test001 End";
330 }
331 
332 /**
333  * @tc.name: SysEventWriteTest001
334  * @tc.desc: Verify the SysEventWrite function
335  * @tc.type: FUNC
336  * @tc.require: I6H5MH
337  */
338 HWTEST_F(UtilsDirectoryTest, SysEventWriteTest001, TestSize.Level1)
339 {
340     GTEST_LOG_(INFO) << "SysEventWriteTest001 Start";
341     try {
342         string uid;
343         SysEventWrite(uid);
344         uid = "test";
345         SysEventWrite(uid);
346         SysEventFileParse(1000);
347     } catch (...) {
348         EXPECT_TRUE(false);
349         GTEST_LOG_(INFO) << " SysEventWriteTest001 ERROR";
350     }
351     GTEST_LOG_(INFO) << "SysEventWriteTest001 End";
352 }
353 
354 /**
355  * @tc.name: IsFilePathInvalid001
356  * @tc.desc: Verify the IsFilePathInvalid function
357  * @tc.type: FUNC
358  * @tc.require: I6H5MH
359  */
360 HWTEST_F(UtilsDirectoryTest, IsFilePathInvalid001, TestSize.Level1)
361 {
362     GTEST_LOG_(INFO) << "IsFilePathInvalid001 Start";
363     try {
364         std::string testPath1 = "../test../test1";
365         std::string testPath2 = "/../test../test1";
366         std::string testPath3 = "test../../test";
367         std::string testPath4 = "test../../";
368         std::string testPath5 = "test../test../..";
369         std::string testPath6 = "/test/..test/..";
370 
371         std::string testPath7 = "test";
372         std::string testPath8 = "/test/test../test";
373         std::string testPath9 = "/test../test../test";
374         std::string testPath10 = "/test../test../test../";
375         std::string testPath11 = "/test../test../test../..test";
376 
377         bool isForbid1 = IsFilePathInvalid(testPath1);
378         EXPECT_TRUE(isForbid1);
379         bool isForbid2 = IsFilePathInvalid(testPath2);
380         EXPECT_TRUE(isForbid2);
381         bool isForbid3 = IsFilePathInvalid(testPath3);
382         EXPECT_TRUE(isForbid3);
383         bool isForbid4 = IsFilePathInvalid(testPath4);
384         EXPECT_TRUE(isForbid4);
385         bool isForbid5 = IsFilePathInvalid(testPath5);
386         EXPECT_TRUE(isForbid5);
387         bool isForbid6 = IsFilePathInvalid(testPath6);
388         EXPECT_TRUE(isForbid6);
389 
390         bool isForbid7 = IsFilePathInvalid(testPath7);
391         EXPECT_FALSE(isForbid7);
392         bool isForbid8 = IsFilePathInvalid(testPath8);
393         EXPECT_FALSE(isForbid8);
394         bool isForbid9 = IsFilePathInvalid(testPath9);
395         EXPECT_FALSE(isForbid9);
396         bool isForbid10 = IsFilePathInvalid(testPath10);
397         EXPECT_FALSE(isForbid10);
398         bool isForbid11 = IsFilePathInvalid(testPath11);
399         EXPECT_FALSE(isForbid11);
400 
401     } catch (...) {
402         GTEST_LOG_(INFO) << " IsFilePathInvalid001 ERROR";
403     }
404     GTEST_LOG_(INFO) << "IsFilePathInvalid001 End";
405 }
406 } // namespace Utils
407 } // namespace DistributedFile
408 } // namespace Storage
409 } // namespace OHOS