github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/accept_bind.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/socket.h>
    17  #include <sys/un.h>
    18  
    19  #include <algorithm>
    20  #include <vector>
    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/file_descriptor.h"
    26  #include "test/util/test_util.h"
    27  
    28  namespace gvisor {
    29  namespace testing {
    30  
    31  namespace {
    32  
    33  TEST_P(AllSocketPairTest, Listen) {
    34    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    35  
    36    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    37                     sockets->first_addr_size()),
    38                SyscallSucceeds());
    39  
    40    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
    41  }
    42  
    43  TEST_P(AllSocketPairTest, ListenIncreaseBacklog) {
    44    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    45  
    46    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    47                     sockets->first_addr_size()),
    48                SyscallSucceeds());
    49  
    50    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
    51    ASSERT_THAT(listen(sockets->first_fd(), 10), SyscallSucceeds());
    52  }
    53  
    54  TEST_P(AllSocketPairTest, ListenDecreaseBacklog) {
    55    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    56  
    57    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    58                     sockets->first_addr_size()),
    59                SyscallSucceeds());
    60  
    61    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
    62    ASSERT_THAT(listen(sockets->first_fd(), 1), SyscallSucceeds());
    63  }
    64  
    65  TEST_P(AllSocketPairTest, ListenBacklogSizes) {
    66    DisableSave ds;
    67    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    68  
    69    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    70                     sockets->first_addr_size()),
    71                SyscallSucceeds());
    72  
    73    int type;
    74    socklen_t typelen = sizeof(type);
    75    EXPECT_THAT(
    76        getsockopt(sockets->first_fd(), SOL_SOCKET, SO_TYPE, &type, &typelen),
    77        SyscallSucceeds());
    78  
    79    std::array<int, 3> backlogs = {-1, 0, 1};
    80    for (auto& backlog : backlogs) {
    81      ASSERT_THAT(listen(sockets->first_fd(), backlog), SyscallSucceeds());
    82  
    83      int expected_accepts = backlog;
    84      if (backlog < 0) {
    85        expected_accepts = 1024;
    86      }
    87      for (int i = 0; i < expected_accepts; i++) {
    88        SCOPED_TRACE(absl::StrCat("i=", i));
    89        // Connect to the listening socket.
    90        const FileDescriptor client =
    91            ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_UNIX, type, 0));
    92        ASSERT_THAT(connect(client.get(), sockets->first_addr(),
    93                            sockets->first_addr_size()),
    94                    SyscallSucceeds());
    95        const FileDescriptor accepted = ASSERT_NO_ERRNO_AND_VALUE(
    96            Accept(sockets->first_fd(), nullptr, nullptr));
    97      }
    98    }
    99  }
   100  
   101  TEST_P(AllSocketPairTest, ListenWithoutBind) {
   102    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   103    ASSERT_THAT(listen(sockets->first_fd(), 0), SyscallFailsWithErrno(EINVAL));
   104  }
   105  
   106  TEST_P(AllSocketPairTest, DoubleBind) {
   107    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   108  
   109    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   110                     sockets->first_addr_size()),
   111                SyscallSucceeds());
   112  
   113    ASSERT_THAT(bind(sockets->first_fd(), sockets->second_addr(),
   114                     sockets->second_addr_size()),
   115                SyscallFailsWithErrno(EINVAL));
   116  }
   117  
   118  TEST_P(AllSocketPairTest, BindListenBind) {
   119    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   120  
   121    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   122                     sockets->first_addr_size()),
   123                SyscallSucceeds());
   124  
   125    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   126  
   127    ASSERT_THAT(bind(sockets->first_fd(), sockets->second_addr(),
   128                     sockets->second_addr_size()),
   129                SyscallFailsWithErrno(EINVAL));
   130  }
   131  
   132  TEST_P(AllSocketPairTest, DoubleListen) {
   133    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   134  
   135    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   136                     sockets->first_addr_size()),
   137                SyscallSucceeds());
   138  
   139    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   140  
   141    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   142  }
   143  
   144  TEST_P(AllSocketPairTest, DoubleConnect) {
   145    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   146  
   147    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   148                     sockets->first_addr_size()),
   149                SyscallSucceeds());
   150  
   151    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   152  
   153    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   154                        sockets->first_addr_size()),
   155                SyscallSucceeds());
   156  
   157    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   158                        sockets->first_addr_size()),
   159                SyscallFailsWithErrno(EISCONN));
   160  }
   161  
   162  TEST_P(AllSocketPairTest, Connect) {
   163    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   164  
   165    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   166                     sockets->first_addr_size()),
   167                SyscallSucceeds());
   168  
   169    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   170  
   171    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   172                        sockets->first_addr_size()),
   173                SyscallSucceeds());
   174  }
   175  
   176  TEST_P(AllSocketPairTest, ConnectWithWrongType) {
   177    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   178  
   179    int type;
   180    socklen_t typelen = sizeof(type);
   181    EXPECT_THAT(
   182        getsockopt(sockets->first_fd(), SOL_SOCKET, SO_TYPE, &type, &typelen),
   183        SyscallSucceeds());
   184    switch (type) {
   185      case SOCK_STREAM:
   186        type = SOCK_SEQPACKET;
   187        break;
   188      case SOCK_SEQPACKET:
   189        type = SOCK_STREAM;
   190        break;
   191    }
   192  
   193    const FileDescriptor another_socket =
   194        ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_UNIX, type, 0));
   195  
   196    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   197                     sockets->first_addr_size()),
   198                SyscallSucceeds());
   199  
   200    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   201  
   202    if (sockets->first_addr()->sa_data[0] != 0) {
   203      ASSERT_THAT(connect(another_socket.get(), sockets->first_addr(),
   204                          sockets->first_addr_size()),
   205                  SyscallFailsWithErrno(EPROTOTYPE));
   206    } else {
   207      ASSERT_THAT(connect(another_socket.get(), sockets->first_addr(),
   208                          sockets->first_addr_size()),
   209                  SyscallFailsWithErrno(ECONNREFUSED));
   210    }
   211  
   212    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   213                        sockets->first_addr_size()),
   214                SyscallSucceeds());
   215  }
   216  
   217  TEST_P(AllSocketPairTest, ConnectNonListening) {
   218    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   219  
   220    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   221                     sockets->first_addr_size()),
   222                SyscallSucceeds());
   223  
   224    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   225                        sockets->first_addr_size()),
   226                SyscallFailsWithErrno(ECONNREFUSED));
   227  }
   228  
   229  TEST_P(AllSocketPairTest, ConnectToFilePath) {
   230    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   231  
   232    struct sockaddr_un addr = {};
   233    addr.sun_family = AF_UNIX;
   234    constexpr char kPath[] = "/tmp";
   235    memcpy(addr.sun_path, kPath, sizeof(kPath));
   236  
   237    ASSERT_THAT(
   238        connect(sockets->second_fd(),
   239                reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)),
   240        SyscallFailsWithErrno(ECONNREFUSED));
   241  }
   242  
   243  TEST_P(AllSocketPairTest, ConnectToInvalidAbstractPath) {
   244    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   245  
   246    struct sockaddr_un addr = {};
   247    addr.sun_family = AF_UNIX;
   248    constexpr char kPath[] = "\0nonexistent";
   249    memcpy(addr.sun_path, kPath, sizeof(kPath));
   250  
   251    ASSERT_THAT(
   252        connect(sockets->second_fd(),
   253                reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)),
   254        SyscallFailsWithErrno(ECONNREFUSED));
   255  }
   256  
   257  TEST_P(AllSocketPairTest, SelfConnect) {
   258    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   259  
   260    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   261                     sockets->first_addr_size()),
   262                SyscallSucceeds());
   263  
   264    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   265  
   266    ASSERT_THAT(connect(sockets->first_fd(), sockets->first_addr(),
   267                        sockets->first_addr_size()),
   268                SyscallFailsWithErrno(EINVAL));
   269  }
   270  
   271  TEST_P(AllSocketPairTest, ConnectWithoutListen) {
   272    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   273  
   274    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   275                     sockets->first_addr_size()),
   276                SyscallSucceeds());
   277  
   278    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   279                        sockets->first_addr_size()),
   280                SyscallFailsWithErrno(ECONNREFUSED));
   281  }
   282  
   283  TEST_P(AllSocketPairTest, Accept) {
   284    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   285  
   286    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   287                     sockets->first_addr_size()),
   288                SyscallSucceeds());
   289  
   290    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   291  
   292    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   293                        sockets->first_addr_size()),
   294                SyscallSucceeds());
   295  
   296    int accepted = -1;
   297    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
   298                SyscallSucceeds());
   299    ASSERT_THAT(close(accepted), SyscallSucceeds());
   300  }
   301  
   302  TEST_P(AllSocketPairTest, AcceptValidAddrLen) {
   303    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   304  
   305    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   306                     sockets->first_addr_size()),
   307                SyscallSucceeds());
   308  
   309    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   310  
   311    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   312                        sockets->first_addr_size()),
   313                SyscallSucceeds());
   314  
   315    int accepted = -1;
   316    struct sockaddr_un addr = {};
   317    socklen_t addr_len = sizeof(addr);
   318    ASSERT_THAT(
   319        accepted = accept(sockets->first_fd(), AsSockAddr(&addr), &addr_len),
   320        SyscallSucceeds());
   321    ASSERT_THAT(close(accepted), SyscallSucceeds());
   322  }
   323  
   324  TEST_P(AllSocketPairTest, AcceptNegativeAddrLen) {
   325    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   326  
   327    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   328                     sockets->first_addr_size()),
   329                SyscallSucceeds());
   330  
   331    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   332  
   333    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   334                        sockets->first_addr_size()),
   335                SyscallSucceeds());
   336  
   337    // With a negative addr_len, accept returns EINVAL,
   338    struct sockaddr_un addr = {};
   339    socklen_t addr_len = -1;
   340    ASSERT_THAT(accept(sockets->first_fd(), AsSockAddr(&addr), &addr_len),
   341                SyscallFailsWithErrno(EINVAL));
   342  }
   343  
   344  TEST_P(AllSocketPairTest, AcceptLargePositiveAddrLen) {
   345    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   346  
   347    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   348                     sockets->first_addr_size()),
   349                SyscallSucceeds());
   350  
   351    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   352  
   353    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   354                        sockets->first_addr_size()),
   355                SyscallSucceeds());
   356  
   357    // With a large (positive) addr_len, accept does not return EINVAL.
   358    int accepted = -1;
   359    char addr_buf[200];
   360    socklen_t addr_len = sizeof(addr_buf);
   361    ASSERT_THAT(accepted = accept(sockets->first_fd(),
   362                                  reinterpret_cast<struct sockaddr*>(addr_buf),
   363                                  &addr_len),
   364                SyscallSucceeds());
   365    // addr_len should have been updated by accept().
   366    EXPECT_LT(addr_len, sizeof(addr_buf));
   367    ASSERT_THAT(close(accepted), SyscallSucceeds());
   368  }
   369  
   370  TEST_P(AllSocketPairTest, AcceptVeryLargePositiveAddrLen) {
   371    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   372  
   373    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   374                     sockets->first_addr_size()),
   375                SyscallSucceeds());
   376  
   377    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   378  
   379    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   380                        sockets->first_addr_size()),
   381                SyscallSucceeds());
   382  
   383    // With a large (positive) addr_len, accept does not return EINVAL.
   384    int accepted = -1;
   385    char addr_buf[2000];
   386    socklen_t addr_len = sizeof(addr_buf);
   387    ASSERT_THAT(accepted = accept(sockets->first_fd(),
   388                                  reinterpret_cast<struct sockaddr*>(addr_buf),
   389                                  &addr_len),
   390                SyscallSucceeds());
   391    // addr_len should have been updated by accept().
   392    EXPECT_LT(addr_len, sizeof(addr_buf));
   393    ASSERT_THAT(close(accepted), SyscallSucceeds());
   394  }
   395  
   396  TEST_P(AllSocketPairTest, AcceptWithoutBind) {
   397    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   398  
   399    ASSERT_THAT(accept(sockets->first_fd(), nullptr, nullptr),
   400                SyscallFailsWithErrno(EINVAL));
   401  }
   402  
   403  TEST_P(AllSocketPairTest, AcceptWithoutListen) {
   404    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   405  
   406    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   407                     sockets->first_addr_size()),
   408                SyscallSucceeds());
   409    ASSERT_THAT(accept(sockets->first_fd(), nullptr, nullptr),
   410                SyscallFailsWithErrno(EINVAL));
   411  }
   412  
   413  TEST_P(AllSocketPairTest, GetRemoteAddress) {
   414    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   415  
   416    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   417                     sockets->first_addr_size()),
   418                SyscallSucceeds());
   419  
   420    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   421  
   422    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   423                        sockets->first_addr_size()),
   424                SyscallSucceeds());
   425  
   426    socklen_t addr_len = sockets->first_addr_size();
   427    struct sockaddr_storage addr = {};
   428    ASSERT_THAT(
   429        getpeername(sockets->second_fd(), (struct sockaddr*)(&addr), &addr_len),
   430        SyscallSucceeds());
   431    EXPECT_EQ(addr_len, sockets->first_addr_len());
   432    EXPECT_EQ(0, memcmp(&addr, sockets->first_addr(), sockets->first_addr_len()));
   433  }
   434  
   435  TEST_P(AllSocketPairTest, UnboundGetLocalAddress) {
   436    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   437  
   438    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   439                     sockets->first_addr_size()),
   440                SyscallSucceeds());
   441  
   442    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   443  
   444    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   445                        sockets->first_addr_size()),
   446                SyscallSucceeds());
   447  
   448    socklen_t addr_len = sockets->first_addr_size();
   449    struct sockaddr_storage addr = {};
   450    ASSERT_THAT(
   451        getsockname(sockets->second_fd(), (struct sockaddr*)(&addr), &addr_len),
   452        SyscallSucceeds());
   453    EXPECT_EQ(addr_len, 2);
   454    EXPECT_EQ(
   455        memcmp(&addr, sockets->second_addr(),
   456               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
   457        0);
   458  }
   459  
   460  TEST_P(AllSocketPairTest, BoundGetLocalAddress) {
   461    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   462  
   463    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   464                     sockets->first_addr_size()),
   465                SyscallSucceeds());
   466  
   467    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   468  
   469    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
   470                     sockets->second_addr_size()),
   471                SyscallSucceeds());
   472  
   473    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   474                        sockets->first_addr_size()),
   475                SyscallSucceeds());
   476  
   477    socklen_t addr_len = sockets->first_addr_size();
   478    struct sockaddr_storage addr = {};
   479    ASSERT_THAT(
   480        getsockname(sockets->second_fd(), (struct sockaddr*)(&addr), &addr_len),
   481        SyscallSucceeds());
   482    EXPECT_EQ(addr_len, sockets->second_addr_len());
   483    EXPECT_EQ(
   484        memcmp(&addr, sockets->second_addr(),
   485               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
   486        0);
   487  }
   488  
   489  TEST_P(AllSocketPairTest, BoundConnector) {
   490    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   491  
   492    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   493                     sockets->first_addr_size()),
   494                SyscallSucceeds());
   495  
   496    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   497  
   498    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
   499                     sockets->second_addr_size()),
   500                SyscallSucceeds());
   501  
   502    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   503                        sockets->first_addr_size()),
   504                SyscallSucceeds());
   505  }
   506  
   507  TEST_P(AllSocketPairTest, UnboundSenderAddr) {
   508    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   509  
   510    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   511                     sockets->first_addr_size()),
   512                SyscallSucceeds());
   513  
   514    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   515  
   516    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   517                        sockets->first_addr_size()),
   518                SyscallSucceeds());
   519  
   520    int accepted = -1;
   521    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
   522                SyscallSucceeds());
   523    FileDescriptor accepted_fd(accepted);
   524  
   525    int i = 0;
   526    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
   527                SyscallSucceedsWithValue(sizeof(i)));
   528  
   529    struct sockaddr_storage addr;
   530    socklen_t addr_len = sizeof(addr);
   531    ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
   532                                     AsSockAddr(&addr), &addr_len),
   533                SyscallSucceedsWithValue(sizeof(i)));
   534    EXPECT_EQ(addr_len, 0);
   535  }
   536  
   537  TEST_P(AllSocketPairTest, BoundSenderAddr) {
   538    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   539  
   540    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   541                     sockets->first_addr_size()),
   542                SyscallSucceeds());
   543  
   544    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   545  
   546    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
   547                     sockets->second_addr_size()),
   548                SyscallSucceeds());
   549  
   550    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   551                        sockets->first_addr_size()),
   552                SyscallSucceeds());
   553  
   554    int accepted = -1;
   555    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
   556                SyscallSucceeds());
   557    FileDescriptor accepted_fd(accepted);
   558  
   559    int i = 0;
   560    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
   561                SyscallSucceedsWithValue(sizeof(i)));
   562  
   563    struct sockaddr_storage addr;
   564    socklen_t addr_len = sizeof(addr);
   565    ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
   566                                     AsSockAddr(&addr), &addr_len),
   567                SyscallSucceedsWithValue(sizeof(i)));
   568    EXPECT_EQ(addr_len, sockets->second_addr_len());
   569    EXPECT_EQ(
   570        memcmp(&addr, sockets->second_addr(),
   571               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
   572        0);
   573  }
   574  
   575  TEST_P(AllSocketPairTest, BindAfterConnectSenderAddr) {
   576    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   577  
   578    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   579                     sockets->first_addr_size()),
   580                SyscallSucceeds());
   581  
   582    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   583  
   584    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   585                        sockets->first_addr_size()),
   586                SyscallSucceeds());
   587  
   588    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
   589                     sockets->second_addr_size()),
   590                SyscallSucceeds());
   591  
   592    int accepted = -1;
   593    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
   594                SyscallSucceeds());
   595    FileDescriptor accepted_fd(accepted);
   596  
   597    int i = 0;
   598    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
   599                SyscallSucceedsWithValue(sizeof(i)));
   600  
   601    struct sockaddr_storage addr;
   602    socklen_t addr_len = sizeof(addr);
   603    ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
   604                                     AsSockAddr(&addr), &addr_len),
   605                SyscallSucceedsWithValue(sizeof(i)));
   606    EXPECT_EQ(addr_len, sockets->second_addr_len());
   607    EXPECT_EQ(
   608        memcmp(&addr, sockets->second_addr(),
   609               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
   610        0);
   611  }
   612  
   613  TEST_P(AllSocketPairTest, BindAfterAcceptSenderAddr) {
   614    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
   615  
   616    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
   617                     sockets->first_addr_size()),
   618                SyscallSucceeds());
   619  
   620    ASSERT_THAT(listen(sockets->first_fd(), 5), SyscallSucceeds());
   621  
   622    ASSERT_THAT(connect(sockets->second_fd(), sockets->first_addr(),
   623                        sockets->first_addr_size()),
   624                SyscallSucceeds());
   625  
   626    int accepted = -1;
   627    ASSERT_THAT(accepted = accept(sockets->first_fd(), nullptr, nullptr),
   628                SyscallSucceeds());
   629    FileDescriptor accepted_fd(accepted);
   630  
   631    ASSERT_THAT(bind(sockets->second_fd(), sockets->second_addr(),
   632                     sockets->second_addr_size()),
   633                SyscallSucceeds());
   634  
   635    int i = 0;
   636    ASSERT_THAT(RetryEINTR(send)(sockets->second_fd(), &i, sizeof(i), 0),
   637                SyscallSucceedsWithValue(sizeof(i)));
   638  
   639    struct sockaddr_storage addr;
   640    socklen_t addr_len = sizeof(addr);
   641    ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
   642                                     AsSockAddr(&addr), &addr_len),
   643                SyscallSucceedsWithValue(sizeof(i)));
   644    EXPECT_EQ(addr_len, sockets->second_addr_len());
   645    EXPECT_EQ(
   646        memcmp(&addr, sockets->second_addr(),
   647               std::min((size_t)addr_len, (size_t)sockets->second_addr_len())),
   648        0);
   649  }
   650  
   651  INSTANTIATE_TEST_SUITE_P(
   652      AllUnixDomainSockets, AllSocketPairTest,
   653      ::testing::ValuesIn(VecCat<SocketPairKind>(
   654          ApplyVec<SocketPairKind>(
   655              FilesystemUnboundUnixDomainSocketPair,
   656              AllBitwiseCombinations(List<int>{SOCK_STREAM, SOCK_SEQPACKET},
   657                                     List<int>{0, SOCK_NONBLOCK})),
   658          ApplyVec<SocketPairKind>(
   659              AbstractUnboundUnixDomainSocketPair,
   660              AllBitwiseCombinations(List<int>{SOCK_STREAM, SOCK_SEQPACKET},
   661                                     List<int>{0, SOCK_NONBLOCK})))));
   662  
   663  }  // namespace
   664  
   665  }  // namespace testing
   666  }  // namespace gvisor