1#include "src/sys/statvfs/linux/statfs_utils.h"
2#include "src/sys/statvfs/statvfs.h"
3#include "test/UnitTest/ErrnoSetterMatcher.h"
4#include "test/UnitTest/LibcTest.h"
5#include <linux/magic.h>
6using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
7
8namespace LIBC_NAMESPACE {
9static int statfs(const char *path, struct statfs *buf) {
10 using namespace statfs_utils;
11 if (cpp::optional<LinuxStatFs> result = linux_statfs(path)) {
12 *buf = *result;
13 return 0;
14 }
15 return -1;
16}
17} // namespace LIBC_NAMESPACE
18
19TEST(LlvmLibcSysStatfsTest, StatfsBasic) {
20 struct statfs buf;
21 ASSERT_THAT(LIBC_NAMESPACE::statfs("/", &buf), Succeeds());
22 ASSERT_THAT(LIBC_NAMESPACE::statfs("/proc", &buf), Succeeds());
23 ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(PROC_SUPER_MAGIC));
24 ASSERT_THAT(LIBC_NAMESPACE::statfs("/sys", &buf), Succeeds());
25 ASSERT_EQ(buf.f_type, static_cast<decltype(buf.f_type)>(SYSFS_MAGIC));
26}
27
28TEST(LlvmLibcSysStatfsTest, StatvfsInvalidPath) {
29 struct statvfs buf;
30 ASSERT_THAT(LIBC_NAMESPACE::statvfs("", &buf), Fails(ENOENT));
31 ASSERT_THAT(LIBC_NAMESPACE::statvfs("/nonexistent", &buf), Fails(ENOENT));
32 ASSERT_THAT(LIBC_NAMESPACE::statvfs("/dev/null/whatever", &buf),
33 Fails(ENOTDIR));
34 ASSERT_THAT(LIBC_NAMESPACE::statvfs(nullptr, &buf), Fails(EFAULT));
35}
36
37TEST(LlvmLibcSysStatfsTest, StatvfsNameTooLong) {
38 struct statvfs buf;
39 ASSERT_THAT(LIBC_NAMESPACE::statvfs("/", &buf), Succeeds());
40 char *name = static_cast<char *>(__builtin_alloca(buf.f_namemax + 3));
41 name[0] = '/';
42 name[buf.f_namemax + 2] = '\0';
43 for (unsigned i = 1; i < buf.f_namemax + 2; ++i) {
44 name[i] = 'a';
45 }
46 ASSERT_THAT(LIBC_NAMESPACE::statvfs(name, &buf), Fails(ENAMETOOLONG));
47}
48

source code of libc/test/src/sys/statvfs/linux/statvfs_test.cpp