| Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 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 | template <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 Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 31 | EXPECT_EQ(255, static_cast<int>(sb.f_namelen)); |
| Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 32 | |
| Elliott Hughes | 3e7e225 | 2024-01-25 10:12:37 -0800 | [diff] [blame] | 33 | // 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 Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 46 | // 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 Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(sys_vfs, statfs) { |
| 53 | struct statfs sb; |
| 54 | ASSERT_EQ(0, statfs("/proc", &sb)); |
| 55 | Check(sb); |
| 56 | } |
| 57 | |
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 58 | TEST(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 Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 65 | TEST(sys_vfs, statfs64_smoke) { |
| Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 66 | struct statfs64 sb; |
| 67 | ASSERT_EQ(0, statfs64("/proc", &sb)); |
| 68 | Check(sb); |
| 69 | } |
| 70 | |
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 71 | TEST(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 Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 78 | TEST(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 Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 85 | |
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 86 | TEST(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 Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 93 | TEST(sys_vfs, fstatfs64_smoke) { |
| Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 94 | 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 Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 100 | |
| 101 | TEST(sys_vfs, fstatfs64_failure) { |
| 102 | struct statfs sb; |
| 103 | errno = 0; |
| 104 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
| 105 | ASSERT_EQ(EBADF, errno); |
| 106 | } |