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 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 27 | #include "utils.h" |
| 28 | |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 29 | template <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 Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 33 | EXPECT_EQ(255, static_cast<int>(sb.f_namelen)); |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 1b48afb | 2024-01-25 10:12:37 -0800 | [diff] [blame^] | 35 | // 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 Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 48 | // 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 Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(sys_vfs, statfs) { |
| 55 | struct statfs sb; |
| 56 | ASSERT_EQ(0, statfs("/proc", &sb)); |
| 57 | Check(sb); |
| 58 | } |
| 59 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 60 | TEST(sys_vfs, statfs_failure) { |
| 61 | struct statfs sb; |
| 62 | errno = 0; |
| 63 | ASSERT_EQ(-1, statfs("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 64 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 67 | TEST(sys_vfs, statfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 68 | struct statfs64 sb; |
| 69 | ASSERT_EQ(0, statfs64("/proc", &sb)); |
| 70 | Check(sb); |
| 71 | } |
| 72 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 73 | TEST(sys_vfs, statfs64_failure) { |
| 74 | struct statfs64 sb; |
| 75 | errno = 0; |
| 76 | ASSERT_EQ(-1, statfs64("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 77 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 80 | TEST(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 Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 88 | TEST(sys_vfs, fstatfs_failure) { |
| 89 | struct statfs sb; |
| 90 | errno = 0; |
| 91 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 92 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 95 | TEST(sys_vfs, fstatfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 96 | 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 Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 102 | |
| 103 | TEST(sys_vfs, fstatfs64_failure) { |
| 104 | struct statfs sb; |
| 105 | errno = 0; |
| 106 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 107 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 108 | } |