blob: 96fd61a85aef02b5ab964aacea47bd3fbba3eb6d [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
Elliott Hughes95646e62023-09-21 14:11:19 -070027#include "utils.h"
28
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080029template <typename StatFsT> void Check(StatFsT& sb) {
30 EXPECT_EQ(4096, static_cast<int>(sb.f_bsize));
31 EXPECT_EQ(0U, sb.f_bfree);
32 EXPECT_EQ(0U, sb.f_ffree);
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080033 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
Elliott Hughesfa495d52015-03-18 15:46:48 -070034
Elliott Hughes1b48afb2024-01-25 10:12:37 -080035 // 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
Elliott Hughesfa495d52015-03-18 15:46:48 -070048 // 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;
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080052}
53
54TEST(sys_vfs, statfs) {
55 struct statfs sb;
56 ASSERT_EQ(0, statfs("/proc", &sb));
57 Check(sb);
58}
59
Elliott Hughes7cebf832020-08-12 14:25:41 -070060TEST(sys_vfs, statfs_failure) {
61 struct statfs sb;
62 errno = 0;
63 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070064 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070065}
66
Colin Cross7da20342021-07-28 11:18:11 -070067TEST(sys_vfs, statfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080068 struct statfs64 sb;
69 ASSERT_EQ(0, statfs64("/proc", &sb));
70 Check(sb);
71}
72
Elliott Hughes7cebf832020-08-12 14:25:41 -070073TEST(sys_vfs, statfs64_failure) {
74 struct statfs64 sb;
75 errno = 0;
76 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070077 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070078}
79
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080080TEST(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}
Elliott Hughesfa495d52015-03-18 15:46:48 -070087
Elliott Hughes7cebf832020-08-12 14:25:41 -070088TEST(sys_vfs, fstatfs_failure) {
89 struct statfs sb;
90 errno = 0;
91 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070092 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -070093}
94
Colin Cross7da20342021-07-28 11:18:11 -070095TEST(sys_vfs, fstatfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080096 struct statfs64 sb;
97 int fd = open("/proc", O_RDONLY);
98 ASSERT_EQ(0, fstatfs64(fd, &sb));
99 close(fd);
100 Check(sb);
101}
Elliott Hughes7cebf832020-08-12 14:25:41 -0700102
103TEST(sys_vfs, fstatfs64_failure) {
104 struct statfs sb;
105 errno = 0;
106 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -0700107 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -0700108}