• 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: StorageRadarTest_RecordKillProcessResult_000
267  * @tc.desc: Verify the StorageRadar function.
268  * @tc.type: FUNC
269  * @tc.require: AR000GK4HB
270  */
271 HWTEST_F(FileUtilsTest, StorageRadarTest_RecordKillProcessResult, TestSize.Level1)
272 {
273     GTEST_LOG_(INFO) << "StorageRadarTest_RecordKillProcessResult_000 start";
274 
275     bool ret = StorageService::StorageRadar::GetInstance().RecordKillProcessResult("", E_OK);
276     ASSERT_TRUE(ret == true);
277 
278     ret = StorageService::StorageRadar::GetInstance().RecordKillProcessResult("", E_ERR);
279     ASSERT_TRUE(ret == true);
280     GTEST_LOG_(INFO) << "StorageRadarTest_RecordKillProcessResult_000 end";
281 }
282 } // STORAGE_DAEMON
283 } // OHOS
284