github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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    SKIP_IF(IsRunningWithVFS1());
    50    auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    51    const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_PATH));
    52  
    53    ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NORMAL),
    54                SyscallFailsWithErrno(EBADF));
    55    ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, POSIX_FADV_NORMAL),
    56                SyscallFailsWithErrno(EBADF));
    57  }
    58  
    59  TEST(FAdvise64Test, InvalidArgs) {
    60    auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    61    const auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
    62  
    63    // Note: offset is allowed to be negative.
    64    ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, static_cast<off_t>(-1),
    65                        POSIX_FADV_NORMAL),
    66                SyscallFailsWithErrno(EINVAL));
    67    ASSERT_THAT(syscall(__NR_fadvise64, fd.get(), 0, 10, 12345),
    68                SyscallFailsWithErrno(EINVAL));
    69  }
    70  
    71  TEST(FAdvise64Test, NoPipes) {
    72    int fds[2];
    73    ASSERT_THAT(pipe(fds), SyscallSucceeds());
    74    const FileDescriptor read(fds[0]);
    75    const FileDescriptor write(fds[1]);
    76  
    77    ASSERT_THAT(syscall(__NR_fadvise64, read.get(), 0, 10, POSIX_FADV_NORMAL),
    78                SyscallFailsWithErrno(ESPIPE));
    79  }
    80  
    81  }  // namespace
    82  }  // namespace testing
    83  }  // namespace gvisor