forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Refactor statvfs tests (#114147)
The previous statvfs tests had several issues, this patch updates them to meet current standards.
- Loading branch information
1 parent
56dcfbe
commit 5d35747
Showing
3 changed files
with
80 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,56 @@ | ||
//===-- Unittests for fstatvfs --------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "hdr/fcntl_macros.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/fcntl/open.h" | ||
#include "src/sys/stat/mkdirat.h" | ||
#include "src/sys/statvfs/fstatvfs.h" | ||
#include "src/sys/statvfs/linux/statfs_utils.h" | ||
#include "src/unistd/close.h" | ||
#include "src/unistd/rmdir.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/LibcTest.h" | ||
#include <linux/magic.h> | ||
#include "test/UnitTest/Test.h" | ||
|
||
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
||
#ifdef SYS_statfs64 | ||
using StatFs = statfs64; | ||
#else | ||
using StatFs = statfs; | ||
#endif | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
static int fstatfs(int fd, StatFs *buf) { | ||
using namespace statfs_utils; | ||
if (cpp::optional<StatFs> result = linux_fstatfs(fd)) { | ||
*buf = *result; | ||
return 0; | ||
} | ||
return -1; | ||
} | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
struct PathFD { | ||
int fd; | ||
explicit PathFD(const char *path) | ||
: fd(LIBC_NAMESPACE::open(path, O_CLOEXEC | O_PATH)) {} | ||
~PathFD() { LIBC_NAMESPACE::close(fd); } | ||
operator int() const { return fd; } | ||
}; | ||
|
||
TEST(LlvmLibcSysStatvfsTest, FstatfsBasic) { | ||
StatFs buf; | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/"), &buf), Succeeds()); | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/proc"), &buf), Succeeds()); | ||
ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(PROC_SUPER_MAGIC)); | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatfs(PathFD("/sys"), &buf), Succeeds()); | ||
ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(SYSFS_MAGIC)); | ||
TEST(LlvmLibcSysFStatvfsTest, FStatvfsBasic) { | ||
struct statvfs buf; | ||
|
||
int fd = LIBC_NAMESPACE::open("/", O_PATH); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
|
||
// The root of the file directory must always exist | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
} | ||
|
||
TEST(LlvmLibcSysStatvfsTest, FstatvfsInvalidFD) { | ||
TEST(LlvmLibcSysFStatvfsTest, FStatvfsInvalidPath) { | ||
struct statvfs buf; | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(-1, &buf), Fails(EBADF)); | ||
|
||
constexpr const char *FILENAME = "testdata/statvfs.testdir"; | ||
auto TEST_DIR = libc_make_test_file_path(FILENAME); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), | ||
Succeeds(0)); | ||
|
||
int fd = LIBC_NAMESPACE::open(TEST_DIR, O_PATH); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
|
||
// create the file, assert it exists, then delete it and assert it doesn't | ||
// exist anymore. | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Succeeds()); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(ENOENT)); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
ASSERT_THAT(LIBC_NAMESPACE::fstatvfs(fd, &buf), Fails(ENOENT)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,43 @@ | ||
//===-- Unittests for statvfs ---------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "hdr/fcntl_macros.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/sys/statvfs/linux/statfs_utils.h" | ||
#include "src/sys/stat/mkdirat.h" | ||
#include "src/sys/statvfs/statvfs.h" | ||
#include "src/unistd/rmdir.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/LibcTest.h" | ||
#include <linux/magic.h> | ||
#include "test/UnitTest/Test.h" | ||
|
||
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
||
#ifdef SYS_statfs64 | ||
using StatFs = statfs64; | ||
#else | ||
using StatFs = statfs; | ||
#endif | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
static int statfs(const char *path, StatFs *buf) { | ||
using namespace statfs_utils; | ||
if (cpp::optional<LinuxStatFs> result = linux_statfs(path)) { | ||
*buf = *result; | ||
return 0; | ||
} | ||
return -1; | ||
} | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
TEST(LlvmLibcSysStatfsTest, StatfsBasic) { | ||
StatFs buf; | ||
ASSERT_THAT(LIBC_NAMESPACE::statfs("/", &buf), Succeeds()); | ||
ASSERT_THAT(LIBC_NAMESPACE::statfs("/proc", &buf), Succeeds()); | ||
ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(PROC_SUPER_MAGIC)); | ||
ASSERT_THAT(LIBC_NAMESPACE::statfs("/sys", &buf), Succeeds()); | ||
ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(SYSFS_MAGIC)); | ||
TEST(LlvmLibcSysStatvfsTest, StatvfsBasic) { | ||
struct statvfs buf; | ||
// The root of the file directory must always exist | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs("/", &buf), Succeeds()); | ||
} | ||
|
||
TEST(LlvmLibcSysStatfsTest, StatvfsInvalidPath) { | ||
TEST(LlvmLibcSysStatvfsTest, StatvfsInvalidPath) { | ||
struct statvfs buf; | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::statvfs("", &buf), Fails(ENOENT)); | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs("/nonexistent", &buf), Fails(ENOENT)); | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs("/dev/null/whatever", &buf), | ||
Fails(ENOTDIR)); | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs(nullptr, &buf), Fails(EFAULT)); | ||
} | ||
|
||
TEST(LlvmLibcSysStatfsTest, StatvfsNameTooLong) { | ||
struct statvfs buf; | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs("/", &buf), Succeeds()); | ||
char *name = static_cast<char *>(__builtin_alloca(buf.f_namemax + 3)); | ||
name[0] = '/'; | ||
name[buf.f_namemax + 2] = '\0'; | ||
for (unsigned i = 1; i < buf.f_namemax + 2; ++i) { | ||
name[i] = 'a'; | ||
} | ||
ASSERT_THAT(LIBC_NAMESPACE::statvfs(name, &buf), Fails(ENAMETOOLONG)); | ||
// create the file, assert it exists, then delete it and assert it doesn't | ||
// exist anymore. | ||
constexpr const char *FILENAME = "testdata/statvfs.testdir"; | ||
auto TEST_DIR = libc_make_test_file_path(FILENAME); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), | ||
Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::statvfs(TEST_DIR, &buf), Succeeds()); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::rmdir(TEST_DIR), Succeeds(0)); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::statvfs(TEST_DIR, &buf), Fails(ENOENT)); | ||
} |