• 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 
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 
27 namespace OHOS {
28 namespace StorageDaemon {
29 using namespace testing::ext;
30 
31 namespace {
32     const uint32_t ALL_PERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
33     const std::string PATH_CHMOD = "/data/storage_daemon_chmod_test_dir";
34     const std::string PATH_CHOWN = "/data/storage_daemon_chown_test_dir";
35     const std::string PATH_RMDIR = "/data/storage_daemon_rmdir_test_dir";
36     const std::string PATH_MKDIR = "/data/storage_daemon_mkdir_test_dir";
37     const std::string PATH_MOUNT = "/data/storage_daemon_mount_test_dir";
38 }
39 
40 int32_t ChMod(const std::string &path, mode_t mode);
41 int32_t ChOwn(const std::string &path, uid_t uid, gid_t gid);
42 int32_t MkDir(const std::string &path, mode_t mode);
43 int32_t RmDir(const std::string &path);
44 
45 class FileUtilsTest : public testing::Test {
46 public:
SetUpTestCase(void)47     static void SetUpTestCase(void) {};
TearDownTestCase(void)48     static void TearDownTestCase(void) {};
49     void SetUp();
50     void TearDown();
51 };
52 
SetUp(void)53 void FileUtilsTest::SetUp(void)
54 {
55     mode_t mode = 002;
56     umask(mode);
57     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHMOD);
58     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHOWN);
59     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MKDIR);
60     StorageTest::StorageTestUtils::RmDirRecurse(PATH_RMDIR);
61     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MOUNT);
62 }
63 
TearDown(void)64 void FileUtilsTest::TearDown(void)
65 {
66     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHMOD);
67     StorageTest::StorageTestUtils::RmDirRecurse(PATH_CHOWN);
68     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MKDIR);
69     StorageTest::StorageTestUtils::RmDirRecurse(PATH_RMDIR);
70     StorageTest::StorageTestUtils::RmDirRecurse(PATH_MOUNT);
71 }
72 
73 /**
74  * @tc.name: FileUtilsTest_ChMod_001
75  * @tc.desc: Verify the ChMod function.
76  * @tc.type: FUNC
77  * @tc.require: AR000GK4HB
78  */
79 HWTEST_F(FileUtilsTest, FileUtilsTest_ChMod_001, TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "FileUtilsTest_ChMod_001 start";
82 
83     mode_t mode = 0660;
84     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_CHMOD, mode);
85     ASSERT_TRUE(bRet);
86     struct stat st;
87     int32_t ret = lstat(PATH_CHMOD.c_str(), &st);
88     ASSERT_TRUE(ret == 0);
89     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
90 
91     mode = 0771;
92     ret = ChMod(std::string(PATH_CHMOD), mode);
93     ASSERT_TRUE(ret == E_OK);
94 
95     ret = lstat(PATH_CHMOD.c_str(), &st);
96     ASSERT_TRUE(ret == 0);
97     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
98 
99     GTEST_LOG_(INFO) << "FileUtilsTest_ChMod_001 end";
100 }
101 
102 /**
103  * @tc.name: FileUtilsTest_ChOwn_001
104  * @tc.desc: Verify the ChOwn function.
105  * @tc.type: FUNC
106  * @tc.require: AR000GK4HB
107  */
108 HWTEST_F(FileUtilsTest, FileUtilsTest_ChOwn_001, TestSize.Level1)
109 {
110     GTEST_LOG_(INFO) << "FileUtilsTest_ChOwn_001 start";
111 
112     mode_t mode = 0660;
113     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_CHOWN, mode);
114     ASSERT_TRUE(bRet);
115     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_CHOWN));
116 
117     uid_t uid = 00;
118     gid_t gid = 01;
119     int32_t ret = ChOwn(PATH_CHOWN, uid, gid);
120     ASSERT_TRUE(ret == E_OK);
121 
122     struct stat st;
123     ret = lstat(PATH_CHOWN.c_str(), &st);
124     ASSERT_TRUE(ret == 0);
125     EXPECT_TRUE(st.st_uid == uid);
126     EXPECT_TRUE(st.st_gid == gid);
127 
128     uid = 01;
129     gid = 00;
130     ret = ChOwn(PATH_CHOWN, uid, gid);
131     ASSERT_TRUE(ret == E_OK);
132 
133     ret = lstat(PATH_CHOWN.c_str(), &st);
134     ASSERT_TRUE(ret == 0);
135     EXPECT_TRUE(st.st_uid == uid);
136     EXPECT_TRUE(st.st_gid == gid);
137 
138     GTEST_LOG_(INFO) << "FileUtilsTest_ChOwn_001 end";
139 }
140 
141 /**
142  * @tc.name: FileUtilsTest_MkDir_001
143  * @tc.desc: Verify the MkDir function.
144  * @tc.type: FUNC
145  * @tc.require: AR000GK4HB
146  */
147 HWTEST_F(FileUtilsTest, FileUtilsTest_MkDir_001, TestSize.Level1)
148 {
149     GTEST_LOG_(INFO) << "FileUtilsTest_MkDir_001 start";
150 
151     mode_t mode = 0771;
152     int32_t ret = MkDir(PATH_MKDIR.c_str(), mode);
153     ASSERT_TRUE(ret == E_OK);
154     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_MKDIR)) << "check the dir";
155 
156     struct stat st;
157     ret = lstat(PATH_MKDIR.c_str(), &st);
158     ASSERT_TRUE(ret == 0);
159     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
160 
161     GTEST_LOG_(INFO) << "FileUtilsTest_MkDir_001 end";
162 }
163 
164 /**
165  * @tc.name: FileUtilsTest_RmDir_001
166  * @tc.desc: Verify the RmDir function.
167  * @tc.type: FUNC
168  * @tc.require: AR000GK4HB
169  */
170 HWTEST_F(FileUtilsTest, FileUtilsTest_RmDir_001, TestSize.Level1)
171 {
172     GTEST_LOG_(INFO) << "FileUtilsTest_RmDir_001 start";
173 
174     mode_t mode = 0771;
175     bool bRet = StorageTest::StorageTestUtils::MkDir(PATH_RMDIR, mode);
176     ASSERT_TRUE(bRet);
177     ASSERT_TRUE(StorageTest::StorageTestUtils::CheckDir(PATH_RMDIR));
178 
179     int32_t ret = RmDir(PATH_RMDIR);
180     ASSERT_TRUE(ret == E_OK);
181     EXPECT_TRUE(false == StorageTest::StorageTestUtils::CheckDir(PATH_RMDIR));
182 
183     GTEST_LOG_(INFO) << "FileUtilsTest_RmDir_001 end";
184 }
185 
186 /**
187  * @tc.name: FileUtilsTest_PrepareDir_001
188  * @tc.desc: Verify the PrepareDir function.
189  * @tc.type: FUNC
190  * @tc.require: AR000GK4HB
191  */
192 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_001, TestSize.Level1)
193 {
194     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_001 start";
195 
196     mode_t mode = 0771;
197     uid_t uid = 00;
198     gid_t gid = 01;
199     int fd = open(PATH_MKDIR.c_str(), O_RDWR | O_CREAT, mode);
200     ASSERT_TRUE(fd > 0);
201 
202     bool ret = PrepareDir(PATH_MKDIR, mode, uid, gid);
203     ASSERT_TRUE(ret != true) << "path is not a dir";
204 
205     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_001 end";
206 }
207 
208 /**
209  * @tc.name: FileUtilsTest_PrepareDir_002
210  * @tc.desc: Verify the PrepareDir function.
211  * @tc.type: FUNC
212  * @tc.require: AR000GK4HB
213  */
214 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_002, TestSize.Level1)
215 {
216     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_002 start";
217 
218     mode_t mode = 0664;
219     StorageTest::StorageTestUtils::MkDir(PATH_MKDIR, mode);
220 
221     mode = 0771;
222     uid_t uid = 00;
223     gid_t gid = 01;
224     bool bRet = PrepareDir(PATH_MKDIR, mode, uid, gid);
225     ASSERT_TRUE(bRet) << "check the dir is exists but mode is incorrect";
226 
227     struct stat st;
228     int ret = lstat(PATH_MKDIR.c_str(), &st);
229     ASSERT_TRUE(ret == 0);
230     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
231     EXPECT_TRUE(st.st_uid == uid);
232     EXPECT_TRUE(st.st_gid == gid);
233 
234     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_002 end";
235 }
236 
237 /**
238  * @tc.name: FileUtilsTest_PrepareDir_003
239  * @tc.desc: Verify the PrepareDir function.
240  * @tc.type: FUNC
241  * @tc.require: AR000GK4HB
242  */
243 HWTEST_F(FileUtilsTest, FileUtilsTest_PrepareDir_003, TestSize.Level1)
244 {
245     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_003 start";
246 
247     mode_t mode = 0771;
248     uid_t uid = 00;
249     gid_t gid = 01;
250     bool bRet = PrepareDir(PATH_MKDIR, mode, uid, gid);
251     ASSERT_TRUE(bRet);
252 
253     struct stat st;
254     int ret = lstat(PATH_MKDIR.c_str(), &st);
255     ASSERT_TRUE(ret == 0);
256     EXPECT_TRUE((st.st_mode & ALL_PERMS) == mode);
257     EXPECT_TRUE(st.st_uid == uid);
258     EXPECT_TRUE(st.st_gid == gid);
259 
260     GTEST_LOG_(INFO) << "FileUtilsTest_PrepareDir_003 end";
261 }
262 } // STORAGE_DAEMON
263 } // OHOS
264