test/jdk/java/net/Socket/LinkLocal.java
author aeubanks
Tue, 16 Apr 2019 13:06:23 -0700
changeset 54770 62b6e7587b1f
parent 49431 5812849b5027
child 54811 9db7c0f561a6
permissions -rw-r--r--
8220673: Add test library support for determining platform IP support Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Connecting to a link-local IPv6 address should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          causes a SocketException to be thrown.
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 44671
diff changeset
    30
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    31
 * @build jdk.test.lib.NetworkConfiguration
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45186
diff changeset
    32
 *        jdk.test.lib.Platform
44671
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
    33
 * @run main LinkLocal
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    34
 * @run main/othervm -Djava.net.preferIPv4Stack=true LinkLocal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
45186
387a39577f09 8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents: 44671
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
44671
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
    38
import java.util.List;
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
    39
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    41
import jdk.test.lib.NetworkConfiguration;
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    42
import jdk.test.lib.net.IPSupport;
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class LinkLocal {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int testCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void TcpTest(InetAddress ia) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println("Test " + testCount + ": TCP connect to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Create ServerSocket on wildcard address and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * try to connect Socket to link-local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Socket s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            s.connect(new InetSocketAddress(ia, ss.getLocalPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.out.println("Test passed - connection established.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // connection was established so accept it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Socket s2 = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.out.println("Test failed: " + e);
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    72
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    73
            s.close();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    74
            ss.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static void UdpTest(InetAddress ia, boolean connected) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println("Test " + testCount + ": UDP connect to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.out.println("Test " + testCount + ": UDP send to " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        DatagramSocket ds1 = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        DatagramSocket ds2 = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            byte b[] = "Hello".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            DatagramPacket p = new DatagramPacket(b, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                System.out.println("DatagramSocket connected.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                p.setAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                p.setPort(ds2.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ds1.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println("Packet has been sent.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   106
            ds2.setSoTimeout(5000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            ds2.receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.println("Test passed - packet received.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            failed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println("Test failed: " + e);
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   112
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   113
            ds1.close();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   114
            ds2.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void TestAddress(InetAddress ia) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        TcpTest(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        UdpTest(ia, true);      /* unconnected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        UdpTest(ia, false);     /* connected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static void main(String args[]) throws Exception {
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
   125
        IPSupport.skipIfCurrentConfigurationIsInvalid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * If an argument is provided ensure that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * a link-local IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            InetAddress ia = InetAddress.getByName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if ( !(ia instanceof Inet6Address) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                !ia.isLinkLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new Exception(ia +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        " is not a link-local IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            TestAddress(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * If no argument is provided then enumerate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * local addresses and run the test on each link-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (args.length == 0) {
44671
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   149
            List<Inet6Address> addrs = NetworkConfiguration.probe()
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   150
                    .ip6Addresses()
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   151
                    .filter(Inet6Address::isLinkLocalAddress)
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   152
                    .collect(Collectors.toList());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   153
44671
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   154
            for (Inet6Address addr : addrs) {
d1d011d4654d 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents: 7668
diff changeset
   155
                TestAddress(addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * Print results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (testCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.out.println("No link-local IPv6 addresses - test skipped!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("**************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            System.out.println(testCount + " test(s) executed, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                failed + " failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (failed > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new Exception( failed + " test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}