gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/fadvise64.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 <errno.h> 16 #include <syscall.h> 17 #include <unistd.h> 18 19 #include "gmock/gmock.h" 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 namespace { 28 29 TEST(FAdvise64Test, Basic) { 30 auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 31 const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 32 33 // fadvise64 is noop in gVisor, so just test that it succeeds. 34 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NORMAL), 35 SyscallSucceeds()); 36 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_RANDOM), 37 SyscallSucceeds()); 38 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_SEQUENTIAL), 39 SyscallSucceeds()); 40 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_WILLNEED), 41 SyscallSucceeds()); 42 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_DONTNEED), 43 SyscallSucceeds()); 44 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NOREUSE), 45 SyscallSucceeds()); 46 } 47 48 TEST(FAdvise64Test, FAdvise64WithOpath) { 49 auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 50 const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_PATH)); 51 52 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NORMAL), 53 SyscallFailsWithErrno(EBADF)); 54 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NORMAL), 55 SyscallFailsWithErrno(EBADF)); 56 } 57 58 TEST(FAdvise64Test, InvalidArgs) { 59 auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 60 const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY)); 61 62 // Note: offset is allowed to be negative. 63 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, static_cast<off_t>(-1), 64 POSIX_FADV_NORMAL), 65 SyscallFailsWithErrno(EINVAL)); 66 ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, 12345), 67 SyscallFailsWithErrno(EINVAL)); 68 } 69 70 TEST(FAdvise64Test, NoPipes) { 71 int fds[2]; 72 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 73 const FileDescriptor read(fds[0]); 74 const FileDescriptor write(fds[1]); 75 76 ASSERT_THAT(syscall(__NR_fadvise64, read.get(), 0, 10, POSIX_FADV_NORMAL), 77 SyscallFailsWithErrno(ESPIPE)); 78 } 79 80 } // namespace 81 } // namespace testing 82 } // namespace gvisor