xref: /llvm-project/libc/test/src/sys/statvfs/linux/statvfs_test.cpp (revision d6219e65996a485adb3883c8cf3335ece68c66cf)
15d35747fSMichael Jones //===-- Unittests for statvfs ---------------------------------------------===//
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 
95d35747fSMichael Jones #include "hdr/fcntl_macros.h"
105ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
111ae0dae3SMichael Jones #include "src/errno/libc_errno.h"
125d35747fSMichael Jones #include "src/sys/stat/mkdirat.h"
1305dc5d92SSchrodinger ZHU Yifan #include "src/sys/statvfs/statvfs.h"
145d35747fSMichael Jones #include "src/unistd/rmdir.h"
1505dc5d92SSchrodinger ZHU Yifan #include "test/UnitTest/ErrnoSetterMatcher.h"
165d35747fSMichael Jones #include "test/UnitTest/Test.h"
175d35747fSMichael Jones 
1805dc5d92SSchrodinger ZHU Yifan using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
1905dc5d92SSchrodinger ZHU Yifan 
205d35747fSMichael Jones TEST(LlvmLibcSysStatvfsTest, StatvfsBasic) {
2105dc5d92SSchrodinger ZHU Yifan   struct statvfs buf;
225d35747fSMichael Jones   // The root of the file directory must always exist
2305dc5d92SSchrodinger ZHU Yifan   ASSERT_THAT(LIBC_NAMESPACE::statvfs("/", &buf), Succeeds());
2405dc5d92SSchrodinger ZHU Yifan }
255d35747fSMichael Jones 
265d35747fSMichael Jones TEST(LlvmLibcSysStatvfsTest, StatvfsInvalidPath) {
275d35747fSMichael Jones   struct statvfs buf;
285d35747fSMichael Jones 
295d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::statvfs("", &buf), Fails(ENOENT));
305d35747fSMichael Jones 
315d35747fSMichael Jones   // create the file, assert it exists, then delete it and assert it doesn't
325d35747fSMichael Jones   // exist anymore.
331ae0dae3SMichael Jones   constexpr const char *FILENAME = "statvfs.testdir";
345d35747fSMichael Jones   auto TEST_DIR = libc_make_test_file_path(FILENAME);
355d35747fSMichael Jones 
36*d6219e65SMichael Jones   // Always delete the folder so that we start in a consistent state.
37*d6219e65SMichael Jones   LIBC_NAMESPACE::rmdir(TEST_DIR);
38*d6219e65SMichael Jones   LIBC_NAMESPACE::libc_errno = 0; // Reset errno
39*d6219e65SMichael Jones 
405d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU),
415d35747fSMichael Jones               Succeeds(0));
425d35747fSMichael Jones 
435d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::statvfs(TEST_DIR, &buf), Succeeds());
445d35747fSMichael Jones 
455d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0));
465d35747fSMichael Jones 
475d35747fSMichael Jones   ASSERT_THAT(LIBC_NAMESPACE::statvfs(TEST_DIR, &buf), Fails(ENOENT));
4805dc5d92SSchrodinger ZHU Yifan }
49