xref: /llvm-project/libc/test/src/sys/statvfs/linux/fstatvfs_test.cpp (revision d6219e65996a485adb3883c8cf3335ece68c66cf)
15d35747fSMichael Jones //===-- Unittests for fstatvfs --------------------------------------------===//
25d35747fSMichael Jones //
35d35747fSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45d35747fSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
55d35747fSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65d35747fSMichael Jones //
75d35747fSMichael Jones //===----------------------------------------------------------------------===//
85d35747fSMichael Jones 
97b663bd9SJob Henandez Lara #include "hdr/fcntl_macros.h"
105ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1105dc5d92SSchrodinger ZHU Yifan #include "src/fcntl/open.h"
125d35747fSMichael Jones #include "src/sys/stat/mkdirat.h"
1305dc5d92SSchrodinger ZHU Yifan #include "src/sys/statvfs/fstatvfs.h"
1405dc5d92SSchrodinger ZHU Yifan #include "src/unistd/close.h"
155d35747fSMichael Jones #include "src/unistd/rmdir.h"
1605dc5d92SSchrodinger ZHU Yifan #include "test/UnitTest/ErrnoSetterMatcher.h"
175d35747fSMichael Jones #include "test/UnitTest/Test.h"
185d35747fSMichael Jones 
1905dc5d92SSchrodinger ZHU Yifan using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
2005dc5d92SSchrodinger ZHU Yifan 
215d35747fSMichael Jones TEST(LlvmLibcSysFStatvfsTest, FStatvfsBasic) {
2205dc5d92SSchrodinger ZHU Yifan   struct statvfs buf;
235d35747fSMichael Jones 
245d35747fSMichael Jones   int fd = LIBC_NAMESPACE::open("/", O_PATH);
255d35747fSMichael Jones   ASSERT_ERRNO_SUCCESS();
265d35747fSMichael Jones   ASSERT_GT(fd, 0);
275d35747fSMichael Jones 
285d35747fSMichael Jones   // The root of the file directory must always exist
295d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds());
305d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
315d35747fSMichael Jones }
325d35747fSMichael Jones 
335d35747fSMichael Jones TEST(LlvmLibcSysFStatvfsTest, FStatvfsInvalidPath) {
345d35747fSMichael Jones   struct statvfs buf;
355d35747fSMichael Jones 
36*d6219e65SMichael Jones   constexpr const char *FILENAME = "fstatvfs.testdir";
375d35747fSMichael Jones   auto TEST_DIR = libc_make_test_file_path(FILENAME);
385d35747fSMichael Jones 
39*d6219e65SMichael Jones   // Always delete the folder so that we start in a consistent state.
40*d6219e65SMichael Jones   LIBC_NAMESPACE::rmdir(TEST_DIR);
41*d6219e65SMichael Jones   LIBC_NAMESPACE::libc_errno = 0; // Reset errno
42*d6219e65SMichael Jones 
435d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU),
445d35747fSMichael Jones               Succeeds(0));
455d35747fSMichael Jones 
465d35747fSMichael Jones   int fd = LIBC_NAMESPACE::open(TEST_DIR, O_PATH);
475d35747fSMichael Jones   ASSERT_ERRNO_SUCCESS();
485d35747fSMichael Jones   ASSERT_GT(fd, 0);
495d35747fSMichael Jones 
505d35747fSMichael Jones   // create the file, assert it exists, then delete it and assert it doesn't
515d35747fSMichael Jones   // exist anymore.
525d35747fSMichael Jones 
535d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds());
541ae0dae3SMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
555d35747fSMichael Jones 
565d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0));
575d35747fSMichael Jones 
581ae0dae3SMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(EBADF));
5905dc5d92SSchrodinger ZHU Yifan }
60