test/jdk/java/net/Socket/B6210227.java
author dfuchs
Wed, 10 Jul 2019 22:33:23 +0200
changeset 55649 ad8e3b295615
parent 54811 9db7c0f561a6
permissions -rw-r--r--
8227539: Replace wildcard address with loopback or local host in tests - part 20 Summary: Update some tests to stop using the wildcard address. Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     1
/*
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     4
 *
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     8
 *
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    13
 * accompanied this code).
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    14
 *
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    18
 *
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    21
 * questions.
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    22
 */
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    23
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    24
/**
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    25
 * @test
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    26
 * @bug 6210227
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    27
 * @library /test/lib
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    28
 * @summary  REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP.
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    29
 *           This test requires binding to the wildcard address.
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    30
 * @run main B6210227
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    31
 * @run main/othervm -Djava.net.preferIPv4Stack=true B6210227
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    32
 * @run main/othervm -Djava.net.preferIPv6Addresses=true B6210227
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    33
 */
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    34
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    35
import java.util.*;
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    36
import java.net.*;
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    37
import java.util.stream.IntStream;
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    38
import java.util.stream.Collectors;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    39
import jdk.test.lib.net.IPSupport;
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    40
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    41
public class B6210227 {
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    43
    {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
    44
        IPSupport.throwSkippedExceptionIfNonOperational();
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    45
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    46
        ServerSocket ss = new ServerSocket(0);
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    47
        int port = ss.getLocalPort();
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    48
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    49
        try {
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    50
            InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    51
            Socket s = new Socket();
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    52
            s.connect( isa, 1000 );
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    53
            InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    54
            String      sLocalHostname = iaLocal.getHostName();
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    55
            byte[]      address = iaLocal.getAddress();
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    56
            if (isWildcard(address)) {
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    57
                if (iaLocal instanceof Inet6Address) {
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    58
                    String msg = IntStream.range(0, address.length)
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    59
                        .mapToObj(i -> "0").collect(Collectors.joining(":"));
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    60
                    throw new RuntimeException(msg + " returned");
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    61
                } else {
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    62
                    throw new RuntimeException ("0.0.0.0 returned");
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    63
                }
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    64
            }
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    65
            System.out.println("local hostname is "+sLocalHostname );
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    66
        } catch(Exception e) {
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    67
            System.out.println("Exception happened");
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    68
            throw e;
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    69
        } finally {
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    70
            ss.close();
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    71
        }
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    72
    }
55649
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    73
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    74
    private static boolean isWildcard(byte[] bytes) {
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    75
        for (int i = 0; i < bytes.length ; i++) {
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    76
            if (bytes[i] != 0) return false;
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    77
        }
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    78
        return true;
ad8e3b295615 8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents: 54811
diff changeset
    79
    }
14672
7498e72592b5 8003597: TEST_BUG: Eliminate dependency on javaweb from closed net tests
robm
parents:
diff changeset
    80
}