test/jdk/java/net/ipv6tests/TcpTest.java
changeset 47216 71c04702a3d5
parent 47017 b19dea422270
child 49799 33dcb9c42f55
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4868820
       
    27  * @key intermittent
       
    28  * @summary IPv6 support for Windows XP and 2003 server
       
    29  * @run main TcpTest -d
       
    30  */
       
    31 
       
    32 import java.net.*;
       
    33 import java.io.*;
       
    34 
       
    35 public class TcpTest extends Tests {
       
    36     static ServerSocket server, server1, server2;
       
    37     static Socket c1, c2, c3, s1, s2, s3;
       
    38     static InetAddress s1peer, s2peer;
       
    39 
       
    40     static InetAddress ia4any;
       
    41     static InetAddress ia6any;
       
    42     static Inet6Address ia6addr;
       
    43     static Inet4Address ia4addr;
       
    44 
       
    45     static {
       
    46         ia6addr = getFirstLocalIPv6Address ();
       
    47         ia4addr = getFirstLocalIPv4Address ();
       
    48         try {
       
    49             ia4any = InetAddress.getByName ("0.0.0.0");
       
    50             ia6any = InetAddress.getByName ("::0");
       
    51         } catch (Exception e) {
       
    52             e.printStackTrace();
       
    53         }
       
    54     }
       
    55 
       
    56     public static void main (String[] args) throws Exception {
       
    57         checkDebug(args);
       
    58         if (ia6addr == null) {
       
    59             System.out.println ("No IPV6 addresses: exiting test");
       
    60             return;
       
    61         }
       
    62         dprintln ("Local Addresses");
       
    63         dprintln (ia4addr.toString());
       
    64         dprintln (ia6addr.toString());
       
    65         test1();
       
    66         test2();
       
    67         test3();
       
    68         test4();
       
    69     }
       
    70 
       
    71     /* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */
       
    72 
       
    73     static void test1 () throws Exception {
       
    74         server = new ServerSocket (0);
       
    75         int port = server.getLocalPort();
       
    76         // try Ipv6 only
       
    77         c1 = new Socket ("::1", port);
       
    78         s1 = server.accept ();
       
    79         simpleDataExchange (c1, s1);
       
    80         s1.close ();
       
    81         c1.close();
       
    82         // try with both IPv4 and Ipv6
       
    83         c1 = new Socket ("127.0.0.1", port);
       
    84         c2 = new Socket ("::1", port);
       
    85         s1 = server.accept();
       
    86         s2 = server.accept();
       
    87         s1peer = s1.getInetAddress();
       
    88         s2peer = s2.getInetAddress();
       
    89         if (s1peer instanceof Inet6Address) {
       
    90             t_assert ((s2peer instanceof Inet4Address));
       
    91             simpleDataExchange (c2, s1);
       
    92             simpleDataExchange (c1, s2);
       
    93         } else {
       
    94             t_assert ((s2peer instanceof Inet6Address));
       
    95             simpleDataExchange (c1, s1);
       
    96             simpleDataExchange (c2, s2);
       
    97         }
       
    98         c1.close();
       
    99         c2.close();
       
   100         s1.close();
       
   101         s2.close();
       
   102         server.close ();
       
   103         System.out.println ("Test1: OK");
       
   104     }
       
   105 
       
   106     /** 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 any address IPv4 (test collision)
       
   110      */
       
   111 
       
   112     static void test2 () throws Exception {
       
   113 
       
   114         server = new ServerSocket ();
       
   115         InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0);
       
   116         server.bind (sadr);
       
   117         dprintln ("server bound to " + sadr);
       
   118         int port = server.getLocalPort();
       
   119         InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port);
       
   120 
       
   121         c1 = new Socket (ia4addr, port);
       
   122         try {
       
   123             dprintln ("connecting to " + ia6addr);
       
   124             c2 = new Socket ();
       
   125             c2.connect (sadr6, 1000);
       
   126             throw new RuntimeException ("connect to IPv6 address should be refused");
       
   127         } catch (IOException e) { }
       
   128         server.close ();
       
   129         c1.close ();
       
   130         if (c2 != null) {
       
   131             c2.close();
       
   132         }
       
   133 
       
   134         /* now try IPv6 only */
       
   135 
       
   136         server = new ServerSocket ();
       
   137         sadr = new InetSocketAddress (ia6addr, 0);
       
   138         dprintln ("binding to " + sadr);
       
   139         server.bind (sadr);
       
   140         port = server.getLocalPort();
       
   141 
       
   142         c1 = new Socket (ia6addr, port);
       
   143         try {
       
   144             dprintln ("connecting to " + ia4addr);
       
   145             c2 = new Socket (ia4addr, port);
       
   146             throw new RuntimeException ("connect to IPv4 address should be refused");
       
   147         } catch (IOException e) { }
       
   148         server.close ();
       
   149         c1.close ();
       
   150         if (c2 != null) {
       
   151             c2.close();
       
   152         }
       
   153 
       
   154         System.out.println ("Test2: OK");
       
   155     }
       
   156 
       
   157     /* Test timeouts on accept(), connect() */
       
   158 
       
   159     static void test3 () throws Exception {
       
   160         server = new ServerSocket (0);
       
   161         server.setSoTimeout (5000);
       
   162         int port = server.getLocalPort();
       
   163         long t1 = System.currentTimeMillis();
       
   164         try {
       
   165             server.accept ();
       
   166             throw new RuntimeException ("accept should not have returned");
       
   167         } catch (SocketTimeoutException e) {}
       
   168         t1 = System.currentTimeMillis() - t1;
       
   169         checkTime (t1, 5000);
       
   170 
       
   171         c1 = new Socket ();
       
   172         c1.connect (new InetSocketAddress (ia4addr, port), 1000);
       
   173         s1 = server.accept ();
       
   174         simpleDataExchange (c1,s1);
       
   175         c2 = new Socket ();
       
   176         c2.connect (new InetSocketAddress (ia6addr, port), 1000);
       
   177         s2 = server.accept ();
       
   178         simpleDataExchange (c2,s2);
       
   179         c3 = new Socket ();
       
   180         c3.connect (new InetSocketAddress (ia6addr, port), 1000);
       
   181         s3 = server.accept ();
       
   182         c2.close (); s2.close();
       
   183         server.close();
       
   184         simpleDataExchange (c3,s3);
       
   185         c1.close (); c2.close();
       
   186         s1.close (); s2.close();
       
   187 
       
   188         System.out.println ("Test3: OK");
       
   189     }
       
   190 
       
   191     /* Test: connect to IPv4 mapped address  */
       
   192 
       
   193     static void test4 () throws Exception {
       
   194         server = new ServerSocket (0);
       
   195         int port = server.getLocalPort();
       
   196 
       
   197         /* create an IPv4 mapped address corresponding to local host */
       
   198 
       
   199         byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0};
       
   200         byte[] ia4 = ia4addr.getAddress();
       
   201         b[12] = ia4[0];
       
   202         b[13] = ia4[1];
       
   203         b[14] = ia4[2];
       
   204         b[15] = ia4[3];
       
   205 
       
   206         InetAddress dest = InetAddress.getByAddress (b);
       
   207         c1 = new Socket (dest, port);
       
   208         s1 = server.accept ();
       
   209         simpleDataExchange (c1,s1);
       
   210         c1.close ();
       
   211         s1.close ();
       
   212         server.close ();
       
   213         System.out.println ("Test4: OK");
       
   214     }
       
   215 }
       
   216