test/java/lang/ProcessBuilder/Basic.java

Print this page




 281                 ! matches(m, "Permission denied"))
 282                 unexpected(e);
 283         } catch (Throwable t) { unexpected(t); }
 284     }
 285 
 286     public static class JavaChild {
 287         public static void main(String args[]) throws Throwable {
 288             String action = args[0];
 289             if (action.equals("sleep")) {
 290                 Thread.sleep(10 * 60 * 1000L);
 291             } else if (action.equals("testIO")) {
 292                 String expected = "standard input";
 293                 char[] buf = new char[expected.length()+1];
 294                 int n = new InputStreamReader(System.in).read(buf,0,buf.length);
 295                 if (n != expected.length())
 296                     System.exit(5);
 297                 if (! new String(buf,0,n).equals(expected))
 298                     System.exit(5);
 299                 System.err.print("standard error");
 300                 System.out.print("standard output");
 301             } else if (action.equals("testInheritIO")) {

 302                 List<String> childArgs = new ArrayList<String>(javaChildArgs);
 303                 childArgs.add("testIO");
 304                 ProcessBuilder pb = new ProcessBuilder(childArgs);

 305                 pb.inheritIO();





 306                 ProcessResults r = run(pb);
 307                 if (! r.out().equals(""))
 308                     System.exit(7);
 309                 if (! r.err().equals(""))
 310                     System.exit(8);
 311                 if (r.exitValue() != 0)
 312                     System.exit(9);
 313             } else if (action.equals("System.getenv(String)")) {
 314                 String val = System.getenv(args[1]);
 315                 printUTF8(val == null ? "null" : val);
 316             } else if (action.equals("System.getenv(\\u1234)")) {
 317                 String val = System.getenv("\u1234");
 318                 printUTF8(val == null ? "null" : val);
 319             } else if (action.equals("System.getenv()")) {
 320                 printUTF8(getenvAsString(System.getenv()));
 321             } else if (action.equals("ArrayOOME")) {
 322                 Object dummy;
 323                 switch(new Random().nextInt(3)) {
 324                 case 0: dummy = new Integer[Integer.MAX_VALUE]; break;
 325                 case 1: dummy = new double[Integer.MAX_VALUE];  break;


1002             pb.redirectError(appender);
1003             ProcessResults r = run(pb);
1004             equal(r.exitValue(), 0);
1005             equal(fileContents(ofile),
1006                   "ofile-contents" +
1007                   "standard error" +
1008                   "standard output");
1009             equal(fileContents(efile), "efile-contents");
1010             equal(r.out(), "");
1011             equal(r.err(), "");
1012             ifile.delete();
1013             ofile.delete();
1014             efile.delete();
1015         }
1016 
1017         //----------------------------------------------------------------
1018         // Testing INHERIT is harder.
1019         // Note that this requires __FOUR__ nested JVMs involved in one test,
1020         // if you count the harness JVM.
1021         //----------------------------------------------------------------
1022         {
1023             redirectIO(pb, PIPE, PIPE, PIPE);
1024             List<String> command = pb.command();
1025             command.set(command.size() - 1, "testInheritIO");
1026             Process p = pb.start();
1027             new PrintStream(p.getOutputStream()).print("standard input");
1028             p.getOutputStream().close();
1029             ProcessResults r = run(p);
1030             equal(r.exitValue(), 0);
1031             equal(r.out(), "standard output");
1032             equal(r.err(), "standard error");
1033         }
1034 
1035         //----------------------------------------------------------------
1036         // Test security implications of I/O redirection
1037         //----------------------------------------------------------------
1038 
1039         // Read access to current directory is always granted;
1040         // So create a tmpfile for input instead.
1041         final File tmpFile = File.createTempFile("Basic", "tmp");
1042         setFileContents(tmpFile, "standard input");
1043 
1044         final Policy policy = new Policy();
1045         Policy.setPolicy(policy);




 281                 ! matches(m, "Permission denied"))
 282                 unexpected(e);
 283         } catch (Throwable t) { unexpected(t); }
 284     }
 285 
 286     public static class JavaChild {
 287         public static void main(String args[]) throws Throwable {
 288             String action = args[0];
 289             if (action.equals("sleep")) {
 290                 Thread.sleep(10 * 60 * 1000L);
 291             } else if (action.equals("testIO")) {
 292                 String expected = "standard input";
 293                 char[] buf = new char[expected.length()+1];
 294                 int n = new InputStreamReader(System.in).read(buf,0,buf.length);
 295                 if (n != expected.length())
 296                     System.exit(5);
 297                 if (! new String(buf,0,n).equals(expected))
 298                     System.exit(5);
 299                 System.err.print("standard error");
 300                 System.out.print("standard output");
 301             } else if (action.equals("testInheritIO")
 302                     || action.equals("testRedirectInherit")) {
 303                 List<String> childArgs = new ArrayList<String>(javaChildArgs);
 304                 childArgs.add("testIO");
 305                 ProcessBuilder pb = new ProcessBuilder(childArgs);
 306                 if (action.equals("testInheritIO")) {
 307                     pb.inheritIO();
 308                 } else {
 309                     pb.redirectInput(Redirect.INHERIT)
 310                       .redirectOutput(Redirect.INHERIT)
 311                       .redirectError(Redirect.INHERIT);
 312                 }
 313                 ProcessResults r = run(pb);
 314                 if (! r.out().equals(""))
 315                     System.exit(7);
 316                 if (! r.err().equals(""))
 317                     System.exit(8);
 318                 if (r.exitValue() != 0)
 319                     System.exit(9);
 320             } else if (action.equals("System.getenv(String)")) {
 321                 String val = System.getenv(args[1]);
 322                 printUTF8(val == null ? "null" : val);
 323             } else if (action.equals("System.getenv(\\u1234)")) {
 324                 String val = System.getenv("\u1234");
 325                 printUTF8(val == null ? "null" : val);
 326             } else if (action.equals("System.getenv()")) {
 327                 printUTF8(getenvAsString(System.getenv()));
 328             } else if (action.equals("ArrayOOME")) {
 329                 Object dummy;
 330                 switch(new Random().nextInt(3)) {
 331                 case 0: dummy = new Integer[Integer.MAX_VALUE]; break;
 332                 case 1: dummy = new double[Integer.MAX_VALUE];  break;


1009             pb.redirectError(appender);
1010             ProcessResults r = run(pb);
1011             equal(r.exitValue(), 0);
1012             equal(fileContents(ofile),
1013                   "ofile-contents" +
1014                   "standard error" +
1015                   "standard output");
1016             equal(fileContents(efile), "efile-contents");
1017             equal(r.out(), "");
1018             equal(r.err(), "");
1019             ifile.delete();
1020             ofile.delete();
1021             efile.delete();
1022         }
1023 
1024         //----------------------------------------------------------------
1025         // Testing INHERIT is harder.
1026         // Note that this requires __FOUR__ nested JVMs involved in one test,
1027         // if you count the harness JVM.
1028         //----------------------------------------------------------------
1029         for (String testName : new String[] { "testInheritIO", "testRedirectInherit" } ) {
1030             redirectIO(pb, PIPE, PIPE, PIPE);
1031             List<String> command = pb.command();
1032             command.set(command.size() - 1, testName);
1033             Process p = pb.start();
1034             new PrintStream(p.getOutputStream()).print("standard input");
1035             p.getOutputStream().close();
1036             ProcessResults r = run(p);
1037             equal(r.exitValue(), 0);
1038             equal(r.out(), "standard output");
1039             equal(r.err(), "standard error");
1040         }
1041 
1042         //----------------------------------------------------------------
1043         // Test security implications of I/O redirection
1044         //----------------------------------------------------------------
1045 
1046         // Read access to current directory is always granted;
1047         // So create a tmpfile for input instead.
1048         final File tmpFile = File.createTempFile("Basic", "tmp");
1049         setFileContents(tmpFile, "standard input");
1050 
1051         final Policy policy = new Policy();
1052         Policy.setPolicy(policy);