1 /*
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 
Check(StatFsT & sb)27 template <typename StatFsT> void Check(StatFsT& sb) {
28   EXPECT_EQ(4096, static_cast<int>(sb.f_bsize));
29   EXPECT_EQ(0U, sb.f_bfree);
30   EXPECT_EQ(0U, sb.f_ffree);
31   EXPECT_EQ(0, sb.f_fsid.__val[0]);
32   EXPECT_EQ(0, sb.f_fsid.__val[1]);
33   EXPECT_EQ(255, static_cast<int>(sb.f_namelen));
34 
35   // The kernel sets a private bit to indicate that f_flags is valid.
36   // This flag is not supposed to be exposed to libc clients.
37   static const uint32_t ST_VALID = 0x0020;
38   EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags;
39 }
40 
TEST(sys_vfs,statfs)41 TEST(sys_vfs, statfs) {
42   struct statfs sb;
43   ASSERT_EQ(0, statfs("/proc", &sb));
44   Check(sb);
45 }
46 
TEST(sys_vfs,statfs_failure)47 TEST(sys_vfs, statfs_failure) {
48   struct statfs sb;
49   errno = 0;
50   ASSERT_EQ(-1, statfs("/does-not-exist", &sb));
51   ASSERT_EQ(ENOENT, errno);
52 }
53 
TEST(sys_vfs,statfs64)54 TEST(sys_vfs, statfs64) {
55   struct statfs64 sb;
56   ASSERT_EQ(0, statfs64("/proc", &sb));
57   Check(sb);
58 }
59 
TEST(sys_vfs,statfs64_failure)60 TEST(sys_vfs, statfs64_failure) {
61   struct statfs64 sb;
62   errno = 0;
63   ASSERT_EQ(-1, statfs64("/does-not-exist", &sb));
64   ASSERT_EQ(ENOENT, errno);
65 }
66 
TEST(sys_vfs,fstatfs)67 TEST(sys_vfs, fstatfs) {
68   struct statfs sb;
69   int fd = open("/proc", O_RDONLY);
70   ASSERT_EQ(0, fstatfs(fd, &sb));
71   close(fd);
72   Check(sb);
73 }
74 
TEST(sys_vfs,fstatfs_failure)75 TEST(sys_vfs, fstatfs_failure) {
76   struct statfs sb;
77   errno = 0;
78   ASSERT_EQ(-1, fstatfs(-1, &sb));
79   ASSERT_EQ(EBADF, errno);
80 }
81 
TEST(sys_vfs,fstatfs64)82 TEST(sys_vfs, fstatfs64) {
83   struct statfs64 sb;
84   int fd = open("/proc", O_RDONLY);
85   ASSERT_EQ(0, fstatfs64(fd, &sb));
86   close(fd);
87   Check(sb);
88 }
89 
TEST(sys_vfs,fstatfs64_failure)90 TEST(sys_vfs, fstatfs64_failure) {
91   struct statfs sb;
92   errno = 0;
93   ASSERT_EQ(-1, fstatfs(-1, &sb));
94   ASSERT_EQ(EBADF, errno);
95 }
96