test/java/lang/ProcessBuilder/Basic.java

Print this page




  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;
  39 import java.util.*;
  40 import java.util.concurrent.CountDownLatch;

  41 import java.security.*;
  42 import java.util.regex.Pattern;
  43 import java.util.regex.Matcher;
  44 import static java.lang.System.getenv;
  45 import static java.lang.System.out;
  46 import static java.lang.Boolean.TRUE;
  47 import static java.util.AbstractMap.SimpleImmutableEntry;
  48 
  49 public class Basic {
  50 
  51     /* used for Windows only */
  52     static final String systemRoot = System.getenv("SystemRoot");
  53 
  54     /* used for Mac OS X only */
  55     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  56 
  57     private static String commandOutput(Reader r) throws Throwable {
  58         StringBuilder sb = new StringBuilder();
  59         int c;
  60         while ((c = r.read()) > 0)


2073         } catch (Throwable t) { unexpected(t); }
2074 
2075         try {
2076             // Don't need environment permission unless READING environment
2077             policy.setPermissions(execPermission);
2078             Runtime.getRuntime().exec("env", new String[]{});
2079         } catch (IOException e) { // OK
2080         } catch (Throwable t) { unexpected(t); }
2081 
2082         try {
2083             // Don't need environment permission unless READING environment
2084             policy.setPermissions(execPermission);
2085             new ProcessBuilder("env").start();
2086         } catch (IOException e) { // OK
2087         } catch (Throwable t) { unexpected(t); }
2088 
2089         // Restore "normal" state without a security manager
2090         policy.setPermissions(new RuntimePermission("setSecurityManager"));
2091         System.setSecurityManager(null);
2092 











2093     }




2094 
































































2095     static void closeStreams(Process p) {
2096         try {
2097             p.getOutputStream().close();
2098             p.getInputStream().close();
2099             p.getErrorStream().close();
2100         } catch (Throwable t) { unexpected(t); }
2101     }
2102 
2103     //----------------------------------------------------------------
2104     // A Policy class designed to make permissions fiddling very easy.
2105     //----------------------------------------------------------------
2106     private static class Policy extends java.security.Policy {
2107         private Permissions perms;
2108 
2109         public void setPermissions(Permission...permissions) {
2110             perms = new Permissions();
2111             for (Permission permission : permissions)
2112                 perms.add(permission);
2113         }
2114 




  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;
  39 import java.util.*;
  40 import java.util.concurrent.CountDownLatch;
  41 import java.util.concurrent.TimeUnit;
  42 import java.security.*;
  43 import java.util.regex.Pattern;
  44 import java.util.regex.Matcher;
  45 import static java.lang.System.getenv;
  46 import static java.lang.System.out;
  47 import static java.lang.Boolean.TRUE;
  48 import static java.util.AbstractMap.SimpleImmutableEntry;
  49 
  50 public class Basic {
  51 
  52     /* used for Windows only */
  53     static final String systemRoot = System.getenv("SystemRoot");
  54 
  55     /* used for Mac OS X only */
  56     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  57 
  58     private static String commandOutput(Reader r) throws Throwable {
  59         StringBuilder sb = new StringBuilder();
  60         int c;
  61         while ((c = r.read()) > 0)


2074         } catch (Throwable t) { unexpected(t); }
2075 
2076         try {
2077             // Don't need environment permission unless READING environment
2078             policy.setPermissions(execPermission);
2079             Runtime.getRuntime().exec("env", new String[]{});
2080         } catch (IOException e) { // OK
2081         } catch (Throwable t) { unexpected(t); }
2082 
2083         try {
2084             // Don't need environment permission unless READING environment
2085             policy.setPermissions(execPermission);
2086             new ProcessBuilder("env").start();
2087         } catch (IOException e) { // OK
2088         } catch (Throwable t) { unexpected(t); }
2089 
2090         // Restore "normal" state without a security manager
2091         policy.setPermissions(new RuntimePermission("setSecurityManager"));
2092         System.setSecurityManager(null);
2093 
2094         //----------------------------------------------------------------
2095         // Check that Process.isAlive() &
2096         // Process.waitFor(0, TimeUnit.MILLISECONDS) work as expected.
2097         //----------------------------------------------------------------
2098         try {
2099             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2100             childArgs.add("sleep");
2101             final Process p = new ProcessBuilder(childArgs).start();
2102             long start = System.nanoTime();
2103             if (!p.isAlive() || p.waitFor(0, TimeUnit.MILLISECONDS)) {
2104                 fail("Test failed: Process exited prematurely");
2105             }
2106             long end = System.nanoTime();
2107             // give waitFor(timeout) a wide berth (100ms)
2108             if ((end - start) > 100000000)
2109                 fail("Test failed: waitFor took too long");
2110 
2111             p.destroy();
2112             p.waitFor();
2113 
2114             if (p.isAlive() ||
2115                 !p.waitFor(0, TimeUnit.MILLISECONDS))
2116             {
2117                 fail("Test failed: Process still alive - please terminate " +
2118                     p.toString() + " manually");
2119             }
2120         } catch (Throwable t) { unexpected(t); }
2121 
2122         //----------------------------------------------------------------
2123         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2124         // works as expected.
2125         //----------------------------------------------------------------
2126         try {
2127             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2128             childArgs.add("sleep");
2129             final Process p = new ProcessBuilder(childArgs).start();
2130             long start = System.nanoTime();
2131 
2132             p.waitFor(1000, TimeUnit.MILLISECONDS);
2133 
2134             long end = System.nanoTime();
2135             if ((end - start) < 500)
2136                 fail("Test failed: waitFor didn't take long enough");
2137 
2138             p.destroy();
2139 
2140             start = System.nanoTime();
2141             p.waitFor(1000, TimeUnit.MILLISECONDS);
2142             end = System.nanoTime();
2143             if ((end - start) > 100000000)
2144                 fail("Test failed: waitFor took too long on a dead process.");
2145         } catch (Throwable t) { unexpected(t); }
2146 
2147         //----------------------------------------------------------------
2148         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2149         // interrupt works as expected.
2150         //----------------------------------------------------------------
2151         try {
2152             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2153             childArgs.add("sleep");
2154             final Process p = new ProcessBuilder(childArgs).start();
2155             final long start = System.nanoTime();
2156 
2157             final Thread thread = new Thread() {
2158                 public void run() {
2159                     try {
2160                         try {
2161                             p.waitFor(10000, TimeUnit.MILLISECONDS);
2162                         } catch (InterruptedException e) {
2163                             return;
2164                         }
2165                         fail("waitFor() wasn't interrupted");
2166                     } catch (Throwable t) { unexpected(t); }}};
2167 
2168             thread.start();
2169             Thread.sleep(1000);
2170             thread.interrupt();
2171             p.destroy();
2172         } catch (Throwable t) { unexpected(t); }
2173     }
2174 
2175     static void closeStreams(Process p) {
2176         try {
2177             p.getOutputStream().close();
2178             p.getInputStream().close();
2179             p.getErrorStream().close();
2180         } catch (Throwable t) { unexpected(t); }
2181     }
2182 
2183     //----------------------------------------------------------------
2184     // A Policy class designed to make permissions fiddling very easy.
2185     //----------------------------------------------------------------
2186     private static class Policy extends java.security.Policy {
2187         private Permissions perms;
2188 
2189         public void setPermissions(Permission...permissions) {
2190             perms = new Permissions();
2191             for (Permission permission : permissions)
2192                 perms.add(permission);
2193         }
2194