1 | #include "llvm-libc-macros/linux/fcntl-macros.h" |
2 | #include "src/fcntl/open.h" |
3 | #include "src/sys/statvfs/fstatvfs.h" |
4 | #include "src/sys/statvfs/linux/statfs_utils.h" |
5 | #include "src/unistd/close.h" |
6 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
7 | #include "test/UnitTest/LibcTest.h" |
8 | #include <linux/magic.h> |
9 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
10 | |
11 | namespace LIBC_NAMESPACE { |
12 | static int fstatfs(int fd, struct statfs *buf) { |
13 | using namespace statfs_utils; |
14 | if (cpp::optional<LinuxStatFs> result = linux_fstatfs(fd)) { |
15 | *buf = *result; |
16 | return 0; |
17 | } |
18 | return -1; |
19 | } |
20 | } // namespace LIBC_NAMESPACE |
21 | |
22 | struct PathFD { |
23 | int fd; |
24 | explicit PathFD(const char *path) |
25 | : fd(LIBC_NAMESPACE::open(path, O_CLOEXEC | O_PATH)) {} |
26 | ~PathFD() { LIBC_NAMESPACE::close(fd); } |
27 | operator int() const { return fd; } |
28 | }; |
29 | |
30 | TEST(LlvmLibcSysStatvfsTest, FstatfsBasic) { |
31 | struct statfs buf; |
32 | ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/" ), &buf), Succeeds()); |
33 | ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/proc" ), &buf), Succeeds()); |
34 | ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(PROC_SUPER_MAGIC)); |
35 | ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/sys" ), &buf), Succeeds()); |
36 | ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(SYSFS_MAGIC)); |
37 | } |
38 | |
39 | TEST(LlvmLibcSysStatvfsTest, FstatvfsInvalidFD) { |
40 | struct statvfs buf; |
41 | ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(-1, &buf), Fails(EBADF)); |
42 | } |
43 | |