blob: 363e49bc5ecf71b6e3f9cc0a2500463e83215410 [file] [log] [blame]
Elliott Hughes1d18e9c2014-02-18 15:48:56 -08001/*
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
27template <typename StatFsT> void Check(StatFsT& sb) {
28 EXPECT_EQ(4096, static_cast<int>(sb.f_bsize));
29 EXPECT_EQ(0U, sb.f_bfree);
30 EXPECT_EQ(0U, sb.f_ffree);
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080031 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
Elliott Hughesfa495d52015-03-18 15:46:48 -070032
Elliott Hughes3e7e2252024-01-25 10:12:37 -080033 // Linux 6.7 requires that all filesystems have a non-zero fsid.
34 if (sb.f_fsid.__val[0] != 0U) {
35 // fs/libfs.c reuses the filesystem's device number.
36 struct stat proc_sb;
37 ASSERT_EQ(0, stat("/proc", &proc_sb));
38 EXPECT_EQ(static_cast<int>(proc_sb.st_dev), sb.f_fsid.__val[0]);
39 EXPECT_EQ(0, sb.f_fsid.__val[1]);
40 } else {
41 // Prior to that, the fsid for /proc was just 0.
42 EXPECT_EQ(0, sb.f_fsid.__val[0]);
43 EXPECT_EQ(0, sb.f_fsid.__val[1]);
44 }
45
Elliott Hughesfa495d52015-03-18 15:46:48 -070046 // The kernel sets a private bit to indicate that f_flags is valid.
47 // This flag is not supposed to be exposed to libc clients.
48 static const uint32_t ST_VALID = 0x0020;
49 EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags;
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080050}
51
52TEST(sys_vfs, statfs) {
53 struct statfs sb;
54 ASSERT_EQ(0, statfs("/proc", &sb));
55 Check(sb);
56}
57
Elliott Hughes7cebf832020-08-12 14:25:41 -070058TEST(sys_vfs, statfs_failure) {
59 struct statfs sb;
60 errno = 0;
61 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
62 ASSERT_EQ(ENOENT, errno);
63}
64
Colin Cross7da20342021-07-28 11:18:11 -070065TEST(sys_vfs, statfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080066 struct statfs64 sb;
67 ASSERT_EQ(0, statfs64("/proc", &sb));
68 Check(sb);
69}
70
Elliott Hughes7cebf832020-08-12 14:25:41 -070071TEST(sys_vfs, statfs64_failure) {
72 struct statfs64 sb;
73 errno = 0;
74 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
75 ASSERT_EQ(ENOENT, errno);
76}
77
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080078TEST(sys_vfs, fstatfs) {
79 struct statfs sb;
80 int fd = open("/proc", O_RDONLY);
81 ASSERT_EQ(0, fstatfs(fd, &sb));
82 close(fd);
83 Check(sb);
84}
Elliott Hughesfa495d52015-03-18 15:46:48 -070085
Elliott Hughes7cebf832020-08-12 14:25:41 -070086TEST(sys_vfs, fstatfs_failure) {
87 struct statfs sb;
88 errno = 0;
89 ASSERT_EQ(-1, fstatfs(-1, &sb));
90 ASSERT_EQ(EBADF, errno);
91}
92
Colin Cross7da20342021-07-28 11:18:11 -070093TEST(sys_vfs, fstatfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080094 struct statfs64 sb;
95 int fd = open("/proc", O_RDONLY);
96 ASSERT_EQ(0, fstatfs64(fd, &sb));
97 close(fd);
98 Check(sb);
99}
Elliott Hughes7cebf832020-08-12 14:25:41 -0700100
101TEST(sys_vfs, fstatfs64_failure) {
102 struct statfs sb;
103 errno = 0;
104 ASSERT_EQ(-1, fstatfs(-1, &sb));
105 ASSERT_EQ(EBADF, errno);
106}