• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 <fcntl.h>
17 #include <sys/mount.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 
21 #include "gtest/gtest.h"
22 #include "common/help_utils.h"
23 #include "storage_service_errno.h"
24 #include "storage_service_log.h"
25 #include "utils/file_utils.h"
26 #include "utils/storage_radar.h"
27 
28 namespace OHOS {
29 namespace StorageDaemon {
30 using namespace testing::ext;
31 
32 namespace {
33     const uint32_t ALL_PERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
34     const std::string PATH_CHMOD = "/data/storage_daemon_chmod_test_dir";
35     const std::string PATH_CHOWN = "/data/storage_daemon_chown_test_dir";
36     const std::string PATH_RMDIR = "/data/storage_daemon_rmdir_test_dir";
37     const std::string PATH_MKDIR = "/data/storage_daemon_mkdir_test_dir";
38     const std::string PATH_MOUNT = "/data/storage_daemon_mount_test_dir";
39 }
40 
41 int32_t ChMod(const std::string &path, mode_t mode);
42 int32_t ChOwn(const std::string &path, uid_t uid, gid_t gid);
43 int32_t MkDir(const std::string &path, mode_t mode);
44 int32_t RmDir(const std::string &path);
45 
46 class FileUtilsTest : public testing::Test {
47 public:
SetUpTestCase(void)48     static void SetUpTestCase(void) {};
TearDownTestCase(void)49     static void TearDownTestCase(void) {};
50     void SetUp();
51     void TearDown();
52 };
53 
SetUp(void)54 void FileUtilsTest::SetUp(void)
55 {
56     mode_t mode = 002;
57     umask(mode);
58     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHMOD);
59     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHOWN);
60     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MKDIR);
61     StorageTest::StorageTestUtils::RmDirRecurse(PATH_RMDIR);
62     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MOUNT);
63 }
64 
TearDown(void)65 void FileUtilsTest::TearDown(void)
66 {
67     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHMOD);
68     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHOWN);
69     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MKDIR);
70     StorageTest::StorageTestUtils::RmDirRecurse(PATH_RMDIR);
71     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MOUNT);
72 }
73 
74 /**
75  * @tc.name: FileUtilsTest_ChMod_001
76  * @tc.desc: Verify the ChMod function.
77  * @tc.type: FUNC
78  * @tc.require: AR000GK4HB
79  */
80 HWTEST_F(FileUtilsTest, FileUtilsTest_ChMod_001, TestSize.Level1)
81 {
82     GTEST_LOG_(INFO) << "FileUtilsTest_ChMod_001 start";
83 
84     mode_t mode = 0660;
85     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_CHMOD, mode);
86     ASSERT_TRUE(bRet);
87     struct stat st;
88     int32_t ret = lstat(PATH_CHMOD.c_str(), &st);
89     ASSERT_TRUE(ret == 0);
90     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
91 
92     mode = 0771;
93     ret = ChMod(std::string(PATH_CHMOD), mode);
94     ASSERT_TRUE(ret == E_OK);
95 
96     ret = lstat(PATH_CHMOD.c_str(), &st);
97     ASSERT_TRUE(ret == 0);
98     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
99 
100     GTEST_LOG_(INFO) << "FileUtilsTest_ChMod_001 end";
101 }
102 
103 /**
104  * @tc.name: FileUtilsTest_ChOwn_001
105  * @tc.desc: Verify the ChOwn function.
106  * @tc.type: FUNC
107  * @tc.require: AR000GK4HB
108  */
109 HWTEST_F(FileUtilsTest, FileUtilsTest_ChOwn_001, TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "FileUtilsTest_ChOwn_001 start";
112 
113     mode_t mode = 0660;
114     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_CHOWN, mode);
115     ASSERT_TRUE(bRet);
116     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_CHOWN));
117 
118     uid_t uid = 00;
119     gid_t gid = 01;
120     int32_t ret = ChOwn(PATH_CHOWN, uid, gid);
121     ASSERT_TRUE(ret == E_OK);
122 
123     struct stat st;
124     ret = lstat(PATH_CHOWN.c_str(), &st);
125     ASSERT_TRUE(ret == 0);
126     EXPECT_TRUE(st.st_uid == uid);
127     EXPECT_TRUE(st.st_gid == gid);
128 
129     uid = 01;
130     gid = 00;
131     ret = ChOwn(PATH_CHOWN, uid, gid);
132     ASSERT_TRUE(ret == E_OK);
133 
134     ret = lstat(PATH_CHOWN.c_str(), &st);
135     ASSERT_TRUE(ret == 0);
136     EXPECT_TRUE(st.st_uid == uid);
137     EXPECT_TRUE(st.st_gid == gid);
138 
139     GTEST_LOG_(INFO) << "FileUtilsTest_ChOwn_001 end";
140 }
141 
142 /**
143  * @tc.name: FileUtilsTest_MkDir_001
144  * @tc.desc: Verify the MkDir function.
145  * @tc.type: FUNC
146  * @tc.require: AR000GK4HB
147  */
148 HWTEST_F(FileUtilsTest, FileUtilsTest_MkDir_001, TestSize.Level1)
149 {
150     GTEST_LOG_(INFO) << "FileUtilsTest_MkDir_001 start";
151 
152     mode_t mode = 0771;
153     int32_t ret = MkDir(PATH_MKDIR.c_str(), mode);
154     ASSERT_TRUE(ret == E_OK);
155     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_MKDIR)) << "check the dir";
156 
157     struct stat st;
158     ret = lstat(PATH_MKDIR.c_str(), &st);
159     ASSERT_TRUE(ret == 0);
160     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
161 
162     GTEST_LOG_(INFO) << "FileUtilsTest_MkDir_001 end";
163 }
164 
165 /**
166  * @tc.name: FileUtilsTest_RmDir_001
167  * @tc.desc: Verify the RmDir function.
168  * @tc.type: FUNC
169  * @tc.require: AR000GK4HB
170  */
171 HWTEST_F(FileUtilsTest, FileUtilsTest_RmDir_001, TestSize.Level1)
172 {
173     GTEST_LOG_(INFO) << "FileUtilsTest_RmDir_001 start";
174 
175     mode_t mode = 0771;
176     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_RMDIR, mode);
177     ASSERT_TRUE(bRet);
178     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_RMDIR));
179 
180     int32_t ret = RmDir(PATH_RMDIR);
181     ASSERT_TRUE(ret == E_OK);
182     EXPECT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_RMDIR) == false);
183 
184     GTEST_LOG_(INFO) << "FileUtilsTest_RmDir_001 end";
185 }
186 
187 /**
188  * @tc.name: FileUtilsTest_PrepareDir_001
189  * @tc.desc: Verify the PrepareDir function.
190  * @tc.type: FUNC
191  * @tc.require: AR000GK4HB
192  */
193 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_001, TestSize.Level1)
194 {
195     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_001 start";
196 
197     mode_t mode = 0771;
198     uid_t uid = 00;
199     gid_t gid = 01;
200     int fd = open(PATH_MKDIR.c_str(), O_RDWR | O_CREAT, mode);
201     ASSERT_TRUE(fd > 0);
202 
203     bool ret = PrepareDir(PATH_MKDIR, mode, uid, gid);
204     ASSERT_TRUE(ret != true) << "path is not a dir";
205 
206     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_001 end";
207     (void)close(fd);
208 }
209 
210 /**
211  * @tc.name: FileUtilsTest_PrepareDir_002
212  * @tc.desc: Verify the PrepareDir function.
213  * @tc.type: FUNC
214  * @tc.require: AR000GK4HB
215  */
216 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_002, TestSize.Level1)
217 {
218     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_002 start";
219 
220     mode_t mode = 0664;
221     StorageTest::StorageTestUtils::MkDir(PATH_MKDIR, mode);
222 
223     mode = 0771;
224     uid_t uid = 00;
225     gid_t gid = 01;
226     bool bRet = PrepareDir(PATH_MKDIR, mode, uid, gid);
227     ASSERT_TRUE(bRet) << "check the dir is exists but mode is incorrect";
228 
229     struct stat st;
230     int ret = lstat(PATH_MKDIR.c_str(), &st);
231     ASSERT_TRUE(ret == 0);
232     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
233     EXPECT_TRUE(st.st_uid == uid);
234     EXPECT_TRUE(st.st_gid == gid);
235 
236     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_002 end";
237 }
238 
239 /**
240  * @tc.name: FileUtilsTest_PrepareDir_003
241  * @tc.desc: Verify the PrepareDir function.
242  * @tc.type: FUNC
243  * @tc.require: AR000GK4HB
244  */
245 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_003, TestSize.Level1)
246 {
247     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_003 start";
248 
249     mode_t mode = 0771;
250     uid_t uid = 00;
251     gid_t gid = 01;
252     bool bRet = PrepareDir(PATH_MKDIR, mode, uid, gid);
253     ASSERT_TRUE(bRet);
254 
255     struct stat st;
256     int ret = lstat(PATH_MKDIR.c_str(), &st);
257     ASSERT_TRUE(ret == 0);
258     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
259     EXPECT_TRUE(st.st_uid == uid);
260     EXPECT_TRUE(st.st_gid == gid);
261 
262     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_003 end";
263 }
264 
265 /**
266  * @tc.name: FileUtilsTest_Split_001
267  * @tc.desc: Verify the Split function.
268  * @tc.type: FUNC
269  * @tc.require: IBDKKD
270  */
271 HWTEST_F(FileUtilsTest, FileUtilsTest_Split_001, TestSize.Level1)
272 {
273     GTEST_LOG_(INFO) << "FileUtilsTest_Split_001 start";
274 
275     std::string test = "this is a good idea";
276     std::string pattern = " ";
277     std::vector<std::string> expectVec{ "this", "is", "a", "good", "idea" };
278     auto ret = Split(test, pattern);
279     EXPECT_EQ(ret, expectVec);
280     GTEST_LOG_(INFO) << "FileUtilsTest_Split_001 end";
281 }
282 
283 /**
284  * @tc.name: FileUtilsTest_DelFolder_001
285  * @tc.desc: Verify the DelFolder function.
286  * @tc.type: FUNC
287  * @tc.require: IBDKKD
288  */
289 HWTEST_F(FileUtilsTest, FileUtilsTest_DelFolder_001, TestSize.Level1)
290 {
291     GTEST_LOG_(INFO) << "FileUtilsTest_DelFolder_001 start";
292 
293     std::string testPath = "/data/test/tdd";
294     EXPECT_TRUE(CreateFolder(testPath));
295     EXPECT_TRUE(DelFolder(testPath));
296     EXPECT_FALSE(DelFolder(testPath));
297     GTEST_LOG_(INFO) << "FileUtilsTest_DelFolder_001 end";
298 }
299 
300 /**
301  * @tc.name: FileUtilsTest_IsFile_001
302  * @tc.desc: Verify the IsFile function.
303  * @tc.type: FUNC
304  * @tc.require: IBDKKD
305  */
306 HWTEST_F(FileUtilsTest, FileUtilsTest_IsFile_001, TestSize.Level1)
307 {
308     GTEST_LOG_(INFO) << "FileUtilsTest_IsFile_001 start";
309 
310     std::string testPath = "/data/test/tdd";
311     EXPECT_TRUE(CreateFolder(testPath));
312     std::string fileName = testPath + "/test.txt";
313     auto fd = open(fileName.c_str(), O_RDWR | O_CREAT);
314     ASSERT_GT(fd, 0);
315     close(fd);
316     EXPECT_TRUE(IsFile(fileName));
317     EXPECT_TRUE(DeleteFile(fileName) == 0);
318     EXPECT_FALSE(IsFile(fileName));
319     EXPECT_TRUE(DelFolder(testPath));
320     GTEST_LOG_(INFO) << "FileUtilsTest_IsFile_001 end";
321 }
322 
323 /**
324  * @tc.name: FileUtilsTest_TravelChmod_001
325  * @tc.desc: Verify the IsFile function.
326  * @tc.type: FUNC
327  * @tc.require: IBDKKD
328  */
329 HWTEST_F(FileUtilsTest, FileUtilsTest_TravelChmod_001, TestSize.Level1)
330 {
331     GTEST_LOG_(INFO) << "FileUtilsTest_TravelChmod_001 start";
332     std::string basePath = "/data/test/tdd";
333     std::string testPath = basePath + "/fold";
334     EXPECT_TRUE(CreateFolder(testPath));
335     std::string fileName = basePath + "/test.txt";
336     auto fd = open(fileName.c_str(), O_RDWR | O_CREAT);
337     ASSERT_GT(fd, 0);
338     close(fd);
339 
340     mode_t mode = 0777;
341     TravelChmod(basePath, mode);
342     TravelChmod(fileName, mode);
343     EXPECT_TRUE(DeleteFile(basePath) == 0);
344     EXPECT_TRUE(DelFolder(basePath));
345     TravelChmod(basePath, mode);
346     GTEST_LOG_(INFO) << "FileUtilsTest_TravelChmod_001 end";
347 }
348 
349 /**
350  * @tc.name: FileUtilsTest_IsTempFolder_001
351  * @tc.desc: Verify the IsTempFolder function.
352  * @tc.type: FUNC
353  * @tc.require: IBDKKD
354  */
355 HWTEST_F(FileUtilsTest, FileUtilsTest_IsTempFolder_001, TestSize.Level1)
356 {
357     GTEST_LOG_(INFO) << "FileUtilsTest_IsTempFolder_001 start";
358     std::string basePath = "/data/test/tdd/fold";
359     std::string sub = "fold";
360     std::string sub2 = "temp";
361     EXPECT_TRUE(CreateFolder(basePath));
362     EXPECT_TRUE(IsTempFolder(basePath, sub));
363     EXPECT_FALSE(IsTempFolder(basePath, sub2));
364     EXPECT_TRUE(DeleteFile(basePath) == 0);
365     EXPECT_TRUE(DelFolder(basePath));
366     GTEST_LOG_(INFO) << "FileUtilsTest_IsTempFolder_001 end";
367 }
368 
369 /**
370  * @tc.name: FileUtilsTest_DelTemp_001
371  * @tc.desc: Verify the DelTemp function.
372  * @tc.type: FUNC
373  * @tc.require: IBDKKD
374  */
375 HWTEST_F(FileUtilsTest, FileUtilsTest_DelTemp_001, TestSize.Level1)
376 {
377     GTEST_LOG_(INFO) << "FileUtilsTest_DelTemp_001 start";
378     std::string basePath = "/data/test/tdd";
379     std::string subPath1 = basePath + "/fold";
380     std::string subPath2 = basePath + "/simple-mtpfs";
381     EXPECT_TRUE(CreateFolder(subPath1));
382     EXPECT_TRUE(CreateFolder(subPath2));
383     DelTemp(basePath);
384     EXPECT_TRUE(IsDir(subPath1));
385     EXPECT_FALSE(IsDir(subPath2));
386     EXPECT_TRUE(DeleteFile(basePath) == 0);
387     EXPECT_TRUE(DelFolder(basePath));
388     GTEST_LOG_(INFO) << "FileUtilsTest_DelTemp_001 end";
389 }
390 
391 /**
392  * @tc.name: FileUtilsTest_KillProcess_001
393  * @tc.desc: Verify the KillProcess function.
394  * @tc.type: FUNC
395  * @tc.require: IBDKKD
396  */
397 HWTEST_F(FileUtilsTest, FileUtilsTest_KillProcess_001, TestSize.Level1)
398 {
399     GTEST_LOG_(INFO) << "FileUtilsTest_KillProcess_001 start";
400     std::vector<ProcessInfo> processList;
401     std::vector<ProcessInfo> killFailList;
402     KillProcess(processList, killFailList);
403 
404     ProcessInfo info1 {.name = "test1", .pid = 65300 };
405     ProcessInfo info2 {.name = "test2", .pid = 65301 };
406     processList.push_back(info1);
407     processList.push_back(info2);
408 
409     KillProcess(processList, killFailList);
410     EXPECT_EQ(killFailList.size(), 0);
411     GTEST_LOG_(INFO) << "FileUtilsTest_KillProcess_001 end";
412 }
413 } // STORAGE_DAEMON
414 } // OHOS
415