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 #if defined(__x86_64__)
31 // On x86_64 based 16kb page size targets, the page size in userspace is simulated to 16kb but
32 // the underlying filesystem block size would remain unchanged, i.e., 4kb.
33 // For more info:
34 // https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic
35 EXPECT_EQ(4096, static_cast<int>(sb.f_bsize));
36 #else
37 EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize));
38 #endif
39 EXPECT_EQ(0U, sb.f_bfree);
40 EXPECT_EQ(0U, sb.f_ffree);
41 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
42
43 // Linux 6.7 requires that all filesystems have a non-zero fsid.
44 if (sb.f_fsid.__val[0] != 0U) {
45 // fs/libfs.c reuses the filesystem's device number.
46 struct stat proc_sb;
47 ASSERT_EQ(0, stat("/proc", &proc_sb));
48 EXPECT_EQ(static_cast<int>(proc_sb.st_dev), sb.f_fsid.__val[0]);
49 EXPECT_EQ(0, sb.f_fsid.__val[1]);
50 } else {
51 // Prior to that, the fsid for /proc was just 0.
52 EXPECT_EQ(0, sb.f_fsid.__val[0]);
53 EXPECT_EQ(0, sb.f_fsid.__val[1]);
54 }
55
56 // The kernel sets a private bit to indicate that f_flags is valid.
57 // This flag is not supposed to be exposed to libc clients.
58 static const uint32_t ST_VALID = 0x0020;
59 EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags;
60 }
61
TEST(sys_vfs,statfs)62 TEST(sys_vfs, statfs) {
63 struct statfs sb;
64 ASSERT_EQ(0, statfs("/proc", &sb));
65 Check(sb);
66 }
67
TEST(sys_vfs,statfs_failure)68 TEST(sys_vfs, statfs_failure) {
69 struct statfs sb;
70 errno = 0;
71 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
72 ASSERT_ERRNO(ENOENT);
73 }
74
TEST(sys_vfs,statfs64_smoke)75 TEST(sys_vfs, statfs64_smoke) {
76 struct statfs64 sb;
77 ASSERT_EQ(0, statfs64("/proc", &sb));
78 Check(sb);
79 }
80
TEST(sys_vfs,statfs64_failure)81 TEST(sys_vfs, statfs64_failure) {
82 struct statfs64 sb;
83 errno = 0;
84 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
85 ASSERT_ERRNO(ENOENT);
86 }
87
TEST(sys_vfs,fstatfs)88 TEST(sys_vfs, fstatfs) {
89 struct statfs sb;
90 int fd = open("/proc", O_RDONLY);
91 ASSERT_EQ(0, fstatfs(fd, &sb));
92 close(fd);
93 Check(sb);
94 }
95
TEST(sys_vfs,fstatfs_failure)96 TEST(sys_vfs, fstatfs_failure) {
97 struct statfs sb;
98 errno = 0;
99 ASSERT_EQ(-1, fstatfs(-1, &sb));
100 ASSERT_ERRNO(EBADF);
101 }
102
TEST(sys_vfs,fstatfs64_smoke)103 TEST(sys_vfs, fstatfs64_smoke) {
104 struct statfs64 sb;
105 int fd = open("/proc", O_RDONLY);
106 ASSERT_EQ(0, fstatfs64(fd, &sb));
107 close(fd);
108 Check(sb);
109 }
110
TEST(sys_vfs,fstatfs64_failure)111 TEST(sys_vfs, fstatfs64_failure) {
112 struct statfs sb;
113 errno = 0;
114 ASSERT_EQ(-1, fstatfs(-1, &sb));
115 ASSERT_ERRNO(EBADF);
116 }
117