Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/B6521014.java
38812 views
/*1* Copyright (c) 2007, 2013, 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*/282930import java.net.*;31import java.io.*;32import java.util.*;333435/*36*37* What this testcase is to test is a (weird) coupling through the38* cached_scope_id field of java.net.Inet6Address. Native method39* NET_InetAddressToSockaddr as in Linux platform will try to write40* and read this field, therefore Inet6Address becomes 'stateful'.41* So the coupling. Certain executive order, e.g. two methods use42* the same Inet6Address instance as illustrated in this test case,43* will show side effect of such coupling.44*45* And on Windows, NET_InetAddressToSockaddr() did not assign appropriate46* sin6_scope_id value to sockaddr_in6 structure if there's no one coming47* with Inet6Address instance, which caused bind exception. This test use48* link-local address without %scope suffix, so it is also going to test49* that.50*51*/52public class B6521014 {5354static InetAddress sin;5556static Inet6Address getLocalAddr () throws Exception {57Enumeration e = NetworkInterface.getNetworkInterfaces();58while (e.hasMoreElements()) {59NetworkInterface ifc = (NetworkInterface) e.nextElement();60if (!ifc.isUp())61continue;62Enumeration addrs = ifc.getInetAddresses();63while (addrs.hasMoreElements()) {64InetAddress a = (InetAddress)addrs.nextElement();65if (a instanceof Inet6Address) {66Inet6Address ia6 = (Inet6Address) a;67if (ia6.isLinkLocalAddress()) {68// remove %scope suffix69return (Inet6Address)InetAddress.getByAddress(ia6.getAddress());70}71}72}73}74return null;75}7677static void test1() throws Exception {78ServerSocket ssock;79Socket sock;80int port;8182ssock = new ServerSocket(0);83port = ssock.getLocalPort();84sock = new Socket();85try {86sock.connect(new InetSocketAddress(sin, port), 100);87} catch (SocketTimeoutException e) {88// time out exception is okay89System.out.println("timed out when connecting.");90}91}9293static void test2() throws Exception {94Socket sock;95ServerSocket ssock;96int port;9798ssock = new ServerSocket(0);99ssock.setSoTimeout(100);100port = ssock.getLocalPort();101sock = new Socket();102sock.bind(new InetSocketAddress(sin, 0));103try {104sock.connect(new InetSocketAddress(sin, port), 100);105} catch (SocketTimeoutException e) {106// time out exception is okay107System.out.println("timed out when connecting.");108}109}110111public static void main(String[] args) throws Exception {112sin = getLocalAddr();113if (sin == null) {114System.out.println("Cannot find a link-local address.");115return;116}117118try {119test1();120test2();121} catch (IOException e) {122throw new RuntimeException("Test failed: cannot create socket.", e);123}124}125}126127128