| 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 | |
| 9 | #include "hdr/fcntl_macros.h" |
| 10 | #include "src/__support/libc_errno.h" |
| 11 | #include "src/__support/macros/config.h" |
| 12 | #include "src/fcntl/open.h" |
| 13 | #include "src/sys/stat/mkdirat.h" |
| 14 | #include "src/sys/statvfs/fstatvfs.h" |
| 15 | #include "src/unistd/close.h" |
| 16 | #include "src/unistd/rmdir.h" |
| 17 | #include "test/UnitTest/ErrnoCheckingTest.h" |
| 18 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
| 19 | #include "test/UnitTest/Test.h" |
| 20 | |
| 21 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
| 22 | using LlvmLibcSysFStatvfsTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 23 | |
| 24 | TEST_F(LlvmLibcSysFStatvfsTest, FStatvfsBasic) { |
| 25 | struct statvfs buf; |
| 26 | |
| 27 | int fd = LIBC_NAMESPACE::open("/" , O_PATH); |
| 28 | ASSERT_ERRNO_SUCCESS(); |
| 29 | ASSERT_GT(fd, 0); |
| 30 | |
| 31 | // The root of the file directory must always exist |
| 32 | ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); |
| 33 | ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
| 34 | } |
| 35 | |
| 36 | TEST_F(LlvmLibcSysFStatvfsTest, FStatvfsInvalidPath) { |
| 37 | struct statvfs buf; |
| 38 | |
| 39 | constexpr const char *FILENAME = "fstatvfs.testdir" ; |
| 40 | auto TEST_DIR = libc_make_test_file_path(FILENAME); |
| 41 | |
| 42 | // Always delete the folder so that we start in a consistent state. |
| 43 | LIBC_NAMESPACE::rmdir(TEST_DIR); |
| 44 | libc_errno = 0; // Reset errno |
| 45 | |
| 46 | ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), |
| 47 | Succeeds(0)); |
| 48 | |
| 49 | int fd = LIBC_NAMESPACE::open(TEST_DIR, O_PATH); |
| 50 | ASSERT_ERRNO_SUCCESS(); |
| 51 | ASSERT_GT(fd, 0); |
| 52 | |
| 53 | // create the file, assert it exists, then delete it and assert it doesn't |
| 54 | // exist anymore. |
| 55 | |
| 56 | ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); |
| 57 | ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
| 58 | |
| 59 | ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0)); |
| 60 | |
| 61 | ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(EBADF)); |
| 62 | } |
| 63 | |