jdk/test/java/net/Socket/LinkLocal.java
author chegar
Thu, 11 Mar 2010 17:50:30 +0000
changeset 5147 96642e83ad41
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy Reviewed-by: michaelm, chegar Contributed-by: damjan.jov@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4469866
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Connecting to a link-local IPv6 address should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          causes a SocketException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class LinkLocal {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static int testCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static int failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static void TcpTest(InetAddress ia) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        System.out.println("Test " + testCount + ": TCP connect to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
         * Create ServerSocket on wildcard address and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
         * try to connect Socket to link-local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Socket s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            s.connect(new InetSocketAddress(ia, ss.getLocalPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println("Test passed - connection established.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // connection was established so accept it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            Socket s2 = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println("Test failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static void UdpTest(InetAddress ia, boolean connected) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println("Test " + testCount + ": UDP connect to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.out.println("Test " + testCount + ": UDP send to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        DatagramSocket ds1 = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        DatagramSocket ds2 = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            byte b[] = "Hello".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            DatagramPacket p = new DatagramPacket(b, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                System.out.println("DatagramSocket connected.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                p.setAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                p.setPort(ds2.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ds1.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println("Packet has been sent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ds2.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ds2.receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.println("Test passed - packet received.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println("Test failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ds1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ds2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void TestAddress(InetAddress ia) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        TcpTest(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        UdpTest(ia, true);      /* unconnected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        UdpTest(ia, false);     /* connected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * If an argument is provided ensure that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * a link-local IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            InetAddress ia = InetAddress.getByName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if ( !(ia instanceof Inet6Address) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                !ia.isLinkLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new Exception(ia +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        " is not a link-local IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            TestAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * If no argument is provided then enumerate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * local addresses and run the test on each link-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                NetworkInterface ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                Enumeration addrs = ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    InetAddress addr = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    if (addr instanceof Inet6Address &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        addr.isLinkLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        TestAddress(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * Print results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (testCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println("No link-local IPv6 addresses - test skipped!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.out.println(testCount + " test(s) executed, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                failed + " failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (failed > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new Exception( failed + " test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}