Path: blob/master/test/jdk/java/net/ipv6tests/TcpTest.java
41149 views
/*1* Copyright (c) 2003, 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 486882026* @key intermittent27* @summary IPv6 support for Windows XP and 2003 server. This test requires28* binding to the wildcard address, and as such is susceptible29* of intermittent failures caused by port reuse policy.30* @library /test/lib31* @build jdk.test.lib.NetworkConfiguration32* jdk.test.lib.Platform33* @run main TcpTest -d34* @run main/othervm -Djdk.net.usePlainSocketImpl TcpTest -d35*/3637import java.net.*;38import java.io.*;3940public class TcpTest extends Tests {41static ServerSocket server, server1, server2;42static Socket c1, c2, c3, s1, s2, s3;43static InetAddress s1peer, s2peer;4445static InetAddress ia4any;46static InetAddress ia6any;47static Inet6Address ia6addr;48static Inet4Address ia4addr;4950static {51ia6addr = getFirstLocalIPv6Address ();52ia4addr = getFirstLocalIPv4Address ();53try {54ia4any = InetAddress.getByName ("0.0.0.0");55ia6any = InetAddress.getByName ("::0");56} catch (Exception e) {57e.printStackTrace();58}59}6061public static void main (String[] args) throws Exception {62checkDebug(args);63if (ia4addr == null) {64System.out.println ("No IPV4 addresses: exiting test");65return;66}67if (ia6addr == null) {68System.out.println ("No IPV6 addresses: exiting test");69return;70}71dprintln ("Local Addresses");72dprintln (ia4addr.toString());73dprintln (ia6addr.toString());74test1();75test2();76test3();77test4();78}7980/* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */8182static void test1 () throws Exception {83server = new ServerSocket (0);84int port = server.getLocalPort();85// try Ipv6 only86c1 = new Socket ("::1", port);87s1 = server.accept ();88simpleDataExchange (c1, s1);89s1.close ();90c1.close();91// try with both IPv4 and Ipv692c1 = new Socket ("127.0.0.1", port);93c2 = new Socket ("::1", port);94s1 = server.accept();95s2 = server.accept();96s1peer = s1.getInetAddress();97s2peer = s2.getInetAddress();98if (s1peer instanceof Inet6Address) {99t_assert ((s2peer instanceof Inet4Address));100simpleDataExchange (c2, s1);101simpleDataExchange (c1, s2);102} else {103t_assert ((s2peer instanceof Inet6Address));104simpleDataExchange (c1, s1);105simpleDataExchange (c2, s2);106}107c1.close();108c2.close();109s1.close();110s2.close();111server.close ();112System.out.println ("Test1: OK");113}114115/** bind tests:116* 1. bind to specific address IPv4 only (any port)117* 2. bind to specific address IPv6 only (any port)118* 3. bind to any address IPv4 (test collision)119*/120121static void test2 () throws Exception {122123server = new ServerSocket ();124InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0);125server.bind (sadr);126dprintln ("server bound to " + sadr);127int port = server.getLocalPort();128InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port);129130c1 = new Socket (ia4addr, port);131try {132dprintln ("connecting to " + ia6addr);133c2 = new Socket ();134c2.connect (sadr6, 1000);135dprintln ("Unexpected successful connection: " + c2);136// Connect should fail with timeout exception. However, if something else is137// accepting connections, verify it is not our server.138server.setSoTimeout(500);139// Ok if accept() fails because of timeout140while (true) {141// acceptedSocket could be connected to c1, but not c2142try (Socket acceptedSocket = server.accept()) {143dprintln("accepted socket: " + acceptedSocket);144if (acceptedSocket.getRemoteSocketAddress().equals(c2.getLocalSocketAddress()))145throw new RuntimeException("connect to IPv6 address should be refused");146}147}148} catch (IOException e) { }149server.close ();150c1.close ();151if (c2 != null) {152c2.close();153}154155/* now try IPv6 only */156157server = new ServerSocket ();158sadr = new InetSocketAddress (ia6addr, 0);159dprintln ("binding to " + sadr);160server.bind (sadr);161port = server.getLocalPort();162163c1 = new Socket (ia6addr, port);164try {165dprintln ("connecting to " + ia4addr);166c2 = new Socket (ia4addr, port);167dprintln ("Unexpected successful connection: " + c2);168// Connect should fail with timeout exception. However, if something else is169// accepting connections, verify it is not our server.170server.setSoTimeout(500);171// Ok if accept() fails because of timeout172while (true) {173// acceptedSocket could be connected to c1, but not c2174try (Socket acceptedSocket = server.accept()) {175dprintln("accepted socket: " + acceptedSocket);176if (acceptedSocket.getRemoteSocketAddress().equals(c2.getLocalSocketAddress()))177throw new RuntimeException("connect to IPv4 address should be refused");178}179}180} catch (IOException e) { }181server.close ();182c1.close ();183if (c2 != null) {184c2.close();185}186187System.out.println ("Test2: OK");188}189190/* Test timeouts on accept(), connect() */191192static void test3 () throws Exception {193server = new ServerSocket (0);194server.setSoTimeout (5000);195int port = server.getLocalPort();196long t1 = System.currentTimeMillis();197try {198server.accept ();199throw new RuntimeException ("accept should not have returned");200} catch (SocketTimeoutException e) {}201t1 = System.currentTimeMillis() - t1;202checkTime (t1, 5000);203204c1 = new Socket ();205c1.connect (new InetSocketAddress (ia4addr, port), 1000);206s1 = server.accept ();207simpleDataExchange (c1,s1);208c2 = new Socket ();209c2.connect (new InetSocketAddress (ia6addr, port), 1000);210s2 = server.accept ();211simpleDataExchange (c2,s2);212c3 = new Socket ();213c3.connect (new InetSocketAddress (ia6addr, port), 1000);214s3 = server.accept ();215c2.close (); s2.close();216server.close();217simpleDataExchange (c3,s3);218c1.close (); c2.close();219s1.close (); s2.close();220221System.out.println ("Test3: OK");222}223224/* Test: connect to IPv4 mapped address */225226static void test4 () throws Exception {227server = new ServerSocket (0);228int port = server.getLocalPort();229230/* create an IPv4 mapped address corresponding to local host */231232byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0};233byte[] ia4 = ia4addr.getAddress();234b[12] = ia4[0];235b[13] = ia4[1];236b[14] = ia4[2];237b[15] = ia4[3];238239InetAddress dest = InetAddress.getByAddress (b);240c1 = new Socket (dest, port);241s1 = server.accept ();242simpleDataExchange (c1,s1);243c1.close ();244s1.close ();245server.close ();246System.out.println ("Test4: OK");247}248}249250251