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); |
| 33 | EXPECT_EQ(0, sb.f_fsid.__val[0]); |
| 34 | EXPECT_EQ(0, sb.f_fsid.__val[1]); |
| 35 | EXPECT_EQ(255, static_cast<int>(sb.f_namelen)); |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 36 | |
| 37 | // The kernel sets a private bit to indicate that f_flags is valid. |
| 38 | // This flag is not supposed to be exposed to libc clients. |
| 39 | static const uint32_t ST_VALID = 0x0020; |
| 40 | EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags; |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST(sys_vfs, statfs) { |
| 44 | struct statfs sb; |
| 45 | ASSERT_EQ(0, statfs("/proc", &sb)); |
| 46 | Check(sb); |
| 47 | } |
| 48 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 49 | TEST(sys_vfs, statfs_failure) { |
| 50 | struct statfs sb; |
| 51 | errno = 0; |
| 52 | ASSERT_EQ(-1, statfs("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 53 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 56 | TEST(sys_vfs, statfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 57 | struct statfs64 sb; |
| 58 | ASSERT_EQ(0, statfs64("/proc", &sb)); |
| 59 | Check(sb); |
| 60 | } |
| 61 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 62 | TEST(sys_vfs, statfs64_failure) { |
| 63 | struct statfs64 sb; |
| 64 | errno = 0; |
| 65 | ASSERT_EQ(-1, statfs64("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 66 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 69 | TEST(sys_vfs, fstatfs) { |
| 70 | struct statfs sb; |
| 71 | int fd = open("/proc", O_RDONLY); |
| 72 | ASSERT_EQ(0, fstatfs(fd, &sb)); |
| 73 | close(fd); |
| 74 | Check(sb); |
| 75 | } |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 77 | TEST(sys_vfs, fstatfs_failure) { |
| 78 | struct statfs sb; |
| 79 | errno = 0; |
| 80 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 81 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 84 | TEST(sys_vfs, fstatfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 85 | struct statfs64 sb; |
| 86 | int fd = open("/proc", O_RDONLY); |
| 87 | ASSERT_EQ(0, fstatfs64(fd, &sb)); |
| 88 | close(fd); |
| 89 | Check(sb); |
| 90 | } |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 91 | |
| 92 | TEST(sys_vfs, fstatfs64_failure) { |
| 93 | struct statfs sb; |
| 94 | errno = 0; |
| 95 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 96 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 97 | } |