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                     redirectIO(pb, INHERIT, INHERIT, INHERIT);
 310                 ProcessResults r = run(pb);
 311                 if (! r.out().equals(""))
 312                     System.exit(7);
 313                 if (! r.err().equals(""))
 314                     System.exit(8);
 315                 if (r.exitValue() != 0)
 316                     System.exit(9);
 317             } else if (action.equals("System.getenv(String)")) {
 318                 String val = System.getenv(args[1]);
 319                 printUTF8(val == null ? "null" : val);
 320             } else if (action.equals("System.getenv(\\u1234)")) {
 321                 String val = System.getenv("\u1234");
 322                 printUTF8(val == null ? "null" : val);
 323             } else if (action.equals("System.getenv()")) {
 324                 printUTF8(getenvAsString(System.getenv()));
 325             } else if (action.equals("ArrayOOME")) {
 326                 Object dummy;
 327                 switch(new Random().nextInt(3)) {
 328                 case 0: dummy = new Integer[Integer.MAX_VALUE]; break;
 329                 case 1: dummy = new double[Integer.MAX_VALUE];  break;


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