github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_non_blocking.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_non_blocking.h" 16 17 #include <stdio.h> 18 #include <sys/socket.h> 19 #include <sys/types.h> 20 #include <sys/un.h> 21 22 #include "gtest/gtest.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 namespace { 30 31 TEST_P(NonBlockingSocketPairTest, ReadNothingAvailable) { 32 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 33 char buf[20] = {}; 34 ASSERT_THAT(ReadFd(sockets->first_fd(), buf, sizeof(buf)), 35 SyscallFailsWithErrno(EAGAIN)); 36 } 37 38 TEST_P(NonBlockingSocketPairTest, RecvNothingAvailable) { 39 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 40 char buf[20] = {}; 41 ASSERT_THAT(RetryEINTR(recv)(sockets->first_fd(), buf, sizeof(buf), 0), 42 SyscallFailsWithErrno(EAGAIN)); 43 } 44 45 TEST_P(NonBlockingSocketPairTest, RecvMsgNothingAvailable) { 46 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 47 48 struct iovec iov; 49 char buf[20] = {}; 50 iov.iov_base = buf; 51 iov.iov_len = sizeof(buf); 52 struct msghdr msg = {}; 53 msg.msg_iov = &iov; 54 msg.msg_iovlen = 1; 55 56 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->first_fd(), &msg, 0), 57 SyscallFailsWithErrno(EAGAIN)); 58 } 59 60 } // namespace 61 } // namespace testing 62 } // namespace gvisor