blob: 90b6da9c5e3080d6184bce8ba61cf7e161ab9eb9 [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) {
Vilas Bhat4c87f0d2024-08-16 00:03:31 +000030#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
Vilas Bhat297e7dd2024-05-01 01:11:30 +000037 EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize));
Vilas Bhat4c87f0d2024-08-16 00:03:31 +000038#endif
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080039 EXPECT_EQ(0U, sb.f_bfree);
40 EXPECT_EQ(0U, sb.f_ffree);
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080041 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
Elliott Hughesfa495d52015-03-18 15:46:48 -070042
Elliott Hughes1b48afb2024-01-25 10:12:37 -080043 // 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
Elliott Hughesfa495d52015-03-18 15:46:48 -070056 // 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;
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080060}
61
62TEST(sys_vfs, statfs) {
63 struct statfs sb;
64 ASSERT_EQ(0, statfs("/proc", &sb));
65 Check(sb);
66}
67
Elliott Hughes7cebf832020-08-12 14:25:41 -070068TEST(sys_vfs, statfs_failure) {
69 struct statfs sb;
70 errno = 0;
71 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070072 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070073}
74
Colin Cross7da20342021-07-28 11:18:11 -070075TEST(sys_vfs, statfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080076 struct statfs64 sb;
77 ASSERT_EQ(0, statfs64("/proc", &sb));
78 Check(sb);
79}
80
Elliott Hughes7cebf832020-08-12 14:25:41 -070081TEST(sys_vfs, statfs64_failure) {
82 struct statfs64 sb;
83 errno = 0;
84 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070085 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070086}
87
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080088TEST(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}
Elliott Hughesfa495d52015-03-18 15:46:48 -070095
Elliott Hughes7cebf832020-08-12 14:25:41 -070096TEST(sys_vfs, fstatfs_failure) {
97 struct statfs sb;
98 errno = 0;
99 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -0700100 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -0700101}
102
Colin Cross7da20342021-07-28 11:18:11 -0700103TEST(sys_vfs, fstatfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -0800104 struct statfs64 sb;
105 int fd = open("/proc", O_RDONLY);
106 ASSERT_EQ(0, fstatfs64(fd, &sb));
107 close(fd);
108 Check(sb);
109}
Elliott Hughes7cebf832020-08-12 14:25:41 -0700110
111TEST(sys_vfs, fstatfs64_failure) {
112 struct statfs sb;
113 errno = 0;
114 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -0700115 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -0700116}