test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/Launcher.java
author michaelm
Fri, 30 Nov 2018 10:29:58 +0000
changeset 52778 dbbf46b13d52
parent 49128 97288886180c
child 58295 7973073dd048
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0 Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49128
97288886180c 8198834: (ch) Enable java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java on linux-x64
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 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
 * A Launcher to launch a java process with its standard input, output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * and error streams connected to a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 23041
diff changeset
    29
import java.net.InetAddress;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 23041
diff changeset
    30
import java.net.InetSocketAddress;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 23041
diff changeset
    31
import java.nio.channels.DatagramChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 23041
diff changeset
    32
import java.nio.channels.ServerSocketChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 23041
diff changeset
    33
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Launcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static {
49128
97288886180c 8198834: (ch) Enable java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java on linux-x64
bpb
parents: 47216
diff changeset
    38
        System.loadLibrary("InheritedChannel");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static native void launch0(String cmdarray[], int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static void launch(String className, String options[], String args[], int fd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // java [-options] class [args...]
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    45
        int optsLen = (options == null) ? 0 : options.length;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    46
        int argsLen = (args == null) ? 0 : args.length;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    47
        int len = 1 + optsLen + 1 + argsLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String cmdarray[] = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int pos = 0;
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    50
        cmdarray[pos++] = Util.javaCommand();
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    51
        if (options != null) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    52
            for (String opt: options) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    53
                cmdarray[pos++] = opt;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    54
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        cmdarray[pos++] = className;
23041
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    57
        if (args != null) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    58
            for (String arg: args) {
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    59
                cmdarray[pos++] = arg;
1aa7ce9cd0f1 8010194: java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh fails with "Timed out waiting ..." (sol)
alanb
parents: 23010
diff changeset
    60
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        launch0(cmdarray, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
52778
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    65
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    66
    /**
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    67
     * Launch 'java' with specified class using a UnixDomainSocket pair linking calling
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    68
     * process to the child VM. UnixDomainSocket is a simplified interface to PF_UNIX sockets
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    69
     * which supports byte a time reads and writes.
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    70
     */
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    71
    public static UnixDomainSocket launchWithUnixDomainSocket(String className) throws IOException {
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    72
        UnixDomainSocket[] socks = UnixDomainSocket.socketpair();
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    73
        launch(className, null, null, socks[0].fd());
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    74
        socks[0].close();
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    75
        return socks[1];
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    76
    }
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 49128
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The launched process will inherit a connected TCP socket. The remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * will be the SocketChannel returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                      ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        SocketChannel sc1 = SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        SocketChannel sc2 = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        launch(className, options, args, Util.getFD(sc2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return sc1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static SocketChannel launchWithSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return launchWithSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static SocketChannel launchWithSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return launchWithSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The launched process will inherited a TCP listener socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Once launched this method tries to connect to service. If a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * can be established a SocketChannel, connected to the service, is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int port = ssc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        launch(className, options, args, Util.getFD(ssc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static SocketChannel launchWithServerSocketChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return launchWithServerSocketChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static SocketChannel launchWithServerSocketChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return launchWithServerSocketChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Launch 'java' with specified class with the specified arguments (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The launch process will inherited a bound UDP socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Once launched this method creates a DatagramChannel and "connects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * it to the service. The created DatagramChannel is then returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * As it is connected any packets sent from the socket will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * sent to the service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        dc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int port = dc.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        launch(className, options, args, Util.getFD(dc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        dc = DatagramChannel.open();
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   149
        InetAddress address = InetAddress.getLocalHost();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   150
        if (address.isLoopbackAddress()) {
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   151
            address = InetAddress.getLoopbackAddress();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   152
        }
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 5506
diff changeset
   153
        InetSocketAddress isa = new InetSocketAddress(address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        dc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static DatagramChannel launchWithDatagramChannel(String className, String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return launchWithDatagramChannel(className, null, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static DatagramChannel launchWithDatagramChannel(String className) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return launchWithDatagramChannel(className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}