4944N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689 0N/A * 5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313 1651N/A * 6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958 0N/A * @summary Basic tests for Process and Environment Variable code 0N/A * @author Martin Buchholz 3664N/A /* used for Windows only */ 4666N/A /* used for Mac OS X only */ 0N/A // compare windows-style, by canonicalizing to upper case, 0N/A // not lower case as String.compareToIgnoreCase does 0N/A "-----------------------------------------------------";
0N/A fail(
"Sorted string contents differ");
0N/A // Ignore magic environment variables added by the launcher 0N/A // You might think the system streams would be 0N/A // buffered, and in fact they are implemented using 0N/A // BufferedOutputStream, but each and every print 0N/A // causes immediate operating system I/O. 0N/A // Not on PATH at all; directories don't exist 0N/A fail(
"Expected IOException not thrown");
0N/A // Not on PATH at all; directories exist 0N/A fail(
"Expected IOException not thrown");
0N/A // Can't execute a directory -- permission denied 0N/A // Report EACCES errno 0N/A // continue searching if EACCES 1270N/A // Check empty PATH component means current directory. 1270N/A // While we're here, let's test different kinds of 1270N/A // Unix executables, and PATH vs explicit searching. 1270N/A // Interpreter scripts with #! 1270N/A // Traditional shell scripts without #! 1270N/A // Test Unix interpreter scripts 1270N/A // Test traditional shell scripts without #! 0N/A // If prog found on both parent and child's PATH, 0N/A // parent's is used. 0N/A else throw new Error(
"JavaChild invocation error");
0N/A "Basic$JavaChild"});
0N/A // If round trip conversion works, should be able to set env vars 0N/A // correctly in child. 0N/A public static boolean is() {
return is; }
0N/A private static final boolean is =
0N/A public static boolean is() {
return is; }
0N/A private static final boolean is =
0N/A public static boolean is() {
return is; }
0N/A private static final boolean is =
0N/A // MacOS X would probably also qualify 0N/A // /bin/false returns an *unspecified* non-zero number. 0N/A /** Returns true if we can expect English OS error strings */ 4666N/A /* Only used for Mac OS X -- 4666N/A * Mac OS X adds the variable __CF_USER_TEXT_ENCODING to an empty 4666N/A * environment. The environment variable JAVA_MAIN_CLASS_<pid> should also 4666N/A * Check for both by removing them both from the list of env variables. 4666N/A // Check for __CF_USER_TEXT_ENCODING 4666N/A fail(
"Environment variable __CF_USER_TEXT_ENCODING not set. " 4666N/A +
"MAC OS X should set __CF_USER_TEXT_ENCODING in " 4666N/A +
"an empty environment.");
4666N/A // Check for JAVA_MAIN_CLASS_<pid> 4666N/A +
"Should be set in Mac OS X env.");
25N/A //---------------------------------------------------------------- 25N/A // Check mutual inequality of different types of Redirect 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A // Check basic properties of different types of Redirect 25N/A //---------------------------------------------------------------- 25N/A "redirect to read from file \"ifile\"");
25N/A "redirect to write to file \"ofile\"");
25N/A "redirect to append to file \"efile\"");
25N/A //---------------------------------------------------------------- 25N/A // Check initial values of redirects 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A // Check setters and getters agree 25N/A //---------------------------------------------------------------- 25N/A // Input file does not exist 25N/A //---------------------------------------------------------------- 25N/A // Writing to non-existent files 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A // Both redirectErrorStream + redirectError 25N/A //---------------------------------------------------------------- 25N/A "standard error" +
"standard output");
25N/A //---------------------------------------------------------------- 25N/A // Appending to existing files 25N/A //---------------------------------------------------------------- 25N/A "ofile-contents" +
"standard output");
25N/A "efile-contents" +
"standard error");
25N/A //---------------------------------------------------------------- 25N/A // Replacing existing files 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A // Appending twice to the same file? 25N/A //---------------------------------------------------------------- 25N/A "standard output");
25N/A //---------------------------------------------------------------- 25N/A // Testing INHERIT is harder. 25N/A // Note that this requires __FOUR__ nested JVMs involved in one test, 25N/A // if you count the harness JVM. 25N/A //---------------------------------------------------------------- 25N/A //---------------------------------------------------------------- 25N/A // Test security implications of I/O redirection 25N/A //---------------------------------------------------------------- 25N/A // Read access to current directory is always granted; 25N/A // So create a tmpfile for input instead. 0N/A //---------------------------------------------------------------- 0N/A // Basic tests for setting, replacing and deleting envvars 0N/A //---------------------------------------------------------------- 0N/A // Remove non-existent env var 0N/A //---------------------------------------------------------------- 0N/A // Pass Empty environment to child 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // System.getenv() is read-only. 0N/A //---------------------------------------------------------------- 0N/A fail(
"Expected UnsupportedOperationException not thrown");
0N/A //---------------------------------------------------------------- 0N/A // System.getenv() always returns the same object in our implementation. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // You can't create an env var name containing "=", 0N/A // or an env var name or value containing NUL. 0N/A //---------------------------------------------------------------- 0N/A new Fun(){
void f(){ m.
put(
"FOO\u0000",
"BAR");}},
0N/A new Fun(){
void f(){ m.
put(
"FOO",
"BAR\u0000");}});
0N/A //---------------------------------------------------------------- 0N/A // Commands must never be null. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Put in a command; get the same one back out. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Commands must contain at least one element. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Commands must not contain null elements at start() time. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Command lists are growable. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Nulls in environment updates generate NullPointerException 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Non-String types in environment updates generate ClassCastException 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Check query operations on environment maps 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Nulls in environment queries are forbidden. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Non-String types in environment queries are forbidden. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Illegal String values in environment queries are (grumble) OK 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Put in a directory; get the same one back out. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // If round-trip conversion works, check envvar pass-through to child 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // A surprisingly large number of ways to delete an environment var. 0N/A //---------------------------------------------------------------- 0N/A // Legally fabricate a ProcessEnvironment.StringEntry, 0N/A // even though it's private. 0N/A //---------------------------------------------------------------- 0N/A // A surprisingly small number of ways to add an environment var. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // A few ways to modify an environment var. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Fiddle with environment sizes 0N/A //---------------------------------------------------------------- 0N/A // Check for environment independence 0N/A //---------------------------------------------------------------- 0N/A // Check that various map invariants hold 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Check effects on external "env" command. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Test Runtime.exec(...envp...) 0N/A // Check for sort order of environment variables on Windows. 0N/A //---------------------------------------------------------------- 0N/A // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~' 0N/A // On Windows, Java must keep the environment sorted. 0N/A // Order is random on Unix, so this test does the sort. 0N/A //---------------------------------------------------------------- 4103N/A // Test Runtime.exec(...envp...) 4103N/A // and check SystemRoot gets set automatically on Windows 4103N/A //---------------------------------------------------------------- 4103N/A //---------------------------------------------------------------- 0N/A // System.getenv() must be consistent with System.getenv(String) 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Fiddle with working directory in child 0N/A //---------------------------------------------------------------- 0N/A {
".",
"..",
"/",
"/bin",
2247N/A "C:",
"c:",
"C:/",
"c:\\",
"\\",
"\\bin",
2247N/A "c:\\windows ",
"c:\\Program Files",
"c:\\Program Files\\" };
2247N/A //dir = new File(dir.getCanonicalPath()); 0N/A //---------------------------------------------------------------- 2247N/A // Working directory with Unicode in child 2247N/A //---------------------------------------------------------------- 2247N/A "ProcessBuilderDir\u4e00\u4e02");
2247N/A //---------------------------------------------------------------- 1651N/A // OOME in child allocating maximally sized array 1651N/A //---------------------------------------------------------------- 1651N/A //---------------------------------------------------------------- 0N/A // Windows has tricky semi-case-insensitive semantics 0N/A //---------------------------------------------------------------- 0N/A "Windows environment variables are not case insensitive");
0N/A //---------------------------------------------------------------- 0N/A // Test proper Unicode child environment transfer 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Test Runtime.exec(...envp...) with envstrings with initial `=' 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Test Runtime.exec(...envp...) with envstrings without any `=' 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Test Runtime.exec(...envp...) with envstrings containing NULs 0N/A //---------------------------------------------------------------- 0N/A "FO\u0000=B\u0000R"};
0N/A "Incorrect handling of envstrings containing NULs");
0N/A //---------------------------------------------------------------- 0N/A // Test the redirectErrorStream property 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // We can find true and false when PATH is null 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // PATH search algorithm on Unix 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Parent's, not child's PATH is used 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Attempt to start bogus program "" 0N/A //---------------------------------------------------------------- 0N/A fail(
"Expected IOException not thrown");
0N/A //---------------------------------------------------------------- 0N/A // Check that attempt to execute program name with funny 0N/A // characters throws an exception containing those characters. 0N/A //---------------------------------------------------------------- 0N/A fail(
"Expected IOException not thrown");
0N/A //---------------------------------------------------------------- 0N/A // Attempt to start process in nonexistent directory fails. 0N/A //---------------------------------------------------------------- 0N/A fail(
"Expected IOException not thrown");
0N/A //---------------------------------------------------------------- 2473N/A // Attempt to write 4095 bytes to the pipe buffer without a 2473N/A // reader to drain it would deadlock, if not for the fact that 0N/A // interprocess pipe buffers are at least 4096 bytes. 2473N/A // Also, check that available reports all the bytes expected 2473N/A // in the pipe buffer, and that I/O operations do the expected 0N/A //---------------------------------------------------------------- 2473N/A //---------------------------------------------------------------- 2473N/A // Check that reads which are pending when Process.destroy is 2473N/A // called, get EOF, not IOException("Stream closed"). 2473N/A //---------------------------------------------------------------- 5376N/A "java.lang.UNIXProcess$DeferredCloseInputStream"))
0N/A //---------------------------------------------------------------- 2800N/A // Check that subprocesses which create subprocesses of their 2800N/A // own do not cause parent to hang waiting for file 2800N/A // descriptors to be closed. 2800N/A //---------------------------------------------------------------- 2883N/A // Check that reader failed because stream was 2800N/A // Subprocess is now dead, but file descriptors remain open. 2800N/A //---------------------------------------------------------- 2800N/A // There remain unsolved issues with asynchronous close. 2800N/A // Here's a highly non-portable experiment to demonstrate: 2800N/A //---------------------------------------------------------- 2800N/A // Initialize signal handler for INTERRUPT_SIGNAL. 2800N/A // Send INTERRUPT_SIGNAL to every thread in this java. 2800N/A // INTERRUPT_SIGNAL == 62 on my machine du jour. 2800N/A // If wakeupJeff worked, reader probably got EBADF. 2800N/A //---------------------------------------------------------------- 0N/A // Attempt to start process with insufficient permissions fails. 0N/A //---------------------------------------------------------------- 0N/A fail(
"Expected IOException not thrown");
0N/A //---------------------------------------------------------------- 0N/A // Check for correct security permission behavior 0N/A //---------------------------------------------------------------- 0N/A // No permissions required to CREATE a ProcessBuilder 0N/A // environment permission by itself insufficient 0N/A // exec permission by itself insufficient 0N/A // Both permissions? OK. 0N/A // Don't need environment permission unless READING environment 0N/A // Don't need environment permission unless READING environment 0N/A // Restore "normal" state without a security manager 0N/A //---------------------------------------------------------------- 0N/A // A Policy class designed to make permissions fiddling very easy. 0N/A //---------------------------------------------------------------- 0N/A //---------------------------------------------------------------- 0N/A // Results of a command 0N/A //---------------------------------------------------------------- 0N/A //--------------------- Infrastructure --------------------------- 0N/A else fail(x +
" not equal to " + y);}