gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/socket_non_stream_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_stream_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 "absl/time/clock.h"
    24  #include "absl/time/time.h"
    25  #include "test/syscalls/linux/unix_domain_socket_test_util.h"
    26  #include "test/util/socket_util.h"
    27  #include "test/util/test_util.h"
    28  #include "test/util/thread_util.h"
    29  
    30  namespace gvisor {
    31  namespace testing {
    32  
    33  TEST_P(BlockingNonStreamSocketPairTest, RecvLessThanBufferWaitAll) {
    34    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    35  
    36    char sent_data[100];
    37    RandomizeBuffer(sent_data, sizeof(sent_data));
    38  
    39    ASSERT_THAT(write(sockets->first_fd(), sent_data, sizeof(sent_data)),
    40                SyscallSucceedsWithValue(sizeof(sent_data)));
    41  
    42    char received_data[sizeof(sent_data) * 2] = {};
    43    ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
    44                                 sizeof(received_data), MSG_WAITALL),
    45                SyscallSucceedsWithValue(sizeof(sent_data)));
    46  }
    47  
    48  // This test tests reading from a socket with MSG_TRUNC | MSG_PEEK and a zero
    49  // length receive buffer and MSG_DONTWAIT. The recvmsg call should block on
    50  // reading the data.
    51  TEST_P(BlockingNonStreamSocketPairTest,
    52         RecvmsgTruncPeekDontwaitZeroLenBlocking) {
    53    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    54  
    55    // NOTE: We don't initially send any data on the socket.
    56    const int data_size = 10;
    57    char sent_data[data_size];
    58    RandomizeBuffer(sent_data, data_size);
    59  
    60    // The receive buffer is of zero length.
    61    char peek_data[0] = {};
    62  
    63    struct iovec peek_iov;
    64    peek_iov.iov_base = peek_data;
    65    peek_iov.iov_len = sizeof(peek_data);
    66    struct msghdr peek_msg = {};
    67    peek_msg.msg_flags = -1;
    68    peek_msg.msg_iov = &peek_iov;
    69    peek_msg.msg_iovlen = 1;
    70  
    71    ScopedThread t([&]() {
    72      // The syscall succeeds returning the full size of the message on the
    73      // socket. This should block until there is data on the socket.
    74      ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &peek_msg,
    75                                      MSG_TRUNC | MSG_PEEK),
    76                  SyscallSucceedsWithValue(data_size));
    77    });
    78  
    79    absl::SleepFor(absl::Seconds(1));
    80    ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), sent_data, data_size, 0),
    81                SyscallSucceedsWithValue(data_size));
    82  }
    83  
    84  }  // namespace testing
    85  }  // namespace gvisor