jdk/test/java/lang/ProcessBuilder/Basic.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8561 ca8d6ccdd9dc
child 9500 268f823d9e1c
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8561
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    29
 *      4947220 7018606
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Basic tests for Process and Environment Variable code
5600
8ca34998a6b3 6943915: Adjust jdk/test/Makefile to deal with .dll and .so libraries needing execute permissions
ohair
parents: 5168
diff changeset
    31
 * @run main/othervm/timeout=300 Basic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    35
import java.lang.ProcessBuilder.Redirect;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    36
import static java.lang.ProcessBuilder.Redirect.*;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
    40
import java.util.concurrent.CountDownLatch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import static java.lang.System.getenv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static java.lang.System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import static java.lang.Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import static java.util.AbstractMap.SimpleImmutableEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    50
    /* used for Windows only */
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    51
    static final String systemRoot = System.getenv("SystemRoot");
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String commandOutput(Reader r) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        while ((c = r.read()) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (c != '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                sb.append((char) c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static String commandOutput(Process p) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        check(p.getInputStream()  == p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        check(p.getOutputStream() == p.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        check(p.getErrorStream()  == p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        String output = commandOutput(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static String commandOutput(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return commandOutput(pb.start());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            String commandline = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            for (String arg : pb.command())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                commandline += " " + arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("Exception trying to run process: " + commandline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static String commandOutput(String...command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return commandOutput(Runtime.getRuntime().exec(command));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            String commandline = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (String arg : command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                commandline += " " + arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println("Exception trying to run process: " + commandline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static void checkCommandOutput(ProcessBuilder pb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                           String expected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                           String failureMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String got = commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        check(got.equals(expected),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
              failureMsg + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
              "Expected: \"" + expected + "\"\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
              "Got: \"" + got + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static String absolutifyPath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (String file : path.split(File.pathSeparator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (sb.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                sb.append(File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            sb.append(new File(file).getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // compare windows-style, by canonicalizing to upper case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // not lower case as String.compareToIgnoreCase does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static class WindowsComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        implements Comparator<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public int compare(String x, String y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return x.toUpperCase(Locale.US)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                .compareTo(y.toUpperCase(Locale.US));
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 String sortedLines(String lines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String[] arr = lines.split("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        List<String> ls = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (String s : arr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ls.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Collections.sort(ls, new WindowsComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (String s : ls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            sb.append(s + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static void compareLinesIgnoreCase(String lines1, String lines2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (! (sortedLines(lines1).equalsIgnoreCase(sortedLines(lines2)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            String dashes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                "-----------------------------------------------------";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            out.print(sortedLines(lines1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            out.print(sortedLines(lines2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            out.println(dashes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.println("sizes: " + sortedLines(lines1).length() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        " " + sortedLines(lines2).length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            fail("Sorted string contents differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final Runtime runtime = Runtime.getRuntime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final String[] winEnvCommand = {"cmd.exe", "/c", "set"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static String winEnvFilter(String env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return env.replaceAll("\r", "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            .replaceAll("(?m)^(?:COMSPEC|PROMPT|PATHEXT)=.*\n","");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static String unixEnvProg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return new File("/usr/bin/env").canExecute() ? "/usr/bin/env"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            : "/bin/env";
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 String nativeEnv(String[] env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return winEnvFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    (commandOutput(runtime.exec(winEnvCommand, env)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return commandOutput(runtime.exec(unixEnvProg(), env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static String nativeEnv(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                pb.command(winEnvCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return winEnvFilter(commandOutput(pb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                pb.command(new String[]{unixEnvProg()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static void checkSizes(Map<String,String> environ, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            equal(size, environ.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            equal(size, environ.entrySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            equal(size, environ.keySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            equal(size, environ.values().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            boolean isEmpty = (size == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            equal(isEmpty, environ.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            equal(isEmpty, environ.entrySet().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            equal(isEmpty, environ.keySet().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            equal(isEmpty, environ.values().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private interface EnvironmentFrobber {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        void doIt(Map<String,String> environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private static void testVariableDeleter(EnvironmentFrobber fooDeleter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            environ.put("Foo", "BAAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            fooDeleter.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            equal(environ.get("Foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            equal(environ.remove("Foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static void testVariableAdder(EnvironmentFrobber fooAdder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            environ.remove("Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            fooAdder.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            equal(environ.get("Foo"), "Bahrein");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static void testVariableModifier(EnvironmentFrobber fooModifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            environ.put("Foo","OldValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            fooModifier.doIt(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            equal(environ.get("Foo"), "NewValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static void printUTF8(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        out.write(s.getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private static String getenvAsString(Map<String,String> environment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        for (Map.Entry<String,String> e : environment.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // Ignore magic environment variables added by the launcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (! e.getKey().equals("NLSPATH") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ! e.getKey().equals("XFILESEARCHPATH") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                ! e.getKey().equals("LD_LIBRARY_PATH"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                sb.append(e.getKey())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    .append('=')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    .append(e.getValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    .append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   259
    static void print4095(OutputStream s, byte b) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        byte[] bytes = new byte[4095];
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   261
        Arrays.fill(bytes, b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        s.write(bytes);         // Might hang!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   265
    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
   266
        try {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   267
            pb.start();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   268
            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
   269
        } catch (IOException e) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   270
            String m = e.getMessage();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   271
            if (EnglishUnix.is() &&
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   272
                ! matches(m, "Permission denied"))
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   273
                unexpected(e);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   274
        } 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
   275
    }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   276
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public static class JavaChild {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        public static void main(String args[]) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            String action = args[0];
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   280
            if (action.equals("sleep")) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   281
                Thread.sleep(10 * 60 * 1000L);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   282
            } else if (action.equals("testIO")) {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   283
                String expected = "standard input";
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   284
                char[] buf = new char[expected.length()+1];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   285
                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
   286
                if (n != expected.length())
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   287
                    System.exit(5);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   288
                if (! new String(buf,0,n).equals(expected))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   289
                    System.exit(5);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   290
                System.err.print("standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   291
                System.out.print("standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   292
            } else if (action.equals("testInheritIO")) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   293
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   294
                childArgs.add("testIO");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   295
                ProcessBuilder pb = new ProcessBuilder(childArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   296
                pb.inheritIO();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   297
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   298
                if (! r.out().equals(""))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   299
                    System.exit(7);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   300
                if (! r.err().equals(""))
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   301
                    System.exit(8);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   302
                if (r.exitValue() != 0)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   303
                    System.exit(9);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   304
            } else if (action.equals("System.getenv(String)")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                String val = System.getenv(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                printUTF8(val == null ? "null" : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else if (action.equals("System.getenv(\\u1234)")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                String val = System.getenv("\u1234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                printUTF8(val == null ? "null" : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            } else if (action.equals("System.getenv()")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                printUTF8(getenvAsString(System.getenv()));
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   312
            } else if (action.equals("ArrayOOME")) {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   313
                Object dummy;
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   314
                switch(new Random().nextInt(3)) {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   315
                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
   316
                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
   317
                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
   318
                default: throw new InternalError();
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
   319
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } else if (action.equals("pwd")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                printUTF8(new File(System.getProperty("user.dir"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                          .getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } else if (action.equals("print4095")) {
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   324
                print4095(System.out, (byte) '!');
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
   325
                print4095(System.err, (byte) 'E');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                System.exit(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } else if (action.equals("OutErr")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // You might think the system streams would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                // buffered, and in fact they are implemented using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // BufferedOutputStream, but each and every print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // causes immediate operating system I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                System.out.print("out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                System.err.print("err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                System.out.print("out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                System.err.print("err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else if (action.equals("null PATH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                equal(System.getenv("PATH"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                check(new File("/bin/true").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                check(new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                ProcessBuilder pb1 = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                ProcessBuilder pb2 = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                ProcessResults r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                for (final ProcessBuilder pb :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                         new ProcessBuilder[] {pb1, pb2}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    pb.command("true");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   348
                    equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    pb.command("false");
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   351
                    equal(run(pb).exitValue(), False.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (failed != 0) throw new Error("null PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } else if (action.equals("PATH search algorithm")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                equal(System.getenv("PATH"), "dir1:dir2:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                check(new File("/bin/true").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                check(new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                String[] cmd = {"prog"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                ProcessBuilder pb1 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                ProcessBuilder pb2 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                ProcessBuilder pb3 = new ProcessBuilder(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                pb3.environment().remove("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                for (final ProcessBuilder pb :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                         new ProcessBuilder[] {pb1, pb2, pb3}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        // Not on PATH at all; directories don't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            pb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                ! matches(m, "No such file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        // Not on PATH at all; directories exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        new File("dir1").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        new File("dir2").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            pb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                ! matches(m, "No such file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        // Can't execute a directory -- permission denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        // Report EACCES errno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        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
   396
                        checkPermissionDenied(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        // continue searching if EACCES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        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
   400
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        new File("dir2/prog").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        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
   406
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   408
                        // 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
   409
                        //
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   410
                        // 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
   411
                        // Unix executables, and PATH vs explicit searching.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        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
   414
                        for (String[] command :
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   415
                                 new String[][] {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   416
                                     new String[] {"./prog"},
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   417
                                     cmd}) {
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   418
                            pb.command(command);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   419
                            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
   420
                            // "Normal" binaries
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   421
                            copy("/bin/true", "./prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   422
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   423
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   424
                            copy("/bin/false", "./prog");
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   425
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   426
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   427
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   428
                            // Interpreter scripts with #!
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   429
                            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
   430
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   431
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   432
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   433
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   434
                            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
   435
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   436
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   437
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   438
                            // Traditional shell scripts without #!
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   439
                            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
   440
                            prog.setExecutable(true);
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(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   442
                                  True.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   443
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   444
                            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
   445
                            prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   446
                            equal(run(pb).exitValue(),
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   447
                                  False.exitValue());
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   448
                            prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   449
                        }
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   450
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   451
                        // Test Unix interpreter scripts
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   452
                        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
   453
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   454
                        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
   455
                        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
   456
                        checkPermissionDenied(pb);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   457
                        dir1Prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   458
                        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
   459
                        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
   460
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   461
                        pb.command(cmd);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   462
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   463
                        // 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
   464
                        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
   465
                        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
   466
                        checkPermissionDenied(pb);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   467
                        dir1Prog.setExecutable(true);
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   468
                        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
   469
                        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
   470
                        dir1Prog.delete();
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
   471
                        pb.command(cmd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        // If prog found on both parent and child's PATH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        // parent's is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        new File("prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        new File("dir3").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        copy("/bin/true", "dir1/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        copy("/bin/false", "dir3/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        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
   482
                        equal(run(pb).exitValue(), True.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        copy("/bin/true", "dir3/prog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        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
   485
                        equal(run(pb).exitValue(), False.exitValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        // cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        new File("dir1/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        new File("dir2/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        new File("dir3/prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        new File("dir1").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        new File("dir2").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        new File("dir3").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        new File("prog").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (failed != 0) throw new Error("PATH search algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            else throw new Error("JavaChild invocation error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private static void copy(String src, String dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        system("/bin/cp", "-fp", src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private static void system(String... command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            ProcessBuilder pb = new ProcessBuilder(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            ProcessResults r = run(pb.start());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            equal(r.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            equal(r.out(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private static String javaChildOutput(ProcessBuilder pb, String...args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        List<String> list = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        for (String arg : args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            list.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        pb.command(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return commandOutput(pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private static String getenvInChild(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return javaChildOutput(pb, "System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    private static String getenvInChild1234(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return javaChildOutput(pb, "System.getenv(\\u1234)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private static String getenvInChild(ProcessBuilder pb, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return javaChildOutput(pb, "System.getenv(String)", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    private static String pwdInChild(ProcessBuilder pb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return javaChildOutput(pb, "pwd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private static final String javaExe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        File.separator + "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private static final String classpath =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        System.getProperty("java.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private static final List<String> javaChildArgs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        Arrays.asList(new String[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            { javaExe, "-classpath", absolutifyPath(classpath),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
              "Basic$JavaChild"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private static void testEncoding(String encoding, String tested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // If round trip conversion works, should be able to set env vars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            // correctly in child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (new String(tested.getBytes()).equals(tested)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                out.println("Testing " + encoding + " environment values");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                pb.environment().put("ASCIINAME",tested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                equal(getenvInChild(pb,"ASCIINAME"), tested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    static class Windows {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            System.getProperty("os.name").startsWith("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    static class Unix {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            (! Windows.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
             new File("/bin/sh").exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
             new File("/bin/true").exists() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
             new File("/bin/false").exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    static class UnicodeOS {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        private static final String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        private static final boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // MacOS X would probably also qualify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            osName.startsWith("Windows")   &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            ! osName.startsWith("Windows 9") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            ! osName.equals("Windows Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    static class True {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        public static int exitValue() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    private static class False {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        public static int exitValue() { return exitValue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        private static final int exitValue = exitValue0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        private static int exitValue0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            // /bin/false returns an *unspecified* non-zero number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                if (! Unix.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    int rc = new ProcessBuilder("/bin/false")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        .start().waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    check(rc != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } catch (Throwable t) { unexpected(t); return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    static class EnglishUnix {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        private final static Boolean is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            (! Windows.is() && isEnglish("LANG") && isEnglish("LC_ALL"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        private static boolean isEnglish(String envvar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            String val = getenv(envvar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return (val == null) || val.matches("en.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /** Returns true if we can expect English OS error strings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        static boolean is() { return is; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    private static boolean matches(String str, String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return Pattern.compile(regex).matcher(str).find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    private static String sortByLinesWindowsly(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        String[] lines = text.split("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        Arrays.sort(lines, new WindowsComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        for (String line : lines)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            sb.append(line).append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    private static void checkMapSanity(Map<String,String> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            Set<String> keySet = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            Collection<String> values = map.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            Set<Map.Entry<String,String>> entrySet = map.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            equal(entrySet.size(), keySet.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            equal(entrySet.size(), values.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            StringBuilder s1 = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            for (Map.Entry<String,String> e : entrySet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                s1.append(e.getKey() + "=" + e.getValue() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            StringBuilder s2 = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            for (String var : keySet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                s2.append(var + "=" + map.get(var) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            equal(s1.toString(), s2.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            Iterator<String> kIter = keySet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            Iterator<String> vIter = values.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            Iterator<Map.Entry<String,String>> eIter = entrySet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            while (eIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                Map.Entry<String,String> entry = eIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                String key   = kIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                String value = vIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                check(entrySet.contains(entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                check(keySet.contains(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                check(values.contains(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                check(map.containsKey(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                check(map.containsValue(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                equal(entry.getKey(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                equal(entry.getValue(), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            check(! kIter.hasNext() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                  ! vIter.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    private static void checkMapEquality(Map<String,String> map1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                         Map<String,String> map2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            equal(map1.size(), map2.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            equal(map1.isEmpty(), map2.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            for (String key : map1.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                equal(map1.get(key), map2.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                check(map2.keySet().contains(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            equal(map1, map2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            equal(map2, map1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            equal(map1.entrySet(), map2.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            equal(map2.entrySet(), map1.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            equal(map1.keySet(), map2.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            equal(map2.keySet(), map1.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            equal(map1.hashCode(), map2.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            equal(map1.entrySet().hashCode(), map2.entrySet().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            equal(map1.keySet().hashCode(), map2.keySet().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   704
    static void checkRedirects(ProcessBuilder pb,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   705
                               Redirect in, Redirect out, Redirect err) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   706
        equal(pb.redirectInput(),  in);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   707
        equal(pb.redirectOutput(), out);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   708
        equal(pb.redirectError(),  err);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   709
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   710
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   711
    static void redirectIO(ProcessBuilder pb,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   712
                           Redirect in, Redirect out, Redirect err) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   713
        pb.redirectInput(in);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   714
        pb.redirectOutput(out);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   715
        pb.redirectError(err);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   716
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   717
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   718
    static void setFileContents(File file, String contents) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   719
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   720
            Writer w = new FileWriter(file);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   721
            w.write(contents);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   722
            w.close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   723
        } catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   724
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   725
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   726
    static String fileContents(File file) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   727
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   728
            Reader r = new FileReader(file);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   729
            StringBuilder sb = new StringBuilder();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   730
            char[] buffer = new char[1024];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   731
            int n;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   732
            while ((n = r.read(buffer)) != -1)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   733
                sb.append(buffer,0,n);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   734
            r.close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   735
            return new String(sb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   736
        } catch (Throwable t) { unexpected(t); return ""; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   737
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   738
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   739
    static void testIORedirection() throws Throwable {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   740
        final File ifile = new File("ifile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   741
        final File ofile = new File("ofile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   742
        final File efile = new File("efile");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   743
        ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   744
        ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   745
        efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   746
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   747
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   748
        // Check mutual inequality of different types of Redirect
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   749
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   750
        Redirect[] redirects =
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   751
            { PIPE,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   752
              INHERIT,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   753
              Redirect.from(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   754
              Redirect.to(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   755
              Redirect.appendTo(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   756
              Redirect.from(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   757
              Redirect.to(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   758
              Redirect.appendTo(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   759
            };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   760
        for (int i = 0; i < redirects.length; i++)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   761
            for (int j = 0; j < redirects.length; j++)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   762
                equal(redirects[i].equals(redirects[j]), (i == j));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   763
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   764
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   765
        // Check basic properties of different types of Redirect
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   766
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   767
        equal(PIPE.type(), Redirect.Type.PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   768
        equal(PIPE.toString(), "PIPE");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   769
        equal(PIPE.file(), null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   770
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   771
        equal(INHERIT.type(), Redirect.Type.INHERIT);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   772
        equal(INHERIT.toString(), "INHERIT");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   773
        equal(INHERIT.file(), null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   774
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   775
        equal(Redirect.from(ifile).type(), Redirect.Type.READ);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   776
        equal(Redirect.from(ifile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   777
              "redirect to read from file \"ifile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   778
        equal(Redirect.from(ifile).file(), ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   779
        equal(Redirect.from(ifile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   780
              Redirect.from(ifile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   781
        equal(Redirect.from(ifile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   782
              Redirect.from(ifile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   783
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   784
        equal(Redirect.to(ofile).type(), Redirect.Type.WRITE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   785
        equal(Redirect.to(ofile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   786
              "redirect to write to file \"ofile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   787
        equal(Redirect.to(ofile).file(), ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   788
        equal(Redirect.to(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   789
              Redirect.to(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   790
        equal(Redirect.to(ofile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   791
              Redirect.to(ofile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   792
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   793
        equal(Redirect.appendTo(ofile).type(), Redirect.Type.APPEND);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   794
        equal(Redirect.appendTo(efile).toString(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   795
              "redirect to append to file \"efile\"");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   796
        equal(Redirect.appendTo(efile).file(), efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   797
        equal(Redirect.appendTo(efile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   798
              Redirect.appendTo(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   799
        equal(Redirect.appendTo(efile).hashCode(),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   800
              Redirect.appendTo(efile).hashCode());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   801
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   802
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   803
        // Check initial values of redirects
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   804
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   805
        List<String> childArgs = new ArrayList<String>(javaChildArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   806
        childArgs.add("testIO");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   807
        final ProcessBuilder pb = new ProcessBuilder(childArgs);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   808
        checkRedirects(pb, PIPE, PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   809
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   810
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   811
        // Check inheritIO
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   812
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   813
        pb.inheritIO();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   814
        checkRedirects(pb, INHERIT, INHERIT, INHERIT);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   815
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   816
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   817
        // Check setters and getters agree
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   818
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   819
        pb.redirectInput(ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   820
        equal(pb.redirectInput().file(), ifile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   821
        equal(pb.redirectInput(), Redirect.from(ifile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   822
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   823
        pb.redirectOutput(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   824
        equal(pb.redirectOutput().file(), ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   825
        equal(pb.redirectOutput(), Redirect.to(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   826
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   827
        pb.redirectError(efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   828
        equal(pb.redirectError().file(), efile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   829
        equal(pb.redirectError(), Redirect.to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   830
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   831
        THROWS(IllegalArgumentException.class,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   832
            new Fun(){void f() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   833
                pb.redirectInput(Redirect.to(ofile)); }},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   834
            new Fun(){void f() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   835
                pb.redirectInput(Redirect.appendTo(ofile)); }},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   836
            new Fun(){void f() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   837
                pb.redirectOutput(Redirect.from(ifile)); }},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   838
            new Fun(){void f() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   839
                pb.redirectError(Redirect.from(ifile)); }});
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   840
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   841
        THROWS(IOException.class,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   842
               // Input file does not exist
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   843
               new Fun(){void f() throws Throwable { pb.start(); }});
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   844
        setFileContents(ifile, "standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   845
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   846
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   847
        // Writing to non-existent files
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
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   851
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   852
            equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   853
            equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   854
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   855
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   856
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   857
            efile.delete();
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
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
        // Both redirectErrorStream + redirectError
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   862
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   863
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   864
            pb.redirectErrorStream(true);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   865
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   866
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   867
            equal(fileContents(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   868
                  "standard error" + "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   869
            equal(fileContents(efile), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   870
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   871
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   872
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   873
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   874
        }
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
        // Appending to existing files
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
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   880
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   881
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   882
            pb.redirectOutput(Redirect.appendTo(ofile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   883
            pb.redirectError(Redirect.appendTo(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   884
            pb.redirectErrorStream(false);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   885
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   886
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   887
            equal(fileContents(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   888
                  "ofile-contents" + "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   889
            equal(fileContents(efile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   890
                  "efile-contents" + "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   891
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   892
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   893
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   894
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   895
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   896
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   897
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   898
        // Replacing existing files
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
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   901
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   902
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   903
            pb.redirectOutput(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   904
            pb.redirectError(Redirect.to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   905
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   906
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   907
            equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   908
            equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   909
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   910
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   911
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   912
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   913
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   914
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   915
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   916
        // Appending twice to the same file?
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
            setFileContents(ofile, "ofile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   920
            setFileContents(efile, "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   921
            Redirect appender = Redirect.appendTo(ofile);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   922
            pb.redirectOutput(appender);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   923
            pb.redirectError(appender);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   924
            ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   925
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   926
            equal(fileContents(ofile),
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   927
                  "ofile-contents" +
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   928
                  "standard error" +
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   929
                  "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   930
            equal(fileContents(efile), "efile-contents");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   931
            equal(r.out(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   932
            equal(r.err(), "");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   933
            ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   934
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   935
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   936
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   937
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   938
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   939
        // Testing INHERIT is harder.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   940
        // 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
   941
        // if you count the harness JVM.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   942
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   943
        {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   944
            redirectIO(pb, PIPE, PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   945
            List<String> command = pb.command();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   946
            command.set(command.size() - 1, "testInheritIO");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   947
            Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   948
            new PrintStream(p.getOutputStream()).print("standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   949
            p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   950
            ProcessResults r = run(p);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   951
            equal(r.exitValue(), 0);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   952
            equal(r.out(), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   953
            equal(r.err(), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   954
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   955
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   956
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   957
        // Test security implications of I/O redirection
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   958
        //----------------------------------------------------------------
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   959
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   960
        // Read access to current directory is always granted;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   961
        // So create a tmpfile for input instead.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   962
        final File tmpFile = File.createTempFile("Basic", "tmp");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   963
        setFileContents(tmpFile, "standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   964
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   965
        final Policy policy = new Policy();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   966
        Policy.setPolicy(policy);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   967
        System.setSecurityManager(new SecurityManager());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   968
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   969
            final Permission xPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   970
                = new FilePermission("<<ALL FILES>>", "execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   971
            final Permission rxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   972
                = new FilePermission("<<ALL FILES>>", "read,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   973
            final Permission wxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   974
                = new FilePermission("<<ALL FILES>>", "write,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   975
            final Permission rwxPermission
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   976
                = new FilePermission("<<ALL FILES>>", "read,write,execute");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   977
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   978
            THROWS(SecurityException.class,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   979
               new Fun() { void f() throws IOException {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   980
                   policy.setPermissions(xPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   981
                   redirectIO(pb, from(tmpFile), PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   982
                   pb.start();}},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   983
               new Fun() { void f() throws IOException {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   984
                   policy.setPermissions(rxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   985
                   redirectIO(pb, PIPE, to(ofile), PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   986
                   pb.start();}},
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   987
               new Fun() { void f() throws IOException {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   988
                   policy.setPermissions(rxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   989
                   redirectIO(pb, PIPE, PIPE, to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   990
                   pb.start();}});
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   991
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   992
            {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   993
                policy.setPermissions(rxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   994
                redirectIO(pb, from(tmpFile), PIPE, PIPE);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   995
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   996
                equal(r.out(), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   997
                equal(r.err(), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   998
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   999
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
                policy.setPermissions(wxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1002
                redirectIO(pb, PIPE, to(ofile), to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1003
                Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1004
                new PrintStream(p.getOutputStream()).print("standard input");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1005
                p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1006
                ProcessResults r = run(p);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1007
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1008
                equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1009
                equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1010
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1011
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1012
            {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1013
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1014
                redirectIO(pb, from(tmpFile), to(ofile), to(efile));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1015
                ProcessResults r = run(pb);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1016
                policy.setPermissions(rwxPermission);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1017
                equal(fileContents(ofile), "standard output");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1018
                equal(fileContents(efile), "standard error");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1019
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1020
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1021
        } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1022
            policy.setPermissions(new RuntimePermission("setSecurityManager"));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1023
            System.setSecurityManager(null);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1024
            tmpFile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1025
            ifile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1026
            ofile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1027
            efile.delete();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1028
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1029
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1030
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            System.out.println("This appears to be a Windows system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (Unix.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            System.out.println("This appears to be a Unix system.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (UnicodeOS.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            System.out.println("This appears to be a Unicode-based OS.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1039
        try { testIORedirection(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1040
        catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1041
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        // Basic tests for setting, replacing and deleting envvars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            Map<String,String> environ = pb.environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            // New env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            environ.put("QUUX", "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            equal(environ.get("QUUX"), "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            equal(getenvInChild(pb,"QUUX"), "BAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            // Modify env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            environ.put("QUUX","bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            equal(environ.get("QUUX"), "bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            equal(getenvInChild(pb,"QUUX"), "bear");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            // Remove env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            environ.remove("QUUX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            equal(environ.get("QUUX"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            equal(getenvInChild(pb,"QUUX"), "null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            // Remove non-existent env var
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            environ.remove("QUUX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            equal(environ.get("QUUX"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            equal(getenvInChild(pb,"QUUX"), "null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            checkMapSanity(environ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        // Pass Empty environment to child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            pb.environment().clear();
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1079
            String expected = Windows.is() ? "SystemRoot="+systemRoot+",": "";
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1080
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1081
                pb.environment().put("SystemRoot", systemRoot);
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1082
            }
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1083
            equal(getenvInChild(pb), expected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        // System.getenv() is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            new Fun(){void f(){ getenv().put("FOO","BAR");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            new Fun(){void f(){ getenv().remove("PATH");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            new Fun(){void f(){ getenv().keySet().remove("PATH");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            new Fun(){void f(){ getenv().values().remove("someValue");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            Collection<Map.Entry<String,String>> c = getenv().entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if (! c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    c.iterator().next().setValue("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    fail("Expected UnsupportedOperationException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                } catch (UnsupportedOperationException e) {} // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        // System.getenv() always returns the same object in our implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            check(System.getenv() == System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // You can't create an env var name containing "=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // or an env var name or value containing NUL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            final Map<String,String> m = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            THROWS(IllegalArgumentException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                new Fun(){void f(){ m.put("FOO=","BAR");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                new Fun(){void f(){ m.put("FOO\u0000","BAR");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                new Fun(){void f(){ m.put("FOO","BAR\u0000");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        // Commands must never be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
               new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                   new ProcessBuilder((List<String>)null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
               new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                   new ProcessBuilder().command((List<String>)null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        // Put in a command; get the same one back out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            List<String> command = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            ProcessBuilder pb = new ProcessBuilder(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            check(pb.command() == command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            List<String> command2 = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            command2.add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            command2.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            pb.command(command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            check(pb.command() == command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            pb.command("foo", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            check(pb.command() != command2 && pb.command().equals(command2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            pb.command(command2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            command2.add("baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            equal(pb.command().get(2), "baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        // Commands must contain at least one element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        THROWS(IndexOutOfBoundsException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                new ProcessBuilder().start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                new ProcessBuilder(new ArrayList<String>()).start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                Runtime.getRuntime().exec(new String[]{});}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // Commands must not contain null elements at start() time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                new ProcessBuilder("foo",null,"bar").start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                new ProcessBuilder((String)null).start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                new ProcessBuilder(new String[]{null}).start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                new ProcessBuilder(new String[]{"foo",null,"bar"}).start();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        // Command lists are growable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            new ProcessBuilder().command().add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            new ProcessBuilder("bar").command().add("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            new ProcessBuilder(new String[]{"1","2"}).command().add("3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        // Nulls in environment updates generate NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            final Map<String,String> env = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                new Fun(){void f(){ env.put("foo",null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                new Fun(){void f(){ env.put(null,"foo");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                new Fun(){void f(){ env.remove(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    for (Map.Entry<String,String> e : env.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                        e.setValue(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    Runtime.getRuntime().exec(new String[]{"foo"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                                              new String[]{null});}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        // Non-String types in environment updates generate ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            final Map<String,String> env = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            THROWS(ClassCastException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                new Fun(){void f(){ env.remove(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                new Fun(){void f(){ env.keySet().remove(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                new Fun(){void f(){ env.values().remove(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                new Fun(){void f(){ env.entrySet().remove(TRUE);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        // Check query operations on environment maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            List<Map<String,String>> envs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                new ArrayList<Map<String,String>>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            envs.add(System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            envs.add(new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            for (final Map<String,String> env : envs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                // Nulls in environment queries are forbidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    new Fun(){void f(){ getenv(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    new Fun(){void f(){ env.get(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    new Fun(){void f(){ env.containsKey(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    new Fun(){void f(){ env.containsValue(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    new Fun(){void f(){ env.keySet().contains(null);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    new Fun(){void f(){ env.values().contains(null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                // Non-String types in environment queries are forbidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                THROWS(ClassCastException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    new Fun(){void f(){ env.get(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    new Fun(){void f(){ env.containsKey(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    new Fun(){void f(){ env.containsValue(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    new Fun(){void f(){ env.keySet().contains(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    new Fun(){void f(){ env.values().contains(TRUE);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                // Illegal String values in environment queries are (grumble) OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                equal(env.get("\u0000"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                check(! env.containsKey("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                check(! env.containsValue("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                check(! env.keySet().contains("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                check(! env.values().contains("\u0000"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            final Set<Map.Entry<String,String>> entrySet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                new ProcessBuilder().environment().entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            THROWS(NullPointerException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                   new Fun(){void f(){ entrySet.contains(null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            THROWS(ClassCastException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                new Fun(){void f(){ entrySet.contains(TRUE);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                    entrySet.contains(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                        new SimpleImmutableEntry<Boolean,String>(TRUE,""));}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            check(! entrySet.contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                  (new SimpleImmutableEntry<String,String>("", "")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        // Put in a directory; get the same one back out.
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
            File foo = new File("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            equal(pb.directory(foo).directory(), foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            equal(pb.directory(null).directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        // If round-trip conversion works, check envvar pass-through to child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            testEncoding("ASCII",   "xyzzy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            testEncoding("Latin1",  "\u00f1\u00e1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            testEncoding("Unicode", "\u22f1\u11e1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        // A surprisingly large number of ways to delete an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    environ.remove("Foo");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    environ.keySet().remove("Foo");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    environ.values().remove("BAAR");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    // Legally fabricate a ProcessEnvironment.StringEntry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    // even though it's private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    Map<String,String> environ2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                    environ2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    environ2.put("Foo","BAAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    // Subtlety alert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    Map.Entry<String,String> e
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                        = environ2.entrySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    environ.entrySet().remove(e);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    Map.Entry<String,String> victim = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    for (Map.Entry<String,String> e : environ.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                            victim = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    if (victim != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                        environ.entrySet().remove(victim);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    Iterator<String> it = environ.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                        String val = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                        if (val.equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    Iterator<Map.Entry<String,String>> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        = environ.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                        Map.Entry<String,String> e = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        testVariableDeleter(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    Iterator<String> it = environ.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                        String val = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                        if (val.equals("BAAR"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                            it.remove();}}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // A surprisingly small number of ways to add an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        testVariableAdder(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    environ.put("Foo","Bahrein");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // A few ways to modify an environment var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        testVariableModifier(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    environ.put("Foo","NewValue");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        testVariableModifier(new EnvironmentFrobber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                public void doIt(Map<String,String> environ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    for (Map.Entry<String,String> e : environ.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        if (e.getKey().equals("Foo"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                            e.setValue("NewValue");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        // Fiddle with environment sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            Map<String,String> environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            int size = environ.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            checkSizes(environ, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            environ.put("UnLiKeLYeNVIROmtNam", "someVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            checkSizes(environ, size+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            // Check for environment independence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            new ProcessBuilder().environment().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            environ.put("UnLiKeLYeNVIROmtNam", "someOtherVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            checkSizes(environ, size+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            environ.remove("UnLiKeLYeNVIROmtNam");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            checkSizes(environ, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            environ.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            environ.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            environ.keySet().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            environ.entrySet().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            environ = new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            environ.values().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            checkSizes(environ, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        // Check that various map invariants hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        checkMapSanity(new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        checkMapSanity(System.getenv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        checkMapEquality(new ProcessBuilder().environment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                         new ProcessBuilder().environment());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        // Check effects on external "env" command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            Set<String> env1 = new HashSet<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                (Arrays.asList(nativeEnv((String[])null).split("\n")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            pb.environment().put("QwErTyUiOp","AsDfGhJk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            Set<String> env2 = new HashSet<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                (Arrays.asList(nativeEnv(pb).split("\n")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            check(env2.size() == env1.size() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            env1.add("QwErTyUiOp=AsDfGhJk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            check(env1.equals(env2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        // Test Runtime.exec(...envp...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        // Check for sort order of environment variables on Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                            "+=+", "_=_", "~=~"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            String output = nativeEnv(envp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n_=_\n~=~\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            // On Windows, Java must keep the environment sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            // Order is random on Unix, so this test does the sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            if (! Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                output = sortByLinesWindowsly(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            equal(output, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        // System.getenv() must be consistent with System.getenv(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            for (Map.Entry<String,String> e : getenv().entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                equal(getenv(e.getKey()), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        // Fiddle with working directory in child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            String canonicalUserDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                new File(System.getProperty("user.dir")).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            String[] sdirs = new String[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                {".", "..", "/", "/bin",
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1471
                 "C:", "c:", "C:/", "c:\\", "\\", "\\bin",
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1472
                 "c:\\windows  ", "c:\\Program Files", "c:\\Program Files\\" };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            for (String sdir : sdirs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                File dir = new File(sdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                if (! (dir.isDirectory() && dir.exists()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                out.println("Testing directory " + dir);
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1478
                //dir = new File(dir.getCanonicalPath());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                equal(pwdInChild(pb), canonicalUserDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                pb.directory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                equal(pb.directory(), dir);
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1486
                equal(pwdInChild(pb), dir.getCanonicalPath());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                pb.directory(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                equal(pb.directory(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                equal(pwdInChild(pb), canonicalUserDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                pb.directory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        //----------------------------------------------------------------
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1497
        // Working directory with Unicode in child
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1498
        //----------------------------------------------------------------
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1499
        try {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1500
            if (UnicodeOS.is()) {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1501
                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
  1502
                                    "ProcessBuilderDir\u4e00\u4e02");
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1503
                try {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1504
                    if (!dir.exists())
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1505
                        dir.mkdir();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1506
                    out.println("Testing Unicode directory:" + dir);
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1507
                    ProcessBuilder pb = new ProcessBuilder();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1508
                    pb.directory(dir);
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1509
                    equal(pwdInChild(pb), dir.getCanonicalPath());
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1510
                } finally {
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1511
                    if (dir.exists())
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1512
                        dir.delete();
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1513
                }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1514
            }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1515
        } catch (Throwable t) { unexpected(t); }
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1516
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 3840
diff changeset
  1517
        //----------------------------------------------------------------
3840
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1518
        // OOME in child allocating maximally sized array
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1519
        // Test for hotspot/jvmti bug 6850957
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1520
        //----------------------------------------------------------------
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1521
        try {
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1522
            List<String> list = new ArrayList<String>(javaChildArgs);
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1523
            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
  1524
                                      javaExe));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1525
            list.add("ArrayOOME");
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1526
            ProcessResults r = run(new ProcessBuilder(list));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1527
            check(r.out().contains("java.lang.OutOfMemoryError:"));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1528
            check(r.out().contains(javaExe));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1529
            check(r.err().contains(System.getProperty("java.version")));
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1530
            equal(r.exitValue(), 1);
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1531
        } catch (Throwable t) { unexpected(t); }
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1532
37fca95e51fd 6850958: Honor -XX:OnOutOfMemoryError when array size exceeds VM limit
martin
parents: 2946
diff changeset
  1533
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        // Windows has tricky semi-case-insensitive semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        if (Windows.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                out.println("Running case insensitve variable tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                for (String[] namePair :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                         new String[][]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    { new String[]{"PATH","PaTh"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                      new String[]{"home","HOME"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                      new String[]{"SYSTEMROOT","SystemRoot"}}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    check((getenv(namePair[0]) == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                           getenv(namePair[1]) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                          ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                          getenv(namePair[0]).equals(getenv(namePair[1])),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                          "Windows environment variables are not case insensitive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // Test proper Unicode child environment transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        if (UnicodeOS.is())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                pb.environment().put("\u1234","\u5678");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                pb.environment().remove("PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                equal(getenvInChild1234(pb), "\u5678");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        // Test Runtime.exec(...envp...) with envstrings with initial `='
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            childArgs.add("System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            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
  1571
            String[] envp;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1572
            String[] envpWin = {"=ExitValue=3", "=C:=\\", "SystemRoot="+systemRoot};
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1573
            String[] envpOth = {"=ExitValue=3", "=C:=\\"};
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1574
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1575
                envp = envpWin;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1576
            } else {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1577
                envp = envpOth;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1578
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            Process p = Runtime.getRuntime().exec(cmdp, envp);
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1580
            String expected = Windows.is() ? "=C:=\\,SystemRoot="+systemRoot+",=ExitValue=3," : "=C:=\\,";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            equal(commandOutput(p), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            if (Windows.is()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                pb.environment().clear();
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1585
                pb.environment().put("SystemRoot", systemRoot);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                pb.environment().put("=ExitValue", "3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                pb.environment().put("=C:", "\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                equal(commandOutput(pb), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        // Test Runtime.exec(...envp...) with envstrings without any `='
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            String[] cmdp = {"echo"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            String[] envp = {"Hello", "World"}; // Yuck!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            Process p = Runtime.getRuntime().exec(cmdp, envp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            equal(commandOutput(p), "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        // Test Runtime.exec(...envp...) with envstrings containing NULs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            childArgs.add("System.getenv()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            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
  1609
            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
  1610
                             "FO\u0000=B\u0000R"};
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1611
            String[] envpOth = {"LC_ALL=C\u0000\u0000", // Yuck!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                             "FO\u0000=B\u0000R"};
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1613
            String[] envp;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1614
            if (Windows.is()) {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1615
                envp = envpWin;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1616
            } else {
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1617
                envp = envpOth;
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1618
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            Process p = Runtime.getRuntime().exec(cmdp, envp);
8561
ca8d6ccdd9dc 7018606: (process) test/java/lang/ProcessBuilder/Basic.java failing intermittently (win)
michaelm
parents: 8540
diff changeset
  1620
            check(commandOutput(p).equals(Windows.is() ? "SystemRoot="+systemRoot+",LC_ALL=C," : "LC_ALL=C,"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                  "Incorrect handling of envstrings containing NULs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        // Test the redirectErrorStream property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            ProcessBuilder pb = new ProcessBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            equal(pb.redirectErrorStream(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            equal(pb.redirectErrorStream(true), pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            equal(pb.redirectErrorStream(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            equal(pb.redirectErrorStream(false), pb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            equal(pb.redirectErrorStream(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            childArgs.add("OutErr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            {
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1641
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                equal(r.out(), "outout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                equal(r.err(), "errerr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                pb.redirectErrorStream(true);
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1647
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                equal(r.out(), "outerrouterr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
2946
f95752c3204a 6850720: (process) Use clone(CLONE_VM), not fork, on Linux to avoid swap exhaustion
martin
parents: 715
diff changeset
  1653
        if (Unix.is()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            // We can find true and false when PATH is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                childArgs.add("null PATH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                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
  1662
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                equal(r.out(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                equal(r.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            // PATH search algorithm on Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                childArgs.add("PATH search algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                ProcessBuilder pb = new ProcessBuilder(childArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                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
  1676
                ProcessResults r = run(pb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                equal(r.out(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                equal(r.err(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                equal(r.exitValue(), True.exitValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            // Parent's, not child's PATH is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                new File("suBdiR").mkdirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                copy("/bin/true", "suBdiR/unliKely");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                final ProcessBuilder pb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    new ProcessBuilder(new String[]{"unliKely"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                pb.environment().put("PATH", "suBdiR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                THROWS(IOException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                       new Fun() {void f() throws Throwable {pb.start();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            } catch (Throwable t) { unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                new File("suBdiR/unliKely").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                new File("suBdiR").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        // Attempt to start bogus program ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            new ProcessBuilder("").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                ! matches(m, "No such file or directory"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        // Check that attempt to execute program name with funny
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        // characters throws an exception containing those characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        for (String programName : new String[] {"\u00f0", "\u01f0"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                new ProcessBuilder(programName).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                Pattern p = Pattern.compile(programName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                if (! matches(m, programName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    || (EnglishUnix.is()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                        && ! matches(m, "No such file or directory")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        // Attempt to start process in nonexistent directory fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            new ProcessBuilder("echo")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                .directory(new File("UnLiKeLY"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                .start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            if (! matches(m, "in directory")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                || (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    ! matches(m, "No such file or directory")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        //----------------------------------------------------------------
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1747
        // Attempt to write 4095 bytes to the pipe buffer without a
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1748
        // reader to drain it would deadlock, if not for the fact that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        // interprocess pipe buffers are at least 4096 bytes.
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1750
        //
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1751
        // Also, check that available reports all the bytes expected
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1752
        // 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
  1753
        // things.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            List<String> childArgs = new ArrayList<String>(javaChildArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            childArgs.add("print4095");
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1758
            final int SIZE = 4095;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1759
            final Process p = new ProcessBuilder(childArgs).start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1760
            print4095(p.getOutputStream(), (byte) '!'); // Might hang!
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1761
            p.waitFor();                                // Might hang!
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1762
            equal(SIZE, p.getInputStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1763
            equal(SIZE, p.getErrorStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1764
            THROWS(IOException.class,
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1765
                   new Fun(){void f() throws IOException {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1766
                       p.getOutputStream().write((byte) '!');
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1767
                       p.getOutputStream().flush();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1768
                       }});
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1769
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1770
            final byte[] bytes = new byte[SIZE + 1];
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1771
            equal(SIZE, p.getInputStream().read(bytes));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1772
            for (int i = 0; i < SIZE; i++)
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1773
                equal((byte) '!', bytes[i]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1774
            equal((byte) 0, bytes[SIZE]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1775
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1776
            equal(SIZE, p.getErrorStream().read(bytes));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1777
            for (int i = 0; i < SIZE; i++)
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1778
                equal((byte) 'E', bytes[i]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1779
            equal((byte) 0, bytes[SIZE]);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1780
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1781
            equal(0, p.getInputStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1782
            equal(0, p.getErrorStream().available());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1783
            equal(-1, p.getErrorStream().read());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1784
            equal(-1, p.getInputStream().read());
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1785
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            equal(p.exitValue(), 5);
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1787
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
  1788
            p.getInputStream().close();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
  1789
            p.getErrorStream().close();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
  1790
            p.getOutputStream().close();
5786
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1791
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1792
            InputStream[] streams = { p.getInputStream(), p.getErrorStream() };
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1793
            for (final InputStream in : streams) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1794
                Fun[] ops = {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1795
                    new Fun(){void f() throws IOException {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1796
                        in.read(); }},
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1797
                    new Fun(){void f() throws IOException {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1798
                        in.read(bytes); }},
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1799
                    new Fun(){void f() throws IOException {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1800
                        in.available(); }}
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1801
                };
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1802
                for (Fun op : ops) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1803
                    try {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1804
                        op.f();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1805
                        fail();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1806
                    } catch (IOException expected) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1807
                        check(expected.getMessage()
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1808
                              .matches("[Ss]tream [Cc]losed"));
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1809
                    }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1810
                }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1811
            }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1812
        } catch (Throwable t) { unexpected(t); }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1813
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1814
        //----------------------------------------------------------------
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1815
        // Check that reads which are pending when Process.destroy is
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1816
        // called, get EOF, not IOException("Stream closed").
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1817
        //----------------------------------------------------------------
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1818
        try {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1819
            final int cases = 4;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1820
            for (int i = 0; i < cases; i++) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1821
                final int action = i;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1822
                List<String> childArgs = new ArrayList<String>(javaChildArgs);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1823
                childArgs.add("sleep");
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1824
                final byte[] bytes = new byte[10];
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1825
                final Process p = new ProcessBuilder(childArgs).start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1826
                final CountDownLatch latch = new CountDownLatch(1);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1827
                final Thread thread = new Thread() {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1828
                    public void run() {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1829
                        try {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1830
                            latch.countDown();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1831
                            int r;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1832
                            switch (action) {
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1833
                            case 0: r = p.getInputStream().read(); break;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1834
                            case 1: r = p.getErrorStream().read(); break;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1835
                            case 2: r = p.getInputStream().read(bytes); break;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1836
                            case 3: r = p.getErrorStream().read(bytes); break;
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1837
                            default: throw new Error();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1838
                            }
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1839
                            equal(-1, r);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1840
                        } catch (Throwable t) { unexpected(t); }}};
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1841
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1842
                thread.start();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1843
                latch.await();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1844
                Thread.sleep(10);
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1845
                p.destroy();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1846
                thread.join();
f60ef38202e7 6944584: Improvements to subprocess handling on Unix
martin
parents: 5627
diff changeset
  1847
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        //----------------------------------------------------------------
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1851
        // 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
  1852
        // 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
  1853
        // descriptors to be closed.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1854
        //----------------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1855
        try {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1856
            if (Unix.is()
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1857
                && new File("/bin/bash").exists()
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1858
                && new File("/bin/sleep").exists()) {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1859
                final String[] cmd = { "/bin/bash", "-c", "(/bin/sleep 6666)" };
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1860
                final ProcessBuilder pb = new ProcessBuilder(cmd);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1861
                final Process p = pb.start();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1862
                final InputStream stdout = p.getInputStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1863
                final InputStream stderr = p.getErrorStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1864
                final OutputStream stdin = p.getOutputStream();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1865
                final Thread reader = new Thread() {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1866
                    public void run() {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1867
                        try { stdout.read(); }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1868
                        catch (IOException e) {
6849
02dc13de9152 6989903: (process) test/java/lang/ProcessBuilder/Basic.java failing with "Bad file number" (sol)
alanb
parents: 6669
diff changeset
  1869
                            // 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
  1870
                            // asynchronously closed.
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1871
                            // e.printStackTrace();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1872
                            if (EnglishUnix.is() &&
6849
02dc13de9152 6989903: (process) test/java/lang/ProcessBuilder/Basic.java failing with "Bad file number" (sol)
alanb
parents: 6669
diff changeset
  1873
                                ! (e.getMessage().matches(".*Bad file.*")))
6669
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1874
                                unexpected(e);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1875
                        }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1876
                        catch (Throwable t) { unexpected(t); }}};
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1877
                reader.setDaemon(true);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1878
                reader.start();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1879
                Thread.sleep(100);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1880
                p.destroy();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1881
                // Subprocess is now dead, but file descriptors remain open.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1882
                check(p.waitFor() != 0);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1883
                check(p.exitValue() != 0);
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1884
                stdout.close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1885
                stderr.close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1886
                stdin.close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1887
                //----------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1888
                // 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
  1889
                // 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
  1890
                //----------------------------------------------------------
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1891
                if (Boolean.getBoolean("wakeupJeff!")) {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1892
                    System.out.println("wakeupJeff!");
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1893
                    // Initialize signal handler for INTERRUPT_SIGNAL.
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1894
                    new FileInputStream("/bin/sleep").getChannel().close();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1895
                    // 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
  1896
                    String[] wakeupJeff = {
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1897
                        "/bin/bash", "-c",
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1898
                        "/bin/ps --noheaders -Lfp $PPID | " +
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1899
                        "/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
  1900
                        // 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
  1901
                        "/usr/bin/xargs kill -62"
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1902
                    };
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1903
                    new ProcessBuilder(wakeupJeff).start().waitFor();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1904
                    // 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
  1905
                    reader.join();
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1906
                }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1907
            }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1908
        } catch (Throwable t) { unexpected(t); }
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1909
8f8d4d5768ae 6981138: (process) Process.waitFor() may hang if subprocess has live descendants (lnx)
martin
parents: 5786
diff changeset
  1910
        //----------------------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        // Attempt to start process with insufficient permissions fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            new File("emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            new FileOutputStream("emptyCommand").close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            new File("emptyCommand").setExecutable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            new ProcessBuilder("./emptyCommand").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            fail("Expected IOException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            new File("./emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            String m = e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            if (EnglishUnix.is() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                ! matches(m, "Permission denied"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        new File("emptyCommand").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        // Check for correct security permission behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        final Policy policy = new Policy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        Policy.setPolicy(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            // No permissions required to CREATE a ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            new ProcessBuilder("env").directory(null).directory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            new ProcessBuilder("env").directory(new File("dir")).directory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            new ProcessBuilder("env").command("??").command();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        THROWS(SecurityException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                System.getenv("foo");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                System.getenv();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                new ProcessBuilder("echo").start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                Runtime.getRuntime().exec("echo");}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                policy.setPermissions(new RuntimePermission("getenv.bar"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                System.getenv("foo");}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            policy.setPermissions(new RuntimePermission("getenv.foo"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            System.getenv("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            policy.setPermissions(new RuntimePermission("getenv.*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            System.getenv("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            System.getenv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            new ProcessBuilder().environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        final Permission execPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            = new FilePermission("<<ALL FILES>>", "execute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        THROWS(SecurityException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                // environment permission by itself insufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                policy.setPermissions(new RuntimePermission("getenv.*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                ProcessBuilder pb = new ProcessBuilder("env");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                pb.environment().put("foo","bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                pb.start();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            new Fun() { void f() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                 // exec permission by itself insufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                 policy.setPermissions(execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                 ProcessBuilder pb = new ProcessBuilder("env");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                 pb.environment().put("foo","bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                 pb.start();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            // Both permissions? OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            policy.setPermissions(new RuntimePermission("getenv.*"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                                  execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            ProcessBuilder pb = new ProcessBuilder("env");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            pb.environment().put("foo","bar");
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1995
            Process p = pb.start();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  1996
            closeStreams(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            // Don't need environment permission unless READING environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            policy.setPermissions(execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            Runtime.getRuntime().exec("env", new String[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            // Don't need environment permission unless READING environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            policy.setPermissions(execPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            new ProcessBuilder("env").start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        } catch (IOException e) { // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        // Restore "normal" state without a security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        policy.setPermissions(new RuntimePermission("setSecurityManager"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        System.setSecurityManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2020
    static void closeStreams(Process p) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2021
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2022
            p.getOutputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2023
            p.getInputStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2024
            p.getErrorStream().close();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2025
        } catch (Throwable t) { unexpected(t); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2026
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2027
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    // A Policy class designed to make permissions fiddling very easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    private static class Policy extends java.security.Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        public void setPermissions(Permission...permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            for (Permission permission : permissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        public Policy() { setPermissions(/* Nothing */); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        public PermissionCollection getPermissions(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        public PermissionCollection getPermissions(ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        public boolean implies(ProtectionDomain pd, Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            return perms.implies(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        public void refresh() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    private static class StreamAccumulator extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        private final InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        private final StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        private Throwable throwable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        public String result () throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            if (throwable != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                throw throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        StreamAccumulator (InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                Reader r = new InputStreamReader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                char[] buf = new char[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                while ((n = r.read(buf)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                    sb.append(buf,0,n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                throwable = t;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2082
            } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2083
                try { is.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2084
                catch (Throwable t) { throwable = t; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2089
    static ProcessResults run(ProcessBuilder pb) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2090
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2091
            return run(pb.start());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2092
        } catch (Throwable t) { unexpected(t); return null; }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2093
    }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
  2094
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    private static ProcessResults run(Process p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        Throwable throwable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        int exitValue = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        String out = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        String err = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        StreamAccumulator outAccumulator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            new StreamAccumulator(p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        StreamAccumulator errAccumulator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            new StreamAccumulator(p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            outAccumulator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            errAccumulator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            exitValue = p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            outAccumulator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            errAccumulator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            out = outAccumulator.result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            err = errAccumulator.result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            throwable = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        return new ProcessResults(out, err, exitValue, throwable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    // Results of a command
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    private static class ProcessResults {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        private final String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        private final String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        private final int exitValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        private final Throwable throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        public ProcessResults(String out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                              String err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                              int exitValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                              Throwable throwable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            this.err = err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            this.exitValue = exitValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            this.throwable = throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        public String out()          { return out; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        public String err()          { return err; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        public int exitValue()       { return exitValue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        public Throwable throwable() { return throwable; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            sb.append("<STDOUT>\n" + out() + "</STDOUT>\n")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                .append("<STDERR>\n" + err() + "</STDERR>\n")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                .append("exitValue = " + exitValue + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            if (throwable != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                sb.append(throwable.getStackTrace());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        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
  2170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    private static abstract class Fun {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
}