gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/accept_bind_stream.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 <stdio.h>
    16  #include <sys/un.h>
    17  
    18  #include <algorithm>
    19  #include <vector>
    20  
    21  #include "gtest/gtest.h"
    22  #include "test/syscalls/linux/unix_domain_socket_test_util.h"
    23  #include "test/util/file_descriptor.h"
    24  #include "test/util/socket_util.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  TEST_P(AllSocketPairTest, BoundSenderAddrCoalesced) {
    33    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    34  
    35    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    36                     sockets->first_addr_size()),
    37                SyscallSucceeds());
    38  
    39    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
    40  
    41    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
    42                        sockets->first_addr_size()),
    43                SyscallSucceeds());
    44  
    45    int accepted = -1;
    46    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
    47                SyscallSucceeds());
    48    FileDescriptor closer(accepted);
    49  
    50    int i = 0;
    51    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
    52                SyscallSucceedsWithValue(sizeof(i)));
    53  
    54    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
    55                     sockets->second_addr_size()),
    56                SyscallSucceeds());
    57  
    58    i = 0;
    59    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
    60                SyscallSucceedsWithValue(sizeof(i)));
    61  
    62    int ri[2] = {0, 0};
    63    struct sockaddr_storage addr;
    64    socklen_t addr_len = sizeof(addr);
    65    ASSERT_THAT(
    66        RetryEINTR(recvfrom)(accepted, ri, sizeof(ri), 0,
    67                             reinterpret_cast<sockaddr*>(&addr), &addr_len),
    68        SyscallSucceedsWithValue(sizeof(ri)));
    69    EXPECT_EQ(addr_len, sockets->second_addr_len());
    70  
    71    EXPECT_EQ(
    72        memcmp(&addr, sockets->second_addr(),
    73               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
    74        0);
    75  }
    76  
    77  INSTANTIATE_TEST_SUITE_P(
    78      AllUnixDomainSockets, AllSocketPairTest,
    79      ::testing::ValuesIn(VecCat<SocketPairKind>(
    80          ApplyVec<SocketPairKind>(FilesystemUnboundUnixDomainSocketPair,
    81                                   AllBitwiseCombinations(List<int>{SOCK_STREAM},
    82                                                          List<int>{
    83                                                              0, SOCK_NONBLOCK})),
    84          ApplyVec<SocketPairKind>(
    85              AbstractUnboundUnixDomainSocketPair,
    86              AllBitwiseCombinations(List<int>{SOCK_STREAM},
    87                                     List<int>{0, SOCK_NONBLOCK})))));
    88  
    89  }  // namespace
    90  
    91  }  // namespace testing
    92  }  // namespace gvisor