• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/file.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/mount.h>
28 #include "securec.h"
29 
30 using namespace testing::ext;
31 using namespace std;
32 
33 const char *SOURCE = "/mnt/source01";
34 const char *TARGET = "/mnt/target01";
35 mode_t MODE_0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
36 
37 class MountApiTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp();
42     void TearDown();
43 private:
44 };
SetUp()45 void MountApiTest::SetUp()
46 {
47 }
TearDown()48 void MountApiTest::TearDown()
49 {
50 }
SetUpTestCase()51 void MountApiTest::SetUpTestCase()
52 {
53     mkdir(SOURCE, MODE_0755);
54     mkdir(TARGET, MODE_0755);
55 }
TearDownTestCase()56 void MountApiTest::TearDownTestCase()
57 {
58     rmdir(SOURCE);
59     rmdir(TARGET);
60 }
61 
62 /*
63  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0100
64  * @tc.name   : MountSuccess_0001
65  * @tc.desc   : Mount dir success by data.
66  * @tc.size   : MediumTest
67  * @tc.type   : Function
68  * @tc.level  : Level 1
69  */
70 HWTEST_F(MountApiTest, MountSuccess_0001, Function | MediumTest | Level1)
71 {
72     const char *filesystemtype = "ext4";
73     const char *data = "rw";
74 
75     int ret = mount(SOURCE, TARGET, filesystemtype, MS_BIND, data);
76     EXPECT_EQ(ret, 0);
77 
78     int flags = MNT_FORCE;
79     ret = umount2(TARGET, flags);
80     EXPECT_EQ(ret, 0);
81 }
82 
83 /*
84  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0200
85  * @tc.name   : MountSuccess_0002
86  * @tc.desc   : Mount dir success by MS_BIND.
87  * @tc.size   : MediumTest
88  * @tc.type   : Function
89  * @tc.level  : Level 1
90  */
91 HWTEST_F(MountApiTest, MountSuccess_0002, Function | MediumTest | Level1)
92 {
93     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND, nullptr);
94     EXPECT_EQ(ret, 0);
95 
96     int flags = MNT_FORCE;
97     ret = umount2(TARGET, flags);
98     EXPECT_EQ(ret, 0);
99 }
100 
101 /*
102  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0300
103  * @tc.name   : MountSuccess_0003
104  * @tc.desc   : Mount dir success by MS_REC.
105  * @tc.size   : MediumTest
106  * @tc.type   : Function
107  * @tc.level  : Level 1
108  */
109 HWTEST_F(MountApiTest, MountSuccess_0003, Function | MediumTest | Level1)
110 {
111     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_REC, nullptr);
112     EXPECT_EQ(ret, 0);
113 
114     int flags = MNT_FORCE;
115     ret = umount2(TARGET, flags);
116     EXPECT_EQ(ret, 0);
117 }
118 
119 /*
120  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0400
121  * @tc.name   : Mountsuccess_0004
122  * @tc.desc   : Mount dir success by MS_NOATIME.
123  * @tc.size   : MediumTest
124  * @tc.type   : Function
125  * @tc.level  : Level 1
126  */
127 HWTEST_F(MountApiTest, Mountsuccess_0004, Function | MediumTest | Level1)
128 {
129     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NOATIME, nullptr);
130     EXPECT_EQ(ret, 0);
131 
132     int flags = MNT_FORCE;
133     ret = umount2(TARGET, flags);
134     EXPECT_EQ(ret, 0);
135 }
136 
137 /*
138  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0500
139  * @tc.name   : MountSuccess_0005
140  * @tc.desc   : Mount dir success by MS_NODIRATIME.
141  * @tc.size   : MediumTest
142  * @tc.type   : Function
143  * @tc.level  : Level 1
144  */
145 HWTEST_F(MountApiTest, MountSuccess_0005, Function | MediumTest | Level1)
146 {
147     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NODIRATIME, nullptr);
148     EXPECT_EQ(ret, 0);
149 
150     int flags = MNT_FORCE;
151     ret = umount2(TARGET, flags);
152     EXPECT_EQ(ret, 0);
153 }
154 
155 /*
156  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0600
157  * @tc.name   : MountSuccess_0006
158  * @tc.desc   : Mount dir success by MS_PRIVATE.
159  * @tc.size   : MediumTest
160  * @tc.type   : Function
161  * @tc.level  : Level 1
162  */
163 HWTEST_F(MountApiTest, MountSuccess_0006, Function | MediumTest | Level1)
164 {
165     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_PRIVATE, nullptr);
166     EXPECT_EQ(ret, 0);
167 
168     int flags = MNT_FORCE;
169     ret = umount2(TARGET, flags);
170     EXPECT_EQ(ret, 0);
171 }
172 
173 /*
174  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0700
175  * @tc.name   : MountSuccess_0007
176  * @tc.desc   : Mount dir success by MS_RELATIME.
177  * @tc.size   : MediumTest
178  * @tc.type   : Function
179  * @tc.level  : Level 1
180  */
181 HWTEST_F(MountApiTest, MountSuccess_0007, Function | MediumTest | Level1)
182 {
183     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_RELATIME, nullptr);
184     EXPECT_EQ(ret, 0);
185 
186     int flags = MNT_FORCE;
187     ret = umount2(TARGET, flags);
188     EXPECT_EQ(ret, 0);
189 }
190 
191 /*
192  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0800
193  * @tc.name   : MountSuccess_0008
194  * @tc.desc   : Mount dir success by MS_RDONLY.
195  * @tc.size   : MediumTest
196  * @tc.type   : Function
197  * @tc.level  : Level 1
198  */
199 HWTEST_F(MountApiTest, MountSuccess_0008, Function | MediumTest | Level1)
200 {
201     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_RDONLY, nullptr);
202     EXPECT_EQ(ret, 0);
203 
204     int flags = MNT_FORCE;
205     ret = umount2(TARGET, flags);
206     EXPECT_EQ(ret, 0);
207 }
208 
209 /*
210  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_0900
211  * @tc.name   : MountSuccess_0009
212  * @tc.desc   : Mount dir success by MS_NOSUID.
213  * @tc.size   : MediumTest
214  * @tc.type   : Function
215  * @tc.level  : Level 1
216  */
217 HWTEST_F(MountApiTest, MountSuccess_0009, Function | MediumTest | Level1)
218 {
219     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NOSUID, nullptr);
220     EXPECT_EQ(ret, 0);
221 
222     int flags = MNT_FORCE;
223     ret = umount2(TARGET, flags);
224     EXPECT_EQ(ret, 0);
225 }
226 
227 /*
228  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1000
229  * @tc.name   : MountSuccess_0010
230  * @tc.desc   : Mount dir success by MS_NODEV.
231  * @tc.size   : MediumTest
232  * @tc.type   : Function
233  * @tc.level  : Level 1
234  */
235 HWTEST_F(MountApiTest, MountSuccess_0010, Function | MediumTest | Level1)
236 {
237     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NODEV, nullptr);
238     EXPECT_EQ(ret, 0);
239 
240     int flags = MNT_FORCE;
241     ret = umount2(TARGET, flags);
242     EXPECT_EQ(ret, 0);
243 }
244 
245 /*
246  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1100
247  * @tc.name   : MountSuccess_0011
248  * @tc.desc   : Umount2 dir success by MS_REMOUNT.
249  * @tc.size   : MediumTest
250  * @tc.type   : Function
251  * @tc.level  : Level 1
252  */
253 HWTEST_F(MountApiTest, MountSuccess_0011, Function | MediumTest | Level1)
254 {
255     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NOEXEC, nullptr);
256     EXPECT_EQ(ret, 0);
257     ret = mount(nullptr, TARGET, nullptr, MS_REMOUNT, nullptr);
258     EXPECT_EQ(ret, 0);
259 
260     int flags = MNT_FORCE;
261     ret = umount2(TARGET, flags);
262     EXPECT_EQ(ret, 0);
263 }
264 
265 /*
266  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1200
267  * @tc.name   : MountSuccess_0012
268  * @tc.desc   : Mount dir success by MS_NOEXEC.
269  * @tc.size   : MediumTest
270  * @tc.type   : Function
271  * @tc.level  : Level 1
272  */
273 HWTEST_F(MountApiTest, MountSuccess_0012, Function | MediumTest | Level1)
274 {
275     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NOEXEC, nullptr);
276     EXPECT_EQ(ret, 0);
277 
278     int flags = MNT_FORCE;
279     ret = umount2(TARGET, flags);
280     EXPECT_EQ(ret, 0);
281 }
282 
283 /*
284  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1300
285  * @tc.name   : MountSuccess_0013
286  * @tc.desc   : Mount dir success by MS_MANDLOCK.
287  * @tc.size   : MediumTest
288  * @tc.type   : Function
289  * @tc.level  : Level 1
290  */
291 HWTEST_F(MountApiTest, MountSuccess_0013, Function | MediumTest | Level1)
292 {
293     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_MANDLOCK, nullptr);
294     EXPECT_EQ(ret, 0);
295 
296     int flags = MNT_FORCE;
297     ret = umount2(TARGET, flags);
298     EXPECT_EQ(ret, 0);
299 }
300 
301 /*
302  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1400
303  * @tc.name   : MountFail_0014
304  * @tc.desc   : Mount dir fail by MS_REMOUNT.
305  * @tc.size   : MediumTest
306  * @tc.type   : Function
307  * @tc.level  : Level 1
308  */
309 HWTEST_F(MountApiTest, MountFail_0014, Function | MediumTest | Level1)
310 {
311     errno = 0;
312     int ret = mount(SOURCE, TARGET, nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
313     EXPECT_EQ(ret, -1);
314     EXPECT_EQ(errno, EINVAL);
315 }
316 
317 /*
318  * @tc.number : SUB_KERNEL_SYSCALL_MOUNT_1500
319  * @tc.name   : MountSuccess_0015
320  * @tc.desc   : Mount dir success by MS_REMOUNT.
321  * @tc.size   : MediumTest
322  * @tc.type   : Function
323  * @tc.level  : Level 1
324  */
325 HWTEST_F(MountApiTest, MountSuccess_0015, Function | MediumTest | Level1)
326 {
327     int ret = mount(SOURCE, TARGET, nullptr, MS_BIND | MS_NOEXEC, nullptr);
328     EXPECT_EQ(ret, 0);
329     ret = mount(nullptr, TARGET, nullptr, MS_REMOUNT, nullptr);
330     EXPECT_EQ(ret, 0);
331 
332     int flags = MNT_FORCE;
333     ret = umount2(TARGET, flags);
334     EXPECT_EQ(ret, 0);
335 }