1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <sys/vfs.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include <string>
26
27 #include "utils.h"
28
Check(StatFsT & sb)29 template <typename StatFsT> void Check(StatFsT& sb) {
30 EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize));
31 EXPECT_EQ(0U, sb.f_bfree);
32 EXPECT_EQ(0U, sb.f_ffree);
33 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
34
35 // Linux 6.7 requires that all filesystems have a non-zero fsid.
36 if (sb.f_fsid.__val[0] != 0U) {
37 // fs/libfs.c reuses the filesystem's device number.
38 struct stat proc_sb;
39 ASSERT_EQ(0, stat("/proc", &proc_sb));
40 EXPECT_EQ(static_cast<int>(proc_sb.st_dev), sb.f_fsid.__val[0]);
41 EXPECT_EQ(0, sb.f_fsid.__val[1]);
42 } else {
43 // Prior to that, the fsid for /proc was just 0.
44 EXPECT_EQ(0, sb.f_fsid.__val[0]);
45 EXPECT_EQ(0, sb.f_fsid.__val[1]);
46 }
47
48 // The kernel sets a private bit to indicate that f_flags is valid.
49 // This flag is not supposed to be exposed to libc clients.
50 static const uint32_t ST_VALID = 0x0020;
51 EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags;
52 }
53
TEST(sys_vfs,statfs)54 TEST(sys_vfs, statfs) {
55 struct statfs sb;
56 ASSERT_EQ(0, statfs("/proc", &sb));
57 Check(sb);
58 }
59
TEST(sys_vfs,statfs_failure)60 TEST(sys_vfs, statfs_failure) {
61 struct statfs sb;
62 errno = 0;
63 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
64 ASSERT_ERRNO(ENOENT);
65 }
66
TEST(sys_vfs,statfs64_smoke)67 TEST(sys_vfs, statfs64_smoke) {
68 struct statfs64 sb;
69 ASSERT_EQ(0, statfs64("/proc", &sb));
70 Check(sb);
71 }
72
TEST(sys_vfs,statfs64_failure)73 TEST(sys_vfs, statfs64_failure) {
74 struct statfs64 sb;
75 errno = 0;
76 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
77 ASSERT_ERRNO(ENOENT);
78 }
79
TEST(sys_vfs,fstatfs)80 TEST(sys_vfs, fstatfs) {
81 struct statfs sb;
82 int fd = open("/proc", O_RDONLY);
83 ASSERT_EQ(0, fstatfs(fd, &sb));
84 close(fd);
85 Check(sb);
86 }
87
TEST(sys_vfs,fstatfs_failure)88 TEST(sys_vfs, fstatfs_failure) {
89 struct statfs sb;
90 errno = 0;
91 ASSERT_EQ(-1, fstatfs(-1, &sb));
92 ASSERT_ERRNO(EBADF);
93 }
94
TEST(sys_vfs,fstatfs64_smoke)95 TEST(sys_vfs, fstatfs64_smoke) {
96 struct statfs64 sb;
97 int fd = open("/proc", O_RDONLY);
98 ASSERT_EQ(0, fstatfs64(fd, &sb));
99 close(fd);
100 Check(sb);
101 }
102
TEST(sys_vfs,fstatfs64_failure)103 TEST(sys_vfs, fstatfs64_failure) {
104 struct statfs sb;
105 errno = 0;
106 ASSERT_EQ(-1, fstatfs(-1, &sb));
107 ASSERT_ERRNO(EBADF);
108 }
109