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