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