< prev index next >

test/java/net/ipv6tests/B6521014.java

Print this page

        

*** 23,39 **** /* * @test * @bug 6521014 6543428 * @summary IOException thrown when Socket tries to bind to an local IPv6 address on SuSE Linux */ - import java.net.*; import java.io.*; import java.util.*; ! /* * * What this testcase is to test is a (weird) coupling through the * cached_scope_id field of java.net.Inet6Address. Native method --- 23,41 ---- /* * @test * @bug 6521014 6543428 * @summary IOException thrown when Socket tries to bind to an local IPv6 address on SuSE Linux + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main B6521014 */ import java.net.*; import java.io.*; import java.util.*; ! import jdk.testlibrary.NetworkConfiguration; /* * * What this testcase is to test is a (weird) coupling through the * cached_scope_id field of java.net.Inet6Address. Native method
*** 50,126 **** * that. * */ public class B6521014 { ! static InetAddress sin; ! ! static Inet6Address getLocalAddr () throws Exception { ! Enumeration e = NetworkInterface.getNetworkInterfaces(); ! while (e.hasMoreElements()) { ! NetworkInterface ifc = (NetworkInterface) e.nextElement(); ! if (!ifc.isUp()) ! continue; ! Enumeration addrs = ifc.getInetAddresses(); ! while (addrs.hasMoreElements()) { ! InetAddress a = (InetAddress)addrs.nextElement(); ! if (a instanceof Inet6Address) { ! Inet6Address ia6 = (Inet6Address) a; ! if (ia6.isLinkLocalAddress()) { ! // remove %scope suffix ! return (Inet6Address)InetAddress.getByAddress(ia6.getAddress()); ! } ! } ! } } - return null; } ! static void test1() throws Exception { ! ServerSocket ssock; ! Socket sock; ! int port; ! ssock = new ServerSocket(0); ! port = ssock.getLocalPort(); ! sock = new Socket(); ! try { sock.connect(new InetSocketAddress(sin, port), 100); } catch (SocketTimeoutException e) { // time out exception is okay System.out.println("timed out when connecting."); } } ! static void test2() throws Exception { ! Socket sock; ! ServerSocket ssock; ! int port; ! ! ssock = new ServerSocket(0); ssock.setSoTimeout(100); - port = ssock.getLocalPort(); - sock = new Socket(); sock.bind(new InetSocketAddress(sin, 0)); - try { sock.connect(new InetSocketAddress(sin, port), 100); ! } catch (SocketTimeoutException e) { // time out exception is okay System.out.println("timed out when connecting."); } } public static void main(String[] args) throws Exception { ! sin = getLocalAddr(); ! if (sin == null) { System.out.println("Cannot find a link-local address."); return; } ! try { ! test1(); ! test2(); ! } catch (IOException e) { ! throw new RuntimeException("Test failed: cannot create socket.", e); ! } } } --- 52,109 ---- * that. * */ public class B6521014 { ! static Inet6Address removeScope(Inet6Address addr) { ! try { ! return (Inet6Address)InetAddress.getByAddress(addr.getAddress()); ! } catch (IOException e) { ! throw new UncheckedIOException(e); } } ! static Optional<Inet6Address> getLocalAddr() throws Exception { ! return NetworkConfiguration.probe() ! .ip6Addresses() ! .filter(Inet6Address::isLinkLocalAddress) ! .map(B6521014::removeScope) ! .findFirst(); ! } ! static void test1(Inet6Address sin) throws Exception { ! try (ServerSocket ssock = new ServerSocket(0); ! Socket sock = new Socket()) { ! int port = ssock.getLocalPort(); sock.connect(new InetSocketAddress(sin, port), 100); } catch (SocketTimeoutException e) { // time out exception is okay System.out.println("timed out when connecting."); } } ! static void test2(Inet6Address sin) throws Exception { ! try (ServerSocket ssock = new ServerSocket(0); ! Socket sock = new Socket()) { ! int port = ssock.getLocalPort(); ssock.setSoTimeout(100); sock.bind(new InetSocketAddress(sin, 0)); sock.connect(new InetSocketAddress(sin, port), 100); ! } catch (SocketTimeoutException expected) { // time out exception is okay System.out.println("timed out when connecting."); } } public static void main(String[] args) throws Exception { ! Optional<Inet6Address> oaddr = getLocalAddr(); ! if (!oaddr.isPresent()) { System.out.println("Cannot find a link-local address."); return; } ! Inet6Address addr = oaddr.get(); ! System.out.println("Using " + addr); ! test1(addr); ! test2(addr); } }
< prev index next >