github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/readahead.cc (about) 1 // Copyright 2019 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 <errno.h> 16 #include <fcntl.h> 17 18 #include "gtest/gtest.h" 19 #include "test/syscalls/linux/socket_test_util.h" 20 #include "test/util/file_descriptor.h" 21 #include "test/util/temp_path.h" 22 #include "test/util/test_util.h" 23 24 namespace gvisor { 25 namespace testing { 26 27 namespace { 28 29 TEST(ReadaheadTest, InvalidFD) { 30 EXPECT_THAT(readahead(-1, 1, 1), SyscallFailsWithErrno(EBADF)); 31 } 32 33 TEST(ReadaheadTest, UnsupportedFile) { 34 FileDescriptor sock = 35 ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_UNIX, SOCK_STREAM, 0)); 36 ASSERT_THAT(readahead(sock.get(), 1, 1), SyscallFailsWithErrno(EINVAL)); 37 } 38 39 TEST(ReadaheadTest, InvalidOffset) { 40 // This test is not valid for some Linux Kernels. 41 SKIP_IF(!IsRunningOnGvisor()); 42 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 43 const FileDescriptor fd = 44 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 45 EXPECT_THAT(readahead(fd.get(), -1, 1), SyscallFailsWithErrno(EINVAL)); 46 } 47 48 TEST(ReadaheadTest, ValidOffset) { 49 constexpr char kData[] = "123"; 50 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 51 GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode)); 52 const FileDescriptor fd = 53 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 54 55 // N.B. The implementation of readahead is filesystem-specific, and a file 56 // backed by ram may return EINVAL because there is nothing to be read. 57 EXPECT_THAT(readahead(fd.get(), 1, 1), AnyOf(SyscallSucceedsWithValue(0), 58 SyscallFailsWithErrno(EINVAL))); 59 } 60 61 TEST(ReadaheadTest, PastEnd) { 62 constexpr char kData[] = "123"; 63 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 64 GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode)); 65 const FileDescriptor fd = 66 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 67 // See above. 68 EXPECT_THAT(readahead(fd.get(), 2, 2), AnyOf(SyscallSucceedsWithValue(0), 69 SyscallFailsWithErrno(EINVAL))); 70 } 71 72 TEST(ReadaheadTest, CrossesEnd) { 73 constexpr char kData[] = "123"; 74 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith( 75 GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode)); 76 const FileDescriptor fd = 77 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 78 // See above. 79 EXPECT_THAT(readahead(fd.get(), 4, 2), AnyOf(SyscallSucceedsWithValue(0), 80 SyscallFailsWithErrno(EINVAL))); 81 } 82 83 TEST(ReadaheadTest, WriteOnly) { 84 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 85 const FileDescriptor fd = 86 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_WRONLY)); 87 EXPECT_THAT(readahead(fd.get(), 0, 1), SyscallFailsWithErrno(EBADF)); 88 } 89 90 TEST(ReadaheadTest, InvalidSize) { 91 // This test is not valid on some Linux kernels. 92 SKIP_IF(!IsRunningOnGvisor()); 93 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 94 const FileDescriptor fd = 95 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 96 EXPECT_THAT(readahead(fd.get(), 0, -1), SyscallFailsWithErrno(EINVAL)); 97 } 98 99 } // namespace 100 101 } // namespace testing 102 } // namespace gvisor