github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_unix_unbound_seqpacket.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 "gtest/gtest.h"
    19  #include "test/syscalls/linux/socket_test_util.h"
    20  #include "test/syscalls/linux/unix_domain_socket_test_util.h"
    21  #include "test/util/test_util.h"
    22  
    23  namespace gvisor {
    24  namespace testing {
    25  
    26  namespace {
    27  
    28  // Test fixture for tests that apply to pairs of unbound seqpacket unix sockets.
    29  using UnboundUnixSeqpacketSocketPairTest = SocketPairTest;
    30  
    31  TEST_P(UnboundUnixSeqpacketSocketPairTest, SendtoWithoutConnect) {
    32    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    33  
    34    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    35                     sockets->first_addr_size()),
    36                SyscallSucceeds());
    37  
    38    char data = 'a';
    39    ASSERT_THAT(sendto(sockets->second_fd(), &data, sizeof(data), 0,
    40                       sockets->first_addr(), sockets->first_addr_size()),
    41                SyscallFailsWithErrno(ENOTCONN));
    42  }
    43  
    44  TEST_P(UnboundUnixSeqpacketSocketPairTest, SendtoWithoutConnectIgnoresAddr) {
    45    // FIXME(b/68223466): gVisor tries to find /foo/bar and thus returns ENOENT.
    46    if (IsRunningOnGvisor()) {
    47      return;
    48    }
    49  
    50    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    51  
    52    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    53                     sockets->first_addr_size()),
    54                SyscallSucceeds());
    55  
    56    // Even a bogus address is completely ignored.
    57    constexpr char kPath[] = "/foo/bar";
    58  
    59    // Sanity check that kPath doesn't exist.
    60    struct stat s;
    61    ASSERT_THAT(stat(kPath, &s), SyscallFailsWithErrno(ENOENT));
    62  
    63    struct sockaddr_un addr = {};
    64    addr.sun_family = AF_UNIX;
    65    memcpy(addr.sun_path, kPath, sizeof(kPath));
    66  
    67    char data = 'a';
    68    ASSERT_THAT(
    69        sendto(sockets->second_fd(), &data, sizeof(data), 0,
    70               reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)),
    71        SyscallFailsWithErrno(ENOTCONN));
    72  }
    73  
    74  INSTANTIATE_TEST_SUITE_P(
    75      AllUnixDomainSockets, UnboundUnixSeqpacketSocketPairTest,
    76      ::testing::ValuesIn(IncludeReversals(VecCat<SocketPairKind>(
    77          ApplyVec<SocketPairKind>(
    78              FilesystemUnboundUnixDomainSocketPair,
    79              AllBitwiseCombinations(List<int>{SOCK_SEQPACKET},
    80                                     List<int>{0, SOCK_NONBLOCK})),
    81          ApplyVec<SocketPairKind>(
    82              AbstractUnboundUnixDomainSocketPair,
    83              AllBitwiseCombinations(List<int>{SOCK_SEQPACKET},
    84                                     List<int>{0, SOCK_NONBLOCK}))))));
    85  
    86  }  // namespace
    87  
    88  }  // namespace testing
    89  }  // namespace gvisor