blob: 5dd7b937a22ba4bfda1e5b35d83a07122a68b891 [file] [log] [blame]
Elliott Hughes06040fd2013-07-09 13:25:03 -07001/*
2 * Copyright (C) 2013 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/statvfs.h>
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
Elliott Hughes3d305f12013-10-21 19:27:19 -070025#include <string>
26
Elliott Hughesdb1ea342014-01-17 18:42:49 -080027template <typename StatVfsT> void Check(StatVfsT& sb) {
Vilas Bhat297e7dd2024-05-01 01:11:30 +000028 EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize));
Elliott Hughes3d305f12013-10-21 19:27:19 -070029 EXPECT_EQ(0U, sb.f_bfree);
30 EXPECT_EQ(0U, sb.f_ffree);
Elliott Hughes3d305f12013-10-21 19:27:19 -070031 EXPECT_EQ(255U, sb.f_namemax);
Elliott Hughesfa495d52015-03-18 15:46:48 -070032
Elliott Hughes7506c372024-01-11 20:46:23 +000033 // Linux 6.7 requires that all filesystems have a non-zero fsid.
34 if (sb.f_fsid != 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(proc_sb.st_dev, sb.f_fsid);
39 } else {
40 // Prior to that, the fsid for /proc was just 0.
41 EXPECT_EQ(0U, sb.f_fsid);
42 }
43
Elliott Hughesfa495d52015-03-18 15:46:48 -070044 // The kernel sets a private bit to indicate that f_flags is valid.
45 // This flag is not supposed to be exposed to libc clients.
46 static const uint32_t ST_VALID = 0x0020;
47 EXPECT_TRUE((sb.f_flag & ST_VALID) == 0) << sb.f_flag;
Elliott Hughes06040fd2013-07-09 13:25:03 -070048}
49
Elliott Hughesdb1ea342014-01-17 18:42:49 -080050TEST(sys_statvfs, statvfs) {
Elliott Hughes06040fd2013-07-09 13:25:03 -070051 struct statvfs sb;
Elliott Hughesdb1ea342014-01-17 18:42:49 -080052 ASSERT_EQ(0, statvfs("/proc", &sb));
53 Check(sb);
54}
Elliott Hughes06040fd2013-07-09 13:25:03 -070055
Colin Cross7da20342021-07-28 11:18:11 -070056TEST(sys_statvfs, statvfs64_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -080057 struct statvfs64 sb;
58 ASSERT_EQ(0, statvfs64("/proc", &sb));
59 Check(sb);
60}
61
62TEST(sys_statvfs, fstatvfs) {
63 struct statvfs sb;
Elliott Hughes3d305f12013-10-21 19:27:19 -070064 int fd = open("/proc", O_RDONLY);
Elliott Hughes06040fd2013-07-09 13:25:03 -070065 ASSERT_EQ(0, fstatvfs(fd, &sb));
66 close(fd);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080067 Check(sb);
68}
Elliott Hughesfa495d52015-03-18 15:46:48 -070069
Colin Cross7da20342021-07-28 11:18:11 -070070TEST(sys_statvfs, fstatvfs64_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -080071 struct statvfs64 sb;
72 int fd = open("/proc", O_RDONLY);
73 ASSERT_EQ(0, fstatvfs64(fd, &sb));
74 close(fd);
75 Check(sb);
Elliott Hughes06040fd2013-07-09 13:25:03 -070076}