blob: 2e116a2a5f5401c821081a27da37614533f0d195 [file] [log] [blame]
Elliott Hughes1d18e9c2014-02-18 15:48:56 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <sys/vfs.h>
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
25#include <string>
26
Elliott Hughes95646e62023-09-21 14:11:19 -070027#include "utils.h"
28
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080029template <typename StatFsT> void Check(StatFsT& sb) {
30 EXPECT_EQ(4096, static_cast<int>(sb.f_bsize));
31 EXPECT_EQ(0U, sb.f_bfree);
32 EXPECT_EQ(0U, sb.f_ffree);
33 EXPECT_EQ(0, sb.f_fsid.__val[0]);
34 EXPECT_EQ(0, sb.f_fsid.__val[1]);
35 EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
Elliott Hughesfa495d52015-03-18 15:46:48 -070036
37 // The kernel sets a private bit to indicate that f_flags is valid.
38 // This flag is not supposed to be exposed to libc clients.
39 static const uint32_t ST_VALID = 0x0020;
40 EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags;
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080041}
42
43TEST(sys_vfs, statfs) {
44 struct statfs sb;
45 ASSERT_EQ(0, statfs("/proc", &sb));
46 Check(sb);
47}
48
Elliott Hughes7cebf832020-08-12 14:25:41 -070049TEST(sys_vfs, statfs_failure) {
50 struct statfs sb;
51 errno = 0;
52 ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070053 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070054}
55
Colin Cross7da20342021-07-28 11:18:11 -070056TEST(sys_vfs, statfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080057 struct statfs64 sb;
58 ASSERT_EQ(0, statfs64("/proc", &sb));
59 Check(sb);
60}
61
Elliott Hughes7cebf832020-08-12 14:25:41 -070062TEST(sys_vfs, statfs64_failure) {
63 struct statfs64 sb;
64 errno = 0;
65 ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070066 ASSERT_ERRNO(ENOENT);
Elliott Hughes7cebf832020-08-12 14:25:41 -070067}
68
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080069TEST(sys_vfs, fstatfs) {
70 struct statfs sb;
71 int fd = open("/proc", O_RDONLY);
72 ASSERT_EQ(0, fstatfs(fd, &sb));
73 close(fd);
74 Check(sb);
75}
Elliott Hughesfa495d52015-03-18 15:46:48 -070076
Elliott Hughes7cebf832020-08-12 14:25:41 -070077TEST(sys_vfs, fstatfs_failure) {
78 struct statfs sb;
79 errno = 0;
80 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070081 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -070082}
83
Colin Cross7da20342021-07-28 11:18:11 -070084TEST(sys_vfs, fstatfs64_smoke) {
Elliott Hughes1d18e9c2014-02-18 15:48:56 -080085 struct statfs64 sb;
86 int fd = open("/proc", O_RDONLY);
87 ASSERT_EQ(0, fstatfs64(fd, &sb));
88 close(fd);
89 Check(sb);
90}
Elliott Hughes7cebf832020-08-12 14:25:41 -070091
92TEST(sys_vfs, fstatfs64_failure) {
93 struct statfs sb;
94 errno = 0;
95 ASSERT_EQ(-1, fstatfs(-1, &sb));
Elliott Hughes95646e62023-09-21 14:11:19 -070096 ASSERT_ERRNO(EBADF);
Elliott Hughes7cebf832020-08-12 14:25:41 -070097}