gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/pwrite64.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 <fcntl.h> 17 #include <linux/unistd.h> 18 #include <sys/socket.h> 19 #include <sys/types.h> 20 #include <unistd.h> 21 22 #include "gtest/gtest.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 // TODO(gvisor.dev/issue/2370): This test is currently very rudimentary. 32 class Pwrite64 : public ::testing::Test { 33 void SetUp() override { 34 name_ = NewTempAbsPath(); 35 int fd; 36 ASSERT_THAT(fd = open(name_.c_str(), O_CREAT, 0644), SyscallSucceeds()); 37 EXPECT_THAT(close(fd), SyscallSucceeds()); 38 } 39 40 void TearDown() override { unlink(name_.c_str()); } 41 42 public: 43 std::string name_; 44 }; 45 46 TEST_F(Pwrite64, AppendOnly) { 47 int fd; 48 ASSERT_THAT(fd = open(name_.c_str(), O_APPEND | O_RDWR), SyscallSucceeds()); 49 constexpr int64_t kBufSize = 1024; 50 std::vector<char> buf(kBufSize); 51 std::fill(buf.begin(), buf.end(), 'a'); 52 EXPECT_THAT(PwriteFd(fd, buf.data(), buf.size(), 0), 53 SyscallSucceedsWithValue(buf.size())); 54 EXPECT_THAT(lseek(fd, 0, SEEK_CUR), SyscallSucceedsWithValue(0)); 55 EXPECT_THAT(close(fd), SyscallSucceeds()); 56 } 57 58 TEST_F(Pwrite64, InvalidArgs) { 59 int fd; 60 ASSERT_THAT(fd = open(name_.c_str(), O_APPEND | O_RDWR), SyscallSucceeds()); 61 constexpr int64_t kBufSize = 1024; 62 std::vector<char> buf(kBufSize); 63 std::fill(buf.begin(), buf.end(), 'a'); 64 EXPECT_THAT(PwriteFd(fd, buf.data(), buf.size(), -1), 65 SyscallFailsWithErrno(EINVAL)); 66 EXPECT_THAT(close(fd), SyscallSucceeds()); 67 } 68 69 TEST_F(Pwrite64, Overflow) { 70 int fd; 71 ASSERT_THAT(fd = open(name_.c_str(), O_APPEND | O_RDWR), SyscallSucceeds()); 72 constexpr int64_t kBufSize = 1024; 73 std::vector<char> buf(kBufSize); 74 std::fill(buf.begin(), buf.end(), 'a'); 75 EXPECT_THAT(PwriteFd(fd, buf.data(), buf.size(), 0x7fffffffffffffffull), 76 SyscallFailsWithErrno(EINVAL)); 77 EXPECT_THAT(close(fd), SyscallSucceeds()); 78 } 79 80 TEST_F(Pwrite64, Pwrite64WithOpath) { 81 const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 82 const FileDescriptor fd = 83 ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_PATH)); 84 85 std::vector<char> buf(1); 86 EXPECT_THAT(PwriteFd(fd.get(), buf.data(), 1, 0), 87 SyscallFailsWithErrno(EBADF)); 88 } 89 90 } // namespace 91 92 } // namespace testing 93 } // namespace gvisor