Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/TcpTest.java
38812 views
/*1* Copyright (c) 2003, 2010, 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* @summary IPv6 support for Windows XP and 2003 server27*/2829import java.net.*;30import java.io.*;3132public class TcpTest extends Tests {33static ServerSocket server, server1, server2;34static Socket c1, c2, c3, s1, s2, s3;35static InetAddress s1peer, s2peer;3637static InetAddress ia4any;38static InetAddress ia6any;39static Inet6Address ia6addr;40static Inet4Address ia4addr;4142static {43ia6addr = getFirstLocalIPv6Address ();44ia4addr = getFirstLocalIPv4Address ();45try {46ia4any = InetAddress.getByName ("0.0.0.0");47ia6any = InetAddress.getByName ("::0");48} catch (Exception e) {49e.printStackTrace();50}51}5253public static void main (String[] args) throws Exception {54checkDebug(args);55if (ia6addr == null) {56System.out.println ("No IPV6 addresses: exiting test");57return;58}59dprintln ("Local Addresses");60dprintln (ia4addr.toString());61dprintln (ia6addr.toString());62test1 (0);63test1 (5100);64test2();65test3();66test4();67}6869/* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */7071static void test1 (int port) throws Exception {72server = new ServerSocket (port);73if (port == 0) {74port = server.getLocalPort();75}76// try Ipv6 only77c1 = new Socket ("::1", port);78s1 = server.accept ();79simpleDataExchange (c1, s1);80s1.close ();81c1.close();82// try with both IPv4 and Ipv683c1 = new Socket ("127.0.0.1", port);84c2 = new Socket ("::1", port);85s1 = server.accept();86s2 = server.accept();87s1peer = s1.getInetAddress();88s2peer = s2.getInetAddress();89if (s1peer instanceof Inet6Address) {90t_assert ((s2peer instanceof Inet4Address));91simpleDataExchange (c2, s1);92simpleDataExchange (c1, s2);93} else {94t_assert ((s2peer instanceof Inet6Address));95simpleDataExchange (c1, s1);96simpleDataExchange (c2, s2);97}98c1.close();99c2.close();100s1.close();101s2.close();102server.close ();103System.out.println ("Test1: OK");104}105106/** bind tests:107* 1. bind to specific address IPv4 only (any port)108* 2. bind to specific address IPv6 only (any port)109* 3. bind to specific address IPv4 only (specific port)110* 4. bind to specific address IPv4 only (specific port)111* 5. bind to any address IPv4 (test collision)112*/113114static void test2 () throws Exception {115116server = new ServerSocket ();117InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0);118server.bind (sadr);119dprintln ("server bound to " + sadr);120int port = server.getLocalPort();121InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port);122123c1 = new Socket (ia4addr, port);124try {125dprintln ("connecting to " + ia6addr);126c2 = new Socket ();127c2.connect (sadr6, 1000);128throw new RuntimeException ("connect to IPv6 address should be refused");129} catch (IOException e) { }130server.close ();131c1.close ();132133/* now try IPv6 only */134135server = new ServerSocket ();136sadr = new InetSocketAddress (ia6addr, 0);137dprintln ("binding to " + sadr);138server.bind (sadr);139port = server.getLocalPort();140141c1 = new Socket (ia6addr, port);142try {143c2 = new Socket (ia4addr, port);144throw new RuntimeException ("connect to IPv4 address should be refused");145} catch (IOException e) { }146server.close ();147c1.close ();148149/* now try IPv6 specific port only */150151server = new ServerSocket ();152sadr = new InetSocketAddress (ia6addr, 5200);153server.bind (sadr);154port = server.getLocalPort();155t_assert (port == 5200);156157c1 = new Socket (ia6addr, port);158try {159c2 = new Socket (ia4addr, port);160throw new RuntimeException ("connect to IPv4 address should be refused");161} catch (IOException e) { }162server.close ();163c1.close ();164165/* now try IPv4 specific port only */166167server = new ServerSocket ();168sadr = new InetSocketAddress (ia4addr, 5200);169server.bind (sadr);170port = server.getLocalPort();171t_assert (port == 5200);172173c1 = new Socket (ia4addr, port);174175try {176c2 = new Socket (ia6addr, port);177throw new RuntimeException ("connect to IPv6 address should be refused");178} catch (IOException e) { }179server.accept().close();180c1.close ();181server.close();182System.out.println ("Test2: OK");183}184185/* Test timeouts on accept(), connect() */186187static void test3 () throws Exception {188server = new ServerSocket (0);189server.setSoTimeout (5000);190int port = server.getLocalPort();191long t1 = System.currentTimeMillis();192try {193server.accept ();194throw new RuntimeException ("accept should not have returned");195} catch (SocketTimeoutException e) {}196t1 = System.currentTimeMillis() - t1;197checkTime (t1, 5000);198199c1 = new Socket ();200c1.connect (new InetSocketAddress (ia4addr, port), 1000);201s1 = server.accept ();202simpleDataExchange (c1,s1);203c2 = new Socket ();204c2.connect (new InetSocketAddress (ia6addr, port), 1000);205s2 = server.accept ();206simpleDataExchange (c2,s2);207c3 = new Socket ();208c3.connect (new InetSocketAddress (ia6addr, port), 1000);209s3 = server.accept ();210c2.close (); s2.close();211server.close();212simpleDataExchange (c3,s3);213c1.close (); c2.close();214s1.close (); s2.close();215216System.out.println ("Test3: OK");217}218219/* Test: connect to IPv4 mapped address */220221static void test4 () throws Exception {222server = new ServerSocket (0);223int port = server.getLocalPort();224225/* create an IPv4 mapped address corresponding to local host */226227byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0};228byte[] ia4 = ia4addr.getAddress();229b[12] = ia4[0];230b[13] = ia4[1];231b[14] = ia4[2];232b[15] = ia4[3];233234InetAddress dest = InetAddress.getByAddress (b);235c1 = new Socket (dest, port);236s1 = server.accept ();237simpleDataExchange (c1,s1);238c1.close ();239s1.close ();240server.close ();241System.out.println ("Test4: OK");242}243}244245246