|
| 1 | +//===-- Unittests for fstatvfs --------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
1 | 9 | #include "hdr/fcntl_macros.h" |
2 | 10 | #include "src/__support/macros/config.h" |
3 | 11 | #include "src/fcntl/open.h" |
| 12 | +#include "src/sys/stat/mkdirat.h" |
4 | 13 | #include "src/sys/statvfs/fstatvfs.h" |
5 | | -#include "src/sys/statvfs/linux/statfs_utils.h" |
6 | 14 | #include "src/unistd/close.h" |
| 15 | +#include "src/unistd/rmdir.h" |
7 | 16 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
8 | | -#include "test/UnitTest/LibcTest.h" |
9 | | -#include <linux/magic.h> |
| 17 | +#include "test/UnitTest/Test.h" |
| 18 | + |
10 | 19 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
11 | 20 |
|
12 | | -#ifdef SYS_statfs64 |
13 | | -using StatFs = statfs64; |
14 | | -#else |
15 | | -using StatFs = statfs; |
16 | | -#endif |
17 | | - |
18 | | -namespace LIBC_NAMESPACE_DECL { |
19 | | -static int fstatfs(int fd, StatFs *buf) { |
20 | | - using namespace statfs_utils; |
21 | | - if (cpp::optional<StatFs> result = linux_fstatfs(fd)) { |
22 | | - *buf = *result; |
23 | | - return 0; |
24 | | - } |
25 | | - return -1; |
26 | | -} |
27 | | -} // namespace LIBC_NAMESPACE_DECL |
28 | | - |
29 | | -struct PathFD { |
30 | | - int fd; |
31 | | - explicit PathFD(const char *path) |
32 | | - : fd(LIBC_NAMESPACE::open(path, O_CLOEXEC | O_PATH)) {} |
33 | | - ~PathFD() { LIBC_NAMESPACE::close(fd); } |
34 | | - operator int() const { return fd; } |
35 | | -}; |
36 | | - |
37 | | -TEST(LlvmLibcSysStatvfsTest, FstatfsBasic) { |
38 | | - StatFs buf; |
39 | | - ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/"), &buf), Succeeds()); |
40 | | - ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/proc"), &buf), Succeeds()); |
41 | | - ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(PROC_SUPER_MAGIC)); |
42 | | - ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/sys"), &buf), Succeeds()); |
43 | | - ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(SYSFS_MAGIC)); |
| 21 | +TEST(LlvmLibcSysFStatvfsTest, FStatvfsBasic) { |
| 22 | + struct statvfs buf; |
| 23 | + |
| 24 | + int fd = LIBC_NAMESPACE::open("/", O_PATH); |
| 25 | + ASSERT_ERRNO_SUCCESS(); |
| 26 | + ASSERT_GT(fd, 0); |
| 27 | + |
| 28 | + // The root of the file directory must always exist |
| 29 | + ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); |
| 30 | + ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
44 | 31 | } |
45 | 32 |
|
46 | | -TEST(LlvmLibcSysStatvfsTest, FstatvfsInvalidFD) { |
| 33 | +TEST(LlvmLibcSysFStatvfsTest, FStatvfsInvalidPath) { |
47 | 34 | struct statvfs buf; |
48 | | - ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(-1, &buf), Fails(EBADF)); |
| 35 | + |
| 36 | + constexpr const char *FILENAME = "testdata/statvfs.testdir"; |
| 37 | + auto TEST_DIR = libc_make_test_file_path(FILENAME); |
| 38 | + |
| 39 | + ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), |
| 40 | + Succeeds(0)); |
| 41 | + |
| 42 | + int fd = LIBC_NAMESPACE::open(TEST_DIR, O_PATH); |
| 43 | + ASSERT_ERRNO_SUCCESS(); |
| 44 | + ASSERT_GT(fd, 0); |
| 45 | + |
| 46 | + // create the file, assert it exists, then delete it and assert it doesn't |
| 47 | + // exist anymore. |
| 48 | + |
| 49 | + ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); |
| 50 | + |
| 51 | + ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0)); |
| 52 | + |
| 53 | + ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(ENOENT)); |
| 54 | + ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
| 55 | + ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(ENOENT)); |
49 | 56 | } |
0 commit comments