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