github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_unix_unbound_filesystem.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 <fcntl.h>
    16  #include <stdio.h>
    17  #include <sys/un.h>
    18  
    19  #include "gtest/gtest.h"
    20  #include "test/syscalls/linux/socket_test_util.h"
    21  #include "test/syscalls/linux/unix_domain_socket_test_util.h"
    22  #include "test/util/file_descriptor.h"
    23  #include "test/util/test_util.h"
    24  
    25  namespace gvisor {
    26  namespace testing {
    27  
    28  namespace {
    29  
    30  // Test fixture for tests that apply to pairs of unbound filesystem unix
    31  // sockets.
    32  using UnboundFilesystemUnixSocketPairTest = SocketPairTest;
    33  
    34  TEST_P(UnboundFilesystemUnixSocketPairTest, AddressAfterNull) {
    35    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    36  
    37    struct sockaddr_un addr =
    38        *reinterpret_cast<const struct sockaddr_un*>(sockets->first_addr());
    39    ASSERT_EQ(addr.sun_path[sizeof(addr.sun_path) - 1], 0);
    40    SKIP_IF(addr.sun_path[sizeof(addr.sun_path) - 2] != 0 ||
    41            addr.sun_path[sizeof(addr.sun_path) - 3] != 0);
    42  
    43    addr.sun_path[sizeof(addr.sun_path) - 2] = 'a';
    44  
    45    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    46                     sockets->first_addr_size()),
    47                SyscallSucceeds());
    48  
    49    ASSERT_THAT(bind(sockets->second_fd(),
    50                     reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)),
    51                SyscallFailsWithErrno(EADDRINUSE));
    52  }
    53  
    54  TEST_P(UnboundFilesystemUnixSocketPairTest, GetSockNameLength) {
    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    sockaddr_storage got_addr = {};
    62    socklen_t got_addr_len = sizeof(got_addr);
    63    ASSERT_THAT(
    64        getsockname(sockets->first_fd(),
    65                    reinterpret_cast<struct sockaddr*>(&got_addr), &got_addr_len),
    66        SyscallSucceeds());
    67  
    68    sockaddr_un want_addr =
    69        *reinterpret_cast<const struct sockaddr_un*>(sockets->first_addr());
    70  
    71    EXPECT_EQ(got_addr_len,
    72              strlen(want_addr.sun_path) + 1 + sizeof(want_addr.sun_family));
    73  }
    74  
    75  TEST_P(UnboundFilesystemUnixSocketPairTest, OpenSocketWithTruncate) {
    76    auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
    77  
    78    ASSERT_THAT(bind(sockets->first_fd(), sockets->first_addr(),
    79                     sockets->first_addr_size()),
    80                SyscallSucceeds());
    81  
    82    const struct sockaddr_un *addr =
    83        reinterpret_cast<const struct sockaddr_un *>(sockets->first_addr());
    84    EXPECT_THAT(chmod(addr->sun_path, 0777), SyscallSucceeds());
    85    EXPECT_THAT(open(addr->sun_path, O_RDONLY | O_TRUNC),
    86                SyscallFailsWithErrno(ENXIO));
    87  }
    88  
    89  INSTANTIATE_TEST_SUITE_P(
    90      AllUnixDomainSockets, UnboundFilesystemUnixSocketPairTest,
    91      ::testing::ValuesIn(ApplyVec<SocketPairKind>(
    92          FilesystemUnboundUnixDomainSocketPair,
    93          AllBitwiseCombinations(List<int>{SOCK_STREAM, SOCK_SEQPACKET,
    94                                           SOCK_DGRAM},
    95                                 List<int>{0, SOCK_NONBLOCK}))));
    96  
    97  }  // namespace
    98  
    99  }  // namespace testing
   100  }  // namespace gvisor