test/jdk/java/net/Socket/RejectIPv6.java
author igerasim
Thu, 19 Apr 2018 09:36:06 -0700
changeset 49833 06a6ae39d892
child 54770 62b6e7587b1f
permissions -rw-r--r--
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49833
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     1
/*
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     4
 *
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     8
 *
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    13
 * accompanied this code).
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    14
 *
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    18
 *
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    21
 * questions.
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    22
 */
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    23
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    24
/*
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    25
 * @test
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    26
 * @bug 8201510
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    27
 * @summary Make sure IPv6 addresses are rejected when the System option
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    28
 *          java.net.preferIPv4Stack is set to true
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv4Stack=true RejectIPv6
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    30
 */
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    31
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    32
import java.net.InetSocketAddress;
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    33
import java.net.ServerSocket;
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    34
import java.net.Socket;
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    35
import java.net.SocketException;
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    36
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    37
public class RejectIPv6 {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    38
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    39
    public static void main(String [] argv) throws Throwable {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    40
        ServerSocket serverSocket = new ServerSocket(0);
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    41
        serverSocket.setSoTimeout(1000);
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    42
        int serverPort = serverSocket.getLocalPort();
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    43
        Socket clientSocket = new Socket();
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    44
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    45
        test("bind", () -> clientSocket.bind(
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    46
                new InetSocketAddress("::1", 0)));
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    47
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    48
        test("connect", () -> clientSocket.connect(
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    49
                new InetSocketAddress("::1", serverPort), 1000));
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    50
    }
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    51
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    52
    static void test(String msg, CodeToTest codeToTest) throws Throwable {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    53
        Thread client = new Thread(() ->
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    54
            {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    55
                try {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    56
                    codeToTest.run();
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    57
                    throw new RuntimeException(msg +
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    58
                            " failed to reject IPv6 address");
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    59
                } catch (SocketException ok) {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    60
                } catch (Exception exc) {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    61
                    throw new RuntimeException("unexpected", exc);
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    62
                }
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    63
            });
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    64
        client.start();
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    65
        client.join();
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    66
    }
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    67
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    68
    interface CodeToTest {
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    69
        void run() throws Exception;
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    70
    }
06a6ae39d892 8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
diff changeset
    71
}