Path: blob/master/test/jdk/java/net/ipv6tests/B6521014.java
41149 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6521014 654342826* @summary IOException thrown when Socket tries to bind to an local IPv6 address on SuSE Linux27* @library /test/lib28* @build jdk.test.lib.NetworkConfiguration29* jdk.test.lib.Platform30* @run main B652101431*/3233import java.net.*;34import java.io.*;35import java.util.*;36import jdk.test.lib.NetworkConfiguration;3738/*39*40* What this testcase is to test is a (weird) coupling through the41* cached_scope_id field of java.net.Inet6Address. Native method42* NET_InetAddressToSockaddr as in Linux platform will try to write43* and read this field, therefore Inet6Address becomes 'stateful'.44* So the coupling. Certain executive order, e.g. two methods use45* the same Inet6Address instance as illustrated in this test case,46* will show side effect of such coupling.47*48* And on Windows, NET_InetAddressToSockaddr() did not assign appropriate49* sin6_scope_id value to sockaddr_in6 structure if there's no one coming50* with Inet6Address instance, which caused bind exception. This test use51* link-local address without %scope suffix, so it is also going to test52* that.53*54*/55public class B6521014 {5657static Inet6Address removeScope(Inet6Address addr) {58try {59return (Inet6Address)InetAddress.getByAddress(addr.getAddress());60} catch (IOException e) {61throw new UncheckedIOException(e);62}63}6465static Optional<Inet6Address> getLocalAddr() throws Exception {66return NetworkConfiguration.probe()67.ip6Addresses()68.filter(Inet6Address::isLinkLocalAddress)69.findFirst();70}7172static void test1(Inet6Address sin) throws Exception {73try (ServerSocket ssock = createBoundServer(sin);74Socket sock = new Socket()) {75int port = ssock.getLocalPort();76sock.connect(new InetSocketAddress(sin, port), 100);77} catch (SocketTimeoutException e) {78// time out exception is okay79System.out.println("timed out when connecting.");80}81}8283static void test2(Inet6Address sin) throws Exception {84try (ServerSocket ssock = createBoundServer(sin);85Socket sock = new Socket()) {86int port = ssock.getLocalPort();87ssock.setSoTimeout(100);88sock.bind(new InetSocketAddress(sin, 0));89sock.connect(new InetSocketAddress(sin, port), 100);90} catch (SocketTimeoutException expected) {91// time out exception is okay92System.out.println("timed out when connecting.");93}94}9596static ServerSocket createBoundServer(Inet6Address sin) throws IOException {97ServerSocket ss = new ServerSocket();98InetSocketAddress address = new InetSocketAddress(sin, 0);99ss.bind(address);100return ss;101}102103public static void main(String[] args) throws Exception {104Optional<Inet6Address> oaddr = getLocalAddr();105if (!oaddr.isPresent()) {106System.out.println("Cannot find a link-local address.");107return;108}109110Inet6Address addr = oaddr.get();111System.out.println("Using " + addr);112test1(addr);113test2(addr);114}115}116117118