gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/socket_ip_unbound_netlink.cc (about)

     1  // Copyright 2019 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 <arpa/inet.h>
    16  #include <netinet/in.h>
    17  #include <sys/socket.h>
    18  #include <sys/types.h>
    19  #include <sys/un.h>
    20  
    21  #include <cstdio>
    22  #include <cstring>
    23  
    24  #include "gmock/gmock.h"
    25  #include "gtest/gtest.h"
    26  #include "test/syscalls/linux/ip_socket_test_util.h"
    27  #include "test/syscalls/linux/socket_netlink_route_util.h"
    28  #include "test/util/capability_util.h"
    29  #include "test/util/socket_util.h"
    30  #include "test/util/test_util.h"
    31  
    32  namespace gvisor {
    33  namespace testing {
    34  
    35  // Test fixture for tests that apply to pairs of IP sockets.
    36  using IPv6UnboundSocketTest = SimpleSocketTest;
    37  
    38  TEST_P(IPv6UnboundSocketTest, ConnectToBadLocalAddress) {
    39    SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_ADMIN)));
    40  
    41    // TODO(gvisor.dev/issue/4595): Addresses on net devices are not saved
    42    // across save/restore.
    43    DisableSave ds;
    44  
    45    // Delete the loopback address from the loopback interface.
    46    Link loopback_link = ASSERT_NO_ERRNO_AND_VALUE(LoopbackLink());
    47    EXPECT_NO_ERRNO(LinkDelLocalAddr(loopback_link.index, AF_INET6,
    48                                     /*prefixlen=*/128, &in6addr_loopback,
    49                                     sizeof(in6addr_loopback)));
    50    Cleanup defer_addr_removal =
    51        Cleanup([loopback_link = std::move(loopback_link)] {
    52          EXPECT_NO_ERRNO(LinkAddLocalAddr(loopback_link.index, AF_INET6,
    53                                           /*prefixlen=*/128, &in6addr_loopback,
    54                                           sizeof(in6addr_loopback)));
    55        });
    56  
    57    TestAddress addr = V6Loopback();
    58    reinterpret_cast<sockaddr_in6*>(&addr.addr)->sin6_port = 65535;
    59    auto sock = ASSERT_NO_ERRNO_AND_VALUE(NewSocket());
    60    EXPECT_THAT(connect(sock->get(), AsSockAddr(&addr.addr), addr.addr_len),
    61                SyscallFailsWithErrno(EADDRNOTAVAIL));
    62  }
    63  
    64  INSTANTIATE_TEST_SUITE_P(IPUnboundSockets, IPv6UnboundSocketTest,
    65                           ::testing::ValuesIn(std::vector<SocketKind>{
    66                               IPv6UDPUnboundSocket(0),
    67                               IPv6TCPUnboundSocket(0)}));
    68  
    69  using IPv4UnboundSocketTest = SimpleSocketTest;
    70  
    71  TEST_P(IPv4UnboundSocketTest, ConnectToBadLocalAddress) {
    72    SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_ADMIN)));
    73  
    74    // TODO(gvisor.dev/issue/4595): Addresses on net devices are not saved
    75    // across save/restore.
    76    DisableSave ds;
    77  
    78    // Delete the loopback address from the loopback interface.
    79    Link loopback_link = ASSERT_NO_ERRNO_AND_VALUE(LoopbackLink());
    80    struct in_addr laddr;
    81    laddr.s_addr = htonl(INADDR_LOOPBACK);
    82    EXPECT_NO_ERRNO(LinkDelLocalAddr(loopback_link.index, AF_INET,
    83                                     /*prefixlen=*/8, &laddr, sizeof(laddr)));
    84    Cleanup defer_addr_removal = Cleanup(
    85        [loopback_link = std::move(loopback_link), addr = std::move(laddr)] {
    86          EXPECT_NO_ERRNO(LinkAddLocalAddr(loopback_link.index, AF_INET,
    87                                           /*prefixlen=*/8, &addr, sizeof(addr)));
    88        });
    89    TestAddress addr = V4Loopback();
    90    reinterpret_cast<sockaddr_in*>(&addr.addr)->sin_port = 65535;
    91    auto sock = ASSERT_NO_ERRNO_AND_VALUE(NewSocket());
    92    EXPECT_THAT(connect(sock->get(), AsSockAddr(&addr.addr), addr.addr_len),
    93                SyscallFailsWithErrno(ENETUNREACH));
    94  }
    95  
    96  INSTANTIATE_TEST_SUITE_P(IPUnboundSockets, IPv4UnboundSocketTest,
    97                           ::testing::ValuesIn(std::vector<SocketKind>{
    98                               IPv4UDPUnboundSocket(0),
    99                               IPv4TCPUnboundSocket(0)}));
   100  
   101  }  // namespace testing
   102  }  // namespace gvisor