github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_ipv6_udp_unbound_netlink.cc (about)

     1  // Copyright 2020 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 "test/syscalls/linux/socket_ipv6_udp_unbound_netlink.h"
    16  
    17  #include <arpa/inet.h>
    18  
    19  #include "gtest/gtest.h"
    20  #include "test/syscalls/linux/socket_netlink_route_util.h"
    21  #include "test/util/capability_util.h"
    22  
    23  namespace gvisor {
    24  namespace testing {
    25  
    26  // Checks that the loopback interface does not consider itself bound to all IPs
    27  // in an associated subnet.
    28  TEST_P(IPv6UDPUnboundSocketNetlinkTest, JoinSubnet) {
    29    SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_ADMIN)));
    30  
    31    // Add an IP address to the loopback interface.
    32    Link loopback_link = ASSERT_NO_ERRNO_AND_VALUE(LoopbackLink());
    33    struct in6_addr addr;
    34    EXPECT_EQ(1, inet_pton(AF_INET6, "2001:db8::1", &addr));
    35    EXPECT_NO_ERRNO(LinkAddLocalAddr(loopback_link.index, AF_INET6,
    36                                     /*prefixlen=*/64, &addr, sizeof(addr)));
    37  
    38    // Binding to an unassigned address but an address that is in the subnet
    39    // associated with the loopback interface should fail.
    40    TestAddress sender_addr("V6NotAssignd1");
    41    sender_addr.addr.ss_family = AF_INET6;
    42    sender_addr.addr_len = sizeof(sockaddr_in6);
    43    EXPECT_EQ(1, inet_pton(AF_INET6, "2001:db8::2",
    44                           reinterpret_cast<sockaddr_in6*>(&sender_addr.addr)
    45                               ->sin6_addr.s6_addr));
    46    auto sock = ASSERT_NO_ERRNO_AND_VALUE(NewSocket());
    47    EXPECT_THAT(
    48        bind(sock->get(), AsSockAddr(&sender_addr.addr), sender_addr.addr_len),
    49        SyscallFailsWithErrno(EADDRNOTAVAIL));
    50  }
    51  
    52  }  // namespace testing
    53  }  // namespace gvisor