github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/sync_file_range.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 <stdio.h>
    17  #include <unistd.h>
    18  
    19  #include <string>
    20  
    21  #include "gtest/gtest.h"
    22  #include "test/util/file_descriptor.h"
    23  #include "test/util/temp_path.h"
    24  #include "test/util/test_util.h"
    25  
    26  namespace gvisor {
    27  namespace testing {
    28  
    29  namespace {
    30  
    31  TEST(SyncFileRangeTest, TempFileSucceeds) {
    32    auto tmpfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    33    auto f = ASSERT_NO_ERRNO_AND_VALUE(Open(tmpfile.path(), O_RDWR));
    34    constexpr char data[] = "some data to sync";
    35    int fd = f.get();
    36  
    37    EXPECT_THAT(write(fd, data, sizeof(data)),
    38                SyscallSucceedsWithValue(sizeof(data)));
    39    EXPECT_THAT(sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE),
    40                SyscallSucceeds());
    41    EXPECT_THAT(sync_file_range(fd, 0, 0, 0), SyscallSucceeds());
    42    EXPECT_THAT(
    43        sync_file_range(fd, 0, 0,
    44                        SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER |
    45                            SYNC_FILE_RANGE_WAIT_BEFORE),
    46        SyscallSucceeds());
    47    EXPECT_THAT(sync_file_range(
    48                    fd, 0, 1, SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER),
    49                SyscallSucceeds());
    50    EXPECT_THAT(sync_file_range(
    51                    fd, 1, 0, SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER),
    52                SyscallSucceeds());
    53  }
    54  
    55  TEST(SyncFileRangeTest, CannotSyncFileRangeOnUnopenedFd) {
    56    auto tmpfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    57    auto f = ASSERT_NO_ERRNO_AND_VALUE(Open(tmpfile.path(), O_RDWR));
    58    constexpr char data[] = "some data to sync";
    59    int fd = f.get();
    60  
    61    EXPECT_THAT(write(fd, data, sizeof(data)),
    62                SyscallSucceedsWithValue(sizeof(data)));
    63  
    64    pid_t pid = fork();
    65    if (pid == 0) {
    66      f.reset();
    67  
    68      // fd is now invalid.
    69      TEST_CHECK(sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE) == -1);
    70      TEST_PCHECK(errno == EBADF);
    71      _exit(0);
    72    }
    73    ASSERT_THAT(pid, SyscallSucceeds());
    74  
    75    int status = 0;
    76    ASSERT_THAT(waitpid(pid, &status, 0), SyscallSucceedsWithValue(pid));
    77    EXPECT_TRUE(WIFEXITED(status));
    78    EXPECT_EQ(WEXITSTATUS(status), 0);
    79  }
    80  
    81  TEST(SyncFileRangeTest, BadArgs) {
    82    auto tmpfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    83    auto f = ASSERT_NO_ERRNO_AND_VALUE(Open(tmpfile.path(), O_RDWR));
    84    int fd = f.get();
    85  
    86    EXPECT_THAT(sync_file_range(fd, -1, 0, 0), SyscallFailsWithErrno(EINVAL));
    87    EXPECT_THAT(sync_file_range(fd, 0, -1, 0), SyscallFailsWithErrno(EINVAL));
    88    EXPECT_THAT(sync_file_range(fd, 8912, INT64_MAX - 4096, 0),
    89                SyscallFailsWithErrno(EINVAL));
    90  }
    91  
    92  TEST(SyncFileRangeTest, CannotSyncFileRangeWithWaitBefore) {
    93    auto tmpfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    94    auto f = ASSERT_NO_ERRNO_AND_VALUE(Open(tmpfile.path(), O_RDWR));
    95    constexpr char data[] = "some data to sync";
    96    int fd = f.get();
    97  
    98    EXPECT_THAT(write(fd, data, sizeof(data)),
    99                SyscallSucceedsWithValue(sizeof(data)));
   100    if (IsRunningOnGvisor()) {
   101      EXPECT_THAT(sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE),
   102                  SyscallFailsWithErrno(ENOSYS));
   103      EXPECT_THAT(
   104          sync_file_range(fd, 0, 0,
   105                          SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE),
   106          SyscallFailsWithErrno(ENOSYS));
   107    }
   108  }
   109  
   110  }  // namespace
   111  }  // namespace testing
   112  }  // namespace gvisor