github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/statfs.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <fcntl.h> 16 #include <linux/magic.h> 17 #include <sys/statfs.h> 18 #include <unistd.h> 19 20 #include "gtest/gtest.h" 21 #include "test/util/file_descriptor.h" 22 #include "test/util/temp_path.h" 23 #include "test/util/test_util.h" 24 25 namespace gvisor { 26 namespace testing { 27 28 namespace { 29 30 TEST(StatfsTest, CannotStatBadPath) { 31 auto temp_file = NewTempAbsPathInDir("/tmp"); 32 33 struct statfs st; 34 EXPECT_THAT(statfs(temp_file.c_str(), &st), SyscallFailsWithErrno(ENOENT)); 35 } 36 37 TEST(StatfsTest, InternalTmpfs) { 38 auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 39 40 struct statfs st; 41 EXPECT_THAT(statfs(temp_file.path().c_str(), &st), SyscallSucceeds()); 42 } 43 44 TEST(StatfsTest, InternalDevShm) { 45 struct statfs st; 46 EXPECT_THAT(statfs("/dev/shm", &st), SyscallSucceeds()); 47 48 // This assumes that /dev/shm is tmpfs. 49 // Note: We could be an overlay on some configurations. 50 EXPECT_TRUE(st.f_type == TMPFS_MAGIC || st.f_type == OVERLAYFS_SUPER_MAGIC); 51 } 52 53 TEST(FstatfsTest, CannotStatBadFd) { 54 struct statfs st; 55 EXPECT_THAT(fstatfs(-1, &st), SyscallFailsWithErrno(EBADF)); 56 } 57 58 TEST(FstatfsTest, InternalTmpfs) { 59 auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 60 const FileDescriptor fd = 61 ASSERT_NO_ERRNO_AND_VALUE(Open(temp_file.path(), O_RDONLY)); 62 63 struct statfs st; 64 EXPECT_THAT(fstatfs(fd.get(), &st), SyscallSucceeds()); 65 } 66 67 TEST(FstatfsTest, CanStatFileWithOpath) { 68 SKIP_IF(IsRunningWithVFS1()); 69 auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 70 const FileDescriptor fd = 71 ASSERT_NO_ERRNO_AND_VALUE(Open(temp_file.path(), O_PATH)); 72 73 struct statfs st; 74 EXPECT_THAT(fstatfs(fd.get(), &st), SyscallSucceeds()); 75 } 76 77 TEST(FstatfsTest, InternalDevShm) { 78 auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 79 const FileDescriptor fd = 80 ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/shm", O_RDONLY)); 81 82 struct statfs st; 83 EXPECT_THAT(fstatfs(fd.get(), &st), SyscallSucceeds()); 84 } 85 86 } // namespace 87 88 } // namespace testing 89 } // namespace gvisor