github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_unix_dgram.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 "test/syscalls/linux/socket_unix_dgram.h" 16 17 #include <stdio.h> 18 #include <sys/un.h> 19 20 #include "gtest/gtest.h" 21 #include "absl/time/clock.h" 22 #include "absl/time/time.h" 23 #include "test/syscalls/linux/socket_test_util.h" 24 #include "test/syscalls/linux/unix_domain_socket_test_util.h" 25 #include "test/util/test_util.h" 26 27 namespace gvisor { 28 namespace testing { 29 30 namespace { 31 32 TEST_P(DgramUnixSocketPairTest, WriteOneSideClosed) { 33 // FIXME(b/35925052): gVisor datagram sockets return EPIPE instead of 34 // ECONNREFUSED. 35 SKIP_IF(IsRunningOnGvisor()); 36 37 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 38 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 39 constexpr char kStr[] = "abc"; 40 ASSERT_THAT(write(sockets->second_fd(), kStr, 3), 41 SyscallFailsWithErrno(ECONNREFUSED)); 42 } 43 44 TEST_P(DgramUnixSocketPairTest, IncreasedSocketSendBufUnblocksWrites) { 45 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 46 int sock = sockets->first_fd(); 47 int buf_size = 0; 48 socklen_t buf_size_len = sizeof(buf_size); 49 ASSERT_THAT(getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &buf_size, &buf_size_len), 50 SyscallSucceeds()); 51 int opts; 52 ASSERT_THAT(opts = fcntl(sock, F_GETFL), SyscallSucceeds()); 53 opts |= O_NONBLOCK; 54 ASSERT_THAT(fcntl(sock, F_SETFL, opts), SyscallSucceeds()); 55 56 std::vector<char> buf(buf_size / 4); 57 // Write till the socket buffer is full. 58 while (RetryEINTR(send)(sock, buf.data(), buf.size(), 0) != -1) { 59 // Sleep to give linux a chance to move data from the send buffer to the 60 // receive buffer. 61 absl::SleepFor(absl::Milliseconds(10)); // 10ms. 62 } 63 // The last error should have been EWOULDBLOCK. 64 ASSERT_EQ(errno, EWOULDBLOCK); 65 66 // Now increase the socket send buffer. 67 buf_size = buf_size * 2; 68 ASSERT_THAT( 69 setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size)), 70 SyscallSucceeds()); 71 72 // The send should succeed again. 73 ASSERT_THAT(RetryEINTR(send)(sock, buf.data(), buf.size(), 0), 74 SyscallSucceeds()); 75 } 76 77 } // namespace 78 79 } // namespace testing 80 } // namespace gvisor