test/jdk/java/lang/ProcessBuilder/Basic.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58566 afbb7ec84228
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55257
442b86eb633c 8224905: java/lang/ProcessBuilder/Basic.java#id1 failed with stream closed
rriggs
parents: 52821
diff changeset
     2
 * Copyright (c) 2003, 2019, 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: 5168
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5168
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5168
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 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
    28
 *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
29381
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
    29
 *      4947220 7018606 7034570 4244896 5049299 8003488 8054494 8058464
55257
442b86eb633c 8224905: java/lang/ProcessBuilder/Basic.java#id1 failed with stream closed
rriggs
parents: 52821
diff changeset
    30
 *      8067796 8224905
44107
a3d145e4f03c 8176337: Mark several tests as intermittently failing
mli
parents: 42338
diff changeset
    31
 * @key intermittent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Basic tests for Process and Environment Variable code
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41825
diff changeset
    33
 * @modules java.base/java.lang:open
5600
8ca34998a6b3 6943915: Adjust jdk/test/Makefile to deal with .dll and .so libraries needing execute permissions
ohair
parents: 5168
diff changeset
    34
 * @run main/othervm/timeout=300 Basic
19399
e2e5122cd62e 5049299: (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
robm
parents: 15265
diff changeset
    35
 * @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
52364
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    39
/*
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    40
 * @test
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    41
 * @modules java.base/java.lang:open
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    42
 * @requires (os.family == "linux")
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    43
 * @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=posix_spawn Basic
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    44
 */
750b500ef4de 8212828: (process) Provide a way for Runtime.exec to use posix_spawn on linux
stuefe
parents: 51422
diff changeset
    45
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    46
import java.lang.ProcessBuilder.Redirect;
30956
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
    47
import java.lang.ProcessHandle;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    48
import static java.lang.ProcessBuilder.Redirect.*;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.*;
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
    51
import java.lang.reflect.Field;
22288
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
    52
import java.nio.file.Files;
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
    53
import java.nio.file.Paths;
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
    54
import java.nio.file.StandardCopyOption;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.*;
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
    56
import java.util.concurrent.CountDownLatch;
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
    57
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.regex.Pattern;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
    60
import java.util.regex.Matcher;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import static java.lang.System.getenv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import static java.lang.System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import static java.lang.Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import static java.util.AbstractMap.SimpleImmutableEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    68
    /* used for Windows only */
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    69
    static final String systemRoot = System.getenv("SystemRoot");
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    70
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
    71
    /* used for Mac OS X only */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
    72
    static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
    73
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
    74
    /* used for AIX only */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
    75
    static final String libpath = System.getenv("LIBPATH");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
    76
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    77
    /**
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    78
     * Returns the number of milliseconds since time given by
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    79
     * startNanoTime, which must have been previously returned from a
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    80
     * call to {@link System.nanoTime()}.
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    81
     */
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    82
    private static long millisElapsedSince(long startNanoTime) {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    83
        return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    84
    }
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static String commandOutput(Reader r) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        while ((c = r.read()) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (c != '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                sb.append((char) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static String commandOutput(Process p) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        check(p.getInputStream()  == p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        check(p.getOutputStream() == p.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        check(p.getErrorStream()  == p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String output = commandOutput(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        equal(p.exitValue(), 0);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   103
        // The debug/fastdebug versions of the VM may write some warnings to stdout
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   104
        // (i.e. "Warning:  Cannot open log file: hotspot.log" if the VM is started
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   105
        // in a directory without write permissions). These warnings will confuse tests
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   106
        // which match the entire output of the child process so better filter them out.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   107
        return output.replaceAll("Warning:.*\\n", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static String commandOutput(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return commandOutput(pb.start());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            String commandline = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            for (String arg : pb.command())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                commandline += " " + arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("Exception trying to run process: " + commandline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static String commandOutput(String...command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return commandOutput(Runtime.getRuntime().exec(command));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String commandline = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            for (String arg : command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                commandline += " " + arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("Exception trying to run process: " + commandline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static void checkCommandOutput(ProcessBuilder pb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                           String expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                           String failureMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String got = commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        check(got.equals(expected),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
              failureMsg + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              "Expected: \"" + expected + "\"\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
              "Got: \"" + got + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static String absolutifyPath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (String file : path.split(File.pathSeparator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (sb.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                sb.append(File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            sb.append(new File(file).getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // compare windows-style, by canonicalizing to upper case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // not lower case as String.compareToIgnoreCase does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static class WindowsComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        implements Comparator<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public int compare(String x, String y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return x.toUpperCase(Locale.US)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                .compareTo(y.toUpperCase(Locale.US));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static String sortedLines(String lines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        String[] arr = lines.split("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        List<String> ls = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        for (String s : arr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            ls.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Collections.sort(ls, new WindowsComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (String s : ls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            sb.append(s + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static void compareLinesIgnoreCase(String lines1, String lines2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (! (sortedLines(lines1).equalsIgnoreCase(sortedLines(lines2)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            String dashes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "-----------------------------------------------------";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            out.print(sortedLines(lines1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            out.print(sortedLines(lines2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            out.println("sizes: " + sortedLines(lines1).length() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        " " + sortedLines(lines2).length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            fail("Sorted string contents differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static final Runtime runtime = Runtime.getRuntime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static final String[] winEnvCommand = {"cmd.exe", "/c", "set"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static String winEnvFilter(String env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return env.replaceAll("\r", "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            .replaceAll("(?m)^(?:COMSPEC|PROMPT|PATHEXT)=.*\n","");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static String unixEnvProg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return new File("/usr/bin/env").canExecute() ? "/usr/bin/env"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            : "/bin/env";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private static String nativeEnv(String[] env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                return winEnvFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    (commandOutput(runtime.exec(winEnvCommand, env)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return commandOutput(runtime.exec(unixEnvProg(), env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static String nativeEnv(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                pb.command(winEnvCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return winEnvFilter(commandOutput(pb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                pb.command(new String[]{unixEnvProg()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static void checkSizes(Map<String,String> environ, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            equal(size, environ.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            equal(size, environ.entrySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            equal(size, environ.keySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            equal(size, environ.values().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            boolean isEmpty = (size == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            equal(isEmpty, environ.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            equal(isEmpty, environ.entrySet().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            equal(isEmpty, environ.keySet().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            equal(isEmpty, environ.values().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private interface EnvironmentFrobber {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        void doIt(Map<String,String> environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static void testVariableDeleter(EnvironmentFrobber fooDeleter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            environ.put("Foo", "BAAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            fooDeleter.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            equal(environ.get("Foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            equal(environ.remove("Foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static void testVariableAdder(EnvironmentFrobber fooAdder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            environ.remove("Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            fooAdder.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            equal(environ.get("Foo"), "Bahrein");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static void testVariableModifier(EnvironmentFrobber fooModifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            environ.put("Foo","OldValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            fooModifier.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            equal(environ.get("Foo"), "NewValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private static void printUTF8(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        out.write(s.getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private static String getenvAsString(Map<String,String> environment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        StringBuilder sb = new StringBuilder();
12882
35d381df9422 7174723: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing [win]
alanb
parents: 12538
diff changeset
   284
        environment = new TreeMap<>(environment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        for (Map.Entry<String,String> e : environment.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // Ignore magic environment variables added by the launcher
46044
55b0a570d3d7 8185365: Tidy up leftover dead code after JDK-8136570
martin
parents: 44640
diff changeset
   287
            if (! e.getKey().equals("LD_LIBRARY_PATH"))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                sb.append(e.getKey())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    .append('=')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    .append(e.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    .append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   295
    static void print4095(OutputStream s, byte b) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        byte[] bytes = new byte[4095];
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   297
        Arrays.fill(bytes, b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        s.write(bytes);         // Might hang!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   301
    static void checkPermissionDenied(ProcessBuilder pb) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   302
        try {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   303
            pb.start();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   304
            fail("Expected IOException not thrown");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   305
        } catch (IOException e) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   306
            String m = e.getMessage();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   307
            if (EnglishUnix.is() &&
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   308
                ! matches(m, "Permission denied"))
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   309
                unexpected(e);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   310
        } catch (Throwable t) { unexpected(t); }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   311
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   312
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public static class JavaChild {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        public static void main(String args[]) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            String action = args[0];
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   316
            if (action.equals("sleep")) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   317
                Thread.sleep(10 * 60 * 1000L);
30956
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
   318
            } else if (action.equals("pid")) {
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 44107
diff changeset
   319
                System.out.println(ProcessHandle.current().pid());
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   320
            } else if (action.equals("testIO")) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   321
                String expected = "standard input";
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   322
                char[] buf = new char[expected.length()+1];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   323
                int n = new InputStreamReader(System.in).read(buf,0,buf.length);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   324
                if (n != expected.length())
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   325
                    System.exit(5);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   326
                if (! new String(buf,0,n).equals(expected))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   327
                    System.exit(5);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   328
                System.err.print("standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   329
                System.out.print("standard output");
20191
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   330
            } else if (action.equals("testInheritIO")
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   331
                    || action.equals("testRedirectInherit")) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   332
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   333
                childArgs.add("testIO");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   334
                ProcessBuilder pb = new ProcessBuilder(childArgs);
20191
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   335
                if (action.equals("testInheritIO"))
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   336
                    pb.inheritIO();
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   337
                else
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
   338
                    redirectIO(pb, INHERIT, INHERIT, INHERIT);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   339
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   340
                if (! r.out().equals(""))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   341
                    System.exit(7);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   342
                if (! r.err().equals(""))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   343
                    System.exit(8);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   344
                if (r.exitValue() != 0)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   345
                    System.exit(9);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   346
            } else if (action.equals("System.getenv(String)")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                String val = System.getenv(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                printUTF8(val == null ? "null" : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } else if (action.equals("System.getenv(\\u1234)")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                String val = System.getenv("\u1234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                printUTF8(val == null ? "null" : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            } else if (action.equals("System.getenv()")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                printUTF8(getenvAsString(System.getenv()));
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   354
            } else if (action.equals("ArrayOOME")) {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   355
                Object dummy;
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   356
                switch(new Random().nextInt(3)) {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   357
                case 0: dummy = new Integer[Integer.MAX_VALUE]; break;
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   358
                case 1: dummy = new double[Integer.MAX_VALUE];  break;
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   359
                case 2: dummy = new byte[Integer.MAX_VALUE][];  break;
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   360
                default: throw new InternalError();
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   361
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            } else if (action.equals("pwd")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                printUTF8(new File(System.getProperty("user.dir"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                          .getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            } else if (action.equals("print4095")) {
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   366
                print4095(System.out, (byte) '!');
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   367
                print4095(System.err, (byte) 'E');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                System.exit(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } else if (action.equals("OutErr")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                // You might think the system streams would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                // buffered, and in fact they are implemented using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                // BufferedOutputStream, but each and every print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // causes immediate operating system I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                System.out.print("out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                System.err.print("err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                System.out.print("out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                System.err.print("err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else if (action.equals("null PATH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                equal(System.getenv("PATH"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                check(new File("/bin/true").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                check(new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                ProcessBuilder pb1 = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                ProcessBuilder pb2 = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                ProcessResults r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                for (final ProcessBuilder pb :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                         new ProcessBuilder[] {pb1, pb2}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    pb.command("true");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   390
                    equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    pb.command("false");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   393
                    equal(run(pb).exitValue(), False.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (failed != 0) throw new Error("null PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            } else if (action.equals("PATH search algorithm")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                equal(System.getenv("PATH"), "dir1:dir2:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                check(new File("/bin/true").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                check(new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                String[] cmd = {"prog"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                ProcessBuilder pb1 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                ProcessBuilder pb2 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                ProcessBuilder pb3 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                pb3.environment().remove("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                for (final ProcessBuilder pb :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                         new ProcessBuilder[] {pb1, pb2, pb3}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        // Not on PATH at all; directories don't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            pb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                ! matches(m, "No such file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        // Not on PATH at all; directories exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        new File("dir1").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        new File("dir2").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            pb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                ! matches(m, "No such file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        // Can't execute a directory -- permission denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        // Report EACCES errno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        new File("dir1/prog").mkdirs();
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   438
                        checkPermissionDenied(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        // continue searching if EACCES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        copy("/bin/true", "dir2/prog");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   442
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        new File("dir2/prog").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        copy("/bin/true", "dir1/prog");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   448
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   450
                        // Check empty PATH component means current directory.
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   451
                        //
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   452
                        // While we're here, let's test different kinds of
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   453
                        // Unix executables, and PATH vs explicit searching.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        new File("dir2/prog").delete();
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   456
                        for (String[] command :
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   457
                                 new String[][] {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   458
                                     new String[] {"./prog"},
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   459
                                     cmd}) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   460
                            pb.command(command);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   461
                            File prog = new File("./prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   462
                            // "Normal" binaries
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   463
                            copy("/bin/true", "./prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   464
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   465
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   466
                            copy("/bin/false", "./prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   467
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   468
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   469
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   470
                            // Interpreter scripts with #!
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   471
                            setFileContents(prog, "#!/bin/true\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   472
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   473
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   474
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   475
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   476
                            setFileContents(prog, "#!/bin/false\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   477
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   478
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   479
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   480
                            // Traditional shell scripts without #!
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   481
                            setFileContents(prog, "exec /bin/true\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   482
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   483
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   484
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   485
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   486
                            setFileContents(prog, "exec /bin/false\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   487
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   488
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   489
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   490
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   491
                        }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   492
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   493
                        // Test Unix interpreter scripts
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   494
                        File dir1Prog = new File("dir1/prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   495
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   496
                        pb.command(new String[] {"prog", "world"});
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   497
                        setFileContents(dir1Prog, "#!/bin/echo hello\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   498
                        checkPermissionDenied(pb);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   499
                        dir1Prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   500
                        equal(run(pb).out(), "hello dir1/prog world\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   501
                        equal(run(pb).exitValue(), True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   502
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   503
                        pb.command(cmd);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   504
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   505
                        // Test traditional shell scripts without #!
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   506
                        setFileContents(dir1Prog, "/bin/echo \"$@\"\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   507
                        pb.command(new String[] {"prog", "hello", "world"});
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   508
                        checkPermissionDenied(pb);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   509
                        dir1Prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   510
                        equal(run(pb).out(), "hello world\n");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   511
                        equal(run(pb).exitValue(), True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   512
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   513
                        pb.command(cmd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        // If prog found on both parent and child's PATH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        // parent's is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        new File("prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        new File("dir3").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        copy("/bin/true", "dir1/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        copy("/bin/false", "dir3/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        pb.environment().put("PATH","dir3");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   524
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        copy("/bin/true", "dir3/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        copy("/bin/false", "dir1/prog");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   527
                        equal(run(pb).exitValue(), False.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        // cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        new File("dir3/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        new File("dir1").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        new File("dir2").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        new File("dir3").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        new File("prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (failed != 0) throw new Error("PATH search algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            else throw new Error("JavaChild invocation error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
22288
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
   547
    private static void copy(String src, String dst) throws IOException {
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
   548
        Files.copy(Paths.get(src), Paths.get(dst),
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
   549
                   StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    private static String javaChildOutput(ProcessBuilder pb, String...args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        List<String> list = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        for (String arg : args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            list.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        pb.command(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private static String getenvInChild(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return javaChildOutput(pb, "System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    private static String getenvInChild1234(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return javaChildOutput(pb, "System.getenv(\\u1234)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private static String getenvInChild(ProcessBuilder pb, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return javaChildOutput(pb, "System.getenv(String)", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    private static String pwdInChild(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return javaChildOutput(pb, "pwd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private static final String javaExe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        File.separator + "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private static final String classpath =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        System.getProperty("java.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    private static final List<String> javaChildArgs =
21836
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
   584
        Arrays.asList(javaExe,
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
   585
                      "-XX:+DisplayVMOutputToStderr",
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
   586
                      "-classpath", absolutifyPath(classpath),
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
   587
                      "Basic$JavaChild");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private static void testEncoding(String encoding, String tested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            // If round trip conversion works, should be able to set env vars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // correctly in child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (new String(tested.getBytes()).equals(tested)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                out.println("Testing " + encoding + " environment values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                pb.environment().put("ASCIINAME",tested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                equal(getenvInChild(pb,"ASCIINAME"), tested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    static class Windows {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            System.getProperty("os.name").startsWith("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   608
    static class AIX {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   609
        public static boolean is() { return is; }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   610
        private static final boolean is =
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   611
            System.getProperty("os.name").equals("AIX");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   612
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   613
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    static class Unix {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            (! Windows.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
             new File("/bin/sh").exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
             new File("/bin/true").exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
             new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    static class UnicodeOS {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        private static final String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // MacOS X would probably also qualify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            osName.startsWith("Windows")   &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            ! osName.startsWith("Windows 9") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            ! osName.equals("Windows Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   633
    static class MacOSX {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   634
        public static boolean is() { return is; }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   635
        private static final String osName = System.getProperty("os.name");
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
   636
        private static final boolean is = osName.contains("OS X");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   637
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   638
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    static class True {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        public static int exitValue() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private static class False {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        public static int exitValue() { return exitValue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        private static final int exitValue = exitValue0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        private static int exitValue0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // /bin/false returns an *unspecified* non-zero number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (! Unix.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    int rc = new ProcessBuilder("/bin/false")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        .start().waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    check(rc != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            } catch (Throwable t) { unexpected(t); return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    static class EnglishUnix {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30956
diff changeset
   662
        private static final Boolean is =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            (! Windows.is() && isEnglish("LANG") && isEnglish("LC_ALL"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        private static boolean isEnglish(String envvar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            String val = getenv(envvar);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   667
            return (val == null) || val.matches("en.*") || val.matches("C");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        /** Returns true if we can expect English OS error strings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   674
    static class DelegatingProcess extends Process {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   675
        final Process p;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   676
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   677
        DelegatingProcess(Process p) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   678
            this.p = p;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   679
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   680
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   681
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   682
        public void destroy() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   683
            p.destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   684
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   685
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   686
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   687
        public int exitValue() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   688
            return p.exitValue();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   689
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   690
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   691
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   692
        public int waitFor() throws InterruptedException {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   693
            return p.waitFor();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   694
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   695
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   696
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   697
        public OutputStream getOutputStream() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   698
            return p.getOutputStream();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   699
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   700
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   701
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   702
        public InputStream getInputStream() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   703
            return p.getInputStream();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   704
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   705
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   706
        @Override
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   707
        public InputStream getErrorStream() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   708
            return p.getErrorStream();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   709
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   710
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
   711
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private static boolean matches(String str, String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return Pattern.compile(regex).matcher(str).find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   716
    private static String matchAndExtract(String str, String regex) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   717
        Matcher matcher = Pattern.compile(regex).matcher(str);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   718
        if (matcher.find()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   719
            return matcher.group();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   720
        } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   721
            return "";
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   722
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   723
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   724
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   725
    /* Only used for Mac OS X --
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   726
     * Mac OS X (may) add the variable __CF_USER_TEXT_ENCODING to an empty
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   727
     * environment. The environment variable JAVA_MAIN_CLASS_<pid> may also
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   728
     * be set in Mac OS X.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   729
     * Remove them both from the list of env variables
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   730
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   731
    private static String removeMacExpectedVars(String vars) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   732
        // Check for __CF_USER_TEXT_ENCODING
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   733
        String cleanedVars = vars.replace("__CF_USER_TEXT_ENCODING="
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   734
                                            +cfUserTextEncoding+",","");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   735
        // Check for JAVA_MAIN_CLASS_<pid>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   736
        String javaMainClassStr
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   737
                = matchAndExtract(cleanedVars,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   738
                                    "JAVA_MAIN_CLASS_\\d+=Basic.JavaChild,");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   739
        return cleanedVars.replace(javaMainClassStr,"");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   740
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
   741
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   742
    /* Only used for AIX --
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   743
     * AIX adds the variable AIXTHREAD_GUARDPAGES=0 to the environment.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   744
     * Remove it from the list of env variables
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   745
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   746
    private static String removeAixExpectedVars(String vars) {
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   747
        return vars.replace("AIXTHREAD_GUARDPAGES=0,", "");
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   748
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
   749
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private static String sortByLinesWindowsly(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        String[] lines = text.split("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        Arrays.sort(lines, new WindowsComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        for (String line : lines)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            sb.append(line).append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    private static void checkMapSanity(Map<String,String> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            Set<String> keySet = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            Collection<String> values = map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            Set<Map.Entry<String,String>> entrySet = map.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            equal(entrySet.size(), keySet.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            equal(entrySet.size(), values.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            StringBuilder s1 = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            for (Map.Entry<String,String> e : entrySet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                s1.append(e.getKey() + "=" + e.getValue() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            StringBuilder s2 = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            for (String var : keySet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                s2.append(var + "=" + map.get(var) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            equal(s1.toString(), s2.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            Iterator<String> kIter = keySet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            Iterator<String> vIter = values.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            Iterator<Map.Entry<String,String>> eIter = entrySet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            while (eIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                Map.Entry<String,String> entry = eIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                String key   = kIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                String value = vIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                check(entrySet.contains(entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                check(keySet.contains(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                check(values.contains(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                check(map.containsKey(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                check(map.containsValue(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                equal(entry.getKey(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                equal(entry.getValue(), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   794
            check(!kIter.hasNext() &&
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   795
                    !vIter.hasNext());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    private static void checkMapEquality(Map<String,String> map1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                         Map<String,String> map2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            equal(map1.size(), map2.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            equal(map1.isEmpty(), map2.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            for (String key : map1.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                equal(map1.get(key), map2.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                check(map2.keySet().contains(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            equal(map1, map2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            equal(map2, map1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            equal(map1.entrySet(), map2.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            equal(map2.entrySet(), map1.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            equal(map1.keySet(), map2.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            equal(map2.keySet(), map1.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            equal(map1.hashCode(), map2.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            equal(map1.entrySet().hashCode(), map2.entrySet().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            equal(map1.keySet().hashCode(), map2.keySet().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   822
    static void checkRedirects(ProcessBuilder pb,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   823
                               Redirect in, Redirect out, Redirect err) {
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   824
        equal(pb.redirectInput(), in);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   825
        equal(pb.redirectOutput(), out);
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   826
        equal(pb.redirectError(), err);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   827
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   828
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   829
    static void redirectIO(ProcessBuilder pb,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   830
                           Redirect in, Redirect out, Redirect err) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   831
        pb.redirectInput(in);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   832
        pb.redirectOutput(out);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   833
        pb.redirectError(err);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   834
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   835
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   836
    static void setFileContents(File file, String contents) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   837
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   838
            Writer w = new FileWriter(file);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   839
            w.write(contents);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   840
            w.close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   841
        } catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   842
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   843
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   844
    static String fileContents(File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   845
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   846
            Reader r = new FileReader(file);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   847
            StringBuilder sb = new StringBuilder();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   848
            char[] buffer = new char[1024];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   849
            int n;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   850
            while ((n = r.read(buffer)) != -1)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   851
                sb.append(buffer,0,n);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   852
            r.close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   853
            return new String(sb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   854
        } catch (Throwable t) { unexpected(t); return ""; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   855
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   856
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   857
    static void testIORedirection() throws Throwable {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   858
        final File ifile = new File("ifile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   859
        final File ofile = new File("ofile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   860
        final File efile = new File("efile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   861
        ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   862
        ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   863
        efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   864
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   865
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   866
        // Check mutual inequality of different types of Redirect
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   867
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   868
        Redirect[] redirects =
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   869
            { PIPE,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   870
              INHERIT,
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   871
              DISCARD,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   872
              Redirect.from(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   873
              Redirect.to(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   874
              Redirect.appendTo(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   875
              Redirect.from(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   876
              Redirect.to(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   877
              Redirect.appendTo(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   878
            };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   879
        for (int i = 0; i < redirects.length; i++)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   880
            for (int j = 0; j < redirects.length; j++)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   881
                equal(redirects[i].equals(redirects[j]), (i == j));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   882
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   883
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   884
        // Check basic properties of different types of Redirect
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   885
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   886
        equal(PIPE.type(), Redirect.Type.PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   887
        equal(PIPE.toString(), "PIPE");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   888
        equal(PIPE.file(), null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   889
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   890
        equal(INHERIT.type(), Redirect.Type.INHERIT);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   891
        equal(INHERIT.toString(), "INHERIT");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   892
        equal(INHERIT.file(), null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   893
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   894
        equal(DISCARD.type(), Redirect.Type.WRITE);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   895
        equal(DISCARD.toString(), "WRITE");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   896
        equal(DISCARD.file(), new File((Windows.is() ? "NUL" : "/dev/null")));
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   897
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   898
        equal(Redirect.from(ifile).type(), Redirect.Type.READ);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   899
        equal(Redirect.from(ifile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   900
              "redirect to read from file \"ifile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   901
        equal(Redirect.from(ifile).file(), ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   902
        equal(Redirect.from(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   903
              Redirect.from(ifile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   904
        equal(Redirect.from(ifile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   905
              Redirect.from(ifile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   906
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   907
        equal(Redirect.to(ofile).type(), Redirect.Type.WRITE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   908
        equal(Redirect.to(ofile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   909
              "redirect to write to file \"ofile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   910
        equal(Redirect.to(ofile).file(), ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   911
        equal(Redirect.to(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   912
              Redirect.to(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   913
        equal(Redirect.to(ofile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   914
              Redirect.to(ofile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   915
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   916
        equal(Redirect.appendTo(ofile).type(), Redirect.Type.APPEND);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   917
        equal(Redirect.appendTo(efile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   918
              "redirect to append to file \"efile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   919
        equal(Redirect.appendTo(efile).file(), efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   920
        equal(Redirect.appendTo(efile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   921
              Redirect.appendTo(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   922
        equal(Redirect.appendTo(efile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   923
              Redirect.appendTo(efile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   924
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   925
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   926
        // Check initial values of redirects
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   927
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   928
        List<String> childArgs = new ArrayList<String>(javaChildArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   929
        childArgs.add("testIO");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   930
        final ProcessBuilder pb = new ProcessBuilder(childArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   931
        checkRedirects(pb, PIPE, PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   932
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   933
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   934
        // Check inheritIO
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   935
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   936
        pb.inheritIO();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   937
        checkRedirects(pb, INHERIT, INHERIT, INHERIT);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   938
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   939
        //----------------------------------------------------------------
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   940
        // Check DISCARD for stdout,stderr
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   941
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   942
        redirectIO(pb, INHERIT, DISCARD, DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   943
        checkRedirects(pb, INHERIT, DISCARD, DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   944
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   945
        //----------------------------------------------------------------
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   946
        // Check setters and getters agree
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   947
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   948
        pb.redirectInput(ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   949
        equal(pb.redirectInput().file(), ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   950
        equal(pb.redirectInput(), Redirect.from(ifile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   951
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   952
        pb.redirectOutput(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   953
        equal(pb.redirectOutput().file(), ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   954
        equal(pb.redirectOutput(), Redirect.to(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   955
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   956
        pb.redirectError(efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   957
        equal(pb.redirectError().file(), efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   958
        equal(pb.redirectError(), Redirect.to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   959
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   960
        THROWS(IllegalArgumentException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
   961
               () -> pb.redirectInput(Redirect.to(ofile)),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
   962
               () -> pb.redirectOutput(Redirect.from(ifile)),
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   963
               () -> pb.redirectError(Redirect.from(ifile)),
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
   964
               () -> pb.redirectInput(DISCARD));
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   965
29381
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   966
        THROWS(NullPointerException.class,
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   967
                () -> pb.redirectInput((File)null),
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   968
                () -> pb.redirectOutput((File)null),
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   969
                () -> pb.redirectError((File)null),
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   970
                () -> pb.redirectInput((Redirect)null),
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   971
                () -> pb.redirectOutput((Redirect)null),
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   972
                () -> pb.redirectError((Redirect)null));
6d4ed10391a6 8058464: (process spec) ProcessBuilder.redirectXXX throws unspecified NPE
rriggs
parents: 28678
diff changeset
   973
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   974
        THROWS(IOException.class,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   975
               // Input file does not exist
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
   976
               () -> pb.start());
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   977
        setFileContents(ifile, "standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   978
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   979
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   980
        // Writing to non-existent files
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   981
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   982
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   983
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   984
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   985
            equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   986
            equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   987
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   988
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   989
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   990
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   991
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   992
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   993
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   994
        // Both redirectErrorStream + redirectError
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   995
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   996
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   997
            pb.redirectErrorStream(true);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   998
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   999
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1000
            equal(fileContents(ofile),
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1001
                    "standard error" + "standard output");
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1002
            equal(fileContents(efile), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1003
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1004
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1005
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1006
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1007
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1008
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1009
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1010
        // Appending to existing files
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1011
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1012
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1013
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1014
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1015
            pb.redirectOutput(Redirect.appendTo(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1016
            pb.redirectError(Redirect.appendTo(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1017
            pb.redirectErrorStream(false);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1018
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1019
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1020
            equal(fileContents(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1021
                  "ofile-contents" + "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1022
            equal(fileContents(efile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1023
                  "efile-contents" + "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1024
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1025
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1026
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1027
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1028
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1029
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1030
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1031
        // Replacing existing files
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1032
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1033
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1034
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1035
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1036
            pb.redirectOutput(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1037
            pb.redirectError(Redirect.to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1038
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1039
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1040
            equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1041
            equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1042
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1043
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1044
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1045
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1046
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1047
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1048
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1049
        // Appending twice to the same file?
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1050
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1051
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1052
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1053
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1054
            Redirect appender = Redirect.appendTo(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1055
            pb.redirectOutput(appender);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1056
            pb.redirectError(appender);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1057
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1058
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1059
            equal(fileContents(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1060
                  "ofile-contents" +
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1061
                  "standard error" +
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1062
                  "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1063
            equal(fileContents(efile), "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1064
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1065
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1066
            ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1067
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1068
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1069
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1070
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1071
        //----------------------------------------------------------------
32763
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1072
        // DISCARDing output
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1073
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1074
        {
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1075
            setFileContents(ifile, "standard input");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1076
            pb.redirectOutput(DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1077
            pb.redirectError(DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1078
            ProcessResults r = run(pb);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1079
            equal(r.exitValue(), 0);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1080
            equal(r.out(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1081
            equal(r.err(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1082
        }
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1083
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1084
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1085
        // DISCARDing output and redirecting error
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1086
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1087
        {
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1088
            setFileContents(ifile, "standard input");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1089
            setFileContents(ofile, "ofile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1090
            setFileContents(efile, "efile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1091
            pb.redirectOutput(DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1092
            pb.redirectError(efile);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1093
            ProcessResults r = run(pb);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1094
            equal(r.exitValue(), 0);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1095
            equal(fileContents(ofile), "ofile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1096
            equal(fileContents(efile), "standard error");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1097
            equal(r.out(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1098
            equal(r.err(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1099
            ofile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1100
            efile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1101
        }
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1102
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1103
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1104
        // DISCARDing error and redirecting output
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1105
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1106
        {
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1107
            setFileContents(ifile, "standard input");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1108
            setFileContents(ofile, "ofile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1109
            setFileContents(efile, "efile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1110
            pb.redirectOutput(ofile);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1111
            pb.redirectError(DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1112
            ProcessResults r = run(pb);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1113
            equal(r.exitValue(), 0);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1114
            equal(fileContents(ofile), "standard output");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1115
            equal(fileContents(efile), "efile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1116
            equal(r.out(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1117
            equal(r.err(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1118
            ofile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1119
            efile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1120
        }
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1121
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1122
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1123
        // DISCARDing output and merging error into output
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1124
        //----------------------------------------------------------------
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1125
        {
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1126
            setFileContents(ifile, "standard input");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1127
            setFileContents(ofile, "ofile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1128
            setFileContents(efile, "efile-contents");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1129
            pb.redirectOutput(DISCARD);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1130
            pb.redirectErrorStream(true);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1131
            pb.redirectError(efile);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1132
            ProcessResults r = run(pb);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1133
            equal(r.exitValue(), 0);
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1134
            equal(fileContents(ofile), "ofile-contents");   // untouched
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1135
            equal(fileContents(efile), "");                 // empty
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1136
            equal(r.out(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1137
            equal(r.err(), "");
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1138
            ifile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1139
            ofile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1140
            efile.delete();
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1141
            pb.redirectErrorStream(false);                  // reset for next test
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1142
        }
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1143
c11c2b9b45a5 8132541: (process) ProcessBuilder support for redirection to discard output
rriggs
parents: 32649
diff changeset
  1144
        //----------------------------------------------------------------
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1145
        // Testing INHERIT is harder.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1146
        // Note that this requires __FOUR__ nested JVMs involved in one test,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1147
        // if you count the harness JVM.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1148
        //----------------------------------------------------------------
20191
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
  1149
        for (String testName : new String[] { "testInheritIO", "testRedirectInherit" } ) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1150
            redirectIO(pb, PIPE, PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1151
            List<String> command = pb.command();
20191
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19399
diff changeset
  1152
            command.set(command.size() - 1, testName);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1153
            Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1154
            new PrintStream(p.getOutputStream()).print("standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1155
            p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1156
            ProcessResults r = run(p);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1157
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1158
            equal(r.out(), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1159
            equal(r.err(), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1160
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1161
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1162
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1163
        // Test security implications of I/O redirection
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1164
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1165
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1166
        // Read access to current directory is always granted;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1167
        // So create a tmpfile for input instead.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1168
        final File tmpFile = File.createTempFile("Basic", "tmp");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1169
        setFileContents(tmpFile, "standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1170
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1171
        final Policy policy = new Policy();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1172
        Policy.setPolicy(policy);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1173
        System.setSecurityManager(new SecurityManager());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1174
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1175
            final Permission xPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1176
                = new FilePermission("<<ALL FILES>>", "execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1177
            final Permission rxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1178
                = new FilePermission("<<ALL FILES>>", "read,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1179
            final Permission wxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1180
                = new FilePermission("<<ALL FILES>>", "write,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1181
            final Permission rwxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1182
                = new FilePermission("<<ALL FILES>>", "read,write,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1183
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1184
            THROWS(SecurityException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1185
                   () -> { policy.setPermissions(xPermission);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1186
                           redirectIO(pb, from(tmpFile), PIPE, PIPE);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1187
                           pb.start();},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1188
                   () -> { policy.setPermissions(rxPermission);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1189
                           redirectIO(pb, PIPE, to(ofile), PIPE);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1190
                           pb.start();},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1191
                   () -> { policy.setPermissions(rxPermission);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1192
                           redirectIO(pb, PIPE, PIPE, to(efile));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1193
                           pb.start();});
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1194
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1195
            {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1196
                policy.setPermissions(rxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1197
                redirectIO(pb, from(tmpFile), PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1198
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1199
                equal(r.out(), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1200
                equal(r.err(), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1201
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1202
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1203
            {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1204
                policy.setPermissions(wxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1205
                redirectIO(pb, PIPE, to(ofile), to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1206
                Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1207
                new PrintStream(p.getOutputStream()).print("standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1208
                p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1209
                ProcessResults r = run(p);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1210
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1211
                equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1212
                equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1213
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1214
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1215
            {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1216
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1217
                redirectIO(pb, from(tmpFile), to(ofile), to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1218
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1219
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1220
                equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1221
                equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1222
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1223
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1224
        } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1225
            policy.setPermissions(new RuntimePermission("setSecurityManager"));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1226
            System.setSecurityManager(null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1227
            tmpFile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1228
            ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1229
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1230
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1231
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1232
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1233
24577
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1234
    static void checkProcessPid() {
30956
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1235
        ProcessBuilder pb = new ProcessBuilder();
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1236
        List<String> list = new ArrayList<String>(javaChildArgs);
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1237
        list.add("pid");
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1238
        pb.command(list);
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1239
        try {
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1240
            Process p = pb.start();
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1241
            String s = commandOutput(p);
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1242
            long actualPid = Long.valueOf(s.trim());
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 44107
diff changeset
  1243
            long expectedPid = p.pid();
30956
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1244
            equal(actualPid, expectedPid);
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1245
        } catch (Throwable t) {
6f95bc555a6f 8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
rriggs
parents: 30899
diff changeset
  1246
            unexpected(t);
24577
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1247
        }
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1248
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1249
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1250
        // Test the default implementation of Process.getPid
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  1251
        DelegatingProcess p = new DelegatingProcess(null);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  1252
        THROWS(UnsupportedOperationException.class,
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 44107
diff changeset
  1253
                () -> p.pid(),
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  1254
                () -> p.toHandle(),
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  1255
                () -> p.supportsNormalTermination(),
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  1256
                () -> p.children(),
33648
9564031a20e0 8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents: 32763
diff changeset
  1257
                () -> p.descendants());
24577
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1258
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1259
    }
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1260
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            System.out.println("This appears to be a Windows system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (Unix.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            System.out.println("This appears to be a Unix system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        if (UnicodeOS.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            System.out.println("This appears to be a Unicode-based OS.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1269
        try { testIORedirection(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1270
        catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        //----------------------------------------------------------------
24577
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1273
        // Basic tests for getPid()
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1274
        //----------------------------------------------------------------
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1275
        checkProcessPid();
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1276
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 22607
diff changeset
  1277
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // Basic tests for setting, replacing and deleting envvars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            Map<String,String> environ = pb.environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            // New env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            environ.put("QUUX", "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            equal(environ.get("QUUX"), "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            equal(getenvInChild(pb,"QUUX"), "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            // Modify env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            environ.put("QUUX","bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            equal(environ.get("QUUX"), "bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            equal(getenvInChild(pb,"QUUX"), "bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            // Remove env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            environ.remove("QUUX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            equal(environ.get("QUUX"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            equal(getenvInChild(pb,"QUUX"), "null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            // Remove non-existent env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            environ.remove("QUUX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            equal(environ.get("QUUX"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            equal(getenvInChild(pb,"QUUX"), "null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // Pass Empty environment to child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            pb.environment().clear();
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1314
            String expected = Windows.is() ? "SystemRoot="+systemRoot+",": "";
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1315
            expected = AIX.is() ? "LIBPATH="+libpath+",": expected;
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1316
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1317
                pb.environment().put("SystemRoot", systemRoot);
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1318
            }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1319
            if (AIX.is()) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1320
                pb.environment().put("LIBPATH", libpath);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1321
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1322
            String result = getenvInChild(pb);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1323
            if (MacOSX.is()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1324
                result = removeMacExpectedVars(result);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1325
            }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1326
            if (AIX.is()) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1327
                result = removeAixExpectedVars(result);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1328
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1329
            equal(result, expected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        // System.getenv() is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1336
               () -> getenv().put("FOO","BAR"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1337
               () -> getenv().remove("PATH"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1338
               () -> getenv().keySet().remove("PATH"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1339
               () -> getenv().values().remove("someValue"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            Collection<Map.Entry<String,String>> c = getenv().entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            if (! c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    c.iterator().next().setValue("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    fail("Expected UnsupportedOperationException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                } catch (UnsupportedOperationException e) {} // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        // System.getenv() always returns the same object in our implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            check(System.getenv() == System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        // You can't create an env var name containing "=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // or an env var name or value containing NUL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            final Map<String,String> m = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            THROWS(IllegalArgumentException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1364
                   () -> m.put("FOO=","BAR"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1365
                   () -> m.put("FOO\u0000","BAR"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1366
                   () -> m.put("FOO","BAR\u0000"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        // Commands must never be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        THROWS(NullPointerException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1373
               () -> new ProcessBuilder((List<String>)null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1374
               () -> new ProcessBuilder().command((List<String>)null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        // Put in a command; get the same one back out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            List<String> command = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            ProcessBuilder pb = new ProcessBuilder(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            check(pb.command() == command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            List<String> command2 = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            command2.add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            command2.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            pb.command(command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            check(pb.command() == command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            pb.command("foo", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            check(pb.command() != command2 && pb.command().equals(command2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            pb.command(command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            command2.add("baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            equal(pb.command().get(2), "baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        // Commands must contain at least one element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        THROWS(IndexOutOfBoundsException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1399
               () -> new ProcessBuilder().start(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1400
               () -> new ProcessBuilder(new ArrayList<String>()).start(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1401
               () -> Runtime.getRuntime().exec(new String[]{}));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        // Commands must not contain null elements at start() time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        THROWS(NullPointerException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1407
               () -> new ProcessBuilder("foo",null,"bar").start(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1408
               () -> new ProcessBuilder((String)null).start(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1409
               () -> new ProcessBuilder(new String[]{null}).start(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1410
               () -> new ProcessBuilder(new String[]{"foo",null,"bar"}).start());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        // Command lists are growable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            new ProcessBuilder().command().add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            new ProcessBuilder("bar").command().add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            new ProcessBuilder(new String[]{"1","2"}).command().add("3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // Nulls in environment updates generate NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            final Map<String,String> env = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            THROWS(NullPointerException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1427
                   () -> env.put("foo",null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1428
                   () -> env.put(null,"foo"),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1429
                   () -> env.remove(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1430
                   () -> { for (Map.Entry<String,String> e : env.entrySet())
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1431
                               e.setValue(null);},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1432
                   () -> Runtime.getRuntime().exec(new String[]{"foo"},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1433
                                                   new String[]{null}));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        // Non-String types in environment updates generate ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            final Map<String,String> env = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            THROWS(ClassCastException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1442
                   () -> env.remove(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1443
                   () -> env.keySet().remove(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1444
                   () -> env.values().remove(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1445
                   () -> env.entrySet().remove(TRUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        // Check query operations on environment maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            List<Map<String,String>> envs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                new ArrayList<Map<String,String>>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            envs.add(System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            envs.add(new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            for (final Map<String,String> env : envs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                // Nulls in environment queries are forbidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                THROWS(NullPointerException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1461
                       () -> getenv(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1462
                       () -> env.get(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1463
                       () -> env.containsKey(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1464
                       () -> env.containsValue(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1465
                       () -> env.keySet().contains(null),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1466
                       () -> env.values().contains(null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                // Non-String types in environment queries are forbidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                THROWS(ClassCastException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1472
                       () -> env.get(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1473
                       () -> env.containsKey(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1474
                       () -> env.containsValue(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1475
                       () -> env.keySet().contains(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1476
                       () -> env.values().contains(TRUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                // Illegal String values in environment queries are (grumble) OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                equal(env.get("\u0000"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                check(! env.containsKey("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                check(! env.containsValue("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                check(! env.keySet().contains("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                check(! env.values().contains("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            final Set<Map.Entry<String,String>> entrySet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                new ProcessBuilder().environment().entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            THROWS(NullPointerException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1494
                   () -> entrySet.contains(null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            THROWS(ClassCastException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1496
                   () -> entrySet.contains(TRUE),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1497
                   () -> entrySet.contains(
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1498
                             new SimpleImmutableEntry<Boolean,String>(TRUE,"")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            check(! entrySet.contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                  (new SimpleImmutableEntry<String,String>("", "")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        // Put in a directory; get the same one back out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            File foo = new File("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            equal(pb.directory(foo).directory(), foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            equal(pb.directory(null).directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        // If round-trip conversion works, check envvar pass-through to child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            testEncoding("ASCII",   "xyzzy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            testEncoding("Latin1",  "\u00f1\u00e1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            testEncoding("Unicode", "\u22f1\u11e1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        // A surprisingly large number of ways to delete an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    environ.remove("Foo");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                    environ.keySet().remove("Foo");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    environ.values().remove("BAAR");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    // Legally fabricate a ProcessEnvironment.StringEntry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    // even though it's private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    Map<String,String> environ2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                        = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    environ2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    environ2.put("Foo","BAAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    // Subtlety alert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    Map.Entry<String,String> e
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                        = environ2.entrySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    environ.entrySet().remove(e);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    Map.Entry<String,String> victim = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    for (Map.Entry<String,String> e : environ.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                            victim = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                    if (victim != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                        environ.entrySet().remove(victim);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    Iterator<String> it = environ.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                        String val = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                        if (val.equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    Iterator<Map.Entry<String,String>> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                        = environ.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                        Map.Entry<String,String> e = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    Iterator<String> it = environ.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                        String val = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                        if (val.equals("BAAR"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        // A surprisingly small number of ways to add an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        testVariableAdder(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                    environ.put("Foo","Bahrein");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        // A few ways to modify an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        testVariableModifier(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    environ.put("Foo","NewValue");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        testVariableModifier(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    for (Map.Entry<String,String> e : environ.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                            e.setValue("NewValue");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        // Fiddle with environment sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            int size = environ.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            checkSizes(environ, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            environ.put("UnLiKeLYeNVIROmtNam", "someVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            checkSizes(environ, size+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            // Check for environment independence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            new ProcessBuilder().environment().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            environ.put("UnLiKeLYeNVIROmtNam", "someOtherVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            checkSizes(environ, size+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            environ.remove("UnLiKeLYeNVIROmtNam");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            checkSizes(environ, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            environ.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            environ.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            environ.keySet().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            environ.entrySet().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            environ.values().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        // Check that various map invariants hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        checkMapSanity(new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        checkMapSanity(System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        checkMapEquality(new ProcessBuilder().environment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                         new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // Check effects on external "env" command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            Set<String> env1 = new HashSet<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                (Arrays.asList(nativeEnv((String[])null).split("\n")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            pb.environment().put("QwErTyUiOp","AsDfGhJk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            Set<String> env2 = new HashSet<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                (Arrays.asList(nativeEnv(pb).split("\n")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            check(env2.size() == env1.size() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            env1.add("QwErTyUiOp=AsDfGhJk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            check(env1.equals(env2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        // Test Runtime.exec(...envp...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        // Check for sort order of environment variables on Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        try {
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1677
            String systemRoot = "SystemRoot=" + System.getenv("SystemRoot");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1680
                            "+=+", "_=_", "~=~", systemRoot};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            String output = nativeEnv(envp);
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1682
            String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n"+systemRoot+"\n_=_\n~=~\n";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            // On Windows, Java must keep the environment sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            // Order is random on Unix, so this test does the sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            if (! Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                output = sortByLinesWindowsly(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            equal(output, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        //----------------------------------------------------------------
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1691
        // Test Runtime.exec(...envp...)
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1692
        // and check SystemRoot gets set automatically on Windows
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1693
        //----------------------------------------------------------------
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1694
        try {
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1695
            if (Windows.is()) {
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1696
                String systemRoot = "SystemRoot=" + System.getenv("SystemRoot");
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1697
                String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1698
                                "+=+", "_=_", "~=~"};
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1699
                String output = nativeEnv(envp);
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1700
                String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n"+systemRoot+"\n_=_\n~=~\n";
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1701
                equal(output, expected);
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1702
            }
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1703
        } catch (Throwable t) { unexpected(t); }
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1704
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 9035
diff changeset
  1705
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        // System.getenv() must be consistent with System.getenv(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            for (Map.Entry<String,String> e : getenv().entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                equal(getenv(e.getKey()), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        // Fiddle with working directory in child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            String canonicalUserDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                new File(System.getProperty("user.dir")).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            String[] sdirs = new String[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                {".", "..", "/", "/bin",
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1721
                 "C:", "c:", "C:/", "c:\\", "\\", "\\bin",
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1722
                 "c:\\windows  ", "c:\\Program Files", "c:\\Program Files\\" };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            for (String sdir : sdirs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                File dir = new File(sdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                if (! (dir.isDirectory() && dir.exists()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                out.println("Testing directory " + dir);
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1728
                //dir = new File(dir.getCanonicalPath());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                equal(pwdInChild(pb), canonicalUserDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                pb.directory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                equal(pb.directory(), dir);
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1736
                equal(pwdInChild(pb), dir.getCanonicalPath());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                pb.directory(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                equal(pwdInChild(pb), canonicalUserDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                pb.directory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        //----------------------------------------------------------------
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1747
        // Working directory with Unicode in child
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1748
        //----------------------------------------------------------------
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1749
        try {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1750
            if (UnicodeOS.is()) {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1751
                File dir = new File(System.getProperty("test.dir", "."),
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1752
                                    "ProcessBuilderDir\u4e00\u4e02");
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1753
                try {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1754
                    if (!dir.exists())
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1755
                        dir.mkdir();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1756
                    out.println("Testing Unicode directory:" + dir);
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1757
                    ProcessBuilder pb = new ProcessBuilder();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1758
                    pb.directory(dir);
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1759
                    equal(pwdInChild(pb), dir.getCanonicalPath());
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1760
                } finally {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1761
                    if (dir.exists())
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1762
                        dir.delete();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1763
                }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1764
            }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1765
        } catch (Throwable t) { unexpected(t); }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1766
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1767
        //----------------------------------------------------------------
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1768
        // OOME in child allocating maximally sized array
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1769
        // Test for hotspot/jvmti bug 6850957
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1770
        //----------------------------------------------------------------
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1771
        try {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1772
            List<String> list = new ArrayList<String>(javaChildArgs);
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1773
            list.add(1, String.format("-XX:OnOutOfMemoryError=%s -version",
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1774
                                      javaExe));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1775
            list.add("ArrayOOME");
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1776
            ProcessResults r = run(new ProcessBuilder(list));
21836
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
  1777
            check(r.err().contains("java.lang.OutOfMemoryError:"));
e41f1922c8a7 6703075: (process) java/lang/ProcessBuilder/Basic.java fails with fastdebug
martin
parents: 21816
diff changeset
  1778
            check(r.err().contains(javaExe));
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1779
            check(r.err().contains(System.getProperty("java.version")));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1780
            equal(r.exitValue(), 1);
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1781
        } catch (Throwable t) { unexpected(t); }
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1782
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1783
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        // Windows has tricky semi-case-insensitive semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        if (Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                out.println("Running case insensitve variable tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                for (String[] namePair :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                         new String[][]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    { new String[]{"PATH","PaTh"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                      new String[]{"home","HOME"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                      new String[]{"SYSTEMROOT","SystemRoot"}}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    check((getenv(namePair[0]) == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                           getenv(namePair[1]) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                          ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                          getenv(namePair[0]).equals(getenv(namePair[1])),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                          "Windows environment variables are not case insensitive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        // Test proper Unicode child environment transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        if (UnicodeOS.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                pb.environment().put("\u1234","\u5678");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                pb.environment().remove("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                equal(getenvInChild1234(pb), "\u5678");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        // Test Runtime.exec(...envp...) with envstrings with initial `='
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            childArgs.add("System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            String[] cmdp = childArgs.toArray(new String[childArgs.size()]);
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1821
            String[] envp;
12882
35d381df9422 7174723: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing [win]
alanb
parents: 12538
diff changeset
  1822
            String[] envpWin = {"=C:=\\", "=ExitValue=3", "SystemRoot="+systemRoot};
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1823
            String[] envpOth = {"=ExitValue=3", "=C:=\\"};
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1824
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1825
                envp = envpWin;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1826
            } else {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1827
                envp = envpOth;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1828
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            Process p = Runtime.getRuntime().exec(cmdp, envp);
12882
35d381df9422 7174723: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing [win]
alanb
parents: 12538
diff changeset
  1830
            String expected = Windows.is() ? "=C:=\\,=ExitValue=3,SystemRoot="+systemRoot+"," : "=C:=\\,";
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1831
            expected = AIX.is() ? expected + "LIBPATH="+libpath+",": expected;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1832
            String commandOutput = commandOutput(p);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1833
            if (MacOSX.is()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1834
                commandOutput = removeMacExpectedVars(commandOutput);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1835
            }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1836
            if (AIX.is()) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1837
                commandOutput = removeAixExpectedVars(commandOutput);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1838
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1839
            equal(commandOutput, expected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                pb.environment().clear();
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1843
                pb.environment().put("SystemRoot", systemRoot);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                pb.environment().put("=ExitValue", "3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                pb.environment().put("=C:", "\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                equal(commandOutput(pb), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        // Test Runtime.exec(...envp...) with envstrings without any `='
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            String[] cmdp = {"echo"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            String[] envp = {"Hello", "World"}; // Yuck!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            Process p = Runtime.getRuntime().exec(cmdp, envp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            equal(commandOutput(p), "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        // Test Runtime.exec(...envp...) with envstrings containing NULs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            childArgs.add("System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            String[] cmdp = childArgs.toArray(new String[childArgs.size()]);
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1867
            String[] envpWin = {"SystemRoot="+systemRoot, "LC_ALL=C\u0000\u0000", // Yuck!
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1868
                             "FO\u0000=B\u0000R"};
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1869
            String[] envpOth = {"LC_ALL=C\u0000\u0000", // Yuck!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                             "FO\u0000=B\u0000R"};
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1871
            String[] envp;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1872
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1873
                envp = envpWin;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1874
            } else {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1875
                envp = envpOth;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1876
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1877
            System.out.println ("cmdp");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1878
            for (int i=0; i<cmdp.length; i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1879
                System.out.printf ("cmdp %d: %s\n", i, cmdp[i]);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1880
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1881
            System.out.println ("envp");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1882
            for (int i=0; i<envp.length; i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1883
                System.out.printf ("envp %d: %s\n", i, envp[i]);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1884
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            Process p = Runtime.getRuntime().exec(cmdp, envp);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1886
            String commandOutput = commandOutput(p);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1887
            if (MacOSX.is()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1888
                commandOutput = removeMacExpectedVars(commandOutput);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1889
            }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1890
            if (AIX.is()) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1891
                commandOutput = removeAixExpectedVars(commandOutput);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1892
            }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10347
diff changeset
  1893
            check(commandOutput.equals(Windows.is()
12882
35d381df9422 7174723: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing [win]
alanb
parents: 12538
diff changeset
  1894
                    ? "LC_ALL=C,SystemRoot="+systemRoot+","
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1895
                    : AIX.is()
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1896
                            ? "LC_ALL=C,LIBPATH="+libpath+","
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  1897
                            : "LC_ALL=C,"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                  "Incorrect handling of envstrings containing NULs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        // Test the redirectErrorStream property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            equal(pb.redirectErrorStream(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            equal(pb.redirectErrorStream(true), pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            equal(pb.redirectErrorStream(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            equal(pb.redirectErrorStream(false), pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            equal(pb.redirectErrorStream(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            childArgs.add("OutErr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            {
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1918
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                equal(r.out(), "outout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                equal(r.err(), "errerr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                pb.redirectErrorStream(true);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1924
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                equal(r.out(), "outerrouterr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1930
        if (Unix.is()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            // We can find true and false when PATH is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                childArgs.add("null PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                pb.environment().remove("PATH");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1939
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                equal(r.out(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                equal(r.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            // PATH search algorithm on Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                childArgs.add("PATH search algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                pb.environment().put("PATH", "dir1:dir2:");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1953
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                equal(r.out(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                equal(r.exitValue(), True.exitValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            // Parent's, not child's PATH is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                new File("suBdiR").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                copy("/bin/true", "suBdiR/unliKely");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                final ProcessBuilder pb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                    new ProcessBuilder(new String[]{"unliKely"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                pb.environment().put("PATH", "suBdiR");
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  1968
                THROWS(IOException.class, () -> pb.start());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            } catch (Throwable t) { unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                new File("suBdiR/unliKely").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                new File("suBdiR").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        // Attempt to start bogus program ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            new ProcessBuilder("").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                ! matches(m, "No such file or directory"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        // Check that attempt to execute program name with funny
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        // characters throws an exception containing those characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        for (String programName : new String[] {"\u00f0", "\u01f0"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                new ProcessBuilder(programName).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                Pattern p = Pattern.compile(programName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                if (! matches(m, programName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                    || (EnglishUnix.is()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                        && ! matches(m, "No such file or directory")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        // Attempt to start process in nonexistent directory fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            new ProcessBuilder("echo")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                .directory(new File("UnLiKeLY"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                .start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            if (! matches(m, "in directory")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                || (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                    ! matches(m, "No such file or directory")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        //----------------------------------------------------------------
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2023
        // Attempt to write 4095 bytes to the pipe buffer without a
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2024
        // reader to drain it would deadlock, if not for the fact that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        // interprocess pipe buffers are at least 4096 bytes.
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2026
        //
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2027
        // Also, check that available reports all the bytes expected
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2028
        // in the pipe buffer, and that I/O operations do the expected
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2029
        // things.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            childArgs.add("print4095");
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2034
            final int SIZE = 4095;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2035
            final Process p = new ProcessBuilder(childArgs).start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2036
            print4095(p.getOutputStream(), (byte) '!'); // Might hang!
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2037
            p.waitFor();                                // Might hang!
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2038
            equal(SIZE, p.getInputStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2039
            equal(SIZE, p.getErrorStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2040
            THROWS(IOException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2041
                   () -> { p.getOutputStream().write((byte) '!');
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2042
                           p.getOutputStream().flush();});
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2043
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2044
            final byte[] bytes = new byte[SIZE + 1];
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2045
            equal(SIZE, p.getInputStream().read(bytes));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2046
            for (int i = 0; i < SIZE; i++)
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2047
                equal((byte) '!', bytes[i]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2048
            equal((byte) 0, bytes[SIZE]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2049
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2050
            equal(SIZE, p.getErrorStream().read(bytes));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2051
            for (int i = 0; i < SIZE; i++)
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2052
                equal((byte) 'E', bytes[i]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2053
            equal((byte) 0, bytes[SIZE]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2054
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2055
            equal(0, p.getInputStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2056
            equal(0, p.getErrorStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2057
            equal(-1, p.getErrorStream().read());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2058
            equal(-1, p.getInputStream().read());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2059
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            equal(p.exitValue(), 5);
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2061
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
  2062
            p.getInputStream().close();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
  2063
            p.getErrorStream().close();
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9500
diff changeset
  2064
            try { p.getOutputStream().close(); } catch (IOException flushFailed) { }
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2065
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2066
            InputStream[] streams = { p.getInputStream(), p.getErrorStream() };
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2067
            for (final InputStream in : streams) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2068
                Fun[] ops = {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2069
                    () -> in.read(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2070
                    () -> in.read(bytes),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2071
                    () -> in.available()
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2072
                };
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2073
                for (Fun op : ops) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2074
                    try {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2075
                        op.f();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2076
                        fail();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2077
                    } catch (IOException expected) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2078
                        check(expected.getMessage()
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2079
                              .matches("[Ss]tream [Cc]losed"));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2080
                    }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2081
                }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2082
            }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2083
        } catch (Throwable t) { unexpected(t); }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2084
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2085
        //----------------------------------------------------------------
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2086
        // Check that reads which are pending when Process.destroy is
52821
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2087
        // called, get EOF, or IOException("Stream closed").
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2088
        //----------------------------------------------------------------
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2089
        try {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2090
            final int cases = 4;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2091
            for (int i = 0; i < cases; i++) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2092
                final int action = i;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2093
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2094
                childArgs.add("sleep");
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2095
                final byte[] bytes = new byte[10];
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2096
                final Process p = new ProcessBuilder(childArgs).start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2097
                final CountDownLatch latch = new CountDownLatch(1);
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2098
                final InputStream s;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2099
                switch (action & 0x1) {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2100
                    case 0: s = p.getInputStream(); break;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2101
                    case 1: s = p.getErrorStream(); break;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2102
                    default: throw new Error();
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2103
                }
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2104
                final Thread thread = new Thread() {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2105
                    public void run() {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2106
                        try {
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2107
                            int r;
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2108
                            latch.countDown();
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2109
                            switch (action & 0x2) {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2110
                                case 0: r = s.read(); break;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2111
                                case 2: r = s.read(bytes); break;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2112
                                default: throw new Error();
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2113
                            }
58566
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2114
                            if (r >= 0) {
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2115
                                // The child sent unexpected output; print it to diagnose
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2116
                                System.out.println("Unexpected child output:");
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2117
                                if ((action & 0x2) == 0) {
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2118
                                    System.out.write(r);    // Single character
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2119
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2120
                                } else {
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2121
                                    System.out.write(bytes, 0, r);
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2122
                                }
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2123
                                for (int c = s.read(); c >= 0; c = s.read())
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2124
                                    System.out.write(c);
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2125
                                System.out.println("\nEND Child output.");
afbb7ec84228 8232135: Add diagnostic output to test java/util/ProcessBuilder/Basic.java
rriggs
parents: 55462
diff changeset
  2126
                            }
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2127
                            equal(-1, r);
52821
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2128
                        } catch (IOException ioe) {
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2129
                            if (!ioe.getMessage().equals("Stream closed")) {
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2130
                                // BufferedInputStream may throw IOE("Stream closed").
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2131
                                unexpected(ioe);
a7e587822a2d 8171426: java/lang/ProcessBuilder/Basic.java failed Stream closed
rriggs
parents: 52364
diff changeset
  2132
                            }
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2133
                        } catch (Throwable t) { unexpected(t); }}};
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2134
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2135
                thread.start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2136
                latch.await();
14183
26e97eede839 8000817: Reinstate accidentally removed sleep() from ProcessBuilder/Basic.java
robm
parents: 14178
diff changeset
  2137
                Thread.sleep(10);
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2138
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2139
                String os = System.getProperty("os.name");
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2140
                if (os.equalsIgnoreCase("Solaris") ||
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2141
                    os.equalsIgnoreCase("SunOS"))
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2142
                {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2143
                    final Object deferred;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2144
                    Class<?> c = s.getClass();
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2145
                    if (c.getName().equals(
28665
fe8344cf6496 8071481: (Process) Merge UNIXProcess.java into ProcessImpl.java
rriggs
parents: 27514
diff changeset
  2146
                        "java.lang.ProcessImpl$DeferredCloseInputStream"))
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2147
                    {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2148
                        deferred = s;
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2149
                    } else {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2150
                        Field deferredField = p.getClass().
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2151
                            getDeclaredField("stdout_inner_stream");
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2152
                        deferredField.setAccessible(true);
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2153
                        deferred = deferredField.get(p);
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2154
                    }
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2155
                    Field useCountField = deferred.getClass().
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2156
                        getDeclaredField("useCount");
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2157
                    useCountField.setAccessible(true);
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2158
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2159
                    while (useCountField.getInt(deferred) <= 0) {
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2160
                        Thread.yield();
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2161
                    }
14183
26e97eede839 8000817: Reinstate accidentally removed sleep() from ProcessBuilder/Basic.java
robm
parents: 14178
diff changeset
  2162
                } else if (s instanceof BufferedInputStream) {
28671
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2163
                    // Wait until after the s.read occurs in "thread" by
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2164
                    // checking when the input stream monitor is acquired
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2165
                    // (BufferedInputStream.read is synchronized)
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2166
                    while (!isLocked(s, 10)) {
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2167
                        Thread.sleep(100);
14183
26e97eede839 8000817: Reinstate accidentally removed sleep() from ProcessBuilder/Basic.java
robm
parents: 14178
diff changeset
  2168
                    }
14178
f2c729b81c6a 7152183: TEST_BUG: java/lang/ProcessBuilder/Basic.java failing intermittently [sol]
robm
parents: 13581
diff changeset
  2169
                }
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2170
                p.destroy();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2171
                thread.join();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  2172
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        //----------------------------------------------------------------
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2176
        // Check that subprocesses which create subprocesses of their
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2177
        // own do not cause parent to hang waiting for file
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2178
        // descriptors to be closed.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2179
        //----------------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2180
        try {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2181
            if (Unix.is()
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2182
                && new File("/bin/bash").exists()
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2183
                && new File("/bin/sleep").exists()) {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2184
                // Notice that we only destroy the process created by us (i.e.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2185
                // our child) but not our grandchild (i.e. '/bin/sleep'). So
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2186
                // pay attention that the grandchild doesn't run too long to
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2187
                // avoid polluting the process space with useless processes.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2188
                // Running the grandchild for 60s should be more than enough.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2189
                final String[] cmd = { "/bin/bash", "-c", "(/bin/sleep 60)" };
22598
kvn
parents: 22597 21836
diff changeset
  2190
                final String[] cmdkill = { "/bin/bash", "-c", "(/usr/bin/pkill -f \"sleep 60\")" };
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2191
                final ProcessBuilder pb = new ProcessBuilder(cmd);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2192
                final Process p = pb.start();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2193
                final InputStream stdout = p.getInputStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2194
                final InputStream stderr = p.getErrorStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2195
                final OutputStream stdin = p.getOutputStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2196
                final Thread reader = new Thread() {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2197
                    public void run() {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2198
                        try { stdout.read(); }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2199
                        catch (IOException e) {
6849
02dc13de9152 6989903: (process) test/java/lang/ProcessBuilder/Basic.java failing with "Bad file number" (sol)
alanb
parents: 6669
diff changeset
  2200
                            // Check that reader failed because stream was
02dc13de9152 6989903: (process) test/java/lang/ProcessBuilder/Basic.java failing with "Bad file number" (sol)
alanb
parents: 6669
diff changeset
  2201
                            // asynchronously closed.
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2202
                            // e.printStackTrace();
55257
442b86eb633c 8224905: java/lang/ProcessBuilder/Basic.java#id1 failed with stream closed
rriggs
parents: 52821
diff changeset
  2203
                            String msg = e.getMessage();
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2204
                            if (EnglishUnix.is() &&
55257
442b86eb633c 8224905: java/lang/ProcessBuilder/Basic.java#id1 failed with stream closed
rriggs
parents: 52821
diff changeset
  2205
                                ! (msg.matches(".*Bad file.*") ||
442b86eb633c 8224905: java/lang/ProcessBuilder/Basic.java#id1 failed with stream closed
rriggs
parents: 52821
diff changeset
  2206
                                        msg.matches(".*Stream closed.*")))
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2207
                                unexpected(e);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2208
                        }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2209
                        catch (Throwable t) { unexpected(t); }}};
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2210
                reader.setDaemon(true);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2211
                reader.start();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2212
                Thread.sleep(100);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2213
                p.destroy();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2214
                check(p.waitFor() != 0);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2215
                check(p.exitValue() != 0);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2216
                // Subprocess is now dead, but file descriptors remain open.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2217
                // Make sure the test will fail if we don't manage to close
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2218
                // the open streams within 30 seconds. Notice that this time
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2219
                // must be shorter than the sleep time of the grandchild.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2220
                Timer t = new Timer("test/java/lang/ProcessBuilder/Basic.java process reaper", true);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2221
                t.schedule(new TimerTask() {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2222
                      public void run() {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2223
                          fail("Subprocesses which create subprocesses of " +
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2224
                               "their own caused the parent to hang while " +
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2225
                               "waiting for file descriptors to be closed.");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2226
                          System.exit(-1);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2227
                      }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2228
                  }, 30000);
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2229
                stdout.close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2230
                stderr.close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2231
                stdin.close();
21813
f7e97f5bb5a2 8028094: TEST_BUG: java/lang/ProcessBuilder/Basic.java leaves "sleep 6666" processes behind
bvaidya
parents: 20191
diff changeset
  2232
                new ProcessBuilder(cmdkill).start();
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2233
                // All streams successfully closed so we can cancel the timer.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2234
                t.cancel();
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2235
                //----------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2236
                // There remain unsolved issues with asynchronous close.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2237
                // Here's a highly non-portable experiment to demonstrate:
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2238
                //----------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2239
                if (Boolean.getBoolean("wakeupJeff!")) {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2240
                    System.out.println("wakeupJeff!");
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2241
                    // Initialize signal handler for INTERRUPT_SIGNAL.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2242
                    new FileInputStream("/bin/sleep").getChannel().close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2243
                    // Send INTERRUPT_SIGNAL to every thread in this java.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2244
                    String[] wakeupJeff = {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2245
                        "/bin/bash", "-c",
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2246
                        "/bin/ps --noheaders -Lfp $PPID | " +
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2247
                        "/usr/bin/perl -nale 'print $F[3]' | " +
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2248
                        // INTERRUPT_SIGNAL == 62 on my machine du jour.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2249
                        "/usr/bin/xargs kill -62"
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2250
                    };
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2251
                    new ProcessBuilder(wakeupJeff).start().waitFor();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2252
                    // If wakeupJeff worked, reader probably got EBADF.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2253
                    reader.join();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2254
                }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2255
            }
40738
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2256
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2257
            //----------------------------------------------------------------
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2258
            // Check the Process toString() method
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2259
            //----------------------------------------------------------------
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2260
            {
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2261
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2262
                childArgs.add("testIO");
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2263
                ProcessBuilder pb = new ProcessBuilder(childArgs);
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2264
                pb.redirectInput(Redirect.PIPE);
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2265
                pb.redirectOutput(DISCARD);
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2266
                pb.redirectError(DISCARD);
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2267
                final Process p = pb.start();
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2268
                // Child process waits until it gets input
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2269
                String s = p.toString();
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2270
                check(s.contains("not exited"));
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 44107
diff changeset
  2271
                check(s.contains("pid=" + p.pid() + ","));
40738
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2272
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2273
                new PrintStream(p.getOutputStream()).print("standard input");
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2274
                p.getOutputStream().close();
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2275
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2276
                // Check the toString after it exits
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2277
                int exitValue = p.waitFor();
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2278
                s = p.toString();
44640
590dec7cadb4 8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents: 44107
diff changeset
  2279
                check(s.contains("pid=" + p.pid() + ","));
40738
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2280
                check(s.contains("exitValue=" + exitValue) &&
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2281
                        !s.contains("not exited"));
86952e676976 8155102: (Process) Process.toString could include pid, isAlive, exitStatus
rriggs
parents: 36966
diff changeset
  2282
            }
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2283
        } catch (Throwable t) { unexpected(t); }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2284
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  2285
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        // Attempt to start process with insufficient permissions fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            new File("emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            new FileOutputStream("emptyCommand").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            new File("emptyCommand").setExecutable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            new ProcessBuilder("./emptyCommand").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            new File("./emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                ! matches(m, "Permission denied"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        new File("emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        // Check for correct security permission behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        final Policy policy = new Policy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        Policy.setPolicy(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            // No permissions required to CREATE a ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            new ProcessBuilder("env").directory(null).directory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            new ProcessBuilder("env").directory(new File("dir")).directory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            new ProcessBuilder("env").command("??").command();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        THROWS(SecurityException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2320
               () -> { policy.setPermissions(/* Nothing */);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2321
                       System.getenv("foo");},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2322
               () -> { policy.setPermissions(/* Nothing */);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2323
                       System.getenv();},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2324
               () -> { policy.setPermissions(/* Nothing */);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2325
                       new ProcessBuilder("echo").start();},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2326
               () -> { policy.setPermissions(/* Nothing */);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2327
                       Runtime.getRuntime().exec("echo");},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2328
               () -> { policy.setPermissions(
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2329
                               new RuntimePermission("getenv.bar"));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2330
                       System.getenv("foo");});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            policy.setPermissions(new RuntimePermission("getenv.foo"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            System.getenv("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            policy.setPermissions(new RuntimePermission("getenv.*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            System.getenv("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            System.getenv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        final Permission execPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            = new FilePermission("<<ALL FILES>>", "execute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        THROWS(SecurityException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2347
               () -> { // environment permission by itself insufficient
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2348
                       policy.setPermissions(new RuntimePermission("getenv.*"));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2349
                       ProcessBuilder pb = new ProcessBuilder("env");
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2350
                       pb.environment().put("foo","bar");
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2351
                       pb.start();},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2352
               () -> { // exec permission by itself insufficient
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2353
                       policy.setPermissions(execPermission);
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2354
                       ProcessBuilder pb = new ProcessBuilder("env");
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2355
                       pb.environment().put("foo","bar");
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2356
                       pb.start();});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            // Both permissions? OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            policy.setPermissions(new RuntimePermission("getenv.*"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                                  execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            ProcessBuilder pb = new ProcessBuilder("env");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            pb.environment().put("foo","bar");
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2364
            Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2365
            closeStreams(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            // Don't need environment permission unless READING environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            policy.setPermissions(execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            Runtime.getRuntime().exec("env", new String[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            // Don't need environment permission unless READING environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            policy.setPermissions(execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            new ProcessBuilder("env").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        // Restore "normal" state without a security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        policy.setPermissions(new RuntimePermission("setSecurityManager"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        System.setSecurityManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2387
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2388
        // Check that Process.isAlive() &
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2389
        // Process.waitFor(0, TimeUnit.MILLISECONDS) work as expected.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2390
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2391
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2392
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2393
            childArgs.add("sleep");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2394
            final Process p = new ProcessBuilder(childArgs).start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2395
            long start = System.nanoTime();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2396
            if (!p.isAlive() || p.waitFor(0, TimeUnit.MILLISECONDS)) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2397
                fail("Test failed: Process exited prematurely");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2398
            }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2399
            long end = System.nanoTime();
27514
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2400
            // give waitFor(timeout) a wide berth (2s)
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2401
            System.out.printf(" waitFor process: delta: %d%n",(end - start) );
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2402
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2403
            if ((end - start) > TimeUnit.SECONDS.toNanos(2))
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2404
                fail("Test failed: waitFor took too long (" + (end - start) + "ns)");
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2405
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2406
            p.destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2407
            p.waitFor();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2408
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2409
            if (p.isAlive() ||
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2410
                !p.waitFor(0, TimeUnit.MILLISECONDS))
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2411
            {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2412
                fail("Test failed: Process still alive - please terminate " +
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2413
                    p.toString() + " manually");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2414
            }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2415
        } catch (Throwable t) { unexpected(t); }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2416
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2417
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2418
        // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2419
        // works as expected.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2420
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2421
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2422
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2423
            childArgs.add("sleep");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2424
            final Process p = new ProcessBuilder(childArgs).start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2425
            long start = System.nanoTime();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2426
27514
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2427
            p.waitFor(10, TimeUnit.MILLISECONDS);
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2428
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2429
            long end = System.nanoTime();
27514
b31703bbd2cb 8043477: java/lang/ProcessBuilder/Basic.java failed with: java.lang.AssertionError: Some tests failed
rriggs
parents: 25804
diff changeset
  2430
            if ((end - start) < TimeUnit.MILLISECONDS.toNanos(10))
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20191
diff changeset
  2431
                fail("Test failed: waitFor didn't take long enough (" + (end - start) + "ns)");
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2432
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2433
            p.destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2434
        } catch (Throwable t) { unexpected(t); }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2435
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2436
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2437
        // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2438
        // interrupt works as expected, if interrupted while waiting.
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2439
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2440
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2441
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2442
            childArgs.add("sleep");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2443
            final Process p = new ProcessBuilder(childArgs).start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2444
            final long start = System.nanoTime();
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2445
            final CountDownLatch aboutToWaitFor = new CountDownLatch(1);
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2446
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2447
            final Thread thread = new Thread() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2448
                public void run() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2449
                    try {
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2450
                        aboutToWaitFor.countDown();
51422
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2451
                        Thread.currentThread().interrupt();
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2452
                        boolean result = p.waitFor(30L * 1000L, TimeUnit.MILLISECONDS);
21985
5c22272c888a 8029525: java/lang/ProcessBuilder/Basic.java fails intermittently
robm
parents: 21836
diff changeset
  2453
                        fail("waitFor() wasn't interrupted, its return value was: " + result);
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2454
                    } catch (InterruptedException success) {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2455
                    } catch (Throwable t) { unexpected(t); }
21985
5c22272c888a 8029525: java/lang/ProcessBuilder/Basic.java fails intermittently
robm
parents: 21836
diff changeset
  2456
                }
5c22272c888a 8029525: java/lang/ProcessBuilder/Basic.java fails intermittently
robm
parents: 21836
diff changeset
  2457
            };
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2458
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2459
            thread.start();
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2460
            aboutToWaitFor.await();
51422
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2461
            thread.interrupt();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2462
            thread.join(10L * 1000L);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2463
            check(millisElapsedSince(start) < 10L * 1000L);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2464
            check(!thread.isAlive());
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2465
            p.destroy();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2466
        } catch (Throwable t) { unexpected(t); }
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2467
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2468
        //----------------------------------------------------------------
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2469
        // Check that Process.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS)
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2470
        // interrupt works as expected, if interrupted while waiting.
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2471
        //----------------------------------------------------------------
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2472
        try {
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2473
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2474
            childArgs.add("sleep");
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2475
            final Process p = new ProcessBuilder(childArgs).start();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2476
            final long start = System.nanoTime();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2477
            final CountDownLatch aboutToWaitFor = new CountDownLatch(1);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2478
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2479
            final Thread thread = new Thread() {
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2480
                public void run() {
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2481
                    try {
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2482
                        aboutToWaitFor.countDown();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2483
                        Thread.currentThread().interrupt();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2484
                        boolean result = p.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2485
                        fail("waitFor() wasn't interrupted, its return value was: " + result);
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2486
                    } catch (InterruptedException success) {
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2487
                    } catch (Throwable t) { unexpected(t); }
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2488
                }
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2489
            };
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2490
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2491
            thread.start();
41257a58a588 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
rriggs
parents: 47216
diff changeset
  2492
            aboutToWaitFor.await();
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2493
            thread.interrupt();
22259
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2494
            thread.join(10L * 1000L);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2495
            check(millisElapsedSince(start) < 10L * 1000L);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2496
            check(!thread.isAlive());
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2497
            p.destroy();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2498
        } catch (Throwable t) { unexpected(t); }
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2499
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2500
        //----------------------------------------------------------------
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2501
        // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2502
        // interrupt works as expected, if interrupted before waiting.
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2503
        //----------------------------------------------------------------
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2504
        try {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2505
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2506
            childArgs.add("sleep");
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2507
            final Process p = new ProcessBuilder(childArgs).start();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2508
            final long start = System.nanoTime();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2509
            final CountDownLatch threadStarted = new CountDownLatch(1);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2510
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2511
            final Thread thread = new Thread() {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2512
                public void run() {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2513
                    try {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2514
                        threadStarted.countDown();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2515
                        do { Thread.yield(); }
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2516
                        while (!Thread.currentThread().isInterrupted());
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2517
                        boolean result = p.waitFor(30L * 1000L, TimeUnit.MILLISECONDS);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2518
                        fail("waitFor() wasn't interrupted, its return value was: " + result);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2519
                    } catch (InterruptedException success) {
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2520
                    } catch (Throwable t) { unexpected(t); }
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2521
                }
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2522
            };
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2523
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2524
            thread.start();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2525
            threadStarted.await();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2526
            thread.interrupt();
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2527
            thread.join(10L * 1000L);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2528
            check(millisElapsedSince(start) < 10L * 1000L);
8e5afc67dca8 8029629: java/lang/ProcessBuilder/Basic.java fails intermittently
rriggs
parents: 21985
diff changeset
  2529
            check(!thread.isAlive());
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2530
            p.destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2531
        } catch (Throwable t) { unexpected(t); }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2532
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2533
        //----------------------------------------------------------------
29604
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2534
        // Check that Process.waitFor(timeout, null) throws NPE.
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2535
        //----------------------------------------------------------------
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2536
        try {
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2537
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2538
            childArgs.add("sleep");
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2539
            final Process p = new ProcessBuilder(childArgs).start();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2540
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2541
                    () ->  p.waitFor(10L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2542
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2543
                    () ->  p.waitFor(0L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2544
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2545
                    () -> p.waitFor(-1L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2546
            // Terminate process and recheck after it exits
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2547
            p.destroy();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2548
            p.waitFor();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2549
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2550
                    () -> p.waitFor(10L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2551
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2552
                    () -> p.waitFor(0L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2553
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2554
                    () -> p.waitFor(-1L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2555
        } catch (Throwable t) { unexpected(t); }
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2556
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2557
        //----------------------------------------------------------------
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2558
        // Check that default implementation of Process.waitFor(timeout, null) throws NPE.
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2559
        //----------------------------------------------------------------
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2560
        try {
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2561
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2562
            childArgs.add("sleep");
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2563
            final Process proc = new ProcessBuilder(childArgs).start();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2564
            final DelegatingProcess p = new DelegatingProcess(proc);
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2565
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2566
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2567
                    () ->  p.waitFor(10L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2568
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2569
                    () ->  p.waitFor(0L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2570
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2571
                    () ->  p.waitFor(-1L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2572
            // Terminate process and recheck after it exits
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2573
            p.destroy();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2574
            p.waitFor();
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2575
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2576
                    () -> p.waitFor(10L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2577
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2578
                    () -> p.waitFor(0L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2579
            THROWS(NullPointerException.class,
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2580
                    () -> p.waitFor(-1L, null));
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2581
        } catch (Throwable t) { unexpected(t); }
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2582
f3e313c492ba 8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
rriggs
parents: 29381
diff changeset
  2583
        //----------------------------------------------------------------
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2584
        // Check the default implementation for
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2585
        // Process.waitFor(long, TimeUnit)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2586
        //----------------------------------------------------------------
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2587
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2588
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2589
            childArgs.add("sleep");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2590
            final Process proc = new ProcessBuilder(childArgs).start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2591
            DelegatingProcess p = new DelegatingProcess(proc);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2592
            long start = System.nanoTime();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2593
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2594
            p.waitFor(1000, TimeUnit.MILLISECONDS);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2595
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2596
            long end = System.nanoTime();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2597
            if ((end - start) < 500000000)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2598
                fail("Test failed: waitFor didn't take long enough");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2599
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2600
            p.destroy();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2601
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2602
            p.waitFor(1000, TimeUnit.MILLISECONDS);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 12882
diff changeset
  2603
        } catch (Throwable t) { unexpected(t); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2606
    static void closeStreams(Process p) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2607
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2608
            p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2609
            p.getInputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2610
            p.getErrorStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2611
        } catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2612
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2613
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    // A Policy class designed to make permissions fiddling very easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    private static class Policy extends java.security.Policy {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 55257
diff changeset
  2618
        static final java.security.Policy DEFAULT_POLICY = java.security.Policy.getPolicy();
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 55257
diff changeset
  2619
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        public void setPermissions(Permission...permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            for (Permission permission : permissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        public Policy() { setPermissions(/* Nothing */); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
        public PermissionCollection getPermissions(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        public PermissionCollection getPermissions(ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        public boolean implies(ProtectionDomain pd, Permission p) {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 55257
diff changeset
  2639
            return perms.implies(p) || DEFAULT_POLICY.implies(pd, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        public void refresh() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    private static class StreamAccumulator extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        private final InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        private final StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
        private Throwable throwable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        public String result () throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            if (throwable != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                throw throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        StreamAccumulator (InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                Reader r = new InputStreamReader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                char[] buf = new char[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                while ((n = r.read(buf)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                    sb.append(buf,0,n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                throwable = t;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2670
            } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2671
                try { is.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2672
                catch (Throwable t) { throwable = t; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2677
    static ProcessResults run(ProcessBuilder pb) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2678
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2679
            return run(pb.start());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2680
        } catch (Throwable t) { unexpected(t); return null; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2681
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2682
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    private static ProcessResults run(Process p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        Throwable throwable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        int exitValue = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        String out = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        String err = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        StreamAccumulator outAccumulator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            new StreamAccumulator(p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        StreamAccumulator errAccumulator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            new StreamAccumulator(p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            outAccumulator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            errAccumulator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
            exitValue = p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            outAccumulator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            errAccumulator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            out = outAccumulator.result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            err = errAccumulator.result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            throwable = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        return new ProcessResults(out, err, exitValue, throwable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
    // Results of a command
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
    private static class ProcessResults {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        private final String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        private final String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        private final int exitValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        private final Throwable throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        public ProcessResults(String out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                              String err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                              int exitValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                              Throwable throwable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            this.err = err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            this.exitValue = exitValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            this.throwable = throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        public String out()          { return out; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        public String err()          { return err; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        public int exitValue()       { return exitValue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        public Throwable throwable() { return throwable; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
            sb.append("<STDOUT>\n" + out() + "</STDOUT>\n")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                .append("<STDERR>\n" + err() + "</STDERR>\n")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                .append("exitValue = " + exitValue + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            if (throwable != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                sb.append(throwable.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
    static void fail() {failed++; Thread.dumpStack();}
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents: 30820
diff changeset
  2751
    static void fail(String msg) {System.err.println(msg); fail();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
        if (x == null ? y == null : x.equals(y)) pass();
22288
81efc55fac99 8031961: (process) java/lang/ProcessBuilder/Basic.java uses "cp -p" which is inefficient
rriggs
parents: 22259
diff changeset
  2757
        else fail(">'" + x + "'<" + " not equal to " + "'" + y + "'");}
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  2758
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 24577
diff changeset
  2763
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                else unexpected(t);}}
28671
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2770
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2771
    static boolean isLocked(final Object monitor, final long millis) throws InterruptedException {
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2772
        return new Thread() {
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2773
            volatile boolean unlocked;
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2774
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2775
            @Override
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2776
            public void run() {
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2777
                synchronized (monitor) { unlocked = true; }
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2778
            }
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2779
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2780
            boolean isLocked() throws InterruptedException {
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2781
                start();
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2782
                join(millis);
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2783
                return !unlocked;
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2784
            }
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2785
        }.isLocked();
cb15fc6cc038 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
psandoz
parents: 27514
diff changeset
  2786
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
}